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

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

usbhost: made device_remove and endpoint_unregister noexcept

  • Property mode set to 100644
File size: 7.8 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 *
34 * A bus_t implementation for USB 2 and lower. Implements USB 2 enumeration and
35 * configurable bandwidth counting.
36 */
37
38#include <assert.h>
39#include <errno.h>
40#include <macros.h>
41#include <stdbool.h>
42#include <stdlib.h>
43#include <str_error.h>
44#include <usb/debug.h>
45#include <usb/descriptor.h>
46#include <usb/request.h>
47#include <usb/usb.h>
48
49#include "endpoint.h"
50#include "ddf_helpers.h"
51
52#include "usb2_bus.h"
53
54/**
55 * Ops receive generic bus_t pointer.
56 */
57static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
58{
59 assert(bus_base);
60 return (usb2_bus_t *) bus_base;
61}
62
63/**
64 * Request a new address. A free address is found and marked as occupied.
65 *
66 * There's no need to synchronize this method, because it is called only with
67 * default address reserved.
68 *
69 * @param bus usb_device_manager
70 * @param addr Pointer to requested address value, place to store new address
71 */
72static int request_address(usb2_bus_t *bus, usb_address_t *addr)
73{
74 // Find a free address
75 usb_address_t new_address = bus->last_address;
76 do {
77 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
78 if (new_address == USB_ADDRESS_DEFAULT)
79 new_address = 1;
80 if (new_address == bus->last_address)
81 return ENOSPC;
82 } while (bus->address_occupied[new_address]);
83 bus->last_address = new_address;
84
85 *addr = new_address;
86 bus->address_occupied[*addr] = true;
87
88 return EOK;
89}
90
91/**
92 * Mark address as free.
93 */
94static void release_address(usb2_bus_t *bus, usb_address_t address)
95{
96 bus->address_occupied[address] = false;
97}
98
99static const usb_target_t usb2_default_target = {{
100 .address = USB_ADDRESS_DEFAULT,
101 .endpoint = 0,
102}};
103
104/**
105 * Transition the device to the addressed state.
106 *
107 * Reserve address, configure the control EP, issue a SET_ADDRESS command.
108 * Configure the device with the new address, mark the device as online.
109 */
110static int address_device(device_t *dev)
111{
112 int err;
113
114 usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
115
116 /* The default address is currently reserved for this device */
117 dev->address = USB_ADDRESS_DEFAULT;
118
119 /** Reserve address early, we want pretty log messages */
120 usb_address_t address = USB_ADDRESS_DEFAULT;
121 if ((err = request_address(bus, &address))) {
122 usb_log_error("Failed to reserve new address: %s.",
123 str_error(err));
124 return err;
125 }
126 usb_log_debug("Device(%d): Reserved new address.", address);
127
128 /* Add default pipe on default address */
129 usb_log_debug("Device(%d): Adding default target (0:0)", address);
130
131 usb_endpoint_descriptors_t ep0_desc = {
132 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
133 };
134 endpoint_t *default_ep;
135 err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
136 if (err != EOK) {
137 usb_log_error("Device(%d): Failed to add default target: %s.",
138 address, str_error(err));
139 goto err_address;
140 }
141
142 if ((err = hcd_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
143 goto err_address;
144
145 /* Set new address */
146 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
147
148 usb_log_debug("Device(%d): Setting USB address.", address);
149 err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
150 NULL, 0, *(uint64_t *)&set_address, "set address");
151 if (err != 0) {
152 usb_log_error("Device(%d): Failed to set new address: %s.",
153 address, str_error(err));
154 goto err_default_control_ep;
155 }
156
157 /* We need to remove ep before we change the address */
158 if ((err = bus_endpoint_remove(default_ep))) {
159 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
160 goto err_address;
161 }
162 endpoint_del_ref(default_ep);
163
164 dev->address = address;
165
166 /* Register EP on the new address */
167 usb_log_debug("Device(%d): Registering control EP.", address);
168 err = bus_endpoint_add(dev, &ep0_desc, NULL);
169 if (err != EOK) {
170 usb_log_error("Device(%d): Failed to register EP0: %s",
171 address, str_error(err));
172 goto err_address;
173 }
174
175 /* From now on, the device is officially online, yay! */
176 fibril_mutex_lock(&dev->guard);
177 dev->online = true;
178 fibril_mutex_unlock(&dev->guard);
179
180 return EOK;
181
182err_default_control_ep:
183 bus_endpoint_remove(default_ep);
184 endpoint_del_ref(default_ep);
185err_address:
186 release_address(bus, address);
187 return err;
188}
189
190/**
191 * Enumerate a USB device. Move it to the addressed state, then explore it
192 * to create a DDF function node with proper characteristics.
193 */
194static int usb2_bus_device_enumerate(device_t *dev)
195{
196 int err;
197 usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
198
199 /* The speed of the new device was reported by the hub when reserving
200 * default address.
201 */
202 dev->speed = bus->base.default_address_speed;
203 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
204
205 if (!dev->hub) {
206 /* The device is the roothub */
207 dev->tt = (usb_tt_address_t) {
208 .address = -1,
209 .port = 0,
210 };
211 } else {
212 hcd_setup_device_tt(dev);
213 }
214
215 /* Assign an address to the device */
216 if ((err = address_device(dev))) {
217 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
218 return err;
219 }
220
221 /* Read the device descriptor, derive the match ids */
222 if ((err = hcd_device_explore(dev))) {
223 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
224 release_address(bus, dev->address);
225 return err;
226 }
227
228 return EOK;
229}
230
231/**
232 * Register an endpoint to the bus. Reserves bandwidth.
233 */
234static int usb2_bus_register_ep(endpoint_t *ep)
235{
236 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
237 assert(fibril_mutex_is_locked(&bus->base.guard));
238 assert(ep);
239
240 /* Check for available bandwidth */
241 if (ep->bandwidth > bus->free_bw)
242 return ENOSPC;
243
244 bus->free_bw -= ep->bandwidth;
245
246 return EOK;
247}
248
249/**
250 * Release bandwidth reserved by the given endpoint.
251 */
252static void usb2_bus_unregister_ep(endpoint_t *ep)
253{
254 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
255 assert(ep);
256
257 bus->free_bw += ep->bandwidth;
258}
259
260const bus_ops_t usb2_bus_ops = {
261 .device_enumerate = usb2_bus_device_enumerate,
262 .endpoint_register = usb2_bus_register_ep,
263 .endpoint_unregister = usb2_bus_unregister_ep,
264};
265
266/** Initialize to default state.
267 *
268 * @param bus usb_bus structure, non-null.
269 * @param available_bandwidth Size of the bandwidth pool.
270 */
271void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
272{
273 assert(bus);
274
275 bus_init(&bus->base, sizeof(device_t));
276 bus->base.ops = &usb2_bus_ops;
277
278 bus->free_bw = available_bandwidth;
279}
280
281/**
282 * @}
283 */
Note: See TracBrowser for help on using the repository browser.