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 #define IRQ_T(state) ((state->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
51 static char *serial_name = "SimSerial driver";
52 static char *serial_version = "0.6";
55 * This has been extracted from asm/serial.h. We need one eventually but
56 * I don't know exactly what we're going to put in it so just fake one
59 #define BASE_BAUD ( 1843200 / 16 )
61 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
64 * Most of the values here are meaningless to this particular driver.
65 * However some values must be preserved for the code (leveraged from serial.c
68 * type must not be UNKNOWN
69 * So I picked arbitrary (guess from where?) values instead
71 static struct serial_state rs_table[NR_PORTS]={
72 /* UART CLK PORT IRQ FLAGS */
73 { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 } /* ttyS0 */
77 * Just for the fun of it !
79 static struct serial_uart_config uart_config[] = {
84 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
86 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
87 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
89 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
93 struct tty_driver *hp_simserial_driver;
95 static struct async_struct *IRQ_ports[NR_IRQS];
97 static struct console *console;
99 static unsigned char *tmp_buf;
101 extern struct console *console_drivers; /* from kernel/printk.c */
104 * ------------------------------------------------------------
105 * rs_stop() and rs_start()
107 * This routines are called before setting or resetting tty->stopped.
108 * They enable or disable transmitter interrupts, as necessary.
109 * ------------------------------------------------------------
111 static void rs_stop(struct tty_struct *tty)
113 #ifdef SIMSERIAL_DEBUG
114 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
115 tty->stopped, tty->hw_stopped, tty->flow_stopped);
120 static void rs_start(struct tty_struct *tty)
122 #ifdef SIMSERIAL_DEBUG
123 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
124 tty->stopped, tty->hw_stopped, tty->flow_stopped);
128 static void receive_chars(struct tty_struct *tty)
131 static unsigned char seen_esc = 0;
133 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
134 if ( ch == 27 && seen_esc == 0 ) {
138 if ( seen_esc==1 && ch == 'O' ) {
141 } else if ( seen_esc == 2 ) {
142 if ( ch == 'P' ) /* F1 */
144 #ifdef CONFIG_MAGIC_SYSRQ
145 if ( ch == 'S' ) { /* F4 */
147 ch = ia64_ssc(0, 0, 0, 0,
159 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
162 tty_flip_buffer_push(tty);
166 * This is the serial driver's interrupt routine for a single port
168 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
170 struct async_struct * info;
173 * I don't know exactly why they don't use the dev_id opaque data
174 * pointer instead of this extra lookup table
176 info = IRQ_ports[irq];
177 if (!info || !info->tty) {
178 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
182 * pretty simple in our case, because we only get interrupts
185 receive_chars(info->tty);
190 * -------------------------------------------------------------------
191 * Here ends the serial interrupt routines.
192 * -------------------------------------------------------------------
195 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
197 struct async_struct *info = (struct async_struct *)tty->driver_data;
200 if (!tty || !info->xmit.buf)
203 local_irq_save(flags);
204 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
205 local_irq_restore(flags);
208 info->xmit.buf[info->xmit.head] = ch;
209 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
210 local_irq_restore(flags);
214 static void transmit_chars(struct async_struct *info, int *intr_done)
220 local_irq_save(flags);
223 char c = info->x_char;
225 console->write(console, &c, 1);
227 info->state->icount.tx++;
233 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
234 #ifdef SIMSERIAL_DEBUG
235 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
236 info->xmit.head, info->xmit.tail, info->tty->stopped);
241 * We removed the loop and try to do it in to chunks. We need
242 * 2 operations maximum because it's a ring buffer.
244 * First from current to tail if possible.
245 * Then from the beginning of the buffer until necessary
248 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
249 SERIAL_XMIT_SIZE - info->xmit.tail);
250 console->write(console, info->xmit.buf+info->xmit.tail, count);
252 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
255 * We have more at the beginning of the buffer
257 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
259 console->write(console, info->xmit.buf, count);
260 info->xmit.tail += count;
263 local_irq_restore(flags);
266 static void rs_flush_chars(struct tty_struct *tty)
268 struct async_struct *info = (struct async_struct *)tty->driver_data;
270 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
274 transmit_chars(info, NULL);
278 static int rs_write(struct tty_struct * tty,
279 const unsigned char *buf, int count)
282 struct async_struct *info = (struct async_struct *)tty->driver_data;
285 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
287 local_irq_save(flags);
289 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
295 memcpy(info->xmit.buf + info->xmit.head, buf, c);
296 info->xmit.head = ((info->xmit.head + c) &
297 (SERIAL_XMIT_SIZE-1));
302 local_irq_restore(flags);
304 * Hey, we transmit directly from here in our case
306 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
307 && !tty->stopped && !tty->hw_stopped) {
308 transmit_chars(info, NULL);
313 static int rs_write_room(struct tty_struct *tty)
315 struct async_struct *info = (struct async_struct *)tty->driver_data;
317 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
320 static int rs_chars_in_buffer(struct tty_struct *tty)
322 struct async_struct *info = (struct async_struct *)tty->driver_data;
324 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
327 static void rs_flush_buffer(struct tty_struct *tty)
329 struct async_struct *info = (struct async_struct *)tty->driver_data;
332 local_irq_save(flags);
333 info->xmit.head = info->xmit.tail = 0;
334 local_irq_restore(flags);
340 * This function is used to send a high-priority XON/XOFF character to
343 static void rs_send_xchar(struct tty_struct *tty, char ch)
345 struct async_struct *info = (struct async_struct *)tty->driver_data;
350 * I guess we could call console->write() directly but
351 * let's do that for now.
353 transmit_chars(info, NULL);
358 * ------------------------------------------------------------
361 * This routine is called by the upper-layer tty layer to signal that
362 * incoming characters should be throttled.
363 * ------------------------------------------------------------
365 static void rs_throttle(struct tty_struct * tty)
367 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
369 printk(KERN_INFO "simrs_throttle called\n");
372 static void rs_unthrottle(struct tty_struct * tty)
374 struct async_struct *info = (struct async_struct *)tty->driver_data;
380 rs_send_xchar(tty, START_CHAR(tty));
382 printk(KERN_INFO "simrs_unthrottle called\n");
386 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
388 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
389 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
390 (cmd != TIOCMIWAIT)) {
391 if (tty->flags & (1 << TTY_IO_ERROR))
397 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
400 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
403 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
406 case TIOCSERGETLSR: /* Get line status register */
407 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
411 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
413 if (copy_to_user((struct async_struct *) arg,
414 info, sizeof(struct async_struct)))
420 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
421 * - mask passed in arg for lines of interest
422 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
423 * Caller should use TIOCGICOUNT to see which one it was
426 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
430 /* "setserial -W" is called in Debian boot */
431 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
440 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
442 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
444 /* Handle turning off CRTSCTS */
445 if ((old_termios->c_cflag & CRTSCTS) &&
446 !(tty->termios->c_cflag & CRTSCTS)) {
452 * This routine will shutdown a serial port; interrupts are disabled, and
453 * DTR is dropped if the hangup on close termio flag is on.
455 static void shutdown(struct async_struct * info)
458 struct serial_state *state = info->state;
461 if (!(state->flags & ASYNC_INITIALIZED))
464 #ifdef SIMSERIAL_DEBUG
465 printk("Shutting down serial port %d (irq %d)....", info->line,
469 local_irq_save(flags);
472 * First unlink the serial port from the IRQ chain...
475 info->next_port->prev_port = info->prev_port;
477 info->prev_port->next_port = info->next_port;
479 IRQ_ports[state->irq] = info->next_port;
482 * Free the IRQ, if necessary
484 if (state->irq && (!IRQ_ports[state->irq] ||
485 !IRQ_ports[state->irq]->next_port)) {
486 if (IRQ_ports[state->irq]) {
487 free_irq(state->irq, NULL);
488 retval = request_irq(state->irq, rs_interrupt_single,
489 IRQ_T(state), "serial",
493 printk(KERN_ERR "serial shutdown: request_irq: error %d"
494 " Couldn't reacquire IRQ.\n", retval);
496 free_irq(state->irq, NULL);
499 if (info->xmit.buf) {
500 free_page((unsigned long) info->xmit.buf);
501 info->xmit.buf = NULL;
504 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
506 state->flags &= ~ASYNC_INITIALIZED;
508 local_irq_restore(flags);
512 * ------------------------------------------------------------
515 * This routine is called when the serial port gets closed. First, we
516 * wait for the last remaining data to be sent. Then, we unlink its
517 * async structure from the interrupt chain if necessary, and we free
518 * that IRQ if nothing is left in the chain.
519 * ------------------------------------------------------------
521 static void rs_close(struct tty_struct *tty, struct file * filp)
523 struct async_struct * info = (struct async_struct *)tty->driver_data;
524 struct serial_state *state;
531 local_irq_save(flags);
532 if (tty_hung_up_p(filp)) {
533 #ifdef SIMSERIAL_DEBUG
534 printk("rs_close: hung_up\n");
536 local_irq_restore(flags);
539 #ifdef SIMSERIAL_DEBUG
540 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
542 if ((tty->count == 1) && (state->count != 1)) {
544 * Uh, oh. tty->count is 1, which means that the tty
545 * structure will be freed. state->count should always
546 * be one in these conditions. If it's greater than
547 * one, we've got real problems, since it means the
548 * serial port won't be shutdown.
550 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
551 "state->count is %d\n", state->count);
554 if (--state->count < 0) {
555 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
556 info->line, state->count);
560 local_irq_restore(flags);
563 state->flags |= ASYNC_CLOSING;
564 local_irq_restore(flags);
567 * Now we wait for the transmit buffer to clear; and we notify
568 * the line discipline to only process XON/XOFF characters.
571 rs_flush_buffer(tty);
572 tty_ldisc_flush(tty);
574 if (info->blocked_open) {
575 if (info->close_delay)
576 schedule_timeout_interruptible(info->close_delay);
577 wake_up_interruptible(&info->open_wait);
579 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
580 wake_up_interruptible(&info->close_wait);
584 * rs_wait_until_sent() --- wait until the transmitter is empty
586 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
592 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
594 static void rs_hangup(struct tty_struct *tty)
596 struct async_struct * info = (struct async_struct *)tty->driver_data;
597 struct serial_state *state = info->state;
599 #ifdef SIMSERIAL_DEBUG
600 printk("rs_hangup: called\n");
603 rs_flush_buffer(tty);
604 if (state->flags & ASYNC_CLOSING)
609 state->flags &= ~ASYNC_NORMAL_ACTIVE;
611 wake_up_interruptible(&info->open_wait);
615 static int get_async_struct(int line, struct async_struct **ret_info)
617 struct async_struct *info;
618 struct serial_state *sstate;
620 sstate = rs_table + line;
623 *ret_info = sstate->info;
626 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
631 init_waitqueue_head(&info->open_wait);
632 init_waitqueue_head(&info->close_wait);
633 info->port = sstate->port;
634 info->xmit_fifo_size = sstate->xmit_fifo_size;
636 info->state = sstate;
639 *ret_info = sstate->info;
642 *ret_info = sstate->info = info;
647 startup(struct async_struct *info)
651 irq_handler_t handler;
652 struct serial_state *state= info->state;
655 page = get_zeroed_page(GFP_KERNEL);
659 local_irq_save(flags);
661 if (state->flags & ASYNC_INITIALIZED) {
666 if (!state->port || !state->type) {
667 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
674 info->xmit.buf = (unsigned char *) page;
676 #ifdef SIMSERIAL_DEBUG
677 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
681 * Allocate the IRQ if necessary
683 if (state->irq && (!IRQ_ports[state->irq] ||
684 !IRQ_ports[state->irq]->next_port)) {
685 if (IRQ_ports[state->irq]) {
689 handler = rs_interrupt_single;
691 retval = request_irq(state->irq, handler, IRQ_T(state),
698 * Insert serial port into IRQ chain.
700 info->prev_port = NULL;
701 info->next_port = IRQ_ports[state->irq];
703 info->next_port->prev_port = info;
704 IRQ_ports[state->irq] = info;
706 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
708 info->xmit.head = info->xmit.tail = 0;
712 * Set up serial timers...
714 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
715 timer_active |= 1 << RS_TIMER;
719 * Set up the tty->alt_speed kludge
722 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
723 info->tty->alt_speed = 57600;
724 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
725 info->tty->alt_speed = 115200;
726 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
727 info->tty->alt_speed = 230400;
728 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
729 info->tty->alt_speed = 460800;
732 state->flags |= ASYNC_INITIALIZED;
733 local_irq_restore(flags);
737 local_irq_restore(flags);
743 * This routine is called whenever a serial port is opened. It
744 * enables interrupts for a serial port, linking in its async structure into
745 * the IRQ chain. It also performs the serial-specific
746 * initialization for the tty structure.
748 static int rs_open(struct tty_struct *tty, struct file * filp)
750 struct async_struct *info;
754 retval = get_async_struct(tty->index, &info);
757 tty->driver_data = info;
760 #ifdef SIMSERIAL_DEBUG
761 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
763 info->tty->low_latency = (info->state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
766 page = get_zeroed_page(GFP_KERNEL);
772 tmp_buf = (unsigned char *) page;
776 * If the port is the middle of closing, bail out now
778 if (tty_hung_up_p(filp) ||
779 (info->state->flags & ASYNC_CLOSING)) {
780 if (info->state->flags & ASYNC_CLOSING)
781 interruptible_sleep_on(&info->close_wait);
782 #ifdef SERIAL_DO_RESTART
783 return ((info->state->flags & ASYNC_HUP_NOTIFY) ?
784 -EAGAIN : -ERESTARTSYS);
791 * Start up serial port
793 retval = startup(info);
799 * figure out which console to use (should be one already)
801 console = console_drivers;
803 if ((console->flags & CON_ENABLED) && console->write) break;
804 console = console->next;
807 #ifdef SIMSERIAL_DEBUG
808 printk("rs_open ttys%d successful\n", info->line);
814 * /proc fs routines....
817 static inline void line_info(struct seq_file *m, struct serial_state *state)
819 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
820 state->line, uart_config[state->type].name,
821 state->port, state->irq);
824 static int rs_proc_show(struct seq_file *m, void *v)
828 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
829 for (i = 0; i < NR_PORTS; i++)
830 line_info(m, &rs_table[i]);
834 static int rs_proc_open(struct inode *inode, struct file *file)
836 return single_open(file, rs_proc_show, NULL);
839 static const struct file_operations rs_proc_fops = {
840 .owner = THIS_MODULE,
841 .open = rs_proc_open,
844 .release = single_release,
848 * ---------------------------------------------------------------------
849 * rs_init() and friends
851 * rs_init() is called at boot-time to initialize the serial driver.
852 * ---------------------------------------------------------------------
856 * This routine prints out the appropriate serial driver version
857 * number, and identifies which options were configured into this
860 static inline void show_serial_version(void)
862 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
863 printk(KERN_INFO " no serial options enabled\n");
866 static const struct tty_operations hp_ops = {
870 .put_char = rs_put_char,
871 .flush_chars = rs_flush_chars,
872 .write_room = rs_write_room,
873 .chars_in_buffer = rs_chars_in_buffer,
874 .flush_buffer = rs_flush_buffer,
876 .throttle = rs_throttle,
877 .unthrottle = rs_unthrottle,
878 .send_xchar = rs_send_xchar,
879 .set_termios = rs_set_termios,
883 .wait_until_sent = rs_wait_until_sent,
884 .proc_fops = &rs_proc_fops,
888 * The serial driver boot-time initialization code!
894 struct serial_state *state;
896 if (!ia64_platform_is("hpsim"))
899 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
900 if (!hp_simserial_driver)
903 show_serial_version();
905 /* Initialize the tty_driver structure */
907 hp_simserial_driver->driver_name = "simserial";
908 hp_simserial_driver->name = "ttyS";
909 hp_simserial_driver->major = TTY_MAJOR;
910 hp_simserial_driver->minor_start = 64;
911 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
912 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
913 hp_simserial_driver->init_termios = tty_std_termios;
914 hp_simserial_driver->init_termios.c_cflag =
915 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
916 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
917 tty_set_operations(hp_simserial_driver, &hp_ops);
920 * Let's have a little bit of fun !
922 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
924 if (state->type == PORT_UNKNOWN) continue;
927 if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
928 panic("%s: out of interrupt vectors!\n",
933 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
935 state->port, state->irq,
936 uart_config[state->type].name);
939 if (tty_register_driver(hp_simserial_driver))
940 panic("Couldn't register simserial driver\n");
946 __initcall(simrs_init);