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