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