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