Deleted Added
sdiff udiff text old ( 184610 ) new ( 184824 )
full compact
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
48#include <dev/usb2/controller/usb2_controller.h>
49#include <dev/usb2/controller/usb2_bus.h>
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
103 USB_BUS_LOCK(xfer->udev->bus);
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;
149 USB_BUS_UNLOCK(xfer->udev->bus);
150
151 usb2_start_hardware(xfer);
152
153 USB_BUS_LOCK(xfer->udev->bus);
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;
170 USB_BUS_UNLOCK(xfer->udev->bus);
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 }
318 USB_XFER_LOCK(xfer);
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) {
347 USB_XFER_UNLOCK(xfer);
348 err = usb2_copy_in_user(xfer->frbuffers + 1,
349 0, data, temp);
350 USB_XFER_LOCK(xfer);
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(
372 xfer->xfer_mtx, temp);
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 {
389 usb2_cv_wait(xfer->udev->default_cv,
390 xfer->xfer_mtx);
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) {
419 USB_XFER_UNLOCK(xfer);
420 err = usb2_copy_out_user(xfer->frbuffers + 1,
421 0, data, temp);
422 USB_XFER_LOCK(xfer);
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 }
466 USB_XFER_UNLOCK(xfer);
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}