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). The code hereafter
8 * is completely leveraged from the serial.c driver.
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>
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
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>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/capability.h>
30 #include <linux/circ_buf.h>
31 #include <linux/console.h>
32 #include <linux/module.h>
33 #include <linux/serial.h>
34 #include <linux/sysrq.h>
37 #include <asm/hpsim.h>
38 #include <asm/hw_irq.h>
39 #include <asm/uaccess.h>
41 #include "hpsim_ssc.h"
43 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
45 #define KEYBOARD_INTR 3 /* must match with simulator! */
47 #define NR_PORTS 1 /* only one port for now */
56 static char *serial_name = "SimSerial driver";
57 static char *serial_version = "0.6";
59 static struct serial_state rs_table[NR_PORTS];
61 struct tty_driver *hp_simserial_driver;
63 static struct console *console;
65 extern struct console *console_drivers; /* from kernel/printk.c */
67 static void receive_chars(struct tty_struct *tty)
70 static unsigned char seen_esc = 0;
72 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
73 if ( ch == 27 && seen_esc == 0 ) {
77 if ( seen_esc==1 && ch == 'O' ) {
80 } else if ( seen_esc == 2 ) {
81 if ( ch == 'P' ) /* F1 */
83 #ifdef CONFIG_MAGIC_SYSRQ
84 if ( ch == 'S' ) { /* F4 */
86 ch = ia64_ssc(0, 0, 0, 0,
98 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
101 tty_flip_buffer_push(tty);
105 * This is the serial driver's interrupt routine for a single port
107 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
109 struct serial_state *info = dev_id;
110 struct tty_struct *tty = tty_port_tty_get(&info->port);
113 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
117 * pretty simple in our case, because we only get interrupts
126 * -------------------------------------------------------------------
127 * Here ends the serial interrupt routines.
128 * -------------------------------------------------------------------
131 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
133 struct serial_state *info = tty->driver_data;
136 if (!tty || !info->xmit.buf)
139 local_irq_save(flags);
140 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
141 local_irq_restore(flags);
144 info->xmit.buf[info->xmit.head] = ch;
145 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
146 local_irq_restore(flags);
150 static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
156 local_irq_save(flags);
159 char c = info->x_char;
161 console->write(console, &c, 1);
168 if (info->xmit.head == info->xmit.tail || tty->stopped ||
170 #ifdef SIMSERIAL_DEBUG
171 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
172 info->xmit.head, info->xmit.tail, tty->stopped);
177 * We removed the loop and try to do it in to chunks. We need
178 * 2 operations maximum because it's a ring buffer.
180 * First from current to tail if possible.
181 * Then from the beginning of the buffer until necessary
184 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
185 SERIAL_XMIT_SIZE - info->xmit.tail);
186 console->write(console, info->xmit.buf+info->xmit.tail, count);
188 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
191 * We have more at the beginning of the buffer
193 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
195 console->write(console, info->xmit.buf, count);
196 info->xmit.tail += count;
199 local_irq_restore(flags);
202 static void rs_flush_chars(struct tty_struct *tty)
204 struct serial_state *info = tty->driver_data;
206 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
210 transmit_chars(tty, info, NULL);
214 static int rs_write(struct tty_struct * tty,
215 const unsigned char *buf, int count)
217 struct serial_state *info = tty->driver_data;
221 if (!tty || !info->xmit.buf)
224 local_irq_save(flags);
226 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
232 memcpy(info->xmit.buf + info->xmit.head, buf, c);
233 info->xmit.head = ((info->xmit.head + c) &
234 (SERIAL_XMIT_SIZE-1));
239 local_irq_restore(flags);
241 * Hey, we transmit directly from here in our case
243 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
244 && !tty->stopped && !tty->hw_stopped) {
245 transmit_chars(tty, info, NULL);
250 static int rs_write_room(struct tty_struct *tty)
252 struct serial_state *info = tty->driver_data;
254 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
257 static int rs_chars_in_buffer(struct tty_struct *tty)
259 struct serial_state *info = tty->driver_data;
261 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
264 static void rs_flush_buffer(struct tty_struct *tty)
266 struct serial_state *info = tty->driver_data;
269 local_irq_save(flags);
270 info->xmit.head = info->xmit.tail = 0;
271 local_irq_restore(flags);
277 * This function is used to send a high-priority XON/XOFF character to
280 static void rs_send_xchar(struct tty_struct *tty, char ch)
282 struct serial_state *info = tty->driver_data;
287 * I guess we could call console->write() directly but
288 * let's do that for now.
290 transmit_chars(tty, info, NULL);
295 * ------------------------------------------------------------
298 * This routine is called by the upper-layer tty layer to signal that
299 * incoming characters should be throttled.
300 * ------------------------------------------------------------
302 static void rs_throttle(struct tty_struct * tty)
304 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
306 printk(KERN_INFO "simrs_throttle called\n");
309 static void rs_unthrottle(struct tty_struct * tty)
311 struct serial_state *info = tty->driver_data;
317 rs_send_xchar(tty, START_CHAR(tty));
319 printk(KERN_INFO "simrs_unthrottle called\n");
323 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
325 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
326 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
327 (cmd != TIOCMIWAIT)) {
328 if (tty->flags & (1 << TTY_IO_ERROR))
334 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
337 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
340 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
343 case TIOCSERGETLSR: /* Get line status register */
344 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
348 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
350 if (copy_to_user((struct async_struct *) arg,
351 info, sizeof(struct async_struct)))
357 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
358 * - mask passed in arg for lines of interest
359 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
360 * Caller should use TIOCGICOUNT to see which one it was
363 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
367 /* "setserial -W" is called in Debian boot */
368 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
377 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
379 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
381 /* Handle turning off CRTSCTS */
382 if ((old_termios->c_cflag & CRTSCTS) &&
383 !(tty->termios->c_cflag & CRTSCTS)) {
388 * This routine will shutdown a serial port; interrupts are disabled, and
389 * DTR is dropped if the hangup on close termio flag is on.
391 static void shutdown(struct tty_port *port)
393 struct serial_state *info = container_of(port, struct serial_state,
397 local_irq_save(flags);
400 free_irq(info->irq, info);
402 if (info->xmit.buf) {
403 free_page((unsigned long) info->xmit.buf);
404 info->xmit.buf = NULL;
407 local_irq_restore(flags);
410 static void rs_close(struct tty_struct *tty, struct file * filp)
412 struct serial_state *info = tty->driver_data;
414 tty_port_close(&info->port, tty, filp);
417 static void rs_hangup(struct tty_struct *tty)
419 struct serial_state *info = tty->driver_data;
421 rs_flush_buffer(tty);
422 tty_port_hangup(&info->port);
425 static int activate(struct tty_port *port, struct tty_struct *tty)
427 struct serial_state *state = container_of(port, struct serial_state,
433 page = get_zeroed_page(GFP_KERNEL);
437 local_irq_save(flags);
442 state->xmit.buf = (unsigned char *) page;
445 * Allocate the IRQ if necessary
448 retval = request_irq(state->irq, rs_interrupt_single, 0,
454 state->xmit.head = state->xmit.tail = 0;
457 * Set up the tty->alt_speed kludge
459 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
460 tty->alt_speed = 57600;
461 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
462 tty->alt_speed = 115200;
463 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
464 tty->alt_speed = 230400;
465 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
466 tty->alt_speed = 460800;
469 local_irq_restore(flags);
475 * This routine is called whenever a serial port is opened. It
476 * enables interrupts for a serial port, linking in its async structure into
477 * the IRQ chain. It also performs the serial-specific
478 * initialization for the tty structure.
480 static int rs_open(struct tty_struct *tty, struct file * filp)
482 struct serial_state *info = rs_table + tty->index;
483 struct tty_port *port = &info->port;
485 tty->driver_data = info;
486 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
489 * figure out which console to use (should be one already)
491 console = console_drivers;
493 if ((console->flags & CON_ENABLED) && console->write) break;
494 console = console->next;
497 return tty_port_open(port, tty, filp);
501 * /proc fs routines....
504 static int rs_proc_show(struct seq_file *m, void *v)
508 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
509 for (i = 0; i < NR_PORTS; i++)
510 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
515 static int rs_proc_open(struct inode *inode, struct file *file)
517 return single_open(file, rs_proc_show, NULL);
520 static const struct file_operations rs_proc_fops = {
521 .owner = THIS_MODULE,
522 .open = rs_proc_open,
525 .release = single_release,
529 * ---------------------------------------------------------------------
530 * rs_init() and friends
532 * rs_init() is called at boot-time to initialize the serial driver.
533 * ---------------------------------------------------------------------
537 * This routine prints out the appropriate serial driver version
538 * number, and identifies which options were configured into this
541 static inline void show_serial_version(void)
543 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
544 printk(KERN_INFO " no serial options enabled\n");
547 static const struct tty_operations hp_ops = {
551 .put_char = rs_put_char,
552 .flush_chars = rs_flush_chars,
553 .write_room = rs_write_room,
554 .chars_in_buffer = rs_chars_in_buffer,
555 .flush_buffer = rs_flush_buffer,
557 .throttle = rs_throttle,
558 .unthrottle = rs_unthrottle,
559 .send_xchar = rs_send_xchar,
560 .set_termios = rs_set_termios,
562 .proc_fops = &rs_proc_fops,
565 static const struct tty_port_operations hp_port_ops = {
566 .activate = activate,
567 .shutdown = shutdown,
571 * The serial driver boot-time initialization code!
573 static int __init simrs_init(void)
575 struct serial_state *state;
578 if (!ia64_platform_is("hpsim"))
581 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
582 if (!hp_simserial_driver)
585 show_serial_version();
587 /* Initialize the tty_driver structure */
589 hp_simserial_driver->driver_name = "simserial";
590 hp_simserial_driver->name = "ttyS";
591 hp_simserial_driver->major = TTY_MAJOR;
592 hp_simserial_driver->minor_start = 64;
593 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
594 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
595 hp_simserial_driver->init_termios = tty_std_termios;
596 hp_simserial_driver->init_termios.c_cflag =
597 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
598 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
599 tty_set_operations(hp_simserial_driver, &hp_ops);
602 * Let's have a little bit of fun !
605 tty_port_init(&state->port);
606 state->port.ops = &hp_port_ops;
607 state->port.close_delay = 0; /* XXX really 0? */
609 retval = hpsim_get_irq(KEYBOARD_INTR);
611 printk(KERN_ERR "%s: out of interrupt vectors!\n",
618 /* the port is imaginary */
619 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
621 retval = tty_register_driver(hp_simserial_driver);
623 printk(KERN_ERR "Couldn't register simserial driver\n");
629 put_tty_driver(hp_simserial_driver);
634 __initcall(simrs_init);