usbdi.c revision 1.179 1 /* $NetBSD: usbdi.c,v 1.179 2018/11/15 02:35:23 manu 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.179 2018/11/15 02:35:23 manu 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(); USBHIST_CALLED(usbdebug);
80
81 USBHIST_LOG(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
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(); USBHIST_CALLED(usbdebug);
95
96 USBHIST_LOG(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
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(); USBHIST_CALLED(usbdebug);
112
113 USBHIST_LOG(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
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(); USBHIST_CALLED(usbdebug);
129
130 USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
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(); USBHIST_CALLED(usbdebug);
141
142 USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
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(); USBHIST_CALLED(usbdebug);
175
176 USBHIST_LOG(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
177 (uintptr_t)iface, address, flags, 0);
178
179 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
180 ep = &iface->ui_endpoints[i];
181 if (ep->ue_edesc == NULL)
182 return USBD_IOERROR;
183 if (ep->ue_edesc->bEndpointAddress == address)
184 goto found;
185 }
186 return USBD_BAD_ADDRESS;
187 found:
188 if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
189 return USBD_IN_USE;
190 err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
191 if (err)
192 return err;
193 LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
194 *pipe = p;
195 return USBD_NORMAL_COMPLETION;
196 }
197
198 usbd_status
199 usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
200 uint8_t flags, struct usbd_pipe **pipe,
201 void *priv, void *buffer, uint32_t len,
202 usbd_callback cb, int ival)
203 {
204 usbd_status err;
205 struct usbd_xfer *xfer;
206 struct usbd_pipe *ipipe;
207
208 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
209
210 USBHIST_LOG(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
211 address, flags, len, 0);
212
213 err = usbd_open_pipe_ival(iface, address,
214 USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
215 &ipipe, ival);
216 if (err)
217 return err;
218 err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
219 if (err)
220 goto bad1;
221
222 usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
223 ipipe->up_intrxfer = xfer;
224 ipipe->up_repeat = 1;
225 err = usbd_transfer(xfer);
226 *pipe = ipipe;
227 if (err != USBD_IN_PROGRESS)
228 goto bad3;
229 return USBD_NORMAL_COMPLETION;
230
231 bad3:
232 ipipe->up_intrxfer = NULL;
233 ipipe->up_repeat = 0;
234
235 usbd_destroy_xfer(xfer);
236 bad1:
237 usbd_close_pipe(ipipe);
238 return err;
239 }
240
241 usbd_status
242 usbd_close_pipe(struct usbd_pipe *pipe)
243 {
244 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
245
246 KASSERT(pipe != NULL);
247
248 usbd_lock_pipe(pipe);
249
250 if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
251 printf("WARNING: pipe closed with active xfers on addr %d\n",
252 pipe->up_dev->ud_addr);
253 usbd_ar_pipe(pipe);
254 }
255
256 KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
257
258 LIST_REMOVE(pipe, up_next);
259 pipe->up_endpoint->ue_refcnt--;
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 pipe->up_methods->upm_close(pipe);
268 usbd_unlock_pipe(pipe);
269 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
270
271 return USBD_NORMAL_COMPLETION;
272 }
273
274 usbd_status
275 usbd_transfer(struct usbd_xfer *xfer)
276 {
277 struct usbd_pipe *pipe = xfer->ux_pipe;
278 usbd_status err;
279 unsigned int size, flags;
280
281 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
282
283 USBHIST_LOG(usbdebug,
284 "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
285 (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
286 KASSERT(xfer->ux_status == USBD_NOT_STARTED);
287
288 #ifdef USB_DEBUG
289 if (usbdebug > 5)
290 usbd_dump_queue(pipe);
291 #endif
292 xfer->ux_done = 0;
293
294 if (pipe->up_aborting) {
295 USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
296 (uintptr_t)xfer, 0, 0, 0);
297 return USBD_CANCELLED;
298 }
299
300 KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
301
302 size = xfer->ux_length;
303 flags = xfer->ux_flags;
304
305 if (size != 0) {
306 /*
307 * Use the xfer buffer if none specified in transfer setup.
308 * isoc transfers always use the xfer buffer, i.e.
309 * ux_buffer is always NULL for isoc.
310 */
311 if (xfer->ux_buffer == NULL) {
312 xfer->ux_buffer = xfer->ux_buf;
313 }
314
315 /*
316 * If not using the xfer buffer copy data to the
317 * xfer buffer for OUT transfers of >0 length
318 */
319 if (xfer->ux_buffer != xfer->ux_buf) {
320 KASSERT(xfer->ux_buf);
321 if (!usbd_xfer_isread(xfer)) {
322 memcpy(xfer->ux_buf, xfer->ux_buffer, size);
323 }
324 }
325 }
326
327 /* xfer is not valid after the transfer method unless synchronous */
328 err = pipe->up_methods->upm_transfer(xfer);
329 USBHIST_LOG(usbdebug, "<- done transfer %#jx, err = %jd",
330 (uintptr_t)xfer, err, 0, 0);
331
332 if (err != USBD_IN_PROGRESS && err) {
333 /*
334 * The transfer made it onto the pipe queue, but didn't get
335 * accepted by the HCD for some reason. It needs removing
336 * from the pipe queue.
337 */
338 usbd_lock_pipe(pipe);
339 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
340 if (pipe->up_serialise)
341 usbd_start_next(pipe);
342 usbd_unlock_pipe(pipe);
343 }
344
345 if (!(flags & USBD_SYNCHRONOUS)) {
346 USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
347 (uintptr_t)xfer, err, 0, 0);
348 return err;
349 }
350
351 if (err != USBD_IN_PROGRESS) {
352 USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)"
353 "(complete/error)", (uintptr_t)xfer, err, 0, 0);
354 return err;
355 }
356
357 /* Sync transfer, wait for completion. */
358 usbd_lock_pipe(pipe);
359 while (!xfer->ux_done) {
360 if (pipe->up_dev->ud_bus->ub_usepolling)
361 panic("usbd_transfer: not done");
362 USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
363 (uintptr_t)xfer, 0, 0, 0);
364
365 err = 0;
366 if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
367 err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
368 } else {
369 cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
370 }
371 if (err) {
372 if (!xfer->ux_done)
373 pipe->up_methods->upm_abort(xfer);
374 break;
375 }
376 }
377 usbd_unlock_pipe(pipe);
378 return xfer->ux_status;
379 }
380
381 /* Like usbd_transfer(), but waits for completion. */
382 usbd_status
383 usbd_sync_transfer(struct usbd_xfer *xfer)
384 {
385 xfer->ux_flags |= USBD_SYNCHRONOUS;
386 return usbd_transfer(xfer);
387 }
388
389 /* Like usbd_transfer(), but waits for completion and listens for signals. */
390 usbd_status
391 usbd_sync_transfer_sig(struct usbd_xfer *xfer)
392 {
393 xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
394 return usbd_transfer(xfer);
395 }
396
397 static void *
398 usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
399 {
400 KASSERT(xfer->ux_buf == NULL);
401 KASSERT(size != 0);
402
403 xfer->ux_bufsize = 0;
404 #if NUSB_DMA > 0
405 struct usbd_bus *bus = xfer->ux_bus;
406
407 if (bus->ub_usedma) {
408 usb_dma_t *dmap = &xfer->ux_dmabuf;
409
410 int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
411 if (err) {
412 return NULL;
413 }
414 xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
415 xfer->ux_bufsize = size;
416
417 return xfer->ux_buf;
418 }
419 #endif
420 KASSERT(xfer->ux_bus->ub_usedma == false);
421 xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
422 xfer->ux_bufsize = size;
423 return xfer->ux_buf;
424 }
425
426 static void
427 usbd_free_buffer(struct usbd_xfer *xfer)
428 {
429 KASSERT(xfer->ux_buf != NULL);
430 KASSERT(xfer->ux_bufsize != 0);
431
432 void *buf = xfer->ux_buf;
433 uint32_t size = xfer->ux_bufsize;
434
435 xfer->ux_buf = NULL;
436 xfer->ux_bufsize = 0;
437
438 #if NUSB_DMA > 0
439 struct usbd_bus *bus = xfer->ux_bus;
440
441 if (bus->ub_usedma) {
442 usb_dma_t *dmap = &xfer->ux_dmabuf;
443
444 usb_freemem(bus, dmap);
445 return;
446 }
447 #endif
448 KASSERT(xfer->ux_bus->ub_usedma == false);
449
450 kmem_free(buf, size);
451 }
452
453 void *
454 usbd_get_buffer(struct usbd_xfer *xfer)
455 {
456 return xfer->ux_buf;
457 }
458
459 struct usbd_pipe *
460 usbd_get_pipe0(struct usbd_device *dev)
461 {
462
463 return dev->ud_pipe0;
464 }
465
466 static struct usbd_xfer *
467 usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
468 {
469 struct usbd_xfer *xfer;
470
471 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
472
473 ASSERT_SLEEPABLE();
474
475 xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
476 if (xfer == NULL)
477 return NULL;
478 xfer->ux_bus = dev->ud_bus;
479 callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
480 cv_init(&xfer->ux_cv, "usbxfer");
481
482 USBHIST_LOG(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
483
484 return xfer;
485 }
486
487 static usbd_status
488 usbd_free_xfer(struct usbd_xfer *xfer)
489 {
490 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
491
492 USBHIST_LOG(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
493 if (xfer->ux_buf) {
494 usbd_free_buffer(xfer);
495 }
496 #if defined(DIAGNOSTIC)
497 if (callout_pending(&xfer->ux_callout)) {
498 callout_stop(&xfer->ux_callout);
499 printf("usbd_free_xfer: timeout_handle pending\n");
500 }
501 #endif
502 cv_destroy(&xfer->ux_cv);
503 xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
504 return USBD_NORMAL_COMPLETION;
505 }
506
507 int
508 usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
509 unsigned int nframes, struct usbd_xfer **xp)
510 {
511 KASSERT(xp != NULL);
512 void *buf = NULL;
513
514 struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
515 if (xfer == NULL)
516 return ENOMEM;
517
518 if (len) {
519 buf = usbd_alloc_buffer(xfer, len);
520 if (!buf) {
521 usbd_free_xfer(xfer);
522 return ENOMEM;
523 }
524 }
525 xfer->ux_pipe = pipe;
526 xfer->ux_flags = flags;
527 xfer->ux_nframes = nframes;
528 xfer->ux_methods = pipe->up_methods;
529
530 if (xfer->ux_methods->upm_init) {
531 int err = xfer->ux_methods->upm_init(xfer);
532 if (err) {
533 if (buf)
534 usbd_free_buffer(xfer);
535 usbd_free_xfer(xfer);
536 return err;
537 }
538 }
539
540 *xp = xfer;
541 return 0;
542 }
543
544 void
545 usbd_destroy_xfer(struct usbd_xfer *xfer)
546 {
547
548 if (xfer->ux_methods->upm_fini) {
549 xfer->ux_methods->upm_fini(xfer);
550 }
551
552 usbd_free_xfer(xfer);
553 }
554
555 void
556 usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
557 uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
558 {
559 KASSERT(xfer->ux_pipe);
560
561 xfer->ux_priv = priv;
562 xfer->ux_buffer = buffer;
563 xfer->ux_length = length;
564 xfer->ux_actlen = 0;
565 xfer->ux_flags = flags;
566 xfer->ux_timeout = timeout;
567 xfer->ux_status = USBD_NOT_STARTED;
568 xfer->ux_callback = callback;
569 xfer->ux_rqflags &= ~URQ_REQUEST;
570 xfer->ux_nframes = 0;
571 }
572
573 void
574 usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
575 void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
576 uint32_t length, uint16_t flags, usbd_callback callback)
577 {
578 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
579
580 xfer->ux_priv = priv;
581 xfer->ux_buffer = buffer;
582 xfer->ux_length = length;
583 xfer->ux_actlen = 0;
584 xfer->ux_flags = flags;
585 xfer->ux_timeout = timeout;
586 xfer->ux_status = USBD_NOT_STARTED;
587 xfer->ux_callback = callback;
588 xfer->ux_request = *req;
589 xfer->ux_rqflags |= URQ_REQUEST;
590 xfer->ux_nframes = 0;
591 }
592
593 void
594 usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
595 uint32_t nframes, uint16_t flags, usbd_callback callback)
596 {
597 xfer->ux_priv = priv;
598 xfer->ux_buffer = NULL;
599 xfer->ux_length = 0;
600 xfer->ux_actlen = 0;
601 xfer->ux_flags = flags;
602 xfer->ux_timeout = USBD_NO_TIMEOUT;
603 xfer->ux_status = USBD_NOT_STARTED;
604 xfer->ux_callback = callback;
605 xfer->ux_rqflags &= ~URQ_REQUEST;
606 xfer->ux_frlengths = frlengths;
607 xfer->ux_nframes = nframes;
608 }
609
610 void
611 usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
612 void **buffer, uint32_t *count, usbd_status *status)
613 {
614 if (priv != NULL)
615 *priv = xfer->ux_priv;
616 if (buffer != NULL)
617 *buffer = xfer->ux_buffer;
618 if (count != NULL)
619 *count = xfer->ux_actlen;
620 if (status != NULL)
621 *status = xfer->ux_status;
622 }
623
624 usb_config_descriptor_t *
625 usbd_get_config_descriptor(struct usbd_device *dev)
626 {
627 KASSERT(dev != NULL);
628
629 return dev->ud_cdesc;
630 }
631
632 usb_interface_descriptor_t *
633 usbd_get_interface_descriptor(struct usbd_interface *iface)
634 {
635 KASSERT(iface != NULL);
636
637 return iface->ui_idesc;
638 }
639
640 usb_device_descriptor_t *
641 usbd_get_device_descriptor(struct usbd_device *dev)
642 {
643 KASSERT(dev != NULL);
644
645 return &dev->ud_ddesc;
646 }
647
648 usb_endpoint_descriptor_t *
649 usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
650 {
651
652 if (index >= iface->ui_idesc->bNumEndpoints)
653 return NULL;
654 return iface->ui_endpoints[index].ue_edesc;
655 }
656
657 /* Some drivers may wish to abort requests on the default pipe, *
658 * but there is no mechanism for getting a handle on it. */
659 usbd_status
660 usbd_abort_default_pipe(struct usbd_device *device)
661 {
662 return usbd_abort_pipe(device->ud_pipe0);
663 }
664
665 usbd_status
666 usbd_abort_pipe(struct usbd_pipe *pipe)
667 {
668 usbd_status err;
669
670 KASSERT(pipe != NULL);
671
672 usbd_lock_pipe(pipe);
673 err = usbd_ar_pipe(pipe);
674 usbd_unlock_pipe(pipe);
675 return err;
676 }
677
678 usbd_status
679 usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
680 {
681 struct usbd_device *dev = pipe->up_dev;
682 usb_device_request_t req;
683 usbd_status err;
684
685 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
686
687 /*
688 * Clearing en endpoint stall resets the endpoint toggle, so
689 * do the same to the HC toggle.
690 */
691 pipe->up_methods->upm_cleartoggle(pipe);
692
693 req.bmRequestType = UT_WRITE_ENDPOINT;
694 req.bRequest = UR_CLEAR_FEATURE;
695 USETW(req.wValue, UF_ENDPOINT_HALT);
696 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
697 USETW(req.wLength, 0);
698 err = usbd_do_request(dev, &req, 0);
699 #if 0
700 XXX should we do this?
701 if (!err) {
702 pipe->state = USBD_PIPE_ACTIVE;
703 /* XXX activate pipe */
704 }
705 #endif
706 return err;
707 }
708
709 void
710 usbd_clear_endpoint_stall_task(void *arg)
711 {
712 struct usbd_pipe *pipe = arg;
713 struct usbd_device *dev = pipe->up_dev;
714 usb_device_request_t req;
715
716 pipe->up_methods->upm_cleartoggle(pipe);
717
718 req.bmRequestType = UT_WRITE_ENDPOINT;
719 req.bRequest = UR_CLEAR_FEATURE;
720 USETW(req.wValue, UF_ENDPOINT_HALT);
721 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
722 USETW(req.wLength, 0);
723 (void)usbd_do_request(dev, &req, 0);
724 }
725
726 void
727 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
728 {
729 usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
730 }
731
732 void
733 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
734 {
735
736 pipe->up_methods->upm_cleartoggle(pipe);
737 }
738
739 usbd_status
740 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
741 {
742 KASSERT(iface != NULL);
743 KASSERT(iface->ui_idesc != NULL);
744
745 *count = iface->ui_idesc->bNumEndpoints;
746 return USBD_NORMAL_COMPLETION;
747 }
748
749 usbd_status
750 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
751 {
752
753 if (dev->ud_cdesc == NULL)
754 return USBD_NOT_CONFIGURED;
755 *count = dev->ud_cdesc->bNumInterface;
756 return USBD_NORMAL_COMPLETION;
757 }
758
759 void
760 usbd_interface2device_handle(struct usbd_interface *iface,
761 struct usbd_device **dev)
762 {
763
764 *dev = iface->ui_dev;
765 }
766
767 usbd_status
768 usbd_device2interface_handle(struct usbd_device *dev,
769 uint8_t ifaceno, struct usbd_interface **iface)
770 {
771
772 if (dev->ud_cdesc == NULL)
773 return USBD_NOT_CONFIGURED;
774 if (ifaceno >= dev->ud_cdesc->bNumInterface)
775 return USBD_INVAL;
776 *iface = &dev->ud_ifaces[ifaceno];
777 return USBD_NORMAL_COMPLETION;
778 }
779
780 struct usbd_device *
781 usbd_pipe2device_handle(struct usbd_pipe *pipe)
782 {
783 KASSERT(pipe != NULL);
784
785 return pipe->up_dev;
786 }
787
788 /* XXXX use altno */
789 usbd_status
790 usbd_set_interface(struct usbd_interface *iface, int altidx)
791 {
792 usb_device_request_t req;
793 usbd_status err;
794 void *endpoints;
795
796 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
797
798 if (LIST_FIRST(&iface->ui_pipes) != NULL)
799 return USBD_IN_USE;
800
801 endpoints = iface->ui_endpoints;
802 int nendpt = iface->ui_idesc->bNumEndpoints;
803 USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
804 (uintptr_t)iface, (uintptr_t)endpoints,
805 iface->ui_idesc->bNumEndpoints, 0);
806 err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
807 if (err)
808 return err;
809
810 /* new setting works, we can free old endpoints */
811 if (endpoints != NULL) {
812 USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
813 (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
814 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
815 }
816 KASSERT(iface->ui_idesc != NULL);
817
818 req.bmRequestType = UT_WRITE_INTERFACE;
819 req.bRequest = UR_SET_INTERFACE;
820 USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
821 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
822 USETW(req.wLength, 0);
823 return usbd_do_request(iface->ui_dev, &req, 0);
824 }
825
826 int
827 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
828 {
829 char *p = (char *)cdesc;
830 char *end = p + UGETW(cdesc->wTotalLength);
831 usb_interface_descriptor_t *d;
832 int n;
833
834 for (n = 0; p < end; p += d->bLength) {
835 d = (usb_interface_descriptor_t *)p;
836 if (p + d->bLength <= end &&
837 d->bDescriptorType == UDESC_INTERFACE &&
838 d->bInterfaceNumber == ifaceno)
839 n++;
840 }
841 return n;
842 }
843
844 int
845 usbd_get_interface_altindex(struct usbd_interface *iface)
846 {
847 return iface->ui_altindex;
848 }
849
850 usbd_status
851 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
852 {
853 usb_device_request_t req;
854
855 req.bmRequestType = UT_READ_INTERFACE;
856 req.bRequest = UR_GET_INTERFACE;
857 USETW(req.wValue, 0);
858 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
859 USETW(req.wLength, 1);
860 return usbd_do_request(iface->ui_dev, &req, aiface);
861 }
862
863 /*** Internal routines ***/
864
865 /* Dequeue all pipe operations, called with bus lock held. */
866 Static usbd_status
867 usbd_ar_pipe(struct usbd_pipe *pipe)
868 {
869 struct usbd_xfer *xfer;
870
871 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
872
873 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
874
875 USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
876 #ifdef USB_DEBUG
877 if (usbdebug > 5)
878 usbd_dump_queue(pipe);
879 #endif
880 pipe->up_repeat = 0;
881 pipe->up_aborting = 1;
882 while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
883 USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
884 "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
885 (uintptr_t)pipe->up_methods, 0);
886 /* Make the HC abort it (and invoke the callback). */
887 pipe->up_methods->upm_abort(xfer);
888 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
889 }
890 pipe->up_aborting = 0;
891 return USBD_NORMAL_COMPLETION;
892 }
893
894 /* Called with USB lock held. */
895 void
896 usb_transfer_complete(struct usbd_xfer *xfer)
897 {
898 struct usbd_pipe *pipe = xfer->ux_pipe;
899 struct usbd_bus *bus = pipe->up_dev->ud_bus;
900 int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
901 int erred;
902 int polling = bus->ub_usepolling;
903 int repeat = pipe->up_repeat;
904
905 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
906
907 USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
908 "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
909 xfer->ux_actlen);
910
911 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
912 KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
913 xfer->ux_state);
914 KASSERT(pipe != NULL);
915
916 /*
917 * If device is known to miss out ack, then pretend that
918 * output timeout is a success. Userland should handle
919 * the logic to verify that the operation succeeded.
920 */
921 if (pipe->up_dev->ud_quirks &&
922 pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
923 xfer->ux_status == USBD_TIMEOUT &&
924 !usbd_xfer_isread(xfer)) {
925 USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
926 "hiding write timeout to %d.%s for %d bytes written",
927 xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
928 xfer->ux_length);
929
930 xfer->ux_status = USBD_NORMAL_COMPLETION;
931 xfer->ux_actlen = xfer->ux_length;
932 }
933
934 erred = xfer->ux_status == USBD_CANCELLED ||
935 xfer->ux_status == USBD_TIMEOUT;
936
937 if (!repeat) {
938 /* Remove request from queue. */
939
940 KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
941 "pipe %p is empty, but xfer %p wants to complete", pipe,
942 xfer);
943 KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
944 "xfer %p is not start of queue (%p is at start)", xfer,
945 SIMPLEQ_FIRST(&pipe->up_queue));
946
947 #ifdef DIAGNOSTIC
948 xfer->ux_state = XFER_BUSY;
949 #endif
950 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
951 }
952 USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
953 (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
954 0);
955
956 /* Count completed transfers. */
957 ++pipe->up_dev->ud_bus->ub_stats.uds_requests
958 [pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
959
960 xfer->ux_done = 1;
961 if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
962 !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
963 USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
964 xfer->ux_actlen, xfer->ux_length, 0, 0);
965 xfer->ux_status = USBD_SHORT_XFER;
966 }
967
968 USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
969 (uintptr_t)pipe->up_methods->upm_done, 0, 0);
970 pipe->up_methods->upm_done(xfer);
971
972 if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
973 KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
974 "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
975
976 /* Only if IN transfer */
977 if (usbd_xfer_isread(xfer)) {
978 memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
979 }
980 }
981
982 USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
983 (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
984
985 if (xfer->ux_callback) {
986 if (!polling) {
987 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
988 if (!(pipe->up_flags & USBD_MPSAFE))
989 KERNEL_LOCK(1, curlwp);
990 }
991
992 xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
993
994 if (!polling) {
995 if (!(pipe->up_flags & USBD_MPSAFE))
996 KERNEL_UNLOCK_ONE(curlwp);
997 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
998 }
999 }
1000
1001 if (sync && !polling) {
1002 USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
1003 (uintptr_t)xfer, 0, 0, 0);
1004 cv_broadcast(&xfer->ux_cv);
1005 }
1006
1007 if (repeat) {
1008 xfer->ux_actlen = 0;
1009 xfer->ux_status = USBD_NOT_STARTED;
1010 } else {
1011 /* XXX should we stop the queue on all errors? */
1012 if (erred && pipe->up_iface != NULL) /* not control pipe */
1013 pipe->up_running = 0;
1014 }
1015 if (pipe->up_running && pipe->up_serialise)
1016 usbd_start_next(pipe);
1017 }
1018
1019 /* Called with USB lock held. */
1020 usbd_status
1021 usb_insert_transfer(struct usbd_xfer *xfer)
1022 {
1023 struct usbd_pipe *pipe = xfer->ux_pipe;
1024 usbd_status err;
1025
1026 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1027
1028 USBHIST_LOG(usbdebug, "xfer = %#jx pipe = %#jx running = %jd "
1029 "timeout = %jd", (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(); USBHIST_CALLED(usbdebug);
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_LOG(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 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1104 struct usbd_xfer *xfer;
1105 usbd_status err;
1106
1107 ASSERT_SLEEPABLE();
1108
1109 size_t len = UGETW(req->wLength);
1110 int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1111 if (error)
1112 return error;
1113
1114 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1115 UGETW(req->wLength), flags, NULL);
1116 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1117 err = usbd_sync_transfer(xfer);
1118 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1119 if (xfer->ux_actlen > xfer->ux_length) {
1120 USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1121 dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1122 USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
1123 "index = %jd",
1124 xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1125 UGETW(xfer->ux_request.wIndex), 0);
1126 USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
1127 "actlen = %jd",
1128 UGETW(xfer->ux_request.wLength),
1129 xfer->ux_length, xfer->ux_actlen, 0);
1130 }
1131 #endif
1132 if (actlen != NULL)
1133 *actlen = xfer->ux_actlen;
1134
1135 usbd_destroy_xfer(xfer);
1136
1137 if (err) {
1138 USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1139 }
1140 return err;
1141 }
1142
1143 static void
1144 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1145 {
1146 usbd_free_xfer(xfer);
1147 }
1148
1149 /*
1150 * Execute a request without waiting for completion.
1151 * Can be used from interrupt context.
1152 */
1153 usbd_status
1154 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1155 usb_device_request_t *req, void *priv, usbd_callback callback)
1156 {
1157 usbd_status err;
1158
1159 if (callback == NULL)
1160 callback = usbd_request_async_cb;
1161
1162 usbd_setup_default_xfer(xfer, dev, priv,
1163 USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1164 callback);
1165 err = usbd_transfer(xfer);
1166 if (err != USBD_IN_PROGRESS) {
1167 usbd_free_xfer(xfer);
1168 return (err);
1169 }
1170 return (USBD_NORMAL_COMPLETION);
1171 }
1172
1173 const struct usbd_quirks *
1174 usbd_get_quirks(struct usbd_device *dev)
1175 {
1176 #ifdef DIAGNOSTIC
1177 if (dev == NULL) {
1178 printf("usbd_get_quirks: dev == NULL\n");
1179 return 0;
1180 }
1181 #endif
1182 return dev->ud_quirks;
1183 }
1184
1185 /* XXX do periodic free() of free list */
1186
1187 /*
1188 * Called from keyboard driver when in polling mode.
1189 */
1190 void
1191 usbd_dopoll(struct usbd_interface *iface)
1192 {
1193 iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1194 }
1195
1196 /*
1197 * This is for keyboard driver as well, which only operates in polling
1198 * mode from the ask root, etc., prompt and from DDB.
1199 */
1200 void
1201 usbd_set_polling(struct usbd_device *dev, int on)
1202 {
1203 if (on)
1204 dev->ud_bus->ub_usepolling++;
1205 else
1206 dev->ud_bus->ub_usepolling--;
1207
1208 /* Kick the host controller when switching modes */
1209 mutex_enter(dev->ud_bus->ub_lock);
1210 dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1211 mutex_exit(dev->ud_bus->ub_lock);
1212 }
1213
1214
1215 usb_endpoint_descriptor_t *
1216 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1217 {
1218 struct usbd_endpoint *ep;
1219 int i;
1220
1221 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1222 ep = &iface->ui_endpoints[i];
1223 if (ep->ue_edesc->bEndpointAddress == address)
1224 return iface->ui_endpoints[i].ue_edesc;
1225 }
1226 return NULL;
1227 }
1228
1229 /*
1230 * usbd_ratecheck() can limit the number of error messages that occurs.
1231 * When a device is unplugged it may take up to 0.25s for the hub driver
1232 * to notice it. If the driver continuously tries to do I/O operations
1233 * this can generate a large number of messages.
1234 */
1235 int
1236 usbd_ratecheck(struct timeval *last)
1237 {
1238 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1239
1240 return ratecheck(last, &errinterval);
1241 }
1242
1243 /*
1244 * Search for a vendor/product pair in an array. The item size is
1245 * given as an argument.
1246 */
1247 const struct usb_devno *
1248 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1249 uint16_t vendor, uint16_t product)
1250 {
1251 while (nentries-- > 0) {
1252 uint16_t tproduct = tbl->ud_product;
1253 if (tbl->ud_vendor == vendor &&
1254 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1255 return tbl;
1256 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1257 }
1258 return NULL;
1259 }
1260
1261
1262 void
1263 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
1264 {
1265 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1266
1267 iter->cur = (const uByte *)cd;
1268 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1269 }
1270
1271 const usb_descriptor_t *
1272 usb_desc_iter_next(usbd_desc_iter_t *iter)
1273 {
1274 const usb_descriptor_t *desc;
1275
1276 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1277 if (iter->cur != iter->end)
1278 printf("usb_desc_iter_next: bad descriptor\n");
1279 return NULL;
1280 }
1281 desc = (const usb_descriptor_t *)iter->cur;
1282 if (desc->bLength == 0) {
1283 printf("usb_desc_iter_next: descriptor length = 0\n");
1284 return NULL;
1285 }
1286 iter->cur += desc->bLength;
1287 if (iter->cur > iter->end) {
1288 printf("usb_desc_iter_next: descriptor length too large\n");
1289 return NULL;
1290 }
1291 return desc;
1292 }
1293
1294 usbd_status
1295 usbd_get_string(struct usbd_device *dev, int si, char *buf)
1296 {
1297 return usbd_get_string0(dev, si, buf, 1);
1298 }
1299
1300 usbd_status
1301 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1302 {
1303 int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1304 usb_string_descriptor_t us;
1305 char *s;
1306 int i, n;
1307 uint16_t c;
1308 usbd_status err;
1309 int size;
1310
1311 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1312
1313 buf[0] = '\0';
1314 if (si == 0)
1315 return USBD_INVAL;
1316 if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1317 return USBD_STALLED;
1318 if (dev->ud_langid == USBD_NOLANG) {
1319 /* Set up default language */
1320 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1321 &size);
1322 if (err || size < 4) {
1323 USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1324 0, 0, 0, 0);
1325 dev->ud_langid = 0; /* Well, just pick something then */
1326 } else {
1327 /* Pick the first language as the default. */
1328 dev->ud_langid = UGETW(us.bString[0]);
1329 }
1330 }
1331 err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1332 if (err)
1333 return err;
1334 s = buf;
1335 n = size / 2 - 1;
1336 if (unicode) {
1337 for (i = 0; i < n; i++) {
1338 c = UGETW(us.bString[i]);
1339 if (swap)
1340 c = (c >> 8) | (c << 8);
1341 s += wput_utf8(s, 3, c);
1342 }
1343 *s++ = 0;
1344 }
1345 #ifdef COMPAT_30
1346 else {
1347 for (i = 0; i < n; i++) {
1348 c = UGETW(us.bString[i]);
1349 if (swap)
1350 c = (c >> 8) | (c << 8);
1351 *s++ = (c < 0x80) ? c : '?';
1352 }
1353 *s++ = 0;
1354 }
1355 #endif
1356 return USBD_NORMAL_COMPLETION;
1357 }
1358