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