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