Changes in uspace/lib/usbdev/src/devdrv.c [70452dd4:b77931d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/devdrv.c
r70452dd4 rb77931d 41 41 #include <assert.h> 42 42 43 static int generic_add_device(ddf_dev_t *); 43 static int generic_device_add(ddf_dev_t *); 44 static int generic_device_remove(ddf_dev_t *); 45 static int generic_device_gone(ddf_dev_t *); 44 46 45 47 static driver_ops_t generic_driver_ops = { 46 .add_device = generic_add_device 48 .add_device = generic_device_add, 49 .dev_remove = generic_device_remove, 50 .dev_gone = generic_device_gone, 47 51 }; 48 52 static driver_t generic_driver = { … … 50 54 }; 51 55 52 static usb_driver_t *driver = NULL;56 static const usb_driver_t *driver = NULL; 53 57 54 58 … … 60 64 * @return Task exit status. 61 65 */ 62 int usb_driver_main( usb_driver_t *drv)66 int usb_driver_main(const usb_driver_t *drv) 63 67 { 64 68 assert(drv != NULL); … … 77 81 * @return Number of pipes (excluding default control pipe). 78 82 */ 79 static size_t count_other_pipes(usb_endpoint_description_t **endpoints) 80 { 81 size_t count = 0; 82 if (endpoints == NULL) { 83 return 0; 84 } 85 86 while (endpoints[count] != NULL) { 87 count++; 88 } 89 83 static inline size_t count_other_pipes( 84 const usb_endpoint_description_t **endpoints) 85 { 86 size_t count; 87 for (count = 0; endpoints && endpoints[count] != NULL; ++count); 90 88 return count; 91 89 } … … 97 95 * @return Error code. 98 96 */ 99 static int initialize_other_pipes( usb_endpoint_description_t **endpoints,97 static int initialize_other_pipes(const usb_endpoint_description_t **endpoints, 100 98 usb_device_t *dev, int alternate_setting) 101 99 { 100 assert(dev); 101 102 102 if (endpoints == NULL) { 103 103 dev->pipes = NULL; … … 111 111 int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints, 112 112 dev->descriptors.configuration, dev->descriptors.configuration_size, 113 dev->interface_no, alternate_setting, 114 &pipes, &pipes_count); 113 dev->interface_no, alternate_setting, &pipes, &pipes_count); 115 114 116 115 if (rc != EOK) { … … 123 122 return EOK; 124 123 } 125 126 /** Callback when new device is supposed to be controlled by this driver.127 * 128 * This callback is a wrapper for USB specific version of @c add_device.124 /*----------------------------------------------------------------------------*/ 125 /** Callback when a new device is supposed to be controlled by this driver. 126 * 127 * This callback is a wrapper for USB specific version of @c device_add. 129 128 * 130 129 * @param gen_dev Device structure as prepared by DDF. 131 130 * @return Error code. 132 131 */ 133 int generic_ add_device(ddf_dev_t *gen_dev)132 int generic_device_add(ddf_dev_t *gen_dev) 134 133 { 135 134 assert(driver); 136 135 assert(driver->ops); 137 assert(driver->ops->add_device); 138 139 int rc; 140 141 usb_device_t *dev = NULL; 136 assert(driver->ops->device_add); 137 138 usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t)); 139 if (dev == NULL) { 140 usb_log_error("USB device `%s' structure allocation failed.\n", 141 gen_dev->name); 142 return ENOMEM; 143 } 142 144 const char *err_msg = NULL; 143 rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);144 if (rc != EOK) { 145 usb_log_error("USB device `%s' creationfailed (%s): %s.\n",145 int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg); 146 if (rc != EOK) { 147 usb_log_error("USB device `%s' init failed (%s): %s.\n", 146 148 gen_dev->name, err_msg, str_error(rc)); 147 149 return rc; 148 150 } 149 151 150 return driver->ops->add_device(dev); 151 } 152 152 rc = driver->ops->device_add(dev); 153 if (rc != EOK) 154 usb_device_deinit(dev); 155 return rc; 156 } 157 /*----------------------------------------------------------------------------*/ 158 /** Callback when a device is supposed to be removed from the system. 159 * 160 * This callback is a wrapper for USB specific version of @c device_remove. 161 * 162 * @param gen_dev Device structure as prepared by DDF. 163 * @return Error code. 164 */ 165 int generic_device_remove(ddf_dev_t *gen_dev) 166 { 167 assert(driver); 168 assert(driver->ops); 169 if (driver->ops->device_rem == NULL) 170 return ENOTSUP; 171 /* Just tell the driver to stop whatever it is doing, keep structures */ 172 return driver->ops->device_rem(gen_dev->driver_data); 173 } 174 /*----------------------------------------------------------------------------*/ 175 /** Callback when a device was removed from the system. 176 * 177 * This callback is a wrapper for USB specific version of @c device_gone. 178 * 179 * @param gen_dev Device structure as prepared by DDF. 180 * @return Error code. 181 */ 182 int generic_device_gone(ddf_dev_t *gen_dev) 183 { 184 assert(driver); 185 assert(driver->ops); 186 if (driver->ops->device_gone == NULL) 187 return ENOTSUP; 188 usb_device_t *usb_dev = gen_dev->driver_data; 189 const int ret = driver->ops->device_gone(usb_dev); 190 if (ret == EOK) 191 usb_device_deinit(usb_dev); 192 193 return ret; 194 } 195 /*----------------------------------------------------------------------------*/ 153 196 /** Destroy existing pipes of a USB device. 154 197 * … … 193 236 */ 194 237 int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting, 195 usb_endpoint_description_t **endpoints)238 const usb_endpoint_description_t **endpoints) 196 239 { 197 240 if (dev->interface_no < 0) { … … 253 296 254 297 return rc; 298 } 299 300 /** Cleanup structure initialized via usb_device_retrieve_descriptors. 301 * 302 * @param[in] descriptors Where to store the descriptors. 303 */ 304 void usb_device_release_descriptors(usb_device_descriptors_t *descriptors) 305 { 306 assert(descriptors); 307 free(descriptors->configuration); 308 descriptors->configuration = NULL; 255 309 } 256 310 … … 272 326 * (not NULL terminated). 273 327 * @param[out] pipes_count_ptr Where to store number of pipes 274 * (set to if you wish to ignore the count).275 * @return Error code. 276 */ 277 int usb_device_create_pipes( ddf_dev_t *dev, usb_device_connection_t *wire,278 usb_endpoint_description_t **endpoints,279 uint8_t *config_descr, size_t config_descr_size,328 * (set to NULL if you wish to ignore the count). 329 * @return Error code. 330 */ 331 int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire, 332 const usb_endpoint_description_t **endpoints, 333 const uint8_t *config_descr, size_t config_descr_size, 280 334 int interface_no, int interface_setting, 281 335 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr) … … 291 345 int rc; 292 346 293 size_t pipe_count = count_other_pipes(endpoints);347 const size_t pipe_count = count_other_pipes(endpoints); 294 348 if (pipe_count == 0) { 349 if (pipes_count_ptr) 350 *pipes_count_ptr = pipe_count; 295 351 *pipes_ptr = NULL; 296 352 return EOK; … … 298 354 299 355 usb_endpoint_mapping_t *pipes 300 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);356 = calloc(pipe_count, sizeof(usb_endpoint_mapping_t)); 301 357 if (pipes == NULL) { 302 358 return ENOMEM; 303 359 } 304 360 305 /* Initialize to NULL to allow smooth rollback. */306 for (i = 0; i < pipe_count; i++) {307 pipes[i].pipe = NULL;308 }309 310 361 /* Now allocate and fully initialize. */ 311 362 for (i = 0; i < pipe_count; i++) { 312 pipes[i].pipe = malloc(sizeof(usb_pipe_t));313 if (pipes[i].pipe == NULL) {314 rc = ENOMEM;315 goto rollback_free_only;316 }317 363 pipes[i].description = endpoints[i]; 318 364 pipes[i].interface_no = interface_no; … … 341 387 for (i = 0; i < pipe_count; i++) { 342 388 if (pipes[i].present) { 343 rc = usb_pipe_register( pipes[i].pipe,389 rc = usb_pipe_register(&pipes[i].pipe, 344 390 pipes[i].descriptor->poll_interval, &hc_conn); 345 391 if (rc != EOK) { … … 349 395 } 350 396 351 usb_hc_connection_close(&hc_conn); 397 if (usb_hc_connection_close(&hc_conn) != EOK) 398 usb_log_warning("%s: Failed to close connection.\n", 399 __FUNCTION__); 352 400 353 401 *pipes_ptr = pipes; … … 367 415 for (i = 0; i < pipe_count; i++) { 368 416 if (pipes[i].present) { 369 usb_pipe_unregister( pipes[i].pipe, &hc_conn);417 usb_pipe_unregister(&pipes[i].pipe, &hc_conn); 370 418 } 371 419 } 372 420 373 usb_hc_connection_close(&hc_conn); 421 if (usb_hc_connection_close(&hc_conn) != EOK) 422 usb_log_warning("usb_device_create_pipes(): " 423 "Failed to close connection.\n"); 374 424 375 425 /* … … 379 429 */ 380 430 rollback_free_only: 381 for (i = 0; i < pipe_count; i++) {382 if (pipes[i].pipe != NULL) {383 free(pipes[i].pipe);384 }385 }386 431 free(pipes); 387 432 … … 395 440 * @param[in] pipes_count Number of endpoints. 396 441 */ 397 int usb_device_destroy_pipes( ddf_dev_t *dev,442 int usb_device_destroy_pipes(const ddf_dev_t *dev, 398 443 usb_endpoint_mapping_t *pipes, size_t pipes_count) 399 444 { 400 445 assert(dev != NULL); 401 assert(((pipes != NULL) && (pipes_count > 0))402 || ((pipes == NULL) && (pipes_count == 0)));403 446 404 447 if (pipes_count == 0) { 448 assert(pipes == NULL); 405 449 return EOK; 406 450 } 451 assert(pipes != NULL); 407 452 408 453 int rc; … … 422 467 size_t i; 423 468 for (i = 0; i < pipes_count; i++) { 424 usb_pipe_unregister(pipes[i].pipe, &hc_conn); 425 free(pipes[i].pipe); 426 } 427 428 usb_hc_connection_close(&hc_conn); 469 usb_log_debug2("Unregistering pipe %zu (%spresent).\n", 470 i, pipes[i].present ? "" : "not "); 471 if (pipes[i].present) 472 usb_pipe_unregister(&pipes[i].pipe, &hc_conn); 473 } 474 475 if (usb_hc_connection_close(&hc_conn) != EOK) 476 usb_log_warning("usb_device_destroy_pipes(): " 477 "Failed to close connection.\n"); 429 478 430 479 free(pipes); … … 433 482 } 434 483 435 /** Initialize control pipe in a device. 436 * 437 * @param dev USB device in question. 438 * @param errmsg Where to store error context. 439 * @return 440 */ 441 static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg) 442 { 443 int rc; 444 445 rc = usb_device_connection_initialize_from_device(&dev->wire, 446 dev->ddf_dev); 447 if (rc != EOK) { 448 *errmsg = "device connection initialization"; 449 return rc; 450 } 451 452 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe, 453 &dev->wire); 454 if (rc != EOK) { 455 *errmsg = "default control pipe initialization"; 456 return rc; 457 } 458 459 return EOK; 460 } 461 462 463 /** Create new instance of USB device. 464 * 484 /** Initialize new instance of USB device. 485 * 486 * @param[in] usb_dev Pointer to the new device. 465 487 * @param[in] ddf_dev Generic DDF device backing the USB one. 466 488 * @param[in] endpoints NULL terminated array of endpoints (NULL for none). 467 * @param[out] dev_ptr Where to store pointer to the new device.468 489 * @param[out] errstr_ptr Where to store description of context 469 490 * (in case error occurs). 470 491 * @return Error code. 471 492 */ 472 int usb_device_create(ddf_dev_t *ddf_dev, 473 usb_endpoint_description_t **endpoints, 474 usb_device_t **dev_ptr, const char **errstr_ptr) 475 { 476 assert(dev_ptr != NULL); 493 int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev, 494 const usb_endpoint_description_t **endpoints, const char **errstr_ptr) 495 { 496 assert(usb_dev != NULL); 477 497 assert(ddf_dev != NULL); 478 498 479 int rc; 480 481 usb_device_t *dev = malloc(sizeof(usb_device_t)); 482 if (dev == NULL) { 483 *errstr_ptr = "structure allocation"; 484 return ENOMEM; 485 } 486 487 // FIXME: proper deallocation in case of errors 488 489 dev->ddf_dev = ddf_dev; 490 dev->driver_data = NULL; 491 dev->descriptors.configuration = NULL; 492 dev->alternate_interfaces = NULL; 493 494 dev->pipes_count = 0; 495 dev->pipes = NULL; 499 *errstr_ptr = NULL; 500 501 usb_dev->ddf_dev = ddf_dev; 502 usb_dev->driver_data = NULL; 503 usb_dev->descriptors.configuration = NULL; 504 usb_dev->pipes_count = 0; 505 usb_dev->pipes = NULL; 496 506 497 507 /* Initialize backing wire and control pipe. */ 498 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr); 499 if (rc != EOK) { 508 int rc = usb_device_connection_initialize_from_device( 509 &usb_dev->wire, ddf_dev); 510 if (rc != EOK) { 511 *errstr_ptr = "device connection initialization"; 512 return rc; 513 } 514 515 /* This pipe was registered by the hub driver, 516 * during device initialization. */ 517 rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe, 518 &usb_dev->wire); 519 if (rc != EOK) { 520 *errstr_ptr = "default control pipe initialization"; 500 521 return rc; 501 522 } 502 523 503 524 /* Get our interface. */ 504 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);525 usb_dev->interface_no = usb_device_get_assigned_interface(ddf_dev); 505 526 506 527 /* Retrieve standard descriptors. */ 507 rc = usb_device_retrieve_descriptors(& dev->ctrl_pipe,508 & dev->descriptors);528 rc = usb_device_retrieve_descriptors(&usb_dev->ctrl_pipe, 529 &usb_dev->descriptors); 509 530 if (rc != EOK) { 510 531 *errstr_ptr = "descriptor retrieval"; … … 512 533 } 513 534 514 /* Create alternate interfaces. */ 515 rc = usb_alternate_interfaces_create(dev->descriptors.configuration, 516 dev->descriptors.configuration_size, dev->interface_no, 517 &dev->alternate_interfaces); 518 if (rc != EOK) { 519 /* We will try to silently ignore this. */ 520 dev->alternate_interfaces = NULL; 521 } 522 523 rc = initialize_other_pipes(endpoints, dev, 0); 524 if (rc != EOK) { 535 /* Create alternate interfaces. We will silently ignore failure. 536 * We might either control one interface or an entire device, 537 * it makes no sense to speak about alternate interfaces when 538 * controlling a device. */ 539 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces, 540 usb_dev->descriptors.configuration, 541 usb_dev->descriptors.configuration_size, usb_dev->interface_no); 542 const int alternate_iface = 543 (rc == EOK) ? usb_dev->alternate_interfaces.current : 0; 544 545 /* TODO Add comment here. */ 546 rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface); 547 if (rc != EOK) { 548 /* Full configuration descriptor is allocated. */ 549 usb_device_release_descriptors(&usb_dev->descriptors); 550 /* Alternate interfaces may be allocated */ 551 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces); 525 552 *errstr_ptr = "pipes initialization"; 526 553 return rc; 527 554 } 528 555 529 *errstr_ptr = NULL;530 *dev_ptr = dev;531 532 556 return EOK; 533 557 } 534 558 535 /** Destroyinstance of a USB device.536 * 537 * @param dev Device to be de stroyed.538 * /539 void usb_device_destroy(usb_device_t *dev) 540 { 541 if (dev == NULL) { 542 return; 543 }544 545 /* Ignore errors and hope for the best. */546 usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count); 547 if (dev->descriptors.configuration != NULL) {548 free(dev->descriptors.configuration);549 }550 551 if (dev->alternate_interfaces != NULL) { 552 if (dev->alternate_interfaces->alternatives != NULL) { 553 free(dev->alternate_interfaces->alternatives); 554 } 555 free(dev->alternate_interfaces);556 }557 558 free(dev); 559 /** Clean instance of a USB device. 560 * 561 * @param dev Device to be de-initialized. 562 * 563 * Does not free/destroy supplied pointer. 564 */ 565 void usb_device_deinit(usb_device_t *dev) 566 { 567 if (dev) { 568 /* Ignore errors and hope for the best. */ 569 destroy_current_pipes(dev); 570 571 usb_alternate_interfaces_deinit(&dev->alternate_interfaces); 572 usb_device_release_descriptors(&dev->descriptors); 573 free(dev->driver_data); 574 } 575 } 576 577 void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size) 578 { 579 assert(usb_dev); 580 assert(usb_dev->driver_data == NULL); 581 return usb_dev->driver_data = calloc(1, size); 582 559 583 } 560 584
Note:
See TracChangeset
for help on using the changeset viewer.