blob: 2a2fe0c56119a086238edb96c57abe265e085bc6 [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>
37#include <asm/hw_irq.h>
38#include <asm/uaccess.h>
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#undef SIMSERIAL_DEBUG /* define this to get some debug information */
41
42#define KEYBOARD_INTR 3 /* must match with simulator! */
43
44#define NR_PORTS 1 /* only one port for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Gleixner121a4222006-07-01 19:29:17 -070046#define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define SSC_GETCHAR 21
49
50extern long ia64_ssc (long, long, long, long, int);
51extern void ia64_ssc_connect_irq (long intr, long irq);
52
53static char *serial_name = "SimSerial driver";
54static char *serial_version = "0.6";
55
56/*
57 * This has been extracted from asm/serial.h. We need one eventually but
58 * I don't know exactly what we're going to put in it so just fake one
59 * for now.
60 */
61#define BASE_BAUD ( 1843200 / 16 )
62
63#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
64
65/*
66 * Most of the values here are meaningless to this particular driver.
67 * However some values must be preserved for the code (leveraged from serial.c
68 * to work correctly).
69 * port must not be 0
70 * type must not be UNKNOWN
71 * So I picked arbitrary (guess from where?) values instead
72 */
73static struct serial_state rs_table[NR_PORTS]={
74 /* UART CLK PORT IRQ FLAGS */
75 { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
76};
77
78/*
79 * Just for the fun of it !
80 */
81static struct serial_uart_config uart_config[] = {
82 { "unknown", 1, 0 },
83 { "8250", 1, 0 },
84 { "16450", 1, 0 },
85 { "16550", 1, 0 },
86 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
87 { "cirrus", 1, 0 },
88 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
89 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
90 UART_STARTECH },
91 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
Al Virocfa7fd72006-10-10 22:46:17 +010092 { NULL, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95struct tty_driver *hp_simserial_driver;
96
97static struct async_struct *IRQ_ports[NR_IRQS];
98
99static struct console *console;
100
101static unsigned char *tmp_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103extern struct console *console_drivers; /* from kernel/printk.c */
104
105/*
106 * ------------------------------------------------------------
107 * rs_stop() and rs_start()
108 *
109 * This routines are called before setting or resetting tty->stopped.
110 * They enable or disable transmitter interrupts, as necessary.
111 * ------------------------------------------------------------
112 */
113static void rs_stop(struct tty_struct *tty)
114{
115#ifdef SIMSERIAL_DEBUG
116 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
117 tty->stopped, tty->hw_stopped, tty->flow_stopped);
118#endif
119
120}
121
122static void rs_start(struct tty_struct *tty)
123{
viro@ZenIV.linux.org.uke72225d2005-09-07 23:23:50 +0100124#ifdef SIMSERIAL_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 printk("rs_start: 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
Al Viro5dcded12006-10-08 14:59:19 +0100130static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 unsigned char ch;
133 static unsigned char seen_esc = 0;
134
135 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
136 if ( ch == 27 && seen_esc == 0 ) {
137 seen_esc = 1;
138 continue;
139 } else {
140 if ( seen_esc==1 && ch == 'O' ) {
141 seen_esc = 2;
142 continue;
143 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700144 if ( ch == 'P' ) /* F1 */
145 show_state();
146#ifdef CONFIG_MAGIC_SYSRQ
147 if ( ch == 'S' ) { /* F4 */
148 do
149 ch = ia64_ssc(0, 0, 0, 0,
150 SSC_GETCHAR);
151 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700152 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 seen_esc = 0;
156 continue;
157 }
158 }
159 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Andreas Schwabd50f5c52006-01-13 23:46:38 +0100161 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 tty_flip_buffer_push(tty);
165}
166
167/*
168 * This is the serial driver's interrupt routine for a single port
169 */
Al Viro5dcded12006-10-08 14:59:19 +0100170static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct async_struct * info;
173
174 /*
175 * I don't know exactly why they don't use the dev_id opaque data
176 * pointer instead of this extra lookup table
177 */
178 info = IRQ_ports[irq];
179 if (!info || !info->tty) {
180 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
181 return IRQ_NONE;
182 }
183 /*
184 * pretty simple in our case, because we only get interrupts
185 * on inbound traffic
186 */
Al Viro5dcded12006-10-08 14:59:19 +0100187 receive_chars(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return IRQ_HANDLED;
189}
190
191/*
192 * -------------------------------------------------------------------
193 * Here ends the serial interrupt routines.
194 * -------------------------------------------------------------------
195 */
196
David Howells6d5aefb2006-12-05 19:36:26 +0000197static void do_softint(struct work_struct *private_)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 printk(KERN_ERR "simserial: do_softint called\n");
200}
201
Alan Coxf34d7a52008-04-30 00:54:13 -0700202static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct async_struct *info = (struct async_struct *)tty->driver_data;
205 unsigned long flags;
206
Alan Coxf34d7a52008-04-30 00:54:13 -0700207 if (!tty || !info->xmit.buf)
208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 local_irq_save(flags);
211 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
212 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 info->xmit.buf[info->xmit.head] = ch;
216 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
217 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700218 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800221static void transmit_chars(struct async_struct *info, int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 int count;
224 unsigned long flags;
225
226
227 local_irq_save(flags);
228
229 if (info->x_char) {
230 char c = info->x_char;
231
232 console->write(console, &c, 1);
233
234 info->state->icount.tx++;
235 info->x_char = 0;
236
237 goto out;
238 }
239
240 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
241#ifdef SIMSERIAL_DEBUG
242 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
243 info->xmit.head, info->xmit.tail, info->tty->stopped);
244#endif
245 goto out;
246 }
247 /*
248 * We removed the loop and try to do it in to chunks. We need
249 * 2 operations maximum because it's a ring buffer.
250 *
251 * First from current to tail if possible.
252 * Then from the beginning of the buffer until necessary
253 */
254
255 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
256 SERIAL_XMIT_SIZE - info->xmit.tail);
257 console->write(console, info->xmit.buf+info->xmit.tail, count);
258
259 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
260
261 /*
262 * We have more at the beginning of the buffer
263 */
264 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
265 if (count) {
266 console->write(console, info->xmit.buf, count);
267 info->xmit.tail += count;
268 }
269out:
270 local_irq_restore(flags);
271}
272
273static void rs_flush_chars(struct tty_struct *tty)
274{
275 struct async_struct *info = (struct async_struct *)tty->driver_data;
276
277 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
278 !info->xmit.buf)
279 return;
280
281 transmit_chars(info, NULL);
282}
283
284
285static int rs_write(struct tty_struct * tty,
286 const unsigned char *buf, int count)
287{
288 int c, ret = 0;
289 struct async_struct *info = (struct async_struct *)tty->driver_data;
290 unsigned long flags;
291
292 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
293
294 local_irq_save(flags);
295 while (1) {
296 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
297 if (count < c)
298 c = count;
299 if (c <= 0) {
300 break;
301 }
302 memcpy(info->xmit.buf + info->xmit.head, buf, c);
303 info->xmit.head = ((info->xmit.head + c) &
304 (SERIAL_XMIT_SIZE-1));
305 buf += c;
306 count -= c;
307 ret += c;
308 }
309 local_irq_restore(flags);
310 /*
311 * Hey, we transmit directly from here in our case
312 */
313 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
314 && !tty->stopped && !tty->hw_stopped) {
315 transmit_chars(info, NULL);
316 }
317 return ret;
318}
319
320static int rs_write_room(struct tty_struct *tty)
321{
322 struct async_struct *info = (struct async_struct *)tty->driver_data;
323
324 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
325}
326
327static int rs_chars_in_buffer(struct tty_struct *tty)
328{
329 struct async_struct *info = (struct async_struct *)tty->driver_data;
330
331 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
332}
333
334static void rs_flush_buffer(struct tty_struct *tty)
335{
336 struct async_struct *info = (struct async_struct *)tty->driver_data;
337 unsigned long flags;
338
339 local_irq_save(flags);
340 info->xmit.head = info->xmit.tail = 0;
341 local_irq_restore(flags);
342
Alan Cox15648f12008-07-16 21:52:25 +0100343 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346/*
347 * This function is used to send a high-priority XON/XOFF character to
348 * the device
349 */
350static void rs_send_xchar(struct tty_struct *tty, char ch)
351{
352 struct async_struct *info = (struct async_struct *)tty->driver_data;
353
354 info->x_char = ch;
355 if (ch) {
356 /*
357 * I guess we could call console->write() directly but
358 * let's do that for now.
359 */
360 transmit_chars(info, NULL);
361 }
362}
363
364/*
365 * ------------------------------------------------------------
366 * rs_throttle()
367 *
368 * This routine is called by the upper-layer tty layer to signal that
369 * incoming characters should be throttled.
370 * ------------------------------------------------------------
371 */
372static void rs_throttle(struct tty_struct * tty)
373{
374 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
375
376 printk(KERN_INFO "simrs_throttle called\n");
377}
378
379static void rs_unthrottle(struct tty_struct * tty)
380{
381 struct async_struct *info = (struct async_struct *)tty->driver_data;
382
383 if (I_IXOFF(tty)) {
384 if (info->x_char)
385 info->x_char = 0;
386 else
387 rs_send_xchar(tty, START_CHAR(tty));
388 }
389 printk(KERN_INFO "simrs_unthrottle called\n");
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Luck, Tony10e82f62011-02-22 11:47:04 -0800393static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
396 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100397 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (tty->flags & (1 << TTY_IO_ERROR))
399 return -EIO;
400 }
401
402 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 case TIOCGSERIAL:
404 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
405 return 0;
406 case TIOCSSERIAL:
407 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
408 return 0;
409 case TIOCSERCONFIG:
410 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
411 return -EINVAL;
412
413 case TIOCSERGETLSR: /* Get line status register */
414 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
415 return -EINVAL;
416
417 case TIOCSERGSTRUCT:
418 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
419#if 0
420 if (copy_to_user((struct async_struct *) arg,
421 info, sizeof(struct async_struct)))
422 return -EFAULT;
423#endif
424 return 0;
425
426 /*
427 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
428 * - mask passed in arg for lines of interest
429 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
430 * Caller should use TIOCGICOUNT to see which one it was
431 */
432 case TIOCMIWAIT:
433 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 case TIOCSERGWILD:
436 case TIOCSERSWILD:
437 /* "setserial -W" is called in Debian boot */
438 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
439 return 0;
440
441 default:
442 return -ENOIOCTLCMD;
443 }
444 return 0;
445}
446
447#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
448
Tony Luckf889a262006-12-12 10:47:36 -0800449static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Handle turning off CRTSCTS */
452 if ((old_termios->c_cflag & CRTSCTS) &&
453 !(tty->termios->c_cflag & CRTSCTS)) {
454 tty->hw_stopped = 0;
455 rs_start(tty);
456 }
457}
458/*
459 * This routine will shutdown a serial port; interrupts are disabled, and
460 * DTR is dropped if the hangup on close termio flag is on.
461 */
462static void shutdown(struct async_struct * info)
463{
464 unsigned long flags;
465 struct serial_state *state;
466 int retval;
467
468 if (!(info->flags & ASYNC_INITIALIZED)) return;
469
470 state = info->state;
471
472#ifdef SIMSERIAL_DEBUG
473 printk("Shutting down serial port %d (irq %d)....", info->line,
474 state->irq);
475#endif
476
477 local_irq_save(flags);
478 {
479 /*
480 * First unlink the serial port from the IRQ chain...
481 */
482 if (info->next_port)
483 info->next_port->prev_port = info->prev_port;
484 if (info->prev_port)
485 info->prev_port->next_port = info->next_port;
486 else
487 IRQ_ports[state->irq] = info->next_port;
488
489 /*
490 * Free the IRQ, if necessary
491 */
492 if (state->irq && (!IRQ_ports[state->irq] ||
493 !IRQ_ports[state->irq]->next_port)) {
494 if (IRQ_ports[state->irq]) {
495 free_irq(state->irq, NULL);
496 retval = request_irq(state->irq, rs_interrupt_single,
497 IRQ_T(info), "serial", NULL);
498
499 if (retval)
500 printk(KERN_ERR "serial shutdown: request_irq: error %d"
501 " Couldn't reacquire IRQ.\n", retval);
502 } else
503 free_irq(state->irq, NULL);
504 }
505
506 if (info->xmit.buf) {
507 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100508 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
512
513 info->flags &= ~ASYNC_INITIALIZED;
514 }
515 local_irq_restore(flags);
516}
517
518/*
519 * ------------------------------------------------------------
520 * rs_close()
521 *
522 * This routine is called when the serial port gets closed. First, we
523 * wait for the last remaining data to be sent. Then, we unlink its
524 * async structure from the interrupt chain if necessary, and we free
525 * that IRQ if nothing is left in the chain.
526 * ------------------------------------------------------------
527 */
528static void rs_close(struct tty_struct *tty, struct file * filp)
529{
530 struct async_struct * info = (struct async_struct *)tty->driver_data;
531 struct serial_state *state;
532 unsigned long flags;
533
534 if (!info ) return;
535
536 state = info->state;
537
538 local_irq_save(flags);
539 if (tty_hung_up_p(filp)) {
540#ifdef SIMSERIAL_DEBUG
541 printk("rs_close: hung_up\n");
542#endif
543 local_irq_restore(flags);
544 return;
545 }
546#ifdef SIMSERIAL_DEBUG
547 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
548#endif
549 if ((tty->count == 1) && (state->count != 1)) {
550 /*
551 * Uh, oh. tty->count is 1, which means that the tty
552 * structure will be freed. state->count should always
553 * be one in these conditions. If it's greater than
554 * one, we've got real problems, since it means the
555 * serial port won't be shutdown.
556 */
557 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
558 "state->count is %d\n", state->count);
559 state->count = 1;
560 }
561 if (--state->count < 0) {
562 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
563 info->line, state->count);
564 state->count = 0;
565 }
566 if (state->count) {
567 local_irq_restore(flags);
568 return;
569 }
570 info->flags |= ASYNC_CLOSING;
571 local_irq_restore(flags);
572
573 /*
574 * Now we wait for the transmit buffer to clear; and we notify
575 * the line discipline to only process XON/XOFF characters.
576 */
577 shutdown(info);
Alan Cox15648f12008-07-16 21:52:25 +0100578 rs_flush_buffer(tty);
579 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 info->event = 0;
Al Virocfa7fd72006-10-10 22:46:17 +0100581 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (info->blocked_open) {
Nishanth Aravamudan9e173c02005-11-07 01:01:11 -0800583 if (info->close_delay)
584 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 wake_up_interruptible(&info->open_wait);
586 }
587 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
588 wake_up_interruptible(&info->close_wait);
589}
590
591/*
592 * rs_wait_until_sent() --- wait until the transmitter is empty
593 */
594static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
595{
596}
597
598
599/*
600 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
601 */
602static void rs_hangup(struct tty_struct *tty)
603{
604 struct async_struct * info = (struct async_struct *)tty->driver_data;
605 struct serial_state *state = info->state;
606
607#ifdef SIMSERIAL_DEBUG
608 printk("rs_hangup: called\n");
609#endif
610
611 state = info->state;
612
613 rs_flush_buffer(tty);
614 if (info->flags & ASYNC_CLOSING)
615 return;
616 shutdown(info);
617
618 info->event = 0;
619 state->count = 0;
620 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Al Virocfa7fd72006-10-10 22:46:17 +0100621 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 wake_up_interruptible(&info->open_wait);
623}
624
625
626static int get_async_struct(int line, struct async_struct **ret_info)
627{
628 struct async_struct *info;
629 struct serial_state *sstate;
630
631 sstate = rs_table + line;
632 sstate->count++;
633 if (sstate->info) {
634 *ret_info = sstate->info;
635 return 0;
636 }
Yan Burman52fd9102006-12-04 14:58:35 -0800637 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!info) {
639 sstate->count--;
640 return -ENOMEM;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 init_waitqueue_head(&info->open_wait);
643 init_waitqueue_head(&info->close_wait);
644 init_waitqueue_head(&info->delta_msr_wait);
645 info->magic = SERIAL_MAGIC;
646 info->port = sstate->port;
647 info->flags = sstate->flags;
648 info->xmit_fifo_size = sstate->xmit_fifo_size;
649 info->line = line;
David Howells6d5aefb2006-12-05 19:36:26 +0000650 INIT_WORK(&info->work, do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 info->state = sstate;
652 if (sstate->info) {
653 kfree(info);
654 *ret_info = sstate->info;
655 return 0;
656 }
657 *ret_info = sstate->info = info;
658 return 0;
659}
660
661static int
662startup(struct async_struct *info)
663{
664 unsigned long flags;
665 int retval=0;
David Howells40220c12006-10-09 12:19:47 +0100666 irq_handler_t handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct serial_state *state= info->state;
668 unsigned long page;
669
670 page = get_zeroed_page(GFP_KERNEL);
671 if (!page)
672 return -ENOMEM;
673
674 local_irq_save(flags);
675
676 if (info->flags & ASYNC_INITIALIZED) {
677 free_page(page);
678 goto errout;
679 }
680
681 if (!state->port || !state->type) {
682 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
683 free_page(page);
684 goto errout;
685 }
686 if (info->xmit.buf)
687 free_page(page);
688 else
689 info->xmit.buf = (unsigned char *) page;
690
691#ifdef SIMSERIAL_DEBUG
692 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
693#endif
694
695 /*
696 * Allocate the IRQ if necessary
697 */
698 if (state->irq && (!IRQ_ports[state->irq] ||
699 !IRQ_ports[state->irq]->next_port)) {
700 if (IRQ_ports[state->irq]) {
701 retval = -EBUSY;
702 goto errout;
703 } else
704 handler = rs_interrupt_single;
705
706 retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
707 if (retval) {
708 if (capable(CAP_SYS_ADMIN)) {
709 if (info->tty)
710 set_bit(TTY_IO_ERROR,
711 &info->tty->flags);
712 retval = 0;
713 }
714 goto errout;
715 }
716 }
717
718 /*
719 * Insert serial port into IRQ chain.
720 */
Al Virocfa7fd72006-10-10 22:46:17 +0100721 info->prev_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 info->next_port = IRQ_ports[state->irq];
723 if (info->next_port)
724 info->next_port->prev_port = info;
725 IRQ_ports[state->irq] = info;
726
727 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
728
729 info->xmit.head = info->xmit.tail = 0;
730
731#if 0
732 /*
733 * Set up serial timers...
734 */
735 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
736 timer_active |= 1 << RS_TIMER;
737#endif
738
739 /*
740 * Set up the tty->alt_speed kludge
741 */
742 if (info->tty) {
743 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
744 info->tty->alt_speed = 57600;
745 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
746 info->tty->alt_speed = 115200;
747 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
748 info->tty->alt_speed = 230400;
749 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
750 info->tty->alt_speed = 460800;
751 }
752
753 info->flags |= ASYNC_INITIALIZED;
754 local_irq_restore(flags);
755 return 0;
756
757errout:
758 local_irq_restore(flags);
759 return retval;
760}
761
762
763/*
764 * This routine is called whenever a serial port is opened. It
765 * enables interrupts for a serial port, linking in its async structure into
766 * the IRQ chain. It also performs the serial-specific
767 * initialization for the tty structure.
768 */
769static int rs_open(struct tty_struct *tty, struct file * filp)
770{
771 struct async_struct *info;
Jiri Slaby410235f2012-03-05 14:52:01 +0100772 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 unsigned long page;
774
Jiri Slaby410235f2012-03-05 14:52:01 +0100775 retval = get_async_struct(tty->index, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (retval)
777 return retval;
778 tty->driver_data = info;
779 info->tty = tty;
780
781#ifdef SIMSERIAL_DEBUG
782 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
783#endif
784 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
785
786 if (!tmp_buf) {
787 page = get_zeroed_page(GFP_KERNEL);
788 if (!page)
789 return -ENOMEM;
790 if (tmp_buf)
791 free_page(page);
792 else
793 tmp_buf = (unsigned char *) page;
794 }
795
796 /*
797 * If the port is the middle of closing, bail out now
798 */
799 if (tty_hung_up_p(filp) ||
800 (info->flags & ASYNC_CLOSING)) {
801 if (info->flags & ASYNC_CLOSING)
802 interruptible_sleep_on(&info->close_wait);
803#ifdef SERIAL_DO_RESTART
804 return ((info->flags & ASYNC_HUP_NOTIFY) ?
805 -EAGAIN : -ERESTARTSYS);
806#else
807 return -EAGAIN;
808#endif
809 }
810
811 /*
812 * Start up serial port
813 */
814 retval = startup(info);
815 if (retval) {
816 return retval;
817 }
818
819 /*
820 * figure out which console to use (should be one already)
821 */
822 console = console_drivers;
823 while (console) {
824 if ((console->flags & CON_ENABLED) && console->write) break;
825 console = console->next;
826 }
827
828#ifdef SIMSERIAL_DEBUG
829 printk("rs_open ttys%d successful\n", info->line);
830#endif
831 return 0;
832}
833
834/*
835 * /proc fs routines....
836 */
837
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700838static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700840 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 state->line, uart_config[state->type].name,
842 state->port, state->irq);
843}
844
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700845static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700847 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700849 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
850 for (i = 0; i < NR_PORTS; i++)
851 line_info(m, &rs_table[i]);
852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700855static int rs_proc_open(struct inode *inode, struct file *file)
856{
857 return single_open(file, rs_proc_show, NULL);
858}
859
860static const struct file_operations rs_proc_fops = {
861 .owner = THIS_MODULE,
862 .open = rs_proc_open,
863 .read = seq_read,
864 .llseek = seq_lseek,
865 .release = single_release,
866};
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
869 * ---------------------------------------------------------------------
870 * rs_init() and friends
871 *
872 * rs_init() is called at boot-time to initialize the serial driver.
873 * ---------------------------------------------------------------------
874 */
875
876/*
877 * This routine prints out the appropriate serial driver version
878 * number, and identifies which options were configured into this
879 * driver.
880 */
881static inline void show_serial_version(void)
882{
883 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
884 printk(KERN_INFO " no serial options enabled\n");
885}
886
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700887static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 .open = rs_open,
889 .close = rs_close,
890 .write = rs_write,
891 .put_char = rs_put_char,
892 .flush_chars = rs_flush_chars,
893 .write_room = rs_write_room,
894 .chars_in_buffer = rs_chars_in_buffer,
895 .flush_buffer = rs_flush_buffer,
896 .ioctl = rs_ioctl,
897 .throttle = rs_throttle,
898 .unthrottle = rs_unthrottle,
899 .send_xchar = rs_send_xchar,
900 .set_termios = rs_set_termios,
901 .stop = rs_stop,
902 .start = rs_start,
903 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700905 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906};
907
908/*
909 * The serial driver boot-time initialization code!
910 */
911static int __init
912simrs_init (void)
913{
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700914 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct serial_state *state;
916
917 if (!ia64_platform_is("hpsim"))
918 return -ENODEV;
919
Jiri Slaby410235f2012-03-05 14:52:01 +0100920 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!hp_simserial_driver)
922 return -ENOMEM;
923
924 show_serial_version();
925
926 /* Initialize the tty_driver structure */
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 hp_simserial_driver->driver_name = "simserial";
929 hp_simserial_driver->name = "ttyS";
930 hp_simserial_driver->major = TTY_MAJOR;
931 hp_simserial_driver->minor_start = 64;
932 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
933 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
934 hp_simserial_driver->init_termios = tty_std_termios;
935 hp_simserial_driver->init_termios.c_cflag =
936 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
937 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
938 tty_set_operations(hp_simserial_driver, &hp_ops);
939
940 /*
941 * Let's have a little bit of fun !
942 */
943 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
944
945 if (state->type == PORT_UNKNOWN) continue;
946
947 if (!state->irq) {
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700948 if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
949 panic("%s: out of interrupt vectors!\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800950 __func__);
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700951 state->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
953 }
954
955 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
956 state->line,
957 state->port, state->irq,
958 uart_config[state->type].name);
959 }
960
961 if (tty_register_driver(hp_simserial_driver))
962 panic("Couldn't register simserial driver\n");
963
964 return 0;
965}
966
967#ifndef MODULE
968__initcall(simrs_init);
969#endif