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