xref: /freebsd/sys/dev/usb/serial/usb_serial.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*	$NetBSD: ucomvar.h,v 1.9 2001/01/23 21:56:17 augustss Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
4*eebd9d53SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
602ac6454SAndrew Thompson  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
702ac6454SAndrew Thompson  * All rights reserved.
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1002ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1102ac6454SAndrew Thompson  * are met:
1202ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1402ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1602ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1702ac6454SAndrew Thompson  *
1802ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1902ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2002ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2102ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2202ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2302ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2402ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2502ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2602ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2702ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2802ac6454SAndrew Thompson  * SUCH DAMAGE.
2902ac6454SAndrew Thompson  */
3002ac6454SAndrew Thompson 
3102ac6454SAndrew Thompson /*-
3202ac6454SAndrew Thompson  * Copyright (c) 1999 The NetBSD Foundation, Inc.
3302ac6454SAndrew Thompson  * All rights reserved.
3402ac6454SAndrew Thompson  *
3502ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3602ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
3702ac6454SAndrew Thompson  * Carlstedt Research & Technology.
3802ac6454SAndrew Thompson  *
3902ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
4002ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
4102ac6454SAndrew Thompson  * are met:
4202ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4402ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4602ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4702ac6454SAndrew Thompson  *
4802ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4902ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5002ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5102ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5202ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5302ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5402ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5502ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5602ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5702ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5802ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
5902ac6454SAndrew Thompson  */
6002ac6454SAndrew Thompson 
6175973647SAndrew Thompson #ifndef _USB_SERIAL_H_
6275973647SAndrew Thompson #define	_USB_SERIAL_H_
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #include <sys/tty.h>
6502ac6454SAndrew Thompson #include <sys/serial.h>
6602ac6454SAndrew Thompson #include <sys/fcntl.h>
673a4a1a7fSHans Petter Selasky #include <sys/sysctl.h>
68bb057644SHans Petter Selasky #include <sys/timepps.h>
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson /* Module interface related macros */
7102ac6454SAndrew Thompson #define	UCOM_MODVER	1
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #define	UCOM_MINVER	1
7402ac6454SAndrew Thompson #define	UCOM_PREFVER	UCOM_MODVER
7502ac6454SAndrew Thompson #define	UCOM_MAXVER	1
76d30d96eaSHans Petter Selasky #define	UCOM_JITTERBUF_SIZE	128	/* bytes */
7702ac6454SAndrew Thompson 
78760bc48eSAndrew Thompson struct usb_device;
79760bc48eSAndrew Thompson struct ucom_softc;
80760bc48eSAndrew Thompson struct usb_device_request;
8102ac6454SAndrew Thompson struct thread;
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson /*
84a593f6b8SAndrew Thompson  * NOTE: There is no guarantee that "ucom_cfg_close()" will
85a593f6b8SAndrew Thompson  * be called after "ucom_cfg_open()" if the device is detached
8602ac6454SAndrew Thompson  * while it is open!
8702ac6454SAndrew Thompson  */
88760bc48eSAndrew Thompson struct ucom_callback {
89a593f6b8SAndrew Thompson 	void    (*ucom_cfg_get_status) (struct ucom_softc *, uint8_t *plsr, uint8_t *pmsr);
90a593f6b8SAndrew Thompson 	void    (*ucom_cfg_set_dtr) (struct ucom_softc *, uint8_t);
91a593f6b8SAndrew Thompson 	void    (*ucom_cfg_set_rts) (struct ucom_softc *, uint8_t);
92a593f6b8SAndrew Thompson 	void    (*ucom_cfg_set_break) (struct ucom_softc *, uint8_t);
93655dc9d0SAndrew Thompson 	void    (*ucom_cfg_set_ring) (struct ucom_softc *, uint8_t);
94a593f6b8SAndrew Thompson 	void    (*ucom_cfg_param) (struct ucom_softc *, struct termios *);
95a593f6b8SAndrew Thompson 	void    (*ucom_cfg_open) (struct ucom_softc *);
96a593f6b8SAndrew Thompson 	void    (*ucom_cfg_close) (struct ucom_softc *);
97a593f6b8SAndrew Thompson 	int     (*ucom_pre_open) (struct ucom_softc *);
98a593f6b8SAndrew Thompson 	int     (*ucom_pre_param) (struct ucom_softc *, struct termios *);
99a593f6b8SAndrew Thompson 	int     (*ucom_ioctl) (struct ucom_softc *, uint32_t, caddr_t, int, struct thread *);
100a593f6b8SAndrew Thompson 	void    (*ucom_start_read) (struct ucom_softc *);
101a593f6b8SAndrew Thompson 	void    (*ucom_stop_read) (struct ucom_softc *);
102a593f6b8SAndrew Thompson 	void    (*ucom_start_write) (struct ucom_softc *);
103a593f6b8SAndrew Thompson 	void    (*ucom_stop_write) (struct ucom_softc *);
104015bb88fSNick Hibma 	void    (*ucom_tty_name) (struct ucom_softc *, char *pbuf, uint16_t buflen, uint16_t unit, uint16_t subunit);
105655dc9d0SAndrew Thompson 	void    (*ucom_poll) (struct ucom_softc *);
1068f42c748SHans Petter Selasky 	void	(*ucom_free) (struct ucom_softc *);
10702ac6454SAndrew Thompson };
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson /* Line status register */
11002ac6454SAndrew Thompson #define	ULSR_RCV_FIFO	0x80
11102ac6454SAndrew Thompson #define	ULSR_TSRE	0x40		/* Transmitter empty: byte sent */
11202ac6454SAndrew Thompson #define	ULSR_TXRDY	0x20		/* Transmitter buffer empty */
11302ac6454SAndrew Thompson #define	ULSR_BI		0x10		/* Break detected */
11402ac6454SAndrew Thompson #define	ULSR_FE		0x08		/* Framing error: bad stop bit */
11502ac6454SAndrew Thompson #define	ULSR_PE		0x04		/* Parity error */
11602ac6454SAndrew Thompson #define	ULSR_OE		0x02		/* Overrun, lost incoming byte */
11702ac6454SAndrew Thompson #define	ULSR_RXRDY	0x01		/* Byte ready in Receive Buffer */
11802ac6454SAndrew Thompson #define	ULSR_RCV_MASK	0x1f		/* Mask for incoming data or error */
11902ac6454SAndrew Thompson 
120760bc48eSAndrew Thompson struct ucom_cfg_task {
121760bc48eSAndrew Thompson 	struct usb_proc_msg hdr;
122760bc48eSAndrew Thompson 	struct ucom_softc *sc;
12302ac6454SAndrew Thompson };
12402ac6454SAndrew Thompson 
125760bc48eSAndrew Thompson struct ucom_param_task {
126760bc48eSAndrew Thompson 	struct usb_proc_msg hdr;
127760bc48eSAndrew Thompson 	struct ucom_softc *sc;
12802ac6454SAndrew Thompson 	struct termios termios_copy;
12902ac6454SAndrew Thompson };
13002ac6454SAndrew Thompson 
131760bc48eSAndrew Thompson struct ucom_super_softc {
132760bc48eSAndrew Thompson 	struct usb_process sc_tq;
1333a4a1a7fSHans Petter Selasky 	int sc_unit;
1343a4a1a7fSHans Petter Selasky 	int sc_subunits;
1358f42c748SHans Petter Selasky 	int sc_refs;
136c01fc06eSHans Petter Selasky 	int sc_flag;	/* see UCOM_FLAG_XXX */
13796d87d2bSHans Petter Selasky 	struct sysctl_oid *sc_sysctl_ttyname;
1383a4a1a7fSHans Petter Selasky 	struct sysctl_oid *sc_sysctl_ttyports;
13996d87d2bSHans Petter Selasky 	char sc_ttyname[16];
14002ac6454SAndrew Thompson };
14102ac6454SAndrew Thompson 
142760bc48eSAndrew Thompson struct ucom_softc {
14302ac6454SAndrew Thompson 	/*
144a164074fSEitan Adler 	 * NOTE: To avoid losing level change information we use two
14502ac6454SAndrew Thompson 	 * tasks instead of one for all commands.
14602ac6454SAndrew Thompson 	 *
14702ac6454SAndrew Thompson 	 * Level changes are transitions like:
14802ac6454SAndrew Thompson 	 *
14902ac6454SAndrew Thompson 	 * ON->OFF
15002ac6454SAndrew Thompson 	 * OFF->ON
15102ac6454SAndrew Thompson 	 * OPEN->CLOSE
15202ac6454SAndrew Thompson 	 * CLOSE->OPEN
15302ac6454SAndrew Thompson 	 */
154760bc48eSAndrew Thompson 	struct ucom_cfg_task	sc_start_task[2];
155760bc48eSAndrew Thompson 	struct ucom_cfg_task	sc_open_task[2];
156760bc48eSAndrew Thompson 	struct ucom_cfg_task	sc_close_task[2];
157760bc48eSAndrew Thompson 	struct ucom_cfg_task	sc_line_state_task[2];
158760bc48eSAndrew Thompson 	struct ucom_cfg_task	sc_status_task[2];
159760bc48eSAndrew Thompson 	struct ucom_param_task	sc_param_task[2];
160bb057644SHans Petter Selasky 	/* pulse capturing support, PPS */
161bb057644SHans Petter Selasky 	struct pps_state	sc_pps;
162afa07055SAndrew Thompson 	/* Used to set "UCOM_FLAG_GP_DATA" flag: */
163760bc48eSAndrew Thompson 	struct usb_proc_msg	*sc_last_start_xfer;
164760bc48eSAndrew Thompson 	const struct ucom_callback *sc_callback;
165760bc48eSAndrew Thompson 	struct ucom_super_softc *sc_super;
16602ac6454SAndrew Thompson 	struct tty *sc_tty;
1677762e8a1SEdward Tomasz Napierala 	struct consdev *sc_consdev;
16802ac6454SAndrew Thompson 	struct mtx *sc_mtx;
16902ac6454SAndrew Thompson 	void   *sc_parent;
1706d917491SHans Petter Selasky 	int sc_subunit;
171d30d96eaSHans Petter Selasky 	uint16_t sc_jitterbuf_in;
172d30d96eaSHans Petter Selasky 	uint16_t sc_jitterbuf_out;
17302ac6454SAndrew Thompson 	uint16_t sc_portno;
174655dc9d0SAndrew Thompson 	uint16_t sc_flag;
17502ac6454SAndrew Thompson #define	UCOM_FLAG_RTS_IFLOW	0x01	/* use RTS input flow control */
17602ac6454SAndrew Thompson #define	UCOM_FLAG_GONE		0x02	/* the device is gone */
17702ac6454SAndrew Thompson #define	UCOM_FLAG_ATTACHED	0x04	/* set if attached */
17802ac6454SAndrew Thompson #define	UCOM_FLAG_GP_DATA	0x08	/* set if get and put data is possible */
17902ac6454SAndrew Thompson #define	UCOM_FLAG_LL_READY	0x20	/* set if low layer is ready */
18002ac6454SAndrew Thompson #define	UCOM_FLAG_HL_READY	0x40	/* set if high layer is ready */
181655dc9d0SAndrew Thompson #define	UCOM_FLAG_CONSOLE	0x80	/* set if device is a console */
182c01fc06eSHans Petter Selasky #define	UCOM_FLAG_WAIT_REFS   0x0100	/* set if we must wait for refs */
183c01fc06eSHans Petter Selasky #define	UCOM_FLAG_FREE_UNIT   0x0200	/* set if we must free the unit */
1843d6709a5SHans Petter Selasky #define	UCOM_FLAG_INWAKEUP    0x0400	/* set if we are in the tsw_inwakeup callback */
1859ad221a5SIan Lepore #define	UCOM_FLAG_LSRTXIDLE   0x0800	/* set if sc_lsr bits ULSR_TSRE+TXRDY work */
1867762e8a1SEdward Tomasz Napierala #define	UCOM_FLAG_DEVICE_MODE 0x1000	/* set if we're an USB device, not a host */
18702ac6454SAndrew Thompson 	uint8_t	sc_lsr;
18802ac6454SAndrew Thompson 	uint8_t	sc_msr;
18902ac6454SAndrew Thompson 	uint8_t	sc_mcr;
19002ac6454SAndrew Thompson 	/* programmed line state bits */
19102ac6454SAndrew Thompson 	uint8_t sc_pls_set;		/* set bits */
19202ac6454SAndrew Thompson 	uint8_t sc_pls_clr;		/* cleared bits */
19302ac6454SAndrew Thompson 	uint8_t sc_pls_curr;		/* last state */
19402ac6454SAndrew Thompson #define	UCOM_LS_DTR	0x01
19502ac6454SAndrew Thompson #define	UCOM_LS_RTS	0x02
19602ac6454SAndrew Thompson #define	UCOM_LS_BREAK	0x04
197655dc9d0SAndrew Thompson #define	UCOM_LS_RING	0x08
198d30d96eaSHans Petter Selasky 	uint8_t sc_jitterbuf[UCOM_JITTERBUF_SIZE];
19902ac6454SAndrew Thompson };
20002ac6454SAndrew Thompson 
2010eb8d462SHans Petter Selasky #define	UCOM_MTX_ASSERT(sc, what) USB_MTX_ASSERT((sc)->sc_mtx, what)
2020eb8d462SHans Petter Selasky #define	UCOM_MTX_LOCK(sc) USB_MTX_LOCK((sc)->sc_mtx)
2030eb8d462SHans Petter Selasky #define	UCOM_MTX_UNLOCK(sc) USB_MTX_UNLOCK((sc)->sc_mtx)
2048f42c748SHans Petter Selasky #define	UCOM_UNLOAD_DRAIN(x) \
205d9b6ab3aSHans Petter Selasky SYSUNINIT(var, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_drain_all, 0)
2068f42c748SHans Petter Selasky 
207a593f6b8SAndrew Thompson #define	ucom_cfg_do_request(udev,com,req,ptr,flags,timo) \
208a593f6b8SAndrew Thompson     usbd_do_request_proc(udev,&(com)->sc_super->sc_tq,req,ptr,flags,NULL,timo)
20902ac6454SAndrew Thompson 
210a593f6b8SAndrew Thompson int	ucom_attach(struct ucom_super_softc *,
2116d917491SHans Petter Selasky 	    struct ucom_softc *, int, void *,
212760bc48eSAndrew Thompson 	    const struct ucom_callback *callback, struct mtx *);
213015bb88fSNick Hibma void	ucom_detach(struct ucom_super_softc *, struct ucom_softc *);
2146416c259SNick Hibma void	ucom_set_pnpinfo_usb(struct ucom_super_softc *, device_t);
2157762e8a1SEdward Tomasz Napierala void	ucom_set_usb_mode(struct ucom_super_softc *, enum usb_hc_mode);
216a593f6b8SAndrew Thompson void	ucom_status_change(struct ucom_softc *);
217a593f6b8SAndrew Thompson uint8_t	ucom_get_data(struct ucom_softc *, struct usb_page_cache *,
21802ac6454SAndrew Thompson 	    uint32_t, uint32_t, uint32_t *);
219a593f6b8SAndrew Thompson void	ucom_put_data(struct ucom_softc *, struct usb_page_cache *,
22002ac6454SAndrew Thompson 	    uint32_t, uint32_t);
221a593f6b8SAndrew Thompson uint8_t	ucom_cfg_is_gone(struct ucom_softc *);
2228f42c748SHans Petter Selasky void	ucom_drain(struct ucom_super_softc *);
2238f42c748SHans Petter Selasky void	ucom_drain_all(void *);
2248f42c748SHans Petter Selasky void	ucom_ref(struct ucom_super_softc *);
2258f42c748SHans Petter Selasky int	ucom_unref(struct ucom_super_softc *);
2269ad221a5SIan Lepore 
2279ad221a5SIan Lepore static inline void
ucom_use_lsr_txbits(struct ucom_softc * sc)2289ad221a5SIan Lepore ucom_use_lsr_txbits(struct ucom_softc *sc)
2299ad221a5SIan Lepore {
2309ad221a5SIan Lepore 
2319ad221a5SIan Lepore 	sc->sc_flag |= UCOM_FLAG_LSRTXIDLE;
2329ad221a5SIan Lepore }
2339ad221a5SIan Lepore 
23475973647SAndrew Thompson #endif					/* _USB_SERIAL_H_ */
235