]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/uart/uart_tty.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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, struct cdev *dev)
127 {
128         struct uart_softc *sc;
129
130         sc = tp->t_sc;
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 = tp->t_sc;
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_oproc(struct tty *tp)
162 {
163         struct uart_softc *sc;
164
165         sc = tp->t_sc;
166         if (sc == NULL || sc->sc_leaving)
167                 return;
168
169         /*
170          * Handle input flow control. Note that if we have hardware support,
171          * we don't do anything here. We continue to receive until our buffer
172          * is full. At that time we cannot empty the UART itself and it will
173          * de-assert RTS for us. In that situation we're completely stuffed.
174          * Without hardware support, we need to toggle RTS ourselves.
175          */
176         if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) {
177                 if ((tp->t_state & TS_TBLOCK) &&
178                     (sc->sc_hwsig & SER_RTS))
179                         UART_SETSIG(sc, SER_DRTS);
180                 else if (!(tp->t_state & TS_TBLOCK) &&
181                     !(sc->sc_hwsig & SER_RTS))
182                         UART_SETSIG(sc, SER_DRTS|SER_RTS);
183         }
184
185         if (tp->t_state & TS_TTSTOP)
186                 return;
187
188         if ((tp->t_state & TS_BUSY) || sc->sc_txbusy)
189                 return;
190
191         if (tp->t_outq.c_cc == 0) {
192                 ttwwakeup(tp);
193                 return;
194         }
195
196         sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz);
197         tp->t_state |= TS_BUSY;
198         UART_TRANSMIT(sc);
199         ttwwakeup(tp);
200 }
201
202 static int
203 uart_tty_param(struct tty *tp, struct termios *t)
204 {
205         struct uart_softc *sc;
206         int databits, parity, stopbits;
207
208         sc = tp->t_sc;
209         if (sc == NULL || sc->sc_leaving)
210                 return (ENODEV);
211         if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
212                 return (EINVAL);
213         /* Fixate certain parameters for system devices. */
214         if (sc->sc_sysdev != NULL) {
215                 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
216                 t->c_cflag |= CLOCAL;
217                 t->c_cflag &= ~HUPCL;
218         }
219         if (t->c_ospeed == 0) {
220                 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
221                 return (0);
222         }
223         switch (t->c_cflag & CSIZE) {
224         case CS5:       databits = 5; break;
225         case CS6:       databits = 6; break;
226         case CS7:       databits = 7; break;
227         default:        databits = 8; break;
228         }
229         stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
230         if (t->c_cflag & PARENB)
231                 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
232                     : UART_PARITY_EVEN;
233         else
234                 parity = UART_PARITY_NONE;
235         if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
236                 return (EINVAL);
237         UART_SETSIG(sc, SER_DDTR | SER_DTR);
238         /* Set input flow control state. */
239         if (!sc->sc_hwiflow) {
240                 if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK))
241                         UART_SETSIG(sc, SER_DRTS);
242                 else
243                         UART_SETSIG(sc, SER_DRTS | SER_RTS);
244         } else
245                 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
246         /* Set output flow control state. */
247         if (sc->sc_hwoflow)
248                 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
249         ttsetwater(tp);
250         return (0);
251 }
252
253 static int
254 uart_tty_modem(struct tty *tp, int biton, int bitoff)
255 {
256         struct uart_softc *sc;
257
258         sc = tp->t_sc;
259         if (biton != 0 || bitoff != 0)
260                 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton);
261         return (sc->sc_hwsig);
262 }
263
264 static void
265 uart_tty_break(struct tty *tp, int state)
266 {
267         struct uart_softc *sc;
268
269         sc = tp->t_sc;
270         UART_IOCTL(sc, UART_IOCTL_BREAK, state);
271 }
272
273 static void
274 uart_tty_stop(struct tty *tp, int rw)
275 {
276         struct uart_softc *sc;
277
278         sc = tp->t_sc;
279         if (sc == NULL || sc->sc_leaving)
280                 return;
281         if (rw & FWRITE) {
282                 if (sc->sc_txbusy) {
283                         sc->sc_txbusy = 0;
284                         UART_FLUSH(sc, UART_FLUSH_TRANSMITTER);
285                 }
286                 tp->t_state &= ~TS_BUSY;
287         }
288         if (rw & FREAD) {
289                 UART_FLUSH(sc, UART_FLUSH_RECEIVER);
290                 sc->sc_rxget = sc->sc_rxput = 0;
291         }
292 }
293
294 void
295 uart_tty_intr(void *arg)
296 {
297         struct uart_softc *sc = arg;
298         struct tty *tp;
299         int c, 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
310         if (pend & SER_INT_RXREADY) {
311                 while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) {
312                         xc = uart_rx_get(sc);
313                         c = xc & 0xff;
314                         if (xc & UART_STAT_FRAMERR)
315                                 c |= TTY_FE;
316                         if (xc & UART_STAT_OVERRUN)
317                                 c |= TTY_OE;
318                         if (xc & UART_STAT_PARERR)
319                                 c |= TTY_PE;
320                         ttyld_rint(tp, c);
321                 }
322         }
323
324         if (pend & SER_INT_BREAK) {
325                 if (tp != NULL && !(tp->t_iflag & IGNBRK))
326                         ttyld_rint(tp, 0);
327         }
328
329         if (pend & SER_INT_SIGCHG) {
330                 sig = pend & SER_INT_SIGMASK;
331                 if (sig & SER_DDCD)
332                         ttyld_modem(tp, sig & SER_DCD);
333                 if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) &&
334                     !sc->sc_hwoflow) {
335                         if (sig & SER_CTS) {
336                                 tp->t_state &= ~TS_TTSTOP;
337                                 ttyld_start(tp);
338                         } else
339                                 tp->t_state |= TS_TTSTOP;
340                 }
341         }
342
343         if (pend & SER_INT_TXIDLE) {
344                 tp->t_state &= ~TS_BUSY;
345                 ttyld_start(tp);
346         }
347 }
348
349 int
350 uart_tty_attach(struct uart_softc *sc)
351 {
352         struct tty *tp;
353         int unit;
354
355         tp = ttyalloc();
356         sc->sc_u.u_tty.tp = tp;
357         tp->t_sc = sc;
358
359         unit = device_get_unit(sc->sc_dev);
360
361         tp->t_oproc = uart_tty_oproc;
362         tp->t_param = uart_tty_param;
363         tp->t_stop = uart_tty_stop;
364         tp->t_modem = uart_tty_modem;
365         tp->t_break = uart_tty_break;
366         tp->t_open = uart_tty_open;
367         tp->t_close = uart_tty_close;
368
369         tp->t_pps = &sc->sc_pps;
370
371         if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
372                 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
373                     "ttyu%r", unit);
374                 ttyconsolemode(tp, 0);
375         }
376
377         swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
378             INTR_TYPE_TTY, &sc->sc_softih);
379
380         ttycreate(tp, TS_CALLOUT, "u%r", unit);
381
382         return (0);
383 }
384
385 int uart_tty_detach(struct uart_softc *sc)
386 {
387         struct tty *tp;
388
389         tp = sc->sc_u.u_tty.tp;
390         tp->t_pps = NULL;
391         ttygone(tp);
392         swi_remove(sc->sc_softih);
393         ttyfree(tp);
394
395         return (0);
396 }