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