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