Changeset 8b1e15ac in mainline for uspace/lib/drv/generic/driver.c
- Timestamp:
- 2011-02-11T22:26:36Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 68414f4a
- Parents:
- 1b367b4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/drv/generic/driver.c
r1b367b4 r8b1e15ac 59 59 60 60 /** Devices */ 61 LIST_INITIALIZE( devices);62 FIBRIL_MUTEX_INITIALIZE( devices_mutex);61 LIST_INITIALIZE(functions); 62 FIBRIL_MUTEX_INITIALIZE(functions_mutex); 63 63 64 64 /** Interrupts */ … … 209 209 } 210 210 211 static void add_to_devices_list(device_t *dev) 212 { 213 fibril_mutex_lock(&devices_mutex); 214 list_append(&dev->link, &devices); 215 fibril_mutex_unlock(&devices_mutex); 216 } 217 218 static void remove_from_devices_list(device_t *dev) 219 { 220 fibril_mutex_lock(&devices_mutex); 221 list_remove(&dev->link); 222 fibril_mutex_unlock(&devices_mutex); 223 } 224 225 static device_t *driver_get_device(link_t *devices, devman_handle_t handle) 226 { 227 device_t *dev = NULL; 228 229 fibril_mutex_lock(&devices_mutex); 230 link_t *link = devices->next; 231 232 while (link != devices) { 233 dev = list_get_instance(link, device_t, link); 234 if (dev->handle == handle) { 235 fibril_mutex_unlock(&devices_mutex); 236 return dev; 211 static void add_to_functions_list(function_t *fun) 212 { 213 fibril_mutex_lock(&functions_mutex); 214 list_append(&fun->link, &functions); 215 fibril_mutex_unlock(&functions_mutex); 216 } 217 218 static void remove_from_functions_list(function_t *fun) 219 { 220 fibril_mutex_lock(&functions_mutex); 221 list_remove(&fun->link); 222 fibril_mutex_unlock(&functions_mutex); 223 } 224 225 static function_t *driver_get_function(link_t *functions, devman_handle_t handle) 226 { 227 function_t *fun = NULL; 228 printf("driver_get_function handle=%" PRIun "\n", handle); 229 230 fibril_mutex_lock(&functions_mutex); 231 link_t *link = functions->next; 232 233 while (link != functions) { 234 fun = list_get_instance(link, function_t, link); 235 printf(" - fun handle %" PRIun "\n", fun->handle); 236 if (fun->handle == handle) { 237 fibril_mutex_unlock(&functions_mutex); 238 return fun; 237 239 } 240 238 241 link = link->next; 239 242 } 240 243 241 fibril_mutex_unlock(& devices_mutex);244 fibril_mutex_unlock(&functions_mutex); 242 245 243 246 return NULL; … … 250 253 251 254 devman_handle_t dev_handle = IPC_GET_ARG1(*icall); 252 devman_handle_t parent_ dev_handle = IPC_GET_ARG2(*icall);255 devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall); 253 256 254 257 device_t *dev = create_device(); 255 258 dev->handle = dev_handle; 256 259 257 260 async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0); 258 261 dev->name = dev_name; 259 260 add_to_devices_list(dev); 261 dev->parent = driver_get_device(&devices, parent_dev_handle); 262 263 /* 264 * Currently not used, parent fun handle is stored in context 265 * of the connection to the parent device driver. 266 */ 267 (void) parent_fun_handle; 262 268 263 269 res = driver->driver_ops->add_device(dev); … … 268 274 printf("%s: failed to add a new device with handle = %" PRIun ".\n", 269 275 driver->name, dev_handle); 270 remove_from_devices_list(dev);271 276 delete_device(dev); 272 277 } … … 311 316 */ 312 317 devman_handle_t handle = IPC_GET_ARG2(*icall); 313 device_t *dev = driver_get_device(&devices, handle);314 315 if ( dev== NULL) {316 printf("%s: driver_connection_gen error - no devicewith handle"318 function_t *fun = driver_get_function(&functions, handle); 319 320 if (fun == NULL) { 321 printf("%s: driver_connection_gen error - no function with handle" 317 322 " %" PRIun " was found.\n", driver->name, handle); 318 323 async_answer_0(iid, ENOENT); … … 327 332 328 333 int ret = EOK; 329 /* open the device*/330 if ( dev->ops != NULL && dev->ops->open != NULL)331 ret = (* dev->ops->open)(dev);334 /* Open device function */ 335 if (fun->ops != NULL && fun->ops->open != NULL) 336 ret = (*fun->ops->open)(fun); 332 337 333 338 async_answer_0(iid, ret); … … 344 349 switch (method) { 345 350 case IPC_M_PHONE_HUNGUP: 346 /* close the device*/347 if ( dev->ops != NULL && dev->ops->close != NULL)348 (* dev->ops->close)(dev);351 /* Close device function */ 352 if (fun->ops != NULL && fun->ops->close != NULL) 353 (*fun->ops->close)(fun); 349 354 async_answer_0(callid, EOK); 350 355 return; … … 356 361 if (!is_valid_iface_idx(iface_idx)) { 357 362 remote_handler_t *default_handler = 358 device_get_default_handler(dev);363 function_get_default_handler(fun); 359 364 if (default_handler != NULL) { 360 (*default_handler)( dev, callid, &call);365 (*default_handler)(fun, callid, &call); 361 366 break; 362 367 } 368 363 369 /* 364 * This is not device's interface and the370 * Function has no such interface and 365 371 * default handler is not provided. 366 372 */ … … 372 378 } 373 379 374 /* calling one of the device's interfaces */380 /* calling one of the function's interfaces */ 375 381 376 382 /* Get the interface ops structure. */ 377 void *ops = device_get_ops(dev, iface_idx);383 void *ops = function_get_ops(fun, iface_idx); 378 384 if (ops == NULL) { 379 385 printf("%s: driver_connection_gen error - ", 380 386 driver->name); 381 printf(" devicewith handle %" PRIun " has no interface "387 printf("Function with handle %" PRIun " has no interface " 382 388 "with id %d.\n", handle, iface_idx); 383 389 async_answer_0(callid, ENOTSUP); … … 408 414 * receive parameters from the remote client and it will 409 415 * pass it to the corresponding local interface method 410 * associated with the deviceby its driver.416 * associated with the function by its driver. 411 417 */ 412 (*iface_method_ptr)( dev, ops, callid, &call);418 (*iface_method_ptr)(fun, ops, callid, &call); 413 419 break; 414 420 } … … 425 431 driver_connection_gen(iid, icall, false); 426 432 } 427 428 433 429 434 /** Function for handling connections to device driver. */ … … 456 461 device_t *create_device(void) 457 462 { 458 device_t *dev = malloc(sizeof(device_t));459 460 if (dev != NULL) {461 memset(dev, 0, sizeof(device_t));462 init_match_ids(&dev->match_ids);463 } 464 463 device_t *dev; 464 465 dev = malloc(sizeof(device_t)); 466 if (dev == NULL) 467 return NULL; 468 469 memset(dev, 0, sizeof(device_t)); 465 470 return dev; 466 471 } 467 472 473 /** Create new function structure. 474 * 475 * @return The device structure. 476 */ 477 function_t *create_function(void) 478 { 479 function_t *fun; 480 481 fun = malloc(sizeof(function_t)); 482 if (fun == NULL) 483 return NULL; 484 485 memset(fun, 0, sizeof(device_t)); 486 487 init_match_ids(&fun->match_ids); 488 link_initialize(&fun->link); 489 490 return fun; 491 } 492 468 493 /** Delete device structure. 469 494 * … … 472 497 void delete_device(device_t *dev) 473 498 { 474 clean_match_ids(&dev->match_ids);475 if (dev->name != NULL)476 free(dev->name);477 499 free(dev); 478 500 } 479 501 480 void *device_get_ops(device_t *dev, dev_inferface_idx_t idx) 502 /** Delete device structure. 503 * 504 * @param dev The device structure. 505 */ 506 void delete_function(function_t *fun) 507 { 508 clean_match_ids(&fun->match_ids); 509 if (fun->name != NULL) 510 free(fun->name); 511 free(fun); 512 } 513 514 void *function_get_ops(function_t *fun, dev_inferface_idx_t idx) 481 515 { 482 516 assert(is_valid_iface_idx(idx)); 483 if ( dev->ops == NULL)517 if (fun->ops == NULL) 484 518 return NULL; 485 return dev->ops->interfaces[idx];486 } 487 488 int child_device_register(device_t *child, device_t *parent)489 { 490 assert( child->name != NULL);519 return fun->ops->interfaces[idx]; 520 } 521 522 int register_function(function_t *fun, device_t *dev) 523 { 524 assert(fun->name != NULL); 491 525 492 526 int res; 493 527 494 add_to_devices_list(child); 495 res = devman_child_device_register(child->name, &child->match_ids, 496 parent->handle, &child->handle); 528 fun->dev = dev; 529 530 add_to_functions_list(fun); 531 res = devman_add_function(fun->name, fun->ftype, &fun->match_ids, 532 dev->handle, &fun->handle); 497 533 if (res != EOK) { 498 remove_from_ devices_list(child);534 remove_from_functions_list(fun); 499 535 return res; 500 536 } … … 511 547 * @return Error code. 512 548 */ 513 int child_device_register_wrapper(device_t *parent, const char *child_name,514 const char * child_match_id, int child_match_score)515 { 516 device_t *child= NULL;517 match_id_t *m atch_id = NULL;549 int register_function_wrapper(device_t *dev, const char *fun_name, 550 const char *match_id, int match_score) 551 { 552 function_t *fun = NULL; 553 match_id_t *m_id = NULL; 518 554 int rc; 519 555 520 child = create_device();521 if ( child== NULL) {556 fun = create_function(); 557 if (fun == NULL) { 522 558 rc = ENOMEM; 523 559 goto failure; 524 560 } 525 561 526 child->name = child_name; 527 528 match_id = create_match_id(); 529 if (match_id == NULL) { 562 fun->dev = dev; 563 fun->name = fun_name; 564 fun->ftype = fun_inner; 565 566 m_id = create_match_id(); 567 if (m_id == NULL) { 530 568 rc = ENOMEM; 531 569 goto failure; 532 570 } 533 571 534 m atch_id->id = child_match_id;535 m atch_id->score = child_match_score;536 add_match_id(& child->match_ids, match_id);537 538 rc = child_device_register(child, parent);572 m_id->id = match_id; 573 m_id->score = match_score; 574 add_match_id(&fun->match_ids, m_id); 575 576 rc = register_function(fun, dev); 539 577 if (rc != EOK) 540 578 goto failure; … … 543 581 544 582 failure: 545 if (m atch_id != NULL) {546 m atch_id->id = NULL;547 delete_match_id(m atch_id);548 } 549 550 if ( child!= NULL) {551 child->name = NULL;552 delete_ device(child);583 if (m_id != NULL) { 584 m_id->id = NULL; 585 delete_match_id(m_id); 586 } 587 588 if (fun != NULL) { 589 fun->name = NULL; 590 delete_function(fun); 553 591 } 554 592 … … 557 595 558 596 /** Get default handler for client requests */ 559 remote_handler_t * device_get_default_handler(device_t *dev)560 { 561 if ( dev->ops == NULL)597 remote_handler_t *function_get_default_handler(function_t *fun) 598 { 599 if (fun->ops == NULL) 562 600 return NULL; 563 return dev->ops->default_handler;564 } 565 566 int add_ device_to_class(device_t *dev, const char *class_name)567 { 568 return devman_add_device_to_class( dev->handle, class_name);601 return fun->ops->default_handler; 602 } 603 604 int add_function_to_class(function_t *fun, const char *class_name) 605 { 606 return devman_add_device_to_class(fun->handle, class_name); 569 607 } 570 608
Note:
See TracChangeset
for help on using the changeset viewer.