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_struct *tty)
58 struct tty_port *port = tty->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(tty);
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;
97 struct tty_struct *tty = tty_port_tty_get(&info->port);
100 printk(KERN_INFO "%s: tty=0 problem\n", __func__);
104 * pretty simple in our case, because we only get interrupts
113 * -------------------------------------------------------------------
114 * Here ends the serial interrupt routines.
115 * -------------------------------------------------------------------
118 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
120 struct serial_state *info = tty->driver_data;
126 local_irq_save(flags);
127 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
128 local_irq_restore(flags);
131 info->xmit.buf[info->xmit.head] = ch;
132 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
133 local_irq_restore(flags);
137 static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
143 local_irq_save(flags);
146 char c = info->x_char;
148 console->write(console, &c, 1);
155 if (info->xmit.head == info->xmit.tail || tty->stopped ||
157 #ifdef SIMSERIAL_DEBUG
158 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
159 info->xmit.head, info->xmit.tail, tty->stopped);
164 * We removed the loop and try to do it in to chunks. We need
165 * 2 operations maximum because it's a ring buffer.
167 * First from current to tail if possible.
168 * Then from the beginning of the buffer until necessary
171 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
172 SERIAL_XMIT_SIZE - info->xmit.tail);
173 console->write(console, info->xmit.buf+info->xmit.tail, count);
175 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
178 * We have more at the beginning of the buffer
180 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
182 console->write(console, info->xmit.buf, count);
183 info->xmit.tail += count;
186 local_irq_restore(flags);
189 static void rs_flush_chars(struct tty_struct *tty)
191 struct serial_state *info = tty->driver_data;
193 if (info->xmit.head == info->xmit.tail || tty->stopped ||
194 tty->hw_stopped || !info->xmit.buf)
197 transmit_chars(tty, info, NULL);
200 static int rs_write(struct tty_struct * tty,
201 const unsigned char *buf, int count)
203 struct serial_state *info = tty->driver_data;
210 local_irq_save(flags);
212 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
218 memcpy(info->xmit.buf + info->xmit.head, buf, c);
219 info->xmit.head = ((info->xmit.head + c) &
220 (SERIAL_XMIT_SIZE-1));
225 local_irq_restore(flags);
227 * Hey, we transmit directly from here in our case
229 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
230 !tty->stopped && !tty->hw_stopped)
231 transmit_chars(tty, info, NULL);
236 static int rs_write_room(struct tty_struct *tty)
238 struct serial_state *info = tty->driver_data;
240 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
243 static int rs_chars_in_buffer(struct tty_struct *tty)
245 struct serial_state *info = tty->driver_data;
247 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
250 static void rs_flush_buffer(struct tty_struct *tty)
252 struct serial_state *info = tty->driver_data;
255 local_irq_save(flags);
256 info->xmit.head = info->xmit.tail = 0;
257 local_irq_restore(flags);
263 * This function is used to send a high-priority XON/XOFF character to
266 static void rs_send_xchar(struct tty_struct *tty, char ch)
268 struct serial_state *info = tty->driver_data;
273 * I guess we could call console->write() directly but
274 * let's do that for now.
276 transmit_chars(tty, info, NULL);
281 * ------------------------------------------------------------
284 * This routine is called by the upper-layer tty layer to signal that
285 * incoming characters should be throttled.
286 * ------------------------------------------------------------
288 static void rs_throttle(struct tty_struct * tty)
291 rs_send_xchar(tty, STOP_CHAR(tty));
293 printk(KERN_INFO "simrs_throttle called\n");
296 static void rs_unthrottle(struct tty_struct * tty)
298 struct serial_state *info = tty->driver_data;
304 rs_send_xchar(tty, START_CHAR(tty));
306 printk(KERN_INFO "simrs_unthrottle called\n");
309 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
311 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
312 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
313 (cmd != TIOCMIWAIT)) {
314 if (tty->flags & (1 << TTY_IO_ERROR))
325 case TIOCSERGETLSR: /* Get line status register */
329 /* "setserial -W" is called in Debian boot */
330 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
336 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
338 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
340 /* Handle turning off CRTSCTS */
341 if ((old_termios->c_cflag & CRTSCTS) &&
342 !(tty->termios.c_cflag & CRTSCTS)) {
347 * This routine will shutdown a serial port; interrupts are disabled, and
348 * DTR is dropped if the hangup on close termio flag is on.
350 static void shutdown(struct tty_port *port)
352 struct serial_state *info = container_of(port, struct serial_state,
356 local_irq_save(flags);
358 free_irq(info->irq, info);
360 if (info->xmit.buf) {
361 free_page((unsigned long) info->xmit.buf);
362 info->xmit.buf = NULL;
364 local_irq_restore(flags);
367 static void rs_close(struct tty_struct *tty, struct file * filp)
369 struct serial_state *info = tty->driver_data;
371 tty_port_close(&info->port, tty, filp);
374 static void rs_hangup(struct tty_struct *tty)
376 struct serial_state *info = tty->driver_data;
378 rs_flush_buffer(tty);
379 tty_port_hangup(&info->port);
382 static int activate(struct tty_port *port, struct tty_struct *tty)
384 struct serial_state *state = container_of(port, struct serial_state,
386 unsigned long flags, page;
389 page = get_zeroed_page(GFP_KERNEL);
393 local_irq_save(flags);
398 state->xmit.buf = (unsigned char *) page;
401 retval = request_irq(state->irq, rs_interrupt_single, 0,
407 state->xmit.head = state->xmit.tail = 0;
410 * Set up the tty->alt_speed kludge
412 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
413 tty->alt_speed = 57600;
414 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
415 tty->alt_speed = 115200;
416 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
417 tty->alt_speed = 230400;
418 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
419 tty->alt_speed = 460800;
422 local_irq_restore(flags);
428 * This routine is called whenever a serial port is opened. It
429 * enables interrupts for a serial port, linking in its async structure into
430 * the IRQ chain. It also performs the serial-specific
431 * initialization for the tty structure.
433 static int rs_open(struct tty_struct *tty, struct file * filp)
435 struct serial_state *info = rs_table + tty->index;
436 struct tty_port *port = &info->port;
438 tty->driver_data = info;
439 port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
442 * figure out which console to use (should be one already)
444 console = console_drivers;
446 if ((console->flags & CON_ENABLED) && console->write) break;
447 console = console->next;
450 return tty_port_open(port, tty, filp);
454 * /proc fs routines....
457 static int rs_proc_show(struct seq_file *m, void *v)
461 seq_printf(m, "simserinfo:1.0\n");
462 for (i = 0; i < NR_PORTS; i++)
463 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
468 static int rs_proc_open(struct inode *inode, struct file *file)
470 return single_open(file, rs_proc_show, NULL);
473 static const struct file_operations rs_proc_fops = {
474 .owner = THIS_MODULE,
475 .open = rs_proc_open,
478 .release = single_release,
481 static const struct tty_operations hp_ops = {
485 .put_char = rs_put_char,
486 .flush_chars = rs_flush_chars,
487 .write_room = rs_write_room,
488 .chars_in_buffer = rs_chars_in_buffer,
489 .flush_buffer = rs_flush_buffer,
491 .throttle = rs_throttle,
492 .unthrottle = rs_unthrottle,
493 .send_xchar = rs_send_xchar,
494 .set_termios = rs_set_termios,
496 .proc_fops = &rs_proc_fops,
499 static const struct tty_port_operations hp_port_ops = {
500 .activate = activate,
501 .shutdown = shutdown,
504 static int __init simrs_init(void)
506 struct serial_state *state;
509 if (!ia64_platform_is("hpsim"))
512 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
513 if (!hp_simserial_driver)
516 printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
518 /* Initialize the tty_driver structure */
520 hp_simserial_driver->driver_name = "simserial";
521 hp_simserial_driver->name = "ttyS";
522 hp_simserial_driver->major = TTY_MAJOR;
523 hp_simserial_driver->minor_start = 64;
524 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
525 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
526 hp_simserial_driver->init_termios = tty_std_termios;
527 hp_simserial_driver->init_termios.c_cflag =
528 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
529 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
530 tty_set_operations(hp_simserial_driver, &hp_ops);
533 tty_port_init(&state->port);
534 state->port.ops = &hp_port_ops;
535 state->port.close_delay = 0; /* XXX really 0? */
537 retval = hpsim_get_irq(KEYBOARD_INTR);
539 printk(KERN_ERR "%s: out of interrupt vectors!\n",
546 /* the port is imaginary */
547 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
549 tty_port_link_device(&state->port, hp_simserial_driver, 0);
550 retval = tty_register_driver(hp_simserial_driver);
552 printk(KERN_ERR "Couldn't register simserial driver\n");
558 put_tty_driver(hp_simserial_driver);
559 tty_port_destroy(&state->port);
564 __initcall(simrs_init);