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