Changeset a4e18e1 in mainline
- Timestamp:
- 2011-04-07T15:04:16Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 36cd378
- Parents:
- 9d06563 (diff), a82889e (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. - Files:
-
- 7 added
- 33 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
.bzrignore
r9d06563 ra4e18e1 93 93 ./uspace/drv/usbhub/usbhub 94 94 ./uspace/drv/usbhid/usbhid 95 ./uspace/drv/usbkbd/usbkbd 95 96 ./uspace/drv/usbmid/usbmid 96 97 ./uspace/drv/usbmouse/usbmouse -
uspace/drv/ohci/batch.c
r9d06563 ra4e18e1 73 73 CHECK_NULL_DISPOSE_RETURN(instance, 74 74 "Failed to allocate batch instance.\n"); 75 usb_transfer_batch_init(instance, target, transfer_type, speed, max_packet_size,76 buffer, NULL, buffer_size, NULL, setup_size, func_in,77 func_ out, arg, fun, NULL);75 usb_transfer_batch_init(instance, target, transfer_type, speed, 76 max_packet_size, buffer, NULL, buffer_size, NULL, setup_size, 77 func_in, func_out, arg, fun, NULL, NULL); 78 78 79 79 if (buffer_size > 0) { -
uspace/drv/ohci/hc.c
r9d06563 ra4e18e1 92 92 instance->ddf_instance = fun; 93 93 usb_device_keeper_init(&instance->manager); 94 ret = bandwidth_init(&instance->bandwidth, BANDWIDTH_AVAILABLE_USB11,95 bandwidth_count_usb11);96 CHECK_RET_RETURN(ret, "Failed to initialize bandwidth allocator: %s.\n",94 ret = usb_endpoint_manager_init(&instance->ep_manager, 95 BANDWIDTH_AVAILABLE_USB11); 96 CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n", 97 97 ret, str_error(ret)); 98 98 … … 183 183 * maintain reset for at least the time specified in USB spec (50 ms)*/ 184 184 async_usleep(50000); 185 186 /* turn off legacy emulation */ 187 volatile uint32_t *ohci_emulation_reg = 188 (uint32_t*)((char*)instance->registers + 0x100); 189 usb_log_info("OHCI legacy register status %p: %x.\n", 190 ohci_emulation_reg, *ohci_emulation_reg); 191 *ohci_emulation_reg = 0; 192 185 193 } 186 194 /*----------------------------------------------------------------------------*/ -
uspace/drv/ohci/hc.h
r9d06563 ra4e18e1 42 42 #include <usb/usb.h> 43 43 #include <usb/host/device_keeper.h> 44 #include <usb/host/ bandwidth.h>44 #include <usb/host/usb_endpoint_manager.h> 45 45 #include <usbhc_iface.h> 46 46 … … 48 48 #include "ohci_regs.h" 49 49 #include "root_hub.h" 50 #include "hw_struct/hcca.h" 50 51 51 52 typedef struct hc { … … 55 56 ddf_fun_t *ddf_instance; 56 57 usb_device_keeper_t manager; 57 bandwidth_t bandwidth;58 usb_endpoint_manager_t ep_manager; 58 59 fid_t interrupt_emulator; 59 60 } hc_t; -
uspace/drv/ohci/iface.c
r9d06563 ra4e18e1 162 162 address, endpoint, usb_str_transfer_type(transfer_type), 163 163 usb_str_speed(speed), direction, size, max_packet_size, interval); 164 return bandwidth_reserve(&hc->bandwidth, address, endpoint, direction,165 speed, transfer_type, max_packet_size, size, interval);164 // TODO use real endpoint here! 165 return usb_endpoint_manager_register_ep(&hc->ep_manager,NULL, 0); 166 166 } 167 167 /*----------------------------------------------------------------------------*/ … … 183 183 usb_log_debug("Unregister endpoint %d:%d %d.\n", 184 184 address, endpoint, direction); 185 return bandwidth_release(&hc->bandwidth, address, endpoint, direction); 186 187 return ENOTSUP; 185 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address, 186 endpoint, direction); 188 187 } 189 188 /*----------------------------------------------------------------------------*/ -
uspace/drv/pciintel/pci.c
r9d06563 ra4e18e1 107 107 } 108 108 109 size_t i; 110 for (i = 0; i < dev_data->hw_resources.count; i++) { 111 if (dev_data->hw_resources.resources[i].type == INTERRUPT) { 112 int irq = dev_data->hw_resources.resources[i].res.interrupt.irq; 113 int rc = async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq); 109 size_t i = 0; 110 hw_resource_list_t *res = &dev_data->hw_resources; 111 for (; i < res->count; i++) { 112 if (res->resources[i].type == INTERRUPT) { 113 const int irq = res->resources[i].res.interrupt.irq; 114 const int rc = 115 async_req_1_0(irc_phone, IRC_ENABLE_INTERRUPT, irq); 114 116 if (rc != EOK) { 115 117 async_hangup(irc_phone); -
uspace/drv/uhci-hcd/batch.c
r9d06563 ra4e18e1 49 49 td_t *tds; 50 50 size_t transfers; 51 usb_device_keeper_t *manager;52 51 } uhci_batch_t; 53 52 … … 73 72 * @param[in] func_out function to call on outbound transaction completion 74 73 * @param[in] arg additional parameter to func_in or func_out 75 * @param[in] manager Pointer totoggle management structure.74 * @param[in] ep Pointer to endpoint toggle management structure. 76 75 * @return Valid pointer if all substructures were successfully created, 77 76 * NULL otherwise. … … 86 85 char* setup_buffer, size_t setup_size, 87 86 usbhc_iface_transfer_in_callback_t func_in, 88 usbhc_iface_transfer_out_callback_t func_out, void *arg, 89 usb_device_keeper_t *manager 87 usbhc_iface_transfer_out_callback_t func_out, void *arg, endpoint_t *ep 90 88 ) 91 89 { … … 105 103 CHECK_NULL_DISPOSE_RETURN(instance, 106 104 "Failed to allocate batch instance.\n"); 107 usb_transfer_batch_init(instance, target, transfer_type, speed, max_packet_size, 105 usb_transfer_batch_init(instance, target, 106 transfer_type, speed, max_packet_size, 108 107 buffer, NULL, buffer_size, NULL, setup_size, func_in, 109 func_out, arg, fun, NULL);108 func_out, arg, fun, ep, NULL); 110 109 111 110 … … 114 113 "Failed to allocate batch instance.\n"); 115 114 bzero(data, sizeof(uhci_batch_t)); 116 data->manager = manager;117 115 instance->private_data = data; 118 116 … … 180 178 instance, i, data->tds[i].status); 181 179 td_print_status(&data->tds[i]); 182 183 usb_device_keeper_set_toggle(data->manager, 184 instance->target, instance->direction, 185 td_toggle(&data->tds[i])); 180 if (instance->ep != NULL) 181 endpoint_toggle_set(instance->ep, 182 td_toggle(&data->tds[i])); 186 183 if (i > 0) 187 184 goto substract_ret; … … 310 307 311 308 const bool low_speed = instance->speed == USB_SPEED_LOW; 312 int toggle = usb_device_keeper_get_toggle( 313 data->manager, instance->target, instance->direction); 309 int toggle = endpoint_toggle_get(instance->ep); 314 310 assert(toggle == 0 || toggle == 1); 315 311 … … 342 338 } 343 339 td_set_ioc(&data->tds[transfer - 1]); 344 usb_device_keeper_set_toggle(data->manager, instance->target, 345 instance->direction, toggle); 340 endpoint_toggle_set(instance->ep, toggle); 346 341 } 347 342 /*----------------------------------------------------------------------------*/ -
uspace/drv/uhci-hcd/batch.h
r9d06563 ra4e18e1 35 35 #define DRV_UHCI_BATCH_H 36 36 37 #include <adt/list.h>38 39 37 #include <usbhc_iface.h> 40 38 #include <usb/usb.h> 41 39 #include <usb/host/device_keeper.h> 40 #include <usb/host/endpoint.h> 42 41 #include <usb/host/batch.h> 43 42 … … 57 56 usbhc_iface_transfer_out_callback_t func_out, 58 57 void *arg, 59 usb_device_keeper_t *manager58 endpoint_t *ep 60 59 ); 61 60 -
uspace/drv/uhci-hcd/hc.c
r9d06563 ra4e18e1 66 66 static int hc_interrupt_emulator(void *arg); 67 67 static int hc_debug_checker(void *arg); 68 68 #if 0 69 69 static bool usb_is_allowed( 70 70 bool low_speed, usb_transfer_type_t transfer, size_t size); 71 #endif 71 72 /*----------------------------------------------------------------------------*/ 72 73 /** Initialize UHCI hcd driver structure … … 239 240 usb_log_debug("Initialized device manager.\n"); 240 241 241 ret = bandwidth_init(&instance->bandwidth, BANDWIDTH_AVAILABLE_USB11, 242 bandwidth_count_usb11); 242 ret = 243 usb_endpoint_manager_init(&instance->ep_manager, 244 BANDWIDTH_AVAILABLE_USB11); 243 245 assert(ret == EOK); 244 246 … … 326 328 assert(instance); 327 329 assert(batch); 328 const int low_speed = (batch->speed == USB_SPEED_LOW);329 if (!usb_is_allowed(330 low_speed, batch->transfer_type, batch->max_packet_size)) {331 usb_log_error("Invalid USB transfer specified %s %d %zu.\n",332 usb_str_speed(batch->speed), batch->transfer_type,333 batch->max_packet_size);334 return ENOTSUP;335 }336 /* Check available bandwidth */337 if (batch->transfer_type == USB_TRANSFER_INTERRUPT ||338 batch->transfer_type == USB_TRANSFER_ISOCHRONOUS) {339 int ret =340 bandwidth_use(&instance->bandwidth, batch->target.address,341 batch->target.endpoint, batch->direction);342 if (ret != EOK) {343 usb_log_warning("Failed(%d) to use reserved bw: %s.\n",344 ret, str_error(ret));345 }346 }347 330 348 331 transfer_list_t *list = … … 398 381 case USB_TRANSFER_INTERRUPT: 399 382 case USB_TRANSFER_ISOCHRONOUS: { 383 /* 400 384 int ret = bandwidth_free(&instance->bandwidth, 401 385 batch->target.address, … … 406 390 "reserved bw: %s.\n", ret, 407 391 str_error(ret)); 392 */ 408 393 } 409 394 default: … … 529 514 * @return True if transaction is allowed by USB specs, false otherwise 530 515 */ 516 #if 0 531 517 bool usb_is_allowed( 532 518 bool low_speed, usb_transfer_type_t transfer, size_t size) … … 546 532 return false; 547 533 } 534 #endif 548 535 /** 549 536 * @} -
uspace/drv/uhci-hcd/hc.h
r9d06563 ra4e18e1 43 43 #include <usbhc_iface.h> 44 44 #include <usb/host/device_keeper.h> 45 #include <usb/host/ bandwidth.h>45 #include <usb/host/usb_endpoint_manager.h> 46 46 47 47 #include "batch.h" … … 85 85 typedef struct hc { 86 86 usb_device_keeper_t manager; 87 bandwidth_t bandwidth;87 usb_endpoint_manager_t ep_manager; 88 88 89 89 regs_t *registers; -
uspace/drv/uhci-hcd/hw_struct/queue_head.h
r9d06563 ra4e18e1 39 39 40 40 #include "link_pointer.h" 41 #include "utils/malloc32.h"42 41 43 42 typedef struct queue_head { -
uspace/drv/uhci-hcd/iface.c
r9d06563 ra4e18e1 36 36 37 37 #include <usb/debug.h> 38 #include <usb/host/endpoint.h> 38 39 39 40 #include "iface.h" … … 54 55 usb_device_keeper_reserve_default_address(&hc->manager, speed); 55 56 return EOK; 57 #if 0 58 endpoint_t *ep = malloc(sizeof(endpoint_t)); 59 if (ep == NULL) 60 return ENOMEM; 61 const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64; 62 endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size); 63 int ret; 64 try_retgister: 65 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, 66 USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0); 67 if (ret == EEXISTS) { 68 async_usleep(1000); 69 goto try_retgister; 70 } 71 if (ret != EOK) { 72 endpoint_destroy(ep); 73 } 74 return ret; 75 #endif 56 76 } 57 77 /*----------------------------------------------------------------------------*/ … … 67 87 assert(hc); 68 88 usb_log_debug("Default address release.\n"); 89 // return usb_endpoint_manager_unregister_ep(&hc->ep_manager, 90 // USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH); 69 91 usb_device_keeper_release_default_address(&hc->manager); 70 92 return EOK; … … 137 159 const usb_speed_t speed = 138 160 usb_device_keeper_get_speed(&hc->manager, address); 139 size_t size = max_packet_size; 161 const size_t size = 162 (transfer_type == USB_TRANSFER_INTERRUPT 163 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ? 164 max_packet_size : 0; 165 int ret; 166 167 endpoint_t *ep = malloc(sizeof(endpoint_t)); 168 if (ep == NULL) 169 return ENOMEM; 170 ret = endpoint_init(ep, address, endpoint, direction, 171 transfer_type, speed, max_packet_size); 172 if (ret != EOK) { 173 free(ep); 174 return ret; 175 } 140 176 141 177 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n", 142 178 address, endpoint, usb_str_transfer_type(transfer_type), 143 179 usb_str_speed(speed), direction, size, max_packet_size, interval); 144 return bandwidth_reserve(&hc->bandwidth, address, endpoint, direction, 145 speed, transfer_type, max_packet_size, size, interval); 180 181 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size); 182 if (ret != EOK) { 183 endpoint_destroy(ep); 184 } else { 185 usb_device_keeper_add_ep(&hc->manager, address, ep); 186 } 187 return ret; 146 188 } 147 189 /*----------------------------------------------------------------------------*/ … … 154 196 usb_log_debug("Unregister endpoint %d:%d %d.\n", 155 197 address, endpoint, direction); 156 return bandwidth_release(&hc->bandwidth, address, endpoint, direction); 198 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address, 199 endpoint, direction); 157 200 } 158 201 /*----------------------------------------------------------------------------*/ … … 175 218 hc_t *hc = fun_to_hc(fun); 176 219 assert(hc); 177 usb_speed_t speed =178 usb_device_keeper_get_speed(&hc->manager, target.address);179 220 180 221 usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n", 181 222 target.address, target.endpoint, size, max_packet_size); 182 223 224 size_t res_bw; 225 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager, 226 target.address, target.endpoint, USB_DIRECTION_OUT, &res_bw); 227 if (ep == NULL) { 228 usb_log_error("Endpoint(%d:%d) not registered for INT OUT.\n", 229 target.address, target.endpoint); 230 return ENOENT; 231 } 232 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type, 233 size, ep->max_packet_size); 234 if (res_bw < bw) 235 { 236 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw " 237 "but only %zu is reserved.\n", 238 target.address, target.endpoint, bw, res_bw); 239 return ENOENT; 240 } 241 assert(ep->speed == 242 usb_device_keeper_get_speed(&hc->manager, target.address)); 243 assert(ep->max_packet_size == max_packet_size); 244 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT); 245 183 246 usb_transfer_batch_t *batch = 184 batch_get(fun, target, USB_TRANSFER_INTERRUPT,max_packet_size,185 speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);247 batch_get(fun, target, ep->transfer_type, ep->max_packet_size, 248 ep->speed, data, size, NULL, 0, NULL, callback, arg, ep); 186 249 if (!batch) 187 250 return ENOMEM; … … 212 275 hc_t *hc = fun_to_hc(fun); 213 276 assert(hc); 214 usb_speed_t speed = 215 usb_device_keeper_get_speed(&hc->manager, target.address); 277 216 278 usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n", 217 279 target.address, target.endpoint, size, max_packet_size); 218 280 281 size_t res_bw; 282 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager, 283 target.address, target.endpoint, USB_DIRECTION_IN, &res_bw); 284 if (ep == NULL) { 285 usb_log_error("Endpoint(%d:%d) not registered for INT IN.\n", 286 target.address, target.endpoint); 287 return ENOENT; 288 } 289 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type, 290 size, ep->max_packet_size); 291 if (res_bw < bw) 292 { 293 usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw " 294 "but only %zu bw is reserved.\n", 295 target.address, target.endpoint, bw, res_bw); 296 return ENOENT; 297 } 298 299 assert(ep->speed == 300 usb_device_keeper_get_speed(&hc->manager, target.address)); 301 assert(ep->max_packet_size == max_packet_size); 302 assert(ep->transfer_type == USB_TRANSFER_INTERRUPT); 303 219 304 usb_transfer_batch_t *batch = 220 batch_get(fun, target, USB_TRANSFER_INTERRUPT,max_packet_size,221 speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);305 batch_get(fun, target, ep->transfer_type, ep->max_packet_size, 306 ep->speed, data, size, NULL, 0, callback, NULL, arg, ep); 222 307 if (!batch) 223 308 return ENOMEM; … … 248 333 hc_t *hc = fun_to_hc(fun); 249 334 assert(hc); 250 usb_speed_t speed =251 usb_device_keeper_get_speed(&hc->manager, target.address);252 335 253 336 usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n", 254 337 target.address, target.endpoint, size, max_packet_size); 255 338 339 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager, 340 target.address, target.endpoint, USB_DIRECTION_OUT, NULL); 341 if (ep == NULL) { 342 usb_log_error("Endpoint(%d:%d) not registered for BULK OUT.\n", 343 target.address, target.endpoint); 344 return ENOENT; 345 } 346 assert(ep->speed == 347 usb_device_keeper_get_speed(&hc->manager, target.address)); 348 assert(ep->max_packet_size == max_packet_size); 349 assert(ep->transfer_type == USB_TRANSFER_BULK); 350 256 351 usb_transfer_batch_t *batch = 257 batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,258 data, size, NULL, 0, NULL, callback, arg, &hc->manager);352 batch_get(fun, target, ep->transfer_type, ep->max_packet_size, 353 ep->speed, data, size, NULL, 0, NULL, callback, arg, ep); 259 354 if (!batch) 260 355 return ENOMEM; … … 285 380 hc_t *hc = fun_to_hc(fun); 286 381 assert(hc); 287 usb_speed_t speed =288 usb_device_keeper_get_speed(&hc->manager, target.address);289 382 usb_log_debug("Bulk IN %d:%d %zu(%zu).\n", 290 383 target.address, target.endpoint, size, max_packet_size); 291 384 385 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager, 386 target.address, target.endpoint, USB_DIRECTION_IN, NULL); 387 if (ep == NULL) { 388 usb_log_error("Endpoint(%d:%d) not registered for BULK IN.\n", 389 target.address, target.endpoint); 390 return ENOENT; 391 } 392 assert(ep->speed == 393 usb_device_keeper_get_speed(&hc->manager, target.address)); 394 assert(ep->max_packet_size == max_packet_size); 395 assert(ep->transfer_type == USB_TRANSFER_BULK); 396 292 397 usb_transfer_batch_t *batch = 293 batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,294 data, size, NULL, 0, callback, NULL, arg, &hc->manager);398 batch_get(fun, target, ep->transfer_type, ep->max_packet_size, 399 ep->speed, data, size, NULL, 0, callback, NULL, arg, ep); 295 400 if (!batch) 296 401 return ENOMEM; … … 328 433 usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n", 329 434 speed, target.address, target.endpoint, size, max_packet_size); 435 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager, 436 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL); 437 if (ep == NULL) { 438 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n", 439 target.address, target.endpoint); 440 } 330 441 331 442 if (setup_size != 8) … … 334 445 usb_transfer_batch_t *batch = 335 446 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed, 336 data, size, setup_data, setup_size, NULL, callback, arg, 337 &hc->manager); 447 data, size, setup_data, setup_size, NULL, callback, arg, ep); 338 448 if (!batch) 339 449 return ENOMEM; … … 373 483 usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n", 374 484 speed, target.address, target.endpoint, size, max_packet_size); 485 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager, 486 target.address, target.endpoint, USB_DIRECTION_BOTH, NULL); 487 if (ep == NULL) { 488 usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n", 489 target.address, target.endpoint); 490 } 375 491 usb_transfer_batch_t *batch = 376 492 batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed, 377 data, size, setup_data, setup_size, callback, NULL, arg, 378 &hc->manager); 493 data, size, setup_data, setup_size, callback, NULL, arg, ep); 379 494 if (!batch) 380 495 return ENOMEM; -
uspace/drv/uhci-hcd/transfer_list.h
r9d06563 ra4e18e1 39 39 #include "batch.h" 40 40 #include "hw_struct/queue_head.h" 41 #include "utils/malloc32.h" 41 42 42 43 typedef struct transfer_list -
uspace/drv/uhci-hcd/utils/malloc32.h
r9d06563 ra4e18e1 36 36 37 37 #include <assert.h> 38 #include <errno.h> 38 39 #include <malloc.h> 39 40 #include <mem.h> -
uspace/drv/uhci-rhd/main.c
r9d06563 ra4e18e1 44 44 45 45 #define NAME "uhci-rhd" 46 46 47 static int hc_get_my_registers(ddf_dev_t *dev, 47 48 uintptr_t *io_reg_address, size_t *io_reg_size); 48 #if 049 49 /*----------------------------------------------------------------------------*/ 50 static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle) 51 { 52 assert(fun); 53 assert(fun->driver_data); 54 assert(handle); 55 56 *handle = ((uhci_root_hub_t*)fun->driver_data)->hc_handle; 57 58 return EOK; 59 } 60 /*----------------------------------------------------------------------------*/ 61 static usb_iface_t uhci_rh_usb_iface = { 62 .get_hc_handle = usb_iface_get_hc_handle, 63 .get_address = usb_iface_get_address_hub_impl 64 }; 65 /*----------------------------------------------------------------------------*/ 66 static ddf_dev_ops_t uhci_rh_ops = { 67 .interfaces[USB_DEV_IFACE] = &uhci_rh_usb_iface, 68 }; 69 #endif 70 /*----------------------------------------------------------------------------*/ 71 /** Initialize a new ddf driver instance of UHCI root hub. 72 * 73 * @param[in] device DDF instance of the device to initialize. 74 * @return Error code. 75 */ 76 static int uhci_rh_add_device(ddf_dev_t *device) 77 { 78 if (!device) 79 return ENOTSUP; 80 81 usb_log_debug2("%s called device %d\n", __FUNCTION__, device->handle); 82 83 //device->ops = &uhci_rh_ops; 84 uintptr_t io_regs = 0; 85 size_t io_size = 0; 86 87 int ret = hc_get_my_registers(device, &io_regs, &io_size); 88 if (ret != EOK) { 89 usb_log_error("Failed to get registers from parent HC: %s.\n", 90 str_error(ret)); 91 } 92 usb_log_debug("I/O regs at %#X (size %zu).\n", io_regs, io_size); 93 94 uhci_root_hub_t *rh = malloc(sizeof(uhci_root_hub_t)); 95 if (!rh) { 96 usb_log_error("Failed to allocate driver instance.\n"); 97 return ENOMEM; 98 } 99 100 ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device); 101 if (ret != EOK) { 102 usb_log_error("Failed to initialize driver instance: %s.\n", 103 str_error(ret)); 104 free(rh); 105 return ret; 106 } 107 108 device->driver_data = rh; 109 usb_log_info("Controlling root hub `%s' (%llu).\n", 110 device->name, device->handle); 111 return EOK; 112 } 50 static int uhci_rh_add_device(ddf_dev_t *device); 113 51 /*----------------------------------------------------------------------------*/ 114 52 static driver_ops_t uhci_rh_driver_ops = { … … 132 70 { 133 71 printf(NAME ": HelenOS UHCI root hub driver.\n"); 72 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME); 73 return ddf_driver_main(&uhci_rh_driver); 74 } 75 /*----------------------------------------------------------------------------*/ 76 /** Initialize a new ddf driver instance of UHCI root hub. 77 * 78 * @param[in] device DDF instance of the device to initialize. 79 * @return Error code. 80 */ 81 static int uhci_rh_add_device(ddf_dev_t *device) 82 { 83 if (!device) 84 return EINVAL; 134 85 135 usb_log_ enable(USB_LOG_LEVEL_DEFAULT, NAME);86 usb_log_debug2("%s called device %d\n", __FUNCTION__, device->handle); 136 87 137 return ddf_driver_main(&uhci_rh_driver); 88 uintptr_t io_regs = 0; 89 size_t io_size = 0; 90 uhci_root_hub_t *rh = NULL; 91 int ret = EOK; 92 93 #define CHECK_RET_FREE_RH_RETURN(ret, message...) \ 94 if (ret != EOK) { \ 95 usb_log_error(message); \ 96 if (rh) \ 97 free(rh); \ 98 return ret; \ 99 } else (void)0 100 101 ret = hc_get_my_registers(device, &io_regs, &io_size); 102 CHECK_RET_FREE_RH_RETURN(ret, 103 "Failed(%d) to get registers from HC: %s.\n", ret, str_error(ret)); 104 usb_log_debug("I/O regs at %#x (size %zu).\n", io_regs, io_size); 105 106 rh = malloc(sizeof(uhci_root_hub_t)); 107 ret = (rh == NULL) ? ENOMEM : EOK; 108 CHECK_RET_FREE_RH_RETURN(ret, 109 "Failed to allocate rh driver instance.\n"); 110 111 ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device); 112 CHECK_RET_FREE_RH_RETURN(ret, 113 "Failed(%d) to initialize rh driver instance: %s.\n", 114 ret, str_error(ret)); 115 116 device->driver_data = rh; 117 usb_log_info("Controlling root hub '%s' (%llu).\n", 118 device->name, device->handle); 119 return EOK; 138 120 } 139 121 /*----------------------------------------------------------------------------*/ … … 156 138 } 157 139 158 int rc;159 160 140 hw_resource_list_t hw_resources; 161 rc = hw_res_get_resource_list(parent_phone, &hw_resources); 162 if (rc != EOK) { 163 goto leave; 141 int ret = hw_res_get_resource_list(parent_phone, &hw_resources); 142 if (ret != EOK) { 143 async_hangup(parent_phone); 144 return ret; 164 145 } 165 146 … … 168 149 bool io_found = false; 169 150 170 size_t i ;171 for ( i = 0; i < hw_resources.count; i++) {151 size_t i = 0; 152 for (; i < hw_resources.count; i++) { 172 153 hw_resource_t *res = &hw_resources.resources[i]; 173 switch (res->type) 174 { 175 case IO_RANGE: 176 io_address = (uintptr_t) res->res.io_range.address; 154 if (res->type == IO_RANGE) { 155 io_address = res->res.io_range.address; 177 156 io_size = res->res.io_range.size; 178 157 io_found = true; 179 180 default:181 break;182 158 } 183 159 } 160 async_hangup(parent_phone); 184 161 185 162 if (!io_found) { 186 rc = ENOENT; 187 goto leave; 163 return ENOENT; 188 164 } 189 190 165 if (io_reg_address != NULL) { 191 166 *io_reg_address = io_address; … … 194 169 *io_reg_size = io_size; 195 170 } 196 rc = EOK; 197 198 leave: 199 async_hangup(parent_phone); 200 return rc; 171 return EOK; 201 172 } 202 173 /** -
uspace/drv/uhci-rhd/port.c
r9d06563 ra4e18e1 43 43 #include "port.h" 44 44 45 static int uhci_port_check(void *port); 46 static int uhci_port_reset_enable(int portno, void *arg); 45 47 static int uhci_port_new_device(uhci_port_t *port, usb_speed_t speed); 46 48 static int uhci_port_remove_device(uhci_port_t *port); 47 49 static int uhci_port_set_enabled(uhci_port_t *port, bool enabled); 48 static int uhci_port_check(void *port);49 static int uhci_port_reset_enable(int portno, void *arg);50 50 static void uhci_port_print_status( 51 51 uhci_port_t *port, const port_status_t value); … … 74 74 pio_write_16(port->address, value); 75 75 } 76 77 76 /*----------------------------------------------------------------------------*/ 78 77 /** Initialize UHCI root hub port instance. … … 259 258 260 259 usb_address_t dev_addr; 261 int r c= usb_hc_new_device_wrapper(port->rh, &port->hc_connection,260 int ret = usb_hc_new_device_wrapper(port->rh, &port->hc_connection, 262 261 speed, uhci_port_reset_enable, port->number, port, 263 262 &dev_addr, &port->attached_device, NULL, NULL, NULL); 264 263 265 if (r c!= EOK) {264 if (ret != EOK) { 266 265 usb_log_error("%s: Failed(%d) to add device: %s.\n", 267 port->id_string, r c, str_error(rc));266 port->id_string, ret, str_error(ret)); 268 267 uhci_port_set_enabled(port, false); 269 return r c;268 return ret; 270 269 } 271 270 … … 287 286 int uhci_port_remove_device(uhci_port_t *port) 288 287 { 289 usb_log_error("%s: Don't know how to remove device % d.\n",290 port->id_string, (unsigned int)port->attached_device);291 return E OK;288 usb_log_error("%s: Don't know how to remove device %llu.\n", 289 port->id_string, port->attached_device); 290 return ENOTSUP; 292 291 } 293 292 /*----------------------------------------------------------------------------*/ … … 341 340 (value & STATUS_CONNECTED_CHANGED) ? " CONNECTED-CHANGE," : "", 342 341 (value & STATUS_CONNECTED) ? " CONNECTED," : "", 343 (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERR OR: NO ALWAYS ONE"342 (value & STATUS_ALWAYS_ONE) ? " ALWAYS ONE" : " ERR: NO ALWAYS ONE" 344 343 ); 345 344 } -
uspace/drv/uhci-rhd/root_hub.c
r9d06563 ra4e18e1 51 51 assert(instance); 52 52 assert(rh); 53 int ret;54 53 55 54 /* Allow access to root hub port registers */ 56 55 assert(sizeof(port_status_t) * UHCI_ROOT_HUB_PORT_COUNT <= size); 57 56 port_status_t *regs; 58 ret = pio_enable(addr, size, (void**)®s);57 int ret = pio_enable(addr, size, (void**)®s); 59 58 if (ret < 0) { 60 59 usb_log_error( … … 84 83 * 85 84 * @param[in] instance Root hub structure to use. 86 * @return Error code.87 85 */ 88 intuhci_root_hub_fini(uhci_root_hub_t* instance)86 void uhci_root_hub_fini(uhci_root_hub_t* instance) 89 87 { 90 88 assert(instance); … … 93 91 uhci_port_fini(&instance->ports[i]); 94 92 } 95 return EOK;96 93 } 97 94 /*----------------------------------------------------------------------------*/ -
uspace/drv/uhci-rhd/root_hub.h
r9d06563 ra4e18e1 50 50 uhci_root_hub_t *instance, void *addr, size_t size, ddf_dev_t *rh); 51 51 52 intuhci_root_hub_fini(uhci_root_hub_t *instance);52 void uhci_root_hub_fini(uhci_root_hub_t *instance); 53 53 #endif 54 54 /** -
uspace/drv/usbmid/explore.c
r9d06563 ra4e18e1 48 48 }; 49 49 50 /** Find starting indexes of all interface descriptors in a configuration. 51 * 52 * @param config_descriptor Full configuration descriptor. 53 * @param config_descriptor_size Size of @p config_descriptor in bytes. 54 * @param interface_positions Array where to store indexes of interfaces. 55 * @param interface_count Size of @p interface_positions array. 56 * @return Number of found interfaces. 57 * @retval (size_t)-1 Error occured. 58 */ 59 static size_t find_interface_descriptors(uint8_t *config_descriptor, 60 size_t config_descriptor_size, 61 size_t *interface_positions, size_t interface_count) 50 /** Tell whether given interface is already in the list. 51 * 52 * @param list List of usbmid_interface_t members to be searched. 53 * @param interface_no Interface number caller is looking for. 54 * @return Interface @p interface_no is already present in the list. 55 */ 56 static bool interface_in_list(link_t *list, int interface_no) 62 57 { 63 if (interface_count == 0) { 64 return (size_t) -1; 65 } 66 58 link_t *l; 59 for (l = list->next; l != list; l = l->next) { 60 usbmid_interface_t *iface 61 = list_get_instance(l, usbmid_interface_t, link); 62 if (iface->interface_no == interface_no) { 63 return true; 64 } 65 } 66 67 return false; 68 } 69 70 /** Create list of interfaces from configuration descriptor. 71 * 72 * @param config_descriptor Configuration descriptor. 73 * @param config_descriptor_size Size of configuration descriptor in bytes. 74 * @param list List where to add the interfaces. 75 */ 76 static void create_interfaces(uint8_t *config_descriptor, 77 size_t config_descriptor_size, link_t *list) 78 { 67 79 usb_dp_parser_data_t data = { 68 80 .data = config_descriptor, … … 75 87 }; 76 88 77 uint8_t *interface = usb_dp_get_nested_descriptor(&parser, &data,89 uint8_t *interface_ptr = usb_dp_get_nested_descriptor(&parser, &data, 78 90 data.data); 79 if (interface == NULL) { 80 return (size_t) -1; 81 } 82 if (interface[1] != USB_DESCTYPE_INTERFACE) { 83 return (size_t) -1; 84 } 85 86 size_t found_interfaces = 0; 87 interface_positions[found_interfaces] = interface - config_descriptor; 88 found_interfaces++; 89 90 while (interface != NULL) { 91 interface = usb_dp_get_sibling_descriptor(&parser, &data, 92 data.data, interface); 93 if ((interface != NULL) 94 && (found_interfaces < interface_count) 95 && (interface[1] == USB_DESCTYPE_INTERFACE)) { 96 interface_positions[found_interfaces] 97 = interface - config_descriptor; 98 found_interfaces++; 99 } 100 } 101 102 return found_interfaces; 91 if (interface_ptr == NULL) { 92 return; 93 } 94 95 do { 96 if (interface_ptr[1] != USB_DESCTYPE_INTERFACE) { 97 goto next_descriptor; 98 } 99 100 usb_standard_interface_descriptor_t *interface 101 = (usb_standard_interface_descriptor_t *) interface_ptr; 102 103 /* Skip alternate interfaces. */ 104 if (!interface_in_list(list, interface->interface_number)) { 105 usbmid_interface_t *iface 106 = malloc(sizeof(usbmid_interface_t)); 107 if (iface == NULL) { 108 break; 109 } 110 link_initialize(&iface->link); 111 iface->fun = NULL; 112 iface->interface_no = interface->interface_number; 113 iface->interface = interface; 114 115 list_append(&iface->link, list); 116 } 117 118 /* TODO: add the alternatives and create match ids from them 119 * as well. 120 */ 121 122 next_descriptor: 123 interface_ptr = usb_dp_get_sibling_descriptor(&parser, &data, 124 data.data, interface_ptr); 125 126 } while (interface_ptr != NULL); 127 103 128 } 104 129 … … 130 155 (usb_standard_configuration_descriptor_t *) config_descriptor_raw; 131 156 132 size_t *interface_descriptors133 = malloc(sizeof(size_t) * config_descriptor->interface_count);134 if (interface_descriptors == NULL) {135 usb_log_error("Out of memory (wanted %zuB).\n",136 sizeof(size_t) * config_descriptor->interface_count);137 free(config_descriptor_raw);138 return false;139 }140 size_t interface_descriptors_count141 = find_interface_descriptors(142 config_descriptor_raw, config_descriptor_size,143 interface_descriptors, config_descriptor->interface_count);144 145 if (interface_descriptors_count == (size_t) -1) {146 usb_log_error("Problem parsing configuration descriptor.\n");147 free(interface_descriptors);148 return false;149 }150 151 157 /* Select the first configuration */ 152 158 rc = usb_request_set_configuration(&dev->ctrl_pipe, … … 155 161 usb_log_error("Failed to set device configuration: %s.\n", 156 162 str_error(rc)); 157 free(interface_descriptors); 158 return false; 159 } 160 163 return false; 164 } 161 165 162 166 /* Create control function */ … … 164 168 if (ctl_fun == NULL) { 165 169 usb_log_error("Failed to create control function.\n"); 166 free(interface_descriptors);167 170 return false; 168 171 } … … 174 177 usb_log_error("Failed to bind control function: %s.\n", 175 178 str_error(rc)); 176 free(interface_descriptors); 177 return false; 178 } 179 180 /* Spawn interface children */ 181 size_t i; 182 for (i = 0; i < interface_descriptors_count; i++) { 183 usb_standard_interface_descriptor_t *interface 184 = (usb_standard_interface_descriptor_t *) 185 (config_descriptor_raw + interface_descriptors[i]); 186 usb_log_debug2("Interface descriptor at index %zu (type %d).\n", 187 interface_descriptors[i], (int) interface->descriptor_type); 179 return false; 180 } 181 182 /* Create interface children. */ 183 link_t interface_list; 184 list_initialize(&interface_list); 185 create_interfaces(config_descriptor_raw, config_descriptor_size, 186 &interface_list); 187 188 link_t *link; 189 for (link = interface_list.next; link != &interface_list; 190 link = link->next) { 191 usbmid_interface_t *iface = list_get_instance(link, 192 usbmid_interface_t, link); 193 188 194 usb_log_info("Creating child for interface %d (%s).\n", 189 (int) interface->interface_number, 190 usb_str_class(interface->interface_class)); 191 rc = usbmid_spawn_interface_child(dev, &dev->descriptors.device, 192 interface); 195 (int) iface->interface_no, 196 usb_str_class(iface->interface->interface_class)); 197 198 rc = usbmid_spawn_interface_child(dev, iface, 199 &dev->descriptors.device, iface->interface); 193 200 if (rc != EOK) { 194 201 usb_log_error("Failed to create interface child: %s.\n", -
uspace/drv/usbmid/usbmid.c
r9d06563 ra4e18e1 79 79 }; 80 80 81 /** Create new interface for USB MID device.82 *83 * @param fun Backing generic DDF device function (representing interface).84 * @param iface_no Interface number.85 * @return New interface.86 * @retval NULL Error occured.87 */88 usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)89 {90 usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));91 if (iface == NULL) {92 usb_log_error("Out of memory (wanted %zuB).\n",93 sizeof(usbmid_interface_t));94 return NULL;95 }96 97 iface->fun = fun;98 iface->interface_no = iface_no;99 100 return iface;101 }102 103 81 104 82 /** Spawn new child device from one interface. 105 83 * 106 84 * @param parent Parent MID device. 85 * @param iface Interface information. 107 86 * @param device_descriptor Device descriptor. 108 87 * @param interface_descriptor Interface descriptor. … … 110 89 */ 111 90 int usbmid_spawn_interface_child(usb_device_t *parent, 91 usbmid_interface_t *iface, 112 92 const usb_standard_device_descriptor_t *device_descriptor, 113 93 const usb_standard_interface_descriptor_t *interface_descriptor) … … 115 95 ddf_fun_t *child = NULL; 116 96 char *child_name = NULL; 117 usbmid_interface_t *child_as_interface = NULL;118 97 int rc; 119 98 … … 137 116 } 138 117 118 iface->fun = child; 139 119 140 141 child_as_interface = usbmid_interface_create(child, 142 (int) interface_descriptor->interface_number); 143 if (child_as_interface == NULL) { 144 rc = ENOMEM; 145 goto error_leave; 146 } 147 148 child->driver_data = child_as_interface; 120 child->driver_data = iface; 149 121 child->ops = &child_device_ops; 150 122 … … 172 144 free(child_name); 173 145 } 174 if (child_as_interface != NULL) {175 free(child_as_interface);176 }177 146 178 147 return rc; -
uspace/drv/usbmid/usbmid.h
r9d06563 ra4e18e1 37 37 #define USBMID_H_ 38 38 39 #include <adt/list.h> 39 40 #include <ddf/driver.h> 40 41 #include <usb/usb.h> … … 49 50 /** Function container. */ 50 51 ddf_fun_t *fun; 51 52 /** Interface descriptor. */ 53 usb_standard_interface_descriptor_t *interface; 52 54 /** Interface number. */ 53 55 int interface_no; 56 /** List link. */ 57 link_t link; 54 58 } usbmid_interface_t; 55 59 56 usbmid_interface_t *usbmid_interface_create(ddf_fun_t *, int);57 60 bool usbmid_explore_device(usb_device_t *); 58 int usbmid_spawn_interface_child(usb_device_t *, 61 int usbmid_spawn_interface_child(usb_device_t *, usbmid_interface_t *, 59 62 const usb_standard_device_descriptor_t *, 60 63 const usb_standard_interface_descriptor_t *); -
uspace/lib/usb/Makefile
r9d06563 ra4e18e1 54 54 src/host/device_keeper.c \ 55 55 src/host/batch.c \ 56 src/host/bandwidth.c 56 src/host/endpoint.c \ 57 src/host/usb_endpoint_manager.c 57 58 58 59 include $(USPACE_PREFIX)/Makefile.common -
uspace/lib/usb/include/usb/devdrv.h
r9d06563 ra4e18e1 47 47 } usb_device_descriptors_t; 48 48 49 /** Wrapper for data related to alternate interface setting. 50 * The pointers will typically point inside configuration descriptor and 51 * thus you shall not deallocate them. 52 */ 53 typedef struct { 54 /** Interface descriptor. */ 55 usb_standard_interface_descriptor_t *interface; 56 /** Pointer to start of descriptor tree bound with this interface. */ 57 uint8_t *nested_descriptors; 58 /** Size of data pointed by nested_descriptors in bytes. */ 59 size_t nested_descriptors_size; 60 } usb_alternate_interface_descriptors_t; 61 62 /** Alternate interface settings. */ 63 typedef struct { 64 /** Array of alternate interfaces descriptions. */ 65 usb_alternate_interface_descriptors_t *alternatives; 66 /** Size of @c alternatives array. */ 67 size_t alternative_count; 68 /** Index of currently selected one. */ 69 size_t current; 70 } usb_alternate_interfaces_t; 71 49 72 /** USB device structure. */ 50 73 typedef struct { … … 56 79 */ 57 80 usb_endpoint_mapping_t *pipes; 81 /** Number of other endpoint pipes. */ 82 size_t pipes_count; 58 83 /** Current interface. 59 84 * Usually, drivers operate on single interface only. … … 61 86 */ 62 87 int interface_no; 88 89 /** Alternative interfaces. 90 * Set to NULL when the driver controls whole device 91 * (i.e. more (or any) interfaces). 92 */ 93 usb_alternate_interfaces_t *alternate_interfaces; 63 94 64 95 /** Some useful descriptors. */ … … 92 123 */ 93 124 const char *name; 94 /** Expected endpoints description, excluding default control endpoint. 125 /** Expected endpoints description. 126 * This description shall exclude default control endpoint (pipe zero) 127 * and must be NULL terminated. 128 * When only control endpoint is expected, you may set NULL directly 129 * without creating one item array containing NULL. 95 130 * 96 * It MUST be of size expected_enpoints_count(excluding default ctrl) + 1 97 * where the last record MUST BE NULL, otherwise catastrophic things may 98 * happen. 131 * When the driver expect single interrupt in endpoint, 132 * the initialization may look like this: 133 \code 134 static usb_endpoint_description_t poll_endpoint_description = { 135 .transfer_type = USB_TRANSFER_INTERRUPT, 136 .direction = USB_DIRECTION_IN, 137 .interface_class = USB_CLASS_HUB, 138 .interface_subclass = 0, 139 .interface_protocol = 0, 140 .flags = 0 141 }; 142 143 static usb_endpoint_description_t *hub_endpoints[] = { 144 &poll_endpoint_description, 145 NULL 146 }; 147 148 static usb_driver_t hub_driver = { 149 .endpoints = hub_endpoints, 150 ... 151 }; 152 \endcode 99 153 */ 100 154 usb_endpoint_description_t **endpoints; … … 105 159 int usb_driver_main(usb_driver_t *); 106 160 161 int usb_device_select_interface(usb_device_t *, uint8_t, 162 usb_endpoint_description_t **); 163 107 164 typedef bool (*usb_polling_callback_t)(usb_device_t *, 108 165 uint8_t *, size_t, void *); 109 166 typedef void (*usb_polling_terminted_callback_t)(usb_device_t *, bool, void *); 110 111 167 112 168 int usb_device_auto_poll(usb_device_t *, size_t, -
uspace/lib/usb/include/usb/host/batch.h
r9d06563 ra4e18e1 39 39 #include <usbhc_iface.h> 40 40 #include <usb/usb.h> 41 #include <usb/host/endpoint.h> 41 42 42 43 typedef struct usb_transfer_batch usb_transfer_batch_t; … … 60 61 ddf_fun_t *fun; 61 62 void *arg; 63 endpoint_t *ep; 62 64 void *private_data; 63 65 }; … … 78 80 void *arg, 79 81 ddf_fun_t *fun, 82 endpoint_t *ep, 80 83 void *private_data 81 84 ); -
uspace/lib/usb/include/usb/host/device_keeper.h
r9d06563 ra4e18e1 40 40 #ifndef LIBUSB_HOST_DEVICE_KEEPER_H 41 41 #define LIBUSB_HOST_DEVICE_KEEPER_H 42 43 #include <adt/list.h> 42 44 #include <devman.h> 43 45 #include <fibril_synch.h> 44 46 #include <usb/usb.h> 47 #include <usb/host/endpoint.h> 45 48 46 49 /** Number of USB address for array dimensions. */ … … 51 54 usb_speed_t speed; 52 55 bool occupied; 56 link_t endpoints; 53 57 uint16_t control_used; 54 uint16_t toggle_status[2];55 58 devman_handle_t handle; 56 59 }; … … 68 71 void usb_device_keeper_init(usb_device_keeper_t *instance); 69 72 70 void usb_device_keeper_reserve_default_address(usb_device_keeper_t *instance, 71 usb_speed_t speed); 73 void usb_device_keeper_add_ep( 74 usb_device_keeper_t *instance, usb_address_t address, endpoint_t *ep); 75 76 void usb_device_keeper_reserve_default_address( 77 usb_device_keeper_t *instance, usb_speed_t speed); 72 78 73 79 void usb_device_keeper_release_default_address(usb_device_keeper_t *instance); 74 80 75 81 void usb_device_keeper_reset_if_need(usb_device_keeper_t *instance, 76 usb_target_t target, 77 const uint8_t *setup_data); 78 79 int usb_device_keeper_get_toggle(usb_device_keeper_t *instance, 80 usb_target_t target, usb_direction_t direction); 81 82 int usb_device_keeper_set_toggle(usb_device_keeper_t *instance, 83 usb_target_t target, usb_direction_t direction, bool toggle); 82 usb_target_t target, const uint8_t *setup_data); 84 83 85 84 usb_address_t device_keeper_get_free_address(usb_device_keeper_t *instance, -
uspace/lib/usb/include/usb/host/usb_endpoint_manager.h
r9d06563 ra4e18e1 37 37 * This structure shall simplify the management. 38 38 */ 39 #ifndef LIBUSB_HOST_ BANDWIDTH_H40 #define LIBUSB_HOST_ BANDWIDTH_H39 #ifndef LIBUSB_HOST_USB_ENDPOINT_MANAGER_H 40 #define LIBUSB_HOST_YSB_ENDPOINT_MANAGER_H 41 41 42 42 #include <adt/hash_table.h> 43 43 #include <fibril_synch.h> 44 44 #include <usb/usb.h> 45 #include <usb/host/endpoint.h> 45 46 46 47 #define BANDWIDTH_TOTAL_USB11 12000000 47 48 #define BANDWIDTH_AVAILABLE_USB11 ((BANDWIDTH_TOTAL_USB11 / 10) * 9) 48 49 49 typedef struct bandwidth{50 hash_table_t reserved;50 typedef struct usb_endpoint_manager { 51 hash_table_t ep_table; 51 52 fibril_mutex_t guard; 52 size_t free;53 size_t (*usage_fnc)(usb_speed_t, usb_transfer_type_t, size_t, size_t);54 } bandwidth_t;53 fibril_condvar_t change; 54 size_t free_bw; 55 } usb_endpoint_manager_t; 55 56 56 57 size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type, 57 58 size_t size, size_t max_packet_size); 58 59 59 int bandwidth_init(bandwidth_t *instance, size_t bandwidth,60 size_t (*usage_fnc)(usb_speed_t, usb_transfer_type_t, size_t, size_t));60 int usb_endpoint_manager_init(usb_endpoint_manager_t *instance, 61 size_t available_bandwidth); 61 62 62 void bandwidth_destroy(bandwidth_t *instance);63 void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance); 63 64 64 int bandwidth_reserve(bandwidth_t *instance, usb_address_t address, 65 usb_endpoint_t endpoint, usb_direction_t direction, usb_speed_t speed, 66 usb_transfer_type_t transfer_type, size_t max_packet_size, size_t size, 67 unsigned interval); 65 int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance, 66 endpoint_t *ep, size_t data_size); 68 67 69 int bandwidth_release(bandwidth_t *instance, usb_address_t address, 70 usb_endpoint_t endpoint, usb_direction_t direction); 68 int usb_endpoint_manager_register_ep_wait(usb_endpoint_manager_t *instance, 69 usb_address_t address, usb_endpoint_t ep, usb_direction_t direction, 70 void *data, void (*data_remove_callback)(void* data, void* arg), void *arg, 71 size_t bw); 71 72 72 int bandwidth_use(bandwidth_t *instance, usb_address_t address,73 usb_ endpoint_t endpoint, usb_direction_t direction);73 int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance, 74 usb_address_t address, usb_endpoint_t ep, usb_direction_t direction); 74 75 75 int bandwidth_free(bandwidth_t *instance, usb_address_t address, 76 usb_endpoint_t endpoint, usb_direction_t direction); 76 endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance, 77 usb_address_t address, usb_endpoint_t ep, usb_direction_t direction, 78 size_t *bw); 77 79 78 80 #endif … … 80 82 * @} 81 83 */ 84 -
uspace/lib/usb/include/usb/pipes.h
r9d06563 ra4e18e1 107 107 /** Interface number the endpoint must belong to (-1 for any). */ 108 108 int interface_no; 109 /** Alternate interface setting to choose. */ 110 int interface_setting; 109 111 /** Found descriptor fitting the description. */ 110 112 usb_standard_endpoint_descriptor_t *descriptor; -
uspace/lib/usb/src/devdrv.c
r9d06563 ra4e18e1 36 36 #include <usb/request.h> 37 37 #include <usb/debug.h> 38 #include <usb/dp.h> 38 39 #include <errno.h> 39 40 #include <str_error.h> … … 86 87 * @return Number of pipes (excluding default control pipe). 87 88 */ 88 static size_t count_other_pipes(usb_ driver_t *drv)89 static size_t count_other_pipes(usb_endpoint_description_t **endpoints) 89 90 { 90 91 size_t count = 0; 91 if ( drv->endpoints == NULL) {92 if (endpoints == NULL) { 92 93 return 0; 93 94 } 94 95 95 while ( drv->endpoints[count] != NULL) {96 while (endpoints[count] != NULL) { 96 97 count++; 97 98 } … … 106 107 * @return Error code. 107 108 */ 108 static int initialize_other_pipes(usb_driver_t *drv, usb_device_t *dev) 109 static int initialize_other_pipes(usb_endpoint_description_t **endpoints, 110 usb_device_t *dev) 109 111 { 110 112 int rc; 111 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev); 112 113 size_t pipe_count = count_other_pipes(drv); 113 114 size_t pipe_count = count_other_pipes(endpoints); 115 if (pipe_count == 0) { 116 return EOK; 117 } 118 114 119 dev->pipes = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count); 115 120 if (dev->pipes == NULL) { … … 133 138 } 134 139 135 dev->pipes[i].description = drv->endpoints[i];140 dev->pipes[i].description = endpoints[i]; 136 141 dev->pipes[i].interface_no = dev->interface_no; 142 dev->pipes[i].interface_setting = 0; 137 143 } 138 144 … … 178 184 usb_hc_connection_close(&hc_conn); 179 185 186 dev->pipes_count = pipe_count; 187 180 188 return EOK; 181 189 … … 227 235 } 228 236 237 /* Get our interface. */ 238 dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev); 239 229 240 /* 230 241 * For further actions, we need open session on default control pipe. … … 257 268 258 269 if (driver->endpoints != NULL) { 259 rc = initialize_other_pipes(driver , dev);270 rc = initialize_other_pipes(driver->endpoints, dev); 260 271 } 261 272 … … 271 282 272 283 return rc; 284 } 285 286 /** Count number of alternate settings of a interface. 287 * 288 * @param config_descr Full configuration descriptor. 289 * @param config_descr_size Size of @p config_descr in bytes. 290 * @param interface_no Interface number. 291 * @return Number of alternate interfaces for @p interface_no interface. 292 */ 293 static size_t count_alternate_interfaces(uint8_t *config_descr, 294 size_t config_descr_size, int interface_no) 295 { 296 assert(config_descr != NULL); 297 usb_dp_parser_t dp_parser = { 298 .nesting = usb_dp_standard_descriptor_nesting 299 }; 300 usb_dp_parser_data_t dp_data = { 301 .data = config_descr, 302 .size = config_descr_size, 303 .arg = NULL 304 }; 305 306 size_t alternate_count = 0; 307 308 uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser, 309 &dp_data, config_descr); 310 while (iface_ptr != NULL) { 311 usb_standard_interface_descriptor_t *iface 312 = (usb_standard_interface_descriptor_t *) iface_ptr; 313 if (iface->descriptor_type == USB_DESCTYPE_INTERFACE) { 314 if (iface->interface_number == interface_no) { 315 alternate_count++; 316 } 317 } 318 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data, 319 config_descr, iface_ptr); 320 } 321 322 return alternate_count; 323 } 324 325 /** Initialize structures related to alternate interfaces. 326 * 327 * @param dev Device where alternate settings shall be initialized. 328 * @return Error code. 329 */ 330 static int initialize_alternate_interfaces(usb_device_t *dev) 331 { 332 if (dev->interface_no < 0) { 333 dev->alternate_interfaces = NULL; 334 return EOK; 335 } 336 337 usb_alternate_interfaces_t *alternates 338 = malloc(sizeof(usb_alternate_interfaces_t)); 339 340 if (alternates == NULL) { 341 return ENOMEM; 342 } 343 344 alternates->alternative_count 345 = count_alternate_interfaces(dev->descriptors.configuration, 346 dev->descriptors.configuration_size, dev->interface_no); 347 348 if (alternates->alternative_count == 0) { 349 free(alternates); 350 return ENOENT; 351 } 352 353 alternates->alternatives = malloc(alternates->alternative_count 354 * sizeof(usb_alternate_interface_descriptors_t)); 355 if (alternates->alternatives == NULL) { 356 free(alternates); 357 return ENOMEM; 358 } 359 360 alternates->current = 0; 361 362 usb_dp_parser_t dp_parser = { 363 .nesting = usb_dp_standard_descriptor_nesting 364 }; 365 usb_dp_parser_data_t dp_data = { 366 .data = dev->descriptors.configuration, 367 .size = dev->descriptors.configuration_size, 368 .arg = NULL 369 }; 370 371 usb_alternate_interface_descriptors_t *cur_alt_iface 372 = &alternates->alternatives[0]; 373 374 uint8_t *iface_ptr = usb_dp_get_nested_descriptor(&dp_parser, 375 &dp_data, dp_data.data); 376 while (iface_ptr != NULL) { 377 usb_standard_interface_descriptor_t *iface 378 = (usb_standard_interface_descriptor_t *) iface_ptr; 379 if ((iface->descriptor_type != USB_DESCTYPE_INTERFACE) 380 || (iface->interface_number != dev->interface_no)) { 381 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, 382 &dp_data, 383 dp_data.data, iface_ptr); 384 continue; 385 } 386 387 cur_alt_iface->interface = iface; 388 cur_alt_iface->nested_descriptors = iface_ptr + sizeof(*iface); 389 390 /* Find next interface to count size of nested descriptors. */ 391 iface_ptr = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data, 392 dp_data.data, iface_ptr); 393 if (iface_ptr == NULL) { 394 uint8_t *next = dp_data.data + dp_data.size; 395 cur_alt_iface->nested_descriptors_size 396 = next - cur_alt_iface->nested_descriptors; 397 } else { 398 cur_alt_iface->nested_descriptors_size 399 = iface_ptr - cur_alt_iface->nested_descriptors; 400 } 401 402 cur_alt_iface++; 403 } 404 405 dev->alternate_interfaces = alternates; 406 407 return EOK; 273 408 } 274 409 … … 301 436 dev->descriptors.configuration = NULL; 302 437 438 dev->pipes_count = 0; 439 dev->pipes = NULL; 440 303 441 rc = initialize_pipes(dev); 304 442 if (rc != EOK) { … … 307 445 } 308 446 447 (void) initialize_alternate_interfaces(dev); 448 309 449 return driver->ops->add_device(dev); 450 } 451 452 /** Destroy existing pipes of a USB device. 453 * 454 * @param dev Device where to destroy the pipes. 455 * @return Error code. 456 */ 457 static int destroy_current_pipes(usb_device_t *dev) 458 { 459 size_t i; 460 int rc; 461 462 /* TODO: this shall be done under some device mutex. */ 463 464 /* First check that no session is opened. */ 465 for (i = 0; i < dev->pipes_count; i++) { 466 if (usb_pipe_is_session_started(dev->pipes[i].pipe)) { 467 return EBUSY; 468 } 469 } 470 471 /* Prepare connection to HC. */ 472 usb_hc_connection_t hc_conn; 473 rc = usb_hc_connection_initialize_from_device(&hc_conn, dev->ddf_dev); 474 if (rc != EOK) { 475 return rc; 476 } 477 rc = usb_hc_connection_open(&hc_conn); 478 if (rc != EOK) { 479 return rc; 480 } 481 482 /* Destroy the pipes. */ 483 for (i = 0; i < dev->pipes_count; i++) { 484 usb_pipe_unregister(dev->pipes[i].pipe, &hc_conn); 485 free(dev->pipes[i].pipe); 486 } 487 488 usb_hc_connection_close(&hc_conn); 489 490 free(dev->pipes); 491 dev->pipes = NULL; 492 dev->pipes_count = 0; 493 494 return EOK; 495 } 496 497 /** Change interface setting of a device. 498 * This function selects new alternate setting of an interface by issuing 499 * proper USB command to the device and also creates new USB pipes 500 * under @c dev->pipes. 501 * 502 * @warning This function is intended for drivers working at interface level. 503 * For drivers controlling the whole device, you need to change interface 504 * manually using usb_request_set_interface() and creating new pipes 505 * with usb_pipe_initialize_from_configuration(). 506 * 507 * @param dev USB device. 508 * @param alternate_setting Alternate setting to choose. 509 * @param endpoints New endpoint descriptions. 510 * @return Error code. 511 */ 512 int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting, 513 usb_endpoint_description_t **endpoints) 514 { 515 if (dev->interface_no < 0) { 516 return EINVAL; 517 } 518 519 int rc; 520 521 /* TODO: more transactional behavior. */ 522 523 /* Destroy existing pipes. */ 524 rc = destroy_current_pipes(dev); 525 if (rc != EOK) { 526 return rc; 527 } 528 529 /* Change the interface itself. */ 530 rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no, 531 alternate_setting); 532 if (rc != EOK) { 533 return rc; 534 } 535 536 /* Create new pipes. */ 537 rc = initialize_other_pipes(endpoints, dev); 538 539 return rc; 310 540 } 311 541 -
uspace/lib/usb/src/host/batch.c
r9d06563 ra4e18e1 54 54 void *arg, 55 55 ddf_fun_t *fun, 56 endpoint_t *ep, 56 57 void *private_data 57 58 ) … … 77 78 instance->next_step = NULL; 78 79 instance->error = EOK; 79 80 instance->ep = ep; 80 81 } 81 82 /*----------------------------------------------------------------------------*/ -
uspace/lib/usb/src/host/device_keeper.c
r9d06563 ra4e18e1 56 56 instance->devices[i].control_used = 0; 57 57 instance->devices[i].handle = 0; 58 instance->devices[i].toggle_status[0] = 0; 59 instance->devices[i].toggle_status[1] = 0; 60 } 58 list_initialize(&instance->devices[i].endpoints); 59 } 60 } 61 /*----------------------------------------------------------------------------*/ 62 void usb_device_keeper_add_ep( 63 usb_device_keeper_t *instance, usb_address_t address, endpoint_t *ep) 64 { 65 assert(instance); 66 fibril_mutex_lock(&instance->guard); 67 assert(instance->devices[address].occupied); 68 list_append(&ep->same_device_eps, &instance->devices[address].endpoints); 69 fibril_mutex_unlock(&instance->guard); 61 70 } 62 71 /*----------------------------------------------------------------------------*/ … … 66 75 * @param[in] speed Speed of the device requesting default address. 67 76 */ 68 void usb_device_keeper_reserve_default_address( usb_device_keeper_t *instance,69 usb_ speed_t speed)77 void usb_device_keeper_reserve_default_address( 78 usb_device_keeper_t *instance, usb_speed_t speed) 70 79 { 71 80 assert(instance); … … 101 110 * Really ugly one. 102 111 */ 103 void usb_device_keeper_reset_if_need( usb_device_keeper_t *instance,104 usb_ target_t target, const uint8_t *data)112 void usb_device_keeper_reset_if_need( 113 usb_device_keeper_t *instance, usb_target_t target, const uint8_t *data) 105 114 { 106 115 assert(instance); … … 119 128 /* recipient is endpoint, value is zero (ENDPOINT_STALL) */ 120 129 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) { 130 link_t *current = 131 instance->devices[target.address].endpoints.next; 132 while (current != 133 &instance->devices[target.address].endpoints) 134 { 121 135 /* endpoint number is < 16, thus first byte is enough */ 122 instance->devices[target.address].toggle_status[0] &=123 ~(1 <<data[4]);124 instance->devices[target.address].toggle_status[1] &=125 ~(1 << data[4]);136 endpoint_toggle_reset_filtered( 137 current, data[4]); 138 current = current->next; 139 } 126 140 } 127 141 break; … … 131 145 /* target must be device */ 132 146 if ((data[0] & 0xf) == 0) { 133 instance->devices[target.address].toggle_status[0] = 0; 134 instance->devices[target.address].toggle_status[1] = 0; 147 link_t *current = 148 instance->devices[target.address].endpoints.next; 149 while (current != 150 &instance->devices[target.address].endpoints) 151 { 152 endpoint_toggle_reset(current); 153 current = current->next; 154 } 135 155 } 136 156 break; 137 157 } 138 158 fibril_mutex_unlock(&instance->guard); 139 }140 /*----------------------------------------------------------------------------*/141 /** Get current value of endpoint toggle.142 *143 * @param[in] instance Device keeper structure to use.144 * @param[in] target Device and endpoint used.145 * @return Error code146 */147 int usb_device_keeper_get_toggle(usb_device_keeper_t *instance,148 usb_target_t target, usb_direction_t direction)149 {150 assert(instance);151 /* only control pipes are bi-directional and those do not need toggle */152 if (direction == USB_DIRECTION_BOTH)153 return ENOENT;154 int ret;155 fibril_mutex_lock(&instance->guard);156 if (target.endpoint > 15 || target.endpoint < 0157 || target.address >= USB_ADDRESS_COUNT || target.address < 0158 || !instance->devices[target.address].occupied) {159 usb_log_error("Invalid data when asking for toggle value.\n");160 ret = EINVAL;161 } else {162 ret = (instance->devices[target.address].toggle_status[direction]163 >> target.endpoint) & 1;164 }165 fibril_mutex_unlock(&instance->guard);166 return ret;167 }168 /*----------------------------------------------------------------------------*/169 /** Set current value of endpoint toggle.170 *171 * @param[in] instance Device keeper structure to use.172 * @param[in] target Device and endpoint used.173 * @param[in] toggle Toggle value.174 * @return Error code.175 */176 int usb_device_keeper_set_toggle(usb_device_keeper_t *instance,177 usb_target_t target, usb_direction_t direction, bool toggle)178 {179 assert(instance);180 /* only control pipes are bi-directional and those do not need toggle */181 if (direction == USB_DIRECTION_BOTH)182 return ENOENT;183 int ret;184 fibril_mutex_lock(&instance->guard);185 if (target.endpoint > 15 || target.endpoint < 0186 || target.address >= USB_ADDRESS_COUNT || target.address < 0187 || !instance->devices[target.address].occupied) {188 usb_log_error("Invalid data when setting toggle value.\n");189 ret = EINVAL;190 } else {191 if (toggle) {192 instance->devices[target.address].toggle_status[direction]193 |= (1 << target.endpoint);194 } else {195 instance->devices[target.address].toggle_status[direction]196 &= ~(1 << target.endpoint);197 }198 ret = EOK;199 }200 fibril_mutex_unlock(&instance->guard);201 return ret;202 159 } 203 160 /*----------------------------------------------------------------------------*/ … … 208 165 * @return Free address, or error code. 209 166 */ 210 usb_address_t device_keeper_get_free_address( usb_device_keeper_t *instance,211 usb_ speed_t speed)167 usb_address_t device_keeper_get_free_address( 168 usb_device_keeper_t *instance, usb_speed_t speed) 212 169 { 213 170 assert(instance); … … 229 186 instance->devices[new_address].occupied = true; 230 187 instance->devices[new_address].speed = speed; 231 instance->devices[new_address].toggle_status[0] = 0;232 instance->devices[new_address].toggle_status[1] = 0;233 188 instance->last_address = new_address; 234 189 fibril_mutex_unlock(&instance->guard); … … 259 214 * @param[in] address Device address 260 215 */ 261 void usb_device_keeper_release( usb_device_keeper_t *instance,262 usb_ address_t address)216 void usb_device_keeper_release( 217 usb_device_keeper_t *instance, usb_address_t address) 263 218 { 264 219 assert(instance); … … 278 233 * @return USB Address, or error code. 279 234 */ 280 usb_address_t usb_device_keeper_find( usb_device_keeper_t *instance,281 devman_handle_t handle)235 usb_address_t usb_device_keeper_find( 236 usb_device_keeper_t *instance, devman_handle_t handle) 282 237 { 283 238 assert(instance); … … 301 256 * @return USB speed. 302 257 */ 303 usb_speed_t usb_device_keeper_get_speed( usb_device_keeper_t *instance,304 usb_ address_t address)258 usb_speed_t usb_device_keeper_get_speed( 259 usb_device_keeper_t *instance, usb_address_t address) 305 260 { 306 261 assert(instance); … … 310 265 } 311 266 /*----------------------------------------------------------------------------*/ 312 void usb_device_keeper_use_control( usb_device_keeper_t *instance,313 usb_ target_t target)267 void usb_device_keeper_use_control( 268 usb_device_keeper_t *instance, usb_target_t target) 314 269 { 315 270 assert(instance); … … 323 278 } 324 279 /*----------------------------------------------------------------------------*/ 325 void usb_device_keeper_release_control( usb_device_keeper_t *instance,326 usb_ target_t target)280 void usb_device_keeper_release_control( 281 usb_device_keeper_t *instance, usb_target_t target) 327 282 { 328 283 assert(instance); -
uspace/lib/usb/src/host/usb_endpoint_manager.c
r9d06563 ra4e18e1 1 1 /* 2 2 * Copyright (c) 2011 Jan Vesely 3 * All rights reserved.3 * All rights eps. 4 4 * 5 5 * Redistribution and use in source and binary forms, with or without … … 27 27 */ 28 28 29 #include <bool.h> 29 30 #include <assert.h> 30 31 #include <errno.h> 31 #include <usb/host/bandwidth.h> 32 32 33 #include <usb/host/usb_endpoint_manager.h> 34 35 #define BUCKET_COUNT 7 36 37 #define MAX_KEYS (3) 33 38 typedef struct { 34 usb_address_t address;35 usb_endpoint_t endpoint;36 usb_direction_t direction;37 } __attribute__((aligned (sizeof(unsigned long)))) transfer_t;38 /*----------------------------------------------------------------------------*/39 typedef struct {40 transfer_t transfer;41 39 link_t link; 42 bool used; 43 size_t required; 44 } transfer_status_t; 45 /*----------------------------------------------------------------------------*/ 46 #define BUCKET_COUNT 7 47 #define MAX_KEYS (sizeof(transfer_t) / sizeof(unsigned long)) 48 /*----------------------------------------------------------------------------*/ 49 static hash_index_t transfer_hash(unsigned long key[]) 40 size_t bw; 41 endpoint_t *ep; 42 } node_t; 43 /*----------------------------------------------------------------------------*/ 44 static hash_index_t node_hash(unsigned long key[]) 50 45 { 51 46 hash_index_t hash = 0; … … 58 53 } 59 54 /*----------------------------------------------------------------------------*/ 60 static int transfer_compare( 61 unsigned long key[], hash_count_t keys, link_t *item) 55 static int node_compare(unsigned long key[], hash_count_t keys, link_t *item) 62 56 { 63 57 assert(item); 64 transfer_status_t *status = 65 hash_table_get_instance(item, transfer_status_t, link); 66 const size_t bytes = 67 keys < MAX_KEYS ? keys * sizeof(unsigned long) : sizeof(transfer_t); 68 return bcmp(key, &status->transfer, bytes); 69 } 70 /*----------------------------------------------------------------------------*/ 71 static void transfer_remove(link_t *item) 58 node_t *node = hash_table_get_instance(item, node_t, link); 59 assert(node); 60 assert(node->ep); 61 bool match = true; 62 switch (keys) { 63 case 3: 64 match = match && (key[2] == node->ep->direction); 65 case 2: 66 match = match && (key[1] == (unsigned long)node->ep->endpoint); 67 case 1: 68 match = match && (key[0] == (unsigned long)node->ep->address); 69 break; 70 default: 71 match = false; 72 } 73 return match; 74 } 75 /*----------------------------------------------------------------------------*/ 76 static void node_remove(link_t *item) 72 77 { 73 78 assert(item); 74 transfer_status_t *status = 75 hash_table_get_instance(item, transfer_status_t, link); 76 assert(status); 77 free(status); 78 } 79 /*----------------------------------------------------------------------------*/ 80 hash_table_operations_t op = { 81 .hash = transfer_hash, 82 .compare = transfer_compare, 83 .remove_callback = transfer_remove, 79 node_t *node = hash_table_get_instance(item, node_t, link); 80 endpoint_destroy(node->ep); 81 free(node); 82 } 83 /*----------------------------------------------------------------------------*/ 84 static hash_table_operations_t op = { 85 .hash = node_hash, 86 .compare = node_compare, 87 .remove_callback = node_remove, 84 88 }; 85 89 /*----------------------------------------------------------------------------*/ … … 87 91 size_t size, size_t max_packet_size) 88 92 { 93 /* We care about bandwidth only for interrupt and isochronous. */ 94 if ((type != USB_TRANSFER_INTERRUPT) 95 && (type != USB_TRANSFER_ISOCHRONOUS)) { 96 return 0; 97 } 98 89 99 const unsigned packet_count = 90 100 (size + max_packet_size - 1) / max_packet_size; … … 120 130 } 121 131 /*----------------------------------------------------------------------------*/ 122 int bandwidth_init(bandwidth_t *instance, size_t bandwidth,123 size_t (*usage_fnc)(usb_speed_t, usb_transfer_type_t, size_t, size_t))132 int usb_endpoint_manager_init(usb_endpoint_manager_t *instance, 133 size_t available_bandwidth) 124 134 { 125 135 assert(instance); 126 136 fibril_mutex_initialize(&instance->guard); 127 instance->free = bandwidth;128 instance-> usage_fnc = usage_fnc;137 fibril_condvar_initialize(&instance->change); 138 instance->free_bw = available_bandwidth; 129 139 bool ht = 130 hash_table_create(&instance-> reserved, BUCKET_COUNT, MAX_KEYS, &op);140 hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op); 131 141 return ht ? EOK : ENOMEM; 132 142 } 133 143 /*----------------------------------------------------------------------------*/ 134 void bandwidth_destroy(bandwidth_t *instance) 135 { 136 hash_table_destroy(&instance->reserved); 137 } 138 /*----------------------------------------------------------------------------*/ 139 int bandwidth_reserve(bandwidth_t *instance, usb_address_t address, 140 usb_endpoint_t endpoint, usb_direction_t direction, usb_speed_t speed, 141 usb_transfer_type_t transfer_type, size_t max_packet_size, size_t size, 142 unsigned interval) 143 { 144 if (transfer_type != USB_TRANSFER_ISOCHRONOUS && 145 transfer_type != USB_TRANSFER_INTERRUPT) { 146 return ENOTSUP; 147 } 148 149 assert(instance); 150 assert(instance->usage_fnc); 151 152 transfer_t trans = { 153 .address = address, 154 .endpoint = endpoint, 155 .direction = direction, 156 }; 144 void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance) 145 { 146 hash_table_destroy(&instance->ep_table); 147 } 148 /*----------------------------------------------------------------------------*/ 149 int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance, 150 endpoint_t *ep, size_t data_size) 151 { 152 assert(ep); 153 size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type, 154 data_size, ep->max_packet_size); 155 assert(instance); 156 157 unsigned long key[MAX_KEYS] = 158 {ep->address, ep->endpoint, ep->direction}; 157 159 fibril_mutex_lock(&instance->guard); 158 const size_t required = 159 instance->usage_fnc(speed, transfer_type, size, max_packet_size); 160 161 if (required > instance->free) { 160 161 link_t *item = 162 hash_table_find(&instance->ep_table, key); 163 if (item != NULL) { 164 fibril_mutex_unlock(&instance->guard); 165 return EEXISTS; 166 } 167 168 if (bw > instance->free_bw) { 162 169 fibril_mutex_unlock(&instance->guard); 163 170 return ENOSPC; 164 171 } 165 172 166 link_t *item = 167 hash_table_find(&instance->reserved, (unsigned long*)&trans); 168 if (item != NULL) { 169 fibril_mutex_unlock(&instance->guard); 170 return EEXISTS; 171 } 172 173 transfer_status_t *status = malloc(sizeof(transfer_status_t)); 174 if (status == NULL) { 173 node_t *node = malloc(sizeof(node_t)); 174 if (node == NULL) { 175 175 fibril_mutex_unlock(&instance->guard); 176 176 return ENOMEM; 177 177 } 178 178 179 status->transfer = trans; 180 status->required = required; 181 status->used = false; 182 link_initialize(&status->link); 183 184 hash_table_insert(&instance->reserved, 185 (unsigned long*)&status->transfer, &status->link); 186 instance->free -= required; 179 node->bw = bw; 180 node->ep = ep; 181 link_initialize(&node->link); 182 183 hash_table_insert(&instance->ep_table, key, &node->link); 184 instance->free_bw -= bw; 187 185 fibril_mutex_unlock(&instance->guard); 186 fibril_condvar_broadcast(&instance->change); 188 187 return EOK; 189 /* TODO: compute bandwidth used */ 190 } 191 /*----------------------------------------------------------------------------*/ 192 int bandwidth_release(bandwidth_t *instance, usb_address_t address, 193 usb_endpoint_t endpoint, usb_direction_t direction) 194 { 195 assert(instance); 196 transfer_t trans = { 197 .address = address, 198 .endpoint = endpoint, 199 .direction = direction, 200 }; 188 } 189 /*----------------------------------------------------------------------------*/ 190 int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance, 191 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction) 192 { 193 assert(instance); 194 unsigned long key[MAX_KEYS] = {address, endpoint, direction}; 195 201 196 fibril_mutex_lock(&instance->guard); 202 link_t *item = 203 hash_table_find(&instance->reserved, (unsigned long*)&trans); 197 link_t *item = hash_table_find(&instance->ep_table, key); 204 198 if (item == NULL) { 205 199 fibril_mutex_unlock(&instance->guard); … … 207 201 } 208 202 209 transfer_status_t *status = 210 hash_table_get_instance(item, transfer_status_t, link); 211 212 instance->free += status->required; 213 214 hash_table_remove(&instance->reserved, 215 (unsigned long*)&trans, MAX_KEYS); 203 node_t *node = hash_table_get_instance(item, node_t, link); 204 instance->free_bw += node->bw; 205 hash_table_remove(&instance->ep_table, key, MAX_KEYS); 216 206 217 207 fibril_mutex_unlock(&instance->guard); 208 fibril_condvar_broadcast(&instance->change); 218 209 return EOK; 219 /* TODO: compute bandwidth freed */ 220 } 221 /*----------------------------------------------------------------------------*/ 222 int bandwidth_use(bandwidth_t *instance, usb_address_t address, 223 usb_endpoint_t endpoint, usb_direction_t direction) 224 { 225 assert(instance); 226 transfer_t trans = { 227 .address = address, 228 .endpoint = endpoint, 229 .direction = direction, 230 }; 210 } 211 /*----------------------------------------------------------------------------*/ 212 endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance, 213 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction, 214 size_t *bw) 215 { 216 assert(instance); 217 unsigned long key[MAX_KEYS] = {address, endpoint, direction}; 218 231 219 fibril_mutex_lock(&instance->guard); 232 link_t *item = 233 hash_table_find(&instance->reserved, (unsigned long*)&trans); 234 int ret = EOK; 235 if (item != NULL) { 236 transfer_status_t *status = 237 hash_table_get_instance(item, transfer_status_t, link); 238 assert(status); 239 if (status->used) { 240 ret = EINPROGRESS; 241 } 242 status->used = true; 243 } else { 244 ret = EINVAL; 245 } 220 link_t *item = hash_table_find(&instance->ep_table, key); 221 if (item == NULL) { 222 fibril_mutex_unlock(&instance->guard); 223 return NULL; 224 } 225 node_t *node = hash_table_get_instance(item, node_t, link); 226 if (bw) 227 *bw = node->bw; 228 246 229 fibril_mutex_unlock(&instance->guard); 247 return ret; 248 } 249 /*----------------------------------------------------------------------------*/ 250 int bandwidth_free(bandwidth_t *instance, usb_address_t address, 251 usb_endpoint_t endpoint, usb_direction_t direction) 252 { 253 assert(instance); 254 transfer_t trans = { 255 .address = address, 256 .endpoint = endpoint, 257 .direction = direction, 258 }; 259 fibril_mutex_lock(&instance->guard); 260 link_t *item = 261 hash_table_find(&instance->reserved, (unsigned long*)&trans); 262 int ret = EOK; 263 if (item != NULL) { 264 transfer_status_t *status = 265 hash_table_get_instance(item, transfer_status_t, link); 266 assert(status); 267 if (!status->used) { 268 ret = ENOENT; 269 } 270 status->used = false; 271 } else { 272 ret = EINVAL; 273 } 274 fibril_mutex_unlock(&instance->guard); 275 return ret; 276 } 230 return node->ep; 231 } -
uspace/lib/usb/src/hub.c
r9d06563 ra4e18e1 142 142 DEV_IFACE_ID(USBHC_DEV_IFACE), 143 143 IPC_M_USBHC_RELEASE_ADDRESS, address); 144 } 145 146 static void unregister_control_endpoint_on_default_address( 147 usb_hc_connection_t *connection) 148 { 149 usb_device_connection_t dev_conn; 150 int rc = usb_device_connection_initialize_on_default_address(&dev_conn, 151 connection); 152 if (rc != EOK) { 153 return; 154 } 155 156 usb_pipe_t ctrl_pipe; 157 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn); 158 if (rc != EOK) { 159 return; 160 } 161 162 usb_pipe_unregister(&ctrl_pipe, connection); 144 163 } 145 164 … … 235 254 goto leave_release_default_address; 236 255 } 256 257 /* Before sending any traffic, we need to register this 258 * endpoint. 259 */ 260 rc = usb_pipe_register(&ctrl_pipe, 0, connection); 261 if (rc != EOK) { 262 rc = EREFUSED; 263 goto leave_release_default_address; 264 } 237 265 rc = usb_pipe_probe_default_control(&ctrl_pipe); 238 266 if (rc != EOK) { … … 244 272 if (rc != EOK) { 245 273 rc = ENOTCONN; 246 goto leave_ release_default_address;274 goto leave_unregister_endpoint; 247 275 } 248 276 … … 256 284 257 285 /* 286 * Register the control endpoint for the new device. 287 */ 288 rc = usb_pipe_register(&ctrl_pipe, 0, connection); 289 if (rc != EOK) { 290 rc = EREFUSED; 291 goto leave_unregister_endpoint; 292 } 293 294 /* 295 * Release the original endpoint. 296 */ 297 unregister_control_endpoint_on_default_address(connection); 298 299 /* 258 300 * Once the address is changed, we can return the default address. 259 301 */ 260 302 usb_hc_release_default_address(connection); 303 261 304 262 305 /* … … 273 316 } 274 317 318 319 275 320 /* 276 321 * And now inform the host controller about the handle. … … 308 353 usb_pipe_end_session(&ctrl_pipe); 309 354 355 leave_unregister_endpoint: 356 usb_pipe_unregister(&ctrl_pipe, connection); 357 310 358 leave_release_default_address: 311 359 usb_hc_release_default_address(connection); -
uspace/lib/usb/src/pipesinit.c
r9d06563 ra4e18e1 121 121 usb_endpoint_mapping_t *mapping, size_t mapping_count, 122 122 usb_endpoint_description_t *found_endpoint, 123 int interface_number )123 int interface_number, int interface_setting) 124 124 { 125 125 while (mapping_count > 0) { … … 127 127 || (mapping->interface_no == interface_number); 128 128 129 bool interface_setting_fits = (mapping->interface_setting < 0) 130 || (mapping->interface_setting == interface_setting); 131 129 132 bool endpoint_descriptions_fits = endpoint_fits_description( 130 133 mapping->description, found_endpoint); 131 134 132 if (interface_number_fits && endpoint_descriptions_fits) { 135 if (interface_number_fits 136 && interface_setting_fits 137 && endpoint_descriptions_fits) { 133 138 return mapping; 134 139 } … … 181 186 */ 182 187 usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping, 183 mapping_count, &description, interface->interface_number); 188 mapping_count, &description, 189 interface->interface_number, interface->alternate_setting); 184 190 if (ep_mapping == NULL) { 185 191 return ENOENT; -
uspace/srv/hw/irc/apic/apic.c
r9d06563 ra4e18e1 87 87 async_answer_0(callid, EOK); 88 88 break; 89 case IPC_M_PHONE_HUNGUP: 90 /* The other side has hung up. */ 91 async_answer_0(callid, EOK); 92 return; 89 93 default: 90 94 async_answer_0(callid, EINVAL); -
uspace/srv/hw/irc/i8259/i8259.c
r9d06563 ra4e18e1 121 121 async_answer_0(callid, EOK); 122 122 break; 123 case IPC_M_PHONE_HUNGUP: 124 /* The other side has hung up. */ 125 async_answer_0(callid, EOK); 126 return; 123 127 default: 124 128 async_answer_0(callid, EINVAL);
Note:
See TracChangeset
for help on using the changeset viewer.