Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0
  2/* suncore.c
  3 *
  4 * Common SUN serial routines.  Based entirely
  5 * upon drivers/sbus/char/sunserial.c which is:
  6 *
  7 * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
  8 *
  9 * Adaptation to new UART layer is:
 10 *
 11 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
 12 */
 13
 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>
 22
 23#include <asm/prom.h>
 24
 25
 26static int sunserial_current_minor = 64;
 27
 28int sunserial_register_minors(struct uart_driver *drv, int count)
 29{
 30	int err = 0;
 31
 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;
 40	}
 41	return err;
 42}
 43EXPORT_SYMBOL(sunserial_register_minors);
 44
 45void sunserial_unregister_minors(struct uart_driver *drv, int count)
 46{
 47	drv->nr -= count;
 48	sunserial_current_minor -= count;
 49
 50	if (drv->nr == 0)
 51		uart_unregister_driver(drv);
 52}
 53EXPORT_SYMBOL(sunserial_unregister_minors);
 54
 55int sunserial_console_match(struct console *con, struct device_node *dp,
 56			    struct uart_driver *drv, int line, bool ignore_line)
 57{
 58	if (!con)
 59		return 0;
 60
 61	drv->cons = con;
 62
 63	if (of_console_device != dp)
 64		return 0;
 65
 66	if (!ignore_line) {
 67		int off = 0;
 68
 69		if (of_console_options &&
 70		    *of_console_options == 'b')
 71			off = 1;
 72
 73		if ((line & 1) != off)
 74			return 0;
 75	}
 76
 77	if (!console_set_on_cmdline) {
 78		con->index = line;
 79		add_preferred_console(con->name, line, NULL);
 80	}
 81	return 1;
 82}
 83EXPORT_SYMBOL(sunserial_console_match);
 84
 85void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
 86{
 87	const char *mode, *s;
 88	char mode_prop[] = "ttyX-mode";
 89	int baud, bits, stop, cflag;
 90	char parity;
 91
 92	if (!strcmp(uart_dp->name, "rsc") ||
 93	    !strcmp(uart_dp->name, "rsc-console") ||
 94	    !strcmp(uart_dp->name, "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 (!strcmp(uart_dp->name, "lom-console")) {
100		mode = "9600,8,n,1,-";
101	} else {
102		struct device_node *dp;
103		char c;
104
105		c = 'a';
106		if (of_console_options)
107			c = *of_console_options;
108
109		mode_prop[3] = c;
110
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	}
116
117	cflag = CREAD | HUPCL | CLOCAL;
118
119	s = mode;
120	baud = simple_strtoul(s, NULL, 0);
121	s = strchr(s, ',');
122	bits = simple_strtoul(++s, NULL, 0);
123	s = strchr(s, ',');
124	parity = *(++s);
125	s = strchr(s, ',');
126	stop = simple_strtoul(++s, NULL, 0);
127	s = strchr(s, ',');
128	/* XXX handshake is not handled here. */
129
130	switch (baud) {
131		case 150: cflag |= B150; break;
132		case 300: cflag |= B300; break;
133		case 600: cflag |= B600; break;
134		case 1200: cflag |= B1200; break;
135		case 2400: cflag |= B2400; break;
136		case 4800: cflag |= B4800; break;
137		case 9600: cflag |= B9600; break;
138		case 19200: cflag |= B19200; break;
139		case 38400: cflag |= B38400; break;
140		case 57600: cflag |= B57600; break;
141		case 115200: cflag |= B115200; break;
142		case 230400: cflag |= B230400; break;
143		case 460800: cflag |= B460800; break;
144		default: baud = 9600; cflag |= B9600; break;
145	}
146
147	switch (bits) {
148		case 5: cflag |= CS5; break;
149		case 6: cflag |= CS6; break;
150		case 7: cflag |= CS7; break;
151		case 8: cflag |= CS8; break;
152		default: cflag |= CS8; break;
153	}
154
155	switch (parity) {
156		case 'o': cflag |= (PARENB | PARODD); break;
157		case 'e': cflag |= PARENB; break;
158		case 'n': default: break;
159	}
160
161	switch (stop) {
162		case 2: cflag |= CSTOPB; break;
163		case 1: default: break;
164	}
165
166	con->cflag = cflag;
167}
168
169/* Sun serial MOUSE auto baud rate detection.  */
170static struct mouse_baud_cflag {
171	int baud;
172	unsigned int cflag;
173} mouse_baud_table[] = {
174	{ 1200, B1200 },
175	{ 2400, B2400 },
176	{ 4800, B4800 },
177	{ 9600, B9600 },
178	{ -1, ~0 },
179	{ -1, ~0 },
180};
181
182unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
183{
184	int i;
185
186	for (i = 0; mouse_baud_table[i].baud != -1; i++)
187		if (mouse_baud_table[i].cflag == (cflag & CBAUD))
188			break;
189
190	i += 1;
191	if (mouse_baud_table[i].baud == -1)
192		i = 0;
193
194	*new_baud = mouse_baud_table[i].baud;
195	return mouse_baud_table[i].cflag;
196}
197
198EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
199
200/* Basically, when the baud rate is wrong the mouse spits out
201 * breaks to us.
202 */
203int suncore_mouse_baud_detection(unsigned char ch, int is_break)
204{
205	static int mouse_got_break = 0;
206	static int ctr = 0;
207
208	if (is_break) {
209		/* Let a few normal bytes go by before we jump the gun
210		 * and say we need to try another baud rate.
211		 */
212		if (mouse_got_break && ctr < 8)
213			return 1;
214
215		/* Ok, we need to try another baud. */
216		ctr = 0;
217		mouse_got_break = 1;
218		return 2;
219	}
220	if (mouse_got_break) {
221		ctr++;
222		if (ch == 0x87) {
223			/* Correct baud rate determined. */
224			mouse_got_break = 0;
225		}
226		return 1;
227	}
228	return 0;
229}
230
231EXPORT_SYMBOL(suncore_mouse_baud_detection);
232
233static int __init suncore_init(void)
234{
235	return 0;
236}
237device_initcall(suncore_init);
238
239#if 0 /* ..def MODULE ; never supported as such */
240MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
241MODULE_DESCRIPTION("Sun serial common layer");
242MODULE_LICENSE("GPL");
243#endif
  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/kernel.h>
 14#include <linux/console.h>
 15#include <linux/tty.h>
 16#include <linux/errno.h>
 17#include <linux/string.h>
 18#include <linux/serial_core.h>
 19#include <linux/sunserialcore.h>
 20#include <linux/init.h>
 21
 22#include <asm/prom.h>
 23
 24
 25static int sunserial_current_minor = 64;
 26
 27int sunserial_register_minors(struct uart_driver *drv, int count)
 28{
 29	int err = 0;
 30
 31	drv->minor = sunserial_current_minor;
 32	drv->nr += count;
 33	/* Register the driver on the first call */
 34	if (drv->nr == count)
 35		err = uart_register_driver(drv);
 36	if (err == 0) {
 37		sunserial_current_minor += count;
 38		drv->tty_driver->name_base = drv->minor - 64;
 39	}
 40	return err;
 41}
 42EXPORT_SYMBOL(sunserial_register_minors);
 43
 44void sunserial_unregister_minors(struct uart_driver *drv, int count)
 45{
 46	drv->nr -= count;
 47	sunserial_current_minor -= count;
 48
 49	if (drv->nr == 0)
 50		uart_unregister_driver(drv);
 51}
 52EXPORT_SYMBOL(sunserial_unregister_minors);
 53
 54int sunserial_console_match(struct console *con, struct device_node *dp,
 55			    struct uart_driver *drv, int line, bool ignore_line)
 56{
 57	if (!con)
 58		return 0;
 59
 60	drv->cons = con;
 61
 62	if (of_console_device != dp)
 63		return 0;
 64
 65	if (!ignore_line) {
 66		int off = 0;
 67
 68		if (of_console_options &&
 69		    *of_console_options == 'b')
 70			off = 1;
 71
 72		if ((line & 1) != off)
 73			return 0;
 74	}
 75
 76	if (!console_set_on_cmdline) {
 77		con->index = line;
 78		add_preferred_console(con->name, line, NULL);
 79	}
 80	return 1;
 81}
 82EXPORT_SYMBOL(sunserial_console_match);
 83
 84void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
 85{
 86	const char *mode, *s;
 87	char mode_prop[] = "ttyX-mode";
 88	int baud, bits, stop, cflag;
 89	char parity;
 90
 91	if (!strcmp(uart_dp->name, "rsc") ||
 92	    !strcmp(uart_dp->name, "rsc-console") ||
 93	    !strcmp(uart_dp->name, "rsc-control")) {
 94		mode = of_get_property(uart_dp,
 95				       "ssp-console-modes", NULL);
 96		if (!mode)
 97			mode = "115200,8,n,1,-";
 98	} else if (!strcmp(uart_dp->name, "lom-console")) {
 99		mode = "9600,8,n,1,-";
100	} else {
101		struct device_node *dp;
102		char c;
103
104		c = 'a';
105		if (of_console_options)
106			c = *of_console_options;
107
108		mode_prop[3] = c;
109
110		dp = of_find_node_by_path("/options");
111		mode = of_get_property(dp, mode_prop, NULL);
112		if (!mode)
113			mode = "9600,8,n,1,-";
114	}
115
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;
139		case 57600: cflag |= B57600; break;
140		case 115200: cflag |= B115200; break;
141		case 230400: cflag |= B230400; break;
142		case 460800: cflag |= B460800; break;
143		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
168/* Sun serial MOUSE auto baud rate detection.  */
169static struct mouse_baud_cflag {
170	int baud;
171	unsigned int cflag;
172} mouse_baud_table[] = {
173	{ 1200, B1200 },
174	{ 2400, B2400 },
175	{ 4800, B4800 },
176	{ 9600, B9600 },
177	{ -1, ~0 },
178	{ -1, ~0 },
179};
180
181unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
182{
183	int i;
184
185	for (i = 0; mouse_baud_table[i].baud != -1; i++)
186		if (mouse_baud_table[i].cflag == (cflag & CBAUD))
187			break;
188
189	i += 1;
190	if (mouse_baud_table[i].baud == -1)
191		i = 0;
192
193	*new_baud = mouse_baud_table[i].baud;
194	return mouse_baud_table[i].cflag;
195}
196
197EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
198
199/* Basically, when the baud rate is wrong the mouse spits out
200 * breaks to us.
201 */
202int suncore_mouse_baud_detection(unsigned char ch, int is_break)
203{
204	static int mouse_got_break = 0;
205	static int ctr = 0;
206
207	if (is_break) {
208		/* Let a few normal bytes go by before we jump the gun
209		 * and say we need to try another baud rate.
210		 */
211		if (mouse_got_break && ctr < 8)
212			return 1;
213
214		/* Ok, we need to try another baud. */
215		ctr = 0;
216		mouse_got_break = 1;
217		return 2;
218	}
219	if (mouse_got_break) {
220		ctr++;
221		if (ch == 0x87) {
222			/* Correct baud rate determined. */
223			mouse_got_break = 0;
224		}
225		return 1;
226	}
227	return 0;
228}
229
230EXPORT_SYMBOL(suncore_mouse_baud_detection);
231
232static int __init suncore_init(void)
233{
234	return 0;
235}
236device_initcall(suncore_init);
237
238#if 0 /* ..def MODULE ; never supported as such */
239MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
240MODULE_DESCRIPTION("Sun serial common layer");
241MODULE_LICENSE("GPL");
242#endif