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