xref: /freebsd/sys/dev/usb/usb_dev.h (revision 71625ec9)
102ac6454SAndrew Thompson /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
49b077d72SHans Petter Selasky  * Copyright (c) 2008-2023 Hans Petter Selasky
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
2875973647SAndrew Thompson #ifndef _USB_DEV_H_
2975973647SAndrew Thompson #define	_USB_DEV_H_
3002ac6454SAndrew Thompson 
31d2b99310SHans Petter Selasky #ifndef USB_GLOBAL_INCLUDE_FILE
3202ac6454SAndrew Thompson #include <sys/file.h>
33ed6d949aSAndrew Thompson #include <sys/selinfo.h>
3402ac6454SAndrew Thompson #include <sys/poll.h>
3502ac6454SAndrew Thompson #include <sys/signalvar.h>
3602ac6454SAndrew Thompson #include <sys/proc.h>
37d2b99310SHans Petter Selasky #endif
3802ac6454SAndrew Thompson 
39760bc48eSAndrew Thompson struct usb_fifo;
40760bc48eSAndrew Thompson struct usb_mbuf;
4102ac6454SAndrew Thompson 
42760bc48eSAndrew Thompson struct usb_symlink {
43760bc48eSAndrew Thompson 	TAILQ_ENTRY(usb_symlink) sym_entry;
4402ac6454SAndrew Thompson 	char	src_path[32];		/* Source path - including terminating
4502ac6454SAndrew Thompson 					 * zero */
4602ac6454SAndrew Thompson 	char	dst_path[32];		/* Destination path - including
4702ac6454SAndrew Thompson 					 * terminating zero */
4802ac6454SAndrew Thompson 	uint8_t	src_len;		/* String length */
4902ac6454SAndrew Thompson 	uint8_t	dst_len;		/* String length */
5002ac6454SAndrew Thompson };
5102ac6454SAndrew Thompson 
5202ac6454SAndrew Thompson /*
53ee3e3ff5SAndrew Thompson  * Private per-device information.
54ee3e3ff5SAndrew Thompson  */
55760bc48eSAndrew Thompson struct usb_cdev_privdata {
56760bc48eSAndrew Thompson 	struct usb_bus		*bus;
57760bc48eSAndrew Thompson 	struct usb_device	*udev;
58760bc48eSAndrew Thompson 	struct usb_interface	*iface;
59ee3e3ff5SAndrew Thompson 	int			bus_index;	/* bus index */
60ee3e3ff5SAndrew Thompson 	int			dev_index;	/* device index */
61ee3e3ff5SAndrew Thompson 	int			ep_addr;	/* endpoint address */
627214348fSAndrew Thompson 	int			fflags;
63ee3e3ff5SAndrew Thompson 	uint8_t			fifo_index;	/* FIFO index */
64e13e19faSAndrew Thompson };
65e13e19faSAndrew Thompson 
66e13e19faSAndrew Thompson /*
67ed6d949aSAndrew Thompson  * The following structure defines a minimum re-implementation of the
68ed6d949aSAndrew Thompson  * ifqueue structure in the kernel.
69ed6d949aSAndrew Thompson  */
70ed6d949aSAndrew Thompson struct usb_ifqueue {
71ed6d949aSAndrew Thompson 	struct usb_mbuf *ifq_head;
72ed6d949aSAndrew Thompson 	struct usb_mbuf *ifq_tail;
73ed6d949aSAndrew Thompson 
74ed6d949aSAndrew Thompson 	usb_size_t ifq_len;
75ed6d949aSAndrew Thompson 	usb_size_t ifq_maxlen;
76ed6d949aSAndrew Thompson };
77ed6d949aSAndrew Thompson 
78ed6d949aSAndrew Thompson /*
79e13e19faSAndrew Thompson  * Private per-device and per-thread reference information
80e13e19faSAndrew Thompson  */
81e13e19faSAndrew Thompson struct usb_cdev_refdata {
82e13e19faSAndrew Thompson 	struct usb_fifo		*rxfifo;
83e13e19faSAndrew Thompson 	struct usb_fifo		*txfifo;
84ee3e3ff5SAndrew Thompson 	uint8_t			is_read;	/* location has read access */
85ee3e3ff5SAndrew Thompson 	uint8_t			is_write;	/* location has write access */
86ee3e3ff5SAndrew Thompson 	uint8_t			is_uref;	/* USB refcount decr. needed */
87ee3e3ff5SAndrew Thompson 	uint8_t			is_usbfs;	/* USB-FS is active */
88a18a7a41SHans Petter Selasky 	uint8_t			do_unlock;	/* USB enum unlock needed */
89ee3e3ff5SAndrew Thompson };
90ee3e3ff5SAndrew Thompson 
91760bc48eSAndrew Thompson struct usb_fs_privdata {
92ee3e3ff5SAndrew Thompson 	int bus_index;
93ee3e3ff5SAndrew Thompson 	int dev_index;
94ee3e3ff5SAndrew Thompson 	int ep_addr;
95ee3e3ff5SAndrew Thompson 	int mode;
96ee3e3ff5SAndrew Thompson 	int fifo_index;
97ee3e3ff5SAndrew Thompson 	struct cdev *cdev;
98ee3e3ff5SAndrew Thompson 
99760bc48eSAndrew Thompson 	LIST_ENTRY(usb_fs_privdata) pd_next;
100ee3e3ff5SAndrew Thompson };
101ee3e3ff5SAndrew Thompson 
102ee3e3ff5SAndrew Thompson /*
103760bc48eSAndrew Thompson  * Most of the fields in the "usb_fifo" structure are used by the
10402ac6454SAndrew Thompson  * generic USB access layer.
10502ac6454SAndrew Thompson  */
106760bc48eSAndrew Thompson struct usb_fifo {
107760bc48eSAndrew Thompson 	struct usb_ifqueue free_q;
108760bc48eSAndrew Thompson 	struct usb_ifqueue used_q;
10902ac6454SAndrew Thompson 	struct selinfo selinfo;
11002ac6454SAndrew Thompson 	struct cv cv_io;
11102ac6454SAndrew Thompson 	struct cv cv_drain;
1129b077d72SHans Petter Selasky 	struct sx fs_fastpath_lock;
113760bc48eSAndrew Thompson 	struct usb_fifo_methods *methods;
114760bc48eSAndrew Thompson 	struct usb_symlink *symlink[2];/* our symlinks */
11502ac6454SAndrew Thompson 	struct proc *async_p;		/* process that wants SIGIO */
116760bc48eSAndrew Thompson 	struct usb_fs_endpoint *fs_ep_ptr;
117760bc48eSAndrew Thompson 	struct usb_device *udev;
1189b077d72SHans Petter Selasky #define	USB_FS_XFER_MAX 126
119760bc48eSAndrew Thompson 	struct usb_xfer *xfer[2];
1209b077d72SHans Petter Selasky 	struct usb_xfer *fs_xfer[USB_FS_XFER_MAX];
12102ac6454SAndrew Thompson 	struct mtx *priv_mtx;		/* client data */
1227214348fSAndrew Thompson 	/* set if FIFO is opened by a FILE: */
123760bc48eSAndrew Thompson 	struct usb_cdev_privdata *curr_cpd;
12402ac6454SAndrew Thompson 	void   *priv_sc0;		/* client data */
12502ac6454SAndrew Thompson 	void   *priv_sc1;		/* client data */
12602ac6454SAndrew Thompson 	void   *queue_data;
1270ec590d2SBrooks Davis 	usb_size_t fs_ep_sz;
128e0a69b51SAndrew Thompson 	usb_timeout_t timeout;		/* timeout in milliseconds */
129e0a69b51SAndrew Thompson 	usb_frlength_t bufsize;		/* BULK and INTERRUPT buffer size */
130e0a69b51SAndrew Thompson 	usb_frcount_t nframes;		/* for isochronous mode */
13102ac6454SAndrew Thompson 	uint16_t dev_ep_index;		/* our device endpoint index */
13202ac6454SAndrew Thompson 	uint8_t	flag_sleeping;		/* set if FIFO is sleeping */
13302ac6454SAndrew Thompson 	uint8_t	flag_iscomplete;	/* set if a USB transfer is complete */
13402ac6454SAndrew Thompson 	uint8_t	flag_iserror;		/* set if FIFO error happened */
13502ac6454SAndrew Thompson 	uint8_t	flag_isselect;		/* set if FIFO is selected */
13602ac6454SAndrew Thompson 	uint8_t	flag_flushing;		/* set if FIFO is flushing data */
13702ac6454SAndrew Thompson 	uint8_t	flag_short;		/* set if short_ok or force_short
13802ac6454SAndrew Thompson 					 * transfer flags should be set */
13902ac6454SAndrew Thompson 	uint8_t	flag_stall;		/* set if clear stall should be run */
140bd73b187SAlfred Perlstein 	uint8_t	flag_write_defrag;	/* set to defrag written data */
141bd73b187SAlfred Perlstein 	uint8_t	flag_have_fragment;	/* set if defragging */
14202ac6454SAndrew Thompson 	uint8_t	iface_index;		/* set to the interface we belong to */
14302ac6454SAndrew Thompson 	uint8_t	fifo_index;		/* set to the FIFO index in "struct
144760bc48eSAndrew Thompson 					 * usb_device" */
14502ac6454SAndrew Thompson 	uint8_t	fs_ep_max;
14602ac6454SAndrew Thompson 	uint8_t	fifo_zlp;		/* zero length packet count */
14702ac6454SAndrew Thompson 	uint8_t	refcount;
14802ac6454SAndrew Thompson #define	USB_FIFO_REF_MAX 0xFF
14902ac6454SAndrew Thompson };
15002ac6454SAndrew Thompson 
151a593f6b8SAndrew Thompson extern struct cdevsw usb_devsw;
152ee3e3ff5SAndrew Thompson 
153a593f6b8SAndrew Thompson int	usb_fifo_wait(struct usb_fifo *fifo);
154a593f6b8SAndrew Thompson void	usb_fifo_signal(struct usb_fifo *fifo);
155a593f6b8SAndrew Thompson uint8_t	usb_fifo_opened(struct usb_fifo *fifo);
156a593f6b8SAndrew Thompson struct usb_symlink *usb_alloc_symlink(const char *target);
157a593f6b8SAndrew Thompson void	usb_free_symlink(struct usb_symlink *ps);
158a593f6b8SAndrew Thompson int	usb_read_symlink(uint8_t *user_ptr, uint32_t startentry,
15902ac6454SAndrew Thompson 	    uint32_t user_len);
16002ac6454SAndrew Thompson 
16175973647SAndrew Thompson #endif					/* _USB_DEV_H_ */
162