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