]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/usb/usb_compat_linux.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / usb / usb_compat_linux.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.
4  * Copyright (c) 2007 Hans Petter Selasky. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/stdint.h>
29 #include <sys/stddef.h>
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/linker_set.h>
37 #include <sys/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/sx.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/priv.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usb_ioctl.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52
53 #define USB_DEBUG_VAR usb_debug
54
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_compat_linux.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_util.h>
60 #include <dev/usb/usb_busdma.h>
61 #include <dev/usb/usb_transfer.h>
62 #include <dev/usb/usb_hub.h>
63 #include <dev/usb/usb_request.h>
64 #include <dev/usb/usb_debug.h>
65
66 struct usb_linux_softc {
67         LIST_ENTRY(usb_linux_softc) sc_attached_list;
68
69         device_t sc_fbsd_dev;
70         struct usb_device *sc_fbsd_udev;
71         struct usb_interface *sc_ui;
72         struct usb_driver *sc_udrv;
73 };
74
75 /* prototypes */
76 static device_probe_t usb_linux_probe;
77 static device_attach_t usb_linux_attach;
78 static device_detach_t usb_linux_detach;
79 static device_suspend_t usb_linux_suspend;
80 static device_resume_t usb_linux_resume;
81
82 static usb_callback_t usb_linux_isoc_callback;
83 static usb_callback_t usb_linux_non_isoc_callback;
84
85 static usb_complete_t usb_linux_wait_complete;
86
87 static uint16_t usb_max_isoc_frames(struct usb_device *);
88 static int      usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *);
89 static const struct usb_device_id *usb_linux_lookup_id(
90                     const struct usb_device_id *, struct usb_attach_arg *);
91 static struct   usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *);
92 static int      usb_linux_create_usb_device(struct usb_device *, device_t);
93 static void     usb_linux_cleanup_interface(struct usb_device *,
94                     struct usb_interface *);
95 static void     usb_linux_complete(struct usb_xfer *);
96 static int      usb_unlink_urb_sub(struct urb *, uint8_t);
97
98 /*------------------------------------------------------------------------*
99  * FreeBSD USB interface
100  *------------------------------------------------------------------------*/
101
102 static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list;
103 static LIST_HEAD(, usb_driver) usb_linux_driver_list;
104
105 static device_method_t usb_linux_methods[] = {
106         /* Device interface */
107         DEVMETHOD(device_probe, usb_linux_probe),
108         DEVMETHOD(device_attach, usb_linux_attach),
109         DEVMETHOD(device_detach, usb_linux_detach),
110         DEVMETHOD(device_suspend, usb_linux_suspend),
111         DEVMETHOD(device_resume, usb_linux_resume),
112
113         {0, 0}
114 };
115
116 static driver_t usb_linux_driver = {
117         .name = "usb_linux",
118         .methods = usb_linux_methods,
119         .size = sizeof(struct usb_linux_softc),
120 };
121
122 static devclass_t usb_linux_devclass;
123
124 DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
125
126 /*------------------------------------------------------------------------*
127  *      usb_linux_lookup_id
128  *
129  * This functions takes an array of "struct usb_device_id" and tries
130  * to match the entries with the information in "struct usb_attach_arg".
131  * If it finds a match the matching entry will be returned.
132  * Else "NULL" will be returned.
133  *------------------------------------------------------------------------*/
134 static const struct usb_device_id *
135 usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa)
136 {
137         if (id == NULL) {
138                 goto done;
139         }
140         /*
141          * Keep on matching array entries until we find one with
142          * "match_flags" equal to zero, which indicates the end of the
143          * array:
144          */
145         for (; id->match_flags; id++) {
146
147                 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
148                     (id->idVendor != uaa->info.idVendor)) {
149                         continue;
150                 }
151                 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
152                     (id->idProduct != uaa->info.idProduct)) {
153                         continue;
154                 }
155                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
156                     (id->bcdDevice_lo > uaa->info.bcdDevice)) {
157                         continue;
158                 }
159                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
160                     (id->bcdDevice_hi < uaa->info.bcdDevice)) {
161                         continue;
162                 }
163                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
164                     (id->bDeviceClass != uaa->info.bDeviceClass)) {
165                         continue;
166                 }
167                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
168                     (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) {
169                         continue;
170                 }
171                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
172                     (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) {
173                         continue;
174                 }
175                 if ((uaa->info.bDeviceClass == 0xFF) &&
176                     !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
177                     (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
178                     USB_DEVICE_ID_MATCH_INT_SUBCLASS |
179                     USB_DEVICE_ID_MATCH_INT_PROTOCOL))) {
180                         continue;
181                 }
182                 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
183                     (id->bInterfaceClass != uaa->info.bInterfaceClass)) {
184                         continue;
185                 }
186                 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
187                     (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) {
188                         continue;
189                 }
190                 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
191                     (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) {
192                         continue;
193                 }
194                 /* we found a match! */
195                 return (id);
196         }
197
198 done:
199         return (NULL);
200 }
201
202 /*------------------------------------------------------------------------*
203  *      usb_linux_probe
204  *
205  * This function is the FreeBSD probe callback. It is called from the
206  * FreeBSD USB stack through the "device_probe_and_attach()" function.
207  *------------------------------------------------------------------------*/
208 static int
209 usb_linux_probe(device_t dev)
210 {
211         struct usb_attach_arg *uaa = device_get_ivars(dev);
212         struct usb_driver *udrv;
213         int err = ENXIO;
214
215         if (uaa->usb_mode != USB_MODE_HOST) {
216                 return (ENXIO);
217         }
218         mtx_lock(&Giant);
219         LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
220                 if (usb_linux_lookup_id(udrv->id_table, uaa)) {
221                         err = 0;
222                         break;
223                 }
224         }
225         mtx_unlock(&Giant);
226
227         return (err);
228 }
229
230 /*------------------------------------------------------------------------*
231  *      usb_linux_get_usb_driver
232  *
233  * This function returns the pointer to the "struct usb_driver" where
234  * the Linux USB device driver "struct usb_device_id" match was found.
235  * We apply a lock before reading out the pointer to avoid races.
236  *------------------------------------------------------------------------*/
237 static struct usb_driver *
238 usb_linux_get_usb_driver(struct usb_linux_softc *sc)
239 {
240         struct usb_driver *udrv;
241
242         mtx_lock(&Giant);
243         udrv = sc->sc_udrv;
244         mtx_unlock(&Giant);
245         return (udrv);
246 }
247
248 /*------------------------------------------------------------------------*
249  *      usb_linux_attach
250  *
251  * This function is the FreeBSD attach callback. It is called from the
252  * FreeBSD USB stack through the "device_probe_and_attach()" function.
253  * This function is called when "usb_linux_probe()" returns zero.
254  *------------------------------------------------------------------------*/
255 static int
256 usb_linux_attach(device_t dev)
257 {
258         struct usb_attach_arg *uaa = device_get_ivars(dev);
259         struct usb_linux_softc *sc = device_get_softc(dev);
260         struct usb_driver *udrv;
261         const struct usb_device_id *id = NULL;
262
263         mtx_lock(&Giant);
264         LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {
265                 id = usb_linux_lookup_id(udrv->id_table, uaa);
266                 if (id)
267                         break;
268         }
269         mtx_unlock(&Giant);
270
271         if (id == NULL) {
272                 return (ENXIO);
273         }
274         if (usb_linux_create_usb_device(uaa->device, dev) != 0)
275                 return (ENOMEM);
276         device_set_usb_desc(dev);
277
278         sc->sc_fbsd_udev = uaa->device;
279         sc->sc_fbsd_dev = dev;
280         sc->sc_udrv = udrv;
281         sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum);
282         if (sc->sc_ui == NULL) {
283                 return (EINVAL);
284         }
285         if (udrv->probe) {
286                 if ((udrv->probe) (sc->sc_ui, id)) {
287                         return (ENXIO);
288                 }
289         }
290         mtx_lock(&Giant);
291         LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list);
292         mtx_unlock(&Giant);
293
294         /* success */
295         return (0);
296 }
297
298 /*------------------------------------------------------------------------*
299  *      usb_linux_detach
300  *
301  * This function is the FreeBSD detach callback. It is called from the
302  * FreeBSD USB stack through the "device_detach()" function.
303  *------------------------------------------------------------------------*/
304 static int
305 usb_linux_detach(device_t dev)
306 {
307         struct usb_linux_softc *sc = device_get_softc(dev);
308         struct usb_driver *udrv = NULL;
309
310         mtx_lock(&Giant);
311         if (sc->sc_attached_list.le_prev) {
312                 LIST_REMOVE(sc, sc_attached_list);
313                 sc->sc_attached_list.le_prev = NULL;
314                 udrv = sc->sc_udrv;
315                 sc->sc_udrv = NULL;
316         }
317         mtx_unlock(&Giant);
318
319         if (udrv && udrv->disconnect) {
320                 (udrv->disconnect) (sc->sc_ui);
321         }
322         /*
323          * Make sure that we free all FreeBSD USB transfers belonging to
324          * this Linux "usb_interface", hence they will most likely not be
325          * needed any more.
326          */
327         usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui);
328         return (0);
329 }
330
331 /*------------------------------------------------------------------------*
332  *      usb_linux_suspend
333  *
334  * This function is the FreeBSD suspend callback. Usually it does nothing.
335  *------------------------------------------------------------------------*/
336 static int
337 usb_linux_suspend(device_t dev)
338 {
339         struct usb_linux_softc *sc = device_get_softc(dev);
340         struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
341         int err;
342
343         if (udrv && udrv->suspend) {
344                 err = (udrv->suspend) (sc->sc_ui, 0);
345         }
346         return (0);
347 }
348
349 /*------------------------------------------------------------------------*
350  *      usb_linux_resume
351  *
352  * This function is the FreeBSD resume callback. Usually it does nothing.
353  *------------------------------------------------------------------------*/
354 static int
355 usb_linux_resume(device_t dev)
356 {
357         struct usb_linux_softc *sc = device_get_softc(dev);
358         struct usb_driver *udrv = usb_linux_get_usb_driver(sc);
359         int err;
360
361         if (udrv && udrv->resume) {
362                 err = (udrv->resume) (sc->sc_ui);
363         }
364         return (0);
365 }
366
367 /*------------------------------------------------------------------------*
368  * Linux emulation layer
369  *------------------------------------------------------------------------*/
370
371 /*------------------------------------------------------------------------*
372  *      usb_max_isoc_frames
373  *
374  * The following function returns the maximum number of isochronous
375  * frames that we support per URB. It is not part of the Linux USB API.
376  *------------------------------------------------------------------------*/
377 static uint16_t
378 usb_max_isoc_frames(struct usb_device *dev)
379 {
380         ;                               /* indent fix */
381         switch (usbd_get_speed(dev)) {
382         case USB_SPEED_LOW:
383         case USB_SPEED_FULL:
384                 return (USB_MAX_FULL_SPEED_ISOC_FRAMES);
385         default:
386                 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES);
387         }
388 }
389
390 /*------------------------------------------------------------------------*
391  *      usb_submit_urb
392  *
393  * This function is used to queue an URB after that it has been
394  * initialized. If it returns non-zero, it means that the URB was not
395  * queued.
396  *------------------------------------------------------------------------*/
397 int
398 usb_submit_urb(struct urb *urb, uint16_t mem_flags)
399 {
400         struct usb_host_endpoint *uhe;
401         uint8_t do_unlock;
402         int err;
403
404         if (urb == NULL)
405                 return (-EINVAL);
406
407         do_unlock = mtx_owned(&Giant) ? 0 : 1;
408         if (do_unlock)
409                 mtx_lock(&Giant);
410
411         if (urb->endpoint == NULL) {
412                 err = -EINVAL;
413                 goto done;
414         }
415
416         /*
417          * Check to see if the urb is in the process of being killed
418          * and stop a urb that is in the process of being killed from
419          * being re-submitted (e.g. from its completion callback
420          * function).
421          */
422         if (urb->kill_count != 0) {
423                 err = -EPERM;
424                 goto done;
425         }
426
427         uhe = urb->endpoint;
428
429         /*
430          * Check that we have got a FreeBSD USB transfer that will dequeue
431          * the URB structure and do the real transfer. If there are no USB
432          * transfers, then we return an error.
433          */
434         if (uhe->bsd_xfer[0] ||
435             uhe->bsd_xfer[1]) {
436                 /* we are ready! */
437
438                 TAILQ_INSERT_HEAD(&uhe->bsd_urb_list, urb, bsd_urb_list);
439
440                 urb->status = -EINPROGRESS;
441
442                 usbd_transfer_start(uhe->bsd_xfer[0]);
443                 usbd_transfer_start(uhe->bsd_xfer[1]);
444                 err = 0;
445         } else {
446                 /* no pipes have been setup yet! */
447                 urb->status = -EINVAL;
448                 err = -EINVAL;
449         }
450 done:
451         if (do_unlock)
452                 mtx_unlock(&Giant);
453         return (err);
454 }
455
456 /*------------------------------------------------------------------------*
457  *      usb_unlink_urb
458  *
459  * This function is used to stop an URB after that it is been
460  * submitted, but before the "complete" callback has been called. On
461  *------------------------------------------------------------------------*/
462 int
463 usb_unlink_urb(struct urb *urb)
464 {
465         return (usb_unlink_urb_sub(urb, 0));
466 }
467
468 static void
469 usb_unlink_bsd(struct usb_xfer *xfer,
470     struct urb *urb, uint8_t drain)
471 {
472         if (xfer == NULL)
473                 return;
474         if (!usbd_transfer_pending(xfer))
475                 return;
476         if (xfer->priv_fifo == (void *)urb) {
477                 if (drain) {
478                         mtx_unlock(&Giant);
479                         usbd_transfer_drain(xfer);
480                         mtx_lock(&Giant);
481                 } else {
482                         usbd_transfer_stop(xfer);
483                 }
484                 usbd_transfer_start(xfer);
485         }
486 }
487
488 static int
489 usb_unlink_urb_sub(struct urb *urb, uint8_t drain)
490 {
491         struct usb_host_endpoint *uhe;
492         uint16_t x;
493         uint8_t do_unlock;
494         int err;
495
496         if (urb == NULL)
497                 return (-EINVAL);
498
499         do_unlock = mtx_owned(&Giant) ? 0 : 1;
500         if (do_unlock)
501                 mtx_lock(&Giant);
502         if (drain)
503                 urb->kill_count++;
504
505         if (urb->endpoint == NULL) {
506                 err = -EINVAL;
507                 goto done;
508         }
509         uhe = urb->endpoint;
510
511         if (urb->bsd_urb_list.tqe_prev) {
512
513                 /* not started yet, just remove it from the queue */
514                 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
515                 urb->bsd_urb_list.tqe_prev = NULL;
516                 urb->status = -ECONNRESET;
517                 urb->actual_length = 0;
518
519                 for (x = 0; x < urb->number_of_packets; x++) {
520                         urb->iso_frame_desc[x].actual_length = 0;
521                 }
522
523                 if (urb->complete) {
524                         (urb->complete) (urb);
525                 }
526         } else {
527
528                 /*
529                  * If the URB is not on the URB list, then check if one of
530                  * the FreeBSD USB transfer are processing the current URB.
531                  * If so, re-start that transfer, which will lead to the
532                  * termination of that URB:
533                  */
534                 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain);
535                 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain);
536         }
537         err = 0;
538 done:
539         if (drain)
540                 urb->kill_count--;
541         if (do_unlock)
542                 mtx_unlock(&Giant);
543         return (err);
544 }
545
546 /*------------------------------------------------------------------------*
547  *      usb_clear_halt
548  *
549  * This function must always be used to clear the stall. Stall is when
550  * an USB endpoint returns a stall message to the USB host controller.
551  * Until the stall is cleared, no data can be transferred.
552  *------------------------------------------------------------------------*/
553 int
554 usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe)
555 {
556         struct usb_config cfg[1];
557         struct usb_endpoint *ep;
558         uint8_t type;
559         uint8_t addr;
560
561         if (uhe == NULL)
562                 return (-EINVAL);
563
564         type = uhe->desc.bmAttributes & UE_XFERTYPE;
565         addr = uhe->desc.bEndpointAddress;
566
567         bzero(cfg, sizeof(cfg));
568
569         cfg[0].type = type;
570         cfg[0].endpoint = addr & UE_ADDR;
571         cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
572
573         ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg);
574         if (ep == NULL)
575                 return (-EINVAL);
576
577         usbd_clear_data_toggle(dev, ep);
578
579         return (usb_control_msg(dev, &dev->ep0,
580             UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT,
581             UF_ENDPOINT_HALT, addr, NULL, 0, 1000));
582 }
583
584 /*------------------------------------------------------------------------*
585  *      usb_start_wait_urb
586  *
587  * This is an internal function that is used to perform synchronous
588  * Linux USB transfers.
589  *------------------------------------------------------------------------*/
590 static int
591 usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen)
592 {
593         int err;
594         uint8_t do_unlock;
595
596         /* you must have a timeout! */
597         if (timeout == 0) {
598                 timeout = 1;
599         }
600         urb->complete = &usb_linux_wait_complete;
601         urb->timeout = timeout;
602         urb->transfer_flags |= URB_WAIT_WAKEUP;
603         urb->transfer_flags &= ~URB_IS_SLEEPING;
604
605         do_unlock = mtx_owned(&Giant) ? 0 : 1;
606         if (do_unlock)
607                 mtx_lock(&Giant);
608         err = usb_submit_urb(urb, 0);
609         if (err)
610                 goto done;
611
612         /*
613          * the URB might have completed before we get here, so check that by
614          * using some flags!
615          */
616         while (urb->transfer_flags & URB_WAIT_WAKEUP) {
617                 urb->transfer_flags |= URB_IS_SLEEPING;
618                 cv_wait(&urb->cv_wait, &Giant);
619                 urb->transfer_flags &= ~URB_IS_SLEEPING;
620         }
621
622         err = urb->status;
623
624 done:
625         if (do_unlock)
626                 mtx_unlock(&Giant);
627         if (err) {
628                 *p_actlen = 0;
629         } else {
630                 *p_actlen = urb->actual_length;
631         }
632         return (err);
633 }
634
635 /*------------------------------------------------------------------------*
636  *      usb_control_msg
637  *
638  * The following function performs a control transfer sequence one any
639  * control, bulk or interrupt endpoint, specified by "uhe". A control
640  * transfer means that you transfer an 8-byte header first followed by
641  * a data-phase as indicated by the 8-byte header. The "timeout" is
642  * given in milliseconds.
643  *
644  * Return values:
645  *   0: Success
646  * < 0: Failure
647  * > 0: Acutal length
648  *------------------------------------------------------------------------*/
649 int
650 usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,
651     uint8_t request, uint8_t requesttype,
652     uint16_t value, uint16_t index, void *data,
653     uint16_t size, usb_timeout_t timeout)
654 {
655         struct usb_device_request req;
656         struct urb *urb;
657         int err;
658         uint16_t actlen;
659         uint8_t type;
660         uint8_t addr;
661
662         req.bmRequestType = requesttype;
663         req.bRequest = request;
664         USETW(req.wValue, value);
665         USETW(req.wIndex, index);
666         USETW(req.wLength, size);
667
668         if (uhe == NULL) {
669                 return (-EINVAL);
670         }
671         type = (uhe->desc.bmAttributes & UE_XFERTYPE);
672         addr = (uhe->desc.bEndpointAddress & UE_ADDR);
673
674         if (type != UE_CONTROL) {
675                 return (-EINVAL);
676         }
677         if (addr == 0) {
678                 /*
679                  * The FreeBSD USB stack supports standard control
680                  * transfers on control endpoint zero:
681                  */
682                 err = usbd_do_request_flags(dev,
683                     NULL, &req, data, USB_SHORT_XFER_OK,
684                     &actlen, timeout);
685                 if (err) {
686                         err = -EPIPE;
687                 } else {
688                         err = actlen;
689                 }
690                 return (err);
691         }
692         if (dev->flags.usb_mode != USB_MODE_HOST) {
693                 /* not supported */
694                 return (-EINVAL);
695         }
696         err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ );
697
698         /*
699          * NOTE: we need to allocate real memory here so that we don't
700          * transfer data to/from the stack!
701          *
702          * 0xFFFF is a FreeBSD specific magic value.
703          */
704         urb = usb_alloc_urb(0xFFFF, size);
705         if (urb == NULL)
706                 return (-ENOMEM);
707
708         urb->dev = dev;
709         urb->endpoint = uhe;
710
711         bcopy(&req, urb->setup_packet, sizeof(req));
712
713         if (size && (!(req.bmRequestType & UT_READ))) {
714                 /* move the data to a real buffer */
715                 bcopy(data, USB_ADD_BYTES(urb->setup_packet,
716                     sizeof(req)), size);
717         }
718         err = usb_start_wait_urb(urb, timeout, &actlen);
719
720         if (req.bmRequestType & UT_READ) {
721                 if (actlen) {
722                         bcopy(USB_ADD_BYTES(urb->setup_packet,
723                             sizeof(req)), data, actlen);
724                 }
725         }
726         usb_free_urb(urb);
727
728         if (err == 0) {
729                 err = actlen;
730         }
731         return (err);
732 }
733
734 /*------------------------------------------------------------------------*
735  *      usb_set_interface
736  *
737  * The following function will select which alternate setting of an
738  * USB interface you plan to use. By default alternate setting with
739  * index zero is selected. Note that "iface_no" is not the interface
740  * index, but rather the value of "bInterfaceNumber".
741  *------------------------------------------------------------------------*/
742 int
743 usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index)
744 {
745         struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no);
746         int err;
747
748         if (p_ui == NULL)
749                 return (-EINVAL);
750         if (alt_index >= p_ui->num_altsetting)
751                 return (-EINVAL);
752         usb_linux_cleanup_interface(dev, p_ui);
753         err = -usbd_set_alt_interface_index(dev,
754             p_ui->bsd_iface_index, alt_index);
755         if (err == 0) {
756                 p_ui->cur_altsetting = p_ui->altsetting + alt_index;
757         }
758         return (err);
759 }
760
761 /*------------------------------------------------------------------------*
762  *      usb_setup_endpoint
763  *
764  * The following function is an extension to the Linux USB API that
765  * allows you to set a maximum buffer size for a given USB endpoint.
766  * The maximum buffer size is per URB. If you don't call this function
767  * to set a maximum buffer size, the endpoint will not be functional.
768  * Note that for isochronous endpoints the maximum buffer size must be
769  * a non-zero dummy, hence this function will base the maximum buffer
770  * size on "wMaxPacketSize".
771  *------------------------------------------------------------------------*/
772 int
773 usb_setup_endpoint(struct usb_device *dev,
774     struct usb_host_endpoint *uhe, usb_size_t bufsize)
775 {
776         struct usb_config cfg[2];
777         uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE;
778         uint8_t addr = uhe->desc.bEndpointAddress;
779
780         if (uhe->fbsd_buf_size == bufsize) {
781                 /* optimize */
782                 return (0);
783         }
784         usbd_transfer_unsetup(uhe->bsd_xfer, 2);
785
786         uhe->fbsd_buf_size = bufsize;
787
788         if (bufsize == 0) {
789                 return (0);
790         }
791         bzero(cfg, sizeof(cfg));
792
793         if (type == UE_ISOCHRONOUS) {
794
795                 /*
796                  * Isochronous transfers are special in that they don't fit
797                  * into the BULK/INTR/CONTROL transfer model.
798                  */
799
800                 cfg[0].type = type;
801                 cfg[0].endpoint = addr & UE_ADDR;
802                 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
803                 cfg[0].callback = &usb_linux_isoc_callback;
804                 cfg[0].bufsize = 0;     /* use wMaxPacketSize */
805                 cfg[0].frames = usb_max_isoc_frames(dev);
806                 cfg[0].flags.proxy_buffer = 1;
807 #if 0
808                 /*
809                  * The Linux USB API allows non back-to-back
810                  * isochronous frames which we do not support. If the
811                  * isochronous frames are not back-to-back we need to
812                  * do a copy, and then we need a buffer for
813                  * that. Enable this at your own risk.
814                  */
815                 cfg[0].flags.ext_buffer = 1;
816 #endif
817                 cfg[0].flags.short_xfer_ok = 1;
818
819                 bcopy(cfg, cfg + 1, sizeof(*cfg));
820
821                 /* Allocate and setup two generic FreeBSD USB transfers */
822
823                 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
824                     uhe->bsd_xfer, cfg, 2, uhe, &Giant)) {
825                         return (-EINVAL);
826                 }
827         } else {
828                 if (bufsize > (1 << 22)) {
829                         /* limit buffer size */
830                         bufsize = (1 << 22);
831                 }
832                 /* Allocate and setup one generic FreeBSD USB transfer */
833
834                 cfg[0].type = type;
835                 cfg[0].endpoint = addr & UE_ADDR;
836                 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);
837                 cfg[0].callback = &usb_linux_non_isoc_callback;
838                 cfg[0].bufsize = bufsize;
839                 cfg[0].flags.ext_buffer = 1;    /* enable zero-copy */
840                 cfg[0].flags.proxy_buffer = 1;
841                 cfg[0].flags.short_xfer_ok = 1;
842
843                 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,
844                     uhe->bsd_xfer, cfg, 1, uhe, &Giant)) {
845                         return (-EINVAL);
846                 }
847         }
848         return (0);
849 }
850
851 /*------------------------------------------------------------------------*
852  *      usb_linux_create_usb_device
853  *
854  * The following function is used to build up a per USB device
855  * structure tree, that mimics the Linux one. The root structure
856  * is returned by this function.
857  *------------------------------------------------------------------------*/
858 static int
859 usb_linux_create_usb_device(struct usb_device *udev, device_t dev)
860 {
861         struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
862         struct usb_descriptor *desc;
863         struct usb_interface_descriptor *id;
864         struct usb_endpoint_descriptor *ed;
865         struct usb_interface *p_ui = NULL;
866         struct usb_host_interface *p_uhi = NULL;
867         struct usb_host_endpoint *p_uhe = NULL;
868         usb_size_t size;
869         uint16_t niface_total;
870         uint16_t nedesc;
871         uint16_t iface_no_curr;
872         uint16_t iface_index;
873         uint8_t pass;
874         uint8_t iface_no;
875
876         /*
877          * We do two passes. One pass for computing necessary memory size
878          * and one pass to initialize all the allocated memory structures.
879          */
880         for (pass = 0; pass < 2; pass++) {
881
882                 iface_no_curr = 0 - 1;
883                 niface_total = 0;
884                 iface_index = 0;
885                 nedesc = 0;
886                 desc = NULL;
887
888                 /*
889                  * Iterate over all the USB descriptors. Use the USB config
890                  * descriptor pointer provided by the FreeBSD USB stack.
891                  */
892                 while ((desc = usb_desc_foreach(cd, desc))) {
893
894                         /*
895                          * Build up a tree according to the descriptors we
896                          * find:
897                          */
898                         switch (desc->bDescriptorType) {
899                         case UDESC_DEVICE:
900                                 break;
901
902                         case UDESC_ENDPOINT:
903                                 ed = (void *)desc;
904                                 if ((ed->bLength < sizeof(*ed)) ||
905                                     (iface_index == 0))
906                                         break;
907                                 if (p_uhe) {
908                                         bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc));
909                                         p_uhe->bsd_iface_index = iface_index - 1;
910                                         p_uhe++;
911                                 }
912                                 if (p_uhi) {
913                                         (p_uhi - 1)->desc.bNumEndpoints++;
914                                 }
915                                 nedesc++;
916                                 break;
917
918                         case UDESC_INTERFACE:
919                                 id = (void *)desc;
920                                 if (id->bLength < sizeof(*id))
921                                         break;
922                                 if (p_uhi) {
923                                         bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc));
924                                         p_uhi->desc.bNumEndpoints = 0;
925                                         p_uhi->endpoint = p_uhe;
926                                         p_uhi->string = "";
927                                         p_uhi->bsd_iface_index = iface_index;
928                                         p_uhi++;
929                                 }
930                                 iface_no = id->bInterfaceNumber;
931                                 niface_total++;
932                                 if (iface_no_curr != iface_no) {
933                                         if (p_ui) {
934                                                 p_ui->altsetting = p_uhi - 1;
935                                                 p_ui->cur_altsetting = p_uhi - 1;
936                                                 p_ui->num_altsetting = 1;
937                                                 p_ui->bsd_iface_index = iface_index;
938                                                 p_ui->linux_udev = udev;
939                                                 p_ui++;
940                                         }
941                                         iface_no_curr = iface_no;
942                                         iface_index++;
943                                 } else {
944                                         if (p_ui) {
945                                                 (p_ui - 1)->num_altsetting++;
946                                         }
947                                 }
948                                 break;
949
950                         default:
951                                 break;
952                         }
953                 }
954
955                 if (pass == 0) {
956
957                         size = (sizeof(*p_uhe) * nedesc) +
958                             (sizeof(*p_ui) * iface_index) +
959                             (sizeof(*p_uhi) * niface_total);
960
961                         p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
962                         p_ui = (void *)(p_uhe + nedesc);
963                         p_uhi = (void *)(p_ui + iface_index);
964
965                         udev->linux_iface_start = p_ui;
966                         udev->linux_iface_end = p_ui + iface_index;
967                         udev->linux_endpoint_start = p_uhe;
968                         udev->linux_endpoint_end = p_uhe + nedesc;
969                         udev->devnum = device_get_unit(dev);
970                         bcopy(&udev->ddesc, &udev->descriptor,
971                             sizeof(udev->descriptor));
972                         bcopy(udev->default_ep.edesc, &udev->ep0.desc,
973                             sizeof(udev->ep0.desc));
974                 }
975         }
976         return (0);
977 }
978
979 /*------------------------------------------------------------------------*
980  *      usb_alloc_urb
981  *
982  * This function should always be used when you allocate an URB for
983  * use with the USB Linux stack. In case of an isochronous transfer
984  * you must specifiy the maximum number of "iso_packets" which you
985  * plan to transfer per URB. This function is always blocking, and
986  * "mem_flags" are not regarded like on Linux.
987  *------------------------------------------------------------------------*/
988 struct urb *
989 usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)
990 {
991         struct urb *urb;
992         usb_size_t size;
993
994         if (iso_packets == 0xFFFF) {
995                 /*
996                  * FreeBSD specific magic value to ask for control transfer
997                  * memory allocation:
998                  */
999                 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags;
1000         } else {
1001                 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0]));
1002         }
1003
1004         urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);
1005         if (urb) {
1006
1007                 cv_init(&urb->cv_wait, "URBWAIT");
1008                 if (iso_packets == 0xFFFF) {
1009                         urb->setup_packet = (void *)(urb + 1);
1010                         urb->transfer_buffer = (void *)(urb->setup_packet +
1011                             sizeof(struct usb_device_request));
1012                 } else {
1013                         urb->number_of_packets = iso_packets;
1014                 }
1015         }
1016         return (urb);
1017 }
1018
1019 /*------------------------------------------------------------------------*
1020  *      usb_find_host_endpoint
1021  *
1022  * The following function will return the Linux USB host endpoint
1023  * structure that matches the given endpoint type and endpoint
1024  * value. If no match is found, NULL is returned. This function is not
1025  * part of the Linux USB API and is only used internally.
1026  *------------------------------------------------------------------------*/
1027 struct usb_host_endpoint *
1028 usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep)
1029 {
1030         struct usb_host_endpoint *uhe;
1031         struct usb_host_endpoint *uhe_end;
1032         struct usb_host_interface *uhi;
1033         struct usb_interface *ui;
1034         uint8_t ea;
1035         uint8_t at;
1036         uint8_t mask;
1037
1038         if (dev == NULL) {
1039                 return (NULL);
1040         }
1041         if (type == UE_CONTROL) {
1042                 mask = UE_ADDR;
1043         } else {
1044                 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR);
1045         }
1046
1047         ep &= mask;
1048
1049         /*
1050          * Iterate over all the interfaces searching the selected alternate
1051          * setting only, and all belonging endpoints.
1052          */
1053         for (ui = dev->linux_iface_start;
1054             ui != dev->linux_iface_end;
1055             ui++) {
1056                 uhi = ui->cur_altsetting;
1057                 if (uhi) {
1058                         uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1059                         for (uhe = uhi->endpoint;
1060                             uhe != uhe_end;
1061                             uhe++) {
1062                                 ea = uhe->desc.bEndpointAddress;
1063                                 at = uhe->desc.bmAttributes;
1064
1065                                 if (((ea & mask) == ep) &&
1066                                     ((at & UE_XFERTYPE) == type)) {
1067                                         return (uhe);
1068                                 }
1069                         }
1070                 }
1071         }
1072
1073         if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) {
1074                 return (&dev->ep0);
1075         }
1076         return (NULL);
1077 }
1078
1079 /*------------------------------------------------------------------------*
1080  *      usb_altnum_to_altsetting
1081  *
1082  * The following function returns a pointer to an alternate setting by
1083  * index given a "usb_interface" pointer. If the alternate setting by
1084  * index does not exist, NULL is returned. And alternate setting is a
1085  * variant of an interface, but usually with slightly different
1086  * characteristics.
1087  *------------------------------------------------------------------------*/
1088 struct usb_host_interface *
1089 usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index)
1090 {
1091         if (alt_index >= intf->num_altsetting) {
1092                 return (NULL);
1093         }
1094         return (intf->altsetting + alt_index);
1095 }
1096
1097 /*------------------------------------------------------------------------*
1098  *      usb_ifnum_to_if
1099  *
1100  * The following function searches up an USB interface by
1101  * "bInterfaceNumber". If no match is found, NULL is returned.
1102  *------------------------------------------------------------------------*/
1103 struct usb_interface *
1104 usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no)
1105 {
1106         struct usb_interface *p_ui;
1107
1108         for (p_ui = dev->linux_iface_start;
1109             p_ui != dev->linux_iface_end;
1110             p_ui++) {
1111                 if ((p_ui->num_altsetting > 0) &&
1112                     (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) {
1113                         return (p_ui);
1114                 }
1115         }
1116         return (NULL);
1117 }
1118
1119 /*------------------------------------------------------------------------*
1120  *      usb_buffer_alloc
1121  *------------------------------------------------------------------------*/
1122 void   *
1123 usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr)
1124 {
1125         return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO));
1126 }
1127
1128 /*------------------------------------------------------------------------*
1129  *      usbd_get_intfdata
1130  *------------------------------------------------------------------------*/
1131 void   *
1132 usbd_get_intfdata(struct usb_interface *intf)
1133 {
1134         return (intf->bsd_priv_sc);
1135 }
1136
1137 /*------------------------------------------------------------------------*
1138  *      usb_linux_register
1139  *
1140  * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1141  * and is used to register a Linux USB driver, so that its
1142  * "usb_device_id" structures gets searched a probe time. This
1143  * function is not part of the Linux USB API, and is for internal use
1144  * only.
1145  *------------------------------------------------------------------------*/
1146 void
1147 usb_linux_register(void *arg)
1148 {
1149         struct usb_driver *drv = arg;
1150
1151         mtx_lock(&Giant);
1152         LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list);
1153         mtx_unlock(&Giant);
1154
1155         usb_needs_explore_all();
1156 }
1157
1158 /*------------------------------------------------------------------------*
1159  *      usb_linux_deregister
1160  *
1161  * The following function is used by the "USB_DRIVER_EXPORT()" macro,
1162  * and is used to deregister a Linux USB driver. This function will
1163  * ensure that all driver instances belonging to the Linux USB device
1164  * driver in question, gets detached before the driver is
1165  * unloaded. This function is not part of the Linux USB API, and is
1166  * for internal use only.
1167  *------------------------------------------------------------------------*/
1168 void
1169 usb_linux_deregister(void *arg)
1170 {
1171         struct usb_driver *drv = arg;
1172         struct usb_linux_softc *sc;
1173
1174 repeat:
1175         mtx_lock(&Giant);
1176         LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) {
1177                 if (sc->sc_udrv == drv) {
1178                         mtx_unlock(&Giant);
1179                         device_detach(sc->sc_fbsd_dev);
1180                         goto repeat;
1181                 }
1182         }
1183         LIST_REMOVE(drv, linux_driver_list);
1184         mtx_unlock(&Giant);
1185 }
1186
1187 /*------------------------------------------------------------------------*
1188  *      usb_linux_free_device
1189  *
1190  * The following function is only used by the FreeBSD USB stack, to
1191  * cleanup and free memory after that a Linux USB device was attached.
1192  *------------------------------------------------------------------------*/
1193 void
1194 usb_linux_free_device(struct usb_device *dev)
1195 {
1196         struct usb_host_endpoint *uhe;
1197         struct usb_host_endpoint *uhe_end;
1198         int err;
1199
1200         uhe = dev->linux_endpoint_start;
1201         uhe_end = dev->linux_endpoint_end;
1202         while (uhe != uhe_end) {
1203                 err = usb_setup_endpoint(dev, uhe, 0);
1204                 uhe++;
1205         }
1206         err = usb_setup_endpoint(dev, &dev->ep0, 0);
1207         free(dev->linux_endpoint_start, M_USBDEV);
1208 }
1209
1210 /*------------------------------------------------------------------------*
1211  *      usb_buffer_free
1212  *------------------------------------------------------------------------*/
1213 void
1214 usb_buffer_free(struct usb_device *dev, usb_size_t size,
1215     void *addr, uint8_t dma_addr)
1216 {
1217         free(addr, M_USBDEV);
1218 }
1219
1220 /*------------------------------------------------------------------------*
1221  *      usb_free_urb
1222  *------------------------------------------------------------------------*/
1223 void
1224 usb_free_urb(struct urb *urb)
1225 {
1226         if (urb == NULL) {
1227                 return;
1228         }
1229         /* make sure that the current URB is not active */
1230         usb_kill_urb(urb);
1231
1232         /* destroy condition variable */
1233         cv_destroy(&urb->cv_wait);
1234
1235         /* just free it */
1236         free(urb, M_USBDEV);
1237 }
1238
1239 /*------------------------------------------------------------------------*
1240  *      usb_init_urb
1241  *
1242  * The following function can be used to initialize a custom URB. It
1243  * is not recommended to use this function. Use "usb_alloc_urb()"
1244  * instead.
1245  *------------------------------------------------------------------------*/
1246 void
1247 usb_init_urb(struct urb *urb)
1248 {
1249         if (urb == NULL) {
1250                 return;
1251         }
1252         bzero(urb, sizeof(*urb));
1253 }
1254
1255 /*------------------------------------------------------------------------*
1256  *      usb_kill_urb
1257  *------------------------------------------------------------------------*/
1258 void
1259 usb_kill_urb(struct urb *urb)
1260 {
1261         usb_unlink_urb_sub(urb, 1);
1262 }
1263
1264 /*------------------------------------------------------------------------*
1265  *      usb_set_intfdata
1266  *
1267  * The following function sets the per Linux USB interface private
1268  * data pointer. It is used by most Linux USB device drivers.
1269  *------------------------------------------------------------------------*/
1270 void
1271 usb_set_intfdata(struct usb_interface *intf, void *data)
1272 {
1273         intf->bsd_priv_sc = data;
1274 }
1275
1276 /*------------------------------------------------------------------------*
1277  *      usb_linux_cleanup_interface
1278  *
1279  * The following function will release all FreeBSD USB transfers
1280  * associated with a Linux USB interface. It is for internal use only.
1281  *------------------------------------------------------------------------*/
1282 static void
1283 usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface)
1284 {
1285         struct usb_host_interface *uhi;
1286         struct usb_host_interface *uhi_end;
1287         struct usb_host_endpoint *uhe;
1288         struct usb_host_endpoint *uhe_end;
1289         int err;
1290
1291         uhi = iface->altsetting;
1292         uhi_end = iface->altsetting + iface->num_altsetting;
1293         while (uhi != uhi_end) {
1294                 uhe = uhi->endpoint;
1295                 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;
1296                 while (uhe != uhe_end) {
1297                         err = usb_setup_endpoint(dev, uhe, 0);
1298                         uhe++;
1299                 }
1300                 uhi++;
1301         }
1302 }
1303
1304 /*------------------------------------------------------------------------*
1305  *      usb_linux_wait_complete
1306  *
1307  * The following function is used by "usb_start_wait_urb()" to wake it
1308  * up, when an USB transfer has finished.
1309  *------------------------------------------------------------------------*/
1310 static void
1311 usb_linux_wait_complete(struct urb *urb)
1312 {
1313         if (urb->transfer_flags & URB_IS_SLEEPING) {
1314                 cv_signal(&urb->cv_wait);
1315         }
1316         urb->transfer_flags &= ~URB_WAIT_WAKEUP;
1317 }
1318
1319 /*------------------------------------------------------------------------*
1320  *      usb_linux_complete
1321  *------------------------------------------------------------------------*/
1322 static void
1323 usb_linux_complete(struct usb_xfer *xfer)
1324 {
1325         struct urb *urb;
1326
1327         urb = usbd_xfer_get_priv(xfer);
1328         usbd_xfer_set_priv(xfer, NULL);
1329         if (urb->complete) {
1330                 (urb->complete) (urb);
1331         }
1332 }
1333
1334 /*------------------------------------------------------------------------*
1335  *      usb_linux_isoc_callback
1336  *
1337  * The following is the FreeBSD isochronous USB callback. Isochronous
1338  * frames are USB packets transferred 1000 or 8000 times per second,
1339  * depending on whether a full- or high- speed USB transfer is
1340  * used.
1341  *------------------------------------------------------------------------*/
1342 static void
1343 usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1344 {
1345         usb_frlength_t max_frame = xfer->max_frame_size;
1346         usb_frlength_t offset;
1347         usb_frcount_t x;
1348         struct urb *urb = usbd_xfer_get_priv(xfer);
1349         struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1350         struct usb_iso_packet_descriptor *uipd;
1351
1352         DPRINTF("\n");
1353
1354         switch (USB_GET_STATE(xfer)) {
1355         case USB_ST_TRANSFERRED:
1356
1357                 if (urb->bsd_isread) {
1358
1359                         /* copy in data with regard to the URB */
1360
1361                         offset = 0;
1362
1363                         for (x = 0; x < urb->number_of_packets; x++) {
1364                                 uipd = urb->iso_frame_desc + x;
1365                                 uipd->actual_length = xfer->frlengths[x];
1366                                 uipd->status = 0;
1367                                 if (!xfer->flags.ext_buffer) {
1368                                         usbd_copy_out(xfer->frbuffers, offset,
1369                                             USB_ADD_BYTES(urb->transfer_buffer,
1370                                             uipd->offset), uipd->actual_length);
1371                                 }
1372                                 offset += max_frame;
1373                         }
1374                 } else {
1375                         for (x = 0; x < urb->number_of_packets; x++) {
1376                                 uipd = urb->iso_frame_desc + x;
1377                                 uipd->actual_length = xfer->frlengths[x];
1378                                 uipd->status = 0;
1379                         }
1380                 }
1381
1382                 urb->actual_length = xfer->actlen;
1383
1384                 /* check for short transfer */
1385                 if (xfer->actlen < xfer->sumlen) {
1386                         /* short transfer */
1387                         if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1388                                 urb->status = -EPIPE;   /* XXX should be
1389                                                          * EREMOTEIO */
1390                         } else {
1391                                 urb->status = 0;
1392                         }
1393                 } else {
1394                         /* success */
1395                         urb->status = 0;
1396                 }
1397
1398                 /* call callback */
1399                 usb_linux_complete(xfer);
1400
1401         case USB_ST_SETUP:
1402 tr_setup:
1403
1404                 if (xfer->priv_fifo == NULL) {
1405
1406                         /* get next transfer */
1407                         urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1408                         if (urb == NULL) {
1409                                 /* nothing to do */
1410                                 return;
1411                         }
1412                         TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1413                         urb->bsd_urb_list.tqe_prev = NULL;
1414
1415                         x = xfer->max_frame_count;
1416                         if (urb->number_of_packets > x) {
1417                                 /* XXX simply truncate the transfer */
1418                                 urb->number_of_packets = x;
1419                         }
1420                 } else {
1421                         DPRINTF("Already got a transfer\n");
1422
1423                         /* already got a transfer (should not happen) */
1424                         urb = usbd_xfer_get_priv(xfer);
1425                 }
1426
1427                 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0;
1428
1429                 if (xfer->flags.ext_buffer) {
1430                         /* set virtual address to load */
1431                         usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0);
1432                 }
1433                 if (!(urb->bsd_isread)) {
1434
1435                         /* copy out data with regard to the URB */
1436
1437                         offset = 0;
1438
1439                         for (x = 0; x < urb->number_of_packets; x++) {
1440                                 uipd = urb->iso_frame_desc + x;
1441                                 usbd_xfer_set_frame_len(xfer, x, uipd->length);
1442                                 if (!xfer->flags.ext_buffer) {
1443                                         usbd_copy_in(xfer->frbuffers, offset,
1444                                             USB_ADD_BYTES(urb->transfer_buffer,
1445                                             uipd->offset), uipd->length);
1446                                 }
1447                                 offset += uipd->length;
1448                         }
1449                 } else {
1450
1451                         /*
1452                          * compute the transfer length into the "offset"
1453                          * variable
1454                          */
1455
1456                         offset = urb->number_of_packets * max_frame;
1457
1458                         /* setup "frlengths" array */
1459
1460                         for (x = 0; x < urb->number_of_packets; x++) {
1461                                 uipd = urb->iso_frame_desc + x;
1462                                 usbd_xfer_set_frame_len(xfer, x, max_frame);
1463                         }
1464                 }
1465                 usbd_xfer_set_priv(xfer, urb);
1466                 xfer->flags.force_short_xfer = 0;
1467                 xfer->timeout = urb->timeout;
1468                 xfer->nframes = urb->number_of_packets;
1469                 usbd_transfer_submit(xfer);
1470                 return;
1471
1472         default:                        /* Error */
1473                 if (xfer->error == USB_ERR_CANCELLED) {
1474                         urb->status = -ECONNRESET;
1475                 } else {
1476                         urb->status = -EPIPE;   /* stalled */
1477                 }
1478
1479                 /* Set zero for "actual_length" */
1480                 urb->actual_length = 0;
1481
1482                 /* Set zero for "actual_length" */
1483                 for (x = 0; x < urb->number_of_packets; x++) {
1484                         urb->iso_frame_desc[x].actual_length = 0;
1485                 }
1486
1487                 /* call callback */
1488                 usb_linux_complete(xfer);
1489
1490                 if (xfer->error == USB_ERR_CANCELLED) {
1491                         /* we need to return in this case */
1492                         return;
1493                 }
1494                 goto tr_setup;
1495
1496         }
1497 }
1498
1499 /*------------------------------------------------------------------------*
1500  *      usb_linux_non_isoc_callback
1501  *
1502  * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB
1503  * callback. It dequeues Linux USB stack compatible URB's, transforms
1504  * the URB fields into a FreeBSD USB transfer, and defragments the USB
1505  * transfer as required. When the transfer is complete the "complete"
1506  * callback is called.
1507  *------------------------------------------------------------------------*/
1508 static void
1509 usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error)
1510 {
1511         enum {
1512                 REQ_SIZE = sizeof(struct usb_device_request)
1513         };
1514         struct urb *urb = usbd_xfer_get_priv(xfer);
1515         struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);
1516         uint8_t *ptr;
1517         usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);
1518         uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0;
1519
1520         DPRINTF("\n");
1521
1522         switch (USB_GET_STATE(xfer)) {
1523         case USB_ST_TRANSFERRED:
1524
1525                 if (xfer->flags_int.control_xfr) {
1526
1527                         /* don't transfer the setup packet again: */
1528
1529                         usbd_xfer_set_frame_len(xfer, 0, 0);
1530                 }
1531                 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) {
1532                         /* copy in data with regard to the URB */
1533                         usbd_copy_out(xfer->frbuffers + data_frame, 0,
1534                             urb->bsd_data_ptr, xfer->frlengths[data_frame]);
1535                 }
1536                 urb->bsd_length_rem -= xfer->frlengths[data_frame];
1537                 urb->bsd_data_ptr += xfer->frlengths[data_frame];
1538                 urb->actual_length += xfer->frlengths[data_frame];
1539
1540                 /* check for short transfer */
1541                 if (xfer->actlen < xfer->sumlen) {
1542                         urb->bsd_length_rem = 0;
1543
1544                         /* short transfer */
1545                         if (urb->transfer_flags & URB_SHORT_NOT_OK) {
1546                                 urb->status = -EPIPE;
1547                         } else {
1548                                 urb->status = 0;
1549                         }
1550                 } else {
1551                         /* check remainder */
1552                         if (urb->bsd_length_rem > 0) {
1553                                 goto setup_bulk;
1554                         }
1555                         /* success */
1556                         urb->status = 0;
1557                 }
1558
1559                 /* call callback */
1560                 usb_linux_complete(xfer);
1561
1562         case USB_ST_SETUP:
1563 tr_setup:
1564                 /* get next transfer */
1565                 urb = TAILQ_FIRST(&uhe->bsd_urb_list);
1566                 if (urb == NULL) {
1567                         /* nothing to do */
1568                         return;
1569                 }
1570                 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);
1571                 urb->bsd_urb_list.tqe_prev = NULL;
1572
1573                 usbd_xfer_set_priv(xfer, urb);
1574                 xfer->flags.force_short_xfer = 0;
1575                 xfer->timeout = urb->timeout;
1576
1577                 if (xfer->flags_int.control_xfr) {
1578
1579                         /*
1580                          * USB control transfers need special handling.
1581                          * First copy in the header, then copy in data!
1582                          */
1583                         if (!xfer->flags.ext_buffer) {
1584                                 usbd_copy_in(xfer->frbuffers, 0,
1585                                     urb->setup_packet, REQ_SIZE);
1586                                 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE);
1587                         } else {
1588                                 /* set virtual address to load */
1589                                 usbd_xfer_set_frame_data(xfer, 0,
1590                                     urb->setup_packet, REQ_SIZE);
1591                         }
1592
1593                         ptr = urb->setup_packet;
1594
1595                         /* setup data transfer direction and length */
1596                         urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0;
1597                         urb->bsd_length_rem = ptr[6] | (ptr[7] << 8);
1598
1599                 } else {
1600
1601                         /* setup data transfer direction */
1602
1603                         urb->bsd_length_rem = urb->transfer_buffer_length;
1604                         urb->bsd_isread = (uhe->desc.bEndpointAddress &
1605                             UE_DIR_IN) ? 1 : 0;
1606                 }
1607
1608                 urb->bsd_data_ptr = urb->transfer_buffer;
1609                 urb->actual_length = 0;
1610
1611 setup_bulk:
1612                 if (max_bulk > urb->bsd_length_rem) {
1613                         max_bulk = urb->bsd_length_rem;
1614                 }
1615                 /* check if we need to force a short transfer */
1616
1617                 if ((max_bulk == urb->bsd_length_rem) &&
1618                     (urb->transfer_flags & URB_ZERO_PACKET) &&
1619                     (!xfer->flags_int.control_xfr)) {
1620                         xfer->flags.force_short_xfer = 1;
1621                 }
1622                 /* check if we need to copy in data */
1623
1624                 if (xfer->flags.ext_buffer) {
1625                         /* set virtual address to load */
1626                         usbd_xfer_set_frame_data(xfer, data_frame,
1627                             urb->bsd_data_ptr, max_bulk);
1628                 } else if (!urb->bsd_isread) {
1629                         /* copy out data with regard to the URB */
1630                         usbd_copy_in(xfer->frbuffers + data_frame, 0,
1631                             urb->bsd_data_ptr, max_bulk);
1632                         usbd_xfer_set_frame_len(xfer, data_frame, max_bulk);
1633                 }
1634                 if (xfer->flags_int.control_xfr) {
1635                         if (max_bulk > 0) {
1636                                 xfer->nframes = 2;
1637                         } else {
1638                                 xfer->nframes = 1;
1639                         }
1640                 } else {
1641                         xfer->nframes = 1;
1642                 }
1643                 usbd_transfer_submit(xfer);
1644                 return;
1645
1646         default:
1647                 if (xfer->error == USB_ERR_CANCELLED) {
1648                         urb->status = -ECONNRESET;
1649                 } else {
1650                         urb->status = -EPIPE;
1651                 }
1652
1653                 /* Set zero for "actual_length" */
1654                 urb->actual_length = 0;
1655
1656                 /* call callback */
1657                 usb_linux_complete(xfer);
1658
1659                 if (xfer->error == USB_ERR_CANCELLED) {
1660                         /* we need to return in this case */
1661                         return;
1662                 }
1663                 goto tr_setup;
1664         }
1665 }