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

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

usbhost refactoring: introduced bus→enumerate_device

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