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