usbdi.c revision 1.187 1 /* $NetBSD: usbdi.c,v 1.187 2020/02/08 07:38:17 maxv Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology, Matthew R. Green (mrg (at) eterna.com.au),
10 * and Nick Hudson.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.187 2020/02/08 07:38:17 maxv Exp $");
36
37 #ifdef _KERNEL_OPT
38 #include "opt_usb.h"
39 #include "opt_compat_netbsd.h"
40 #include "usb_dma.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #include <sys/kmem.h>
48 #include <sys/proc.h>
49 #include <sys/bus.h>
50 #include <sys/cpu.h>
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usbdivar.h>
56 #include <dev/usb/usb_mem.h>
57 #include <dev/usb/usb_quirks.h>
58 #include <dev/usb/usbhist.h>
59
60 /* UTF-8 encoding stuff */
61 #include <fs/unicode.h>
62
63 extern int usbdebug;
64
65 Static usbd_status usbd_ar_pipe(struct usbd_pipe *);
66 Static void usbd_start_next(struct usbd_pipe *);
67 Static usbd_status usbd_open_pipe_ival
68 (struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
69 static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
70 static void usbd_free_buffer(struct usbd_xfer *);
71 static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
72 static usbd_status usbd_free_xfer(struct usbd_xfer *);
73 static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
74
75 #if defined(USB_DEBUG)
76 void
77 usbd_dump_iface(struct usbd_interface *iface)
78 {
79 USBHIST_FUNC();
80 USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
81
82 if (iface == NULL)
83 return;
84 USBHIST_LOG(usbdebug, " device = %#jx idesc = %#jx index = %d",
85 (uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
86 iface->ui_index, 0);
87 USBHIST_LOG(usbdebug, " altindex=%d priv=%#jx",
88 iface->ui_altindex, (uintptr_t)iface->ui_priv, 0, 0);
89 }
90
91 void
92 usbd_dump_device(struct usbd_device *dev)
93 {
94 USBHIST_FUNC();
95 USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
96
97 if (dev == NULL)
98 return;
99 USBHIST_LOG(usbdebug, " bus = %#jx default_pipe = %#jx",
100 (uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
101 USBHIST_LOG(usbdebug, " address = %jd config = %jd depth = %jd ",
102 dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
103 USBHIST_LOG(usbdebug, " speed = %jd self_powered = %jd "
104 "power = %jd langid = %jd",
105 dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
106 }
107
108 void
109 usbd_dump_endpoint(struct usbd_endpoint *endp)
110 {
111 USBHIST_FUNC();
112 USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
113
114 if (endp == NULL)
115 return;
116 USBHIST_LOG(usbdebug, " edesc = %#jx refcnt = %jd",
117 (uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
118 if (endp->ue_edesc)
119 USBHIST_LOG(usbdebug, " bEndpointAddress=0x%02x",
120 endp->ue_edesc->bEndpointAddress, 0, 0, 0);
121 }
122
123 void
124 usbd_dump_queue(struct usbd_pipe *pipe)
125 {
126 struct usbd_xfer *xfer;
127
128 USBHIST_FUNC();
129 USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
130
131 SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
132 USBHIST_LOG(usbdebug, " xfer = %#jx", (uintptr_t)xfer,
133 0, 0, 0);
134 }
135 }
136
137 void
138 usbd_dump_pipe(struct usbd_pipe *pipe)
139 {
140 USBHIST_FUNC();
141 USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
142
143 if (pipe == NULL)
144 return;
145 usbd_dump_iface(pipe->up_iface);
146 usbd_dump_device(pipe->up_dev);
147 usbd_dump_endpoint(pipe->up_endpoint);
148 USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
149 USBHIST_LOG(usbdebug, " running = %jd aborting = %jd",
150 pipe->up_running, pipe->up_aborting, 0, 0);
151 USBHIST_LOG(usbdebug, " intrxfer = %#jx, repeat = %jd, "
152 "interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
153 pipe->up_interval, 0);
154 }
155 #endif
156
157 usbd_status
158 usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
159 uint8_t flags, struct usbd_pipe **pipe)
160 {
161 return (usbd_open_pipe_ival(iface, address, flags, pipe,
162 USBD_DEFAULT_INTERVAL));
163 }
164
165 usbd_status
166 usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
167 uint8_t flags, struct usbd_pipe **pipe, int ival)
168 {
169 struct usbd_pipe *p;
170 struct usbd_endpoint *ep;
171 usbd_status err;
172 int i;
173
174 USBHIST_FUNC();
175 USBHIST_CALLARGS(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
176 (uintptr_t)iface, address, flags, 0);
177
178 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
179 ep = &iface->ui_endpoints[i];
180 if (ep->ue_edesc == NULL)
181 return USBD_IOERROR;
182 if (ep->ue_edesc->bEndpointAddress == address)
183 goto found;
184 }
185 return USBD_BAD_ADDRESS;
186 found:
187 if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
188 return USBD_IN_USE;
189 err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
190 if (err)
191 return err;
192 LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
193 *pipe = p;
194 return USBD_NORMAL_COMPLETION;
195 }
196
197 usbd_status
198 usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
199 uint8_t flags, struct usbd_pipe **pipe,
200 void *priv, void *buffer, uint32_t len,
201 usbd_callback cb, int ival)
202 {
203 usbd_status err;
204 struct usbd_xfer *xfer;
205 struct usbd_pipe *ipipe;
206
207 USBHIST_FUNC();
208 USBHIST_CALLARGS(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
209 address, flags, len, 0);
210
211 err = usbd_open_pipe_ival(iface, address,
212 USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
213 &ipipe, ival);
214 if (err)
215 return err;
216 err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
217 if (err)
218 goto bad1;
219
220 usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
221 ipipe->up_intrxfer = xfer;
222 ipipe->up_repeat = 1;
223 err = usbd_transfer(xfer);
224 *pipe = ipipe;
225 if (err != USBD_IN_PROGRESS)
226 goto bad3;
227 return USBD_NORMAL_COMPLETION;
228
229 bad3:
230 ipipe->up_intrxfer = NULL;
231 ipipe->up_repeat = 0;
232
233 usbd_destroy_xfer(xfer);
234 bad1:
235 usbd_close_pipe(ipipe);
236 return err;
237 }
238
239 usbd_status
240 usbd_close_pipe(struct usbd_pipe *pipe)
241 {
242 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
243
244 KASSERT(pipe != NULL);
245
246 usbd_lock_pipe(pipe);
247
248 if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
249 printf("WARNING: pipe closed with active xfers on addr %d\n",
250 pipe->up_dev->ud_addr);
251 usbd_ar_pipe(pipe);
252 }
253
254 KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
255
256 LIST_REMOVE(pipe, up_next);
257 pipe->up_endpoint->ue_refcnt--;
258
259 pipe->up_methods->upm_close(pipe);
260
261 if (pipe->up_intrxfer != NULL) {
262 usbd_unlock_pipe(pipe);
263 usbd_destroy_xfer(pipe->up_intrxfer);
264 usbd_lock_pipe(pipe);
265 }
266
267 usbd_unlock_pipe(pipe);
268 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
269
270 return USBD_NORMAL_COMPLETION;
271 }
272
273 usbd_status
274 usbd_transfer(struct usbd_xfer *xfer)
275 {
276 struct usbd_pipe *pipe = xfer->ux_pipe;
277 usbd_status err;
278 unsigned int size, flags;
279
280 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
281 "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
282 (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
283 KASSERT(xfer->ux_status == USBD_NOT_STARTED);
284
285 #ifdef USB_DEBUG
286 if (usbdebug > 5)
287 usbd_dump_queue(pipe);
288 #endif
289 xfer->ux_done = 0;
290
291 if (pipe->up_aborting) {
292 USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
293 (uintptr_t)xfer, 0, 0, 0);
294 return USBD_CANCELLED;
295 }
296
297 KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
298
299 size = xfer->ux_length;
300 flags = xfer->ux_flags;
301
302 if (size != 0) {
303 /*
304 * Use the xfer buffer if none specified in transfer setup.
305 * isoc transfers always use the xfer buffer, i.e.
306 * ux_buffer is always NULL for isoc.
307 */
308 if (xfer->ux_buffer == NULL) {
309 xfer->ux_buffer = xfer->ux_buf;
310 }
311
312 /*
313 * If not using the xfer buffer copy data to the
314 * xfer buffer for OUT transfers of >0 length
315 */
316 if (xfer->ux_buffer != xfer->ux_buf) {
317 KASSERT(xfer->ux_buf);
318 if (!usbd_xfer_isread(xfer)) {
319 memcpy(xfer->ux_buf, xfer->ux_buffer, size);
320 }
321 }
322 }
323
324 /* xfer is not valid after the transfer method unless synchronous */
325 err = pipe->up_methods->upm_transfer(xfer);
326
327 if (err != USBD_IN_PROGRESS && err) {
328 /*
329 * The transfer made it onto the pipe queue, but didn't get
330 * accepted by the HCD for some reason. It needs removing
331 * from the pipe queue.
332 */
333 USBHIST_LOG(usbdebug, "xfer failed: %s, reinserting",
334 err, 0, 0, 0);
335 usbd_lock_pipe(pipe);
336 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
337 if (pipe->up_serialise)
338 usbd_start_next(pipe);
339 usbd_unlock_pipe(pipe);
340 }
341
342 if (!(flags & USBD_SYNCHRONOUS)) {
343 USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
344 (uintptr_t)xfer, err, 0, 0);
345 return err;
346 }
347
348 if (err != USBD_IN_PROGRESS) {
349 USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
350 (uintptr_t)xfer, err, 0, 0);
351 return err;
352 }
353
354 /* Sync transfer, wait for completion. */
355 usbd_lock_pipe(pipe);
356 while (!xfer->ux_done) {
357 if (pipe->up_dev->ud_bus->ub_usepolling)
358 panic("usbd_transfer: not done");
359 USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
360 (uintptr_t)xfer, 0, 0, 0);
361
362 err = 0;
363 if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
364 err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
365 } else {
366 cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
367 }
368 if (err) {
369 if (!xfer->ux_done)
370 pipe->up_methods->upm_abort(xfer);
371 break;
372 }
373 }
374 usbd_unlock_pipe(pipe);
375 return xfer->ux_status;
376 }
377
378 /* Like usbd_transfer(), but waits for completion. */
379 usbd_status
380 usbd_sync_transfer(struct usbd_xfer *xfer)
381 {
382 xfer->ux_flags |= USBD_SYNCHRONOUS;
383 return usbd_transfer(xfer);
384 }
385
386 /* Like usbd_transfer(), but waits for completion and listens for signals. */
387 usbd_status
388 usbd_sync_transfer_sig(struct usbd_xfer *xfer)
389 {
390 xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
391 return usbd_transfer(xfer);
392 }
393
394 static void *
395 usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
396 {
397 KASSERT(xfer->ux_buf == NULL);
398 KASSERT(size != 0);
399
400 xfer->ux_bufsize = 0;
401 #if NUSB_DMA > 0
402 struct usbd_bus *bus = xfer->ux_bus;
403
404 if (bus->ub_usedma) {
405 usb_dma_t *dmap = &xfer->ux_dmabuf;
406
407 int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
408 if (err) {
409 return NULL;
410 }
411 xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
412 xfer->ux_bufsize = size;
413
414 return xfer->ux_buf;
415 }
416 #endif
417 KASSERT(xfer->ux_bus->ub_usedma == false);
418 xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
419 xfer->ux_bufsize = size;
420 return xfer->ux_buf;
421 }
422
423 static void
424 usbd_free_buffer(struct usbd_xfer *xfer)
425 {
426 KASSERT(xfer->ux_buf != NULL);
427 KASSERT(xfer->ux_bufsize != 0);
428
429 void *buf = xfer->ux_buf;
430 uint32_t size = xfer->ux_bufsize;
431
432 xfer->ux_buf = NULL;
433 xfer->ux_bufsize = 0;
434
435 #if NUSB_DMA > 0
436 struct usbd_bus *bus = xfer->ux_bus;
437
438 if (bus->ub_usedma) {
439 usb_dma_t *dmap = &xfer->ux_dmabuf;
440
441 usb_freemem(bus, dmap);
442 return;
443 }
444 #endif
445 KASSERT(xfer->ux_bus->ub_usedma == false);
446
447 kmem_free(buf, size);
448 }
449
450 void *
451 usbd_get_buffer(struct usbd_xfer *xfer)
452 {
453 return xfer->ux_buf;
454 }
455
456 struct usbd_pipe *
457 usbd_get_pipe0(struct usbd_device *dev)
458 {
459
460 return dev->ud_pipe0;
461 }
462
463 static struct usbd_xfer *
464 usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
465 {
466 struct usbd_xfer *xfer;
467
468 USBHIST_FUNC();
469
470 ASSERT_SLEEPABLE();
471
472 xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
473 if (xfer == NULL)
474 goto out;
475 xfer->ux_bus = dev->ud_bus;
476 callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
477 cv_init(&xfer->ux_cv, "usbxfer");
478
479 out:
480 USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
481
482 return xfer;
483 }
484
485 static usbd_status
486 usbd_free_xfer(struct usbd_xfer *xfer)
487 {
488 USBHIST_FUNC();
489 USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
490
491 if (xfer->ux_buf) {
492 usbd_free_buffer(xfer);
493 }
494 #if defined(DIAGNOSTIC)
495 if (callout_pending(&xfer->ux_callout)) {
496 callout_stop(&xfer->ux_callout);
497 printf("usbd_free_xfer: timeout_handle pending\n");
498 }
499 #endif
500 cv_destroy(&xfer->ux_cv);
501 xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
502 return USBD_NORMAL_COMPLETION;
503 }
504
505 int
506 usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
507 unsigned int nframes, struct usbd_xfer **xp)
508 {
509 KASSERT(xp != NULL);
510 void *buf = NULL;
511
512 struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
513 if (xfer == NULL)
514 return ENOMEM;
515
516 if (len) {
517 buf = usbd_alloc_buffer(xfer, len);
518 if (!buf) {
519 usbd_free_xfer(xfer);
520 return ENOMEM;
521 }
522 }
523 xfer->ux_pipe = pipe;
524 xfer->ux_flags = flags;
525 xfer->ux_nframes = nframes;
526 xfer->ux_methods = pipe->up_methods;
527
528 if (xfer->ux_methods->upm_init) {
529 int err = xfer->ux_methods->upm_init(xfer);
530 if (err) {
531 if (buf)
532 usbd_free_buffer(xfer);
533 usbd_free_xfer(xfer);
534 return err;
535 }
536 }
537
538 *xp = xfer;
539 return 0;
540 }
541
542 void
543 usbd_destroy_xfer(struct usbd_xfer *xfer)
544 {
545
546 if (xfer->ux_methods->upm_fini) {
547 xfer->ux_methods->upm_fini(xfer);
548 }
549
550 usbd_free_xfer(xfer);
551 }
552
553 void
554 usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
555 uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
556 {
557 KASSERT(xfer->ux_pipe);
558
559 xfer->ux_priv = priv;
560 xfer->ux_buffer = buffer;
561 xfer->ux_length = length;
562 xfer->ux_actlen = 0;
563 xfer->ux_flags = flags;
564 xfer->ux_timeout = timeout;
565 xfer->ux_status = USBD_NOT_STARTED;
566 xfer->ux_callback = callback;
567 xfer->ux_rqflags &= ~URQ_REQUEST;
568 xfer->ux_nframes = 0;
569 }
570
571 void
572 usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
573 void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
574 uint32_t length, uint16_t flags, usbd_callback callback)
575 {
576 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
577
578 xfer->ux_priv = priv;
579 xfer->ux_buffer = buffer;
580 xfer->ux_length = length;
581 xfer->ux_actlen = 0;
582 xfer->ux_flags = flags;
583 xfer->ux_timeout = timeout;
584 xfer->ux_status = USBD_NOT_STARTED;
585 xfer->ux_callback = callback;
586 xfer->ux_request = *req;
587 xfer->ux_rqflags |= URQ_REQUEST;
588 xfer->ux_nframes = 0;
589 }
590
591 void
592 usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
593 uint32_t nframes, uint16_t flags, usbd_callback callback)
594 {
595 xfer->ux_priv = priv;
596 xfer->ux_buffer = NULL;
597 xfer->ux_length = 0;
598 xfer->ux_actlen = 0;
599 xfer->ux_flags = flags;
600 xfer->ux_timeout = USBD_NO_TIMEOUT;
601 xfer->ux_status = USBD_NOT_STARTED;
602 xfer->ux_callback = callback;
603 xfer->ux_rqflags &= ~URQ_REQUEST;
604 xfer->ux_frlengths = frlengths;
605 xfer->ux_nframes = nframes;
606 }
607
608 void
609 usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
610 void **buffer, uint32_t *count, usbd_status *status)
611 {
612 if (priv != NULL)
613 *priv = xfer->ux_priv;
614 if (buffer != NULL)
615 *buffer = xfer->ux_buffer;
616 if (count != NULL)
617 *count = xfer->ux_actlen;
618 if (status != NULL)
619 *status = xfer->ux_status;
620 }
621
622 usb_config_descriptor_t *
623 usbd_get_config_descriptor(struct usbd_device *dev)
624 {
625 KASSERT(dev != NULL);
626
627 return dev->ud_cdesc;
628 }
629
630 usb_interface_descriptor_t *
631 usbd_get_interface_descriptor(struct usbd_interface *iface)
632 {
633 KASSERT(iface != NULL);
634
635 return iface->ui_idesc;
636 }
637
638 usb_device_descriptor_t *
639 usbd_get_device_descriptor(struct usbd_device *dev)
640 {
641 KASSERT(dev != NULL);
642
643 return &dev->ud_ddesc;
644 }
645
646 usb_endpoint_descriptor_t *
647 usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
648 {
649
650 if (index >= iface->ui_idesc->bNumEndpoints)
651 return NULL;
652 return iface->ui_endpoints[index].ue_edesc;
653 }
654
655 /* Some drivers may wish to abort requests on the default pipe, *
656 * but there is no mechanism for getting a handle on it. */
657 usbd_status
658 usbd_abort_default_pipe(struct usbd_device *device)
659 {
660 return usbd_abort_pipe(device->ud_pipe0);
661 }
662
663 usbd_status
664 usbd_abort_pipe(struct usbd_pipe *pipe)
665 {
666 usbd_status err;
667
668 KASSERT(pipe != NULL);
669
670 usbd_lock_pipe(pipe);
671 err = usbd_ar_pipe(pipe);
672 usbd_unlock_pipe(pipe);
673 return err;
674 }
675
676 usbd_status
677 usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
678 {
679 struct usbd_device *dev = pipe->up_dev;
680 usb_device_request_t req;
681 usbd_status err;
682
683 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
684
685 /*
686 * Clearing en endpoint stall resets the endpoint toggle, so
687 * do the same to the HC toggle.
688 */
689 pipe->up_methods->upm_cleartoggle(pipe);
690
691 req.bmRequestType = UT_WRITE_ENDPOINT;
692 req.bRequest = UR_CLEAR_FEATURE;
693 USETW(req.wValue, UF_ENDPOINT_HALT);
694 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
695 USETW(req.wLength, 0);
696 err = usbd_do_request(dev, &req, 0);
697 #if 0
698 XXX should we do this?
699 if (!err) {
700 pipe->state = USBD_PIPE_ACTIVE;
701 /* XXX activate pipe */
702 }
703 #endif
704 return err;
705 }
706
707 void
708 usbd_clear_endpoint_stall_task(void *arg)
709 {
710 struct usbd_pipe *pipe = arg;
711 struct usbd_device *dev = pipe->up_dev;
712 usb_device_request_t req;
713
714 pipe->up_methods->upm_cleartoggle(pipe);
715
716 req.bmRequestType = UT_WRITE_ENDPOINT;
717 req.bRequest = UR_CLEAR_FEATURE;
718 USETW(req.wValue, UF_ENDPOINT_HALT);
719 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
720 USETW(req.wLength, 0);
721 (void)usbd_do_request(dev, &req, 0);
722 }
723
724 void
725 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
726 {
727 usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
728 }
729
730 void
731 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
732 {
733
734 pipe->up_methods->upm_cleartoggle(pipe);
735 }
736
737 usbd_status
738 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
739 {
740 KASSERT(iface != NULL);
741 KASSERT(iface->ui_idesc != NULL);
742
743 *count = iface->ui_idesc->bNumEndpoints;
744 return USBD_NORMAL_COMPLETION;
745 }
746
747 usbd_status
748 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
749 {
750
751 if (dev->ud_cdesc == NULL)
752 return USBD_NOT_CONFIGURED;
753 *count = dev->ud_cdesc->bNumInterface;
754 return USBD_NORMAL_COMPLETION;
755 }
756
757 void
758 usbd_interface2device_handle(struct usbd_interface *iface,
759 struct usbd_device **dev)
760 {
761
762 *dev = iface->ui_dev;
763 }
764
765 usbd_status
766 usbd_device2interface_handle(struct usbd_device *dev,
767 uint8_t ifaceno, struct usbd_interface **iface)
768 {
769
770 if (dev->ud_cdesc == NULL)
771 return USBD_NOT_CONFIGURED;
772 if (ifaceno >= dev->ud_cdesc->bNumInterface)
773 return USBD_INVAL;
774 *iface = &dev->ud_ifaces[ifaceno];
775 return USBD_NORMAL_COMPLETION;
776 }
777
778 struct usbd_device *
779 usbd_pipe2device_handle(struct usbd_pipe *pipe)
780 {
781 KASSERT(pipe != NULL);
782
783 return pipe->up_dev;
784 }
785
786 /* XXXX use altno */
787 usbd_status
788 usbd_set_interface(struct usbd_interface *iface, int altidx)
789 {
790 usb_device_request_t req;
791 usbd_status err;
792 void *endpoints;
793
794 USBHIST_FUNC();
795
796 if (LIST_FIRST(&iface->ui_pipes) != NULL)
797 return USBD_IN_USE;
798
799 endpoints = iface->ui_endpoints;
800 int nendpt = iface->ui_idesc->bNumEndpoints;
801 USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
802 (uintptr_t)iface, (uintptr_t)endpoints,
803 iface->ui_idesc->bNumEndpoints, 0);
804 err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
805 if (err)
806 return err;
807
808 /* new setting works, we can free old endpoints */
809 if (endpoints != NULL) {
810 USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
811 (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
812 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
813 }
814 KASSERT(iface->ui_idesc != NULL);
815
816 req.bmRequestType = UT_WRITE_INTERFACE;
817 req.bRequest = UR_SET_INTERFACE;
818 USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
819 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
820 USETW(req.wLength, 0);
821 return usbd_do_request(iface->ui_dev, &req, 0);
822 }
823
824 int
825 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
826 {
827 char *p = (char *)cdesc;
828 char *end = p + UGETW(cdesc->wTotalLength);
829 usb_interface_descriptor_t *d;
830 int n;
831
832 for (n = 0; p < end; p += d->bLength) {
833 d = (usb_interface_descriptor_t *)p;
834 if (p + d->bLength <= end &&
835 d->bDescriptorType == UDESC_INTERFACE &&
836 d->bInterfaceNumber == ifaceno)
837 n++;
838 }
839 return n;
840 }
841
842 int
843 usbd_get_interface_altindex(struct usbd_interface *iface)
844 {
845 return iface->ui_altindex;
846 }
847
848 usbd_status
849 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
850 {
851 usb_device_request_t req;
852
853 req.bmRequestType = UT_READ_INTERFACE;
854 req.bRequest = UR_GET_INTERFACE;
855 USETW(req.wValue, 0);
856 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
857 USETW(req.wLength, 1);
858 return usbd_do_request(iface->ui_dev, &req, aiface);
859 }
860
861 /*** Internal routines ***/
862
863 /* Dequeue all pipe operations, called with bus lock held. */
864 Static usbd_status
865 usbd_ar_pipe(struct usbd_pipe *pipe)
866 {
867 struct usbd_xfer *xfer;
868
869 USBHIST_FUNC();
870 USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
871
872 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
873
874 #ifdef USB_DEBUG
875 if (usbdebug > 5)
876 usbd_dump_queue(pipe);
877 #endif
878 pipe->up_repeat = 0;
879 pipe->up_aborting = 1;
880 while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
881 USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
882 "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
883 (uintptr_t)pipe->up_methods, 0);
884 if (xfer->ux_status == USBD_NOT_STARTED) {
885 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
886 } else {
887 /* Make the HC abort it (and invoke the callback). */
888 pipe->up_methods->upm_abort(xfer);
889 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
890 }
891 }
892 pipe->up_aborting = 0;
893 return USBD_NORMAL_COMPLETION;
894 }
895
896 /* Called with USB lock held. */
897 void
898 usb_transfer_complete(struct usbd_xfer *xfer)
899 {
900 struct usbd_pipe *pipe = xfer->ux_pipe;
901 struct usbd_bus *bus = pipe->up_dev->ud_bus;
902 int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
903 int erred;
904 int polling = bus->ub_usepolling;
905 int repeat = pipe->up_repeat;
906
907 USBHIST_FUNC();
908 USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
909 "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
910 xfer->ux_actlen);
911
912 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
913 KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
914 xfer->ux_state);
915 KASSERT(pipe != NULL);
916
917 /*
918 * If device is known to miss out ack, then pretend that
919 * output timeout is a success. Userland should handle
920 * the logic to verify that the operation succeeded.
921 */
922 if (pipe->up_dev->ud_quirks &&
923 pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
924 xfer->ux_status == USBD_TIMEOUT &&
925 !usbd_xfer_isread(xfer)) {
926 USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
927 "hiding write timeout to %d.%s for %d bytes written",
928 (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
929 xfer->ux_length);
930
931 xfer->ux_status = USBD_NORMAL_COMPLETION;
932 xfer->ux_actlen = xfer->ux_length;
933 }
934
935 erred = xfer->ux_status == USBD_CANCELLED ||
936 xfer->ux_status == USBD_TIMEOUT;
937
938 if (!repeat) {
939 /* Remove request from queue. */
940
941 KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
942 "pipe %p is empty, but xfer %p wants to complete", pipe,
943 xfer);
944 KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
945 "xfer %p is not start of queue (%p is at start)", xfer,
946 SIMPLEQ_FIRST(&pipe->up_queue));
947
948 #ifdef DIAGNOSTIC
949 xfer->ux_state = XFER_BUSY;
950 #endif
951 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
952 }
953 USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
954 (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
955 0);
956
957 /* Count completed transfers. */
958 ++pipe->up_dev->ud_bus->ub_stats.uds_requests
959 [pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
960
961 xfer->ux_done = 1;
962 if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
963 !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
964 USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
965 xfer->ux_actlen, xfer->ux_length, 0, 0);
966 xfer->ux_status = USBD_SHORT_XFER;
967 }
968
969 USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
970 (uintptr_t)pipe->up_methods->upm_done, 0, 0);
971 pipe->up_methods->upm_done(xfer);
972
973 if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
974 KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
975 "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
976
977 /* Only if IN transfer */
978 if (usbd_xfer_isread(xfer)) {
979 memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
980 }
981 }
982
983 USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
984 (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
985
986 if (xfer->ux_callback) {
987 if (!polling) {
988 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
989 if (!(pipe->up_flags & USBD_MPSAFE))
990 KERNEL_LOCK(1, curlwp);
991 }
992
993 xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
994
995 if (!polling) {
996 if (!(pipe->up_flags & USBD_MPSAFE))
997 KERNEL_UNLOCK_ONE(curlwp);
998 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
999 }
1000 }
1001
1002 if (sync && !polling) {
1003 USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
1004 (uintptr_t)xfer, 0, 0, 0);
1005 cv_broadcast(&xfer->ux_cv);
1006 }
1007
1008 if (repeat) {
1009 xfer->ux_actlen = 0;
1010 xfer->ux_status = USBD_NOT_STARTED;
1011 } else {
1012 /* XXX should we stop the queue on all errors? */
1013 if (erred && pipe->up_iface != NULL) /* not control pipe */
1014 pipe->up_running = 0;
1015 }
1016 if (pipe->up_running && pipe->up_serialise)
1017 usbd_start_next(pipe);
1018 }
1019
1020 /* Called with USB lock held. */
1021 usbd_status
1022 usb_insert_transfer(struct usbd_xfer *xfer)
1023 {
1024 struct usbd_pipe *pipe = xfer->ux_pipe;
1025 usbd_status err;
1026
1027 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
1028 "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
1029 (uintptr_t)xfer, (uintptr_t)pipe,
1030 pipe->up_running, xfer->ux_timeout);
1031
1032 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1033 KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
1034 xfer->ux_state);
1035
1036 #ifdef DIAGNOSTIC
1037 xfer->ux_state = XFER_ONQU;
1038 #endif
1039 SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1040 if (pipe->up_running && pipe->up_serialise)
1041 err = USBD_IN_PROGRESS;
1042 else {
1043 pipe->up_running = 1;
1044 err = USBD_NORMAL_COMPLETION;
1045 }
1046 USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
1047 err, 0, 0);
1048 return err;
1049 }
1050
1051 /* Called with USB lock held. */
1052 void
1053 usbd_start_next(struct usbd_pipe *pipe)
1054 {
1055 struct usbd_xfer *xfer;
1056 usbd_status err;
1057
1058 USBHIST_FUNC();
1059
1060 KASSERT(pipe != NULL);
1061 KASSERT(pipe->up_methods != NULL);
1062 KASSERT(pipe->up_methods->upm_start != NULL);
1063 KASSERT(pipe->up_serialise == true);
1064
1065 int polling = pipe->up_dev->ud_bus->ub_usepolling;
1066 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1067
1068 /* Get next request in queue. */
1069 xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1070 USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
1071 (uintptr_t)xfer, 0, 0);
1072 if (xfer == NULL) {
1073 pipe->up_running = 0;
1074 } else {
1075 if (!polling)
1076 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1077 err = pipe->up_methods->upm_start(xfer);
1078 if (!polling)
1079 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1080
1081 if (err != USBD_IN_PROGRESS) {
1082 USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
1083 pipe->up_running = 0;
1084 /* XXX do what? */
1085 }
1086 }
1087
1088 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1089 }
1090
1091 usbd_status
1092 usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
1093 {
1094
1095 return usbd_do_request_flags(dev, req, data, 0, 0,
1096 USBD_DEFAULT_TIMEOUT);
1097 }
1098
1099 usbd_status
1100 usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
1101 void *data, uint16_t flags, int *actlen, uint32_t timeout)
1102 {
1103 size_t len = UGETW(req->wLength);
1104
1105 return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
1106 }
1107
1108 usbd_status
1109 usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
1110 size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
1111 {
1112 struct usbd_xfer *xfer;
1113 usbd_status err;
1114
1115 KASSERT(len >= UGETW(req->wLength));
1116
1117 USBHIST_FUNC();
1118 USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
1119 (uintptr_t)dev, (uintptr_t)req, flags, len);
1120
1121 ASSERT_SLEEPABLE();
1122
1123 int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1124 if (error)
1125 return error;
1126
1127 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1128 UGETW(req->wLength), flags, NULL);
1129 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1130 err = usbd_sync_transfer(xfer);
1131 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1132 if (xfer->ux_actlen > xfer->ux_length) {
1133 USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1134 dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1135 USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
1136 "index = %jd",
1137 xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1138 UGETW(xfer->ux_request.wIndex), 0);
1139 USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
1140 "actlen = %jd",
1141 UGETW(xfer->ux_request.wLength),
1142 xfer->ux_length, xfer->ux_actlen, 0);
1143 }
1144 #endif
1145 if (actlen != NULL)
1146 *actlen = xfer->ux_actlen;
1147
1148 usbd_destroy_xfer(xfer);
1149
1150 if (err) {
1151 USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1152 }
1153 return err;
1154 }
1155
1156 static void
1157 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1158 {
1159 usbd_free_xfer(xfer);
1160 }
1161
1162 /*
1163 * Execute a request without waiting for completion.
1164 * Can be used from interrupt context.
1165 */
1166 usbd_status
1167 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1168 usb_device_request_t *req, void *priv, usbd_callback callback)
1169 {
1170 usbd_status err;
1171
1172 if (callback == NULL)
1173 callback = usbd_request_async_cb;
1174
1175 usbd_setup_default_xfer(xfer, dev, priv,
1176 USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1177 callback);
1178 err = usbd_transfer(xfer);
1179 if (err != USBD_IN_PROGRESS) {
1180 usbd_free_xfer(xfer);
1181 return (err);
1182 }
1183 return (USBD_NORMAL_COMPLETION);
1184 }
1185
1186 const struct usbd_quirks *
1187 usbd_get_quirks(struct usbd_device *dev)
1188 {
1189 #ifdef DIAGNOSTIC
1190 if (dev == NULL) {
1191 printf("usbd_get_quirks: dev == NULL\n");
1192 return 0;
1193 }
1194 #endif
1195 return dev->ud_quirks;
1196 }
1197
1198 /* XXX do periodic free() of free list */
1199
1200 /*
1201 * Called from keyboard driver when in polling mode.
1202 */
1203 void
1204 usbd_dopoll(struct usbd_interface *iface)
1205 {
1206 iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1207 }
1208
1209 /*
1210 * This is for keyboard driver as well, which only operates in polling
1211 * mode from the ask root, etc., prompt and from DDB.
1212 */
1213 void
1214 usbd_set_polling(struct usbd_device *dev, int on)
1215 {
1216 if (on)
1217 dev->ud_bus->ub_usepolling++;
1218 else
1219 dev->ud_bus->ub_usepolling--;
1220
1221 /* Kick the host controller when switching modes */
1222 mutex_enter(dev->ud_bus->ub_lock);
1223 dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1224 mutex_exit(dev->ud_bus->ub_lock);
1225 }
1226
1227
1228 usb_endpoint_descriptor_t *
1229 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1230 {
1231 struct usbd_endpoint *ep;
1232 int i;
1233
1234 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1235 ep = &iface->ui_endpoints[i];
1236 if (ep->ue_edesc->bEndpointAddress == address)
1237 return iface->ui_endpoints[i].ue_edesc;
1238 }
1239 return NULL;
1240 }
1241
1242 /*
1243 * usbd_ratecheck() can limit the number of error messages that occurs.
1244 * When a device is unplugged it may take up to 0.25s for the hub driver
1245 * to notice it. If the driver continuously tries to do I/O operations
1246 * this can generate a large number of messages.
1247 */
1248 int
1249 usbd_ratecheck(struct timeval *last)
1250 {
1251 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1252
1253 return ratecheck(last, &errinterval);
1254 }
1255
1256 /*
1257 * Search for a vendor/product pair in an array. The item size is
1258 * given as an argument.
1259 */
1260 const struct usb_devno *
1261 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1262 uint16_t vendor, uint16_t product)
1263 {
1264 while (nentries-- > 0) {
1265 uint16_t tproduct = tbl->ud_product;
1266 if (tbl->ud_vendor == vendor &&
1267 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1268 return tbl;
1269 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1270 }
1271 return NULL;
1272 }
1273
1274
1275 void
1276 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
1277 {
1278 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1279
1280 iter->cur = (const uByte *)cd;
1281 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1282 }
1283
1284 const usb_descriptor_t *
1285 usb_desc_iter_peek(usbd_desc_iter_t *iter)
1286 {
1287 const usb_descriptor_t *desc;
1288
1289 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1290 if (iter->cur != iter->end)
1291 printf("usb_desc_iter_peek_next: bad descriptor\n");
1292 return NULL;
1293 }
1294 desc = (const usb_descriptor_t *)iter->cur;
1295 if (desc->bLength == 0) {
1296 printf("usb_desc_iter_peek_next: descriptor length = 0\n");
1297 return NULL;
1298 }
1299 if (iter->cur + desc->bLength > iter->end) {
1300 printf("usb_desc_iter_peek_next: descriptor length too large\n");
1301 return NULL;
1302 }
1303 return desc;
1304 }
1305
1306 const usb_descriptor_t *
1307 usb_desc_iter_next(usbd_desc_iter_t *iter)
1308 {
1309 const usb_descriptor_t *desc;
1310
1311 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1312 if (iter->cur != iter->end)
1313 printf("usb_desc_iter_next: bad descriptor\n");
1314 return NULL;
1315 }
1316 desc = (const usb_descriptor_t *)iter->cur;
1317 if (desc->bLength == 0) {
1318 printf("usb_desc_iter_next: descriptor length = 0\n");
1319 return NULL;
1320 }
1321 iter->cur += desc->bLength;
1322 if (iter->cur > iter->end) {
1323 printf("usb_desc_iter_next: descriptor length too large\n");
1324 return NULL;
1325 }
1326 return desc;
1327 }
1328
1329 /* Return the next interface descriptor, skipping over any other
1330 * descriptors. Returns NULL at the end or on error. */
1331 const usb_interface_descriptor_t *
1332 usb_desc_iter_next_interface(usbd_desc_iter_t *iter)
1333 {
1334 const usb_descriptor_t *desc;
1335
1336 while ((desc = usb_desc_iter_peek(iter)) != NULL &&
1337 desc->bDescriptorType != UDESC_INTERFACE)
1338 {
1339 usb_desc_iter_next(iter);
1340 }
1341
1342 return (const usb_interface_descriptor_t *)usb_desc_iter_next(iter);
1343 }
1344
1345 /* Returns the next non-interface descriptor, returning NULL when the
1346 * next descriptor would be an interface descriptor. */
1347 const usb_descriptor_t *
1348 usb_desc_iter_next_non_interface(usbd_desc_iter_t *iter)
1349 {
1350 const usb_descriptor_t *desc;
1351
1352 if ((desc = usb_desc_iter_peek(iter)) != NULL &&
1353 desc->bDescriptorType != UDESC_INTERFACE)
1354 {
1355 return usb_desc_iter_next(iter);
1356 } else {
1357 return NULL;
1358 }
1359 }
1360
1361 usbd_status
1362 usbd_get_string(struct usbd_device *dev, int si, char *buf)
1363 {
1364 return usbd_get_string0(dev, si, buf, 1);
1365 }
1366
1367 usbd_status
1368 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1369 {
1370 int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1371 usb_string_descriptor_t us;
1372 char *s;
1373 int i, n;
1374 uint16_t c;
1375 usbd_status err;
1376 int size;
1377
1378 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1379
1380 buf[0] = '\0';
1381 if (si == 0)
1382 return USBD_INVAL;
1383 if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1384 return USBD_STALLED;
1385 if (dev->ud_langid == USBD_NOLANG) {
1386 /* Set up default language */
1387 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1388 &size);
1389 if (err || size < 4) {
1390 USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1391 0, 0, 0, 0);
1392 dev->ud_langid = 0; /* Well, just pick something then */
1393 } else {
1394 /* Pick the first language as the default. */
1395 dev->ud_langid = UGETW(us.bString[0]);
1396 }
1397 }
1398 err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1399 if (err)
1400 return err;
1401 s = buf;
1402 n = size / 2 - 1;
1403 if (unicode) {
1404 for (i = 0; i < n; i++) {
1405 c = UGETW(us.bString[i]);
1406 if (swap)
1407 c = (c >> 8) | (c << 8);
1408 s += wput_utf8(s, 3, c);
1409 }
1410 *s++ = 0;
1411 }
1412 #ifdef COMPAT_30
1413 else {
1414 for (i = 0; i < n; i++) {
1415 c = UGETW(us.bString[i]);
1416 if (swap)
1417 c = (c >> 8) | (c << 8);
1418 *s++ = (c < 0x80) ? c : '?';
1419 }
1420 *s++ = 0;
1421 }
1422 #endif
1423 return USBD_NORMAL_COMPLETION;
1424 }
1425