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

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

usbhost endpoint: removed target

The reasons for having usb_target_t inside endpoint have been dismissed. Enpoint is not a target of a transaction, so this was just misleading.

  • Property mode set to 100644
File size: 13.7 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 /** Reserve address early, we want pretty log messages */
112 const usb_address_t address = bus_reserve_default_address(bus, dev->speed);
113 if (address < 0) {
114 usb_log_error("Failed to reserve new address: %s.",
115 str_error(address));
116 return address;
117 }
118 usb_log_debug("Device(%d): Reserved new address.", address);
119
120 /* Add default pipe on default address */
121 usb_log_debug("Device(%d): Adding default target (0:0)", address);
122
123 endpoint_t *default_ep;
124 err = bus_add_endpoint(bus, dev, &usb2_default_control_ep, &default_ep);
125 if (err != EOK) {
126 usb_log_error("Device(%d): Failed to add default target: %s.",
127 address, str_error(err));
128 goto err_address;
129 }
130
131 /* Get max packet size for default pipe */
132 usb_standard_device_descriptor_t desc = { 0 };
133 const usb_device_request_setup_packet_t get_device_desc_8 =
134 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
135
136 usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
137 address);
138 ssize_t got = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_IN,
139 (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
140 "read first 8 bytes of dev descriptor");
141
142 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
143 err = got < 0 ? got : EOVERFLOW;
144 usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
145 address, str_error(err));
146 goto err_default_control_ep;
147 }
148
149 /* Set new address */
150 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
151
152 usb_log_debug("Device(%d): Setting USB address.", address);
153 err = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_OUT,
154 NULL, 0, *(uint64_t *)&set_address, "set address");
155 if (err != 0) {
156 usb_log_error("Device(%d): Failed to set new address: %s.",
157 address, str_error(got));
158 goto err_default_control_ep;
159 }
160
161 dev->address = address;
162
163 const usb_endpoint_desc_t control_ep = {
164 .endpoint_no = 0,
165 .transfer_type = USB_TRANSFER_CONTROL,
166 .direction = USB_DIRECTION_BOTH,
167 .max_packet_size = ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
168 .packets = ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc.max_packet_size)),
169 };
170
171 /* Register EP on the new address */
172 usb_log_debug("Device(%d): Registering control EP.", address);
173 err = bus_add_endpoint(bus, dev, &control_ep, NULL);
174 if (err != EOK) {
175 usb_log_error("Device(%d): Failed to register EP0: %s",
176 address, str_error(err));
177 goto err_default_control_ep;
178 }
179
180 err = bus_remove_endpoint(bus, default_ep);
181 assert(err == EOK);
182 endpoint_del_ref(default_ep);
183
184 err = bus_release_address(bus, address);
185 assert(err == EOK);
186
187 return EOK;
188
189err_default_control_ep:
190 bus_remove_endpoint(bus, default_ep);
191 endpoint_del_ref(default_ep);
192err_address:
193 bus_release_address(bus, address);
194 return err;
195}
196
197/** Enumerate a new USB device
198 */
199static int usb2_bus_enumerate_device(bus_t *bus, hcd_t *hcd, device_t *dev)
200{
201 int err;
202
203 /* The speed of the new device was reported by the hub when reserving
204 * default address.
205 */
206 if ((err = usb2_bus_get_speed(bus, USB_ADDRESS_DEFAULT, &dev->speed))) {
207 usb_log_error("Failed to verify speed: %s.", str_error(err));
208 return err;
209 }
210 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
211
212 /* Manage TT */
213 if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
214 /* For LS devices under HS hub */
215 /* TODO: How about SS hubs? */
216 dev->tt.address = dev->hub->address;
217 dev->tt.port = dev->port;
218 }
219 else {
220 /* Inherit hub's TT */
221 dev->tt = dev->hub->tt;
222 }
223
224 /* Assign an address to the device */
225 if ((err = usb2_bus_address_device(bus, hcd, dev))) {
226 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
227 return err;
228 }
229
230 /* Read the device descriptor, derive the match ids */
231 if ((err = hcd_ddf_device_explore(hcd, dev))) {
232 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
233 bus_release_address(bus, dev->address);
234 return err;
235 }
236
237 return EOK;
238}
239
240/** Get a free USB address
241 *
242 * @param[in] bus Device manager structure to use.
243 * @return Free address, or error code.
244 */
245static int usb_bus_get_free_address(usb2_bus_t *bus, usb_address_t *addr)
246{
247 usb_address_t new_address = bus->last_address;
248 do {
249 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
250 if (new_address == USB_ADDRESS_DEFAULT)
251 new_address = 1;
252 if (new_address == bus->last_address)
253 return ENOSPC;
254 } while (bus->devices[new_address].occupied);
255
256 assert(new_address != USB_ADDRESS_DEFAULT);
257 bus->last_address = new_address;
258
259 *addr = new_address;
260 return EOK;
261}
262
263/** Find endpoint.
264 * @param bus usb_bus structure, non-null.
265 * @param target Endpoint address.
266 * @param direction Communication direction.
267 * @return Pointer to endpoint_t structure representing given communication
268 * target, NULL if there is no such endpoint registered.
269 * @note Assumes that the internal mutex is locked.
270 */
271static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, device_t *device, usb_target_t target, usb_direction_t direction)
272{
273 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
274
275 assert(device->address == target.address);
276
277 list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
278 if (((direction == ep->direction)
279 || (ep->direction == USB_DIRECTION_BOTH)
280 || (direction == USB_DIRECTION_BOTH))
281 && (target.endpoint == ep->endpoint))
282 return ep;
283 }
284 return NULL;
285}
286
287static endpoint_t *usb2_bus_create_ep(bus_t *bus)
288{
289 endpoint_t *ep = malloc(sizeof(endpoint_t));
290 if (!ep)
291 return NULL;
292
293 endpoint_init(ep, bus);
294 return ep;
295}
296
297static usb_target_t usb2_ep_to_target(endpoint_t *ep)
298{
299 assert(ep);
300 assert(ep->device);
301
302 return (usb_target_t) {{
303 .address = ep->device->address,
304 .endpoint = ep->endpoint,
305 }};
306}
307
308/** Register an endpoint to the bus. Reserves bandwidth.
309 * @param bus usb_bus structure, non-null.
310 * @param endpoint USB endpoint number.
311 */
312static int usb2_bus_register_ep(bus_t *bus_base, endpoint_t *ep, const usb_endpoint_desc_t *desc)
313{
314 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
315 assert(ep);
316
317 assert(ep->device);
318
319 /* Extract USB2-related information from endpoint_desc */
320 ep->endpoint = desc->endpoint_no;
321 ep->direction = desc->direction;
322 ep->transfer_type = desc->transfer_type;
323 ep->max_packet_size = desc->max_packet_size;
324 ep->packets = desc->packets;
325
326 ep->bandwidth = bus_base->ops.count_bw(ep, desc->max_packet_size);
327
328 /* Check for existence */
329 if (usb2_bus_find_ep(bus_base, ep->device, usb2_ep_to_target(ep), ep->direction))
330 return EEXIST;
331
332 /* Check for available bandwidth */
333 if (ep->bandwidth > bus->free_bw)
334 return ENOSPC;
335
336 list_append(&ep->link, get_list(bus, ep->device->address));
337 bus->free_bw -= ep->bandwidth;
338
339 return EOK;
340}
341
342
343/** Release bandwidth reserved by the given endpoint.
344 */
345static int usb2_bus_unregister_ep(bus_t *bus_base, endpoint_t *ep)
346{
347 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
348 assert(ep);
349
350 bus->free_bw += ep->bandwidth;
351
352 return EOK;
353}
354
355static int usb2_bus_reset_toggle(bus_t *bus_base, usb_target_t target, bool all)
356{
357 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
358
359 if (!usb_target_is_valid(target))
360 return EINVAL;
361
362 int ret = ENOENT;
363
364 list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
365 assert(ep->device->address == target.address);
366
367 if (all || ep->endpoint == target.endpoint) {
368 endpoint_toggle_set(ep, 0);
369 ret = EOK;
370 }
371 }
372 return ret;
373}
374
375/** Unregister and destroy all endpoints using given address.
376 * @param bus usb_bus structure, non-null.
377 * @param address USB address.
378 * @param endpoint USB endpoint number.
379 * @param direction Communication direction.
380 * @return Error code.
381 */
382static int usb2_bus_release_address(bus_t *bus_base, usb_address_t address)
383{
384 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
385
386 if (!usb_address_is_valid(address))
387 return EINVAL;
388
389 const int ret = bus->devices[address].occupied ? EOK : ENOENT;
390 bus->devices[address].occupied = false;
391
392 list_t *list = get_list(bus, address);
393 for (link_t *link = list_first(list); link != NULL; ) {
394 endpoint_t *ep = list_get_instance(link, endpoint_t, link);
395 link = list_next(link, list);
396
397 assert(ep->device->address == address);
398 list_remove(&ep->link);
399
400 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
401 address, ep->endpoint, usb_str_direction(ep->direction));
402
403 /* Drop bus reference */
404 endpoint_del_ref(ep);
405 }
406
407 return ret;
408}
409
410/** Request USB address.
411 * @param bus usb_device_manager
412 * @param addr Pointer to requested address value, place to store new address
413 * @parma strict Fail if the requested address is not available.
414 * @return Error code.
415 * @note Default address is only available in strict mode.
416 */
417static int usb2_bus_request_address(bus_t *bus_base, usb_address_t *addr, bool strict, usb_speed_t speed)
418{
419 int err;
420
421 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
422 assert(addr);
423
424 if (!usb_address_is_valid(*addr))
425 return EINVAL;
426
427 /* Only grant default address to strict requests */
428 if ((*addr == USB_ADDRESS_DEFAULT) && !strict) {
429 if ((err = usb_bus_get_free_address(bus, addr)))
430 return err;
431 }
432 else if (bus->devices[*addr].occupied) {
433 if (strict) {
434 return ENOENT;
435 }
436 if ((err = usb_bus_get_free_address(bus, addr)))
437 return err;
438 }
439
440 assert(usb_address_is_valid(*addr));
441 assert(bus->devices[*addr].occupied == false);
442 assert(*addr != USB_ADDRESS_DEFAULT || strict);
443
444 bus->devices[*addr].occupied = true;
445 bus->devices[*addr].speed = speed;
446
447 return EOK;
448}
449
450static const bus_ops_t usb2_bus_ops = {
451 .enumerate_device = usb2_bus_enumerate_device,
452 .create_endpoint = usb2_bus_create_ep,
453 .find_endpoint = usb2_bus_find_ep,
454 .unregister_endpoint = usb2_bus_unregister_ep,
455 .register_endpoint = usb2_bus_register_ep,
456 .request_address = usb2_bus_request_address,
457 .release_address = usb2_bus_release_address,
458 .reset_toggle = usb2_bus_reset_toggle,
459};
460
461/** Initialize to default state.
462 *
463 * @param bus usb_bus structure, non-null.
464 * @param available_bandwidth Size of the bandwidth pool.
465 * @param bw_count function to use to calculate endpoint bw requirements.
466 * @return Error code.
467 */
468int usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth, count_bw_func_t count_bw)
469{
470 assert(bus);
471
472 bus_init(&bus->base, sizeof(device_t));
473
474 bus->base.ops = usb2_bus_ops;
475 bus->base.ops.count_bw = count_bw;
476
477 bus->free_bw = available_bandwidth;
478 bus->last_address = 0;
479 for (unsigned i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
480 list_initialize(&bus->devices[i].endpoint_list);
481 bus->devices[i].speed = USB_SPEED_MAX;
482 bus->devices[i].occupied = false;
483 }
484 return EOK;
485}
486/**
487 * @}
488 */
Note: See TracBrowser for help on using the repository browser.