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 <assert.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <usb/debug.h>
|
---|
43 | #include <usb/descriptor.h>
|
---|
44 | #include <usb/request.h>
|
---|
45 | #include <usb/usb.h>
|
---|
46 |
|
---|
47 | #include "endpoint.h"
|
---|
48 | #include "ddf_helpers.h"
|
---|
49 |
|
---|
50 | #include "usb2_bus.h"
|
---|
51 |
|
---|
52 | /** Ops receive generic bus_t pointer. */
|
---|
53 | static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
|
---|
54 | {
|
---|
55 | assert(bus_base);
|
---|
56 | return (usb2_bus_t *) bus_base;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** Get list that holds endpoints for given address.
|
---|
60 | * @param bus usb2_bus structure, non-null.
|
---|
61 | * @param addr USB address, must be >= 0.
|
---|
62 | * @return Pointer to the appropriate list.
|
---|
63 | */
|
---|
64 | static list_t * get_list(usb2_bus_t *bus, usb_address_t addr)
|
---|
65 | {
|
---|
66 | assert(bus);
|
---|
67 | assert(addr >= 0);
|
---|
68 | return &bus->devices[addr % ARRAY_SIZE(bus->devices)].endpoint_list;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Get speed assigned to USB address.
|
---|
72 | *
|
---|
73 | * @param[in] bus Device manager structure to use.
|
---|
74 | * @param[in] address Address the caller wants to find.
|
---|
75 | * @param[out] speed Assigned speed.
|
---|
76 | * @return Error code.
|
---|
77 | */
|
---|
78 | static int usb2_bus_get_speed(bus_t *bus_base, usb_address_t address, usb_speed_t *speed)
|
---|
79 | {
|
---|
80 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
81 |
|
---|
82 | if (!usb_address_is_valid(address)) {
|
---|
83 | return EINVAL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | const int ret = bus->devices[address].occupied ? EOK : ENOENT;
|
---|
87 | if (speed && bus->devices[address].occupied) {
|
---|
88 | *speed = bus->devices[address].speed;
|
---|
89 | }
|
---|
90 |
|
---|
91 | return ret;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static const usb_endpoint_desc_t usb2_default_control_ep = {
|
---|
95 | .endpoint_no = 0,
|
---|
96 | .transfer_type = USB_TRANSFER_CONTROL,
|
---|
97 | .direction = USB_DIRECTION_BOTH,
|
---|
98 | .max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
99 | .packets = 1,
|
---|
100 | };
|
---|
101 |
|
---|
102 |
|
---|
103 | static const usb_target_t usb2_default_target = {{
|
---|
104 | .address = USB_ADDRESS_DEFAULT,
|
---|
105 | .endpoint = 0,
|
---|
106 | }};
|
---|
107 |
|
---|
108 | static int usb2_bus_address_device(bus_t *bus, hcd_t *hcd, device_t *dev)
|
---|
109 | {
|
---|
110 | int err;
|
---|
111 |
|
---|
112 | /* The default address is currently reserved for this device */
|
---|
113 | dev->address = USB_ADDRESS_DEFAULT;
|
---|
114 |
|
---|
115 | /** Reserve address early, we want pretty log messages */
|
---|
116 | usb_address_t address;
|
---|
117 | if ((err = bus_request_address(bus, &address, false, dev->speed))) {
|
---|
118 | usb_log_error("Failed to reserve new address: %s.",
|
---|
119 | str_error(err));
|
---|
120 | return err;
|
---|
121 | }
|
---|
122 | usb_log_debug("Device(%d): Reserved new address.", address);
|
---|
123 |
|
---|
124 | /* Add default pipe on default address */
|
---|
125 | usb_log_debug("Device(%d): Adding default target (0:0)", address);
|
---|
126 |
|
---|
127 | endpoint_t *default_ep;
|
---|
128 | err = bus_add_endpoint(bus, dev, &usb2_default_control_ep, &default_ep);
|
---|
129 | if (err != EOK) {
|
---|
130 | usb_log_error("Device(%d): Failed to add default target: %s.",
|
---|
131 | address, str_error(err));
|
---|
132 | goto err_address;
|
---|
133 | }
|
---|
134 |
|
---|
135 | uint16_t max_packet_size;
|
---|
136 | if ((err = hcd_get_ep0_max_packet_size(&max_packet_size, hcd, dev)))
|
---|
137 | goto err_address;
|
---|
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, dev, usb2_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(err));
|
---|
148 | goto err_default_control_ep;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* We need to remove ep before we change the address */
|
---|
152 | if ((err = bus_remove_endpoint(bus, default_ep))) {
|
---|
153 | usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
|
---|
154 | goto err_address;
|
---|
155 | }
|
---|
156 | endpoint_del_ref(default_ep);
|
---|
157 |
|
---|
158 | dev->address = address;
|
---|
159 |
|
---|
160 | const usb_endpoint_desc_t control_ep = {
|
---|
161 | .endpoint_no = 0,
|
---|
162 | .transfer_type = USB_TRANSFER_CONTROL,
|
---|
163 | .direction = USB_DIRECTION_BOTH,
|
---|
164 | .max_packet_size = max_packet_size,
|
---|
165 | .packets = 1,
|
---|
166 | };
|
---|
167 |
|
---|
168 | /* Register EP on the new address */
|
---|
169 | usb_log_debug("Device(%d): Registering control EP.", address);
|
---|
170 | err = bus_add_endpoint(bus, dev, &control_ep, NULL);
|
---|
171 | if (err != EOK) {
|
---|
172 | usb_log_error("Device(%d): Failed to register EP0: %s",
|
---|
173 | address, str_error(err));
|
---|
174 | goto err_address;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return EOK;
|
---|
178 |
|
---|
179 | err_default_control_ep:
|
---|
180 | bus_remove_endpoint(bus, default_ep);
|
---|
181 | endpoint_del_ref(default_ep);
|
---|
182 | err_address:
|
---|
183 | bus_release_address(bus, address);
|
---|
184 | return err;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Enumerate a new USB device
|
---|
188 | */
|
---|
189 | static int usb2_bus_enumerate_device(bus_t *bus, hcd_t *hcd, device_t *dev)
|
---|
190 | {
|
---|
191 | int err;
|
---|
192 |
|
---|
193 | /* The speed of the new device was reported by the hub when reserving
|
---|
194 | * default address.
|
---|
195 | */
|
---|
196 | if ((err = usb2_bus_get_speed(bus, USB_ADDRESS_DEFAULT, &dev->speed))) {
|
---|
197 | usb_log_error("Failed to verify speed: %s.", str_error(err));
|
---|
198 | return err;
|
---|
199 | }
|
---|
200 | usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
|
---|
201 |
|
---|
202 | if (!dev->hub) {
|
---|
203 | /* The device is the roothub */
|
---|
204 | dev->tt = (usb_tt_address_t) {
|
---|
205 | .address = -1,
|
---|
206 | .port = 0,
|
---|
207 | };
|
---|
208 | } else {
|
---|
209 | hcd_setup_device_tt(dev);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* Assign an address to the device */
|
---|
213 | if ((err = usb2_bus_address_device(bus, hcd, dev))) {
|
---|
214 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
---|
215 | return err;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /* Read the device descriptor, derive the match ids */
|
---|
219 | if ((err = hcd_ddf_device_explore(hcd, dev))) {
|
---|
220 | usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
|
---|
221 | bus_release_address(bus, dev->address);
|
---|
222 | return err;
|
---|
223 | }
|
---|
224 |
|
---|
225 | return EOK;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** Get a free USB address
|
---|
229 | *
|
---|
230 | * @param[in] bus Device manager structure to use.
|
---|
231 | * @return Free address, or error code.
|
---|
232 | */
|
---|
233 | static int usb_bus_get_free_address(usb2_bus_t *bus, usb_address_t *addr)
|
---|
234 | {
|
---|
235 | usb_address_t new_address = bus->last_address;
|
---|
236 | do {
|
---|
237 | new_address = (new_address + 1) % USB_ADDRESS_COUNT;
|
---|
238 | if (new_address == USB_ADDRESS_DEFAULT)
|
---|
239 | new_address = 1;
|
---|
240 | if (new_address == bus->last_address)
|
---|
241 | return ENOSPC;
|
---|
242 | } while (bus->devices[new_address].occupied);
|
---|
243 |
|
---|
244 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
245 | bus->last_address = new_address;
|
---|
246 |
|
---|
247 | *addr = new_address;
|
---|
248 | return EOK;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Find endpoint.
|
---|
252 | * @param bus usb_bus structure, non-null.
|
---|
253 | * @param target Endpoint address.
|
---|
254 | * @param direction Communication direction.
|
---|
255 | * @return Pointer to endpoint_t structure representing given communication
|
---|
256 | * target, NULL if there is no such endpoint registered.
|
---|
257 | * @note Assumes that the internal mutex is locked.
|
---|
258 | */
|
---|
259 | static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, device_t *device, usb_target_t target, usb_direction_t direction)
|
---|
260 | {
|
---|
261 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
262 |
|
---|
263 | assert(device->address == target.address);
|
---|
264 |
|
---|
265 | list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
|
---|
266 | if (((direction == ep->direction)
|
---|
267 | || (ep->direction == USB_DIRECTION_BOTH)
|
---|
268 | || (direction == USB_DIRECTION_BOTH))
|
---|
269 | && (target.endpoint == ep->endpoint))
|
---|
270 | return ep;
|
---|
271 | }
|
---|
272 | return NULL;
|
---|
273 | }
|
---|
274 |
|
---|
275 | static endpoint_t *usb2_bus_create_ep(bus_t *bus)
|
---|
276 | {
|
---|
277 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
278 | if (!ep)
|
---|
279 | return NULL;
|
---|
280 |
|
---|
281 | endpoint_init(ep, bus);
|
---|
282 | return ep;
|
---|
283 | }
|
---|
284 |
|
---|
285 | static usb_target_t usb2_ep_to_target(endpoint_t *ep)
|
---|
286 | {
|
---|
287 | assert(ep);
|
---|
288 | assert(ep->device);
|
---|
289 |
|
---|
290 | return (usb_target_t) {{
|
---|
291 | .address = ep->device->address,
|
---|
292 | .endpoint = ep->endpoint,
|
---|
293 | }};
|
---|
294 | }
|
---|
295 |
|
---|
296 | /** Register an endpoint to the bus. Reserves bandwidth.
|
---|
297 | * @param bus usb_bus structure, non-null.
|
---|
298 | * @param endpoint USB endpoint number.
|
---|
299 | */
|
---|
300 | static int usb2_bus_register_ep(bus_t *bus_base, device_t *device, endpoint_t *ep, const usb_endpoint_desc_t *desc)
|
---|
301 | {
|
---|
302 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
303 | assert(ep);
|
---|
304 |
|
---|
305 | ep->device = device;
|
---|
306 |
|
---|
307 | /* Extract USB2-related information from endpoint_desc */
|
---|
308 | ep->endpoint = desc->endpoint_no;
|
---|
309 | ep->direction = desc->direction;
|
---|
310 | ep->transfer_type = desc->transfer_type;
|
---|
311 | ep->max_packet_size = desc->max_packet_size;
|
---|
312 | ep->packets = desc->packets;
|
---|
313 |
|
---|
314 | ep->bandwidth = bus_base->ops.count_bw(ep, desc->max_packet_size);
|
---|
315 |
|
---|
316 | /* Check for existence */
|
---|
317 | if (usb2_bus_find_ep(bus_base, ep->device, usb2_ep_to_target(ep), ep->direction))
|
---|
318 | return EEXIST;
|
---|
319 |
|
---|
320 | /* Check for available bandwidth */
|
---|
321 | if (ep->bandwidth > bus->free_bw)
|
---|
322 | return ENOSPC;
|
---|
323 |
|
---|
324 | list_append(&ep->link, get_list(bus, ep->device->address));
|
---|
325 | bus->free_bw -= ep->bandwidth;
|
---|
326 |
|
---|
327 | return EOK;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | /** Release bandwidth reserved by the given endpoint.
|
---|
332 | */
|
---|
333 | static int usb2_bus_unregister_ep(bus_t *bus_base, endpoint_t *ep)
|
---|
334 | {
|
---|
335 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
336 | assert(ep);
|
---|
337 |
|
---|
338 | list_remove(&ep->link);
|
---|
339 | ep->device = NULL;
|
---|
340 |
|
---|
341 | bus->free_bw += ep->bandwidth;
|
---|
342 |
|
---|
343 | return EOK;
|
---|
344 | }
|
---|
345 |
|
---|
346 | static int usb2_bus_reset_toggle(bus_t *bus_base, usb_target_t target, toggle_reset_mode_t mode)
|
---|
347 | {
|
---|
348 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
349 |
|
---|
350 | if (!usb_target_is_valid(target))
|
---|
351 | return EINVAL;
|
---|
352 |
|
---|
353 | if (mode == RESET_NONE)
|
---|
354 | return EOK;
|
---|
355 |
|
---|
356 | int ret = ENOENT;
|
---|
357 |
|
---|
358 | list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
|
---|
359 | assert(ep->device->address == target.address);
|
---|
360 |
|
---|
361 | if (mode == RESET_ALL || ep->endpoint == target.endpoint) {
|
---|
362 | endpoint_toggle_set(ep, 0);
|
---|
363 | ret = EOK;
|
---|
364 | }
|
---|
365 | }
|
---|
366 | return ret;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /** Unregister and destroy all endpoints using given address.
|
---|
370 | * @param bus usb_bus structure, non-null.
|
---|
371 | * @param address USB address.
|
---|
372 | * @param endpoint USB endpoint number.
|
---|
373 | * @param direction Communication direction.
|
---|
374 | * @return Error code.
|
---|
375 | */
|
---|
376 | static int usb2_bus_release_address(bus_t *bus_base, usb_address_t address)
|
---|
377 | {
|
---|
378 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
379 |
|
---|
380 | if (!usb_address_is_valid(address))
|
---|
381 | return EINVAL;
|
---|
382 |
|
---|
383 | const int ret = bus->devices[address].occupied ? EOK : ENOENT;
|
---|
384 | bus->devices[address].occupied = false;
|
---|
385 |
|
---|
386 | list_t *list = get_list(bus, address);
|
---|
387 | for (link_t *link = list_first(list); link != NULL; ) {
|
---|
388 | endpoint_t *ep = list_get_instance(link, endpoint_t, link);
|
---|
389 | link = list_next(link, list);
|
---|
390 |
|
---|
391 | assert(ep->device->address == address);
|
---|
392 | list_remove(&ep->link);
|
---|
393 |
|
---|
394 | usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
|
---|
395 | address, ep->endpoint, usb_str_direction(ep->direction));
|
---|
396 |
|
---|
397 | /* Drop bus reference */
|
---|
398 | endpoint_del_ref(ep);
|
---|
399 | }
|
---|
400 |
|
---|
401 | return ret;
|
---|
402 | }
|
---|
403 |
|
---|
404 | /** Request USB address.
|
---|
405 | * @param bus usb_device_manager
|
---|
406 | * @param addr Pointer to requested address value, place to store new address
|
---|
407 | * @parma strict Fail if the requested address is not available.
|
---|
408 | * @return Error code.
|
---|
409 | * @note Default address is only available in strict mode.
|
---|
410 | */
|
---|
411 | static int usb2_bus_request_address(bus_t *bus_base, usb_address_t *addr, bool strict, usb_speed_t speed)
|
---|
412 | {
|
---|
413 | int err;
|
---|
414 |
|
---|
415 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
416 | assert(addr);
|
---|
417 |
|
---|
418 | if (!usb_address_is_valid(*addr))
|
---|
419 | return EINVAL;
|
---|
420 |
|
---|
421 | /* Only grant default address to strict requests */
|
---|
422 | if ((*addr == USB_ADDRESS_DEFAULT) && !strict) {
|
---|
423 | if ((err = usb_bus_get_free_address(bus, addr)))
|
---|
424 | return err;
|
---|
425 | }
|
---|
426 | else if (bus->devices[*addr].occupied) {
|
---|
427 | if (strict) {
|
---|
428 | return ENOENT;
|
---|
429 | }
|
---|
430 | if ((err = usb_bus_get_free_address(bus, addr)))
|
---|
431 | return err;
|
---|
432 | }
|
---|
433 |
|
---|
434 | assert(usb_address_is_valid(*addr));
|
---|
435 | assert(bus->devices[*addr].occupied == false);
|
---|
436 | assert(*addr != USB_ADDRESS_DEFAULT || strict);
|
---|
437 |
|
---|
438 | bus->devices[*addr].occupied = true;
|
---|
439 | bus->devices[*addr].speed = speed;
|
---|
440 |
|
---|
441 | return EOK;
|
---|
442 | }
|
---|
443 |
|
---|
444 | static const bus_ops_t usb2_bus_ops = {
|
---|
445 | .enumerate_device = usb2_bus_enumerate_device,
|
---|
446 | .create_endpoint = usb2_bus_create_ep,
|
---|
447 | .find_endpoint = usb2_bus_find_ep,
|
---|
448 | .unregister_endpoint = usb2_bus_unregister_ep,
|
---|
449 | .register_endpoint = usb2_bus_register_ep,
|
---|
450 | .request_address = usb2_bus_request_address,
|
---|
451 | .release_address = usb2_bus_release_address,
|
---|
452 | .reset_toggle = usb2_bus_reset_toggle,
|
---|
453 | };
|
---|
454 |
|
---|
455 | /** Initialize to default state.
|
---|
456 | *
|
---|
457 | * @param bus usb_bus structure, non-null.
|
---|
458 | * @param available_bandwidth Size of the bandwidth pool.
|
---|
459 | * @param bw_count function to use to calculate endpoint bw requirements.
|
---|
460 | * @return Error code.
|
---|
461 | */
|
---|
462 | int usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth, count_bw_func_t count_bw)
|
---|
463 | {
|
---|
464 | assert(bus);
|
---|
465 |
|
---|
466 | bus_init(&bus->base, sizeof(device_t));
|
---|
467 |
|
---|
468 | bus->base.ops = usb2_bus_ops;
|
---|
469 | bus->base.ops.count_bw = count_bw;
|
---|
470 |
|
---|
471 | bus->free_bw = available_bandwidth;
|
---|
472 | bus->last_address = 0;
|
---|
473 | for (unsigned i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
|
---|
474 | list_initialize(&bus->devices[i].endpoint_list);
|
---|
475 | bus->devices[i].speed = USB_SPEED_MAX;
|
---|
476 | bus->devices[i].occupied = false;
|
---|
477 | }
|
---|
478 | return EOK;
|
---|
479 | }
|
---|
480 | /**
|
---|
481 | * @}
|
---|
482 | */
|
---|