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