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