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