Changeset 00aece0 in mainline for uspace/lib/usbdev/src/devdrv.c
- Timestamp:
- 2012-02-18T16:47:38Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4449c6c
- Parents:
- bd5f3b7 (diff), f943dd3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usbdev/src/devdrv.c
rbd5f3b7 r00aece0 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> 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 .dev_add = 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; 53 56 static const usb_driver_t *driver = NULL; 54 57 55 58 /** Main routine of USB device driver. … … 60 63 * @return Task exit status. 61 64 */ 62 int usb_driver_main( usb_driver_t *drv)65 int usb_driver_main(const usb_driver_t *drv) 63 66 { 64 67 assert(drv != NULL); … … 71 74 return ddf_driver_main(&generic_driver); 72 75 } 73 76 /*----------------------------------------------------------------------------*/ 74 77 /** Count number of pipes the driver expects. 75 78 * … … 77 80 * @return Number of pipes (excluding default control pipe). 78 81 */ 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 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); 90 87 return count; 91 88 } 92 93 /** Initialize endpoint pipes, excluding default control one. 94 * 95 * @param drv The device driver. 96 * @param dev Device to be initialized. 97 * @return Error code. 98 */ 99 static int initialize_other_pipes(usb_endpoint_description_t **endpoints, 100 usb_device_t *dev, int alternate_setting) 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, 114 &pipes, &pipes_count); 115 116 if (rc != EOK) { 117 return rc; 118 } 119 120 dev->pipes = pipes; 121 dev->pipes_count = pipes_count; 122 123 return EOK; 124 } 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. 89 /*----------------------------------------------------------------------------*/ 90 /** Callback when a new device is supposed to be controlled by this driver. 91 * 92 * This callback is a wrapper for USB specific version of @c device_add. 129 93 * 130 94 * @param gen_dev Device structure as prepared by DDF. 131 95 * @return Error code. 132 96 */ 133 int generic_ add_device(ddf_dev_t *gen_dev)97 int generic_device_add(ddf_dev_t *gen_dev) 134 98 { 135 99 assert(driver); 136 100 assert(driver->ops); 137 assert(driver->ops->add_device); 138 139 int rc; 140 141 usb_device_t *dev = NULL; 101 assert(driver->ops->device_add); 102 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. */ 142 112 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",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", 146 116 gen_dev->name, err_msg, str_error(rc)); 147 117 return rc; 148 118 } 149 119 150 return driver->ops->add_device(dev); 151 } 152 120 /* Start USB driver specific initialization. */ 121 rc = driver->ops->device_add(dev); 122 if (rc != EOK) 123 usb_device_deinit(dev); 124 return rc; 125 } 126 /*----------------------------------------------------------------------------*/ 127 /** Callback when a device is supposed to be removed from the system. 128 * 129 * This callback is a wrapper for USB specific version of @c device_remove. 130 * 131 * @param gen_dev Device structure as prepared by DDF. 132 * @return Error code. 133 */ 134 int generic_device_remove(ddf_dev_t *gen_dev) 135 { 136 assert(driver); 137 assert(driver->ops); 138 if (driver->ops->device_rem == NULL) 139 return ENOTSUP; 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; 147 } 148 /*----------------------------------------------------------------------------*/ 149 /** Callback when a device was removed from the system. 150 * 151 * This callback is a wrapper for USB specific version of @c device_gone. 152 * 153 * @param gen_dev Device structure as prepared by DDF. 154 * @return Error code. 155 */ 156 int generic_device_gone(ddf_dev_t *gen_dev) 157 { 158 assert(driver); 159 assert(driver->ops); 160 if (driver->ops->device_gone == NULL) 161 return ENOTSUP; 162 usb_device_t *usb_dev = gen_dev->driver_data; 163 const int ret = driver->ops->device_gone(usb_dev); 164 if (ret == EOK) 165 usb_device_deinit(usb_dev); 166 167 return ret; 168 } 169 /*----------------------------------------------------------------------------*/ 153 170 /** Destroy existing pipes of a USB device. 154 171 * 155 172 * @param dev Device where to destroy the pipes. 156 * @return Error code. 157 */ 158 static int destroy_current_pipes(usb_device_t *dev) 159 { 160 int rc = usb_device_destroy_pipes(dev->ddf_dev, 161 dev->pipes, dev->pipes_count); 162 if (rc != EOK) { 163 return rc; 164 } 165 173 */ 174 static void destroy_current_pipes(usb_device_t *dev) 175 { 176 usb_device_destroy_pipes(dev->pipes, dev->pipes_count); 166 177 dev->pipes = NULL; 167 178 dev->pipes_count = 0; 168 169 return EOK; 170 } 171 179 } 180 /*----------------------------------------------------------------------------*/ 172 181 /** Change interface setting of a device. 173 182 * This function selects new alternate setting of an interface by issuing … … 193 202 */ 194 203 int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting, 195 usb_endpoint_description_t **endpoints)204 const usb_endpoint_description_t **endpoints) 196 205 { 197 206 if (dev->interface_no < 0) { … … 199 208 } 200 209 201 int rc;202 203 210 /* Destroy existing pipes. */ 204 rc = destroy_current_pipes(dev); 205 if (rc != EOK) { 206 return rc; 207 } 211 destroy_current_pipes(dev); 208 212 209 213 /* Change the interface itself. */ 210 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,214 int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no, 211 215 alternate_setting); 212 216 if (rc != EOK) { … … 215 219 216 220 /* Create new pipes. */ 217 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); 218 225 219 226 return rc; … … 255 262 } 256 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 257 275 /** Create pipes for a device. 258 276 * … … 262 280 * - registers endpoints with the host controller 263 281 * 264 * @param[in] dev Generic DDF device backing the USB one.265 282 * @param[in] wire Initialized backing connection to the host controller. 266 283 * @param[in] endpoints Endpoints description, NULL terminated. … … 272 289 * (not NULL terminated). 273 290 * @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,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, 295 const usb_endpoint_description_t **endpoints, 296 const uint8_t *config_descr, size_t config_descr_size, 280 297 int interface_no, int interface_setting, 281 298 usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr) 282 299 { 283 assert(dev != NULL);284 300 assert(wire != NULL); 285 assert(endpoints != NULL);286 301 assert(config_descr != NULL); 287 302 assert(config_descr_size > 0); … … 291 306 int rc; 292 307 293 size_t pipe_count = count_other_pipes(endpoints);308 const size_t pipe_count = count_other_pipes(endpoints); 294 309 if (pipe_count == 0) { 310 if (pipes_count_ptr) 311 *pipes_count_ptr = pipe_count; 295 312 *pipes_ptr = NULL; 296 313 return EOK; … … 298 315 299 316 usb_endpoint_mapping_t *pipes 300 = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);317 = calloc(pipe_count, sizeof(usb_endpoint_mapping_t)); 301 318 if (pipes == NULL) { 302 319 return ENOMEM; 303 320 } 304 321 305 /* Initialize to NULL to allow smooth rollback. */322 /* Now initialize. */ 306 323 for (i = 0; i < pipe_count; i++) { 307 pipes[i].pipe = NULL;308 }309 310 /* Now allocate and fully initialize. */311 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 324 pipes[i].description = endpoints[i]; 318 325 pipes[i].interface_no = interface_no; … … 324 331 config_descr, config_descr_size, wire); 325 332 if (rc != EOK) { 326 goto rollback_free_only; 327 } 328 329 /* Register the endpoints with HC. */ 330 usb_hc_connection_t hc_conn; 331 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev); 332 if (rc != EOK) { 333 goto rollback_free_only; 334 } 335 336 rc = usb_hc_connection_open(&hc_conn); 337 if (rc != EOK) { 338 goto rollback_free_only; 339 } 340 333 free(pipes); 334 return rc; 335 } 336 337 /* Register created pipes. */ 341 338 for (i = 0; i < pipe_count; i++) { 342 339 if (pipes[i].present) { 343 rc = usb_pipe_register( pipes[i].pipe,344 pipes[i].descriptor->poll_interval , &hc_conn);340 rc = usb_pipe_register(&pipes[i].pipe, 341 pipes[i].descriptor->poll_interval); 345 342 if (rc != EOK) { 346 343 goto rollback_unregister_endpoints; … … 348 345 } 349 346 } 350 351 usb_hc_connection_close(&hc_conn);352 347 353 348 *pipes_ptr = pipes; … … 367 362 for (i = 0; i < pipe_count; i++) { 368 363 if (pipes[i].present) { 369 usb_pipe_unregister( pipes[i].pipe, &hc_conn);364 usb_pipe_unregister(&pipes[i].pipe); 370 365 } 371 366 } 372 367 373 usb_hc_connection_close(&hc_conn);374 375 /*376 * Jump here if something went wrong before some actual communication377 * with HC. Then the only thing that needs to be done is to free378 * allocated memory.379 */380 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 368 free(pipes); 387 388 369 return rc; 389 370 } … … 391 372 /** Destroy pipes previously created by usb_device_create_pipes. 392 373 * 393 * @param[in] dev Generic DDF device backing the USB one.394 374 * @param[in] pipes Endpoint mapping to be destroyed. 395 375 * @param[in] pipes_count Number of endpoints. 396 376 */ 397 int usb_device_destroy_pipes(ddf_dev_t *dev, 398 usb_endpoint_mapping_t *pipes, size_t pipes_count) 399 { 400 assert(dev != NULL); 401 assert(((pipes != NULL) && (pipes_count > 0)) 402 || ((pipes == NULL) && (pipes_count == 0))); 403 404 if (pipes_count == 0) { 405 return EOK; 406 } 407 408 int rc; 409 410 /* Prepare connection to HC to allow endpoint unregistering. */ 411 usb_hc_connection_t hc_conn; 412 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev); 413 if (rc != EOK) { 414 return rc; 415 } 416 rc = usb_hc_connection_open(&hc_conn); 417 if (rc != EOK) { 418 return rc; 419 } 420 377 void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count) 378 { 421 379 /* Destroy the pipes. */ 422 size_t i; 423 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); 429 380 for (size_t i = 0; i < pipes_count; ++i) { 381 assert(pipes); 382 usb_log_debug2("Unregistering pipe %zu: %spresent.\n", 383 i, pipes[i].present ? "" : "not "); 384 if (pipes[i].present) 385 usb_pipe_unregister(&pipes[i].pipe); 386 } 430 387 free(pipes); 431 432 return EOK; 433 } 434 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 * 388 } 389 390 /** Initialize new instance of USB device. 391 * 392 * @param[in] usb_dev Pointer to the new device. 465 393 * @param[in] ddf_dev Generic DDF device backing the USB one. 466 394 * @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 395 * @param[out] errstr_ptr Where to store description of context 469 396 * (in case error occurs). 470 397 * @return Error code. 471 398 */ 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); 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); 477 403 assert(ddf_dev != NULL); 478 404 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; 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); 496 426 497 427 /* Initialize backing wire and control pipe. */ 498 rc = init_wire_and_ctrl_pipe(dev, errstr_ptr); 499 if (rc != EOK) { 500 return rc; 501 } 502 503 /* Get our interface. */ 504 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 } 505 450 506 451 /* Retrieve standard descriptors. */ 507 rc = usb_device_retrieve_descriptors( &dev->ctrl_pipe,508 & dev->descriptors);452 rc = usb_device_retrieve_descriptors( 453 &usb_dev->ctrl_pipe, &usb_dev->descriptors); 509 454 if (rc != EOK) { 510 455 *errstr_ptr = "descriptor retrieval"; 511 return rc; 512 } 513 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) { 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); 525 482 *errstr_ptr = "pipes initialization"; 526 483 return rc; 527 484 } 528 485 529 *errstr_ptr = NULL; 530 *dev_ptr = dev; 531 486 usb_hc_connection_close(&usb_dev->hc_conn); 532 487 return EOK; 533 488 } 534 489 535 /** Destroy instance of a USB device. 536 * 537 * @param dev Device to be destroyed. 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); 490 /** Clean instance of a USB device. 491 * 492 * @param dev Device to be de-initialized. 493 * 494 * Does not free/destroy supplied pointer. 495 */ 496 void usb_device_deinit(usb_device_t *dev) 497 { 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 */ 515 void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size) 516 { 517 assert(usb_dev); 518 assert(usb_dev->driver_data == NULL); 519 return usb_dev->driver_data = calloc(1, size); 520 559 521 } 560 522
Note:
See TracChangeset
for help on using the changeset viewer.