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