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