usb4bsd: Fixes, fixes, fixes.
[dragonfly.git] / sys / bus / u4b / usb_dev.c
CommitLineData
12bd3c8b
SW
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>
12bd3c8b
SW
31#include <sys/param.h>
32#include <sys/queue.h>
33#include <sys/types.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
3a76bbe8 36#include <sys/thread2.h>
12bd3c8b
SW
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>
12bd3c8b
SW
43#include <sys/unistd.h>
44#include <sys/callout.h>
45#include <sys/malloc.h>
46#include <sys/priv.h>
47#include <sys/vnode.h>
48#include <sys/conf.h>
49#include <sys/fcntl.h>
50
722d05c3
SW
51#include <bus/u4b/usb.h>
52#include <bus/u4b/usb_ioctl.h>
53#include <bus/u4b/usbdi.h>
54#include <bus/u4b/usbdi_util.h>
12bd3c8b
SW
55
56#define USB_DEBUG_VAR usb_fifo_debug
57
722d05c3
SW
58#include <bus/u4b/usb_core.h>
59#include <bus/u4b/usb_dev.h>
60#include <bus/u4b/usb_mbuf.h>
61#include <bus/u4b/usb_process.h>
62#include <bus/u4b/usb_device.h>
63#include <bus/u4b/usb_debug.h>
64#include <bus/u4b/usb_busdma.h>
65#include <bus/u4b/usb_generic.h>
66#include <bus/u4b/usb_dynamic.h>
67#include <bus/u4b/usb_util.h>
12bd3c8b 68
722d05c3
SW
69#include <bus/u4b/usb_controller.h>
70#include <bus/u4b/usb_bus.h>
12bd3c8b
SW
71
72#include <sys/filio.h>
73#include <sys/ttycom.h>
722d05c3 74#include <sys/kern_syscall.h>
12bd3c8b
SW
75
76#include <machine/stdarg.h>
77
78#if USB_HAVE_UGEN
79
80#ifdef USB_DEBUG
81static int usb_fifo_debug = 0;
82
83static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW, 0, "USB device");
84SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RW,
85 &usb_fifo_debug, 0, "Debug Level");
86
87TUNABLE_INT("hw.usb.dev.debug", &usb_fifo_debug);
88#endif
89
12bd3c8b 90#define USB_UCRED struct ucred *ucred,
12bd3c8b
SW
91
92/* prototypes */
93
94static int usb_fifo_open(struct usb_cdev_privdata *,
95 struct usb_fifo *, int);
96static void usb_fifo_close(struct usb_fifo *, int);
97static void usb_dev_init(void *);
98static void usb_dev_init_post(void *);
99static void usb_dev_uninit(void *);
100static int usb_fifo_uiomove(struct usb_fifo *, void *, int,
101 struct uio *);
102static void usb_fifo_check_methods(struct usb_fifo_methods *);
103static struct usb_fifo *usb_fifo_alloc(void);
104static struct usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
105 uint8_t);
106static void usb_loc_fill(struct usb_fs_privdata *,
107 struct usb_cdev_privdata *);
12bd3c8b
SW
108static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
109static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
110static void usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
111
3a76bbe8
SW
112static void usb_filter_detach(struct knote *kn);
113static int usb_filter_read(struct knote *kn, long hint);
114static int usb_filter_write(struct knote *kn, long hint);
115
12bd3c8b 116static d_open_t usb_open;
722d05c3 117static d_close_t usb_close;
12bd3c8b
SW
118static d_ioctl_t usb_ioctl;
119static d_read_t usb_read;
120static d_write_t usb_write;
722d05c3 121static d_kqfilter_t usb_kqfilter;
3a76bbe8
SW
122static d_open_t usb_static_open;
123static d_close_t usb_static_close;
12bd3c8b
SW
124static d_ioctl_t usb_static_ioctl;
125
126static usb_fifo_open_t usb_fifo_dummy_open;
127static usb_fifo_close_t usb_fifo_dummy_close;
128static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
129static usb_fifo_cmd_t usb_fifo_dummy_cmd;
130
131/* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
63da4a34
SW
132struct dev_ops usb_ops = {
133 { "usbdev", 0, D_MEM },
12bd3c8b 134 .d_open = usb_open,
63da4a34
SW
135 .d_close = usb_close,
136 .d_ioctl = usb_ioctl,
12bd3c8b
SW
137 .d_read = usb_read,
138 .d_write = usb_write,
63da4a34 139 .d_kqfilter = usb_kqfilter
12bd3c8b
SW
140};
141
142static struct cdev* usb_dev = NULL;
143
722d05c3 144/* character device structure used for /bus/u4b */
63da4a34
SW
145static struct dev_ops usb_static_ops = {
146 { "usb", 0, D_MEM },
3a76bbe8
SW
147 .d_open = usb_static_open,
148 .d_close = usb_static_close,
12bd3c8b 149 .d_ioctl = usb_static_ioctl,
12bd3c8b
SW
150};
151
152static TAILQ_HEAD(, usb_symlink) usb_sym_head;
722d05c3 153static struct lock usb_sym_lock;
12bd3c8b 154
722d05c3 155struct lock usb_ref_lock;
12bd3c8b 156
3a76bbe8
SW
157#if 0
158static int usb_nevents = 0;
159static struct kqinfo usb_kqevent;
160#endif
161
12bd3c8b
SW
162/*------------------------------------------------------------------------*
163 * usb_loc_fill
164 *
165 * This is used to fill out a usb_cdev_privdata structure based on the
166 * device's address as contained in usb_fs_privdata.
167 *------------------------------------------------------------------------*/
168static void
169usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
170{
171 cpd->bus_index = pd->bus_index;
172 cpd->dev_index = pd->dev_index;
173 cpd->ep_addr = pd->ep_addr;
174 cpd->fifo_index = pd->fifo_index;
175}
176
177/*------------------------------------------------------------------------*
178 * usb_ref_device
179 *
180 * This function is used to atomically refer an USB device by its
181 * device location. If this function returns success the USB device
182 * will not dissappear until the USB device is unreferenced.
183 *
184 * Return values:
185 * 0: Success, refcount incremented on the given USB device.
186 * Else: Failure.
187 *------------------------------------------------------------------------*/
188static usb_error_t
189usb_ref_device(struct usb_cdev_privdata *cpd,
190 struct usb_cdev_refdata *crd, int need_uref)
191{
192 struct usb_fifo **ppf;
193 struct usb_fifo *f;
194
195 DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
196
197 /* clear all refs */
198 memset(crd, 0, sizeof(*crd));
199
63da4a34 200 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
201 cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
202 if (cpd->bus == NULL) {
203 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
204 goto error;
205 }
206 cpd->udev = cpd->bus->devices[cpd->dev_index];
207 if (cpd->udev == NULL) {
208 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
209 goto error;
210 }
211 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
212 DPRINTFN(2, "no dev ref\n");
213 goto error;
214 }
215 if (need_uref) {
216 DPRINTFN(2, "ref udev - needed\n");
217 cpd->udev->refcount++;
218
722d05c3 219 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
220
221 /*
222 * We need to grab the sx-lock before grabbing the
223 * FIFO refs to avoid deadlock at detach!
224 */
225 usbd_enum_lock(cpd->udev);
226
63da4a34 227 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
228
229 /*
230 * Set "is_uref" after grabbing the default SX lock
231 */
232 crd->is_uref = 1;
233 }
234
235 /* check if we are doing an open */
236 if (cpd->fflags == 0) {
237 /* use zero defaults */
238 } else {
239 /* check for write */
240 if (cpd->fflags & FWRITE) {
241 ppf = cpd->udev->fifo;
242 f = ppf[cpd->fifo_index + USB_FIFO_TX];
243 crd->txfifo = f;
244 crd->is_write = 1; /* ref */
245 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
246 goto error;
247 if (f->curr_cpd != cpd)
248 goto error;
249 /* check if USB-FS is active */
250 if (f->fs_ep_max != 0) {
251 crd->is_usbfs = 1;
252 }
253 }
254
255 /* check for read */
256 if (cpd->fflags & FREAD) {
257 ppf = cpd->udev->fifo;
258 f = ppf[cpd->fifo_index + USB_FIFO_RX];
259 crd->rxfifo = f;
260 crd->is_read = 1; /* ref */
261 if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
262 goto error;
263 if (f->curr_cpd != cpd)
264 goto error;
265 /* check if USB-FS is active */
266 if (f->fs_ep_max != 0) {
267 crd->is_usbfs = 1;
268 }
269 }
270 }
271
272 /* when everything is OK we increment the refcounts */
273 if (crd->is_write) {
274 DPRINTFN(2, "ref write\n");
275 crd->txfifo->refcount++;
276 }
277 if (crd->is_read) {
278 DPRINTFN(2, "ref read\n");
279 crd->rxfifo->refcount++;
280 }
3a76bbe8 281 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
282
283 return (0);
284
285error:
286 if (crd->is_uref) {
287 usbd_enum_unlock(cpd->udev);
288
289 if (--(cpd->udev->refcount) == 0) {
290 cv_signal(&cpd->udev->ref_cv);
291 }
292 }
63da4a34 293 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
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 *------------------------------------------------------------------------*/
308static usb_error_t
309usb_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 *------------------------------------------------------------------------*/
333static void
334usb_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->is_uref)
341 usbd_enum_unlock(cpd->udev);
342
63da4a34 343 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
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 }
63da4a34 362 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
363}
364
365static struct usb_fifo *
366usb_fifo_alloc(void)
367{
368 struct usb_fifo *f;
369
722d05c3 370 f = kmalloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
12bd3c8b
SW
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 *------------------------------------------------------------------------*/
382static int
383usb_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;
722d05c3 506 f->priv_lock = &udev->device_lock;
12bd3c8b
SW
507 f->priv_sc0 = ep;
508 f->methods = &usb_ugen_methods;
509 f->iface_index = ep->iface_index;
510 f->udev = udev;
63da4a34 511 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b 512 udev->fifo[n + USB_FIFO_TX] = f;
722d05c3 513 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
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;
722d05c3 533 f->priv_lock = &udev->device_lock;
12bd3c8b
SW
534 f->priv_sc0 = ep;
535 f->methods = &usb_ugen_methods;
536 f->iface_index = ep->iface_index;
537 f->udev = udev;
722d05c3 538 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b 539 udev->fifo[n + USB_FIFO_RX] = f;
722d05c3 540 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
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
557void
558usb_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 }
63da4a34 573 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
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) {
63da4a34
SW
590 lockmgr(&usb_ref_lock, LK_RELEASE); /* avoid LOR */
591 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
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 }
63da4a34
SW
597 lockmgr(f->priv_lock, LK_RELEASE);
598 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
599
600 /* wait for sync */
601 cv_wait(&f->cv_drain, &usb_ref_lock);
602 }
63da4a34 603 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
604
605 /* take care of closing the device here, if any */
606 usb_fifo_close(f, 0);
607
608 cv_destroy(&f->cv_io);
609 cv_destroy(&f->cv_drain);
610
722d05c3 611 kfree(f, M_USBDEV);
12bd3c8b
SW
612}
613
614static struct usb_endpoint *
615usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
616{
617 struct usb_endpoint *ep;
618 uint8_t ep_dir;
619
620 if (ep_index == 0) {
621 ep = &udev->ctrl_ep;
622 } else {
623 if (dir == USB_FIFO_RX) {
624 if (udev->flags.usb_mode == USB_MODE_HOST) {
625 ep_dir = UE_DIR_IN;
626 } else {
627 ep_dir = UE_DIR_OUT;
628 }
629 } else {
630 if (udev->flags.usb_mode == USB_MODE_HOST) {
631 ep_dir = UE_DIR_OUT;
632 } else {
633 ep_dir = UE_DIR_IN;
634 }
635 }
636 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
637 }
638
639 if (ep == NULL) {
640 /* if the endpoint does not exist then return */
641 return (NULL);
642 }
643 if (ep->edesc == NULL) {
644 /* invalid endpoint */
645 return (NULL);
646 }
647 return (ep); /* success */
648}
649
650/*------------------------------------------------------------------------*
651 * usb_fifo_open
652 *
653 * Returns:
654 * 0: Success
655 * Else: Failure
656 *------------------------------------------------------------------------*/
657static int
658usb_fifo_open(struct usb_cdev_privdata *cpd,
659 struct usb_fifo *f, int fflags)
660{
661 int err;
662
663 if (f == NULL) {
664 /* no FIFO there */
665 DPRINTFN(2, "no FIFO\n");
666 return (ENXIO);
667 }
668 /* remove FWRITE and FREAD flags */
669 fflags &= ~(FWRITE | FREAD);
670
671 /* set correct file flags */
672 if ((f->fifo_index & 1) == USB_FIFO_TX) {
673 fflags |= FWRITE;
674 } else {
675 fflags |= FREAD;
676 }
677
678 /* check if we are already opened */
679 /* we don't need any locks when checking this variable */
680 if (f->curr_cpd != NULL) {
681 err = EBUSY;
682 goto done;
683 }
684
685 /* reset short flag before open */
686 f->flag_short = 0;
687
688 /* call open method */
689 err = (f->methods->f_open) (f, fflags);
690 if (err) {
691 goto done;
692 }
722d05c3 693 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
694
695 /* reset sleep flag */
696 f->flag_sleeping = 0;
697
698 /* reset error flag */
699 f->flag_iserror = 0;
700
701 /* reset complete flag */
702 f->flag_iscomplete = 0;
703
704 /* reset select flag */
705 f->flag_isselect = 0;
706
707 /* reset flushing flag */
708 f->flag_flushing = 0;
709
710 /* reset ASYNC proc flag */
711 f->async_p = NULL;
712
722d05c3 713 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
714 /* flag the fifo as opened to prevent others */
715 f->curr_cpd = cpd;
722d05c3 716 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
717
718 /* reset queue */
719 usb_fifo_reset(f);
720
722d05c3 721 lockmgr(f->priv_lock, LK_RELEASE);
12bd3c8b
SW
722done:
723 return (err);
724}
725
726/*------------------------------------------------------------------------*
727 * usb_fifo_reset
728 *------------------------------------------------------------------------*/
729void
730usb_fifo_reset(struct usb_fifo *f)
731{
732 struct usb_mbuf *m;
733
734 if (f == NULL) {
735 return;
736 }
737 while (1) {
738 USB_IF_DEQUEUE(&f->used_q, m);
739 if (m) {
740 USB_IF_ENQUEUE(&f->free_q, m);
741 } else {
742 break;
743 }
744 }
745 /* reset have fragment flag */
746 f->flag_have_fragment = 0;
747}
748
749/*------------------------------------------------------------------------*
750 * usb_fifo_close
751 *------------------------------------------------------------------------*/
752static void
753usb_fifo_close(struct usb_fifo *f, int fflags)
754{
755 int err;
756
757 /* check if we are not opened */
758 if (f->curr_cpd == NULL) {
759 /* nothing to do - already closed */
760 return;
761 }
722d05c3 762 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
763
764 /* clear current cdev private data pointer */
765 f->curr_cpd = NULL;
766
767 /* check if we are selected */
768 if (f->flag_isselect) {
63da4a34 769#if 0 /* XXXDF */
12bd3c8b 770 selwakeup(&f->selinfo);
722d05c3 771#endif
12bd3c8b
SW
772 f->flag_isselect = 0;
773 }
774 /* check if a thread wants SIGIO */
722d05c3
SW
775 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
776 ksignal(f->async_p, SIGIO);
63da4a34 777 lwkt_reltoken(&f->async_p->p_token);
12bd3c8b
SW
778 f->async_p = NULL;
779 }
780 /* remove FWRITE and FREAD flags */
781 fflags &= ~(FWRITE | FREAD);
782
783 /* flush written data, if any */
784 if ((f->fifo_index & 1) == USB_FIFO_TX) {
785
786 if (!f->flag_iserror) {
787
788 /* set flushing flag */
789 f->flag_flushing = 1;
790
791 /* get the last packet in */
792 if (f->flag_have_fragment) {
793 struct usb_mbuf *m;
794 f->flag_have_fragment = 0;
795 USB_IF_DEQUEUE(&f->free_q, m);
796 if (m) {
797 USB_IF_ENQUEUE(&f->used_q, m);
798 }
799 }
800
801 /* start write transfer, if not already started */
802 (f->methods->f_start_write) (f);
803
804 /* check if flushed already */
805 while (f->flag_flushing &&
806 (!f->flag_iserror)) {
807 /* wait until all data has been written */
808 f->flag_sleeping = 1;
722d05c3 809 err = cv_wait_sig(&f->cv_io, f->priv_lock);
12bd3c8b
SW
810 if (err) {
811 DPRINTF("signal received\n");
812 break;
813 }
814 }
815 }
816 fflags |= FWRITE;
817
818 /* stop write transfer, if not already stopped */
819 (f->methods->f_stop_write) (f);
820 } else {
821 fflags |= FREAD;
822
823 /* stop write transfer, if not already stopped */
824 (f->methods->f_stop_read) (f);
825 }
826
827 /* check if we are sleeping */
828 if (f->flag_sleeping) {
829 DPRINTFN(2, "Sleeping at close!\n");
830 }
722d05c3 831 lockmgr(f->priv_lock, LK_RELEASE);
12bd3c8b
SW
832
833 /* call close method */
834 (f->methods->f_close) (f, fflags);
835
836 DPRINTF("closed\n");
837}
838
839/*------------------------------------------------------------------------*
840 * usb_open - cdev callback
841 *------------------------------------------------------------------------*/
842static int
722d05c3 843usb_open(struct dev_open_args *ap)
12bd3c8b 844{
63da4a34
SW
845 struct cdev *dev = ap->a_head.a_dev;
846 int fflags = ap->a_oflags;
12bd3c8b
SW
847 struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
848 struct usb_cdev_refdata refs;
849 struct usb_cdev_privdata *cpd;
850 int err, ep;
851
852 DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
853
854 KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
855 if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
856 ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
857 DPRINTFN(2, "access mode not supported\n");
858 return (EPERM);
859 }
860
722d05c3 861 cpd = kmalloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
12bd3c8b
SW
862 ep = cpd->ep_addr = pd->ep_addr;
863
864 usb_loc_fill(pd, cpd);
865 err = usb_ref_device(cpd, &refs, 1);
866 if (err) {
867 DPRINTFN(2, "cannot ref device\n");
722d05c3 868 kfree(cpd, M_USBDEV);
12bd3c8b
SW
869 return (ENXIO);
870 }
871 cpd->fflags = fflags; /* access mode for open lifetime */
872
873 /* create FIFOs, if any */
874 err = usb_fifo_create(cpd, &refs);
875 /* check for error */
876 if (err) {
877 DPRINTFN(2, "cannot create fifo\n");
878 usb_unref_device(cpd, &refs);
722d05c3 879 kfree(cpd, M_USBDEV);
12bd3c8b
SW
880 return (err);
881 }
882 if (fflags & FREAD) {
883 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
884 if (err) {
885 DPRINTFN(2, "read open failed\n");
886 usb_unref_device(cpd, &refs);
722d05c3 887 kfree(cpd, M_USBDEV);
12bd3c8b
SW
888 return (err);
889 }
890 }
891 if (fflags & FWRITE) {
892 err = usb_fifo_open(cpd, refs.txfifo, fflags);
893 if (err) {
894 DPRINTFN(2, "write open failed\n");
895 if (fflags & FREAD) {
896 usb_fifo_close(refs.rxfifo, fflags);
897 }
898 usb_unref_device(cpd, &refs);
722d05c3 899 kfree(cpd, M_USBDEV);
12bd3c8b
SW
900 return (err);
901 }
902 }
903 usb_unref_device(cpd, &refs);
63da4a34 904#if 0 /* XXX: markusp: which privs? */
12bd3c8b 905 devfs_set_cdevpriv(cpd, usb_close);
63da4a34
SW
906#endif
907 /* XXX: This might not work as I expect! */
722d05c3 908 dev->si_drv2 = (void *)cpd;
12bd3c8b
SW
909 return (0);
910}
911
912/*------------------------------------------------------------------------*
913 * usb_close - cdev callback
914 *------------------------------------------------------------------------*/
722d05c3
SW
915static int
916usb_close(struct dev_close_args *ap)
12bd3c8b 917{
63da4a34 918 struct cdev *dev = ap->a_head.a_dev;
12bd3c8b 919 struct usb_cdev_refdata refs;
722d05c3 920 struct usb_cdev_privdata *cpd = (struct usb_cdev_privdata *)dev->si_drv2;
12bd3c8b
SW
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);
950done:
722d05c3 951 kfree(cpd, M_USBDEV);
63da4a34 952 return 0;
12bd3c8b
SW
953}
954
955static void
956usb_dev_init(void *arg)
957{
722d05c3 958 lockinit(&usb_ref_lock, "USB ref mutex", 0, 0);
63da4a34 959 lockinit(&usb_sym_lock, "USB sym mutex", 0, 0);
12bd3c8b
SW
960 TAILQ_INIT(&usb_sym_head);
961
962 /* check the UGEN methods */
963 usb_fifo_check_methods(&usb_ugen_methods);
964}
965
722d05c3 966/* XXX SI_SUB_KLD? */
3a76bbe8 967SYSINIT(usb_dev_init, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, usb_dev_init, NULL);
12bd3c8b
SW
968
969static void
970usb_dev_init_post(void *arg)
971{
972 /*
63da4a34 973 * Create /dev/usb - this is needed for usbconfig(8), which
12bd3c8b
SW
974 * needs a well-known device name to access.
975 */
63da4a34 976 usb_dev = make_dev(&usb_static_ops, 0, UID_ROOT, GID_OPERATOR,
12bd3c8b
SW
977 0644, USB_DEVICE_NAME);
978 if (usb_dev == NULL) {
979 DPRINTFN(0, "Could not create usb bus device\n");
980 }
981}
982
3a76bbe8
SW
983SYSINIT(usb_dev_init_post, SI_SUB_DRIVERS, SI_ORDER_FIRST, usb_dev_init_post,
984 NULL);
12bd3c8b
SW
985
986static void
987usb_dev_uninit(void *arg)
988{
989 if (usb_dev != NULL) {
990 destroy_dev(usb_dev);
991 usb_dev = NULL;
992 }
722d05c3
SW
993 lockuninit(&usb_ref_lock);
994 lockuninit(&usb_sym_lock);
12bd3c8b
SW
995}
996
997SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
998
999static int
63da4a34
SW
1000usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
1001 struct thread *td)
12bd3c8b
SW
1002{
1003 int error = 0;
1004
1005 switch (cmd) {
1006 case FIODTYPE:
1007 *(int *)addr = 0; /* character device */
1008 break;
1009
1010 case FIONBIO:
1011 /* handled by upper FS layer */
1012 break;
1013
1014 case FIOASYNC:
1015 if (*(int *)addr) {
1016 if (f->async_p != NULL) {
1017 error = EBUSY;
1018 break;
1019 }
1020 f->async_p = USB_TD_GET_PROC(td);
1021 } else {
1022 f->async_p = NULL;
1023 }
1024 break;
1025
1026 /* XXX this is not the most general solution */
1027 case TIOCSPGRP:
1028 if (f->async_p == NULL) {
1029 error = EINVAL;
1030 break;
1031 }
1032 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
1033 error = EPERM;
1034 break;
1035 }
1036 break;
1037 default:
1038 return (ENOIOCTL);
1039 }
1040 DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
1041 return (error);
1042}
1043
1044/*------------------------------------------------------------------------*
1045 * usb_ioctl - cdev callback
1046 *------------------------------------------------------------------------*/
1047static int
722d05c3 1048usb_ioctl(struct dev_ioctl_args *ap)
12bd3c8b 1049{
63da4a34
SW
1050 struct cdev *dev = ap->a_head.a_dev;
1051 u_long cmd = ap->a_cmd;
1052 caddr_t addr = ap->a_data;
1053 /* XXX: What is this thread and where is it supposed to come from */
1054 struct thread *td = curthread;
12bd3c8b
SW
1055 struct usb_cdev_refdata refs;
1056 struct usb_cdev_privdata* cpd;
1057 struct usb_fifo *f;
1058 int fflags;
1059 int err;
1060
1061 DPRINTFN(2, "cmd=0x%lx\n", cmd);
1062
63da4a34 1063#if 0 /* XXX: cdev? */
12bd3c8b
SW
1064 err = devfs_get_cdevpriv((void **)&cpd);
1065 if (err != 0)
1066 return (err);
63da4a34
SW
1067#endif
1068
1069 /*
1070 * XXX: This might not work as I would like it to
1071 * also I need a proper return value if it does
1072 */
1073 if(dev->si_drv2 == NULL)
1074 return(-1);
1075
1076 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
1077
12bd3c8b
SW
1078 /*
1079 * Performance optimisation: We try to check for IOCTL's that
1080 * don't need the USB reference first. Then we grab the USB
1081 * reference if we need it!
1082 */
1083 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1084 if (err)
1085 return (ENXIO);
1086
1087 fflags = cpd->fflags;
1088
1089 f = NULL; /* set default value */
1090 err = ENOIOCTL; /* set default value */
1091
1092 if (fflags & FWRITE) {
1093 f = refs.txfifo;
1094 err = usb_ioctl_f_sub(f, cmd, addr, td);
1095 }
1096 if (fflags & FREAD) {
1097 f = refs.rxfifo;
1098 err = usb_ioctl_f_sub(f, cmd, addr, td);
1099 }
1100 KASSERT(f != NULL, ("fifo not found"));
1101 if (err != ENOIOCTL)
1102 goto done;
1103
1104 err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
1105
1106 DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
1107
1108 if (err != ENOIOCTL)
1109 goto done;
1110
1111 if (usb_usb_ref_device(cpd, &refs)) {
1112 err = ENXIO;
1113 goto done;
1114 }
1115
1116 err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
1117
1118 DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
1119
1120 if (err == ENOIOCTL)
1121 err = ENOTTY;
1122
1123 if (err)
1124 goto done;
1125
1126 /* Wait for re-enumeration, if any */
1127
1128 while (f->udev->re_enumerate_wait != 0) {
1129
1130 usb_unref_device(cpd, &refs);
1131
1132 usb_pause_mtx(NULL, hz / 128);
1133
1134 if (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
1135 err = ENXIO;
1136 goto done;
1137 }
1138 }
1139
1140done:
1141 usb_unref_device(cpd, &refs);
1142 return (err);
1143}
1144
3a76bbe8
SW
1145static struct filterops usb_filtops_read =
1146 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_read };
1147
1148static struct filterops usb_filtops_write =
1149 { FILTEROP_ISFD, NULL, usb_filter_detach, usb_filter_write };
1150
722d05c3
SW
1151static int
1152usb_kqfilter(struct dev_kqfilter_args *ap)
1153{
3a76bbe8
SW
1154 cdev_t dev = ap->a_head.a_dev;
1155 struct knote *kn = ap->a_kn;
1156
1157 ap->a_result = 0;
1158
1159 switch(kn->kn_filter) {
1160 case EVFILT_READ:
1161 kn->kn_fop = &usb_filtops_read;
1162 kn->kn_hook = (caddr_t)dev;
1163 break;
1164 case EVFILT_WRITE:
1165 kn->kn_fop = &usb_filtops_write;
1166 kn->kn_hook = (caddr_t)dev;
1167 break;
1168 default:
1169 ap->a_result = EOPNOTSUPP;
1170 return(0);
1171 }
1172
1173 return(0);
1174}
1175
1176static void
1177usb_filter_detach(struct knote *kn)
1178{
1179#if 0
1180 struct klist *klist;
1181
1182 klist = &usb_kqevent.ki_note;
1183 knote_remove(klist, kn);
1184#endif
1185}
1186
1187static int
1188usb_filter_read(struct knote *kn, long hint)
1189{
1190 return(0);
1191}
1192
1193static int
1194usb_filter_write(struct knote *kn, long hint)
1195{
1196 return(0);
722d05c3
SW
1197}
1198
63da4a34 1199#if 0 /* XXX implement using kqfilter */
12bd3c8b
SW
1200/* ARGSUSED */
1201static int
1202usb_poll(struct cdev* dev, int events, struct thread* td)
1203{
1204 struct usb_cdev_refdata refs;
1205 struct usb_cdev_privdata* cpd;
1206 struct usb_fifo *f;
1207 struct usb_mbuf *m;
1208 int fflags, revents;
1209
1210 if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
1211 usb_ref_device(cpd, &refs, 0) != 0)
1212 return (events &
1213 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
1214
1215 fflags = cpd->fflags;
1216
1217 /* Figure out who needs service */
1218 revents = 0;
1219 if ((events & (POLLOUT | POLLWRNORM)) &&
1220 (fflags & FWRITE)) {
1221
1222 f = refs.txfifo;
1223
722d05c3 1224 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
1225
1226 if (!refs.is_usbfs) {
1227 if (f->flag_iserror) {
1228 /* we got an error */
1229 m = (void *)1;
1230 } else {
1231 if (f->queue_data == NULL) {
1232 /*
1233 * start write transfer, if not
1234 * already started
1235 */
1236 (f->methods->f_start_write) (f);
1237 }
1238 /* check if any packets are available */
1239 USB_IF_POLL(&f->free_q, m);
1240 }
1241 } else {
1242 if (f->flag_iscomplete) {
1243 m = (void *)1;
1244 } else {
1245 m = NULL;
1246 }
1247 }
1248
1249 if (m) {
1250 revents |= events & (POLLOUT | POLLWRNORM);
1251 } else {
1252 f->flag_isselect = 1;
1253 selrecord(td, &f->selinfo);
1254 }
1255
722d05c3 1256 lockmgr(f->priv_lock);
12bd3c8b
SW
1257 }
1258 if ((events & (POLLIN | POLLRDNORM)) &&
1259 (fflags & FREAD)) {
1260
1261 f = refs.rxfifo;
1262
722d05c3 1263 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
1264
1265 if (!refs.is_usbfs) {
1266 if (f->flag_iserror) {
1267 /* we have and error */
1268 m = (void *)1;
1269 } else {
1270 if (f->queue_data == NULL) {
1271 /*
1272 * start read transfer, if not
1273 * already started
1274 */
1275 (f->methods->f_start_read) (f);
1276 }
1277 /* check if any packets are available */
1278 USB_IF_POLL(&f->used_q, m);
1279 }
1280 } else {
1281 if (f->flag_iscomplete) {
1282 m = (void *)1;
1283 } else {
1284 m = NULL;
1285 }
1286 }
1287
1288 if (m) {
1289 revents |= events & (POLLIN | POLLRDNORM);
1290 } else {
1291 f->flag_isselect = 1;
1292 selrecord(td, &f->selinfo);
1293
1294 if (!refs.is_usbfs) {
1295 /* start reading data */
1296 (f->methods->f_start_read) (f);
1297 }
1298 }
1299
722d05c3 1300 lockmgr(f->priv_lock, LK_RELEASE);
12bd3c8b
SW
1301 }
1302 usb_unref_device(cpd, &refs);
1303 return (revents);
1304}
722d05c3 1305#endif
63da4a34 1306
12bd3c8b 1307static int
722d05c3 1308usb_read(struct dev_read_args *ap)
12bd3c8b 1309{
63da4a34
SW
1310 struct cdev *dev = ap->a_head.a_dev;
1311 struct uio *uio = ap->a_uio;
1312 int ioflag = ap->a_ioflag;
12bd3c8b
SW
1313 struct usb_cdev_refdata refs;
1314 struct usb_cdev_privdata* cpd;
1315 struct usb_fifo *f;
1316 struct usb_mbuf *m;
1317 int fflags;
1318 int resid;
1319 int io_len;
1320 int err;
1321 uint8_t tr_data = 0;
1322
63da4a34 1323#if 0
12bd3c8b
SW
1324 err = devfs_get_cdevpriv((void **)&cpd);
1325 if (err != 0)
1326 return (err);
63da4a34 1327#endif
12bd3c8b 1328
63da4a34
SW
1329 if(dev->si_drv2 == NULL)
1330 return(-1);
722d05c3 1331
63da4a34 1332 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
722d05c3 1333
12bd3c8b
SW
1334 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1335 if (err) {
1336 return (ENXIO);
1337 }
1338 fflags = cpd->fflags;
1339
1340 f = refs.rxfifo;
1341 if (f == NULL) {
1342 /* should not happen */
1343 usb_unref_device(cpd, &refs);
1344 return (EPERM);
1345 }
1346
1347 resid = uio->uio_resid;
1348
722d05c3 1349 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
1350
1351 /* check for permanent read error */
1352 if (f->flag_iserror) {
1353 err = EIO;
1354 goto done;
1355 }
1356 /* check if USB-FS interface is active */
1357 if (refs.is_usbfs) {
1358 /*
1359 * The queue is used for events that should be
1360 * retrieved using the "USB_FS_COMPLETE" ioctl.
1361 */
1362 err = EINVAL;
1363 goto done;
1364 }
1365 while (uio->uio_resid > 0) {
1366
1367 USB_IF_DEQUEUE(&f->used_q, m);
1368
1369 if (m == NULL) {
1370
1371 /* start read transfer, if not already started */
1372
1373 (f->methods->f_start_read) (f);
1374
1375 if (ioflag & IO_NDELAY) {
1376 if (tr_data) {
1377 /* return length before error */
1378 break;
1379 }
1380 err = EWOULDBLOCK;
1381 break;
1382 }
1383 DPRINTF("sleeping\n");
1384
1385 err = usb_fifo_wait(f);
1386 if (err) {
1387 break;
1388 }
1389 continue;
1390 }
1391 if (f->methods->f_filter_read) {
1392 /*
1393 * Sometimes it is convenient to process data at the
1394 * expense of a userland process instead of a kernel
1395 * process.
1396 */
1397 (f->methods->f_filter_read) (f, m);
1398 }
1399 tr_data = 1;
1400
1401 io_len = MIN(m->cur_data_len, uio->uio_resid);
1402
1403 DPRINTFN(2, "transfer %d bytes from %p\n",
1404 io_len, m->cur_data_ptr);
1405
1406 err = usb_fifo_uiomove(f,
1407 m->cur_data_ptr, io_len, uio);
1408
1409 m->cur_data_len -= io_len;
1410 m->cur_data_ptr += io_len;
1411
1412 if (m->cur_data_len == 0) {
1413
1414 uint8_t last_packet;
1415
1416 last_packet = m->last_packet;
1417
1418 USB_IF_ENQUEUE(&f->free_q, m);
1419
1420 if (last_packet) {
1421 /* keep framing */
1422 break;
1423 }
1424 } else {
1425 USB_IF_PREPEND(&f->used_q, m);
1426 }
1427
1428 if (err) {
1429 break;
1430 }
1431 }
1432done:
722d05c3 1433 lockmgr(f->priv_lock, LK_RELEASE);
12bd3c8b
SW
1434
1435 usb_unref_device(cpd, &refs);
1436
1437 return (err);
1438}
1439
1440static int
722d05c3 1441usb_write(struct dev_write_args *ap)
12bd3c8b 1442{
63da4a34
SW
1443 struct cdev *dev = ap->a_head.a_dev;
1444 struct uio *uio = ap->a_uio;
1445 int ioflag = ap->a_ioflag;
12bd3c8b
SW
1446 struct usb_cdev_refdata refs;
1447 struct usb_cdev_privdata* cpd;
1448 struct usb_fifo *f;
1449 struct usb_mbuf *m;
1450 uint8_t *pdata;
1451 int fflags;
1452 int resid;
1453 int io_len;
1454 int err;
1455 uint8_t tr_data = 0;
1456
1457 DPRINTFN(2, "\n");
1458
63da4a34 1459#if 0 /* XXXDF */
12bd3c8b
SW
1460 err = devfs_get_cdevpriv((void **)&cpd);
1461 if (err != 0)
1462 return (err);
722d05c3 1463#endif
63da4a34
SW
1464
1465 if(dev->si_drv2 == NULL)
1466 return(-1);
722d05c3 1467
63da4a34 1468 cpd = (struct usb_cdev_privdata *)dev->si_drv2;
722d05c3 1469
12bd3c8b
SW
1470 err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
1471 if (err) {
1472 return (ENXIO);
1473 }
1474 fflags = cpd->fflags;
1475
1476 f = refs.txfifo;
1477 if (f == NULL) {
1478 /* should not happen */
1479 usb_unref_device(cpd, &refs);
1480 return (EPERM);
1481 }
1482 resid = uio->uio_resid;
1483
722d05c3 1484 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
1485
1486 /* check for permanent write error */
1487 if (f->flag_iserror) {
1488 err = EIO;
1489 goto done;
1490 }
1491 /* check if USB-FS interface is active */
1492 if (refs.is_usbfs) {
1493 /*
1494 * The queue is used for events that should be
1495 * retrieved using the "USB_FS_COMPLETE" ioctl.
1496 */
1497 err = EINVAL;
1498 goto done;
1499 }
1500 if (f->queue_data == NULL) {
1501 /* start write transfer, if not already started */
1502 (f->methods->f_start_write) (f);
1503 }
1504 /* we allow writing zero length data */
1505 do {
1506 USB_IF_DEQUEUE(&f->free_q, m);
1507
1508 if (m == NULL) {
1509
1510 if (ioflag & IO_NDELAY) {
1511 if (tr_data) {
1512 /* return length before error */
1513 break;
1514 }
1515 err = EWOULDBLOCK;
1516 break;
1517 }
1518 DPRINTF("sleeping\n");
1519
1520 err = usb_fifo_wait(f);
1521 if (err) {
1522 break;
1523 }
1524 continue;
1525 }
1526 tr_data = 1;
1527
1528 if (f->flag_have_fragment == 0) {
1529 USB_MBUF_RESET(m);
1530 io_len = m->cur_data_len;
1531 pdata = m->cur_data_ptr;
1532 if (io_len > uio->uio_resid)
1533 io_len = uio->uio_resid;
1534 m->cur_data_len = io_len;
1535 } else {
1536 io_len = m->max_data_len - m->cur_data_len;
1537 pdata = m->cur_data_ptr + m->cur_data_len;
1538 if (io_len > uio->uio_resid)
1539 io_len = uio->uio_resid;
1540 m->cur_data_len += io_len;
1541 }
1542
1543 DPRINTFN(2, "transfer %d bytes to %p\n",
1544 io_len, pdata);
1545
1546 err = usb_fifo_uiomove(f, pdata, io_len, uio);
1547
1548 if (err) {
1549 f->flag_have_fragment = 0;
1550 USB_IF_ENQUEUE(&f->free_q, m);
1551 break;
1552 }
1553
1554 /* check if the buffer is ready to be transmitted */
1555
1556 if ((f->flag_write_defrag == 0) ||
1557 (m->cur_data_len == m->max_data_len)) {
1558 f->flag_have_fragment = 0;
1559
1560 /*
1561 * Check for write filter:
1562 *
1563 * Sometimes it is convenient to process data
1564 * at the expense of a userland process
1565 * instead of a kernel process.
1566 */
1567 if (f->methods->f_filter_write) {
1568 (f->methods->f_filter_write) (f, m);
1569 }
1570
1571 /* Put USB mbuf in the used queue */
1572 USB_IF_ENQUEUE(&f->used_q, m);
1573
1574 /* Start writing data, if not already started */
1575 (f->methods->f_start_write) (f);
1576 } else {
1577 /* Wait for more data or close */
1578 f->flag_have_fragment = 1;
1579 USB_IF_PREPEND(&f->free_q, m);
1580 }
1581
1582 } while (uio->uio_resid > 0);
1583done:
722d05c3 1584 lockmgr(f->priv_lock, LK_RELEASE);
12bd3c8b
SW
1585
1586 usb_unref_device(cpd, &refs);
1587
1588 return (err);
1589}
1590
3a76bbe8
SW
1591
1592static int
1593usb_static_open(struct dev_open_args *ap)
1594{
1595 return(0);
1596}
1597
1598static int
1599usb_static_close(struct dev_close_args *ap)
1600{
1601 return(0);
1602}
1603
12bd3c8b 1604int
722d05c3 1605usb_static_ioctl(struct dev_ioctl_args *ap)
12bd3c8b 1606{
63da4a34
SW
1607 u_long cmd = ap->a_cmd;
1608 caddr_t data = ap->a_data;
3a76bbe8 1609 struct thread *td = curthread; /* XXX: curthread the correct choice? */
63da4a34 1610 int fflag = ap->a_fflag;
12bd3c8b
SW
1611 union {
1612 struct usb_read_dir *urd;
1613 void* data;
1614 } u;
1615 int err;
1616
1617 u.data = data;
1618 switch (cmd) {
1619 case USB_READ_DIR:
1620 err = usb_read_symlink(u.urd->urd_data,
1621 u.urd->urd_startentry, u.urd->urd_maxlen);
1622 break;
1623 case USB_DEV_QUIRK_GET:
1624 case USB_QUIRK_NAME_GET:
1625 case USB_DEV_QUIRK_ADD:
1626 case USB_DEV_QUIRK_REMOVE:
1627 err = usb_quirk_ioctl_p(cmd, data, fflag, td);
1628 break;
1629 case USB_GET_TEMPLATE:
1630 *(int *)data = usb_template;
1631 err = 0;
1632 break;
1633 case USB_SET_TEMPLATE:
1634 err = priv_check(curthread, PRIV_DRIVER);
1635 if (err)
1636 break;
1637 usb_template = *(int *)data;
1638 break;
1639 default:
1640 err = ENOTTY;
1641 break;
1642 }
1643 return (err);
1644}
1645
1646static int
1647usb_fifo_uiomove(struct usb_fifo *f, void *cp,
1648 int n, struct uio *uio)
1649{
1650 int error;
1651
722d05c3 1652 lockmgr(f->priv_lock, LK_RELEASE);
12bd3c8b
SW
1653
1654 /*
1655 * "uiomove()" can sleep so one needs to make a wrapper,
1656 * exiting the mutex and checking things:
1657 */
1658 error = uiomove(cp, n, uio);
1659
722d05c3 1660 lockmgr(f->priv_lock, LK_EXCLUSIVE);
12bd3c8b
SW
1661
1662 return (error);
1663}
1664
1665int
1666usb_fifo_wait(struct usb_fifo *f)
1667{
1668 int err;
1669
3a76bbe8 1670 KKASSERT(lockowned(f->priv_lock));
12bd3c8b
SW
1671
1672 if (f->flag_iserror) {
1673 /* we are gone */
1674 return (EIO);
1675 }
1676 f->flag_sleeping = 1;
1677
722d05c3 1678 err = cv_wait_sig(&f->cv_io, f->priv_lock);
12bd3c8b
SW
1679
1680 if (f->flag_iserror) {
1681 /* we are gone */
1682 err = EIO;
1683 }
1684 return (err);
1685}
1686
1687void
1688usb_fifo_signal(struct usb_fifo *f)
1689{
1690 if (f->flag_sleeping) {
1691 f->flag_sleeping = 0;
1692 cv_broadcast(&f->cv_io);
1693 }
1694}
1695
1696void
1697usb_fifo_wakeup(struct usb_fifo *f)
1698{
1699 usb_fifo_signal(f);
1700
1701 if (f->flag_isselect) {
63da4a34 1702#if 0 /* XXXDF */
12bd3c8b 1703 selwakeup(&f->selinfo);
722d05c3 1704#endif
12bd3c8b
SW
1705 f->flag_isselect = 0;
1706 }
722d05c3
SW
1707 if (f->async_p != NULL && lwkt_trytoken(&f->async_p->p_token)) {
1708 ksignal(f->async_p, SIGIO);
63da4a34 1709 lwkt_reltoken(&f->async_p->p_token);
12bd3c8b
SW
1710 }
1711}
1712
1713static int
1714usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
1715{
1716 return (0);
1717}
1718
1719static void
1720usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
1721{
1722 return;
1723}
1724
1725static int
1726usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1727{
1728 return (ENOIOCTL);
1729}
1730
1731static void
1732usb_fifo_dummy_cmd(struct usb_fifo *fifo)
1733{
1734 fifo->flag_flushing = 0; /* not flushing */
1735}
1736
1737static void
1738usb_fifo_check_methods(struct usb_fifo_methods *pm)
1739{
1740 /* check that all callback functions are OK */
1741
1742 if (pm->f_open == NULL)
1743 pm->f_open = &usb_fifo_dummy_open;
1744
1745 if (pm->f_close == NULL)
1746 pm->f_close = &usb_fifo_dummy_close;
1747
1748 if (pm->f_ioctl == NULL)
1749 pm->f_ioctl = &usb_fifo_dummy_ioctl;
1750
1751 if (pm->f_ioctl_post == NULL)
1752 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
1753
1754 if (pm->f_start_read == NULL)
1755 pm->f_start_read = &usb_fifo_dummy_cmd;
1756
1757 if (pm->f_stop_read == NULL)
1758 pm->f_stop_read = &usb_fifo_dummy_cmd;
1759
1760 if (pm->f_start_write == NULL)
1761 pm->f_start_write = &usb_fifo_dummy_cmd;
1762
1763 if (pm->f_stop_write == NULL)
1764 pm->f_stop_write = &usb_fifo_dummy_cmd;
1765}
1766
1767/*------------------------------------------------------------------------*
1768 * usb_fifo_attach
1769 *
1770 * The following function will create a duplex FIFO.
1771 *
1772 * Return values:
1773 * 0: Success.
1774 * Else: Failure.
1775 *------------------------------------------------------------------------*/
1776int
1777usb_fifo_attach(struct usb_device *udev, void *priv_sc,
722d05c3 1778 struct lock *priv_lock, struct usb_fifo_methods *pm,
12bd3c8b
SW
1779 struct usb_fifo_sc *f_sc, uint16_t unit, uint16_t subunit,
1780 uint8_t iface_index, uid_t uid, gid_t gid, int mode)
1781{
1782 struct usb_fifo *f_tx;
1783 struct usb_fifo *f_rx;
1784 char devname[32];
1785 uint8_t n;
1786
1787 f_sc->fp[USB_FIFO_TX] = NULL;
1788 f_sc->fp[USB_FIFO_RX] = NULL;
1789
1790 if (pm == NULL)
1791 return (EINVAL);
1792
1793 /* check the methods */
1794 usb_fifo_check_methods(pm);
1795
12bd3c8b
SW
1796 /* search for a free FIFO slot */
1797 for (n = 0;; n += 2) {
1798
1799 if (n == USB_FIFO_MAX) {
1800 /* end of FIFOs reached */
1801 return (ENOMEM);
1802 }
1803 /* Check for TX FIFO */
1804 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
1805 continue;
1806 }
1807 /* Check for RX FIFO */
1808 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
1809 continue;
1810 }
1811 break;
1812 }
1813
1814 f_tx = usb_fifo_alloc();
1815 f_rx = usb_fifo_alloc();
1816
1817 if ((f_tx == NULL) || (f_rx == NULL)) {
1818 usb_fifo_free(f_tx);
1819 usb_fifo_free(f_rx);
1820 return (ENOMEM);
1821 }
1822 /* initialise FIFO structures */
1823
1824 f_tx->fifo_index = n + USB_FIFO_TX;
1825 f_tx->dev_ep_index = -1;
722d05c3 1826 f_tx->priv_lock = priv_lock;
12bd3c8b
SW
1827 f_tx->priv_sc0 = priv_sc;
1828 f_tx->methods = pm;
1829 f_tx->iface_index = iface_index;
1830 f_tx->udev = udev;
1831
1832 f_rx->fifo_index = n + USB_FIFO_RX;
1833 f_rx->dev_ep_index = -1;
722d05c3 1834 f_rx->priv_lock = priv_lock;
12bd3c8b
SW
1835 f_rx->priv_sc0 = priv_sc;
1836 f_rx->methods = pm;
1837 f_rx->iface_index = iface_index;
1838 f_rx->udev = udev;
1839
1840 f_sc->fp[USB_FIFO_TX] = f_tx;
1841 f_sc->fp[USB_FIFO_RX] = f_rx;
1842
722d05c3 1843 lockmgr(&usb_ref_lock, LK_EXCLUSIVE);
12bd3c8b
SW
1844 udev->fifo[f_tx->fifo_index] = f_tx;
1845 udev->fifo[f_rx->fifo_index] = f_rx;
722d05c3 1846 lockmgr(&usb_ref_lock, LK_RELEASE);
12bd3c8b
SW
1847
1848 for (n = 0; n != 4; n++) {
1849
1850 if (pm->basename[n] == NULL) {
1851 continue;
1852 }
1853 if (subunit == 0xFFFF) {
722d05c3 1854 if (ksnprintf(devname, sizeof(devname),
12bd3c8b
SW
1855 "%s%u%s", pm->basename[n],
1856 unit, pm->postfix[n] ?
1857 pm->postfix[n] : "")) {
1858 /* ignore */
1859 }
1860 } else {
722d05c3 1861 if (ksnprintf(devname, sizeof(devname),
12bd3c8b
SW
1862 "%s%u.%u%s", pm->basename[n],
1863 unit, subunit, pm->postfix[n] ?
1864 pm->postfix[n] : "")) {
1865 /* ignore */
1866 }
1867 }
1868
1869 /*
1870 * Distribute the symbolic links into two FIFO structures:
1871 */
1872 if (n & 1) {
1873 f_rx->symlink[n / 2] =
1874 usb_alloc_symlink(devname);
1875 } else {
1876 f_tx->symlink[n / 2] =
1877 usb_alloc_symlink(devname);
1878 }
1879
1880 /* Create the device */
1881 f_sc->dev = usb_make_dev(udev, devname, -1,
1882 f_tx->fifo_index & f_rx->fifo_index,
1883 FREAD|FWRITE, uid, gid, mode);
1884 }
1885
1886 DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
1887 return (0);
1888}
1889
1890/*------------------------------------------------------------------------*
1891 * usb_fifo_alloc_buffer
1892 *
1893 * Return values:
1894 * 0: Success
1895 * Else failure
1896 *------------------------------------------------------------------------*/
1897int
1898usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
1899 uint16_t nbuf)
1900{
1901 usb_fifo_free_buffer(f);
1902
1903 /* allocate an endpoint */
1904 f->free_q.ifq_maxlen = nbuf;
1905 f->used_q.ifq_maxlen = nbuf;
1906
1907 f->queue_data = usb_alloc_mbufs(
1908 M_USBDEV, &f->free_q, bufsize, nbuf);
1909
1910 if ((f->queue_data == NULL) && bufsize && nbuf) {
1911 return (ENOMEM);
1912 }
1913 return (0); /* success */
1914}
1915
1916/*------------------------------------------------------------------------*
1917 * usb_fifo_free_buffer
1918 *
1919 * This function will free the buffers associated with a FIFO. This
1920 * function can be called multiple times in a row.
1921 *------------------------------------------------------------------------*/
1922void
1923usb_fifo_free_buffer(struct usb_fifo *f)
1924{
1925 if (f->queue_data) {
1926 /* free old buffer */
722d05c3 1927 kfree(f->queue_data, M_USBDEV);
12bd3c8b
SW
1928 f->queue_data = NULL;
1929 }
1930 /* reset queues */
1931
1932 memset(&f->free_q, 0, sizeof(f->free_q));
1933 memset(&f->used_q, 0, sizeof(f->used_q));
1934}
1935
1936void
1937usb_fifo_detach(struct usb_fifo_sc *f_sc)
1938{
1939 if (f_sc == NULL) {
1940 return;
1941 }
1942 usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
1943 usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
1944
1945 f_sc->fp[USB_FIFO_TX] = NULL;
1946 f_sc->fp[USB_FIFO_RX] = NULL;
1947
1948 usb_destroy_dev(f_sc->dev);
1949
1950 f_sc->dev = NULL;
1951
1952 DPRINTFN(2, "detached %p\n", f_sc);
1953}
1954
1955usb_size_t
1956usb_fifo_put_bytes_max(struct usb_fifo *f)
1957{
1958 struct usb_mbuf *m;
1959 usb_size_t len;
1960
1961 USB_IF_POLL(&f->free_q, m);
1962
1963 if (m) {
1964 len = m->max_data_len;
1965 } else {
1966 len = 0;
1967 }
1968 return (len);
1969}
1970
1971/*------------------------------------------------------------------------*
1972 * usb_fifo_put_data
1973 *
1974 * what:
1975 * 0 - normal operation
1976 * 1 - set last packet flag to enforce framing
1977 *------------------------------------------------------------------------*/
1978void
1979usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
1980 usb_frlength_t offset, usb_frlength_t len, uint8_t what)
1981{
1982 struct usb_mbuf *m;
1983 usb_frlength_t io_len;
1984
1985 while (len || (what == 1)) {
1986
1987 USB_IF_DEQUEUE(&f->free_q, m);
1988
1989 if (m) {
1990 USB_MBUF_RESET(m);
1991
1992 io_len = MIN(len, m->cur_data_len);
1993
1994 usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
1995
1996 m->cur_data_len = io_len;
1997 offset += io_len;
1998 len -= io_len;
1999
2000 if ((len == 0) && (what == 1)) {
2001 m->last_packet = 1;
2002 }
2003 USB_IF_ENQUEUE(&f->used_q, m);
2004
2005 usb_fifo_wakeup(f);
2006
2007 if ((len == 0) || (what == 1)) {
2008 break;
2009 }
2010 } else {
2011 break;
2012 }
2013 }
2014}
2015
2016void
2017usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
2018 usb_size_t len, uint8_t what)
2019{
2020 struct usb_mbuf *m;
2021 usb_size_t io_len;
2022
2023 while (len || (what == 1)) {
2024
2025 USB_IF_DEQUEUE(&f->free_q, m);
2026
2027 if (m) {
2028 USB_MBUF_RESET(m);
2029
2030 io_len = MIN(len, m->cur_data_len);
2031
2032 memcpy(m->cur_data_ptr, ptr, io_len);
2033
2034 m->cur_data_len = io_len;
2035 ptr = USB_ADD_BYTES(ptr, io_len);
2036 len -= io_len;
2037
2038 if ((len == 0) && (what == 1)) {
2039 m->last_packet = 1;
2040 }
2041 USB_IF_ENQUEUE(&f->used_q, m);
2042
2043 usb_fifo_wakeup(f);
2044
2045 if ((len == 0) || (what == 1)) {
2046 break;
2047 }
2048 } else {
2049 break;
2050 }
2051 }
2052}
2053
2054uint8_t
2055usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
2056{
2057 struct usb_mbuf *m;
2058
2059 USB_IF_DEQUEUE(&f->free_q, m);
2060
2061 if (m) {
2062 m->cur_data_len = len;
2063 m->cur_data_ptr = ptr;
2064 USB_IF_ENQUEUE(&f->used_q, m);
2065 usb_fifo_wakeup(f);
2066 return (1);
2067 }
2068 return (0);
2069}
2070
2071void
2072usb_fifo_put_data_error(struct usb_fifo *f)
2073{
2074 f->flag_iserror = 1;
2075 usb_fifo_wakeup(f);
2076}
2077
2078/*------------------------------------------------------------------------*
2079 * usb_fifo_get_data
2080 *
2081 * what:
2082 * 0 - normal operation
2083 * 1 - only get one "usb_mbuf"
2084 *
2085 * returns:
2086 * 0 - no more data
2087 * 1 - data in buffer
2088 *------------------------------------------------------------------------*/
2089uint8_t
2090usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
2091 usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
2092 uint8_t what)
2093{
2094 struct usb_mbuf *m;
2095 usb_frlength_t io_len;
2096 uint8_t tr_data = 0;
2097
2098 actlen[0] = 0;
2099
2100 while (1) {
2101
2102 USB_IF_DEQUEUE(&f->used_q, m);
2103
2104 if (m) {
2105
2106 tr_data = 1;
2107
2108 io_len = MIN(len, m->cur_data_len);
2109
2110 usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
2111
2112 len -= io_len;
2113 offset += io_len;
2114 actlen[0] += io_len;
2115 m->cur_data_ptr += io_len;
2116 m->cur_data_len -= io_len;
2117
2118 if ((m->cur_data_len == 0) || (what == 1)) {
2119 USB_IF_ENQUEUE(&f->free_q, m);
2120
2121 usb_fifo_wakeup(f);
2122
2123 if (what == 1) {
2124 break;
2125 }
2126 } else {
2127 USB_IF_PREPEND(&f->used_q, m);
2128 }
2129 } else {
2130
2131 if (tr_data) {
2132 /* wait for data to be written out */
2133 break;
2134 }
2135 if (f->flag_flushing) {
2136 /* check if we should send a short packet */
2137 if (f->flag_short != 0) {
2138 f->flag_short = 0;
2139 tr_data = 1;
2140 break;
2141 }
2142 /* flushing complete */
2143 f->flag_flushing = 0;
2144 usb_fifo_wakeup(f);
2145 }
2146 break;
2147 }
2148 if (len == 0) {
2149 break;
2150 }
2151 }
2152 return (tr_data);
2153}
2154
2155uint8_t
2156usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
2157 usb_size_t len, usb_size_t *actlen, uint8_t what)
2158{
2159 struct usb_mbuf *m;
2160 usb_size_t io_len;
2161 uint8_t tr_data = 0;
2162
2163 actlen[0] = 0;
2164
2165 while (1) {
2166
2167 USB_IF_DEQUEUE(&f->used_q, m);
2168
2169 if (m) {
2170
2171 tr_data = 1;
2172
2173 io_len = MIN(len, m->cur_data_len);
2174
2175 memcpy(ptr, m->cur_data_ptr, io_len);
2176
2177 len -= io_len;
2178 ptr = USB_ADD_BYTES(ptr, io_len);
2179 actlen[0] += io_len;
2180 m->cur_data_ptr += io_len;
2181 m->cur_data_len -= io_len;
2182
2183 if ((m->cur_data_len == 0) || (what == 1)) {
2184 USB_IF_ENQUEUE(&f->free_q, m);
2185
2186 usb_fifo_wakeup(f);
2187
2188 if (what == 1) {
2189 break;
2190 }
2191 } else {
2192 USB_IF_PREPEND(&f->used_q, m);
2193 }
2194 } else {
2195
2196 if (tr_data) {
2197 /* wait for data to be written out */
2198 break;
2199 }
2200 if (f->flag_flushing) {
2201 /* check if we should send a short packet */
2202 if (f->flag_short != 0) {
2203 f->flag_short = 0;
2204 tr_data = 1;
2205 break;
2206 }
2207 /* flushing complete */
2208 f->flag_flushing = 0;
2209 usb_fifo_wakeup(f);
2210 }
2211 break;
2212 }
2213 if (len == 0) {
2214 break;
2215 }
2216 }
2217 return (tr_data);
2218}
2219
2220uint8_t
2221usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
2222{
2223 struct usb_mbuf *m;
2224
2225 USB_IF_POLL(&f->used_q, m);
2226
2227 if (m) {
2228 *plen = m->cur_data_len;
2229 *pptr = m->cur_data_ptr;
2230
2231 return (1);
2232 }
2233 return (0);
2234}
2235
2236void
2237usb_fifo_get_data_error(struct usb_fifo *f)
2238{
2239 f->flag_iserror = 1;
2240 usb_fifo_wakeup(f);
2241}
2242
2243/*------------------------------------------------------------------------*
2244 * usb_alloc_symlink
2245 *
2246 * Return values:
2247 * NULL: Failure
2248 * Else: Pointer to symlink entry
2249 *------------------------------------------------------------------------*/
2250struct usb_symlink *
2251usb_alloc_symlink(const char *target)
2252{
2253 struct usb_symlink *ps;
2254
722d05c3 2255 ps = kmalloc(sizeof(*ps), M_USBDEV, M_WAITOK);
12bd3c8b
SW
2256 if (ps == NULL) {
2257 return (ps);
2258 }
2259 /* XXX no longer needed */
2260 strlcpy(ps->src_path, target, sizeof(ps->src_path));
2261 ps->src_len = strlen(ps->src_path);
2262 strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
2263 ps->dst_len = strlen(ps->dst_path);
2264
722d05c3 2265 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
12bd3c8b 2266 TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
722d05c3 2267 lockmgr(&usb_sym_lock, LK_RELEASE);
12bd3c8b
SW
2268 return (ps);
2269}
2270
2271/*------------------------------------------------------------------------*
2272 * usb_free_symlink
2273 *------------------------------------------------------------------------*/
2274void
2275usb_free_symlink(struct usb_symlink *ps)
2276{
2277 if (ps == NULL) {
2278 return;
2279 }
722d05c3 2280 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
12bd3c8b 2281 TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
722d05c3 2282 lockmgr(&usb_sym_lock, LK_RELEASE);
12bd3c8b 2283
722d05c3 2284 kfree(ps, M_USBDEV);
12bd3c8b
SW
2285}
2286
2287/*------------------------------------------------------------------------*
2288 * usb_read_symlink
2289 *
2290 * Return value:
2291 * 0: Success
2292 * Else: Failure
2293 *------------------------------------------------------------------------*/
2294int
2295usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
2296{
2297 struct usb_symlink *ps;
2298 uint32_t temp;
2299 uint32_t delta = 0;
2300 uint8_t len;
2301 int error = 0;
2302
722d05c3 2303 lockmgr(&usb_sym_lock, LK_EXCLUSIVE);
12bd3c8b
SW
2304
2305 TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
2306
2307 /*
2308 * Compute total length of source and destination symlink
2309 * strings pluss one length byte and two NUL bytes:
2310 */
2311 temp = ps->src_len + ps->dst_len + 3;
2312
2313 if (temp > 255) {
2314 /*
2315 * Skip entry because this length cannot fit
2316 * into one byte:
2317 */
2318 continue;
2319 }
2320 if (startentry != 0) {
2321 /* decrement read offset */
2322 startentry--;
2323 continue;
2324 }
2325 if (temp > user_len) {
2326 /* out of buffer space */
2327 break;
2328 }
2329 len = temp;
2330
2331 /* copy out total length */
2332
2333 error = copyout(&len,
2334 USB_ADD_BYTES(user_ptr, delta), 1);
2335 if (error) {
2336 break;
2337 }
2338 delta += 1;
2339
2340 /* copy out source string */
2341
2342 error = copyout(ps->src_path,
2343 USB_ADD_BYTES(user_ptr, delta), ps->src_len);
2344 if (error) {
2345 break;
2346 }
2347 len = 0;
2348 delta += ps->src_len;
2349 error = copyout(&len,
2350 USB_ADD_BYTES(user_ptr, delta), 1);
2351 if (error) {
2352 break;
2353 }
2354 delta += 1;
2355
2356 /* copy out destination string */
2357
2358 error = copyout(ps->dst_path,
2359 USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
2360 if (error) {
2361 break;
2362 }
2363 len = 0;
2364 delta += ps->dst_len;
2365 error = copyout(&len,
2366 USB_ADD_BYTES(user_ptr, delta), 1);
2367 if (error) {
2368 break;
2369 }
2370 delta += 1;
2371
2372 user_len -= temp;
2373 }
2374
2375 /* a zero length entry indicates the end */
2376
2377 if ((user_len != 0) && (error == 0)) {
2378
2379 len = 0;
2380
2381 error = copyout(&len,
2382 USB_ADD_BYTES(user_ptr, delta), 1);
2383 }
722d05c3 2384 lockmgr(&usb_sym_lock, LK_RELEASE);
12bd3c8b
SW
2385 return (error);
2386}
2387
2388void
2389usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
2390{
2391 if (f == NULL)
2392 return;
2393
2394 /* send a Zero Length Packet, ZLP, before close */
2395 f->flag_short = onoff;
2396}
2397
2398void
2399usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
2400{
2401 if (f == NULL)
2402 return;
2403
2404 /* defrag written data */
2405 f->flag_write_defrag = onoff;
2406 /* reset defrag state */
2407 f->flag_have_fragment = 0;
2408}
2409
2410void *
2411usb_fifo_softc(struct usb_fifo *f)
2412{
2413 return (f->priv_sc0);
2414}
2415#endif /* USB_HAVE_UGEN */