No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / usb / usbdi.c
blob25c1c136b065738a45ec1436e7445d7596fa0254
1 /* $NetBSD: usbdi.c,v 1.125 2008/12/12 05:35:11 jmorse Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.28 1999/11/17 22:33:49 n_hibma Exp $ */
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.125 2008/12/12 05:35:11 jmorse Exp $");
37 #include "opt_compat_netbsd.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
46 #include <sys/bus.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usbdivar.h>
52 #include <dev/usb/usb_mem.h>
53 #include <dev/usb/usb_quirks.h>
55 /* UTF-8 encoding stuff */
56 #include <fs/unicode.h>
58 #ifdef USB_DEBUG
59 #define DPRINTF(x) if (usbdebug) logprintf x
60 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
61 extern int usbdebug;
62 #else
63 #define DPRINTF(x)
64 #define DPRINTFN(n,x)
65 #endif
67 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
68 Static void usbd_do_request_async_cb
69 (usbd_xfer_handle, usbd_private_handle, usbd_status);
70 Static void usbd_start_next(usbd_pipe_handle pipe);
71 Static usbd_status usbd_open_pipe_ival
72 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
74 static inline int
75 usbd_xfer_isread(usbd_xfer_handle xfer)
77 if (xfer->rqflags & URQ_REQUEST)
78 return (xfer->request.bmRequestType & UT_READ);
79 else
80 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
81 UE_DIR_IN);
84 #ifdef USB_DEBUG
85 void
86 usbd_dump_iface(struct usbd_interface *iface)
88 printf("usbd_dump_iface: iface=%p\n", iface);
89 if (iface == NULL)
90 return;
91 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
92 iface->device, iface->idesc, iface->index, iface->altindex,
93 iface->priv);
96 void
97 usbd_dump_device(struct usbd_device *dev)
99 printf("usbd_dump_device: dev=%p\n", dev);
100 if (dev == NULL)
101 return;
102 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
103 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
104 "power=%d langid=%d\n",
105 dev->address, dev->config, dev->depth, dev->speed,
106 dev->self_powered, dev->power, dev->langid);
109 void
110 usbd_dump_endpoint(struct usbd_endpoint *endp)
112 printf("usbd_dump_endpoint: endp=%p\n", endp);
113 if (endp == NULL)
114 return;
115 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
116 if (endp->edesc)
117 printf(" bEndpointAddress=0x%02x\n",
118 endp->edesc->bEndpointAddress);
121 void
122 usbd_dump_queue(usbd_pipe_handle pipe)
124 usbd_xfer_handle xfer;
126 printf("usbd_dump_queue: pipe=%p\n", pipe);
127 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
128 printf(" xfer=%p\n", xfer);
132 void
133 usbd_dump_pipe(usbd_pipe_handle pipe)
135 printf("usbd_dump_pipe: pipe=%p\n", pipe);
136 if (pipe == NULL)
137 return;
138 usbd_dump_iface(pipe->iface);
139 usbd_dump_device(pipe->device);
140 usbd_dump_endpoint(pipe->endpoint);
141 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
142 pipe->refcnt, pipe->running, pipe->aborting);
143 printf(" intrxfer=%p, repeat=%d, interval=%d\n",
144 pipe->intrxfer, pipe->repeat, pipe->interval);
146 #endif
148 usbd_status
149 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
150 u_int8_t flags, usbd_pipe_handle *pipe)
152 return (usbd_open_pipe_ival(iface, address, flags, pipe,
153 USBD_DEFAULT_INTERVAL));
156 usbd_status
157 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
158 u_int8_t flags, usbd_pipe_handle *pipe, int ival)
160 usbd_pipe_handle p;
161 struct usbd_endpoint *ep;
162 usbd_status err;
163 int i;
165 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
166 iface, address, flags));
168 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
169 ep = &iface->endpoints[i];
170 if (ep->edesc == NULL)
171 return (USBD_IOERROR);
172 if (ep->edesc->bEndpointAddress == address)
173 goto found;
175 return (USBD_BAD_ADDRESS);
176 found:
177 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
178 return (USBD_IN_USE);
179 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
180 if (err)
181 return (err);
182 LIST_INSERT_HEAD(&iface->pipes, p, next);
183 *pipe = p;
184 return (USBD_NORMAL_COMPLETION);
187 usbd_status
188 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
189 u_int8_t flags, usbd_pipe_handle *pipe,
190 usbd_private_handle priv, void *buffer, u_int32_t len,
191 usbd_callback cb, int ival)
193 usbd_status err;
194 usbd_xfer_handle xfer;
195 usbd_pipe_handle ipipe;
197 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
198 address, flags, len));
200 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
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;
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);
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);
228 usbd_status
229 usbd_close_pipe(usbd_pipe_handle pipe)
231 #ifdef DIAGNOSTIC
232 if (pipe == NULL) {
233 printf("usbd_close_pipe: pipe==NULL\n");
234 return (USBD_NORMAL_COMPLETION);
236 #endif
238 if (--pipe->refcnt != 0)
239 return (USBD_NORMAL_COMPLETION);
240 if (! SIMPLEQ_EMPTY(&pipe->queue))
241 return (USBD_PENDING_REQUESTS);
242 LIST_REMOVE(pipe, next);
243 pipe->endpoint->refcnt--;
244 pipe->methods->close(pipe);
245 if (pipe->intrxfer != NULL)
246 usbd_free_xfer(pipe->intrxfer);
247 free(pipe, M_USB);
248 return (USBD_NORMAL_COMPLETION);
251 usbd_status
252 usbd_transfer(usbd_xfer_handle xfer)
254 usbd_pipe_handle pipe = xfer->pipe;
255 usb_dma_t *dmap = &xfer->dmabuf;
256 usbd_status err;
257 unsigned int size, flags;
258 int s;
260 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n",
261 xfer, xfer->flags, pipe, pipe->running));
262 #ifdef USB_DEBUG
263 if (usbdebug > 5)
264 usbd_dump_queue(pipe);
265 #endif
266 xfer->done = 0;
268 if (pipe->aborting)
269 return (USBD_CANCELLED);
271 size = xfer->length;
272 /* If there is no buffer, allocate one. */
273 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
274 struct usbd_bus *bus = pipe->device->bus;
276 #ifdef DIAGNOSTIC
277 if (xfer->rqflags & URQ_AUTO_DMABUF)
278 printf("usbd_transfer: has old buffer!\n");
279 #endif
280 err = bus->methods->allocm(bus, dmap, size);
281 if (err)
282 return (err);
283 xfer->rqflags |= URQ_AUTO_DMABUF;
286 flags = xfer->flags;
288 /* Copy data if going out. */
289 if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer))
290 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
292 /* xfer is not valid after the transfer method unless synchronous */
293 err = pipe->methods->transfer(xfer);
295 if (err != USBD_IN_PROGRESS && err) {
296 /* The transfer has not been queued, so free buffer. */
297 if (xfer->rqflags & URQ_AUTO_DMABUF) {
298 struct usbd_bus *bus = pipe->device->bus;
300 bus->methods->freem(bus, &xfer->dmabuf);
301 xfer->rqflags &= ~URQ_AUTO_DMABUF;
305 if (!(flags & USBD_SYNCHRONOUS))
306 return (err);
308 /* Sync transfer, wait for completion. */
309 if (err != USBD_IN_PROGRESS)
310 return (err);
311 s = splusb();
312 if (!xfer->done) {
313 if (pipe->device->bus->use_polling)
314 panic("usbd_transfer: not done");
315 tsleep(xfer, PRIBIO, "usbsyn", 0);
317 splx(s);
318 return (xfer->status);
321 /* Like usbd_transfer(), but waits for completion. */
322 usbd_status
323 usbd_sync_transfer(usbd_xfer_handle xfer)
325 xfer->flags |= USBD_SYNCHRONOUS;
326 return (usbd_transfer(xfer));
329 void *
330 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
332 struct usbd_bus *bus = xfer->device->bus;
333 usbd_status err;
335 #ifdef DIAGNOSTIC
336 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
337 printf("usbd_alloc_buffer: xfer already has a buffer\n");
338 #endif
339 err = bus->methods->allocm(bus, &xfer->dmabuf, size);
340 if (err)
341 return (NULL);
342 xfer->rqflags |= URQ_DEV_DMABUF;
343 return (KERNADDR(&xfer->dmabuf, 0));
346 void
347 usbd_free_buffer(usbd_xfer_handle xfer)
349 #ifdef DIAGNOSTIC
350 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
351 printf("usbd_free_buffer: no buffer\n");
352 return;
354 #endif
355 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
356 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
359 void *
360 usbd_get_buffer(usbd_xfer_handle xfer)
362 if (!(xfer->rqflags & URQ_DEV_DMABUF))
363 return (0);
364 return (KERNADDR(&xfer->dmabuf, 0));
367 usbd_xfer_handle
368 usbd_alloc_xfer(usbd_device_handle dev)
370 usbd_xfer_handle xfer;
372 xfer = dev->bus->methods->allocx(dev->bus);
373 if (xfer == NULL)
374 return (NULL);
375 xfer->device = dev;
376 usb_callout_init(xfer->timeout_handle);
377 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
378 return (xfer);
381 usbd_status
382 usbd_free_xfer(usbd_xfer_handle xfer)
384 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
385 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
386 usbd_free_buffer(xfer);
387 #if defined(DIAGNOSTIC)
388 if (callout_pending(&xfer->timeout_handle)) {
389 callout_stop(&xfer->timeout_handle);
390 printf("usbd_free_xfer: timout_handle pending");
392 #endif
393 xfer->device->bus->methods->freex(xfer->device->bus, xfer);
394 return (USBD_NORMAL_COMPLETION);
397 void
398 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
399 usbd_private_handle priv, void *buffer, u_int32_t length,
400 u_int16_t flags, u_int32_t timeout,
401 usbd_callback callback)
403 xfer->pipe = pipe;
404 xfer->priv = priv;
405 xfer->buffer = buffer;
406 xfer->length = length;
407 xfer->actlen = 0;
408 xfer->flags = flags;
409 xfer->timeout = timeout;
410 xfer->status = USBD_NOT_STARTED;
411 xfer->callback = callback;
412 xfer->rqflags &= ~URQ_REQUEST;
413 xfer->nframes = 0;
416 void
417 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
418 usbd_private_handle priv, u_int32_t timeout,
419 usb_device_request_t *req, void *buffer,
420 u_int32_t length, u_int16_t flags,
421 usbd_callback callback)
423 xfer->pipe = dev->default_pipe;
424 xfer->priv = priv;
425 xfer->buffer = buffer;
426 xfer->length = length;
427 xfer->actlen = 0;
428 xfer->flags = flags;
429 xfer->timeout = timeout;
430 xfer->status = USBD_NOT_STARTED;
431 xfer->callback = callback;
432 xfer->request = *req;
433 xfer->rqflags |= URQ_REQUEST;
434 xfer->nframes = 0;
437 void
438 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
439 usbd_private_handle priv, u_int16_t *frlengths,
440 u_int32_t nframes, u_int16_t flags, usbd_callback callback)
442 xfer->pipe = pipe;
443 xfer->priv = priv;
444 xfer->buffer = 0;
445 xfer->length = 0;
446 xfer->actlen = 0;
447 xfer->flags = flags;
448 xfer->timeout = USBD_NO_TIMEOUT;
449 xfer->status = USBD_NOT_STARTED;
450 xfer->callback = callback;
451 xfer->rqflags &= ~URQ_REQUEST;
452 xfer->frlengths = frlengths;
453 xfer->nframes = nframes;
456 void
457 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
458 void **buffer, u_int32_t *count, usbd_status *status)
460 if (priv != NULL)
461 *priv = xfer->priv;
462 if (buffer != NULL)
463 *buffer = xfer->buffer;
464 if (count != NULL)
465 *count = xfer->actlen;
466 if (status != NULL)
467 *status = xfer->status;
470 usb_config_descriptor_t *
471 usbd_get_config_descriptor(usbd_device_handle dev)
473 #ifdef DIAGNOSTIC
474 if (dev == NULL) {
475 printf("usbd_get_config_descriptor: dev == NULL\n");
476 return (NULL);
478 #endif
479 return (dev->cdesc);
482 usb_interface_descriptor_t *
483 usbd_get_interface_descriptor(usbd_interface_handle iface)
485 #ifdef DIAGNOSTIC
486 if (iface == NULL) {
487 printf("usbd_get_interface_descriptor: dev == NULL\n");
488 return (NULL);
490 #endif
491 return (iface->idesc);
494 usb_device_descriptor_t *
495 usbd_get_device_descriptor(usbd_device_handle dev)
497 return (&dev->ddesc);
500 usb_endpoint_descriptor_t *
501 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
503 if (index >= iface->idesc->bNumEndpoints)
504 return (0);
505 return (iface->endpoints[index].edesc);
508 /* Some drivers may wish to abort requests on the default pipe, *
509 * but there is no mechanism for getting a handle on it. */
510 usbd_status
511 usbd_abort_default_pipe(struct usbd_device *device)
514 return usbd_abort_pipe(device->default_pipe);
517 usbd_status
518 usbd_abort_pipe(usbd_pipe_handle pipe)
520 usbd_status err;
521 int s;
523 #ifdef DIAGNOSTIC
524 if (pipe == NULL) {
525 printf("usbd_abort_pipe: pipe==NULL\n");
526 return (USBD_NORMAL_COMPLETION);
528 #endif
529 s = splusb();
530 err = usbd_ar_pipe(pipe);
531 splx(s);
532 return (err);
535 usbd_status
536 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
538 usbd_device_handle dev = pipe->device;
539 usb_device_request_t req;
540 usbd_status err;
542 DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
545 * Clearing en endpoint stall resets the endpoint toggle, so
546 * do the same to the HC toggle.
548 pipe->methods->cleartoggle(pipe);
550 req.bmRequestType = UT_WRITE_ENDPOINT;
551 req.bRequest = UR_CLEAR_FEATURE;
552 USETW(req.wValue, UF_ENDPOINT_HALT);
553 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
554 USETW(req.wLength, 0);
555 err = usbd_do_request(dev, &req, 0);
556 #if 0
557 XXX should we do this?
558 if (!err) {
559 pipe->state = USBD_PIPE_ACTIVE;
560 /* XXX activate pipe */
562 #endif
563 return (err);
566 usbd_status
567 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
569 usbd_device_handle dev = pipe->device;
570 usb_device_request_t req;
571 usbd_status err;
573 pipe->methods->cleartoggle(pipe);
575 req.bmRequestType = UT_WRITE_ENDPOINT;
576 req.bRequest = UR_CLEAR_FEATURE;
577 USETW(req.wValue, UF_ENDPOINT_HALT);
578 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
579 USETW(req.wLength, 0);
580 err = usbd_do_request_async(dev, &req, 0);
581 return (err);
584 void
585 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
587 pipe->methods->cleartoggle(pipe);
590 usbd_status
591 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
593 #ifdef DIAGNOSTIC
594 if (iface == NULL || iface->idesc == NULL) {
595 printf("usbd_endpoint_count: NULL pointer\n");
596 return (USBD_INVAL);
598 #endif
599 *count = iface->idesc->bNumEndpoints;
600 return (USBD_NORMAL_COMPLETION);
603 usbd_status
604 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
606 if (dev->cdesc == NULL)
607 return (USBD_NOT_CONFIGURED);
608 *count = dev->cdesc->bNumInterface;
609 return (USBD_NORMAL_COMPLETION);
612 void
613 usbd_interface2device_handle(usbd_interface_handle iface,
614 usbd_device_handle *dev)
616 *dev = iface->device;
619 usbd_status
620 usbd_device2interface_handle(usbd_device_handle dev,
621 u_int8_t ifaceno, usbd_interface_handle *iface)
623 if (dev->cdesc == NULL)
624 return (USBD_NOT_CONFIGURED);
625 if (ifaceno >= dev->cdesc->bNumInterface)
626 return (USBD_INVAL);
627 *iface = &dev->ifaces[ifaceno];
628 return (USBD_NORMAL_COMPLETION);
631 usbd_device_handle
632 usbd_pipe2device_handle(usbd_pipe_handle pipe)
634 return (pipe->device);
637 /* XXXX use altno */
638 usbd_status
639 usbd_set_interface(usbd_interface_handle iface, int altidx)
641 usb_device_request_t req;
642 usbd_status err;
643 void *endpoints;
645 if (LIST_FIRST(&iface->pipes) != 0)
646 return (USBD_IN_USE);
648 endpoints = iface->endpoints;
649 err = usbd_fill_iface_data(iface->device, iface->index, altidx);
650 if (err)
651 return (err);
653 /* new setting works, we can free old endpoints */
654 if (endpoints != NULL)
655 free(endpoints, M_USB);
657 #ifdef DIAGNOSTIC
658 if (iface->idesc == NULL) {
659 printf("usbd_set_interface: NULL pointer\n");
660 return (USBD_INVAL);
662 #endif
664 req.bmRequestType = UT_WRITE_INTERFACE;
665 req.bRequest = UR_SET_INTERFACE;
666 USETW(req.wValue, iface->idesc->bAlternateSetting);
667 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
668 USETW(req.wLength, 0);
669 return (usbd_do_request(iface->device, &req, 0));
673 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
675 char *p = (char *)cdesc;
676 char *end = p + UGETW(cdesc->wTotalLength);
677 usb_interface_descriptor_t *d;
678 int n;
680 for (n = 0; p < end; p += d->bLength) {
681 d = (usb_interface_descriptor_t *)p;
682 if (p + d->bLength <= end &&
683 d->bDescriptorType == UDESC_INTERFACE &&
684 d->bInterfaceNumber == ifaceno)
685 n++;
687 return (n);
691 usbd_get_interface_altindex(usbd_interface_handle iface)
693 return (iface->altindex);
696 usbd_status
697 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
699 usb_device_request_t req;
701 req.bmRequestType = UT_READ_INTERFACE;
702 req.bRequest = UR_GET_INTERFACE;
703 USETW(req.wValue, 0);
704 USETW(req.wIndex, iface->idesc->bInterfaceNumber);
705 USETW(req.wLength, 1);
706 return (usbd_do_request(iface->device, &req, aiface));
709 /*** Internal routines ***/
711 /* Dequeue all pipe operations, called at splusb(). */
712 Static usbd_status
713 usbd_ar_pipe(usbd_pipe_handle pipe)
715 usbd_xfer_handle xfer;
717 SPLUSBCHECK;
719 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
720 #ifdef USB_DEBUG
721 if (usbdebug > 5)
722 usbd_dump_queue(pipe);
723 #endif
724 pipe->repeat = 0;
725 pipe->aborting = 1;
726 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
727 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
728 pipe, xfer, pipe->methods));
729 /* Make the HC abort it (and invoke the callback). */
730 pipe->methods->abort(xfer);
731 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
733 pipe->aborting = 0;
734 return (USBD_NORMAL_COMPLETION);
737 /* Called at splusb() */
738 void
739 usb_transfer_complete(usbd_xfer_handle xfer)
741 usbd_pipe_handle pipe = xfer->pipe;
742 usb_dma_t *dmap = &xfer->dmabuf;
743 int sync = xfer->flags & USBD_SYNCHRONOUS;
744 int erred = xfer->status == USBD_CANCELLED ||
745 xfer->status == USBD_TIMEOUT;
746 int repeat, polling;
748 SPLUSBCHECK;
750 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
751 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
752 #ifdef DIAGNOSTIC
753 if (xfer->busy_free != XFER_ONQU) {
754 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
755 xfer, xfer->busy_free);
757 #endif
759 #ifdef DIAGNOSTIC
760 if (pipe == NULL) {
761 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
762 return;
764 #endif
765 repeat = pipe->repeat;
766 polling = pipe->device->bus->use_polling;
767 /* XXXX */
768 if (polling)
769 pipe->running = 0;
771 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
772 usbd_xfer_isread(xfer)) {
773 #ifdef DIAGNOSTIC
774 if (xfer->actlen > xfer->length) {
775 printf("usb_transfer_complete: actlen > len %d > %d\n",
776 xfer->actlen, xfer->length);
777 xfer->actlen = xfer->length;
779 #endif
780 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
783 /* if we allocated the buffer in usbd_transfer() we free it here. */
784 if (xfer->rqflags & URQ_AUTO_DMABUF) {
785 if (!repeat) {
786 struct usbd_bus *bus = pipe->device->bus;
787 bus->methods->freem(bus, dmap);
788 xfer->rqflags &= ~URQ_AUTO_DMABUF;
792 if (!repeat) {
793 /* Remove request from queue. */
794 #ifdef DIAGNOSTIC
795 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
796 printf("usb_transfer_complete: bad dequeue %p != %p\n",
797 xfer, SIMPLEQ_FIRST(&pipe->queue));
798 xfer->busy_free = XFER_BUSY;
799 #endif
800 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
802 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
803 repeat, SIMPLEQ_FIRST(&pipe->queue)));
805 /* Count completed transfers. */
806 ++pipe->device->bus->stats.uds_requests
807 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
809 xfer->done = 1;
810 if (!xfer->status && xfer->actlen < xfer->length &&
811 !(xfer->flags & USBD_SHORT_XFER_OK)) {
812 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
813 xfer->actlen, xfer->length));
814 xfer->status = USBD_SHORT_XFER;
817 if (repeat) {
818 if (xfer->callback)
819 xfer->callback(xfer, xfer->priv, xfer->status);
820 pipe->methods->done(xfer);
821 } else {
822 pipe->methods->done(xfer);
823 if (xfer->callback)
824 xfer->callback(xfer, xfer->priv, xfer->status);
827 if (sync && !polling)
828 wakeup(xfer);
830 if (!repeat) {
831 /* XXX should we stop the queue on all errors? */
832 if (erred && pipe->iface != NULL) /* not control pipe */
833 pipe->running = 0;
834 else
835 usbd_start_next(pipe);
839 usbd_status
840 usb_insert_transfer(usbd_xfer_handle xfer)
842 usbd_pipe_handle pipe = xfer->pipe;
843 usbd_status err;
844 int s;
846 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
847 pipe, pipe->running, xfer->timeout));
848 #ifdef DIAGNOSTIC
849 if (xfer->busy_free != XFER_BUSY) {
850 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
851 xfer, xfer->busy_free);
852 return (USBD_INVAL);
854 xfer->busy_free = XFER_ONQU;
855 #endif
856 s = splusb();
857 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
858 if (pipe->running)
859 err = USBD_IN_PROGRESS;
860 else {
861 pipe->running = 1;
862 err = USBD_NORMAL_COMPLETION;
864 splx(s);
865 return (err);
868 /* Called at splusb() */
869 void
870 usbd_start_next(usbd_pipe_handle pipe)
872 usbd_xfer_handle xfer;
873 usbd_status err;
875 SPLUSBCHECK;
877 #ifdef DIAGNOSTIC
878 if (pipe == NULL) {
879 printf("usbd_start_next: pipe == NULL\n");
880 return;
882 if (pipe->methods == NULL || pipe->methods->start == NULL) {
883 printf("usbd_start_next: pipe=%p no start method\n", pipe);
884 return;
886 #endif
888 /* Get next request in queue. */
889 xfer = SIMPLEQ_FIRST(&pipe->queue);
890 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
891 if (xfer == NULL) {
892 pipe->running = 0;
893 } else {
894 err = pipe->methods->start(xfer);
895 if (err != USBD_IN_PROGRESS) {
896 printf("usbd_start_next: error=%d\n", err);
897 pipe->running = 0;
898 /* XXX do what? */
903 usbd_status
904 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
906 return (usbd_do_request_flags(dev, req, data, 0, 0,
907 USBD_DEFAULT_TIMEOUT));
910 usbd_status
911 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
912 void *data, u_int16_t flags, int *actlen, u_int32_t timo)
914 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
915 data, flags, actlen, timo));
918 usbd_status
919 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
920 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
921 u_int32_t timeout)
923 usbd_xfer_handle xfer;
924 usbd_status err;
926 #ifdef DIAGNOSTIC
927 if (dev->bus->intr_context) {
928 printf("usbd_do_request: not in process context\n");
929 return (USBD_INVAL);
931 #endif
933 xfer = usbd_alloc_xfer(dev);
934 if (xfer == NULL)
935 return (USBD_NOMEM);
936 usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
937 data, UGETW(req->wLength), flags, 0);
938 xfer->pipe = pipe;
939 err = usbd_sync_transfer(xfer);
940 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
941 if (xfer->actlen > xfer->length) {
942 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
943 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
944 dev->address, xfer->request.bmRequestType,
945 xfer->request.bRequest, UGETW(xfer->request.wValue),
946 UGETW(xfer->request.wIndex),
947 UGETW(xfer->request.wLength),
948 xfer->length, xfer->actlen));
950 #endif
951 if (actlen != NULL)
952 *actlen = xfer->actlen;
953 if (err == USBD_STALLED) {
955 * The control endpoint has stalled. Control endpoints
956 * should not halt, but some may do so anyway so clear
957 * any halt condition.
959 usb_device_request_t treq;
960 usb_status_t status;
961 u_int16_t s;
962 usbd_status nerr;
964 treq.bmRequestType = UT_READ_ENDPOINT;
965 treq.bRequest = UR_GET_STATUS;
966 USETW(treq.wValue, 0);
967 USETW(treq.wIndex, 0);
968 USETW(treq.wLength, sizeof(usb_status_t));
969 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
970 &treq, &status,sizeof(usb_status_t),
971 0, 0);
972 nerr = usbd_sync_transfer(xfer);
973 if (nerr)
974 goto bad;
975 s = UGETW(status.wStatus);
976 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
977 if (!(s & UES_HALT))
978 goto bad;
979 treq.bmRequestType = UT_WRITE_ENDPOINT;
980 treq.bRequest = UR_CLEAR_FEATURE;
981 USETW(treq.wValue, UF_ENDPOINT_HALT);
982 USETW(treq.wIndex, 0);
983 USETW(treq.wLength, 0);
984 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
985 &treq, &status, 0, 0, 0);
986 nerr = usbd_sync_transfer(xfer);
987 if (nerr)
988 goto bad;
991 bad:
992 usbd_free_xfer(xfer);
993 return (err);
996 void
997 usbd_do_request_async_cb(usbd_xfer_handle xfer,
998 usbd_private_handle priv, usbd_status status)
1000 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
1001 if (xfer->actlen > xfer->length) {
1002 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
1003 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
1004 xfer->pipe->device->address,
1005 xfer->request.bmRequestType,
1006 xfer->request.bRequest, UGETW(xfer->request.wValue),
1007 UGETW(xfer->request.wIndex),
1008 UGETW(xfer->request.wLength),
1009 xfer->length, xfer->actlen));
1011 #endif
1012 usbd_free_xfer(xfer);
1016 * Execute a request without waiting for completion.
1017 * Can be used from interrupt context.
1019 usbd_status
1020 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
1021 void *data)
1023 usbd_xfer_handle xfer;
1024 usbd_status err;
1026 xfer = usbd_alloc_xfer(dev);
1027 if (xfer == NULL)
1028 return (USBD_NOMEM);
1029 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
1030 data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
1031 err = usbd_transfer(xfer);
1032 if (err != USBD_IN_PROGRESS) {
1033 usbd_free_xfer(xfer);
1034 return (err);
1036 return (USBD_NORMAL_COMPLETION);
1039 const struct usbd_quirks *
1040 usbd_get_quirks(usbd_device_handle dev)
1042 #ifdef DIAGNOSTIC
1043 if (dev == NULL) {
1044 printf("usbd_get_quirks: dev == NULL\n");
1045 return 0;
1047 #endif
1048 return (dev->quirks);
1051 /* XXX do periodic free() of free list */
1054 * Called from keyboard driver when in polling mode.
1056 void
1057 usbd_dopoll(usbd_interface_handle iface)
1059 iface->device->bus->methods->do_poll(iface->device->bus);
1062 void
1063 usbd_set_polling(usbd_device_handle dev, int on)
1065 if (on)
1066 dev->bus->use_polling++;
1067 else
1068 dev->bus->use_polling--;
1069 /* When polling we need to make sure there is nothing pending to do. */
1070 if (dev->bus->use_polling)
1071 dev->bus->methods->soft_intr(dev->bus);
1075 usb_endpoint_descriptor_t *
1076 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
1078 struct usbd_endpoint *ep;
1079 int i;
1081 for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1082 ep = &iface->endpoints[i];
1083 if (ep->edesc->bEndpointAddress == address)
1084 return (iface->endpoints[i].edesc);
1086 return (0);
1090 * usbd_ratecheck() can limit the number of error messages that occurs.
1091 * When a device is unplugged it may take up to 0.25s for the hub driver
1092 * to notice it. If the driver continuosly tries to do I/O operations
1093 * this can generate a large number of messages.
1096 usbd_ratecheck(struct timeval *last)
1098 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
1100 return (ratecheck(last, &errinterval));
1104 * Search for a vendor/product pair in an array. The item size is
1105 * given as an argument.
1107 const struct usb_devno *
1108 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1109 u_int16_t vendor, u_int16_t product)
1111 while (nentries-- > 0) {
1112 u_int16_t tproduct = tbl->ud_product;
1113 if (tbl->ud_vendor == vendor &&
1114 (tproduct == product || tproduct == USB_PRODUCT_ANY))
1115 return (tbl);
1116 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1118 return (NULL);
1122 void
1123 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
1125 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1127 iter->cur = (const uByte *)cd;
1128 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
1131 const usb_descriptor_t *
1132 usb_desc_iter_next(usbd_desc_iter_t *iter)
1134 const usb_descriptor_t *desc;
1136 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
1137 if (iter->cur != iter->end)
1138 printf("usb_desc_iter_next: bad descriptor\n");
1139 return NULL;
1141 desc = (const usb_descriptor_t *)iter->cur;
1142 if (desc->bLength == 0) {
1143 printf("usb_desc_iter_next: descriptor length = 0\n");
1144 return NULL;
1146 iter->cur += desc->bLength;
1147 if (iter->cur > iter->end) {
1148 printf("usb_desc_iter_next: descriptor length too large\n");
1149 return NULL;
1151 return desc;
1154 usbd_status
1155 usbd_get_string(usbd_device_handle dev, int si, char *buf)
1157 return usbd_get_string0(dev, si, buf, 1);
1160 usbd_status
1161 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
1163 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
1164 usb_string_descriptor_t us;
1165 char *s;
1166 int i, n;
1167 u_int16_t c;
1168 usbd_status err;
1169 int size;
1171 buf[0] = '\0';
1172 if (si == 0)
1173 return (USBD_INVAL);
1174 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
1175 return (USBD_STALLED);
1176 if (dev->langid == USBD_NOLANG) {
1177 /* Set up default language */
1178 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
1179 &size);
1180 if (err || size < 4) {
1181 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
1182 dev->langid = 0; /* Well, just pick something then */
1183 } else {
1184 /* Pick the first language as the default. */
1185 dev->langid = UGETW(us.bString[0]);
1188 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
1189 if (err)
1190 return (err);
1191 s = buf;
1192 n = size / 2 - 1;
1193 if (unicode) {
1194 for (i = 0; i < n; i++) {
1195 c = UGETW(us.bString[i]);
1196 if (swap)
1197 c = (c >> 8) | (c << 8);
1198 s += wput_utf8(s, 3, c);
1200 *s++ = 0;
1202 #ifdef COMPAT_30
1203 else {
1204 for (i = 0; i < n; i++) {
1205 c = UGETW(us.bString[i]);
1206 if (swap)
1207 c = (c >> 8) | (c << 8);
1208 *s++ = (c < 0x80) ? c : '?';
1210 *s++ = 0;
1212 #endif
1213 return (USBD_NORMAL_COMPLETION);