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