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