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