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