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