blob: fa4ae94243c21a070bf25d23af022648258172e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
26int sunserial_current_minor = 64;
27
28EXPORT_SYMBOL(sunserial_current_minor);
29
30void
31sunserial_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";
Eddie C. Dostc126cf82006-01-18 14:54:31 -080037 char *ssp_console_modes_prop = "ssp-console-modes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 int baud, bits, stop, cflag;
39 char parity;
40 int carrier = 0;
41 int rtsdtr = 1;
42 int topnd, nd;
43
44 if (!serial_console)
45 return;
46
Eddie C. Dostc126cf82006-01-18 14:54:31 -080047 switch (serial_console) {
48 case PROMDEV_OTTYA:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 mode_prop[3] = 'a';
50 cd_prop[3] = 'a';
51 dtr_prop[3] = 'a';
Eddie C. Dostc126cf82006-01-18 14:54:31 -080052 break;
53
54 case PROMDEV_OTTYB:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 mode_prop[3] = 'b';
56 cd_prop[3] = 'b';
57 dtr_prop[3] = 'b';
Eddie C. Dostc126cf82006-01-18 14:54:31 -080058 break;
59
60 case PROMDEV_ORSC:
61
62 nd = prom_pathtoinode("rsc");
63 if (!nd) {
64 strcpy(mode, "115200,8,n,1,-");
65 goto no_options;
66 }
67
68 if (!prom_node_has_property(nd, ssp_console_modes_prop)) {
69 strcpy(mode, "115200,8,n,1,-");
70 goto no_options;
71 }
72
73 memset(mode, 0, sizeof(mode));
74 prom_getstring(nd, ssp_console_modes_prop, mode, sizeof(mode));
75 goto no_options;
76
77 default:
78 strcpy(mode, "9600,8,n,1,-");
79 goto no_options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
82 topnd = prom_getchild(prom_root_node);
83 nd = prom_searchsiblings(topnd, "options");
84 if (!nd) {
85 strcpy(mode, "9600,8,n,1,-");
86 goto no_options;
87 }
88
89 if (!prom_node_has_property(nd, mode_prop)) {
90 strcpy(mode, "9600,8,n,1,-");
91 goto no_options;
92 }
93
94 memset(mode, 0, sizeof(mode));
95 prom_getstring(nd, mode_prop, mode, sizeof(mode));
96
97 if (prom_node_has_property(nd, cd_prop)) {
98 memset(buf, 0, sizeof(buf));
99 prom_getstring(nd, cd_prop, buf, sizeof(buf));
100 if (!strcmp(buf, "false"))
101 carrier = 1;
102
103 /* XXX: this is unused below. */
104 }
105
106 if (prom_node_has_property(nd, dtr_prop)) {
107 memset(buf, 0, sizeof(buf));
108 prom_getstring(nd, dtr_prop, buf, sizeof(buf));
109 if (!strcmp(buf, "false"))
110 rtsdtr = 0;
111
112 /* XXX: this is unused below. */
113 }
114
115no_options:
116 cflag = CREAD | HUPCL | CLOCAL;
117
118 s = mode;
119 baud = simple_strtoul(s, NULL, 0);
120 s = strchr(s, ',');
121 bits = simple_strtoul(++s, NULL, 0);
122 s = strchr(s, ',');
123 parity = *(++s);
124 s = strchr(s, ',');
125 stop = simple_strtoul(++s, NULL, 0);
126 s = strchr(s, ',');
127 /* XXX handshake is not handled here. */
128
129 switch (baud) {
130 case 150: cflag |= B150; break;
131 case 300: cflag |= B300; break;
132 case 600: cflag |= B600; break;
133 case 1200: cflag |= B1200; break;
134 case 2400: cflag |= B2400; break;
135 case 4800: cflag |= B4800; break;
136 case 9600: cflag |= B9600; break;
137 case 19200: cflag |= B19200; break;
138 case 38400: cflag |= B38400; break;
Eddie C. Dostc126cf82006-01-18 14:54:31 -0800139 case 57600: cflag |= B57600; break;
140 case 115200: cflag |= B115200; break;
141 case 230400: cflag |= B230400; break;
142 case 460800: cflag |= B460800; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 default: baud = 9600; cflag |= B9600; break;
144 }
145
146 switch (bits) {
147 case 5: cflag |= CS5; break;
148 case 6: cflag |= CS6; break;
149 case 7: cflag |= CS7; break;
150 case 8: cflag |= CS8; break;
151 default: cflag |= CS8; break;
152 }
153
154 switch (parity) {
155 case 'o': cflag |= (PARENB | PARODD); break;
156 case 'e': cflag |= PARENB; break;
157 case 'n': default: break;
158 }
159
160 switch (stop) {
161 case 2: cflag |= CSTOPB; break;
162 case 1: default: break;
163 }
164
165 con->cflag = cflag;
166}
167
168EXPORT_SYMBOL(sunserial_console_termios);
169
170/* Sun serial MOUSE auto baud rate detection. */
171static struct mouse_baud_cflag {
172 int baud;
173 unsigned int cflag;
174} mouse_baud_table[] = {
175 { 1200, B1200 },
176 { 2400, B2400 },
177 { 4800, B4800 },
178 { 9600, B9600 },
179 { -1, ~0 },
180 { -1, ~0 },
181};
182
183unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
184{
185 int i;
186
187 for (i = 0; mouse_baud_table[i].baud != -1; i++)
188 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
189 break;
190
191 i += 1;
192 if (mouse_baud_table[i].baud == -1)
193 i = 0;
194
195 *new_baud = mouse_baud_table[i].baud;
196 return mouse_baud_table[i].cflag;
197}
198
199EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
200
201/* Basically, when the baud rate is wrong the mouse spits out
202 * breaks to us.
203 */
204int suncore_mouse_baud_detection(unsigned char ch, int is_break)
205{
206 static int mouse_got_break = 0;
207 static int ctr = 0;
208
209 if (is_break) {
210 /* Let a few normal bytes go by before we jump the gun
211 * and say we need to try another baud rate.
212 */
213 if (mouse_got_break && ctr < 8)
214 return 1;
215
216 /* Ok, we need to try another baud. */
217 ctr = 0;
218 mouse_got_break = 1;
219 return 2;
220 }
221 if (mouse_got_break) {
222 ctr++;
223 if (ch == 0x87) {
224 /* Correct baud rate determined. */
225 mouse_got_break = 0;
226 }
227 return 1;
228 }
229 return 0;
230}
231
232EXPORT_SYMBOL(suncore_mouse_baud_detection);
233
234static int __init suncore_init(void)
235{
236 return 0;
237}
238
239static void __exit suncore_exit(void)
240{
241}
242
243module_init(suncore_init);
244module_exit(suncore_exit);
245
246MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
247MODULE_DESCRIPTION("Sun serial common layer");
248MODULE_LICENSE("GPL");