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