xref: /freebsd/sys/dev/uart/uart_tty.c (revision bacb482d94518367b83ecea533ae07cc1b15da34)
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 	sc = tp->t_sc;
154 	if (sc == NULL || sc->sc_leaving)
155 		return;
156 
157 	/*
158 	 * Handle input flow control. Note that if we have hardware support,
159 	 * we don't do anything here. We continue to receive until our buffer
160 	 * is full. At that time we cannot empty the UART itself and it will
161 	 * de-assert RTS for us. In that situation we're completely stuffed.
162 	 * Without hardware support, we need to toggle RTS ourselves.
163 	 */
164 	if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) {
165 		if ((tp->t_state & TS_TBLOCK) &&
166 		    (sc->sc_hwsig & SER_RTS))
167 			UART_SETSIG(sc, SER_DRTS);
168 		else if (!(tp->t_state & TS_TBLOCK) &&
169 		    !(sc->sc_hwsig & SER_RTS))
170 			UART_SETSIG(sc, SER_DRTS|SER_RTS);
171 	}
172 
173 	if (tp->t_state & TS_TTSTOP)
174 		return;
175 
176 	if ((tp->t_state & TS_BUSY) || sc->sc_txbusy)
177 		return;
178 
179 	if (tp->t_outq.c_cc == 0) {
180 		ttwwakeup(tp);
181 		return;
182 	}
183 
184 	sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz);
185 	tp->t_state |= TS_BUSY;
186 	UART_TRANSMIT(sc);
187 	ttwwakeup(tp);
188 }
189 
190 static int
191 uart_tty_param(struct tty *tp, struct termios *t)
192 {
193 	struct uart_softc *sc;
194 	int databits, parity, stopbits;
195 
196 	sc = tp->t_sc;
197 	if (sc == NULL || sc->sc_leaving)
198 		return (ENODEV);
199 	if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
200 		return (EINVAL);
201 	/* Fixate certain parameters for system devices. */
202 	if (sc->sc_sysdev != NULL) {
203 		t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
204 		t->c_cflag |= CLOCAL;
205 		t->c_cflag &= ~HUPCL;
206 	}
207 	if (t->c_ospeed == 0) {
208 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
209 		return (0);
210 	}
211 	switch (t->c_cflag & CSIZE) {
212 	case CS5:	databits = 5; break;
213 	case CS6:	databits = 6; break;
214 	case CS7:	databits = 7; break;
215 	default:	databits = 8; break;
216 	}
217 	stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
218 	if (t->c_cflag & PARENB)
219 		parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
220 		    : UART_PARITY_EVEN;
221 	else
222 		parity = UART_PARITY_NONE;
223 	if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
224 		return (EINVAL);
225 	UART_SETSIG(sc, SER_DDTR | SER_DTR);
226 	/* Set input flow control state. */
227 	if (!sc->sc_hwiflow) {
228 		if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK))
229 			UART_SETSIG(sc, SER_DRTS);
230 		else
231 			UART_SETSIG(sc, SER_DRTS | SER_RTS);
232 	} else
233 		UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
234 	/* Set output flow control state. */
235 	if (sc->sc_hwoflow)
236 		UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
237 	ttsetwater(tp);
238 	return (0);
239 }
240 
241 static int
242 uart_tty_modem(struct tty *tp, int biton, int bitoff)
243 {
244 	struct uart_softc *sc;
245 
246 	sc = tp->t_sc;
247 	if (biton != 0 || bitoff != 0)
248 		UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton);
249 	return (sc->sc_hwsig);
250 }
251 
252 static void
253 uart_tty_break(struct tty *tp, int state)
254 {
255 	struct uart_softc *sc;
256 
257 	sc = tp->t_sc;
258 	UART_IOCTL(sc, UART_IOCTL_BREAK, state);
259 }
260 
261 static void
262 uart_tty_stop(struct tty *tp, int rw)
263 {
264 	struct uart_softc *sc;
265 
266 	sc = tp->t_sc;
267 	if (sc == NULL || sc->sc_leaving)
268 		return;
269 	if (rw & FWRITE) {
270 		if (sc->sc_txbusy) {
271 			sc->sc_txbusy = 0;
272 			UART_FLUSH(sc, UART_FLUSH_TRANSMITTER);
273 		}
274 		tp->t_state &= ~TS_BUSY;
275 	}
276 	if (rw & FREAD) {
277 		UART_FLUSH(sc, UART_FLUSH_RECEIVER);
278 		sc->sc_rxget = sc->sc_rxput = 0;
279 	}
280 }
281 
282 void
283 uart_tty_intr(void *arg)
284 {
285 	struct uart_softc *sc = arg;
286 	struct tty *tp;
287 	int c, pend, sig, xc;
288 
289 	if (sc->sc_leaving)
290 		return;
291 
292 	pend = atomic_readandclear_32(&sc->sc_ttypend);
293 	if (!(pend & UART_IPEND_MASK))
294 		return;
295 
296 	tp = sc->sc_u.u_tty.tp;
297 
298 	if (pend & UART_IPEND_RXREADY) {
299 		while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) {
300 			xc = uart_rx_get(sc);
301 			c = xc & 0xff;
302 			if (xc & UART_STAT_FRAMERR)
303 				c |= TTY_FE;
304 			if (xc & UART_STAT_PARERR)
305 				c |= TTY_PE;
306 			ttyld_rint(tp, c);
307 		}
308 	}
309 
310 	if (pend & UART_IPEND_BREAK) {
311 		if (tp != NULL && !(tp->t_iflag & IGNBRK))
312 			ttyld_rint(tp, 0);
313 	}
314 
315 	if (pend & UART_IPEND_SIGCHG) {
316 		sig = pend & UART_IPEND_SIGMASK;
317 		if (sig & SER_DDCD)
318 			ttyld_modem(tp, sig & SER_DCD);
319 		if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) &&
320 		    !sc->sc_hwoflow) {
321 			if (sig & SER_CTS) {
322 				tp->t_state &= ~TS_TTSTOP;
323 				ttyld_start(tp);
324 			} else
325 				tp->t_state |= TS_TTSTOP;
326 		}
327 	}
328 
329 	if (pend & UART_IPEND_TXIDLE) {
330 		tp->t_state &= ~TS_BUSY;
331 		ttyld_start(tp);
332 	}
333 }
334 
335 int
336 uart_tty_attach(struct uart_softc *sc)
337 {
338 	struct tty *tp;
339 
340 	tp = ttyalloc();
341 	sc->sc_u.u_tty.tp = tp;
342 	tp->t_sc = sc;
343 
344 	sc->sc_u.u_tty.si[0] = make_dev(&uart_cdevsw,
345 	    device_get_unit(sc->sc_dev), UID_ROOT, GID_WHEEL, 0600, "ttyu%r",
346 	    device_get_unit(sc->sc_dev));
347 	sc->sc_u.u_tty.si[0]->si_drv1 = sc;
348 	sc->sc_u.u_tty.si[0]->si_tty = tp;
349 	sc->sc_u.u_tty.si[1] = make_dev(&uart_cdevsw,
350 	    device_get_unit(sc->sc_dev) | UART_MINOR_CALLOUT, UID_UUCP,
351 	    GID_DIALER, 0660, "uart%r", device_get_unit(sc->sc_dev));
352 	sc->sc_u.u_tty.si[1]->si_drv1 = sc;
353 	sc->sc_u.u_tty.si[1]->si_tty = tp;
354 
355 	tp->t_oproc = uart_tty_oproc;
356 	tp->t_param = uart_tty_param;
357 	tp->t_stop = uart_tty_stop;
358 	tp->t_modem = uart_tty_modem;
359 	tp->t_break = uart_tty_break;
360 
361 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
362 		sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
363 		    "ttyu%r", device_get_unit(sc->sc_dev));
364 	}
365 
366 	swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
367 	    INTR_TYPE_TTY, &sc->sc_softih);
368 
369 	return (0);
370 }
371 
372 int uart_tty_detach(struct uart_softc *sc)
373 {
374 
375 	ithread_remove_handler(sc->sc_softih);
376 	destroy_dev(sc->sc_u.u_tty.si[0]);
377 	destroy_dev(sc->sc_u.u_tty.si[1]);
378 	/* ttyfree(sc->sc_u.u_tty.tp); */
379 
380 	return (0);
381 }
382 
383 static int
384 uart_tty_open(struct cdev *dev, int flags, int mode, struct thread *td)
385 {
386 	struct uart_softc *sc;
387 	struct tty *tp;
388 	int error;
389 
390  loop:
391 	sc = dev->si_drv1;
392 	if (sc == NULL || sc->sc_leaving)
393 		return (ENODEV);
394 
395 	tp = dev->si_tty;
396 
397 	if (sc->sc_opened) {
398 		KASSERT(tp->t_state & TS_ISOPEN, ("foo"));
399 		/*
400 		 * The device is open, so everything has been initialized.
401 		 * Handle conflicts.
402 		 */
403 		if (minor(dev) & UART_MINOR_CALLOUT) {
404 			if (!sc->sc_callout)
405 				return (EBUSY);
406 		} else {
407 			if (sc->sc_callout) {
408 				if (flags & O_NONBLOCK)
409 					return (EBUSY);
410 				error =	tsleep(sc, TTIPRI|PCATCH, "uartbi", 0);
411 				if (error)
412 					return (error);
413 				goto loop;
414 			}
415 		}
416 		if (tp->t_state & TS_XCLUDE && suser(td) != 0)
417 			return (EBUSY);
418 	} else {
419 		KASSERT(!(tp->t_state & TS_ISOPEN), ("foo"));
420 		/*
421 		 * The device isn't open, so there are no conflicts.
422 		 * Initialize it.  Initialization is done twice in many
423 		 * cases: to preempt sleeping callin opens if we are
424 		 * callout, and to complete a callin open after DCD rises.
425 		 */
426 		sc->sc_callout = (minor(dev) & UART_MINOR_CALLOUT) ? 1 : 0;
427 		tp->t_dev = dev;
428 
429 		tp->t_cflag = TTYDEF_CFLAG;
430 		tp->t_iflag = TTYDEF_IFLAG;
431 		tp->t_lflag = TTYDEF_LFLAG;
432 		tp->t_oflag = TTYDEF_OFLAG;
433 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
434 		ttychars(tp);
435 		error = uart_tty_param(tp, &tp->t_termios);
436 		if (error)
437 			return (error);
438 		/*
439 		 * Handle initial DCD.
440 		 */
441 		if ((sc->sc_hwsig & SER_DCD) || sc->sc_callout)
442 			ttyld_modem(tp, 1);
443 	}
444 	/*
445 	 * Wait for DCD if necessary.
446 	 */
447 	if (!(tp->t_state & TS_CARR_ON) && !sc->sc_callout &&
448 	    !(tp->t_cflag & CLOCAL) && !(flags & O_NONBLOCK)) {
449 		error = tsleep(TSA_CARR_ON(tp), TTIPRI|PCATCH, "uartdcd", 0);
450 		if (error)
451 			return (error);
452 		goto loop;
453 	}
454 	error = tty_open(dev, tp);
455 	if (error)
456 		return (error);
457 	error = ttyld_open(tp, dev);
458 	if (error)
459 		return (error);
460 
461 	KASSERT(tp->t_state & TS_ISOPEN, ("foo"));
462 	sc->sc_opened = 1;
463 	return (0);
464 }
465 
466 static int
467 uart_tty_close(struct cdev *dev, int flags, int mode, struct thread *td)
468 {
469 	struct uart_softc *sc;
470 	struct tty *tp;
471 
472 	sc = dev->si_drv1;
473 	if (sc == NULL || sc->sc_leaving)
474 		return (ENODEV);
475 	tp = dev->si_tty;
476 	if (!sc->sc_opened) {
477 		KASSERT(!(tp->t_state & TS_ISOPEN), ("foo"));
478 		return (0);
479 	}
480 	KASSERT(tp->t_state & TS_ISOPEN, ("foo"));
481 
482 	if (sc->sc_hwiflow)
483 		UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
484 	if (sc->sc_hwoflow)
485 		UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
486 	if (sc->sc_sysdev == NULL)
487 		UART_SETSIG(sc, SER_DDTR | SER_DRTS);
488 
489 	/* Disable pulse capturing. */
490 	sc->sc_pps.ppsparam.mode = 0;
491 
492 	ttyld_close(tp, flags);
493 	tty_close(tp);
494 	wakeup(sc);
495 	wakeup(TSA_CARR_ON(tp));
496 	KASSERT(!(tp->t_state & TS_ISOPEN), ("foo"));
497 	sc->sc_opened = 0;
498 	return (0);
499 }
500 
501 static int
502 uart_tty_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
503     struct thread *td)
504 {
505 	struct uart_softc *sc;
506 	struct tty *tp;
507 	int error;
508 
509 	sc = dev->si_drv1;
510 	if (sc == NULL || sc->sc_leaving)
511 		return (ENODEV);
512 
513 	tp = dev->si_tty;
514 	error = ttyioctl(dev, cmd, data, flags, td);
515 	if (error != ENOTTY)
516 		return (error);
517 
518 	error = pps_ioctl(cmd, data, &sc->sc_pps);
519 	if (error == ENODEV)
520 		error = ENOTTY;
521 	return (error);
522 }
523