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