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