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