Deleted Added
full compact
usb_request.c (184610) usb_request.c (184824)
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_request.c 184610 2008-11-04 02:31:03Z alfred $ */
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_request.c 184824 2008-11-10 20:54:31Z thompsa $ */
2/*-
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <dev/usb2/include/usb2_defs.h>
30#include <dev/usb2/include/usb2_mfunc.h>
31#include <dev/usb2/include/usb2_error.h>
32#include <dev/usb2/include/usb2_standard.h>
33#include <dev/usb2/include/usb2_ioctl.h>
34#include <dev/usb2/include/usb2_hid.h>
35
36#define USB_DEBUG_VAR usb2_debug
37
38#include <dev/usb2/core/usb2_core.h>
39#include <dev/usb2/core/usb2_busdma.h>
40#include <dev/usb2/core/usb2_request.h>
41#include <dev/usb2/core/usb2_process.h>
42#include <dev/usb2/core/usb2_transfer.h>
43#include <dev/usb2/core/usb2_debug.h>
44#include <dev/usb2/core/usb2_device.h>
45#include <dev/usb2/core/usb2_util.h>
46#include <dev/usb2/core/usb2_dynamic.h>
47
2/*-
3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <dev/usb2/include/usb2_defs.h>
30#include <dev/usb2/include/usb2_mfunc.h>
31#include <dev/usb2/include/usb2_error.h>
32#include <dev/usb2/include/usb2_standard.h>
33#include <dev/usb2/include/usb2_ioctl.h>
34#include <dev/usb2/include/usb2_hid.h>
35
36#define USB_DEBUG_VAR usb2_debug
37
38#include <dev/usb2/core/usb2_core.h>
39#include <dev/usb2/core/usb2_busdma.h>
40#include <dev/usb2/core/usb2_request.h>
41#include <dev/usb2/core/usb2_process.h>
42#include <dev/usb2/core/usb2_transfer.h>
43#include <dev/usb2/core/usb2_debug.h>
44#include <dev/usb2/core/usb2_device.h>
45#include <dev/usb2/core/usb2_util.h>
46#include <dev/usb2/core/usb2_dynamic.h>
47
48#include <dev/usb2/controller/usb2_controller.h>
49#include <dev/usb2/controller/usb2_bus.h>
48#include <sys/ctype.h>
49
50#if USB_DEBUG
51static int usb2_pr_poll_delay = USB_PORT_RESET_DELAY;
52static int usb2_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
53static int usb2_ss_delay = 0;
54
55SYSCTL_INT(_hw_usb2, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
56 &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms");
57SYSCTL_INT(_hw_usb2, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
58 &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
59SYSCTL_INT(_hw_usb2, OID_AUTO, ss_delay, CTLFLAG_RW,
60 &usb2_ss_delay, 0, "USB status stage delay in ms");
61#endif
62
63/*------------------------------------------------------------------------*
64 * usb2_do_request_callback
65 *
66 * This function is the USB callback for generic USB Host control
67 * transfers.
68 *------------------------------------------------------------------------*/
69void
70usb2_do_request_callback(struct usb2_xfer *xfer)
71{
72 ; /* workaround for a bug in "indent" */
73
74 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
75
76 switch (USB_GET_STATE(xfer)) {
77 case USB_ST_SETUP:
78 usb2_start_hardware(xfer);
79 break;
80 default:
81 usb2_cv_signal(xfer->udev->default_cv);
82 break;
83 }
84 return;
85}
86
87/*------------------------------------------------------------------------*
88 * usb2_do_clear_stall_callback
89 *
90 * This function is the USB callback for generic clear stall requests.
91 *------------------------------------------------------------------------*/
92void
93usb2_do_clear_stall_callback(struct usb2_xfer *xfer)
94{
95 struct usb2_device_request req;
96 struct usb2_pipe *pipe;
97 struct usb2_pipe *pipe_end;
98 struct usb2_pipe *pipe_first;
99 uint8_t to = USB_EP_MAX;
100
50#include <sys/ctype.h>
51
52#if USB_DEBUG
53static int usb2_pr_poll_delay = USB_PORT_RESET_DELAY;
54static int usb2_pr_recovery_delay = USB_PORT_RESET_RECOVERY;
55static int usb2_ss_delay = 0;
56
57SYSCTL_INT(_hw_usb2, OID_AUTO, pr_poll_delay, CTLFLAG_RW,
58 &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms");
59SYSCTL_INT(_hw_usb2, OID_AUTO, pr_recovery_delay, CTLFLAG_RW,
60 &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms");
61SYSCTL_INT(_hw_usb2, OID_AUTO, ss_delay, CTLFLAG_RW,
62 &usb2_ss_delay, 0, "USB status stage delay in ms");
63#endif
64
65/*------------------------------------------------------------------------*
66 * usb2_do_request_callback
67 *
68 * This function is the USB callback for generic USB Host control
69 * transfers.
70 *------------------------------------------------------------------------*/
71void
72usb2_do_request_callback(struct usb2_xfer *xfer)
73{
74 ; /* workaround for a bug in "indent" */
75
76 DPRINTF("st=%u\n", USB_GET_STATE(xfer));
77
78 switch (USB_GET_STATE(xfer)) {
79 case USB_ST_SETUP:
80 usb2_start_hardware(xfer);
81 break;
82 default:
83 usb2_cv_signal(xfer->udev->default_cv);
84 break;
85 }
86 return;
87}
88
89/*------------------------------------------------------------------------*
90 * usb2_do_clear_stall_callback
91 *
92 * This function is the USB callback for generic clear stall requests.
93 *------------------------------------------------------------------------*/
94void
95usb2_do_clear_stall_callback(struct usb2_xfer *xfer)
96{
97 struct usb2_device_request req;
98 struct usb2_pipe *pipe;
99 struct usb2_pipe *pipe_end;
100 struct usb2_pipe *pipe_first;
101 uint8_t to = USB_EP_MAX;
102
101 mtx_lock(xfer->usb2_mtx);
103 USB_BUS_LOCK(xfer->udev->bus);
102
103 /* round robin pipe clear stall */
104
105 pipe = xfer->udev->pipe_curr;
106 pipe_end = xfer->udev->pipes + USB_EP_MAX;
107 pipe_first = xfer->udev->pipes;
108 if (pipe == NULL) {
109 pipe = pipe_first;
110 }
111 switch (USB_GET_STATE(xfer)) {
112 case USB_ST_TRANSFERRED:
113 if (pipe->edesc &&
114 pipe->is_stalled) {
115 pipe->toggle_next = 0;
116 pipe->is_stalled = 0;
117 /* start up the current or next transfer, if any */
118 usb2_command_wrapper(&pipe->pipe_q,
119 pipe->pipe_q.curr);
120 }
121 pipe++;
122
123 case USB_ST_SETUP:
124tr_setup:
125 if (pipe == pipe_end) {
126 pipe = pipe_first;
127 }
128 if (pipe->edesc &&
129 pipe->is_stalled) {
130
131 /* setup a clear-stall packet */
132
133 req.bmRequestType = UT_WRITE_ENDPOINT;
134 req.bRequest = UR_CLEAR_FEATURE;
135 USETW(req.wValue, UF_ENDPOINT_HALT);
136 req.wIndex[0] = pipe->edesc->bEndpointAddress;
137 req.wIndex[1] = 0;
138 USETW(req.wLength, 0);
139
140 /* copy in the transfer */
141
142 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
143
144 /* set length */
145 xfer->frlengths[0] = sizeof(req);
146 xfer->nframes = 1;
104
105 /* round robin pipe clear stall */
106
107 pipe = xfer->udev->pipe_curr;
108 pipe_end = xfer->udev->pipes + USB_EP_MAX;
109 pipe_first = xfer->udev->pipes;
110 if (pipe == NULL) {
111 pipe = pipe_first;
112 }
113 switch (USB_GET_STATE(xfer)) {
114 case USB_ST_TRANSFERRED:
115 if (pipe->edesc &&
116 pipe->is_stalled) {
117 pipe->toggle_next = 0;
118 pipe->is_stalled = 0;
119 /* start up the current or next transfer, if any */
120 usb2_command_wrapper(&pipe->pipe_q,
121 pipe->pipe_q.curr);
122 }
123 pipe++;
124
125 case USB_ST_SETUP:
126tr_setup:
127 if (pipe == pipe_end) {
128 pipe = pipe_first;
129 }
130 if (pipe->edesc &&
131 pipe->is_stalled) {
132
133 /* setup a clear-stall packet */
134
135 req.bmRequestType = UT_WRITE_ENDPOINT;
136 req.bRequest = UR_CLEAR_FEATURE;
137 USETW(req.wValue, UF_ENDPOINT_HALT);
138 req.wIndex[0] = pipe->edesc->bEndpointAddress;
139 req.wIndex[1] = 0;
140 USETW(req.wLength, 0);
141
142 /* copy in the transfer */
143
144 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
145
146 /* set length */
147 xfer->frlengths[0] = sizeof(req);
148 xfer->nframes = 1;
147 mtx_unlock(xfer->usb2_mtx);
149 USB_BUS_UNLOCK(xfer->udev->bus);
148
149 usb2_start_hardware(xfer);
150
150
151 usb2_start_hardware(xfer);
152
151 mtx_lock(xfer->usb2_mtx);
153 USB_BUS_LOCK(xfer->udev->bus);
152 break;
153 }
154 pipe++;
155 if (--to)
156 goto tr_setup;
157 break;
158
159 default:
160 if (xfer->error == USB_ERR_CANCELLED) {
161 break;
162 }
163 goto tr_setup;
164 }
165
166 /* store current pipe */
167 xfer->udev->pipe_curr = pipe;
154 break;
155 }
156 pipe++;
157 if (--to)
158 goto tr_setup;
159 break;
160
161 default:
162 if (xfer->error == USB_ERR_CANCELLED) {
163 break;
164 }
165 goto tr_setup;
166 }
167
168 /* store current pipe */
169 xfer->udev->pipe_curr = pipe;
168 mtx_unlock(xfer->usb2_mtx);
170 USB_BUS_UNLOCK(xfer->udev->bus);
169 return;
170}
171
172/*------------------------------------------------------------------------*
173 * usb2_do_request_flags and usb2_do_request
174 *
175 * Description of arguments passed to these functions:
176 *
177 * "udev" - this is the "usb2_device" structure pointer on which the
178 * request should be performed. It is possible to call this function
179 * in both Host Side mode and Device Side mode.
180 *
181 * "mtx" - if this argument is non-NULL the mutex pointed to by it
182 * will get dropped and picked up during the execution of this
183 * function, hence this function sometimes needs to sleep. If this
184 * argument is NULL it has no effect.
185 *
186 * "req" - this argument must always be non-NULL and points to an
187 * 8-byte structure holding the USB request to be done. The USB
188 * request structure has a bit telling the direction of the USB
189 * request, if it is a read or a write.
190 *
191 * "data" - if the "wLength" part of the structure pointed to by "req"
192 * is non-zero this argument must point to a valid kernel buffer which
193 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
194 * be NULL.
195 *
196 * "flags" - here is a list of valid flags:
197 *
198 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
199 * specified
200 *
201 * o USB_USE_POLLING: forces the transfer to complete from the
202 * current context by polling the interrupt handler. This flag can be
203 * used to perform USB transfers after that the kernel has crashed.
204 *
205 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
206 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
207 * sysctl. This flag is mostly useful for debugging.
208 *
209 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
210 * pointer.
211 *
212 * "actlen" - if non-NULL the actual transfer length will be stored in
213 * the 16-bit unsigned integer pointed to by "actlen". This
214 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
215 * used.
216 *
217 * "timeout" - gives the timeout for the control transfer in
218 * milliseconds. A "timeout" value less than 50 milliseconds is
219 * treated like a 50 millisecond timeout. A "timeout" value greater
220 * than 30 seconds is treated like a 30 second timeout. This USB stack
221 * does not allow control requests without a timeout.
222 *
223 * NOTE: This function is thread safe. All calls to
224 * "usb2_do_request_flags" will be serialised by the use of an
225 * internal "sx_lock".
226 *
227 * Returns:
228 * 0: Success
229 * Else: Failure
230 *------------------------------------------------------------------------*/
231usb2_error_t
232usb2_do_request_flags(struct usb2_device *udev, struct mtx *mtx,
233 struct usb2_device_request *req, void *data, uint32_t flags,
234 uint16_t *actlen, uint32_t timeout)
235{
236 struct usb2_xfer *xfer;
237 const void *desc;
238 int err = 0;
239 uint32_t start_ticks;
240 uint32_t delta_ticks;
241 uint32_t max_ticks;
242 uint16_t length;
243 uint16_t temp;
244
245 if (timeout < 50) {
246 /* timeout is too small */
247 timeout = 50;
248 }
249 if (timeout > 30000) {
250 /* timeout is too big */
251 timeout = 30000;
252 }
253 length = UGETW(req->wLength);
254
255 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
256 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
257 udev, req->bmRequestType, req->bRequest,
258 req->wValue[1], req->wValue[0],
259 req->wIndex[1], req->wIndex[0],
260 req->wLength[1], req->wLength[0]);
261
262 /*
263 * Set "actlen" to a known value in case the caller does not
264 * check the return value:
265 */
266 if (actlen) {
267 *actlen = 0;
268 }
269 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
270 DPRINTF("USB device mode\n");
271 (usb2_temp_get_desc_p) (udev, req, &desc, &temp);
272 if (length > temp) {
273 if (!(flags & USB_SHORT_XFER_OK)) {
274 return (USB_ERR_SHORT_XFER);
275 }
276 length = temp;
277 }
278 if (actlen) {
279 *actlen = length;
280 }
281 if (length > 0) {
282 if (flags & USB_USER_DATA_PTR) {
283 if (copyout(desc, data, length)) {
284 return (USB_ERR_INVAL);
285 }
286 } else {
287 bcopy(desc, data, length);
288 }
289 }
290 return (0); /* success */
291 }
292 if (mtx) {
293 mtx_unlock(mtx);
294 if (mtx != &Giant) {
295 mtx_assert(mtx, MA_NOTOWNED);
296 }
297 }
298 /*
299 * Grab the default sx-lock so that serialisation
300 * is achieved when multiple threads are involved:
301 */
302
303 sx_xlock(udev->default_sx);
304
305 /*
306 * Setup a new USB transfer or use the existing one, if any:
307 */
308 usb2_default_transfer_setup(udev);
309
310 xfer = udev->default_xfer[0];
311 if (xfer == NULL) {
312 /* most likely out of memory */
313 err = USB_ERR_NOMEM;
314 goto done;
315 }
171 return;
172}
173
174/*------------------------------------------------------------------------*
175 * usb2_do_request_flags and usb2_do_request
176 *
177 * Description of arguments passed to these functions:
178 *
179 * "udev" - this is the "usb2_device" structure pointer on which the
180 * request should be performed. It is possible to call this function
181 * in both Host Side mode and Device Side mode.
182 *
183 * "mtx" - if this argument is non-NULL the mutex pointed to by it
184 * will get dropped and picked up during the execution of this
185 * function, hence this function sometimes needs to sleep. If this
186 * argument is NULL it has no effect.
187 *
188 * "req" - this argument must always be non-NULL and points to an
189 * 8-byte structure holding the USB request to be done. The USB
190 * request structure has a bit telling the direction of the USB
191 * request, if it is a read or a write.
192 *
193 * "data" - if the "wLength" part of the structure pointed to by "req"
194 * is non-zero this argument must point to a valid kernel buffer which
195 * can hold at least "wLength" bytes. If "wLength" is zero "data" can
196 * be NULL.
197 *
198 * "flags" - here is a list of valid flags:
199 *
200 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than
201 * specified
202 *
203 * o USB_USE_POLLING: forces the transfer to complete from the
204 * current context by polling the interrupt handler. This flag can be
205 * used to perform USB transfers after that the kernel has crashed.
206 *
207 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed
208 * at a later point in time. This is tunable by the "hw.usb.ss_delay"
209 * sysctl. This flag is mostly useful for debugging.
210 *
211 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland
212 * pointer.
213 *
214 * "actlen" - if non-NULL the actual transfer length will be stored in
215 * the 16-bit unsigned integer pointed to by "actlen". This
216 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is
217 * used.
218 *
219 * "timeout" - gives the timeout for the control transfer in
220 * milliseconds. A "timeout" value less than 50 milliseconds is
221 * treated like a 50 millisecond timeout. A "timeout" value greater
222 * than 30 seconds is treated like a 30 second timeout. This USB stack
223 * does not allow control requests without a timeout.
224 *
225 * NOTE: This function is thread safe. All calls to
226 * "usb2_do_request_flags" will be serialised by the use of an
227 * internal "sx_lock".
228 *
229 * Returns:
230 * 0: Success
231 * Else: Failure
232 *------------------------------------------------------------------------*/
233usb2_error_t
234usb2_do_request_flags(struct usb2_device *udev, struct mtx *mtx,
235 struct usb2_device_request *req, void *data, uint32_t flags,
236 uint16_t *actlen, uint32_t timeout)
237{
238 struct usb2_xfer *xfer;
239 const void *desc;
240 int err = 0;
241 uint32_t start_ticks;
242 uint32_t delta_ticks;
243 uint32_t max_ticks;
244 uint16_t length;
245 uint16_t temp;
246
247 if (timeout < 50) {
248 /* timeout is too small */
249 timeout = 50;
250 }
251 if (timeout > 30000) {
252 /* timeout is too big */
253 timeout = 30000;
254 }
255 length = UGETW(req->wLength);
256
257 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x "
258 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n",
259 udev, req->bmRequestType, req->bRequest,
260 req->wValue[1], req->wValue[0],
261 req->wIndex[1], req->wIndex[0],
262 req->wLength[1], req->wLength[0]);
263
264 /*
265 * Set "actlen" to a known value in case the caller does not
266 * check the return value:
267 */
268 if (actlen) {
269 *actlen = 0;
270 }
271 if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
272 DPRINTF("USB device mode\n");
273 (usb2_temp_get_desc_p) (udev, req, &desc, &temp);
274 if (length > temp) {
275 if (!(flags & USB_SHORT_XFER_OK)) {
276 return (USB_ERR_SHORT_XFER);
277 }
278 length = temp;
279 }
280 if (actlen) {
281 *actlen = length;
282 }
283 if (length > 0) {
284 if (flags & USB_USER_DATA_PTR) {
285 if (copyout(desc, data, length)) {
286 return (USB_ERR_INVAL);
287 }
288 } else {
289 bcopy(desc, data, length);
290 }
291 }
292 return (0); /* success */
293 }
294 if (mtx) {
295 mtx_unlock(mtx);
296 if (mtx != &Giant) {
297 mtx_assert(mtx, MA_NOTOWNED);
298 }
299 }
300 /*
301 * Grab the default sx-lock so that serialisation
302 * is achieved when multiple threads are involved:
303 */
304
305 sx_xlock(udev->default_sx);
306
307 /*
308 * Setup a new USB transfer or use the existing one, if any:
309 */
310 usb2_default_transfer_setup(udev);
311
312 xfer = udev->default_xfer[0];
313 if (xfer == NULL) {
314 /* most likely out of memory */
315 err = USB_ERR_NOMEM;
316 goto done;
317 }
316 mtx_lock(xfer->priv_mtx);
318 USB_XFER_LOCK(xfer);
317
318 if (flags & USB_DELAY_STATUS_STAGE) {
319 xfer->flags.manual_status = 1;
320 } else {
321 xfer->flags.manual_status = 0;
322 }
323
324 xfer->timeout = timeout;
325
326 start_ticks = ticks;
327
328 max_ticks = USB_MS_TO_TICKS(timeout);
329
330 usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
331
332 xfer->frlengths[0] = sizeof(*req);
333 xfer->nframes = 2;
334
335 while (1) {
336 temp = length;
337 if (temp > xfer->max_data_length) {
338 temp = xfer->max_data_length;
339 }
340 xfer->frlengths[1] = temp;
341
342 if (temp > 0) {
343 if (!(req->bmRequestType & UT_READ)) {
344 if (flags & USB_USER_DATA_PTR) {
319
320 if (flags & USB_DELAY_STATUS_STAGE) {
321 xfer->flags.manual_status = 1;
322 } else {
323 xfer->flags.manual_status = 0;
324 }
325
326 xfer->timeout = timeout;
327
328 start_ticks = ticks;
329
330 max_ticks = USB_MS_TO_TICKS(timeout);
331
332 usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req));
333
334 xfer->frlengths[0] = sizeof(*req);
335 xfer->nframes = 2;
336
337 while (1) {
338 temp = length;
339 if (temp > xfer->max_data_length) {
340 temp = xfer->max_data_length;
341 }
342 xfer->frlengths[1] = temp;
343
344 if (temp > 0) {
345 if (!(req->bmRequestType & UT_READ)) {
346 if (flags & USB_USER_DATA_PTR) {
345 mtx_unlock(xfer->priv_mtx);
347 USB_XFER_UNLOCK(xfer);
346 err = usb2_copy_in_user(xfer->frbuffers + 1,
347 0, data, temp);
348 err = usb2_copy_in_user(xfer->frbuffers + 1,
349 0, data, temp);
348 mtx_lock(xfer->priv_mtx);
350 USB_XFER_LOCK(xfer);
349 if (err) {
350 err = USB_ERR_INVAL;
351 break;
352 }
353 } else {
354 usb2_copy_in(xfer->frbuffers + 1, 0, data, temp);
355 }
356 }
357 xfer->nframes = 2;
358 } else {
359 if (xfer->frlengths[0] == 0) {
360 if (xfer->flags.manual_status) {
361#if USB_DEBUG
362 int temp;
363
364 temp = usb2_ss_delay;
365 if (temp > 5000) {
366 temp = 5000;
367 }
368 if (temp > 0) {
369 usb2_pause_mtx(
351 if (err) {
352 err = USB_ERR_INVAL;
353 break;
354 }
355 } else {
356 usb2_copy_in(xfer->frbuffers + 1, 0, data, temp);
357 }
358 }
359 xfer->nframes = 2;
360 } else {
361 if (xfer->frlengths[0] == 0) {
362 if (xfer->flags.manual_status) {
363#if USB_DEBUG
364 int temp;
365
366 temp = usb2_ss_delay;
367 if (temp > 5000) {
368 temp = 5000;
369 }
370 if (temp > 0) {
371 usb2_pause_mtx(
370 xfer->priv_mtx, temp);
372 xfer->xfer_mtx, temp);
371 }
372#endif
373 xfer->flags.manual_status = 0;
374 } else {
375 break;
376 }
377 }
378 xfer->nframes = 1;
379 }
380
381 usb2_transfer_start(xfer);
382
383 while (usb2_transfer_pending(xfer)) {
384 if ((flags & USB_USE_POLLING) || cold) {
385 usb2_do_poll(udev->default_xfer, USB_DEFAULT_XFER_MAX);
386 } else {
373 }
374#endif
375 xfer->flags.manual_status = 0;
376 } else {
377 break;
378 }
379 }
380 xfer->nframes = 1;
381 }
382
383 usb2_transfer_start(xfer);
384
385 while (usb2_transfer_pending(xfer)) {
386 if ((flags & USB_USE_POLLING) || cold) {
387 usb2_do_poll(udev->default_xfer, USB_DEFAULT_XFER_MAX);
388 } else {
387 usb2_cv_wait(xfer->udev->default_cv, xfer->priv_mtx);
389 usb2_cv_wait(xfer->udev->default_cv,
390 xfer->xfer_mtx);
388 }
389 }
390
391 err = xfer->error;
392
393 if (err) {
394 break;
395 }
396 /* subtract length of SETUP packet, if any */
397
398 if (xfer->aframes > 0) {
399 xfer->actlen -= xfer->frlengths[0];
400 } else {
401 xfer->actlen = 0;
402 }
403
404 /* check for short packet */
405
406 if (temp > xfer->actlen) {
407 temp = xfer->actlen;
408 if (!(flags & USB_SHORT_XFER_OK)) {
409 err = USB_ERR_SHORT_XFER;
410 }
411 length = temp;
412 }
413 if (temp > 0) {
414 if (req->bmRequestType & UT_READ) {
415 if (flags & USB_USER_DATA_PTR) {
391 }
392 }
393
394 err = xfer->error;
395
396 if (err) {
397 break;
398 }
399 /* subtract length of SETUP packet, if any */
400
401 if (xfer->aframes > 0) {
402 xfer->actlen -= xfer->frlengths[0];
403 } else {
404 xfer->actlen = 0;
405 }
406
407 /* check for short packet */
408
409 if (temp > xfer->actlen) {
410 temp = xfer->actlen;
411 if (!(flags & USB_SHORT_XFER_OK)) {
412 err = USB_ERR_SHORT_XFER;
413 }
414 length = temp;
415 }
416 if (temp > 0) {
417 if (req->bmRequestType & UT_READ) {
418 if (flags & USB_USER_DATA_PTR) {
416 mtx_unlock(xfer->priv_mtx);
419 USB_XFER_UNLOCK(xfer);
417 err = usb2_copy_out_user(xfer->frbuffers + 1,
418 0, data, temp);
420 err = usb2_copy_out_user(xfer->frbuffers + 1,
421 0, data, temp);
419 mtx_lock(xfer->priv_mtx);
422 USB_XFER_LOCK(xfer);
420 if (err) {
421 err = USB_ERR_INVAL;
422 break;
423 }
424 } else {
425 usb2_copy_out(xfer->frbuffers + 1,
426 0, data, temp);
427 }
428 }
429 }
430 /*
431 * Clear "frlengths[0]" so that we don't send the setup
432 * packet again:
433 */
434 xfer->frlengths[0] = 0;
435
436 /* update length and data pointer */
437 length -= temp;
438 data = USB_ADD_BYTES(data, temp);
439
440 if (actlen) {
441 (*actlen) += temp;
442 }
443 /* check for timeout */
444
445 delta_ticks = ticks - start_ticks;
446 if (delta_ticks > max_ticks) {
447 if (!err) {
448 err = USB_ERR_TIMEOUT;
449 }
450 }
451 if (err) {
452 break;
453 }
454 }
455
456 if (err) {
457 /*
458 * Make sure that the control endpoint is no longer
459 * blocked in case of a non-transfer related error:
460 */
461 usb2_transfer_stop(xfer);
462 }
423 if (err) {
424 err = USB_ERR_INVAL;
425 break;
426 }
427 } else {
428 usb2_copy_out(xfer->frbuffers + 1,
429 0, data, temp);
430 }
431 }
432 }
433 /*
434 * Clear "frlengths[0]" so that we don't send the setup
435 * packet again:
436 */
437 xfer->frlengths[0] = 0;
438
439 /* update length and data pointer */
440 length -= temp;
441 data = USB_ADD_BYTES(data, temp);
442
443 if (actlen) {
444 (*actlen) += temp;
445 }
446 /* check for timeout */
447
448 delta_ticks = ticks - start_ticks;
449 if (delta_ticks > max_ticks) {
450 if (!err) {
451 err = USB_ERR_TIMEOUT;
452 }
453 }
454 if (err) {
455 break;
456 }
457 }
458
459 if (err) {
460 /*
461 * Make sure that the control endpoint is no longer
462 * blocked in case of a non-transfer related error:
463 */
464 usb2_transfer_stop(xfer);
465 }
463 mtx_unlock(xfer->priv_mtx);
466 USB_XFER_UNLOCK(xfer);
464
465done:
466 sx_xunlock(udev->default_sx);
467
468 if (mtx) {
469 mtx_lock(mtx);
470 }
471 return ((usb2_error_t)err);
472}
473
474/*------------------------------------------------------------------------*
475 * usb2_req_reset_port
476 *
477 * This function will instruct an USB HUB to perform a reset sequence
478 * on the specified port number.
479 *
480 * Returns:
481 * 0: Success. The USB device should now be at address zero.
482 * Else: Failure. No USB device is present and the USB port should be
483 * disabled.
484 *------------------------------------------------------------------------*/
485usb2_error_t
486usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port)
487{
488 struct usb2_port_status ps;
489 usb2_error_t err;
490 uint16_t n;
491
492#if USB_DEBUG
493 uint16_t pr_poll_delay;
494 uint16_t pr_recovery_delay;
495
496#endif
497 err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
498 if (err) {
499 goto done;
500 }
501#if USB_DEBUG
502 /* range check input parameters */
503 pr_poll_delay = usb2_pr_poll_delay;
504 if (pr_poll_delay < 1) {
505 pr_poll_delay = 1;
506 } else if (pr_poll_delay > 1000) {
507 pr_poll_delay = 1000;
508 }
509 pr_recovery_delay = usb2_pr_recovery_delay;
510 if (pr_recovery_delay > 1000) {
511 pr_recovery_delay = 1000;
512 }
513#endif
514 n = 0;
515 while (1) {
516#if USB_DEBUG
517 /* wait for the device to recover from reset */
518 usb2_pause_mtx(mtx, pr_poll_delay);
519 n += pr_poll_delay;
520#else
521 /* wait for the device to recover from reset */
522 usb2_pause_mtx(mtx, USB_PORT_RESET_DELAY);
523 n += USB_PORT_RESET_DELAY;
524#endif
525 err = usb2_req_get_port_status(udev, mtx, &ps, port);
526 if (err) {
527 goto done;
528 }
529 /* if the device disappeared, just give up */
530 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
531 goto done;
532 }
533 /* check if reset is complete */
534 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
535 break;
536 }
537 /* check for timeout */
538 if (n > 1000) {
539 n = 0;
540 break;
541 }
542 }
543
544 /* clear port reset first */
545 err = usb2_req_clear_port_feature(
546 udev, mtx, port, UHF_C_PORT_RESET);
547 if (err) {
548 goto done;
549 }
550 /* check for timeout */
551 if (n == 0) {
552 err = USB_ERR_TIMEOUT;
553 goto done;
554 }
555#if USB_DEBUG
556 /* wait for the device to recover from reset */
557 usb2_pause_mtx(mtx, pr_recovery_delay);
558#else
559 /* wait for the device to recover from reset */
560 usb2_pause_mtx(mtx, USB_PORT_RESET_RECOVERY);
561#endif
562
563done:
564 DPRINTFN(2, "port %d reset returning error=%s\n",
565 port, usb2_errstr(err));
566 return (err);
567}
568
569/*------------------------------------------------------------------------*
570 * usb2_req_get_desc
571 *
572 * This function can be used to retrieve USB descriptors. It contains
573 * some additional logic like zeroing of missing descriptor bytes and
574 * retrying an USB descriptor in case of failure. The "min_len"
575 * argument specifies the minimum descriptor length. The "max_len"
576 * argument specifies the maximum descriptor length. If the real
577 * descriptor length is less than the minimum length the missing
578 * byte(s) will be zeroed. The length field, first byte, of the USB
579 * descriptor will get overwritten in case it indicates a length that
580 * is too big. Also the type field, second byte, of the USB descriptor
581 * will get forced to the correct type.
582 *
583 * Returns:
584 * 0: Success
585 * Else: Failure
586 *------------------------------------------------------------------------*/
587usb2_error_t
588usb2_req_get_desc(struct usb2_device *udev, struct mtx *mtx, void *desc,
589 uint16_t min_len, uint16_t max_len,
590 uint16_t id, uint8_t type, uint8_t index,
591 uint8_t retries)
592{
593 struct usb2_device_request req;
594 uint8_t *buf;
595 usb2_error_t err;
596
597 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
598 id, type, index, max_len);
599
600 req.bmRequestType = UT_READ_DEVICE;
601 req.bRequest = UR_GET_DESCRIPTOR;
602 USETW2(req.wValue, type, index);
603 USETW(req.wIndex, id);
604
605 while (1) {
606
607 if ((min_len < 2) || (max_len < 2)) {
608 err = USB_ERR_INVAL;
609 goto done;
610 }
611 USETW(req.wLength, min_len);
612
613 err = usb2_do_request(udev, mtx, &req, desc);
614
615 if (err) {
616 if (!retries) {
617 goto done;
618 }
619 retries--;
620
621 usb2_pause_mtx(mtx, 200);
622
623 continue;
624 }
625 buf = desc;
626
627 if (min_len == max_len) {
628
629 /* enforce correct type and length */
630
631 if (buf[0] > min_len) {
632 buf[0] = min_len;
633 }
634 buf[1] = type;
635
636 goto done;
637 }
638 /* range check */
639
640 if (max_len > buf[0]) {
641 max_len = buf[0];
642 }
643 /* zero minimum data */
644
645 while (min_len > max_len) {
646 min_len--;
647 buf[min_len] = 0;
648 }
649
650 /* set new minimum length */
651
652 min_len = max_len;
653 }
654done:
655 return (err);
656}
657
658/*------------------------------------------------------------------------*
659 * usb2_req_get_string_any
660 *
661 * This function will return the string given by "string_index"
662 * using the first language ID. The maximum length "len" includes
663 * the terminating zero. The "len" argument should be twice as
664 * big pluss 2 bytes, compared with the actual maximum string length !
665 *
666 * Returns:
667 * 0: Success
668 * Else: Failure
669 *------------------------------------------------------------------------*/
670usb2_error_t
671usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf,
672 uint16_t len, uint8_t string_index)
673{
674 char *s;
675 uint8_t *temp;
676 uint16_t i;
677 uint16_t n;
678 uint16_t c;
679 uint8_t swap;
680 usb2_error_t err;
681
682 if (len == 0) {
683 /* should not happen */
684 return (USB_ERR_NORMAL_COMPLETION);
685 }
686 buf[0] = 0;
687
688 if (string_index == 0) {
689 /* this is the language table */
690 return (USB_ERR_INVAL);
691 }
692 if (udev->flags.no_strings) {
693 return (USB_ERR_STALLED);
694 }
695 err = usb2_req_get_string_desc
696 (udev, mtx, buf, len, udev->langid, string_index);
697 if (err) {
698 return (err);
699 }
700 temp = (uint8_t *)buf;
701
702 if (temp[0] < 2) {
703 /* string length is too short */
704 return (USB_ERR_INVAL);
705 }
706 /* reserve one byte for terminating zero */
707 len--;
708
709 /* find maximum length */
710 s = buf;
711 n = (temp[0] / 2) - 1;
712 if (n > len) {
713 n = len;
714 }
715 /* skip descriptor header */
716 temp += 2;
717
718 /* reset swap state */
719 swap = 3;
720
721 /* convert and filter */
722 for (i = 0; (i != n); i++) {
723 c = UGETW(temp + (2 * i));
724
725 /* convert from Unicode, handle buggy strings */
726 if (((c & 0xff00) == 0) && (swap & 1)) {
727 /* Little Endian, default */
728 *s = c;
729 swap = 1;
730 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
731 /* Big Endian */
732 *s = c >> 8;
733 swap = 2;
734 } else {
735 *s = '.';
736 }
737
738 /*
739 * Filter by default - we don't allow greater and less than
740 * signs because they might confuse the dmesg printouts!
741 */
742 if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
743 *s = '.';
744 }
745 s++;
746 }
747 *s = 0;
748 return (USB_ERR_NORMAL_COMPLETION);
749}
750
751/*------------------------------------------------------------------------*
752 * usb2_req_get_string_desc
753 *
754 * If you don't know the language ID, consider using
755 * "usb2_req_get_string_any()".
756 *
757 * Returns:
758 * 0: Success
759 * Else: Failure
760 *------------------------------------------------------------------------*/
761usb2_error_t
762usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc,
763 uint16_t max_len, uint16_t lang_id,
764 uint8_t string_index)
765{
766 return (usb2_req_get_desc(udev, mtx, sdesc, 2, max_len, lang_id,
767 UDESC_STRING, string_index, 0));
768}
769
770/*------------------------------------------------------------------------*
771 * usb2_req_get_config_desc
772 *
773 * Returns:
774 * 0: Success
775 * Else: Failure
776 *------------------------------------------------------------------------*/
777usb2_error_t
778usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx,
779 struct usb2_config_descriptor *d, uint8_t conf_index)
780{
781 usb2_error_t err;
782
783 DPRINTFN(4, "confidx=%d\n", conf_index);
784
785 err = usb2_req_get_desc(udev, mtx, d, sizeof(*d),
786 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
787 if (err) {
788 goto done;
789 }
790 /* Extra sanity checking */
791 if (UGETW(d->wTotalLength) < sizeof(*d)) {
792 err = USB_ERR_INVAL;
793 }
794done:
795 return (err);
796}
797
798/*------------------------------------------------------------------------*
799 * usb2_req_get_config_desc_full
800 *
801 * This function gets the complete USB configuration descriptor and
802 * ensures that "wTotalLength" is correct.
803 *
804 * Returns:
805 * 0: Success
806 * Else: Failure
807 *------------------------------------------------------------------------*/
808usb2_error_t
809usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx,
810 struct usb2_config_descriptor **ppcd, struct malloc_type *mtype,
811 uint8_t index)
812{
813 struct usb2_config_descriptor cd;
814 struct usb2_config_descriptor *cdesc;
815 uint16_t len;
816 usb2_error_t err;
817
818 DPRINTFN(4, "index=%d\n", index);
819
820 *ppcd = NULL;
821
822 err = usb2_req_get_config_desc(udev, mtx, &cd, index);
823 if (err) {
824 return (err);
825 }
826 /* get full descriptor */
827 len = UGETW(cd.wTotalLength);
828 if (len < sizeof(*cdesc)) {
829 /* corrupt descriptor */
830 return (USB_ERR_INVAL);
831 }
832 cdesc = malloc(len, mtype, M_WAITOK);
833 if (cdesc == NULL) {
834 return (USB_ERR_NOMEM);
835 }
836 err = usb2_req_get_desc(udev, mtx, cdesc, len, len, 0,
837 UDESC_CONFIG, index, 3);
838 if (err) {
839 free(cdesc, mtype);
840 return (err);
841 }
842 /* make sure that the device is not fooling us: */
843 USETW(cdesc->wTotalLength, len);
844
845 *ppcd = cdesc;
846
847 return (0); /* success */
848}
849
850/*------------------------------------------------------------------------*
851 * usb2_req_get_device_desc
852 *
853 * Returns:
854 * 0: Success
855 * Else: Failure
856 *------------------------------------------------------------------------*/
857usb2_error_t
858usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx,
859 struct usb2_device_descriptor *d)
860{
861 DPRINTFN(4, "\n");
862 return (usb2_req_get_desc(udev, mtx, d, sizeof(*d),
863 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
864}
865
866/*------------------------------------------------------------------------*
867 * usb2_req_get_alt_interface_no
868 *
869 * Returns:
870 * 0: Success
871 * Else: Failure
872 *------------------------------------------------------------------------*/
873usb2_error_t
874usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
875 uint8_t *alt_iface_no, uint8_t iface_index)
876{
877 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
878 struct usb2_device_request req;
879
880 if ((iface == NULL) || (iface->idesc == NULL)) {
881 return (USB_ERR_INVAL);
882 }
883 req.bmRequestType = UT_READ_INTERFACE;
884 req.bRequest = UR_GET_INTERFACE;
885 USETW(req.wValue, 0);
886 req.wIndex[0] = iface->idesc->bInterfaceNumber;
887 req.wIndex[1] = 0;
888 USETW(req.wLength, 1);
889 return (usb2_do_request(udev, mtx, &req, alt_iface_no));
890}
891
892/*------------------------------------------------------------------------*
893 * usb2_req_set_alt_interface_no
894 *
895 * Returns:
896 * 0: Success
897 * Else: Failure
898 *------------------------------------------------------------------------*/
899usb2_error_t
900usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
901 uint8_t iface_index, uint8_t alt_no)
902{
903 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
904 struct usb2_device_request req;
905
906 if ((iface == NULL) || (iface->idesc == NULL)) {
907 return (USB_ERR_INVAL);
908 }
909 req.bmRequestType = UT_WRITE_INTERFACE;
910 req.bRequest = UR_SET_INTERFACE;
911 req.wValue[0] = alt_no;
912 req.wValue[1] = 0;
913 req.wIndex[0] = iface->idesc->bInterfaceNumber;
914 req.wIndex[1] = 0;
915 USETW(req.wLength, 0);
916 return (usb2_do_request(udev, mtx, &req, 0));
917}
918
919/*------------------------------------------------------------------------*
920 * usb2_req_get_device_status
921 *
922 * Returns:
923 * 0: Success
924 * Else: Failure
925 *------------------------------------------------------------------------*/
926usb2_error_t
927usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx,
928 struct usb2_status *st)
929{
930 struct usb2_device_request req;
931
932 req.bmRequestType = UT_READ_DEVICE;
933 req.bRequest = UR_GET_STATUS;
934 USETW(req.wValue, 0);
935 USETW(req.wIndex, 0);
936 USETW(req.wLength, sizeof(*st));
937 return (usb2_do_request(udev, mtx, &req, st));
938}
939
940/*------------------------------------------------------------------------*
941 * usb2_req_get_hub_descriptor
942 *
943 * Returns:
944 * 0: Success
945 * Else: Failure
946 *------------------------------------------------------------------------*/
947usb2_error_t
948usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx,
949 struct usb2_hub_descriptor *hd, uint8_t nports)
950{
951 struct usb2_device_request req;
952 uint16_t len = (nports + 7 + (8 * 8)) / 8;
953
954 req.bmRequestType = UT_READ_CLASS_DEVICE;
955 req.bRequest = UR_GET_DESCRIPTOR;
956 USETW2(req.wValue, UDESC_HUB, 0);
957 USETW(req.wIndex, 0);
958 USETW(req.wLength, len);
959 return (usb2_do_request(udev, mtx, &req, hd));
960}
961
962/*------------------------------------------------------------------------*
963 * usb2_req_get_hub_status
964 *
965 * Returns:
966 * 0: Success
967 * Else: Failure
968 *------------------------------------------------------------------------*/
969usb2_error_t
970usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx,
971 struct usb2_hub_status *st)
972{
973 struct usb2_device_request req;
974
975 req.bmRequestType = UT_READ_CLASS_DEVICE;
976 req.bRequest = UR_GET_STATUS;
977 USETW(req.wValue, 0);
978 USETW(req.wIndex, 0);
979 USETW(req.wLength, sizeof(struct usb2_hub_status));
980 return (usb2_do_request(udev, mtx, &req, st));
981}
982
983/*------------------------------------------------------------------------*
984 * usb2_req_set_address
985 *
986 * This function is used to set the address for an USB device. After
987 * port reset the USB device will respond at address zero.
988 *
989 * Returns:
990 * 0: Success
991 * Else: Failure
992 *------------------------------------------------------------------------*/
993usb2_error_t
994usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr)
995{
996 struct usb2_device_request req;
997
998 DPRINTFN(6, "setting device address=%d\n", addr);
999
1000 req.bmRequestType = UT_WRITE_DEVICE;
1001 req.bRequest = UR_SET_ADDRESS;
1002 USETW(req.wValue, addr);
1003 USETW(req.wIndex, 0);
1004 USETW(req.wLength, 0);
1005
1006 /* Setting the address should not take more than 1 second ! */
1007 return (usb2_do_request_flags(udev, mtx, &req, NULL,
1008 USB_DELAY_STATUS_STAGE, NULL, 1000));
1009}
1010
1011/*------------------------------------------------------------------------*
1012 * usb2_req_get_port_status
1013 *
1014 * Returns:
1015 * 0: Success
1016 * Else: Failure
1017 *------------------------------------------------------------------------*/
1018usb2_error_t
1019usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx,
1020 struct usb2_port_status *ps, uint8_t port)
1021{
1022 struct usb2_device_request req;
1023
1024 req.bmRequestType = UT_READ_CLASS_OTHER;
1025 req.bRequest = UR_GET_STATUS;
1026 USETW(req.wValue, 0);
1027 req.wIndex[0] = port;
1028 req.wIndex[1] = 0;
1029 USETW(req.wLength, sizeof *ps);
1030 return (usb2_do_request(udev, mtx, &req, ps));
1031}
1032
1033/*------------------------------------------------------------------------*
1034 * usb2_req_clear_hub_feature
1035 *
1036 * Returns:
1037 * 0: Success
1038 * Else: Failure
1039 *------------------------------------------------------------------------*/
1040usb2_error_t
1041usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1042 uint16_t sel)
1043{
1044 struct usb2_device_request req;
1045
1046 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1047 req.bRequest = UR_CLEAR_FEATURE;
1048 USETW(req.wValue, sel);
1049 USETW(req.wIndex, 0);
1050 USETW(req.wLength, 0);
1051 return (usb2_do_request(udev, mtx, &req, 0));
1052}
1053
1054/*------------------------------------------------------------------------*
1055 * usb2_req_set_hub_feature
1056 *
1057 * Returns:
1058 * 0: Success
1059 * Else: Failure
1060 *------------------------------------------------------------------------*/
1061usb2_error_t
1062usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1063 uint16_t sel)
1064{
1065 struct usb2_device_request req;
1066
1067 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1068 req.bRequest = UR_SET_FEATURE;
1069 USETW(req.wValue, sel);
1070 USETW(req.wIndex, 0);
1071 USETW(req.wLength, 0);
1072 return (usb2_do_request(udev, mtx, &req, 0));
1073}
1074
1075/*------------------------------------------------------------------------*
1076 * usb2_req_clear_port_feature
1077 *
1078 * Returns:
1079 * 0: Success
1080 * Else: Failure
1081 *------------------------------------------------------------------------*/
1082usb2_error_t
1083usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx,
1084 uint8_t port, uint16_t sel)
1085{
1086 struct usb2_device_request req;
1087
1088 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1089 req.bRequest = UR_CLEAR_FEATURE;
1090 USETW(req.wValue, sel);
1091 req.wIndex[0] = port;
1092 req.wIndex[1] = 0;
1093 USETW(req.wLength, 0);
1094 return (usb2_do_request(udev, mtx, &req, 0));
1095}
1096
1097/*------------------------------------------------------------------------*
1098 * usb2_req_set_port_feature
1099 *
1100 * Returns:
1101 * 0: Success
1102 * Else: Failure
1103 *------------------------------------------------------------------------*/
1104usb2_error_t
1105usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx,
1106 uint8_t port, uint16_t sel)
1107{
1108 struct usb2_device_request req;
1109
1110 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1111 req.bRequest = UR_SET_FEATURE;
1112 USETW(req.wValue, sel);
1113 req.wIndex[0] = port;
1114 req.wIndex[1] = 0;
1115 USETW(req.wLength, 0);
1116 return (usb2_do_request(udev, mtx, &req, 0));
1117}
1118
1119/*------------------------------------------------------------------------*
1120 * usb2_req_set_protocol
1121 *
1122 * Returns:
1123 * 0: Success
1124 * Else: Failure
1125 *------------------------------------------------------------------------*/
1126usb2_error_t
1127usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx,
1128 uint8_t iface_index, uint16_t report)
1129{
1130 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1131 struct usb2_device_request req;
1132
1133 if ((iface == NULL) || (iface->idesc == NULL)) {
1134 return (USB_ERR_INVAL);
1135 }
1136 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1137 iface, report, iface->idesc->bInterfaceNumber);
1138
1139 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1140 req.bRequest = UR_SET_PROTOCOL;
1141 USETW(req.wValue, report);
1142 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1143 req.wIndex[1] = 0;
1144 USETW(req.wLength, 0);
1145 return (usb2_do_request(udev, mtx, &req, 0));
1146}
1147
1148/*------------------------------------------------------------------------*
1149 * usb2_req_set_report
1150 *
1151 * Returns:
1152 * 0: Success
1153 * Else: Failure
1154 *------------------------------------------------------------------------*/
1155usb2_error_t
1156usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len,
1157 uint8_t iface_index, uint8_t type, uint8_t id)
1158{
1159 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1160 struct usb2_device_request req;
1161
1162 if ((iface == NULL) || (iface->idesc == NULL)) {
1163 return (USB_ERR_INVAL);
1164 }
1165 DPRINTFN(5, "len=%d\n", len);
1166
1167 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1168 req.bRequest = UR_SET_REPORT;
1169 USETW2(req.wValue, type, id);
1170 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1171 req.wIndex[1] = 0;
1172 USETW(req.wLength, len);
1173 return (usb2_do_request(udev, mtx, &req, data));
1174}
1175
1176/*------------------------------------------------------------------------*
1177 * usb2_req_get_report
1178 *
1179 * Returns:
1180 * 0: Success
1181 * Else: Failure
1182 *------------------------------------------------------------------------*/
1183usb2_error_t
1184usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data,
1185 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1186{
1187 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1188 struct usb2_device_request req;
1189
1190 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1191 return (USB_ERR_INVAL);
1192 }
1193 DPRINTFN(5, "len=%d\n", len);
1194
1195 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1196 req.bRequest = UR_GET_REPORT;
1197 USETW2(req.wValue, type, id);
1198 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1199 req.wIndex[1] = 0;
1200 USETW(req.wLength, len);
1201 return (usb2_do_request(udev, mtx, &req, data));
1202}
1203
1204/*------------------------------------------------------------------------*
1205 * usb2_req_set_idle
1206 *
1207 * Returns:
1208 * 0: Success
1209 * Else: Failure
1210 *------------------------------------------------------------------------*/
1211usb2_error_t
1212usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx,
1213 uint8_t iface_index, uint8_t duration, uint8_t id)
1214{
1215 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1216 struct usb2_device_request req;
1217
1218 if ((iface == NULL) || (iface->idesc == NULL)) {
1219 return (USB_ERR_INVAL);
1220 }
1221 DPRINTFN(5, "%d %d\n", duration, id);
1222
1223 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1224 req.bRequest = UR_SET_IDLE;
1225 USETW2(req.wValue, duration, id);
1226 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1227 req.wIndex[1] = 0;
1228 USETW(req.wLength, 0);
1229 return (usb2_do_request(udev, mtx, &req, 0));
1230}
1231
1232/*------------------------------------------------------------------------*
1233 * usb2_req_get_report_descriptor
1234 *
1235 * Returns:
1236 * 0: Success
1237 * Else: Failure
1238 *------------------------------------------------------------------------*/
1239usb2_error_t
1240usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx,
1241 void *d, uint16_t size, uint8_t iface_index)
1242{
1243 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1244 struct usb2_device_request req;
1245
1246 if ((iface == NULL) || (iface->idesc == NULL)) {
1247 return (USB_ERR_INVAL);
1248 }
1249 req.bmRequestType = UT_READ_INTERFACE;
1250 req.bRequest = UR_GET_DESCRIPTOR;
1251 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1252 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1253 req.wIndex[1] = 0;
1254 USETW(req.wLength, size);
1255 return (usb2_do_request(udev, mtx, &req, d));
1256}
1257
1258/*------------------------------------------------------------------------*
1259 * usb2_req_set_config
1260 *
1261 * This function is used to select the current configuration number in
1262 * both USB device side mode and USB host side mode. When setting the
1263 * configuration the function of the interfaces can change.
1264 *
1265 * Returns:
1266 * 0: Success
1267 * Else: Failure
1268 *------------------------------------------------------------------------*/
1269usb2_error_t
1270usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf)
1271{
1272 struct usb2_device_request req;
1273
1274 DPRINTF("setting config %d\n", conf);
1275
1276 /* do "set configuration" request */
1277
1278 req.bmRequestType = UT_WRITE_DEVICE;
1279 req.bRequest = UR_SET_CONFIG;
1280 req.wValue[0] = conf;
1281 req.wValue[1] = 0;
1282 USETW(req.wIndex, 0);
1283 USETW(req.wLength, 0);
1284 return (usb2_do_request(udev, mtx, &req, 0));
1285}
1286
1287/*------------------------------------------------------------------------*
1288 * usb2_req_get_config
1289 *
1290 * Returns:
1291 * 0: Success
1292 * Else: Failure
1293 *------------------------------------------------------------------------*/
1294usb2_error_t
1295usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf)
1296{
1297 struct usb2_device_request req;
1298
1299 req.bmRequestType = UT_READ_DEVICE;
1300 req.bRequest = UR_GET_CONFIG;
1301 USETW(req.wValue, 0);
1302 USETW(req.wIndex, 0);
1303 USETW(req.wLength, 1);
1304 return (usb2_do_request(udev, mtx, &req, pconf));
1305}
1306
1307/*------------------------------------------------------------------------*
1308 * usb2_req_re_enumerate
1309 *
1310 * Returns:
1311 * 0: Success
1312 * Else: Failure
1313 *------------------------------------------------------------------------*/
1314usb2_error_t
1315usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx)
1316{
1317 struct usb2_device_descriptor ddesc;
1318 struct usb2_device *parent_hub;
1319 usb2_error_t err;
1320 uint8_t old_addr;
1321
1322 old_addr = udev->address;
1323 parent_hub = udev->parent_hub;
1324 if (parent_hub == NULL) {
1325 err = USB_ERR_INVAL;
1326 goto done;
1327 }
1328 err = usb2_req_reset_port(parent_hub, mtx, udev->port_no);
1329 if (err) {
1330 DPRINTFN(0, "addr=%d, port reset failed\n", old_addr);
1331 goto done;
1332 }
1333 /*
1334 * After that the port has been reset our device should be at
1335 * address zero:
1336 */
1337 udev->address = USB_START_ADDR;
1338
1339 /*
1340 * Restore device address:
1341 */
1342 err = usb2_req_set_address(udev, mtx, old_addr);
1343 if (err) {
1344 /* XXX ignore any errors! */
1345 DPRINTFN(0, "addr=%d, set address failed\n",
1346 old_addr);
1347 err = 0;
1348 }
1349 /* restore device address */
1350 udev->address = old_addr;
1351
1352 /* allow device time to set new address */
1353 usb2_pause_mtx(mtx, USB_SET_ADDRESS_SETTLE);
1354
1355 /* get the device descriptor */
1356 err = usb2_req_get_device_desc(udev, mtx, &ddesc);
1357 if (err) {
1358 DPRINTFN(0, "addr=%d, getting device "
1359 "descriptor failed!\n", old_addr);
1360 goto done;
1361 }
1362done:
1363 /* restore address */
1364 udev->address = old_addr;
1365
1366 if (err == 0) {
1367 /* restore configuration */
1368 err = usb2_req_set_config(udev, mtx, udev->curr_config_no);
1369 /* wait a little bit, just in case */
1370 usb2_pause_mtx(mtx, 10);
1371 }
1372 return (err);
1373}
467
468done:
469 sx_xunlock(udev->default_sx);
470
471 if (mtx) {
472 mtx_lock(mtx);
473 }
474 return ((usb2_error_t)err);
475}
476
477/*------------------------------------------------------------------------*
478 * usb2_req_reset_port
479 *
480 * This function will instruct an USB HUB to perform a reset sequence
481 * on the specified port number.
482 *
483 * Returns:
484 * 0: Success. The USB device should now be at address zero.
485 * Else: Failure. No USB device is present and the USB port should be
486 * disabled.
487 *------------------------------------------------------------------------*/
488usb2_error_t
489usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port)
490{
491 struct usb2_port_status ps;
492 usb2_error_t err;
493 uint16_t n;
494
495#if USB_DEBUG
496 uint16_t pr_poll_delay;
497 uint16_t pr_recovery_delay;
498
499#endif
500 err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET);
501 if (err) {
502 goto done;
503 }
504#if USB_DEBUG
505 /* range check input parameters */
506 pr_poll_delay = usb2_pr_poll_delay;
507 if (pr_poll_delay < 1) {
508 pr_poll_delay = 1;
509 } else if (pr_poll_delay > 1000) {
510 pr_poll_delay = 1000;
511 }
512 pr_recovery_delay = usb2_pr_recovery_delay;
513 if (pr_recovery_delay > 1000) {
514 pr_recovery_delay = 1000;
515 }
516#endif
517 n = 0;
518 while (1) {
519#if USB_DEBUG
520 /* wait for the device to recover from reset */
521 usb2_pause_mtx(mtx, pr_poll_delay);
522 n += pr_poll_delay;
523#else
524 /* wait for the device to recover from reset */
525 usb2_pause_mtx(mtx, USB_PORT_RESET_DELAY);
526 n += USB_PORT_RESET_DELAY;
527#endif
528 err = usb2_req_get_port_status(udev, mtx, &ps, port);
529 if (err) {
530 goto done;
531 }
532 /* if the device disappeared, just give up */
533 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) {
534 goto done;
535 }
536 /* check if reset is complete */
537 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) {
538 break;
539 }
540 /* check for timeout */
541 if (n > 1000) {
542 n = 0;
543 break;
544 }
545 }
546
547 /* clear port reset first */
548 err = usb2_req_clear_port_feature(
549 udev, mtx, port, UHF_C_PORT_RESET);
550 if (err) {
551 goto done;
552 }
553 /* check for timeout */
554 if (n == 0) {
555 err = USB_ERR_TIMEOUT;
556 goto done;
557 }
558#if USB_DEBUG
559 /* wait for the device to recover from reset */
560 usb2_pause_mtx(mtx, pr_recovery_delay);
561#else
562 /* wait for the device to recover from reset */
563 usb2_pause_mtx(mtx, USB_PORT_RESET_RECOVERY);
564#endif
565
566done:
567 DPRINTFN(2, "port %d reset returning error=%s\n",
568 port, usb2_errstr(err));
569 return (err);
570}
571
572/*------------------------------------------------------------------------*
573 * usb2_req_get_desc
574 *
575 * This function can be used to retrieve USB descriptors. It contains
576 * some additional logic like zeroing of missing descriptor bytes and
577 * retrying an USB descriptor in case of failure. The "min_len"
578 * argument specifies the minimum descriptor length. The "max_len"
579 * argument specifies the maximum descriptor length. If the real
580 * descriptor length is less than the minimum length the missing
581 * byte(s) will be zeroed. The length field, first byte, of the USB
582 * descriptor will get overwritten in case it indicates a length that
583 * is too big. Also the type field, second byte, of the USB descriptor
584 * will get forced to the correct type.
585 *
586 * Returns:
587 * 0: Success
588 * Else: Failure
589 *------------------------------------------------------------------------*/
590usb2_error_t
591usb2_req_get_desc(struct usb2_device *udev, struct mtx *mtx, void *desc,
592 uint16_t min_len, uint16_t max_len,
593 uint16_t id, uint8_t type, uint8_t index,
594 uint8_t retries)
595{
596 struct usb2_device_request req;
597 uint8_t *buf;
598 usb2_error_t err;
599
600 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
601 id, type, index, max_len);
602
603 req.bmRequestType = UT_READ_DEVICE;
604 req.bRequest = UR_GET_DESCRIPTOR;
605 USETW2(req.wValue, type, index);
606 USETW(req.wIndex, id);
607
608 while (1) {
609
610 if ((min_len < 2) || (max_len < 2)) {
611 err = USB_ERR_INVAL;
612 goto done;
613 }
614 USETW(req.wLength, min_len);
615
616 err = usb2_do_request(udev, mtx, &req, desc);
617
618 if (err) {
619 if (!retries) {
620 goto done;
621 }
622 retries--;
623
624 usb2_pause_mtx(mtx, 200);
625
626 continue;
627 }
628 buf = desc;
629
630 if (min_len == max_len) {
631
632 /* enforce correct type and length */
633
634 if (buf[0] > min_len) {
635 buf[0] = min_len;
636 }
637 buf[1] = type;
638
639 goto done;
640 }
641 /* range check */
642
643 if (max_len > buf[0]) {
644 max_len = buf[0];
645 }
646 /* zero minimum data */
647
648 while (min_len > max_len) {
649 min_len--;
650 buf[min_len] = 0;
651 }
652
653 /* set new minimum length */
654
655 min_len = max_len;
656 }
657done:
658 return (err);
659}
660
661/*------------------------------------------------------------------------*
662 * usb2_req_get_string_any
663 *
664 * This function will return the string given by "string_index"
665 * using the first language ID. The maximum length "len" includes
666 * the terminating zero. The "len" argument should be twice as
667 * big pluss 2 bytes, compared with the actual maximum string length !
668 *
669 * Returns:
670 * 0: Success
671 * Else: Failure
672 *------------------------------------------------------------------------*/
673usb2_error_t
674usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf,
675 uint16_t len, uint8_t string_index)
676{
677 char *s;
678 uint8_t *temp;
679 uint16_t i;
680 uint16_t n;
681 uint16_t c;
682 uint8_t swap;
683 usb2_error_t err;
684
685 if (len == 0) {
686 /* should not happen */
687 return (USB_ERR_NORMAL_COMPLETION);
688 }
689 buf[0] = 0;
690
691 if (string_index == 0) {
692 /* this is the language table */
693 return (USB_ERR_INVAL);
694 }
695 if (udev->flags.no_strings) {
696 return (USB_ERR_STALLED);
697 }
698 err = usb2_req_get_string_desc
699 (udev, mtx, buf, len, udev->langid, string_index);
700 if (err) {
701 return (err);
702 }
703 temp = (uint8_t *)buf;
704
705 if (temp[0] < 2) {
706 /* string length is too short */
707 return (USB_ERR_INVAL);
708 }
709 /* reserve one byte for terminating zero */
710 len--;
711
712 /* find maximum length */
713 s = buf;
714 n = (temp[0] / 2) - 1;
715 if (n > len) {
716 n = len;
717 }
718 /* skip descriptor header */
719 temp += 2;
720
721 /* reset swap state */
722 swap = 3;
723
724 /* convert and filter */
725 for (i = 0; (i != n); i++) {
726 c = UGETW(temp + (2 * i));
727
728 /* convert from Unicode, handle buggy strings */
729 if (((c & 0xff00) == 0) && (swap & 1)) {
730 /* Little Endian, default */
731 *s = c;
732 swap = 1;
733 } else if (((c & 0x00ff) == 0) && (swap & 2)) {
734 /* Big Endian */
735 *s = c >> 8;
736 swap = 2;
737 } else {
738 *s = '.';
739 }
740
741 /*
742 * Filter by default - we don't allow greater and less than
743 * signs because they might confuse the dmesg printouts!
744 */
745 if ((*s == '<') || (*s == '>') || (!isprint(*s))) {
746 *s = '.';
747 }
748 s++;
749 }
750 *s = 0;
751 return (USB_ERR_NORMAL_COMPLETION);
752}
753
754/*------------------------------------------------------------------------*
755 * usb2_req_get_string_desc
756 *
757 * If you don't know the language ID, consider using
758 * "usb2_req_get_string_any()".
759 *
760 * Returns:
761 * 0: Success
762 * Else: Failure
763 *------------------------------------------------------------------------*/
764usb2_error_t
765usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc,
766 uint16_t max_len, uint16_t lang_id,
767 uint8_t string_index)
768{
769 return (usb2_req_get_desc(udev, mtx, sdesc, 2, max_len, lang_id,
770 UDESC_STRING, string_index, 0));
771}
772
773/*------------------------------------------------------------------------*
774 * usb2_req_get_config_desc
775 *
776 * Returns:
777 * 0: Success
778 * Else: Failure
779 *------------------------------------------------------------------------*/
780usb2_error_t
781usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx,
782 struct usb2_config_descriptor *d, uint8_t conf_index)
783{
784 usb2_error_t err;
785
786 DPRINTFN(4, "confidx=%d\n", conf_index);
787
788 err = usb2_req_get_desc(udev, mtx, d, sizeof(*d),
789 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0);
790 if (err) {
791 goto done;
792 }
793 /* Extra sanity checking */
794 if (UGETW(d->wTotalLength) < sizeof(*d)) {
795 err = USB_ERR_INVAL;
796 }
797done:
798 return (err);
799}
800
801/*------------------------------------------------------------------------*
802 * usb2_req_get_config_desc_full
803 *
804 * This function gets the complete USB configuration descriptor and
805 * ensures that "wTotalLength" is correct.
806 *
807 * Returns:
808 * 0: Success
809 * Else: Failure
810 *------------------------------------------------------------------------*/
811usb2_error_t
812usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx,
813 struct usb2_config_descriptor **ppcd, struct malloc_type *mtype,
814 uint8_t index)
815{
816 struct usb2_config_descriptor cd;
817 struct usb2_config_descriptor *cdesc;
818 uint16_t len;
819 usb2_error_t err;
820
821 DPRINTFN(4, "index=%d\n", index);
822
823 *ppcd = NULL;
824
825 err = usb2_req_get_config_desc(udev, mtx, &cd, index);
826 if (err) {
827 return (err);
828 }
829 /* get full descriptor */
830 len = UGETW(cd.wTotalLength);
831 if (len < sizeof(*cdesc)) {
832 /* corrupt descriptor */
833 return (USB_ERR_INVAL);
834 }
835 cdesc = malloc(len, mtype, M_WAITOK);
836 if (cdesc == NULL) {
837 return (USB_ERR_NOMEM);
838 }
839 err = usb2_req_get_desc(udev, mtx, cdesc, len, len, 0,
840 UDESC_CONFIG, index, 3);
841 if (err) {
842 free(cdesc, mtype);
843 return (err);
844 }
845 /* make sure that the device is not fooling us: */
846 USETW(cdesc->wTotalLength, len);
847
848 *ppcd = cdesc;
849
850 return (0); /* success */
851}
852
853/*------------------------------------------------------------------------*
854 * usb2_req_get_device_desc
855 *
856 * Returns:
857 * 0: Success
858 * Else: Failure
859 *------------------------------------------------------------------------*/
860usb2_error_t
861usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx,
862 struct usb2_device_descriptor *d)
863{
864 DPRINTFN(4, "\n");
865 return (usb2_req_get_desc(udev, mtx, d, sizeof(*d),
866 sizeof(*d), 0, UDESC_DEVICE, 0, 3));
867}
868
869/*------------------------------------------------------------------------*
870 * usb2_req_get_alt_interface_no
871 *
872 * Returns:
873 * 0: Success
874 * Else: Failure
875 *------------------------------------------------------------------------*/
876usb2_error_t
877usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
878 uint8_t *alt_iface_no, uint8_t iface_index)
879{
880 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
881 struct usb2_device_request req;
882
883 if ((iface == NULL) || (iface->idesc == NULL)) {
884 return (USB_ERR_INVAL);
885 }
886 req.bmRequestType = UT_READ_INTERFACE;
887 req.bRequest = UR_GET_INTERFACE;
888 USETW(req.wValue, 0);
889 req.wIndex[0] = iface->idesc->bInterfaceNumber;
890 req.wIndex[1] = 0;
891 USETW(req.wLength, 1);
892 return (usb2_do_request(udev, mtx, &req, alt_iface_no));
893}
894
895/*------------------------------------------------------------------------*
896 * usb2_req_set_alt_interface_no
897 *
898 * Returns:
899 * 0: Success
900 * Else: Failure
901 *------------------------------------------------------------------------*/
902usb2_error_t
903usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx,
904 uint8_t iface_index, uint8_t alt_no)
905{
906 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
907 struct usb2_device_request req;
908
909 if ((iface == NULL) || (iface->idesc == NULL)) {
910 return (USB_ERR_INVAL);
911 }
912 req.bmRequestType = UT_WRITE_INTERFACE;
913 req.bRequest = UR_SET_INTERFACE;
914 req.wValue[0] = alt_no;
915 req.wValue[1] = 0;
916 req.wIndex[0] = iface->idesc->bInterfaceNumber;
917 req.wIndex[1] = 0;
918 USETW(req.wLength, 0);
919 return (usb2_do_request(udev, mtx, &req, 0));
920}
921
922/*------------------------------------------------------------------------*
923 * usb2_req_get_device_status
924 *
925 * Returns:
926 * 0: Success
927 * Else: Failure
928 *------------------------------------------------------------------------*/
929usb2_error_t
930usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx,
931 struct usb2_status *st)
932{
933 struct usb2_device_request req;
934
935 req.bmRequestType = UT_READ_DEVICE;
936 req.bRequest = UR_GET_STATUS;
937 USETW(req.wValue, 0);
938 USETW(req.wIndex, 0);
939 USETW(req.wLength, sizeof(*st));
940 return (usb2_do_request(udev, mtx, &req, st));
941}
942
943/*------------------------------------------------------------------------*
944 * usb2_req_get_hub_descriptor
945 *
946 * Returns:
947 * 0: Success
948 * Else: Failure
949 *------------------------------------------------------------------------*/
950usb2_error_t
951usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx,
952 struct usb2_hub_descriptor *hd, uint8_t nports)
953{
954 struct usb2_device_request req;
955 uint16_t len = (nports + 7 + (8 * 8)) / 8;
956
957 req.bmRequestType = UT_READ_CLASS_DEVICE;
958 req.bRequest = UR_GET_DESCRIPTOR;
959 USETW2(req.wValue, UDESC_HUB, 0);
960 USETW(req.wIndex, 0);
961 USETW(req.wLength, len);
962 return (usb2_do_request(udev, mtx, &req, hd));
963}
964
965/*------------------------------------------------------------------------*
966 * usb2_req_get_hub_status
967 *
968 * Returns:
969 * 0: Success
970 * Else: Failure
971 *------------------------------------------------------------------------*/
972usb2_error_t
973usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx,
974 struct usb2_hub_status *st)
975{
976 struct usb2_device_request req;
977
978 req.bmRequestType = UT_READ_CLASS_DEVICE;
979 req.bRequest = UR_GET_STATUS;
980 USETW(req.wValue, 0);
981 USETW(req.wIndex, 0);
982 USETW(req.wLength, sizeof(struct usb2_hub_status));
983 return (usb2_do_request(udev, mtx, &req, st));
984}
985
986/*------------------------------------------------------------------------*
987 * usb2_req_set_address
988 *
989 * This function is used to set the address for an USB device. After
990 * port reset the USB device will respond at address zero.
991 *
992 * Returns:
993 * 0: Success
994 * Else: Failure
995 *------------------------------------------------------------------------*/
996usb2_error_t
997usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr)
998{
999 struct usb2_device_request req;
1000
1001 DPRINTFN(6, "setting device address=%d\n", addr);
1002
1003 req.bmRequestType = UT_WRITE_DEVICE;
1004 req.bRequest = UR_SET_ADDRESS;
1005 USETW(req.wValue, addr);
1006 USETW(req.wIndex, 0);
1007 USETW(req.wLength, 0);
1008
1009 /* Setting the address should not take more than 1 second ! */
1010 return (usb2_do_request_flags(udev, mtx, &req, NULL,
1011 USB_DELAY_STATUS_STAGE, NULL, 1000));
1012}
1013
1014/*------------------------------------------------------------------------*
1015 * usb2_req_get_port_status
1016 *
1017 * Returns:
1018 * 0: Success
1019 * Else: Failure
1020 *------------------------------------------------------------------------*/
1021usb2_error_t
1022usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx,
1023 struct usb2_port_status *ps, uint8_t port)
1024{
1025 struct usb2_device_request req;
1026
1027 req.bmRequestType = UT_READ_CLASS_OTHER;
1028 req.bRequest = UR_GET_STATUS;
1029 USETW(req.wValue, 0);
1030 req.wIndex[0] = port;
1031 req.wIndex[1] = 0;
1032 USETW(req.wLength, sizeof *ps);
1033 return (usb2_do_request(udev, mtx, &req, ps));
1034}
1035
1036/*------------------------------------------------------------------------*
1037 * usb2_req_clear_hub_feature
1038 *
1039 * Returns:
1040 * 0: Success
1041 * Else: Failure
1042 *------------------------------------------------------------------------*/
1043usb2_error_t
1044usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1045 uint16_t sel)
1046{
1047 struct usb2_device_request req;
1048
1049 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1050 req.bRequest = UR_CLEAR_FEATURE;
1051 USETW(req.wValue, sel);
1052 USETW(req.wIndex, 0);
1053 USETW(req.wLength, 0);
1054 return (usb2_do_request(udev, mtx, &req, 0));
1055}
1056
1057/*------------------------------------------------------------------------*
1058 * usb2_req_set_hub_feature
1059 *
1060 * Returns:
1061 * 0: Success
1062 * Else: Failure
1063 *------------------------------------------------------------------------*/
1064usb2_error_t
1065usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx,
1066 uint16_t sel)
1067{
1068 struct usb2_device_request req;
1069
1070 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1071 req.bRequest = UR_SET_FEATURE;
1072 USETW(req.wValue, sel);
1073 USETW(req.wIndex, 0);
1074 USETW(req.wLength, 0);
1075 return (usb2_do_request(udev, mtx, &req, 0));
1076}
1077
1078/*------------------------------------------------------------------------*
1079 * usb2_req_clear_port_feature
1080 *
1081 * Returns:
1082 * 0: Success
1083 * Else: Failure
1084 *------------------------------------------------------------------------*/
1085usb2_error_t
1086usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx,
1087 uint8_t port, uint16_t sel)
1088{
1089 struct usb2_device_request req;
1090
1091 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1092 req.bRequest = UR_CLEAR_FEATURE;
1093 USETW(req.wValue, sel);
1094 req.wIndex[0] = port;
1095 req.wIndex[1] = 0;
1096 USETW(req.wLength, 0);
1097 return (usb2_do_request(udev, mtx, &req, 0));
1098}
1099
1100/*------------------------------------------------------------------------*
1101 * usb2_req_set_port_feature
1102 *
1103 * Returns:
1104 * 0: Success
1105 * Else: Failure
1106 *------------------------------------------------------------------------*/
1107usb2_error_t
1108usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx,
1109 uint8_t port, uint16_t sel)
1110{
1111 struct usb2_device_request req;
1112
1113 req.bmRequestType = UT_WRITE_CLASS_OTHER;
1114 req.bRequest = UR_SET_FEATURE;
1115 USETW(req.wValue, sel);
1116 req.wIndex[0] = port;
1117 req.wIndex[1] = 0;
1118 USETW(req.wLength, 0);
1119 return (usb2_do_request(udev, mtx, &req, 0));
1120}
1121
1122/*------------------------------------------------------------------------*
1123 * usb2_req_set_protocol
1124 *
1125 * Returns:
1126 * 0: Success
1127 * Else: Failure
1128 *------------------------------------------------------------------------*/
1129usb2_error_t
1130usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx,
1131 uint8_t iface_index, uint16_t report)
1132{
1133 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1134 struct usb2_device_request req;
1135
1136 if ((iface == NULL) || (iface->idesc == NULL)) {
1137 return (USB_ERR_INVAL);
1138 }
1139 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n",
1140 iface, report, iface->idesc->bInterfaceNumber);
1141
1142 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1143 req.bRequest = UR_SET_PROTOCOL;
1144 USETW(req.wValue, report);
1145 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1146 req.wIndex[1] = 0;
1147 USETW(req.wLength, 0);
1148 return (usb2_do_request(udev, mtx, &req, 0));
1149}
1150
1151/*------------------------------------------------------------------------*
1152 * usb2_req_set_report
1153 *
1154 * Returns:
1155 * 0: Success
1156 * Else: Failure
1157 *------------------------------------------------------------------------*/
1158usb2_error_t
1159usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len,
1160 uint8_t iface_index, uint8_t type, uint8_t id)
1161{
1162 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1163 struct usb2_device_request req;
1164
1165 if ((iface == NULL) || (iface->idesc == NULL)) {
1166 return (USB_ERR_INVAL);
1167 }
1168 DPRINTFN(5, "len=%d\n", len);
1169
1170 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1171 req.bRequest = UR_SET_REPORT;
1172 USETW2(req.wValue, type, id);
1173 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1174 req.wIndex[1] = 0;
1175 USETW(req.wLength, len);
1176 return (usb2_do_request(udev, mtx, &req, data));
1177}
1178
1179/*------------------------------------------------------------------------*
1180 * usb2_req_get_report
1181 *
1182 * Returns:
1183 * 0: Success
1184 * Else: Failure
1185 *------------------------------------------------------------------------*/
1186usb2_error_t
1187usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data,
1188 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id)
1189{
1190 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1191 struct usb2_device_request req;
1192
1193 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) {
1194 return (USB_ERR_INVAL);
1195 }
1196 DPRINTFN(5, "len=%d\n", len);
1197
1198 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1199 req.bRequest = UR_GET_REPORT;
1200 USETW2(req.wValue, type, id);
1201 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1202 req.wIndex[1] = 0;
1203 USETW(req.wLength, len);
1204 return (usb2_do_request(udev, mtx, &req, data));
1205}
1206
1207/*------------------------------------------------------------------------*
1208 * usb2_req_set_idle
1209 *
1210 * Returns:
1211 * 0: Success
1212 * Else: Failure
1213 *------------------------------------------------------------------------*/
1214usb2_error_t
1215usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx,
1216 uint8_t iface_index, uint8_t duration, uint8_t id)
1217{
1218 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1219 struct usb2_device_request req;
1220
1221 if ((iface == NULL) || (iface->idesc == NULL)) {
1222 return (USB_ERR_INVAL);
1223 }
1224 DPRINTFN(5, "%d %d\n", duration, id);
1225
1226 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1227 req.bRequest = UR_SET_IDLE;
1228 USETW2(req.wValue, duration, id);
1229 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1230 req.wIndex[1] = 0;
1231 USETW(req.wLength, 0);
1232 return (usb2_do_request(udev, mtx, &req, 0));
1233}
1234
1235/*------------------------------------------------------------------------*
1236 * usb2_req_get_report_descriptor
1237 *
1238 * Returns:
1239 * 0: Success
1240 * Else: Failure
1241 *------------------------------------------------------------------------*/
1242usb2_error_t
1243usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx,
1244 void *d, uint16_t size, uint8_t iface_index)
1245{
1246 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
1247 struct usb2_device_request req;
1248
1249 if ((iface == NULL) || (iface->idesc == NULL)) {
1250 return (USB_ERR_INVAL);
1251 }
1252 req.bmRequestType = UT_READ_INTERFACE;
1253 req.bRequest = UR_GET_DESCRIPTOR;
1254 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
1255 req.wIndex[0] = iface->idesc->bInterfaceNumber;
1256 req.wIndex[1] = 0;
1257 USETW(req.wLength, size);
1258 return (usb2_do_request(udev, mtx, &req, d));
1259}
1260
1261/*------------------------------------------------------------------------*
1262 * usb2_req_set_config
1263 *
1264 * This function is used to select the current configuration number in
1265 * both USB device side mode and USB host side mode. When setting the
1266 * configuration the function of the interfaces can change.
1267 *
1268 * Returns:
1269 * 0: Success
1270 * Else: Failure
1271 *------------------------------------------------------------------------*/
1272usb2_error_t
1273usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf)
1274{
1275 struct usb2_device_request req;
1276
1277 DPRINTF("setting config %d\n", conf);
1278
1279 /* do "set configuration" request */
1280
1281 req.bmRequestType = UT_WRITE_DEVICE;
1282 req.bRequest = UR_SET_CONFIG;
1283 req.wValue[0] = conf;
1284 req.wValue[1] = 0;
1285 USETW(req.wIndex, 0);
1286 USETW(req.wLength, 0);
1287 return (usb2_do_request(udev, mtx, &req, 0));
1288}
1289
1290/*------------------------------------------------------------------------*
1291 * usb2_req_get_config
1292 *
1293 * Returns:
1294 * 0: Success
1295 * Else: Failure
1296 *------------------------------------------------------------------------*/
1297usb2_error_t
1298usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf)
1299{
1300 struct usb2_device_request req;
1301
1302 req.bmRequestType = UT_READ_DEVICE;
1303 req.bRequest = UR_GET_CONFIG;
1304 USETW(req.wValue, 0);
1305 USETW(req.wIndex, 0);
1306 USETW(req.wLength, 1);
1307 return (usb2_do_request(udev, mtx, &req, pconf));
1308}
1309
1310/*------------------------------------------------------------------------*
1311 * usb2_req_re_enumerate
1312 *
1313 * Returns:
1314 * 0: Success
1315 * Else: Failure
1316 *------------------------------------------------------------------------*/
1317usb2_error_t
1318usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx)
1319{
1320 struct usb2_device_descriptor ddesc;
1321 struct usb2_device *parent_hub;
1322 usb2_error_t err;
1323 uint8_t old_addr;
1324
1325 old_addr = udev->address;
1326 parent_hub = udev->parent_hub;
1327 if (parent_hub == NULL) {
1328 err = USB_ERR_INVAL;
1329 goto done;
1330 }
1331 err = usb2_req_reset_port(parent_hub, mtx, udev->port_no);
1332 if (err) {
1333 DPRINTFN(0, "addr=%d, port reset failed\n", old_addr);
1334 goto done;
1335 }
1336 /*
1337 * After that the port has been reset our device should be at
1338 * address zero:
1339 */
1340 udev->address = USB_START_ADDR;
1341
1342 /*
1343 * Restore device address:
1344 */
1345 err = usb2_req_set_address(udev, mtx, old_addr);
1346 if (err) {
1347 /* XXX ignore any errors! */
1348 DPRINTFN(0, "addr=%d, set address failed\n",
1349 old_addr);
1350 err = 0;
1351 }
1352 /* restore device address */
1353 udev->address = old_addr;
1354
1355 /* allow device time to set new address */
1356 usb2_pause_mtx(mtx, USB_SET_ADDRESS_SETTLE);
1357
1358 /* get the device descriptor */
1359 err = usb2_req_get_device_desc(udev, mtx, &ddesc);
1360 if (err) {
1361 DPRINTFN(0, "addr=%d, getting device "
1362 "descriptor failed!\n", old_addr);
1363 goto done;
1364 }
1365done:
1366 /* restore address */
1367 udev->address = old_addr;
1368
1369 if (err == 0) {
1370 /* restore configuration */
1371 err = usb2_req_set_config(udev, mtx, udev->curr_config_no);
1372 /* wait a little bit, just in case */
1373 usb2_pause_mtx(mtx, 10);
1374 }
1375 return (err);
1376}