[IPV4]: Correct rp_filter help text.
[linux-2.6/verdex.git] / arch / ia64 / hp / sim / simserial.c
blob324ea7565e2c14f90df86a561e202eb0d63a4382
1 /*
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>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/capability.h>
29 #include <linux/console.h>
30 #include <linux/module.h>
31 #include <linux/serial.h>
32 #include <linux/serialP.h>
33 #include <linux/sysrq.h>
35 #include <asm/irq.h>
36 #include <asm/hw_irq.h>
37 #include <asm/uaccess.h>
39 #ifdef CONFIG_KDB
40 # include <linux/kdb.h>
41 #endif
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 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
51 #define SSC_GETCHAR 21
53 extern long ia64_ssc (long, long, long, long, int);
54 extern void ia64_ssc_connect_irq (long intr, long irq);
56 static char *serial_name = "SimSerial driver";
57 static char *serial_version = "0.6";
60 * This has been extracted from asm/serial.h. We need one eventually but
61 * I don't know exactly what we're going to put in it so just fake one
62 * for now.
64 #define BASE_BAUD ( 1843200 / 16 )
66 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
69 * Most of the values here are meaningless to this particular driver.
70 * However some values must be preserved for the code (leveraged from serial.c
71 * to work correctly).
72 * port must not be 0
73 * type must not be UNKNOWN
74 * So I picked arbitrary (guess from where?) values instead
76 static struct serial_state rs_table[NR_PORTS]={
77 /* UART CLK PORT IRQ FLAGS */
78 { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
82 * Just for the fun of it !
84 static struct serial_uart_config uart_config[] = {
85 { "unknown", 1, 0 },
86 { "8250", 1, 0 },
87 { "16450", 1, 0 },
88 { "16550", 1, 0 },
89 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
90 { "cirrus", 1, 0 },
91 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
92 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
93 UART_STARTECH },
94 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
95 { NULL, 0}
98 struct tty_driver *hp_simserial_driver;
100 static struct async_struct *IRQ_ports[NR_IRQS];
102 static struct console *console;
104 static unsigned char *tmp_buf;
106 extern struct console *console_drivers; /* from kernel/printk.c */
109 * ------------------------------------------------------------
110 * rs_stop() and rs_start()
112 * This routines are called before setting or resetting tty->stopped.
113 * They enable or disable transmitter interrupts, as necessary.
114 * ------------------------------------------------------------
116 static void rs_stop(struct tty_struct *tty)
118 #ifdef SIMSERIAL_DEBUG
119 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
120 tty->stopped, tty->hw_stopped, tty->flow_stopped);
121 #endif
125 static void rs_start(struct tty_struct *tty)
127 #ifdef SIMSERIAL_DEBUG
128 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
129 tty->stopped, tty->hw_stopped, tty->flow_stopped);
130 #endif
133 static void receive_chars(struct tty_struct *tty)
135 unsigned char ch;
136 static unsigned char seen_esc = 0;
138 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
139 if ( ch == 27 && seen_esc == 0 ) {
140 seen_esc = 1;
141 continue;
142 } else {
143 if ( seen_esc==1 && ch == 'O' ) {
144 seen_esc = 2;
145 continue;
146 } else if ( seen_esc == 2 ) {
147 if ( ch == 'P' ) /* F1 */
148 show_state();
149 #ifdef CONFIG_MAGIC_SYSRQ
150 if ( ch == 'S' ) { /* F4 */
152 ch = ia64_ssc(0, 0, 0, 0,
153 SSC_GETCHAR);
154 while (!ch);
155 handle_sysrq(ch, NULL);
157 #endif
158 seen_esc = 0;
159 continue;
162 seen_esc = 0;
164 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
165 break;
167 tty_flip_buffer_push(tty);
171 * This is the serial driver's interrupt routine for a single port
173 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
175 struct async_struct * info;
178 * I don't know exactly why they don't use the dev_id opaque data
179 * pointer instead of this extra lookup table
181 info = IRQ_ports[irq];
182 if (!info || !info->tty) {
183 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
184 return IRQ_NONE;
187 * pretty simple in our case, because we only get interrupts
188 * on inbound traffic
190 receive_chars(info->tty);
191 return IRQ_HANDLED;
195 * -------------------------------------------------------------------
196 * Here ends the serial interrupt routines.
197 * -------------------------------------------------------------------
200 #if 0
202 * not really used in our situation so keep them commented out for now
204 static DECLARE_TASK_QUEUE(tq_serial); /* used to be at the top of the file */
205 static void do_serial_bh(void)
207 run_task_queue(&tq_serial);
208 printk(KERN_ERR "do_serial_bh: called\n");
210 #endif
212 static void do_softint(struct work_struct *private_)
214 printk(KERN_ERR "simserial: do_softint called\n");
217 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
219 struct async_struct *info = (struct async_struct *)tty->driver_data;
220 unsigned long flags;
222 if (!tty || !info->xmit.buf) return;
224 local_irq_save(flags);
225 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
226 local_irq_restore(flags);
227 return;
229 info->xmit.buf[info->xmit.head] = ch;
230 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
231 local_irq_restore(flags);
234 static void transmit_chars(struct async_struct *info, int *intr_done)
236 int count;
237 unsigned long flags;
240 local_irq_save(flags);
242 if (info->x_char) {
243 char c = info->x_char;
245 console->write(console, &c, 1);
247 info->state->icount.tx++;
248 info->x_char = 0;
250 goto out;
253 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
254 #ifdef SIMSERIAL_DEBUG
255 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
256 info->xmit.head, info->xmit.tail, info->tty->stopped);
257 #endif
258 goto out;
261 * We removed the loop and try to do it in to chunks. We need
262 * 2 operations maximum because it's a ring buffer.
264 * First from current to tail if possible.
265 * Then from the beginning of the buffer until necessary
268 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
269 SERIAL_XMIT_SIZE - info->xmit.tail);
270 console->write(console, info->xmit.buf+info->xmit.tail, count);
272 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
275 * We have more at the beginning of the buffer
277 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
278 if (count) {
279 console->write(console, info->xmit.buf, count);
280 info->xmit.tail += count;
282 out:
283 local_irq_restore(flags);
286 static void rs_flush_chars(struct tty_struct *tty)
288 struct async_struct *info = (struct async_struct *)tty->driver_data;
290 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
291 !info->xmit.buf)
292 return;
294 transmit_chars(info, NULL);
298 static int rs_write(struct tty_struct * tty,
299 const unsigned char *buf, int count)
301 int c, ret = 0;
302 struct async_struct *info = (struct async_struct *)tty->driver_data;
303 unsigned long flags;
305 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
307 local_irq_save(flags);
308 while (1) {
309 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
310 if (count < c)
311 c = count;
312 if (c <= 0) {
313 break;
315 memcpy(info->xmit.buf + info->xmit.head, buf, c);
316 info->xmit.head = ((info->xmit.head + c) &
317 (SERIAL_XMIT_SIZE-1));
318 buf += c;
319 count -= c;
320 ret += c;
322 local_irq_restore(flags);
324 * Hey, we transmit directly from here in our case
326 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
327 && !tty->stopped && !tty->hw_stopped) {
328 transmit_chars(info, NULL);
330 return ret;
333 static int rs_write_room(struct tty_struct *tty)
335 struct async_struct *info = (struct async_struct *)tty->driver_data;
337 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
340 static int rs_chars_in_buffer(struct tty_struct *tty)
342 struct async_struct *info = (struct async_struct *)tty->driver_data;
344 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
347 static void rs_flush_buffer(struct tty_struct *tty)
349 struct async_struct *info = (struct async_struct *)tty->driver_data;
350 unsigned long flags;
352 local_irq_save(flags);
353 info->xmit.head = info->xmit.tail = 0;
354 local_irq_restore(flags);
356 wake_up_interruptible(&tty->write_wait);
358 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
359 tty->ldisc.write_wakeup)
360 (tty->ldisc.write_wakeup)(tty);
364 * This function is used to send a high-priority XON/XOFF character to
365 * the device
367 static void rs_send_xchar(struct tty_struct *tty, char ch)
369 struct async_struct *info = (struct async_struct *)tty->driver_data;
371 info->x_char = ch;
372 if (ch) {
374 * I guess we could call console->write() directly but
375 * let's do that for now.
377 transmit_chars(info, NULL);
382 * ------------------------------------------------------------
383 * rs_throttle()
385 * This routine is called by the upper-layer tty layer to signal that
386 * incoming characters should be throttled.
387 * ------------------------------------------------------------
389 static void rs_throttle(struct tty_struct * tty)
391 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
393 printk(KERN_INFO "simrs_throttle called\n");
396 static void rs_unthrottle(struct tty_struct * tty)
398 struct async_struct *info = (struct async_struct *)tty->driver_data;
400 if (I_IXOFF(tty)) {
401 if (info->x_char)
402 info->x_char = 0;
403 else
404 rs_send_xchar(tty, START_CHAR(tty));
406 printk(KERN_INFO "simrs_unthrottle called\n");
410 * rs_break() --- routine which turns the break handling on or off
412 static void rs_break(struct tty_struct *tty, int break_state)
416 static int rs_ioctl(struct tty_struct *tty, struct file * file,
417 unsigned int cmd, unsigned long arg)
419 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
420 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
421 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
422 if (tty->flags & (1 << TTY_IO_ERROR))
423 return -EIO;
426 switch (cmd) {
427 case TIOCMGET:
428 printk(KERN_INFO "rs_ioctl: TIOCMGET called\n");
429 return -EINVAL;
430 case TIOCMBIS:
431 case TIOCMBIC:
432 case TIOCMSET:
433 printk(KERN_INFO "rs_ioctl: TIOCMBIS/BIC/SET called\n");
434 return -EINVAL;
435 case TIOCGSERIAL:
436 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
437 return 0;
438 case TIOCSSERIAL:
439 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
440 return 0;
441 case TIOCSERCONFIG:
442 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
443 return -EINVAL;
445 case TIOCSERGETLSR: /* Get line status register */
446 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
447 return -EINVAL;
449 case TIOCSERGSTRUCT:
450 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
451 #if 0
452 if (copy_to_user((struct async_struct *) arg,
453 info, sizeof(struct async_struct)))
454 return -EFAULT;
455 #endif
456 return 0;
459 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
460 * - mask passed in arg for lines of interest
461 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
462 * Caller should use TIOCGICOUNT to see which one it was
464 case TIOCMIWAIT:
465 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
466 return 0;
468 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
469 * Return: write counters to the user passed counter struct
470 * NB: both 1->0 and 0->1 transitions are counted except for
471 * RI where only 0->1 is counted.
473 case TIOCGICOUNT:
474 printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
475 return 0;
477 case TIOCSERGWILD:
478 case TIOCSERSWILD:
479 /* "setserial -W" is called in Debian boot */
480 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
481 return 0;
483 default:
484 return -ENOIOCTLCMD;
486 return 0;
489 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
491 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
493 unsigned int cflag = tty->termios->c_cflag;
495 if ( (cflag == old_termios->c_cflag)
496 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
497 == RELEVANT_IFLAG(old_termios->c_iflag)))
498 return;
501 /* Handle turning off CRTSCTS */
502 if ((old_termios->c_cflag & CRTSCTS) &&
503 !(tty->termios->c_cflag & CRTSCTS)) {
504 tty->hw_stopped = 0;
505 rs_start(tty);
509 * This routine will shutdown a serial port; interrupts are disabled, and
510 * DTR is dropped if the hangup on close termio flag is on.
512 static void shutdown(struct async_struct * info)
514 unsigned long flags;
515 struct serial_state *state;
516 int retval;
518 if (!(info->flags & ASYNC_INITIALIZED)) return;
520 state = info->state;
522 #ifdef SIMSERIAL_DEBUG
523 printk("Shutting down serial port %d (irq %d)....", info->line,
524 state->irq);
525 #endif
527 local_irq_save(flags);
530 * First unlink the serial port from the IRQ chain...
532 if (info->next_port)
533 info->next_port->prev_port = info->prev_port;
534 if (info->prev_port)
535 info->prev_port->next_port = info->next_port;
536 else
537 IRQ_ports[state->irq] = info->next_port;
540 * Free the IRQ, if necessary
542 if (state->irq && (!IRQ_ports[state->irq] ||
543 !IRQ_ports[state->irq]->next_port)) {
544 if (IRQ_ports[state->irq]) {
545 free_irq(state->irq, NULL);
546 retval = request_irq(state->irq, rs_interrupt_single,
547 IRQ_T(info), "serial", NULL);
549 if (retval)
550 printk(KERN_ERR "serial shutdown: request_irq: error %d"
551 " Couldn't reacquire IRQ.\n", retval);
552 } else
553 free_irq(state->irq, NULL);
556 if (info->xmit.buf) {
557 free_page((unsigned long) info->xmit.buf);
558 info->xmit.buf = NULL;
561 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
563 info->flags &= ~ASYNC_INITIALIZED;
565 local_irq_restore(flags);
569 * ------------------------------------------------------------
570 * rs_close()
572 * This routine is called when the serial port gets closed. First, we
573 * wait for the last remaining data to be sent. Then, we unlink its
574 * async structure from the interrupt chain if necessary, and we free
575 * that IRQ if nothing is left in the chain.
576 * ------------------------------------------------------------
578 static void rs_close(struct tty_struct *tty, struct file * filp)
580 struct async_struct * info = (struct async_struct *)tty->driver_data;
581 struct serial_state *state;
582 unsigned long flags;
584 if (!info ) return;
586 state = info->state;
588 local_irq_save(flags);
589 if (tty_hung_up_p(filp)) {
590 #ifdef SIMSERIAL_DEBUG
591 printk("rs_close: hung_up\n");
592 #endif
593 local_irq_restore(flags);
594 return;
596 #ifdef SIMSERIAL_DEBUG
597 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
598 #endif
599 if ((tty->count == 1) && (state->count != 1)) {
601 * Uh, oh. tty->count is 1, which means that the tty
602 * structure will be freed. state->count should always
603 * be one in these conditions. If it's greater than
604 * one, we've got real problems, since it means the
605 * serial port won't be shutdown.
607 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
608 "state->count is %d\n", state->count);
609 state->count = 1;
611 if (--state->count < 0) {
612 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
613 info->line, state->count);
614 state->count = 0;
616 if (state->count) {
617 local_irq_restore(flags);
618 return;
620 info->flags |= ASYNC_CLOSING;
621 local_irq_restore(flags);
624 * Now we wait for the transmit buffer to clear; and we notify
625 * the line discipline to only process XON/XOFF characters.
627 shutdown(info);
628 if (tty->driver->flush_buffer) tty->driver->flush_buffer(tty);
629 if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty);
630 info->event = 0;
631 info->tty = NULL;
632 if (info->blocked_open) {
633 if (info->close_delay)
634 schedule_timeout_interruptible(info->close_delay);
635 wake_up_interruptible(&info->open_wait);
637 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
638 wake_up_interruptible(&info->close_wait);
642 * rs_wait_until_sent() --- wait until the transmitter is empty
644 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
650 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
652 static void rs_hangup(struct tty_struct *tty)
654 struct async_struct * info = (struct async_struct *)tty->driver_data;
655 struct serial_state *state = info->state;
657 #ifdef SIMSERIAL_DEBUG
658 printk("rs_hangup: called\n");
659 #endif
661 state = info->state;
663 rs_flush_buffer(tty);
664 if (info->flags & ASYNC_CLOSING)
665 return;
666 shutdown(info);
668 info->event = 0;
669 state->count = 0;
670 info->flags &= ~ASYNC_NORMAL_ACTIVE;
671 info->tty = NULL;
672 wake_up_interruptible(&info->open_wait);
676 static int get_async_struct(int line, struct async_struct **ret_info)
678 struct async_struct *info;
679 struct serial_state *sstate;
681 sstate = rs_table + line;
682 sstate->count++;
683 if (sstate->info) {
684 *ret_info = sstate->info;
685 return 0;
687 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
688 if (!info) {
689 sstate->count--;
690 return -ENOMEM;
692 init_waitqueue_head(&info->open_wait);
693 init_waitqueue_head(&info->close_wait);
694 init_waitqueue_head(&info->delta_msr_wait);
695 info->magic = SERIAL_MAGIC;
696 info->port = sstate->port;
697 info->flags = sstate->flags;
698 info->xmit_fifo_size = sstate->xmit_fifo_size;
699 info->line = line;
700 INIT_WORK(&info->work, do_softint);
701 info->state = sstate;
702 if (sstate->info) {
703 kfree(info);
704 *ret_info = sstate->info;
705 return 0;
707 *ret_info = sstate->info = info;
708 return 0;
711 static int
712 startup(struct async_struct *info)
714 unsigned long flags;
715 int retval=0;
716 irq_handler_t handler;
717 struct serial_state *state= info->state;
718 unsigned long page;
720 page = get_zeroed_page(GFP_KERNEL);
721 if (!page)
722 return -ENOMEM;
724 local_irq_save(flags);
726 if (info->flags & ASYNC_INITIALIZED) {
727 free_page(page);
728 goto errout;
731 if (!state->port || !state->type) {
732 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
733 free_page(page);
734 goto errout;
736 if (info->xmit.buf)
737 free_page(page);
738 else
739 info->xmit.buf = (unsigned char *) page;
741 #ifdef SIMSERIAL_DEBUG
742 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
743 #endif
746 * Allocate the IRQ if necessary
748 if (state->irq && (!IRQ_ports[state->irq] ||
749 !IRQ_ports[state->irq]->next_port)) {
750 if (IRQ_ports[state->irq]) {
751 retval = -EBUSY;
752 goto errout;
753 } else
754 handler = rs_interrupt_single;
756 retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
757 if (retval) {
758 if (capable(CAP_SYS_ADMIN)) {
759 if (info->tty)
760 set_bit(TTY_IO_ERROR,
761 &info->tty->flags);
762 retval = 0;
764 goto errout;
769 * Insert serial port into IRQ chain.
771 info->prev_port = NULL;
772 info->next_port = IRQ_ports[state->irq];
773 if (info->next_port)
774 info->next_port->prev_port = info;
775 IRQ_ports[state->irq] = info;
777 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
779 info->xmit.head = info->xmit.tail = 0;
781 #if 0
783 * Set up serial timers...
785 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
786 timer_active |= 1 << RS_TIMER;
787 #endif
790 * Set up the tty->alt_speed kludge
792 if (info->tty) {
793 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
794 info->tty->alt_speed = 57600;
795 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
796 info->tty->alt_speed = 115200;
797 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
798 info->tty->alt_speed = 230400;
799 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
800 info->tty->alt_speed = 460800;
803 info->flags |= ASYNC_INITIALIZED;
804 local_irq_restore(flags);
805 return 0;
807 errout:
808 local_irq_restore(flags);
809 return retval;
814 * This routine is called whenever a serial port is opened. It
815 * enables interrupts for a serial port, linking in its async structure into
816 * the IRQ chain. It also performs the serial-specific
817 * initialization for the tty structure.
819 static int rs_open(struct tty_struct *tty, struct file * filp)
821 struct async_struct *info;
822 int retval, line;
823 unsigned long page;
825 line = tty->index;
826 if ((line < 0) || (line >= NR_PORTS))
827 return -ENODEV;
828 retval = get_async_struct(line, &info);
829 if (retval)
830 return retval;
831 tty->driver_data = info;
832 info->tty = tty;
834 #ifdef SIMSERIAL_DEBUG
835 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
836 #endif
837 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
839 if (!tmp_buf) {
840 page = get_zeroed_page(GFP_KERNEL);
841 if (!page)
842 return -ENOMEM;
843 if (tmp_buf)
844 free_page(page);
845 else
846 tmp_buf = (unsigned char *) page;
850 * If the port is the middle of closing, bail out now
852 if (tty_hung_up_p(filp) ||
853 (info->flags & ASYNC_CLOSING)) {
854 if (info->flags & ASYNC_CLOSING)
855 interruptible_sleep_on(&info->close_wait);
856 #ifdef SERIAL_DO_RESTART
857 return ((info->flags & ASYNC_HUP_NOTIFY) ?
858 -EAGAIN : -ERESTARTSYS);
859 #else
860 return -EAGAIN;
861 #endif
865 * Start up serial port
867 retval = startup(info);
868 if (retval) {
869 return retval;
873 * figure out which console to use (should be one already)
875 console = console_drivers;
876 while (console) {
877 if ((console->flags & CON_ENABLED) && console->write) break;
878 console = console->next;
881 #ifdef SIMSERIAL_DEBUG
882 printk("rs_open ttys%d successful\n", info->line);
883 #endif
884 return 0;
888 * /proc fs routines....
891 static inline int line_info(char *buf, struct serial_state *state)
893 return sprintf(buf, "%d: uart:%s port:%lX irq:%d\n",
894 state->line, uart_config[state->type].name,
895 state->port, state->irq);
898 static int rs_read_proc(char *page, char **start, off_t off, int count,
899 int *eof, void *data)
901 int i, len = 0, l;
902 off_t begin = 0;
904 len += sprintf(page, "simserinfo:1.0 driver:%s\n", serial_version);
905 for (i = 0; i < NR_PORTS && len < 4000; i++) {
906 l = line_info(page + len, &rs_table[i]);
907 len += l;
908 if (len+begin > off+count)
909 goto done;
910 if (len+begin < off) {
911 begin += len;
912 len = 0;
915 *eof = 1;
916 done:
917 if (off >= len+begin)
918 return 0;
919 *start = page + (begin-off);
920 return ((count < begin+len-off) ? count : begin+len-off);
924 * ---------------------------------------------------------------------
925 * rs_init() and friends
927 * rs_init() is called at boot-time to initialize the serial driver.
928 * ---------------------------------------------------------------------
932 * This routine prints out the appropriate serial driver version
933 * number, and identifies which options were configured into this
934 * driver.
936 static inline void show_serial_version(void)
938 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
939 printk(KERN_INFO " no serial options enabled\n");
942 static const struct tty_operations hp_ops = {
943 .open = rs_open,
944 .close = rs_close,
945 .write = rs_write,
946 .put_char = rs_put_char,
947 .flush_chars = rs_flush_chars,
948 .write_room = rs_write_room,
949 .chars_in_buffer = rs_chars_in_buffer,
950 .flush_buffer = rs_flush_buffer,
951 .ioctl = rs_ioctl,
952 .throttle = rs_throttle,
953 .unthrottle = rs_unthrottle,
954 .send_xchar = rs_send_xchar,
955 .set_termios = rs_set_termios,
956 .stop = rs_stop,
957 .start = rs_start,
958 .hangup = rs_hangup,
959 .break_ctl = rs_break,
960 .wait_until_sent = rs_wait_until_sent,
961 .read_proc = rs_read_proc,
965 * The serial driver boot-time initialization code!
967 static int __init
968 simrs_init (void)
970 int i, rc;
971 struct serial_state *state;
973 if (!ia64_platform_is("hpsim"))
974 return -ENODEV;
976 hp_simserial_driver = alloc_tty_driver(1);
977 if (!hp_simserial_driver)
978 return -ENOMEM;
980 show_serial_version();
982 /* Initialize the tty_driver structure */
984 hp_simserial_driver->owner = THIS_MODULE;
985 hp_simserial_driver->driver_name = "simserial";
986 hp_simserial_driver->name = "ttyS";
987 hp_simserial_driver->major = TTY_MAJOR;
988 hp_simserial_driver->minor_start = 64;
989 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
990 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
991 hp_simserial_driver->init_termios = tty_std_termios;
992 hp_simserial_driver->init_termios.c_cflag =
993 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
994 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
995 tty_set_operations(hp_simserial_driver, &hp_ops);
998 * Let's have a little bit of fun !
1000 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
1002 if (state->type == PORT_UNKNOWN) continue;
1004 if (!state->irq) {
1005 if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
1006 panic("%s: out of interrupt vectors!\n",
1007 __FUNCTION__);
1008 state->irq = rc;
1009 ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
1012 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
1013 state->line,
1014 state->port, state->irq,
1015 uart_config[state->type].name);
1018 if (tty_register_driver(hp_simserial_driver))
1019 panic("Couldn't register simserial driver\n");
1021 return 0;
1024 #ifndef MODULE
1025 __initcall(simrs_init);
1026 #endif