1/*	$NetBSD: ucomvar.h,v 1.9 2001/01/23 21:56:17 augustss Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*-
32 * Copyright (c) 1999 The NetBSD Foundation, Inc.
33 * All rights reserved.
34 *
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Lennart Augustsson (lennart@augustsson.net) at
37 * Carlstedt Research & Technology.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 */
60
61#ifndef _USB_SERIAL_H_
62#define	_USB_SERIAL_H_
63
64#include <sys/tty.h>
65#include <sys/serial.h>
66#include <sys/fcntl.h>
67#include <sys/sysctl.h>
68#include <sys/timepps.h>
69
70/* Module interface related macros */
71#define	UCOM_MODVER	1
72
73#define	UCOM_MINVER	1
74#define	UCOM_PREFVER	UCOM_MODVER
75#define	UCOM_MAXVER	1
76#define	UCOM_JITTERBUF_SIZE	128	/* bytes */
77
78struct usb_device;
79struct ucom_softc;
80struct usb_device_request;
81struct thread;
82
83/*
84 * NOTE: There is no guarantee that "ucom_cfg_close()" will
85 * be called after "ucom_cfg_open()" if the device is detached
86 * while it is open!
87 */
88struct ucom_callback {
89	void    (*ucom_cfg_get_status) (struct ucom_softc *, uint8_t *plsr, uint8_t *pmsr);
90	void    (*ucom_cfg_set_dtr) (struct ucom_softc *, uint8_t);
91	void    (*ucom_cfg_set_rts) (struct ucom_softc *, uint8_t);
92	void    (*ucom_cfg_set_break) (struct ucom_softc *, uint8_t);
93	void    (*ucom_cfg_set_ring) (struct ucom_softc *, uint8_t);
94	void    (*ucom_cfg_param) (struct ucom_softc *, struct termios *);
95	void    (*ucom_cfg_open) (struct ucom_softc *);
96	void    (*ucom_cfg_close) (struct ucom_softc *);
97	int     (*ucom_pre_open) (struct ucom_softc *);
98	int     (*ucom_pre_param) (struct ucom_softc *, struct termios *);
99	int     (*ucom_ioctl) (struct ucom_softc *, uint32_t, caddr_t, int, struct thread *);
100	void    (*ucom_start_read) (struct ucom_softc *);
101	void    (*ucom_stop_read) (struct ucom_softc *);
102	void    (*ucom_start_write) (struct ucom_softc *);
103	void    (*ucom_stop_write) (struct ucom_softc *);
104	void    (*ucom_tty_name) (struct ucom_softc *, char *pbuf, uint16_t buflen, uint16_t unit, uint16_t subunit);
105	void    (*ucom_poll) (struct ucom_softc *);
106	void	(*ucom_free) (struct ucom_softc *);
107};
108
109/* Line status register */
110#define	ULSR_RCV_FIFO	0x80
111#define	ULSR_TSRE	0x40		/* Transmitter empty: byte sent */
112#define	ULSR_TXRDY	0x20		/* Transmitter buffer empty */
113#define	ULSR_BI		0x10		/* Break detected */
114#define	ULSR_FE		0x08		/* Framing error: bad stop bit */
115#define	ULSR_PE		0x04		/* Parity error */
116#define	ULSR_OE		0x02		/* Overrun, lost incoming byte */
117#define	ULSR_RXRDY	0x01		/* Byte ready in Receive Buffer */
118#define	ULSR_RCV_MASK	0x1f		/* Mask for incoming data or error */
119
120struct ucom_cfg_task {
121	struct usb_proc_msg hdr;
122	struct ucom_softc *sc;
123};
124
125struct ucom_param_task {
126	struct usb_proc_msg hdr;
127	struct ucom_softc *sc;
128	struct termios termios_copy;
129};
130
131struct ucom_super_softc {
132	struct usb_process sc_tq;
133	int sc_unit;
134	int sc_subunits;
135	int sc_refs;
136	int sc_flag;	/* see UCOM_FLAG_XXX */
137	struct sysctl_oid *sc_sysctl_ttyname;
138	struct sysctl_oid *sc_sysctl_ttyports;
139	char sc_ttyname[16];
140};
141
142struct ucom_softc {
143	/*
144	 * NOTE: To avoid losing level change information we use two
145	 * tasks instead of one for all commands.
146	 *
147	 * Level changes are transitions like:
148	 *
149	 * ON->OFF
150	 * OFF->ON
151	 * OPEN->CLOSE
152	 * CLOSE->OPEN
153	 */
154	struct ucom_cfg_task	sc_start_task[2];
155	struct ucom_cfg_task	sc_open_task[2];
156	struct ucom_cfg_task	sc_close_task[2];
157	struct ucom_cfg_task	sc_line_state_task[2];
158	struct ucom_cfg_task	sc_status_task[2];
159	struct ucom_param_task	sc_param_task[2];
160	/* pulse capturing support, PPS */
161	struct pps_state	sc_pps;
162	/* Used to set "UCOM_FLAG_GP_DATA" flag: */
163	struct usb_proc_msg	*sc_last_start_xfer;
164	const struct ucom_callback *sc_callback;
165	struct ucom_super_softc *sc_super;
166	struct tty *sc_tty;
167	struct consdev *sc_consdev;
168	struct mtx *sc_mtx;
169	void   *sc_parent;
170	int sc_subunit;
171	uint16_t sc_jitterbuf_in;
172	uint16_t sc_jitterbuf_out;
173	uint16_t sc_portno;
174	uint16_t sc_flag;
175#define	UCOM_FLAG_RTS_IFLOW	0x01	/* use RTS input flow control */
176#define	UCOM_FLAG_GONE		0x02	/* the device is gone */
177#define	UCOM_FLAG_ATTACHED	0x04	/* set if attached */
178#define	UCOM_FLAG_GP_DATA	0x08	/* set if get and put data is possible */
179#define	UCOM_FLAG_LL_READY	0x20	/* set if low layer is ready */
180#define	UCOM_FLAG_HL_READY	0x40	/* set if high layer is ready */
181#define	UCOM_FLAG_CONSOLE	0x80	/* set if device is a console */
182#define	UCOM_FLAG_WAIT_REFS   0x0100	/* set if we must wait for refs */
183#define	UCOM_FLAG_FREE_UNIT   0x0200	/* set if we must free the unit */
184#define	UCOM_FLAG_INWAKEUP    0x0400	/* set if we are in the tsw_inwakeup callback */
185#define	UCOM_FLAG_LSRTXIDLE   0x0800	/* set if sc_lsr bits ULSR_TSRE+TXRDY work */
186#define	UCOM_FLAG_DEVICE_MODE 0x1000	/* set if we're an USB device, not a host */
187	uint8_t	sc_lsr;
188	uint8_t	sc_msr;
189	uint8_t	sc_mcr;
190	/* programmed line state bits */
191	uint8_t sc_pls_set;		/* set bits */
192	uint8_t sc_pls_clr;		/* cleared bits */
193	uint8_t sc_pls_curr;		/* last state */
194#define	UCOM_LS_DTR	0x01
195#define	UCOM_LS_RTS	0x02
196#define	UCOM_LS_BREAK	0x04
197#define	UCOM_LS_RING	0x08
198	uint8_t sc_jitterbuf[UCOM_JITTERBUF_SIZE];
199};
200
201#define	UCOM_MTX_ASSERT(sc, what) USB_MTX_ASSERT((sc)->sc_mtx, what)
202#define	UCOM_MTX_LOCK(sc) USB_MTX_LOCK((sc)->sc_mtx)
203#define	UCOM_MTX_UNLOCK(sc) USB_MTX_UNLOCK((sc)->sc_mtx)
204#define	UCOM_UNLOAD_DRAIN(x) \
205SYSUNINIT(var, SI_SUB_KLD - 2, SI_ORDER_ANY, ucom_drain_all, 0)
206
207#define	ucom_cfg_do_request(udev,com,req,ptr,flags,timo) \
208    usbd_do_request_proc(udev,&(com)->sc_super->sc_tq,req,ptr,flags,NULL,timo)
209
210int	ucom_attach(struct ucom_super_softc *,
211	    struct ucom_softc *, int, void *,
212	    const struct ucom_callback *callback, struct mtx *);
213void	ucom_detach(struct ucom_super_softc *, struct ucom_softc *);
214void	ucom_set_pnpinfo_usb(struct ucom_super_softc *, device_t);
215void	ucom_set_usb_mode(struct ucom_super_softc *, enum usb_hc_mode);
216void	ucom_status_change(struct ucom_softc *);
217uint8_t	ucom_get_data(struct ucom_softc *, struct usb_page_cache *,
218	    uint32_t, uint32_t, uint32_t *);
219void	ucom_put_data(struct ucom_softc *, struct usb_page_cache *,
220	    uint32_t, uint32_t);
221uint8_t	ucom_cfg_is_gone(struct ucom_softc *);
222void	ucom_drain(struct ucom_super_softc *);
223void	ucom_drain_all(void *);
224void	ucom_ref(struct ucom_super_softc *);
225int	ucom_unref(struct ucom_super_softc *);
226
227static inline void
228ucom_use_lsr_txbits(struct ucom_softc *sc)
229{
230
231	sc->sc_flag |= UCOM_FLAG_LSRTXIDLE;
232}
233
234#endif					/* _USB_SERIAL_H_ */
235