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

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

usbhost: manage endpoints by library + get/set_toggle → reset_toggle

That simplifies things A LOT. Now you can find endpoints for device in
an array inside device. This array is managed automatically in
register/unregister endpoint. HC drivers still needs to write to it when
setting up/tearing down the device.

Sorry for these two changes being in one commit, but splitting them
would be simply more work for no benefit.

  • Property mode set to 100644
File size: 9.5 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 <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. */
53static 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 a free USB address
60 *
61 * @param[in] bus Device manager structure to use.
62 * @return Free address, or error code.
63 */
64static int get_free_address(usb2_bus_t *bus, usb_address_t *addr)
65{
66 usb_address_t new_address = bus->last_address;
67 do {
68 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
69 if (new_address == USB_ADDRESS_DEFAULT)
70 new_address = 1;
71 if (new_address == bus->last_address)
72 return ENOSPC;
73 } while (bus->address_occupied[new_address]);
74
75 assert(new_address != USB_ADDRESS_DEFAULT);
76 bus->last_address = new_address;
77
78 *addr = new_address;
79 return EOK;
80}
81
82/** Unregister and destroy all endpoints using given address.
83 * @param bus usb_bus structure, non-null.
84 * @param address USB address.
85 * @param endpoint USB endpoint number.
86 * @param direction Communication direction.
87 * @return Error code.
88 */
89static int release_address(usb2_bus_t *bus, usb_address_t address)
90{
91 if (!usb_address_is_valid(address))
92 return EINVAL;
93
94 const int ret = bus->address_occupied[address] ? EOK : ENOENT;
95 bus->address_occupied[address] = false;
96 return ret;
97}
98
99/** Request USB address.
100 * @param bus usb_device_manager
101 * @param addr Pointer to requested address value, place to store new address
102 * @parma strict Fail if the requested address is not available.
103 * @return Error code.
104 * @note Default address is only available in strict mode.
105 */
106static int request_address(usb2_bus_t *bus, usb_address_t *addr, bool strict)
107{
108 int err;
109
110 assert(bus);
111 assert(addr);
112
113 if (!usb_address_is_valid(*addr))
114 return EINVAL;
115
116 /* Only grant default address to strict requests */
117 if ((*addr == USB_ADDRESS_DEFAULT) && !strict) {
118 if ((err = get_free_address(bus, addr)))
119 return err;
120 }
121 else if (bus->address_occupied[*addr]) {
122 if (strict) {
123 return ENOENT;
124 }
125 if ((err = get_free_address(bus, addr)))
126 return err;
127 }
128
129 assert(usb_address_is_valid(*addr));
130 assert(bus->address_occupied[*addr] == false);
131 assert(*addr != USB_ADDRESS_DEFAULT || strict);
132
133 bus->address_occupied[*addr] = true;
134
135 return EOK;
136}
137
138static const usb_target_t usb2_default_target = {{
139 .address = USB_ADDRESS_DEFAULT,
140 .endpoint = 0,
141}};
142
143static int address_device(device_t *dev)
144{
145 int err;
146
147 usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
148
149 /* The default address is currently reserved for this device */
150 dev->address = USB_ADDRESS_DEFAULT;
151
152 /** Reserve address early, we want pretty log messages */
153 usb_address_t address = USB_ADDRESS_DEFAULT;
154 if ((err = request_address(bus, &address, false))) {
155 usb_log_error("Failed to reserve new address: %s.",
156 str_error(err));
157 return err;
158 }
159 usb_log_debug("Device(%d): Reserved new address.", address);
160
161 /* Add default pipe on default address */
162 usb_log_debug("Device(%d): Adding default target (0:0)", address);
163
164 usb_endpoint_descriptors_t ep0_desc = {
165 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
166 };
167 endpoint_t *default_ep;
168 err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
169 if (err != EOK) {
170 usb_log_error("Device(%d): Failed to add default target: %s.",
171 address, str_error(err));
172 goto err_address;
173 }
174
175 if ((err = hcd_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
176 goto err_address;
177
178 /* Set new address */
179 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
180
181 usb_log_debug("Device(%d): Setting USB address.", address);
182 err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
183 NULL, 0, *(uint64_t *)&set_address, "set address");
184 if (err != 0) {
185 usb_log_error("Device(%d): Failed to set new address: %s.",
186 address, str_error(err));
187 goto err_default_control_ep;
188 }
189
190 /* We need to remove ep before we change the address */
191 if ((err = bus_endpoint_remove(default_ep))) {
192 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
193 goto err_address;
194 }
195 endpoint_del_ref(default_ep);
196
197 dev->address = address;
198
199 /* Register EP on the new address */
200 usb_log_debug("Device(%d): Registering control EP.", address);
201 err = bus_endpoint_add(dev, &ep0_desc, NULL);
202 if (err != EOK) {
203 usb_log_error("Device(%d): Failed to register EP0: %s",
204 address, str_error(err));
205 goto err_address;
206 }
207
208 /* From now on, the device is officially online, yay! */
209 fibril_mutex_lock(&dev->guard);
210 dev->online = true;
211 fibril_mutex_unlock(&dev->guard);
212
213 return EOK;
214
215err_default_control_ep:
216 bus_endpoint_remove(default_ep);
217 endpoint_del_ref(default_ep);
218err_address:
219 release_address(bus, address);
220 return err;
221}
222
223/** Enumerate a new USB device
224 */
225static int usb2_bus_device_enumerate(device_t *dev)
226{
227 int err;
228 usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
229
230 /* The speed of the new device was reported by the hub when reserving
231 * default address.
232 */
233 dev->speed = bus->default_address_speed;
234 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
235
236 if (!dev->hub) {
237 /* The device is the roothub */
238 dev->tt = (usb_tt_address_t) {
239 .address = -1,
240 .port = 0,
241 };
242 } else {
243 hcd_setup_device_tt(dev);
244 }
245
246 /* Assign an address to the device */
247 if ((err = address_device(dev))) {
248 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
249 return err;
250 }
251
252 /* Read the device descriptor, derive the match ids */
253 if ((err = hcd_device_explore(dev))) {
254 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
255 release_address(bus, dev->address);
256 return err;
257 }
258
259 return EOK;
260}
261
262static endpoint_t *usb2_bus_create_ep(device_t *dev, const usb_endpoint_descriptors_t *desc)
263{
264 endpoint_t *ep = malloc(sizeof(endpoint_t));
265 if (!ep)
266 return NULL;
267
268 endpoint_init(ep, dev, desc);
269 return ep;
270}
271
272/** Register an endpoint to the bus. Reserves bandwidth.
273 * @param bus usb_bus structure, non-null.
274 * @param endpoint USB endpoint number.
275 */
276static int usb2_bus_register_ep(endpoint_t *ep)
277{
278 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
279 assert(fibril_mutex_is_locked(&bus->base.guard));
280 assert(ep);
281
282 /* Check for available bandwidth */
283 if (ep->bandwidth > bus->free_bw)
284 return ENOSPC;
285
286 bus->free_bw -= ep->bandwidth;
287
288 return EOK;
289}
290
291/** Release bandwidth reserved by the given endpoint.
292 */
293static int usb2_bus_unregister_ep(endpoint_t *ep)
294{
295 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
296 assert(ep);
297
298 bus->free_bw += ep->bandwidth;
299
300 return EOK;
301}
302
303static int usb2_bus_register_default_address(bus_t *bus_base, usb_speed_t speed)
304{
305 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
306 usb_address_t addr = USB_ADDRESS_DEFAULT;
307 const int err = request_address(bus, &addr, true);
308 if (err)
309 return err;
310 bus->default_address_speed = speed;
311 return EOK;
312}
313
314static int usb2_bus_release_default_address(bus_t *bus_base)
315{
316 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
317 return release_address(bus, USB_ADDRESS_DEFAULT);
318}
319
320const bus_ops_t usb2_bus_ops = {
321 .reserve_default_address = usb2_bus_register_default_address,
322 .release_default_address = usb2_bus_release_default_address,
323 .device_enumerate = usb2_bus_device_enumerate,
324 .endpoint_create = usb2_bus_create_ep,
325 .endpoint_register = usb2_bus_register_ep,
326 .endpoint_unregister = usb2_bus_unregister_ep,
327};
328
329/** Initialize to default state.
330 *
331 * @param bus usb_bus structure, non-null.
332 * @param available_bandwidth Size of the bandwidth pool.
333 * @param bw_count function to use to calculate endpoint bw requirements.
334 * @return Error code.
335 */
336int usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
337{
338 assert(bus);
339
340 bus_init(&bus->base, sizeof(device_t));
341 bus->base.ops = &usb2_bus_ops;
342
343 bus->free_bw = available_bandwidth;
344
345 return EOK;
346}
347/**
348 * @}
349 */
Note: See TracBrowser for help on using the repository browser.