ocfs2: avoid inode removal while nfsd is accessing it
[linux/fpc-iii.git] / drivers / tty / serial / suncore.c
blob2491551c293e714f6a6112c02e98d2279fcca842
1 // SPDX-License-Identifier: GPL-2.0
2 /* suncore.c
4 * Common SUN serial routines. Based entirely
5 * upon drivers/sbus/char/sunserial.c which is:
7 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
9 * Adaptation to new UART layer is:
11 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
14 #include <linux/kernel.h>
15 #include <linux/console.h>
16 #include <linux/tty.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/serial_core.h>
20 #include <linux/sunserialcore.h>
21 #include <linux/init.h>
23 #include <asm/prom.h>
26 static int sunserial_current_minor = 64;
28 int sunserial_register_minors(struct uart_driver *drv, int count)
30 int err = 0;
32 drv->minor = sunserial_current_minor;
33 drv->nr += count;
34 /* Register the driver on the first call */
35 if (drv->nr == count)
36 err = uart_register_driver(drv);
37 if (err == 0) {
38 sunserial_current_minor += count;
39 drv->tty_driver->name_base = drv->minor - 64;
41 return err;
43 EXPORT_SYMBOL(sunserial_register_minors);
45 void sunserial_unregister_minors(struct uart_driver *drv, int count)
47 drv->nr -= count;
48 sunserial_current_minor -= count;
50 if (drv->nr == 0)
51 uart_unregister_driver(drv);
53 EXPORT_SYMBOL(sunserial_unregister_minors);
55 int sunserial_console_match(struct console *con, struct device_node *dp,
56 struct uart_driver *drv, int line, bool ignore_line)
58 if (!con)
59 return 0;
61 drv->cons = con;
63 if (of_console_device != dp)
64 return 0;
66 if (!ignore_line) {
67 int off = 0;
69 if (of_console_options &&
70 *of_console_options == 'b')
71 off = 1;
73 if ((line & 1) != off)
74 return 0;
77 if (!console_set_on_cmdline) {
78 con->index = line;
79 add_preferred_console(con->name, line, NULL);
81 return 1;
83 EXPORT_SYMBOL(sunserial_console_match);
85 void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
87 const char *mode, *s;
88 char mode_prop[] = "ttyX-mode";
89 int baud, bits, stop, cflag;
90 char parity;
92 if (of_node_name_eq(uart_dp, "rsc") ||
93 of_node_name_eq(uart_dp, "rsc-console") ||
94 of_node_name_eq(uart_dp, "rsc-control")) {
95 mode = of_get_property(uart_dp,
96 "ssp-console-modes", NULL);
97 if (!mode)
98 mode = "115200,8,n,1,-";
99 } else if (of_node_name_eq(uart_dp, "lom-console")) {
100 mode = "9600,8,n,1,-";
101 } else {
102 struct device_node *dp;
103 char c;
105 c = 'a';
106 if (of_console_options)
107 c = *of_console_options;
109 mode_prop[3] = c;
111 dp = of_find_node_by_path("/options");
112 mode = of_get_property(dp, mode_prop, NULL);
113 if (!mode)
114 mode = "9600,8,n,1,-";
115 of_node_put(dp);
118 cflag = CREAD | HUPCL | CLOCAL;
120 s = mode;
121 baud = simple_strtoul(s, NULL, 0);
122 s = strchr(s, ',');
123 bits = simple_strtoul(++s, NULL, 0);
124 s = strchr(s, ',');
125 parity = *(++s);
126 s = strchr(s, ',');
127 stop = simple_strtoul(++s, NULL, 0);
128 s = strchr(s, ',');
129 /* XXX handshake is not handled here. */
131 switch (baud) {
132 case 150: cflag |= B150; break;
133 case 300: cflag |= B300; break;
134 case 600: cflag |= B600; break;
135 case 1200: cflag |= B1200; break;
136 case 2400: cflag |= B2400; break;
137 case 4800: cflag |= B4800; break;
138 case 9600: cflag |= B9600; break;
139 case 19200: cflag |= B19200; break;
140 case 38400: cflag |= B38400; break;
141 case 57600: cflag |= B57600; break;
142 case 115200: cflag |= B115200; break;
143 case 230400: cflag |= B230400; break;
144 case 460800: cflag |= B460800; break;
145 default: baud = 9600; cflag |= B9600; break;
148 switch (bits) {
149 case 5: cflag |= CS5; break;
150 case 6: cflag |= CS6; break;
151 case 7: cflag |= CS7; break;
152 case 8: cflag |= CS8; break;
153 default: cflag |= CS8; break;
156 switch (parity) {
157 case 'o': cflag |= (PARENB | PARODD); break;
158 case 'e': cflag |= PARENB; break;
159 case 'n': default: break;
162 switch (stop) {
163 case 2: cflag |= CSTOPB; break;
164 case 1: default: break;
167 con->cflag = cflag;
170 /* Sun serial MOUSE auto baud rate detection. */
171 static 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 },
183 unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
185 int i;
187 for (i = 0; mouse_baud_table[i].baud != -1; i++)
188 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
189 break;
191 i += 1;
192 if (mouse_baud_table[i].baud == -1)
193 i = 0;
195 *new_baud = mouse_baud_table[i].baud;
196 return mouse_baud_table[i].cflag;
199 EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
201 /* Basically, when the baud rate is wrong the mouse spits out
202 * breaks to us.
204 int suncore_mouse_baud_detection(unsigned char ch, int is_break)
206 static int mouse_got_break = 0;
207 static int ctr = 0;
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.
213 if (mouse_got_break && ctr < 8)
214 return 1;
216 /* Ok, we need to try another baud. */
217 ctr = 0;
218 mouse_got_break = 1;
219 return 2;
221 if (mouse_got_break) {
222 ctr++;
223 if (ch == 0x87) {
224 /* Correct baud rate determined. */
225 mouse_got_break = 0;
227 return 1;
229 return 0;
232 EXPORT_SYMBOL(suncore_mouse_baud_detection);
234 static int __init suncore_init(void)
236 return 0;
238 device_initcall(suncore_init);
240 #if 0 /* ..def MODULE ; never supported as such */
241 MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
242 MODULE_DESCRIPTION("Sun serial common layer");
243 MODULE_LICENSE("GPL");
244 #endif