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/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/major.h>
20 #include <linux/fcntl.h>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/capability.h>
25 #include <linux/circ_buf.h>
26 #include <linux/console.h>
27 #include <linux/irq.h>
28 #include <linux/module.h>
29 #include <linux/serial.h>
30 #include <linux/sysrq.h>
31 #include <linux/uaccess.h>
33 #include <asm/hpsim.h>
35 #include "hpsim_ssc.h"
37 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
39 #define KEYBOARD_INTR 3 /* must match with simulator! */
41 #define NR_PORTS 1 /* only one port for now */
50 static struct serial_state rs_table
[NR_PORTS
];
52 struct tty_driver
*hp_simserial_driver
;
54 static struct console
*console
;
56 static void receive_chars(struct tty_port
*port
)
59 static unsigned char seen_esc
= 0;
61 while ( (ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
)) ) {
62 if (ch
== 27 && seen_esc
== 0) {
65 } else if (seen_esc
== 1 && ch
== 'O') {
68 } else if (seen_esc
== 2) {
69 if (ch
== 'P') /* F1 */
71 #ifdef CONFIG_MAGIC_SYSRQ
72 if (ch
== 'S') { /* F4 */
74 ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
);
84 if (tty_insert_flip_char(port
, ch
, TTY_NORMAL
) == 0)
87 tty_flip_buffer_push(port
);
91 * This is the serial driver's interrupt routine for a single port
93 static irqreturn_t
rs_interrupt_single(int irq
, void *dev_id
)
95 struct serial_state
*info
= dev_id
;
97 receive_chars(&info
->port
);
103 * -------------------------------------------------------------------
104 * Here ends the serial interrupt routines.
105 * -------------------------------------------------------------------
108 static int rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
110 struct serial_state
*info
= tty
->driver_data
;
116 local_irq_save(flags
);
117 if (CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) == 0) {
118 local_irq_restore(flags
);
121 info
->xmit
.buf
[info
->xmit
.head
] = ch
;
122 info
->xmit
.head
= (info
->xmit
.head
+ 1) & (SERIAL_XMIT_SIZE
-1);
123 local_irq_restore(flags
);
127 static void transmit_chars(struct tty_struct
*tty
, struct serial_state
*info
,
133 local_irq_save(flags
);
136 char c
= info
->x_char
;
138 console
->write(console
, &c
, 1);
145 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
) {
146 #ifdef SIMSERIAL_DEBUG
147 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
148 info
->xmit
.head
, info
->xmit
.tail
, tty
->stopped
);
153 * We removed the loop and try to do it in to chunks. We need
154 * 2 operations maximum because it's a ring buffer.
156 * First from current to tail if possible.
157 * Then from the beginning of the buffer until necessary
160 count
= min(CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
),
161 SERIAL_XMIT_SIZE
- info
->xmit
.tail
);
162 console
->write(console
, info
->xmit
.buf
+info
->xmit
.tail
, count
);
164 info
->xmit
.tail
= (info
->xmit
.tail
+count
) & (SERIAL_XMIT_SIZE
-1);
167 * We have more at the beginning of the buffer
169 count
= CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
171 console
->write(console
, info
->xmit
.buf
, count
);
172 info
->xmit
.tail
+= count
;
175 local_irq_restore(flags
);
178 static void rs_flush_chars(struct tty_struct
*tty
)
180 struct serial_state
*info
= tty
->driver_data
;
182 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
||
186 transmit_chars(tty
, info
, NULL
);
189 static int rs_write(struct tty_struct
* tty
,
190 const unsigned char *buf
, int count
)
192 struct serial_state
*info
= tty
->driver_data
;
199 local_irq_save(flags
);
201 c
= CIRC_SPACE_TO_END(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
207 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
208 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
209 (SERIAL_XMIT_SIZE
-1));
214 local_irq_restore(flags
);
216 * Hey, we transmit directly from here in our case
218 if (CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) &&
220 transmit_chars(tty
, info
, NULL
);
225 static int rs_write_room(struct tty_struct
*tty
)
227 struct serial_state
*info
= tty
->driver_data
;
229 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
232 static int rs_chars_in_buffer(struct tty_struct
*tty
)
234 struct serial_state
*info
= tty
->driver_data
;
236 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
239 static void rs_flush_buffer(struct tty_struct
*tty
)
241 struct serial_state
*info
= tty
->driver_data
;
244 local_irq_save(flags
);
245 info
->xmit
.head
= info
->xmit
.tail
= 0;
246 local_irq_restore(flags
);
252 * This function is used to send a high-priority XON/XOFF character to
255 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
257 struct serial_state
*info
= tty
->driver_data
;
262 * I guess we could call console->write() directly but
263 * let's do that for now.
265 transmit_chars(tty
, info
, NULL
);
270 * ------------------------------------------------------------
273 * This routine is called by the upper-layer tty layer to signal that
274 * incoming characters should be throttled.
275 * ------------------------------------------------------------
277 static void rs_throttle(struct tty_struct
* tty
)
280 rs_send_xchar(tty
, STOP_CHAR(tty
));
282 printk(KERN_INFO
"simrs_throttle called\n");
285 static void rs_unthrottle(struct tty_struct
* tty
)
287 struct serial_state
*info
= tty
->driver_data
;
293 rs_send_xchar(tty
, START_CHAR(tty
));
295 printk(KERN_INFO
"simrs_unthrottle called\n");
298 static int rs_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
300 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
301 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
302 (cmd
!= TIOCMIWAIT
)) {
303 if (tty
->flags
& (1 << TTY_IO_ERROR
))
314 case TIOCSERGETLSR
: /* Get line status register */
318 /* "setserial -W" is called in Debian boot */
319 printk (KERN_INFO
"TIOCSER?WILD ioctl obsolete, ignored.\n");
325 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
328 * This routine will shutdown a serial port; interrupts are disabled, and
329 * DTR is dropped if the hangup on close termio flag is on.
331 static void shutdown(struct tty_port
*port
)
333 struct serial_state
*info
= container_of(port
, struct serial_state
,
337 local_irq_save(flags
);
339 free_irq(info
->irq
, info
);
341 if (info
->xmit
.buf
) {
342 free_page((unsigned long) info
->xmit
.buf
);
343 info
->xmit
.buf
= NULL
;
345 local_irq_restore(flags
);
348 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
350 struct serial_state
*info
= tty
->driver_data
;
352 tty_port_close(&info
->port
, tty
, filp
);
355 static void rs_hangup(struct tty_struct
*tty
)
357 struct serial_state
*info
= tty
->driver_data
;
359 rs_flush_buffer(tty
);
360 tty_port_hangup(&info
->port
);
363 static int activate(struct tty_port
*port
, struct tty_struct
*tty
)
365 struct serial_state
*state
= container_of(port
, struct serial_state
,
367 unsigned long flags
, page
;
370 page
= get_zeroed_page(GFP_KERNEL
);
374 local_irq_save(flags
);
379 state
->xmit
.buf
= (unsigned char *) page
;
382 retval
= request_irq(state
->irq
, rs_interrupt_single
, 0,
388 state
->xmit
.head
= state
->xmit
.tail
= 0;
391 * Set up the tty->alt_speed kludge
393 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
394 tty
->alt_speed
= 57600;
395 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
396 tty
->alt_speed
= 115200;
397 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
398 tty
->alt_speed
= 230400;
399 if ((port
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
400 tty
->alt_speed
= 460800;
403 local_irq_restore(flags
);
409 * This routine is called whenever a serial port is opened. It
410 * enables interrupts for a serial port, linking in its async structure into
411 * the IRQ chain. It also performs the serial-specific
412 * initialization for the tty structure.
414 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
416 struct serial_state
*info
= rs_table
+ tty
->index
;
417 struct tty_port
*port
= &info
->port
;
419 tty
->driver_data
= info
;
420 port
->low_latency
= (port
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
423 * figure out which console to use (should be one already)
425 console
= console_drivers
;
427 if ((console
->flags
& CON_ENABLED
) && console
->write
) break;
428 console
= console
->next
;
431 return tty_port_open(port
, tty
, filp
);
435 * /proc fs routines....
438 static int rs_proc_show(struct seq_file
*m
, void *v
)
442 seq_printf(m
, "simserinfo:1.0\n");
443 for (i
= 0; i
< NR_PORTS
; i
++)
444 seq_printf(m
, "%d: uart:16550 port:3F8 irq:%d\n",
449 static int rs_proc_open(struct inode
*inode
, struct file
*file
)
451 return single_open(file
, rs_proc_show
, NULL
);
454 static const struct file_operations rs_proc_fops
= {
455 .owner
= THIS_MODULE
,
456 .open
= rs_proc_open
,
459 .release
= single_release
,
462 static const struct tty_operations hp_ops
= {
466 .put_char
= rs_put_char
,
467 .flush_chars
= rs_flush_chars
,
468 .write_room
= rs_write_room
,
469 .chars_in_buffer
= rs_chars_in_buffer
,
470 .flush_buffer
= rs_flush_buffer
,
472 .throttle
= rs_throttle
,
473 .unthrottle
= rs_unthrottle
,
474 .send_xchar
= rs_send_xchar
,
476 .proc_fops
= &rs_proc_fops
,
479 static const struct tty_port_operations hp_port_ops
= {
480 .activate
= activate
,
481 .shutdown
= shutdown
,
484 static int __init
simrs_init(void)
486 struct serial_state
*state
;
489 if (!ia64_platform_is("hpsim"))
492 hp_simserial_driver
= alloc_tty_driver(NR_PORTS
);
493 if (!hp_simserial_driver
)
496 printk(KERN_INFO
"SimSerial driver with no serial options enabled\n");
498 /* Initialize the tty_driver structure */
500 hp_simserial_driver
->driver_name
= "simserial";
501 hp_simserial_driver
->name
= "ttyS";
502 hp_simserial_driver
->major
= TTY_MAJOR
;
503 hp_simserial_driver
->minor_start
= 64;
504 hp_simserial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
505 hp_simserial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
506 hp_simserial_driver
->init_termios
= tty_std_termios
;
507 hp_simserial_driver
->init_termios
.c_cflag
=
508 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
509 hp_simserial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
510 tty_set_operations(hp_simserial_driver
, &hp_ops
);
513 tty_port_init(&state
->port
);
514 state
->port
.ops
= &hp_port_ops
;
515 state
->port
.close_delay
= 0; /* XXX really 0? */
517 retval
= hpsim_get_irq(KEYBOARD_INTR
);
519 printk(KERN_ERR
"%s: out of interrupt vectors!\n",
526 /* the port is imaginary */
527 printk(KERN_INFO
"ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state
->irq
);
529 tty_port_link_device(&state
->port
, hp_simserial_driver
, 0);
530 retval
= tty_register_driver(hp_simserial_driver
);
532 printk(KERN_ERR
"Couldn't register simserial driver\n");
538 put_tty_driver(hp_simserial_driver
);
539 tty_port_destroy(&state
->port
);
544 __initcall(simrs_init
);