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/module.h>
28 #include <linux/serial.h>
29 #include <linux/sysrq.h>
32 #include <asm/hpsim.h>
33 #include <asm/hw_irq.h>
34 #include <asm/uaccess.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 char *serial_name = "SimSerial driver";
52 static char *serial_version = "0.6";
54 static struct serial_state rs_table[NR_PORTS];
56 struct tty_driver *hp_simserial_driver;
58 static struct console *console;
60 extern struct console *console_drivers; /* from kernel/printk.c */
62 static void receive_chars(struct tty_struct *tty)
65 static unsigned char seen_esc = 0;
67 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
68 if ( ch == 27 && seen_esc == 0 ) {
72 if ( seen_esc==1 && ch == 'O' ) {
75 } else if ( seen_esc == 2 ) {
76 if ( ch == 'P' ) /* F1 */
78 #ifdef CONFIG_MAGIC_SYSRQ
79 if ( ch == 'S' ) { /* F4 */
81 ch = ia64_ssc(0, 0, 0, 0,
93 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
96 tty_flip_buffer_push(tty);
100 * This is the serial driver's interrupt routine for a single port
102 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
104 struct serial_state *info = dev_id;
105 struct tty_struct *tty = tty_port_tty_get(&info->port);
108 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
112 * pretty simple in our case, because we only get interrupts
121 * -------------------------------------------------------------------
122 * Here ends the serial interrupt routines.
123 * -------------------------------------------------------------------
126 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
128 struct serial_state *info = tty->driver_data;
131 if (!tty || !info->xmit.buf)
134 local_irq_save(flags);
135 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
136 local_irq_restore(flags);
139 info->xmit.buf[info->xmit.head] = ch;
140 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
141 local_irq_restore(flags);
145 static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
151 local_irq_save(flags);
154 char c = info->x_char;
156 console->write(console, &c, 1);
163 if (info->xmit.head == info->xmit.tail || tty->stopped ||
165 #ifdef SIMSERIAL_DEBUG
166 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
167 info->xmit.head, info->xmit.tail, tty->stopped);
172 * We removed the loop and try to do it in to chunks. We need
173 * 2 operations maximum because it's a ring buffer.
175 * First from current to tail if possible.
176 * Then from the beginning of the buffer until necessary
179 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
180 SERIAL_XMIT_SIZE - info->xmit.tail);
181 console->write(console, info->xmit.buf+info->xmit.tail, count);
183 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
186 * We have more at the beginning of the buffer
188 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
190 console->write(console, info->xmit.buf, count);
191 info->xmit.tail += count;
194 local_irq_restore(flags);
197 static void rs_flush_chars(struct tty_struct *tty)
199 struct serial_state *info = tty->driver_data;
201 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
205 transmit_chars(tty, info, NULL);
209 static int rs_write(struct tty_struct * tty,
210 const unsigned char *buf, int count)
212 struct serial_state *info = tty->driver_data;
216 if (!tty || !info->xmit.buf)
219 local_irq_save(flags);
221 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
227 memcpy(info->xmit.buf + info->xmit.head, buf, c);
228 info->xmit.head = ((info->xmit.head + c) &
229 (SERIAL_XMIT_SIZE-1));
234 local_irq_restore(flags);
236 * Hey, we transmit directly from here in our case
238 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
239 && !tty->stopped && !tty->hw_stopped) {
240 transmit_chars(tty, info, NULL);
245 static int rs_write_room(struct tty_struct *tty)
247 struct serial_state *info = tty->driver_data;
249 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
252 static int rs_chars_in_buffer(struct tty_struct *tty)
254 struct serial_state *info = tty->driver_data;
256 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
259 static void rs_flush_buffer(struct tty_struct *tty)
261 struct serial_state *info = tty->driver_data;
264 local_irq_save(flags);
265 info->xmit.head = info->xmit.tail = 0;
266 local_irq_restore(flags);
272 * This function is used to send a high-priority XON/XOFF character to
275 static void rs_send_xchar(struct tty_struct *tty, char ch)
277 struct serial_state *info = tty->driver_data;
282 * I guess we could call console->write() directly but
283 * let's do that for now.
285 transmit_chars(tty, info, NULL);
290 * ------------------------------------------------------------
293 * This routine is called by the upper-layer tty layer to signal that
294 * incoming characters should be throttled.
295 * ------------------------------------------------------------
297 static void rs_throttle(struct tty_struct * tty)
299 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
301 printk(KERN_INFO "simrs_throttle called\n");
304 static void rs_unthrottle(struct tty_struct * tty)
306 struct serial_state *info = tty->driver_data;
312 rs_send_xchar(tty, START_CHAR(tty));
314 printk(KERN_INFO "simrs_unthrottle called\n");
318 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
320 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
321 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
322 (cmd != TIOCMIWAIT)) {
323 if (tty->flags & (1 << TTY_IO_ERROR))
329 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
332 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
335 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
338 case TIOCSERGETLSR: /* Get line status register */
339 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
343 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
345 if (copy_to_user((struct async_struct *) arg,
346 info, sizeof(struct async_struct)))
352 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
353 * - mask passed in arg for lines of interest
354 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
355 * Caller should use TIOCGICOUNT to see which one it was
358 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
362 /* "setserial -W" is called in Debian boot */
363 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
372 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
374 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
376 /* Handle turning off CRTSCTS */
377 if ((old_termios->c_cflag & CRTSCTS) &&
378 !(tty->termios->c_cflag & CRTSCTS)) {
383 * This routine will shutdown a serial port; interrupts are disabled, and
384 * DTR is dropped if the hangup on close termio flag is on.
386 static void shutdown(struct tty_port *port)
388 struct serial_state *info = container_of(port, struct serial_state,
392 local_irq_save(flags);
395 free_irq(info->irq, info);
397 if (info->xmit.buf) {
398 free_page((unsigned long) info->xmit.buf);
399 info->xmit.buf = NULL;
402 local_irq_restore(flags);
405 static void rs_close(struct tty_struct *tty, struct file * filp)
407 struct serial_state *info = tty->driver_data;
409 tty_port_close(&info->port, tty, filp);
412 static void rs_hangup(struct tty_struct *tty)
414 struct serial_state *info = tty->driver_data;
416 rs_flush_buffer(tty);
417 tty_port_hangup(&info->port);
420 static int activate(struct tty_port *port, struct tty_struct *tty)
422 struct serial_state *state = container_of(port, struct serial_state,
428 page = get_zeroed_page(GFP_KERNEL);
432 local_irq_save(flags);
437 state->xmit.buf = (unsigned char *) page;
440 retval = request_irq(state->irq, rs_interrupt_single, 0,
446 state->xmit.head = state->xmit.tail = 0;
449 * Set up the tty->alt_speed kludge
451 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
452 tty->alt_speed = 57600;
453 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
454 tty->alt_speed = 115200;
455 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
456 tty->alt_speed = 230400;
457 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
458 tty->alt_speed = 460800;
461 local_irq_restore(flags);
467 * This routine is called whenever a serial port is opened. It
468 * enables interrupts for a serial port, linking in its async structure into
469 * the IRQ chain. It also performs the serial-specific
470 * initialization for the tty structure.
472 static int rs_open(struct tty_struct *tty, struct file * filp)
474 struct serial_state *info = rs_table + tty->index;
475 struct tty_port *port = &info->port;
477 tty->driver_data = info;
478 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
481 * figure out which console to use (should be one already)
483 console = console_drivers;
485 if ((console->flags & CON_ENABLED) && console->write) break;
486 console = console->next;
489 return tty_port_open(port, tty, filp);
493 * /proc fs routines....
496 static int rs_proc_show(struct seq_file *m, void *v)
500 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
501 for (i = 0; i < NR_PORTS; i++)
502 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
507 static int rs_proc_open(struct inode *inode, struct file *file)
509 return single_open(file, rs_proc_show, NULL);
512 static const struct file_operations rs_proc_fops = {
513 .owner = THIS_MODULE,
514 .open = rs_proc_open,
517 .release = single_release,
520 static inline void show_serial_version(void)
522 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
523 printk(KERN_INFO " no serial options enabled\n");
526 static const struct tty_operations hp_ops = {
530 .put_char = rs_put_char,
531 .flush_chars = rs_flush_chars,
532 .write_room = rs_write_room,
533 .chars_in_buffer = rs_chars_in_buffer,
534 .flush_buffer = rs_flush_buffer,
536 .throttle = rs_throttle,
537 .unthrottle = rs_unthrottle,
538 .send_xchar = rs_send_xchar,
539 .set_termios = rs_set_termios,
541 .proc_fops = &rs_proc_fops,
544 static const struct tty_port_operations hp_port_ops = {
545 .activate = activate,
546 .shutdown = shutdown,
549 static int __init simrs_init(void)
551 struct serial_state *state;
554 if (!ia64_platform_is("hpsim"))
557 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
558 if (!hp_simserial_driver)
561 show_serial_version();
563 /* Initialize the tty_driver structure */
565 hp_simserial_driver->driver_name = "simserial";
566 hp_simserial_driver->name = "ttyS";
567 hp_simserial_driver->major = TTY_MAJOR;
568 hp_simserial_driver->minor_start = 64;
569 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
570 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
571 hp_simserial_driver->init_termios = tty_std_termios;
572 hp_simserial_driver->init_termios.c_cflag =
573 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
574 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
575 tty_set_operations(hp_simserial_driver, &hp_ops);
578 tty_port_init(&state->port);
579 state->port.ops = &hp_port_ops;
580 state->port.close_delay = 0; /* XXX really 0? */
582 retval = hpsim_get_irq(KEYBOARD_INTR);
584 printk(KERN_ERR "%s: out of interrupt vectors!\n",
591 /* the port is imaginary */
592 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
594 retval = tty_register_driver(hp_simserial_driver);
596 printk(KERN_ERR "Couldn't register simserial driver\n");
602 put_tty_driver(hp_simserial_driver);
607 __initcall(simrs_init);