xref: /freebsd/sys/dev/uart/uart_tty.c (revision e796cc77c586c2955b2f3940dbf4991b31e8d289)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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 ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/cons.h>
37 #include <sys/fcntl.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/reboot.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <sys/tty.h>
45 #include <machine/resource.h>
46 #include <machine/stdarg.h>
47 
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_bus.h>
50 #include <dev/uart/uart_cpu.h>
51 
52 #include "uart_if.h"
53 
54 static cn_probe_t uart_cnprobe;
55 static cn_init_t uart_cninit;
56 static cn_term_t uart_cnterm;
57 static cn_getc_t uart_cngetc;
58 static cn_putc_t uart_cnputc;
59 static cn_grab_t uart_cngrab;
60 static cn_ungrab_t uart_cnungrab;
61 
62 static tsw_open_t uart_tty_open;
63 static tsw_close_t uart_tty_close;
64 static tsw_outwakeup_t uart_tty_outwakeup;
65 static tsw_inwakeup_t uart_tty_inwakeup;
66 static tsw_ioctl_t uart_tty_ioctl;
67 static tsw_param_t uart_tty_param;
68 static tsw_modem_t uart_tty_modem;
69 static tsw_free_t uart_tty_free;
70 static tsw_busy_t uart_tty_busy;
71 
72 CONSOLE_DRIVER(uart);
73 
74 static struct uart_devinfo uart_console;
75 
76 static void
77 uart_cnprobe(struct consdev *cp)
78 {
79 
80 	cp->cn_pri = CN_DEAD;
81 
82 	KASSERT(uart_console.cookie == NULL, ("foo"));
83 
84 	if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
85 		return;
86 
87 	if (uart_probe(&uart_console))
88 		return;
89 
90 	strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
91 	cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
92 	cp->cn_arg = &uart_console;
93 }
94 
95 static void
96 uart_cninit(struct consdev *cp)
97 {
98 	struct uart_devinfo *di;
99 
100 	/*
101 	 * Yedi trick: we need to be able to define cn_dev before we go
102 	 * single- or multi-user. The problem is that we don't know at
103 	 * this time what the device will be. Hence, we need to link from
104 	 * the uart_devinfo to the consdev that corresponds to it so that
105 	 * we can define cn_dev in uart_bus_attach() when we find the
106 	 * device during bus enumeration. That's when we'll know what the
107 	 * the unit number will be.
108 	 */
109 	di = cp->cn_arg;
110 	KASSERT(di->cookie == NULL, ("foo"));
111 	di->cookie = cp;
112 	di->type = UART_DEV_CONSOLE;
113 	uart_add_sysdev(di);
114 	uart_init(di);
115 }
116 
117 static void
118 uart_cnterm(struct consdev *cp)
119 {
120 
121 	uart_term(cp->cn_arg);
122 }
123 
124 static void
125 uart_cngrab(struct consdev *cp)
126 {
127 
128 	uart_grab(cp->cn_arg);
129 }
130 
131 static void
132 uart_cnungrab(struct consdev *cp)
133 {
134 
135 	uart_ungrab(cp->cn_arg);
136 }
137 
138 static void
139 uart_cnputc(struct consdev *cp, int c)
140 {
141 
142 	uart_putc(cp->cn_arg, c);
143 }
144 
145 static int
146 uart_cngetc(struct consdev *cp)
147 {
148 
149 	return (uart_poll(cp->cn_arg));
150 }
151 
152 static int
153 uart_tty_open(struct tty *tp)
154 {
155 	struct uart_softc *sc;
156 
157 	sc = tty_softc(tp);
158 
159 	if (sc == NULL || sc->sc_leaving)
160 		return (ENXIO);
161 
162 	sc->sc_opened = 1;
163 	return (0);
164 }
165 
166 static void
167 uart_tty_close(struct tty *tp)
168 {
169 	struct uart_softc *sc;
170 
171 	sc = tty_softc(tp);
172 	if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
173 		return;
174 
175 	if (sc->sc_hwiflow)
176 		UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
177 	if (sc->sc_hwoflow)
178 		UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
179 	if (sc->sc_sysdev == NULL)
180 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
181 
182 	wakeup(sc);
183 	sc->sc_opened = 0;
184 }
185 
186 static void
187 uart_tty_outwakeup(struct tty *tp)
188 {
189 	struct uart_softc *sc;
190 
191 	sc = tty_softc(tp);
192 	if (sc == NULL || sc->sc_leaving)
193 		return;
194 
195 	if (sc->sc_txbusy)
196 		return;
197 
198 	/*
199 	 * Respect RTS/CTS (output) flow control if enabled and not already
200 	 * handled by hardware.
201 	 */
202 	if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow &&
203 	    !(sc->sc_hwsig & SER_CTS))
204 		return;
205 
206 	sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
207 	if (sc->sc_txdatasz != 0)
208 		UART_TRANSMIT(sc);
209 }
210 
211 static void
212 uart_tty_inwakeup(struct tty *tp)
213 {
214 	struct uart_softc *sc;
215 
216 	sc = tty_softc(tp);
217 	if (sc == NULL || sc->sc_leaving)
218 		return;
219 
220 	if (sc->sc_isquelch) {
221 		if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow)
222 			UART_SETSIG(sc, SER_DRTS|SER_RTS);
223 		sc->sc_isquelch = 0;
224 		uart_sched_softih(sc, SER_INT_RXREADY);
225 	}
226 }
227 
228 static int
229 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data,
230     struct thread *td __unused)
231 {
232 	struct uart_softc *sc;
233 
234 	sc = tty_softc(tp);
235 
236 	switch (cmd) {
237 	case TIOCSBRK:
238 		UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
239 		return (0);
240 	case TIOCCBRK:
241 		UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
242 		return (0);
243 	default:
244 		return pps_ioctl(cmd, data, &sc->sc_pps);
245 	}
246 }
247 
248 static int
249 uart_tty_param(struct tty *tp, struct termios *t)
250 {
251 	struct uart_softc *sc;
252 	int databits, parity, stopbits;
253 
254 	sc = tty_softc(tp);
255 	if (sc == NULL || sc->sc_leaving)
256 		return (ENODEV);
257 	if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
258 		return (EINVAL);
259 	if (t->c_ospeed == 0) {
260 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
261 		return (0);
262 	}
263 	switch (t->c_cflag & CSIZE) {
264 	case CS5:	databits = 5; break;
265 	case CS6:	databits = 6; break;
266 	case CS7:	databits = 7; break;
267 	default:	databits = 8; break;
268 	}
269 	stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
270 	if (t->c_cflag & PARENB)
271 		parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD :
272 		    UART_PARITY_EVEN;
273 	else
274 		parity = UART_PARITY_NONE;
275 	if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
276 		return (EINVAL);
277 	UART_SETSIG(sc, SER_DDTR | SER_DTR);
278 	/* Set input flow control state. */
279 	if (!sc->sc_hwiflow) {
280 		if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch)
281 			UART_SETSIG(sc, SER_DRTS);
282 		else
283 			UART_SETSIG(sc, SER_DRTS | SER_RTS);
284 	} else
285 		UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
286 	/* Set output flow control state. */
287 	if (sc->sc_hwoflow)
288 		UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
289 
290 	return (0);
291 }
292 
293 static int
294 uart_tty_modem(struct tty *tp, int biton, int bitoff)
295 {
296 	struct uart_softc *sc;
297 
298 	sc = tty_softc(tp);
299 	if (biton != 0 || bitoff != 0)
300 		UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton);
301 	return (sc->sc_hwsig);
302 }
303 
304 void
305 uart_tty_intr(void *arg)
306 {
307 	struct uart_softc *sc = arg;
308 	struct tty *tp;
309 	int c, err = 0, pend, sig, xc;
310 
311 	if (sc->sc_leaving)
312 		return;
313 
314 	pend = atomic_readandclear_32(&sc->sc_ttypend);
315 	if (!(pend & SER_INT_MASK))
316 		return;
317 
318 	tp = sc->sc_u.u_tty.tp;
319 	tty_lock(tp);
320 
321 	if (pend & SER_INT_RXREADY) {
322 		while (!uart_rx_empty(sc) && !sc->sc_isquelch) {
323 			xc = uart_rx_peek(sc);
324 			c = xc & 0xff;
325 			if (xc & UART_STAT_FRAMERR)
326 				err |= TRE_FRAMING;
327 			if (xc & UART_STAT_OVERRUN)
328 				err |= TRE_OVERRUN;
329 			if (xc & UART_STAT_PARERR)
330 				err |= TRE_PARITY;
331 			if (ttydisc_rint(tp, c, err) != 0) {
332 				sc->sc_isquelch = 1;
333 				if ((tp->t_termios.c_cflag & CRTS_IFLOW) &&
334 				    !sc->sc_hwiflow)
335 					UART_SETSIG(sc, SER_DRTS);
336 			} else
337 				uart_rx_next(sc);
338 		}
339 	}
340 
341 	if (pend & SER_INT_BREAK)
342 		ttydisc_rint(tp, 0, TRE_BREAK);
343 
344 	if (pend & SER_INT_SIGCHG) {
345 		sig = pend & SER_INT_SIGMASK;
346 		if (sig & SER_DDCD)
347 			ttydisc_modem(tp, sig & SER_DCD);
348 		if (sig & SER_DCTS)
349 			uart_tty_outwakeup(tp);
350 	}
351 
352 	if (pend & SER_INT_TXIDLE)
353 		uart_tty_outwakeup(tp);
354 	ttydisc_rint_done(tp);
355 	tty_unlock(tp);
356 }
357 
358 static void
359 uart_tty_free(void *arg __unused)
360 {
361 
362 	/*
363 	 * XXX: uart(4) could reuse the device unit number before it is
364 	 * being freed by the TTY layer. We should use this hook to free
365 	 * the device unit number, but unfortunately newbus does not
366 	 * seem to support such a construct.
367 	 */
368 }
369 
370 static bool
371 uart_tty_busy(struct tty *tp)
372 {
373 	struct uart_softc *sc;
374 
375 	sc = tty_softc(tp);
376 	if (sc == NULL || sc->sc_leaving)
377                 return (FALSE);
378 
379 	return (sc->sc_txbusy);
380 }
381 
382 static struct ttydevsw uart_tty_class = {
383 	.tsw_flags	= TF_INITLOCK|TF_CALLOUT,
384 	.tsw_open	= uart_tty_open,
385 	.tsw_close	= uart_tty_close,
386 	.tsw_outwakeup	= uart_tty_outwakeup,
387 	.tsw_inwakeup	= uart_tty_inwakeup,
388 	.tsw_ioctl	= uart_tty_ioctl,
389 	.tsw_param	= uart_tty_param,
390 	.tsw_modem	= uart_tty_modem,
391 	.tsw_free	= uart_tty_free,
392 	.tsw_busy	= uart_tty_busy,
393 };
394 
395 int
396 uart_tty_attach(struct uart_softc *sc)
397 {
398 	struct tty *tp;
399 	int unit;
400 
401 	sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc);
402 
403 	unit = device_get_unit(sc->sc_dev);
404 
405 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
406 		sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
407 		    "ttyu%r", unit);
408 		tty_init_console(tp, sc->sc_sysdev->baudrate);
409 	}
410 
411 	swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
412 	    INTR_TYPE_TTY, &sc->sc_softih);
413 
414 	tty_makedev(tp, NULL, "u%r", unit);
415 
416 	return (0);
417 }
418 
419 int
420 uart_tty_detach(struct uart_softc *sc)
421 {
422 	struct tty *tp;
423 
424 	tp = sc->sc_u.u_tty.tp;
425 
426 	tty_lock(tp);
427 	swi_remove(sc->sc_softih);
428 	tty_rel_gone(tp);
429 
430 	return (0);
431 }
432 
433 struct mtx *
434 uart_tty_getlock(struct uart_softc *sc)
435 {
436 
437 	if (sc->sc_u.u_tty.tp != NULL)
438 		return (tty_getlock(sc->sc_u.u_tty.tp));
439 	else
440 		return (NULL);
441 }
442