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