blob: 0d324e85379ed15bbc2159c817f7368a969ecb99 [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 */
Jiri Slaby9c8efec2012-03-05 14:52:12 +010075 { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 } /* ttyS0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
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
Alan Coxf34d7a52008-04-30 00:54:13 -0700197static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct async_struct *info = (struct async_struct *)tty->driver_data;
200 unsigned long flags;
201
Alan Coxf34d7a52008-04-30 00:54:13 -0700202 if (!tty || !info->xmit.buf)
203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 local_irq_save(flags);
206 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
207 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 info->xmit.buf[info->xmit.head] = ch;
211 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
212 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700213 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800216static void transmit_chars(struct async_struct *info, int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int count;
219 unsigned long flags;
220
221
222 local_irq_save(flags);
223
224 if (info->x_char) {
225 char c = info->x_char;
226
227 console->write(console, &c, 1);
228
229 info->state->icount.tx++;
230 info->x_char = 0;
231
232 goto out;
233 }
234
235 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
236#ifdef SIMSERIAL_DEBUG
237 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
238 info->xmit.head, info->xmit.tail, info->tty->stopped);
239#endif
240 goto out;
241 }
242 /*
243 * We removed the loop and try to do it in to chunks. We need
244 * 2 operations maximum because it's a ring buffer.
245 *
246 * First from current to tail if possible.
247 * Then from the beginning of the buffer until necessary
248 */
249
250 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
251 SERIAL_XMIT_SIZE - info->xmit.tail);
252 console->write(console, info->xmit.buf+info->xmit.tail, count);
253
254 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
255
256 /*
257 * We have more at the beginning of the buffer
258 */
259 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
260 if (count) {
261 console->write(console, info->xmit.buf, count);
262 info->xmit.tail += count;
263 }
264out:
265 local_irq_restore(flags);
266}
267
268static void rs_flush_chars(struct tty_struct *tty)
269{
270 struct async_struct *info = (struct async_struct *)tty->driver_data;
271
272 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
273 !info->xmit.buf)
274 return;
275
276 transmit_chars(info, NULL);
277}
278
279
280static int rs_write(struct tty_struct * tty,
281 const unsigned char *buf, int count)
282{
283 int c, ret = 0;
284 struct async_struct *info = (struct async_struct *)tty->driver_data;
285 unsigned long flags;
286
287 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
288
289 local_irq_save(flags);
290 while (1) {
291 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
292 if (count < c)
293 c = count;
294 if (c <= 0) {
295 break;
296 }
297 memcpy(info->xmit.buf + info->xmit.head, buf, c);
298 info->xmit.head = ((info->xmit.head + c) &
299 (SERIAL_XMIT_SIZE-1));
300 buf += c;
301 count -= c;
302 ret += c;
303 }
304 local_irq_restore(flags);
305 /*
306 * Hey, we transmit directly from here in our case
307 */
308 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
309 && !tty->stopped && !tty->hw_stopped) {
310 transmit_chars(info, NULL);
311 }
312 return ret;
313}
314
315static int rs_write_room(struct tty_struct *tty)
316{
317 struct async_struct *info = (struct async_struct *)tty->driver_data;
318
319 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
320}
321
322static int rs_chars_in_buffer(struct tty_struct *tty)
323{
324 struct async_struct *info = (struct async_struct *)tty->driver_data;
325
326 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
327}
328
329static void rs_flush_buffer(struct tty_struct *tty)
330{
331 struct async_struct *info = (struct async_struct *)tty->driver_data;
332 unsigned long flags;
333
334 local_irq_save(flags);
335 info->xmit.head = info->xmit.tail = 0;
336 local_irq_restore(flags);
337
Alan Cox15648f12008-07-16 21:52:25 +0100338 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
342 * This function is used to send a high-priority XON/XOFF character to
343 * the device
344 */
345static void rs_send_xchar(struct tty_struct *tty, char ch)
346{
347 struct async_struct *info = (struct async_struct *)tty->driver_data;
348
349 info->x_char = ch;
350 if (ch) {
351 /*
352 * I guess we could call console->write() directly but
353 * let's do that for now.
354 */
355 transmit_chars(info, NULL);
356 }
357}
358
359/*
360 * ------------------------------------------------------------
361 * rs_throttle()
362 *
363 * This routine is called by the upper-layer tty layer to signal that
364 * incoming characters should be throttled.
365 * ------------------------------------------------------------
366 */
367static void rs_throttle(struct tty_struct * tty)
368{
369 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
370
371 printk(KERN_INFO "simrs_throttle called\n");
372}
373
374static void rs_unthrottle(struct tty_struct * tty)
375{
376 struct async_struct *info = (struct async_struct *)tty->driver_data;
377
378 if (I_IXOFF(tty)) {
379 if (info->x_char)
380 info->x_char = 0;
381 else
382 rs_send_xchar(tty, START_CHAR(tty));
383 }
384 printk(KERN_INFO "simrs_unthrottle called\n");
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Luck, Tony10e82f62011-02-22 11:47:04 -0800388static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
391 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100392 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (tty->flags & (1 << TTY_IO_ERROR))
394 return -EIO;
395 }
396
397 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 case TIOCGSERIAL:
399 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
400 return 0;
401 case TIOCSSERIAL:
402 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
403 return 0;
404 case TIOCSERCONFIG:
405 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
406 return -EINVAL;
407
408 case TIOCSERGETLSR: /* Get line status register */
409 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
410 return -EINVAL;
411
412 case TIOCSERGSTRUCT:
413 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
414#if 0
415 if (copy_to_user((struct async_struct *) arg,
416 info, sizeof(struct async_struct)))
417 return -EFAULT;
418#endif
419 return 0;
420
421 /*
422 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
423 * - mask passed in arg for lines of interest
424 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
425 * Caller should use TIOCGICOUNT to see which one it was
426 */
427 case TIOCMIWAIT:
428 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 case TIOCSERGWILD:
431 case TIOCSERSWILD:
432 /* "setserial -W" is called in Debian boot */
433 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
434 return 0;
435
436 default:
437 return -ENOIOCTLCMD;
438 }
439 return 0;
440}
441
442#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
443
Tony Luckf889a262006-12-12 10:47:36 -0800444static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* Handle turning off CRTSCTS */
447 if ((old_termios->c_cflag & CRTSCTS) &&
448 !(tty->termios->c_cflag & CRTSCTS)) {
449 tty->hw_stopped = 0;
450 rs_start(tty);
451 }
452}
453/*
454 * This routine will shutdown a serial port; interrupts are disabled, and
455 * DTR is dropped if the hangup on close termio flag is on.
456 */
457static void shutdown(struct async_struct * info)
458{
459 unsigned long flags;
460 struct serial_state *state;
461 int retval;
462
463 if (!(info->flags & ASYNC_INITIALIZED)) return;
464
465 state = info->state;
466
467#ifdef SIMSERIAL_DEBUG
468 printk("Shutting down serial port %d (irq %d)....", info->line,
469 state->irq);
470#endif
471
472 local_irq_save(flags);
473 {
474 /*
475 * First unlink the serial port from the IRQ chain...
476 */
477 if (info->next_port)
478 info->next_port->prev_port = info->prev_port;
479 if (info->prev_port)
480 info->prev_port->next_port = info->next_port;
481 else
482 IRQ_ports[state->irq] = info->next_port;
483
484 /*
485 * Free the IRQ, if necessary
486 */
487 if (state->irq && (!IRQ_ports[state->irq] ||
488 !IRQ_ports[state->irq]->next_port)) {
489 if (IRQ_ports[state->irq]) {
490 free_irq(state->irq, NULL);
491 retval = request_irq(state->irq, rs_interrupt_single,
492 IRQ_T(info), "serial", NULL);
493
494 if (retval)
495 printk(KERN_ERR "serial shutdown: request_irq: error %d"
496 " Couldn't reacquire IRQ.\n", retval);
497 } else
498 free_irq(state->irq, NULL);
499 }
500
501 if (info->xmit.buf) {
502 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100503 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
507
508 info->flags &= ~ASYNC_INITIALIZED;
509 }
510 local_irq_restore(flags);
511}
512
513/*
514 * ------------------------------------------------------------
515 * rs_close()
516 *
517 * This routine is called when the serial port gets closed. First, we
518 * wait for the last remaining data to be sent. Then, we unlink its
519 * async structure from the interrupt chain if necessary, and we free
520 * that IRQ if nothing is left in the chain.
521 * ------------------------------------------------------------
522 */
523static void rs_close(struct tty_struct *tty, struct file * filp)
524{
525 struct async_struct * info = (struct async_struct *)tty->driver_data;
526 struct serial_state *state;
527 unsigned long flags;
528
529 if (!info ) return;
530
531 state = info->state;
532
533 local_irq_save(flags);
534 if (tty_hung_up_p(filp)) {
535#ifdef SIMSERIAL_DEBUG
536 printk("rs_close: hung_up\n");
537#endif
538 local_irq_restore(flags);
539 return;
540 }
541#ifdef SIMSERIAL_DEBUG
542 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
543#endif
544 if ((tty->count == 1) && (state->count != 1)) {
545 /*
546 * Uh, oh. tty->count is 1, which means that the tty
547 * structure will be freed. state->count should always
548 * be one in these conditions. If it's greater than
549 * one, we've got real problems, since it means the
550 * serial port won't be shutdown.
551 */
552 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
553 "state->count is %d\n", state->count);
554 state->count = 1;
555 }
556 if (--state->count < 0) {
557 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
558 info->line, state->count);
559 state->count = 0;
560 }
561 if (state->count) {
562 local_irq_restore(flags);
563 return;
564 }
565 info->flags |= ASYNC_CLOSING;
566 local_irq_restore(flags);
567
568 /*
569 * Now we wait for the transmit buffer to clear; and we notify
570 * the line discipline to only process XON/XOFF characters.
571 */
572 shutdown(info);
Alan Cox15648f12008-07-16 21:52:25 +0100573 rs_flush_buffer(tty);
574 tty_ldisc_flush(tty);
Al Virocfa7fd72006-10-10 22:46:17 +0100575 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (info->blocked_open) {
Nishanth Aravamudan9e173c02005-11-07 01:01:11 -0800577 if (info->close_delay)
578 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 wake_up_interruptible(&info->open_wait);
580 }
581 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
582 wake_up_interruptible(&info->close_wait);
583}
584
585/*
586 * rs_wait_until_sent() --- wait until the transmitter is empty
587 */
588static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
589{
590}
591
592
593/*
594 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
595 */
596static void rs_hangup(struct tty_struct *tty)
597{
598 struct async_struct * info = (struct async_struct *)tty->driver_data;
599 struct serial_state *state = info->state;
600
601#ifdef SIMSERIAL_DEBUG
602 printk("rs_hangup: called\n");
603#endif
604
605 state = info->state;
606
607 rs_flush_buffer(tty);
608 if (info->flags & ASYNC_CLOSING)
609 return;
610 shutdown(info);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 state->count = 0;
613 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Al Virocfa7fd72006-10-10 22:46:17 +0100614 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 wake_up_interruptible(&info->open_wait);
616}
617
618
619static int get_async_struct(int line, struct async_struct **ret_info)
620{
621 struct async_struct *info;
622 struct serial_state *sstate;
623
624 sstate = rs_table + line;
625 sstate->count++;
626 if (sstate->info) {
627 *ret_info = sstate->info;
628 return 0;
629 }
Yan Burman52fd9102006-12-04 14:58:35 -0800630 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!info) {
632 sstate->count--;
633 return -ENOMEM;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 init_waitqueue_head(&info->open_wait);
636 init_waitqueue_head(&info->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 info->port = sstate->port;
638 info->flags = sstate->flags;
639 info->xmit_fifo_size = sstate->xmit_fifo_size;
640 info->line = line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 info->state = sstate;
642 if (sstate->info) {
643 kfree(info);
644 *ret_info = sstate->info;
645 return 0;
646 }
647 *ret_info = sstate->info = info;
648 return 0;
649}
650
651static int
652startup(struct async_struct *info)
653{
654 unsigned long flags;
655 int retval=0;
David Howells40220c12006-10-09 12:19:47 +0100656 irq_handler_t handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct serial_state *state= info->state;
658 unsigned long page;
659
660 page = get_zeroed_page(GFP_KERNEL);
661 if (!page)
662 return -ENOMEM;
663
664 local_irq_save(flags);
665
666 if (info->flags & ASYNC_INITIALIZED) {
667 free_page(page);
668 goto errout;
669 }
670
671 if (!state->port || !state->type) {
672 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
673 free_page(page);
674 goto errout;
675 }
676 if (info->xmit.buf)
677 free_page(page);
678 else
679 info->xmit.buf = (unsigned char *) page;
680
681#ifdef SIMSERIAL_DEBUG
682 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
683#endif
684
685 /*
686 * Allocate the IRQ if necessary
687 */
688 if (state->irq && (!IRQ_ports[state->irq] ||
689 !IRQ_ports[state->irq]->next_port)) {
690 if (IRQ_ports[state->irq]) {
691 retval = -EBUSY;
692 goto errout;
693 } else
694 handler = rs_interrupt_single;
695
696 retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
697 if (retval) {
698 if (capable(CAP_SYS_ADMIN)) {
699 if (info->tty)
700 set_bit(TTY_IO_ERROR,
701 &info->tty->flags);
702 retval = 0;
703 }
704 goto errout;
705 }
706 }
707
708 /*
709 * Insert serial port into IRQ chain.
710 */
Al Virocfa7fd72006-10-10 22:46:17 +0100711 info->prev_port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 info->next_port = IRQ_ports[state->irq];
713 if (info->next_port)
714 info->next_port->prev_port = info;
715 IRQ_ports[state->irq] = info;
716
717 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
718
719 info->xmit.head = info->xmit.tail = 0;
720
721#if 0
722 /*
723 * Set up serial timers...
724 */
725 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
726 timer_active |= 1 << RS_TIMER;
727#endif
728
729 /*
730 * Set up the tty->alt_speed kludge
731 */
732 if (info->tty) {
733 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
734 info->tty->alt_speed = 57600;
735 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
736 info->tty->alt_speed = 115200;
737 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
738 info->tty->alt_speed = 230400;
739 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
740 info->tty->alt_speed = 460800;
741 }
742
743 info->flags |= ASYNC_INITIALIZED;
744 local_irq_restore(flags);
745 return 0;
746
747errout:
748 local_irq_restore(flags);
749 return retval;
750}
751
752
753/*
754 * This routine is called whenever a serial port is opened. It
755 * enables interrupts for a serial port, linking in its async structure into
756 * the IRQ chain. It also performs the serial-specific
757 * initialization for the tty structure.
758 */
759static int rs_open(struct tty_struct *tty, struct file * filp)
760{
761 struct async_struct *info;
Jiri Slaby410235f2012-03-05 14:52:01 +0100762 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 unsigned long page;
764
Jiri Slaby410235f2012-03-05 14:52:01 +0100765 retval = get_async_struct(tty->index, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (retval)
767 return retval;
768 tty->driver_data = info;
769 info->tty = tty;
770
771#ifdef SIMSERIAL_DEBUG
772 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
773#endif
774 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
775
776 if (!tmp_buf) {
777 page = get_zeroed_page(GFP_KERNEL);
778 if (!page)
779 return -ENOMEM;
780 if (tmp_buf)
781 free_page(page);
782 else
783 tmp_buf = (unsigned char *) page;
784 }
785
786 /*
787 * If the port is the middle of closing, bail out now
788 */
789 if (tty_hung_up_p(filp) ||
790 (info->flags & ASYNC_CLOSING)) {
791 if (info->flags & ASYNC_CLOSING)
792 interruptible_sleep_on(&info->close_wait);
793#ifdef SERIAL_DO_RESTART
794 return ((info->flags & ASYNC_HUP_NOTIFY) ?
795 -EAGAIN : -ERESTARTSYS);
796#else
797 return -EAGAIN;
798#endif
799 }
800
801 /*
802 * Start up serial port
803 */
804 retval = startup(info);
805 if (retval) {
806 return retval;
807 }
808
809 /*
810 * figure out which console to use (should be one already)
811 */
812 console = console_drivers;
813 while (console) {
814 if ((console->flags & CON_ENABLED) && console->write) break;
815 console = console->next;
816 }
817
818#ifdef SIMSERIAL_DEBUG
819 printk("rs_open ttys%d successful\n", info->line);
820#endif
821 return 0;
822}
823
824/*
825 * /proc fs routines....
826 */
827
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700828static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700830 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 state->line, uart_config[state->type].name,
832 state->port, state->irq);
833}
834
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700835static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700837 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700839 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
840 for (i = 0; i < NR_PORTS; i++)
841 line_info(m, &rs_table[i]);
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700845static int rs_proc_open(struct inode *inode, struct file *file)
846{
847 return single_open(file, rs_proc_show, NULL);
848}
849
850static const struct file_operations rs_proc_fops = {
851 .owner = THIS_MODULE,
852 .open = rs_proc_open,
853 .read = seq_read,
854 .llseek = seq_lseek,
855 .release = single_release,
856};
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*
859 * ---------------------------------------------------------------------
860 * rs_init() and friends
861 *
862 * rs_init() is called at boot-time to initialize the serial driver.
863 * ---------------------------------------------------------------------
864 */
865
866/*
867 * This routine prints out the appropriate serial driver version
868 * number, and identifies which options were configured into this
869 * driver.
870 */
871static inline void show_serial_version(void)
872{
873 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
874 printk(KERN_INFO " no serial options enabled\n");
875}
876
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700877static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 .open = rs_open,
879 .close = rs_close,
880 .write = rs_write,
881 .put_char = rs_put_char,
882 .flush_chars = rs_flush_chars,
883 .write_room = rs_write_room,
884 .chars_in_buffer = rs_chars_in_buffer,
885 .flush_buffer = rs_flush_buffer,
886 .ioctl = rs_ioctl,
887 .throttle = rs_throttle,
888 .unthrottle = rs_unthrottle,
889 .send_xchar = rs_send_xchar,
890 .set_termios = rs_set_termios,
891 .stop = rs_stop,
892 .start = rs_start,
893 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700895 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896};
897
898/*
899 * The serial driver boot-time initialization code!
900 */
901static int __init
902simrs_init (void)
903{
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700904 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct serial_state *state;
906
907 if (!ia64_platform_is("hpsim"))
908 return -ENODEV;
909
Jiri Slaby410235f2012-03-05 14:52:01 +0100910 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!hp_simserial_driver)
912 return -ENOMEM;
913
914 show_serial_version();
915
916 /* Initialize the tty_driver structure */
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 hp_simserial_driver->driver_name = "simserial";
919 hp_simserial_driver->name = "ttyS";
920 hp_simserial_driver->major = TTY_MAJOR;
921 hp_simserial_driver->minor_start = 64;
922 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
923 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
924 hp_simserial_driver->init_termios = tty_std_termios;
925 hp_simserial_driver->init_termios.c_cflag =
926 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
927 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
928 tty_set_operations(hp_simserial_driver, &hp_ops);
929
930 /*
931 * Let's have a little bit of fun !
932 */
933 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
934
935 if (state->type == PORT_UNKNOWN) continue;
936
937 if (!state->irq) {
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700938 if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
939 panic("%s: out of interrupt vectors!\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800940 __func__);
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700941 state->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
943 }
944
945 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
946 state->line,
947 state->port, state->irq,
948 uart_config[state->type].name);
949 }
950
951 if (tty_register_driver(hp_simserial_driver))
952 panic("Couldn't register simserial driver\n");
953
954 return 0;
955}
956
957#ifndef MODULE
958__initcall(simrs_init);
959#endif