[IA64] build broken for ia64 simserial.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ia64 / hp / sim / simserial.c
blob27f23fa5ca156e3a65622b43d05d1322fb871174
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/config.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/major.h>
26 #include <linux/fcntl.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/capability.h>
30 #include <linux/console.h>
31 #include <linux/module.h>
32 #include <linux/serial.h>
33 #include <linux/serialP.h>
34 #include <linux/sysrq.h>
36 #include <asm/irq.h>
37 #include <asm/hw_irq.h>
38 #include <asm/uaccess.h>
40 #ifdef CONFIG_KDB
41 # include <linux/kdb.h>
42 #endif
44 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
46 #define KEYBOARD_INTR 3 /* must match with simulator! */
48 #define NR_PORTS 1 /* only one port for now */
49 #define SERIAL_INLINE 1
51 #ifdef SERIAL_INLINE
52 #define _INLINE_ inline
53 #endif
55 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
57 #define SSC_GETCHAR 21
59 extern long ia64_ssc (long, long, long, long, int);
60 extern void ia64_ssc_connect_irq (long intr, long irq);
62 static char *serial_name = "SimSerial driver";
63 static char *serial_version = "0.6";
66 * This has been extracted from asm/serial.h. We need one eventually but
67 * I don't know exactly what we're going to put in it so just fake one
68 * for now.
70 #define BASE_BAUD ( 1843200 / 16 )
72 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
75 * Most of the values here are meaningless to this particular driver.
76 * However some values must be preserved for the code (leveraged from serial.c
77 * to work correctly).
78 * port must not be 0
79 * type must not be UNKNOWN
80 * So I picked arbitrary (guess from where?) values instead
82 static struct serial_state rs_table[NR_PORTS]={
83 /* UART CLK PORT IRQ FLAGS */
84 { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
88 * Just for the fun of it !
90 static struct serial_uart_config uart_config[] = {
91 { "unknown", 1, 0 },
92 { "8250", 1, 0 },
93 { "16450", 1, 0 },
94 { "16550", 1, 0 },
95 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
96 { "cirrus", 1, 0 },
97 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
98 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
99 UART_STARTECH },
100 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
101 { 0, 0}
104 struct tty_driver *hp_simserial_driver;
106 static struct async_struct *IRQ_ports[NR_IRQS];
108 static struct console *console;
110 static unsigned char *tmp_buf;
111 static DECLARE_MUTEX(tmp_buf_sem);
113 extern struct console *console_drivers; /* from kernel/printk.c */
116 * ------------------------------------------------------------
117 * rs_stop() and rs_start()
119 * This routines are called before setting or resetting tty->stopped.
120 * They enable or disable transmitter interrupts, as necessary.
121 * ------------------------------------------------------------
123 static void rs_stop(struct tty_struct *tty)
125 #ifdef SIMSERIAL_DEBUG
126 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
127 tty->stopped, tty->hw_stopped, tty->flow_stopped);
128 #endif
132 static void rs_start(struct tty_struct *tty)
134 #ifdef SIMSERIAL_DEBUG
135 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
136 tty->stopped, tty->hw_stopped, tty->flow_stopped);
137 #endif
140 static void receive_chars(struct tty_struct *tty, struct pt_regs *regs)
142 unsigned char ch;
143 static unsigned char seen_esc = 0;
145 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
146 if ( ch == 27 && seen_esc == 0 ) {
147 seen_esc = 1;
148 continue;
149 } else {
150 if ( seen_esc==1 && ch == 'O' ) {
151 seen_esc = 2;
152 continue;
153 } else if ( seen_esc == 2 ) {
154 if ( ch == 'P' ) /* F1 */
155 show_state();
156 #ifdef CONFIG_MAGIC_SYSRQ
157 if ( ch == 'S' ) { /* F4 */
159 ch = ia64_ssc(0, 0, 0, 0,
160 SSC_GETCHAR);
161 while (!ch);
162 handle_sysrq(ch, regs, NULL);
164 #endif
165 seen_esc = 0;
166 continue;
169 seen_esc = 0;
171 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
172 break;
174 tty_flip_buffer_push(tty);
178 * This is the serial driver's interrupt routine for a single port
180 static irqreturn_t rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
182 struct async_struct * info;
185 * I don't know exactly why they don't use the dev_id opaque data
186 * pointer instead of this extra lookup table
188 info = IRQ_ports[irq];
189 if (!info || !info->tty) {
190 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
191 return IRQ_NONE;
194 * pretty simple in our case, because we only get interrupts
195 * on inbound traffic
197 receive_chars(info->tty, regs);
198 return IRQ_HANDLED;
202 * -------------------------------------------------------------------
203 * Here ends the serial interrupt routines.
204 * -------------------------------------------------------------------
207 #if 0
209 * not really used in our situation so keep them commented out for now
211 static DECLARE_TASK_QUEUE(tq_serial); /* used to be at the top of the file */
212 static void do_serial_bh(void)
214 run_task_queue(&tq_serial);
215 printk(KERN_ERR "do_serial_bh: called\n");
217 #endif
219 static void do_softint(void *private_)
221 printk(KERN_ERR "simserial: do_softint called\n");
224 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
226 struct async_struct *info = (struct async_struct *)tty->driver_data;
227 unsigned long flags;
229 if (!tty || !info->xmit.buf) return;
231 local_irq_save(flags);
232 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
233 local_irq_restore(flags);
234 return;
236 info->xmit.buf[info->xmit.head] = ch;
237 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
238 local_irq_restore(flags);
241 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
243 int count;
244 unsigned long flags;
247 local_irq_save(flags);
249 if (info->x_char) {
250 char c = info->x_char;
252 console->write(console, &c, 1);
254 info->state->icount.tx++;
255 info->x_char = 0;
257 goto out;
260 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
261 #ifdef SIMSERIAL_DEBUG
262 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
263 info->xmit.head, info->xmit.tail, info->tty->stopped);
264 #endif
265 goto out;
268 * We removed the loop and try to do it in to chunks. We need
269 * 2 operations maximum because it's a ring buffer.
271 * First from current to tail if possible.
272 * Then from the beginning of the buffer until necessary
275 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
276 SERIAL_XMIT_SIZE - info->xmit.tail);
277 console->write(console, info->xmit.buf+info->xmit.tail, count);
279 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
282 * We have more at the beginning of the buffer
284 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
285 if (count) {
286 console->write(console, info->xmit.buf, count);
287 info->xmit.tail += count;
289 out:
290 local_irq_restore(flags);
293 static void rs_flush_chars(struct tty_struct *tty)
295 struct async_struct *info = (struct async_struct *)tty->driver_data;
297 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
298 !info->xmit.buf)
299 return;
301 transmit_chars(info, NULL);
305 static int rs_write(struct tty_struct * tty,
306 const unsigned char *buf, int count)
308 int c, ret = 0;
309 struct async_struct *info = (struct async_struct *)tty->driver_data;
310 unsigned long flags;
312 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
314 local_irq_save(flags);
315 while (1) {
316 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
317 if (count < c)
318 c = count;
319 if (c <= 0) {
320 break;
322 memcpy(info->xmit.buf + info->xmit.head, buf, c);
323 info->xmit.head = ((info->xmit.head + c) &
324 (SERIAL_XMIT_SIZE-1));
325 buf += c;
326 count -= c;
327 ret += c;
329 local_irq_restore(flags);
331 * Hey, we transmit directly from here in our case
333 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
334 && !tty->stopped && !tty->hw_stopped) {
335 transmit_chars(info, NULL);
337 return ret;
340 static int rs_write_room(struct tty_struct *tty)
342 struct async_struct *info = (struct async_struct *)tty->driver_data;
344 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
347 static int rs_chars_in_buffer(struct tty_struct *tty)
349 struct async_struct *info = (struct async_struct *)tty->driver_data;
351 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
354 static void rs_flush_buffer(struct tty_struct *tty)
356 struct async_struct *info = (struct async_struct *)tty->driver_data;
357 unsigned long flags;
359 local_irq_save(flags);
360 info->xmit.head = info->xmit.tail = 0;
361 local_irq_restore(flags);
363 wake_up_interruptible(&tty->write_wait);
365 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
366 tty->ldisc.write_wakeup)
367 (tty->ldisc.write_wakeup)(tty);
371 * This function is used to send a high-priority XON/XOFF character to
372 * the device
374 static void rs_send_xchar(struct tty_struct *tty, char ch)
376 struct async_struct *info = (struct async_struct *)tty->driver_data;
378 info->x_char = ch;
379 if (ch) {
381 * I guess we could call console->write() directly but
382 * let's do that for now.
384 transmit_chars(info, NULL);
389 * ------------------------------------------------------------
390 * rs_throttle()
392 * This routine is called by the upper-layer tty layer to signal that
393 * incoming characters should be throttled.
394 * ------------------------------------------------------------
396 static void rs_throttle(struct tty_struct * tty)
398 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
400 printk(KERN_INFO "simrs_throttle called\n");
403 static void rs_unthrottle(struct tty_struct * tty)
405 struct async_struct *info = (struct async_struct *)tty->driver_data;
407 if (I_IXOFF(tty)) {
408 if (info->x_char)
409 info->x_char = 0;
410 else
411 rs_send_xchar(tty, START_CHAR(tty));
413 printk(KERN_INFO "simrs_unthrottle called\n");
417 * rs_break() --- routine which turns the break handling on or off
419 static void rs_break(struct tty_struct *tty, int break_state)
423 static int rs_ioctl(struct tty_struct *tty, struct file * file,
424 unsigned int cmd, unsigned long arg)
426 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
427 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
428 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
429 if (tty->flags & (1 << TTY_IO_ERROR))
430 return -EIO;
433 switch (cmd) {
434 case TIOCMGET:
435 printk(KERN_INFO "rs_ioctl: TIOCMGET called\n");
436 return -EINVAL;
437 case TIOCMBIS:
438 case TIOCMBIC:
439 case TIOCMSET:
440 printk(KERN_INFO "rs_ioctl: TIOCMBIS/BIC/SET called\n");
441 return -EINVAL;
442 case TIOCGSERIAL:
443 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
444 return 0;
445 case TIOCSSERIAL:
446 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
447 return 0;
448 case TIOCSERCONFIG:
449 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
450 return -EINVAL;
452 case TIOCSERGETLSR: /* Get line status register */
453 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
454 return -EINVAL;
456 case TIOCSERGSTRUCT:
457 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
458 #if 0
459 if (copy_to_user((struct async_struct *) arg,
460 info, sizeof(struct async_struct)))
461 return -EFAULT;
462 #endif
463 return 0;
466 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
467 * - mask passed in arg for lines of interest
468 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
469 * Caller should use TIOCGICOUNT to see which one it was
471 case TIOCMIWAIT:
472 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
473 return 0;
475 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
476 * Return: write counters to the user passed counter struct
477 * NB: both 1->0 and 0->1 transitions are counted except for
478 * RI where only 0->1 is counted.
480 case TIOCGICOUNT:
481 printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
482 return 0;
484 case TIOCSERGWILD:
485 case TIOCSERSWILD:
486 /* "setserial -W" is called in Debian boot */
487 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
488 return 0;
490 default:
491 return -ENOIOCTLCMD;
493 return 0;
496 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
498 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
500 unsigned int cflag = tty->termios->c_cflag;
502 if ( (cflag == old_termios->c_cflag)
503 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
504 == RELEVANT_IFLAG(old_termios->c_iflag)))
505 return;
508 /* Handle turning off CRTSCTS */
509 if ((old_termios->c_cflag & CRTSCTS) &&
510 !(tty->termios->c_cflag & CRTSCTS)) {
511 tty->hw_stopped = 0;
512 rs_start(tty);
516 * This routine will shutdown a serial port; interrupts are disabled, and
517 * DTR is dropped if the hangup on close termio flag is on.
519 static void shutdown(struct async_struct * info)
521 unsigned long flags;
522 struct serial_state *state;
523 int retval;
525 if (!(info->flags & ASYNC_INITIALIZED)) return;
527 state = info->state;
529 #ifdef SIMSERIAL_DEBUG
530 printk("Shutting down serial port %d (irq %d)....", info->line,
531 state->irq);
532 #endif
534 local_irq_save(flags);
537 * First unlink the serial port from the IRQ chain...
539 if (info->next_port)
540 info->next_port->prev_port = info->prev_port;
541 if (info->prev_port)
542 info->prev_port->next_port = info->next_port;
543 else
544 IRQ_ports[state->irq] = info->next_port;
547 * Free the IRQ, if necessary
549 if (state->irq && (!IRQ_ports[state->irq] ||
550 !IRQ_ports[state->irq]->next_port)) {
551 if (IRQ_ports[state->irq]) {
552 free_irq(state->irq, NULL);
553 retval = request_irq(state->irq, rs_interrupt_single,
554 IRQ_T(info), "serial", NULL);
556 if (retval)
557 printk(KERN_ERR "serial shutdown: request_irq: error %d"
558 " Couldn't reacquire IRQ.\n", retval);
559 } else
560 free_irq(state->irq, NULL);
563 if (info->xmit.buf) {
564 free_page((unsigned long) info->xmit.buf);
565 info->xmit.buf = 0;
568 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
570 info->flags &= ~ASYNC_INITIALIZED;
572 local_irq_restore(flags);
576 * ------------------------------------------------------------
577 * rs_close()
579 * This routine is called when the serial port gets closed. First, we
580 * wait for the last remaining data to be sent. Then, we unlink its
581 * async structure from the interrupt chain if necessary, and we free
582 * that IRQ if nothing is left in the chain.
583 * ------------------------------------------------------------
585 static void rs_close(struct tty_struct *tty, struct file * filp)
587 struct async_struct * info = (struct async_struct *)tty->driver_data;
588 struct serial_state *state;
589 unsigned long flags;
591 if (!info ) return;
593 state = info->state;
595 local_irq_save(flags);
596 if (tty_hung_up_p(filp)) {
597 #ifdef SIMSERIAL_DEBUG
598 printk("rs_close: hung_up\n");
599 #endif
600 local_irq_restore(flags);
601 return;
603 #ifdef SIMSERIAL_DEBUG
604 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
605 #endif
606 if ((tty->count == 1) && (state->count != 1)) {
608 * Uh, oh. tty->count is 1, which means that the tty
609 * structure will be freed. state->count should always
610 * be one in these conditions. If it's greater than
611 * one, we've got real problems, since it means the
612 * serial port won't be shutdown.
614 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
615 "state->count is %d\n", state->count);
616 state->count = 1;
618 if (--state->count < 0) {
619 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
620 info->line, state->count);
621 state->count = 0;
623 if (state->count) {
624 local_irq_restore(flags);
625 return;
627 info->flags |= ASYNC_CLOSING;
628 local_irq_restore(flags);
631 * Now we wait for the transmit buffer to clear; and we notify
632 * the line discipline to only process XON/XOFF characters.
634 shutdown(info);
635 if (tty->driver->flush_buffer) tty->driver->flush_buffer(tty);
636 if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty);
637 info->event = 0;
638 info->tty = 0;
639 if (info->blocked_open) {
640 if (info->close_delay)
641 schedule_timeout_interruptible(info->close_delay);
642 wake_up_interruptible(&info->open_wait);
644 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
645 wake_up_interruptible(&info->close_wait);
649 * rs_wait_until_sent() --- wait until the transmitter is empty
651 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
657 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
659 static void rs_hangup(struct tty_struct *tty)
661 struct async_struct * info = (struct async_struct *)tty->driver_data;
662 struct serial_state *state = info->state;
664 #ifdef SIMSERIAL_DEBUG
665 printk("rs_hangup: called\n");
666 #endif
668 state = info->state;
670 rs_flush_buffer(tty);
671 if (info->flags & ASYNC_CLOSING)
672 return;
673 shutdown(info);
675 info->event = 0;
676 state->count = 0;
677 info->flags &= ~ASYNC_NORMAL_ACTIVE;
678 info->tty = 0;
679 wake_up_interruptible(&info->open_wait);
683 static int get_async_struct(int line, struct async_struct **ret_info)
685 struct async_struct *info;
686 struct serial_state *sstate;
688 sstate = rs_table + line;
689 sstate->count++;
690 if (sstate->info) {
691 *ret_info = sstate->info;
692 return 0;
694 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
695 if (!info) {
696 sstate->count--;
697 return -ENOMEM;
699 memset(info, 0, sizeof(struct async_struct));
700 init_waitqueue_head(&info->open_wait);
701 init_waitqueue_head(&info->close_wait);
702 init_waitqueue_head(&info->delta_msr_wait);
703 info->magic = SERIAL_MAGIC;
704 info->port = sstate->port;
705 info->flags = sstate->flags;
706 info->xmit_fifo_size = sstate->xmit_fifo_size;
707 info->line = line;
708 INIT_WORK(&info->work, do_softint, info);
709 info->state = sstate;
710 if (sstate->info) {
711 kfree(info);
712 *ret_info = sstate->info;
713 return 0;
715 *ret_info = sstate->info = info;
716 return 0;
719 static int
720 startup(struct async_struct *info)
722 unsigned long flags;
723 int retval=0;
724 irqreturn_t (*handler)(int, void *, struct pt_regs *);
725 struct serial_state *state= info->state;
726 unsigned long page;
728 page = get_zeroed_page(GFP_KERNEL);
729 if (!page)
730 return -ENOMEM;
732 local_irq_save(flags);
734 if (info->flags & ASYNC_INITIALIZED) {
735 free_page(page);
736 goto errout;
739 if (!state->port || !state->type) {
740 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
741 free_page(page);
742 goto errout;
744 if (info->xmit.buf)
745 free_page(page);
746 else
747 info->xmit.buf = (unsigned char *) page;
749 #ifdef SIMSERIAL_DEBUG
750 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
751 #endif
754 * Allocate the IRQ if necessary
756 if (state->irq && (!IRQ_ports[state->irq] ||
757 !IRQ_ports[state->irq]->next_port)) {
758 if (IRQ_ports[state->irq]) {
759 retval = -EBUSY;
760 goto errout;
761 } else
762 handler = rs_interrupt_single;
764 retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
765 if (retval) {
766 if (capable(CAP_SYS_ADMIN)) {
767 if (info->tty)
768 set_bit(TTY_IO_ERROR,
769 &info->tty->flags);
770 retval = 0;
772 goto errout;
777 * Insert serial port into IRQ chain.
779 info->prev_port = 0;
780 info->next_port = IRQ_ports[state->irq];
781 if (info->next_port)
782 info->next_port->prev_port = info;
783 IRQ_ports[state->irq] = info;
785 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
787 info->xmit.head = info->xmit.tail = 0;
789 #if 0
791 * Set up serial timers...
793 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
794 timer_active |= 1 << RS_TIMER;
795 #endif
798 * Set up the tty->alt_speed kludge
800 if (info->tty) {
801 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
802 info->tty->alt_speed = 57600;
803 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
804 info->tty->alt_speed = 115200;
805 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
806 info->tty->alt_speed = 230400;
807 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
808 info->tty->alt_speed = 460800;
811 info->flags |= ASYNC_INITIALIZED;
812 local_irq_restore(flags);
813 return 0;
815 errout:
816 local_irq_restore(flags);
817 return retval;
822 * This routine is called whenever a serial port is opened. It
823 * enables interrupts for a serial port, linking in its async structure into
824 * the IRQ chain. It also performs the serial-specific
825 * initialization for the tty structure.
827 static int rs_open(struct tty_struct *tty, struct file * filp)
829 struct async_struct *info;
830 int retval, line;
831 unsigned long page;
833 line = tty->index;
834 if ((line < 0) || (line >= NR_PORTS))
835 return -ENODEV;
836 retval = get_async_struct(line, &info);
837 if (retval)
838 return retval;
839 tty->driver_data = info;
840 info->tty = tty;
842 #ifdef SIMSERIAL_DEBUG
843 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
844 #endif
845 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
847 if (!tmp_buf) {
848 page = get_zeroed_page(GFP_KERNEL);
849 if (!page)
850 return -ENOMEM;
851 if (tmp_buf)
852 free_page(page);
853 else
854 tmp_buf = (unsigned char *) page;
858 * If the port is the middle of closing, bail out now
860 if (tty_hung_up_p(filp) ||
861 (info->flags & ASYNC_CLOSING)) {
862 if (info->flags & ASYNC_CLOSING)
863 interruptible_sleep_on(&info->close_wait);
864 #ifdef SERIAL_DO_RESTART
865 return ((info->flags & ASYNC_HUP_NOTIFY) ?
866 -EAGAIN : -ERESTARTSYS);
867 #else
868 return -EAGAIN;
869 #endif
873 * Start up serial port
875 retval = startup(info);
876 if (retval) {
877 return retval;
881 * figure out which console to use (should be one already)
883 console = console_drivers;
884 while (console) {
885 if ((console->flags & CON_ENABLED) && console->write) break;
886 console = console->next;
889 #ifdef SIMSERIAL_DEBUG
890 printk("rs_open ttys%d successful\n", info->line);
891 #endif
892 return 0;
896 * /proc fs routines....
899 static inline int line_info(char *buf, struct serial_state *state)
901 return sprintf(buf, "%d: uart:%s port:%lX irq:%d\n",
902 state->line, uart_config[state->type].name,
903 state->port, state->irq);
906 static int rs_read_proc(char *page, char **start, off_t off, int count,
907 int *eof, void *data)
909 int i, len = 0, l;
910 off_t begin = 0;
912 len += sprintf(page, "simserinfo:1.0 driver:%s\n", serial_version);
913 for (i = 0; i < NR_PORTS && len < 4000; i++) {
914 l = line_info(page + len, &rs_table[i]);
915 len += l;
916 if (len+begin > off+count)
917 goto done;
918 if (len+begin < off) {
919 begin += len;
920 len = 0;
923 *eof = 1;
924 done:
925 if (off >= len+begin)
926 return 0;
927 *start = page + (begin-off);
928 return ((count < begin+len-off) ? count : begin+len-off);
932 * ---------------------------------------------------------------------
933 * rs_init() and friends
935 * rs_init() is called at boot-time to initialize the serial driver.
936 * ---------------------------------------------------------------------
940 * This routine prints out the appropriate serial driver version
941 * number, and identifies which options were configured into this
942 * driver.
944 static inline void show_serial_version(void)
946 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
947 printk(KERN_INFO " no serial options enabled\n");
950 static struct tty_operations hp_ops = {
951 .open = rs_open,
952 .close = rs_close,
953 .write = rs_write,
954 .put_char = rs_put_char,
955 .flush_chars = rs_flush_chars,
956 .write_room = rs_write_room,
957 .chars_in_buffer = rs_chars_in_buffer,
958 .flush_buffer = rs_flush_buffer,
959 .ioctl = rs_ioctl,
960 .throttle = rs_throttle,
961 .unthrottle = rs_unthrottle,
962 .send_xchar = rs_send_xchar,
963 .set_termios = rs_set_termios,
964 .stop = rs_stop,
965 .start = rs_start,
966 .hangup = rs_hangup,
967 .break_ctl = rs_break,
968 .wait_until_sent = rs_wait_until_sent,
969 .read_proc = rs_read_proc,
973 * The serial driver boot-time initialization code!
975 static int __init
976 simrs_init (void)
978 int i, rc;
979 struct serial_state *state;
981 if (!ia64_platform_is("hpsim"))
982 return -ENODEV;
984 hp_simserial_driver = alloc_tty_driver(1);
985 if (!hp_simserial_driver)
986 return -ENOMEM;
988 show_serial_version();
990 /* Initialize the tty_driver structure */
992 hp_simserial_driver->owner = THIS_MODULE;
993 hp_simserial_driver->driver_name = "simserial";
994 hp_simserial_driver->name = "ttyS";
995 hp_simserial_driver->major = TTY_MAJOR;
996 hp_simserial_driver->minor_start = 64;
997 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
998 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
999 hp_simserial_driver->init_termios = tty_std_termios;
1000 hp_simserial_driver->init_termios.c_cflag =
1001 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1002 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
1003 tty_set_operations(hp_simserial_driver, &hp_ops);
1006 * Let's have a little bit of fun !
1008 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
1010 if (state->type == PORT_UNKNOWN) continue;
1012 if (!state->irq) {
1013 if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
1014 panic("%s: out of interrupt vectors!\n",
1015 __FUNCTION__);
1016 state->irq = rc;
1017 ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
1020 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
1021 state->line,
1022 state->port, state->irq,
1023 uart_config[state->type].name);
1026 if (tty_register_driver(hp_simserial_driver))
1027 panic("Couldn't register simserial driver\n");
1029 return 0;
1032 #ifndef MODULE
1033 __initcall(simrs_init);
1034 #endif