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