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