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