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