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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e2d387 was 904b1bc, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix remaining ccheck issues.

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek
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 * Request a new address. A free address is found and marked as occupied.
57 *
58 * There's no need to synchronize this method, because it is called only with
59 * default address reserved.
60 *
61 * @param bus usb_device_manager
62 * @param addr Pointer to requested address value, place to store new address
63 */
64static int request_address(usb2_bus_helper_t *helper, usb_address_t *addr)
65{
66 // Find a free address
67 usb_address_t new_address = helper->last_address;
68 do {
69 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
70 if (new_address == USB_ADDRESS_DEFAULT)
71 new_address = 1;
72 if (new_address == helper->last_address)
73 return ENOSPC;
74 } while (helper->address_occupied[new_address]);
75 helper->last_address = new_address;
76
77 *addr = new_address;
78 helper->address_occupied[*addr] = true;
79
80 return EOK;
81}
82
83/**
84 * Mark address as free.
85 */
86static void release_address(usb2_bus_helper_t *helper, usb_address_t address)
87{
88 helper->address_occupied[address] = false;
89}
90
91static const usb_target_t usb2_default_target = {
92 {
93 .address = USB_ADDRESS_DEFAULT,
94 .endpoint = 0,
95 }
96};
97
98/**
99 * Transition the device to the addressed state.
100 *
101 * Reserve address, configure the control EP, issue a SET_ADDRESS command.
102 * Configure the device with the new address,
103 */
104static int address_device(usb2_bus_helper_t *helper, device_t *dev)
105{
106 int err;
107
108 /* The default address is currently reserved for this device */
109 dev->address = USB_ADDRESS_DEFAULT;
110
111 /** Reserve address early, we want pretty log messages */
112 usb_address_t address = USB_ADDRESS_DEFAULT;
113 if ((err = request_address(helper, &address))) {
114 usb_log_error("Failed to reserve new address: %s.",
115 str_error(err));
116 return err;
117 }
118 usb_log_debug("Device(%d): Reserved new address.", address);
119
120 /* Add default pipe on default address */
121 usb_log_debug("Device(%d): Adding default target (0:0)", address);
122
123 usb_endpoint_descriptors_t ep0_desc = {
124 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
125 };
126
127 /* Temporary reference */
128 endpoint_t *default_ep;
129 err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
130 if (err != EOK) {
131 usb_log_error("Device(%d): Failed to add default target: %s.",
132 address, str_error(err));
133 goto err_address;
134 }
135
136 if ((err = hc_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, dev)))
137 goto err_address;
138
139 /* Set new address */
140 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
141
142 usb_log_debug("Device(%d): Setting USB address.", address);
143 err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
144 NULL, 0, *(uint64_t *)&set_address, "set address", NULL);
145 if (err) {
146 usb_log_error("Device(%d): Failed to set new address: %s.",
147 address, str_error(err));
148 goto err_default_control_ep;
149 }
150
151 /* We need to remove ep before we change the address */
152 if ((err = bus_endpoint_remove(default_ep))) {
153 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
154 goto err_address;
155 }
156
157 /* Temporary reference */
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 return EOK;
172
173err_default_control_ep:
174 bus_endpoint_remove(default_ep);
175 /* Temporary reference */
176 endpoint_del_ref(default_ep);
177err_address:
178 release_address(helper, address);
179 return err;
180}
181
182/**
183 * Enumerate a USB device. Move it to the addressed state, then explore it
184 * to create a DDF function node with proper characteristics.
185 */
186int usb2_bus_device_enumerate(usb2_bus_helper_t *helper, device_t *dev)
187{
188 int err;
189 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
190
191 /* Assign an address to the device */
192 if ((err = address_device(helper, dev))) {
193 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
194 return err;
195 }
196
197 /* Read the device descriptor, derive the match ids */
198 if ((err = hc_device_explore(dev))) {
199 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
200 release_address(helper, dev->address);
201 return err;
202 }
203
204 return EOK;
205}
206
207void usb2_bus_device_gone(usb2_bus_helper_t *helper, device_t *dev)
208{
209 release_address(helper, dev->address);
210}
211
212/**
213 * Register an endpoint to the bus. Reserves bandwidth.
214 */
215int usb2_bus_endpoint_register(usb2_bus_helper_t *helper, endpoint_t *ep)
216{
217 assert(ep);
218 assert(fibril_mutex_is_locked(&ep->device->guard));
219
220 size_t bw = helper->bw_accounting->count_bw(ep);
221
222 /* Check for available bandwidth */
223 if (bw > helper->free_bw)
224 return ENOSPC;
225
226 helper->free_bw -= bw;
227
228 return EOK;
229}
230
231/**
232 * Release bandwidth reserved by the given endpoint.
233 */
234void usb2_bus_endpoint_unregister(usb2_bus_helper_t *helper, endpoint_t *ep)
235{
236 assert(helper);
237 assert(ep);
238
239 helper->free_bw += helper->bw_accounting->count_bw(ep);
240}
241
242/**
243 * Initialize to default state.
244 *
245 * @param helper usb_bus_helper structure, non-null.
246 * @param bw_accounting a structure defining bandwidth accounting
247 */
248void usb2_bus_helper_init(usb2_bus_helper_t *helper, const bandwidth_accounting_t *bw_accounting)
249{
250 assert(helper);
251 assert(bw_accounting);
252
253 helper->bw_accounting = bw_accounting;
254 helper->free_bw = bw_accounting->available_bandwidth;
255
256 /*
257 * The first address allocated is for the roothub. This way, its
258 * address will be 127, and the first connected USB device will have
259 * address 1.
260 */
261 helper->last_address = USB_ADDRESS_COUNT - 2;
262}
263
264/**
265 * @}
266 */
Note: See TracBrowser for help on using the repository browser.