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