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
10 * Stephane Eranian <eranian@hpl.hp.com>
11 * David Mosberger-Tang <davidm@hpl.hp.com>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/tty.h>
19 #include <linux/tty_flip.h>
20 #include <linux/major.h>
21 #include <linux/fcntl.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/capability.h>
26 #include <linux/circ_buf.h>
27 #include <linux/console.h>
28 #include <linux/irq.h>
29 #include <linux/module.h>
30 #include <linux/serial.h>
31 #include <linux/sysrq.h>
32 #include <linux/uaccess.h>
34 #include <asm/hpsim.h>
36 #include "hpsim_ssc.h"
38 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
40 #define KEYBOARD_INTR 3 /* must match with simulator! */
42 #define NR_PORTS 1 /* only one port for now */
51 static struct serial_state rs_table
[NR_PORTS
];
53 struct tty_driver
*hp_simserial_driver
;
55 static struct console
*console
;
57 static void receive_chars(struct tty_port
*port
)
60 static unsigned char seen_esc
= 0;
62 while ( (ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
)) ) {
63 if (ch
== 27 && seen_esc
== 0) {
66 } else if (seen_esc
== 1 && ch
== 'O') {
69 } else if (seen_esc
== 2) {
70 if (ch
== 'P') /* F1 */
72 #ifdef CONFIG_MAGIC_SYSRQ
73 if (ch
== 'S') { /* F4 */
75 ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
);
85 if (tty_insert_flip_char(port
, ch
, TTY_NORMAL
) == 0)
88 tty_flip_buffer_push(port
);
92 * This is the serial driver's interrupt routine for a single port
94 static irqreturn_t
rs_interrupt_single(int irq
, void *dev_id
)
96 struct serial_state
*info
= dev_id
;
98 receive_chars(&info
->port
);
104 * -------------------------------------------------------------------
105 * Here ends the serial interrupt routines.
106 * -------------------------------------------------------------------
109 static int rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
111 struct serial_state
*info
= tty
->driver_data
;
117 local_irq_save(flags
);
118 if (CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) == 0) {
119 local_irq_restore(flags
);
122 info
->xmit
.buf
[info
->xmit
.head
] = ch
;
123 info
->xmit
.head
= (info
->xmit
.head
+ 1) & (SERIAL_XMIT_SIZE
-1);
124 local_irq_restore(flags
);
128 static void transmit_chars(struct tty_struct
*tty
, struct serial_state
*info
,
134 local_irq_save(flags
);
137 char c
= info
->x_char
;
139 console
->write(console
, &c
, 1);
146 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
) {
147 #ifdef SIMSERIAL_DEBUG
148 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
149 info
->xmit
.head
, info
->xmit
.tail
, tty
->stopped
);
154 * We removed the loop and try to do it in to chunks. We need
155 * 2 operations maximum because it's a ring buffer.
157 * First from current to tail if possible.
158 * Then from the beginning of the buffer until necessary
161 count
= min(CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
),
162 SERIAL_XMIT_SIZE
- info
->xmit
.tail
);
163 console
->write(console
, info
->xmit
.buf
+info
->xmit
.tail
, count
);
165 info
->xmit
.tail
= (info
->xmit
.tail
+count
) & (SERIAL_XMIT_SIZE
-1);
168 * We have more at the beginning of the buffer
170 count
= CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
172 console
->write(console
, info
->xmit
.buf
, count
);
173 info
->xmit
.tail
+= count
;
176 local_irq_restore(flags
);
179 static void rs_flush_chars(struct tty_struct
*tty
)
181 struct serial_state
*info
= tty
->driver_data
;
183 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
||
187 transmit_chars(tty
, info
, NULL
);
190 static int rs_write(struct tty_struct
* tty
,
191 const unsigned char *buf
, int count
)
193 struct serial_state
*info
= tty
->driver_data
;
200 local_irq_save(flags
);
202 c
= CIRC_SPACE_TO_END(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
208 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
209 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
210 (SERIAL_XMIT_SIZE
-1));
215 local_irq_restore(flags
);
217 * Hey, we transmit directly from here in our case
219 if (CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) &&
221 transmit_chars(tty
, info
, NULL
);
226 static int rs_write_room(struct tty_struct
*tty
)
228 struct serial_state
*info
= tty
->driver_data
;
230 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
233 static int rs_chars_in_buffer(struct tty_struct
*tty
)
235 struct serial_state
*info
= tty
->driver_data
;
237 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
240 static void rs_flush_buffer(struct tty_struct
*tty
)
242 struct serial_state
*info
= tty
->driver_data
;
245 local_irq_save(flags
);
246 info
->xmit
.head
= info
->xmit
.tail
= 0;
247 local_irq_restore(flags
);
253 * This function is used to send a high-priority XON/XOFF character to
256 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
258 struct serial_state
*info
= tty
->driver_data
;
263 * I guess we could call console->write() directly but
264 * let's do that for now.
266 transmit_chars(tty
, info
, NULL
);
271 * ------------------------------------------------------------
274 * This routine is called by the upper-layer tty layer to signal that
275 * incoming characters should be throttled.
276 * ------------------------------------------------------------
278 static void rs_throttle(struct tty_struct
* tty
)
281 rs_send_xchar(tty
, STOP_CHAR(tty
));
283 printk(KERN_INFO
"simrs_throttle called\n");
286 static void rs_unthrottle(struct tty_struct
* tty
)
288 struct serial_state
*info
= tty
->driver_data
;
294 rs_send_xchar(tty
, START_CHAR(tty
));
296 printk(KERN_INFO
"simrs_unthrottle called\n");
299 static int rs_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
301 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
302 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
303 (cmd
!= TIOCMIWAIT
)) {
304 if (tty_io_error(tty
))
315 case TIOCSERGETLSR
: /* Get line status register */
319 /* "setserial -W" is called in Debian boot */
320 printk (KERN_INFO
"TIOCSER?WILD ioctl obsolete, ignored.\n");
326 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
329 * This routine will shutdown a serial port; interrupts are disabled, and
330 * DTR is dropped if the hangup on close termio flag is on.
332 static void shutdown(struct tty_port
*port
)
334 struct serial_state
*info
= container_of(port
, struct serial_state
,
338 local_irq_save(flags
);
340 free_irq(info
->irq
, info
);
342 if (info
->xmit
.buf
) {
343 free_page((unsigned long) info
->xmit
.buf
);
344 info
->xmit
.buf
= NULL
;
346 local_irq_restore(flags
);
349 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
351 struct serial_state
*info
= tty
->driver_data
;
353 tty_port_close(&info
->port
, tty
, filp
);
356 static void rs_hangup(struct tty_struct
*tty
)
358 struct serial_state
*info
= tty
->driver_data
;
360 rs_flush_buffer(tty
);
361 tty_port_hangup(&info
->port
);
364 static int activate(struct tty_port
*port
, struct tty_struct
*tty
)
366 struct serial_state
*state
= container_of(port
, struct serial_state
,
368 unsigned long flags
, page
;
371 page
= get_zeroed_page(GFP_KERNEL
);
375 local_irq_save(flags
);
380 state
->xmit
.buf
= (unsigned char *) page
;
383 retval
= request_irq(state
->irq
, rs_interrupt_single
, 0,
389 state
->xmit
.head
= state
->xmit
.tail
= 0;
391 local_irq_restore(flags
);
397 * This routine is called whenever a serial port is opened. It
398 * enables interrupts for a serial port, linking in its async structure into
399 * the IRQ chain. It also performs the serial-specific
400 * initialization for the tty structure.
402 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
404 struct serial_state
*info
= rs_table
+ tty
->index
;
405 struct tty_port
*port
= &info
->port
;
407 tty
->driver_data
= info
;
408 port
->low_latency
= (port
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
411 * figure out which console to use (should be one already)
413 console
= console_drivers
;
415 if ((console
->flags
& CON_ENABLED
) && console
->write
) break;
416 console
= console
->next
;
419 return tty_port_open(port
, tty
, filp
);
423 * /proc fs routines....
426 static int rs_proc_show(struct seq_file
*m
, void *v
)
430 seq_printf(m
, "simserinfo:1.0\n");
431 for (i
= 0; i
< NR_PORTS
; i
++)
432 seq_printf(m
, "%d: uart:16550 port:3F8 irq:%d\n",
437 static int rs_proc_open(struct inode
*inode
, struct file
*file
)
439 return single_open(file
, rs_proc_show
, NULL
);
442 static const struct file_operations rs_proc_fops
= {
443 .owner
= THIS_MODULE
,
444 .open
= rs_proc_open
,
447 .release
= single_release
,
450 static const struct tty_operations hp_ops
= {
454 .put_char
= rs_put_char
,
455 .flush_chars
= rs_flush_chars
,
456 .write_room
= rs_write_room
,
457 .chars_in_buffer
= rs_chars_in_buffer
,
458 .flush_buffer
= rs_flush_buffer
,
460 .throttle
= rs_throttle
,
461 .unthrottle
= rs_unthrottle
,
462 .send_xchar
= rs_send_xchar
,
464 .proc_fops
= &rs_proc_fops
,
467 static const struct tty_port_operations hp_port_ops
= {
468 .activate
= activate
,
469 .shutdown
= shutdown
,
472 static int __init
simrs_init(void)
474 struct serial_state
*state
;
477 if (!ia64_platform_is("hpsim"))
480 hp_simserial_driver
= alloc_tty_driver(NR_PORTS
);
481 if (!hp_simserial_driver
)
484 printk(KERN_INFO
"SimSerial driver with no serial options enabled\n");
486 /* Initialize the tty_driver structure */
488 hp_simserial_driver
->driver_name
= "simserial";
489 hp_simserial_driver
->name
= "ttyS";
490 hp_simserial_driver
->major
= TTY_MAJOR
;
491 hp_simserial_driver
->minor_start
= 64;
492 hp_simserial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
493 hp_simserial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
494 hp_simserial_driver
->init_termios
= tty_std_termios
;
495 hp_simserial_driver
->init_termios
.c_cflag
=
496 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
497 hp_simserial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
498 tty_set_operations(hp_simserial_driver
, &hp_ops
);
501 tty_port_init(&state
->port
);
502 state
->port
.ops
= &hp_port_ops
;
503 state
->port
.close_delay
= 0; /* XXX really 0? */
505 retval
= hpsim_get_irq(KEYBOARD_INTR
);
507 printk(KERN_ERR
"%s: out of interrupt vectors!\n",
514 /* the port is imaginary */
515 printk(KERN_INFO
"ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state
->irq
);
517 tty_port_link_device(&state
->port
, hp_simserial_driver
, 0);
518 retval
= tty_register_driver(hp_simserial_driver
);
520 printk(KERN_ERR
"Couldn't register simserial driver\n");
526 put_tty_driver(hp_simserial_driver
);
527 tty_port_destroy(&state
->port
);
532 __initcall(simrs_init
);