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