Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simulated Serial Driver (fake serial) |
| 3 | * |
| 4 | * This driver is mostly used for bringup purposes and will go away. |
| 5 | * It has a strong dependency on the system console. All outputs |
| 6 | * are rerouted to the same facility as the one used by printk which, in our |
Jiri Slaby | adb636f | 2012-03-05 14:52:39 +0100 | [diff] [blame] | 7 | * case means sys_sim.c console (goes via the simulator). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co |
| 10 | * Stephane Eranian <eranian@hpl.hp.com> |
| 11 | * David Mosberger-Tang <davidm@hpl.hp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/tty.h> |
| 18 | #include <linux/tty_flip.h> |
| 19 | #include <linux/major.h> |
| 20 | #include <linux/fcntl.h> |
| 21 | #include <linux/mm.h> |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 22 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/slab.h> |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 24 | #include <linux/capability.h> |
Jiri Slaby | 3c4782d | 2012-03-05 14:52:31 +0100 | [diff] [blame] | 25 | #include <linux/circ_buf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/console.h> |
Jiri Slaby | 8b91633 | 2012-03-05 14:52:40 +0100 | [diff] [blame] | 27 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/serial.h> |
David Mosberger-Tang | 819c67e | 2005-06-09 22:40:00 -0700 | [diff] [blame] | 30 | #include <linux/sysrq.h> |
Jiri Slaby | 8b91633 | 2012-03-05 14:52:40 +0100 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Jiri Slaby | 035cfe5 | 2012-03-08 21:01:17 +0100 | [diff] [blame] | 33 | #include <asm/hpsim.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jiri Slaby | 035cfe5 | 2012-03-08 21:01:17 +0100 | [diff] [blame] | 35 | #include "hpsim_ssc.h" |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #undef SIMSERIAL_DEBUG /* define this to get some debug information */ |
| 38 | |
| 39 | #define KEYBOARD_INTR 3 /* must match with simulator! */ |
| 40 | |
| 41 | #define NR_PORTS 1 /* only one port for now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jiri Slaby | 3c4782d | 2012-03-05 14:52:31 +0100 | [diff] [blame] | 43 | struct serial_state { |
Jiri Slaby | 7f32f8d | 2012-03-05 14:52:32 +0100 | [diff] [blame] | 44 | struct tty_port port; |
Jiri Slaby | 3c4782d | 2012-03-05 14:52:31 +0100 | [diff] [blame] | 45 | struct circ_buf xmit; |
| 46 | int irq; |
| 47 | int x_char; |
| 48 | }; |
| 49 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 50 | static struct serial_state rs_table[NR_PORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | struct tty_driver *hp_simserial_driver; |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static struct console *console; |
| 55 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame^] | 56 | static void receive_chars(struct tty_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | unsigned char ch; |
| 59 | static unsigned char seen_esc = 0; |
| 60 | |
| 61 | while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) { |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 62 | if (ch == 27 && seen_esc == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | seen_esc = 1; |
| 64 | continue; |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 65 | } else if (seen_esc == 1 && ch == 'O') { |
| 66 | seen_esc = 2; |
| 67 | continue; |
| 68 | } else if (seen_esc == 2) { |
| 69 | if (ch == 'P') /* F1 */ |
| 70 | show_state(); |
David Mosberger-Tang | 819c67e | 2005-06-09 22:40:00 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_MAGIC_SYSRQ |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 72 | if (ch == 'S') { /* F4 */ |
| 73 | do { |
| 74 | ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR); |
| 75 | } while (!ch); |
| 76 | handle_sysrq(ch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 78 | #endif |
| 79 | seen_esc = 0; |
| 80 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | seen_esc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 84 | if (tty_insert_flip_char(port, ch, TTY_NORMAL) == 0) |
Andreas Schwab | d50f5c5 | 2006-01-13 23:46:38 +0100 | [diff] [blame] | 85 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame^] | 87 | tty_flip_buffer_push(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * This is the serial driver's interrupt routine for a single port |
| 92 | */ |
Al Viro | 5dcded1 | 2006-10-08 14:59:19 +0100 | [diff] [blame] | 93 | static irqreturn_t rs_interrupt_single(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 95 | struct serial_state *info = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame^] | 97 | receive_chars(&info->port); |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return IRQ_HANDLED; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * ------------------------------------------------------------------- |
| 104 | * Here ends the serial interrupt routines. |
| 105 | * ------------------------------------------------------------------- |
| 106 | */ |
| 107 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 108 | static int rs_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 110 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | unsigned long flags; |
| 112 | |
Jiri Slaby | 6e9ebcf | 2012-03-05 14:52:42 +0100 | [diff] [blame] | 113 | if (!info->xmit.buf) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 114 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | local_irq_save(flags); |
| 117 | if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) { |
| 118 | local_irq_restore(flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 119 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | info->xmit.buf[info->xmit.head] = ch; |
| 122 | info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1); |
| 123 | local_irq_restore(flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 124 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 127 | static void transmit_chars(struct tty_struct *tty, struct serial_state *info, |
| 128 | int *intr_done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | int count; |
| 131 | unsigned long flags; |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | local_irq_save(flags); |
| 134 | |
| 135 | if (info->x_char) { |
| 136 | char c = info->x_char; |
| 137 | |
| 138 | console->write(console, &c, 1); |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | info->x_char = 0; |
| 141 | |
| 142 | goto out; |
| 143 | } |
| 144 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 145 | if (info->xmit.head == info->xmit.tail || tty->stopped || |
| 146 | tty->hw_stopped) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | #ifdef SIMSERIAL_DEBUG |
| 148 | printk("transmit_chars: head=%d, tail=%d, stopped=%d\n", |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 149 | info->xmit.head, info->xmit.tail, tty->stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | #endif |
| 151 | goto out; |
| 152 | } |
| 153 | /* |
| 154 | * We removed the loop and try to do it in to chunks. We need |
| 155 | * 2 operations maximum because it's a ring buffer. |
| 156 | * |
| 157 | * First from current to tail if possible. |
| 158 | * Then from the beginning of the buffer until necessary |
| 159 | */ |
| 160 | |
| 161 | count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE), |
| 162 | SERIAL_XMIT_SIZE - info->xmit.tail); |
| 163 | console->write(console, info->xmit.buf+info->xmit.tail, count); |
| 164 | |
| 165 | info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1); |
| 166 | |
| 167 | /* |
| 168 | * We have more at the beginning of the buffer |
| 169 | */ |
| 170 | count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 171 | if (count) { |
| 172 | console->write(console, info->xmit.buf, count); |
| 173 | info->xmit.tail += count; |
| 174 | } |
| 175 | out: |
| 176 | local_irq_restore(flags); |
| 177 | } |
| 178 | |
| 179 | static void rs_flush_chars(struct tty_struct *tty) |
| 180 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 181 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 183 | if (info->xmit.head == info->xmit.tail || tty->stopped || |
| 184 | tty->hw_stopped || !info->xmit.buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return; |
| 186 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 187 | transmit_chars(tty, info, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | static int rs_write(struct tty_struct * tty, |
| 191 | const unsigned char *buf, int count) |
| 192 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 193 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | int c, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | unsigned long flags; |
| 196 | |
Jiri Slaby | 6e9ebcf | 2012-03-05 14:52:42 +0100 | [diff] [blame] | 197 | if (!info->xmit.buf) |
Jiri Slaby | d88405d | 2012-03-05 14:52:29 +0100 | [diff] [blame] | 198 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | local_irq_save(flags); |
| 201 | while (1) { |
| 202 | c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 203 | if (count < c) |
| 204 | c = count; |
| 205 | if (c <= 0) { |
| 206 | break; |
| 207 | } |
| 208 | memcpy(info->xmit.buf + info->xmit.head, buf, c); |
| 209 | info->xmit.head = ((info->xmit.head + c) & |
| 210 | (SERIAL_XMIT_SIZE-1)); |
| 211 | buf += c; |
| 212 | count -= c; |
| 213 | ret += c; |
| 214 | } |
| 215 | local_irq_restore(flags); |
| 216 | /* |
| 217 | * Hey, we transmit directly from here in our case |
| 218 | */ |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 219 | if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) && |
| 220 | !tty->stopped && !tty->hw_stopped) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 221 | transmit_chars(tty, info, NULL); |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | static int rs_write_room(struct tty_struct *tty) |
| 227 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 228 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 231 | } |
| 232 | |
| 233 | static int rs_chars_in_buffer(struct tty_struct *tty) |
| 234 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 235 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 238 | } |
| 239 | |
| 240 | static void rs_flush_buffer(struct tty_struct *tty) |
| 241 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 242 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | unsigned long flags; |
| 244 | |
| 245 | local_irq_save(flags); |
| 246 | info->xmit.head = info->xmit.tail = 0; |
| 247 | local_irq_restore(flags); |
| 248 | |
Alan Cox | 15648f1 | 2008-07-16 21:52:25 +0100 | [diff] [blame] | 249 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * This function is used to send a high-priority XON/XOFF character to |
| 254 | * the device |
| 255 | */ |
| 256 | static void rs_send_xchar(struct tty_struct *tty, char ch) |
| 257 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 258 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | info->x_char = ch; |
| 261 | if (ch) { |
| 262 | /* |
| 263 | * I guess we could call console->write() directly but |
| 264 | * let's do that for now. |
| 265 | */ |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 266 | transmit_chars(tty, info, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * ------------------------------------------------------------ |
| 272 | * rs_throttle() |
| 273 | * |
| 274 | * This routine is called by the upper-layer tty layer to signal that |
| 275 | * incoming characters should be throttled. |
| 276 | * ------------------------------------------------------------ |
| 277 | */ |
| 278 | static void rs_throttle(struct tty_struct * tty) |
| 279 | { |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 280 | if (I_IXOFF(tty)) |
| 281 | rs_send_xchar(tty, STOP_CHAR(tty)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | printk(KERN_INFO "simrs_throttle called\n"); |
| 284 | } |
| 285 | |
| 286 | static void rs_unthrottle(struct tty_struct * tty) |
| 287 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 288 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | if (I_IXOFF(tty)) { |
| 291 | if (info->x_char) |
| 292 | info->x_char = 0; |
| 293 | else |
| 294 | rs_send_xchar(tty, START_CHAR(tty)); |
| 295 | } |
| 296 | printk(KERN_INFO "simrs_unthrottle called\n"); |
| 297 | } |
| 298 | |
Luck, Tony | 10e82f6 | 2011-02-22 11:47:04 -0800 | [diff] [blame] | 299 | static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
| 301 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && |
| 302 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 303 | (cmd != TIOCMIWAIT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 305 | return -EIO; |
| 306 | } |
| 307 | |
| 308 | switch (cmd) { |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 309 | case TIOCGSERIAL: |
| 310 | case TIOCSSERIAL: |
| 311 | case TIOCSERGSTRUCT: |
| 312 | case TIOCMIWAIT: |
| 313 | return 0; |
| 314 | case TIOCSERCONFIG: |
| 315 | case TIOCSERGETLSR: /* Get line status register */ |
| 316 | return -EINVAL; |
| 317 | case TIOCSERGWILD: |
| 318 | case TIOCSERSWILD: |
| 319 | /* "setserial -W" is called in Debian boot */ |
| 320 | printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n"); |
| 321 | return 0; |
| 322 | } |
| 323 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) |
| 327 | |
Tony Luck | f889a26 | 2006-12-12 10:47:36 -0800 | [diff] [blame] | 328 | static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | /* Handle turning off CRTSCTS */ |
| 331 | if ((old_termios->c_cflag & CRTSCTS) && |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 332 | !(tty->termios.c_cflag & CRTSCTS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | tty->hw_stopped = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | /* |
| 337 | * This routine will shutdown a serial port; interrupts are disabled, and |
| 338 | * DTR is dropped if the hangup on close termio flag is on. |
| 339 | */ |
Jiri Slaby | 458cd31 | 2012-03-05 14:52:38 +0100 | [diff] [blame] | 340 | static void shutdown(struct tty_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Jiri Slaby | 458cd31 | 2012-03-05 14:52:38 +0100 | [diff] [blame] | 342 | struct serial_state *info = container_of(port, struct serial_state, |
| 343 | port); |
| 344 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | local_irq_save(flags); |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 347 | if (info->irq) |
| 348 | free_irq(info->irq, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 350 | if (info->xmit.buf) { |
| 351 | free_page((unsigned long) info->xmit.buf); |
| 352 | info->xmit.buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | local_irq_restore(flags); |
| 355 | } |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | static void rs_close(struct tty_struct *tty, struct file * filp) |
| 358 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 359 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Jiri Slaby | 458cd31 | 2012-03-05 14:52:38 +0100 | [diff] [blame] | 361 | tty_port_close(&info->port, tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | static void rs_hangup(struct tty_struct *tty) |
| 365 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 366 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | rs_flush_buffer(tty); |
Jiri Slaby | 458cd31 | 2012-03-05 14:52:38 +0100 | [diff] [blame] | 369 | tty_port_hangup(&info->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Jiri Slaby | 9aead90 | 2012-03-05 14:52:37 +0100 | [diff] [blame] | 372 | static int activate(struct tty_port *port, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Jiri Slaby | 9aead90 | 2012-03-05 14:52:37 +0100 | [diff] [blame] | 374 | struct serial_state *state = container_of(port, struct serial_state, |
| 375 | port); |
Jiri Slaby | f66279c | 2012-03-05 14:52:41 +0100 | [diff] [blame] | 376 | unsigned long flags, page; |
| 377 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | page = get_zeroed_page(GFP_KERNEL); |
| 380 | if (!page) |
| 381 | return -ENOMEM; |
| 382 | |
| 383 | local_irq_save(flags); |
| 384 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 385 | if (state->xmit.buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | free_page(page); |
| 387 | else |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 388 | state->xmit.buf = (unsigned char *) page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Jiri Slaby | 964105b | 2012-03-05 14:52:17 +0100 | [diff] [blame] | 390 | if (state->irq) { |
Jiri Slaby | 2f8c521 | 2012-03-05 14:52:18 +0100 | [diff] [blame] | 391 | retval = request_irq(state->irq, rs_interrupt_single, 0, |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 392 | "simserial", state); |
Jiri Slaby | 9e12dd5 | 2012-03-08 21:01:19 +0100 | [diff] [blame] | 393 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 397 | state->xmit.head = state->xmit.tail = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | /* |
| 400 | * Set up the tty->alt_speed kludge |
| 401 | */ |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 402 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 403 | tty->alt_speed = 57600; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 404 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 405 | tty->alt_speed = 115200; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 406 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 407 | tty->alt_speed = 230400; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 408 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 409 | tty->alt_speed = 460800; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | errout: |
| 412 | local_irq_restore(flags); |
| 413 | return retval; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | /* |
| 418 | * This routine is called whenever a serial port is opened. It |
| 419 | * enables interrupts for a serial port, linking in its async structure into |
| 420 | * the IRQ chain. It also performs the serial-specific |
| 421 | * initialization for the tty structure. |
| 422 | */ |
| 423 | static int rs_open(struct tty_struct *tty, struct file * filp) |
| 424 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 425 | struct serial_state *info = rs_table + tty->index; |
Jiri Slaby | 7f32f8d | 2012-03-05 14:52:32 +0100 | [diff] [blame] | 426 | struct tty_port *port = &info->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 428 | tty->driver_data = info; |
Jiri Slaby | d6c53c0 | 2013-01-03 15:53:05 +0100 | [diff] [blame] | 429 | port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | * figure out which console to use (should be one already) |
| 433 | */ |
| 434 | console = console_drivers; |
| 435 | while (console) { |
| 436 | if ((console->flags & CON_ENABLED) && console->write) break; |
| 437 | console = console->next; |
| 438 | } |
| 439 | |
Jiri Slaby | 9aead90 | 2012-03-05 14:52:37 +0100 | [diff] [blame] | 440 | return tty_port_open(port, tty, filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * /proc fs routines.... |
| 445 | */ |
| 446 | |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 447 | static int rs_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 449 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Jiri Slaby | 6e9ebcf | 2012-03-05 14:52:42 +0100 | [diff] [blame] | 451 | seq_printf(m, "simserinfo:1.0\n"); |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 452 | for (i = 0; i < NR_PORTS; i++) |
Jiri Slaby | 98e3a9e | 2012-03-05 14:52:30 +0100 | [diff] [blame] | 453 | seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n", |
| 454 | i, rs_table[i].irq); |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 458 | static int rs_proc_open(struct inode *inode, struct file *file) |
| 459 | { |
| 460 | return single_open(file, rs_proc_show, NULL); |
| 461 | } |
| 462 | |
| 463 | static const struct file_operations rs_proc_fops = { |
| 464 | .owner = THIS_MODULE, |
| 465 | .open = rs_proc_open, |
| 466 | .read = seq_read, |
| 467 | .llseek = seq_lseek, |
| 468 | .release = single_release, |
| 469 | }; |
| 470 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 471 | static const struct tty_operations hp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | .open = rs_open, |
| 473 | .close = rs_close, |
| 474 | .write = rs_write, |
| 475 | .put_char = rs_put_char, |
| 476 | .flush_chars = rs_flush_chars, |
| 477 | .write_room = rs_write_room, |
| 478 | .chars_in_buffer = rs_chars_in_buffer, |
| 479 | .flush_buffer = rs_flush_buffer, |
| 480 | .ioctl = rs_ioctl, |
| 481 | .throttle = rs_throttle, |
| 482 | .unthrottle = rs_unthrottle, |
| 483 | .send_xchar = rs_send_xchar, |
| 484 | .set_termios = rs_set_termios, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | .hangup = rs_hangup, |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 486 | .proc_fops = &rs_proc_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
Jiri Slaby | 3734303 | 2012-03-05 14:52:34 +0100 | [diff] [blame] | 489 | static const struct tty_port_operations hp_port_ops = { |
Jiri Slaby | 9aead90 | 2012-03-05 14:52:37 +0100 | [diff] [blame] | 490 | .activate = activate, |
Jiri Slaby | 458cd31 | 2012-03-05 14:52:38 +0100 | [diff] [blame] | 491 | .shutdown = shutdown, |
Jiri Slaby | 3734303 | 2012-03-05 14:52:34 +0100 | [diff] [blame] | 492 | }; |
| 493 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 494 | static int __init simrs_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 496 | struct serial_state *state; |
| 497 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
| 499 | if (!ia64_platform_is("hpsim")) |
| 500 | return -ENODEV; |
| 501 | |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 502 | hp_simserial_driver = alloc_tty_driver(NR_PORTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (!hp_simserial_driver) |
| 504 | return -ENOMEM; |
| 505 | |
Jiri Slaby | 6e9ebcf | 2012-03-05 14:52:42 +0100 | [diff] [blame] | 506 | printk(KERN_INFO "SimSerial driver with no serial options enabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | /* Initialize the tty_driver structure */ |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | hp_simserial_driver->driver_name = "simserial"; |
| 511 | hp_simserial_driver->name = "ttyS"; |
| 512 | hp_simserial_driver->major = TTY_MAJOR; |
| 513 | hp_simserial_driver->minor_start = 64; |
| 514 | hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 515 | hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL; |
| 516 | hp_simserial_driver->init_termios = tty_std_termios; |
| 517 | hp_simserial_driver->init_termios.c_cflag = |
| 518 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 519 | hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW; |
| 520 | tty_set_operations(hp_simserial_driver, &hp_ops); |
| 521 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 522 | state = rs_table; |
Jiri Slaby | 7f32f8d | 2012-03-05 14:52:32 +0100 | [diff] [blame] | 523 | tty_port_init(&state->port); |
Jiri Slaby | 3734303 | 2012-03-05 14:52:34 +0100 | [diff] [blame] | 524 | state->port.ops = &hp_port_ops; |
Jiri Slaby | 7f32f8d | 2012-03-05 14:52:32 +0100 | [diff] [blame] | 525 | state->port.close_delay = 0; /* XXX really 0? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 527 | retval = hpsim_get_irq(KEYBOARD_INTR); |
| 528 | if (retval < 0) { |
| 529 | printk(KERN_ERR "%s: out of interrupt vectors!\n", |
| 530 | __func__); |
| 531 | goto err_free_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 534 | state->irq = retval; |
| 535 | |
| 536 | /* the port is imaginary */ |
Jiri Slaby | 98e3a9e | 2012-03-05 14:52:30 +0100 | [diff] [blame] | 537 | printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq); |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 538 | |
Jiri Slaby | b19e2ca | 2012-08-07 21:47:51 +0200 | [diff] [blame] | 539 | tty_port_link_device(&state->port, hp_simserial_driver, 0); |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 540 | retval = tty_register_driver(hp_simserial_driver); |
| 541 | if (retval) { |
| 542 | printk(KERN_ERR "Couldn't register simserial driver\n"); |
| 543 | goto err_free_tty; |
| 544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | return 0; |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 547 | err_free_tty: |
| 548 | put_tty_driver(hp_simserial_driver); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 549 | tty_port_destroy(&state->port); |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 550 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | #ifndef MODULE |
| 554 | __initcall(simrs_init); |
| 555 | #endif |