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