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