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

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

usbhost: make bandwidth accounting a usb2_bus-thing

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