Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* suncore.c
0003  *
0004  * Common SUN serial routines.  Based entirely
0005  * upon drivers/sbus/char/sunserial.c which is:
0006  *
0007  * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
0008  *
0009  * Adaptation to new UART layer is:
0010  *
0011  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
0012  */
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/console.h>
0016 #include <linux/tty.h>
0017 #include <linux/errno.h>
0018 #include <linux/string.h>
0019 #include <linux/serial_core.h>
0020 #include <linux/sunserialcore.h>
0021 #include <linux/init.h>
0022 
0023 #include <asm/prom.h>
0024 
0025 
0026 static int sunserial_current_minor = 64;
0027 
0028 int sunserial_register_minors(struct uart_driver *drv, int count)
0029 {
0030     int err = 0;
0031 
0032     drv->minor = sunserial_current_minor;
0033     drv->nr += count;
0034     /* Register the driver on the first call */
0035     if (drv->nr == count)
0036         err = uart_register_driver(drv);
0037     if (err == 0) {
0038         sunserial_current_minor += count;
0039         drv->tty_driver->name_base = drv->minor - 64;
0040     }
0041     return err;
0042 }
0043 EXPORT_SYMBOL(sunserial_register_minors);
0044 
0045 void sunserial_unregister_minors(struct uart_driver *drv, int count)
0046 {
0047     drv->nr -= count;
0048     sunserial_current_minor -= count;
0049 
0050     if (drv->nr == 0)
0051         uart_unregister_driver(drv);
0052 }
0053 EXPORT_SYMBOL(sunserial_unregister_minors);
0054 
0055 int sunserial_console_match(struct console *con, struct device_node *dp,
0056                 struct uart_driver *drv, int line, bool ignore_line)
0057 {
0058     if (!con)
0059         return 0;
0060 
0061     drv->cons = con;
0062 
0063     if (of_console_device != dp)
0064         return 0;
0065 
0066     if (!ignore_line) {
0067         int off = 0;
0068 
0069         if (of_console_options &&
0070             *of_console_options == 'b')
0071             off = 1;
0072 
0073         if ((line & 1) != off)
0074             return 0;
0075     }
0076 
0077     if (!console_set_on_cmdline) {
0078         con->index = line;
0079         add_preferred_console(con->name, line, NULL);
0080     }
0081     return 1;
0082 }
0083 EXPORT_SYMBOL(sunserial_console_match);
0084 
0085 void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
0086 {
0087     const char *mode, *s;
0088     char mode_prop[] = "ttyX-mode";
0089     int baud, bits, stop, cflag;
0090     char parity;
0091 
0092     if (of_node_name_eq(uart_dp, "rsc") ||
0093         of_node_name_eq(uart_dp, "rsc-console") ||
0094         of_node_name_eq(uart_dp, "rsc-control")) {
0095         mode = of_get_property(uart_dp,
0096                        "ssp-console-modes", NULL);
0097         if (!mode)
0098             mode = "115200,8,n,1,-";
0099     } else if (of_node_name_eq(uart_dp, "lom-console")) {
0100         mode = "9600,8,n,1,-";
0101     } else {
0102         struct device_node *dp;
0103         char c;
0104 
0105         c = 'a';
0106         if (of_console_options)
0107             c = *of_console_options;
0108 
0109         mode_prop[3] = c;
0110 
0111         dp = of_find_node_by_path("/options");
0112         mode = of_get_property(dp, mode_prop, NULL);
0113         if (!mode)
0114             mode = "9600,8,n,1,-";
0115         of_node_put(dp);
0116     }
0117 
0118     cflag = CREAD | HUPCL | CLOCAL;
0119 
0120     s = mode;
0121     baud = simple_strtoul(s, NULL, 0);
0122     s = strchr(s, ',');
0123     bits = simple_strtoul(++s, NULL, 0);
0124     s = strchr(s, ',');
0125     parity = *(++s);
0126     s = strchr(s, ',');
0127     stop = simple_strtoul(++s, NULL, 0);
0128     s = strchr(s, ',');
0129     /* XXX handshake is not handled here. */
0130 
0131     switch (baud) {
0132         case 150: cflag |= B150; break;
0133         case 300: cflag |= B300; break;
0134         case 600: cflag |= B600; break;
0135         case 1200: cflag |= B1200; break;
0136         case 2400: cflag |= B2400; break;
0137         case 4800: cflag |= B4800; break;
0138         case 9600: cflag |= B9600; break;
0139         case 19200: cflag |= B19200; break;
0140         case 38400: cflag |= B38400; break;
0141         case 57600: cflag |= B57600; break;
0142         case 115200: cflag |= B115200; break;
0143         case 230400: cflag |= B230400; break;
0144         case 460800: cflag |= B460800; break;
0145         default: baud = 9600; cflag |= B9600; break;
0146     }
0147 
0148     switch (bits) {
0149         case 5: cflag |= CS5; break;
0150         case 6: cflag |= CS6; break;
0151         case 7: cflag |= CS7; break;
0152         case 8: cflag |= CS8; break;
0153         default: cflag |= CS8; break;
0154     }
0155 
0156     switch (parity) {
0157         case 'o': cflag |= (PARENB | PARODD); break;
0158         case 'e': cflag |= PARENB; break;
0159         case 'n': default: break;
0160     }
0161 
0162     switch (stop) {
0163         case 2: cflag |= CSTOPB; break;
0164         case 1: default: break;
0165     }
0166 
0167     con->cflag = cflag;
0168 }
0169 
0170 /* Sun serial MOUSE auto baud rate detection.  */
0171 static struct mouse_baud_cflag {
0172     int baud;
0173     unsigned int cflag;
0174 } mouse_baud_table[] = {
0175     { 1200, B1200 },
0176     { 2400, B2400 },
0177     { 4800, B4800 },
0178     { 9600, B9600 },
0179     { -1, ~0 },
0180     { -1, ~0 },
0181 };
0182 
0183 unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
0184 {
0185     int i;
0186 
0187     for (i = 0; mouse_baud_table[i].baud != -1; i++)
0188         if (mouse_baud_table[i].cflag == (cflag & CBAUD))
0189             break;
0190 
0191     i += 1;
0192     if (mouse_baud_table[i].baud == -1)
0193         i = 0;
0194 
0195     *new_baud = mouse_baud_table[i].baud;
0196     return mouse_baud_table[i].cflag;
0197 }
0198 
0199 EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
0200 
0201 /* Basically, when the baud rate is wrong the mouse spits out
0202  * breaks to us.
0203  */
0204 int suncore_mouse_baud_detection(unsigned char ch, int is_break)
0205 {
0206     static int mouse_got_break = 0;
0207     static int ctr = 0;
0208 
0209     if (is_break) {
0210         /* Let a few normal bytes go by before we jump the gun
0211          * and say we need to try another baud rate.
0212          */
0213         if (mouse_got_break && ctr < 8)
0214             return 1;
0215 
0216         /* Ok, we need to try another baud. */
0217         ctr = 0;
0218         mouse_got_break = 1;
0219         return 2;
0220     }
0221     if (mouse_got_break) {
0222         ctr++;
0223         if (ch == 0x87) {
0224             /* Correct baud rate determined. */
0225             mouse_got_break = 0;
0226         }
0227         return 1;
0228     }
0229     return 0;
0230 }
0231 
0232 EXPORT_SYMBOL(suncore_mouse_baud_detection);
0233 
0234 static int __init suncore_init(void)
0235 {
0236     return 0;
0237 }
0238 device_initcall(suncore_init);
0239 
0240 #if 0 /* ..def MODULE ; never supported as such */
0241 MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
0242 MODULE_DESCRIPTION("Sun serial common layer");
0243 MODULE_LICENSE("GPL");
0244 #endif