blob: 64ab004b47634a68ef64780721364ce31725ad5b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static char *serial_name = "SimSerial driver";
50static char *serial_version = "0.6";
51
Jiri Slabyfd2d7a62012-03-05 14:52:28 +010052static struct serial_state rs_table[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54struct tty_driver *hp_simserial_driver;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct console *console;
57
58static unsigned char *tmp_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60extern struct console *console_drivers; /* from kernel/printk.c */
61
62/*
63 * ------------------------------------------------------------
64 * rs_stop() and rs_start()
65 *
66 * This routines are called before setting or resetting tty->stopped.
67 * They enable or disable transmitter interrupts, as necessary.
68 * ------------------------------------------------------------
69 */
70static void rs_stop(struct tty_struct *tty)
71{
72#ifdef SIMSERIAL_DEBUG
73 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
74 tty->stopped, tty->hw_stopped, tty->flow_stopped);
75#endif
76
77}
78
79static void rs_start(struct tty_struct *tty)
80{
viro@ZenIV.linux.org.uke72225d2005-09-07 23:23:50 +010081#ifdef SIMSERIAL_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
83 tty->stopped, tty->hw_stopped, tty->flow_stopped);
84#endif
85}
86
Al Viro5dcded12006-10-08 14:59:19 +010087static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 unsigned char ch;
90 static unsigned char seen_esc = 0;
91
92 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
93 if ( ch == 27 && seen_esc == 0 ) {
94 seen_esc = 1;
95 continue;
96 } else {
97 if ( seen_esc==1 && ch == 'O' ) {
98 seen_esc = 2;
99 continue;
100 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700101 if ( ch == 'P' ) /* F1 */
102 show_state();
103#ifdef CONFIG_MAGIC_SYSRQ
104 if ( ch == 'S' ) { /* F4 */
105 do
106 ch = ia64_ssc(0, 0, 0, 0,
107 SSC_GETCHAR);
108 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700109 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 seen_esc = 0;
113 continue;
114 }
115 }
116 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Andreas Schwabd50f5c52006-01-13 23:46:38 +0100118 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 tty_flip_buffer_push(tty);
122}
123
124/*
125 * This is the serial driver's interrupt routine for a single port
126 */
Al Viro5dcded12006-10-08 14:59:19 +0100127static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jiri Slaby916b7652012-03-05 14:52:20 +0100129 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jiri Slaby87758792012-03-05 14:52:24 +0100131 if (!info->tport.tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
133 return IRQ_NONE;
134 }
135 /*
136 * pretty simple in our case, because we only get interrupts
137 * on inbound traffic
138 */
Jiri Slaby87758792012-03-05 14:52:24 +0100139 receive_chars(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return IRQ_HANDLED;
141}
142
143/*
144 * -------------------------------------------------------------------
145 * Here ends the serial interrupt routines.
146 * -------------------------------------------------------------------
147 */
148
Alan Coxf34d7a52008-04-30 00:54:13 -0700149static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Jiri Slaby916b7652012-03-05 14:52:20 +0100151 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned long flags;
153
Alan Coxf34d7a52008-04-30 00:54:13 -0700154 if (!tty || !info->xmit.buf)
155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 local_irq_save(flags);
158 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
159 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 info->xmit.buf[info->xmit.head] = ch;
163 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
164 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700165 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Jiri Slaby5e99d542012-03-05 14:52:23 +0100168static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
169 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int count;
172 unsigned long flags;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 local_irq_save(flags);
175
176 if (info->x_char) {
177 char c = info->x_char;
178
179 console->write(console, &c, 1);
180
Jiri Slaby916b7652012-03-05 14:52:20 +0100181 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 info->x_char = 0;
183
184 goto out;
185 }
186
Jiri Slaby5e99d542012-03-05 14:52:23 +0100187 if (info->xmit.head == info->xmit.tail || tty->stopped ||
188 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#ifdef SIMSERIAL_DEBUG
190 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100191 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
193 goto out;
194 }
195 /*
196 * We removed the loop and try to do it in to chunks. We need
197 * 2 operations maximum because it's a ring buffer.
198 *
199 * First from current to tail if possible.
200 * Then from the beginning of the buffer until necessary
201 */
202
203 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
204 SERIAL_XMIT_SIZE - info->xmit.tail);
205 console->write(console, info->xmit.buf+info->xmit.tail, count);
206
207 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
208
209 /*
210 * We have more at the beginning of the buffer
211 */
212 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
213 if (count) {
214 console->write(console, info->xmit.buf, count);
215 info->xmit.tail += count;
216 }
217out:
218 local_irq_restore(flags);
219}
220
221static void rs_flush_chars(struct tty_struct *tty)
222{
Jiri Slaby916b7652012-03-05 14:52:20 +0100223 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
226 !info->xmit.buf)
227 return;
228
Jiri Slaby5e99d542012-03-05 14:52:23 +0100229 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232
233static int rs_write(struct tty_struct * tty,
234 const unsigned char *buf, int count)
235{
Jiri Slaby916b7652012-03-05 14:52:20 +0100236 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned long flags;
239
240 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
241
242 local_irq_save(flags);
243 while (1) {
244 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
245 if (count < c)
246 c = count;
247 if (c <= 0) {
248 break;
249 }
250 memcpy(info->xmit.buf + info->xmit.head, buf, c);
251 info->xmit.head = ((info->xmit.head + c) &
252 (SERIAL_XMIT_SIZE-1));
253 buf += c;
254 count -= c;
255 ret += c;
256 }
257 local_irq_restore(flags);
258 /*
259 * Hey, we transmit directly from here in our case
260 */
261 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
262 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100263 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 return ret;
266}
267
268static int rs_write_room(struct tty_struct *tty)
269{
Jiri Slaby916b7652012-03-05 14:52:20 +0100270 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
273}
274
275static int rs_chars_in_buffer(struct tty_struct *tty)
276{
Jiri Slaby916b7652012-03-05 14:52:20 +0100277 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
280}
281
282static void rs_flush_buffer(struct tty_struct *tty)
283{
Jiri Slaby916b7652012-03-05 14:52:20 +0100284 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned long flags;
286
287 local_irq_save(flags);
288 info->xmit.head = info->xmit.tail = 0;
289 local_irq_restore(flags);
290
Alan Cox15648f12008-07-16 21:52:25 +0100291 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * This function is used to send a high-priority XON/XOFF character to
296 * the device
297 */
298static void rs_send_xchar(struct tty_struct *tty, char ch)
299{
Jiri Slaby916b7652012-03-05 14:52:20 +0100300 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 info->x_char = ch;
303 if (ch) {
304 /*
305 * I guess we could call console->write() directly but
306 * let's do that for now.
307 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100308 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310}
311
312/*
313 * ------------------------------------------------------------
314 * rs_throttle()
315 *
316 * This routine is called by the upper-layer tty layer to signal that
317 * incoming characters should be throttled.
318 * ------------------------------------------------------------
319 */
320static void rs_throttle(struct tty_struct * tty)
321{
322 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
323
324 printk(KERN_INFO "simrs_throttle called\n");
325}
326
327static void rs_unthrottle(struct tty_struct * tty)
328{
Jiri Slaby916b7652012-03-05 14:52:20 +0100329 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (I_IXOFF(tty)) {
332 if (info->x_char)
333 info->x_char = 0;
334 else
335 rs_send_xchar(tty, START_CHAR(tty));
336 }
337 printk(KERN_INFO "simrs_unthrottle called\n");
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Luck, Tony10e82f62011-02-22 11:47:04 -0800341static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
344 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100345 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (tty->flags & (1 << TTY_IO_ERROR))
347 return -EIO;
348 }
349
350 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case TIOCGSERIAL:
352 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
353 return 0;
354 case TIOCSSERIAL:
355 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
356 return 0;
357 case TIOCSERCONFIG:
358 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
359 return -EINVAL;
360
361 case TIOCSERGETLSR: /* Get line status register */
362 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
363 return -EINVAL;
364
365 case TIOCSERGSTRUCT:
366 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
367#if 0
368 if (copy_to_user((struct async_struct *) arg,
369 info, sizeof(struct async_struct)))
370 return -EFAULT;
371#endif
372 return 0;
373
374 /*
375 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
376 * - mask passed in arg for lines of interest
377 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
378 * Caller should use TIOCGICOUNT to see which one it was
379 */
380 case TIOCMIWAIT:
381 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 case TIOCSERGWILD:
384 case TIOCSERSWILD:
385 /* "setserial -W" is called in Debian boot */
386 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
387 return 0;
388
389 default:
390 return -ENOIOCTLCMD;
391 }
392 return 0;
393}
394
395#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
396
Tony Luckf889a262006-12-12 10:47:36 -0800397static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* Handle turning off CRTSCTS */
400 if ((old_termios->c_cflag & CRTSCTS) &&
401 !(tty->termios->c_cflag & CRTSCTS)) {
402 tty->hw_stopped = 0;
403 rs_start(tty);
404 }
405}
406/*
407 * This routine will shutdown a serial port; interrupts are disabled, and
408 * DTR is dropped if the hangup on close termio flag is on.
409 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100410static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Jiri Slaby01bd7302012-03-05 14:52:27 +0100414 if (!(info->tport.flags & ASYNC_INITIALIZED))
Jiri Slaby979b6d82012-03-05 14:52:15 +0100415 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100418 printk("Shutting down serial port %d (irq %d)...\n", info->line,
419 info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#endif
421
422 local_irq_save(flags);
423 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100424 if (info->irq)
425 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (info->xmit.buf) {
428 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100429 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
Jiri Slaby5e99d542012-03-05 14:52:23 +0100432 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Jiri Slaby01bd7302012-03-05 14:52:27 +0100434 info->tport.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 local_irq_restore(flags);
437}
438
439/*
440 * ------------------------------------------------------------
441 * rs_close()
442 *
443 * This routine is called when the serial port gets closed. First, we
444 * wait for the last remaining data to be sent. Then, we unlink its
445 * async structure from the interrupt chain if necessary, and we free
446 * that IRQ if nothing is left in the chain.
447 * ------------------------------------------------------------
448 */
449static void rs_close(struct tty_struct *tty, struct file * filp)
450{
Jiri Slaby916b7652012-03-05 14:52:20 +0100451 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 unsigned long flags;
453
Jiri Slaby916b7652012-03-05 14:52:20 +0100454 if (!info)
455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 local_irq_save(flags);
458 if (tty_hung_up_p(filp)) {
459#ifdef SIMSERIAL_DEBUG
460 printk("rs_close: hung_up\n");
461#endif
462 local_irq_restore(flags);
463 return;
464 }
465#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100466 printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#endif
Jiri Slaby12c80352012-03-05 14:52:26 +0100468 if ((tty->count == 1) && (info->tport.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * Uh, oh. tty->count is 1, which means that the tty
Jiri Slaby12c80352012-03-05 14:52:26 +0100471 * structure will be freed. info->tport.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * be one in these conditions. If it's greater than
473 * one, we've got real problems, since it means the
474 * serial port won't be shutdown.
475 */
476 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby12c80352012-03-05 14:52:26 +0100477 "info->tport.count is %d\n", info->tport.count);
478 info->tport.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100480 if (--info->tport.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +0100482 info->line, info->tport.count);
483 info->tport.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100485 if (info->tport.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 local_irq_restore(flags);
487 return;
488 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100489 info->tport.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 local_irq_restore(flags);
491
492 /*
493 * Now we wait for the transmit buffer to clear; and we notify
494 * the line discipline to only process XON/XOFF characters.
495 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100496 shutdown(tty, info);
Alan Cox15648f12008-07-16 21:52:25 +0100497 rs_flush_buffer(tty);
498 tty_ldisc_flush(tty);
Jiri Slaby87758792012-03-05 14:52:24 +0100499 info->tport.tty = NULL;
500 if (info->tport.blocked_open) {
Jiri Slaby799be6f2012-03-05 14:52:25 +0100501 if (info->tport.close_delay)
502 schedule_timeout_interruptible(info->tport.close_delay);
Jiri Slaby87758792012-03-05 14:52:24 +0100503 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100505 info->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby87758792012-03-05 14:52:24 +0100506 wake_up_interruptible(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/*
510 * rs_wait_until_sent() --- wait until the transmitter is empty
511 */
512static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
513{
514}
515
516
517/*
518 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
519 */
520static void rs_hangup(struct tty_struct *tty)
521{
Jiri Slaby916b7652012-03-05 14:52:20 +0100522 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524#ifdef SIMSERIAL_DEBUG
525 printk("rs_hangup: called\n");
526#endif
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 rs_flush_buffer(tty);
Jiri Slaby01bd7302012-03-05 14:52:27 +0100529 if (info->tport.flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return;
Jiri Slaby5e99d542012-03-05 14:52:23 +0100531 shutdown(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jiri Slaby12c80352012-03-05 14:52:26 +0100533 info->tport.count = 0;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100534 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby87758792012-03-05 14:52:24 +0100535 info->tport.tty = NULL;
536 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539
Jiri Slaby5e99d542012-03-05 14:52:23 +0100540static int startup(struct tty_struct *tty, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100542 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long flags;
544 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned long page;
546
547 page = get_zeroed_page(GFP_KERNEL);
548 if (!page)
549 return -ENOMEM;
550
551 local_irq_save(flags);
552
Jiri Slaby01bd7302012-03-05 14:52:27 +0100553 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 free_page(page);
555 goto errout;
556 }
557
Jiri Slaby916b7652012-03-05 14:52:20 +0100558 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 free_page(page);
560 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100561 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100564 printk("startup: ttys%d (irq %d)...", state->line, state->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
566
567 /*
568 * Allocate the IRQ if necessary
569 */
Jiri Slaby964105b2012-03-05 14:52:17 +0100570 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100571 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100572 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100573 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Jiri Slaby5e99d542012-03-05 14:52:23 +0100577 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jiri Slaby916b7652012-03-05 14:52:20 +0100579 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581#if 0
582 /*
583 * Set up serial timers...
584 */
585 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
586 timer_active |= 1 << RS_TIMER;
587#endif
588
589 /*
590 * Set up the tty->alt_speed kludge
591 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100592 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100593 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100594 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100595 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100596 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100597 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100598 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100599 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Jiri Slaby01bd7302012-03-05 14:52:27 +0100601 port->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 local_irq_restore(flags);
603 return 0;
604
605errout:
606 local_irq_restore(flags);
607 return retval;
608}
609
610
611/*
612 * This routine is called whenever a serial port is opened. It
613 * enables interrupts for a serial port, linking in its async structure into
614 * the IRQ chain. It also performs the serial-specific
615 * initialization for the tty structure.
616 */
617static int rs_open(struct tty_struct *tty, struct file * filp)
618{
Jiri Slaby916b7652012-03-05 14:52:20 +0100619 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +0100620 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long page;
622
Jiri Slaby12c80352012-03-05 14:52:26 +0100623 info->tport.count++;
Jiri Slaby87758792012-03-05 14:52:24 +0100624 info->tport.tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +0100625 tty->driver_data = info;
Jiri Slaby87758792012-03-05 14:52:24 +0100626 tty->port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100629 printk("rs_open %s, count = %d\n", tty->name, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630#endif
Jiri Slaby01bd7302012-03-05 14:52:27 +0100631 tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (!tmp_buf) {
634 page = get_zeroed_page(GFP_KERNEL);
635 if (!page)
636 return -ENOMEM;
637 if (tmp_buf)
638 free_page(page);
639 else
640 tmp_buf = (unsigned char *) page;
641 }
642
643 /*
644 * If the port is the middle of closing, bail out now
645 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100646 if (tty_hung_up_p(filp) || (info->tport.flags & ASYNC_CLOSING)) {
647 if (info->tport.flags & ASYNC_CLOSING)
Jiri Slaby87758792012-03-05 14:52:24 +0100648 interruptible_sleep_on(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#ifdef SERIAL_DO_RESTART
Jiri Slaby01bd7302012-03-05 14:52:27 +0100650 return ((info->tport.flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 -EAGAIN : -ERESTARTSYS);
652#else
653 return -EAGAIN;
654#endif
655 }
656
657 /*
658 * Start up serial port
659 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100660 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (retval) {
662 return retval;
663 }
664
665 /*
666 * figure out which console to use (should be one already)
667 */
668 console = console_drivers;
669 while (console) {
670 if ((console->flags & CON_ENABLED) && console->write) break;
671 console = console->next;
672 }
673
674#ifdef SIMSERIAL_DEBUG
675 printk("rs_open ttys%d successful\n", info->line);
676#endif
677 return 0;
678}
679
680/*
681 * /proc fs routines....
682 */
683
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700684static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100686 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
687 state->line, state->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700690static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700692 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700694 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
695 for (i = 0; i < NR_PORTS; i++)
696 line_info(m, &rs_table[i]);
697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700700static int rs_proc_open(struct inode *inode, struct file *file)
701{
702 return single_open(file, rs_proc_show, NULL);
703}
704
705static const struct file_operations rs_proc_fops = {
706 .owner = THIS_MODULE,
707 .open = rs_proc_open,
708 .read = seq_read,
709 .llseek = seq_lseek,
710 .release = single_release,
711};
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/*
714 * ---------------------------------------------------------------------
715 * rs_init() and friends
716 *
717 * rs_init() is called at boot-time to initialize the serial driver.
718 * ---------------------------------------------------------------------
719 */
720
721/*
722 * This routine prints out the appropriate serial driver version
723 * number, and identifies which options were configured into this
724 * driver.
725 */
726static inline void show_serial_version(void)
727{
728 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
729 printk(KERN_INFO " no serial options enabled\n");
730}
731
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700732static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 .open = rs_open,
734 .close = rs_close,
735 .write = rs_write,
736 .put_char = rs_put_char,
737 .flush_chars = rs_flush_chars,
738 .write_room = rs_write_room,
739 .chars_in_buffer = rs_chars_in_buffer,
740 .flush_buffer = rs_flush_buffer,
741 .ioctl = rs_ioctl,
742 .throttle = rs_throttle,
743 .unthrottle = rs_unthrottle,
744 .send_xchar = rs_send_xchar,
745 .set_termios = rs_set_termios,
746 .stop = rs_stop,
747 .start = rs_start,
748 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700750 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751};
752
753/*
754 * The serial driver boot-time initialization code!
755 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100756static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100758 struct serial_state *state;
759 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (!ia64_platform_is("hpsim"))
762 return -ENODEV;
763
Jiri Slaby410235f2012-03-05 14:52:01 +0100764 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!hp_simserial_driver)
766 return -ENOMEM;
767
768 show_serial_version();
769
770 /* Initialize the tty_driver structure */
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 hp_simserial_driver->driver_name = "simserial";
773 hp_simserial_driver->name = "ttyS";
774 hp_simserial_driver->major = TTY_MAJOR;
775 hp_simserial_driver->minor_start = 64;
776 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
777 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
778 hp_simserial_driver->init_termios = tty_std_termios;
779 hp_simserial_driver->init_termios.c_cflag =
780 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
781 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
782 tty_set_operations(hp_simserial_driver, &hp_ops);
783
784 /*
785 * Let's have a little bit of fun !
786 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100787 state = rs_table;
788 tty_port_init(&state->tport);
789 state->tport.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100791 retval = hpsim_get_irq(KEYBOARD_INTR);
792 if (retval < 0) {
793 printk(KERN_ERR "%s: out of interrupt vectors!\n",
794 __func__);
795 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100798 state->irq = retval;
799
800 /* the port is imaginary */
801 printk(KERN_INFO "ttyS%d at 0x03f8 (irq = %d) is a 16550\n",
802 state->line, state->irq);
803
804 retval = tty_register_driver(hp_simserial_driver);
805 if (retval) {
806 printk(KERN_ERR "Couldn't register simserial driver\n");
807 goto err_free_tty;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100811err_free_tty:
812 put_tty_driver(hp_simserial_driver);
813 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816#ifndef MODULE
817__initcall(simrs_init);
818#endif