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