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