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