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

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

usbhost: documentation & cleanup

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[41924f30]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
[1102eca]33 *
34 * A bus_t implementation for USB 2 and lower. Implements USB 2 enumeration and
35 * configurable bandwidth counting.
[41924f30]36 */
37
38#include <assert.h>
39#include <errno.h>
40#include <macros.h>
41#include <stdbool.h>
[64fea02]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"
[41924f30]53
[1102eca]54/**
55 * Ops receive generic bus_t pointer.
56 */
[41924f30]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
[1102eca]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 *
[10cd715]69 * @param bus usb_device_manager
70 * @param addr Pointer to requested address value, place to store new address
71 */
[5e2b1ae6]72static int request_address(usb2_bus_t *bus, usb_address_t *addr)
[10cd715]73{
[3dc3f99]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;
[10cd715]84
[3dc3f99]85 *addr = new_address;
[56257ba]86 bus->address_occupied[*addr] = true;
[10cd715]87
88 return EOK;
89}
90
[1102eca]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
[56db65d]99static const usb_target_t usb2_default_target = {{
100 .address = USB_ADDRESS_DEFAULT,
101 .endpoint = 0,
102}};
103
[1102eca]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 */
[6832245]110static int address_device(device_t *dev)
[20eaa82]111{
112 int err;
113
[6832245]114 usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
115
[58ac3ec]116 /* The default address is currently reserved for this device */
117 dev->address = USB_ADDRESS_DEFAULT;
118
[20eaa82]119 /** Reserve address early, we want pretty log messages */
[837d53d]120 usb_address_t address = USB_ADDRESS_DEFAULT;
[5e2b1ae6]121 if ((err = request_address(bus, &address))) {
[20eaa82]122 usb_log_error("Failed to reserve new address: %s.",
[8b8c164]123 str_error(err));
124 return err;
[20eaa82]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);
[0206d35]130
[9efad54]131 usb_endpoint_descriptors_t ep0_desc = {
132 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
133 };
[0206d35]134 endpoint_t *default_ep;
[9efad54]135 err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
[20eaa82]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
[9efad54]142 if ((err = hcd_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
[306a36d]143 goto err_address;
[20eaa82]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);
[32fb6bce]149 err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
[20eaa82]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.",
[306a36d]153 address, str_error(err));
[56db65d]154 goto err_default_control_ep;
[20eaa82]155 }
156
[8b8c164]157 /* We need to remove ep before we change the address */
[6832245]158 if ((err = bus_endpoint_remove(default_ep))) {
[8b8c164]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
[20eaa82]164 dev->address = address;
165
166 /* Register EP on the new address */
167 usb_log_debug("Device(%d): Registering control EP.", address);
[9efad54]168 err = bus_endpoint_add(dev, &ep0_desc, NULL);
[20eaa82]169 if (err != EOK) {
170 usb_log_error("Device(%d): Failed to register EP0: %s",
171 address, str_error(err));
[8b8c164]172 goto err_address;
[20eaa82]173 }
174
[deb2e55]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
[20eaa82]180 return EOK;
181
[56db65d]182err_default_control_ep:
[6832245]183 bus_endpoint_remove(default_ep);
[0206d35]184 endpoint_del_ref(default_ep);
[20eaa82]185err_address:
[10cd715]186 release_address(bus, address);
[20eaa82]187 return err;
188}
189
[1102eca]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.
[20eaa82]193 */
[6832245]194static int usb2_bus_device_enumerate(device_t *dev)
[20eaa82]195{
196 int err;
[6832245]197 usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
[20eaa82]198
199 /* The speed of the new device was reported by the hub when reserving
200 * default address.
201 */
[5e2b1ae6]202 dev->speed = bus->base.default_address_speed;
[20eaa82]203 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
204
[ff14aede]205 if (!dev->hub) {
206 /* The device is the roothub */
[8b8c164]207 dev->tt = (usb_tt_address_t) {
208 .address = -1,
209 .port = 0,
210 };
[ff14aede]211 } else {
212 hcd_setup_device_tt(dev);
[20eaa82]213 }
214
215 /* Assign an address to the device */
[6832245]216 if ((err = address_device(dev))) {
[20eaa82]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 */
[32fb6bce]222 if ((err = hcd_device_explore(dev))) {
[20eaa82]223 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
[10cd715]224 release_address(bus, dev->address);
[20eaa82]225 return err;
226 }
227
228 return EOK;
229}
230
[1102eca]231/**
232 * Register an endpoint to the bus. Reserves bandwidth.
[41924f30]233 */
[6832245]234static int usb2_bus_register_ep(endpoint_t *ep)
[41924f30]235{
[6832245]236 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
237 assert(fibril_mutex_is_locked(&bus->base.guard));
[41924f30]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
[1102eca]249/**
250 * Release bandwidth reserved by the given endpoint.
[41924f30]251 */
[6832245]252static int usb2_bus_unregister_ep(endpoint_t *ep)
[41924f30]253{
[6832245]254 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
[41924f30]255 assert(ep);
256
257 bus->free_bw += ep->bandwidth;
258
259 return EOK;
260}
261
[6832245]262const bus_ops_t usb2_bus_ops = {
263 .device_enumerate = usb2_bus_device_enumerate,
[66c16b0]264 .endpoint_register = usb2_bus_register_ep,
265 .endpoint_unregister = usb2_bus_unregister_ep,
[41924f30]266};
267
268/** Initialize to default state.
269 *
270 * @param bus usb_bus structure, non-null.
271 * @param available_bandwidth Size of the bandwidth pool.
272 */
[5e2b1ae6]273void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
[41924f30]274{
275 assert(bus);
276
[32fb6bce]277 bus_init(&bus->base, sizeof(device_t));
[6832245]278 bus->base.ops = &usb2_bus_ops;
[41924f30]279
280 bus->free_bw = available_bandwidth;
281}
[1102eca]282
[41924f30]283/**
284 * @}
285 */
Note: See TracBrowser for help on using the repository browser.