Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / arch / ia64 / hp / sim / simserial.c
1 /*
2  * Simulated Serial Driver (fake serial)
3  *
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.
9  *
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>
13  *
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
17  */
18
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>
34
35 #include <asm/irq.h>
36 #include <asm/hw_irq.h>
37 #include <asm/uaccess.h>
38
39 #undef SIMSERIAL_DEBUG  /* define this to get some debug information */
40
41 #define KEYBOARD_INTR   3       /* must match with simulator! */
42
43 #define NR_PORTS        1       /* only one port for now */
44
45 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
46
47 #define SSC_GETCHAR     21
48
49 extern long ia64_ssc (long, long, long, long, int);
50 extern void ia64_ssc_connect_irq (long intr, long irq);
51
52 static char *serial_name = "SimSerial driver";
53 static char *serial_version = "0.6";
54
55 /*
56  * This has been extracted from asm/serial.h. We need one eventually but
57  * I don't know exactly what we're going to put in it so just fake one
58  * for now.
59  */
60 #define BASE_BAUD ( 1843200 / 16 )
61
62 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
63
64 /*
65  * Most of the values here are meaningless to this particular driver.
66  * However some values must be preserved for the code (leveraged from serial.c
67  * to work correctly).
68  * port must not be 0
69  * type must not be UNKNOWN
70  * So I picked arbitrary (guess from where?) values instead
71  */
72 static struct serial_state rs_table[NR_PORTS]={
73   /* UART CLK   PORT IRQ     FLAGS        */
74   { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 }  /* ttyS0 */
75 };
76
77 /*
78  * Just for the fun of it !
79  */
80 static struct serial_uart_config uart_config[] = {
81         { "unknown", 1, 0 },
82         { "8250", 1, 0 },
83         { "16450", 1, 0 },
84         { "16550", 1, 0 },
85         { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
86         { "cirrus", 1, 0 },
87         { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
88         { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
89                   UART_STARTECH },
90         { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
91         { NULL, 0}
92 };
93
94 struct tty_driver *hp_simserial_driver;
95
96 static struct async_struct *IRQ_ports[NR_IRQS];
97
98 static struct console *console;
99
100 static unsigned char *tmp_buf;
101
102 extern struct console *console_drivers; /* from kernel/printk.c */
103
104 /*
105  * ------------------------------------------------------------
106  * rs_stop() and rs_start()
107  *
108  * This routines are called before setting or resetting tty->stopped.
109  * They enable or disable transmitter interrupts, as necessary.
110  * ------------------------------------------------------------
111  */
112 static void rs_stop(struct tty_struct *tty)
113 {
114 #ifdef SIMSERIAL_DEBUG
115         printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
116                 tty->stopped, tty->hw_stopped, tty->flow_stopped);
117 #endif
118
119 }
120
121 static void rs_start(struct tty_struct *tty)
122 {
123 #ifdef SIMSERIAL_DEBUG
124         printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
125                 tty->stopped, tty->hw_stopped, tty->flow_stopped);
126 #endif
127 }
128
129 static  void receive_chars(struct tty_struct *tty)
130 {
131         unsigned char ch;
132         static unsigned char seen_esc = 0;
133
134         while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
135                 if ( ch == 27 && seen_esc == 0 ) {
136                         seen_esc = 1;
137                         continue;
138                 } else {
139                         if ( seen_esc==1 && ch == 'O' ) {
140                                 seen_esc = 2;
141                                 continue;
142                         } else if ( seen_esc == 2 ) {
143                                 if ( ch == 'P' ) /* F1 */
144                                         show_state();
145 #ifdef CONFIG_MAGIC_SYSRQ
146                                 if ( ch == 'S' ) { /* F4 */
147                                         do
148                                                 ch = ia64_ssc(0, 0, 0, 0,
149                                                               SSC_GETCHAR);
150                                         while (!ch);
151                                         handle_sysrq(ch, NULL);
152                                 }
153 #endif
154                                 seen_esc = 0;
155                                 continue;
156                         }
157                 }
158                 seen_esc = 0;
159
160                 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
161                         break;
162         }
163         tty_flip_buffer_push(tty);
164 }
165
166 /*
167  * This is the serial driver's interrupt routine for a single port
168  */
169 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
170 {
171         struct async_struct * info;
172
173         /*
174          * I don't know exactly why they don't use the dev_id opaque data
175          * pointer instead of this extra lookup table
176          */
177         info = IRQ_ports[irq];
178         if (!info || !info->tty) {
179                 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
180                 return IRQ_NONE;
181         }
182         /*
183          * pretty simple in our case, because we only get interrupts
184          * on inbound traffic
185          */
186         receive_chars(info->tty);
187         return IRQ_HANDLED;
188 }
189
190 /*
191  * -------------------------------------------------------------------
192  * Here ends the serial interrupt routines.
193  * -------------------------------------------------------------------
194  */
195
196 static void do_softint(struct work_struct *private_)
197 {
198         printk(KERN_ERR "simserial: do_softint called\n");
199 }
200
201 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
202 {
203         struct async_struct *info = (struct async_struct *)tty->driver_data;
204         unsigned long flags;
205
206         if (!tty || !info->xmit.buf)
207                 return 0;
208
209         local_irq_save(flags);
210         if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
211                 local_irq_restore(flags);
212                 return 0;
213         }
214         info->xmit.buf[info->xmit.head] = ch;
215         info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
216         local_irq_restore(flags);
217         return 1;
218 }
219
220 static void transmit_chars(struct async_struct *info, int *intr_done)
221 {
222         int count;
223         unsigned long flags;
224
225
226         local_irq_save(flags);
227
228         if (info->x_char) {
229                 char c = info->x_char;
230
231                 console->write(console, &c, 1);
232
233                 info->state->icount.tx++;
234                 info->x_char = 0;
235
236                 goto out;
237         }
238
239         if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
240 #ifdef SIMSERIAL_DEBUG
241                 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
242                        info->xmit.head, info->xmit.tail, info->tty->stopped);
243 #endif
244                 goto out;
245         }
246         /*
247          * We removed the loop and try to do it in to chunks. We need
248          * 2 operations maximum because it's a ring buffer.
249          *
250          * First from current to tail if possible.
251          * Then from the beginning of the buffer until necessary
252          */
253
254         count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
255                     SERIAL_XMIT_SIZE - info->xmit.tail);
256         console->write(console, info->xmit.buf+info->xmit.tail, count);
257
258         info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
259
260         /*
261          * We have more at the beginning of the buffer
262          */
263         count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
264         if (count) {
265                 console->write(console, info->xmit.buf, count);
266                 info->xmit.tail += count;
267         }
268 out:
269         local_irq_restore(flags);
270 }
271
272 static void rs_flush_chars(struct tty_struct *tty)
273 {
274         struct async_struct *info = (struct async_struct *)tty->driver_data;
275
276         if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
277             !info->xmit.buf)
278                 return;
279
280         transmit_chars(info, NULL);
281 }
282
283
284 static int rs_write(struct tty_struct * tty,
285                     const unsigned char *buf, int count)
286 {
287         int     c, ret = 0;
288         struct async_struct *info = (struct async_struct *)tty->driver_data;
289         unsigned long flags;
290
291         if (!tty || !info->xmit.buf || !tmp_buf) return 0;
292
293         local_irq_save(flags);
294         while (1) {
295                 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
296                 if (count < c)
297                         c = count;
298                 if (c <= 0) {
299                         break;
300                 }
301                 memcpy(info->xmit.buf + info->xmit.head, buf, c);
302                 info->xmit.head = ((info->xmit.head + c) &
303                                    (SERIAL_XMIT_SIZE-1));
304                 buf += c;
305                 count -= c;
306                 ret += c;
307         }
308         local_irq_restore(flags);
309         /*
310          * Hey, we transmit directly from here in our case
311          */
312         if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
313             && !tty->stopped && !tty->hw_stopped) {
314                 transmit_chars(info, NULL);
315         }
316         return ret;
317 }
318
319 static int rs_write_room(struct tty_struct *tty)
320 {
321         struct async_struct *info = (struct async_struct *)tty->driver_data;
322
323         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
324 }
325
326 static int rs_chars_in_buffer(struct tty_struct *tty)
327 {
328         struct async_struct *info = (struct async_struct *)tty->driver_data;
329
330         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
331 }
332
333 static void rs_flush_buffer(struct tty_struct *tty)
334 {
335         struct async_struct *info = (struct async_struct *)tty->driver_data;
336         unsigned long flags;
337
338         local_irq_save(flags);
339         info->xmit.head = info->xmit.tail = 0;
340         local_irq_restore(flags);
341
342         tty_wakeup(tty);
343 }
344
345 /*
346  * This function is used to send a high-priority XON/XOFF character to
347  * the device
348  */
349 static void rs_send_xchar(struct tty_struct *tty, char ch)
350 {
351         struct async_struct *info = (struct async_struct *)tty->driver_data;
352
353         info->x_char = ch;
354         if (ch) {
355                 /*
356                  * I guess we could call console->write() directly but
357                  * let's do that for now.
358                  */
359                 transmit_chars(info, NULL);
360         }
361 }
362
363 /*
364  * ------------------------------------------------------------
365  * rs_throttle()
366  *
367  * This routine is called by the upper-layer tty layer to signal that
368  * incoming characters should be throttled.
369  * ------------------------------------------------------------
370  */
371 static void rs_throttle(struct tty_struct * tty)
372 {
373         if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
374
375         printk(KERN_INFO "simrs_throttle called\n");
376 }
377
378 static void rs_unthrottle(struct tty_struct * tty)
379 {
380         struct async_struct *info = (struct async_struct *)tty->driver_data;
381
382         if (I_IXOFF(tty)) {
383                 if (info->x_char)
384                         info->x_char = 0;
385                 else
386                         rs_send_xchar(tty, START_CHAR(tty));
387         }
388         printk(KERN_INFO "simrs_unthrottle called\n");
389 }
390
391
392 static int rs_ioctl(struct tty_struct *tty, struct file * file,
393                     unsigned int cmd, unsigned long arg)
394 {
395         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
396             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
397             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
398                 if (tty->flags & (1 << TTY_IO_ERROR))
399                     return -EIO;
400         }
401
402         switch (cmd) {
403                 case TIOCGSERIAL:
404                         printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
405                         return 0;
406                 case TIOCSSERIAL:
407                         printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
408                         return 0;
409                 case TIOCSERCONFIG:
410                         printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
411                         return -EINVAL;
412
413                 case TIOCSERGETLSR: /* Get line status register */
414                         printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
415                         return  -EINVAL;
416
417                 case TIOCSERGSTRUCT:
418                         printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
419 #if 0
420                         if (copy_to_user((struct async_struct *) arg,
421                                          info, sizeof(struct async_struct)))
422                                 return -EFAULT;
423 #endif
424                         return 0;
425
426                 /*
427                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
428                  * - mask passed in arg for lines of interest
429                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
430                  * Caller should use TIOCGICOUNT to see which one it was
431                  */
432                 case TIOCMIWAIT:
433                         printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
434                         return 0;
435                 /*
436                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
437                  * Return: write counters to the user passed counter struct
438                  * NB: both 1->0 and 0->1 transitions are counted except for
439                  *     RI where only 0->1 is counted.
440                  */
441                 case TIOCGICOUNT:
442                         printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
443                         return 0;
444
445                 case TIOCSERGWILD:
446                 case TIOCSERSWILD:
447                         /* "setserial -W" is called in Debian boot */
448                         printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
449                         return 0;
450
451                 default:
452                         return -ENOIOCTLCMD;
453                 }
454         return 0;
455 }
456
457 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
458
459 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
460 {
461         /* Handle turning off CRTSCTS */
462         if ((old_termios->c_cflag & CRTSCTS) &&
463             !(tty->termios->c_cflag & CRTSCTS)) {
464                 tty->hw_stopped = 0;
465                 rs_start(tty);
466         }
467 }
468 /*
469  * This routine will shutdown a serial port; interrupts are disabled, and
470  * DTR is dropped if the hangup on close termio flag is on.
471  */
472 static void shutdown(struct async_struct * info)
473 {
474         unsigned long   flags;
475         struct serial_state *state;
476         int             retval;
477
478         if (!(info->flags & ASYNC_INITIALIZED)) return;
479
480         state = info->state;
481
482 #ifdef SIMSERIAL_DEBUG
483         printk("Shutting down serial port %d (irq %d)....", info->line,
484                state->irq);
485 #endif
486
487         local_irq_save(flags);
488         {
489                 /*
490                  * First unlink the serial port from the IRQ chain...
491                  */
492                 if (info->next_port)
493                         info->next_port->prev_port = info->prev_port;
494                 if (info->prev_port)
495                         info->prev_port->next_port = info->next_port;
496                 else
497                         IRQ_ports[state->irq] = info->next_port;
498
499                 /*
500                  * Free the IRQ, if necessary
501                  */
502                 if (state->irq && (!IRQ_ports[state->irq] ||
503                                    !IRQ_ports[state->irq]->next_port)) {
504                         if (IRQ_ports[state->irq]) {
505                                 free_irq(state->irq, NULL);
506                                 retval = request_irq(state->irq, rs_interrupt_single,
507                                                      IRQ_T(info), "serial", NULL);
508
509                                 if (retval)
510                                         printk(KERN_ERR "serial shutdown: request_irq: error %d"
511                                                "  Couldn't reacquire IRQ.\n", retval);
512                         } else
513                                 free_irq(state->irq, NULL);
514                 }
515
516                 if (info->xmit.buf) {
517                         free_page((unsigned long) info->xmit.buf);
518                         info->xmit.buf = NULL;
519                 }
520
521                 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
522
523                 info->flags &= ~ASYNC_INITIALIZED;
524         }
525         local_irq_restore(flags);
526 }
527
528 /*
529  * ------------------------------------------------------------
530  * rs_close()
531  *
532  * This routine is called when the serial port gets closed.  First, we
533  * wait for the last remaining data to be sent.  Then, we unlink its
534  * async structure from the interrupt chain if necessary, and we free
535  * that IRQ if nothing is left in the chain.
536  * ------------------------------------------------------------
537  */
538 static void rs_close(struct tty_struct *tty, struct file * filp)
539 {
540         struct async_struct * info = (struct async_struct *)tty->driver_data;
541         struct serial_state *state;
542         unsigned long flags;
543
544         if (!info ) return;
545
546         state = info->state;
547
548         local_irq_save(flags);
549         if (tty_hung_up_p(filp)) {
550 #ifdef SIMSERIAL_DEBUG
551                 printk("rs_close: hung_up\n");
552 #endif
553                 local_irq_restore(flags);
554                 return;
555         }
556 #ifdef SIMSERIAL_DEBUG
557         printk("rs_close ttys%d, count = %d\n", info->line, state->count);
558 #endif
559         if ((tty->count == 1) && (state->count != 1)) {
560                 /*
561                  * Uh, oh.  tty->count is 1, which means that the tty
562                  * structure will be freed.  state->count should always
563                  * be one in these conditions.  If it's greater than
564                  * one, we've got real problems, since it means the
565                  * serial port won't be shutdown.
566                  */
567                 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
568                        "state->count is %d\n", state->count);
569                 state->count = 1;
570         }
571         if (--state->count < 0) {
572                 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
573                        info->line, state->count);
574                 state->count = 0;
575         }
576         if (state->count) {
577                 local_irq_restore(flags);
578                 return;
579         }
580         info->flags |= ASYNC_CLOSING;
581         local_irq_restore(flags);
582
583         /*
584          * Now we wait for the transmit buffer to clear; and we notify
585          * the line discipline to only process XON/XOFF characters.
586          */
587         shutdown(info);
588         rs_flush_buffer(tty);
589         tty_ldisc_flush(tty);
590         info->event = 0;
591         info->tty = NULL;
592         if (info->blocked_open) {
593                 if (info->close_delay)
594                         schedule_timeout_interruptible(info->close_delay);
595                 wake_up_interruptible(&info->open_wait);
596         }
597         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
598         wake_up_interruptible(&info->close_wait);
599 }
600
601 /*
602  * rs_wait_until_sent() --- wait until the transmitter is empty
603  */
604 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
605 {
606 }
607
608
609 /*
610  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
611  */
612 static void rs_hangup(struct tty_struct *tty)
613 {
614         struct async_struct * info = (struct async_struct *)tty->driver_data;
615         struct serial_state *state = info->state;
616
617 #ifdef SIMSERIAL_DEBUG
618         printk("rs_hangup: called\n");
619 #endif
620
621         state = info->state;
622
623         rs_flush_buffer(tty);
624         if (info->flags & ASYNC_CLOSING)
625                 return;
626         shutdown(info);
627
628         info->event = 0;
629         state->count = 0;
630         info->flags &= ~ASYNC_NORMAL_ACTIVE;
631         info->tty = NULL;
632         wake_up_interruptible(&info->open_wait);
633 }
634
635
636 static int get_async_struct(int line, struct async_struct **ret_info)
637 {
638         struct async_struct *info;
639         struct serial_state *sstate;
640
641         sstate = rs_table + line;
642         sstate->count++;
643         if (sstate->info) {
644                 *ret_info = sstate->info;
645                 return 0;
646         }
647         info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
648         if (!info) {
649                 sstate->count--;
650                 return -ENOMEM;
651         }
652         init_waitqueue_head(&info->open_wait);
653         init_waitqueue_head(&info->close_wait);
654         init_waitqueue_head(&info->delta_msr_wait);
655         info->magic = SERIAL_MAGIC;
656         info->port = sstate->port;
657         info->flags = sstate->flags;
658         info->xmit_fifo_size = sstate->xmit_fifo_size;
659         info->line = line;
660         INIT_WORK(&info->work, do_softint);
661         info->state = sstate;
662         if (sstate->info) {
663                 kfree(info);
664                 *ret_info = sstate->info;
665                 return 0;
666         }
667         *ret_info = sstate->info = info;
668         return 0;
669 }
670
671 static int
672 startup(struct async_struct *info)
673 {
674         unsigned long flags;
675         int     retval=0;
676         irq_handler_t handler;
677         struct serial_state *state= info->state;
678         unsigned long page;
679
680         page = get_zeroed_page(GFP_KERNEL);
681         if (!page)
682                 return -ENOMEM;
683
684         local_irq_save(flags);
685
686         if (info->flags & ASYNC_INITIALIZED) {
687                 free_page(page);
688                 goto errout;
689         }
690
691         if (!state->port || !state->type) {
692                 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
693                 free_page(page);
694                 goto errout;
695         }
696         if (info->xmit.buf)
697                 free_page(page);
698         else
699                 info->xmit.buf = (unsigned char *) page;
700
701 #ifdef SIMSERIAL_DEBUG
702         printk("startup: ttys%d (irq %d)...", info->line, state->irq);
703 #endif
704
705         /*
706          * Allocate the IRQ if necessary
707          */
708         if (state->irq && (!IRQ_ports[state->irq] ||
709                           !IRQ_ports[state->irq]->next_port)) {
710                 if (IRQ_ports[state->irq]) {
711                         retval = -EBUSY;
712                         goto errout;
713                 } else
714                         handler = rs_interrupt_single;
715
716                 retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
717                 if (retval) {
718                         if (capable(CAP_SYS_ADMIN)) {
719                                 if (info->tty)
720                                         set_bit(TTY_IO_ERROR,
721                                                 &info->tty->flags);
722                                 retval = 0;
723                         }
724                         goto errout;
725                 }
726         }
727
728         /*
729          * Insert serial port into IRQ chain.
730          */
731         info->prev_port = NULL;
732         info->next_port = IRQ_ports[state->irq];
733         if (info->next_port)
734                 info->next_port->prev_port = info;
735         IRQ_ports[state->irq] = info;
736
737         if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
738
739         info->xmit.head = info->xmit.tail = 0;
740
741 #if 0
742         /*
743          * Set up serial timers...
744          */
745         timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
746         timer_active |= 1 << RS_TIMER;
747 #endif
748
749         /*
750          * Set up the tty->alt_speed kludge
751          */
752         if (info->tty) {
753                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
754                         info->tty->alt_speed = 57600;
755                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
756                         info->tty->alt_speed = 115200;
757                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
758                         info->tty->alt_speed = 230400;
759                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
760                         info->tty->alt_speed = 460800;
761         }
762
763         info->flags |= ASYNC_INITIALIZED;
764         local_irq_restore(flags);
765         return 0;
766
767 errout:
768         local_irq_restore(flags);
769         return retval;
770 }
771
772
773 /*
774  * This routine is called whenever a serial port is opened.  It
775  * enables interrupts for a serial port, linking in its async structure into
776  * the IRQ chain.   It also performs the serial-specific
777  * initialization for the tty structure.
778  */
779 static int rs_open(struct tty_struct *tty, struct file * filp)
780 {
781         struct async_struct     *info;
782         int                     retval, line;
783         unsigned long           page;
784
785         line = tty->index;
786         if ((line < 0) || (line >= NR_PORTS))
787                 return -ENODEV;
788         retval = get_async_struct(line, &info);
789         if (retval)
790                 return retval;
791         tty->driver_data = info;
792         info->tty = tty;
793
794 #ifdef SIMSERIAL_DEBUG
795         printk("rs_open %s, count = %d\n", tty->name, info->state->count);
796 #endif
797         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
798
799         if (!tmp_buf) {
800                 page = get_zeroed_page(GFP_KERNEL);
801                 if (!page)
802                         return -ENOMEM;
803                 if (tmp_buf)
804                         free_page(page);
805                 else
806                         tmp_buf = (unsigned char *) page;
807         }
808
809         /*
810          * If the port is the middle of closing, bail out now
811          */
812         if (tty_hung_up_p(filp) ||
813             (info->flags & ASYNC_CLOSING)) {
814                 if (info->flags & ASYNC_CLOSING)
815                         interruptible_sleep_on(&info->close_wait);
816 #ifdef SERIAL_DO_RESTART
817                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
818                         -EAGAIN : -ERESTARTSYS);
819 #else
820                 return -EAGAIN;
821 #endif
822         }
823
824         /*
825          * Start up serial port
826          */
827         retval = startup(info);
828         if (retval) {
829                 return retval;
830         }
831
832         /*
833          * figure out which console to use (should be one already)
834          */
835         console = console_drivers;
836         while (console) {
837                 if ((console->flags & CON_ENABLED) && console->write) break;
838                 console = console->next;
839         }
840
841 #ifdef SIMSERIAL_DEBUG
842         printk("rs_open ttys%d successful\n", info->line);
843 #endif
844         return 0;
845 }
846
847 /*
848  * /proc fs routines....
849  */
850
851 static inline int line_info(char *buf, struct serial_state *state)
852 {
853         return sprintf(buf, "%d: uart:%s port:%lX irq:%d\n",
854                        state->line, uart_config[state->type].name,
855                        state->port, state->irq);
856 }
857
858 static int rs_read_proc(char *page, char **start, off_t off, int count,
859                  int *eof, void *data)
860 {
861         int i, len = 0, l;
862         off_t   begin = 0;
863
864         len += sprintf(page, "simserinfo:1.0 driver:%s\n", serial_version);
865         for (i = 0; i < NR_PORTS && len < 4000; i++) {
866                 l = line_info(page + len, &rs_table[i]);
867                 len += l;
868                 if (len+begin > off+count)
869                         goto done;
870                 if (len+begin < off) {
871                         begin += len;
872                         len = 0;
873                 }
874         }
875         *eof = 1;
876 done:
877         if (off >= len+begin)
878                 return 0;
879         *start = page + (begin-off);
880         return ((count < begin+len-off) ? count : begin+len-off);
881 }
882
883 /*
884  * ---------------------------------------------------------------------
885  * rs_init() and friends
886  *
887  * rs_init() is called at boot-time to initialize the serial driver.
888  * ---------------------------------------------------------------------
889  */
890
891 /*
892  * This routine prints out the appropriate serial driver version
893  * number, and identifies which options were configured into this
894  * driver.
895  */
896 static inline void show_serial_version(void)
897 {
898         printk(KERN_INFO "%s version %s with", serial_name, serial_version);
899         printk(KERN_INFO " no serial options enabled\n");
900 }
901
902 static const struct tty_operations hp_ops = {
903         .open = rs_open,
904         .close = rs_close,
905         .write = rs_write,
906         .put_char = rs_put_char,
907         .flush_chars = rs_flush_chars,
908         .write_room = rs_write_room,
909         .chars_in_buffer = rs_chars_in_buffer,
910         .flush_buffer = rs_flush_buffer,
911         .ioctl = rs_ioctl,
912         .throttle = rs_throttle,
913         .unthrottle = rs_unthrottle,
914         .send_xchar = rs_send_xchar,
915         .set_termios = rs_set_termios,
916         .stop = rs_stop,
917         .start = rs_start,
918         .hangup = rs_hangup,
919         .wait_until_sent = rs_wait_until_sent,
920         .read_proc = rs_read_proc,
921 };
922
923 /*
924  * The serial driver boot-time initialization code!
925  */
926 static int __init
927 simrs_init (void)
928 {
929         int                     i, rc;
930         struct serial_state     *state;
931
932         if (!ia64_platform_is("hpsim"))
933                 return -ENODEV;
934
935         hp_simserial_driver = alloc_tty_driver(1);
936         if (!hp_simserial_driver)
937                 return -ENOMEM;
938
939         show_serial_version();
940
941         /* Initialize the tty_driver structure */
942
943         hp_simserial_driver->owner = THIS_MODULE;
944         hp_simserial_driver->driver_name = "simserial";
945         hp_simserial_driver->name = "ttyS";
946         hp_simserial_driver->major = TTY_MAJOR;
947         hp_simserial_driver->minor_start = 64;
948         hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
949         hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
950         hp_simserial_driver->init_termios = tty_std_termios;
951         hp_simserial_driver->init_termios.c_cflag =
952                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
953         hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
954         tty_set_operations(hp_simserial_driver, &hp_ops);
955
956         /*
957          * Let's have a little bit of fun !
958          */
959         for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
960
961                 if (state->type == PORT_UNKNOWN) continue;
962
963                 if (!state->irq) {
964                         if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
965                                 panic("%s: out of interrupt vectors!\n",
966                                       __func__);
967                         state->irq = rc;
968                         ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
969                 }
970
971                 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
972                        state->line,
973                        state->port, state->irq,
974                        uart_config[state->type].name);
975         }
976
977         if (tty_register_driver(hp_simserial_driver))
978                 panic("Couldn't register simserial driver\n");
979
980         return 0;
981 }
982
983 #ifndef MODULE
984 __initcall(simrs_init);
985 #endif