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 | */
|
---|
57 | static 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 | */
|
---|
72 | static 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 | */
|
---|
94 | static void release_address(usb2_bus_t *bus, usb_address_t address)
|
---|
95 | {
|
---|
96 | bus->address_occupied[address] = false;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static 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,
|
---|
109 | */
|
---|
110 | static 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 |
|
---|
163 | dev->address = address;
|
---|
164 |
|
---|
165 | /* Register EP on the new address */
|
---|
166 | usb_log_debug("Device(%d): Registering control EP.", address);
|
---|
167 | err = bus_endpoint_add(dev, &ep0_desc, NULL);
|
---|
168 | if (err != EOK) {
|
---|
169 | usb_log_error("Device(%d): Failed to register EP0: %s",
|
---|
170 | address, str_error(err));
|
---|
171 | goto err_address;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return EOK;
|
---|
175 |
|
---|
176 | err_default_control_ep:
|
---|
177 | bus_endpoint_remove(default_ep);
|
---|
178 | err_address:
|
---|
179 | release_address(bus, address);
|
---|
180 | return err;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Enumerate a USB device. Move it to the addressed state, then explore it
|
---|
185 | * to create a DDF function node with proper characteristics.
|
---|
186 | */
|
---|
187 | static int usb2_bus_device_enumerate(device_t *dev)
|
---|
188 | {
|
---|
189 | int err;
|
---|
190 | usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
|
---|
191 |
|
---|
192 | /* The speed of the new device was reported by the hub when reserving
|
---|
193 | * default address.
|
---|
194 | */
|
---|
195 | dev->speed = bus->base.default_address_speed;
|
---|
196 | usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
|
---|
197 |
|
---|
198 | if (!dev->hub) {
|
---|
199 | /* The device is the roothub */
|
---|
200 | dev->tt = (usb_tt_address_t) {
|
---|
201 | .address = -1,
|
---|
202 | .port = 0,
|
---|
203 | };
|
---|
204 | } else {
|
---|
205 | hcd_setup_device_tt(dev);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* Assign an address to the device */
|
---|
209 | if ((err = address_device(dev))) {
|
---|
210 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
---|
211 | return err;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Read the device descriptor, derive the match ids */
|
---|
215 | if ((err = hcd_device_explore(dev))) {
|
---|
216 | usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
|
---|
217 | release_address(bus, dev->address);
|
---|
218 | return err;
|
---|
219 | }
|
---|
220 |
|
---|
221 | return EOK;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static void usb2_bus_device_gone(device_t *dev)
|
---|
225 | {
|
---|
226 | // TODO: Implement me!
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Register an endpoint to the bus. Reserves bandwidth.
|
---|
231 | */
|
---|
232 | static int usb2_bus_register_ep(endpoint_t *ep)
|
---|
233 | {
|
---|
234 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
235 | assert(fibril_mutex_is_locked(&ep->device->guard));
|
---|
236 | assert(ep);
|
---|
237 |
|
---|
238 | /* Check for available bandwidth */
|
---|
239 | if (ep->bandwidth > bus->free_bw)
|
---|
240 | return ENOSPC;
|
---|
241 |
|
---|
242 | bus->free_bw -= ep->bandwidth;
|
---|
243 |
|
---|
244 | return EOK;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Release bandwidth reserved by the given endpoint.
|
---|
249 | */
|
---|
250 | static void usb2_bus_unregister_ep(endpoint_t *ep)
|
---|
251 | {
|
---|
252 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
253 | assert(ep);
|
---|
254 |
|
---|
255 | bus->free_bw += ep->bandwidth;
|
---|
256 | }
|
---|
257 |
|
---|
258 | const bus_ops_t usb2_bus_ops = {
|
---|
259 | .device_enumerate = usb2_bus_device_enumerate,
|
---|
260 | .device_gone = usb2_bus_device_gone,
|
---|
261 | .endpoint_register = usb2_bus_register_ep,
|
---|
262 | .endpoint_unregister = usb2_bus_unregister_ep,
|
---|
263 | };
|
---|
264 |
|
---|
265 | /** Initialize to default state.
|
---|
266 | *
|
---|
267 | * @param bus usb_bus structure, non-null.
|
---|
268 | * @param available_bandwidth Size of the bandwidth pool.
|
---|
269 | */
|
---|
270 | void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
|
---|
271 | {
|
---|
272 | assert(bus);
|
---|
273 |
|
---|
274 | bus_init(&bus->base, sizeof(device_t));
|
---|
275 | bus->base.ops = &usb2_bus_ops;
|
---|
276 |
|
---|
277 | bus->free_bw = available_bandwidth;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * @}
|
---|
282 | */
|
---|