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";
52 static struct serial_state rs_table
[NR_PORTS
];
54 struct tty_driver
*hp_simserial_driver
;
56 static struct console
*console
;
58 static unsigned char *tmp_buf
;
60 extern struct console
*console_drivers
; /* from kernel/printk.c */
63 * ------------------------------------------------------------
64 * rs_stop() and rs_start()
66 * This routines are called before setting or resetting tty->stopped.
67 * They enable or disable transmitter interrupts, as necessary.
68 * ------------------------------------------------------------
70 static void rs_stop(struct tty_struct
*tty
)
72 #ifdef SIMSERIAL_DEBUG
73 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
74 tty
->stopped
, tty
->hw_stopped
, tty
->flow_stopped
);
79 static void rs_start(struct tty_struct
*tty
)
81 #ifdef SIMSERIAL_DEBUG
82 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
83 tty
->stopped
, tty
->hw_stopped
, tty
->flow_stopped
);
87 static void receive_chars(struct tty_struct
*tty
)
90 static unsigned char seen_esc
= 0;
92 while ( (ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
)) ) {
93 if ( ch
== 27 && seen_esc
== 0 ) {
97 if ( seen_esc
==1 && ch
== 'O' ) {
100 } else if ( seen_esc
== 2 ) {
101 if ( ch
== 'P' ) /* F1 */
103 #ifdef CONFIG_MAGIC_SYSRQ
104 if ( ch
== 'S' ) { /* F4 */
106 ch
= ia64_ssc(0, 0, 0, 0,
118 if (tty_insert_flip_char(tty
, ch
, TTY_NORMAL
) == 0)
121 tty_flip_buffer_push(tty
);
125 * This is the serial driver's interrupt routine for a single port
127 static irqreturn_t
rs_interrupt_single(int irq
, void *dev_id
)
129 struct serial_state
*info
= dev_id
;
131 if (!info
->tport
.tty
) {
132 printk(KERN_INFO
"simrs_interrupt_single: info|tty=0 info=%p problem\n", info
);
136 * pretty simple in our case, because we only get interrupts
139 receive_chars(info
->tport
.tty
);
144 * -------------------------------------------------------------------
145 * Here ends the serial interrupt routines.
146 * -------------------------------------------------------------------
149 static int rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
151 struct serial_state
*info
= tty
->driver_data
;
154 if (!tty
|| !info
->xmit
.buf
)
157 local_irq_save(flags
);
158 if (CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) == 0) {
159 local_irq_restore(flags
);
162 info
->xmit
.buf
[info
->xmit
.head
] = ch
;
163 info
->xmit
.head
= (info
->xmit
.head
+ 1) & (SERIAL_XMIT_SIZE
-1);
164 local_irq_restore(flags
);
168 static void transmit_chars(struct tty_struct
*tty
, struct serial_state
*info
,
174 local_irq_save(flags
);
177 char c
= info
->x_char
;
179 console
->write(console
, &c
, 1);
187 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
||
189 #ifdef SIMSERIAL_DEBUG
190 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
191 info
->xmit
.head
, info
->xmit
.tail
, tty
->stopped
);
196 * We removed the loop and try to do it in to chunks. We need
197 * 2 operations maximum because it's a ring buffer.
199 * First from current to tail if possible.
200 * Then from the beginning of the buffer until necessary
203 count
= min(CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
),
204 SERIAL_XMIT_SIZE
- info
->xmit
.tail
);
205 console
->write(console
, info
->xmit
.buf
+info
->xmit
.tail
, count
);
207 info
->xmit
.tail
= (info
->xmit
.tail
+count
) & (SERIAL_XMIT_SIZE
-1);
210 * We have more at the beginning of the buffer
212 count
= CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
214 console
->write(console
, info
->xmit
.buf
, count
);
215 info
->xmit
.tail
+= count
;
218 local_irq_restore(flags
);
221 static void rs_flush_chars(struct tty_struct
*tty
)
223 struct serial_state
*info
= tty
->driver_data
;
225 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
|| tty
->hw_stopped
||
229 transmit_chars(tty
, info
, NULL
);
233 static int rs_write(struct tty_struct
* tty
,
234 const unsigned char *buf
, int count
)
236 struct serial_state
*info
= tty
->driver_data
;
240 if (!tty
|| !info
->xmit
.buf
|| !tmp_buf
) return 0;
242 local_irq_save(flags
);
244 c
= CIRC_SPACE_TO_END(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
250 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
251 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
252 (SERIAL_XMIT_SIZE
-1));
257 local_irq_restore(flags
);
259 * Hey, we transmit directly from here in our case
261 if (CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
)
262 && !tty
->stopped
&& !tty
->hw_stopped
) {
263 transmit_chars(tty
, info
, NULL
);
268 static int rs_write_room(struct tty_struct
*tty
)
270 struct serial_state
*info
= tty
->driver_data
;
272 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
275 static int rs_chars_in_buffer(struct tty_struct
*tty
)
277 struct serial_state
*info
= tty
->driver_data
;
279 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
282 static void rs_flush_buffer(struct tty_struct
*tty
)
284 struct serial_state
*info
= tty
->driver_data
;
287 local_irq_save(flags
);
288 info
->xmit
.head
= info
->xmit
.tail
= 0;
289 local_irq_restore(flags
);
295 * This function is used to send a high-priority XON/XOFF character to
298 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
300 struct serial_state
*info
= tty
->driver_data
;
305 * I guess we could call console->write() directly but
306 * let's do that for now.
308 transmit_chars(tty
, info
, NULL
);
313 * ------------------------------------------------------------
316 * This routine is called by the upper-layer tty layer to signal that
317 * incoming characters should be throttled.
318 * ------------------------------------------------------------
320 static void rs_throttle(struct tty_struct
* tty
)
322 if (I_IXOFF(tty
)) rs_send_xchar(tty
, STOP_CHAR(tty
));
324 printk(KERN_INFO
"simrs_throttle called\n");
327 static void rs_unthrottle(struct tty_struct
* tty
)
329 struct serial_state
*info
= tty
->driver_data
;
335 rs_send_xchar(tty
, START_CHAR(tty
));
337 printk(KERN_INFO
"simrs_unthrottle called\n");
341 static int rs_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
343 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
344 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
345 (cmd
!= TIOCMIWAIT
)) {
346 if (tty
->flags
& (1 << TTY_IO_ERROR
))
352 printk(KERN_INFO
"simrs_ioctl TIOCGSERIAL called\n");
355 printk(KERN_INFO
"simrs_ioctl TIOCSSERIAL called\n");
358 printk(KERN_INFO
"rs_ioctl: TIOCSERCONFIG called\n");
361 case TIOCSERGETLSR
: /* Get line status register */
362 printk(KERN_INFO
"rs_ioctl: TIOCSERGETLSR called\n");
366 printk(KERN_INFO
"rs_ioctl: TIOCSERGSTRUCT called\n");
368 if (copy_to_user((struct async_struct
*) arg
,
369 info
, sizeof(struct async_struct
)))
375 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
376 * - mask passed in arg for lines of interest
377 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
378 * Caller should use TIOCGICOUNT to see which one it was
381 printk(KERN_INFO
"rs_ioctl: TIOCMIWAIT: called\n");
385 /* "setserial -W" is called in Debian boot */
386 printk (KERN_INFO
"TIOCSER?WILD ioctl obsolete, ignored.\n");
395 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
397 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
399 /* Handle turning off CRTSCTS */
400 if ((old_termios
->c_cflag
& CRTSCTS
) &&
401 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
407 * This routine will shutdown a serial port; interrupts are disabled, and
408 * DTR is dropped if the hangup on close termio flag is on.
410 static void shutdown(struct tty_struct
*tty
, struct serial_state
*info
)
414 if (!(info
->tport
.flags
& ASYNC_INITIALIZED
))
417 #ifdef SIMSERIAL_DEBUG
418 printk("Shutting down serial port %d (irq %d)...\n", info
->line
,
422 local_irq_save(flags
);
425 free_irq(info
->irq
, info
);
427 if (info
->xmit
.buf
) {
428 free_page((unsigned long) info
->xmit
.buf
);
429 info
->xmit
.buf
= NULL
;
432 set_bit(TTY_IO_ERROR
, &tty
->flags
);
434 info
->tport
.flags
&= ~ASYNC_INITIALIZED
;
436 local_irq_restore(flags
);
440 * ------------------------------------------------------------
443 * This routine is called when the serial port gets closed. First, we
444 * wait for the last remaining data to be sent. Then, we unlink its
445 * async structure from the interrupt chain if necessary, and we free
446 * that IRQ if nothing is left in the chain.
447 * ------------------------------------------------------------
449 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
451 struct serial_state
*info
= tty
->driver_data
;
457 local_irq_save(flags
);
458 if (tty_hung_up_p(filp
)) {
459 #ifdef SIMSERIAL_DEBUG
460 printk("rs_close: hung_up\n");
462 local_irq_restore(flags
);
465 #ifdef SIMSERIAL_DEBUG
466 printk("rs_close ttys%d, count = %d\n", info
->line
, info
->tport
.count
);
468 if ((tty
->count
== 1) && (info
->tport
.count
!= 1)) {
470 * Uh, oh. tty->count is 1, which means that the tty
471 * structure will be freed. info->tport.count should always
472 * be one in these conditions. If it's greater than
473 * one, we've got real problems, since it means the
474 * serial port won't be shutdown.
476 printk(KERN_ERR
"rs_close: bad serial port count; tty->count is 1, "
477 "info->tport.count is %d\n", info
->tport
.count
);
478 info
->tport
.count
= 1;
480 if (--info
->tport
.count
< 0) {
481 printk(KERN_ERR
"rs_close: bad serial port count for ttys%d: %d\n",
482 info
->line
, info
->tport
.count
);
483 info
->tport
.count
= 0;
485 if (info
->tport
.count
) {
486 local_irq_restore(flags
);
489 info
->tport
.flags
|= ASYNC_CLOSING
;
490 local_irq_restore(flags
);
493 * Now we wait for the transmit buffer to clear; and we notify
494 * the line discipline to only process XON/XOFF characters.
497 rs_flush_buffer(tty
);
498 tty_ldisc_flush(tty
);
499 info
->tport
.tty
= NULL
;
500 if (info
->tport
.blocked_open
) {
501 if (info
->tport
.close_delay
)
502 schedule_timeout_interruptible(info
->tport
.close_delay
);
503 wake_up_interruptible(&info
->tport
.open_wait
);
505 info
->tport
.flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
506 wake_up_interruptible(&info
->tport
.close_wait
);
510 * rs_wait_until_sent() --- wait until the transmitter is empty
512 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
518 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
520 static void rs_hangup(struct tty_struct
*tty
)
522 struct serial_state
*info
= tty
->driver_data
;
524 #ifdef SIMSERIAL_DEBUG
525 printk("rs_hangup: called\n");
528 rs_flush_buffer(tty
);
529 if (info
->tport
.flags
& ASYNC_CLOSING
)
533 info
->tport
.count
= 0;
534 info
->tport
.flags
&= ~ASYNC_NORMAL_ACTIVE
;
535 info
->tport
.tty
= NULL
;
536 wake_up_interruptible(&info
->tport
.open_wait
);
540 static int startup(struct tty_struct
*tty
, struct serial_state
*state
)
542 struct tty_port
*port
= &state
->tport
;
547 page
= get_zeroed_page(GFP_KERNEL
);
551 local_irq_save(flags
);
553 if (port
->flags
& ASYNC_INITIALIZED
) {
561 state
->xmit
.buf
= (unsigned char *) page
;
563 #ifdef SIMSERIAL_DEBUG
564 printk("startup: ttys%d (irq %d)...", state
->line
, state
->irq
);
568 * Allocate the IRQ if necessary
571 retval
= request_irq(state
->irq
, rs_interrupt_single
, 0,
577 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
579 state
->xmit
.head
= state
->xmit
.tail
= 0;
583 * Set up serial timers...
585 timer_table
[RS_TIMER
].expires
= jiffies
+ 2*HZ
/100;
586 timer_active
|= 1 << RS_TIMER
;
590 * Set up the tty->alt_speed kludge
592 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
593 tty
->alt_speed
= 57600;
594 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
595 tty
->alt_speed
= 115200;
596 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
597 tty
->alt_speed
= 230400;
598 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
599 tty
->alt_speed
= 460800;
601 port
->flags
|= ASYNC_INITIALIZED
;
602 local_irq_restore(flags
);
606 local_irq_restore(flags
);
612 * This routine is called whenever a serial port is opened. It
613 * enables interrupts for a serial port, linking in its async structure into
614 * the IRQ chain. It also performs the serial-specific
615 * initialization for the tty structure.
617 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
619 struct serial_state
*info
= rs_table
+ tty
->index
;
624 info
->tport
.tty
= tty
;
625 tty
->driver_data
= info
;
626 tty
->port
= &info
->tport
;
628 #ifdef SIMSERIAL_DEBUG
629 printk("rs_open %s, count = %d\n", tty
->name
, info
->tport
.count
);
631 tty
->low_latency
= (info
->tport
.flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
634 page
= get_zeroed_page(GFP_KERNEL
);
640 tmp_buf
= (unsigned char *) page
;
644 * If the port is the middle of closing, bail out now
646 if (tty_hung_up_p(filp
) || (info
->tport
.flags
& ASYNC_CLOSING
)) {
647 if (info
->tport
.flags
& ASYNC_CLOSING
)
648 interruptible_sleep_on(&info
->tport
.close_wait
);
649 #ifdef SERIAL_DO_RESTART
650 return ((info
->tport
.flags
& ASYNC_HUP_NOTIFY
) ?
651 -EAGAIN
: -ERESTARTSYS
);
658 * Start up serial port
660 retval
= startup(tty
, info
);
666 * figure out which console to use (should be one already)
668 console
= console_drivers
;
670 if ((console
->flags
& CON_ENABLED
) && console
->write
) break;
671 console
= console
->next
;
674 #ifdef SIMSERIAL_DEBUG
675 printk("rs_open ttys%d successful\n", info
->line
);
681 * /proc fs routines....
684 static inline void line_info(struct seq_file
*m
, struct serial_state
*state
)
686 seq_printf(m
, "%d: uart:16550 port:3F8 irq:%d\n",
687 state
->line
, state
->irq
);
690 static int rs_proc_show(struct seq_file
*m
, void *v
)
694 seq_printf(m
, "simserinfo:1.0 driver:%s\n", serial_version
);
695 for (i
= 0; i
< NR_PORTS
; i
++)
696 line_info(m
, &rs_table
[i
]);
700 static int rs_proc_open(struct inode
*inode
, struct file
*file
)
702 return single_open(file
, rs_proc_show
, NULL
);
705 static const struct file_operations rs_proc_fops
= {
706 .owner
= THIS_MODULE
,
707 .open
= rs_proc_open
,
710 .release
= single_release
,
714 * ---------------------------------------------------------------------
715 * rs_init() and friends
717 * rs_init() is called at boot-time to initialize the serial driver.
718 * ---------------------------------------------------------------------
722 * This routine prints out the appropriate serial driver version
723 * number, and identifies which options were configured into this
726 static inline void show_serial_version(void)
728 printk(KERN_INFO
"%s version %s with", serial_name
, serial_version
);
729 printk(KERN_INFO
" no serial options enabled\n");
732 static const struct tty_operations hp_ops
= {
736 .put_char
= rs_put_char
,
737 .flush_chars
= rs_flush_chars
,
738 .write_room
= rs_write_room
,
739 .chars_in_buffer
= rs_chars_in_buffer
,
740 .flush_buffer
= rs_flush_buffer
,
742 .throttle
= rs_throttle
,
743 .unthrottle
= rs_unthrottle
,
744 .send_xchar
= rs_send_xchar
,
745 .set_termios
= rs_set_termios
,
749 .wait_until_sent
= rs_wait_until_sent
,
750 .proc_fops
= &rs_proc_fops
,
754 * The serial driver boot-time initialization code!
756 static int __init
simrs_init(void)
758 struct serial_state
*state
;
761 if (!ia64_platform_is("hpsim"))
764 hp_simserial_driver
= alloc_tty_driver(NR_PORTS
);
765 if (!hp_simserial_driver
)
768 show_serial_version();
770 /* Initialize the tty_driver structure */
772 hp_simserial_driver
->driver_name
= "simserial";
773 hp_simserial_driver
->name
= "ttyS";
774 hp_simserial_driver
->major
= TTY_MAJOR
;
775 hp_simserial_driver
->minor_start
= 64;
776 hp_simserial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
777 hp_simserial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
778 hp_simserial_driver
->init_termios
= tty_std_termios
;
779 hp_simserial_driver
->init_termios
.c_cflag
=
780 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
781 hp_simserial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
782 tty_set_operations(hp_simserial_driver
, &hp_ops
);
785 * Let's have a little bit of fun !
788 tty_port_init(&state
->tport
);
789 state
->tport
.close_delay
= 0; /* XXX really 0? */
791 retval
= hpsim_get_irq(KEYBOARD_INTR
);
793 printk(KERN_ERR
"%s: out of interrupt vectors!\n",
800 /* the port is imaginary */
801 printk(KERN_INFO
"ttyS%d at 0x03f8 (irq = %d) is a 16550\n",
802 state
->line
, state
->irq
);
804 retval
= tty_register_driver(hp_simserial_driver
);
806 printk(KERN_ERR
"Couldn't register simserial driver\n");
812 put_tty_driver(hp_simserial_driver
);
817 __initcall(simrs_init
);