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