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