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