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