xref: /freebsd/sys/dev/uart/uart_tty.c (revision 7dfd9569a2f0637fb9a48157b1c1bfe5709faee3)
1 /*-
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/cons.h>
35 #include <sys/fcntl.h>
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/reboot.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <sys/termios.h>
43 #include <sys/tty.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
46 
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_cpu.h>
50 
51 #include "uart_if.h"
52 
53 static cn_probe_t uart_cnprobe;
54 static cn_init_t uart_cninit;
55 static cn_term_t uart_cnterm;
56 static cn_getc_t uart_cngetc;
57 static cn_checkc_t uart_cncheckc;
58 static cn_putc_t uart_cnputc;
59 
60 CONS_DRIVER(uart, uart_cnprobe, uart_cninit, uart_cnterm, uart_cngetc,
61     uart_cncheckc, uart_cnputc, NULL);
62 
63 static struct uart_devinfo uart_console;
64 
65 static void
66 uart_cnprobe(struct consdev *cp)
67 {
68 
69 	cp->cn_pri = CN_DEAD;
70 
71 	KASSERT(uart_console.cookie == NULL, ("foo"));
72 
73 	if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
74 		return;
75 
76 	if (uart_probe(&uart_console))
77 		return;
78 
79 	strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
80 	cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
81 	cp->cn_arg = &uart_console;
82 }
83 
84 static void
85 uart_cninit(struct consdev *cp)
86 {
87 	struct uart_devinfo *di;
88 
89 	/*
90 	 * Yedi trick: we need to be able to define cn_dev before we go
91 	 * single- or multi-user. The problem is that we don't know at
92 	 * this time what the device will be. Hence, we need to link from
93 	 * the uart_devinfo to the consdev that corresponds to it so that
94 	 * we can define cn_dev in uart_bus_attach() when we find the
95 	 * device during bus enumeration. That's when we'll know what the
96 	 * the unit number will be.
97 	 */
98 	di = cp->cn_arg;
99 	KASSERT(di->cookie == NULL, ("foo"));
100 	di->cookie = cp;
101 	di->type = UART_DEV_CONSOLE;
102 	uart_add_sysdev(di);
103 	uart_init(di);
104 }
105 
106 static void
107 uart_cnterm(struct consdev *cp)
108 {
109 
110 	uart_term(cp->cn_arg);
111 }
112 
113 static void
114 uart_cnputc(struct consdev *cp, int c)
115 {
116 
117 	uart_putc(cp->cn_arg, c);
118 }
119 
120 static int
121 uart_cncheckc(struct consdev *cp)
122 {
123 
124 	return (uart_poll(cp->cn_arg));
125 }
126 
127 static int
128 uart_cngetc(struct consdev *cp)
129 {
130 
131 	return (uart_getc(cp->cn_arg));
132 }
133 
134 static int
135 uart_tty_open(struct tty *tp, struct cdev *dev)
136 {
137 	struct uart_softc *sc;
138 
139 	sc = tp->t_sc;
140 
141 	if (sc == NULL || sc->sc_leaving)
142 		return (ENXIO);
143 
144 	sc->sc_opened = 1;
145 	return (0);
146 }
147 
148 static void
149 uart_tty_close(struct tty *tp)
150 {
151 	struct uart_softc *sc;
152 
153 	sc = tp->t_sc;
154 	if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
155 		return;
156 
157 	if (sc->sc_hwiflow)
158 		UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
159 	if (sc->sc_hwoflow)
160 		UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
161 	if (sc->sc_sysdev == NULL)
162 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
163 
164 	wakeup(sc);
165 	sc->sc_opened = 0;
166 	return;
167 }
168 
169 static void
170 uart_tty_oproc(struct tty *tp)
171 {
172 	struct uart_softc *sc;
173 
174 	sc = tp->t_sc;
175 	if (sc == NULL || sc->sc_leaving)
176 		return;
177 
178 	/*
179 	 * Handle input flow control. Note that if we have hardware support,
180 	 * we don't do anything here. We continue to receive until our buffer
181 	 * is full. At that time we cannot empty the UART itself and it will
182 	 * de-assert RTS for us. In that situation we're completely stuffed.
183 	 * Without hardware support, we need to toggle RTS ourselves.
184 	 */
185 	if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) {
186 		if ((tp->t_state & TS_TBLOCK) &&
187 		    (sc->sc_hwsig & SER_RTS))
188 			UART_SETSIG(sc, SER_DRTS);
189 		else if (!(tp->t_state & TS_TBLOCK) &&
190 		    !(sc->sc_hwsig & SER_RTS))
191 			UART_SETSIG(sc, SER_DRTS|SER_RTS);
192 	}
193 
194 	if (tp->t_state & TS_TTSTOP)
195 		return;
196 
197 	if ((tp->t_state & TS_BUSY) || sc->sc_txbusy)
198 		return;
199 
200 	if (tp->t_outq.c_cc == 0) {
201 		ttwwakeup(tp);
202 		return;
203 	}
204 
205 	sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz);
206 	tp->t_state |= TS_BUSY;
207 	UART_TRANSMIT(sc);
208 	ttwwakeup(tp);
209 }
210 
211 static int
212 uart_tty_param(struct tty *tp, struct termios *t)
213 {
214 	struct uart_softc *sc;
215 	int databits, parity, stopbits;
216 
217 	sc = tp->t_sc;
218 	if (sc == NULL || sc->sc_leaving)
219 		return (ENODEV);
220 	if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
221 		return (EINVAL);
222 	/* Fixate certain parameters for system devices. */
223 	if (sc->sc_sysdev != NULL) {
224 		t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
225 		t->c_cflag |= CLOCAL;
226 		t->c_cflag &= ~HUPCL;
227 	}
228 	if (t->c_ospeed == 0) {
229 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
230 		return (0);
231 	}
232 	switch (t->c_cflag & CSIZE) {
233 	case CS5:	databits = 5; break;
234 	case CS6:	databits = 6; break;
235 	case CS7:	databits = 7; break;
236 	default:	databits = 8; break;
237 	}
238 	stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
239 	if (t->c_cflag & PARENB)
240 		parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
241 		    : UART_PARITY_EVEN;
242 	else
243 		parity = UART_PARITY_NONE;
244 	if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
245 		return (EINVAL);
246 	UART_SETSIG(sc, SER_DDTR | SER_DTR);
247 	/* Set input flow control state. */
248 	if (!sc->sc_hwiflow) {
249 		if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK))
250 			UART_SETSIG(sc, SER_DRTS);
251 		else
252 			UART_SETSIG(sc, SER_DRTS | SER_RTS);
253 	} else
254 		UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
255 	/* Set output flow control state. */
256 	if (sc->sc_hwoflow)
257 		UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
258 	ttsetwater(tp);
259 	return (0);
260 }
261 
262 static int
263 uart_tty_modem(struct tty *tp, int biton, int bitoff)
264 {
265 	struct uart_softc *sc;
266 
267 	sc = tp->t_sc;
268 	if (biton != 0 || bitoff != 0)
269 		UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton);
270 	return (sc->sc_hwsig);
271 }
272 
273 static void
274 uart_tty_break(struct tty *tp, int state)
275 {
276 	struct uart_softc *sc;
277 
278 	sc = tp->t_sc;
279 	UART_IOCTL(sc, UART_IOCTL_BREAK, state);
280 }
281 
282 static void
283 uart_tty_stop(struct tty *tp, int rw)
284 {
285 	struct uart_softc *sc;
286 
287 	sc = tp->t_sc;
288 	if (sc == NULL || sc->sc_leaving)
289 		return;
290 	if (rw & FWRITE) {
291 		if (sc->sc_txbusy) {
292 			sc->sc_txbusy = 0;
293 			UART_FLUSH(sc, UART_FLUSH_TRANSMITTER);
294 		}
295 		tp->t_state &= ~TS_BUSY;
296 	}
297 	if (rw & FREAD) {
298 		UART_FLUSH(sc, UART_FLUSH_RECEIVER);
299 		sc->sc_rxget = sc->sc_rxput = 0;
300 	}
301 }
302 
303 void
304 uart_tty_intr(void *arg)
305 {
306 	struct uart_softc *sc = arg;
307 	struct tty *tp;
308 	int c, pend, sig, xc;
309 
310 	if (sc->sc_leaving)
311 		return;
312 
313 	pend = atomic_readandclear_32(&sc->sc_ttypend);
314 	if (!(pend & SER_INT_MASK))
315 		return;
316 
317 	tp = sc->sc_u.u_tty.tp;
318 
319 	if (pend & SER_INT_RXREADY) {
320 		while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) {
321 			xc = uart_rx_get(sc);
322 			c = xc & 0xff;
323 			if (xc & UART_STAT_FRAMERR)
324 				c |= TTY_FE;
325 			if (xc & UART_STAT_PARERR)
326 				c |= TTY_PE;
327 			ttyld_rint(tp, c);
328 		}
329 	}
330 
331 	if (pend & SER_INT_BREAK) {
332 		if (tp != NULL && !(tp->t_iflag & IGNBRK))
333 			ttyld_rint(tp, 0);
334 	}
335 
336 	if (pend & SER_INT_SIGCHG) {
337 		sig = pend & SER_INT_SIGMASK;
338 		if (sig & SER_DDCD)
339 			ttyld_modem(tp, sig & SER_DCD);
340 		if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) &&
341 		    !sc->sc_hwoflow) {
342 			if (sig & SER_CTS) {
343 				tp->t_state &= ~TS_TTSTOP;
344 				ttyld_start(tp);
345 			} else
346 				tp->t_state |= TS_TTSTOP;
347 		}
348 	}
349 
350 	if (pend & SER_INT_TXIDLE) {
351 		tp->t_state &= ~TS_BUSY;
352 		ttyld_start(tp);
353 	}
354 }
355 
356 int
357 uart_tty_attach(struct uart_softc *sc)
358 {
359 	struct tty *tp;
360 	int unit;
361 
362 	tp = ttyalloc();
363 	sc->sc_u.u_tty.tp = tp;
364 	tp->t_sc = sc;
365 
366 	unit = device_get_unit(sc->sc_dev);
367 
368 	tp->t_oproc = uart_tty_oproc;
369 	tp->t_param = uart_tty_param;
370 	tp->t_stop = uart_tty_stop;
371 	tp->t_modem = uart_tty_modem;
372 	tp->t_break = uart_tty_break;
373 	tp->t_open = uart_tty_open;
374 	tp->t_close = uart_tty_close;
375 
376 	tp->t_pps = &sc->sc_pps;
377 
378 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
379 		sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
380 		    "ttyu%r", unit);
381 		ttyconsolemode(tp, 0);
382 	}
383 
384 	swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
385 	    INTR_TYPE_TTY, &sc->sc_softih);
386 
387 	ttycreate(tp, TS_CALLOUT, "u%r", unit);
388 
389 	return (0);
390 }
391 
392 int uart_tty_detach(struct uart_softc *sc)
393 {
394 	struct tty *tp;
395 
396 	tp = sc->sc_u.u_tty.tp;
397 	tp->t_pps = NULL;
398 	ttygone(tp);
399 	swi_remove(sc->sc_softih);
400 	ttyfree(tp);
401 
402 	return (0);
403 }
404