2 * Simulated Serial Driver (fake serial)
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).
9 * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/major.h>
20 #include <linux/fcntl.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/capability.h>
25 #include <linux/circ_buf.h>
26 #include <linux/console.h>
27 #include <linux/irq.h>
28 #include <linux/module.h>
29 #include <linux/serial.h>
30 #include <linux/sysrq.h>
31 #include <linux/uaccess.h>
33 #include <asm/hpsim.h>
35 #include "hpsim_ssc.h"
37 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
39 #define KEYBOARD_INTR 3 /* must match with simulator! */
41 #define NR_PORTS 1 /* only one port for now */
50 static struct serial_state rs_table[NR_PORTS];
52 struct tty_driver *hp_simserial_driver;
54 static struct console *console;
56 static void receive_chars(struct tty_struct *tty)
59 static unsigned char seen_esc = 0;
61 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
62 if (ch == 27 && seen_esc == 0) {
65 } else if (seen_esc == 1 && ch == 'O') {
68 } else if (seen_esc == 2) {
69 if (ch == 'P') /* F1 */
71 #ifdef CONFIG_MAGIC_SYSRQ
72 if (ch == 'S') { /* F4 */
74 ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
84 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
87 tty_flip_buffer_push(tty);
91 * This is the serial driver's interrupt routine for a single port
93 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
95 struct serial_state *info = dev_id;
96 struct tty_struct *tty = tty_port_tty_get(&info->port);
99 printk(KERN_INFO "%s: tty=0 problem\n", __func__);
103 * pretty simple in our case, because we only get interrupts
112 * -------------------------------------------------------------------
113 * Here ends the serial interrupt routines.
114 * -------------------------------------------------------------------
117 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
119 struct serial_state *info = tty->driver_data;
125 local_irq_save(flags);
126 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
127 local_irq_restore(flags);
130 info->xmit.buf[info->xmit.head] = ch;
131 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
132 local_irq_restore(flags);
136 static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
142 local_irq_save(flags);
145 char c = info->x_char;
147 console->write(console, &c, 1);
154 if (info->xmit.head == info->xmit.tail || tty->stopped ||
156 #ifdef SIMSERIAL_DEBUG
157 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
158 info->xmit.head, info->xmit.tail, tty->stopped);
163 * We removed the loop and try to do it in to chunks. We need
164 * 2 operations maximum because it's a ring buffer.
166 * First from current to tail if possible.
167 * Then from the beginning of the buffer until necessary
170 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
171 SERIAL_XMIT_SIZE - info->xmit.tail);
172 console->write(console, info->xmit.buf+info->xmit.tail, count);
174 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
177 * We have more at the beginning of the buffer
179 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
181 console->write(console, info->xmit.buf, count);
182 info->xmit.tail += count;
185 local_irq_restore(flags);
188 static void rs_flush_chars(struct tty_struct *tty)
190 struct serial_state *info = tty->driver_data;
192 if (info->xmit.head == info->xmit.tail || tty->stopped ||
193 tty->hw_stopped || !info->xmit.buf)
196 transmit_chars(tty, info, NULL);
199 static int rs_write(struct tty_struct * tty,
200 const unsigned char *buf, int count)
202 struct serial_state *info = tty->driver_data;
209 local_irq_save(flags);
211 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
217 memcpy(info->xmit.buf + info->xmit.head, buf, c);
218 info->xmit.head = ((info->xmit.head + c) &
219 (SERIAL_XMIT_SIZE-1));
224 local_irq_restore(flags);
226 * Hey, we transmit directly from here in our case
228 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
229 !tty->stopped && !tty->hw_stopped)
230 transmit_chars(tty, info, NULL);
235 static int rs_write_room(struct tty_struct *tty)
237 struct serial_state *info = tty->driver_data;
239 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
242 static int rs_chars_in_buffer(struct tty_struct *tty)
244 struct serial_state *info = tty->driver_data;
246 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
249 static void rs_flush_buffer(struct tty_struct *tty)
251 struct serial_state *info = tty->driver_data;
254 local_irq_save(flags);
255 info->xmit.head = info->xmit.tail = 0;
256 local_irq_restore(flags);
262 * This function is used to send a high-priority XON/XOFF character to
265 static void rs_send_xchar(struct tty_struct *tty, char ch)
267 struct serial_state *info = tty->driver_data;
272 * I guess we could call console->write() directly but
273 * let's do that for now.
275 transmit_chars(tty, info, NULL);
280 * ------------------------------------------------------------
283 * This routine is called by the upper-layer tty layer to signal that
284 * incoming characters should be throttled.
285 * ------------------------------------------------------------
287 static void rs_throttle(struct tty_struct * tty)
290 rs_send_xchar(tty, STOP_CHAR(tty));
292 printk(KERN_INFO "simrs_throttle called\n");
295 static void rs_unthrottle(struct tty_struct * tty)
297 struct serial_state *info = tty->driver_data;
303 rs_send_xchar(tty, START_CHAR(tty));
305 printk(KERN_INFO "simrs_unthrottle called\n");
308 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
310 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
311 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
312 (cmd != TIOCMIWAIT)) {
313 if (tty->flags & (1 << TTY_IO_ERROR))
324 case TIOCSERGETLSR: /* Get line status register */
328 /* "setserial -W" is called in Debian boot */
329 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
335 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
337 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
339 /* Handle turning off CRTSCTS */
340 if ((old_termios->c_cflag & CRTSCTS) &&
341 !(tty->termios.c_cflag & CRTSCTS)) {
346 * This routine will shutdown a serial port; interrupts are disabled, and
347 * DTR is dropped if the hangup on close termio flag is on.
349 static void shutdown(struct tty_port *port)
351 struct serial_state *info = container_of(port, struct serial_state,
355 local_irq_save(flags);
357 free_irq(info->irq, info);
359 if (info->xmit.buf) {
360 free_page((unsigned long) info->xmit.buf);
361 info->xmit.buf = NULL;
363 local_irq_restore(flags);
366 static void rs_close(struct tty_struct *tty, struct file * filp)
368 struct serial_state *info = tty->driver_data;
370 tty_port_close(&info->port, tty, filp);
373 static void rs_hangup(struct tty_struct *tty)
375 struct serial_state *info = tty->driver_data;
377 rs_flush_buffer(tty);
378 tty_port_hangup(&info->port);
381 static int activate(struct tty_port *port, struct tty_struct *tty)
383 struct serial_state *state = container_of(port, struct serial_state,
385 unsigned long flags, page;
388 page = get_zeroed_page(GFP_KERNEL);
392 local_irq_save(flags);
397 state->xmit.buf = (unsigned char *) page;
400 retval = request_irq(state->irq, rs_interrupt_single, 0,
406 state->xmit.head = state->xmit.tail = 0;
409 * Set up the tty->alt_speed kludge
411 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
412 tty->alt_speed = 57600;
413 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
414 tty->alt_speed = 115200;
415 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
416 tty->alt_speed = 230400;
417 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
418 tty->alt_speed = 460800;
421 local_irq_restore(flags);
427 * This routine is called whenever a serial port is opened. It
428 * enables interrupts for a serial port, linking in its async structure into
429 * the IRQ chain. It also performs the serial-specific
430 * initialization for the tty structure.
432 static int rs_open(struct tty_struct *tty, struct file * filp)
434 struct serial_state *info = rs_table + tty->index;
435 struct tty_port *port = &info->port;
437 tty->driver_data = info;
438 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
441 * figure out which console to use (should be one already)
443 console = console_drivers;
445 if ((console->flags & CON_ENABLED) && console->write) break;
446 console = console->next;
449 return tty_port_open(port, tty, filp);
453 * /proc fs routines....
456 static int rs_proc_show(struct seq_file *m, void *v)
460 seq_printf(m, "simserinfo:1.0\n");
461 for (i = 0; i < NR_PORTS; i++)
462 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
467 static int rs_proc_open(struct inode *inode, struct file *file)
469 return single_open(file, rs_proc_show, NULL);
472 static const struct file_operations rs_proc_fops = {
473 .owner = THIS_MODULE,
474 .open = rs_proc_open,
477 .release = single_release,
480 static const struct tty_operations hp_ops = {
484 .put_char = rs_put_char,
485 .flush_chars = rs_flush_chars,
486 .write_room = rs_write_room,
487 .chars_in_buffer = rs_chars_in_buffer,
488 .flush_buffer = rs_flush_buffer,
490 .throttle = rs_throttle,
491 .unthrottle = rs_unthrottle,
492 .send_xchar = rs_send_xchar,
493 .set_termios = rs_set_termios,
495 .proc_fops = &rs_proc_fops,
498 static const struct tty_port_operations hp_port_ops = {
499 .activate = activate,
500 .shutdown = shutdown,
503 static int __init simrs_init(void)
505 struct serial_state *state;
508 if (!ia64_platform_is("hpsim"))
511 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
512 if (!hp_simserial_driver)
515 printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
517 /* Initialize the tty_driver structure */
519 hp_simserial_driver->driver_name = "simserial";
520 hp_simserial_driver->name = "ttyS";
521 hp_simserial_driver->major = TTY_MAJOR;
522 hp_simserial_driver->minor_start = 64;
523 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
524 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
525 hp_simserial_driver->init_termios = tty_std_termios;
526 hp_simserial_driver->init_termios.c_cflag =
527 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
528 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
529 tty_set_operations(hp_simserial_driver, &hp_ops);
532 tty_port_init(&state->port);
533 state->port.ops = &hp_port_ops;
534 state->port.close_delay = 0; /* XXX really 0? */
536 retval = hpsim_get_irq(KEYBOARD_INTR);
538 printk(KERN_ERR "%s: out of interrupt vectors!\n",
545 /* the port is imaginary */
546 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
548 tty_port_link_device(&state->port, hp_simserial_driver, 0);
549 retval = tty_register_driver(hp_simserial_driver);
551 printk(KERN_ERR "Couldn't register simserial driver\n");
557 put_tty_driver(hp_simserial_driver);
562 __initcall(simrs_init);