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