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

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

usbhost: inline get_free_address

As it's now used only once, it is not necessary to split this much.

Also, removed some afterchecks. There's no reason to assert results of
simple code.

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