xref: /freebsd-14.2/sys/dev/uart/uart_tty.c (revision 8595e76a)
1098ca2bdSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
427d5dc18SMarcel Moolenaar  * Copyright (c) 2003 Marcel Moolenaar
527d5dc18SMarcel Moolenaar  * All rights reserved.
627d5dc18SMarcel Moolenaar  *
727d5dc18SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
827d5dc18SMarcel Moolenaar  * modification, are permitted provided that the following conditions
927d5dc18SMarcel Moolenaar  * are met:
1027d5dc18SMarcel Moolenaar  *
1127d5dc18SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1227d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1327d5dc18SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1427d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1527d5dc18SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1627d5dc18SMarcel Moolenaar  *
1727d5dc18SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1827d5dc18SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1927d5dc18SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2027d5dc18SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2127d5dc18SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2227d5dc18SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2327d5dc18SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2427d5dc18SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2527d5dc18SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2627d5dc18SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727d5dc18SMarcel Moolenaar  */
2827d5dc18SMarcel Moolenaar 
2927d5dc18SMarcel Moolenaar #include <sys/cdefs.h>
3027d5dc18SMarcel Moolenaar #include <sys/param.h>
3127d5dc18SMarcel Moolenaar #include <sys/systm.h>
3227d5dc18SMarcel Moolenaar #include <sys/bus.h>
3327d5dc18SMarcel Moolenaar #include <sys/conf.h>
3427d5dc18SMarcel Moolenaar #include <sys/cons.h>
3527d5dc18SMarcel Moolenaar #include <sys/fcntl.h>
3627d5dc18SMarcel Moolenaar #include <sys/interrupt.h>
3727d5dc18SMarcel Moolenaar #include <sys/kernel.h>
3827d5dc18SMarcel Moolenaar #include <sys/malloc.h>
3927d5dc18SMarcel Moolenaar #include <sys/reboot.h>
4027d5dc18SMarcel Moolenaar #include <machine/bus.h>
4127d5dc18SMarcel Moolenaar #include <sys/rman.h>
4227d5dc18SMarcel Moolenaar #include <sys/tty.h>
4327d5dc18SMarcel Moolenaar #include <machine/resource.h>
4427d5dc18SMarcel Moolenaar #include <machine/stdarg.h>
4527d5dc18SMarcel Moolenaar 
4627d5dc18SMarcel Moolenaar #include <dev/uart/uart.h>
4727d5dc18SMarcel Moolenaar #include <dev/uart/uart_bus.h>
4827d5dc18SMarcel Moolenaar #include <dev/uart/uart_cpu.h>
4927d5dc18SMarcel Moolenaar 
5027d5dc18SMarcel Moolenaar #include "uart_if.h"
5127d5dc18SMarcel Moolenaar 
5227d5dc18SMarcel Moolenaar static cn_probe_t uart_cnprobe;
5327d5dc18SMarcel Moolenaar static cn_init_t uart_cninit;
54ec6faf94SAndriy Gapon static cn_init_t uart_cnresume;
5527d5dc18SMarcel Moolenaar static cn_term_t uart_cnterm;
5627d5dc18SMarcel Moolenaar static cn_getc_t uart_cngetc;
5727d5dc18SMarcel Moolenaar static cn_putc_t uart_cnputc;
589976156fSAndriy Gapon static cn_grab_t uart_cngrab;
599976156fSAndriy Gapon static cn_ungrab_t uart_cnungrab;
6027d5dc18SMarcel Moolenaar 
6157169ceaSMarius Strobl static tsw_open_t uart_tty_open;
6257169ceaSMarius Strobl static tsw_close_t uart_tty_close;
6357169ceaSMarius Strobl static tsw_outwakeup_t uart_tty_outwakeup;
6457169ceaSMarius Strobl static tsw_inwakeup_t uart_tty_inwakeup;
6557169ceaSMarius Strobl static tsw_ioctl_t uart_tty_ioctl;
6657169ceaSMarius Strobl static tsw_param_t uart_tty_param;
6757169ceaSMarius Strobl static tsw_modem_t uart_tty_modem;
6857169ceaSMarius Strobl static tsw_free_t uart_tty_free;
6957169ceaSMarius Strobl static tsw_busy_t uart_tty_busy;
7057169ceaSMarius Strobl 
71ec6faf94SAndriy Gapon CONSOLE_DRIVER(
72ec6faf94SAndriy Gapon 	uart,
73ec6faf94SAndriy Gapon 	.cn_resume = uart_cnresume,
74ec6faf94SAndriy Gapon );
7527d5dc18SMarcel Moolenaar 
7627d5dc18SMarcel Moolenaar static struct uart_devinfo uart_console;
7727d5dc18SMarcel Moolenaar 
7805b727feSMitchell Horne /* TTY swi(9) event. Allows all uart soft handlers to share one ithread. */
7905b727feSMitchell Horne static struct intr_event *tty_intr_event;
8005b727feSMitchell Horne 
8127d5dc18SMarcel Moolenaar static void
uart_cnprobe(struct consdev * cp)8227d5dc18SMarcel Moolenaar uart_cnprobe(struct consdev *cp)
8327d5dc18SMarcel Moolenaar {
8427d5dc18SMarcel Moolenaar 
8527d5dc18SMarcel Moolenaar 	cp->cn_pri = CN_DEAD;
8627d5dc18SMarcel Moolenaar 
8727d5dc18SMarcel Moolenaar 	KASSERT(uart_console.cookie == NULL, ("foo"));
8827d5dc18SMarcel Moolenaar 
8927d5dc18SMarcel Moolenaar 	if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
9027d5dc18SMarcel Moolenaar 		return;
9127d5dc18SMarcel Moolenaar 
9227d5dc18SMarcel Moolenaar 	if (uart_probe(&uart_console))
9327d5dc18SMarcel Moolenaar 		return;
9427d5dc18SMarcel Moolenaar 
959f0974f9SMarcel Moolenaar 	strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
9627d5dc18SMarcel Moolenaar 	cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
9727d5dc18SMarcel Moolenaar 	cp->cn_arg = &uart_console;
9827d5dc18SMarcel Moolenaar }
9927d5dc18SMarcel Moolenaar 
10027d5dc18SMarcel Moolenaar static void
uart_cninit(struct consdev * cp)10127d5dc18SMarcel Moolenaar uart_cninit(struct consdev *cp)
10227d5dc18SMarcel Moolenaar {
10327d5dc18SMarcel Moolenaar 	struct uart_devinfo *di;
10427d5dc18SMarcel Moolenaar 
10527d5dc18SMarcel Moolenaar 	/*
10627d5dc18SMarcel Moolenaar 	 * Yedi trick: we need to be able to define cn_dev before we go
10727d5dc18SMarcel Moolenaar 	 * single- or multi-user. The problem is that we don't know at
10827d5dc18SMarcel Moolenaar 	 * this time what the device will be. Hence, we need to link from
10927d5dc18SMarcel Moolenaar 	 * the uart_devinfo to the consdev that corresponds to it so that
11027d5dc18SMarcel Moolenaar 	 * we can define cn_dev in uart_bus_attach() when we find the
11127d5dc18SMarcel Moolenaar 	 * device during bus enumeration. That's when we'll know what the
11227d5dc18SMarcel Moolenaar 	 * the unit number will be.
11327d5dc18SMarcel Moolenaar 	 */
11427d5dc18SMarcel Moolenaar 	di = cp->cn_arg;
11527d5dc18SMarcel Moolenaar 	KASSERT(di->cookie == NULL, ("foo"));
11627d5dc18SMarcel Moolenaar 	di->cookie = cp;
11727d5dc18SMarcel Moolenaar 	di->type = UART_DEV_CONSOLE;
11827d5dc18SMarcel Moolenaar 	uart_add_sysdev(di);
11927d5dc18SMarcel Moolenaar 	uart_init(di);
12027d5dc18SMarcel Moolenaar }
12127d5dc18SMarcel Moolenaar 
12227d5dc18SMarcel Moolenaar static void
uart_cnresume(struct consdev * cp)123ec6faf94SAndriy Gapon uart_cnresume(struct consdev *cp)
124ec6faf94SAndriy Gapon {
125ec6faf94SAndriy Gapon 
126ec6faf94SAndriy Gapon 	uart_init(cp->cn_arg);
127ec6faf94SAndriy Gapon }
128ec6faf94SAndriy Gapon 
129ec6faf94SAndriy Gapon static void
uart_cnterm(struct consdev * cp)13027d5dc18SMarcel Moolenaar uart_cnterm(struct consdev *cp)
13127d5dc18SMarcel Moolenaar {
13227d5dc18SMarcel Moolenaar 
13327d5dc18SMarcel Moolenaar 	uart_term(cp->cn_arg);
13427d5dc18SMarcel Moolenaar }
13527d5dc18SMarcel Moolenaar 
13627d5dc18SMarcel Moolenaar static void
uart_cngrab(struct consdev * cp)1379976156fSAndriy Gapon uart_cngrab(struct consdev *cp)
1389976156fSAndriy Gapon {
139f83ed22cSWarner Losh 
140f83ed22cSWarner Losh 	uart_grab(cp->cn_arg);
1419976156fSAndriy Gapon }
1429976156fSAndriy Gapon 
1439976156fSAndriy Gapon static void
uart_cnungrab(struct consdev * cp)1449976156fSAndriy Gapon uart_cnungrab(struct consdev *cp)
1459976156fSAndriy Gapon {
146f83ed22cSWarner Losh 
147f83ed22cSWarner Losh 	uart_ungrab(cp->cn_arg);
1489976156fSAndriy Gapon }
1499976156fSAndriy Gapon 
1509976156fSAndriy Gapon static void
uart_cnputc(struct consdev * cp,int c)15127d5dc18SMarcel Moolenaar uart_cnputc(struct consdev *cp, int c)
15227d5dc18SMarcel Moolenaar {
15327d5dc18SMarcel Moolenaar 
15427d5dc18SMarcel Moolenaar 	uart_putc(cp->cn_arg, c);
15527d5dc18SMarcel Moolenaar }
15627d5dc18SMarcel Moolenaar 
15727d5dc18SMarcel Moolenaar static int
uart_cngetc(struct consdev * cp)15827d5dc18SMarcel Moolenaar uart_cngetc(struct consdev *cp)
15927d5dc18SMarcel Moolenaar {
16027d5dc18SMarcel Moolenaar 
1617672c959SPoul-Henning Kamp 	return (uart_poll(cp->cn_arg));
16227d5dc18SMarcel Moolenaar }
16327d5dc18SMarcel Moolenaar 
16479a8d927SPoul-Henning Kamp static int
uart_tty_open(struct tty * tp)165bc093719SEd Schouten uart_tty_open(struct tty *tp)
16679a8d927SPoul-Henning Kamp {
16779a8d927SPoul-Henning Kamp 	struct uart_softc *sc;
16879a8d927SPoul-Henning Kamp 
169bc093719SEd Schouten 	sc = tty_softc(tp);
170793bcd17SMarcel Moolenaar 
171793bcd17SMarcel Moolenaar 	if (sc == NULL || sc->sc_leaving)
172793bcd17SMarcel Moolenaar 		return (ENXIO);
173793bcd17SMarcel Moolenaar 
17479a8d927SPoul-Henning Kamp 	sc->sc_opened = 1;
17579a8d927SPoul-Henning Kamp 	return (0);
17679a8d927SPoul-Henning Kamp }
17779a8d927SPoul-Henning Kamp 
17879a8d927SPoul-Henning Kamp static void
uart_tty_close(struct tty * tp)17979a8d927SPoul-Henning Kamp uart_tty_close(struct tty *tp)
18079a8d927SPoul-Henning Kamp {
18179a8d927SPoul-Henning Kamp 	struct uart_softc *sc;
18279a8d927SPoul-Henning Kamp 
183bc093719SEd Schouten 	sc = tty_softc(tp);
184fbbec42fSPoul-Henning Kamp 	if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
18579a8d927SPoul-Henning Kamp 		return;
18679a8d927SPoul-Henning Kamp 
18779a8d927SPoul-Henning Kamp 	if (sc->sc_hwiflow)
18879a8d927SPoul-Henning Kamp 		UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
18979a8d927SPoul-Henning Kamp 	if (sc->sc_hwoflow)
19079a8d927SPoul-Henning Kamp 		UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
19179a8d927SPoul-Henning Kamp 	if (sc->sc_sysdev == NULL)
19279a8d927SPoul-Henning Kamp 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
19379a8d927SPoul-Henning Kamp 
19479a8d927SPoul-Henning Kamp 	wakeup(sc);
19579a8d927SPoul-Henning Kamp 	sc->sc_opened = 0;
19679a8d927SPoul-Henning Kamp }
19779a8d927SPoul-Henning Kamp 
19827d5dc18SMarcel Moolenaar static void
uart_tty_outwakeup(struct tty * tp)199bc093719SEd Schouten uart_tty_outwakeup(struct tty *tp)
20027d5dc18SMarcel Moolenaar {
20127d5dc18SMarcel Moolenaar 	struct uart_softc *sc;
20227d5dc18SMarcel Moolenaar 
203bc093719SEd Schouten 	sc = tty_softc(tp);
20427d5dc18SMarcel Moolenaar 	if (sc == NULL || sc->sc_leaving)
20527d5dc18SMarcel Moolenaar 		return;
20627d5dc18SMarcel Moolenaar 
207bc093719SEd Schouten 	if (sc->sc_txbusy)
20827d5dc18SMarcel Moolenaar 		return;
20927d5dc18SMarcel Moolenaar 
210f1fb9647SMarcel Moolenaar 	/*
211f1fb9647SMarcel Moolenaar 	 * Respect RTS/CTS (output) flow control if enabled and not already
212f1fb9647SMarcel Moolenaar 	 * handled by hardware.
213f1fb9647SMarcel Moolenaar 	 */
214f1fb9647SMarcel Moolenaar 	if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow &&
215f1fb9647SMarcel Moolenaar 	    !(sc->sc_hwsig & SER_CTS))
216f1fb9647SMarcel Moolenaar 		return;
217f1fb9647SMarcel Moolenaar 
218bc093719SEd Schouten 	sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
219bc093719SEd Schouten 	if (sc->sc_txdatasz != 0)
220bc093719SEd Schouten 		UART_TRANSMIT(sc);
22127d5dc18SMarcel Moolenaar }
22227d5dc18SMarcel Moolenaar 
2230acb3c4aSMarcel Moolenaar static void
uart_tty_inwakeup(struct tty * tp)2240acb3c4aSMarcel Moolenaar uart_tty_inwakeup(struct tty *tp)
2250acb3c4aSMarcel Moolenaar {
2260acb3c4aSMarcel Moolenaar 	struct uart_softc *sc;
2270acb3c4aSMarcel Moolenaar 
2280acb3c4aSMarcel Moolenaar 	sc = tty_softc(tp);
2290acb3c4aSMarcel Moolenaar 	if (sc == NULL || sc->sc_leaving)
2300acb3c4aSMarcel Moolenaar 		return;
2310acb3c4aSMarcel Moolenaar 
2320acb3c4aSMarcel Moolenaar 	if (sc->sc_isquelch) {
2330acb3c4aSMarcel Moolenaar 		if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow)
2340acb3c4aSMarcel Moolenaar 			UART_SETSIG(sc, SER_DRTS|SER_RTS);
2350acb3c4aSMarcel Moolenaar 		sc->sc_isquelch = 0;
2360acb3c4aSMarcel Moolenaar 		uart_sched_softih(sc, SER_INT_RXREADY);
2370acb3c4aSMarcel Moolenaar 	}
2380acb3c4aSMarcel Moolenaar }
2390acb3c4aSMarcel Moolenaar 
240bc093719SEd Schouten static int
uart_tty_ioctl(struct tty * tp,u_long cmd,caddr_t data,struct thread * td __unused)24157169ceaSMarius Strobl uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data,
24257169ceaSMarius Strobl     struct thread *td __unused)
243bc093719SEd Schouten {
244bc093719SEd Schouten 	struct uart_softc *sc;
245bc093719SEd Schouten 
246bc093719SEd Schouten 	sc = tty_softc(tp);
247bc093719SEd Schouten 
248bc093719SEd Schouten 	switch (cmd) {
249bc093719SEd Schouten 	case TIOCSBRK:
250bc093719SEd Schouten 		UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
251bc093719SEd Schouten 		return (0);
252bc093719SEd Schouten 	case TIOCCBRK:
253bc093719SEd Schouten 		UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
254bc093719SEd Schouten 		return (0);
255bc093719SEd Schouten 	default:
256bc093719SEd Schouten 		return pps_ioctl(cmd, data, &sc->sc_pps);
257bc093719SEd Schouten 	}
25827d5dc18SMarcel Moolenaar }
25927d5dc18SMarcel Moolenaar 
26027d5dc18SMarcel Moolenaar static int
uart_tty_param(struct tty * tp,struct termios * t)26127d5dc18SMarcel Moolenaar uart_tty_param(struct tty *tp, struct termios *t)
26227d5dc18SMarcel Moolenaar {
26327d5dc18SMarcel Moolenaar 	struct uart_softc *sc;
26427d5dc18SMarcel Moolenaar 	int databits, parity, stopbits;
26527d5dc18SMarcel Moolenaar 
266bc093719SEd Schouten 	sc = tty_softc(tp);
26727d5dc18SMarcel Moolenaar 	if (sc == NULL || sc->sc_leaving)
26827d5dc18SMarcel Moolenaar 		return (ENODEV);
26927d5dc18SMarcel Moolenaar 	if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
27027d5dc18SMarcel Moolenaar 		return (EINVAL);
27127d5dc18SMarcel Moolenaar 	if (t->c_ospeed == 0) {
27228710806SPoul-Henning Kamp 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
27327d5dc18SMarcel Moolenaar 		return (0);
27427d5dc18SMarcel Moolenaar 	}
27527d5dc18SMarcel Moolenaar 	switch (t->c_cflag & CSIZE) {
27627d5dc18SMarcel Moolenaar 	case CS5:	databits = 5; break;
27727d5dc18SMarcel Moolenaar 	case CS6:	databits = 6; break;
27827d5dc18SMarcel Moolenaar 	case CS7:	databits = 7; break;
27927d5dc18SMarcel Moolenaar 	default:	databits = 8; break;
28027d5dc18SMarcel Moolenaar 	}
28127d5dc18SMarcel Moolenaar 	stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
28227d5dc18SMarcel Moolenaar 	if (t->c_cflag & PARENB)
28357169ceaSMarius Strobl 		parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD :
28457169ceaSMarius Strobl 		    UART_PARITY_EVEN;
28527d5dc18SMarcel Moolenaar 	else
28627d5dc18SMarcel Moolenaar 		parity = UART_PARITY_NONE;
287b662bdc2SMarcel Moolenaar 	if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
288b662bdc2SMarcel Moolenaar 		return (EINVAL);
289705aad98SStephen Hurd 	if ((t->c_cflag & CNO_RTSDTR) == 0)
29028710806SPoul-Henning Kamp 		UART_SETSIG(sc, SER_DDTR | SER_DTR);
29127d5dc18SMarcel Moolenaar 	/* Set input flow control state. */
29227d5dc18SMarcel Moolenaar 	if (!sc->sc_hwiflow) {
2930acb3c4aSMarcel Moolenaar 		if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch)
29428710806SPoul-Henning Kamp 			UART_SETSIG(sc, SER_DRTS);
295705aad98SStephen Hurd 		else {
296705aad98SStephen Hurd 			if ((t->c_cflag & CNO_RTSDTR) == 0)
29728710806SPoul-Henning Kamp 				UART_SETSIG(sc, SER_DRTS | SER_RTS);
298705aad98SStephen Hurd 		}
29927d5dc18SMarcel Moolenaar 	} else
30027d5dc18SMarcel Moolenaar 		UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
30127d5dc18SMarcel Moolenaar 	/* Set output flow control state. */
30227d5dc18SMarcel Moolenaar 	if (sc->sc_hwoflow)
30327d5dc18SMarcel Moolenaar 		UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
304bc093719SEd Schouten 
30527d5dc18SMarcel Moolenaar 	return (0);
30627d5dc18SMarcel Moolenaar }
30727d5dc18SMarcel Moolenaar 
30896df2b0bSPoul-Henning Kamp static int
uart_tty_modem(struct tty * tp,int biton,int bitoff)30996df2b0bSPoul-Henning Kamp uart_tty_modem(struct tty *tp, int biton, int bitoff)
31096df2b0bSPoul-Henning Kamp {
31196df2b0bSPoul-Henning Kamp 	struct uart_softc *sc;
31296df2b0bSPoul-Henning Kamp 
313bc093719SEd Schouten 	sc = tty_softc(tp);
31496df2b0bSPoul-Henning Kamp 	if (biton != 0 || bitoff != 0)
31596df2b0bSPoul-Henning Kamp 		UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton);
31696df2b0bSPoul-Henning Kamp 	return (sc->sc_hwsig);
31796df2b0bSPoul-Henning Kamp }
31896df2b0bSPoul-Henning Kamp 
31927d5dc18SMarcel Moolenaar void
uart_tty_intr(void * arg)32027d5dc18SMarcel Moolenaar uart_tty_intr(void *arg)
32127d5dc18SMarcel Moolenaar {
32227d5dc18SMarcel Moolenaar 	struct uart_softc *sc = arg;
32327d5dc18SMarcel Moolenaar 	struct tty *tp;
324bc093719SEd Schouten 	int c, err = 0, pend, sig, xc;
32527d5dc18SMarcel Moolenaar 
32627d5dc18SMarcel Moolenaar 	if (sc->sc_leaving)
32727d5dc18SMarcel Moolenaar 		return;
32827d5dc18SMarcel Moolenaar 
32927d5dc18SMarcel Moolenaar 	pend = atomic_readandclear_32(&sc->sc_ttypend);
3302d511805SMarcel Moolenaar 	if (!(pend & SER_INT_MASK))
33127d5dc18SMarcel Moolenaar 		return;
33227d5dc18SMarcel Moolenaar 
33327d5dc18SMarcel Moolenaar 	tp = sc->sc_u.u_tty.tp;
334bc093719SEd Schouten 	tty_lock(tp);
33527d5dc18SMarcel Moolenaar 
3362d511805SMarcel Moolenaar 	if (pend & SER_INT_RXREADY) {
3370acb3c4aSMarcel Moolenaar 		while (!uart_rx_empty(sc) && !sc->sc_isquelch) {
3380acb3c4aSMarcel Moolenaar 			xc = uart_rx_peek(sc);
33927d5dc18SMarcel Moolenaar 			c = xc & 0xff;
34027d5dc18SMarcel Moolenaar 			if (xc & UART_STAT_FRAMERR)
341bc093719SEd Schouten 				err |= TRE_FRAMING;
342dd5b096fSMarcel Moolenaar 			if (xc & UART_STAT_OVERRUN)
343bc093719SEd Schouten 				err |= TRE_OVERRUN;
34427d5dc18SMarcel Moolenaar 			if (xc & UART_STAT_PARERR)
345bc093719SEd Schouten 				err |= TRE_PARITY;
3460acb3c4aSMarcel Moolenaar 			if (ttydisc_rint(tp, c, err) != 0) {
3470acb3c4aSMarcel Moolenaar 				sc->sc_isquelch = 1;
3480acb3c4aSMarcel Moolenaar 				if ((tp->t_termios.c_cflag & CRTS_IFLOW) &&
3490acb3c4aSMarcel Moolenaar 				    !sc->sc_hwiflow)
3500acb3c4aSMarcel Moolenaar 					UART_SETSIG(sc, SER_DRTS);
3510acb3c4aSMarcel Moolenaar 			} else
3520acb3c4aSMarcel Moolenaar 				uart_rx_next(sc);
35327d5dc18SMarcel Moolenaar 		}
35427d5dc18SMarcel Moolenaar 	}
35527d5dc18SMarcel Moolenaar 
356bc093719SEd Schouten 	if (pend & SER_INT_BREAK)
357bc093719SEd Schouten 		ttydisc_rint(tp, 0, TRE_BREAK);
35827d5dc18SMarcel Moolenaar 
3592d511805SMarcel Moolenaar 	if (pend & SER_INT_SIGCHG) {
3602d511805SMarcel Moolenaar 		sig = pend & SER_INT_SIGMASK;
36128710806SPoul-Henning Kamp 		if (sig & SER_DDCD)
362bc093719SEd Schouten 			ttydisc_modem(tp, sig & SER_DCD);
363f1fb9647SMarcel Moolenaar 		if (sig & SER_DCTS)
364bc093719SEd Schouten 			uart_tty_outwakeup(tp);
36527d5dc18SMarcel Moolenaar 	}
36627d5dc18SMarcel Moolenaar 
367bc093719SEd Schouten 	if (pend & SER_INT_TXIDLE)
368bc093719SEd Schouten 		uart_tty_outwakeup(tp);
369bc093719SEd Schouten 	ttydisc_rint_done(tp);
370bc093719SEd Schouten 	tty_unlock(tp);
37127d5dc18SMarcel Moolenaar }
372bc093719SEd Schouten 
3739b866e4eSEd Schouten static void
uart_tty_free(void * arg __unused)37457169ceaSMarius Strobl uart_tty_free(void *arg __unused)
3759b866e4eSEd Schouten {
3769b866e4eSEd Schouten 
3779b866e4eSEd Schouten 	/*
3789b866e4eSEd Schouten 	 * XXX: uart(4) could reuse the device unit number before it is
3799b866e4eSEd Schouten 	 * being freed by the TTY layer. We should use this hook to free
3809b866e4eSEd Schouten 	 * the device unit number, but unfortunately newbus does not
3819b866e4eSEd Schouten 	 * seem to support such a construct.
3829b866e4eSEd Schouten 	 */
3839b866e4eSEd Schouten }
3849b866e4eSEd Schouten 
3859750d9e5SMarius Strobl static bool
uart_tty_busy(struct tty * tp)3869750d9e5SMarius Strobl uart_tty_busy(struct tty *tp)
3879750d9e5SMarius Strobl {
3889750d9e5SMarius Strobl 	struct uart_softc *sc;
3899750d9e5SMarius Strobl 
3909750d9e5SMarius Strobl 	sc = tty_softc(tp);
3919750d9e5SMarius Strobl 	if (sc == NULL || sc->sc_leaving)
392*8595e76aSMarius Strobl                 return (false);
3939750d9e5SMarius Strobl 
394*8595e76aSMarius Strobl 	/*
395*8595e76aSMarius Strobl 	 * The tty locking is sufficient here; we may lose the race against
396*8595e76aSMarius Strobl 	 * uart_bus_ihand()/uart_intr() clearing sc_txbusy underneath us, in
397*8595e76aSMarius Strobl 	 * which case we will incorrectly but non-fatally report a busy Tx
398*8595e76aSMarius Strobl 	 * path upward. However, tty locking ensures that no additional output
399*8595e76aSMarius Strobl 	 * is enqueued before UART_TXBUSY() returns, which means that there
400*8595e76aSMarius Strobl 	 * are no Tx interrupts to be lost.
401*8595e76aSMarius Strobl 	 */
402*8595e76aSMarius Strobl 	if (sc->sc_txbusy)
403*8595e76aSMarius Strobl 		return (true);
404*8595e76aSMarius Strobl 	return (UART_TXBUSY(sc));
4059750d9e5SMarius Strobl }
4069750d9e5SMarius Strobl 
407bc093719SEd Schouten static struct ttydevsw uart_tty_class = {
408bc093719SEd Schouten 	.tsw_flags	= TF_INITLOCK|TF_CALLOUT,
409bc093719SEd Schouten 	.tsw_open	= uart_tty_open,
410bc093719SEd Schouten 	.tsw_close	= uart_tty_close,
411bc093719SEd Schouten 	.tsw_outwakeup	= uart_tty_outwakeup,
4120acb3c4aSMarcel Moolenaar 	.tsw_inwakeup	= uart_tty_inwakeup,
413bc093719SEd Schouten 	.tsw_ioctl	= uart_tty_ioctl,
414bc093719SEd Schouten 	.tsw_param	= uart_tty_param,
415bc093719SEd Schouten 	.tsw_modem	= uart_tty_modem,
4169b866e4eSEd Schouten 	.tsw_free	= uart_tty_free,
4179750d9e5SMarius Strobl 	.tsw_busy	= uart_tty_busy,
418bc093719SEd Schouten };
41927d5dc18SMarcel Moolenaar 
42027d5dc18SMarcel Moolenaar int
uart_tty_attach(struct uart_softc * sc)42127d5dc18SMarcel Moolenaar uart_tty_attach(struct uart_softc *sc)
42227d5dc18SMarcel Moolenaar {
42327d5dc18SMarcel Moolenaar 	struct tty *tp;
42479a8d927SPoul-Henning Kamp 	int unit;
42527d5dc18SMarcel Moolenaar 
426c5e30cc0SEd Schouten 	sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc);
42727d5dc18SMarcel Moolenaar 
42879a8d927SPoul-Henning Kamp 	unit = device_get_unit(sc->sc_dev);
42927d5dc18SMarcel Moolenaar 
43027d5dc18SMarcel Moolenaar 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
43112d984b6SMarcel Moolenaar 		sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
43279a8d927SPoul-Henning Kamp 		    "ttyu%r", unit);
433f725b213SMarcel Moolenaar 		tty_init_console(tp, sc->sc_sysdev->baudrate);
43427d5dc18SMarcel Moolenaar 	}
43527d5dc18SMarcel Moolenaar 
436e0f66ef8SJohn Baldwin 	swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
43727d5dc18SMarcel Moolenaar 	    INTR_TYPE_TTY, &sc->sc_softih);
43827d5dc18SMarcel Moolenaar 
439bc093719SEd Schouten 	tty_makedev(tp, NULL, "u%r", unit);
44079a8d927SPoul-Henning Kamp 
44127d5dc18SMarcel Moolenaar 	return (0);
44227d5dc18SMarcel Moolenaar }
44327d5dc18SMarcel Moolenaar 
4447d376cbcSAlexander Kabaev int
uart_tty_detach(struct uart_softc * sc)4457d376cbcSAlexander Kabaev uart_tty_detach(struct uart_softc *sc)
44627d5dc18SMarcel Moolenaar {
44779a8d927SPoul-Henning Kamp 	struct tty *tp;
44827d5dc18SMarcel Moolenaar 
44979a8d927SPoul-Henning Kamp 	tp = sc->sc_u.u_tty.tp;
450bc093719SEd Schouten 
451bc093719SEd Schouten 	tty_lock(tp);
452284b6708SJohn Baldwin 	swi_remove(sc->sc_softih);
453bc093719SEd Schouten 	tty_rel_gone(tp);
45427d5dc18SMarcel Moolenaar 
45527d5dc18SMarcel Moolenaar 	return (0);
45627d5dc18SMarcel Moolenaar }
457b59236ceSIan Lepore 
458b59236ceSIan Lepore struct mtx *
uart_tty_getlock(struct uart_softc * sc)459b59236ceSIan Lepore uart_tty_getlock(struct uart_softc *sc)
460b59236ceSIan Lepore {
461b59236ceSIan Lepore 
462b59236ceSIan Lepore 	if (sc->sc_u.u_tty.tp != NULL)
463b59236ceSIan Lepore 		return (tty_getlock(sc->sc_u.u_tty.tp));
464b59236ceSIan Lepore 	else
465b59236ceSIan Lepore 		return (NULL);
466b59236ceSIan Lepore }
467