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