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