source: mainline/uspace/lib/usbhost/src/usb2_bus.c@ 58ac3ec

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 58ac3ec was 58ac3ec, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

uhci: finished forgotten refactoring to generic batch

  • Property mode set to 100644
File size: 14.1 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * HC Endpoint management.
34 */
35
36#include <usb/host/usb2_bus.h>
37#include <usb/host/endpoint.h>
38#include <usb/host/ddf_helpers.h>
39#include <usb/debug.h>
40#include <usb/request.h>
41#include <usb/descriptor.h>
42#include <usb/usb.h>
43
44#include <assert.h>
45#include <errno.h>
46#include <macros.h>
47#include <str_error.h>
48#include <stdlib.h>
49#include <stdbool.h>
50
51/** Ops receive generic bus_t pointer. */
52static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
53{
54 assert(bus_base);
55 return (usb2_bus_t *) bus_base;
56}
57
58/** Get list that holds endpoints for given address.
59 * @param bus usb2_bus structure, non-null.
60 * @param addr USB address, must be >= 0.
61 * @return Pointer to the appropriate list.
62 */
63static list_t * get_list(usb2_bus_t *bus, usb_address_t addr)
64{
65 assert(bus);
66 assert(addr >= 0);
67 return &bus->devices[addr % ARRAY_SIZE(bus->devices)].endpoint_list;
68}
69
70/** Get speed assigned to USB address.
71 *
72 * @param[in] bus Device manager structure to use.
73 * @param[in] address Address the caller wants to find.
74 * @param[out] speed Assigned speed.
75 * @return Error code.
76 */
77static int usb2_bus_get_speed(bus_t *bus_base, usb_address_t address, usb_speed_t *speed)
78{
79 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
80
81 if (!usb_address_is_valid(address)) {
82 return EINVAL;
83 }
84
85 const int ret = bus->devices[address].occupied ? EOK : ENOENT;
86 if (speed && bus->devices[address].occupied) {
87 *speed = bus->devices[address].speed;
88 }
89
90 return ret;
91}
92
93static const usb_endpoint_desc_t usb2_default_control_ep = {
94 .endpoint_no = 0,
95 .transfer_type = USB_TRANSFER_CONTROL,
96 .direction = USB_DIRECTION_BOTH,
97 .max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
98 .packets = 1,
99};
100
101
102static const usb_target_t usb2_default_target = {{
103 .address = USB_ADDRESS_DEFAULT,
104 .endpoint = 0,
105}};
106
107static int usb2_bus_address_device(bus_t *bus, hcd_t *hcd, device_t *dev)
108{
109 int err;
110
111 /* The default address is currently reserved for this device */
112 dev->address = USB_ADDRESS_DEFAULT;
113
114 /** Reserve address early, we want pretty log messages */
115 usb_address_t address;
116 if ((err = bus_request_address(bus, &address, false, dev->speed))) {
117 usb_log_error("Failed to reserve new address: %s.",
118 str_error(err));
119 return err;
120 }
121 usb_log_debug("Device(%d): Reserved new address.", address);
122
123 /* Add default pipe on default address */
124 usb_log_debug("Device(%d): Adding default target (0:0)", address);
125
126 endpoint_t *default_ep;
127 err = bus_add_endpoint(bus, dev, &usb2_default_control_ep, &default_ep);
128 if (err != EOK) {
129 usb_log_error("Device(%d): Failed to add default target: %s.",
130 address, str_error(err));
131 goto err_address;
132 }
133
134 /* Get max packet size for default pipe */
135 usb_standard_device_descriptor_t desc = { 0 };
136 const usb_device_request_setup_packet_t get_device_desc_8 =
137 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
138
139 usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
140 address);
141 ssize_t got = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_IN,
142 (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
143 "read first 8 bytes of dev descriptor");
144
145 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
146 err = got < 0 ? got : EOVERFLOW;
147 usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
148 address, str_error(err));
149 goto err_default_control_ep;
150 }
151
152 /* Set new address */
153 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
154
155 usb_log_debug("Device(%d): Setting USB address.", address);
156 err = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_OUT,
157 NULL, 0, *(uint64_t *)&set_address, "set address");
158 if (err != 0) {
159 usb_log_error("Device(%d): Failed to set new address: %s.",
160 address, str_error(got));
161 goto err_default_control_ep;
162 }
163
164 /* We need to remove ep before we change the address */
165 if ((err = bus_remove_endpoint(bus, default_ep))) {
166 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
167 goto err_address;
168 }
169 endpoint_del_ref(default_ep);
170
171 dev->address = address;
172
173 const usb_endpoint_desc_t control_ep = {
174 .endpoint_no = 0,
175 .transfer_type = USB_TRANSFER_CONTROL,
176 .direction = USB_DIRECTION_BOTH,
177 .max_packet_size = ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
178 .packets = ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc.max_packet_size)),
179 };
180
181 /* Register EP on the new address */
182 usb_log_debug("Device(%d): Registering control EP.", address);
183 err = bus_add_endpoint(bus, dev, &control_ep, NULL);
184 if (err != EOK) {
185 usb_log_error("Device(%d): Failed to register EP0: %s",
186 address, str_error(err));
187 goto err_address;
188 }
189
190 return EOK;
191
192err_default_control_ep:
193 bus_remove_endpoint(bus, default_ep);
194 endpoint_del_ref(default_ep);
195err_address:
196 bus_release_address(bus, address);
197 return err;
198}
199
200/** Enumerate a new USB device
201 */
202static int usb2_bus_enumerate_device(bus_t *bus, hcd_t *hcd, device_t *dev)
203{
204 int err;
205
206 /* The speed of the new device was reported by the hub when reserving
207 * default address.
208 */
209 if ((err = usb2_bus_get_speed(bus, USB_ADDRESS_DEFAULT, &dev->speed))) {
210 usb_log_error("Failed to verify speed: %s.", str_error(err));
211 return err;
212 }
213 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
214
215 if (dev->hub) {
216 /* Manage TT */
217 if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
218 /* For LS devices under HS hub */
219 /* TODO: How about SS hubs? */
220 dev->tt.address = dev->hub->address;
221 dev->tt.port = dev->port;
222 }
223 else {
224 /* Inherit hub's TT */
225 dev->tt = dev->hub->tt;
226 }
227 }
228 else {
229 dev->tt = (usb_tt_address_t) {
230 .address = -1,
231 .port = 0,
232 };
233 }
234
235 /* Assign an address to the device */
236 if ((err = usb2_bus_address_device(bus, hcd, dev))) {
237 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
238 return err;
239 }
240
241 /* Read the device descriptor, derive the match ids */
242 if ((err = hcd_ddf_device_explore(hcd, dev))) {
243 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
244 bus_release_address(bus, dev->address);
245 return err;
246 }
247
248 return EOK;
249}
250
251/** Get a free USB address
252 *
253 * @param[in] bus Device manager structure to use.
254 * @return Free address, or error code.
255 */
256static int usb_bus_get_free_address(usb2_bus_t *bus, usb_address_t *addr)
257{
258 usb_address_t new_address = bus->last_address;
259 do {
260 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
261 if (new_address == USB_ADDRESS_DEFAULT)
262 new_address = 1;
263 if (new_address == bus->last_address)
264 return ENOSPC;
265 } while (bus->devices[new_address].occupied);
266
267 assert(new_address != USB_ADDRESS_DEFAULT);
268 bus->last_address = new_address;
269
270 *addr = new_address;
271 return EOK;
272}
273
274/** Find endpoint.
275 * @param bus usb_bus structure, non-null.
276 * @param target Endpoint address.
277 * @param direction Communication direction.
278 * @return Pointer to endpoint_t structure representing given communication
279 * target, NULL if there is no such endpoint registered.
280 * @note Assumes that the internal mutex is locked.
281 */
282static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, device_t *device, usb_target_t target, usb_direction_t direction)
283{
284 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
285
286 assert(device->address == target.address);
287
288 list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
289 if (((direction == ep->direction)
290 || (ep->direction == USB_DIRECTION_BOTH)
291 || (direction == USB_DIRECTION_BOTH))
292 && (target.endpoint == ep->endpoint))
293 return ep;
294 }
295 return NULL;
296}
297
298static endpoint_t *usb2_bus_create_ep(bus_t *bus)
299{
300 endpoint_t *ep = malloc(sizeof(endpoint_t));
301 if (!ep)
302 return NULL;
303
304 endpoint_init(ep, bus);
305 return ep;
306}
307
308static usb_target_t usb2_ep_to_target(endpoint_t *ep)
309{
310 assert(ep);
311 assert(ep->device);
312
313 return (usb_target_t) {{
314 .address = ep->device->address,
315 .endpoint = ep->endpoint,
316 }};
317}
318
319/** Register an endpoint to the bus. Reserves bandwidth.
320 * @param bus usb_bus structure, non-null.
321 * @param endpoint USB endpoint number.
322 */
323static int usb2_bus_register_ep(bus_t *bus_base, device_t *device, endpoint_t *ep, const usb_endpoint_desc_t *desc)
324{
325 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
326 assert(ep);
327
328 ep->device = device;
329
330 /* Extract USB2-related information from endpoint_desc */
331 ep->endpoint = desc->endpoint_no;
332 ep->direction = desc->direction;
333 ep->transfer_type = desc->transfer_type;
334 ep->max_packet_size = desc->max_packet_size;
335 ep->packets = desc->packets;
336
337 ep->bandwidth = bus_base->ops.count_bw(ep, desc->max_packet_size);
338
339 /* Check for existence */
340 if (usb2_bus_find_ep(bus_base, ep->device, usb2_ep_to_target(ep), ep->direction))
341 return EEXIST;
342
343 /* Check for available bandwidth */
344 if (ep->bandwidth > bus->free_bw)
345 return ENOSPC;
346
347 list_append(&ep->link, get_list(bus, ep->device->address));
348 bus->free_bw -= ep->bandwidth;
349
350 return EOK;
351}
352
353
354/** Release bandwidth reserved by the given endpoint.
355 */
356static int usb2_bus_unregister_ep(bus_t *bus_base, endpoint_t *ep)
357{
358 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
359 assert(ep);
360
361 list_remove(&ep->link);
362 ep->device = NULL;
363
364 bus->free_bw += ep->bandwidth;
365
366 return EOK;
367}
368
369static int usb2_bus_reset_toggle(bus_t *bus_base, usb_target_t target, bool all)
370{
371 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
372
373 if (!usb_target_is_valid(target))
374 return EINVAL;
375
376 int ret = ENOENT;
377
378 list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
379 assert(ep->device->address == target.address);
380
381 if (all || ep->endpoint == target.endpoint) {
382 endpoint_toggle_set(ep, 0);
383 ret = EOK;
384 }
385 }
386 return ret;
387}
388
389/** Unregister and destroy all endpoints using given address.
390 * @param bus usb_bus structure, non-null.
391 * @param address USB address.
392 * @param endpoint USB endpoint number.
393 * @param direction Communication direction.
394 * @return Error code.
395 */
396static int usb2_bus_release_address(bus_t *bus_base, usb_address_t address)
397{
398 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
399
400 if (!usb_address_is_valid(address))
401 return EINVAL;
402
403 const int ret = bus->devices[address].occupied ? EOK : ENOENT;
404 bus->devices[address].occupied = false;
405
406 list_t *list = get_list(bus, address);
407 for (link_t *link = list_first(list); link != NULL; ) {
408 endpoint_t *ep = list_get_instance(link, endpoint_t, link);
409 link = list_next(link, list);
410
411 assert(ep->device->address == address);
412 list_remove(&ep->link);
413
414 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
415 address, ep->endpoint, usb_str_direction(ep->direction));
416
417 /* Drop bus reference */
418 endpoint_del_ref(ep);
419 }
420
421 return ret;
422}
423
424/** Request USB address.
425 * @param bus usb_device_manager
426 * @param addr Pointer to requested address value, place to store new address
427 * @parma strict Fail if the requested address is not available.
428 * @return Error code.
429 * @note Default address is only available in strict mode.
430 */
431static int usb2_bus_request_address(bus_t *bus_base, usb_address_t *addr, bool strict, usb_speed_t speed)
432{
433 int err;
434
435 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
436 assert(addr);
437
438 if (!usb_address_is_valid(*addr))
439 return EINVAL;
440
441 /* Only grant default address to strict requests */
442 if ((*addr == USB_ADDRESS_DEFAULT) && !strict) {
443 if ((err = usb_bus_get_free_address(bus, addr)))
444 return err;
445 }
446 else if (bus->devices[*addr].occupied) {
447 if (strict) {
448 return ENOENT;
449 }
450 if ((err = usb_bus_get_free_address(bus, addr)))
451 return err;
452 }
453
454 assert(usb_address_is_valid(*addr));
455 assert(bus->devices[*addr].occupied == false);
456 assert(*addr != USB_ADDRESS_DEFAULT || strict);
457
458 bus->devices[*addr].occupied = true;
459 bus->devices[*addr].speed = speed;
460
461 return EOK;
462}
463
464static const bus_ops_t usb2_bus_ops = {
465 .enumerate_device = usb2_bus_enumerate_device,
466 .create_endpoint = usb2_bus_create_ep,
467 .find_endpoint = usb2_bus_find_ep,
468 .unregister_endpoint = usb2_bus_unregister_ep,
469 .register_endpoint = usb2_bus_register_ep,
470 .request_address = usb2_bus_request_address,
471 .release_address = usb2_bus_release_address,
472 .reset_toggle = usb2_bus_reset_toggle,
473};
474
475/** Initialize to default state.
476 *
477 * @param bus usb_bus structure, non-null.
478 * @param available_bandwidth Size of the bandwidth pool.
479 * @param bw_count function to use to calculate endpoint bw requirements.
480 * @return Error code.
481 */
482int usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth, count_bw_func_t count_bw)
483{
484 assert(bus);
485
486 bus_init(&bus->base, sizeof(device_t));
487
488 bus->base.ops = usb2_bus_ops;
489 bus->base.ops.count_bw = count_bw;
490
491 bus->free_bw = available_bandwidth;
492 bus->last_address = 0;
493 for (unsigned i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
494 list_initialize(&bus->devices[i].endpoint_list);
495 bus->devices[i].speed = USB_SPEED_MAX;
496 bus->devices[i].occupied = false;
497 }
498 return EOK;
499}
500/**
501 * @}
502 */
Note: See TracBrowser for help on using the repository browser.