usbdi.c revision 1.188 1 /* $NetBSD: usbdi.c,v 1.188 2020/02/08 07:53:23 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.188 2020/02/08 07:53:23 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 usbd_status err;
681
682 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
683
684 /*
685 * Clearing en endpoint stall resets the endpoint toggle, so
686 * do the same to the HC toggle.
687 */
688 pipe->up_methods->upm_cleartoggle(pipe);
689
690 err = usbd_clear_endpoint_feature(dev,
691 pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
692 #if 0
693 XXX should we do this?
694 if (!err) {
695 pipe->state = USBD_PIPE_ACTIVE;
696 /* XXX activate pipe */
697 }
698 #endif
699 return err;
700 }
701
702 void
703 usbd_clear_endpoint_stall_task(void *arg)
704 {
705 struct usbd_pipe *pipe = arg;
706 struct usbd_device *dev = pipe->up_dev;
707
708 pipe->up_methods->upm_cleartoggle(pipe);
709
710 (void)usbd_clear_endpoint_feature(dev,
711 pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
712 }
713
714 void
715 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
716 {
717 usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
718 }
719
720 void
721 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
722 {
723
724 pipe->up_methods->upm_cleartoggle(pipe);
725 }
726
727 usbd_status
728 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
729 {
730 KASSERT(iface != NULL);
731 KASSERT(iface->ui_idesc != NULL);
732
733 *count = iface->ui_idesc->bNumEndpoints;
734 return USBD_NORMAL_COMPLETION;
735 }
736
737 usbd_status
738 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
739 {
740
741 if (dev->ud_cdesc == NULL)
742 return USBD_NOT_CONFIGURED;
743 *count = dev->ud_cdesc->bNumInterface;
744 return USBD_NORMAL_COMPLETION;
745 }
746
747 void
748 usbd_interface2device_handle(struct usbd_interface *iface,
749 struct usbd_device **dev)
750 {
751
752 *dev = iface->ui_dev;
753 }
754
755 usbd_status
756 usbd_device2interface_handle(struct usbd_device *dev,
757 uint8_t ifaceno, struct usbd_interface **iface)
758 {
759
760 if (dev->ud_cdesc == NULL)
761 return USBD_NOT_CONFIGURED;
762 if (ifaceno >= dev->ud_cdesc->bNumInterface)
763 return USBD_INVAL;
764 *iface = &dev->ud_ifaces[ifaceno];
765 return USBD_NORMAL_COMPLETION;
766 }
767
768 struct usbd_device *
769 usbd_pipe2device_handle(struct usbd_pipe *pipe)
770 {
771 KASSERT(pipe != NULL);
772
773 return pipe->up_dev;
774 }
775
776 /* XXXX use altno */
777 usbd_status
778 usbd_set_interface(struct usbd_interface *iface, int altidx)
779 {
780 usb_device_request_t req;
781 usbd_status err;
782 void *endpoints;
783
784 USBHIST_FUNC();
785
786 if (LIST_FIRST(&iface->ui_pipes) != NULL)
787 return USBD_IN_USE;
788
789 endpoints = iface->ui_endpoints;
790 int nendpt = iface->ui_idesc->bNumEndpoints;
791 USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
792 (uintptr_t)iface, (uintptr_t)endpoints,
793 iface->ui_idesc->bNumEndpoints, 0);
794 err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
795 if (err)
796 return err;
797
798 /* new setting works, we can free old endpoints */
799 if (endpoints != NULL) {
800 USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
801 (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
802 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
803 }
804 KASSERT(iface->ui_idesc != NULL);
805
806 req.bmRequestType = UT_WRITE_INTERFACE;
807 req.bRequest = UR_SET_INTERFACE;
808 USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
809 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
810 USETW(req.wLength, 0);
811 return usbd_do_request(iface->ui_dev, &req, 0);
812 }
813
814 int
815 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
816 {
817 char *p = (char *)cdesc;
818 char *end = p + UGETW(cdesc->wTotalLength);
819 usb_interface_descriptor_t *d;
820 int n;
821
822 for (n = 0; p < end; p += d->bLength) {
823 d = (usb_interface_descriptor_t *)p;
824 if (p + d->bLength <= end &&
825 d->bDescriptorType == UDESC_INTERFACE &&
826 d->bInterfaceNumber == ifaceno)
827 n++;
828 }
829 return n;
830 }
831
832 int
833 usbd_get_interface_altindex(struct usbd_interface *iface)
834 {
835 return iface->ui_altindex;
836 }
837
838 usbd_status
839 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
840 {
841 usb_device_request_t req;
842
843 req.bmRequestType = UT_READ_INTERFACE;
844 req.bRequest = UR_GET_INTERFACE;
845 USETW(req.wValue, 0);
846 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
847 USETW(req.wLength, 1);
848 return usbd_do_request(iface->ui_dev, &req, aiface);
849 }
850
851 /*** Internal routines ***/
852
853 /* Dequeue all pipe operations, called with bus lock held. */
854 Static usbd_status
855 usbd_ar_pipe(struct usbd_pipe *pipe)
856 {
857 struct usbd_xfer *xfer;
858
859 USBHIST_FUNC();
860 USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
861
862 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
863
864 #ifdef USB_DEBUG
865 if (usbdebug > 5)
866 usbd_dump_queue(pipe);
867 #endif
868 pipe->up_repeat = 0;
869 pipe->up_aborting = 1;
870 while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
871 USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
872 "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
873 (uintptr_t)pipe->up_methods, 0);
874 if (xfer->ux_status == USBD_NOT_STARTED) {
875 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
876 } else {
877 /* Make the HC abort it (and invoke the callback). */
878 pipe->up_methods->upm_abort(xfer);
879 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
880 }
881 }
882 pipe->up_aborting = 0;
883 return USBD_NORMAL_COMPLETION;
884 }
885
886 /* Called with USB lock held. */
887 void
888 usb_transfer_complete(struct usbd_xfer *xfer)
889 {
890 struct usbd_pipe *pipe = xfer->ux_pipe;
891 struct usbd_bus *bus = pipe->up_dev->ud_bus;
892 int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
893 int erred;
894 int polling = bus->ub_usepolling;
895 int repeat = pipe->up_repeat;
896
897 USBHIST_FUNC();
898 USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
899 "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
900 xfer->ux_actlen);
901
902 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
903 KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
904 xfer->ux_state);
905 KASSERT(pipe != NULL);
906
907 /*
908 * If device is known to miss out ack, then pretend that
909 * output timeout is a success. Userland should handle
910 * the logic to verify that the operation succeeded.
911 */
912 if (pipe->up_dev->ud_quirks &&
913 pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
914 xfer->ux_status == USBD_TIMEOUT &&
915 !usbd_xfer_isread(xfer)) {
916 USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
917 "hiding write timeout to %d.%s for %d bytes written",
918 (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
919 xfer->ux_length);
920
921 xfer->ux_status = USBD_NORMAL_COMPLETION;
922 xfer->ux_actlen = xfer->ux_length;
923 }
924
925 erred = xfer->ux_status == USBD_CANCELLED ||
926 xfer->ux_status == USBD_TIMEOUT;
927
928 if (!repeat) {
929 /* Remove request from queue. */
930
931 KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
932 "pipe %p is empty, but xfer %p wants to complete", pipe,
933 xfer);
934 KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
935 "xfer %p is not start of queue (%p is at start)", xfer,
936 SIMPLEQ_FIRST(&pipe->up_queue));
937
938 #ifdef DIAGNOSTIC
939 xfer->ux_state = XFER_BUSY;
940 #endif
941 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
942 }
943 USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
944 (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
945 0);
946
947 /* Count completed transfers. */
948 ++pipe->up_dev->ud_bus->ub_stats.uds_requests
949 [pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
950
951 xfer->ux_done = 1;
952 if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
953 !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
954 USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
955 xfer->ux_actlen, xfer->ux_length, 0, 0);
956 xfer->ux_status = USBD_SHORT_XFER;
957 }
958
959 USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
960 (uintptr_t)pipe->up_methods->upm_done, 0, 0);
961 pipe->up_methods->upm_done(xfer);
962
963 if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
964 KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
965 "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
966
967 /* Only if IN transfer */
968 if (usbd_xfer_isread(xfer)) {
969 memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
970 }
971 }
972
973 USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
974 (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
975
976 if (xfer->ux_callback) {
977 if (!polling) {
978 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
979 if (!(pipe->up_flags & USBD_MPSAFE))
980 KERNEL_LOCK(1, curlwp);
981 }
982
983 xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
984
985 if (!polling) {
986 if (!(pipe->up_flags & USBD_MPSAFE))
987 KERNEL_UNLOCK_ONE(curlwp);
988 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
989 }
990 }
991
992 if (sync && !polling) {
993 USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
994 (uintptr_t)xfer, 0, 0, 0);
995 cv_broadcast(&xfer->ux_cv);
996 }
997
998 if (repeat) {
999 xfer->ux_actlen = 0;
1000 xfer->ux_status = USBD_NOT_STARTED;
1001 } else {
1002 /* XXX should we stop the queue on all errors? */
1003 if (erred && pipe->up_iface != NULL) /* not control pipe */
1004 pipe->up_running = 0;
1005 }
1006 if (pipe->up_running && pipe->up_serialise)
1007 usbd_start_next(pipe);
1008 }
1009
1010 /* Called with USB lock held. */
1011 usbd_status
1012 usb_insert_transfer(struct usbd_xfer *xfer)
1013 {
1014 struct usbd_pipe *pipe = xfer->ux_pipe;
1015 usbd_status err;
1016
1017 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
1018 "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
1019 (uintptr_t)xfer, (uintptr_t)pipe,
1020 pipe->up_running, xfer->ux_timeout);
1021
1022 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1023 KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
1024 xfer->ux_state);
1025
1026 #ifdef DIAGNOSTIC
1027 xfer->ux_state = XFER_ONQU;
1028 #endif
1029 SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1030 if (pipe->up_running && pipe->up_serialise)
1031 err = USBD_IN_PROGRESS;
1032 else {
1033 pipe->up_running = 1;
1034 err = USBD_NORMAL_COMPLETION;
1035 }
1036 USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
1037 err, 0, 0);
1038 return err;
1039 }
1040
1041 /* Called with USB lock held. */
1042 void
1043 usbd_start_next(struct usbd_pipe *pipe)
1044 {
1045 struct usbd_xfer *xfer;
1046 usbd_status err;
1047
1048 USBHIST_FUNC();
1049
1050 KASSERT(pipe != NULL);
1051 KASSERT(pipe->up_methods != NULL);
1052 KASSERT(pipe->up_methods->upm_start != NULL);
1053 KASSERT(pipe->up_serialise == true);
1054
1055 int polling = pipe->up_dev->ud_bus->ub_usepolling;
1056 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1057
1058 /* Get next request in queue. */
1059 xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1060 USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
1061 (uintptr_t)xfer, 0, 0);
1062 if (xfer == NULL) {
1063 pipe->up_running = 0;
1064 } else {
1065 if (!polling)
1066 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1067 err = pipe->up_methods->upm_start(xfer);
1068 if (!polling)
1069 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1070
1071 if (err != USBD_IN_PROGRESS) {
1072 USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
1073 pipe->up_running = 0;
1074 /* XXX do what? */
1075 }
1076 }
1077
1078 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1079 }
1080
1081 usbd_status
1082 usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
1083 {
1084
1085 return usbd_do_request_flags(dev, req, data, 0, 0,
1086 USBD_DEFAULT_TIMEOUT);
1087 }
1088
1089 usbd_status
1090 usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
1091 void *data, uint16_t flags, int *actlen, uint32_t timeout)
1092 {
1093 size_t len = UGETW(req->wLength);
1094
1095 return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
1096 }
1097
1098 usbd_status
1099 usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
1100 size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
1101 {
1102 struct usbd_xfer *xfer;
1103 usbd_status err;
1104
1105 KASSERT(len >= UGETW(req->wLength));
1106
1107 USBHIST_FUNC();
1108 USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
1109 (uintptr_t)dev, (uintptr_t)req, flags, len);
1110
1111 ASSERT_SLEEPABLE();
1112
1113 int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1114 if (error)
1115 return error;
1116
1117 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1118 UGETW(req->wLength), flags, NULL);
1119 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1120 err = usbd_sync_transfer(xfer);
1121 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1122 if (xfer->ux_actlen > xfer->ux_length) {
1123 USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1124 dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1125 USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
1126 "index = %jd",
1127 xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1128 UGETW(xfer->ux_request.wIndex), 0);
1129 USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
1130 "actlen = %jd",
1131 UGETW(xfer->ux_request.wLength),
1132 xfer->ux_length, xfer->ux_actlen, 0);
1133 }
1134 #endif
1135 if (actlen != NULL)
1136 *actlen = xfer->ux_actlen;
1137
1138 usbd_destroy_xfer(xfer);
1139
1140 if (err) {
1141 USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1142 }
1143 return err;
1144 }
1145
1146 static void
1147 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1148 {
1149 usbd_free_xfer(xfer);
1150 }
1151
1152 /*
1153 * Execute a request without waiting for completion.
1154 * Can be used from interrupt context.
1155 */
1156 usbd_status
1157 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1158 usb_device_request_t *req, void *priv, usbd_callback callback)
1159 {
1160 usbd_status err;
1161
1162 if (callback == NULL)
1163 callback = usbd_request_async_cb;
1164
1165 usbd_setup_default_xfer(xfer, dev, priv,
1166 USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1167 callback);
1168 err = usbd_transfer(xfer);
1169 if (err != USBD_IN_PROGRESS) {
1170 usbd_free_xfer(xfer);
1171 return (err);
1172 }
1173 return (USBD_NORMAL_COMPLETION);
1174 }
1175
1176 const struct usbd_quirks *
1177 usbd_get_quirks(struct usbd_device *dev)
1178 {
1179 #ifdef DIAGNOSTIC
1180 if (dev == NULL) {
1181 printf("usbd_get_quirks: dev == NULL\n");
1182 return 0;
1183 }
1184 #endif
1185 return dev->ud_quirks;
1186 }
1187
1188 /* XXX do periodic free() of free list */
1189
1190 /*
1191 * Called from keyboard driver when in polling mode.
1192 */
1193 void
1194 usbd_dopoll(struct usbd_interface *iface)
1195 {
1196 iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1197 }
1198
1199 /*
1200 * This is for keyboard driver as well, which only operates in polling
1201 * mode from the ask root, etc., prompt and from DDB.
1202 */
1203 void
1204 usbd_set_polling(struct usbd_device *dev, int on)
1205 {
1206 if (on)
1207 dev->ud_bus->ub_usepolling++;
1208 else
1209 dev->ud_bus->ub_usepolling--;
1210
1211 /* Kick the host controller when switching modes */
1212 mutex_enter(dev->ud_bus->ub_lock);
1213 dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1214 mutex_exit(dev->ud_bus->ub_lock);
1215 }
1216
1217
1218 usb_endpoint_descriptor_t *
1219 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1220 {
1221 struct usbd_endpoint *ep;
1222 int i;
1223
1224 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1225 ep = &iface->ui_endpoints[i];
1226 if (ep->ue_edesc->bEndpointAddress == address)
1227 return iface->ui_endpoints[i].ue_edesc;
1228 }
1229 return NULL;
1230 }
1231
1232 /*
1233 * usbd_ratecheck() can limit the number of error messages that occurs.
1234 * When a device is unplugged it may take up to 0.25s for the hub driver
1235 * to notice it. If the driver continuously tries to do I/O operations
1236 * this can generate a large number of messages.
1237 */
1238 int
1239 usbd_ratecheck(struct timeval *last)
1240 {
1241 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1242
1243 return ratecheck(last, &errinterval);
1244 }
1245
1246 /*
1247 * Search for a vendor/product pair in an array. The item size is
1248 * given as an argument.
1249 */
1250 const struct usb_devno *
1251 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1252 uint16_t vendor, uint16_t product)
1253 {
1254 while (nentries-- > 0) {
1255 uint16_t tproduct = tbl->ud_product;
1256 if (tbl->ud_vendor == vendor &&
1257 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1258 return tbl;
1259 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1260 }
1261 return NULL;
1262 }
1263
1264
1265 void
1266 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
1267 {
1268 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1269
1270 iter->cur = (const uByte *)cd;
1271 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1272 }
1273
1274 const usb_descriptor_t *
1275 usb_desc_iter_peek(usbd_desc_iter_t *iter)
1276 {
1277 const usb_descriptor_t *desc;
1278
1279 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1280 if (iter->cur != iter->end)
1281 printf("usb_desc_iter_peek_next: bad descriptor\n");
1282 return NULL;
1283 }
1284 desc = (const usb_descriptor_t *)iter->cur;
1285 if (desc->bLength == 0) {
1286 printf("usb_desc_iter_peek_next: descriptor length = 0\n");
1287 return NULL;
1288 }
1289 if (iter->cur + desc->bLength > iter->end) {
1290 printf("usb_desc_iter_peek_next: descriptor length too large\n");
1291 return NULL;
1292 }
1293 return desc;
1294 }
1295
1296 const usb_descriptor_t *
1297 usb_desc_iter_next(usbd_desc_iter_t *iter)
1298 {
1299 const usb_descriptor_t *desc;
1300
1301 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1302 if (iter->cur != iter->end)
1303 printf("usb_desc_iter_next: bad descriptor\n");
1304 return NULL;
1305 }
1306 desc = (const usb_descriptor_t *)iter->cur;
1307 if (desc->bLength == 0) {
1308 printf("usb_desc_iter_next: descriptor length = 0\n");
1309 return NULL;
1310 }
1311 iter->cur += desc->bLength;
1312 if (iter->cur > iter->end) {
1313 printf("usb_desc_iter_next: descriptor length too large\n");
1314 return NULL;
1315 }
1316 return desc;
1317 }
1318
1319 /* Return the next interface descriptor, skipping over any other
1320 * descriptors. Returns NULL at the end or on error. */
1321 const usb_interface_descriptor_t *
1322 usb_desc_iter_next_interface(usbd_desc_iter_t *iter)
1323 {
1324 const usb_descriptor_t *desc;
1325
1326 while ((desc = usb_desc_iter_peek(iter)) != NULL &&
1327 desc->bDescriptorType != UDESC_INTERFACE)
1328 {
1329 usb_desc_iter_next(iter);
1330 }
1331
1332 return (const usb_interface_descriptor_t *)usb_desc_iter_next(iter);
1333 }
1334
1335 /* Returns the next non-interface descriptor, returning NULL when the
1336 * next descriptor would be an interface descriptor. */
1337 const usb_descriptor_t *
1338 usb_desc_iter_next_non_interface(usbd_desc_iter_t *iter)
1339 {
1340 const usb_descriptor_t *desc;
1341
1342 if ((desc = usb_desc_iter_peek(iter)) != NULL &&
1343 desc->bDescriptorType != UDESC_INTERFACE)
1344 {
1345 return usb_desc_iter_next(iter);
1346 } else {
1347 return NULL;
1348 }
1349 }
1350
1351 usbd_status
1352 usbd_get_string(struct usbd_device *dev, int si, char *buf)
1353 {
1354 return usbd_get_string0(dev, si, buf, 1);
1355 }
1356
1357 usbd_status
1358 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1359 {
1360 int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1361 usb_string_descriptor_t us;
1362 char *s;
1363 int i, n;
1364 uint16_t c;
1365 usbd_status err;
1366 int size;
1367
1368 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1369
1370 buf[0] = '\0';
1371 if (si == 0)
1372 return USBD_INVAL;
1373 if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1374 return USBD_STALLED;
1375 if (dev->ud_langid == USBD_NOLANG) {
1376 /* Set up default language */
1377 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1378 &size);
1379 if (err || size < 4) {
1380 USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1381 0, 0, 0, 0);
1382 dev->ud_langid = 0; /* Well, just pick something then */
1383 } else {
1384 /* Pick the first language as the default. */
1385 dev->ud_langid = UGETW(us.bString[0]);
1386 }
1387 }
1388 err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1389 if (err)
1390 return err;
1391 s = buf;
1392 n = size / 2 - 1;
1393 if (unicode) {
1394 for (i = 0; i < n; i++) {
1395 c = UGETW(us.bString[i]);
1396 if (swap)
1397 c = (c >> 8) | (c << 8);
1398 s += wput_utf8(s, 3, c);
1399 }
1400 *s++ = 0;
1401 }
1402 #ifdef COMPAT_30
1403 else {
1404 for (i = 0; i < n; i++) {
1405 c = UGETW(us.bString[i]);
1406 if (swap)
1407 c = (c >> 8) | (c << 8);
1408 *s++ = (c < 0x80) ? c : '?';
1409 }
1410 *s++ = 0;
1411 }
1412 #endif
1413 return USBD_NORMAL_COMPLETION;
1414 }
1415