blob: c35552df035e2f79308a8a74c7e11f5fbfd1984e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct console *console;
96
97static unsigned char *tmp_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99extern struct console *console_drivers; /* from kernel/printk.c */
100
101/*
102 * ------------------------------------------------------------
103 * rs_stop() and rs_start()
104 *
105 * This routines are called before setting or resetting tty->stopped.
106 * They enable or disable transmitter interrupts, as necessary.
107 * ------------------------------------------------------------
108 */
109static void rs_stop(struct tty_struct *tty)
110{
111#ifdef SIMSERIAL_DEBUG
112 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
113 tty->stopped, tty->hw_stopped, tty->flow_stopped);
114#endif
115
116}
117
118static void rs_start(struct tty_struct *tty)
119{
viro@ZenIV.linux.org.uke72225d2005-09-07 23:23:50 +0100120#ifdef SIMSERIAL_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
122 tty->stopped, tty->hw_stopped, tty->flow_stopped);
123#endif
124}
125
Al Viro5dcded12006-10-08 14:59:19 +0100126static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 unsigned char ch;
129 static unsigned char seen_esc = 0;
130
131 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
132 if ( ch == 27 && seen_esc == 0 ) {
133 seen_esc = 1;
134 continue;
135 } else {
136 if ( seen_esc==1 && ch == 'O' ) {
137 seen_esc = 2;
138 continue;
139 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700140 if ( ch == 'P' ) /* F1 */
141 show_state();
142#ifdef CONFIG_MAGIC_SYSRQ
143 if ( ch == 'S' ) { /* F4 */
144 do
145 ch = ia64_ssc(0, 0, 0, 0,
146 SSC_GETCHAR);
147 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700148 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 seen_esc = 0;
152 continue;
153 }
154 }
155 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Andreas Schwabd50f5c52006-01-13 23:46:38 +0100157 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 tty_flip_buffer_push(tty);
161}
162
163/*
164 * This is the serial driver's interrupt routine for a single port
165 */
Al Viro5dcded12006-10-08 14:59:19 +0100166static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jiri Slaby964105b2012-03-05 14:52:17 +0100168 struct async_struct *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Jiri Slaby964105b2012-03-05 14:52:17 +0100170 if (!info->tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
172 return IRQ_NONE;
173 }
174 /*
175 * pretty simple in our case, because we only get interrupts
176 * on inbound traffic
177 */
Al Viro5dcded12006-10-08 14:59:19 +0100178 receive_chars(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return IRQ_HANDLED;
180}
181
182/*
183 * -------------------------------------------------------------------
184 * Here ends the serial interrupt routines.
185 * -------------------------------------------------------------------
186 */
187
Alan Coxf34d7a52008-04-30 00:54:13 -0700188static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct async_struct *info = (struct async_struct *)tty->driver_data;
191 unsigned long flags;
192
Alan Coxf34d7a52008-04-30 00:54:13 -0700193 if (!tty || !info->xmit.buf)
194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 local_irq_save(flags);
197 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
198 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 info->xmit.buf[info->xmit.head] = ch;
202 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
203 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700204 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800207static void transmit_chars(struct async_struct *info, int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int count;
210 unsigned long flags;
211
212
213 local_irq_save(flags);
214
215 if (info->x_char) {
216 char c = info->x_char;
217
218 console->write(console, &c, 1);
219
220 info->state->icount.tx++;
221 info->x_char = 0;
222
223 goto out;
224 }
225
226 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
227#ifdef SIMSERIAL_DEBUG
228 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
229 info->xmit.head, info->xmit.tail, info->tty->stopped);
230#endif
231 goto out;
232 }
233 /*
234 * We removed the loop and try to do it in to chunks. We need
235 * 2 operations maximum because it's a ring buffer.
236 *
237 * First from current to tail if possible.
238 * Then from the beginning of the buffer until necessary
239 */
240
241 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
242 SERIAL_XMIT_SIZE - info->xmit.tail);
243 console->write(console, info->xmit.buf+info->xmit.tail, count);
244
245 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
246
247 /*
248 * We have more at the beginning of the buffer
249 */
250 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
251 if (count) {
252 console->write(console, info->xmit.buf, count);
253 info->xmit.tail += count;
254 }
255out:
256 local_irq_restore(flags);
257}
258
259static void rs_flush_chars(struct tty_struct *tty)
260{
261 struct async_struct *info = (struct async_struct *)tty->driver_data;
262
263 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
264 !info->xmit.buf)
265 return;
266
267 transmit_chars(info, NULL);
268}
269
270
271static int rs_write(struct tty_struct * tty,
272 const unsigned char *buf, int count)
273{
274 int c, ret = 0;
275 struct async_struct *info = (struct async_struct *)tty->driver_data;
276 unsigned long flags;
277
278 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
279
280 local_irq_save(flags);
281 while (1) {
282 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
283 if (count < c)
284 c = count;
285 if (c <= 0) {
286 break;
287 }
288 memcpy(info->xmit.buf + info->xmit.head, buf, c);
289 info->xmit.head = ((info->xmit.head + c) &
290 (SERIAL_XMIT_SIZE-1));
291 buf += c;
292 count -= c;
293 ret += c;
294 }
295 local_irq_restore(flags);
296 /*
297 * Hey, we transmit directly from here in our case
298 */
299 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
300 && !tty->stopped && !tty->hw_stopped) {
301 transmit_chars(info, NULL);
302 }
303 return ret;
304}
305
306static int rs_write_room(struct tty_struct *tty)
307{
308 struct async_struct *info = (struct async_struct *)tty->driver_data;
309
310 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
311}
312
313static int rs_chars_in_buffer(struct tty_struct *tty)
314{
315 struct async_struct *info = (struct async_struct *)tty->driver_data;
316
317 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
318}
319
320static void rs_flush_buffer(struct tty_struct *tty)
321{
322 struct async_struct *info = (struct async_struct *)tty->driver_data;
323 unsigned long flags;
324
325 local_irq_save(flags);
326 info->xmit.head = info->xmit.tail = 0;
327 local_irq_restore(flags);
328
Alan Cox15648f12008-07-16 21:52:25 +0100329 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
333 * This function is used to send a high-priority XON/XOFF character to
334 * the device
335 */
336static void rs_send_xchar(struct tty_struct *tty, char ch)
337{
338 struct async_struct *info = (struct async_struct *)tty->driver_data;
339
340 info->x_char = ch;
341 if (ch) {
342 /*
343 * I guess we could call console->write() directly but
344 * let's do that for now.
345 */
346 transmit_chars(info, NULL);
347 }
348}
349
350/*
351 * ------------------------------------------------------------
352 * rs_throttle()
353 *
354 * This routine is called by the upper-layer tty layer to signal that
355 * incoming characters should be throttled.
356 * ------------------------------------------------------------
357 */
358static void rs_throttle(struct tty_struct * tty)
359{
360 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
361
362 printk(KERN_INFO "simrs_throttle called\n");
363}
364
365static void rs_unthrottle(struct tty_struct * tty)
366{
367 struct async_struct *info = (struct async_struct *)tty->driver_data;
368
369 if (I_IXOFF(tty)) {
370 if (info->x_char)
371 info->x_char = 0;
372 else
373 rs_send_xchar(tty, START_CHAR(tty));
374 }
375 printk(KERN_INFO "simrs_unthrottle called\n");
376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Luck, Tony10e82f62011-02-22 11:47:04 -0800379static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
382 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100383 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (tty->flags & (1 << TTY_IO_ERROR))
385 return -EIO;
386 }
387
388 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 case TIOCGSERIAL:
390 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
391 return 0;
392 case TIOCSSERIAL:
393 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
394 return 0;
395 case TIOCSERCONFIG:
396 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
397 return -EINVAL;
398
399 case TIOCSERGETLSR: /* Get line status register */
400 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
401 return -EINVAL;
402
403 case TIOCSERGSTRUCT:
404 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
405#if 0
406 if (copy_to_user((struct async_struct *) arg,
407 info, sizeof(struct async_struct)))
408 return -EFAULT;
409#endif
410 return 0;
411
412 /*
413 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
414 * - mask passed in arg for lines of interest
415 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
416 * Caller should use TIOCGICOUNT to see which one it was
417 */
418 case TIOCMIWAIT:
419 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 case TIOCSERGWILD:
422 case TIOCSERSWILD:
423 /* "setserial -W" is called in Debian boot */
424 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
425 return 0;
426
427 default:
428 return -ENOIOCTLCMD;
429 }
430 return 0;
431}
432
433#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
434
Tony Luckf889a262006-12-12 10:47:36 -0800435static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Handle turning off CRTSCTS */
438 if ((old_termios->c_cflag & CRTSCTS) &&
439 !(tty->termios->c_cflag & CRTSCTS)) {
440 tty->hw_stopped = 0;
441 rs_start(tty);
442 }
443}
444/*
445 * This routine will shutdown a serial port; interrupts are disabled, and
446 * DTR is dropped if the hangup on close termio flag is on.
447 */
448static void shutdown(struct async_struct * info)
449{
450 unsigned long flags;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100451 struct serial_state *state = info->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jiri Slaby979b6d82012-03-05 14:52:15 +0100453 if (!(state->flags & ASYNC_INITIALIZED))
454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456#ifdef SIMSERIAL_DEBUG
457 printk("Shutting down serial port %d (irq %d)....", info->line,
458 state->irq);
459#endif
460
461 local_irq_save(flags);
462 {
Jiri Slaby964105b2012-03-05 14:52:17 +0100463 if (state->irq)
464 free_irq(state->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (info->xmit.buf) {
467 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100468 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
472
Jiri Slaby979b6d82012-03-05 14:52:15 +0100473 state->flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 local_irq_restore(flags);
476}
477
478/*
479 * ------------------------------------------------------------
480 * rs_close()
481 *
482 * This routine is called when the serial port gets closed. First, we
483 * wait for the last remaining data to be sent. Then, we unlink its
484 * async structure from the interrupt chain if necessary, and we free
485 * that IRQ if nothing is left in the chain.
486 * ------------------------------------------------------------
487 */
488static void rs_close(struct tty_struct *tty, struct file * filp)
489{
490 struct async_struct * info = (struct async_struct *)tty->driver_data;
491 struct serial_state *state;
492 unsigned long flags;
493
494 if (!info ) return;
495
496 state = info->state;
497
498 local_irq_save(flags);
499 if (tty_hung_up_p(filp)) {
500#ifdef SIMSERIAL_DEBUG
501 printk("rs_close: hung_up\n");
502#endif
503 local_irq_restore(flags);
504 return;
505 }
506#ifdef SIMSERIAL_DEBUG
507 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
508#endif
509 if ((tty->count == 1) && (state->count != 1)) {
510 /*
511 * Uh, oh. tty->count is 1, which means that the tty
512 * structure will be freed. state->count should always
513 * be one in these conditions. If it's greater than
514 * one, we've got real problems, since it means the
515 * serial port won't be shutdown.
516 */
517 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
518 "state->count is %d\n", state->count);
519 state->count = 1;
520 }
521 if (--state->count < 0) {
522 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slabyd8522562012-03-05 14:52:16 +0100523 state->line, state->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 state->count = 0;
525 }
526 if (state->count) {
527 local_irq_restore(flags);
528 return;
529 }
Jiri Slaby979b6d82012-03-05 14:52:15 +0100530 state->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 local_irq_restore(flags);
532
533 /*
534 * Now we wait for the transmit buffer to clear; and we notify
535 * the line discipline to only process XON/XOFF characters.
536 */
537 shutdown(info);
Alan Cox15648f12008-07-16 21:52:25 +0100538 rs_flush_buffer(tty);
539 tty_ldisc_flush(tty);
Al Virocfa7fd72006-10-10 22:46:17 +0100540 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (info->blocked_open) {
Jiri Slabyd8522562012-03-05 14:52:16 +0100542 if (state->close_delay)
543 schedule_timeout_interruptible(state->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 wake_up_interruptible(&info->open_wait);
545 }
Jiri Slaby979b6d82012-03-05 14:52:15 +0100546 state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 wake_up_interruptible(&info->close_wait);
548}
549
550/*
551 * rs_wait_until_sent() --- wait until the transmitter is empty
552 */
553static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
554{
555}
556
557
558/*
559 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
560 */
561static void rs_hangup(struct tty_struct *tty)
562{
563 struct async_struct * info = (struct async_struct *)tty->driver_data;
564 struct serial_state *state = info->state;
565
566#ifdef SIMSERIAL_DEBUG
567 printk("rs_hangup: called\n");
568#endif
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 rs_flush_buffer(tty);
Jiri Slaby979b6d82012-03-05 14:52:15 +0100571 if (state->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
573 shutdown(info);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 state->count = 0;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100576 state->flags &= ~ASYNC_NORMAL_ACTIVE;
Al Virocfa7fd72006-10-10 22:46:17 +0100577 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 wake_up_interruptible(&info->open_wait);
579}
580
581
582static int get_async_struct(int line, struct async_struct **ret_info)
583{
584 struct async_struct *info;
585 struct serial_state *sstate;
586
587 sstate = rs_table + line;
588 sstate->count++;
589 if (sstate->info) {
590 *ret_info = sstate->info;
591 return 0;
592 }
Yan Burman52fd9102006-12-04 14:58:35 -0800593 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!info) {
595 sstate->count--;
596 return -ENOMEM;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 init_waitqueue_head(&info->open_wait);
599 init_waitqueue_head(&info->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 info->state = sstate;
601 if (sstate->info) {
602 kfree(info);
603 *ret_info = sstate->info;
604 return 0;
605 }
606 *ret_info = sstate->info = info;
607 return 0;
608}
609
610static int
611startup(struct async_struct *info)
612{
613 unsigned long flags;
614 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct serial_state *state= info->state;
616 unsigned long page;
617
618 page = get_zeroed_page(GFP_KERNEL);
619 if (!page)
620 return -ENOMEM;
621
622 local_irq_save(flags);
623
Jiri Slaby979b6d82012-03-05 14:52:15 +0100624 if (state->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 free_page(page);
626 goto errout;
627 }
628
629 if (!state->port || !state->type) {
630 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
631 free_page(page);
632 goto errout;
633 }
634 if (info->xmit.buf)
635 free_page(page);
636 else
637 info->xmit.buf = (unsigned char *) page;
638
639#ifdef SIMSERIAL_DEBUG
640 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
641#endif
642
643 /*
644 * Allocate the IRQ if necessary
645 */
Jiri Slaby964105b2012-03-05 14:52:17 +0100646 if (state->irq) {
647 retval = request_irq(state->irq, rs_interrupt_single,
648 IRQ_T(state), "simserial", info);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100649 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
654
655 info->xmit.head = info->xmit.tail = 0;
656
657#if 0
658 /*
659 * Set up serial timers...
660 */
661 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
662 timer_active |= 1 << RS_TIMER;
663#endif
664
665 /*
666 * Set up the tty->alt_speed kludge
667 */
668 if (info->tty) {
Jiri Slaby979b6d82012-03-05 14:52:15 +0100669 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 info->tty->alt_speed = 57600;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100671 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 info->tty->alt_speed = 115200;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100673 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 info->tty->alt_speed = 230400;
Jiri Slaby979b6d82012-03-05 14:52:15 +0100675 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 info->tty->alt_speed = 460800;
677 }
678
Jiri Slaby979b6d82012-03-05 14:52:15 +0100679 state->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 local_irq_restore(flags);
681 return 0;
682
683errout:
684 local_irq_restore(flags);
685 return retval;
686}
687
688
689/*
690 * This routine is called whenever a serial port is opened. It
691 * enables interrupts for a serial port, linking in its async structure into
692 * the IRQ chain. It also performs the serial-specific
693 * initialization for the tty structure.
694 */
695static int rs_open(struct tty_struct *tty, struct file * filp)
696{
697 struct async_struct *info;
Jiri Slaby410235f2012-03-05 14:52:01 +0100698 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 unsigned long page;
700
Jiri Slaby410235f2012-03-05 14:52:01 +0100701 retval = get_async_struct(tty->index, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (retval)
703 return retval;
704 tty->driver_data = info;
705 info->tty = tty;
706
707#ifdef SIMSERIAL_DEBUG
708 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
709#endif
Jiri Slaby979b6d82012-03-05 14:52:15 +0100710 info->tty->low_latency = (info->state->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (!tmp_buf) {
713 page = get_zeroed_page(GFP_KERNEL);
714 if (!page)
715 return -ENOMEM;
716 if (tmp_buf)
717 free_page(page);
718 else
719 tmp_buf = (unsigned char *) page;
720 }
721
722 /*
723 * If the port is the middle of closing, bail out now
724 */
725 if (tty_hung_up_p(filp) ||
Jiri Slaby979b6d82012-03-05 14:52:15 +0100726 (info->state->flags & ASYNC_CLOSING)) {
727 if (info->state->flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 interruptible_sleep_on(&info->close_wait);
729#ifdef SERIAL_DO_RESTART
Jiri Slaby979b6d82012-03-05 14:52:15 +0100730 return ((info->state->flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 -EAGAIN : -ERESTARTSYS);
732#else
733 return -EAGAIN;
734#endif
735 }
736
737 /*
738 * Start up serial port
739 */
740 retval = startup(info);
741 if (retval) {
742 return retval;
743 }
744
745 /*
746 * figure out which console to use (should be one already)
747 */
748 console = console_drivers;
749 while (console) {
750 if ((console->flags & CON_ENABLED) && console->write) break;
751 console = console->next;
752 }
753
754#ifdef SIMSERIAL_DEBUG
755 printk("rs_open ttys%d successful\n", info->line);
756#endif
757 return 0;
758}
759
760/*
761 * /proc fs routines....
762 */
763
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700764static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700766 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 state->line, uart_config[state->type].name,
768 state->port, state->irq);
769}
770
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700771static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700773 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700775 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
776 for (i = 0; i < NR_PORTS; i++)
777 line_info(m, &rs_table[i]);
778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700781static int rs_proc_open(struct inode *inode, struct file *file)
782{
783 return single_open(file, rs_proc_show, NULL);
784}
785
786static const struct file_operations rs_proc_fops = {
787 .owner = THIS_MODULE,
788 .open = rs_proc_open,
789 .read = seq_read,
790 .llseek = seq_lseek,
791 .release = single_release,
792};
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/*
795 * ---------------------------------------------------------------------
796 * rs_init() and friends
797 *
798 * rs_init() is called at boot-time to initialize the serial driver.
799 * ---------------------------------------------------------------------
800 */
801
802/*
803 * This routine prints out the appropriate serial driver version
804 * number, and identifies which options were configured into this
805 * driver.
806 */
807static inline void show_serial_version(void)
808{
809 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
810 printk(KERN_INFO " no serial options enabled\n");
811}
812
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700813static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 .open = rs_open,
815 .close = rs_close,
816 .write = rs_write,
817 .put_char = rs_put_char,
818 .flush_chars = rs_flush_chars,
819 .write_room = rs_write_room,
820 .chars_in_buffer = rs_chars_in_buffer,
821 .flush_buffer = rs_flush_buffer,
822 .ioctl = rs_ioctl,
823 .throttle = rs_throttle,
824 .unthrottle = rs_unthrottle,
825 .send_xchar = rs_send_xchar,
826 .set_termios = rs_set_termios,
827 .stop = rs_stop,
828 .start = rs_start,
829 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700831 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
834/*
835 * The serial driver boot-time initialization code!
836 */
837static int __init
838simrs_init (void)
839{
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700840 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct serial_state *state;
842
843 if (!ia64_platform_is("hpsim"))
844 return -ENODEV;
845
Jiri Slaby410235f2012-03-05 14:52:01 +0100846 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (!hp_simserial_driver)
848 return -ENOMEM;
849
850 show_serial_version();
851
852 /* Initialize the tty_driver structure */
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 hp_simserial_driver->driver_name = "simserial";
855 hp_simserial_driver->name = "ttyS";
856 hp_simserial_driver->major = TTY_MAJOR;
857 hp_simserial_driver->minor_start = 64;
858 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
859 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
860 hp_simserial_driver->init_termios = tty_std_termios;
861 hp_simserial_driver->init_termios.c_cflag =
862 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
863 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
864 tty_set_operations(hp_simserial_driver, &hp_ops);
865
866 /*
867 * Let's have a little bit of fun !
868 */
869 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
870
871 if (state->type == PORT_UNKNOWN) continue;
872
873 if (!state->irq) {
Jiri Slaby6efb6b72012-03-08 21:01:18 +0100874 if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700875 panic("%s: out of interrupt vectors!\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800876 __func__);
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700877 state->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
881 state->line,
882 state->port, state->irq,
883 uart_config[state->type].name);
884 }
885
886 if (tty_register_driver(hp_simserial_driver))
887 panic("Couldn't register simserial driver\n");
888
889 return 0;
890}
891
892#ifndef MODULE
893__initcall(simrs_init);
894#endif