1184610Salfred/*	$NetBSD: ucomvar.h,v 1.9 2001/01/23 21:56:17 augustss Exp $	*/
2184610Salfred/*	$FreeBSD$	*/
3184610Salfred
4184610Salfred/*-
5184610Salfred * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
6184610Salfred * All rights reserved.
7184610Salfred *
8184610Salfred * Redistribution and use in source and binary forms, with or without
9184610Salfred * modification, are permitted provided that the following conditions
10184610Salfred * are met:
11184610Salfred * 1. Redistributions of source code must retain the above copyright
12184610Salfred *    notice, this list of conditions and the following disclaimer.
13184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
14184610Salfred *    notice, this list of conditions and the following disclaimer in the
15184610Salfred *    documentation and/or other materials provided with the distribution.
16184610Salfred *
17184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27184610Salfred * SUCH DAMAGE.
28184610Salfred */
29184610Salfred
30184610Salfred/*-
31184610Salfred * Copyright (c) 1999 The NetBSD Foundation, Inc.
32184610Salfred * All rights reserved.
33184610Salfred *
34184610Salfred * This code is derived from software contributed to The NetBSD Foundation
35184610Salfred * by Lennart Augustsson (lennart@augustsson.net) at
36184610Salfred * Carlstedt Research & Technology.
37184610Salfred *
38184610Salfred * Redistribution and use in source and binary forms, with or without
39184610Salfred * modification, are permitted provided that the following conditions
40184610Salfred * are met:
41184610Salfred * 1. Redistributions of source code must retain the above copyright
42184610Salfred *    notice, this list of conditions and the following disclaimer.
43184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
44184610Salfred *    notice, this list of conditions and the following disclaimer in the
45184610Salfred *    documentation and/or other materials provided with the distribution.
46184610Salfred * 3. All advertising materials mentioning features or use of this software
47184610Salfred *    must display the following acknowledgement:
48184610Salfred *        This product includes software developed by the NetBSD
49184610Salfred *        Foundation, Inc. and its contributors.
50184610Salfred * 4. Neither the name of The NetBSD Foundation nor the names of its
51184610Salfred *    contributors may be used to endorse or promote products derived
52184610Salfred *    from this software without specific prior written permission.
53184610Salfred *
54184610Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55184610Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56184610Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57184610Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64184610Salfred * POSSIBILITY OF SUCH DAMAGE.
65184610Salfred */
66184610Salfred
67194230Sthompsa#ifndef _USB_SERIAL_H_
68194230Sthompsa#define	_USB_SERIAL_H_
69184610Salfred
70184610Salfred#include <sys/tty.h>
71184610Salfred#include <sys/serial.h>
72184610Salfred#include <sys/fcntl.h>
73230204Shselasky#include <sys/sysctl.h>
74184610Salfred
75184610Salfred/* Module interface related macros */
76184610Salfred#define	UCOM_MODVER	1
77184610Salfred
78184610Salfred#define	UCOM_MINVER	1
79184610Salfred#define	UCOM_PREFVER	UCOM_MODVER
80184610Salfred#define	UCOM_MAXVER	1
81242619Shselasky#define	UCOM_JITTERBUF_SIZE	128	/* bytes */
82184610Salfred
83192984Sthompsastruct usb_device;
84192984Sthompsastruct ucom_softc;
85192984Sthompsastruct usb_device_request;
86184610Salfredstruct thread;
87184610Salfred
88188413Sthompsa/*
89194228Sthompsa * NOTE: There is no guarantee that "ucom_cfg_close()" will
90194228Sthompsa * be called after "ucom_cfg_open()" if the device is detached
91184610Salfred * while it is open!
92184610Salfred */
93192984Sthompsastruct ucom_callback {
94194228Sthompsa	void    (*ucom_cfg_get_status) (struct ucom_softc *, uint8_t *plsr, uint8_t *pmsr);
95194228Sthompsa	void    (*ucom_cfg_set_dtr) (struct ucom_softc *, uint8_t);
96194228Sthompsa	void    (*ucom_cfg_set_rts) (struct ucom_softc *, uint8_t);
97194228Sthompsa	void    (*ucom_cfg_set_break) (struct ucom_softc *, uint8_t);
98197570Sthompsa	void    (*ucom_cfg_set_ring) (struct ucom_softc *, uint8_t);
99194228Sthompsa	void    (*ucom_cfg_param) (struct ucom_softc *, struct termios *);
100194228Sthompsa	void    (*ucom_cfg_open) (struct ucom_softc *);
101194228Sthompsa	void    (*ucom_cfg_close) (struct ucom_softc *);
102194228Sthompsa	int     (*ucom_pre_open) (struct ucom_softc *);
103194228Sthompsa	int     (*ucom_pre_param) (struct ucom_softc *, struct termios *);
104194228Sthompsa	int     (*ucom_ioctl) (struct ucom_softc *, uint32_t, caddr_t, int, struct thread *);
105194228Sthompsa	void    (*ucom_start_read) (struct ucom_softc *);
106194228Sthompsa	void    (*ucom_stop_read) (struct ucom_softc *);
107194228Sthompsa	void    (*ucom_start_write) (struct ucom_softc *);
108194228Sthompsa	void    (*ucom_stop_write) (struct ucom_softc *);
109214761Sn_hibma	void    (*ucom_tty_name) (struct ucom_softc *, char *pbuf, uint16_t buflen, uint16_t unit, uint16_t subunit);
110197570Sthompsa	void    (*ucom_poll) (struct ucom_softc *);
111239179Shselasky	void	(*ucom_free) (struct ucom_softc *);
112184610Salfred};
113184610Salfred
114184610Salfred/* Line status register */
115184610Salfred#define	ULSR_RCV_FIFO	0x80
116184610Salfred#define	ULSR_TSRE	0x40		/* Transmitter empty: byte sent */
117184610Salfred#define	ULSR_TXRDY	0x20		/* Transmitter buffer empty */
118184610Salfred#define	ULSR_BI		0x10		/* Break detected */
119184610Salfred#define	ULSR_FE		0x08		/* Framing error: bad stop bit */
120184610Salfred#define	ULSR_PE		0x04		/* Parity error */
121184610Salfred#define	ULSR_OE		0x02		/* Overrun, lost incoming byte */
122184610Salfred#define	ULSR_RXRDY	0x01		/* Byte ready in Receive Buffer */
123184610Salfred#define	ULSR_RCV_MASK	0x1f		/* Mask for incoming data or error */
124184610Salfred
125192984Sthompsastruct ucom_cfg_task {
126192984Sthompsa	struct usb_proc_msg hdr;
127192984Sthompsa	struct ucom_softc *sc;
128187176Sthompsa};
129187176Sthompsa
130192984Sthompsastruct ucom_param_task {
131192984Sthompsa	struct usb_proc_msg hdr;
132192984Sthompsa	struct ucom_softc *sc;
133188413Sthompsa	struct termios termios_copy;
134187176Sthompsa};
135187176Sthompsa
136192984Sthompsastruct ucom_super_softc {
137192984Sthompsa	struct usb_process sc_tq;
138230204Shselasky	int sc_unit;
139230204Shselasky	int sc_subunits;
140239179Shselasky	int sc_refs;
141239299Shselasky	int sc_flag;	/* see UCOM_FLAG_XXX */
142230209Shselasky	struct sysctl_oid *sc_sysctl_ttyname;
143230204Shselasky	struct sysctl_oid *sc_sysctl_ttyports;
144230209Shselasky	char sc_ttyname[16];
145184610Salfred};
146184610Salfred
147192984Sthompsastruct ucom_softc {
148188413Sthompsa	/*
149250576Seadler	 * NOTE: To avoid losing level change information we use two
150188413Sthompsa	 * tasks instead of one for all commands.
151188413Sthompsa	 *
152188413Sthompsa	 * Level changes are transitions like:
153188413Sthompsa	 *
154188413Sthompsa	 * ON->OFF
155188413Sthompsa	 * OFF->ON
156188413Sthompsa	 * OPEN->CLOSE
157188413Sthompsa	 * CLOSE->OPEN
158188413Sthompsa	 */
159192984Sthompsa	struct ucom_cfg_task	sc_start_task[2];
160192984Sthompsa	struct ucom_cfg_task	sc_open_task[2];
161192984Sthompsa	struct ucom_cfg_task	sc_close_task[2];
162192984Sthompsa	struct ucom_cfg_task	sc_line_state_task[2];
163192984Sthompsa	struct ucom_cfg_task	sc_status_task[2];
164192984Sthompsa	struct ucom_param_task	sc_param_task[2];
165190742Sthompsa	/* Used to set "UCOM_FLAG_GP_DATA" flag: */
166192984Sthompsa	struct usb_proc_msg	*sc_last_start_xfer;
167192984Sthompsa	const struct ucom_callback *sc_callback;
168192984Sthompsa	struct ucom_super_softc *sc_super;
169184610Salfred	struct tty *sc_tty;
170188413Sthompsa	struct mtx *sc_mtx;
171184610Salfred	void   *sc_parent;
172233774Shselasky	int sc_subunit;
173242619Shselasky	uint16_t sc_jitterbuf_in;
174242619Shselasky	uint16_t sc_jitterbuf_out;
175184610Salfred	uint16_t sc_portno;
176197570Sthompsa	uint16_t sc_flag;
177184610Salfred#define	UCOM_FLAG_RTS_IFLOW	0x01	/* use RTS input flow control */
178184610Salfred#define	UCOM_FLAG_GONE		0x02	/* the device is gone */
179184610Salfred#define	UCOM_FLAG_ATTACHED	0x04	/* set if attached */
180184610Salfred#define	UCOM_FLAG_GP_DATA	0x08	/* set if get and put data is possible */
181184610Salfred#define	UCOM_FLAG_LL_READY	0x20	/* set if low layer is ready */
182184610Salfred#define	UCOM_FLAG_HL_READY	0x40	/* set if high layer is ready */
183197570Sthompsa#define	UCOM_FLAG_CONSOLE	0x80	/* set if device is a console */
184239299Shselasky#define	UCOM_FLAG_WAIT_REFS   0x0100	/* set if we must wait for refs */
185239299Shselasky#define	UCOM_FLAG_FREE_UNIT   0x0200	/* set if we must free the unit */
186244489Shselasky#define	UCOM_FLAG_INWAKEUP    0x0400	/* set if we are in the tsw_inwakeup callback */
187184610Salfred	uint8_t	sc_lsr;
188184610Salfred	uint8_t	sc_msr;
189184610Salfred	uint8_t	sc_mcr;
190188413Sthompsa	/* programmed line state bits */
191188413Sthompsa	uint8_t sc_pls_set;		/* set bits */
192188413Sthompsa	uint8_t sc_pls_clr;		/* cleared bits */
193188413Sthompsa	uint8_t sc_pls_curr;		/* last state */
194188413Sthompsa#define	UCOM_LS_DTR	0x01
195188413Sthompsa#define	UCOM_LS_RTS	0x02
196188413Sthompsa#define	UCOM_LS_BREAK	0x04
197197570Sthompsa#define	UCOM_LS_RING	0x08
198242619Shselasky	uint8_t sc_jitterbuf[UCOM_JITTERBUF_SIZE];
199184610Salfred};
200184610Salfred
201239179Shselasky#define	UCOM_MTX_ASSERT(sc, what) mtx_assert((sc)->sc_mtx, what)
202239179Shselasky#define	UCOM_MTX_LOCK(sc) mtx_lock((sc)->sc_mtx)
203239179Shselasky#define	UCOM_MTX_UNLOCK(sc) mtx_unlock((sc)->sc_mtx)
204239179Shselasky#define	UCOM_UNLOAD_DRAIN(x) \
205239179ShselaskySYSUNINIT(var, SI_SUB_KLD - 3, SI_ORDER_ANY, ucom_drain_all, 0)
206239179Shselasky
207194228Sthompsa#define	ucom_cfg_do_request(udev,com,req,ptr,flags,timo) \
208194228Sthompsa    usbd_do_request_proc(udev,&(com)->sc_super->sc_tq,req,ptr,flags,NULL,timo)
209188413Sthompsa
210194228Sthompsaint	ucom_attach(struct ucom_super_softc *,
211233774Shselasky	    struct ucom_softc *, int, void *,
212192984Sthompsa	    const struct ucom_callback *callback, struct mtx *);
213214761Sn_hibmavoid	ucom_detach(struct ucom_super_softc *, struct ucom_softc *);
214214843Sn_hibmavoid	ucom_set_pnpinfo_usb(struct ucom_super_softc *, device_t);
215194228Sthompsavoid	ucom_status_change(struct ucom_softc *);
216194228Sthompsauint8_t	ucom_get_data(struct ucom_softc *, struct usb_page_cache *,
217188413Sthompsa	    uint32_t, uint32_t, uint32_t *);
218194228Sthompsavoid	ucom_put_data(struct ucom_softc *, struct usb_page_cache *,
219188413Sthompsa	    uint32_t, uint32_t);
220194228Sthompsauint8_t	ucom_cfg_is_gone(struct ucom_softc *);
221239179Shselaskyvoid	ucom_drain(struct ucom_super_softc *);
222239179Shselaskyvoid	ucom_drain_all(void *);
223239179Shselaskyvoid	ucom_ref(struct ucom_super_softc *);
224239179Shselaskyint	ucom_unref(struct ucom_super_softc *);
225194230Sthompsa#endif					/* _USB_SERIAL_H_ */
226