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