ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / serial / suncore.c
1 /* suncore.c
2  *
3  * Common SUN serial routines.  Based entirely
4  * upon drivers/sbus/char/sunserial.c which is:
5  *
6  * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
7  *
8  * Adaptation to new UART layer is:
9  *
10  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
11  */
12
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/console.h>
17 #include <linux/tty.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21
22 #include <asm/oplib.h>
23
24 #include "suncore.h"
25
26 int sunserial_current_minor = 64;
27
28 EXPORT_SYMBOL(sunserial_current_minor);
29
30 void
31 sunserial_console_termios(struct console *con)
32 {
33         char mode[16], buf[16], *s;
34         char *mode_prop = "ttyX-mode";
35         char *cd_prop = "ttyX-ignore-cd";
36         char *dtr_prop = "ttyX-rts-dtr-off";
37         int baud, bits, stop, cflag;
38         char parity;
39         int carrier = 0;
40         int rtsdtr = 1;
41         int topnd, nd;
42
43         if (!serial_console)
44                 return;
45
46         if (serial_console == 1) {
47                 mode_prop[3] = 'a';
48                 cd_prop[3] = 'a';
49                 dtr_prop[3] = 'a';
50         } else {
51                 mode_prop[3] = 'b';
52                 cd_prop[3] = 'b';
53                 dtr_prop[3] = 'b';
54         }
55
56         topnd = prom_getchild(prom_root_node);
57         nd = prom_searchsiblings(topnd, "options");
58         if (!nd) {
59                 strcpy(mode, "9600,8,n,1,-");
60                 goto no_options;
61         }
62
63         if (!prom_node_has_property(nd, mode_prop)) {
64                 strcpy(mode, "9600,8,n,1,-");
65                 goto no_options;
66         }
67
68         memset(mode, 0, sizeof(mode));
69         prom_getstring(nd, mode_prop, mode, sizeof(mode));
70
71         if (prom_node_has_property(nd, cd_prop)) {
72                 memset(buf, 0, sizeof(buf));
73                 prom_getstring(nd, cd_prop, buf, sizeof(buf));
74                 if (!strcmp(buf, "false"))
75                         carrier = 1;
76
77                 /* XXX: this is unused below. */
78         }
79
80         if (prom_node_has_property(nd, dtr_prop)) {
81                 memset(buf, 0, sizeof(buf));
82                 prom_getstring(nd, dtr_prop, buf, sizeof(buf));
83                 if (!strcmp(buf, "false"))
84                         rtsdtr = 0;
85
86                 /* XXX: this is unused below. */
87         }
88
89 no_options:
90         cflag = CREAD | HUPCL | CLOCAL;
91
92         s = mode;
93         baud = simple_strtoul(s, 0, 0);
94         s = strchr(s, ',');
95         bits = simple_strtoul(++s, 0, 0);
96         s = strchr(s, ',');
97         parity = *(++s);
98         s = strchr(s, ',');
99         stop = simple_strtoul(++s, 0, 0);
100         s = strchr(s, ',');
101         /* XXX handshake is not handled here. */
102
103         switch (baud) {
104                 case 150: cflag |= B150; break;
105                 case 300: cflag |= B300; break;
106                 case 600: cflag |= B600; break;
107                 case 1200: cflag |= B1200; break;
108                 case 2400: cflag |= B2400; break;
109                 case 4800: cflag |= B4800; break;
110                 case 9600: cflag |= B9600; break;
111                 case 19200: cflag |= B19200; break;
112                 case 38400: cflag |= B38400; break;
113                 default: baud = 9600; cflag |= B9600; break;
114         }
115
116         switch (bits) {
117                 case 5: cflag |= CS5; break;
118                 case 6: cflag |= CS6; break;
119                 case 7: cflag |= CS7; break;
120                 case 8: cflag |= CS8; break;
121                 default: cflag |= CS8; break;
122         }
123
124         switch (parity) {
125                 case 'o': cflag |= (PARENB | PARODD); break;
126                 case 'e': cflag |= PARENB; break;
127                 case 'n': default: break;
128         }
129
130         switch (stop) {
131                 case 2: cflag |= CSTOPB; break;
132                 case 1: default: break;
133         }
134
135         con->cflag = cflag;
136 }
137
138 EXPORT_SYMBOL(sunserial_console_termios);
139
140 /* Sun serial MOUSE auto baud rate detection.  */
141 static struct mouse_baud_cflag {
142         int baud;
143         unsigned int cflag;
144 } mouse_baud_table[] = {
145         { 1200, B1200 },
146         { 2400, B2400 },
147         { 4800, B4800 },
148         { 9600, B9600 },
149         { -1, ~0 },
150         { -1, ~0 },
151 };
152
153 unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
154 {
155         int i;
156
157         for (i = 0; mouse_baud_table[i].baud != -1; i++)
158                 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
159                         break;
160
161         i += 1;
162         if (mouse_baud_table[i].baud == -1)
163                 i = 0;
164
165         *new_baud = mouse_baud_table[i].baud;
166         return mouse_baud_table[i].cflag;
167 }
168
169 EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
170
171 /* Basically, when the baud rate is wrong the mouse spits out
172  * breaks to us.
173  */
174 int suncore_mouse_baud_detection(unsigned char ch, int is_break)
175 {
176         static int mouse_got_break = 0;
177         static int ctr = 0;
178
179         if (is_break) {
180                 /* Let a few normal bytes go by before we jump the gun
181                  * and say we need to try another baud rate.
182                  */
183                 if (mouse_got_break && ctr < 8)
184                         return 1;
185
186                 /* Ok, we need to try another baud. */
187                 ctr = 0;
188                 mouse_got_break = 1;
189                 return 2;
190         }
191         if (mouse_got_break) {
192                 ctr++;
193                 if (ch == 0x87) {
194                         /* Correct baud rate determined. */
195                         mouse_got_break = 0;
196                 }
197                 return 1;
198         }
199         return 0;
200 }
201
202 EXPORT_SYMBOL(suncore_mouse_baud_detection);
203
204 static int __init suncore_init(void)
205 {
206         return 0;
207 }
208
209 static void __exit suncore_exit(void)
210 {
211 }
212
213 module_init(suncore_init);
214 module_exit(suncore_exit);
215
216 MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
217 MODULE_DESCRIPTION("Sun serial common layer");
218 MODULE_LICENSE("GPL");