Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / usb / usbdi.c
1 /*      $NetBSD: usbdi.c,v 1.60 2000/01/19 00:23:58 augustss Exp $      */
2 /*      $FreeBSD: src/sys/dev/usb/usbdi.c,v 1.34.2.7 2002/11/06 14:03:37 joe Exp $      */
3
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #if defined(__NetBSD__) || defined(__OpenBSD__)
44 #include <sys/device.h>
45 #elif defined(__FreeBSD__)
46 #include <sys/module.h>
47 #include <sys/bus.h>
48 #include "usb_if.h"
49 #if defined(DIAGNOSTIC) && defined(__i386__)
50 #include <machine/cpu.h>
51 #endif
52 #endif
53 #include <sys/malloc.h>
54 #include <sys/proc.h>
55
56 #include <machine/bus.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdivar.h>
62 #include <dev/usb/usb_mem.h>
63
64 #if defined(__FreeBSD__)
65 #include "usb_if.h"
66 #include <machine/clock.h>
67 #define delay(d)        DELAY(d)
68 #endif
69
70 #ifdef USB_DEBUG
71 #define DPRINTF(x)      if (usbdebug) logprintf x
72 #define DPRINTFN(n,x)   if (usbdebug>(n)) logprintf x
73 extern int usbdebug;
74 #else
75 #define DPRINTF(x)
76 #define DPRINTFN(n,x)
77 #endif
78
79 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
80 Static void usbd_do_request_async_cb 
81     (usbd_xfer_handle, usbd_private_handle, usbd_status);
82 Static void usbd_start_next(usbd_pipe_handle pipe);
83 Static usbd_status usbd_open_pipe_ival
84     (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
85
86 Static int usbd_nbuses = 0;
87
88 void
89 usbd_init(void)
90 {
91         usbd_nbuses++;
92 }
93
94 void
95 usbd_finish(void)
96 {
97         --usbd_nbuses;
98 }
99
100 Static __inline int usbd_xfer_isread(usbd_xfer_handle xfer);
101 Static __inline int
102 usbd_xfer_isread(xfer)
103         usbd_xfer_handle xfer;
104 {
105         if (xfer->rqflags & URQ_REQUEST)
106                 return (xfer->request.bmRequestType & UT_READ);
107         else
108                 return (xfer->pipe->endpoint->edesc->bEndpointAddress &
109                         UE_DIR_IN);
110 }
111
112 #ifdef USB_DEBUG
113 void
114 usbd_dump_queue(usbd_pipe_handle);
115
116 void
117 usbd_dump_queue(usbd_pipe_handle pipe)
118 {
119         usbd_xfer_handle xfer;
120
121         printf("usbd_dump_queue: pipe=%p\n", pipe);
122         for (xfer = SIMPLEQ_FIRST(&pipe->queue);
123              xfer;
124              xfer = SIMPLEQ_NEXT(xfer, next)) {
125                 printf("  xfer=%p\n", xfer);
126         }
127 }
128 #endif
129
130 usbd_status 
131 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
132                u_int8_t flags, usbd_pipe_handle *pipe)
133
134         return (usbd_open_pipe_ival(iface, address, flags, pipe, 
135                                     USBD_DEFAULT_INTERVAL));
136 }
137
138 usbd_status 
139 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
140                     u_int8_t flags, usbd_pipe_handle *pipe, int ival)
141
142         usbd_pipe_handle p;
143         struct usbd_endpoint *ep;
144         usbd_status err;
145         int i;
146
147         DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
148                     iface, address, flags));
149
150         for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
151                 ep = &iface->endpoints[i];
152                 if (ep->edesc == NULL)
153                         return (USBD_IOERROR);
154                 if (ep->edesc->bEndpointAddress == address)
155                         goto found;
156         }
157         return (USBD_BAD_ADDRESS);
158  found:
159         if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
160                 return (USBD_IN_USE);
161         err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
162         if (err)
163                 return (err);
164         LIST_INSERT_HEAD(&iface->pipes, p, next);
165         *pipe = p;
166         return (USBD_NORMAL_COMPLETION);
167 }
168
169 usbd_status 
170 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
171                     u_int8_t flags, usbd_pipe_handle *pipe,
172                     usbd_private_handle priv, void *buffer, u_int32_t len,
173                     usbd_callback cb, int ival)
174 {
175         usbd_status err;
176         usbd_xfer_handle xfer;
177         usbd_pipe_handle ipipe;
178
179         DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
180                     address, flags, len));
181
182         err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE, 
183                                   &ipipe, ival);
184         if (err)
185                 return (err);
186         xfer = usbd_alloc_xfer(iface->device);
187         if (xfer == NULL) {
188                 err = USBD_NOMEM;
189                 goto bad1;
190         }
191         usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
192             USBD_NO_TIMEOUT, cb);
193         ipipe->intrxfer = xfer;
194         ipipe->repeat = 1;
195         err = usbd_transfer(xfer);
196         *pipe = ipipe;
197         if (err != USBD_IN_PROGRESS)
198                 goto bad2;
199         return (USBD_NORMAL_COMPLETION);
200
201  bad2:
202         ipipe->intrxfer = NULL;
203         ipipe->repeat = 0;
204         usbd_free_xfer(xfer);
205  bad1:
206         usbd_close_pipe(ipipe);
207         return (err);
208 }
209
210 usbd_status
211 usbd_close_pipe(usbd_pipe_handle pipe)
212 {
213 #ifdef DIAGNOSTIC
214         if (pipe == NULL) {
215                 printf("usbd_close_pipe: pipe==NULL\n");
216                 return (USBD_NORMAL_COMPLETION);
217         }
218 #endif
219
220         if (--pipe->refcnt != 0)
221                 return (USBD_NORMAL_COMPLETION);
222         if (SIMPLEQ_FIRST(&pipe->queue) != 0)
223                 return (USBD_PENDING_REQUESTS);
224         LIST_REMOVE(pipe, next);
225         pipe->endpoint->refcnt--;
226         pipe->methods->close(pipe);
227         if (pipe->intrxfer != NULL)
228                 usbd_free_xfer(pipe->intrxfer);
229         free(pipe, M_USB);
230         return (USBD_NORMAL_COMPLETION);
231 }
232
233 usbd_status
234 usbd_transfer(usbd_xfer_handle xfer)
235 {
236         usbd_pipe_handle pipe = xfer->pipe;
237         usb_dma_t *dmap = &xfer->dmabuf;
238         usbd_status err;
239         u_int size;
240         int s;
241
242         DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
243                     xfer, xfer->flags, pipe, pipe->running));
244 #ifdef USB_DEBUG
245         if (usbdebug > 5)
246                 usbd_dump_queue(pipe);
247 #endif
248         xfer->done = 0;
249
250         size = xfer->length;
251         /* If there is no buffer, allocate one. */
252         if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
253                 struct usbd_bus *bus = pipe->device->bus;
254
255 #ifdef DIAGNOSTIC
256                 if (xfer->rqflags & URQ_AUTO_DMABUF)
257                         printf("usbd_transfer: has old buffer!\n");
258 #endif
259                 err = bus->methods->allocm(bus, dmap, size);
260                 if (err)
261                         return (err);
262                 xfer->rqflags |= URQ_AUTO_DMABUF;
263         }
264
265         /* Copy data if going out. */
266         if (!(xfer->flags & USBD_NO_COPY) && size != 0 && 
267             !usbd_xfer_isread(xfer))
268                 memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
269
270         err = pipe->methods->transfer(xfer);
271
272         if (err != USBD_IN_PROGRESS && err) {
273                 /* The transfer has not been queued, so free buffer. */
274                 if (xfer->rqflags & URQ_AUTO_DMABUF) {
275                         struct usbd_bus *bus = pipe->device->bus;
276
277                         bus->methods->freem(bus, &xfer->dmabuf);
278                         xfer->rqflags &= ~URQ_AUTO_DMABUF;
279                 }
280         }
281
282         if (!(xfer->flags & USBD_SYNCHRONOUS))
283                 return (err);
284
285         /* Sync transfer, wait for completion. */
286         if (err != USBD_IN_PROGRESS)
287                 return (err);
288         s = splusb();
289         if (!xfer->done) {
290                 if (pipe->device->bus->use_polling)
291                         panic("usbd_transfer: not done\n");
292                 /* XXX Temporary hack XXX */
293                 if (xfer->flags & USBD_NO_TSLEEP) {
294                         int i;
295                         usbd_bus_handle bus = pipe->device->bus;
296                         int to = xfer->timeout * 1000;
297                         for (i = 0; i < to; i += 10) {
298                                 delay(10);
299                                 bus->methods->do_poll(bus);
300                                 if (xfer->done)
301                                         break;
302                         }
303                         if (!xfer->done)
304                                 pipe->methods->abort(xfer);
305                 } else
306                 /* XXX End hack XXX */
307                         tsleep(xfer, PRIBIO, "usbsyn", 0);
308         }
309         splx(s);
310         return (xfer->status);
311 }
312
313 /* Like usbd_transfer(), but waits for completion. */
314 usbd_status
315 usbd_sync_transfer(usbd_xfer_handle xfer)
316 {
317         xfer->flags |= USBD_SYNCHRONOUS;
318         return (usbd_transfer(xfer));
319 }
320
321 void *
322 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
323 {
324         struct usbd_bus *bus = xfer->device->bus;
325         usbd_status err;
326
327         err = bus->methods->allocm(bus, &xfer->dmabuf, size);
328         if (err)
329                 return (0);
330         xfer->rqflags |= URQ_DEV_DMABUF;
331         return (KERNADDR(&xfer->dmabuf, 0));
332 }
333
334 void
335 usbd_free_buffer(usbd_xfer_handle xfer)
336 {
337 #ifdef DIAGNOSTIC
338         if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
339                 printf("usbd_free_buffer: no buffer\n");
340                 return;
341         }
342 #endif
343         xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
344         xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
345 }
346
347 void *
348 usbd_get_buffer(usbd_xfer_handle xfer)
349 {
350         if (!(xfer->rqflags & URQ_DEV_DMABUF))
351                 return (0);
352         return (KERNADDR(&xfer->dmabuf, 0));
353 }
354
355 usbd_xfer_handle 
356 usbd_alloc_xfer(usbd_device_handle dev)
357 {
358         usbd_xfer_handle xfer;
359
360         xfer = dev->bus->methods->allocx(dev->bus);
361         if (xfer == NULL)
362                 return (NULL);
363         xfer->device = dev;
364         DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
365         return (xfer);
366 }
367
368 usbd_status 
369 usbd_free_xfer(usbd_xfer_handle xfer)
370 {
371         DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
372         if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
373                 usbd_free_buffer(xfer);
374         xfer->device->bus->methods->freex(xfer->device->bus, xfer);
375         return (USBD_NORMAL_COMPLETION);
376 }
377
378 void
379 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
380                 usbd_private_handle priv, void *buffer, u_int32_t length,
381                 u_int16_t flags, u_int32_t timeout,
382                 usbd_callback callback)
383 {
384         xfer->pipe = pipe;
385         xfer->priv = priv;
386         xfer->buffer = buffer;
387         xfer->length = length;
388         xfer->actlen = 0;
389         xfer->flags = flags;
390         xfer->timeout = timeout;
391         xfer->status = USBD_NOT_STARTED;
392         xfer->callback = callback;
393         xfer->rqflags &= ~URQ_REQUEST;
394         xfer->nframes = 0;
395 }
396
397 void
398 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
399                         usbd_private_handle priv, u_int32_t timeout,
400                         usb_device_request_t *req, void *buffer,
401                         u_int32_t length, u_int16_t flags,
402                         usbd_callback callback)
403 {
404         xfer->pipe = dev->default_pipe;
405         xfer->priv = priv;
406         xfer->buffer = buffer;
407         xfer->length = length;
408         xfer->actlen = 0;
409         xfer->flags = flags;
410         xfer->timeout = timeout;
411         xfer->status = USBD_NOT_STARTED;
412         xfer->callback = callback;
413         xfer->request = *req;
414         xfer->rqflags |= URQ_REQUEST;
415         xfer->nframes = 0;
416 }
417
418 void
419 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
420                      usbd_private_handle priv, u_int16_t *frlengths,
421                      u_int32_t nframes, u_int16_t flags, usbd_callback callback)
422 {
423         xfer->pipe = pipe;
424         xfer->priv = priv;
425         xfer->buffer = 0;
426         xfer->length = 0;
427         xfer->actlen = 0;
428         xfer->flags = flags;
429         xfer->timeout = USBD_NO_TIMEOUT;
430         xfer->status = USBD_NOT_STARTED;
431         xfer->callback = callback;
432         xfer->rqflags &= ~URQ_REQUEST;
433         xfer->frlengths = frlengths;
434         xfer->nframes = nframes;
435 }
436
437 void
438 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
439                      void **buffer, u_int32_t *count, usbd_status *status)
440 {
441         if (priv != NULL)
442                 *priv = xfer->priv;
443         if (buffer != NULL)
444                 *buffer = xfer->buffer;
445         if (count != NULL)
446                 *count = xfer->actlen;
447         if (status != NULL)
448                 *status = xfer->status;
449 }
450
451 usb_config_descriptor_t *
452 usbd_get_config_descriptor(usbd_device_handle dev)
453 {
454 #ifdef DIAGNOSTIC
455         if (dev == NULL) {
456                 printf("usbd_get_config_descriptor: dev == NULL\n");
457                 return (NULL);
458         }
459 #endif
460         return (dev->cdesc);
461 }
462
463 usb_interface_descriptor_t *
464 usbd_get_interface_descriptor(usbd_interface_handle iface)
465 {
466 #ifdef DIAGNOSTIC
467         if (iface == NULL) {
468                 printf("usbd_get_interface_descriptor: dev == NULL\n");
469                 return (NULL);
470         }
471 #endif
472         return (iface->idesc);
473 }
474
475 usb_device_descriptor_t *
476 usbd_get_device_descriptor(usbd_device_handle dev)
477 {
478         return (&dev->ddesc);
479 }
480
481 usb_endpoint_descriptor_t *
482 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
483 {
484         if (index >= iface->idesc->bNumEndpoints)
485                 return (0);
486         return (iface->endpoints[index].edesc);
487 }
488
489 usbd_status 
490 usbd_abort_pipe(usbd_pipe_handle pipe)
491 {
492         usbd_status err;
493         int s;
494
495 #ifdef DIAGNOSTIC
496         if (pipe == NULL) {
497                 printf("usbd_close_pipe: pipe==NULL\n");
498                 return (USBD_NORMAL_COMPLETION);
499         }
500 #endif
501         s = splusb();
502         err = usbd_ar_pipe(pipe);
503         splx(s);
504         return (err);
505 }
506         
507 usbd_status 
508 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
509 {
510         usbd_device_handle dev = pipe->device;
511         usb_device_request_t req;
512         usbd_status err;
513
514         DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
515
516         /* 
517          * Clearing en endpoint stall resets the enpoint toggle, so
518          * do the same to the HC toggle.
519          */
520         pipe->methods->cleartoggle(pipe);
521
522         req.bmRequestType = UT_WRITE_ENDPOINT;
523         req.bRequest = UR_CLEAR_FEATURE;
524         USETW(req.wValue, UF_ENDPOINT_HALT);
525         USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
526         USETW(req.wLength, 0);
527         err = usbd_do_request(dev, &req, 0);
528 #if 0
529 XXX should we do this?
530         if (!err) {
531                 pipe->state = USBD_PIPE_ACTIVE;
532                 /* XXX activate pipe */
533         }
534 #endif
535         return (err);
536 }
537
538 usbd_status 
539 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
540 {
541         usbd_device_handle dev = pipe->device;
542         usb_device_request_t req;
543         usbd_status err;
544
545         pipe->methods->cleartoggle(pipe);
546
547         req.bmRequestType = UT_WRITE_ENDPOINT;
548         req.bRequest = UR_CLEAR_FEATURE;
549         USETW(req.wValue, UF_ENDPOINT_HALT);
550         USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
551         USETW(req.wLength, 0);
552         err = usbd_do_request_async(dev, &req, 0);
553         return (err);
554 }
555
556 void
557 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
558 {
559         pipe->methods->cleartoggle(pipe);
560 }
561
562
563 usbd_status 
564 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
565 {
566         *count = iface->idesc->bNumEndpoints;
567         return (USBD_NORMAL_COMPLETION);
568 }
569
570 usbd_status 
571 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
572 {
573         if (dev->cdesc == NULL)
574                 return (USBD_NOT_CONFIGURED);
575         *count = dev->cdesc->bNumInterface;
576         return (USBD_NORMAL_COMPLETION);
577 }
578
579 usbd_status 
580 usbd_interface2device_handle(usbd_interface_handle iface,
581                              usbd_device_handle *dev)
582 {
583         *dev = iface->device;
584         return (USBD_NORMAL_COMPLETION);
585 }
586
587 usbd_status 
588 usbd_device2interface_handle(usbd_device_handle dev,
589                              u_int8_t ifaceno, usbd_interface_handle *iface)
590 {
591         if (dev->cdesc == NULL)
592                 return (USBD_NOT_CONFIGURED);
593         if (ifaceno >= dev->cdesc->bNumInterface)
594                 return (USBD_INVAL);
595         *iface = &dev->ifaces[ifaceno];
596         return (USBD_NORMAL_COMPLETION);
597 }
598
599 usbd_device_handle
600 usbd_pipe2device_handle(usbd_pipe_handle pipe)
601 {
602         return (pipe->device);
603 }
604
605 /* XXXX use altno */
606 usbd_status
607 usbd_set_interface(usbd_interface_handle iface, int altidx)
608 {
609         usb_device_request_t req;
610         usbd_status err;
611
612         if (LIST_FIRST(&iface->pipes) != 0)
613                 return (USBD_IN_USE);
614
615         if (iface->endpoints)
616                 free(iface->endpoints, M_USB);
617         iface->endpoints = 0;
618         iface->idesc = 0;
619
620         err = usbd_fill_iface_data(iface->device, iface->index, altidx);
621         if (err)
622                 return (err);
623
624         req.bmRequestType = UT_WRITE_INTERFACE;
625         req.bRequest = UR_SET_INTERFACE;
626         USETW(req.wValue, iface->idesc->bAlternateSetting);
627         USETW(req.wIndex, iface->idesc->bInterfaceNumber);
628         USETW(req.wLength, 0);
629         return (usbd_do_request(iface->device, &req, 0));
630 }
631
632 int
633 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
634 {
635         char *p = (char *)cdesc;
636         char *end = p + UGETW(cdesc->wTotalLength);
637         usb_interface_descriptor_t *d;
638         int n;
639
640         for (n = 0; p < end; p += d->bLength) {
641                 d = (usb_interface_descriptor_t *)p;
642                 if (p + d->bLength <= end && 
643                     d->bDescriptorType == UDESC_INTERFACE &&
644                     d->bInterfaceNumber == ifaceno)
645                         n++;
646         }
647         return (n);
648 }
649
650 int
651 usbd_get_interface_altindex(usbd_interface_handle iface)
652 {
653         return (iface->altindex);
654 }
655
656 usbd_status
657 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
658 {
659         usb_device_request_t req;
660
661         req.bmRequestType = UT_READ_INTERFACE;
662         req.bRequest = UR_GET_INTERFACE;
663         USETW(req.wValue, 0);
664         USETW(req.wIndex, iface->idesc->bInterfaceNumber);
665         USETW(req.wLength, 1);
666         return (usbd_do_request(iface->device, &req, aiface));
667 }
668
669 /*** Internal routines ***/
670
671 /* Dequeue all pipe operations, called at splusb(). */
672 Static usbd_status
673 usbd_ar_pipe(usbd_pipe_handle pipe)
674 {
675         usbd_xfer_handle xfer;
676
677         SPLUSBCHECK;
678
679         DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
680 #ifdef USB_DEBUG
681         if (usbdebug > 5)
682                 usbd_dump_queue(pipe);
683 #endif
684         pipe->repeat = 0;
685         while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
686                 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n", 
687                             pipe, xfer, pipe->methods));
688                 /* Make the HC abort it (and invoke the callback). */
689                 pipe->methods->abort(xfer);
690                 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
691         }
692         return (USBD_NORMAL_COMPLETION);
693 }
694
695 /* Called at splusb() */
696 void
697 usb_transfer_complete(usbd_xfer_handle xfer)
698 {
699         usbd_pipe_handle pipe = xfer->pipe;
700         usb_dma_t *dmap = &xfer->dmabuf;
701         int repeat = pipe->repeat;
702         int polling;
703
704         SPLUSBCHECK;
705
706         DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
707                      "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
708
709 #ifdef DIAGNOSTIC
710         if (pipe == NULL) {
711                 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
712                 return;
713         }
714 #endif
715         polling = pipe->device->bus->use_polling;
716         /* XXXX */
717         if (polling)
718                 pipe->running = 0;
719
720         if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
721             usbd_xfer_isread(xfer)) {
722 #ifdef DIAGNOSTIC
723                 if (xfer->actlen > xfer->length) {
724                         printf("usb_transfer_complete: actlen > len %d > %d\n",
725                                xfer->actlen, xfer->length);
726                         xfer->actlen = xfer->length;
727                 }
728 #endif
729                 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
730         }
731
732         /* if we allocated the buffer in usbd_transfer() we free it here. */
733         if (xfer->rqflags & URQ_AUTO_DMABUF) {
734                 if (!repeat) {
735                         struct usbd_bus *bus = pipe->device->bus;
736                         bus->methods->freem(bus, dmap);
737                         xfer->rqflags &= ~URQ_AUTO_DMABUF;
738                 }
739         }
740
741         if (pipe->methods->done != NULL)
742                 pipe->methods->done(xfer);
743
744         if (!repeat) {
745                 /* Remove request from queue. */
746 #ifdef DIAGNOSTIC
747                 if (xfer != SIMPLEQ_FIRST(&pipe->queue))
748                         printf("usb_transfer_complete: bad dequeue %p != %p\n",
749                                xfer, SIMPLEQ_FIRST(&pipe->queue));
750 #endif
751                 SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
752         }
753
754         /* Count completed transfers. */
755         ++pipe->device->bus->stats.uds_requests
756                 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
757
758         xfer->done = 1;
759         if (!xfer->status && xfer->actlen < xfer->length &&
760             !(xfer->flags & USBD_SHORT_XFER_OK)) {
761                 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
762                              xfer->actlen, xfer->length));
763                 xfer->status = USBD_SHORT_XFER;
764         }
765
766         if (xfer->callback)
767                 xfer->callback(xfer, xfer->priv, xfer->status);
768
769         if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
770                 wakeup(xfer);
771
772         if (!repeat) {
773                 /* XXX should we stop the queue on all errors? */
774                 if ((xfer->status == USBD_CANCELLED
775                      || xfer->status == USBD_TIMEOUT)
776                     && pipe->iface != NULL)             /* not control pipe */
777                         pipe->running = 0;
778                 else
779                         usbd_start_next(pipe);
780         }
781 }
782
783 usbd_status
784 usb_insert_transfer(usbd_xfer_handle xfer)
785 {
786         usbd_pipe_handle pipe = xfer->pipe;
787         usbd_status err;
788         int s;
789
790         DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n", 
791                     pipe, pipe->running, xfer->timeout));
792         s = splusb();
793         SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
794         if (pipe->running)
795                 err = USBD_IN_PROGRESS;
796         else {
797                 pipe->running = 1;
798                 err = USBD_NORMAL_COMPLETION;
799         }
800         splx(s);
801         return (err);
802 }
803
804 /* Called at splusb() */
805 void
806 usbd_start_next(usbd_pipe_handle pipe)
807 {
808         usbd_xfer_handle xfer;
809         usbd_status err;
810
811         SPLUSBCHECK;
812
813 #ifdef DIAGNOSTIC
814         if (pipe == NULL) {
815                 printf("usbd_start_next: pipe == NULL\n");
816                 return;
817         }
818         if (pipe->methods == NULL || pipe->methods->start == NULL) {
819                 printf("usbd_start_next: pipe=%p no start method\n", pipe);
820                 return;
821         }
822 #endif
823
824         /* Get next request in queue. */
825         xfer = SIMPLEQ_FIRST(&pipe->queue);
826         DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
827         if (xfer == NULL) {
828                 pipe->running = 0;
829         } else {
830                 err = pipe->methods->start(xfer);
831                 if (err != USBD_IN_PROGRESS) {
832                         printf("usbd_start_next: error=%d\n", err);
833                         pipe->running = 0;
834                         /* XXX do what? */
835                 }
836         }
837 }
838
839 usbd_status
840 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
841 {
842         return (usbd_do_request_flags(dev, req, data, 0, 0));
843 }
844
845 usbd_status
846 usbd_do_request_flags(usbd_device_handle dev,
847         usb_device_request_t *req, void *data, u_int16_t flags, int *actlen)
848 {
849         usbd_xfer_handle xfer;
850         usbd_status err;
851
852 #ifdef DIAGNOSTIC
853 #if defined(__i386__) && defined(__FreeBSD__)
854         KASSERT(intr_nesting_level == 0,
855                 ("usbd_do_request: in interrupt context"));
856 #endif
857         if (dev->bus->intr_context) {
858                 printf("usbd_do_request: not in process context\n");
859                 return (USBD_INVAL);
860         }
861 #endif
862
863         xfer = usbd_alloc_xfer(dev);
864         if (xfer == NULL)
865                 return (USBD_NOMEM);
866         usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
867                                    data, UGETW(req->wLength), flags, 0);
868         err = usbd_sync_transfer(xfer);
869 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
870         if (xfer->actlen > xfer->length)
871                 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
872                          "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
873                          dev->address, xfer->request.bmRequestType,
874                          xfer->request.bRequest, UGETW(xfer->request.wValue),
875                          UGETW(xfer->request.wIndex), 
876                          UGETW(xfer->request.wLength), 
877                          xfer->length, xfer->actlen));
878 #endif
879         if (actlen != NULL)
880                 *actlen = xfer->actlen;
881         if (err == USBD_STALLED) {
882                 /* 
883                  * The control endpoint has stalled.  Control endpoints
884                  * should not halt, but some may do so anyway so clear
885                  * any halt condition.
886                  */
887                 usb_device_request_t treq;
888                 usb_status_t status;
889                 u_int16_t s;
890                 usbd_status nerr;
891
892                 treq.bmRequestType = UT_READ_ENDPOINT;
893                 treq.bRequest = UR_GET_STATUS;
894                 USETW(treq.wValue, 0);
895                 USETW(treq.wIndex, 0);
896                 USETW(treq.wLength, sizeof(usb_status_t));
897                 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
898                                            &treq, &status,sizeof(usb_status_t),
899                                            0, 0);
900                 nerr = usbd_sync_transfer(xfer);
901                 if (nerr)
902                         goto bad;
903                 s = UGETW(status.wStatus);
904                 DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
905                 if (!(s & UES_HALT))
906                         goto bad;
907                 treq.bmRequestType = UT_WRITE_ENDPOINT;
908                 treq.bRequest = UR_CLEAR_FEATURE;
909                 USETW(treq.wValue, UF_ENDPOINT_HALT);
910                 USETW(treq.wIndex, 0);
911                 USETW(treq.wLength, 0);
912                 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
913                                            &treq, &status, 0, 0, 0);
914                 nerr = usbd_sync_transfer(xfer);
915                 if (nerr)
916                         goto bad;
917         }
918
919  bad:
920         usbd_free_xfer(xfer);
921         return (err);
922 }
923
924 void
925 usbd_do_request_async_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
926                          usbd_status status)
927 {
928 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
929         if (xfer->actlen > xfer->length)
930                 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
931                          "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
932                          xfer->pipe->device->address, 
933                          xfer->request.bmRequestType,
934                          xfer->request.bRequest, UGETW(xfer->request.wValue),
935                          UGETW(xfer->request.wIndex), 
936                          UGETW(xfer->request.wLength), 
937                          xfer->length, xfer->actlen));
938 #endif
939         usbd_free_xfer(xfer);
940 }
941
942 /*
943  * Execute a request without waiting for completion.
944  * Can be used from interrupt context.
945  */
946 usbd_status
947 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
948                       void *data)
949 {
950         usbd_xfer_handle xfer;
951         usbd_status err;
952
953         xfer = usbd_alloc_xfer(dev);
954         if (xfer == NULL)
955                 return (USBD_NOMEM);
956         usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
957             data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
958         err = usbd_transfer(xfer);
959         if (err != USBD_IN_PROGRESS) {
960                 usbd_free_xfer(xfer);
961                 return (err);
962         }
963         return (USBD_NORMAL_COMPLETION);
964 }
965
966 struct usbd_quirks *
967 usbd_get_quirks(usbd_device_handle dev)
968 {
969         return (dev->quirks);
970 }
971
972 /* XXX do periodic free() of free list */
973
974 /*
975  * Called from keyboard driver when in polling mode.
976  */
977 void
978 usbd_dopoll(usbd_interface_handle iface)
979 {
980         iface->device->bus->methods->do_poll(iface->device->bus);
981 }
982
983 void
984 usbd_set_polling(usbd_interface_handle iface, int on)
985 {
986         if (on)
987                 iface->device->bus->use_polling++;
988         else
989                 iface->device->bus->use_polling--;
990 }
991
992
993 usb_endpoint_descriptor_t *
994 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
995 {
996         struct usbd_endpoint *ep;
997         int i;
998
999         for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
1000                 ep = &iface->endpoints[i];
1001                 if (ep->edesc->bEndpointAddress == address)
1002                         return (iface->endpoints[i].edesc);
1003         }
1004         return (0);
1005 }
1006
1007 /*
1008  * Search for a vendor/product pair in an array.  The item size is
1009  * given as an argument.
1010  */
1011 const struct usb_devno *
1012 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
1013                  u_int16_t vendor, u_int16_t product)
1014 {
1015         while (nentries-- > 0) {
1016                 if (tbl->ud_vendor == vendor && tbl->ud_product == product)
1017                         return (tbl);
1018                 tbl = (const struct usb_devno *)((const char *)tbl + sz);
1019         }
1020         return (NULL);
1021 }
1022
1023 #if defined(__FreeBSD__)
1024 int
1025 usbd_driver_load(module_t mod, int what, void *arg)
1026 {
1027         /* XXX should implement something like a function that removes all generic devices */
1028  
1029         return (0);
1030 }
1031
1032 #endif