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