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