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

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

usbhost: reserve default speed in library

  • Property mode set to 100644
File size: 8.6 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 * @return Error code.
103 * @note Default address is only available in strict mode.
104 */
105static int request_address(usb2_bus_t *bus, usb_address_t *addr)
106{
107 int err;
108
109 assert(bus);
110 assert(addr);
111
112 if (!usb_address_is_valid(*addr))
113 return EINVAL;
114
115 if ((err = get_free_address(bus, addr)))
116 return err;
117
118 assert(usb_address_is_valid(*addr));
119 assert(bus->address_occupied[*addr] == false);
120 assert(*addr != USB_ADDRESS_DEFAULT);
121
122 bus->address_occupied[*addr] = true;
123
124 return EOK;
125}
126
127static const usb_target_t usb2_default_target = {{
128 .address = USB_ADDRESS_DEFAULT,
129 .endpoint = 0,
130}};
131
132static int address_device(device_t *dev)
133{
134 int err;
135
136 usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
137
138 /* The default address is currently reserved for this device */
139 dev->address = USB_ADDRESS_DEFAULT;
140
141 /** Reserve address early, we want pretty log messages */
142 usb_address_t address = USB_ADDRESS_DEFAULT;
143 if ((err = request_address(bus, &address))) {
144 usb_log_error("Failed to reserve new address: %s.",
145 str_error(err));
146 return err;
147 }
148 usb_log_debug("Device(%d): Reserved new address.", address);
149
150 /* Add default pipe on default address */
151 usb_log_debug("Device(%d): Adding default target (0:0)", address);
152
153 usb_endpoint_descriptors_t ep0_desc = {
154 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
155 };
156 endpoint_t *default_ep;
157 err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
158 if (err != EOK) {
159 usb_log_error("Device(%d): Failed to add default target: %s.",
160 address, str_error(err));
161 goto err_address;
162 }
163
164 if ((err = hcd_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
165 goto err_address;
166
167 /* Set new address */
168 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
169
170 usb_log_debug("Device(%d): Setting USB address.", address);
171 err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
172 NULL, 0, *(uint64_t *)&set_address, "set address");
173 if (err != 0) {
174 usb_log_error("Device(%d): Failed to set new address: %s.",
175 address, str_error(err));
176 goto err_default_control_ep;
177 }
178
179 /* We need to remove ep before we change the address */
180 if ((err = bus_endpoint_remove(default_ep))) {
181 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
182 goto err_address;
183 }
184 endpoint_del_ref(default_ep);
185
186 dev->address = address;
187
188 /* Register EP on the new address */
189 usb_log_debug("Device(%d): Registering control EP.", address);
190 err = bus_endpoint_add(dev, &ep0_desc, NULL);
191 if (err != EOK) {
192 usb_log_error("Device(%d): Failed to register EP0: %s",
193 address, str_error(err));
194 goto err_address;
195 }
196
197 /* From now on, the device is officially online, yay! */
198 fibril_mutex_lock(&dev->guard);
199 dev->online = true;
200 fibril_mutex_unlock(&dev->guard);
201
202 return EOK;
203
204err_default_control_ep:
205 bus_endpoint_remove(default_ep);
206 endpoint_del_ref(default_ep);
207err_address:
208 release_address(bus, address);
209 return err;
210}
211
212/** Enumerate a new USB device
213 */
214static int usb2_bus_device_enumerate(device_t *dev)
215{
216 int err;
217 usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
218
219 /* The speed of the new device was reported by the hub when reserving
220 * default address.
221 */
222 dev->speed = bus->base.default_address_speed;
223 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
224
225 if (!dev->hub) {
226 /* The device is the roothub */
227 dev->tt = (usb_tt_address_t) {
228 .address = -1,
229 .port = 0,
230 };
231 } else {
232 hcd_setup_device_tt(dev);
233 }
234
235 /* Assign an address to the device */
236 if ((err = address_device(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_device_explore(dev))) {
243 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
244 release_address(bus, dev->address);
245 return err;
246 }
247
248 return EOK;
249}
250
251static endpoint_t *usb2_bus_create_ep(device_t *dev, const usb_endpoint_descriptors_t *desc)
252{
253 endpoint_t *ep = malloc(sizeof(endpoint_t));
254 if (!ep)
255 return NULL;
256
257 endpoint_init(ep, dev, desc);
258 return ep;
259}
260
261/** Register an endpoint to the bus. Reserves bandwidth.
262 * @param bus usb_bus structure, non-null.
263 * @param endpoint USB endpoint number.
264 */
265static int usb2_bus_register_ep(endpoint_t *ep)
266{
267 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
268 assert(fibril_mutex_is_locked(&bus->base.guard));
269 assert(ep);
270
271 /* Check for available bandwidth */
272 if (ep->bandwidth > bus->free_bw)
273 return ENOSPC;
274
275 bus->free_bw -= ep->bandwidth;
276
277 return EOK;
278}
279
280/** Release bandwidth reserved by the given endpoint.
281 */
282static int usb2_bus_unregister_ep(endpoint_t *ep)
283{
284 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
285 assert(ep);
286
287 bus->free_bw += ep->bandwidth;
288
289 return EOK;
290}
291
292const bus_ops_t usb2_bus_ops = {
293 .device_enumerate = usb2_bus_device_enumerate,
294 .endpoint_create = usb2_bus_create_ep,
295 .endpoint_register = usb2_bus_register_ep,
296 .endpoint_unregister = usb2_bus_unregister_ep,
297};
298
299/** Initialize to default state.
300 *
301 * @param bus usb_bus structure, non-null.
302 * @param available_bandwidth Size of the bandwidth pool.
303 * @param bw_count function to use to calculate endpoint bw requirements.
304 * @return Error code.
305 */
306void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
307{
308 assert(bus);
309
310 bus_init(&bus->base, sizeof(device_t));
311 bus->base.ops = &usb2_bus_ops;
312
313 bus->free_bw = available_bandwidth;
314}
315/**
316 * @}
317 */
Note: See TracBrowser for help on using the repository browser.