3 * Copyright (c) 2006-2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 * usb_dev.c - An abstraction layer for creating devices under /dev/...
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/linker_set.h>
39 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
49 #include <sys/vnode.h>
51 #include <sys/fcntl.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usb_ioctl.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
58 #define USB_DEBUG_VAR usb_fifo_debug
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_dev.h>
62 #include <dev/usb/usb_mbuf.h>
63 #include <dev/usb/usb_process.h>
64 #include <dev/usb/usb_device.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_generic.h>
68 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_util.h>
71 #include <dev/usb/usb_controller.h>
72 #include <dev/usb/usb_bus.h>
74 #include <sys/filio.h>
75 #include <sys/ttycom.h>
76 #include <sys/syscallsubr.h>
78 #include <machine/stdarg.h>
83 static int usb_fifo_debug = 0;
85 SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
86 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
87 &usb_fifo_debug, 0, "Debug Level");
89 TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
92 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
93 ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
94 #define USB_UCRED struct ucred *ucred,
101 static int usb_fifo_open(struct usb_cdev_privdata *,
102 struct usb_fifo *, int);
103 static void usb_fifo_close(struct usb_fifo *, int);
104 static void usb_dev_init(void *);
105 static void usb_dev_init_post(void *);
106 static void usb_dev_uninit(void *);
107 static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
109 static void usb_fifo_check_methods(struct usb_fifo_methods *);
110 static struct usb_fifo *usb_fifo_alloc(void);
111 static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
113 static void usb_loc_fill(struct usb_fs_privdata *,
114 struct usb_cdev_privdata *);
115 static void usb_close(void *);
116 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
117 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
118 static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
120 static d_open_t usb_open;
121 static d_ioctl_t usb_ioctl;
122 static d_read_t usb_read;
123 static d_write_t usb_write;
124 static d_poll_t usb_poll;
126 static d_ioctl_t usb_static_ioctl;
128 static usb_fifo_open_t usb_fifo_dummy_open;
129 static usb_fifo_close_t usb_fifo_dummy_close;
130 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
131 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
133 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
134 struct cdevsw usb_devsw = {
135 .d_version = D_VERSION,
137 .d_ioctl = usb_ioctl,
139 .d_flags = D_TRACKCLOSE,
141 .d_write = usb_write,
145 static struct cdev* usb_dev = NULL;
147 /* character device structure used for /dev/usb */
148 static struct cdevsw usb_static_devsw = {
149 .d_version = D_VERSION,
150 .d_ioctl = usb_static_ioctl,
154 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
155 static struct sx usb_sym_lock;
157 struct mtx usb_ref_lock;
159 /*------------------------------------------------------------------------*
162 * This is used to fill out a usb_cdev_privdata structure based on the
163 * device's address as contained in usb_fs_privdata.
164 *------------------------------------------------------------------------*/
166 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
168 cpd->bus_index = pd->bus_index;
169 cpd->dev_index = pd->dev_index;
170 cpd->ep_addr = pd->ep_addr;
171 cpd->fifo_index = pd->fifo_index;
174 /*------------------------------------------------------------------------*
177 * This function is used to atomically refer an USB device by its
178 * device location. If this function returns success the USB device
179 * will not dissappear until the USB device is unreferenced.
182 * 0: Success, refcount incremented on the given USB device.
184 *------------------------------------------------------------------------*/
186 usb_ref_device(struct usb_cdev_privdata *cpd,
187 struct usb_cdev_refdata *crd, int need_uref)
189 struct usb_fifo **ppf;
192 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
195 memset(crd, 0, sizeof(*crd));
197 mtx_lock(&usb_ref_lock);
198 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
199 if (cpd->bus == NULL) {
200 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
203 cpd->udev = cpd->bus->devices[cpd->dev_index];
204 if (cpd->udev == NULL) {
205 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
208 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
209 DPRINTFN(2, "no dev ref\n");
213 DPRINTFN(2, "ref udev - needed\n");
214 cpd->udev->refcount++;
216 mtx_unlock(&usb_ref_lock);
219 * We need to grab the sx-lock before grabbing the
220 * FIFO refs to avoid deadlock at detach!
222 usbd_enum_lock(cpd->udev);
224 mtx_lock(&usb_ref_lock);
227 * Set "is_uref" after grabbing the default SX lock
232 /* check if we are doing an open */
233 if (cpd->fflags == 0) {
234 /* use zero defaults */
236 /* check for write */
237 if (cpd->fflags & FWRITE) {
238 ppf = cpd->udev->fifo;
239 f = ppf[cpd->fifo_index + USB_FIFO_TX];
241 crd->is_write = 1; /* ref */
242 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
244 if (f->curr_cpd != cpd)
246 /* check if USB-FS is active */
247 if (f->fs_ep_max != 0) {
253 if (cpd->fflags & FREAD) {
254 ppf = cpd->udev->fifo;
255 f = ppf[cpd->fifo_index + USB_FIFO_RX];
257 crd->is_read = 1; /* ref */
258 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
260 if (f->curr_cpd != cpd)
262 /* check if USB-FS is active */
263 if (f->fs_ep_max != 0) {
269 /* when everything is OK we increment the refcounts */
271 DPRINTFN(2, "ref write\n");
272 crd->txfifo->refcount++;
275 DPRINTFN(2, "ref read\n");
276 crd->rxfifo->refcount++;
278 mtx_unlock(&usb_ref_lock);
284 usbd_enum_unlock(cpd->udev);
286 if (--(cpd->udev->refcount) == 0) {
287 cv_signal(&cpd->udev->ref_cv);
290 mtx_unlock(&usb_ref_lock);
291 DPRINTFN(2, "fail\n");
292 return (USB_ERR_INVAL);
295 /*------------------------------------------------------------------------*
298 * This function is used to upgrade an USB reference to include the
299 * USB device reference on a USB location.
302 * 0: Success, refcount incremented on the given USB device.
304 *------------------------------------------------------------------------*/
306 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
307 struct usb_cdev_refdata *crd)
310 * Check if we already got an USB reference on this location:
313 return (0); /* success */
316 * To avoid deadlock at detach we need to drop the FIFO ref
317 * and re-acquire a new ref!
319 usb_unref_device(cpd, crd);
321 return (usb_ref_device(cpd, crd, 1 /* need uref */));
324 /*------------------------------------------------------------------------*
327 * This function will release the reference count by one unit for the
329 *------------------------------------------------------------------------*/
331 usb_unref_device(struct usb_cdev_privdata *cpd,
332 struct usb_cdev_refdata *crd)
335 DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
338 usbd_enum_unlock(cpd->udev);
340 mtx_lock(&usb_ref_lock);
342 if (--(crd->rxfifo->refcount) == 0) {
343 cv_signal(&crd->rxfifo->cv_drain);
348 if (--(crd->txfifo->refcount) == 0) {
349 cv_signal(&crd->txfifo->cv_drain);
354 if (--(cpd->udev->refcount) == 0) {
355 cv_signal(&cpd->udev->ref_cv);
359 mtx_unlock(&usb_ref_lock);
362 static struct usb_fifo *
367 f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
369 cv_init(&f->cv_io, "FIFO-IO");
370 cv_init(&f->cv_drain, "FIFO-DRAIN");
376 /*------------------------------------------------------------------------*
378 *------------------------------------------------------------------------*/
380 usb_fifo_create(struct usb_cdev_privdata *cpd,
381 struct usb_cdev_refdata *crd)
383 struct usb_device *udev = cpd->udev;
385 struct usb_endpoint *ep;
391 int e = cpd->ep_addr;
393 is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
394 is_rx = (cpd->fflags & FREAD) ? 1 : 0;
398 /* Preallocated FIFO */
400 DPRINTFN(5, "Preallocated FIFO\n");
402 f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
408 f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
416 KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
418 /* search for a free FIFO slot */
419 DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
420 for (n = 0;; n += 2) {
422 if (n == USB_FIFO_MAX) {
427 /* end of FIFOs reached */
428 DPRINTFN(5, "out of FIFOs\n");
432 /* Check for TX FIFO */
434 f = udev->fifo[n + USB_FIFO_TX];
436 if (f->dev_ep_index != e) {
437 /* wrong endpoint index */
440 if (f->curr_cpd != NULL) {
445 } else if (no_null) {
449 /* Check for RX FIFO */
451 f = udev->fifo[n + USB_FIFO_RX];
453 if (f->dev_ep_index != e) {
454 /* wrong endpoint index */
457 if (f->curr_cpd != NULL) {
462 } else if (no_null) {
470 if (e >= (USB_EP_MAX / 2)) {
471 /* we don't create any endpoints in this range */
472 DPRINTFN(5, "ep out of range\n");
473 return (is_busy ? EBUSY : EINVAL);
477 if ((e != 0) && is_busy) {
479 * Only the default control endpoint is allowed to be
480 * opened multiple times!
482 DPRINTFN(5, "busy\n");
488 (udev->fifo[n + USB_FIFO_TX] == NULL)) {
489 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
490 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
492 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
495 f = usb_fifo_alloc();
497 DPRINTFN(5, "could not alloc tx fifo\n");
500 /* update some fields */
501 f->fifo_index = n + USB_FIFO_TX;
503 f->priv_mtx = &udev->device_mtx;
505 f->methods = &usb_ugen_methods;
506 f->iface_index = ep->iface_index;
508 mtx_lock(&usb_ref_lock);
509 udev->fifo[n + USB_FIFO_TX] = f;
510 mtx_unlock(&usb_ref_lock);
514 (udev->fifo[n + USB_FIFO_RX] == NULL)) {
516 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
517 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
519 DPRINTFN(5, "dev_get_endpoint returned NULL\n");
522 f = usb_fifo_alloc();
524 DPRINTFN(5, "could not alloc rx fifo\n");
527 /* update some fields */
528 f->fifo_index = n + USB_FIFO_RX;
530 f->priv_mtx = &udev->device_mtx;
532 f->methods = &usb_ugen_methods;
533 f->iface_index = ep->iface_index;
535 mtx_lock(&usb_ref_lock);
536 udev->fifo[n + USB_FIFO_RX] = f;
537 mtx_unlock(&usb_ref_lock);
540 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
543 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
545 /* fill out fifo index */
546 DPRINTFN(5, "fifo index = %d\n", n);
555 usb_fifo_free(struct usb_fifo *f)
563 /* destroy symlink devices, if any */
564 for (n = 0; n != 2; n++) {
566 usb_free_symlink(f->symlink[n]);
567 f->symlink[n] = NULL;
570 mtx_lock(&usb_ref_lock);
572 /* delink ourselves to stop calls from userland */
573 if ((f->fifo_index < USB_FIFO_MAX) &&
575 (f->udev->fifo[f->fifo_index] == f)) {
576 f->udev->fifo[f->fifo_index] = NULL;
578 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
581 /* decrease refcount */
583 /* prevent any write flush */
585 /* need to wait until all callers have exited */
586 while (f->refcount != 0) {
587 mtx_unlock(&usb_ref_lock); /* avoid LOR */
588 mtx_lock(f->priv_mtx);
589 /* get I/O thread out of any sleep state */
590 if (f->flag_sleeping) {
591 f->flag_sleeping = 0;
592 cv_broadcast(&f->cv_io);
594 mtx_unlock(f->priv_mtx);
595 mtx_lock(&usb_ref_lock);
598 cv_wait(&f->cv_drain, &usb_ref_lock);
600 mtx_unlock(&usb_ref_lock);
602 /* take care of closing the device here, if any */
603 usb_fifo_close(f, 0);
605 cv_destroy(&f->cv_io);
606 cv_destroy(&f->cv_drain);
611 static struct usb_endpoint *
612 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
614 struct usb_endpoint *ep;
620 if (dir == USB_FIFO_RX) {
621 if (udev->flags.usb_mode == USB_MODE_HOST) {
627 if (udev->flags.usb_mode == USB_MODE_HOST) {
633 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
637 /* if the endpoint does not exist then return */
640 if (ep->edesc == NULL) {
641 /* invalid endpoint */
644 return (ep); /* success */
647 /*------------------------------------------------------------------------*
653 *------------------------------------------------------------------------*/
655 usb_fifo_open(struct usb_cdev_privdata *cpd,
656 struct usb_fifo *f, int fflags)
662 DPRINTFN(2, "no FIFO\n");
665 /* remove FWRITE and FREAD flags */
666 fflags &= ~(FWRITE | FREAD);
668 /* set correct file flags */
669 if ((f->fifo_index & 1) == USB_FIFO_TX) {
675 /* check if we are already opened */
676 /* we don't need any locks when checking this variable */
677 if (f->curr_cpd != NULL) {
682 /* reset short flag before open */
685 /* call open method */
686 err = (f->methods->f_open) (f, fflags);
690 mtx_lock(f->priv_mtx);
692 /* reset sleep flag */
693 f->flag_sleeping = 0;
695 /* reset error flag */
698 /* reset complete flag */
699 f->flag_iscomplete = 0;
701 /* reset select flag */
702 f->flag_isselect = 0;
704 /* reset flushing flag */
705 f->flag_flushing = 0;
707 /* reset ASYNC proc flag */
710 mtx_lock(&usb_ref_lock);
711 /* flag the fifo as opened to prevent others */
713 mtx_unlock(&usb_ref_lock);
718 mtx_unlock(f->priv_mtx);
723 /*------------------------------------------------------------------------*
725 *------------------------------------------------------------------------*/
727 usb_fifo_reset(struct usb_fifo *f)
735 USB_IF_DEQUEUE(&f->used_q, m);
737 USB_IF_ENQUEUE(&f->free_q, m);
742 /* reset have fragment flag */
743 f->flag_have_fragment = 0;
746 /*------------------------------------------------------------------------*
748 *------------------------------------------------------------------------*/
750 usb_fifo_close(struct usb_fifo *f, int fflags)
754 /* check if we are not opened */
755 if (f->curr_cpd == NULL) {
756 /* nothing to do - already closed */
759 mtx_lock(f->priv_mtx);
761 /* clear current cdev private data pointer */
764 /* check if we are selected */
765 if (f->flag_isselect) {
766 selwakeup(&f->selinfo);
767 f->flag_isselect = 0;
769 /* check if a thread wants SIGIO */
770 if (f->async_p != NULL) {
771 PROC_LOCK(f->async_p);
772 psignal(f->async_p, SIGIO);
773 PROC_UNLOCK(f->async_p);
776 /* remove FWRITE and FREAD flags */
777 fflags &= ~(FWRITE | FREAD);
779 /* flush written data, if any */
780 if ((f->fifo_index & 1) == USB_FIFO_TX) {
782 if (!f->flag_iserror) {
784 /* set flushing flag */
785 f->flag_flushing = 1;
787 /* get the last packet in */
788 if (f->flag_have_fragment) {
790 f->flag_have_fragment = 0;
791 USB_IF_DEQUEUE(&f->free_q, m);
793 USB_IF_ENQUEUE(&f->used_q, m);
797 /* start write transfer, if not already started */
798 (f->methods->f_start_write) (f);
800 /* check if flushed already */
801 while (f->flag_flushing &&
802 (!f->flag_iserror)) {
803 /* wait until all data has been written */
804 f->flag_sleeping = 1;
805 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
807 DPRINTF("signal received\n");
814 /* stop write transfer, if not already stopped */
815 (f->methods->f_stop_write) (f);
819 /* stop write transfer, if not already stopped */
820 (f->methods->f_stop_read) (f);
823 /* check if we are sleeping */
824 if (f->flag_sleeping) {
825 DPRINTFN(2, "Sleeping at close!\n");
827 mtx_unlock(f->priv_mtx);
829 /* call close method */
830 (f->methods->f_close) (f, fflags);
835 /*------------------------------------------------------------------------*
836 * usb_open - cdev callback
837 *------------------------------------------------------------------------*/
839 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
841 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
842 struct usb_cdev_refdata refs;
843 struct usb_cdev_privdata *cpd;
846 DPRINTFN(2, "%s fflags=0x%08x\n", dev->si_name, fflags);
848 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
849 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
850 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
851 DPRINTFN(2, "access mode not supported\n");
855 cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
856 ep = cpd->ep_addr = pd->ep_addr;
858 usb_loc_fill(pd, cpd);
859 err = usb_ref_device(cpd, &refs, 1);
861 DPRINTFN(2, "cannot ref device\n");
865 cpd->fflags = fflags; /* access mode for open lifetime */
867 /* create FIFOs, if any */
868 err = usb_fifo_create(cpd, &refs);
869 /* check for error */
871 DPRINTFN(2, "cannot create fifo\n");
872 usb_unref_device(cpd, &refs);
876 if (fflags & FREAD) {
877 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
879 DPRINTFN(2, "read open failed\n");
880 usb_unref_device(cpd, &refs);
885 if (fflags & FWRITE) {
886 err = usb_fifo_open(cpd, refs.txfifo, fflags);
888 DPRINTFN(2, "write open failed\n");
889 if (fflags & FREAD) {
890 usb_fifo_close(refs.rxfifo, fflags);
892 usb_unref_device(cpd, &refs);
897 usb_unref_device(cpd, &refs);
898 devfs_set_cdevpriv(cpd, usb_close);
903 /*------------------------------------------------------------------------*
904 * usb_close - cdev callback
905 *------------------------------------------------------------------------*/
909 struct usb_cdev_refdata refs;
910 struct usb_cdev_privdata *cpd = arg;
913 DPRINTFN(2, "cpd=%p\n", cpd);
915 err = usb_ref_device(cpd, &refs, 1);
920 if (cpd->fflags & FREAD) {
921 usb_fifo_close(refs.rxfifo, cpd->fflags);
923 if (cpd->fflags & FWRITE) {
924 usb_fifo_close(refs.txfifo, cpd->fflags);
927 usb_unref_device(cpd, &refs);
933 usb_dev_init(void *arg)
935 mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
936 sx_init(&usb_sym_lock, "USB sym mutex");
937 TAILQ_INIT(&usb_sym_head);
939 /* check the UGEN methods */
940 usb_fifo_check_methods(&usb_ugen_methods);
943 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
946 usb_dev_init_post(void *arg)
949 * Create /dev/usb - this is needed for usbconfig(8), which
950 * needs a well-known device name to access.
952 usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
953 0644, USB_DEVICE_NAME);
954 if (usb_dev == NULL) {
955 DPRINTFN(0, "Could not create usb bus device\n");
959 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
962 usb_dev_uninit(void *arg)
964 if (usb_dev != NULL) {
965 destroy_dev(usb_dev);
968 mtx_destroy(&usb_ref_lock);
969 sx_destroy(&usb_sym_lock);
972 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
975 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
982 *(int *)addr = 0; /* character device */
986 /* handled by upper FS layer */
991 if (f->async_p != NULL) {
995 f->async_p = USB_TD_GET_PROC(td);
1001 /* XXX this is not the most general solution */
1003 if (f->async_p == NULL) {
1007 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1015 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1019 /*------------------------------------------------------------------------*
1020 * usb_ioctl - cdev callback
1021 *------------------------------------------------------------------------*/
1023 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
1025 struct usb_cdev_refdata refs;
1026 struct usb_cdev_privdata* cpd;
1031 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1033 err = devfs_get_cdevpriv((void **)&cpd);
1038 * Performance optimisation: We try to check for IOCTL's that
1039 * don't need the USB reference first. Then we grab the USB
1040 * reference if we need it!
1042 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1046 fflags = cpd->fflags;
1048 f = NULL; /* set default value */
1049 err = ENOIOCTL; /* set default value */
1051 if (fflags & FWRITE) {
1053 err = usb_ioctl_f_sub(f, cmd, addr, td);
1055 if (fflags & FREAD) {
1057 err = usb_ioctl_f_sub(f, cmd, addr, td);
1059 KASSERT(f != NULL, ("fifo not found"));
1060 if (err != ENOIOCTL)
1063 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1065 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1067 if (err != ENOIOCTL)
1070 if (usb_usb_ref_device(cpd, &refs)) {
1075 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1077 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1079 if (err == ENOIOCTL)
1085 /* Wait for re-enumeration, if any */
1087 while (f->udev->re_enumerate_wait != 0) {
1089 usb_unref_device(cpd, &refs);
1091 usb_pause_mtx(NULL, hz / 128);
1093 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1100 usb_unref_device(cpd, &refs);
1106 usb_poll(struct cdev* dev, int events, struct thread* td)
1108 struct usb_cdev_refdata refs;
1109 struct usb_cdev_privdata* cpd;
1112 int fflags, revents;
1114 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1115 usb_ref_device(cpd, &refs, 0) != 0)
1117 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1119 fflags = cpd->fflags;
1121 /* Figure out who needs service */
1123 if ((events & (POLLOUT | POLLWRNORM)) &&
1124 (fflags & FWRITE)) {
1128 mtx_lock(f->priv_mtx);
1130 if (!refs.is_usbfs) {
1131 if (f->flag_iserror) {
1132 /* we got an error */
1135 if (f->queue_data == NULL) {
1137 * start write transfer, if not
1140 (f->methods->f_start_write) (f);
1142 /* check if any packets are available */
1143 USB_IF_POLL(&f->free_q, m);
1146 if (f->flag_iscomplete) {
1154 revents |= events & (POLLOUT | POLLWRNORM);
1156 f->flag_isselect = 1;
1157 selrecord(td, &f->selinfo);
1160 mtx_unlock(f->priv_mtx);
1162 if ((events & (POLLIN | POLLRDNORM)) &&
1167 mtx_lock(f->priv_mtx);
1169 if (!refs.is_usbfs) {
1170 if (f->flag_iserror) {
1171 /* we have and error */
1174 if (f->queue_data == NULL) {
1176 * start read transfer, if not
1179 (f->methods->f_start_read) (f);
1181 /* check if any packets are available */
1182 USB_IF_POLL(&f->used_q, m);
1185 if (f->flag_iscomplete) {
1193 revents |= events & (POLLIN | POLLRDNORM);
1195 f->flag_isselect = 1;
1196 selrecord(td, &f->selinfo);
1198 if (!refs.is_usbfs) {
1199 /* start reading data */
1200 (f->methods->f_start_read) (f);
1204 mtx_unlock(f->priv_mtx);
1206 usb_unref_device(cpd, &refs);
1211 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
1213 struct usb_cdev_refdata refs;
1214 struct usb_cdev_privdata* cpd;
1221 uint8_t tr_data = 0;
1223 err = devfs_get_cdevpriv((void **)&cpd);
1227 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1231 fflags = cpd->fflags;
1235 /* should not happen */
1236 usb_unref_device(cpd, &refs);
1240 resid = uio->uio_resid;
1242 mtx_lock(f->priv_mtx);
1244 /* check for permanent read error */
1245 if (f->flag_iserror) {
1249 /* check if USB-FS interface is active */
1250 if (refs.is_usbfs) {
1252 * The queue is used for events that should be
1253 * retrieved using the "USB_FS_COMPLETE" ioctl.
1258 while (uio->uio_resid > 0) {
1260 USB_IF_DEQUEUE(&f->used_q, m);
1264 /* start read transfer, if not already started */
1266 (f->methods->f_start_read) (f);
1268 if (ioflag & IO_NDELAY) {
1270 /* return length before error */
1276 DPRINTF("sleeping\n");
1278 err = usb_fifo_wait(f);
1284 if (f->methods->f_filter_read) {
1286 * Sometimes it is convenient to process data at the
1287 * expense of a userland process instead of a kernel
1290 (f->methods->f_filter_read) (f, m);
1294 io_len = MIN(m->cur_data_len, uio->uio_resid);
1296 DPRINTFN(2, "transfer %d bytes from %p\n",
1297 io_len, m->cur_data_ptr);
1299 err = usb_fifo_uiomove(f,
1300 m->cur_data_ptr, io_len, uio);
1302 m->cur_data_len -= io_len;
1303 m->cur_data_ptr += io_len;
1305 if (m->cur_data_len == 0) {
1307 uint8_t last_packet;
1309 last_packet = m->last_packet;
1311 USB_IF_ENQUEUE(&f->free_q, m);
1318 USB_IF_PREPEND(&f->used_q, m);
1326 mtx_unlock(f->priv_mtx);
1328 usb_unref_device(cpd, &refs);
1334 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
1336 struct usb_cdev_refdata refs;
1337 struct usb_cdev_privdata* cpd;
1345 uint8_t tr_data = 0;
1349 err = devfs_get_cdevpriv((void **)&cpd);
1353 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1357 fflags = cpd->fflags;
1361 /* should not happen */
1362 usb_unref_device(cpd, &refs);
1365 resid = uio->uio_resid;
1367 mtx_lock(f->priv_mtx);
1369 /* check for permanent write error */
1370 if (f->flag_iserror) {
1374 /* check if USB-FS interface is active */
1375 if (refs.is_usbfs) {
1377 * The queue is used for events that should be
1378 * retrieved using the "USB_FS_COMPLETE" ioctl.
1383 if (f->queue_data == NULL) {
1384 /* start write transfer, if not already started */
1385 (f->methods->f_start_write) (f);
1387 /* we allow writing zero length data */
1389 USB_IF_DEQUEUE(&f->free_q, m);
1393 if (ioflag & IO_NDELAY) {
1395 /* return length before error */
1401 DPRINTF("sleeping\n");
1403 err = usb_fifo_wait(f);
1411 if (f->flag_have_fragment == 0) {
1413 io_len = m->cur_data_len;
1414 pdata = m->cur_data_ptr;
1415 if (io_len > uio->uio_resid)
1416 io_len = uio->uio_resid;
1417 m->cur_data_len = io_len;
1419 io_len = m->max_data_len - m->cur_data_len;
1420 pdata = m->cur_data_ptr + m->cur_data_len;
1421 if (io_len > uio->uio_resid)
1422 io_len = uio->uio_resid;
1423 m->cur_data_len += io_len;
1426 DPRINTFN(2, "transfer %d bytes to %p\n",
1429 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1432 f->flag_have_fragment = 0;
1433 USB_IF_ENQUEUE(&f->free_q, m);
1437 /* check if the buffer is ready to be transmitted */
1439 if ((f->flag_write_defrag == 0) ||
1440 (m->cur_data_len == m->max_data_len)) {
1441 f->flag_have_fragment = 0;
1444 * Check for write filter:
1446 * Sometimes it is convenient to process data
1447 * at the expense of a userland process
1448 * instead of a kernel process.
1450 if (f->methods->f_filter_write) {
1451 (f->methods->f_filter_write) (f, m);
1454 /* Put USB mbuf in the used queue */
1455 USB_IF_ENQUEUE(&f->used_q, m);
1457 /* Start writing data, if not already started */
1458 (f->methods->f_start_write) (f);
1460 /* Wait for more data or close */
1461 f->flag_have_fragment = 1;
1462 USB_IF_PREPEND(&f->free_q, m);
1465 } while (uio->uio_resid > 0);
1467 mtx_unlock(f->priv_mtx);
1469 usb_unref_device(cpd, &refs);
1475 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1479 struct usb_read_dir *urd;
1487 err = usb_read_symlink(u.urd->urd_data,
1488 u.urd->urd_startentry, u.urd->urd_maxlen);
1490 case USB_DEV_QUIRK_GET:
1491 case USB_QUIRK_NAME_GET:
1492 case USB_DEV_QUIRK_ADD:
1493 case USB_DEV_QUIRK_REMOVE:
1494 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1496 case USB_GET_TEMPLATE:
1497 *(int *)data = usb_template;
1500 case USB_SET_TEMPLATE:
1501 err = priv_check(curthread, PRIV_DRIVER);
1504 usb_template = *(int *)data;
1514 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1515 int n, struct uio *uio)
1519 mtx_unlock(f->priv_mtx);
1522 * "uiomove()" can sleep so one needs to make a wrapper,
1523 * exiting the mutex and checking things:
1525 error = uiomove(cp, n, uio);
1527 mtx_lock(f->priv_mtx);
1533 usb_fifo_wait(struct usb_fifo *f)
1537 mtx_assert(f->priv_mtx, MA_OWNED);
1539 if (f->flag_iserror) {
1543 f->flag_sleeping = 1;
1545 err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1547 if (f->flag_iserror) {
1555 usb_fifo_signal(struct usb_fifo *f)
1557 if (f->flag_sleeping) {
1558 f->flag_sleeping = 0;
1559 cv_broadcast(&f->cv_io);
1564 usb_fifo_wakeup(struct usb_fifo *f)
1568 if (f->flag_isselect) {
1569 selwakeup(&f->selinfo);
1570 f->flag_isselect = 0;
1572 if (f->async_p != NULL) {
1573 PROC_LOCK(f->async_p);
1574 psignal(f->async_p, SIGIO);
1575 PROC_UNLOCK(f->async_p);
1580 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1586 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1592 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1598 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1600 fifo->flag_flushing = 0; /* not flushing */
1604 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1606 /* check that all callback functions are OK */
1608 if (pm->f_open == NULL)
1609 pm->f_open = &usb_fifo_dummy_open;
1611 if (pm->f_close == NULL)
1612 pm->f_close = &usb_fifo_dummy_close;
1614 if (pm->f_ioctl == NULL)
1615 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1617 if (pm->f_ioctl_post == NULL)
1618 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1620 if (pm->f_start_read == NULL)
1621 pm->f_start_read = &usb_fifo_dummy_cmd;
1623 if (pm->f_stop_read == NULL)
1624 pm->f_stop_read = &usb_fifo_dummy_cmd;
1626 if (pm->f_start_write == NULL)
1627 pm->f_start_write = &usb_fifo_dummy_cmd;
1629 if (pm->f_stop_write == NULL)
1630 pm->f_stop_write = &usb_fifo_dummy_cmd;
1633 /*------------------------------------------------------------------------*
1636 * The following function will create a duplex FIFO.
1641 *------------------------------------------------------------------------*/
1643 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1644 struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1645 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1646 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1648 struct usb_fifo *f_tx;
1649 struct usb_fifo *f_rx;
1652 struct usb_fs_privdata* pd;
1654 f_sc->fp[USB_FIFO_TX] = NULL;
1655 f_sc->fp[USB_FIFO_RX] = NULL;
1660 /* check the methods */
1661 usb_fifo_check_methods(pm);
1663 if (priv_mtx == NULL)
1666 /* search for a free FIFO slot */
1667 for (n = 0;; n += 2) {
1669 if (n == USB_FIFO_MAX) {
1670 /* end of FIFOs reached */
1673 /* Check for TX FIFO */
1674 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1677 /* Check for RX FIFO */
1678 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1684 f_tx = usb_fifo_alloc();
1685 f_rx = usb_fifo_alloc();
1687 if ((f_tx == NULL) || (f_rx == NULL)) {
1688 usb_fifo_free(f_tx);
1689 usb_fifo_free(f_rx);
1692 /* initialise FIFO structures */
1694 f_tx->fifo_index = n + USB_FIFO_TX;
1695 f_tx->dev_ep_index = -1;
1696 f_tx->priv_mtx = priv_mtx;
1697 f_tx->priv_sc0 = priv_sc;
1699 f_tx->iface_index = iface_index;
1702 f_rx->fifo_index = n + USB_FIFO_RX;
1703 f_rx->dev_ep_index = -1;
1704 f_rx->priv_mtx = priv_mtx;
1705 f_rx->priv_sc0 = priv_sc;
1707 f_rx->iface_index = iface_index;
1710 f_sc->fp[USB_FIFO_TX] = f_tx;
1711 f_sc->fp[USB_FIFO_RX] = f_rx;
1713 mtx_lock(&usb_ref_lock);
1714 udev->fifo[f_tx->fifo_index] = f_tx;
1715 udev->fifo[f_rx->fifo_index] = f_rx;
1716 mtx_unlock(&usb_ref_lock);
1718 for (n = 0; n != 4; n++) {
1720 if (pm->basename[n] == NULL) {
1723 if (subunit == 0xFFFF) {
1724 if (snprintf(devname, sizeof(devname),
1725 "%s%u%s", pm->basename[n],
1726 unit, pm->postfix[n] ?
1727 pm->postfix[n] : "")) {
1731 if (snprintf(devname, sizeof(devname),
1732 "%s%u.%u%s", pm->basename[n],
1733 unit, subunit, pm->postfix[n] ?
1734 pm->postfix[n] : "")) {
1740 * Distribute the symbolic links into two FIFO structures:
1743 f_rx->symlink[n / 2] =
1744 usb_alloc_symlink(devname);
1746 f_tx->symlink[n / 2] =
1747 usb_alloc_symlink(devname);
1751 * Initialize device private data - this is used to find the
1752 * actual USB device itself.
1754 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO);
1755 pd->bus_index = device_get_unit(udev->bus->bdev);
1756 pd->dev_index = udev->device_index;
1757 pd->ep_addr = -1; /* not an endpoint */
1758 pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index;
1759 pd->mode = FREAD|FWRITE;
1761 /* Now, create the device itself */
1762 f_sc->dev = make_dev(&usb_devsw, 0, uid, gid, mode,
1764 /* XXX setting si_drv1 and creating the device is not atomic! */
1765 f_sc->dev->si_drv1 = pd;
1768 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1772 /*------------------------------------------------------------------------*
1773 * usb_fifo_alloc_buffer
1778 *------------------------------------------------------------------------*/
1780 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1783 usb_fifo_free_buffer(f);
1785 /* allocate an endpoint */
1786 f->free_q.ifq_maxlen = nbuf;
1787 f->used_q.ifq_maxlen = nbuf;
1789 f->queue_data = usb_alloc_mbufs(
1790 M_USBDEV, &f->free_q, bufsize, nbuf);
1792 if ((f->queue_data == NULL) && bufsize && nbuf) {
1795 return (0); /* success */
1798 /*------------------------------------------------------------------------*
1799 * usb_fifo_free_buffer
1801 * This function will free the buffers associated with a FIFO. This
1802 * function can be called multiple times in a row.
1803 *------------------------------------------------------------------------*/
1805 usb_fifo_free_buffer(struct usb_fifo *f)
1807 if (f->queue_data) {
1808 /* free old buffer */
1809 free(f->queue_data, M_USBDEV);
1810 f->queue_data = NULL;
1814 bzero(&f->free_q, sizeof(f->free_q));
1815 bzero(&f->used_q, sizeof(f->used_q));
1819 usb_fifo_cleanup(void* ptr)
1821 free(ptr, M_USBDEV);
1825 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1830 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1831 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1833 f_sc->fp[USB_FIFO_TX] = NULL;
1834 f_sc->fp[USB_FIFO_RX] = NULL;
1836 if (f_sc->dev != NULL) {
1837 destroy_dev_sched_cb(f_sc->dev,
1838 usb_fifo_cleanup, f_sc->dev->si_drv1);
1842 DPRINTFN(2, "detached %p\n", f_sc);
1846 usb_fifo_put_bytes_max(struct usb_fifo *f)
1851 USB_IF_POLL(&f->free_q, m);
1854 len = m->max_data_len;
1861 /*------------------------------------------------------------------------*
1865 * 0 - normal operation
1866 * 1 - set last packet flag to enforce framing
1867 *------------------------------------------------------------------------*/
1869 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1870 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1873 usb_frlength_t io_len;
1875 while (len || (what == 1)) {
1877 USB_IF_DEQUEUE(&f->free_q, m);
1882 io_len = MIN(len, m->cur_data_len);
1884 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1886 m->cur_data_len = io_len;
1890 if ((len == 0) && (what == 1)) {
1893 USB_IF_ENQUEUE(&f->used_q, m);
1897 if ((len == 0) || (what == 1)) {
1907 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1908 usb_size_t len, uint8_t what)
1913 while (len || (what == 1)) {
1915 USB_IF_DEQUEUE(&f->free_q, m);
1920 io_len = MIN(len, m->cur_data_len);
1922 bcopy(ptr, m->cur_data_ptr, io_len);
1924 m->cur_data_len = io_len;
1925 ptr = USB_ADD_BYTES(ptr, io_len);
1928 if ((len == 0) && (what == 1)) {
1931 USB_IF_ENQUEUE(&f->used_q, m);
1935 if ((len == 0) || (what == 1)) {
1945 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1949 USB_IF_DEQUEUE(&f->free_q, m);
1952 m->cur_data_len = len;
1953 m->cur_data_ptr = ptr;
1954 USB_IF_ENQUEUE(&f->used_q, m);
1962 usb_fifo_put_data_error(struct usb_fifo *f)
1964 f->flag_iserror = 1;
1968 /*------------------------------------------------------------------------*
1972 * 0 - normal operation
1973 * 1 - only get one "usb_mbuf"
1977 * 1 - data in buffer
1978 *------------------------------------------------------------------------*/
1980 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1981 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1985 usb_frlength_t io_len;
1986 uint8_t tr_data = 0;
1992 USB_IF_DEQUEUE(&f->used_q, m);
1998 io_len = MIN(len, m->cur_data_len);
2000 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2004 actlen[0] += io_len;
2005 m->cur_data_ptr += io_len;
2006 m->cur_data_len -= io_len;
2008 if ((m->cur_data_len == 0) || (what == 1)) {
2009 USB_IF_ENQUEUE(&f->free_q, m);
2017 USB_IF_PREPEND(&f->used_q, m);
2022 /* wait for data to be written out */
2025 if (f->flag_flushing) {
2026 /* check if we should send a short packet */
2027 if (f->flag_short != 0) {
2032 /* flushing complete */
2033 f->flag_flushing = 0;
2046 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2047 usb_size_t len, usb_size_t *actlen, uint8_t what)
2051 uint8_t tr_data = 0;
2057 USB_IF_DEQUEUE(&f->used_q, m);
2063 io_len = MIN(len, m->cur_data_len);
2065 bcopy(m->cur_data_ptr, ptr, io_len);
2068 ptr = USB_ADD_BYTES(ptr, io_len);
2069 actlen[0] += io_len;
2070 m->cur_data_ptr += io_len;
2071 m->cur_data_len -= io_len;
2073 if ((m->cur_data_len == 0) || (what == 1)) {
2074 USB_IF_ENQUEUE(&f->free_q, m);
2082 USB_IF_PREPEND(&f->used_q, m);
2087 /* wait for data to be written out */
2090 if (f->flag_flushing) {
2091 /* check if we should send a short packet */
2092 if (f->flag_short != 0) {
2097 /* flushing complete */
2098 f->flag_flushing = 0;
2111 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2115 USB_IF_POLL(&f->used_q, m);
2118 *plen = m->cur_data_len;
2119 *pptr = m->cur_data_ptr;
2127 usb_fifo_get_data_error(struct usb_fifo *f)
2129 f->flag_iserror = 1;
2133 /*------------------------------------------------------------------------*
2138 * Else: Pointer to symlink entry
2139 *------------------------------------------------------------------------*/
2140 struct usb_symlink *
2141 usb_alloc_symlink(const char *target)
2143 struct usb_symlink *ps;
2145 ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2149 /* XXX no longer needed */
2150 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2151 ps->src_len = strlen(ps->src_path);
2152 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2153 ps->dst_len = strlen(ps->dst_path);
2155 sx_xlock(&usb_sym_lock);
2156 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2157 sx_unlock(&usb_sym_lock);
2161 /*------------------------------------------------------------------------*
2163 *------------------------------------------------------------------------*/
2165 usb_free_symlink(struct usb_symlink *ps)
2170 sx_xlock(&usb_sym_lock);
2171 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2172 sx_unlock(&usb_sym_lock);
2177 /*------------------------------------------------------------------------*
2183 *------------------------------------------------------------------------*/
2185 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2187 struct usb_symlink *ps;
2193 sx_xlock(&usb_sym_lock);
2195 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2198 * Compute total length of source and destination symlink
2199 * strings pluss one length byte and two NUL bytes:
2201 temp = ps->src_len + ps->dst_len + 3;
2205 * Skip entry because this length cannot fit
2210 if (startentry != 0) {
2211 /* decrement read offset */
2215 if (temp > user_len) {
2216 /* out of buffer space */
2221 /* copy out total length */
2223 error = copyout(&len,
2224 USB_ADD_BYTES(user_ptr, delta), 1);
2230 /* copy out source string */
2232 error = copyout(ps->src_path,
2233 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2238 delta += ps->src_len;
2239 error = copyout(&len,
2240 USB_ADD_BYTES(user_ptr, delta), 1);
2246 /* copy out destination string */
2248 error = copyout(ps->dst_path,
2249 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2254 delta += ps->dst_len;
2255 error = copyout(&len,
2256 USB_ADD_BYTES(user_ptr, delta), 1);
2265 /* a zero length entry indicates the end */
2267 if ((user_len != 0) && (error == 0)) {
2271 error = copyout(&len,
2272 USB_ADD_BYTES(user_ptr, delta), 1);
2274 sx_unlock(&usb_sym_lock);
2279 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2284 /* send a Zero Length Packet, ZLP, before close */
2285 f->flag_short = onoff;
2289 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2294 /* defrag written data */
2295 f->flag_write_defrag = onoff;
2296 /* reset defrag state */
2297 f->flag_have_fragment = 0;
2301 usb_fifo_softc(struct usb_fifo *f)
2303 return (f->priv_sc0);
2305 #endif /* USB_HAVE_UGEN */