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 |
| 7 | * case means sys_sim.c console (goes via the simulator). The code hereafter |
| 8 | * is completely leveraged from the serial.c driver. |
| 9 | * |
| 10 | * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co |
| 11 | * Stephane Eranian <eranian@hpl.hp.com> |
| 12 | * David Mosberger-Tang <davidm@hpl.hp.com> |
| 13 | * |
| 14 | * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close(). |
| 15 | * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c. |
| 16 | * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking |
| 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/tty.h> |
| 23 | #include <linux/tty_flip.h> |
| 24 | #include <linux/major.h> |
| 25 | #include <linux/fcntl.h> |
| 26 | #include <linux/mm.h> |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/slab.h> |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 29 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/console.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/serial.h> |
| 33 | #include <linux/serialP.h> |
David Mosberger-Tang | 819c67e | 2005-06-09 22:40:00 -0700 | [diff] [blame] | 34 | #include <linux/sysrq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <asm/irq.h> |
Jiri Slaby | 035cfe5 | 2012-03-08 21:01:17 +0100 | [diff] [blame] | 37 | #include <asm/hpsim.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/hw_irq.h> |
| 39 | #include <asm/uaccess.h> |
| 40 | |
Jiri Slaby | 035cfe5 | 2012-03-08 21:01:17 +0100 | [diff] [blame] | 41 | #include "hpsim_ssc.h" |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #undef SIMSERIAL_DEBUG /* define this to get some debug information */ |
| 44 | |
| 45 | #define KEYBOARD_INTR 3 /* must match with simulator! */ |
| 46 | |
| 47 | #define NR_PORTS 1 /* only one port for now */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static char *serial_name = "SimSerial driver"; |
| 50 | static char *serial_version = "0.6"; |
| 51 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 52 | static struct serial_state rs_table[NR_PORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | struct tty_driver *hp_simserial_driver; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct console *console; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | extern struct console *console_drivers; /* from kernel/printk.c */ |
| 59 | |
| 60 | /* |
| 61 | * ------------------------------------------------------------ |
| 62 | * rs_stop() and rs_start() |
| 63 | * |
| 64 | * This routines are called before setting or resetting tty->stopped. |
| 65 | * They enable or disable transmitter interrupts, as necessary. |
| 66 | * ------------------------------------------------------------ |
| 67 | */ |
| 68 | static void rs_stop(struct tty_struct *tty) |
| 69 | { |
| 70 | #ifdef SIMSERIAL_DEBUG |
| 71 | printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n", |
| 72 | tty->stopped, tty->hw_stopped, tty->flow_stopped); |
| 73 | #endif |
| 74 | |
| 75 | } |
| 76 | |
| 77 | static void rs_start(struct tty_struct *tty) |
| 78 | { |
viro@ZenIV.linux.org.uk | e72225d | 2005-09-07 23:23:50 +0100 | [diff] [blame] | 79 | #ifdef SIMSERIAL_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n", |
| 81 | tty->stopped, tty->hw_stopped, tty->flow_stopped); |
| 82 | #endif |
| 83 | } |
| 84 | |
Al Viro | 5dcded1 | 2006-10-08 14:59:19 +0100 | [diff] [blame] | 85 | static void receive_chars(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | unsigned char ch; |
| 88 | static unsigned char seen_esc = 0; |
| 89 | |
| 90 | while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) { |
| 91 | if ( ch == 27 && seen_esc == 0 ) { |
| 92 | seen_esc = 1; |
| 93 | continue; |
| 94 | } else { |
| 95 | if ( seen_esc==1 && ch == 'O' ) { |
| 96 | seen_esc = 2; |
| 97 | continue; |
| 98 | } else if ( seen_esc == 2 ) { |
David Mosberger-Tang | 819c67e | 2005-06-09 22:40:00 -0700 | [diff] [blame] | 99 | if ( ch == 'P' ) /* F1 */ |
| 100 | show_state(); |
| 101 | #ifdef CONFIG_MAGIC_SYSRQ |
| 102 | if ( ch == 'S' ) { /* F4 */ |
| 103 | do |
| 104 | ch = ia64_ssc(0, 0, 0, 0, |
| 105 | SSC_GETCHAR); |
| 106 | while (!ch); |
Dmitry Torokhov | f335397 | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 107 | handle_sysrq(ch); |
David Mosberger-Tang | 819c67e | 2005-06-09 22:40:00 -0700 | [diff] [blame] | 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | seen_esc = 0; |
| 111 | continue; |
| 112 | } |
| 113 | } |
| 114 | seen_esc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Andreas Schwab | d50f5c5 | 2006-01-13 23:46:38 +0100 | [diff] [blame] | 116 | if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0) |
| 117 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | tty_flip_buffer_push(tty); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * This is the serial driver's interrupt routine for a single port |
| 124 | */ |
Al Viro | 5dcded1 | 2006-10-08 14:59:19 +0100 | [diff] [blame] | 125 | static irqreturn_t rs_interrupt_single(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 127 | struct serial_state *info = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 129 | if (!info->tport.tty) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info); |
| 131 | return IRQ_NONE; |
| 132 | } |
| 133 | /* |
| 134 | * pretty simple in our case, because we only get interrupts |
| 135 | * on inbound traffic |
| 136 | */ |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 137 | receive_chars(info->tport.tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return IRQ_HANDLED; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * ------------------------------------------------------------------- |
| 143 | * Here ends the serial interrupt routines. |
| 144 | * ------------------------------------------------------------------- |
| 145 | */ |
| 146 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 147 | static int rs_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 149 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | unsigned long flags; |
| 151 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 152 | if (!tty || !info->xmit.buf) |
| 153 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | local_irq_save(flags); |
| 156 | if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) { |
| 157 | local_irq_restore(flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 158 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | info->xmit.buf[info->xmit.head] = ch; |
| 161 | info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1); |
| 162 | local_irq_restore(flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 163 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 166 | static void transmit_chars(struct tty_struct *tty, struct serial_state *info, |
| 167 | int *intr_done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | int count; |
| 170 | unsigned long flags; |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | local_irq_save(flags); |
| 173 | |
| 174 | if (info->x_char) { |
| 175 | char c = info->x_char; |
| 176 | |
| 177 | console->write(console, &c, 1); |
| 178 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 179 | info->icount.tx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | info->x_char = 0; |
| 181 | |
| 182 | goto out; |
| 183 | } |
| 184 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 185 | if (info->xmit.head == info->xmit.tail || tty->stopped || |
| 186 | tty->hw_stopped) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | #ifdef SIMSERIAL_DEBUG |
| 188 | printk("transmit_chars: head=%d, tail=%d, stopped=%d\n", |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 189 | info->xmit.head, info->xmit.tail, tty->stopped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | #endif |
| 191 | goto out; |
| 192 | } |
| 193 | /* |
| 194 | * We removed the loop and try to do it in to chunks. We need |
| 195 | * 2 operations maximum because it's a ring buffer. |
| 196 | * |
| 197 | * First from current to tail if possible. |
| 198 | * Then from the beginning of the buffer until necessary |
| 199 | */ |
| 200 | |
| 201 | count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE), |
| 202 | SERIAL_XMIT_SIZE - info->xmit.tail); |
| 203 | console->write(console, info->xmit.buf+info->xmit.tail, count); |
| 204 | |
| 205 | info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1); |
| 206 | |
| 207 | /* |
| 208 | * We have more at the beginning of the buffer |
| 209 | */ |
| 210 | count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 211 | if (count) { |
| 212 | console->write(console, info->xmit.buf, count); |
| 213 | info->xmit.tail += count; |
| 214 | } |
| 215 | out: |
| 216 | local_irq_restore(flags); |
| 217 | } |
| 218 | |
| 219 | static void rs_flush_chars(struct tty_struct *tty) |
| 220 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 221 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped || |
| 224 | !info->xmit.buf) |
| 225 | return; |
| 226 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 227 | transmit_chars(tty, info, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | static int rs_write(struct tty_struct * tty, |
| 232 | const unsigned char *buf, int count) |
| 233 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 234 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | int c, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | unsigned long flags; |
| 237 | |
Jiri Slaby | d88405d | 2012-03-05 14:52:29 +0100 | [diff] [blame^] | 238 | if (!tty || !info->xmit.buf) |
| 239 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | local_irq_save(flags); |
| 242 | while (1) { |
| 243 | c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 244 | if (count < c) |
| 245 | c = count; |
| 246 | if (c <= 0) { |
| 247 | break; |
| 248 | } |
| 249 | memcpy(info->xmit.buf + info->xmit.head, buf, c); |
| 250 | info->xmit.head = ((info->xmit.head + c) & |
| 251 | (SERIAL_XMIT_SIZE-1)); |
| 252 | buf += c; |
| 253 | count -= c; |
| 254 | ret += c; |
| 255 | } |
| 256 | local_irq_restore(flags); |
| 257 | /* |
| 258 | * Hey, we transmit directly from here in our case |
| 259 | */ |
| 260 | if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) |
| 261 | && !tty->stopped && !tty->hw_stopped) { |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 262 | transmit_chars(tty, info, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int rs_write_room(struct tty_struct *tty) |
| 268 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 269 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 272 | } |
| 273 | |
| 274 | static int rs_chars_in_buffer(struct tty_struct *tty) |
| 275 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 276 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE); |
| 279 | } |
| 280 | |
| 281 | static void rs_flush_buffer(struct tty_struct *tty) |
| 282 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 283 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | unsigned long flags; |
| 285 | |
| 286 | local_irq_save(flags); |
| 287 | info->xmit.head = info->xmit.tail = 0; |
| 288 | local_irq_restore(flags); |
| 289 | |
Alan Cox | 15648f1 | 2008-07-16 21:52:25 +0100 | [diff] [blame] | 290 | tty_wakeup(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* |
| 294 | * This function is used to send a high-priority XON/XOFF character to |
| 295 | * the device |
| 296 | */ |
| 297 | static void rs_send_xchar(struct tty_struct *tty, char ch) |
| 298 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 299 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | info->x_char = ch; |
| 302 | if (ch) { |
| 303 | /* |
| 304 | * I guess we could call console->write() directly but |
| 305 | * let's do that for now. |
| 306 | */ |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 307 | transmit_chars(tty, info, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * ------------------------------------------------------------ |
| 313 | * rs_throttle() |
| 314 | * |
| 315 | * This routine is called by the upper-layer tty layer to signal that |
| 316 | * incoming characters should be throttled. |
| 317 | * ------------------------------------------------------------ |
| 318 | */ |
| 319 | static void rs_throttle(struct tty_struct * tty) |
| 320 | { |
| 321 | if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty)); |
| 322 | |
| 323 | printk(KERN_INFO "simrs_throttle called\n"); |
| 324 | } |
| 325 | |
| 326 | static void rs_unthrottle(struct tty_struct * tty) |
| 327 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 328 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | if (I_IXOFF(tty)) { |
| 331 | if (info->x_char) |
| 332 | info->x_char = 0; |
| 333 | else |
| 334 | rs_send_xchar(tty, START_CHAR(tty)); |
| 335 | } |
| 336 | printk(KERN_INFO "simrs_unthrottle called\n"); |
| 337 | } |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Luck, Tony | 10e82f6 | 2011-02-22 11:47:04 -0800 | [diff] [blame] | 340 | 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] | 341 | { |
| 342 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && |
| 343 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && |
Alan Cox | 0587102 | 2010-09-16 18:21:52 +0100 | [diff] [blame] | 344 | (cmd != TIOCMIWAIT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 346 | return -EIO; |
| 347 | } |
| 348 | |
| 349 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | case TIOCGSERIAL: |
| 351 | printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n"); |
| 352 | return 0; |
| 353 | case TIOCSSERIAL: |
| 354 | printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n"); |
| 355 | return 0; |
| 356 | case TIOCSERCONFIG: |
| 357 | printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n"); |
| 358 | return -EINVAL; |
| 359 | |
| 360 | case TIOCSERGETLSR: /* Get line status register */ |
| 361 | printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n"); |
| 362 | return -EINVAL; |
| 363 | |
| 364 | case TIOCSERGSTRUCT: |
| 365 | printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n"); |
| 366 | #if 0 |
| 367 | if (copy_to_user((struct async_struct *) arg, |
| 368 | info, sizeof(struct async_struct))) |
| 369 | return -EFAULT; |
| 370 | #endif |
| 371 | return 0; |
| 372 | |
| 373 | /* |
| 374 | * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change |
| 375 | * - mask passed in arg for lines of interest |
| 376 | * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) |
| 377 | * Caller should use TIOCGICOUNT to see which one it was |
| 378 | */ |
| 379 | case TIOCMIWAIT: |
| 380 | printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n"); |
| 381 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | case TIOCSERGWILD: |
| 383 | case TIOCSERSWILD: |
| 384 | /* "setserial -W" is called in Debian boot */ |
| 385 | printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n"); |
| 386 | return 0; |
| 387 | |
| 388 | default: |
| 389 | return -ENOIOCTLCMD; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) |
| 395 | |
Tony Luck | f889a26 | 2006-12-12 10:47:36 -0800 | [diff] [blame] | 396 | 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] | 397 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | /* Handle turning off CRTSCTS */ |
| 399 | if ((old_termios->c_cflag & CRTSCTS) && |
| 400 | !(tty->termios->c_cflag & CRTSCTS)) { |
| 401 | tty->hw_stopped = 0; |
| 402 | rs_start(tty); |
| 403 | } |
| 404 | } |
| 405 | /* |
| 406 | * This routine will shutdown a serial port; interrupts are disabled, and |
| 407 | * DTR is dropped if the hangup on close termio flag is on. |
| 408 | */ |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 409 | static void shutdown(struct tty_struct *tty, struct serial_state *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
| 411 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 413 | if (!(info->tport.flags & ASYNC_INITIALIZED)) |
Jiri Slaby | 979b6d8 | 2012-03-05 14:52:15 +0100 | [diff] [blame] | 414 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | #ifdef SIMSERIAL_DEBUG |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 417 | printk("Shutting down serial port %d (irq %d)...\n", info->line, |
| 418 | info->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | #endif |
| 420 | |
| 421 | local_irq_save(flags); |
| 422 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 423 | if (info->irq) |
| 424 | free_irq(info->irq, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | if (info->xmit.buf) { |
| 427 | free_page((unsigned long) info->xmit.buf); |
Al Viro | cfa7fd7 | 2006-10-10 22:46:17 +0100 | [diff] [blame] | 428 | info->xmit.buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 431 | set_bit(TTY_IO_ERROR, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 433 | info->tport.flags &= ~ASYNC_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | local_irq_restore(flags); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * ------------------------------------------------------------ |
| 440 | * rs_close() |
| 441 | * |
| 442 | * This routine is called when the serial port gets closed. First, we |
| 443 | * wait for the last remaining data to be sent. Then, we unlink its |
| 444 | * async structure from the interrupt chain if necessary, and we free |
| 445 | * that IRQ if nothing is left in the chain. |
| 446 | * ------------------------------------------------------------ |
| 447 | */ |
| 448 | static void rs_close(struct tty_struct *tty, struct file * filp) |
| 449 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 450 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | unsigned long flags; |
| 452 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 453 | if (!info) |
| 454 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | local_irq_save(flags); |
| 457 | if (tty_hung_up_p(filp)) { |
| 458 | #ifdef SIMSERIAL_DEBUG |
| 459 | printk("rs_close: hung_up\n"); |
| 460 | #endif |
| 461 | local_irq_restore(flags); |
| 462 | return; |
| 463 | } |
| 464 | #ifdef SIMSERIAL_DEBUG |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 465 | printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | #endif |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 467 | if ((tty->count == 1) && (info->tport.count != 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* |
| 469 | * Uh, oh. tty->count is 1, which means that the tty |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 470 | * structure will be freed. info->tport.count should always |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | * be one in these conditions. If it's greater than |
| 472 | * one, we've got real problems, since it means the |
| 473 | * serial port won't be shutdown. |
| 474 | */ |
| 475 | printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, " |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 476 | "info->tport.count is %d\n", info->tport.count); |
| 477 | info->tport.count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 479 | if (--info->tport.count < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n", |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 481 | info->line, info->tport.count); |
| 482 | info->tport.count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 484 | if (info->tport.count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | local_irq_restore(flags); |
| 486 | return; |
| 487 | } |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 488 | info->tport.flags |= ASYNC_CLOSING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | local_irq_restore(flags); |
| 490 | |
| 491 | /* |
| 492 | * Now we wait for the transmit buffer to clear; and we notify |
| 493 | * the line discipline to only process XON/XOFF characters. |
| 494 | */ |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 495 | shutdown(tty, info); |
Alan Cox | 15648f1 | 2008-07-16 21:52:25 +0100 | [diff] [blame] | 496 | rs_flush_buffer(tty); |
| 497 | tty_ldisc_flush(tty); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 498 | info->tport.tty = NULL; |
| 499 | if (info->tport.blocked_open) { |
Jiri Slaby | 799be6f | 2012-03-05 14:52:25 +0100 | [diff] [blame] | 500 | if (info->tport.close_delay) |
| 501 | schedule_timeout_interruptible(info->tport.close_delay); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 502 | wake_up_interruptible(&info->tport.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 504 | info->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 505 | wake_up_interruptible(&info->tport.close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | * rs_wait_until_sent() --- wait until the transmitter is empty |
| 510 | */ |
| 511 | static void rs_wait_until_sent(struct tty_struct *tty, int timeout) |
| 512 | { |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* |
| 517 | * rs_hangup() --- called by tty_hangup() when a hangup is signaled. |
| 518 | */ |
| 519 | static void rs_hangup(struct tty_struct *tty) |
| 520 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 521 | struct serial_state *info = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | #ifdef SIMSERIAL_DEBUG |
| 524 | printk("rs_hangup: called\n"); |
| 525 | #endif |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | rs_flush_buffer(tty); |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 528 | if (info->tport.flags & ASYNC_CLOSING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return; |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 530 | shutdown(tty, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 532 | info->tport.count = 0; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 533 | info->tport.flags &= ~ASYNC_NORMAL_ACTIVE; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 534 | info->tport.tty = NULL; |
| 535 | wake_up_interruptible(&info->tport.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 539 | static int startup(struct tty_struct *tty, struct serial_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 541 | struct tty_port *port = &state->tport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | unsigned long flags; |
| 543 | int retval=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | unsigned long page; |
| 545 | |
| 546 | page = get_zeroed_page(GFP_KERNEL); |
| 547 | if (!page) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | local_irq_save(flags); |
| 551 | |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 552 | if (port->flags & ASYNC_INITIALIZED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | free_page(page); |
| 554 | goto errout; |
| 555 | } |
| 556 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 557 | if (state->xmit.buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | free_page(page); |
| 559 | else |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 560 | state->xmit.buf = (unsigned char *) page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | #ifdef SIMSERIAL_DEBUG |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 563 | printk("startup: ttys%d (irq %d)...", state->line, state->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | #endif |
| 565 | |
| 566 | /* |
| 567 | * Allocate the IRQ if necessary |
| 568 | */ |
Jiri Slaby | 964105b | 2012-03-05 14:52:17 +0100 | [diff] [blame] | 569 | if (state->irq) { |
Jiri Slaby | 2f8c521 | 2012-03-05 14:52:18 +0100 | [diff] [blame] | 570 | retval = request_irq(state->irq, rs_interrupt_single, 0, |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 571 | "simserial", state); |
Jiri Slaby | 9e12dd5 | 2012-03-08 21:01:19 +0100 | [diff] [blame] | 572 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 576 | clear_bit(TTY_IO_ERROR, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 578 | state->xmit.head = state->xmit.tail = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
| 580 | #if 0 |
| 581 | /* |
| 582 | * Set up serial timers... |
| 583 | */ |
| 584 | timer_table[RS_TIMER].expires = jiffies + 2*HZ/100; |
| 585 | timer_active |= 1 << RS_TIMER; |
| 586 | #endif |
| 587 | |
| 588 | /* |
| 589 | * Set up the tty->alt_speed kludge |
| 590 | */ |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 591 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 592 | tty->alt_speed = 57600; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 593 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 594 | tty->alt_speed = 115200; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 595 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 596 | tty->alt_speed = 230400; |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 597 | if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 598 | tty->alt_speed = 460800; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 600 | port->flags |= ASYNC_INITIALIZED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | local_irq_restore(flags); |
| 602 | return 0; |
| 603 | |
| 604 | errout: |
| 605 | local_irq_restore(flags); |
| 606 | return retval; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /* |
| 611 | * This routine is called whenever a serial port is opened. It |
| 612 | * enables interrupts for a serial port, linking in its async structure into |
| 613 | * the IRQ chain. It also performs the serial-specific |
| 614 | * initialization for the tty structure. |
| 615 | */ |
| 616 | static int rs_open(struct tty_struct *tty, struct file * filp) |
| 617 | { |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 618 | struct serial_state *info = rs_table + tty->index; |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 619 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 621 | info->tport.count++; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 622 | info->tport.tty = tty; |
Jiri Slaby | 916b765 | 2012-03-05 14:52:20 +0100 | [diff] [blame] | 623 | tty->driver_data = info; |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 624 | tty->port = &info->tport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | #ifdef SIMSERIAL_DEBUG |
Jiri Slaby | 12c8035 | 2012-03-05 14:52:26 +0100 | [diff] [blame] | 627 | printk("rs_open %s, count = %d\n", tty->name, info->tport.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | #endif |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 629 | tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | /* |
| 632 | * If the port is the middle of closing, bail out now |
| 633 | */ |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 634 | if (tty_hung_up_p(filp) || (info->tport.flags & ASYNC_CLOSING)) { |
| 635 | if (info->tport.flags & ASYNC_CLOSING) |
Jiri Slaby | 8775879 | 2012-03-05 14:52:24 +0100 | [diff] [blame] | 636 | interruptible_sleep_on(&info->tport.close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | #ifdef SERIAL_DO_RESTART |
Jiri Slaby | 01bd730 | 2012-03-05 14:52:27 +0100 | [diff] [blame] | 638 | return ((info->tport.flags & ASYNC_HUP_NOTIFY) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | -EAGAIN : -ERESTARTSYS); |
| 640 | #else |
| 641 | return -EAGAIN; |
| 642 | #endif |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Start up serial port |
| 647 | */ |
Jiri Slaby | 5e99d54 | 2012-03-05 14:52:23 +0100 | [diff] [blame] | 648 | retval = startup(tty, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if (retval) { |
| 650 | return retval; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * figure out which console to use (should be one already) |
| 655 | */ |
| 656 | console = console_drivers; |
| 657 | while (console) { |
| 658 | if ((console->flags & CON_ENABLED) && console->write) break; |
| 659 | console = console->next; |
| 660 | } |
| 661 | |
| 662 | #ifdef SIMSERIAL_DEBUG |
| 663 | printk("rs_open ttys%d successful\n", info->line); |
| 664 | #endif |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * /proc fs routines.... |
| 670 | */ |
| 671 | |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 672 | static inline void line_info(struct seq_file *m, struct serial_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | { |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 674 | seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n", |
| 675 | state->line, state->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 678 | static int rs_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 680 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 682 | seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version); |
| 683 | for (i = 0; i < NR_PORTS; i++) |
| 684 | line_info(m, &rs_table[i]); |
| 685 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 688 | static int rs_proc_open(struct inode *inode, struct file *file) |
| 689 | { |
| 690 | return single_open(file, rs_proc_show, NULL); |
| 691 | } |
| 692 | |
| 693 | static const struct file_operations rs_proc_fops = { |
| 694 | .owner = THIS_MODULE, |
| 695 | .open = rs_proc_open, |
| 696 | .read = seq_read, |
| 697 | .llseek = seq_lseek, |
| 698 | .release = single_release, |
| 699 | }; |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | /* |
| 702 | * --------------------------------------------------------------------- |
| 703 | * rs_init() and friends |
| 704 | * |
| 705 | * rs_init() is called at boot-time to initialize the serial driver. |
| 706 | * --------------------------------------------------------------------- |
| 707 | */ |
| 708 | |
| 709 | /* |
| 710 | * This routine prints out the appropriate serial driver version |
| 711 | * number, and identifies which options were configured into this |
| 712 | * driver. |
| 713 | */ |
| 714 | static inline void show_serial_version(void) |
| 715 | { |
| 716 | printk(KERN_INFO "%s version %s with", serial_name, serial_version); |
| 717 | printk(KERN_INFO " no serial options enabled\n"); |
| 718 | } |
| 719 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 720 | static const struct tty_operations hp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | .open = rs_open, |
| 722 | .close = rs_close, |
| 723 | .write = rs_write, |
| 724 | .put_char = rs_put_char, |
| 725 | .flush_chars = rs_flush_chars, |
| 726 | .write_room = rs_write_room, |
| 727 | .chars_in_buffer = rs_chars_in_buffer, |
| 728 | .flush_buffer = rs_flush_buffer, |
| 729 | .ioctl = rs_ioctl, |
| 730 | .throttle = rs_throttle, |
| 731 | .unthrottle = rs_unthrottle, |
| 732 | .send_xchar = rs_send_xchar, |
| 733 | .set_termios = rs_set_termios, |
| 734 | .stop = rs_stop, |
| 735 | .start = rs_start, |
| 736 | .hangup = rs_hangup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | .wait_until_sent = rs_wait_until_sent, |
Alexey Dobriyan | bf54215 | 2009-03-31 15:19:23 -0700 | [diff] [blame] | 738 | .proc_fops = &rs_proc_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | }; |
| 740 | |
| 741 | /* |
| 742 | * The serial driver boot-time initialization code! |
| 743 | */ |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 744 | static int __init simrs_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 746 | struct serial_state *state; |
| 747 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | if (!ia64_platform_is("hpsim")) |
| 750 | return -ENODEV; |
| 751 | |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 752 | hp_simserial_driver = alloc_tty_driver(NR_PORTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | if (!hp_simserial_driver) |
| 754 | return -ENOMEM; |
| 755 | |
| 756 | show_serial_version(); |
| 757 | |
| 758 | /* Initialize the tty_driver structure */ |
| 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | hp_simserial_driver->driver_name = "simserial"; |
| 761 | hp_simserial_driver->name = "ttyS"; |
| 762 | hp_simserial_driver->major = TTY_MAJOR; |
| 763 | hp_simserial_driver->minor_start = 64; |
| 764 | hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 765 | hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL; |
| 766 | hp_simserial_driver->init_termios = tty_std_termios; |
| 767 | hp_simserial_driver->init_termios.c_cflag = |
| 768 | B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
| 769 | hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW; |
| 770 | tty_set_operations(hp_simserial_driver, &hp_ops); |
| 771 | |
| 772 | /* |
| 773 | * Let's have a little bit of fun ! |
| 774 | */ |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 775 | state = rs_table; |
| 776 | tty_port_init(&state->tport); |
| 777 | state->tport.close_delay = 0; /* XXX really 0? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 779 | retval = hpsim_get_irq(KEYBOARD_INTR); |
| 780 | if (retval < 0) { |
| 781 | printk(KERN_ERR "%s: out of interrupt vectors!\n", |
| 782 | __func__); |
| 783 | goto err_free_tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 786 | state->irq = retval; |
| 787 | |
| 788 | /* the port is imaginary */ |
| 789 | printk(KERN_INFO "ttyS%d at 0x03f8 (irq = %d) is a 16550\n", |
| 790 | state->line, state->irq); |
| 791 | |
| 792 | retval = tty_register_driver(hp_simserial_driver); |
| 793 | if (retval) { |
| 794 | printk(KERN_ERR "Couldn't register simserial driver\n"); |
| 795 | goto err_free_tty; |
| 796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | return 0; |
Jiri Slaby | fd2d7a6 | 2012-03-05 14:52:28 +0100 | [diff] [blame] | 799 | err_free_tty: |
| 800 | put_tty_driver(hp_simserial_driver); |
| 801 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | #ifndef MODULE |
| 805 | __initcall(simrs_init); |
| 806 | #endif |