xref: /freebsd/sys/dev/usb/usb_dev.h (revision 0ec590d2)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
3718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4718cf2ccSPedro F. Giffuni  *
502ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2975973647SAndrew Thompson #ifndef _USB_DEV_H_
3075973647SAndrew Thompson #define	_USB_DEV_H_
3102ac6454SAndrew Thompson 
32d2b99310SHans Petter Selasky #ifndef USB_GLOBAL_INCLUDE_FILE
3302ac6454SAndrew Thompson #include <sys/file.h>
34ed6d949aSAndrew Thompson #include <sys/selinfo.h>
3502ac6454SAndrew Thompson #include <sys/poll.h>
3602ac6454SAndrew Thompson #include <sys/signalvar.h>
3702ac6454SAndrew Thompson #include <sys/proc.h>
38d2b99310SHans Petter Selasky #endif
3902ac6454SAndrew Thompson 
40760bc48eSAndrew Thompson struct usb_fifo;
41760bc48eSAndrew Thompson struct usb_mbuf;
4202ac6454SAndrew Thompson 
43760bc48eSAndrew Thompson struct usb_symlink {
44760bc48eSAndrew Thompson 	TAILQ_ENTRY(usb_symlink) sym_entry;
4502ac6454SAndrew Thompson 	char	src_path[32];		/* Source path - including terminating
4602ac6454SAndrew Thompson 					 * zero */
4702ac6454SAndrew Thompson 	char	dst_path[32];		/* Destination path - including
4802ac6454SAndrew Thompson 					 * terminating zero */
4902ac6454SAndrew Thompson 	uint8_t	src_len;		/* String length */
5002ac6454SAndrew Thompson 	uint8_t	dst_len;		/* String length */
5102ac6454SAndrew Thompson };
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson /*
54ee3e3ff5SAndrew Thompson  * Private per-device information.
55ee3e3ff5SAndrew Thompson  */
56760bc48eSAndrew Thompson struct usb_cdev_privdata {
57760bc48eSAndrew Thompson 	struct usb_bus		*bus;
58760bc48eSAndrew Thompson 	struct usb_device	*udev;
59760bc48eSAndrew Thompson 	struct usb_interface	*iface;
60ee3e3ff5SAndrew Thompson 	int			bus_index;	/* bus index */
61ee3e3ff5SAndrew Thompson 	int			dev_index;	/* device index */
62ee3e3ff5SAndrew Thompson 	int			ep_addr;	/* endpoint address */
637214348fSAndrew Thompson 	int			fflags;
64ee3e3ff5SAndrew Thompson 	uint8_t			fifo_index;	/* FIFO index */
65e13e19faSAndrew Thompson };
66e13e19faSAndrew Thompson 
67e13e19faSAndrew Thompson /*
68ed6d949aSAndrew Thompson  * The following structure defines a minimum re-implementation of the
69ed6d949aSAndrew Thompson  * ifqueue structure in the kernel.
70ed6d949aSAndrew Thompson  */
71ed6d949aSAndrew Thompson struct usb_ifqueue {
72ed6d949aSAndrew Thompson 	struct usb_mbuf *ifq_head;
73ed6d949aSAndrew Thompson 	struct usb_mbuf *ifq_tail;
74ed6d949aSAndrew Thompson 
75ed6d949aSAndrew Thompson 	usb_size_t ifq_len;
76ed6d949aSAndrew Thompson 	usb_size_t ifq_maxlen;
77ed6d949aSAndrew Thompson };
78ed6d949aSAndrew Thompson 
79ed6d949aSAndrew Thompson /*
80e13e19faSAndrew Thompson  * Private per-device and per-thread reference information
81e13e19faSAndrew Thompson  */
82e13e19faSAndrew Thompson struct usb_cdev_refdata {
83e13e19faSAndrew Thompson 	struct usb_fifo		*rxfifo;
84e13e19faSAndrew Thompson 	struct usb_fifo		*txfifo;
85ee3e3ff5SAndrew Thompson 	uint8_t			is_read;	/* location has read access */
86ee3e3ff5SAndrew Thompson 	uint8_t			is_write;	/* location has write access */
87ee3e3ff5SAndrew Thompson 	uint8_t			is_uref;	/* USB refcount decr. needed */
88ee3e3ff5SAndrew Thompson 	uint8_t			is_usbfs;	/* USB-FS is active */
89a18a7a41SHans Petter Selasky 	uint8_t			do_unlock;	/* USB enum unlock needed */
90ee3e3ff5SAndrew Thompson };
91ee3e3ff5SAndrew Thompson 
92760bc48eSAndrew Thompson struct usb_fs_privdata {
93ee3e3ff5SAndrew Thompson 	int bus_index;
94ee3e3ff5SAndrew Thompson 	int dev_index;
95ee3e3ff5SAndrew Thompson 	int ep_addr;
96ee3e3ff5SAndrew Thompson 	int mode;
97ee3e3ff5SAndrew Thompson 	int fifo_index;
98ee3e3ff5SAndrew Thompson 	struct cdev *cdev;
99ee3e3ff5SAndrew Thompson 
100760bc48eSAndrew Thompson 	LIST_ENTRY(usb_fs_privdata) pd_next;
101ee3e3ff5SAndrew Thompson };
102ee3e3ff5SAndrew Thompson 
103ee3e3ff5SAndrew Thompson /*
104760bc48eSAndrew Thompson  * Most of the fields in the "usb_fifo" structure are used by the
10502ac6454SAndrew Thompson  * generic USB access layer.
10602ac6454SAndrew Thompson  */
107760bc48eSAndrew Thompson struct usb_fifo {
108760bc48eSAndrew Thompson 	struct usb_ifqueue free_q;
109760bc48eSAndrew Thompson 	struct usb_ifqueue used_q;
11002ac6454SAndrew Thompson 	struct selinfo selinfo;
11102ac6454SAndrew Thompson 	struct cv cv_io;
11202ac6454SAndrew Thompson 	struct cv cv_drain;
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;
118760bc48eSAndrew Thompson 	struct usb_xfer *xfer[2];
119760bc48eSAndrew Thompson 	struct usb_xfer **fs_xfer;
12002ac6454SAndrew Thompson 	struct mtx *priv_mtx;		/* client data */
1217214348fSAndrew Thompson 	/* set if FIFO is opened by a FILE: */
122760bc48eSAndrew Thompson 	struct usb_cdev_privdata *curr_cpd;
12302ac6454SAndrew Thompson 	void   *priv_sc0;		/* client data */
12402ac6454SAndrew Thompson 	void   *priv_sc1;		/* client data */
12502ac6454SAndrew Thompson 	void   *queue_data;
1260ec590d2SBrooks Davis 	usb_size_t fs_ep_sz;
127e0a69b51SAndrew Thompson 	usb_timeout_t timeout;		/* timeout in milliseconds */
128e0a69b51SAndrew Thompson 	usb_frlength_t bufsize;		/* BULK and INTERRUPT buffer size */
129e0a69b51SAndrew Thompson 	usb_frcount_t nframes;		/* for isochronous mode */
13002ac6454SAndrew Thompson 	uint16_t dev_ep_index;		/* our device endpoint index */
13102ac6454SAndrew Thompson 	uint8_t	flag_sleeping;		/* set if FIFO is sleeping */
13202ac6454SAndrew Thompson 	uint8_t	flag_iscomplete;	/* set if a USB transfer is complete */
13302ac6454SAndrew Thompson 	uint8_t	flag_iserror;		/* set if FIFO error happened */
13402ac6454SAndrew Thompson 	uint8_t	flag_isselect;		/* set if FIFO is selected */
13502ac6454SAndrew Thompson 	uint8_t	flag_flushing;		/* set if FIFO is flushing data */
13602ac6454SAndrew Thompson 	uint8_t	flag_short;		/* set if short_ok or force_short
13702ac6454SAndrew Thompson 					 * transfer flags should be set */
13802ac6454SAndrew Thompson 	uint8_t	flag_stall;		/* set if clear stall should be run */
139bd73b187SAlfred Perlstein 	uint8_t	flag_write_defrag;	/* set to defrag written data */
140bd73b187SAlfred Perlstein 	uint8_t	flag_have_fragment;	/* set if defragging */
14102ac6454SAndrew Thompson 	uint8_t	iface_index;		/* set to the interface we belong to */
14202ac6454SAndrew Thompson 	uint8_t	fifo_index;		/* set to the FIFO index in "struct
143760bc48eSAndrew Thompson 					 * usb_device" */
14402ac6454SAndrew Thompson 	uint8_t	fs_ep_max;
14502ac6454SAndrew Thompson 	uint8_t	fifo_zlp;		/* zero length packet count */
14602ac6454SAndrew Thompson 	uint8_t	refcount;
14702ac6454SAndrew Thompson #define	USB_FIFO_REF_MAX 0xFF
14802ac6454SAndrew Thompson };
14902ac6454SAndrew Thompson 
150a593f6b8SAndrew Thompson extern struct cdevsw usb_devsw;
151ee3e3ff5SAndrew Thompson 
152a593f6b8SAndrew Thompson int	usb_fifo_wait(struct usb_fifo *fifo);
153a593f6b8SAndrew Thompson void	usb_fifo_signal(struct usb_fifo *fifo);
154a593f6b8SAndrew Thompson uint8_t	usb_fifo_opened(struct usb_fifo *fifo);
155a593f6b8SAndrew Thompson struct usb_symlink *usb_alloc_symlink(const char *target);
156a593f6b8SAndrew Thompson void	usb_free_symlink(struct usb_symlink *ps);
157a593f6b8SAndrew Thompson int	usb_read_symlink(uint8_t *user_ptr, uint32_t startentry,
15802ac6454SAndrew Thompson 	    uint32_t user_len);
15902ac6454SAndrew Thompson 
16075973647SAndrew Thompson #endif					/* _USB_DEV_H_ */
161