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