Changes in uspace/lib/usbdev/src/devdrv.c [b803845:9d58539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/devdrv.c
rb803845 r9d58539 1 1 /* 2 2 * Copyright (c) 2011 Vojtech Horky 3 * Copyright (c) 2011 Jan Vesely 3 4 * All rights reserved. 4 5 * … … 26 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 */ 28 29 29 /** @addtogroup libusbdev 30 30 * @{ … … 36 36 #include <usb/dev/request.h> 37 37 #include <usb/debug.h> 38 #include <usb/dev /dp.h>38 #include <usb/dev.h> 39 39 #include <errno.h> 40 40 #include <str_error.h> … … 46 46 47 47 static driver_ops_t generic_driver_ops = { 48 . add_device= generic_device_add,48 .dev_add = generic_device_add, 49 49 .dev_remove = generic_device_remove, 50 50 .dev_gone = generic_device_gone, … … 56 56 static const usb_driver_t *driver = NULL; 57 57 58 59 58 /** Main routine of USB device driver. 60 59 * … … 64 63 * @return Task exit status. 65 64 */ 66 int usb_driver_main( usb_driver_t *drv)65 int usb_driver_main(const usb_driver_t *drv) 67 66 { 68 67 assert(drv != NULL); … … 75 74 return ddf_driver_main(&generic_driver); 76 75 } 77 76 /*----------------------------------------------------------------------------*/ 78 77 /** Count number of pipes the driver expects. 79 78 * … … 81 80 * @return Number of pipes (excluding default control pipe). 82 81 */ 83 static size_t count_other_pipes(const usb_endpoint_description_t **endpoints) 84 { 85 size_t count = 0; 86 if (endpoints == NULL) { 87 return 0; 88 } 89 90 while (endpoints[count] != NULL) { 91 count++; 92 } 93 82 static inline size_t count_other_pipes( 83 const usb_endpoint_description_t **endpoints) 84 { 85 size_t count; 86 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count); 94 87 return count; 95 }96 97 /** Initialize endpoint pipes, excluding default control one.98 *99 * @param drv The device driver.100 * @param dev Device to be initialized.101 * @return Error code.102 */103 static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,104 usb_device_t *dev, int alternate_setting)105 {106 if (endpoints == NULL) {107 dev->pipes = NULL;108 dev->pipes_count = 0;109 return EOK;110 }111 112 usb_endpoint_mapping_t *pipes;113 size_t pipes_count;114 115 int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,116 dev->descriptors.configuration, dev->descriptors.configuration_size,117 dev->interface_no, alternate_setting, &pipes, &pipes_count);118 119 if (rc != EOK) {120 return rc;121 }122 123 dev->pipes = pipes;124 dev->pipes_count = pipes_count;125 126 return EOK;127 88 } 128 89 /*----------------------------------------------------------------------------*/ … … 140 101 assert(driver->ops->device_add); 141 102 142 int rc; 143 144 usb_device_t *dev = NULL; 103 /* Get place for driver data. */ 104 usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t)); 105 if (dev == NULL) { 106 usb_log_error("USB device `%s' structure allocation failed.\n", 107 gen_dev->name); 108 return ENOMEM; 109 } 110 111 /* Initialize generic USB driver data. */ 145 112 const char *err_msg = NULL; 146 rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);147 if (rc != EOK) { 148 usb_log_error("USB device `%s' creationfailed (%s): %s.\n",113 int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg); 114 if (rc != EOK) { 115 usb_log_error("USB device `%s' init failed (%s): %s.\n", 149 116 gen_dev->name, err_msg, str_error(rc)); 150 117 return rc; 151 118 } 152 gen_dev->driver_data = dev; 153 119 120 /* Start USB driver specific initialization. */ 154 121 rc = driver->ops->device_add(dev); 155 122 if (rc != EOK) … … 171 138 if (driver->ops->device_rem == NULL) 172 139 return ENOTSUP; 173 /* Just tell the driver to stop whatever it is doing, keep structures */ 174 return driver->ops->device_rem(gen_dev->driver_data); 140 /* Just tell the driver to stop whatever it is doing */ 141 usb_device_t *usb_dev = gen_dev->driver_data; 142 const int ret = driver->ops->device_rem(usb_dev); 143 if (ret != EOK) 144 return ret; 145 usb_device_deinit(usb_dev); 146 return EOK; 175 147 } 176 148 /*----------------------------------------------------------------------------*/ … … 199 171 * 200 172 * @param dev Device where to destroy the pipes. 201 * @return Error code. 202 */ 203 static int destroy_current_pipes(usb_device_t *dev) 204 { 205 int rc = usb_device_destroy_pipes(dev->ddf_dev, 206 dev->pipes, dev->pipes_count); 207 if (rc != EOK) { 208 return rc; 209 } 210 173 */ 174 static void destroy_current_pipes(usb_device_t *dev) 175 { 176 usb_device_destroy_pipes(dev->pipes, dev->pipes_count); 211 177 dev->pipes = NULL; 212 178 dev->pipes_count = 0; 213 214 return EOK; 215 } 216 179 } 180 /*----------------------------------------------------------------------------*/ 217 181 /** Change interface setting of a device. 218 182 * This function selects new alternate setting of an interface by issuing … … 244 208 } 245 209 246 int rc;247 248 210 /* Destroy existing pipes. */ 249 rc = destroy_current_pipes(dev); 250 if (rc != EOK) { 251 return rc; 252 } 211 destroy_current_pipes(dev); 253 212 254 213 /* Change the interface itself. */ 255 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,214 int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no, 256 215 alternate_setting); 257 216 if (rc != EOK) { … … 260 219 261 220 /* Create new pipes. */ 262 rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting); 221 rc = usb_device_create_pipes(&dev->wire, endpoints, 222 dev->descriptors.configuration, dev->descriptors.configuration_size, 223 dev->interface_no, (int)alternate_setting, 224 &dev->pipes, &dev->pipes_count); 263 225 264 226 return rc; … … 300 262 } 301 263 264 /** Cleanup structure initialized via usb_device_retrieve_descriptors. 265 * 266 * @param[in] descriptors Where to store the descriptors. 267 */ 268 void usb_device_release_descriptors(usb_device_descriptors_t *descriptors) 269 { 270 assert(descriptors); 271 free(descriptors->configuration); 272 descriptors->configuration = NULL; 273 } 274 302 275 /** Create pipes for a device. 303 276 * … … 307 280 * - registers endpoints with the host controller 308 281 * 309 * @param[in] dev Generic DDF device backing the USB one.310 282 * @param[in] wire Initialized backing connection to the host controller. 311 283 * @param[in] endpoints Endpoints description, NULL terminated. … … 317 289 * (not NULL terminated). 318 290 * @param[out] pipes_count_ptr Where to store number of pipes 319 * (set to if you wish to ignore the count).320 * @return Error code. 321 */ 322 int usb_device_create_pipes( const ddf_dev_t *dev,usb_device_connection_t *wire,291 * (set to NULL if you wish to ignore the count). 292 * @return Error code. 293 */ 294 int usb_device_create_pipes(usb_device_connection_t *wire, 323 295 const usb_endpoint_description_t **endpoints, 324 296 const uint8_t *config_descr, size_t config_descr_size, … … 326 298 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr) 327 299 { 328 assert(dev != NULL);329 300 assert(wire != NULL); 330 assert(endpoints != NULL);331 301 assert(config_descr != NULL); 332 302 assert(config_descr_size > 0); … … 338 308 const size_t pipe_count = count_other_pipes(endpoints); 339 309 if (pipe_count == 0) { 340 *pipes_count_ptr = pipe_count; 310 if (pipes_count_ptr) 311 *pipes_count_ptr = pipe_count; 341 312 *pipes_ptr = NULL; 342 313 return EOK; … … 344 315 345 316 usb_endpoint_mapping_t *pipes 346 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);317 = calloc(pipe_count, sizeof(usb_endpoint_mapping_t)); 347 318 if (pipes == NULL) { 348 319 return ENOMEM; 349 320 } 350 321 351 /* Initialize to NULL to allow smooth rollback. */322 /* Now initialize. */ 352 323 for (i = 0; i < pipe_count; i++) { 353 pipes[i].pipe = NULL;354 }355 356 /* Now allocate and fully initialize. */357 for (i = 0; i < pipe_count; i++) {358 pipes[i].pipe = malloc(sizeof(usb_pipe_t));359 if (pipes[i].pipe == NULL) {360 rc = ENOMEM;361 goto rollback_free_only;362 }363 324 pipes[i].description = endpoints[i]; 364 325 pipes[i].interface_no = interface_no; … … 370 331 config_descr, config_descr_size, wire); 371 332 if (rc != EOK) { 372 goto rollback_free_only; 373 } 374 375 /* Register the endpoints with HC. */ 376 usb_hc_connection_t hc_conn; 377 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev); 378 if (rc != EOK) { 379 goto rollback_free_only; 380 } 381 382 rc = usb_hc_connection_open(&hc_conn); 383 if (rc != EOK) { 384 goto rollback_free_only; 385 } 386 333 free(pipes); 334 return rc; 335 } 336 337 /* Register created pipes. */ 387 338 for (i = 0; i < pipe_count; i++) { 388 339 if (pipes[i].present) { 389 rc = usb_pipe_register( pipes[i].pipe,390 pipes[i].descriptor->poll_interval , &hc_conn);340 rc = usb_pipe_register(&pipes[i].pipe, 341 pipes[i].descriptor->poll_interval); 391 342 if (rc != EOK) { 392 343 goto rollback_unregister_endpoints; … … 394 345 } 395 346 } 396 397 if (usb_hc_connection_close(&hc_conn) != EOK)398 usb_log_warning("usb_device_create_pipes(): "399 "Failed to close connection.\n");400 347 401 348 *pipes_ptr = pipes; … … 415 362 for (i = 0; i < pipe_count; i++) { 416 363 if (pipes[i].present) { 417 usb_pipe_unregister( pipes[i].pipe, &hc_conn);364 usb_pipe_unregister(&pipes[i].pipe); 418 365 } 419 366 } 420 367 421 if (usb_hc_connection_close(&hc_conn) != EOK)422 usb_log_warning("usb_device_create_pipes(): "423 "Failed to close connection.\n");424 425 /*426 * Jump here if something went wrong before some actual communication427 * with HC. Then the only thing that needs to be done is to free428 * allocated memory.429 */430 rollback_free_only:431 for (i = 0; i < pipe_count; i++) {432 if (pipes[i].pipe != NULL) {433 free(pipes[i].pipe);434 }435 }436 368 free(pipes); 437 438 369 return rc; 439 370 } … … 441 372 /** Destroy pipes previously created by usb_device_create_pipes. 442 373 * 443 * @param[in] dev Generic DDF device backing the USB one.444 374 * @param[in] pipes Endpoint mapping to be destroyed. 445 375 * @param[in] pipes_count Number of endpoints. 446 376 */ 447 int usb_device_destroy_pipes(const ddf_dev_t *dev, 448 usb_endpoint_mapping_t *pipes, size_t pipes_count) 449 { 450 assert(dev != NULL); 451 452 if (pipes_count == 0) { 453 assert(pipes == NULL); 454 return EOK; 455 } 456 assert(pipes != NULL); 457 458 int rc; 459 460 /* Prepare connection to HC to allow endpoint unregistering. */ 461 usb_hc_connection_t hc_conn; 462 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev); 463 if (rc != EOK) { 464 return rc; 465 } 466 rc = usb_hc_connection_open(&hc_conn); 467 if (rc != EOK) { 468 return rc; 469 } 470 377 void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count) 378 { 471 379 /* Destroy the pipes. */ 472 size_t i;473 for (i = 0; i < pipes_count; i++) {474 usb_log_debug2("Unregistering pipe %zu (%spresent).\n",380 for (size_t i = 0; i < pipes_count; ++i) { 381 assert(pipes); 382 usb_log_debug2("Unregistering pipe %zu: %spresent.\n", 475 383 i, pipes[i].present ? "" : "not "); 476 384 if (pipes[i].present) 477 usb_pipe_unregister(pipes[i].pipe, &hc_conn); 478 free(pipes[i].pipe); 479 } 480 481 if (usb_hc_connection_close(&hc_conn) != EOK) 482 usb_log_warning("usb_device_destroy_pipes(): " 483 "Failed to close connection.\n"); 484 385 usb_pipe_unregister(&pipes[i].pipe); 386 } 485 387 free(pipes); 486 487 return EOK; 488 } 489 490 /** Initialize control pipe in a device. 491 * 492 * @param dev USB device in question. 493 * @param errmsg Where to store error context. 494 * @return 495 */ 496 static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg) 497 { 498 int rc; 499 500 rc = usb_device_connection_initialize_from_device(&dev->wire, 501 dev->ddf_dev); 502 if (rc != EOK) { 503 *errmsg = "device connection initialization"; 504 return rc; 505 } 506 507 rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe, 508 &dev->wire); 509 if (rc != EOK) { 510 *errmsg = "default control pipe initialization"; 511 return rc; 512 } 513 514 return EOK; 515 } 516 517 518 /** Create new instance of USB device. 519 * 388 } 389 390 /** Initialize new instance of USB device. 391 * 392 * @param[in] usb_dev Pointer to the new device. 520 393 * @param[in] ddf_dev Generic DDF device backing the USB one. 521 394 * @param[in] endpoints NULL terminated array of endpoints (NULL for none). 522 * @param[out] dev_ptr Where to store pointer to the new device.523 395 * @param[out] errstr_ptr Where to store description of context 524 396 * (in case error occurs). 525 397 * @return Error code. 526 398 */ 527 int usb_device_create(ddf_dev_t *ddf_dev, 528 const usb_endpoint_description_t **endpoints, 529 usb_device_t **dev_ptr, const char **errstr_ptr) 530 { 531 assert(dev_ptr != NULL); 399 int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev, 400 const usb_endpoint_description_t **endpoints, const char **errstr_ptr) 401 { 402 assert(usb_dev != NULL); 532 403 assert(ddf_dev != NULL); 533 404 534 int rc; 535 536 usb_device_t *dev = malloc(sizeof(usb_device_t)); 537 if (dev == NULL) { 538 *errstr_ptr = "structure allocation"; 539 return ENOMEM; 540 } 541 542 // FIXME: proper deallocation in case of errors 543 544 dev->ddf_dev = ddf_dev; 545 dev->driver_data = NULL; 546 dev->descriptors.configuration = NULL; 547 dev->alternate_interfaces = NULL; 548 549 dev->pipes_count = 0; 550 dev->pipes = NULL; 405 *errstr_ptr = NULL; 406 407 usb_dev->ddf_dev = ddf_dev; 408 usb_dev->driver_data = NULL; 409 usb_dev->descriptors.configuration = NULL; 410 usb_dev->pipes_count = 0; 411 usb_dev->pipes = NULL; 412 413 /* Get assigned params */ 414 devman_handle_t hc_handle; 415 usb_address_t address; 416 417 int rc = usb_get_info_by_handle(ddf_dev->handle, 418 &hc_handle, &address, &usb_dev->interface_no); 419 if (rc != EOK) { 420 *errstr_ptr = "device parameters retrieval"; 421 return rc; 422 } 423 424 /* Initialize hc connection. */ 425 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle); 551 426 552 427 /* Initialize backing wire and control pipe. */ 553 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr); 554 if (rc != EOK) { 555 return rc; 556 } 557 558 /* Get our interface. */ 559 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev); 428 rc = usb_device_connection_initialize( 429 &usb_dev->wire, &usb_dev->hc_conn, address); 430 if (rc != EOK) { 431 *errstr_ptr = "device connection initialization"; 432 return rc; 433 } 434 435 /* This pipe was registered by the hub driver, 436 * during device initialization. */ 437 rc = usb_pipe_initialize_default_control( 438 &usb_dev->ctrl_pipe, &usb_dev->wire); 439 if (rc != EOK) { 440 *errstr_ptr = "default control pipe initialization"; 441 return rc; 442 } 443 444 /* Open hc connection for pipe registration. */ 445 rc = usb_hc_connection_open(&usb_dev->hc_conn); 446 if (rc != EOK) { 447 *errstr_ptr = "hc connection open"; 448 return rc; 449 } 560 450 561 451 /* Retrieve standard descriptors. */ 562 rc = usb_device_retrieve_descriptors( &dev->ctrl_pipe,563 & dev->descriptors);452 rc = usb_device_retrieve_descriptors( 453 &usb_dev->ctrl_pipe, &usb_dev->descriptors); 564 454 if (rc != EOK) { 565 455 *errstr_ptr = "descriptor retrieval"; 566 return rc; 567 } 568 569 /* Create alternate interfaces. */ 570 rc = usb_alternate_interfaces_create(dev->descriptors.configuration, 571 dev->descriptors.configuration_size, dev->interface_no, 572 &dev->alternate_interfaces); 573 if (rc != EOK) { 574 /* We will try to silently ignore this. */ 575 dev->alternate_interfaces = NULL; 576 } 577 578 rc = initialize_other_pipes(endpoints, dev, 0); 579 if (rc != EOK) { 456 usb_hc_connection_close(&usb_dev->hc_conn); 457 return rc; 458 } 459 460 /* Create alternate interfaces. We will silently ignore failure. 461 * We might either control one interface or an entire device, 462 * it makes no sense to speak about alternate interfaces when 463 * controlling a device. */ 464 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces, 465 usb_dev->descriptors.configuration, 466 usb_dev->descriptors.configuration_size, usb_dev->interface_no); 467 const int alternate_iface = 468 (rc == EOK) ? usb_dev->alternate_interfaces.current : 0; 469 470 /* Create and register other pipes than default control (EP 0) */ 471 rc = usb_device_create_pipes(&usb_dev->wire, endpoints, 472 usb_dev->descriptors.configuration, 473 usb_dev->descriptors.configuration_size, 474 usb_dev->interface_no, (int)alternate_iface, 475 &usb_dev->pipes, &usb_dev->pipes_count); 476 if (rc != EOK) { 477 usb_hc_connection_close(&usb_dev->hc_conn); 478 /* Full configuration descriptor is allocated. */ 479 usb_device_release_descriptors(&usb_dev->descriptors); 480 /* Alternate interfaces may be allocated */ 481 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces); 580 482 *errstr_ptr = "pipes initialization"; 581 483 return rc; 582 484 } 583 485 584 *errstr_ptr = NULL; 585 *dev_ptr = dev; 586 486 usb_hc_connection_close(&usb_dev->hc_conn); 587 487 return EOK; 588 488 } 589 489 590 /** Destroyinstance of a USB device.490 /** Clean instance of a USB device. 591 491 * 592 492 * @param dev Device to be de-initialized. … … 596 496 void usb_device_deinit(usb_device_t *dev) 597 497 { 598 if (dev == NULL) { 599 return; 600 } 601 602 /* Ignore errors and hope for the best. */ 603 destroy_current_pipes(dev); 604 605 if (dev->alternate_interfaces != NULL) { 606 free(dev->alternate_interfaces->alternatives); 607 } 608 free(dev->alternate_interfaces); 609 free(dev->descriptors.configuration); 610 free(dev->driver_data); 611 } 612 498 if (dev) { 499 /* Destroy existing pipes. */ 500 destroy_current_pipes(dev); 501 /* Ignore errors and hope for the best. */ 502 usb_hc_connection_deinitialize(&dev->hc_conn); 503 usb_alternate_interfaces_deinit(&dev->alternate_interfaces); 504 usb_device_release_descriptors(&dev->descriptors); 505 free(dev->driver_data); 506 dev->driver_data = NULL; 507 } 508 } 509 510 /** Allocate driver specific data. 511 * @param usb_dev usb_device structure. 512 * @param size requested data size. 513 * @return Pointer to the newly allocated space, NULL on failure. 514 */ 613 515 void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size) 614 516 {
Note:
See TracChangeset
for help on using the changeset viewer.