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