]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/usb_dev.c
MFC r213379, r213426, r213426, r213427, r213432, r213435, r213437, r213439,
[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/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 = ENOTTY;
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                         break;
1499                 case USB_SET_TEMPLATE:
1500                         err = priv_check(curthread, PRIV_DRIVER);
1501                         if (err)
1502                                 break;
1503                         usb_template = *(int *)data;
1504                         break;
1505         }
1506         return (err);
1507 }
1508
1509 static int
1510 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1511     int n, struct uio *uio)
1512 {
1513         int error;
1514
1515         mtx_unlock(f->priv_mtx);
1516
1517         /*
1518          * "uiomove()" can sleep so one needs to make a wrapper,
1519          * exiting the mutex and checking things:
1520          */
1521         error = uiomove(cp, n, uio);
1522
1523         mtx_lock(f->priv_mtx);
1524
1525         return (error);
1526 }
1527
1528 int
1529 usb_fifo_wait(struct usb_fifo *f)
1530 {
1531         int err;
1532
1533         mtx_assert(f->priv_mtx, MA_OWNED);
1534
1535         if (f->flag_iserror) {
1536                 /* we are gone */
1537                 return (EIO);
1538         }
1539         f->flag_sleeping = 1;
1540
1541         err = cv_wait_sig(&f->cv_io, f->priv_mtx);
1542
1543         if (f->flag_iserror) {
1544                 /* we are gone */
1545                 err = EIO;
1546         }
1547         return (err);
1548 }
1549
1550 void
1551 usb_fifo_signal(struct usb_fifo *f)
1552 {
1553         if (f->flag_sleeping) {
1554                 f->flag_sleeping = 0;
1555                 cv_broadcast(&f->cv_io);
1556         }
1557 }
1558
1559 void
1560 usb_fifo_wakeup(struct usb_fifo *f)
1561 {
1562         usb_fifo_signal(f);
1563
1564         if (f->flag_isselect) {
1565                 selwakeup(&f->selinfo);
1566                 f->flag_isselect = 0;
1567         }
1568         if (f->async_p != NULL) {
1569                 PROC_LOCK(f->async_p);
1570                 psignal(f->async_p, SIGIO);
1571                 PROC_UNLOCK(f->async_p);
1572         }
1573 }
1574
1575 static int
1576 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1577 {
1578         return (0);
1579 }
1580
1581 static void
1582 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1583 {
1584         return;
1585 }
1586
1587 static int
1588 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1589 {
1590         return (ENOIOCTL);
1591 }
1592
1593 static void
1594 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1595 {
1596         fifo->flag_flushing = 0;        /* not flushing */
1597 }
1598
1599 static void
1600 usb_fifo_check_methods(struct usb_fifo_methods *pm)
1601 {
1602         /* check that all callback functions are OK */
1603
1604         if (pm->f_open == NULL)
1605                 pm->f_open = &usb_fifo_dummy_open;
1606
1607         if (pm->f_close == NULL)
1608                 pm->f_close = &usb_fifo_dummy_close;
1609
1610         if (pm->f_ioctl == NULL)
1611                 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1612
1613         if (pm->f_ioctl_post == NULL)
1614                 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1615
1616         if (pm->f_start_read == NULL)
1617                 pm->f_start_read = &usb_fifo_dummy_cmd;
1618
1619         if (pm->f_stop_read == NULL)
1620                 pm->f_stop_read = &usb_fifo_dummy_cmd;
1621
1622         if (pm->f_start_write == NULL)
1623                 pm->f_start_write = &usb_fifo_dummy_cmd;
1624
1625         if (pm->f_stop_write == NULL)
1626                 pm->f_stop_write = &usb_fifo_dummy_cmd;
1627 }
1628
1629 /*------------------------------------------------------------------------*
1630  *      usb_fifo_attach
1631  *
1632  * The following function will create a duplex FIFO.
1633  *
1634  * Return values:
1635  * 0: Success.
1636  * Else: Failure.
1637  *------------------------------------------------------------------------*/
1638 int
1639 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
1640     struct mtx *priv_mtx, struct usb_fifo_methods *pm,
1641     struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1642     uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1643 {
1644         struct usb_fifo *f_tx;
1645         struct usb_fifo *f_rx;
1646         char devname[32];
1647         uint8_t n;
1648         struct usb_fs_privdata* pd;
1649
1650         f_sc->fp[USB_FIFO_TX] = NULL;
1651         f_sc->fp[USB_FIFO_RX] = NULL;
1652
1653         if (pm == NULL)
1654                 return (EINVAL);
1655
1656         /* check the methods */
1657         usb_fifo_check_methods(pm);
1658
1659         if (priv_mtx == NULL)
1660                 priv_mtx = &Giant;
1661
1662         /* search for a free FIFO slot */
1663         for (n = 0;; n += 2) {
1664
1665                 if (n == USB_FIFO_MAX) {
1666                         /* end of FIFOs reached */
1667                         return (ENOMEM);
1668                 }
1669                 /* Check for TX FIFO */
1670                 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1671                         continue;
1672                 }
1673                 /* Check for RX FIFO */
1674                 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1675                         continue;
1676                 }
1677                 break;
1678         }
1679
1680         f_tx = usb_fifo_alloc();
1681         f_rx = usb_fifo_alloc();
1682
1683         if ((f_tx == NULL) || (f_rx == NULL)) {
1684                 usb_fifo_free(f_tx);
1685                 usb_fifo_free(f_rx);
1686                 return (ENOMEM);
1687         }
1688         /* initialise FIFO structures */
1689
1690         f_tx->fifo_index = n + USB_FIFO_TX;
1691         f_tx->dev_ep_index = -1;
1692         f_tx->priv_mtx = priv_mtx;
1693         f_tx->priv_sc0 = priv_sc;
1694         f_tx->methods = pm;
1695         f_tx->iface_index = iface_index;
1696         f_tx->udev = udev;
1697
1698         f_rx->fifo_index = n + USB_FIFO_RX;
1699         f_rx->dev_ep_index = -1;
1700         f_rx->priv_mtx = priv_mtx;
1701         f_rx->priv_sc0 = priv_sc;
1702         f_rx->methods = pm;
1703         f_rx->iface_index = iface_index;
1704         f_rx->udev = udev;
1705
1706         f_sc->fp[USB_FIFO_TX] = f_tx;
1707         f_sc->fp[USB_FIFO_RX] = f_rx;
1708
1709         mtx_lock(&usb_ref_lock);
1710         udev->fifo[f_tx->fifo_index] = f_tx;
1711         udev->fifo[f_rx->fifo_index] = f_rx;
1712         mtx_unlock(&usb_ref_lock);
1713
1714         for (n = 0; n != 4; n++) {
1715
1716                 if (pm->basename[n] == NULL) {
1717                         continue;
1718                 }
1719                 if (subunit == 0xFFFF) {
1720                         if (snprintf(devname, sizeof(devname),
1721                             "%s%u%s", pm->basename[n],
1722                             unit, pm->postfix[n] ?
1723                             pm->postfix[n] : "")) {
1724                                 /* ignore */
1725                         }
1726                 } else {
1727                         if (snprintf(devname, sizeof(devname),
1728                             "%s%u.%u%s", pm->basename[n],
1729                             unit, subunit, pm->postfix[n] ?
1730                             pm->postfix[n] : "")) {
1731                                 /* ignore */
1732                         }
1733                 }
1734
1735                 /*
1736                  * Distribute the symbolic links into two FIFO structures:
1737                  */
1738                 if (n & 1) {
1739                         f_rx->symlink[n / 2] =
1740                             usb_alloc_symlink(devname);
1741                 } else {
1742                         f_tx->symlink[n / 2] =
1743                             usb_alloc_symlink(devname);
1744                 }
1745
1746                 /*
1747                  * Initialize device private data - this is used to find the
1748                  * actual USB device itself.
1749                  */
1750                 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, M_WAITOK | M_ZERO);
1751                 pd->bus_index = device_get_unit(udev->bus->bdev);
1752                 pd->dev_index = udev->device_index;
1753                 pd->ep_addr = -1;       /* not an endpoint */
1754                 pd->fifo_index = f_tx->fifo_index & f_rx->fifo_index;
1755                 pd->mode = FREAD|FWRITE;
1756
1757                 /* Now, create the device itself */
1758                 f_sc->dev = make_dev(&usb_devsw, 0, uid, gid, mode,
1759                     "%s", devname);
1760                 /* XXX setting si_drv1 and creating the device is not atomic! */
1761                 f_sc->dev->si_drv1 = pd;
1762         }
1763
1764         DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1765         return (0);
1766 }
1767
1768 /*------------------------------------------------------------------------*
1769  *      usb_fifo_alloc_buffer
1770  *
1771  * Return values:
1772  * 0: Success
1773  * Else failure
1774  *------------------------------------------------------------------------*/
1775 int
1776 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1777     uint16_t nbuf)
1778 {
1779         usb_fifo_free_buffer(f);
1780
1781         /* allocate an endpoint */
1782         f->free_q.ifq_maxlen = nbuf;
1783         f->used_q.ifq_maxlen = nbuf;
1784
1785         f->queue_data = usb_alloc_mbufs(
1786             M_USBDEV, &f->free_q, bufsize, nbuf);
1787
1788         if ((f->queue_data == NULL) && bufsize && nbuf) {
1789                 return (ENOMEM);
1790         }
1791         return (0);                     /* success */
1792 }
1793
1794 /*------------------------------------------------------------------------*
1795  *      usb_fifo_free_buffer
1796  *
1797  * This function will free the buffers associated with a FIFO. This
1798  * function can be called multiple times in a row.
1799  *------------------------------------------------------------------------*/
1800 void
1801 usb_fifo_free_buffer(struct usb_fifo *f)
1802 {
1803         if (f->queue_data) {
1804                 /* free old buffer */
1805                 free(f->queue_data, M_USBDEV);
1806                 f->queue_data = NULL;
1807         }
1808         /* reset queues */
1809
1810         bzero(&f->free_q, sizeof(f->free_q));
1811         bzero(&f->used_q, sizeof(f->used_q));
1812 }
1813
1814 static void
1815 usb_fifo_cleanup(void* ptr) 
1816 {
1817         free(ptr, M_USBDEV);
1818 }
1819
1820 void
1821 usb_fifo_detach(struct usb_fifo_sc *f_sc)
1822 {
1823         if (f_sc == NULL) {
1824                 return;
1825         }
1826         usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1827         usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1828
1829         f_sc->fp[USB_FIFO_TX] = NULL;
1830         f_sc->fp[USB_FIFO_RX] = NULL;
1831
1832         if (f_sc->dev != NULL) {
1833                 destroy_dev_sched_cb(f_sc->dev, 
1834                     usb_fifo_cleanup, f_sc->dev->si_drv1);
1835                 f_sc->dev = NULL;
1836         }
1837
1838         DPRINTFN(2, "detached %p\n", f_sc);
1839 }
1840
1841 usb_size_t
1842 usb_fifo_put_bytes_max(struct usb_fifo *f)
1843 {
1844         struct usb_mbuf *m;
1845         usb_size_t len;
1846
1847         USB_IF_POLL(&f->free_q, m);
1848
1849         if (m) {
1850                 len = m->max_data_len;
1851         } else {
1852                 len = 0;
1853         }
1854         return (len);
1855 }
1856
1857 /*------------------------------------------------------------------------*
1858  *      usb_fifo_put_data
1859  *
1860  * what:
1861  *  0 - normal operation
1862  *  1 - set last packet flag to enforce framing
1863  *------------------------------------------------------------------------*/
1864 void
1865 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1866     usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1867 {
1868         struct usb_mbuf *m;
1869         usb_frlength_t io_len;
1870
1871         while (len || (what == 1)) {
1872
1873                 USB_IF_DEQUEUE(&f->free_q, m);
1874
1875                 if (m) {
1876                         USB_MBUF_RESET(m);
1877
1878                         io_len = MIN(len, m->cur_data_len);
1879
1880                         usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1881
1882                         m->cur_data_len = io_len;
1883                         offset += io_len;
1884                         len -= io_len;
1885
1886                         if ((len == 0) && (what == 1)) {
1887                                 m->last_packet = 1;
1888                         }
1889                         USB_IF_ENQUEUE(&f->used_q, m);
1890
1891                         usb_fifo_wakeup(f);
1892
1893                         if ((len == 0) || (what == 1)) {
1894                                 break;
1895                         }
1896                 } else {
1897                         break;
1898                 }
1899         }
1900 }
1901
1902 void
1903 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
1904     usb_size_t len, uint8_t what)
1905 {
1906         struct usb_mbuf *m;
1907         usb_size_t io_len;
1908
1909         while (len || (what == 1)) {
1910
1911                 USB_IF_DEQUEUE(&f->free_q, m);
1912
1913                 if (m) {
1914                         USB_MBUF_RESET(m);
1915
1916                         io_len = MIN(len, m->cur_data_len);
1917
1918                         bcopy(ptr, m->cur_data_ptr, io_len);
1919
1920                         m->cur_data_len = io_len;
1921                         ptr = USB_ADD_BYTES(ptr, io_len);
1922                         len -= io_len;
1923
1924                         if ((len == 0) && (what == 1)) {
1925                                 m->last_packet = 1;
1926                         }
1927                         USB_IF_ENQUEUE(&f->used_q, m);
1928
1929                         usb_fifo_wakeup(f);
1930
1931                         if ((len == 0) || (what == 1)) {
1932                                 break;
1933                         }
1934                 } else {
1935                         break;
1936                 }
1937         }
1938 }
1939
1940 uint8_t
1941 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
1942 {
1943         struct usb_mbuf *m;
1944
1945         USB_IF_DEQUEUE(&f->free_q, m);
1946
1947         if (m) {
1948                 m->cur_data_len = len;
1949                 m->cur_data_ptr = ptr;
1950                 USB_IF_ENQUEUE(&f->used_q, m);
1951                 usb_fifo_wakeup(f);
1952                 return (1);
1953         }
1954         return (0);
1955 }
1956
1957 void
1958 usb_fifo_put_data_error(struct usb_fifo *f)
1959 {
1960         f->flag_iserror = 1;
1961         usb_fifo_wakeup(f);
1962 }
1963
1964 /*------------------------------------------------------------------------*
1965  *      usb_fifo_get_data
1966  *
1967  * what:
1968  *  0 - normal operation
1969  *  1 - only get one "usb_mbuf"
1970  *
1971  * returns:
1972  *  0 - no more data
1973  *  1 - data in buffer
1974  *------------------------------------------------------------------------*/
1975 uint8_t
1976 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
1977     usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
1978     uint8_t what)
1979 {
1980         struct usb_mbuf *m;
1981         usb_frlength_t io_len;
1982         uint8_t tr_data = 0;
1983
1984         actlen[0] = 0;
1985
1986         while (1) {
1987
1988                 USB_IF_DEQUEUE(&f->used_q, m);
1989
1990                 if (m) {
1991
1992                         tr_data = 1;
1993
1994                         io_len = MIN(len, m->cur_data_len);
1995
1996                         usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
1997
1998                         len -= io_len;
1999                         offset += io_len;
2000                         actlen[0] += io_len;
2001                         m->cur_data_ptr += io_len;
2002                         m->cur_data_len -= io_len;
2003
2004                         if ((m->cur_data_len == 0) || (what == 1)) {
2005                                 USB_IF_ENQUEUE(&f->free_q, m);
2006
2007                                 usb_fifo_wakeup(f);
2008
2009                                 if (what == 1) {
2010                                         break;
2011                                 }
2012                         } else {
2013                                 USB_IF_PREPEND(&f->used_q, m);
2014                         }
2015                 } else {
2016
2017                         if (tr_data) {
2018                                 /* wait for data to be written out */
2019                                 break;
2020                         }
2021                         if (f->flag_flushing) {
2022                                 /* check if we should send a short packet */
2023                                 if (f->flag_short != 0) {
2024                                         f->flag_short = 0;
2025                                         tr_data = 1;
2026                                         break;
2027                                 }
2028                                 /* flushing complete */
2029                                 f->flag_flushing = 0;
2030                                 usb_fifo_wakeup(f);
2031                         }
2032                         break;
2033                 }
2034                 if (len == 0) {
2035                         break;
2036                 }
2037         }
2038         return (tr_data);
2039 }
2040
2041 uint8_t
2042 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2043     usb_size_t len, usb_size_t *actlen, uint8_t what)
2044 {
2045         struct usb_mbuf *m;
2046         usb_size_t io_len;
2047         uint8_t tr_data = 0;
2048
2049         actlen[0] = 0;
2050
2051         while (1) {
2052
2053                 USB_IF_DEQUEUE(&f->used_q, m);
2054
2055                 if (m) {
2056
2057                         tr_data = 1;
2058
2059                         io_len = MIN(len, m->cur_data_len);
2060
2061                         bcopy(m->cur_data_ptr, ptr, io_len);
2062
2063                         len -= io_len;
2064                         ptr = USB_ADD_BYTES(ptr, io_len);
2065                         actlen[0] += io_len;
2066                         m->cur_data_ptr += io_len;
2067                         m->cur_data_len -= io_len;
2068
2069                         if ((m->cur_data_len == 0) || (what == 1)) {
2070                                 USB_IF_ENQUEUE(&f->free_q, m);
2071
2072                                 usb_fifo_wakeup(f);
2073
2074                                 if (what == 1) {
2075                                         break;
2076                                 }
2077                         } else {
2078                                 USB_IF_PREPEND(&f->used_q, m);
2079                         }
2080                 } else {
2081
2082                         if (tr_data) {
2083                                 /* wait for data to be written out */
2084                                 break;
2085                         }
2086                         if (f->flag_flushing) {
2087                                 /* check if we should send a short packet */
2088                                 if (f->flag_short != 0) {
2089                                         f->flag_short = 0;
2090                                         tr_data = 1;
2091                                         break;
2092                                 }
2093                                 /* flushing complete */
2094                                 f->flag_flushing = 0;
2095                                 usb_fifo_wakeup(f);
2096                         }
2097                         break;
2098                 }
2099                 if (len == 0) {
2100                         break;
2101                 }
2102         }
2103         return (tr_data);
2104 }
2105
2106 uint8_t
2107 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2108 {
2109         struct usb_mbuf *m;
2110
2111         USB_IF_POLL(&f->used_q, m);
2112
2113         if (m) {
2114                 *plen = m->cur_data_len;
2115                 *pptr = m->cur_data_ptr;
2116
2117                 return (1);
2118         }
2119         return (0);
2120 }
2121
2122 void
2123 usb_fifo_get_data_error(struct usb_fifo *f)
2124 {
2125         f->flag_iserror = 1;
2126         usb_fifo_wakeup(f);
2127 }
2128
2129 /*------------------------------------------------------------------------*
2130  *      usb_alloc_symlink
2131  *
2132  * Return values:
2133  * NULL: Failure
2134  * Else: Pointer to symlink entry
2135  *------------------------------------------------------------------------*/
2136 struct usb_symlink *
2137 usb_alloc_symlink(const char *target)
2138 {
2139         struct usb_symlink *ps;
2140
2141         ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
2142         if (ps == NULL) {
2143                 return (ps);
2144         }
2145         /* XXX no longer needed */
2146         strlcpy(ps->src_path, target, sizeof(ps->src_path));
2147         ps->src_len = strlen(ps->src_path);
2148         strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2149         ps->dst_len = strlen(ps->dst_path);
2150
2151         sx_xlock(&usb_sym_lock);
2152         TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
2153         sx_unlock(&usb_sym_lock);
2154         return (ps);
2155 }
2156
2157 /*------------------------------------------------------------------------*
2158  *      usb_free_symlink
2159  *------------------------------------------------------------------------*/
2160 void
2161 usb_free_symlink(struct usb_symlink *ps)
2162 {
2163         if (ps == NULL) {
2164                 return;
2165         }
2166         sx_xlock(&usb_sym_lock);
2167         TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
2168         sx_unlock(&usb_sym_lock);
2169
2170         free(ps, M_USBDEV);
2171 }
2172
2173 /*------------------------------------------------------------------------*
2174  *      usb_read_symlink
2175  *
2176  * Return value:
2177  * 0: Success
2178  * Else: Failure
2179  *------------------------------------------------------------------------*/
2180 int
2181 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2182 {
2183         struct usb_symlink *ps;
2184         uint32_t temp;
2185         uint32_t delta = 0;
2186         uint8_t len;
2187         int error = 0;
2188
2189         sx_xlock(&usb_sym_lock);
2190
2191         TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2192
2193                 /*
2194                  * Compute total length of source and destination symlink
2195                  * strings pluss one length byte and two NUL bytes:
2196                  */
2197                 temp = ps->src_len + ps->dst_len + 3;
2198
2199                 if (temp > 255) {
2200                         /*
2201                          * Skip entry because this length cannot fit
2202                          * into one byte:
2203                          */
2204                         continue;
2205                 }
2206                 if (startentry != 0) {
2207                         /* decrement read offset */
2208                         startentry--;
2209                         continue;
2210                 }
2211                 if (temp > user_len) {
2212                         /* out of buffer space */
2213                         break;
2214                 }
2215                 len = temp;
2216
2217                 /* copy out total length */
2218
2219                 error = copyout(&len,
2220                     USB_ADD_BYTES(user_ptr, delta), 1);
2221                 if (error) {
2222                         break;
2223                 }
2224                 delta += 1;
2225
2226                 /* copy out source string */
2227
2228                 error = copyout(ps->src_path,
2229                     USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2230                 if (error) {
2231                         break;
2232                 }
2233                 len = 0;
2234                 delta += ps->src_len;
2235                 error = copyout(&len,
2236                     USB_ADD_BYTES(user_ptr, delta), 1);
2237                 if (error) {
2238                         break;
2239                 }
2240                 delta += 1;
2241
2242                 /* copy out destination string */
2243
2244                 error = copyout(ps->dst_path,
2245                     USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2246                 if (error) {
2247                         break;
2248                 }
2249                 len = 0;
2250                 delta += ps->dst_len;
2251                 error = copyout(&len,
2252                     USB_ADD_BYTES(user_ptr, delta), 1);
2253                 if (error) {
2254                         break;
2255                 }
2256                 delta += 1;
2257
2258                 user_len -= temp;
2259         }
2260
2261         /* a zero length entry indicates the end */
2262
2263         if ((user_len != 0) && (error == 0)) {
2264
2265                 len = 0;
2266
2267                 error = copyout(&len,
2268                     USB_ADD_BYTES(user_ptr, delta), 1);
2269         }
2270         sx_unlock(&usb_sym_lock);
2271         return (error);
2272 }
2273
2274 void
2275 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2276 {
2277         if (f == NULL)
2278                 return;
2279
2280         /* send a Zero Length Packet, ZLP, before close */
2281         f->flag_short = onoff;
2282 }
2283
2284 void
2285 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2286 {
2287         if (f == NULL)
2288                 return;
2289
2290         /* defrag written data */
2291         f->flag_write_defrag = onoff;
2292         /* reset defrag state */
2293         f->flag_have_fragment = 0;
2294 }
2295
2296 void *
2297 usb_fifo_softc(struct usb_fifo *f)
2298 {
2299         return (f->priv_sc0);
2300 }
2301 #endif  /* USB_HAVE_UGEN */