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