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