xref: /freebsd/sys/dev/uart/uart_tty.c (revision c4f02a891fe62fe1277c89859922804ea2c27bcd)
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 #define	UART_MINOR_CALLOUT	0x10000
54 
55 static cn_probe_t uart_cnprobe;
56 static cn_init_t uart_cninit;
57 static cn_term_t uart_cnterm;
58 static cn_getc_t uart_cngetc;
59 static cn_checkc_t uart_cncheckc;
60 static cn_putc_t uart_cnputc;
61 
62 CONS_DRIVER(uart, uart_cnprobe, uart_cninit, uart_cnterm, uart_cngetc,
63     uart_cncheckc, uart_cnputc, NULL);
64 
65 static d_open_t uart_tty_open;
66 static d_close_t uart_tty_close;
67 static d_ioctl_t uart_tty_ioctl;
68 
69 static struct cdevsw uart_cdevsw = {
70 	.d_open = uart_tty_open,
71 	.d_close = uart_tty_close,
72 	.d_read = ttyread,
73 	.d_write = ttywrite,
74 	.d_ioctl = uart_tty_ioctl,
75 	.d_poll = ttypoll,
76 	.d_name = uart_driver_name,
77 	.d_maj = MAJOR_AUTO,
78 	.d_flags = D_TTY,
79 	.d_kqfilter = ttykqfilter,
80 };
81 
82 static struct uart_devinfo uart_console;
83 
84 static void
85 uart_cnprobe(struct consdev *cp)
86 {
87 
88 	cp->cn_pri = CN_DEAD;
89 
90 	KASSERT(uart_console.cookie == NULL, ("foo"));
91 
92 	if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
93 		return;
94 
95 	if (uart_probe(&uart_console))
96 		return;
97 
98 	cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
99 	cp->cn_arg = &uart_console;
100 }
101 
102 static void
103 uart_cninit(struct consdev *cp)
104 {
105 	struct uart_devinfo *di;
106 
107 	/*
108 	 * Yedi trick: we need to be able to define cn_dev before we go
109 	 * single- or multi-user. The problem is that we don't know at
110 	 * this time what the device will be. Hence, we need to link from
111 	 * the uart_devinfo to the consdev that corresponds to it so that
112 	 * we can define cn_dev in uart_bus_attach() when we find the
113 	 * device during bus enumeration. That's when we'll know what the
114 	 * the unit number will be.
115 	 */
116 	di = cp->cn_arg;
117 	KASSERT(di->cookie == NULL, ("foo"));
118 	di->cookie = cp;
119 	di->type = UART_DEV_CONSOLE;
120 	uart_add_sysdev(di);
121 	uart_init(di);
122 }
123 
124 static void
125 uart_cnterm(struct consdev *cp)
126 {
127 
128 	uart_term(cp->cn_arg);
129 }
130 
131 static void
132 uart_cnputc(struct consdev *cp, int c)
133 {
134 
135 	uart_putc(cp->cn_arg, c);
136 }
137 
138 static int
139 uart_cncheckc(struct consdev *cp)
140 {
141 
142 	return (uart_poll(cp->cn_arg));
143 }
144 
145 static int
146 uart_cngetc(struct consdev *cp)
147 {
148 
149 	return (uart_getc(cp->cn_arg));
150 }
151 
152 static void
153 uart_tty_oproc(struct tty *tp)
154 {
155 	struct uart_softc *sc;
156 
157 	KASSERT(tp->t_dev != NULL, ("foo"));
158 	sc = tp->t_dev->si_drv1;
159 	if (sc == NULL || sc->sc_leaving)
160 		return;
161 
162 	/*
163 	 * Handle input flow control. Note that if we have hardware support,
164 	 * we don't do anything here. We continue to receive until our buffer
165 	 * is full. At that time we cannot empty the UART itself and it will
166 	 * de-assert RTS for us. In that situation we're completely stuffed.
167 	 * Without hardware support, we need to toggle RTS ourselves.
168 	 */
169 	if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) {
170 		if ((tp->t_state & TS_TBLOCK) &&
171 		    (sc->sc_hwsig & UART_SIG_RTS))
172 			UART_SETSIG(sc, UART_SIG_DRTS);
173 		else if (!(tp->t_state & TS_TBLOCK) &&
174 		    !(sc->sc_hwsig & UART_SIG_RTS))
175 			UART_SETSIG(sc, UART_SIG_DRTS|UART_SIG_RTS);
176 	}
177 
178 	if (tp->t_state & TS_TTSTOP)
179 		return;
180 
181 	if ((tp->t_state & TS_BUSY) || sc->sc_txbusy)
182 		return;
183 
184 	if (tp->t_outq.c_cc == 0) {
185 		ttwwakeup(tp);
186 		return;
187 	}
188 
189 	sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz);
190 	tp->t_state |= TS_BUSY;
191 	UART_TRANSMIT(sc);
192 	ttwwakeup(tp);
193 }
194 
195 static int
196 uart_tty_param(struct tty *tp, struct termios *t)
197 {
198 	struct uart_softc *sc;
199 	int databits, parity, stopbits;
200 
201 	KASSERT(tp->t_dev != NULL, ("foo"));
202 	sc = tp->t_dev->si_drv1;
203 	if (sc == NULL || sc->sc_leaving)
204 		return (ENODEV);
205 	if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
206 		return (EINVAL);
207 	/* Fixate certain parameters for system devices. */
208 	if (sc->sc_sysdev != NULL) {
209 		t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
210 		t->c_cflag |= CLOCAL;
211 		t->c_cflag &= ~HUPCL;
212 	}
213 	if (t->c_ospeed == 0) {
214 		UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DRTS);
215 		return (0);
216 	}
217 	switch (t->c_cflag & CSIZE) {
218 	case CS5:	databits = 5; break;
219 	case CS6:	databits = 6; break;
220 	case CS7:	databits = 7; break;
221 	default:	databits = 8; break;
222 	}
223 	stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
224 	if (t->c_cflag & PARENB)
225 		parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
226 		    : UART_PARITY_EVEN;
227 	else
228 		parity = UART_PARITY_NONE;
229 	UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity);
230 	UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DTR);
231 	/* Set input flow control state. */
232 	if (!sc->sc_hwiflow) {
233 		if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK))
234 			UART_SETSIG(sc, UART_SIG_DRTS);
235 		else
236 			UART_SETSIG(sc, UART_SIG_DRTS | UART_SIG_RTS);
237 	} else
238 		UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
239 	/* Set output flow control state. */
240 	if (sc->sc_hwoflow)
241 		UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
242 	ttsetwater(tp);
243 	return (0);
244 }
245 
246 static void
247 uart_tty_stop(struct tty *tp, int rw)
248 {
249 	struct uart_softc *sc;
250 
251 	KASSERT(tp->t_dev != NULL, ("foo"));
252 	sc = tp->t_dev->si_drv1;
253 	if (sc == NULL || sc->sc_leaving)
254 		return;
255 	if (rw & FWRITE) {
256 		if (sc->sc_txbusy) {
257 			sc->sc_txbusy = 0;
258 			UART_FLUSH(sc, UART_FLUSH_TRANSMITTER);
259 		}
260 		tp->t_state &= ~TS_BUSY;
261 	}
262 	if (rw & FREAD) {
263 		UART_FLUSH(sc, UART_FLUSH_RECEIVER);
264 		sc->sc_rxget = sc->sc_rxput = 0;
265 	}
266 }
267 
268 void
269 uart_tty_intr(void *arg)
270 {
271 	struct uart_softc *sc = arg;
272 	struct tty *tp;
273 	int c, pend, sig, xc;
274 
275 	if (sc->sc_leaving)
276 		return;
277 
278 	pend = atomic_readandclear_32(&sc->sc_ttypend);
279 	if (!(pend & UART_IPEND_MASK))
280 		return;
281 
282 	tp = sc->sc_u.u_tty.tp;
283 
284 	if (pend & UART_IPEND_RXREADY) {
285 		while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) {
286 			xc = uart_rx_get(sc);
287 			c = xc & 0xff;
288 			if (xc & UART_STAT_FRAMERR)
289 				c |= TTY_FE;
290 			if (xc & UART_STAT_PARERR)
291 				c |= TTY_PE;
292 			(*linesw[tp->t_line].l_rint)(c, tp);
293 		}
294 	}
295 
296 	if (pend & UART_IPEND_BREAK) {
297 		if (tp != NULL && !(tp->t_iflag & IGNBRK))
298 			(*linesw[tp->t_line].l_rint)(0, tp);
299 	}
300 
301 	if (pend & UART_IPEND_SIGCHG) {
302 		sig = pend & UART_IPEND_SIGMASK;
303 		if (sig & UART_SIG_DDCD)
304 			(*linesw[tp->t_line].l_modem)(tp, sig & UART_SIG_DCD);
305 		if ((sig & UART_SIG_DCTS) && (tp->t_cflag & CCTS_OFLOW) &&
306 		    !sc->sc_hwoflow) {
307 			if (sig & UART_SIG_CTS) {
308 				tp->t_state &= ~TS_TTSTOP;
309 				(*linesw[tp->t_line].l_start)(tp);
310 			} else
311 				tp->t_state |= TS_TTSTOP;
312 		}
313 	}
314 
315 	if (pend & UART_IPEND_TXIDLE) {
316 		tp->t_state &= ~TS_BUSY;
317 		(*linesw[tp->t_line].l_start)(tp);
318 	}
319 }
320 
321 int
322 uart_tty_attach(struct uart_softc *sc)
323 {
324 	struct tty *tp;
325 
326 	tp = ttymalloc(NULL);
327 	sc->sc_u.u_tty.tp = tp;
328 
329 	sc->sc_u.u_tty.si[0] = make_dev(&uart_cdevsw,
330 	    device_get_unit(sc->sc_dev), UID_ROOT, GID_WHEEL, 0600, "ttyu%r",
331 	    device_get_unit(sc->sc_dev));
332 	sc->sc_u.u_tty.si[0]->si_drv1 = sc;
333 	sc->sc_u.u_tty.si[0]->si_tty = tp;
334 	sc->sc_u.u_tty.si[1] = make_dev(&uart_cdevsw,
335 	    device_get_unit(sc->sc_dev) | UART_MINOR_CALLOUT, UID_UUCP,
336 	    GID_DIALER, 0660, "uart%r", device_get_unit(sc->sc_dev));
337 	sc->sc_u.u_tty.si[1]->si_drv1 = sc;
338 	sc->sc_u.u_tty.si[1]->si_tty = tp;
339 
340 	tp->t_oproc = uart_tty_oproc;
341 	tp->t_param = uart_tty_param;
342 	tp->t_stop = uart_tty_stop;
343 
344 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
345 		sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
346 		    "ttyu%r", device_get_unit(sc->sc_dev));
347 	}
348 
349 	swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
350 	    INTR_TYPE_TTY, &sc->sc_softih);
351 
352 	return (0);
353 }
354 
355 int uart_tty_detach(struct uart_softc *sc)
356 {
357 
358 	ithread_remove_handler(sc->sc_softih);
359 	destroy_dev(sc->sc_u.u_tty.si[0]);
360 	destroy_dev(sc->sc_u.u_tty.si[1]);
361 	/* ttyfree(sc->sc_u.u_tty.tp); */
362 
363 	return (0);
364 }
365 
366 static int
367 uart_tty_open(dev_t dev, int flags, int mode, struct thread *td)
368 {
369 	struct uart_softc *sc;
370 	struct tty *tp;
371 	int error;
372 
373 	sc = dev->si_drv1;
374 	if (sc == NULL || sc->sc_leaving)
375 		return (ENODEV);
376 
377 	tp = dev->si_tty;
378 
379  loop:
380 	if (sc->sc_opened) {
381 		KASSERT(tp->t_state & TS_ISOPEN, ("foo"));
382 		/*
383 		 * The device is open, so everything has been initialized.
384 		 * Handle conflicts.
385 		 */
386 		if (minor(dev) & UART_MINOR_CALLOUT) {
387 			if (!sc->sc_callout)
388 				return (EBUSY);
389 		} else {
390 			if (sc->sc_callout) {
391 				if (flags & O_NONBLOCK)
392 					return (EBUSY);
393 				error =	tsleep(sc, TTIPRI|PCATCH, "uartbi", 0);
394 				if (error)
395 					return (error);
396 				sc = dev->si_drv1;
397 				if (sc == NULL || sc->sc_leaving)
398 					return (ENODEV);
399 				goto loop;
400 			}
401 		}
402 		if (tp->t_state & TS_XCLUDE && suser(td) != 0)
403 			return (EBUSY);
404 	} else {
405 		KASSERT(!(tp->t_state & TS_ISOPEN), ("foo"));
406 		/*
407 		 * The device isn't open, so there are no conflicts.
408 		 * Initialize it.  Initialization is done twice in many
409 		 * cases: to preempt sleeping callin opens if we are
410 		 * callout, and to complete a callin open after DCD rises.
411 		 */
412 		sc->sc_callout = (minor(dev) & UART_MINOR_CALLOUT) ? 1 : 0;
413 		tp->t_dev = dev;
414 
415 		tp->t_cflag = TTYDEF_CFLAG;
416 		tp->t_iflag = TTYDEF_IFLAG;
417 		tp->t_lflag = TTYDEF_LFLAG;
418 		tp->t_oflag = TTYDEF_OFLAG;
419 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
420 		ttychars(tp);
421 		error = uart_tty_param(tp, &tp->t_termios);
422 		if (error)
423 			return (error);
424 		/*
425 		 * Handle initial DCD.
426 		 */
427 		if ((sc->sc_hwsig & UART_SIG_DCD) || sc->sc_callout)
428 			(*linesw[tp->t_line].l_modem)(tp, 1);
429 	}
430 	/*
431 	 * Wait for DCD if necessary.
432 	 */
433 	if (!(tp->t_state & TS_CARR_ON) && !sc->sc_callout &&
434 	    !(tp->t_cflag & CLOCAL) && !(flags & O_NONBLOCK)) {
435 		error = tsleep(TSA_CARR_ON(tp), TTIPRI|PCATCH, "uartdcd", 0);
436 		if (error)
437 			return (error);
438 		sc = dev->si_drv1;
439 		if (sc == NULL || sc->sc_leaving)
440 			return (ENODEV);
441 		goto loop;
442 	}
443 	error = ttyopen(dev, tp);
444 	if (error)
445 		return (error);
446 	error = (*linesw[tp->t_line].l_open)(dev, tp);
447 	if (error)
448 		return (error);
449 
450 	KASSERT(tp->t_state & TS_ISOPEN, ("foo"));
451 	sc->sc_opened = 1;
452 	return (0);
453 }
454 
455 static int
456 uart_tty_close(dev_t dev, int flags, int mode, struct thread *td)
457 {
458 	struct uart_softc *sc;
459 	struct tty *tp;
460 
461 	sc = dev->si_drv1;
462 	if (sc == NULL || sc->sc_leaving)
463 		return (ENODEV);
464 	tp = dev->si_tty;
465 	if (!sc->sc_opened) {
466 		KASSERT(!(tp->t_state & TS_ISOPEN), ("foo"));
467 		return (0);
468 	}
469 	KASSERT(tp->t_state & TS_ISOPEN, ("foo"));
470 
471 	if (sc->sc_hwiflow)
472 		UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
473 	if (sc->sc_hwoflow)
474 		UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
475 	if (sc->sc_sysdev == NULL)
476 		UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DRTS);
477 
478 	/* Disable pulse capturing. */
479 	sc->sc_pps.ppsparam.mode = 0;
480 
481 	(*linesw[tp->t_line].l_close)(tp, flags);
482 	ttyclose(tp);
483 	wakeup(sc);
484 	wakeup(TSA_CARR_ON(tp));
485 	KASSERT(!(tp->t_state & TS_ISOPEN), ("foo"));
486 	sc->sc_opened = 0;
487 	return (0);
488 }
489 
490 static int
491 uart_tty_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
492     struct thread *td)
493 {
494 	struct uart_softc *sc;
495 	struct tty *tp;
496 	int bits, error, sig;
497 
498 	sc = dev->si_drv1;
499 	if (sc == NULL || sc->sc_leaving)
500 		return (ENODEV);
501 
502 	tp = dev->si_tty;
503 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flags, td);
504 	if (error != ENOIOCTL)
505 		return (error);
506 	error = ttioctl(tp, cmd, data, flags);
507 	if (error != ENOIOCTL)
508 		return (error);
509 
510 	error = 0;
511 	switch (cmd) {
512 	case TIOCSBRK:
513 		UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
514 		break;
515 	case TIOCCBRK:
516 		UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
517 		break;
518 	case TIOCSDTR:
519 		UART_SETSIG(sc, UART_SIG_DDTR | UART_SIG_DTR);
520 		break;
521 	case TIOCCDTR:
522 		UART_SETSIG(sc, UART_SIG_DDTR);
523 		break;
524 	case TIOCMSET:
525 		bits = *(int*)data;
526 		sig = UART_SIG_DDTR | UART_SIG_DRTS;
527 		if (bits & TIOCM_DTR)
528 			sig |= UART_SIG_DTR;
529 		if (bits & TIOCM_RTS)
530 			sig |= UART_SIG_RTS;
531 		UART_SETSIG(sc, sig);
532 		break;
533         case TIOCMBIS:
534 		bits = *(int*)data;
535 		sig = 0;
536 		if (bits & TIOCM_DTR)
537 			sig |= UART_SIG_DDTR | UART_SIG_DTR;
538 		if (bits & TIOCM_RTS)
539 			sig |= UART_SIG_DRTS | UART_SIG_RTS;
540 		UART_SETSIG(sc, sig);
541 		break;
542         case TIOCMBIC:
543 		bits = *(int*)data;
544 		sig = 0;
545 		if (bits & TIOCM_DTR)
546 			sig |= UART_SIG_DDTR;
547 		if (bits & TIOCM_RTS)
548 			sig |= UART_SIG_DRTS;
549 		UART_SETSIG(sc, sig);
550 		break;
551         case TIOCMGET:
552 		sig = sc->sc_hwsig;
553 		bits = TIOCM_LE;
554 		if (sig & UART_SIG_DTR)
555 			bits |= TIOCM_DTR;
556 		if (sig & UART_SIG_RTS)
557 			bits |= TIOCM_RTS;
558 		if (sig & UART_SIG_DSR)
559 			bits |= TIOCM_DSR;
560 		if (sig & UART_SIG_CTS)
561 			bits |= TIOCM_CTS;
562 		if (sig & UART_SIG_DCD)
563 			bits |= TIOCM_CD;
564 		if (sig & (UART_SIG_DRI | UART_SIG_RI))
565 			bits |= TIOCM_RI;
566 		*(int*)data = bits;
567 		break;
568 	default:
569 		error = pps_ioctl(cmd, data, &sc->sc_pps);
570 		if (error == ENODEV)
571 			error = ENOTTY;
572 		break;
573 	}
574 	return (error);
575 }
576