Changeset a6afb4c in mainline
- Timestamp:
- 2018-01-23T14:02:35Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4db49344
- Parents:
- e7e1fd3
- git-author:
- Ondřej Hlavatý <aearsis@…> (2018-01-23 13:35:50)
- git-committer:
- Ondřej Hlavatý <aearsis@…> (2018-01-23 14:02:35)
- Location:
- uspace/lib
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/drv/include/usbhc_iface.h
re7e1fd3 ra6afb4c 60 60 * Negative values could be used to indicate error. 61 61 */ 62 typedef int16_t usb_endpoint_t;62 typedef uint16_t usb_endpoint_t; 63 63 64 64 /** USB address type. 65 65 * Negative values could be used to indicate error. 66 66 */ 67 typedef int16_t usb_address_t; 67 typedef uint16_t usb_address_t; 68 69 /** 70 * USB Stream ID type. 71 */ 72 typedef uint16_t usb_stream_t; 68 73 69 74 /** USB transfer type. */ … … 72 77 USB_TRANSFER_ISOCHRONOUS = 1, 73 78 USB_TRANSFER_BULK = 2, 74 USB_TRANSFER_INTERRUPT = 3 79 USB_TRANSFER_INTERRUPT = 3, 75 80 } usb_transfer_type_t; 81 82 #define USB_TRANSFER_COUNT (USB_TRANSFER_INTERRUPT + 1) 76 83 77 84 /** USB data transfer direction. */ … … 79 86 USB_DIRECTION_IN, 80 87 USB_DIRECTION_OUT, 81 USB_DIRECTION_BOTH 88 USB_DIRECTION_BOTH, 82 89 } usb_direction_t; 90 91 #define USB_DIRECTION_COUNT (USB_DIRECTION_BOTH + 1) 83 92 84 93 /** USB complete address type. … … 89 98 usb_address_t address; 90 99 usb_endpoint_t endpoint; 91 u int32_t stream;100 usb_stream_t stream; 92 101 } __attribute__((packed)); 93 102 uint64_t packed; -
uspace/lib/usb/include/usb/usb.h
re7e1fd3 ra6afb4c 63 63 } 64 64 65 static inline bool usb_speed_is_valid(const usb_speed_t s) 66 { 67 return (s >= USB_SPEED_LOW) && (s < USB_SPEED_MAX); 68 } 69 65 70 const char *usb_str_speed(usb_speed_t); 66 71 … … 97 102 static inline bool usb_address_is_valid(usb_address_t a) 98 103 { 99 return (a >= USB_ADDRESS_DEFAULT) && (a <= USB11_ADDRESS_MAX);104 return a <= USB11_ADDRESS_MAX; 100 105 } 101 106 … … 105 110 /** Maximum endpoint number in USB */ 106 111 #define USB_ENDPOINT_MAX 16 112 113 /** There might be two directions for every endpoint number (except 0) */ 114 #define USB_ENDPOINT_COUNT (2 * USB_ENDPOINT_MAX) 107 115 108 116 /** Check USB endpoint for allowed values. … … 115 123 static inline bool usb_endpoint_is_valid(usb_endpoint_t ep) 116 124 { 117 return (ep >= USB_ENDPOINT_DEFAULT_CONTROL) && 118 (ep < USB_ENDPOINT_MAX); 125 return ep < USB_ENDPOINT_MAX; 119 126 } 120 127 121 /** Check USB target for allowed values (address and endpoint). 128 /** 129 * Check USB target for allowed values (address, endpoint, stream). 122 130 * 123 131 * @param target. 124 132 * @return True, if values are wihtin limits, false otherwise. 125 133 */ 126 static inline bool usb_target_is_valid(usb_target_t target)134 static inline bool usb_target_is_valid(usb_target_t *target) 127 135 { 128 return usb_address_is_valid(target.address) && 129 usb_endpoint_is_valid(target.endpoint); 136 return usb_address_is_valid(target->address) && 137 usb_endpoint_is_valid(target->endpoint); 138 139 // A 16-bit Stream ID is always valid. 130 140 } 131 141 … … 136 146 * @return Whether @p a and @p b points to the same pipe on the same device. 137 147 */ 138 static inline intusb_target_same(usb_target_t a, usb_target_t b)148 static inline bool usb_target_same(usb_target_t a, usb_target_t b) 139 149 { 140 150 return (a.address == b.address) … … 142 152 } 143 153 144 /** General handle type. 145 * Used by various USB functions as opaque handle. 146 */ 147 typedef sysarg_t usb_handle_t; 154 static inline bool usb_transfer_type_is_valid(usb_transfer_type_t type) 155 { 156 return (type >= 0) && (type < USB_TRANSFER_COUNT); 157 } 158 159 static inline bool usb_direction_is_valid(usb_direction_t dir) 160 { 161 return (dir >= 0) && (dir < USB_DIRECTION_COUNT); 162 } 148 163 149 164 /** USB packet identifier. */ -
uspace/lib/usbhost/include/usb/host/bus.h
re7e1fd3 ra6afb4c 84 84 usb_speed_t speed; 85 85 usb_address_t address; 86 endpoint_t *endpoints [ 2 * USB_ENDPOINT_MAX];86 endpoint_t *endpoints [USB_ENDPOINT_COUNT]; 87 87 88 88 /* Managing bus */ -
uspace/lib/usbhost/src/bus.c
re7e1fd3 ra6afb4c 42 42 #include <errno.h> 43 43 #include <mem.h> 44 #include <macros.h> 44 45 #include <stdio.h> 45 46 #include <str_error.h> … … 345 346 * For different arguments, the result is stable but not defined. 346 347 */ 347 static int bus_endpoint_index(usb_endpoint_t ep, usb_direction_t dir)348 static size_t bus_endpoint_index(usb_endpoint_t ep, usb_direction_t dir) 348 349 { 349 350 return 2 * ep + (dir == USB_DIRECTION_OUT); … … 384 385 endpoint_add_ref(ep); 385 386 387 const size_t idx = bus_endpoint_index(ep->endpoint, ep->direction); 388 if (idx >= ARRAY_SIZE(device->endpoints)) { 389 usb_log_warning("Invalid endpoint description (ep no %u out of " 390 "bounds)", ep->endpoint); 391 goto drop; 392 } 393 386 394 if (ep->max_transfer_size == 0) { 387 395 usb_log_warning("Invalid endpoint description (mps %zu, " 388 396 "%u packets)", ep->max_packet_size, ep->packets_per_uframe); 389 /* Bus reference */ 390 endpoint_del_ref(ep); 391 return EINVAL; 397 goto drop; 392 398 } 393 399 … … 397 403 usb_str_direction(ep->direction), 398 404 ep->max_transfer_size); 399 400 const int idx = bus_endpoint_index(ep->endpoint, ep->direction);401 405 402 406 fibril_mutex_lock(&device->guard); … … 423 427 424 428 return EOK; 429 drop: 430 /* Bus reference */ 431 endpoint_del_ref(ep); 432 return EINVAL; 425 433 } 426 434 … … 428 436 * Search for an endpoint. Returns a reference. 429 437 */ 430 endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint, usb_direction_t dir) 438 endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint, 439 usb_direction_t dir) 431 440 { 432 441 assert(device); 433 442 434 const int idx = bus_endpoint_index(endpoint, dir); 435 const int ctrl_idx = bus_endpoint_index(endpoint, USB_DIRECTION_BOTH); 443 const size_t idx = bus_endpoint_index(endpoint, dir); 444 const size_t ctrl_idx = bus_endpoint_index(endpoint, USB_DIRECTION_BOTH); 445 446 endpoint_t *ep = NULL; 436 447 437 448 fibril_mutex_lock(&device->guard); 438 endpoint_t *ep = device->endpoints[idx]; 449 if (idx < ARRAY_SIZE(device->endpoints)) 450 ep = device->endpoints[idx]; 439 451 /* 440 452 * If the endpoint was not found, it's still possible it is a control 441 453 * endpoint having direction BOTH. 442 454 */ 443 if (!ep ) {455 if (!ep && ctrl_idx < ARRAY_SIZE(device->endpoints)) { 444 456 ep = device->endpoints[ctrl_idx]; 445 457 if (ep && ep->transfer_type != USB_TRANSFER_CONTROL) … … 478 490 ep->max_transfer_size); 479 491 480 const int idx = bus_endpoint_index(ep->endpoint, ep->direction); 492 const size_t idx = bus_endpoint_index(ep->endpoint, ep->direction); 493 494 if (idx >= ARRAY_SIZE(device->endpoints)) 495 return EINVAL; 481 496 482 497 fibril_mutex_lock(&device->guard); … … 495 510 496 511 /** 497 * Reserve the default address on the bus. Also, report the speed of the device 498 * that is listening on the default address. 499 * 500 * The speed is then used for devices enumerated while the address is reserved. 512 * Reserve the default address on the bus for the specified device (hub). 501 513 */ 502 514 int bus_reserve_default_address(bus_t *bus, device_t *dev) … … 564 576 assert(ep->device == device); 565 577 566 const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data, 567 on_complete, arg, name); 578 /* 579 * This method is already callable from HC only, so we can force these 580 * conditions harder. 581 * Invalid values from devices shall be caught on DDF interface already. 582 */ 583 assert(usb_target_is_valid(&target)); 584 assert(usb_direction_is_valid(direction)); 585 assert(direction != USB_DIRECTION_BOTH); 586 assert(size == 0 || data != NULL); 587 assert(arg == NULL || on_complete != NULL); 588 assert(name); 589 590 const int err = endpoint_send_batch(ep, target, direction, 591 data, size, setup_data, on_complete, arg, name); 568 592 569 593 /* Temporary reference */ … … 573 597 } 574 598 599 /** 600 * A structure to pass data from the completion callback to the caller. 601 */ 575 602 typedef struct { 576 603 fibril_mutex_t done_mtx; 577 604 fibril_condvar_t done_cv; 578 unsigneddone;605 bool done; 579 606 580 607 size_t transferred_size; … … 592 619 d->error = error; 593 620 fibril_mutex_lock(&d->done_mtx); 594 d->done = 1;621 d->done = true; 595 622 fibril_condvar_broadcast(&d->done_cv); 596 623 fibril_mutex_unlock(&d->done_mtx); … … 599 626 600 627 /** 601 * Issue a transfer on the bus, wait for result.628 * Issue a transfer on the bus, wait for the result. 602 629 * 603 630 * @param device Device for which to send the batch … … 613 640 const char *name) 614 641 { 615 sync_data_t sd = { .done = 0};642 sync_data_t sd = { .done = false }; 616 643 fibril_mutex_initialize(&sd.done_mtx); 617 644 fibril_condvar_initialize(&sd.done_cv); 618 645 619 646 const int ret = bus_device_send_batch(device, target, direction, 620 data, size, setup_data, 621 sync_transfer_complete, &sd, name); 647 data, size, setup_data, sync_transfer_complete, &sd, name); 622 648 if (ret != EOK) 623 649 return ret; 624 650 651 /* 652 * Note: There are requests that are completed synchronously. It is not 653 * therefore possible to just lock the mutex before and wait. 654 */ 625 655 fibril_mutex_lock(&sd.done_mtx); 626 while (!sd.done) {656 while (!sd.done) 627 657 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx); 628 }629 658 fibril_mutex_unlock(&sd.done_mtx); 630 659 -
uspace/lib/usbhost/src/ddf_helpers.c
re7e1fd3 ra6afb4c 154 154 int err; 155 155 156 if (!usb_speed_is_valid(speed)) 157 return EINVAL; 158 156 159 usb_log_debug("Hub %d reported a new %s speed device on port: %u", 157 160 hub->address, usb_str_speed(speed), port); … … 212 215 213 216 if (!victim) { 214 usb_log_warning("Hub '%s' tried to remove non-exist ant"217 usb_log_warning("Hub '%s' tried to remove non-existent" 215 218 " device.", ddf_fun_get_name(fun)); 216 219 return ENOENT; … … 271 274 target.address = dev->address; 272 275 276 if (!usb_target_is_valid(&target)) 277 return EINVAL; 278 279 if (size > 0 && data == NULL) 280 return EBADMEM; 281 282 if (!callback && arg) 283 return EBADMEM; 284 273 285 return bus_device_send_batch(dev, target, USB_DIRECTION_IN, 274 286 data, size, setup_data, … … 295 307 296 308 target.address = dev->address; 309 310 if (!usb_target_is_valid(&target)) 311 return EINVAL; 312 313 if (size > 0 && data == NULL) 314 return EBADMEM; 315 316 if (!callback && arg) 317 return EBADMEM; 297 318 298 319 return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
Note:
See TracChangeset
for help on using the changeset viewer.