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