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