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/console.h>
31 #include <linux/module.h>
32 #include <linux/serial.h>
33 #include <linux/serialP.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 */
49 static char *serial_name = "SimSerial driver";
50 static char *serial_version = "0.6";
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
57 #define BASE_BAUD ( 1843200 / 16 )
59 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
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
66 * type must not be UNKNOWN
67 * So I picked arbitrary (guess from where?) values instead
69 static struct serial_state rs_table[NR_PORTS]={
70 /* UART CLK PORT IRQ FLAGS */
71 { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 } /* ttyS0 */
75 * Just for the fun of it !
77 static struct serial_uart_config uart_config[] = {
82 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
84 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
85 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
87 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
91 struct tty_driver *hp_simserial_driver;
93 static struct console *console;
95 static unsigned char *tmp_buf;
97 extern struct console *console_drivers; /* from kernel/printk.c */
100 * ------------------------------------------------------------
101 * rs_stop() and rs_start()
103 * This routines are called before setting or resetting tty->stopped.
104 * They enable or disable transmitter interrupts, as necessary.
105 * ------------------------------------------------------------
107 static void rs_stop(struct tty_struct *tty)
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);
116 static void rs_start(struct tty_struct *tty)
118 #ifdef SIMSERIAL_DEBUG
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);
124 static void receive_chars(struct tty_struct *tty)
127 static unsigned char seen_esc = 0;
129 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
130 if ( ch == 27 && seen_esc == 0 ) {
134 if ( seen_esc==1 && ch == 'O' ) {
137 } else if ( seen_esc == 2 ) {
138 if ( ch == 'P' ) /* F1 */
140 #ifdef CONFIG_MAGIC_SYSRQ
141 if ( ch == 'S' ) { /* F4 */
143 ch = ia64_ssc(0, 0, 0, 0,
155 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
158 tty_flip_buffer_push(tty);
162 * This is the serial driver's interrupt routine for a single port
164 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
166 struct serial_state *info = dev_id;
168 if (!info->tport.tty) {
169 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
173 * pretty simple in our case, because we only get interrupts
176 receive_chars(info->tport.tty);
181 * -------------------------------------------------------------------
182 * Here ends the serial interrupt routines.
183 * -------------------------------------------------------------------
186 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
188 struct serial_state *info = tty->driver_data;
191 if (!tty || !info->xmit.buf)
194 local_irq_save(flags);
195 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
196 local_irq_restore(flags);
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);
205 static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
211 local_irq_save(flags);
214 char c = info->x_char;
216 console->write(console, &c, 1);
224 if (info->xmit.head == info->xmit.tail || tty->stopped ||
226 #ifdef SIMSERIAL_DEBUG
227 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
228 info->xmit.head, info->xmit.tail, tty->stopped);
233 * We removed the loop and try to do it in to chunks. We need
234 * 2 operations maximum because it's a ring buffer.
236 * First from current to tail if possible.
237 * Then from the beginning of the buffer until necessary
240 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
241 SERIAL_XMIT_SIZE - info->xmit.tail);
242 console->write(console, info->xmit.buf+info->xmit.tail, count);
244 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
247 * We have more at the beginning of the buffer
249 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
251 console->write(console, info->xmit.buf, count);
252 info->xmit.tail += count;
255 local_irq_restore(flags);
258 static void rs_flush_chars(struct tty_struct *tty)
260 struct serial_state *info = tty->driver_data;
262 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
266 transmit_chars(tty, info, NULL);
270 static int rs_write(struct tty_struct * tty,
271 const unsigned char *buf, int count)
273 struct serial_state *info = tty->driver_data;
277 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
279 local_irq_save(flags);
281 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
287 memcpy(info->xmit.buf + info->xmit.head, buf, c);
288 info->xmit.head = ((info->xmit.head + c) &
289 (SERIAL_XMIT_SIZE-1));
294 local_irq_restore(flags);
296 * Hey, we transmit directly from here in our case
298 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
299 && !tty->stopped && !tty->hw_stopped) {
300 transmit_chars(tty, info, NULL);
305 static int rs_write_room(struct tty_struct *tty)
307 struct serial_state *info = tty->driver_data;
309 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
312 static int rs_chars_in_buffer(struct tty_struct *tty)
314 struct serial_state *info = tty->driver_data;
316 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
319 static void rs_flush_buffer(struct tty_struct *tty)
321 struct serial_state *info = tty->driver_data;
324 local_irq_save(flags);
325 info->xmit.head = info->xmit.tail = 0;
326 local_irq_restore(flags);
332 * This function is used to send a high-priority XON/XOFF character to
335 static void rs_send_xchar(struct tty_struct *tty, char ch)
337 struct serial_state *info = tty->driver_data;
342 * I guess we could call console->write() directly but
343 * let's do that for now.
345 transmit_chars(tty, info, NULL);
350 * ------------------------------------------------------------
353 * This routine is called by the upper-layer tty layer to signal that
354 * incoming characters should be throttled.
355 * ------------------------------------------------------------
357 static void rs_throttle(struct tty_struct * tty)
359 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
361 printk(KERN_INFO "simrs_throttle called\n");
364 static void rs_unthrottle(struct tty_struct * tty)
366 struct serial_state *info = tty->driver_data;
372 rs_send_xchar(tty, START_CHAR(tty));
374 printk(KERN_INFO "simrs_unthrottle called\n");
378 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
380 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
381 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
382 (cmd != TIOCMIWAIT)) {
383 if (tty->flags & (1 << TTY_IO_ERROR))
389 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
392 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
395 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
398 case TIOCSERGETLSR: /* Get line status register */
399 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
403 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
405 if (copy_to_user((struct async_struct *) arg,
406 info, sizeof(struct async_struct)))
412 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
413 * - mask passed in arg for lines of interest
414 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
415 * Caller should use TIOCGICOUNT to see which one it was
418 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
422 /* "setserial -W" is called in Debian boot */
423 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
432 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
434 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
436 /* Handle turning off CRTSCTS */
437 if ((old_termios->c_cflag & CRTSCTS) &&
438 !(tty->termios->c_cflag & CRTSCTS)) {
444 * This routine will shutdown a serial port; interrupts are disabled, and
445 * DTR is dropped if the hangup on close termio flag is on.
447 static void shutdown(struct tty_struct *tty, struct serial_state *info)
451 if (!(info->tport.flags & ASYNC_INITIALIZED))
454 #ifdef SIMSERIAL_DEBUG
455 printk("Shutting down serial port %d (irq %d)...\n", info->line,
459 local_irq_save(flags);
462 free_irq(info->irq, info);
464 if (info->xmit.buf) {
465 free_page((unsigned long) info->xmit.buf);
466 info->xmit.buf = NULL;
469 set_bit(TTY_IO_ERROR, &tty->flags);
471 info->tport.flags &= ~ASYNC_INITIALIZED;
473 local_irq_restore(flags);
477 * ------------------------------------------------------------
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 * ------------------------------------------------------------
486 static void rs_close(struct tty_struct *tty, struct file * filp)
488 struct serial_state *info = tty->driver_data;
494 local_irq_save(flags);
495 if (tty_hung_up_p(filp)) {
496 #ifdef SIMSERIAL_DEBUG
497 printk("rs_close: hung_up\n");
499 local_irq_restore(flags);
502 #ifdef SIMSERIAL_DEBUG
503 printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count);
505 if ((tty->count == 1) && (info->tport.count != 1)) {
507 * Uh, oh. tty->count is 1, which means that the tty
508 * structure will be freed. info->tport.count should always
509 * be one in these conditions. If it's greater than
510 * one, we've got real problems, since it means the
511 * serial port won't be shutdown.
513 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
514 "info->tport.count is %d\n", info->tport.count);
515 info->tport.count = 1;
517 if (--info->tport.count < 0) {
518 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
519 info->line, info->tport.count);
520 info->tport.count = 0;
522 if (info->tport.count) {
523 local_irq_restore(flags);
526 info->tport.flags |= ASYNC_CLOSING;
527 local_irq_restore(flags);
530 * Now we wait for the transmit buffer to clear; and we notify
531 * the line discipline to only process XON/XOFF characters.
534 rs_flush_buffer(tty);
535 tty_ldisc_flush(tty);
536 info->tport.tty = NULL;
537 if (info->tport.blocked_open) {
538 if (info->tport.close_delay)
539 schedule_timeout_interruptible(info->tport.close_delay);
540 wake_up_interruptible(&info->tport.open_wait);
542 info->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
543 wake_up_interruptible(&info->tport.close_wait);
547 * rs_wait_until_sent() --- wait until the transmitter is empty
549 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
555 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
557 static void rs_hangup(struct tty_struct *tty)
559 struct serial_state *info = tty->driver_data;
561 #ifdef SIMSERIAL_DEBUG
562 printk("rs_hangup: called\n");
565 rs_flush_buffer(tty);
566 if (info->tport.flags & ASYNC_CLOSING)
570 info->tport.count = 0;
571 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
572 info->tport.tty = NULL;
573 wake_up_interruptible(&info->tport.open_wait);
577 static int startup(struct tty_struct *tty, struct serial_state *state)
579 struct tty_port *port = &state->tport;
584 page = get_zeroed_page(GFP_KERNEL);
588 local_irq_save(flags);
590 if (port->flags & ASYNC_INITIALIZED) {
595 if (!state->port || !state->type) {
596 set_bit(TTY_IO_ERROR, &tty->flags);
603 state->xmit.buf = (unsigned char *) page;
605 #ifdef SIMSERIAL_DEBUG
606 printk("startup: ttys%d (irq %d)...", state->line, state->irq);
610 * Allocate the IRQ if necessary
613 retval = request_irq(state->irq, rs_interrupt_single, 0,
619 clear_bit(TTY_IO_ERROR, &tty->flags);
621 state->xmit.head = state->xmit.tail = 0;
625 * Set up serial timers...
627 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
628 timer_active |= 1 << RS_TIMER;
632 * Set up the tty->alt_speed kludge
634 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
635 tty->alt_speed = 57600;
636 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
637 tty->alt_speed = 115200;
638 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
639 tty->alt_speed = 230400;
640 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
641 tty->alt_speed = 460800;
643 port->flags |= ASYNC_INITIALIZED;
644 local_irq_restore(flags);
648 local_irq_restore(flags);
654 * This routine is called whenever a serial port is opened. It
655 * enables interrupts for a serial port, linking in its async structure into
656 * the IRQ chain. It also performs the serial-specific
657 * initialization for the tty structure.
659 static int rs_open(struct tty_struct *tty, struct file * filp)
661 struct serial_state *info = rs_table + tty->index;
666 info->tport.tty = tty;
667 tty->driver_data = info;
668 tty->port = &info->tport;
670 #ifdef SIMSERIAL_DEBUG
671 printk("rs_open %s, count = %d\n", tty->name, info->tport.count);
673 tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
676 page = get_zeroed_page(GFP_KERNEL);
682 tmp_buf = (unsigned char *) page;
686 * If the port is the middle of closing, bail out now
688 if (tty_hung_up_p(filp) || (info->tport.flags & ASYNC_CLOSING)) {
689 if (info->tport.flags & ASYNC_CLOSING)
690 interruptible_sleep_on(&info->tport.close_wait);
691 #ifdef SERIAL_DO_RESTART
692 return ((info->tport.flags & ASYNC_HUP_NOTIFY) ?
693 -EAGAIN : -ERESTARTSYS);
700 * Start up serial port
702 retval = startup(tty, info);
708 * figure out which console to use (should be one already)
710 console = console_drivers;
712 if ((console->flags & CON_ENABLED) && console->write) break;
713 console = console->next;
716 #ifdef SIMSERIAL_DEBUG
717 printk("rs_open ttys%d successful\n", info->line);
723 * /proc fs routines....
726 static inline void line_info(struct seq_file *m, struct serial_state *state)
728 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
729 state->line, uart_config[state->type].name,
730 state->port, state->irq);
733 static int rs_proc_show(struct seq_file *m, void *v)
737 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
738 for (i = 0; i < NR_PORTS; i++)
739 line_info(m, &rs_table[i]);
743 static int rs_proc_open(struct inode *inode, struct file *file)
745 return single_open(file, rs_proc_show, NULL);
748 static const struct file_operations rs_proc_fops = {
749 .owner = THIS_MODULE,
750 .open = rs_proc_open,
753 .release = single_release,
757 * ---------------------------------------------------------------------
758 * rs_init() and friends
760 * rs_init() is called at boot-time to initialize the serial driver.
761 * ---------------------------------------------------------------------
765 * This routine prints out the appropriate serial driver version
766 * number, and identifies which options were configured into this
769 static inline void show_serial_version(void)
771 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
772 printk(KERN_INFO " no serial options enabled\n");
775 static const struct tty_operations hp_ops = {
779 .put_char = rs_put_char,
780 .flush_chars = rs_flush_chars,
781 .write_room = rs_write_room,
782 .chars_in_buffer = rs_chars_in_buffer,
783 .flush_buffer = rs_flush_buffer,
785 .throttle = rs_throttle,
786 .unthrottle = rs_unthrottle,
787 .send_xchar = rs_send_xchar,
788 .set_termios = rs_set_termios,
792 .wait_until_sent = rs_wait_until_sent,
793 .proc_fops = &rs_proc_fops,
797 * The serial driver boot-time initialization code!
803 struct serial_state *state;
805 if (!ia64_platform_is("hpsim"))
808 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
809 if (!hp_simserial_driver)
812 show_serial_version();
814 /* Initialize the tty_driver structure */
816 hp_simserial_driver->driver_name = "simserial";
817 hp_simserial_driver->name = "ttyS";
818 hp_simserial_driver->major = TTY_MAJOR;
819 hp_simserial_driver->minor_start = 64;
820 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
821 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
822 hp_simserial_driver->init_termios = tty_std_termios;
823 hp_simserial_driver->init_termios.c_cflag =
824 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
825 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
826 tty_set_operations(hp_simserial_driver, &hp_ops);
829 * Let's have a little bit of fun !
831 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
832 tty_port_init(&state->tport);
833 state->tport.close_delay = 0; /* XXX really 0? */
835 if (state->type == PORT_UNKNOWN) continue;
838 if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
839 panic("%s: out of interrupt vectors!\n",
844 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
846 state->port, state->irq,
847 uart_config[state->type].name);
850 if (tty_register_driver(hp_simserial_driver))
851 panic("Couldn't register simserial driver\n");
857 __initcall(simrs_init);