blob: a08a53f033b42f0790e0007cb7fa8fffb6b04bbb [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>
Alexey Dobriyanbf542152009-03-31 15:19:23 -070027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
Randy Dunlapa9415642006-01-11 12:17:48 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/console.h>
31#include <linux/module.h>
32#include <linux/serial.h>
33#include <linux/serialP.h>
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070034#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/irq.h>
Jiri Slaby035cfe52012-03-08 21:01:17 +010037#include <asm/hpsim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/hw_irq.h>
39#include <asm/uaccess.h>
40
Jiri Slaby035cfe52012-03-08 21:01:17 +010041#include "hpsim_ssc.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jiri Slaby979b6d82012-03-05 14:52:15 +010049#define IRQ_T(state) ((state->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static char *serial_name = "SimSerial driver";
52static char *serial_version = "0.6";
53
54/*
55 * This has been extracted from asm/serial.h. We need one eventually but
56 * I don't know exactly what we're going to put in it so just fake one
57 * for now.
58 */
59#define BASE_BAUD ( 1843200 / 16 )
60
61#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
62
63/*
64 * Most of the values here are meaningless to this particular driver.
65 * However some values must be preserved for the code (leveraged from serial.c
66 * to work correctly).
67 * port must not be 0
68 * type must not be UNKNOWN
69 * So I picked arbitrary (guess from where?) values instead
70 */
71static struct serial_state rs_table[NR_PORTS]={
72 /* UART CLK PORT IRQ FLAGS */
Jiri Slaby9c8efec2012-03-05 14:52:12 +010073 { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 } /* ttyS0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76/*
77 * Just for the fun of it !
78 */
79static struct serial_uart_config uart_config[] = {
80 { "unknown", 1, 0 },
81 { "8250", 1, 0 },
82 { "16450", 1, 0 },
83 { "16550", 1, 0 },
84 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
85 { "cirrus", 1, 0 },
86 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
87 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
88 UART_STARTECH },
89 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
Al Virocfa7fd72006-10-10 22:46:17 +010090 { NULL, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93struct tty_driver *hp_simserial_driver;
94
95static struct async_struct *IRQ_ports[NR_IRQS];
96
97static struct console *console;
98
99static unsigned char *tmp_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101extern struct console *console_drivers; /* from kernel/printk.c */
102
103/*
104 * ------------------------------------------------------------
105 * rs_stop() and rs_start()
106 *
107 * This routines are called before setting or resetting tty->stopped.
108 * They enable or disable transmitter interrupts, as necessary.
109 * ------------------------------------------------------------
110 */
111static void rs_stop(struct tty_struct *tty)
112{
113#ifdef SIMSERIAL_DEBUG
114 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
115 tty->stopped, tty->hw_stopped, tty->flow_stopped);
116#endif
117
118}
119
120static void rs_start(struct tty_struct *tty)
121{
viro@ZenIV.linux.org.uke72225d2005-09-07 23:23:50 +0100122#ifdef SIMSERIAL_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
124 tty->stopped, tty->hw_stopped, tty->flow_stopped);
125#endif
126}
127
Al Viro5dcded12006-10-08 14:59:19 +0100128static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 unsigned char ch;
131 static unsigned char seen_esc = 0;
132
133 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
134 if ( ch == 27 && seen_esc == 0 ) {
135 seen_esc = 1;
136 continue;
137 } else {
138 if ( seen_esc==1 && ch == 'O' ) {
139 seen_esc = 2;
140 continue;
141 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700142 if ( ch == 'P' ) /* F1 */
143 show_state();
144#ifdef CONFIG_MAGIC_SYSRQ
145 if ( ch == 'S' ) { /* F4 */
146 do
147 ch = ia64_ssc(0, 0, 0, 0,
148 SSC_GETCHAR);
149 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700150 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 seen_esc = 0;
154 continue;
155 }
156 }
157 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Andreas Schwabd50f5c52006-01-13 23:46:38 +0100159 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 tty_flip_buffer_push(tty);
163}
164
165/*
166 * This is the serial driver's interrupt routine for a single port
167 */
Al Viro5dcded12006-10-08 14:59:19 +0100168static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct async_struct * info;
171
172 /*
173 * I don't know exactly why they don't use the dev_id opaque data
174 * pointer instead of this extra lookup table
175 */
176 info = IRQ_ports[irq];
177 if (!info || !info->tty) {
178 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
179 return IRQ_NONE;
180 }
181 /*
182 * pretty simple in our case, because we only get interrupts
183 * on inbound traffic
184 */
Al Viro5dcded12006-10-08 14:59:19 +0100185 receive_chars(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return IRQ_HANDLED;
187}
188
189/*
190 * -------------------------------------------------------------------
191 * Here ends the serial interrupt routines.
192 * -------------------------------------------------------------------
193 */
194
Alan Coxf34d7a52008-04-30 00:54:13 -0700195static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct async_struct *info = (struct async_struct *)tty->driver_data;
198 unsigned long flags;
199
Alan Coxf34d7a52008-04-30 00:54:13 -0700200 if (!tty || !info->xmit.buf)
201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 local_irq_save(flags);
204 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
205 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 info->xmit.buf[info->xmit.head] = ch;
209 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
210 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700211 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800214static void transmit_chars(struct async_struct *info, int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 int count;
217 unsigned long flags;
218
219
220 local_irq_save(flags);
221
222 if (info->x_char) {
223 char c = info->x_char;
224
225 console->write(console, &c, 1);
226
227 info->state->icount.tx++;
228 info->x_char = 0;
229
230 goto out;
231 }
232
233 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
234#ifdef SIMSERIAL_DEBUG
235 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
236 info->xmit.head, info->xmit.tail, info->tty->stopped);
237#endif
238 goto out;
239 }
240 /*
241 * We removed the loop and try to do it in to chunks. We need
242 * 2 operations maximum because it's a ring buffer.
243 *
244 * First from current to tail if possible.
245 * Then from the beginning of the buffer until necessary
246 */
247
248 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
249 SERIAL_XMIT_SIZE - info->xmit.tail);
250 console->write(console, info->xmit.buf+info->xmit.tail, count);
251
252 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
253
254 /*
255 * We have more at the beginning of the buffer
256 */
257 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
258 if (count) {
259 console->write(console, info->xmit.buf, count);
260 info->xmit.tail += count;
261 }
262out:
263 local_irq_restore(flags);
264}
265
266static void rs_flush_chars(struct tty_struct *tty)
267{
268 struct async_struct *info = (struct async_struct *)tty->driver_data;
269
270 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
271 !info->xmit.buf)
272 return;
273
274 transmit_chars(info, NULL);
275}
276
277
278static int rs_write(struct tty_struct * tty,
279 const unsigned char *buf, int count)
280{
281 int c, ret = 0;
282 struct async_struct *info = (struct async_struct *)tty->driver_data;
283 unsigned long flags;
284
285 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
286
287 local_irq_save(flags);
288 while (1) {
289 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
290 if (count < c)
291 c = count;
292 if (c <= 0) {
293 break;
294 }
295 memcpy(info->xmit.buf + info->xmit.head, buf, c);
296 info->xmit.head = ((info->xmit.head + c) &
297 (SERIAL_XMIT_SIZE-1));
298 buf += c;
299 count -= c;
300 ret += c;
301 }
302 local_irq_restore(flags);
303 /*
304 * Hey, we transmit directly from here in our case
305 */
306 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
307 && !tty->stopped && !tty->hw_stopped) {
308 transmit_chars(info, NULL);
309 }
310 return ret;
311}
312
313static int rs_write_room(struct tty_struct *tty)
314{
315 struct async_struct *info = (struct async_struct *)tty->driver_data;
316
317 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
318}
319
320static int rs_chars_in_buffer(struct tty_struct *tty)
321{
322 struct async_struct *info = (struct async_struct *)tty->driver_data;
323
324 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
325}
326
327static void rs_flush_buffer(struct tty_struct *tty)
328{
329 struct async_struct *info = (struct async_struct *)tty->driver_data;
330 unsigned long flags;
331
332 local_irq_save(flags);
333 info->xmit.head = info->xmit.tail = 0;
334 local_irq_restore(flags);
335
Alan Cox15648f12008-07-16 21:52:25 +0100336 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/*
340 * This function is used to send a high-priority XON/XOFF character to
341 * the device
342 */
343static void rs_send_xchar(struct tty_struct *tty, char ch)
344{
345 struct async_struct *info = (struct async_struct *)tty->driver_data;
346
347 info->x_char = ch;
348 if (ch) {
349 /*
350 * I guess we could call console->write() directly but
351 * let's do that for now.
352 */
353 transmit_chars(info, NULL);
354 }
355}
356
357/*
358 * ------------------------------------------------------------
359 * rs_throttle()
360 *
361 * This routine is called by the upper-layer tty layer to signal that
362 * incoming characters should be throttled.
363 * ------------------------------------------------------------
364 */
365static void rs_throttle(struct tty_struct * tty)
366{
367 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
368
369 printk(KERN_INFO "simrs_throttle called\n");
370}
371
372static void rs_unthrottle(struct tty_struct * tty)
373{
374 struct async_struct *info = (struct async_struct *)tty->driver_data;
375
376 if (I_IXOFF(tty)) {
377 if (info->x_char)
378 info->x_char = 0;
379 else
380 rs_send_xchar(tty, START_CHAR(tty));
381 }
382 printk(KERN_INFO "simrs_unthrottle called\n");
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Luck, Tony10e82f62011-02-22 11:47:04 -0800386static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
389 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100390 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (tty->flags & (1 << TTY_IO_ERROR))
392 return -EIO;
393 }
394
395 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 case TIOCGSERIAL:
397 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
398 return 0;
399 case TIOCSSERIAL:
400 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
401 return 0;
402 case TIOCSERCONFIG:
403 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
404 return -EINVAL;
405
406 case TIOCSERGETLSR: /* Get line status register */
407 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
408 return -EINVAL;
409
410 case TIOCSERGSTRUCT:
411 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
412#if 0
413 if (copy_to_user((struct async_struct *) arg,
414 info, sizeof(struct async_struct)))
415 return -EFAULT;
416#endif
417 return 0;
418
419 /*
420 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
421 * - mask passed in arg for lines of interest
422 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
423 * Caller should use TIOCGICOUNT to see which one it was
424 */
425 case TIOCMIWAIT:
426 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 case TIOCSERGWILD:
429 case TIOCSERSWILD:
430 /* "setserial -W" is called in Debian boot */
431 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
432 return 0;
433
434 default:
435 return -ENOIOCTLCMD;
436 }
437 return 0;
438}
439
440#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
441
Tony Luckf889a262006-12-12 10:47:36 -0800442static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Handle turning off CRTSCTS */
445 if ((old_termios->c_cflag & CRTSCTS) &&
446 !(tty->termios->c_cflag & CRTSCTS)) {
447 tty->hw_stopped = 0;
448 rs_start(tty);
449 }
450}
451/*
452 * This routine will shutdown a serial port; interrupts are disabled, and
453 * DTR is dropped if the hangup on close termio flag is on.
454 */
455static void shutdown(struct async_struct * info)
456{
457 unsigned long flags;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100458 struct serial_state *state = info->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int retval;
460
Jiri Slaby979b6d82012-03-05 14:52:15 +0100461 if (!(state->flags & ASYNC_INITIALIZED))
462 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464#ifdef SIMSERIAL_DEBUG
465 printk("Shutting down serial port %d (irq %d)....", info->line,
466 state->irq);
467#endif
468
469 local_irq_save(flags);
470 {
471 /*
472 * First unlink the serial port from the IRQ chain...
473 */
474 if (info->next_port)
475 info->next_port->prev_port = info->prev_port;
476 if (info->prev_port)
477 info->prev_port->next_port = info->next_port;
478 else
479 IRQ_ports[state->irq] = info->next_port;
480
481 /*
482 * Free the IRQ, if necessary
483 */
484 if (state->irq && (!IRQ_ports[state->irq] ||
485 !IRQ_ports[state->irq]->next_port)) {
486 if (IRQ_ports[state->irq]) {
487 free_irq(state->irq, NULL);
488 retval = request_irq(state->irq, rs_interrupt_single,
Jiri Slaby979b6d82012-03-05 14:52:15 +0100489 IRQ_T(state), "serial",
490 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (retval)
493 printk(KERN_ERR "serial shutdown: request_irq: error %d"
494 " Couldn't reacquire IRQ.\n", retval);
495 } else
496 free_irq(state->irq, NULL);
497 }
498
499 if (info->xmit.buf) {
500 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100501 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
504 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
505
Jiri Slaby979b6d82012-03-05 14:52:15 +0100506 state->flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 local_irq_restore(flags);
509}
510
511/*
512 * ------------------------------------------------------------
513 * rs_close()
514 *
515 * This routine is called when the serial port gets closed. First, we
516 * wait for the last remaining data to be sent. Then, we unlink its
517 * async structure from the interrupt chain if necessary, and we free
518 * that IRQ if nothing is left in the chain.
519 * ------------------------------------------------------------
520 */
521static void rs_close(struct tty_struct *tty, struct file * filp)
522{
523 struct async_struct * info = (struct async_struct *)tty->driver_data;
524 struct serial_state *state;
525 unsigned long flags;
526
527 if (!info ) return;
528
529 state = info->state;
530
531 local_irq_save(flags);
532 if (tty_hung_up_p(filp)) {
533#ifdef SIMSERIAL_DEBUG
534 printk("rs_close: hung_up\n");
535#endif
536 local_irq_restore(flags);
537 return;
538 }
539#ifdef SIMSERIAL_DEBUG
540 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
541#endif
542 if ((tty->count == 1) && (state->count != 1)) {
543 /*
544 * Uh, oh. tty->count is 1, which means that the tty
545 * structure will be freed. state->count should always
546 * be one in these conditions. If it's greater than
547 * one, we've got real problems, since it means the
548 * serial port won't be shutdown.
549 */
550 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
551 "state->count is %d\n", state->count);
552 state->count = 1;
553 }
554 if (--state->count < 0) {
555 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
556 info->line, state->count);
557 state->count = 0;
558 }
559 if (state->count) {
560 local_irq_restore(flags);
561 return;
562 }
Jiri Slaby979b6d82012-03-05 14:52:15 +0100563 state->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 local_irq_restore(flags);
565
566 /*
567 * Now we wait for the transmit buffer to clear; and we notify
568 * the line discipline to only process XON/XOFF characters.
569 */
570 shutdown(info);
Alan Cox15648f12008-07-16 21:52:25 +0100571 rs_flush_buffer(tty);
572 tty_ldisc_flush(tty);
Al Virocfa7fd72006-10-10 22:46:17 +0100573 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (info->blocked_open) {
Nishanth Aravamudan9e173c02005-11-07 01:01:11 -0800575 if (info->close_delay)
576 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 wake_up_interruptible(&info->open_wait);
578 }
Jiri Slaby979b6d82012-03-05 14:52:15 +0100579 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 wake_up_interruptible(&info->close_wait);
581}
582
583/*
584 * rs_wait_until_sent() --- wait until the transmitter is empty
585 */
586static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
587{
588}
589
590
591/*
592 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
593 */
594static void rs_hangup(struct tty_struct *tty)
595{
596 struct async_struct * info = (struct async_struct *)tty->driver_data;
597 struct serial_state *state = info->state;
598
599#ifdef SIMSERIAL_DEBUG
600 printk("rs_hangup: called\n");
601#endif
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 rs_flush_buffer(tty);
Jiri Slaby979b6d82012-03-05 14:52:15 +0100604 if (state->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return;
606 shutdown(info);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 state->count = 0;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100609 state->flags &= ~ASYNC_NORMAL_ACTIVE;
Al Virocfa7fd72006-10-10 22:46:17 +0100610 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 wake_up_interruptible(&info->open_wait);
612}
613
614
615static int get_async_struct(int line, struct async_struct **ret_info)
616{
617 struct async_struct *info;
618 struct serial_state *sstate;
619
620 sstate = rs_table + line;
621 sstate->count++;
622 if (sstate->info) {
623 *ret_info = sstate->info;
624 return 0;
625 }
Yan Burman52fd9102006-12-04 14:58:35 -0800626 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!info) {
628 sstate->count--;
629 return -ENOMEM;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 init_waitqueue_head(&info->open_wait);
632 init_waitqueue_head(&info->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 info->port = sstate->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 info->xmit_fifo_size = sstate->xmit_fifo_size;
635 info->line = line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 info->state = sstate;
637 if (sstate->info) {
638 kfree(info);
639 *ret_info = sstate->info;
640 return 0;
641 }
642 *ret_info = sstate->info = info;
643 return 0;
644}
645
646static int
647startup(struct async_struct *info)
648{
649 unsigned long flags;
650 int retval=0;
David Howells40220c12006-10-09 12:19:47 +0100651 irq_handler_t handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct serial_state *state= info->state;
653 unsigned long page;
654
655 page = get_zeroed_page(GFP_KERNEL);
656 if (!page)
657 return -ENOMEM;
658
659 local_irq_save(flags);
660
Jiri Slaby979b6d82012-03-05 14:52:15 +0100661 if (state->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 free_page(page);
663 goto errout;
664 }
665
666 if (!state->port || !state->type) {
667 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
668 free_page(page);
669 goto errout;
670 }
671 if (info->xmit.buf)
672 free_page(page);
673 else
674 info->xmit.buf = (unsigned char *) page;
675
676#ifdef SIMSERIAL_DEBUG
677 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
678#endif
679
680 /*
681 * Allocate the IRQ if necessary
682 */
683 if (state->irq && (!IRQ_ports[state->irq] ||
684 !IRQ_ports[state->irq]->next_port)) {
685 if (IRQ_ports[state->irq]) {
686 retval = -EBUSY;
687 goto errout;
688 } else
689 handler = rs_interrupt_single;
690
Jiri Slaby979b6d82012-03-05 14:52:15 +0100691 retval = request_irq(state->irq, handler, IRQ_T(state),
692 "simserial", NULL);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100693 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 /*
698 * Insert serial port into IRQ chain.
699 */
Al Virocfa7fd72006-10-10 22:46:17 +0100700 info->prev_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 info->next_port = IRQ_ports[state->irq];
702 if (info->next_port)
703 info->next_port->prev_port = info;
704 IRQ_ports[state->irq] = info;
705
706 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
707
708 info->xmit.head = info->xmit.tail = 0;
709
710#if 0
711 /*
712 * Set up serial timers...
713 */
714 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
715 timer_active |= 1 << RS_TIMER;
716#endif
717
718 /*
719 * Set up the tty->alt_speed kludge
720 */
721 if (info->tty) {
Jiri Slaby979b6d82012-03-05 14:52:15 +0100722 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 info->tty->alt_speed = 57600;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100724 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 info->tty->alt_speed = 115200;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100726 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 info->tty->alt_speed = 230400;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100728 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 info->tty->alt_speed = 460800;
730 }
731
Jiri Slaby979b6d82012-03-05 14:52:15 +0100732 state->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 local_irq_restore(flags);
734 return 0;
735
736errout:
737 local_irq_restore(flags);
738 return retval;
739}
740
741
742/*
743 * This routine is called whenever a serial port is opened. It
744 * enables interrupts for a serial port, linking in its async structure into
745 * the IRQ chain. It also performs the serial-specific
746 * initialization for the tty structure.
747 */
748static int rs_open(struct tty_struct *tty, struct file * filp)
749{
750 struct async_struct *info;
Jiri Slaby410235f2012-03-05 14:52:01 +0100751 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 unsigned long page;
753
Jiri Slaby410235f2012-03-05 14:52:01 +0100754 retval = get_async_struct(tty->index, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (retval)
756 return retval;
757 tty->driver_data = info;
758 info->tty = tty;
759
760#ifdef SIMSERIAL_DEBUG
761 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
762#endif
Jiri Slaby979b6d82012-03-05 14:52:15 +0100763 info->tty->low_latency = (info->state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (!tmp_buf) {
766 page = get_zeroed_page(GFP_KERNEL);
767 if (!page)
768 return -ENOMEM;
769 if (tmp_buf)
770 free_page(page);
771 else
772 tmp_buf = (unsigned char *) page;
773 }
774
775 /*
776 * If the port is the middle of closing, bail out now
777 */
778 if (tty_hung_up_p(filp) ||
Jiri Slaby979b6d82012-03-05 14:52:15 +0100779 (info->state->flags & ASYNC_CLOSING)) {
780 if (info->state->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 interruptible_sleep_on(&info->close_wait);
782#ifdef SERIAL_DO_RESTART
Jiri Slaby979b6d82012-03-05 14:52:15 +0100783 return ((info->state->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 -EAGAIN : -ERESTARTSYS);
785#else
786 return -EAGAIN;
787#endif
788 }
789
790 /*
791 * Start up serial port
792 */
793 retval = startup(info);
794 if (retval) {
795 return retval;
796 }
797
798 /*
799 * figure out which console to use (should be one already)
800 */
801 console = console_drivers;
802 while (console) {
803 if ((console->flags & CON_ENABLED) && console->write) break;
804 console = console->next;
805 }
806
807#ifdef SIMSERIAL_DEBUG
808 printk("rs_open ttys%d successful\n", info->line);
809#endif
810 return 0;
811}
812
813/*
814 * /proc fs routines....
815 */
816
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700817static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700819 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 state->line, uart_config[state->type].name,
821 state->port, state->irq);
822}
823
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700824static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700826 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700828 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
829 for (i = 0; i < NR_PORTS; i++)
830 line_info(m, &rs_table[i]);
831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700834static int rs_proc_open(struct inode *inode, struct file *file)
835{
836 return single_open(file, rs_proc_show, NULL);
837}
838
839static const struct file_operations rs_proc_fops = {
840 .owner = THIS_MODULE,
841 .open = rs_proc_open,
842 .read = seq_read,
843 .llseek = seq_lseek,
844 .release = single_release,
845};
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*
848 * ---------------------------------------------------------------------
849 * rs_init() and friends
850 *
851 * rs_init() is called at boot-time to initialize the serial driver.
852 * ---------------------------------------------------------------------
853 */
854
855/*
856 * This routine prints out the appropriate serial driver version
857 * number, and identifies which options were configured into this
858 * driver.
859 */
860static inline void show_serial_version(void)
861{
862 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
863 printk(KERN_INFO " no serial options enabled\n");
864}
865
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700866static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .open = rs_open,
868 .close = rs_close,
869 .write = rs_write,
870 .put_char = rs_put_char,
871 .flush_chars = rs_flush_chars,
872 .write_room = rs_write_room,
873 .chars_in_buffer = rs_chars_in_buffer,
874 .flush_buffer = rs_flush_buffer,
875 .ioctl = rs_ioctl,
876 .throttle = rs_throttle,
877 .unthrottle = rs_unthrottle,
878 .send_xchar = rs_send_xchar,
879 .set_termios = rs_set_termios,
880 .stop = rs_stop,
881 .start = rs_start,
882 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700884 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885};
886
887/*
888 * The serial driver boot-time initialization code!
889 */
890static int __init
891simrs_init (void)
892{
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700893 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct serial_state *state;
895
896 if (!ia64_platform_is("hpsim"))
897 return -ENODEV;
898
Jiri Slaby410235f2012-03-05 14:52:01 +0100899 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!hp_simserial_driver)
901 return -ENOMEM;
902
903 show_serial_version();
904
905 /* Initialize the tty_driver structure */
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 hp_simserial_driver->driver_name = "simserial";
908 hp_simserial_driver->name = "ttyS";
909 hp_simserial_driver->major = TTY_MAJOR;
910 hp_simserial_driver->minor_start = 64;
911 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
912 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
913 hp_simserial_driver->init_termios = tty_std_termios;
914 hp_simserial_driver->init_termios.c_cflag =
915 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
916 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
917 tty_set_operations(hp_simserial_driver, &hp_ops);
918
919 /*
920 * Let's have a little bit of fun !
921 */
922 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
923
924 if (state->type == PORT_UNKNOWN) continue;
925
926 if (!state->irq) {
Jiri Slaby6efb6b72012-03-08 21:01:18 +0100927 if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700928 panic("%s: out of interrupt vectors!\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800929 __func__);
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700930 state->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
934 state->line,
935 state->port, state->irq,
936 uart_config[state->type].name);
937 }
938
939 if (tty_register_driver(hp_simserial_driver))
940 panic("Couldn't register simserial driver\n");
941
942 return 0;
943}
944
945#ifndef MODULE
946__initcall(simrs_init);
947#endif