1 /* $FreeBSD: stable/9/sys/dev/usb/usb_dev.c 301254 2016-06-03 08:56:54Z hselasky $ */
2 /*-
3 * Copyright (c) 2006-2008 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 * usb_dev.c - An abstraction layer for creating devices under /dev/...
28 */
29
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/module.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
43 #include <sys/sx.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
47 #include <sys/priv.h>
48 #include <sys/vnode.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_ioctl.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56
57 #define USB_DEBUG_VAR usb_fifo_debug
58
59 #include <dev/usb/usb_core.h>
60 #include <dev/usb/usb_dev.h>
61 #include <dev/usb/usb_mbuf.h>
62 #include <dev/usb/usb_process.h>
63 #include <dev/usb/usb_device.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_generic.h>
67 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_util.h>
69
70 #include <dev/usb/usb_controller.h>
71 #include <dev/usb/usb_bus.h>
72
73 #include <sys/filio.h>
74 #include <sys/ttycom.h>
75 #include <sys/syscallsubr.h>
76
77 #include <machine/stdarg.h>
78
79 #if USB_HAVE_UGEN
80
81 #ifdef USB_DEBUG
82 static int usb_fifo_debug = 0;
83
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
85 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
86 &usb_fifo_debug, 0, "Debug Level");
87 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
88 #endif
89
90 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
91 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
92 #define USB_UCRED struct ucred *ucred,
93 #else
94 #define USB_UCRED
95 #endif
96
97 /* prototypes */
98
99 static int usb_fifo_open(struct usb_cdev_privdata *,
100 struct usb_fifo *, int);
101 static void usb_fifo_close(struct usb_fifo *, int);
102 static void usb_dev_init(void *);
103 static void usb_dev_init_post(void *);
104 static void usb_dev_uninit(void *);
105 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
106 struct uio *);
107 static void usb_fifo_check_methods(struct usb_fifo_methods *);
108 static struct usb_fifo *usb_fifo_alloc(struct mtx *);
109 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
110 uint8_t);
111 static void usb_loc_fill(struct usb_fs_privdata *,
112 struct usb_cdev_privdata *);
113 static void usb_close(void *);
114 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
115 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
116 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
117
118 static d_open_t usb_open;
119 static d_ioctl_t usb_ioctl;
120 static d_read_t usb_read;
121 static d_write_t usb_write;
122 static d_poll_t usb_poll;
123 static d_kqfilter_t usb_kqfilter;
124
125 static d_ioctl_t usb_static_ioctl;
126
127 static usb_fifo_open_t usb_fifo_dummy_open;
128 static usb_fifo_close_t usb_fifo_dummy_close;
129 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
130 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
131
132 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
133 struct cdevsw usb_devsw = {
134 .d_version = D_VERSION,
135 .d_open = usb_open,
136 .d_ioctl = usb_ioctl,
137 .d_name = "usbdev",
138 .d_flags = D_TRACKCLOSE,
139 .d_read = usb_read,
140 .d_write = usb_write,
141 .d_poll = usb_poll,
142 .d_kqfilter = usb_kqfilter,
143 };
144
145 static struct cdev* usb_dev = NULL;
146
147 /* character device structure used for /dev/usb */
148 static struct cdevsw usb_static_devsw = {
149 .d_version = D_VERSION,
150 .d_ioctl = usb_static_ioctl,
151 .d_name = "usb"
152 };
153
154 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
155 static struct sx usb_sym_lock;
156
157 struct mtx usb_ref_lock;
158
159 /*------------------------------------------------------------------------*
160 * usb_loc_fill
161 *
162 * This is used to fill out a usb_cdev_privdata structure based on the
163 * device's address as contained in usb_fs_privdata.
164 *------------------------------------------------------------------------*/
165 static void
usb_loc_fill(struct usb_fs_privdata * pd,struct usb_cdev_privdata * cpd)166 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
167 {
168 cpd->bus_index = pd->bus_index;
169 cpd->dev_index = pd->dev_index;
170 cpd->ep_addr = pd->ep_addr;
171 cpd->fifo_index = pd->fifo_index;
172 }
173
174 /*------------------------------------------------------------------------*
175 * usb_ref_device
176 *
177 * This function is used to atomically refer an USB device by its
178 * device location. If this function returns success the USB device
179 * will not dissappear until the USB device is unreferenced.
180 *
181 * Return values:
182 * 0: Success, refcount incremented on the given USB device.
183 * Else: Failure.
184 *------------------------------------------------------------------------*/
185 static usb_error_t
usb_ref_device(struct usb_cdev_privdata * cpd,struct usb_cdev_refdata * crd,int need_uref)186 usb_ref_device(struct usb_cdev_privdata *cpd,
187 struct usb_cdev_refdata *crd, int need_uref)
188 {
189 struct usb_fifo **ppf;
190 struct usb_fifo *f;
191
192 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
193
194 /* clear all refs */
195 memset(crd, 0, sizeof(*crd));
196
197 mtx_lock(&usb_ref_lock);
198 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
199 if (cpd->bus == NULL) {
200 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
201 goto error;
202 }
203 cpd->udev = cpd->bus->devices[cpd->dev_index];
204 if (cpd->udev == NULL) {
205 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
206 goto error;
207 }
208 if (cpd->udev->state == USB_STATE_DETACHED &&
209 (need_uref != 2)) {
210 DPRINTFN(2, "device is detached\n");
211 goto error;
212 }
213 if (need_uref) {
214 DPRINTFN(2, "ref udev - needed\n");
215
216 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
217 DPRINTFN(2, "no dev ref\n");
218 goto error;
219 }
220 cpd->udev->refcount++;
221
222 mtx_unlock(&usb_ref_lock);
223
224 /*
225 * We need to grab the enumeration SX-lock before
226 * grabbing the FIFO refs to avoid deadlock at detach!
227 */
228 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
229
230 mtx_lock(&usb_ref_lock);
231
232 /*
233 * Set "is_uref" after grabbing the default SX lock
234 */
235 crd->is_uref = 1;
236
237 /* check for signal */
238 if (crd->do_unlock > 1) {
239 crd->do_unlock = 0;
240 goto error;
241 }
242 }
243
244 /* check if we are doing an open */
245 if (cpd->fflags == 0) {
246 /* use zero defaults */
247 } else {
248 /* check for write */
249 if (cpd->fflags & FWRITE) {
250 ppf = cpd->udev->fifo;
251 f = ppf[cpd->fifo_index + USB_FIFO_TX];
252 crd->txfifo = f;
253 crd->is_write = 1; /* ref */
254 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
255 goto error;
256 if (f->curr_cpd != cpd)
257 goto error;
258 /* check if USB-FS is active */
259 if (f->fs_ep_max != 0) {
260 crd->is_usbfs = 1;
261 }
262 }
263
264 /* check for read */
265 if (cpd->fflags & FREAD) {
266 ppf = cpd->udev->fifo;
267 f = ppf[cpd->fifo_index + USB_FIFO_RX];
268 crd->rxfifo = f;
269 crd->is_read = 1; /* ref */
270 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
271 goto error;
272 if (f->curr_cpd != cpd)
273 goto error;
274 /* check if USB-FS is active */
275 if (f->fs_ep_max != 0) {
276 crd->is_usbfs = 1;
277 }
278 }
279 }
280
281 /* when everything is OK we increment the refcounts */
282 if (crd->is_write) {
283 DPRINTFN(2, "ref write\n");
284 crd->txfifo->refcount++;
285 }
286 if (crd->is_read) {
287 DPRINTFN(2, "ref read\n");
288 crd->rxfifo->refcount++;
289 }
290 mtx_unlock(&usb_ref_lock);
291
292 return (0);
293
294 error:
295 if (crd->do_unlock)
296 usbd_enum_unlock(cpd->udev);
297
298 if (crd->is_uref) {
299 if (--(cpd->udev->refcount) == 0)
300 cv_broadcast(&cpd->udev->ref_cv);
301 }
302 mtx_unlock(&usb_ref_lock);
303 DPRINTFN(2, "fail\n");
304
305 /* clear all refs */
306 memset(crd, 0, sizeof(*crd));
307
308 return (USB_ERR_INVAL);
309 }
310
311 /*------------------------------------------------------------------------*
312 * usb_usb_ref_device
313 *
314 * This function is used to upgrade an USB reference to include the
315 * USB device reference on a USB location.
316 *
317 * Return values:
318 * 0: Success, refcount incremented on the given USB device.
319 * Else: Failure.
320 *------------------------------------------------------------------------*/
321 static usb_error_t
usb_usb_ref_device(struct usb_cdev_privdata * cpd,struct usb_cdev_refdata * crd)322 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
323 struct usb_cdev_refdata *crd)
324 {
325 /*
326 * Check if we already got an USB reference on this location:
327 */
328 if (crd->is_uref)
329 return (0); /* success */
330
331 /*
332 * To avoid deadlock at detach we need to drop the FIFO ref
333 * and re-acquire a new ref!
334 */
335 usb_unref_device(cpd, crd);
336
337 return (usb_ref_device(cpd, crd, 1 /* need uref */));
338 }
339
340 /*------------------------------------------------------------------------*
341 * usb_unref_device
342 *
343 * This function will release the reference count by one unit for the
344 * given USB device.
345 *------------------------------------------------------------------------*/
346 static void
usb_unref_device(struct usb_cdev_privdata * cpd,struct usb_cdev_refdata * crd)347 usb_unref_device(struct usb_cdev_privdata *cpd,
348 struct usb_cdev_refdata *crd)
349 {
350
351 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
352
353 if (crd->do_unlock)
354 usbd_enum_unlock(cpd->udev);
355
356 mtx_lock(&usb_ref_lock);
357 if (crd->is_read) {
358 if (--(crd->rxfifo->refcount) == 0) {
359 cv_signal(&crd->rxfifo->cv_drain);
360 }
361 crd->is_read = 0;
362 }
363 if (crd->is_write) {
364 if (--(crd->txfifo->refcount) == 0) {
365 cv_signal(&crd->txfifo->cv_drain);
366 }
367 crd->is_write = 0;
368 }
369 if (crd->is_uref) {
370 crd->is_uref = 0;
371 if (--(cpd->udev->refcount) == 0)
372 cv_broadcast(&cpd->udev->ref_cv);
373 }
374 mtx_unlock(&usb_ref_lock);
375 }
376
377 static struct usb_fifo *
usb_fifo_alloc(struct mtx * mtx)378 usb_fifo_alloc(struct mtx *mtx)
379 {
380 struct usb_fifo *f;
381
382 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
383 if (f != NULL) {
384 cv_init(&f->cv_io, "FIFO-IO");
385 cv_init(&f->cv_drain, "FIFO-DRAIN");
386 f->priv_mtx = mtx;
387 f->refcount = 1;
388 knlist_init_mtx(&f->selinfo.si_note, mtx);
389 }
390 return (f);
391 }
392
393 /*------------------------------------------------------------------------*
394 * usb_fifo_create
395 *------------------------------------------------------------------------*/
396 static int
usb_fifo_create(struct usb_cdev_privdata * cpd,struct usb_cdev_refdata * crd)397 usb_fifo_create(struct usb_cdev_privdata *cpd,
398 struct usb_cdev_refdata *crd)
399 {
400 struct usb_device *udev = cpd->udev;
401 struct usb_fifo *f;
402 struct usb_endpoint *ep;
403 uint8_t n;
404 uint8_t is_tx;
405 uint8_t is_rx;
406 uint8_t no_null;
407 uint8_t is_busy;
408 int e = cpd->ep_addr;
409
410 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
411 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
412 no_null = 1;
413 is_busy = 0;
414
415 /* Preallocated FIFO */
416 if (e < 0) {
417 DPRINTFN(5, "Preallocated FIFO\n");
418 if (is_tx) {
419 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
420 if (f == NULL)
421 return (EINVAL);
422 crd->txfifo = f;
423 }
424 if (is_rx) {
425 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
426 if (f == NULL)
427 return (EINVAL);
428 crd->rxfifo = f;
429 }
430 return (0);
431 }
432
433 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
434
435 /* search for a free FIFO slot */
436 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
437 for (n = 0;; n += 2) {
438
439 if (n == USB_FIFO_MAX) {
440 if (no_null) {
441 no_null = 0;
442 n = 0;
443 } else {
444 /* end of FIFOs reached */
445 DPRINTFN(5, "out of FIFOs\n");
446 return (ENOMEM);
447 }
448 }
449 /* Check for TX FIFO */
450 if (is_tx) {
451 f = udev->fifo[n + USB_FIFO_TX];
452 if (f != NULL) {
453 if (f->dev_ep_index != e) {
454 /* wrong endpoint index */
455 continue;
456 }
457 if (f->curr_cpd != NULL) {
458 /* FIFO is opened */
459 is_busy = 1;
460 continue;
461 }
462 } else if (no_null) {
463 continue;
464 }
465 }
466 /* Check for RX FIFO */
467 if (is_rx) {
468 f = udev->fifo[n + USB_FIFO_RX];
469 if (f != NULL) {
470 if (f->dev_ep_index != e) {
471 /* wrong endpoint index */
472 continue;
473 }
474 if (f->curr_cpd != NULL) {
475 /* FIFO is opened */
476 is_busy = 1;
477 continue;
478 }
479 } else if (no_null) {
480 continue;
481 }
482 }
483 break;
484 }
485
486 if (no_null == 0) {
487 if (e >= (USB_EP_MAX / 2)) {
488 /* we don't create any endpoints in this range */
489 DPRINTFN(5, "ep out of range\n");
490 return (is_busy ? EBUSY : EINVAL);
491 }
492 }
493
494 if ((e != 0) && is_busy) {
495 /*
496 * Only the default control endpoint is allowed to be
497 * opened multiple times!
498 */
499 DPRINTFN(5, "busy\n");
500 return (EBUSY);
501 }
502
503 /* Check TX FIFO */
504 if (is_tx &&
505 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
506 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
507 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
508 if (ep == NULL) {
509 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
510 return (EINVAL);
511 }
512 f = usb_fifo_alloc(&udev->device_mtx);
513 if (f == NULL) {
514 DPRINTFN(5, "could not alloc tx fifo\n");
515 return (ENOMEM);
516 }
517 /* update some fields */
518 f->fifo_index = n + USB_FIFO_TX;
519 f->dev_ep_index = e;
520 f->priv_sc0 = ep;
521 f->methods = &usb_ugen_methods;
522 f->iface_index = ep->iface_index;
523 f->udev = udev;
524 mtx_lock(&usb_ref_lock);
525 udev->fifo[n + USB_FIFO_TX] = f;
526 mtx_unlock(&usb_ref_lock);
527 }
528 /* Check RX FIFO */
529 if (is_rx &&
530 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
531
532 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
533 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
534 if (ep == NULL) {
535 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
536 return (EINVAL);
537 }
538 f = usb_fifo_alloc(&udev->device_mtx);
539 if (f == NULL) {
540 DPRINTFN(5, "could not alloc rx fifo\n");
541 return (ENOMEM);
542 }
543 /* update some fields */
544 f->fifo_index = n + USB_FIFO_RX;
545 f->dev_ep_index = e;
546 f->priv_sc0 = ep;
547 f->methods = &usb_ugen_methods;
548 f->iface_index = ep->iface_index;
549 f->udev = udev;
550 mtx_lock(&usb_ref_lock);
551 udev->fifo[n + USB_FIFO_RX] = f;
552 mtx_unlock(&usb_ref_lock);
553 }
554 if (is_tx) {
555 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
556 }
557 if (is_rx) {
558 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
559 }
560 /* fill out fifo index */
561 DPRINTFN(5, "fifo index = %d\n", n);
562 cpd->fifo_index = n;
563
564 /* complete */
565
566 return (0);
567 }
568
569 void
usb_fifo_free(struct usb_fifo * f)570 usb_fifo_free(struct usb_fifo *f)
571 {
572 uint8_t n;
573
574 if (f == NULL) {
575 /* be NULL safe */
576 return;
577 }
578 /* destroy symlink devices, if any */
579 for (n = 0; n != 2; n++) {
580 if (f->symlink[n]) {
581 usb_free_symlink(f->symlink[n]);
582 f->symlink[n] = NULL;
583 }
584 }
585 mtx_lock(&usb_ref_lock);
586
587 /* delink ourselves to stop calls from userland */
588 if ((f->fifo_index < USB_FIFO_MAX) &&
589 (f->udev != NULL) &&
590 (f->udev->fifo[f->fifo_index] == f)) {
591 f->udev->fifo[f->fifo_index] = NULL;
592 } else {
593 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
594 }
595
596 /* decrease refcount */
597 f->refcount--;
598 /* need to wait until all callers have exited */
599 while (f->refcount != 0) {
600 mtx_unlock(&usb_ref_lock); /* avoid LOR */
601 mtx_lock(f->priv_mtx);
602 /* prevent write flush, if any */
603 f->flag_iserror = 1;
604 /* get I/O thread out of any sleep state */
605 if (f->flag_sleeping) {
606 f->flag_sleeping = 0;
607 cv_broadcast(&f->cv_io);
608 }
609 mtx_unlock(f->priv_mtx);
610 mtx_lock(&usb_ref_lock);
611
612 /*
613 * Check if the "f->refcount" variable reached zero
614 * during the unlocked time before entering wait:
615 */
616 if (f->refcount == 0)
617 break;
618
619 /* wait for sync */
620 cv_wait(&f->cv_drain, &usb_ref_lock);
621 }
622 mtx_unlock(&usb_ref_lock);
623
624 /* take care of closing the device here, if any */
625 usb_fifo_close(f, 0);
626
627 cv_destroy(&f->cv_io);
628 cv_destroy(&f->cv_drain);
629
630 knlist_clear(&f->selinfo.si_note, 0);
631 seldrain(&f->selinfo);
632 knlist_destroy(&f->selinfo.si_note);
633
634 free(f, M_USBDEV);
635 }
636
637 static struct usb_endpoint *
usb_dev_get_ep(struct usb_device * udev,uint8_t ep_index,uint8_t dir)638 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
639 {
640 struct usb_endpoint *ep;
641 uint8_t ep_dir;
642
643 if (ep_index == 0) {
644 ep = &udev->ctrl_ep;
645 } else {
646 if (dir == USB_FIFO_RX) {
647 if (udev->flags.usb_mode == USB_MODE_HOST) {
648 ep_dir = UE_DIR_IN;
649 } else {
650 ep_dir = UE_DIR_OUT;
651 }
652 } else {
653 if (udev->flags.usb_mode == USB_MODE_HOST) {
654 ep_dir = UE_DIR_OUT;
655 } else {
656 ep_dir = UE_DIR_IN;
657 }
658 }
659 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
660 }
661
662 if (ep == NULL) {
663 /* if the endpoint does not exist then return */
664 return (NULL);
665 }
666 if (ep->edesc == NULL) {
667 /* invalid endpoint */
668 return (NULL);
669 }
670 return (ep); /* success */
671 }
672
673 /*------------------------------------------------------------------------*
674 * usb_fifo_open
675 *
676 * Returns:
677 * 0: Success
678 * Else: Failure
679 *------------------------------------------------------------------------*/
680 static int
usb_fifo_open(struct usb_cdev_privdata * cpd,struct usb_fifo * f,int fflags)681 usb_fifo_open(struct usb_cdev_privdata *cpd,
682 struct usb_fifo *f, int fflags)
683 {
684 int err;
685
686 if (f == NULL) {
687 /* no FIFO there */
688 DPRINTFN(2, "no FIFO\n");
689 return (ENXIO);
690 }
691 /* remove FWRITE and FREAD flags */
692 fflags &= ~(FWRITE | FREAD);
693
694 /* set correct file flags */
695 if ((f->fifo_index & 1) == USB_FIFO_TX) {
696 fflags |= FWRITE;
697 } else {
698 fflags |= FREAD;
699 }
700
701 /* check if we are already opened */
702 /* we don't need any locks when checking this variable */
703 if (f->curr_cpd != NULL) {
704 err = EBUSY;
705 goto done;
706 }
707
708 /* reset short flag before open */
709 f->flag_short = 0;
710
711 /* call open method */
712 err = (f->methods->f_open) (f, fflags);
713 if (err) {
714 goto done;
715 }
716 mtx_lock(f->priv_mtx);
717
718 /* reset sleep flag */
719 f->flag_sleeping = 0;
720
721 /* reset error flag */
722 f->flag_iserror = 0;
723
724 /* reset complete flag */
725 f->flag_iscomplete = 0;
726
727 /* reset select flag */
728 f->flag_isselect = 0;
729
730 /* reset flushing flag */
731 f->flag_flushing = 0;
732
733 /* reset ASYNC proc flag */
734 f->async_p = NULL;
735
736 mtx_lock(&usb_ref_lock);
737 /* flag the fifo as opened to prevent others */
738 f->curr_cpd = cpd;
739 mtx_unlock(&usb_ref_lock);
740
741 /* reset queue */
742 usb_fifo_reset(f);
743
744 mtx_unlock(f->priv_mtx);
745 done:
746 return (err);
747 }
748
749 /*------------------------------------------------------------------------*
750 * usb_fifo_reset
751 *------------------------------------------------------------------------*/
752 void
usb_fifo_reset(struct usb_fifo * f)753 usb_fifo_reset(struct usb_fifo *f)
754 {
755 struct usb_mbuf *m;
756
757 if (f == NULL) {
758 return;
759 }
760 while (1) {
761 USB_IF_DEQUEUE(&f->used_q, m);
762 if (m) {
763 USB_IF_ENQUEUE(&f->free_q, m);
764 } else {
765 break;
766 }
767 }
768 /* reset have fragment flag */
769 f->flag_have_fragment = 0;
770 }
771
772 /*------------------------------------------------------------------------*
773 * usb_fifo_close
774 *------------------------------------------------------------------------*/
775 static void
usb_fifo_close(struct usb_fifo * f,int fflags)776 usb_fifo_close(struct usb_fifo *f, int fflags)
777 {
778 int err;
779
780 /* check if we are not opened */
781 if (f->curr_cpd == NULL) {
782 /* nothing to do - already closed */
783 return;
784 }
785 mtx_lock(f->priv_mtx);
786
787 /* clear current cdev private data pointer */
788 mtx_lock(&usb_ref_lock);
789 f->curr_cpd = NULL;
790 mtx_unlock(&usb_ref_lock);
791
792 /* check if we are watched by kevent */
793 KNOTE_LOCKED(&f->selinfo.si_note, 0);
794
795 /* check if we are selected */
796 if (f->flag_isselect) {
797 selwakeup(&f->selinfo);
798 f->flag_isselect = 0;
799 }
800 /* check if a thread wants SIGIO */
801 if (f->async_p != NULL) {
802 PROC_LOCK(f->async_p);
803 kern_psignal(f->async_p, SIGIO);
804 PROC_UNLOCK(f->async_p);
805 f->async_p = NULL;
806 }
807 /* remove FWRITE and FREAD flags */
808 fflags &= ~(FWRITE | FREAD);
809
810 /* flush written data, if any */
811 if ((f->fifo_index & 1) == USB_FIFO_TX) {
812
813 if (!f->flag_iserror) {
814
815 /* set flushing flag */
816 f->flag_flushing = 1;
817
818 /* get the last packet in */
819 if (f->flag_have_fragment) {
820 struct usb_mbuf *m;
821 f->flag_have_fragment = 0;
822 USB_IF_DEQUEUE(&f->free_q, m);
823 if (m) {
824 USB_IF_ENQUEUE(&f->used_q, m);
825 }
826 }
827
828 /* start write transfer, if not already started */
829 (f->methods->f_start_write) (f);
830
831 /* check if flushed already */
832 while (f->flag_flushing &&
833 (!f->flag_iserror)) {
834 /* wait until all data has been written */
835 f->flag_sleeping = 1;
836 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
837 USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
838 if (err) {
839 DPRINTF("signal received\n");
840 break;
841 }
842 }
843 }
844 fflags |= FWRITE;
845
846 /* stop write transfer, if not already stopped */
847 (f->methods->f_stop_write) (f);
848 } else {
849 fflags |= FREAD;
850
851 /* stop write transfer, if not already stopped */
852 (f->methods->f_stop_read) (f);
853 }
854
855 /* check if we are sleeping */
856 if (f->flag_sleeping) {
857 DPRINTFN(2, "Sleeping at close!\n");
858 }
859 mtx_unlock(f->priv_mtx);
860
861 /* call close method */
862 (f->methods->f_close) (f, fflags);
863
864 DPRINTF("closed\n");
865 }
866
867 /*------------------------------------------------------------------------*
868 * usb_open - cdev callback
869 *------------------------------------------------------------------------*/
870 static int
usb_open(struct cdev * dev,int fflags,int devtype,struct thread * td)871 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
872 {
873 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
874 struct usb_cdev_refdata refs;
875 struct usb_cdev_privdata *cpd;
876 int err, ep;
877
878 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
879
880 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
881 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
882 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
883 DPRINTFN(2, "access mode not supported\n");
884 return (EPERM);
885 }
886
887 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
888 ep = cpd->ep_addr = pd->ep_addr;
889
890 usb_loc_fill(pd, cpd);
891 err = usb_ref_device(cpd, &refs, 1);
892 if (err) {
893 DPRINTFN(2, "cannot ref device\n");
894 free(cpd, M_USBDEV);
895 return (ENXIO);
896 }
897 cpd->fflags = fflags; /* access mode for open lifetime */
898
899 /* create FIFOs, if any */
900 err = usb_fifo_create(cpd, &refs);
901 /* check for error */
902 if (err) {
903 DPRINTFN(2, "cannot create fifo\n");
904 usb_unref_device(cpd, &refs);
905 free(cpd, M_USBDEV);
906 return (err);
907 }
908 if (fflags & FREAD) {
909 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
910 if (err) {
911 DPRINTFN(2, "read open failed\n");
912 usb_unref_device(cpd, &refs);
913 free(cpd, M_USBDEV);
914 return (err);
915 }
916 }
917 if (fflags & FWRITE) {
918 err = usb_fifo_open(cpd, refs.txfifo, fflags);
919 if (err) {
920 DPRINTFN(2, "write open failed\n");
921 if (fflags & FREAD) {
922 usb_fifo_close(refs.rxfifo, fflags);
923 }
924 usb_unref_device(cpd, &refs);
925 free(cpd, M_USBDEV);
926 return (err);
927 }
928 }
929 usb_unref_device(cpd, &refs);
930 devfs_set_cdevpriv(cpd, usb_close);
931
932 return (0);
933 }
934
935 /*------------------------------------------------------------------------*
936 * usb_close - cdev callback
937 *------------------------------------------------------------------------*/
938 static void
usb_close(void * arg)939 usb_close(void *arg)
940 {
941 struct usb_cdev_refdata refs;
942 struct usb_cdev_privdata *cpd = arg;
943 int err;
944
945 DPRINTFN(2, "cpd=%p\n", cpd);
946
947 err = usb_ref_device(cpd, &refs,
948 2 /* uref and allow detached state */);
949 if (err) {
950 DPRINTFN(2, "Cannot grab USB reference when "
951 "closing USB file handle\n");
952 goto done;
953 }
954 if (cpd->fflags & FREAD) {
955 usb_fifo_close(refs.rxfifo, cpd->fflags);
956 }
957 if (cpd->fflags & FWRITE) {
958 usb_fifo_close(refs.txfifo, cpd->fflags);
959 }
960 usb_unref_device(cpd, &refs);
961 done:
962 free(cpd, M_USBDEV);
963 }
964
965 static void
usb_dev_init(void * arg)966 usb_dev_init(void *arg)
967 {
968 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
969 sx_init(&usb_sym_lock, "USB sym mutex");
970 TAILQ_INIT(&usb_sym_head);
971
972 /* check the UGEN methods */
973 usb_fifo_check_methods(&usb_ugen_methods);
974 }
975
976 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
977
978 static void
usb_dev_init_post(void * arg)979 usb_dev_init_post(void *arg)
980 {
981 /*
982 * Create /dev/usb - this is needed for usbconfig(8), which
983 * needs a well-known device name to access.
984 */
985 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
986 0644, USB_DEVICE_NAME);
987 if (usb_dev == NULL) {
988 DPRINTFN(0, "Could not create usb bus device\n");
989 }
990 }
991
992 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
993
994 static void
usb_dev_uninit(void * arg)995 usb_dev_uninit(void *arg)
996 {
997 if (usb_dev != NULL) {
998 destroy_dev(usb_dev);
999 usb_dev = NULL;
1000 }
1001 mtx_destroy(&usb_ref_lock);
1002 sx_destroy(&usb_sym_lock);
1003 }
1004
1005 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
1006
1007 static int
usb_ioctl_f_sub(struct usb_fifo * f,u_long cmd,void * addr,struct thread * td)1008 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1009 struct thread *td)
1010 {
1011 int error = 0;
1012
1013 switch (cmd) {
1014 case FIODTYPE:
1015 *(int *)addr = 0; /* character device */
1016 break;
1017
1018 case FIONBIO:
1019 /* handled by upper FS layer */
1020 break;
1021
1022 case FIOASYNC:
1023 if (*(int *)addr) {
1024 if (f->async_p != NULL) {
1025 error = EBUSY;
1026 break;
1027 }
1028 f->async_p = USB_TD_GET_PROC(td);
1029 } else {
1030 f->async_p = NULL;
1031 }
1032 break;
1033
1034 /* XXX this is not the most general solution */
1035 case TIOCSPGRP:
1036 if (f->async_p == NULL) {
1037 error = EINVAL;
1038 break;
1039 }
1040 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1041 error = EPERM;
1042 break;
1043 }
1044 break;
1045 default:
1046 return (ENOIOCTL);
1047 }
1048 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1049 return (error);
1050 }
1051
1052 /*------------------------------------------------------------------------*
1053 * usb_ioctl - cdev callback
1054 *------------------------------------------------------------------------*/
1055 static int
usb_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int fflag,struct thread * td)1056 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1057 {
1058 struct usb_cdev_refdata refs;
1059 struct usb_cdev_privdata* cpd;
1060 struct usb_fifo *f;
1061 int fflags;
1062 int err;
1063
1064 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1065
1066 err = devfs_get_cdevpriv((void **)&cpd);
1067 if (err != 0)
1068 return (err);
1069
1070 /*
1071 * Performance optimisation: We try to check for IOCTL's that
1072 * don't need the USB reference first. Then we grab the USB
1073 * reference if we need it!
1074 */
1075 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1076 if (err)
1077 return (ENXIO);
1078
1079 fflags = cpd->fflags;
1080
1081 f = NULL; /* set default value */
1082 err = ENOIOCTL; /* set default value */
1083
1084 if (fflags & FWRITE) {
1085 f = refs.txfifo;
1086 err = usb_ioctl_f_sub(f, cmd, addr, td);
1087 }
1088 if (fflags & FREAD) {
1089 f = refs.rxfifo;
1090 err = usb_ioctl_f_sub(f, cmd, addr, td);
1091 }
1092 KASSERT(f != NULL, ("fifo not found"));
1093 if (err != ENOIOCTL)
1094 goto done;
1095
1096 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1097
1098 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1099
1100 if (err != ENOIOCTL)
1101 goto done;
1102
1103 if (usb_usb_ref_device(cpd, &refs)) {
1104 /* we lost the reference */
1105 return (ENXIO);
1106 }
1107
1108 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1109
1110 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1111
1112 if (err == ENOIOCTL)
1113 err = ENOTTY;
1114
1115 if (err)
1116 goto done;
1117
1118 /* Wait for re-enumeration, if any */
1119
1120 while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
1121
1122 usb_unref_device(cpd, &refs);
1123
1124 usb_pause_mtx(NULL, hz / 128);
1125
1126 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1127 if (usb_ref_device(cpd, &refs, 0)) {
1128 /* device no longer exists */
1129 return (ENXIO);
1130 }
1131 usb_unref_device(cpd, &refs);
1132 usb_pause_mtx(NULL, hz / 128);
1133 }
1134 }
1135
1136 done:
1137 usb_unref_device(cpd, &refs);
1138 return (err);
1139 }
1140
1141 static void
usb_filter_detach(struct knote * kn)1142 usb_filter_detach(struct knote *kn)
1143 {
1144 struct usb_fifo *f = kn->kn_hook;
1145 knlist_remove(&f->selinfo.si_note, kn, 0);
1146 }
1147
1148 static int
usb_filter_write(struct knote * kn,long hint)1149 usb_filter_write(struct knote *kn, long hint)
1150 {
1151 struct usb_cdev_privdata* cpd;
1152 struct usb_fifo *f;
1153 struct usb_mbuf *m;
1154
1155 DPRINTFN(2, "\n");
1156
1157 f = kn->kn_hook;
1158
1159 mtx_assert(f->priv_mtx, MA_OWNED);
1160
1161 cpd = f->curr_cpd;
1162 if (cpd == NULL) {
1163 m = (void *)1;
1164 } else if (f->fs_ep_max == 0) {
1165 if (f->flag_iserror) {
1166 /* we got an error */
1167 m = (void *)1;
1168 } else {
1169 if (f->queue_data == NULL) {
1170 /*
1171 * start write transfer, if not
1172 * already started
1173 */
1174 (f->methods->f_start_write) (f);
1175 }
1176 /* check if any packets are available */
1177 USB_IF_POLL(&f->free_q, m);
1178 }
1179 } else {
1180 if (f->flag_iscomplete) {
1181 m = (void *)1;
1182 } else {
1183 m = NULL;
1184 }
1185 }
1186 return (m ? 1 : 0);
1187 }
1188
1189 static int
usb_filter_read(struct knote * kn,long hint)1190 usb_filter_read(struct knote *kn, long hint)
1191 {
1192 struct usb_cdev_privdata* cpd;
1193 struct usb_fifo *f;
1194 struct usb_mbuf *m;
1195
1196 DPRINTFN(2, "\n");
1197
1198 f = kn->kn_hook;
1199
1200 mtx_assert(f->priv_mtx, MA_OWNED);
1201
1202 cpd = f->curr_cpd;
1203 if (cpd == NULL) {
1204 m = (void *)1;
1205 } else if (f->fs_ep_max == 0) {
1206 if (f->flag_iserror) {
1207 /* we have an error */
1208 m = (void *)1;
1209 } else {
1210 if (f->queue_data == NULL) {
1211 /*
1212 * start read transfer, if not
1213 * already started
1214 */
1215 (f->methods->f_start_read) (f);
1216 }
1217 /* check if any packets are available */
1218 USB_IF_POLL(&f->used_q, m);
1219
1220 /* start reading data, if any */
1221 if (m == NULL)
1222 (f->methods->f_start_read) (f);
1223 }
1224 } else {
1225 if (f->flag_iscomplete) {
1226 m = (void *)1;
1227 } else {
1228 m = NULL;
1229 }
1230 }
1231 return (m ? 1 : 0);
1232 }
1233
1234 static struct filterops usb_filtops_write = {
1235 .f_isfd = 1,
1236 .f_detach = usb_filter_detach,
1237 .f_event = usb_filter_write,
1238 };
1239
1240 static struct filterops usb_filtops_read = {
1241 .f_isfd = 1,
1242 .f_detach = usb_filter_detach,
1243 .f_event = usb_filter_read,
1244 };
1245
1246
1247 /* ARGSUSED */
1248 static int
usb_kqfilter(struct cdev * dev,struct knote * kn)1249 usb_kqfilter(struct cdev* dev, struct knote *kn)
1250 {
1251 struct usb_cdev_refdata refs;
1252 struct usb_cdev_privdata* cpd;
1253 struct usb_fifo *f;
1254 int fflags;
1255 int err = EINVAL;
1256
1257 DPRINTFN(2, "\n");
1258
1259 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1260 usb_ref_device(cpd, &refs, 0) != 0)
1261 return (ENXIO);
1262
1263 fflags = cpd->fflags;
1264
1265 /* Figure out who needs service */
1266 switch (kn->kn_filter) {
1267 case EVFILT_WRITE:
1268 if (fflags & FWRITE) {
1269 f = refs.txfifo;
1270 kn->kn_fop = &usb_filtops_write;
1271 err = 0;
1272 }
1273 break;
1274 case EVFILT_READ:
1275 if (fflags & FREAD) {
1276 f = refs.rxfifo;
1277 kn->kn_fop = &usb_filtops_read;
1278 err = 0;
1279 }
1280 break;
1281 default:
1282 err = EOPNOTSUPP;
1283 break;
1284 }
1285
1286 if (err == 0) {
1287 kn->kn_hook = f;
1288 mtx_lock(f->priv_mtx);
1289 knlist_add(&f->selinfo.si_note, kn, 1);
1290 mtx_unlock(f->priv_mtx);
1291 }
1292
1293 usb_unref_device(cpd, &refs);
1294 return (err);
1295 }
1296
1297 /* ARGSUSED */
1298 static int
usb_poll(struct cdev * dev,int events,struct thread * td)1299 usb_poll(struct cdev* dev, int events, struct thread* td)
1300 {
1301 struct usb_cdev_refdata refs;
1302 struct usb_cdev_privdata* cpd;
1303 struct usb_fifo *f;
1304 struct usb_mbuf *m;
1305 int fflags, revents;
1306
1307 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1308 usb_ref_device(cpd, &refs, 0) != 0)
1309 return (events &
1310 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1311
1312 fflags = cpd->fflags;
1313
1314 /* Figure out who needs service */
1315 revents = 0;
1316 if ((events & (POLLOUT | POLLWRNORM)) &&
1317 (fflags & FWRITE)) {
1318
1319 f = refs.txfifo;
1320
1321 mtx_lock(f->priv_mtx);
1322
1323 if (!refs.is_usbfs) {
1324 if (f->flag_iserror) {
1325 /* we got an error */
1326 m = (void *)1;
1327 } else {
1328 if (f->queue_data == NULL) {
1329 /*
1330 * start write transfer, if not
1331 * already started
1332 */
1333 (f->methods->f_start_write) (f);
1334 }
1335 /* check if any packets are available */
1336 USB_IF_POLL(&f->free_q, m);
1337 }
1338 } else {
1339 if (f->flag_iscomplete) {
1340 m = (void *)1;
1341 } else {
1342 m = NULL;
1343 }
1344 }
1345
1346 if (m) {
1347 revents |= events & (POLLOUT | POLLWRNORM);
1348 } else {
1349 f->flag_isselect = 1;
1350 selrecord(td, &f->selinfo);
1351 }
1352
1353 mtx_unlock(f->priv_mtx);
1354 }
1355 if ((events & (POLLIN | POLLRDNORM)) &&
1356 (fflags & FREAD)) {
1357
1358 f = refs.rxfifo;
1359
1360 mtx_lock(f->priv_mtx);
1361
1362 if (!refs.is_usbfs) {
1363 if (f->flag_iserror) {
1364 /* we have an error */
1365 m = (void *)1;
1366 } else {
1367 if (f->queue_data == NULL) {
1368 /*
1369 * start read transfer, if not
1370 * already started
1371 */
1372 (f->methods->f_start_read) (f);
1373 }
1374 /* check if any packets are available */
1375 USB_IF_POLL(&f->used_q, m);
1376 }
1377 } else {
1378 if (f->flag_iscomplete) {
1379 m = (void *)1;
1380 } else {
1381 m = NULL;
1382 }
1383 }
1384
1385 if (m) {
1386 revents |= events & (POLLIN | POLLRDNORM);
1387 } else {
1388 f->flag_isselect = 1;
1389 selrecord(td, &f->selinfo);
1390
1391 if (!refs.is_usbfs) {
1392 /* start reading data */
1393 (f->methods->f_start_read) (f);
1394 }
1395 }
1396
1397 mtx_unlock(f->priv_mtx);
1398 }
1399 usb_unref_device(cpd, &refs);
1400 return (revents);
1401 }
1402
1403 static int
usb_read(struct cdev * dev,struct uio * uio,int ioflag)1404 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1405 {
1406 struct usb_cdev_refdata refs;
1407 struct usb_cdev_privdata* cpd;
1408 struct usb_fifo *f;
1409 struct usb_mbuf *m;
1410 int fflags;
1411 int resid;
1412 int io_len;
1413 int err;
1414 uint8_t tr_data = 0;
1415
1416 err = devfs_get_cdevpriv((void **)&cpd);
1417 if (err != 0)
1418 return (err);
1419
1420 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1421 if (err)
1422 return (ENXIO);
1423
1424 fflags = cpd->fflags;
1425
1426 f = refs.rxfifo;
1427 if (f == NULL) {
1428 /* should not happen */
1429 usb_unref_device(cpd, &refs);
1430 return (EPERM);
1431 }
1432
1433 resid = uio->uio_resid;
1434
1435 mtx_lock(f->priv_mtx);
1436
1437 /* check for permanent read error */
1438 if (f->flag_iserror) {
1439 err = EIO;
1440 goto done;
1441 }
1442 /* check if USB-FS interface is active */
1443 if (refs.is_usbfs) {
1444 /*
1445 * The queue is used for events that should be
1446 * retrieved using the "USB_FS_COMPLETE" ioctl.
1447 */
1448 err = EINVAL;
1449 goto done;
1450 }
1451 while (uio->uio_resid > 0) {
1452
1453 USB_IF_DEQUEUE(&f->used_q, m);
1454
1455 if (m == NULL) {
1456
1457 /* start read transfer, if not already started */
1458
1459 (f->methods->f_start_read) (f);
1460
1461 if (ioflag & IO_NDELAY) {
1462 if (tr_data) {
1463 /* return length before error */
1464 break;
1465 }
1466 err = EWOULDBLOCK;
1467 break;
1468 }
1469 DPRINTF("sleeping\n");
1470
1471 err = usb_fifo_wait(f);
1472 if (err) {
1473 break;
1474 }
1475 continue;
1476 }
1477 if (f->methods->f_filter_read) {
1478 /*
1479 * Sometimes it is convenient to process data at the
1480 * expense of a userland process instead of a kernel
1481 * process.
1482 */
1483 (f->methods->f_filter_read) (f, m);
1484 }
1485 tr_data = 1;
1486
1487 io_len = MIN(m->cur_data_len, uio->uio_resid);
1488
1489 DPRINTFN(2, "transfer %d bytes from %p\n",
1490 io_len, m->cur_data_ptr);
1491
1492 err = usb_fifo_uiomove(f,
1493 m->cur_data_ptr, io_len, uio);
1494
1495 m->cur_data_len -= io_len;
1496 m->cur_data_ptr += io_len;
1497
1498 if (m->cur_data_len == 0) {
1499
1500 uint8_t last_packet;
1501
1502 last_packet = m->last_packet;
1503
1504 USB_IF_ENQUEUE(&f->free_q, m);
1505
1506 if (last_packet) {
1507 /* keep framing */
1508 break;
1509 }
1510 } else {
1511 USB_IF_PREPEND(&f->used_q, m);
1512 }
1513
1514 if (err) {
1515 break;
1516 }
1517 }
1518 done:
1519 mtx_unlock(f->priv_mtx);
1520
1521 usb_unref_device(cpd, &refs);
1522
1523 return (err);
1524 }
1525
1526 static int
usb_write(struct cdev * dev,struct uio * uio,int ioflag)1527 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1528 {
1529 struct usb_cdev_refdata refs;
1530 struct usb_cdev_privdata* cpd;
1531 struct usb_fifo *f;
1532 struct usb_mbuf *m;
1533 uint8_t *pdata;
1534 int fflags;
1535 int resid;
1536 int io_len;
1537 int err;
1538 uint8_t tr_data = 0;
1539
1540 DPRINTFN(2, "\n");
1541
1542 err = devfs_get_cdevpriv((void **)&cpd);
1543 if (err != 0)
1544 return (err);
1545
1546 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1547 if (err)
1548 return (ENXIO);
1549
1550 fflags = cpd->fflags;
1551
1552 f = refs.txfifo;
1553 if (f == NULL) {
1554 /* should not happen */
1555 usb_unref_device(cpd, &refs);
1556 return (EPERM);
1557 }
1558 resid = uio->uio_resid;
1559
1560 mtx_lock(f->priv_mtx);
1561
1562 /* check for permanent write error */
1563 if (f->flag_iserror) {
1564 err = EIO;
1565 goto done;
1566 }
1567 /* check if USB-FS interface is active */
1568 if (refs.is_usbfs) {
1569 /*
1570 * The queue is used for events that should be
1571 * retrieved using the "USB_FS_COMPLETE" ioctl.
1572 */
1573 err = EINVAL;
1574 goto done;
1575 }
1576 if (f->queue_data == NULL) {
1577 /* start write transfer, if not already started */
1578 (f->methods->f_start_write) (f);
1579 }
1580 /* we allow writing zero length data */
1581 do {
1582 USB_IF_DEQUEUE(&f->free_q, m);
1583
1584 if (m == NULL) {
1585
1586 if (ioflag & IO_NDELAY) {
1587 if (tr_data) {
1588 /* return length before error */
1589 break;
1590 }
1591 err = EWOULDBLOCK;
1592 break;
1593 }
1594 DPRINTF("sleeping\n");
1595
1596 err = usb_fifo_wait(f);
1597 if (err) {
1598 break;
1599 }
1600 continue;
1601 }
1602 tr_data = 1;
1603
1604 if (f->flag_have_fragment == 0) {
1605 USB_MBUF_RESET(m);
1606 io_len = m->cur_data_len;
1607 pdata = m->cur_data_ptr;
1608 if (io_len > uio->uio_resid)
1609 io_len = uio->uio_resid;
1610 m->cur_data_len = io_len;
1611 } else {
1612 io_len = m->max_data_len - m->cur_data_len;
1613 pdata = m->cur_data_ptr + m->cur_data_len;
1614 if (io_len > uio->uio_resid)
1615 io_len = uio->uio_resid;
1616 m->cur_data_len += io_len;
1617 }
1618
1619 DPRINTFN(2, "transfer %d bytes to %p\n",
1620 io_len, pdata);
1621
1622 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1623
1624 if (err) {
1625 f->flag_have_fragment = 0;
1626 USB_IF_ENQUEUE(&f->free_q, m);
1627 break;
1628 }
1629
1630 /* check if the buffer is ready to be transmitted */
1631
1632 if ((f->flag_write_defrag == 0) ||
1633 (m->cur_data_len == m->max_data_len)) {
1634 f->flag_have_fragment = 0;
1635
1636 /*
1637 * Check for write filter:
1638 *
1639 * Sometimes it is convenient to process data
1640 * at the expense of a userland process
1641 * instead of a kernel process.
1642 */
1643 if (f->methods->f_filter_write) {
1644 (f->methods->f_filter_write) (f, m);
1645 }
1646
1647 /* Put USB mbuf in the used queue */
1648 USB_IF_ENQUEUE(&f->used_q, m);
1649
1650 /* Start writing data, if not already started */
1651 (f->methods->f_start_write) (f);
1652 } else {
1653 /* Wait for more data or close */
1654 f->flag_have_fragment = 1;
1655 USB_IF_PREPEND(&f->free_q, m);
1656 }
1657
1658 } while (uio->uio_resid > 0);
1659 done:
1660 mtx_unlock(f->priv_mtx);
1661
1662 usb_unref_device(cpd, &refs);
1663
1664 return (err);
1665 }
1666
1667 int
usb_static_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)1668 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1669 struct thread *td)
1670 {
1671 union {
1672 struct usb_read_dir *urd;
1673 void* data;
1674 } u;
1675 int err;
1676
1677 u.data = data;
1678 switch (cmd) {
1679 case USB_READ_DIR:
1680 err = usb_read_symlink(u.urd->urd_data,
1681 u.urd->urd_startentry, u.urd->urd_maxlen);
1682 break;
1683 case USB_DEV_QUIRK_GET:
1684 case USB_QUIRK_NAME_GET:
1685 case USB_DEV_QUIRK_ADD:
1686 case USB_DEV_QUIRK_REMOVE:
1687 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1688 break;
1689 case USB_GET_TEMPLATE:
1690 *(int *)data = usb_template;
1691 err = 0;
1692 break;
1693 case USB_SET_TEMPLATE:
1694 err = priv_check(curthread, PRIV_DRIVER);
1695 if (err)
1696 break;
1697 usb_template = *(int *)data;
1698 break;
1699 default:
1700 err = ENOTTY;
1701 break;
1702 }
1703 return (err);
1704 }
1705
1706 static int
usb_fifo_uiomove(struct usb_fifo * f,void * cp,int n,struct uio * uio)1707 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1708 int n, struct uio *uio)
1709 {
1710 int error;
1711
1712 mtx_unlock(f->priv_mtx);
1713
1714 /*
1715 * "uiomove()" can sleep so one needs to make a wrapper,
1716 * exiting the mutex and checking things:
1717 */
1718 error = uiomove(cp, n, uio);
1719
1720 mtx_lock(f->priv_mtx);
1721
1722 return (error);
1723 }
1724
1725 int
usb_fifo_wait(struct usb_fifo * f)1726 usb_fifo_wait(struct usb_fifo *f)
1727 {
1728 int err;
1729
1730 mtx_assert(f->priv_mtx, MA_OWNED);
1731
1732 if (f->flag_iserror) {
1733 /* we are gone */
1734 return (EIO);
1735 }
1736 f->flag_sleeping = 1;
1737
1738 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1739
1740 if (f->flag_iserror) {
1741 /* we are gone */
1742 err = EIO;
1743 }
1744 return (err);
1745 }
1746
1747 void
usb_fifo_signal(struct usb_fifo * f)1748 usb_fifo_signal(struct usb_fifo *f)
1749 {
1750 if (f->flag_sleeping) {
1751 f->flag_sleeping = 0;
1752 cv_broadcast(&f->cv_io);
1753 }
1754 }
1755
1756 void
usb_fifo_wakeup(struct usb_fifo * f)1757 usb_fifo_wakeup(struct usb_fifo *f)
1758 {
1759 usb_fifo_signal(f);
1760
1761 KNOTE_LOCKED(&f->selinfo.si_note, 0);
1762
1763 if (f->flag_isselect) {
1764 selwakeup(&f->selinfo);
1765 f->flag_isselect = 0;
1766 }
1767 if (f->async_p != NULL) {
1768 PROC_LOCK(f->async_p);
1769 kern_psignal(f->async_p, SIGIO);
1770 PROC_UNLOCK(f->async_p);
1771 }
1772 }
1773
1774 static int
usb_fifo_dummy_open(struct usb_fifo * fifo,int fflags)1775 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1776 {
1777 return (0);
1778 }
1779
1780 static void
usb_fifo_dummy_close(struct usb_fifo * fifo,int fflags)1781 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1782 {
1783 return;
1784 }
1785
1786 static int
usb_fifo_dummy_ioctl(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)1787 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1788 {
1789 return (ENOIOCTL);
1790 }
1791
1792 static void
usb_fifo_dummy_cmd(struct usb_fifo * fifo)1793 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1794 {
1795 fifo->flag_flushing = 0; /* not flushing */
1796 }
1797
1798 static void
usb_fifo_check_methods(struct usb_fifo_methods * pm)1799 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1800 {
1801 /* check that all callback functions are OK */
1802
1803 if (pm->f_open == NULL)
1804 pm->f_open = &usb_fifo_dummy_open;
1805
1806 if (pm->f_close == NULL)
1807 pm->f_close = &usb_fifo_dummy_close;
1808
1809 if (pm->f_ioctl == NULL)
1810 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1811
1812 if (pm->f_ioctl_post == NULL)
1813 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1814
1815 if (pm->f_start_read == NULL)
1816 pm->f_start_read = &usb_fifo_dummy_cmd;
1817
1818 if (pm->f_stop_read == NULL)
1819 pm->f_stop_read = &usb_fifo_dummy_cmd;
1820
1821 if (pm->f_start_write == NULL)
1822 pm->f_start_write = &usb_fifo_dummy_cmd;
1823
1824 if (pm->f_stop_write == NULL)
1825 pm->f_stop_write = &usb_fifo_dummy_cmd;
1826 }
1827
1828 /*------------------------------------------------------------------------*
1829 * usb_fifo_attach
1830 *
1831 * The following function will create a duplex FIFO.
1832 *
1833 * Return values:
1834 * 0: Success.
1835 * Else: Failure.
1836 *------------------------------------------------------------------------*/
1837 int
usb_fifo_attach(struct usb_device * udev,void * priv_sc,struct mtx * priv_mtx,struct usb_fifo_methods * pm,struct usb_fifo_sc * f_sc,uint16_t unit,int16_t subunit,uint8_t iface_index,uid_t uid,gid_t gid,int mode)1838 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1839 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1840 struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
1841 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1842 {
1843 struct usb_fifo *f_tx;
1844 struct usb_fifo *f_rx;
1845 char devname[32];
1846 uint8_t n;
1847
1848 f_sc->fp[USB_FIFO_TX] = NULL;
1849 f_sc->fp[USB_FIFO_RX] = NULL;
1850
1851 if (pm == NULL)
1852 return (EINVAL);
1853
1854 /* check the methods */
1855 usb_fifo_check_methods(pm);
1856
1857 if (priv_mtx == NULL)
1858 priv_mtx = &Giant;
1859
1860 /* search for a free FIFO slot */
1861 for (n = 0;; n += 2) {
1862
1863 if (n == USB_FIFO_MAX) {
1864 /* end of FIFOs reached */
1865 return (ENOMEM);
1866 }
1867 /* Check for TX FIFO */
1868 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1869 continue;
1870 }
1871 /* Check for RX FIFO */
1872 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1873 continue;
1874 }
1875 break;
1876 }
1877
1878 f_tx = usb_fifo_alloc(priv_mtx);
1879 f_rx = usb_fifo_alloc(priv_mtx);
1880
1881 if ((f_tx == NULL) || (f_rx == NULL)) {
1882 usb_fifo_free(f_tx);
1883 usb_fifo_free(f_rx);
1884 return (ENOMEM);
1885 }
1886 /* initialise FIFO structures */
1887
1888 f_tx->fifo_index = n + USB_FIFO_TX;
1889 f_tx->dev_ep_index = -1;
1890 f_tx->priv_sc0 = priv_sc;
1891 f_tx->methods = pm;
1892 f_tx->iface_index = iface_index;
1893 f_tx->udev = udev;
1894
1895 f_rx->fifo_index = n + USB_FIFO_RX;
1896 f_rx->dev_ep_index = -1;
1897 f_rx->priv_sc0 = priv_sc;
1898 f_rx->methods = pm;
1899 f_rx->iface_index = iface_index;
1900 f_rx->udev = udev;
1901
1902 f_sc->fp[USB_FIFO_TX] = f_tx;
1903 f_sc->fp[USB_FIFO_RX] = f_rx;
1904
1905 mtx_lock(&usb_ref_lock);
1906 udev->fifo[f_tx->fifo_index] = f_tx;
1907 udev->fifo[f_rx->fifo_index] = f_rx;
1908 mtx_unlock(&usb_ref_lock);
1909
1910 for (n = 0; n != 4; n++) {
1911
1912 if (pm->basename[n] == NULL) {
1913 continue;
1914 }
1915 if (subunit < 0) {
1916 if (snprintf(devname, sizeof(devname),
1917 "%s%u%s", pm->basename[n],
1918 unit, pm->postfix[n] ?
1919 pm->postfix[n] : "")) {
1920 /* ignore */
1921 }
1922 } else {
1923 if (snprintf(devname, sizeof(devname),
1924 "%s%u.%d%s", pm->basename[n],
1925 unit, subunit, pm->postfix[n] ?
1926 pm->postfix[n] : "")) {
1927 /* ignore */
1928 }
1929 }
1930
1931 /*
1932 * Distribute the symbolic links into two FIFO structures:
1933 */
1934 if (n & 1) {
1935 f_rx->symlink[n / 2] =
1936 usb_alloc_symlink(devname);
1937 } else {
1938 f_tx->symlink[n / 2] =
1939 usb_alloc_symlink(devname);
1940 }
1941
1942 /* Create the device */
1943 f_sc->dev = usb_make_dev(udev, devname, -1,
1944 f_tx->fifo_index & f_rx->fifo_index,
1945 FREAD|FWRITE, uid, gid, mode);
1946 }
1947
1948 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1949 return (0);
1950 }
1951
1952 /*------------------------------------------------------------------------*
1953 * usb_fifo_alloc_buffer
1954 *
1955 * Return values:
1956 * 0: Success
1957 * Else failure
1958 *------------------------------------------------------------------------*/
1959 int
usb_fifo_alloc_buffer(struct usb_fifo * f,usb_size_t bufsize,uint16_t nbuf)1960 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1961 uint16_t nbuf)
1962 {
1963 usb_fifo_free_buffer(f);
1964
1965 /* allocate an endpoint */
1966 f->free_q.ifq_maxlen = nbuf;
1967 f->used_q.ifq_maxlen = nbuf;
1968
1969 f->queue_data = usb_alloc_mbufs(
1970 M_USBDEV, &f->free_q, bufsize, nbuf);
1971
1972 if ((f->queue_data == NULL) && bufsize && nbuf) {
1973 return (ENOMEM);
1974 }
1975 return (0); /* success */
1976 }
1977
1978 /*------------------------------------------------------------------------*
1979 * usb_fifo_free_buffer
1980 *
1981 * This function will free the buffers associated with a FIFO. This
1982 * function can be called multiple times in a row.
1983 *------------------------------------------------------------------------*/
1984 void
usb_fifo_free_buffer(struct usb_fifo * f)1985 usb_fifo_free_buffer(struct usb_fifo *f)
1986 {
1987 if (f->queue_data) {
1988 /* free old buffer */
1989 free(f->queue_data, M_USBDEV);
1990 f->queue_data = NULL;
1991 }
1992 /* reset queues */
1993
1994 memset(&f->free_q, 0, sizeof(f->free_q));
1995 memset(&f->used_q, 0, sizeof(f->used_q));
1996 }
1997
1998 void
usb_fifo_detach(struct usb_fifo_sc * f_sc)1999 usb_fifo_detach(struct usb_fifo_sc *f_sc)
2000 {
2001 if (f_sc == NULL) {
2002 return;
2003 }
2004 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
2005 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
2006
2007 f_sc->fp[USB_FIFO_TX] = NULL;
2008 f_sc->fp[USB_FIFO_RX] = NULL;
2009
2010 usb_destroy_dev(f_sc->dev);
2011
2012 f_sc->dev = NULL;
2013
2014 DPRINTFN(2, "detached %p\n", f_sc);
2015 }
2016
2017 usb_size_t
usb_fifo_put_bytes_max(struct usb_fifo * f)2018 usb_fifo_put_bytes_max(struct usb_fifo *f)
2019 {
2020 struct usb_mbuf *m;
2021 usb_size_t len;
2022
2023 USB_IF_POLL(&f->free_q, m);
2024
2025 if (m) {
2026 len = m->max_data_len;
2027 } else {
2028 len = 0;
2029 }
2030 return (len);
2031 }
2032
2033 /*------------------------------------------------------------------------*
2034 * usb_fifo_put_data
2035 *
2036 * what:
2037 * 0 - normal operation
2038 * 1 - set last packet flag to enforce framing
2039 *------------------------------------------------------------------------*/
2040 void
usb_fifo_put_data(struct usb_fifo * f,struct usb_page_cache * pc,usb_frlength_t offset,usb_frlength_t len,uint8_t what)2041 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
2042 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
2043 {
2044 struct usb_mbuf *m;
2045 usb_frlength_t io_len;
2046
2047 while (len || (what == 1)) {
2048
2049 USB_IF_DEQUEUE(&f->free_q, m);
2050
2051 if (m) {
2052 USB_MBUF_RESET(m);
2053
2054 io_len = MIN(len, m->cur_data_len);
2055
2056 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
2057
2058 m->cur_data_len = io_len;
2059 offset += io_len;
2060 len -= io_len;
2061
2062 if ((len == 0) && (what == 1)) {
2063 m->last_packet = 1;
2064 }
2065 USB_IF_ENQUEUE(&f->used_q, m);
2066
2067 usb_fifo_wakeup(f);
2068
2069 if ((len == 0) || (what == 1)) {
2070 break;
2071 }
2072 } else {
2073 break;
2074 }
2075 }
2076 }
2077
2078 void
usb_fifo_put_data_linear(struct usb_fifo * f,void * ptr,usb_size_t len,uint8_t what)2079 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2080 usb_size_t len, uint8_t what)
2081 {
2082 struct usb_mbuf *m;
2083 usb_size_t io_len;
2084
2085 while (len || (what == 1)) {
2086
2087 USB_IF_DEQUEUE(&f->free_q, m);
2088
2089 if (m) {
2090 USB_MBUF_RESET(m);
2091
2092 io_len = MIN(len, m->cur_data_len);
2093
2094 memcpy(m->cur_data_ptr, ptr, io_len);
2095
2096 m->cur_data_len = io_len;
2097 ptr = USB_ADD_BYTES(ptr, io_len);
2098 len -= io_len;
2099
2100 if ((len == 0) && (what == 1)) {
2101 m->last_packet = 1;
2102 }
2103 USB_IF_ENQUEUE(&f->used_q, m);
2104
2105 usb_fifo_wakeup(f);
2106
2107 if ((len == 0) || (what == 1)) {
2108 break;
2109 }
2110 } else {
2111 break;
2112 }
2113 }
2114 }
2115
2116 uint8_t
usb_fifo_put_data_buffer(struct usb_fifo * f,void * ptr,usb_size_t len)2117 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2118 {
2119 struct usb_mbuf *m;
2120
2121 USB_IF_DEQUEUE(&f->free_q, m);
2122
2123 if (m) {
2124 m->cur_data_len = len;
2125 m->cur_data_ptr = ptr;
2126 USB_IF_ENQUEUE(&f->used_q, m);
2127 usb_fifo_wakeup(f);
2128 return (1);
2129 }
2130 return (0);
2131 }
2132
2133 void
usb_fifo_put_data_error(struct usb_fifo * f)2134 usb_fifo_put_data_error(struct usb_fifo *f)
2135 {
2136 f->flag_iserror = 1;
2137 usb_fifo_wakeup(f);
2138 }
2139
2140 /*------------------------------------------------------------------------*
2141 * usb_fifo_get_data
2142 *
2143 * what:
2144 * 0 - normal operation
2145 * 1 - only get one "usb_mbuf"
2146 *
2147 * returns:
2148 * 0 - no more data
2149 * 1 - data in buffer
2150 *------------------------------------------------------------------------*/
2151 uint8_t
usb_fifo_get_data(struct usb_fifo * f,struct usb_page_cache * pc,usb_frlength_t offset,usb_frlength_t len,usb_frlength_t * actlen,uint8_t what)2152 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2153 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2154 uint8_t what)
2155 {
2156 struct usb_mbuf *m;
2157 usb_frlength_t io_len;
2158 uint8_t tr_data = 0;
2159
2160 actlen[0] = 0;
2161
2162 while (1) {
2163
2164 USB_IF_DEQUEUE(&f->used_q, m);
2165
2166 if (m) {
2167
2168 tr_data = 1;
2169
2170 io_len = MIN(len, m->cur_data_len);
2171
2172 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2173
2174 len -= io_len;
2175 offset += io_len;
2176 actlen[0] += io_len;
2177 m->cur_data_ptr += io_len;
2178 m->cur_data_len -= io_len;
2179
2180 if ((m->cur_data_len == 0) || (what == 1)) {
2181 USB_IF_ENQUEUE(&f->free_q, m);
2182
2183 usb_fifo_wakeup(f);
2184
2185 if (what == 1) {
2186 break;
2187 }
2188 } else {
2189 USB_IF_PREPEND(&f->used_q, m);
2190 }
2191 } else {
2192
2193 if (tr_data) {
2194 /* wait for data to be written out */
2195 break;
2196 }
2197 if (f->flag_flushing) {
2198 /* check if we should send a short packet */
2199 if (f->flag_short != 0) {
2200 f->flag_short = 0;
2201 tr_data = 1;
2202 break;
2203 }
2204 /* flushing complete */
2205 f->flag_flushing = 0;
2206 usb_fifo_wakeup(f);
2207 }
2208 break;
2209 }
2210 if (len == 0) {
2211 break;
2212 }
2213 }
2214 return (tr_data);
2215 }
2216
2217 uint8_t
usb_fifo_get_data_linear(struct usb_fifo * f,void * ptr,usb_size_t len,usb_size_t * actlen,uint8_t what)2218 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2219 usb_size_t len, usb_size_t *actlen, uint8_t what)
2220 {
2221 struct usb_mbuf *m;
2222 usb_size_t io_len;
2223 uint8_t tr_data = 0;
2224
2225 actlen[0] = 0;
2226
2227 while (1) {
2228
2229 USB_IF_DEQUEUE(&f->used_q, m);
2230
2231 if (m) {
2232
2233 tr_data = 1;
2234
2235 io_len = MIN(len, m->cur_data_len);
2236
2237 memcpy(ptr, m->cur_data_ptr, io_len);
2238
2239 len -= io_len;
2240 ptr = USB_ADD_BYTES(ptr, io_len);
2241 actlen[0] += io_len;
2242 m->cur_data_ptr += io_len;
2243 m->cur_data_len -= io_len;
2244
2245 if ((m->cur_data_len == 0) || (what == 1)) {
2246 USB_IF_ENQUEUE(&f->free_q, m);
2247
2248 usb_fifo_wakeup(f);
2249
2250 if (what == 1) {
2251 break;
2252 }
2253 } else {
2254 USB_IF_PREPEND(&f->used_q, m);
2255 }
2256 } else {
2257
2258 if (tr_data) {
2259 /* wait for data to be written out */
2260 break;
2261 }
2262 if (f->flag_flushing) {
2263 /* check if we should send a short packet */
2264 if (f->flag_short != 0) {
2265 f->flag_short = 0;
2266 tr_data = 1;
2267 break;
2268 }
2269 /* flushing complete */
2270 f->flag_flushing = 0;
2271 usb_fifo_wakeup(f);
2272 }
2273 break;
2274 }
2275 if (len == 0) {
2276 break;
2277 }
2278 }
2279 return (tr_data);
2280 }
2281
2282 uint8_t
usb_fifo_get_data_buffer(struct usb_fifo * f,void ** pptr,usb_size_t * plen)2283 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2284 {
2285 struct usb_mbuf *m;
2286
2287 USB_IF_POLL(&f->used_q, m);
2288
2289 if (m) {
2290 *plen = m->cur_data_len;
2291 *pptr = m->cur_data_ptr;
2292
2293 return (1);
2294 }
2295 return (0);
2296 }
2297
2298 void
usb_fifo_get_data_error(struct usb_fifo * f)2299 usb_fifo_get_data_error(struct usb_fifo *f)
2300 {
2301 f->flag_iserror = 1;
2302 usb_fifo_wakeup(f);
2303 }
2304
2305 /*------------------------------------------------------------------------*
2306 * usb_alloc_symlink
2307 *
2308 * Return values:
2309 * NULL: Failure
2310 * Else: Pointer to symlink entry
2311 *------------------------------------------------------------------------*/
2312 struct usb_symlink *
usb_alloc_symlink(const char * target)2313 usb_alloc_symlink(const char *target)
2314 {
2315 struct usb_symlink *ps;
2316
2317 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2318 if (ps == NULL) {
2319 return (ps);
2320 }
2321 /* XXX no longer needed */
2322 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2323 ps->src_len = strlen(ps->src_path);
2324 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2325 ps->dst_len = strlen(ps->dst_path);
2326
2327 sx_xlock(&usb_sym_lock);
2328 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2329 sx_unlock(&usb_sym_lock);
2330 return (ps);
2331 }
2332
2333 /*------------------------------------------------------------------------*
2334 * usb_free_symlink
2335 *------------------------------------------------------------------------*/
2336 void
usb_free_symlink(struct usb_symlink * ps)2337 usb_free_symlink(struct usb_symlink *ps)
2338 {
2339 if (ps == NULL) {
2340 return;
2341 }
2342 sx_xlock(&usb_sym_lock);
2343 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2344 sx_unlock(&usb_sym_lock);
2345
2346 free(ps, M_USBDEV);
2347 }
2348
2349 /*------------------------------------------------------------------------*
2350 * usb_read_symlink
2351 *
2352 * Return value:
2353 * 0: Success
2354 * Else: Failure
2355 *------------------------------------------------------------------------*/
2356 int
usb_read_symlink(uint8_t * user_ptr,uint32_t startentry,uint32_t user_len)2357 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2358 {
2359 struct usb_symlink *ps;
2360 uint32_t temp;
2361 uint32_t delta = 0;
2362 uint8_t len;
2363 int error = 0;
2364
2365 sx_xlock(&usb_sym_lock);
2366
2367 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2368
2369 /*
2370 * Compute total length of source and destination symlink
2371 * strings pluss one length byte and two NUL bytes:
2372 */
2373 temp = ps->src_len + ps->dst_len + 3;
2374
2375 if (temp > 255) {
2376 /*
2377 * Skip entry because this length cannot fit
2378 * into one byte:
2379 */
2380 continue;
2381 }
2382 if (startentry != 0) {
2383 /* decrement read offset */
2384 startentry--;
2385 continue;
2386 }
2387 if (temp > user_len) {
2388 /* out of buffer space */
2389 break;
2390 }
2391 len = temp;
2392
2393 /* copy out total length */
2394
2395 error = copyout(&len,
2396 USB_ADD_BYTES(user_ptr, delta), 1);
2397 if (error) {
2398 break;
2399 }
2400 delta += 1;
2401
2402 /* copy out source string */
2403
2404 error = copyout(ps->src_path,
2405 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2406 if (error) {
2407 break;
2408 }
2409 len = 0;
2410 delta += ps->src_len;
2411 error = copyout(&len,
2412 USB_ADD_BYTES(user_ptr, delta), 1);
2413 if (error) {
2414 break;
2415 }
2416 delta += 1;
2417
2418 /* copy out destination string */
2419
2420 error = copyout(ps->dst_path,
2421 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2422 if (error) {
2423 break;
2424 }
2425 len = 0;
2426 delta += ps->dst_len;
2427 error = copyout(&len,
2428 USB_ADD_BYTES(user_ptr, delta), 1);
2429 if (error) {
2430 break;
2431 }
2432 delta += 1;
2433
2434 user_len -= temp;
2435 }
2436
2437 /* a zero length entry indicates the end */
2438
2439 if ((user_len != 0) && (error == 0)) {
2440
2441 len = 0;
2442
2443 error = copyout(&len,
2444 USB_ADD_BYTES(user_ptr, delta), 1);
2445 }
2446 sx_unlock(&usb_sym_lock);
2447 return (error);
2448 }
2449
2450 void
usb_fifo_set_close_zlp(struct usb_fifo * f,uint8_t onoff)2451 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2452 {
2453 if (f == NULL)
2454 return;
2455
2456 /* send a Zero Length Packet, ZLP, before close */
2457 f->flag_short = onoff;
2458 }
2459
2460 void
usb_fifo_set_write_defrag(struct usb_fifo * f,uint8_t onoff)2461 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2462 {
2463 if (f == NULL)
2464 return;
2465
2466 /* defrag written data */
2467 f->flag_write_defrag = onoff;
2468 /* reset defrag state */
2469 f->flag_have_fragment = 0;
2470 }
2471
2472 void *
usb_fifo_softc(struct usb_fifo * f)2473 usb_fifo_softc(struct usb_fifo *f)
2474 {
2475 return (f->priv_sc0);
2476 }
2477 #endif /* USB_HAVE_UGEN */
2478