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

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

usbhost: handle superspeed hubs

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2 * Copyright (c) 2013 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
35#include <macros.h>
36#include <str_error.h>
37#include <usb/debug.h>
38#include <usb/descriptor.h>
39#include <usb/request.h>
40
41#include "ddf_helpers.h"
42#include "utility.h"
43
44/**
45 * Get initial max packet size for the control endpoint 0.
46 *
47 * For LS, HS, and SS devices this value is final and fixed.
48 * For FS devices, the default value of 8 is returned. The caller needs
49 * to fetch the first 8B of the device descriptor later in the initialization
50 * process and determine whether it should be increased.
51 *
52 * @return Max packet size for EP 0 (in bytes)
53 */
54uint16_t hc_get_ep0_initial_mps(usb_speed_t speed)
55{
56 static const uint16_t mps_fixed [] = {
57 [USB_SPEED_LOW] = 8,
58 [USB_SPEED_HIGH] = 64,
59 [USB_SPEED_SUPER] = 512,
60 };
61
62 if (speed < ARRAY_SIZE(mps_fixed) && mps_fixed[speed] != 0) {
63 return mps_fixed[speed];
64 }
65 return 8; // USB_SPEED_FULL default
66}
67
68/**
69 * Get max packet size for the control endpoint 0.
70 *
71 * For LS, HS, and SS devices the corresponding fixed value is obtained.
72 * For FS devices the first 8B of the device descriptor are fetched to
73 * determine it.
74 *
75 * @return Max packet size for EP 0 (in bytes)
76 */
77int hc_get_ep0_max_packet_size(uint16_t *mps, bus_t *bus, device_t *dev)
78{
79 assert(mps);
80
81 *mps = hc_get_ep0_initial_mps(dev->speed);
82 if (dev->speed != USB_SPEED_FULL) {
83 return EOK;
84 }
85
86 const usb_target_t control_ep = {{
87 .address = dev->address,
88 .endpoint = 0,
89 }};
90
91 usb_standard_device_descriptor_t desc = { 0 };
92 const usb_device_request_setup_packet_t get_device_desc_8 =
93 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
94
95 usb_log_debug("Requesting first 8B of device descriptor to determine MPS.");
96 ssize_t got = bus_device_send_batch_sync(dev, control_ep, USB_DIRECTION_IN,
97 (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
98 "read first 8 bytes of dev descriptor");
99
100 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
101 const int err = got < 0 ? got : EOVERFLOW;
102 usb_log_error("Failed to get 8B of dev descr: %s.", str_error(err));
103 return err;
104 }
105
106 if (desc.descriptor_type != USB_DESCTYPE_DEVICE) {
107 usb_log_error("The device responded with wrong device descriptor.");
108 return EIO;
109 }
110
111 uint16_t version = uint16_usb2host(desc.usb_spec_version);
112 if (version < 0x0300) {
113 /* USB 2 and below have MPS raw in the field */
114 *mps = desc.max_packet_size;
115 } else {
116 /* USB 3 have MPS as an 2-based exponent */
117 *mps = (1 << desc.max_packet_size);
118 }
119 return EOK;
120}
121
122int hc_get_device_desc(device_t *device, usb_standard_device_descriptor_t *desc)
123{
124 const usb_target_t control_ep = {{
125 .address = device->address,
126 .endpoint = 0,
127 }};
128
129 /* Get std device descriptor */
130 const usb_device_request_setup_packet_t get_device_desc =
131 GET_DEVICE_DESC(sizeof(*desc));
132
133 usb_log_debug("Device(%d): Requesting full device descriptor.",
134 device->address);
135 ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
136 (char *) desc, sizeof(*desc), *(uint64_t *)&get_device_desc,
137 "read device descriptor");
138
139 if (got < 0)
140 return got;
141
142 return got == sizeof(*desc) ? EOK : EOVERFLOW;
143}
144
145int hc_get_hub_desc(device_t *device, usb_hub_descriptor_header_t *desc)
146{
147 const usb_target_t control_ep = {{
148 .address = device->address,
149 .endpoint = 0,
150 }};
151
152 const usb_descriptor_type_t type = device->speed >= USB_SPEED_SUPER
153 ? USB_DESCTYPE_SSPEED_HUB : USB_DESCTYPE_HUB;
154
155 const usb_device_request_setup_packet_t get_hub_desc = {
156 .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST
157 | (USB_REQUEST_TYPE_CLASS << 5)
158 | USB_REQUEST_RECIPIENT_DEVICE,
159 .request = USB_DEVREQ_GET_DESCRIPTOR, \
160 .value = uint16_host2usb(type << 8), \
161 .length = sizeof(*desc),
162 };
163
164 usb_log_debug("Device(%d): Requesting hub descriptor.",
165 device->address);
166 ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
167 (char *) desc, sizeof(*desc), *(uint64_t *)&get_hub_desc,
168 "get hub descriptor");
169
170 if (got < 0)
171 return got;
172
173 return got == sizeof(*desc) ? EOK : EOVERFLOW;
174}
175
176int hc_device_explore(device_t *device)
177{
178 int err;
179 usb_standard_device_descriptor_t desc = { 0 };
180
181 if ((err = hc_get_device_desc(device, &desc))) {
182 usb_log_error("Device(%d): Failed to get dev descriptor: %s",
183 device->address, str_error(err));
184 return err;
185 }
186
187 if ((err = hcd_ddf_setup_match_ids(device, &desc))) {
188 usb_log_error("Device(%d): Failed to setup match ids: %s", device->address, str_error(err));
189 return err;
190 }
191
192 return EOK;
193}
194
195/** Announce root hub to the DDF
196 *
197 * @param[in] device Host controller ddf device
198 * @return Error code
199 */
200int hc_setup_virtual_root_hub(hc_device_t *hcd, usb_speed_t rh_speed)
201{
202 int err;
203
204 assert(hcd);
205
206 device_t *dev = hcd_ddf_fun_create(hcd, rh_speed);
207 if (!dev) {
208 usb_log_error("Failed to create function for the root hub.");
209 return ENOMEM;
210 }
211
212 ddf_fun_set_name(dev->fun, "roothub");
213
214 /* Assign an address to the device */
215 if ((err = bus_device_enumerate(dev))) {
216 usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
217 goto err_usb_dev;
218 }
219
220 if ((err = ddf_fun_bind(dev->fun))) {
221 usb_log_error("Failed to register roothub: %s.", str_error(err));
222 goto err_enumerated;
223 }
224
225 return EOK;
226
227err_enumerated:
228 bus_device_gone(dev);
229err_usb_dev:
230 hcd_ddf_fun_destroy(dev);
231 return err;
232}
233
234/**
235 * Check setup packet data for signs of toggle reset.
236 *
237 * @param[in] batch USB batch
238 * @param[in] reset_cb Callback to reset an endpoint
239 */
240void hc_reset_toggles(const usb_transfer_batch_t *batch, endpoint_reset_toggle_t reset_cb)
241{
242 if (batch->ep->transfer_type != USB_TRANSFER_CONTROL
243 || batch->dir != USB_DIRECTION_OUT)
244 return;
245
246 const usb_device_request_setup_packet_t *request = &batch->setup.packet;
247 device_t * const dev = batch->ep->device;
248
249 switch (request->request)
250 {
251 /* Clear Feature ENPOINT_STALL */
252 case USB_DEVREQ_CLEAR_FEATURE: /*resets only cleared ep */
253 /* 0x2 ( HOST to device | STANDART | TO ENPOINT) */
254 if ((request->request_type == 0x2) &&
255 (request->value == USB_FEATURE_ENDPOINT_HALT)) {
256 const uint16_t index = uint16_usb2host(request->index);
257 const usb_endpoint_t ep_num = index & 0xf;
258 const usb_direction_t dir = (index >> 7) ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
259
260 endpoint_t *ep = bus_find_endpoint(dev, ep_num, dir);
261 if (ep) {
262 reset_cb(ep);
263 endpoint_del_ref(ep);
264 } else {
265 usb_log_warning("Device(%u): Resetting unregistered endpoint %u %s.", dev->address, ep_num, usb_str_direction(dir));
266 }
267 }
268 break;
269 case USB_DEVREQ_SET_CONFIGURATION:
270 case USB_DEVREQ_SET_INTERFACE:
271 /* Recipient must be device, this resets all endpoints,
272 * In fact there should be no endpoints but EP 0 registered
273 * as different interfaces use different endpoints,
274 * unless you're changing configuration or alternative
275 * interface of an already setup device. */
276 if (!(request->request_type & SETUP_REQUEST_TYPE_DEVICE_TO_HOST))
277 for (usb_endpoint_t i = 0; i < 2 * USB_ENDPOINT_MAX; ++i)
278 if (dev->endpoints[i])
279 reset_cb(dev->endpoints[i]);
280 break;
281 default:
282 break;
283 }
284}
285
286/**
287 * @}
288 */
Note: See TracBrowser for help on using the repository browser.