usbdi.c revision 1.162.2.7 1 /* $NetBSD: usbdi.c,v 1.162.2.7 2014/12/03 14:18:07 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2012 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 and Matthew R. Green (mrg (at) eterna.com.au).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.162.2.7 2014/12/03 14:18:07 skrll Exp $");
35
36 #ifdef _KERNEL_OPT
37 #include "opt_usb.h"
38 #include "opt_compat_netbsd.h"
39 #endif
40
41 #include "usb_dma.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #include <sys/malloc.h>
48 #include <sys/kmem.h>
49 #include <sys/proc.h>
50 #include <sys/bus.h>
51 #include <sys/cpu.h>
52
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdivar.h>
57 #include <dev/usb/usb_mem.h>
58 #include <dev/usb/usb_quirks.h>
59 #include <dev/usb/usbhist.h>
60
61 /* UTF-8 encoding stuff */
62 #include <fs/unicode.h>
63
64 extern int usbdebug;
65
66 Static usbd_status usbd_ar_pipe(usbd_pipe_handle);
67 Static void usbd_start_next(usbd_pipe_handle);
68 Static usbd_status usbd_open_pipe_ival
69 (usbd_interface_handle, uint8_t, uint8_t, usbd_pipe_handle *, int);
70
71 static inline int
72 usbd_xfer_isread(usbd_xfer_handle xfer)
73 {
74 if (xfer->ux_rqflags & URQ_REQUEST)
75 return (xfer->ux_request.bmRequestType & UT_READ);
76 else
77 return (xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress &
78 UE_DIR_IN);
79 }
80
81 #if defined(USB_DEBUG)
82 void
83 usbd_dump_iface(struct usbd_interface *iface)
84 {
85 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
86
87 USBHIST_LOG(usbdebug, "iface %p\n", iface, 0, 0, 0);
88 if (iface == NULL)
89 return;
90 USBHIST_LOG(usbdebug, " device = %p idesc = %p index = %d",
91 iface->ui_dev, iface->ui_idesc, iface->ui_index, 0);
92 USBHIST_LOG(usbdebug, " altindex=%d priv=%p",
93 iface->ui_altindex, iface->ui_priv, 0, 0);
94 }
95
96 void
97 usbd_dump_device(struct usbd_device *dev)
98 {
99 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
100
101 USBHIST_LOG(usbdebug, "dev = %p", dev, 0, 0, 0);
102 if (dev == NULL)
103 return;
104 USBHIST_LOG(usbdebug, " bus = %p default_pipe = %p",
105 dev->ud_bus, dev->ud_pipe0, 0, 0);
106 USBHIST_LOG(usbdebug, " address = %d config = %d depth = %d ",
107 dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
108 USBHIST_LOG(usbdebug, " speed = %d self_powered = %d "
109 "power = %d langid = %d",
110 dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
111 }
112
113 void
114 usbd_dump_endpoint(struct usbd_endpoint *endp)
115 {
116 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
117
118 USBHIST_LOG(usbdebug, "endp = %p", endp, 0, 0, 0);
119 if (endp == NULL)
120 return;
121 USBHIST_LOG(usbdebug, " edesc = %p refcnt = %d",
122 endp->ue_edesc, endp->ue_refcnt, 0, 0);
123 if (endp->ue_edesc)
124 USBHIST_LOG(usbdebug, " bEndpointAddress=0x%02x",
125 endp->ue_edesc->bEndpointAddress, 0, 0, 0);
126 }
127
128 void
129 usbd_dump_queue(usbd_pipe_handle pipe)
130 {
131 usbd_xfer_handle xfer;
132
133 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
134
135 USBHIST_LOG(usbdebug, "pipe = %p", pipe, 0, 0, 0);
136 SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
137 USBHIST_LOG(usbdebug, " xfer = %p", xfer, 0, 0, 0);
138 }
139 }
140
141 void
142 usbd_dump_pipe(usbd_pipe_handle pipe)
143 {
144 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
145
146 USBHIST_LOG(usbdebug, "pipe = %p", pipe, 0, 0, 0);
147 if (pipe == NULL)
148 return;
149 usbd_dump_iface(pipe->up_iface);
150 usbd_dump_device(pipe->up_dev);
151 usbd_dump_endpoint(pipe->up_endpoint);
152 USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
153 USBHIST_LOG(usbdebug, " refcnt = %d running = %d aborting = %d",
154 pipe->up_refcnt, pipe->up_running, pipe->up_aborting, 0);
155 USBHIST_LOG(usbdebug, " intrxfer = %p, repeat = %d, interval = %d",
156 pipe->up_intrxfer, pipe->up_repeat, pipe->up_interval, 0);
157 }
158 #endif
159
160 usbd_status
161 usbd_open_pipe(usbd_interface_handle iface, uint8_t address,
162 uint8_t flags, usbd_pipe_handle *pipe)
163 {
164 return (usbd_open_pipe_ival(iface, address, flags, pipe,
165 USBD_DEFAULT_INTERVAL));
166 }
167
168 usbd_status
169 usbd_open_pipe_ival(usbd_interface_handle iface, uint8_t address,
170 uint8_t flags, usbd_pipe_handle *pipe, int ival)
171 {
172 usbd_pipe_handle p;
173 struct usbd_endpoint *ep;
174 usbd_status err;
175 int i;
176
177 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
178
179 USBHIST_LOG(usbdebug, "iface = %p address = 0x%x flags = 0x%x",
180 iface, address, flags, 0);
181
182 for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
183 ep = &iface->ui_endpoints[i];
184 if (ep->ue_edesc == NULL)
185 return (USBD_IOERROR);
186 if (ep->ue_edesc->bEndpointAddress == address)
187 goto found;
188 }
189 return (USBD_BAD_ADDRESS);
190 found:
191 if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
192 return (USBD_IN_USE);
193 err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
194 if (err)
195 return (err);
196 LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
197 *pipe = p;
198 return (USBD_NORMAL_COMPLETION);
199 }
200
201 usbd_status
202 usbd_open_pipe_intr(usbd_interface_handle iface, uint8_t address,
203 uint8_t flags, usbd_pipe_handle *pipe,
204 usbd_private_handle priv, void *buffer, uint32_t len,
205 usbd_callback cb, int ival)
206 {
207 usbd_status err;
208 usbd_xfer_handle xfer;
209 usbd_pipe_handle ipipe;
210
211 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
212
213 USBHIST_LOG(usbdebug, "address = 0x%x flags = 0x%x len = %d",
214 address, flags, len, 0);
215
216 err = usbd_open_pipe_ival(iface, address,
217 USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
218 &ipipe, ival);
219 if (err)
220 return (err);
221 xfer = usbd_alloc_xfer(iface->ui_dev);
222 if (xfer == NULL) {
223 err = USBD_NOMEM;
224 goto bad1;
225 }
226 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
227 USBD_NO_TIMEOUT, cb);
228 ipipe->up_intrxfer = xfer;
229 ipipe->up_repeat = 1;
230 err = usbd_transfer(xfer);
231 *pipe = ipipe;
232 if (err != USBD_IN_PROGRESS)
233 goto bad2;
234 return (USBD_NORMAL_COMPLETION);
235
236 bad2:
237 ipipe->up_intrxfer = NULL;
238 ipipe->up_repeat = 0;
239 usbd_free_xfer(xfer);
240 bad1:
241 usbd_close_pipe(ipipe);
242 return (err);
243 }
244
245 usbd_status
246 usbd_close_pipe(usbd_pipe_handle pipe)
247 {
248
249 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
250
251 #ifdef DIAGNOSTIC
252 if (pipe == NULL) {
253 USBHIST_LOG(usbdebug, "pipe == NULL", 0, 0, 0, 0);
254 return (USBD_NORMAL_COMPLETION);
255 }
256 #endif
257
258 usbd_lock_pipe(pipe);
259 if (--pipe->up_refcnt != 0) {
260 usbd_unlock_pipe(pipe);
261 return (USBD_NORMAL_COMPLETION);
262 }
263 if (! SIMPLEQ_EMPTY(&pipe->up_queue)) {
264 usbd_unlock_pipe(pipe);
265 return (USBD_PENDING_REQUESTS);
266 }
267 LIST_REMOVE(pipe, up_next);
268 pipe->up_endpoint->ue_refcnt--;
269 pipe->up_methods->upm_close(pipe);
270 usbd_unlock_pipe(pipe);
271 if (pipe->up_intrxfer != NULL)
272 usbd_free_xfer(pipe->up_intrxfer);
273 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
274 return (USBD_NORMAL_COMPLETION);
275 }
276
277 usbd_status
278 usbd_transfer(usbd_xfer_handle xfer)
279 {
280 usbd_pipe_handle pipe = xfer->ux_pipe;
281 usbd_status err;
282 unsigned int size, flags;
283
284 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
285
286 USBHIST_LOG(usbdebug,
287 "xfer = %p, flags = %#x, pipe = %p, running = %d",
288 xfer, xfer->ux_flags, pipe, pipe->up_running);
289
290 #ifdef USB_DEBUG
291 if (usbdebug > 5)
292 usbd_dump_queue(pipe);
293 #endif
294 xfer->ux_done = 0;
295
296 if (pipe->up_aborting) {
297 USBHIST_LOG(usbdebug, "<- done xfer %p, aborting", xfer, 0, 0,
298 0);
299 return (USBD_CANCELLED);
300 }
301
302 struct usbd_bus *bus = pipe->up_dev->ud_bus;
303
304 size = xfer->ux_length;
305 flags = xfer->ux_flags;
306
307 /*
308 * isoc transfers are always size == 0, whereas other transfers can
309 * require a URQ_AUTO_BUFFER buffer.
310 *
311 * URQ_AUTO_BUFFER will be removed at some point, i.e. the transfer
312 * should provide the buffer.
313 */
314 if (size != 0) {
315 if (xfer->ux_buf == NULL) {
316 xfer->ux_buf = usbd_alloc_buffer(xfer, size);
317 if (xfer->ux_buf == NULL)
318 return USBD_NOMEM;
319
320 #ifdef DIAGNOSTIC
321 if (xfer->ux_rqflags & URQ_AUTO_BUFFER)
322 printf("usbd_transfer: has old buffer!\n");
323 #endif
324 xfer->ux_rqflags |= URQ_AUTO_BUFFER;
325 }
326 }
327 if (bus->ub_usedma) {
328 /*
329 * Copy data if not using the xfer buffer. isoc transfers
330 * always use DMA buffer, i.e. buffer == NULL
331 */
332 if (xfer->ux_buffer == NULL) {
333 xfer->ux_buffer = xfer->ux_buf;
334 } else if (xfer->ux_buffer != xfer->ux_buf) {
335 memcpy(xfer->ux_buf, xfer->ux_buffer, size);
336 }
337 } else {
338 xfer->ux_buffer = xfer->ux_buf;
339 }
340
341 /* xfer is not valid after the transfer method unless synchronous */
342 err = pipe->up_methods->upm_transfer(xfer);
343 USBHIST_LOG(usbdebug, "<- done transfer %p, err = %d", xfer, err, 0, 0);
344
345 if (err != USBD_IN_PROGRESS && err) {
346 /* The transfer has not been queued, so free buffer. */
347 if (xfer->ux_rqflags & URQ_AUTO_BUFFER) {
348 usbd_free_buffer(xfer);
349 xfer->ux_rqflags &= ~URQ_AUTO_BUFFER;
350 }
351 }
352
353 if (!(flags & USBD_SYNCHRONOUS)) {
354 USBHIST_LOG(usbdebug, "<- done xfer %p, not sync", xfer, 0, 0,
355 0);
356 return (err);
357 }
358
359 /* Sync transfer, wait for completion. */
360 if (err != USBD_IN_PROGRESS) {
361 USBHIST_LOG(usbdebug, "<- done xfer %p, not in progress", xfer,
362 0, 0, 0);
363 return (err);
364 }
365 usbd_lock_pipe(pipe);
366 while (!xfer->ux_done) {
367 if (pipe->up_dev->ud_bus->ub_usepolling)
368 panic("usbd_transfer: not done");
369 USBHIST_LOG(usbdebug, "<- sleeping on xfer %p", xfer, 0, 0, 0);
370
371 err = 0;
372 if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
373 err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
374 } else {
375 cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
376 }
377 if (err) {
378 if (!xfer->ux_done)
379 pipe->up_methods->upm_abort(xfer);
380 break;
381 }
382 }
383 usbd_unlock_pipe(pipe);
384 return (xfer->ux_status);
385 }
386
387 /* Like usbd_transfer(), but waits for completion. */
388 usbd_status
389 usbd_sync_transfer(usbd_xfer_handle xfer)
390 {
391 xfer->ux_flags |= USBD_SYNCHRONOUS;
392 return (usbd_transfer(xfer));
393 }
394
395 /* Like usbd_transfer(), but waits for completion and listens for signals. */
396 usbd_status
397 usbd_sync_transfer_sig(usbd_xfer_handle xfer)
398 {
399 xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
400 return (usbd_transfer(xfer));
401 }
402
403 void *
404 usbd_alloc_buffer(usbd_xfer_handle xfer, uint32_t size)
405 {
406
407 KASSERT(xfer->ux_buf == NULL);
408 KASSERT(size != 0);
409 KASSERT(!(xfer->ux_rqflags & URQ_AUTO_BUFFER));
410
411 xfer->ux_bufsize = 0;
412 #if NUSB_DMA > 0
413 struct usbd_bus *bus = xfer->ux_dev->ud_bus;
414
415 if (bus->ub_usedma) {
416 usb_dma_t *dmap = &xfer->ux_dmabuf;
417
418 int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
419 if (err) {
420 return NULL;
421 }
422 xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
423 xfer->ux_bufsize = size;
424
425 return xfer->ux_buf;
426 }
427 #endif
428 KASSERT(xfer->ux_dev->ud_bus->ub_usedma == false);
429 xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
430
431 if (xfer->ux_buf != NULL) {
432 xfer->ux_bufsize = size;
433 }
434
435 return xfer->ux_buf;
436 }
437
438 void
439 usbd_free_buffer(usbd_xfer_handle xfer)
440 {
441 KASSERT(xfer->ux_buf != NULL);
442 KASSERT(xfer->ux_bufsize != 0);
443
444 void *buf = xfer->ux_buf;
445 uint32_t size = xfer->ux_bufsize;
446
447 xfer->ux_rqflags &= ~URQ_AUTO_BUFFER;
448 xfer->ux_buf = NULL;
449 xfer->ux_bufsize = 0;
450
451 #if NUSB_DMA > 0
452 struct usbd_bus *bus = xfer->ux_dev->ud_bus;
453
454 if (bus->ub_usedma) {
455 usb_dma_t *dmap = &xfer->ux_dmabuf;
456
457 usb_freemem(bus, dmap);
458 return;
459 }
460 #endif
461 KASSERT(xfer->ux_dev->ud_bus->ub_usedma == false);
462
463 kmem_free(buf, size);
464 }
465
466 void *
467 usbd_get_buffer(usbd_xfer_handle xfer)
468 {
469
470 return xfer->ux_buf;
471 }
472
473 usbd_xfer_handle
474 usbd_alloc_xfer(usbd_device_handle dev)
475 {
476 usbd_xfer_handle xfer;
477
478 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
479
480 ASSERT_SLEEPABLE();
481
482 xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus);
483 if (xfer == NULL)
484 return (NULL);
485 xfer->ux_dev = dev;
486 callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
487 cv_init(&xfer->ux_cv, "usbxfer");
488 cv_init(&xfer->ux_hccv, "usbhcxfer");
489
490 USBHIST_LOG(usbdebug, "returns %p", xfer, 0, 0, 0);
491
492 return (xfer);
493 }
494
495 usbd_status
496 usbd_free_xfer(usbd_xfer_handle xfer)
497 {
498 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
499
500 USBHIST_LOG(usbdebug, "%p", xfer, 0, 0, 0);
501 if (xfer->ux_buf) {
502 usbd_free_buffer(xfer);
503 }
504 #if defined(DIAGNOSTIC)
505 if (callout_pending(&xfer->ux_callout)) {
506 callout_stop(&xfer->ux_callout);
507 printf("usbd_free_xfer: timeout_handle pending\n");
508 }
509 #endif
510 cv_destroy(&xfer->ux_cv);
511 cv_destroy(&xfer->ux_hccv);
512 xfer->ux_dev->ud_bus->ub_methods->ubm_freex(xfer->ux_dev->ud_bus, xfer);
513 return (USBD_NORMAL_COMPLETION);
514 }
515
516 void
517 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
518 usbd_private_handle priv, void *buffer, uint32_t length,
519 uint16_t flags, uint32_t timeout,
520 usbd_callback callback)
521 {
522 xfer->ux_pipe = pipe;
523 xfer->ux_priv = priv;
524 xfer->ux_buffer = buffer;
525 xfer->ux_length = length;
526 xfer->ux_actlen = 0;
527 xfer->ux_flags = flags;
528 xfer->ux_timeout = timeout;
529 xfer->ux_status = USBD_NOT_STARTED;
530 xfer->ux_callback = callback;
531 xfer->ux_rqflags &= ~URQ_REQUEST;
532 xfer->ux_nframes = 0;
533 }
534
535 void
536 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
537 usbd_private_handle priv, uint32_t timeout,
538 usb_device_request_t *req, void *buffer,
539 uint32_t length, uint16_t flags,
540 usbd_callback callback)
541 {
542 xfer->ux_pipe = dev->ud_pipe0;
543 xfer->ux_priv = priv;
544 xfer->ux_buffer = buffer;
545 xfer->ux_length = length;
546 xfer->ux_actlen = 0;
547 xfer->ux_flags = flags;
548 xfer->ux_timeout = timeout;
549 xfer->ux_status = USBD_NOT_STARTED;
550 xfer->ux_callback = callback;
551 xfer->ux_request = *req;
552 xfer->ux_rqflags |= URQ_REQUEST;
553 xfer->ux_nframes = 0;
554 }
555
556 void
557 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
558 usbd_private_handle priv, uint16_t *frlengths,
559 uint32_t nframes, uint16_t flags, usbd_callback callback)
560 {
561 xfer->ux_pipe = pipe;
562 xfer->ux_priv = priv;
563 xfer->ux_buffer = 0;
564 xfer->ux_length = 0;
565 xfer->ux_actlen = 0;
566 xfer->ux_flags = flags;
567 xfer->ux_timeout = USBD_NO_TIMEOUT;
568 xfer->ux_status = USBD_NOT_STARTED;
569 xfer->ux_callback = callback;
570 xfer->ux_rqflags &= ~URQ_REQUEST;
571 xfer->ux_frlengths = frlengths;
572 xfer->ux_nframes = nframes;
573 }
574
575 void
576 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
577 void **buffer, uint32_t *count, usbd_status *status)
578 {
579 if (priv != NULL)
580 *priv = xfer->ux_priv;
581 if (buffer != NULL)
582 *buffer = xfer->ux_buffer;
583 if (count != NULL)
584 *count = xfer->ux_actlen;
585 if (status != NULL)
586 *status = xfer->ux_status;
587 }
588
589 usb_config_descriptor_t *
590 usbd_get_config_descriptor(usbd_device_handle dev)
591 {
592 #ifdef DIAGNOSTIC
593 if (dev == NULL) {
594 printf("usbd_get_config_descriptor: dev == NULL\n");
595 return (NULL);
596 }
597 #endif
598 return (dev->ud_cdesc);
599 }
600
601 usb_interface_descriptor_t *
602 usbd_get_interface_descriptor(usbd_interface_handle iface)
603 {
604 #ifdef DIAGNOSTIC
605 if (iface == NULL) {
606 printf("usbd_get_interface_descriptor: dev == NULL\n");
607 return (NULL);
608 }
609 #endif
610 return (iface->ui_idesc);
611 }
612
613 usb_device_descriptor_t *
614 usbd_get_device_descriptor(usbd_device_handle dev)
615 {
616 return (&dev->ud_ddesc);
617 }
618
619 usb_endpoint_descriptor_t *
620 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, uint8_t index)
621 {
622 if (index >= iface->ui_idesc->bNumEndpoints)
623 return (NULL);
624 return (iface->ui_endpoints[index].ue_edesc);
625 }
626
627 /* Some drivers may wish to abort requests on the default pipe, *
628 * but there is no mechanism for getting a handle on it. */
629 usbd_status
630 usbd_abort_default_pipe(struct usbd_device *device)
631 {
632
633 return usbd_abort_pipe(device->ud_pipe0);
634 }
635
636 usbd_status
637 usbd_abort_pipe(usbd_pipe_handle pipe)
638 {
639 usbd_status err;
640
641 #ifdef DIAGNOSTIC
642 if (pipe == NULL) {
643 printf("usbd_abort_pipe: pipe==NULL\n");
644 return (USBD_NORMAL_COMPLETION);
645 }
646 #endif
647 usbd_lock_pipe(pipe);
648 err = usbd_ar_pipe(pipe);
649 usbd_unlock_pipe(pipe);
650 return (err);
651 }
652
653 usbd_status
654 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
655 {
656 usbd_device_handle dev = pipe->up_dev;
657 usb_device_request_t req;
658 usbd_status err;
659
660 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
661
662 /*
663 * Clearing en endpoint stall resets the endpoint toggle, so
664 * do the same to the HC toggle.
665 */
666 pipe->up_methods->upm_cleartoggle(pipe);
667
668 req.bmRequestType = UT_WRITE_ENDPOINT;
669 req.bRequest = UR_CLEAR_FEATURE;
670 USETW(req.wValue, UF_ENDPOINT_HALT);
671 USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
672 USETW(req.wLength, 0);
673 err = usbd_do_request(dev, &req, 0);
674 #if 0
675 XXX should we do this?
676 if (!err) {
677 pipe->state = USBD_PIPE_ACTIVE;
678 /* XXX activate pipe */
679 }
680 #endif
681 return (err);
682 }
683
684 void
685 usbd_clear_endpoint_stall_task(void *arg)
686 {
687 usbd_pipe_handle pipe = arg;
688 usbd_device_handle dev = pipe->up_dev;
689 usb_device_request_t req;
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 (void)usbd_do_request(dev, &req, 0);
699 }
700
701 void
702 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
703 {
704
705 usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
706 }
707
708 void
709 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
710 {
711 pipe->up_methods->upm_cleartoggle(pipe);
712 }
713
714 usbd_status
715 usbd_endpoint_count(usbd_interface_handle iface, uint8_t *count)
716 {
717 #ifdef DIAGNOSTIC
718 if (iface == NULL || iface->ui_idesc == NULL) {
719 printf("usbd_endpoint_count: NULL pointer\n");
720 return (USBD_INVAL);
721 }
722 #endif
723 *count = iface->ui_idesc->bNumEndpoints;
724 return (USBD_NORMAL_COMPLETION);
725 }
726
727 usbd_status
728 usbd_interface_count(usbd_device_handle dev, uint8_t *count)
729 {
730 if (dev->ud_cdesc == NULL)
731 return (USBD_NOT_CONFIGURED);
732 *count = dev->ud_cdesc->bNumInterface;
733 return (USBD_NORMAL_COMPLETION);
734 }
735
736 void
737 usbd_interface2device_handle(usbd_interface_handle iface,
738 usbd_device_handle *dev)
739 {
740 *dev = iface->ui_dev;
741 }
742
743 usbd_status
744 usbd_device2interface_handle(usbd_device_handle dev,
745 uint8_t ifaceno, usbd_interface_handle *iface)
746 {
747 if (dev->ud_cdesc == NULL)
748 return (USBD_NOT_CONFIGURED);
749 if (ifaceno >= dev->ud_cdesc->bNumInterface)
750 return (USBD_INVAL);
751 *iface = &dev->ud_ifaces[ifaceno];
752 return (USBD_NORMAL_COMPLETION);
753 }
754
755 usbd_device_handle
756 usbd_pipe2device_handle(usbd_pipe_handle pipe)
757 {
758 return (pipe->up_dev);
759 }
760
761 /* XXXX use altno */
762 usbd_status
763 usbd_set_interface(usbd_interface_handle iface, int altidx)
764 {
765 usb_device_request_t req;
766 usbd_status err;
767 void *endpoints;
768
769 if (LIST_FIRST(&iface->ui_pipes) != 0)
770 return (USBD_IN_USE);
771
772 endpoints = iface->ui_endpoints;
773 err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
774 if (err)
775 return (err);
776
777 /* new setting works, we can free old endpoints */
778 if (endpoints != NULL) {
779 int nendpt = iface->ui_idesc->bNumEndpoints;
780 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
781 }
782
783 #ifdef DIAGNOSTIC
784 if (iface->ui_idesc == NULL) {
785 printf("usbd_set_interface: NULL pointer\n");
786 return (USBD_INVAL);
787 }
788 #endif
789
790 req.bmRequestType = UT_WRITE_INTERFACE;
791 req.bRequest = UR_SET_INTERFACE;
792 USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
793 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
794 USETW(req.wLength, 0);
795 return (usbd_do_request(iface->ui_dev, &req, 0));
796 }
797
798 int
799 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
800 {
801 char *p = (char *)cdesc;
802 char *end = p + UGETW(cdesc->wTotalLength);
803 usb_interface_descriptor_t *d;
804 int n;
805
806 for (n = 0; p < end; p += d->bLength) {
807 d = (usb_interface_descriptor_t *)p;
808 if (p + d->bLength <= end &&
809 d->bDescriptorType == UDESC_INTERFACE &&
810 d->bInterfaceNumber == ifaceno)
811 n++;
812 }
813 return (n);
814 }
815
816 int
817 usbd_get_interface_altindex(usbd_interface_handle iface)
818 {
819 return (iface->ui_altindex);
820 }
821
822 usbd_status
823 usbd_get_interface(usbd_interface_handle iface, uint8_t *aiface)
824 {
825 usb_device_request_t req;
826
827 req.bmRequestType = UT_READ_INTERFACE;
828 req.bRequest = UR_GET_INTERFACE;
829 USETW(req.wValue, 0);
830 USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
831 USETW(req.wLength, 1);
832 return (usbd_do_request(iface->ui_dev, &req, aiface));
833 }
834
835 /*** Internal routines ***/
836
837 /* Dequeue all pipe operations, called at splusb(). */
838 Static usbd_status
839 usbd_ar_pipe(usbd_pipe_handle pipe)
840 {
841 usbd_xfer_handle xfer;
842
843 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
844
845 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
846
847 USBHIST_LOG(usbdebug, "pipe = %p", pipe, 0, 0, 0);
848 #ifdef USB_DEBUG
849 if (usbdebug > 5)
850 usbd_dump_queue(pipe);
851 #endif
852 pipe->up_repeat = 0;
853 pipe->up_aborting = 1;
854 while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
855 USBHIST_LOG(usbdebug, "pipe = %p xfer = %p (methods = %p)",
856 pipe, xfer, pipe->up_methods, 0);
857 /* Make the HC abort it (and invoke the callback). */
858 pipe->up_methods->upm_abort(xfer);
859 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
860 }
861 pipe->up_aborting = 0;
862 return (USBD_NORMAL_COMPLETION);
863 }
864
865 /* Called with USB lock held. */
866 void
867 usb_transfer_complete(usbd_xfer_handle xfer)
868 {
869 usbd_pipe_handle pipe = xfer->ux_pipe;
870 struct usbd_bus *bus = pipe->up_dev->ud_bus;
871 int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
872 int erred = xfer->ux_status == USBD_CANCELLED ||
873 xfer->ux_status == USBD_TIMEOUT;
874 int polling = bus->ub_usepolling;
875 int repeat;
876
877 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
878
879 USBHIST_LOG(usbdebug, "pipe = %p xfer = %p status = %d actlen = %d",
880 pipe, xfer, xfer->ux_status, xfer->ux_actlen);
881
882 KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
883
884 #ifdef DIAGNOSTIC
885 if (xfer->ux_state != XFER_ONQU) {
886 printf("usb_transfer_complete: xfer=%p not queued 0x%08x\n",
887 xfer, xfer->ux_state);
888 }
889 #endif
890
891 #ifdef DIAGNOSTIC
892 if (pipe == NULL) {
893 printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer);
894 return;
895 }
896 #endif
897 repeat = pipe->up_repeat;
898 /* XXXX */
899 if (polling)
900 pipe->up_running = 0;
901
902 if (xfer->ux_buffer != xfer->ux_buf) {
903 /* can only be different for DMA busses */
904 KASSERT(bus->ub_usedma);
905 KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
906 "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
907
908 memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
909 }
910
911 /* XXX remove at some point */
912 /* if we allocated the buffer in usbd_transfer() we free it here. */
913 if (xfer->ux_rqflags & URQ_AUTO_BUFFER) {
914 if (!repeat) {
915 usbd_free_buffer(xfer);
916 xfer->ux_rqflags &= ~URQ_AUTO_BUFFER;
917 }
918 }
919
920 if (!repeat) {
921 /* Remove request from queue. */
922
923 KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
924 "pipe %p is empty, but xfer %p wants to complete", pipe,
925 xfer);
926 #ifdef DIAGNOSTIC
927 if (xfer != SIMPLEQ_FIRST(&pipe->up_queue))
928 printf("%s: bad dequeue %p != %p\n", __func__,
929 xfer, SIMPLEQ_FIRST(&pipe->up_queue));
930 xfer->ux_state = XFER_BUSY;
931 #endif
932 SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
933 }
934 USBHIST_LOG(usbdebug, "xfer %p: repeat %d new head = %p",
935 xfer, repeat, SIMPLEQ_FIRST(&pipe->up_queue), 0);
936
937 /* Count completed transfers. */
938 ++pipe->up_dev->ud_bus->ub_stats.uds_requests
939 [pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
940
941 xfer->ux_done = 1;
942 if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
943 !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
944 USBHIST_LOG(usbdebug, "short transfer %d < %d",
945 xfer->ux_actlen, xfer->ux_length, 0, 0);
946 xfer->ux_status = USBD_SHORT_XFER;
947 }
948
949 if (repeat) {
950 USBHIST_LOG(usbdebug, "xfer %p doing callback %p status %x",
951 xfer, xfer->ux_callback, xfer->ux_status, 0);
952 if (xfer->ux_callback) {
953 if (!polling)
954 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
955
956 if (!(pipe->up_flags & USBD_MPSAFE))
957 KERNEL_LOCK(1, curlwp);
958 xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
959 USBHIST_LOG(usbdebug, "xfer %p doing done %p", xfer,
960 pipe->up_methods->upm_done, 0, 0);
961 if (!(pipe->up_flags & USBD_MPSAFE))
962 KERNEL_UNLOCK_ONE(curlwp);
963
964 if (!polling)
965 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
966 }
967 pipe->up_methods->upm_done(xfer);
968 } else {
969 USBHIST_LOG(usbdebug, "xfer %p doing done %p", xfer,
970 pipe->up_methods->upm_done, 0, 0);
971 pipe->up_methods->upm_done(xfer);
972 USBHIST_LOG(usbdebug, "xfer %p doing callback %p status %x",
973 xfer, xfer->ux_callback, xfer->ux_status, 0);
974 if (xfer->ux_callback) {
975 if (!polling)
976 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
977
978 if (!(pipe->up_flags & USBD_MPSAFE))
979 KERNEL_LOCK(1, curlwp);
980 xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
981 if (!(pipe->up_flags & USBD_MPSAFE))
982 KERNEL_UNLOCK_ONE(curlwp);
983
984 if (!polling)
985 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
986 }
987 }
988
989 if (sync && !polling) {
990 USBHIST_LOG(usbdebug, "<- done xfer %p, wakeup", xfer, 0, 0, 0);
991 cv_broadcast(&xfer->ux_cv);
992 }
993
994 if (!repeat) {
995 /* XXX should we stop the queue on all errors? */
996 if (erred && pipe->up_iface != NULL) /* not control pipe */
997 pipe->up_running = 0;
998 else
999 usbd_start_next(pipe);
1000 }
1001 }
1002
1003 /* Called with USB lock held. */
1004 usbd_status
1005 usb_insert_transfer(usbd_xfer_handle xfer)
1006 {
1007 usbd_pipe_handle pipe = xfer->ux_pipe;
1008 usbd_status err;
1009
1010 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1011
1012 USBHIST_LOG(usbdebug, "pipe = %p running = %d timeout = %d",
1013 pipe, pipe->up_running, xfer->ux_timeout, 0);
1014
1015 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1016
1017 #ifdef DIAGNOSTIC
1018 if (xfer->ux_state != XFER_BUSY) {
1019 USBHIST_LOG(usbdebug, "<- done, xfer %p not busy", xfer, 0, 0,
1020 0);
1021 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
1022 xfer, xfer->ux_state);
1023 return (USBD_INVAL);
1024 }
1025 xfer->ux_state = XFER_ONQU;
1026 #endif
1027 SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
1028 if (pipe->up_running)
1029 err = USBD_IN_PROGRESS;
1030 else {
1031 pipe->up_running = 1;
1032 err = USBD_NORMAL_COMPLETION;
1033 }
1034 USBHIST_LOG(usbdebug, "<- done xfer %p, err %d", xfer, err, 0, 0);
1035 return (err);
1036 }
1037
1038 /* Called with USB lock held. */
1039 void
1040 usbd_start_next(usbd_pipe_handle pipe)
1041 {
1042 usbd_xfer_handle xfer;
1043 usbd_status err;
1044
1045 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1046
1047 #ifdef DIAGNOSTIC
1048 if (pipe == NULL) {
1049 printf("usbd_start_next: pipe == NULL\n");
1050 return;
1051 }
1052 if (pipe->up_methods == NULL || pipe->up_methods->upm_start == NULL) {
1053 printf("usbd_start_next: pipe=%p no start method\n", pipe);
1054 return;
1055 }
1056 #endif
1057
1058 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1059
1060 /* Get next request in queue. */
1061 xfer = SIMPLEQ_FIRST(&pipe->up_queue);
1062 USBHIST_LOG(usbdebug, "pipe = %p, xfer = %p", pipe, xfer, 0, 0);
1063 if (xfer == NULL) {
1064 pipe->up_running = 0;
1065 } else {
1066 mutex_exit(pipe->up_dev->ud_bus->ub_lock);
1067 err = pipe->up_methods->upm_start(xfer);
1068 mutex_enter(pipe->up_dev->ud_bus->ub_lock);
1069
1070 if (err != USBD_IN_PROGRESS) {
1071 USBHIST_LOG(usbdebug, "error = %d", err, 0, 0, 0);
1072 pipe->up_running = 0;
1073 /* XXX do what? */
1074 }
1075 }
1076
1077 KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
1078 }
1079
1080 usbd_status
1081 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
1082 {
1083 return (usbd_do_request_flags(dev, req, data, 0, 0,
1084 USBD_DEFAULT_TIMEOUT));
1085 }
1086
1087 usbd_status
1088 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
1089 void *data, uint16_t flags, int *actlen, uint32_t timo)
1090 {
1091 return (usbd_do_request_flags_pipe(dev, dev->ud_pipe0, req,
1092 data, flags, actlen, timo));
1093 }
1094
1095 usbd_status
1096 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
1097 usb_device_request_t *req, void *data, uint16_t flags, int *actlen,
1098 uint32_t timeout)
1099 {
1100 usbd_xfer_handle xfer;
1101 usbd_status err;
1102
1103 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1104
1105 ASSERT_SLEEPABLE();
1106
1107 xfer = usbd_alloc_xfer(dev);
1108 if (xfer == NULL)
1109 return (USBD_NOMEM);
1110 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
1111 data, UGETW(req->wLength), flags, 0);
1112 xfer->ux_pipe = pipe;
1113 err = usbd_sync_transfer(xfer);
1114 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1115 if (xfer->ux_actlen > xfer->ux_length) {
1116 USBHIST_LOG(usbdebug, "overrun addr = %d type = 0x%02x",
1117 dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
1118 USBHIST_LOG(usbdebug, " req = 0x%02x val = %d index = %d",
1119 xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
1120 UGETW(xfer->ux_request.wIndex), 0);
1121 USBHIST_LOG(usbdebug, " rlen = %d length = %d actlen = %d",
1122 UGETW(xfer->ux_request.wLength),
1123 xfer->ux_length, xfer->ux_actlen, 0);
1124 }
1125 #endif
1126 if (actlen != NULL)
1127 *actlen = xfer->ux_actlen;
1128 if (err == USBD_STALLED) {
1129 /*
1130 * The control endpoint has stalled. Control endpoints
1131 * should not halt, but some may do so anyway so clear
1132 * any halt condition.
1133 */
1134 usb_device_request_t treq;
1135 usb_status_t status;
1136 uint16_t s;
1137 usbd_status nerr;
1138
1139 treq.bmRequestType = UT_READ_ENDPOINT;
1140 treq.bRequest = UR_GET_STATUS;
1141 USETW(treq.wValue, 0);
1142 USETW(treq.wIndex, 0);
1143 USETW(treq.wLength, sizeof(usb_status_t));
1144 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1145 &treq, &status,sizeof(usb_status_t),
1146 0, 0);
1147 nerr = usbd_sync_transfer(xfer);
1148 if (nerr)
1149 goto bad;
1150 s = UGETW(status.wStatus);
1151 USBHIST_LOG(usbdebug, "status = 0x%04x", s, 0, 0, 0);
1152 if (!(s & UES_HALT))
1153 goto bad;
1154 treq.bmRequestType = UT_WRITE_ENDPOINT;
1155 treq.bRequest = UR_CLEAR_FEATURE;
1156 USETW(treq.wValue, UF_ENDPOINT_HALT);
1157 USETW(treq.wIndex, 0);
1158 USETW(treq.wLength, 0);
1159 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
1160 &treq, &status, 0, 0, 0);
1161 nerr = usbd_sync_transfer(xfer);
1162 if (nerr)
1163 goto bad;
1164 }
1165
1166 bad:
1167 if (err) {
1168 USBHIST_LOG(usbdebug, "returning err = %s",
1169 usbd_errstr(err), 0, 0, 0);
1170 }
1171 usbd_free_xfer(xfer);
1172 return (err);
1173 }
1174
1175 const struct usbd_quirks *
1176 usbd_get_quirks(usbd_device_handle dev)
1177 {
1178 #ifdef DIAGNOSTIC
1179 if (dev == NULL) {
1180 printf("usbd_get_quirks: dev == NULL\n");
1181 return 0;
1182 }
1183 #endif
1184 return (dev->ud_quirks);
1185 }
1186
1187 /* XXX do periodic free() of free list */
1188
1189 /*
1190 * Called from keyboard driver when in polling mode.
1191 */
1192 void
1193 usbd_dopoll(usbd_interface_handle iface)
1194 {
1195 iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
1196 }
1197
1198 /*
1199 * XXX use this more??? ub_usepolling it touched manually all over
1200 */
1201 void
1202 usbd_set_polling(usbd_device_handle 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(usbd_interface_handle 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 continuosly 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(usbd_device_handle 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(usbd_device_handle dev, int si, char *buf)
1297 {
1298 return usbd_get_string0(dev, si, buf, 1);
1299 }
1300
1301 usbd_status
1302 usbd_get_string0(usbd_device_handle 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