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

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

usbhost: prepare bandwidth accounting privatization to usb2_bus

The endpoint does not account the bandwidth by default. The next step is
moving the count_bw callback to the usb2 bus.

  • Property mode set to 100644
File size: 7.9 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>
[944f8fdd]47#include <usb/host/utility.h>
[64fea02]48#include <usb/usb.h>
49
50#include "endpoint.h"
51#include "ddf_helpers.h"
52
53#include "usb2_bus.h"
[41924f30]54
[1102eca]55/**
56 * Ops receive generic bus_t pointer.
57 */
[41924f30]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
[1102eca]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 *
[10cd715]70 * @param bus usb_device_manager
71 * @param addr Pointer to requested address value, place to store new address
72 */
[5e2b1ae6]73static int request_address(usb2_bus_t *bus, usb_address_t *addr)
[10cd715]74{
[3dc3f99]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;
[10cd715]85
[3dc3f99]86 *addr = new_address;
[56257ba]87 bus->address_occupied[*addr] = true;
[10cd715]88
89 return EOK;
90}
91
[1102eca]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
[56db65d]100static const usb_target_t usb2_default_target = {{
101 .address = USB_ADDRESS_DEFAULT,
102 .endpoint = 0,
103}};
104
[1102eca]105/**
106 * Transition the device to the addressed state.
107 *
108 * Reserve address, configure the control EP, issue a SET_ADDRESS command.
[53a9d02]109 * Configure the device with the new address,
[1102eca]110 */
[6832245]111static int address_device(device_t *dev)
[20eaa82]112{
113 int err;
114
[6832245]115 usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
116
[58ac3ec]117 /* The default address is currently reserved for this device */
118 dev->address = USB_ADDRESS_DEFAULT;
119
[20eaa82]120 /** Reserve address early, we want pretty log messages */
[837d53d]121 usb_address_t address = USB_ADDRESS_DEFAULT;
[5e2b1ae6]122 if ((err = request_address(bus, &address))) {
[20eaa82]123 usb_log_error("Failed to reserve new address: %s.",
[8b8c164]124 str_error(err));
125 return err;
[20eaa82]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);
[0206d35]131
[9efad54]132 usb_endpoint_descriptors_t ep0_desc = {
133 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
134 };
[0206d35]135 endpoint_t *default_ep;
[9efad54]136 err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
[20eaa82]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
[944f8fdd]143 if ((err = hc_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
[306a36d]144 goto err_address;
[20eaa82]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);
[32fb6bce]150 err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
[20eaa82]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.",
[306a36d]154 address, str_error(err));
[56db65d]155 goto err_default_control_ep;
[20eaa82]156 }
157
[8b8c164]158 /* We need to remove ep before we change the address */
[6832245]159 if ((err = bus_endpoint_remove(default_ep))) {
[8b8c164]160 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
161 goto err_address;
162 }
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
175 return EOK;
176
[56db65d]177err_default_control_ep:
[6832245]178 bus_endpoint_remove(default_ep);
[20eaa82]179err_address:
[10cd715]180 release_address(bus, address);
[20eaa82]181 return err;
182}
183
[1102eca]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.
[20eaa82]187 */
[6832245]188static int usb2_bus_device_enumerate(device_t *dev)
[20eaa82]189{
190 int err;
[6832245]191 usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
[20eaa82]192
193 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
194
195 /* Assign an address to the device */
[6832245]196 if ((err = address_device(dev))) {
[20eaa82]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 */
[944f8fdd]202 if ((err = hc_device_explore(dev))) {
[20eaa82]203 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
[10cd715]204 release_address(bus, dev->address);
[20eaa82]205 return err;
206 }
207
208 return EOK;
209}
210
[5f0b366]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 bus_t *bus = ep->device->bus;
222 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_count_bw);
223 if (!ops)
224 return 0;
225
226 return ops->endpoint_count_bw(ep, ep->max_transfer_size);
227}
228
[1102eca]229/**
230 * Register an endpoint to the bus. Reserves bandwidth.
[41924f30]231 */
[6832245]232static int usb2_bus_register_ep(endpoint_t *ep)
[41924f30]233{
[6832245]234 usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
[53a9d02]235 assert(fibril_mutex_is_locked(&ep->device->guard));
[41924f30]236 assert(ep);
237
[5f0b366]238 size_t bw = endpoint_count_bw(ep);
239
[41924f30]240 /* Check for available bandwidth */
[5f0b366]241 if (bw > bus->free_bw)
[41924f30]242 return ENOSPC;
243
[5f0b366]244 bus->free_bw -= bw;
[41924f30]245
246 return EOK;
247}
248
[1102eca]249/**
250 * Release bandwidth reserved by the given endpoint.
[41924f30]251 */
[bad4a05]252static void 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
[5f0b366]257 bus->free_bw += endpoint_count_bw(ep);
[41924f30]258}
259
[6832245]260const bus_ops_t usb2_bus_ops = {
261 .device_enumerate = usb2_bus_device_enumerate,
[66c16b0]262 .endpoint_register = usb2_bus_register_ep,
263 .endpoint_unregister = usb2_bus_unregister_ep,
[41924f30]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 */
[5e2b1ae6]271void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
[41924f30]272{
273 assert(bus);
274
[32fb6bce]275 bus_init(&bus->base, sizeof(device_t));
[6832245]276 bus->base.ops = &usb2_bus_ops;
[41924f30]277
278 bus->free_bw = available_bandwidth;
[c1a966e]279
280 /*
281 * The first address allocated is for the roothub. This way, its
282 * address will be 127, and the first connected USB device will have
283 * address 1.
284 */
285 bus->last_address = USB_ADDRESS_COUNT - 2;
[41924f30]286}
[1102eca]287
[41924f30]288/**
289 * @}
290 */
Note: See TracBrowser for help on using the repository browser.