usbdi.c revision 1.175.2.5 1 /* $NetBSD: usbdi.c,v 1.175.2.5 2019/01/18 08:50:44 pgoyette 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.175.2.5 2019/01/18 08:50:44 pgoyette 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 pipe->up_methods->upm_close(pipe);
262
263 if (pipe->up_intrxfer != NULL) {
264 usbd_unlock_pipe(pipe);
265 usbd_destroy_xfer(pipe->up_intrxfer);
266 usbd_lock_pipe(pipe);
267 }
268
269 usbd_unlock_pipe(pipe);
270 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
271
272 return USBD_NORMAL_COMPLETION;
273 }
274
275 usbd_status
276 usbd_transfer(struct usbd_xfer *xfer)
277 {
278 struct usbd_pipe *pipe = xfer->ux_pipe;
279 usbd_status err;
280 unsigned int size, flags;
281
282 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
283
284 USBHIST_LOG(usbdebug,
285 "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
286 (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
287 KASSERT(xfer->ux_status == USBD_NOT_STARTED);
288
289 #ifdef USB_DEBUG
290 if (usbdebug > 5)
291 usbd_dump_queue(pipe);
292 #endif
293 xfer->ux_done = 0;
294
295 if (pipe->up_aborting) {
296 USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
297 (uintptr_t)xfer, 0, 0, 0);
298 return USBD_CANCELLED;
299 }
300
301 KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
302
303 size = xfer->ux_length;
304 flags = xfer->ux_flags;
305
306 if (size != 0) {
307 /*
308 * Use the xfer buffer if none specified in transfer setup.
309 * isoc transfers always use the xfer buffer, i.e.
310 * ux_buffer is always NULL for isoc.
311 */
312 if (xfer->ux_buffer == NULL) {
313 xfer->ux_buffer = xfer->ux_buf;
314 }
315
316 /*
317 * If not using the xfer buffer copy data to the
318 * xfer buffer for OUT transfers of >0 length
319 */
320 if (xfer->ux_buffer != xfer->ux_buf) {
321 KASSERT(xfer->ux_buf);
322 if (!usbd_xfer_isread(xfer)) {
323 memcpy(xfer->ux_buf, xfer->ux_buffer, size);
324 }
325 }
326 }
327
328 /* xfer is not valid after the transfer method unless synchronous */
329 err = pipe->up_methods->upm_transfer(xfer);
330 USBHIST_LOG(usbdebug, "<- done transfer %#jx, err = %jd",
331 (uintptr_t)xfer, err, 0, 0);
332
333 if (err != USBD_IN_PROGRESS && err) {
334 /*
335 * The transfer made it onto the pipe queue, but didn't get
336 * accepted by the HCD for some reason. It needs removing
337 * from the pipe queue.
338 */
339 usbd_lock_pipe(pipe);
340 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
341 if (pipe->up_serialise)
342 usbd_start_next(pipe);
343 usbd_unlock_pipe(pipe);
344 }
345
346 if (!(flags & USBD_SYNCHRONOUS)) {
347 USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
348 (uintptr_t)xfer, err, 0, 0);
349 return err;
350 }
351
352 if (err != USBD_IN_PROGRESS) {
353 USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)"
354 "(complete/error)", (uintptr_t)xfer, err, 0, 0);
355 return err;
356 }
357
358 /* Sync transfer, wait for completion. */
359 usbd_lock_pipe(pipe);
360 while (!xfer->ux_done) {
361 if (pipe->up_dev->ud_bus->ub_usepolling)
362 panic("usbd_transfer: not done");
363 USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
364 (uintptr_t)xfer, 0, 0, 0);
365
366 err = 0;
367 if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
368 err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
369 } else {
370 cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
371 }
372 if (err) {
373 if (!xfer->ux_done)
374 pipe->up_methods->upm_abort(xfer);
375 break;
376 }
377 }
378 usbd_unlock_pipe(pipe);
379 return xfer->ux_status;
380 }
381
382 /* Like usbd_transfer(), but waits for completion. */
383 usbd_status
384 usbd_sync_transfer(struct usbd_xfer *xfer)
385 {
386 xfer->ux_flags |= USBD_SYNCHRONOUS;
387 return usbd_transfer(xfer);
388 }
389
390 /* Like usbd_transfer(), but waits for completion and listens for signals. */
391 usbd_status
392 usbd_sync_transfer_sig(struct usbd_xfer *xfer)
393 {
394 xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
395 return usbd_transfer(xfer);
396 }
397
398 static void *
399 usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
400 {
401 KASSERT(xfer->ux_buf == NULL);
402 KASSERT(size != 0);
403
404 xfer->ux_bufsize = 0;
405 #if NUSB_DMA > 0
406 struct usbd_bus *bus = xfer->ux_bus;
407
408 if (bus->ub_usedma) {
409 usb_dma_t *dmap = &xfer->ux_dmabuf;
410
411 int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
412 if (err) {
413 return NULL;
414 }
415 xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
416 xfer->ux_bufsize = size;
417
418 return xfer->ux_buf;
419 }
420 #endif
421 KASSERT(xfer->ux_bus->ub_usedma == false);
422 xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
423 xfer->ux_bufsize = size;
424 return xfer->ux_buf;
425 }
426
427 static void
428 usbd_free_buffer(struct usbd_xfer *xfer)
429 {
430 KASSERT(xfer->ux_buf != NULL);
431 KASSERT(xfer->ux_bufsize != 0);
432
433 void *buf = xfer->ux_buf;
434 uint32_t size = xfer->ux_bufsize;
435
436 xfer->ux_buf = NULL;
437 xfer->ux_bufsize = 0;
438
439 #if NUSB_DMA > 0
440 struct usbd_bus *bus = xfer->ux_bus;
441
442 if (bus->ub_usedma) {
443 usb_dma_t *dmap = &xfer->ux_dmabuf;
444
445 usb_freemem(bus, dmap);
446 return;
447 }
448 #endif
449 KASSERT(xfer->ux_bus->ub_usedma == false);
450
451 kmem_free(buf, size);
452 }
453
454 void *
455 usbd_get_buffer(struct usbd_xfer *xfer)
456 {
457 return xfer->ux_buf;
458 }
459
460 struct usbd_pipe *
461 usbd_get_pipe0(struct usbd_device *dev)
462 {
463
464 return dev->ud_pipe0;
465 }
466
467 static struct usbd_xfer *
468 usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
469 {
470 struct usbd_xfer *xfer;
471
472 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
473
474 ASSERT_SLEEPABLE();
475
476 xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
477 if (xfer == NULL)
478 return NULL;
479 xfer->ux_bus = dev->ud_bus;
480 callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
481 cv_init(&xfer->ux_cv, "usbxfer");
482
483 USBHIST_LOG(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
484
485 return xfer;
486 }
487
488 static usbd_status
489 usbd_free_xfer(struct usbd_xfer *xfer)
490 {
491 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
492
493 USBHIST_LOG(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
494 if (xfer->ux_buf) {
495 usbd_free_buffer(xfer);
496 }
497 #if defined(DIAGNOSTIC)
498 if (callout_pending(&xfer->ux_callout)) {
499 callout_stop(&xfer->ux_callout);
500 printf("usbd_free_xfer: timeout_handle pending\n");
501 }
502 #endif
503 cv_destroy(&xfer->ux_cv);
504 xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
505 return USBD_NORMAL_COMPLETION;
506 }
507
508 int
509 usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
510 unsigned int nframes, struct usbd_xfer **xp)
511 {
512 KASSERT(xp != NULL);
513 void *buf = NULL;
514
515 struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
516 if (xfer == NULL)
517 return ENOMEM;
518
519 if (len) {
520 buf = usbd_alloc_buffer(xfer, len);
521 if (!buf) {
522 usbd_free_xfer(xfer);
523 return ENOMEM;
524 }
525 }
526 xfer->ux_pipe = pipe;
527 xfer->ux_flags = flags;
528 xfer->ux_nframes = nframes;
529 xfer->ux_methods = pipe->up_methods;
530
531 if (xfer->ux_methods->upm_init) {
532 int err = xfer->ux_methods->upm_init(xfer);
533 if (err) {
534 if (buf)
535 usbd_free_buffer(xfer);
536 usbd_free_xfer(xfer);
537 return err;
538 }
539 }
540
541 *xp = xfer;
542 return 0;
543 }
544
545 void
546 usbd_destroy_xfer(struct usbd_xfer *xfer)
547 {
548
549 if (xfer->ux_methods->upm_fini) {
550 xfer->ux_methods->upm_fini(xfer);
551 }
552
553 usbd_free_xfer(xfer);
554 }
555
556 void
557 usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
558 uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
559 {
560 KASSERT(xfer->ux_pipe);
561
562 xfer->ux_priv = priv;
563 xfer->ux_buffer = buffer;
564 xfer->ux_length = length;
565 xfer->ux_actlen = 0;
566 xfer->ux_flags = flags;
567 xfer->ux_timeout = timeout;
568 xfer->ux_status = USBD_NOT_STARTED;
569 xfer->ux_callback = callback;
570 xfer->ux_rqflags &= ~URQ_REQUEST;
571 xfer->ux_nframes = 0;
572 }
573
574 void
575 usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
576 void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
577 uint32_t length, uint16_t flags, usbd_callback callback)
578 {
579 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
580
581 xfer->ux_priv = priv;
582 xfer->ux_buffer = buffer;
583 xfer->ux_length = length;
584 xfer->ux_actlen = 0;
585 xfer->ux_flags = flags;
586 xfer->ux_timeout = timeout;
587 xfer->ux_status = USBD_NOT_STARTED;
588 xfer->ux_callback = callback;
589 xfer->ux_request = *req;
590 xfer->ux_rqflags |= URQ_REQUEST;
591 xfer->ux_nframes = 0;
592 }
593
594 void
595 usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
596 uint32_t nframes, uint16_t flags, usbd_callback callback)
597 {
598 xfer->ux_priv = priv;
599 xfer->ux_buffer = NULL;
600 xfer->ux_length = 0;
601 xfer->ux_actlen = 0;
602 xfer->ux_flags = flags;
603 xfer->ux_timeout = USBD_NO_TIMEOUT;
604 xfer->ux_status = USBD_NOT_STARTED;
605 xfer->ux_callback = callback;
606 xfer->ux_rqflags &= ~URQ_REQUEST;
607 xfer->ux_frlengths = frlengths;
608 xfer->ux_nframes = nframes;
609 }
610
611 void
612 usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
613 void **buffer, uint32_t *count, usbd_status *status)
614 {
615 if (priv != NULL)
616 *priv = xfer->ux_priv;
617 if (buffer != NULL)
618 *buffer = xfer->ux_buffer;
619 if (count != NULL)
620 *count = xfer->ux_actlen;
621 if (status != NULL)
622 *status = xfer->ux_status;
623 }
624
625 usb_config_descriptor_t *
626 usbd_get_config_descriptor(struct usbd_device *dev)
627 {
628 KASSERT(dev != NULL);
629
630 return dev->ud_cdesc;
631 }
632
633 usb_interface_descriptor_t *
634 usbd_get_interface_descriptor(struct usbd_interface *iface)
635 {
636 KASSERT(iface != NULL);
637
638 return iface->ui_idesc;
639 }
640
641 usb_device_descriptor_t *
642 usbd_get_device_descriptor(struct usbd_device *dev)
643 {
644 KASSERT(dev != NULL);
645
646 return &dev->ud_ddesc;
647 }
648
649 usb_endpoint_descriptor_t *
650 usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
651 {
652
653 if (index >= iface->ui_idesc->bNumEndpoints)
654 return NULL;
655 return iface->ui_endpoints[index].ue_edesc;
656 }
657
658 /* Some drivers may wish to abort requests on the default pipe, *
659 * but there is no mechanism for getting a handle on it. */
660 usbd_status
661 usbd_abort_default_pipe(struct usbd_device *device)
662 {
663 return usbd_abort_pipe(device->ud_pipe0);
664 }
665
666 usbd_status
667 usbd_abort_pipe(struct usbd_pipe *pipe)
668 {
669 usbd_status err;
670
671 KASSERT(pipe != NULL);
672
673 usbd_lock_pipe(pipe);
674 err = usbd_ar_pipe(pipe);
675 usbd_unlock_pipe(pipe);
676 return err;
677 }
678
679 usbd_status
680 usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
681 {
682 struct usbd_device *dev = pipe->up_dev;
683 usb_device_request_t req;
684 usbd_status err;
685
686 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
687
688 /*
689 * Clearing en endpoint stall resets the endpoint toggle, so
690 * do the same to the HC toggle.
691 */
692 pipe->up_methods->upm_cleartoggle(pipe);
693
694 req.bmRequestType = UT_WRITE_ENDPOINT;
695 req.bRequest = UR_CLEAR_FEATURE;
696 USETW(req.wValue, UF_ENDPOINT_HALT);
697 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
698 USETW(req.wLength, 0);
699 err = usbd_do_request(dev, &req, 0);
700 #if 0
701 XXX should we do this?
702 if (!err) {
703 pipe->state = USBD_PIPE_ACTIVE;
704 /* XXX activate pipe */
705 }
706 #endif
707 return err;
708 }
709
710 void
711 usbd_clear_endpoint_stall_task(void *arg)
712 {
713 struct usbd_pipe *pipe = arg;
714 struct usbd_device *dev = pipe->up_dev;
715 usb_device_request_t req;
716
717 pipe->up_methods->upm_cleartoggle(pipe);
718
719 req.bmRequestType = UT_WRITE_ENDPOINT;
720 req.bRequest = UR_CLEAR_FEATURE;
721 USETW(req.wValue, UF_ENDPOINT_HALT);
722 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
723 USETW(req.wLength, 0);
724 (void)usbd_do_request(dev, &req, 0);
725 }
726
727 void
728 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
729 {
730 usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
731 }
732
733 void
734 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
735 {
736
737 pipe->up_methods->upm_cleartoggle(pipe);
738 }
739
740 usbd_status
741 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
742 {
743 KASSERT(iface != NULL);
744 KASSERT(iface->ui_idesc != NULL);
745
746 *count = iface->ui_idesc->bNumEndpoints;
747 return USBD_NORMAL_COMPLETION;
748 }
749
750 usbd_status
751 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
752 {
753
754 if (dev->ud_cdesc == NULL)
755 return USBD_NOT_CONFIGURED;
756 *count = dev->ud_cdesc->bNumInterface;
757 return USBD_NORMAL_COMPLETION;
758 }
759
760 void
761 usbd_interface2device_handle(struct usbd_interface *iface,
762 struct usbd_device **dev)
763 {
764
765 *dev = iface->ui_dev;
766 }
767
768 usbd_status
769 usbd_device2interface_handle(struct usbd_device *dev,
770 uint8_t ifaceno, struct usbd_interface **iface)
771 {
772
773 if (dev->ud_cdesc == NULL)
774 return USBD_NOT_CONFIGURED;
775 if (ifaceno >= dev->ud_cdesc->bNumInterface)
776 return USBD_INVAL;
777 *iface = &dev->ud_ifaces[ifaceno];
778 return USBD_NORMAL_COMPLETION;
779 }
780
781 struct usbd_device *
782 usbd_pipe2device_handle(struct usbd_pipe *pipe)
783 {
784 KASSERT(pipe != NULL);
785
786 return pipe->up_dev;
787 }
788
789 /* XXXX use altno */
790 usbd_status
791 usbd_set_interface(struct usbd_interface *iface, int altidx)
792 {
793 usb_device_request_t req;
794 usbd_status err;
795 void *endpoints;
796
797 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
798
799 if (LIST_FIRST(&iface->ui_pipes) != NULL)
800 return USBD_IN_USE;
801
802 endpoints = iface->ui_endpoints;
803 int nendpt = iface->ui_idesc->bNumEndpoints;
804 USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
805 (uintptr_t)iface, (uintptr_t)endpoints,
806 iface->ui_idesc->bNumEndpoints, 0);
807 err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
808 if (err)
809 return err;
810
811 /* new setting works, we can free old endpoints */
812 if (endpoints != NULL) {
813 USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
814 (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
815 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
816 }
817 KASSERT(iface->ui_idesc != NULL);
818
819 req.bmRequestType = UT_WRITE_INTERFACE;
820 req.bRequest = UR_SET_INTERFACE;
821 USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
822 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
823 USETW(req.wLength, 0);
824 return usbd_do_request(iface->ui_dev, &req, 0);
825 }
826
827 int
828 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
829 {
830 char *p = (char *)cdesc;
831 char *end = p + UGETW(cdesc->wTotalLength);
832 usb_interface_descriptor_t *d;
833 int n;
834
835 for (n = 0; p < end; p += d->bLength) {
836 d = (usb_interface_descriptor_t *)p;
837 if (p + d->bLength <= end &&
838 d->bDescriptorType == UDESC_INTERFACE &&
839 d->bInterfaceNumber == ifaceno)
840 n++;
841 }
842 return n;
843 }
844
845 int
846 usbd_get_interface_altindex(struct usbd_interface *iface)
847 {
848 return iface->ui_altindex;
849 }
850
851 usbd_status
852 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
853 {
854 usb_device_request_t req;
855
856 req.bmRequestType = UT_READ_INTERFACE;
857 req.bRequest = UR_GET_INTERFACE;
858 USETW(req.wValue, 0);
859 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
860 USETW(req.wLength, 1);
861 return usbd_do_request(iface->ui_dev, &req, aiface);
862 }
863
864 /*** Internal routines ***/
865
866 /* Dequeue all pipe operations, called with bus lock held. */
867 Static usbd_status
868 usbd_ar_pipe(struct usbd_pipe *pipe)
869 {
870 struct usbd_xfer *xfer;
871
872 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
873
874 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
875
876 USBHIST_LOG(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
877 #ifdef USB_DEBUG
878 if (usbdebug > 5)
879 usbd_dump_queue(pipe);
880 #endif
881 pipe->up_repeat = 0;
882 pipe->up_aborting = 1;
883 while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
884 USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
885 "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
886 (uintptr_t)pipe->up_methods, 0);
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 pipe->up_aborting = 0;
892 return USBD_NORMAL_COMPLETION;
893 }
894
895 /* Called with USB lock held. */
896 void
897 usb_transfer_complete(struct usbd_xfer *xfer)
898 {
899 struct usbd_pipe *pipe = xfer->ux_pipe;
900 struct usbd_bus *bus = pipe->up_dev->ud_bus;
901 int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
902 int erred;
903 int polling = bus->ub_usepolling;
904 int repeat = pipe->up_repeat;
905
906 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
907
908 USBHIST_LOG(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_CALLED(usbdebug);
1028
1029 USBHIST_LOG(usbdebug, "xfer = %#jx pipe = %#jx running = %jd "
1030 "timeout = %jd", (uintptr_t)xfer, (uintptr_t)pipe,
1031 pipe->up_running, xfer->ux_timeout);
1032
1033 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1034 KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
1035 xfer->ux_state);
1036
1037 #ifdef DIAGNOSTIC
1038 xfer->ux_state = XFER_ONQU;
1039 #endif
1040 SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1041 if (pipe->up_running && pipe->up_serialise)
1042 err = USBD_IN_PROGRESS;
1043 else {
1044 pipe->up_running = 1;
1045 err = USBD_NORMAL_COMPLETION;
1046 }
1047 USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
1048 err, 0, 0);
1049 return err;
1050 }
1051
1052 /* Called with USB lock held. */
1053 void
1054 usbd_start_next(struct usbd_pipe *pipe)
1055 {
1056 struct usbd_xfer *xfer;
1057 usbd_status err;
1058
1059 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1060
1061 KASSERT(pipe != NULL);
1062 KASSERT(pipe->up_methods != NULL);
1063 KASSERT(pipe->up_methods->upm_start != NULL);
1064 KASSERT(pipe->up_serialise == true);
1065
1066 int polling = pipe->up_dev->ud_bus->ub_usepolling;
1067 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1068
1069 /* Get next request in queue. */
1070 xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1071 USBHIST_LOG(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
1072 (uintptr_t)xfer, 0, 0);
1073 if (xfer == NULL) {
1074 pipe->up_running = 0;
1075 } else {
1076 if (!polling)
1077 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1078 err = pipe->up_methods->upm_start(xfer);
1079 if (!polling)
1080 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1081
1082 if (err != USBD_IN_PROGRESS) {
1083 USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
1084 pipe->up_running = 0;
1085 /* XXX do what? */
1086 }
1087 }
1088
1089 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1090 }
1091
1092 usbd_status
1093 usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
1094 {
1095
1096 return usbd_do_request_flags(dev, req, data, 0, 0,
1097 USBD_DEFAULT_TIMEOUT);
1098 }
1099
1100 usbd_status
1101 usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
1102 void *data, uint16_t flags, int *actlen, uint32_t timeout)
1103 {
1104 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1105 struct usbd_xfer *xfer;
1106 usbd_status err;
1107
1108 ASSERT_SLEEPABLE();
1109
1110 size_t len = UGETW(req->wLength);
1111 int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
1112 if (error)
1113 return error;
1114
1115 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
1116 UGETW(req->wLength), flags, NULL);
1117 KASSERT(xfer->ux_pipe == dev->ud_pipe0);
1118 err = usbd_sync_transfer(xfer);
1119 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1120 if (xfer->ux_actlen > xfer->ux_length) {
1121 USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
1122 dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1123 USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
1124 "index = %jd",
1125 xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1126 UGETW(xfer->ux_request.wIndex), 0);
1127 USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
1128 "actlen = %jd",
1129 UGETW(xfer->ux_request.wLength),
1130 xfer->ux_length, xfer->ux_actlen, 0);
1131 }
1132 #endif
1133 if (actlen != NULL)
1134 *actlen = xfer->ux_actlen;
1135
1136 usbd_destroy_xfer(xfer);
1137
1138 if (err) {
1139 USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
1140 }
1141 return err;
1142 }
1143
1144 static void
1145 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
1146 {
1147 usbd_free_xfer(xfer);
1148 }
1149
1150 /*
1151 * Execute a request without waiting for completion.
1152 * Can be used from interrupt context.
1153 */
1154 usbd_status
1155 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
1156 usb_device_request_t *req, void *priv, usbd_callback callback)
1157 {
1158 usbd_status err;
1159
1160 if (callback == NULL)
1161 callback = usbd_request_async_cb;
1162
1163 usbd_setup_default_xfer(xfer, dev, priv,
1164 USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
1165 callback);
1166 err = usbd_transfer(xfer);
1167 if (err != USBD_IN_PROGRESS) {
1168 usbd_free_xfer(xfer);
1169 return (err);
1170 }
1171 return (USBD_NORMAL_COMPLETION);
1172 }
1173
1174 const struct usbd_quirks *
1175 usbd_get_quirks(struct usbd_device *dev)
1176 {
1177 #ifdef DIAGNOSTIC
1178 if (dev == NULL) {
1179 printf("usbd_get_quirks: dev == NULL\n");
1180 return 0;
1181 }
1182 #endif
1183 return dev->ud_quirks;
1184 }
1185
1186 /* XXX do periodic free() of free list */
1187
1188 /*
1189 * Called from keyboard driver when in polling mode.
1190 */
1191 void
1192 usbd_dopoll(struct usbd_interface *iface)
1193 {
1194 iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1195 }
1196
1197 /*
1198 * This is for keyboard driver as well, which only operates in polling
1199 * mode from the ask root, etc., prompt and from DDB.
1200 */
1201 void
1202 usbd_set_polling(struct usbd_device *dev, int on)
1203 {
1204 if (on)
1205 dev->ud_bus->ub_usepolling++;
1206 else
1207 dev->ud_bus->ub_usepolling--;
1208
1209 /* Kick the host controller when switching modes */
1210 mutex_enter(dev->ud_bus->ub_lock);
1211 dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
1212 mutex_exit(dev->ud_bus->ub_lock);
1213 }
1214
1215
1216 usb_endpoint_descriptor_t *
1217 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
1218 {
1219 struct usbd_endpoint *ep;
1220 int i;
1221
1222 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
1223 ep = &iface->ui_endpoints[i];
1224 if (ep->ue_edesc->bEndpointAddress == address)
1225 return iface->ui_endpoints[i].ue_edesc;
1226 }
1227 return NULL;
1228 }
1229
1230 /*
1231 * usbd_ratecheck() can limit the number of error messages that occurs.
1232 * When a device is unplugged it may take up to 0.25s for the hub driver
1233 * to notice it. If the driver continuously tries to do I/O operations
1234 * this can generate a large number of messages.
1235 */
1236 int
1237 usbd_ratecheck(struct timeval *last)
1238 {
1239 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1240
1241 return ratecheck(last, &errinterval);
1242 }
1243
1244 /*
1245 * Search for a vendor/product pair in an array. The item size is
1246 * given as an argument.
1247 */
1248 const struct usb_devno *
1249 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1250 uint16_t vendor, uint16_t product)
1251 {
1252 while (nentries-- > 0) {
1253 uint16_t tproduct = tbl->ud_product;
1254 if (tbl->ud_vendor == vendor &&
1255 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1256 return tbl;
1257 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1258 }
1259 return NULL;
1260 }
1261
1262
1263 void
1264 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
1265 {
1266 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1267
1268 iter->cur = (const uByte *)cd;
1269 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1270 }
1271
1272 const usb_descriptor_t *
1273 usb_desc_iter_next(usbd_desc_iter_t *iter)
1274 {
1275 const usb_descriptor_t *desc;
1276
1277 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1278 if (iter->cur != iter->end)
1279 printf("usb_desc_iter_next: bad descriptor\n");
1280 return NULL;
1281 }
1282 desc = (const usb_descriptor_t *)iter->cur;
1283 if (desc->bLength == 0) {
1284 printf("usb_desc_iter_next: descriptor length = 0\n");
1285 return NULL;
1286 }
1287 iter->cur += desc->bLength;
1288 if (iter->cur > iter->end) {
1289 printf("usb_desc_iter_next: descriptor length too large\n");
1290 return NULL;
1291 }
1292 return desc;
1293 }
1294
1295 usbd_status
1296 usbd_get_string(struct usbd_device *dev, int si, char *buf)
1297 {
1298 return usbd_get_string0(dev, si, buf, 1);
1299 }
1300
1301 usbd_status
1302 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
1303 {
1304 int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
1305 usb_string_descriptor_t us;
1306 char *s;
1307 int i, n;
1308 uint16_t c;
1309 usbd_status err;
1310 int size;
1311
1312 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1313
1314 buf[0] = '\0';
1315 if (si == 0)
1316 return USBD_INVAL;
1317 if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
1318 return USBD_STALLED;
1319 if (dev->ud_langid == USBD_NOLANG) {
1320 /* Set up default language */
1321 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1322 &size);
1323 if (err || size < 4) {
1324 USBHIST_LOG(usbdebug, "getting lang failed, using 0",
1325 0, 0, 0, 0);
1326 dev->ud_langid = 0; /* Well, just pick something then */
1327 } else {
1328 /* Pick the first language as the default. */
1329 dev->ud_langid = UGETW(us.bString[0]);
1330 }
1331 }
1332 err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
1333 if (err)
1334 return err;
1335 s = buf;
1336 n = size / 2 - 1;
1337 if (unicode) {
1338 for (i = 0; i < n; i++) {
1339 c = UGETW(us.bString[i]);
1340 if (swap)
1341 c = (c >> 8) | (c << 8);
1342 s += wput_utf8(s, 3, c);
1343 }
1344 *s++ = 0;
1345 }
1346 #ifdef COMPAT_30
1347 else {
1348 for (i = 0; i < n; i++) {
1349 c = UGETW(us.bString[i]);
1350 if (swap)
1351 c = (c >> 8) | (c << 8);
1352 *s++ = (c < 0x80) ? c : '?';
1353 }
1354 *s++ = 0;
1355 }
1356 #endif
1357 return USBD_NORMAL_COMPLETION;
1358 }
1359