Changes in uspace/lib/usbdev/src/devdrv.c [0c0f823b:4291215] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/devdrv.c
r0c0f823b r4291215 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> … … 56 56 static const usb_driver_t *driver = NULL; 57 57 58 59 58 /** Main routine of USB device driver. 60 59 * … … 75 74 return ddf_driver_main(&generic_driver); 76 75 } 77 76 /*----------------------------------------------------------------------------*/ 78 77 /** Count number of pipes the driver expects. 79 78 * … … 85 84 { 86 85 size_t count; 87 for (count = 0; endpoints && endpoints[count] != NULL; ++count);86 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count); 88 87 return count; 89 }90 91 /** Initialize endpoint pipes, excluding default control one.92 *93 * @param drv The device driver.94 * @param dev Device to be initialized.95 * @return Error code.96 */97 static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,98 usb_device_t *dev, int alternate_setting)99 {100 assert(dev);101 102 if (endpoints == NULL) {103 dev->pipes = NULL;104 dev->pipes_count = 0;105 return EOK;106 }107 108 usb_endpoint_mapping_t *pipes;109 size_t pipes_count;110 111 int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,112 dev->descriptors.configuration, dev->descriptors.configuration_size,113 dev->interface_no, alternate_setting, &pipes, &pipes_count);114 115 if (rc != EOK) {116 return rc;117 }118 119 dev->pipes = pipes;120 dev->pipes_count = pipes_count;121 122 return EOK;123 88 } 124 89 /*----------------------------------------------------------------------------*/ … … 136 101 assert(driver->ops->device_add); 137 102 103 /* Get place for driver data. */ 138 104 usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t)); 139 105 if (dev == NULL) { … … 142 108 return ENOMEM; 143 109 } 110 111 /* Initialize generic USB driver data. */ 144 112 const char *err_msg = NULL; 145 113 int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg); … … 150 118 } 151 119 120 /* Start USB driver specific initialization. */ 152 121 rc = driver->ops->device_add(dev); 153 122 if (rc != EOK) … … 169 138 if (driver->ops->device_rem == NULL) 170 139 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); 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; 173 147 } 174 148 /*----------------------------------------------------------------------------*/ … … 197 171 * 198 172 * @param dev Device where to destroy the pipes. 199 * @return Error code. 200 */ 201 static int destroy_current_pipes(usb_device_t *dev) 202 { 203 int rc = usb_device_destroy_pipes(dev->ddf_dev, 204 dev->pipes, dev->pipes_count); 205 if (rc != EOK) { 206 return rc; 207 } 208 173 */ 174 static void destroy_current_pipes(usb_device_t *dev) 175 { 176 usb_device_destroy_pipes(dev->pipes, dev->pipes_count); 209 177 dev->pipes = NULL; 210 178 dev->pipes_count = 0; 211 212 return EOK; 213 } 214 179 } 180 /*----------------------------------------------------------------------------*/ 215 181 /** Change interface setting of a device. 216 182 * This function selects new alternate setting of an interface by issuing … … 242 208 } 243 209 244 int rc;245 246 210 /* Destroy existing pipes. */ 247 rc = destroy_current_pipes(dev); 248 if (rc != EOK) { 249 return rc; 250 } 211 destroy_current_pipes(dev); 251 212 252 213 /* Change the interface itself. */ 253 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,214 int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no, 254 215 alternate_setting); 255 216 if (rc != EOK) { … … 258 219 259 220 /* Create new pipes. */ 260 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); 261 225 262 226 return rc; … … 316 280 * - registers endpoints with the host controller 317 281 * 318 * @param[in] dev Generic DDF device backing the USB one.319 282 * @param[in] wire Initialized backing connection to the host controller. 320 283 * @param[in] endpoints Endpoints description, NULL terminated. … … 329 292 * @return Error code. 330 293 */ 331 int usb_device_create_pipes( const ddf_dev_t *dev,usb_device_connection_t *wire,294 int usb_device_create_pipes(usb_device_connection_t *wire, 332 295 const usb_endpoint_description_t **endpoints, 333 296 const uint8_t *config_descr, size_t config_descr_size, … … 335 298 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr) 336 299 { 337 assert(dev != NULL);338 300 assert(wire != NULL); 339 assert(endpoints != NULL);340 301 assert(config_descr != NULL); 341 302 assert(config_descr_size > 0); … … 359 320 } 360 321 361 /* Now allocate and fullyinitialize. */322 /* Now initialize. */ 362 323 for (i = 0; i < pipe_count; i++) { 363 324 pipes[i].description = endpoints[i]; … … 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 340 rc = usb_pipe_register(&pipes[i].pipe, 390 pipes[i].descriptor->poll_interval , &hc_conn);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("%s: Failed to close connection.\n",399 __FUNCTION__);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 368 free(pipes); 432 433 369 return rc; 434 370 } … … 436 372 /** Destroy pipes previously created by usb_device_create_pipes. 437 373 * 438 * @param[in] dev Generic DDF device backing the USB one.439 374 * @param[in] pipes Endpoint mapping to be destroyed. 440 375 * @param[in] pipes_count Number of endpoints. 441 376 */ 442 int usb_device_destroy_pipes(const ddf_dev_t *dev, 443 usb_endpoint_mapping_t *pipes, size_t pipes_count) 444 { 445 assert(dev != NULL); 446 447 if (pipes_count == 0) { 448 assert(pipes == NULL); 449 return EOK; 450 } 451 assert(pipes != NULL); 452 453 int rc; 454 455 /* Prepare connection to HC to allow endpoint unregistering. */ 456 usb_hc_connection_t hc_conn; 457 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev); 458 if (rc != EOK) { 459 return rc; 460 } 461 rc = usb_hc_connection_open(&hc_conn); 462 if (rc != EOK) { 463 return rc; 464 } 465 377 void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count) 378 { 466 379 /* Destroy the pipes. */ 467 size_t i;468 for (i = 0; i < pipes_count; i++) {469 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", 470 383 i, pipes[i].present ? "" : "not "); 471 384 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"); 478 385 usb_pipe_unregister(&pipes[i].pipe); 386 } 479 387 free(pipes); 480 481 return EOK;482 388 } 483 389 … … 505 411 usb_dev->pipes = NULL; 506 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); 426 507 427 /* Initialize backing wire and control pipe. */ 508 int rc = usb_device_connection_initialize_from_device(509 &usb_dev->wire, ddf_dev);428 rc = usb_device_connection_initialize( 429 &usb_dev->wire, &usb_dev->hc_conn, address); 510 430 if (rc != EOK) { 511 431 *errstr_ptr = "device connection initialization"; … … 515 435 /* This pipe was registered by the hub driver, 516 436 * during device initialization. */ 517 rc = usb_pipe_initialize_default_control( &usb_dev->ctrl_pipe,518 &usb_dev-> wire);437 rc = usb_pipe_initialize_default_control( 438 &usb_dev->ctrl_pipe, &usb_dev->wire); 519 439 if (rc != EOK) { 520 440 *errstr_ptr = "default control pipe initialization"; … … 522 442 } 523 443 524 /* Get our interface. */ 525 usb_dev->interface_no = usb_device_get_assigned_interface(ddf_dev); 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 } 526 450 527 451 /* Retrieve standard descriptors. */ 528 rc = usb_device_retrieve_descriptors( &usb_dev->ctrl_pipe,529 &usb_dev-> descriptors);452 rc = usb_device_retrieve_descriptors( 453 &usb_dev->ctrl_pipe, &usb_dev->descriptors); 530 454 if (rc != EOK) { 531 455 *errstr_ptr = "descriptor retrieval"; 456 usb_hc_connection_close(&usb_dev->hc_conn); 532 457 return rc; 533 458 } … … 543 468 (rc == EOK) ? usb_dev->alternate_interfaces.current : 0; 544 469 545 /* TODO Add comment here. */ 546 rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface); 547 if (rc != EOK) { 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); 548 478 /* Full configuration descriptor is allocated. */ 549 479 usb_device_release_descriptors(&usb_dev->descriptors); … … 554 484 } 555 485 486 usb_hc_connection_close(&usb_dev->hc_conn); 556 487 return EOK; 557 488 } … … 566 497 { 567 498 if (dev) { 499 /* Destroy existing pipes. */ 500 destroy_current_pipes(dev); 568 501 /* Ignore errors and hope for the best. */ 569 destroy_current_pipes(dev); 570 502 usb_hc_connection_deinitialize(&dev->hc_conn); 571 503 usb_alternate_interfaces_deinit(&dev->alternate_interfaces); 572 504 usb_device_release_descriptors(&dev->descriptors); 573 505 free(dev->driver_data); 574 } 575 } 576 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 */ 577 515 void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size) 578 516 {
Note:
See TracChangeset
for help on using the changeset viewer.