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