blob: 3698a2fe221ddbaff86e262dd7844ca6e5644b7b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070058extern struct console *console_drivers; /* from kernel/printk.c */
59
60/*
61 * ------------------------------------------------------------
62 * rs_stop() and rs_start()
63 *
64 * This routines are called before setting or resetting tty->stopped.
65 * They enable or disable transmitter interrupts, as necessary.
66 * ------------------------------------------------------------
67 */
68static void rs_stop(struct tty_struct *tty)
69{
70#ifdef SIMSERIAL_DEBUG
71 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
72 tty->stopped, tty->hw_stopped, tty->flow_stopped);
73#endif
74
75}
76
77static void rs_start(struct tty_struct *tty)
78{
viro@ZenIV.linux.org.uke72225d2005-09-07 23:23:50 +010079#ifdef SIMSERIAL_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
81 tty->stopped, tty->hw_stopped, tty->flow_stopped);
82#endif
83}
84
Al Viro5dcded12006-10-08 14:59:19 +010085static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 unsigned char ch;
88 static unsigned char seen_esc = 0;
89
90 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
91 if ( ch == 27 && seen_esc == 0 ) {
92 seen_esc = 1;
93 continue;
94 } else {
95 if ( seen_esc==1 && ch == 'O' ) {
96 seen_esc = 2;
97 continue;
98 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070099 if ( ch == 'P' ) /* F1 */
100 show_state();
101#ifdef CONFIG_MAGIC_SYSRQ
102 if ( ch == 'S' ) { /* F4 */
103 do
104 ch = ia64_ssc(0, 0, 0, 0,
105 SSC_GETCHAR);
106 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700107 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 seen_esc = 0;
111 continue;
112 }
113 }
114 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Andreas Schwabd50f5c52006-01-13 23:46:38 +0100116 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 tty_flip_buffer_push(tty);
120}
121
122/*
123 * This is the serial driver's interrupt routine for a single port
124 */
Al Viro5dcded12006-10-08 14:59:19 +0100125static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jiri Slaby916b7652012-03-05 14:52:20 +0100127 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jiri Slaby87758792012-03-05 14:52:24 +0100129 if (!info->tport.tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
131 return IRQ_NONE;
132 }
133 /*
134 * pretty simple in our case, because we only get interrupts
135 * on inbound traffic
136 */
Jiri Slaby87758792012-03-05 14:52:24 +0100137 receive_chars(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return IRQ_HANDLED;
139}
140
141/*
142 * -------------------------------------------------------------------
143 * Here ends the serial interrupt routines.
144 * -------------------------------------------------------------------
145 */
146
Alan Coxf34d7a52008-04-30 00:54:13 -0700147static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Jiri Slaby916b7652012-03-05 14:52:20 +0100149 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long flags;
151
Alan Coxf34d7a52008-04-30 00:54:13 -0700152 if (!tty || !info->xmit.buf)
153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 local_irq_save(flags);
156 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
157 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 info->xmit.buf[info->xmit.head] = ch;
161 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
162 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700163 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jiri Slaby5e99d542012-03-05 14:52:23 +0100166static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
167 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 int count;
170 unsigned long flags;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 local_irq_save(flags);
173
174 if (info->x_char) {
175 char c = info->x_char;
176
177 console->write(console, &c, 1);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 info->x_char = 0;
180
181 goto out;
182 }
183
Jiri Slaby5e99d542012-03-05 14:52:23 +0100184 if (info->xmit.head == info->xmit.tail || tty->stopped ||
185 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#ifdef SIMSERIAL_DEBUG
187 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100188 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190 goto out;
191 }
192 /*
193 * We removed the loop and try to do it in to chunks. We need
194 * 2 operations maximum because it's a ring buffer.
195 *
196 * First from current to tail if possible.
197 * Then from the beginning of the buffer until necessary
198 */
199
200 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
201 SERIAL_XMIT_SIZE - info->xmit.tail);
202 console->write(console, info->xmit.buf+info->xmit.tail, count);
203
204 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
205
206 /*
207 * We have more at the beginning of the buffer
208 */
209 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
210 if (count) {
211 console->write(console, info->xmit.buf, count);
212 info->xmit.tail += count;
213 }
214out:
215 local_irq_restore(flags);
216}
217
218static void rs_flush_chars(struct tty_struct *tty)
219{
Jiri Slaby916b7652012-03-05 14:52:20 +0100220 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
223 !info->xmit.buf)
224 return;
225
Jiri Slaby5e99d542012-03-05 14:52:23 +0100226 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229
230static int rs_write(struct tty_struct * tty,
231 const unsigned char *buf, int count)
232{
Jiri Slaby916b7652012-03-05 14:52:20 +0100233 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 unsigned long flags;
236
Jiri Slabyd88405d2012-03-05 14:52:29 +0100237 if (!tty || !info->xmit.buf)
238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 local_irq_save(flags);
241 while (1) {
242 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
243 if (count < c)
244 c = count;
245 if (c <= 0) {
246 break;
247 }
248 memcpy(info->xmit.buf + info->xmit.head, buf, c);
249 info->xmit.head = ((info->xmit.head + c) &
250 (SERIAL_XMIT_SIZE-1));
251 buf += c;
252 count -= c;
253 ret += c;
254 }
255 local_irq_restore(flags);
256 /*
257 * Hey, we transmit directly from here in our case
258 */
259 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
260 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100261 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 return ret;
264}
265
266static int rs_write_room(struct tty_struct *tty)
267{
Jiri Slaby916b7652012-03-05 14:52:20 +0100268 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
271}
272
273static int rs_chars_in_buffer(struct tty_struct *tty)
274{
Jiri Slaby916b7652012-03-05 14:52:20 +0100275 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
278}
279
280static void rs_flush_buffer(struct tty_struct *tty)
281{
Jiri Slaby916b7652012-03-05 14:52:20 +0100282 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned long flags;
284
285 local_irq_save(flags);
286 info->xmit.head = info->xmit.tail = 0;
287 local_irq_restore(flags);
288
Alan Cox15648f12008-07-16 21:52:25 +0100289 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/*
293 * This function is used to send a high-priority XON/XOFF character to
294 * the device
295 */
296static void rs_send_xchar(struct tty_struct *tty, char ch)
297{
Jiri Slaby916b7652012-03-05 14:52:20 +0100298 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 info->x_char = ch;
301 if (ch) {
302 /*
303 * I guess we could call console->write() directly but
304 * let's do that for now.
305 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100306 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308}
309
310/*
311 * ------------------------------------------------------------
312 * rs_throttle()
313 *
314 * This routine is called by the upper-layer tty layer to signal that
315 * incoming characters should be throttled.
316 * ------------------------------------------------------------
317 */
318static void rs_throttle(struct tty_struct * tty)
319{
320 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
321
322 printk(KERN_INFO "simrs_throttle called\n");
323}
324
325static void rs_unthrottle(struct tty_struct * tty)
326{
Jiri Slaby916b7652012-03-05 14:52:20 +0100327 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (I_IXOFF(tty)) {
330 if (info->x_char)
331 info->x_char = 0;
332 else
333 rs_send_xchar(tty, START_CHAR(tty));
334 }
335 printk(KERN_INFO "simrs_unthrottle called\n");
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Luck, Tony10e82f62011-02-22 11:47:04 -0800339static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
342 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100343 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (tty->flags & (1 << TTY_IO_ERROR))
345 return -EIO;
346 }
347
348 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 case TIOCGSERIAL:
350 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
351 return 0;
352 case TIOCSSERIAL:
353 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
354 return 0;
355 case TIOCSERCONFIG:
356 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
357 return -EINVAL;
358
359 case TIOCSERGETLSR: /* Get line status register */
360 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
361 return -EINVAL;
362
363 case TIOCSERGSTRUCT:
364 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
365#if 0
366 if (copy_to_user((struct async_struct *) arg,
367 info, sizeof(struct async_struct)))
368 return -EFAULT;
369#endif
370 return 0;
371
372 /*
373 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
374 * - mask passed in arg for lines of interest
375 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
376 * Caller should use TIOCGICOUNT to see which one it was
377 */
378 case TIOCMIWAIT:
379 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case TIOCSERGWILD:
382 case TIOCSERSWILD:
383 /* "setserial -W" is called in Debian boot */
384 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
385 return 0;
386
387 default:
388 return -ENOIOCTLCMD;
389 }
390 return 0;
391}
392
393#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
394
Tony Luckf889a262006-12-12 10:47:36 -0800395static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Handle turning off CRTSCTS */
398 if ((old_termios->c_cflag & CRTSCTS) &&
399 !(tty->termios->c_cflag & CRTSCTS)) {
400 tty->hw_stopped = 0;
401 rs_start(tty);
402 }
403}
404/*
405 * This routine will shutdown a serial port; interrupts are disabled, and
406 * DTR is dropped if the hangup on close termio flag is on.
407 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100408static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Jiri Slaby01bd7302012-03-05 14:52:27 +0100412 if (!(info->tport.flags & ASYNC_INITIALIZED))
Jiri Slaby979b6d82012-03-05 14:52:15 +0100413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100416 printk("Shutting down serial port %d (irq %d)...\n", info->line,
417 info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#endif
419
420 local_irq_save(flags);
421 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100422 if (info->irq)
423 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (info->xmit.buf) {
426 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100427 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Jiri Slaby5e99d542012-03-05 14:52:23 +0100430 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jiri Slaby01bd7302012-03-05 14:52:27 +0100432 info->tport.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 local_irq_restore(flags);
435}
436
437/*
438 * ------------------------------------------------------------
439 * rs_close()
440 *
441 * This routine is called when the serial port gets closed. First, we
442 * wait for the last remaining data to be sent. Then, we unlink its
443 * async structure from the interrupt chain if necessary, and we free
444 * that IRQ if nothing is left in the chain.
445 * ------------------------------------------------------------
446 */
447static void rs_close(struct tty_struct *tty, struct file * filp)
448{
Jiri Slaby916b7652012-03-05 14:52:20 +0100449 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 unsigned long flags;
451
Jiri Slaby916b7652012-03-05 14:52:20 +0100452 if (!info)
453 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 local_irq_save(flags);
456 if (tty_hung_up_p(filp)) {
457#ifdef SIMSERIAL_DEBUG
458 printk("rs_close: hung_up\n");
459#endif
460 local_irq_restore(flags);
461 return;
462 }
463#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100464 printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#endif
Jiri Slaby12c80352012-03-05 14:52:26 +0100466 if ((tty->count == 1) && (info->tport.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
468 * Uh, oh. tty->count is 1, which means that the tty
Jiri Slaby12c80352012-03-05 14:52:26 +0100469 * structure will be freed. info->tport.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * be one in these conditions. If it's greater than
471 * one, we've got real problems, since it means the
472 * serial port won't be shutdown.
473 */
474 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby12c80352012-03-05 14:52:26 +0100475 "info->tport.count is %d\n", info->tport.count);
476 info->tport.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100478 if (--info->tport.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100480 tty->index, info->tport.count);
Jiri Slaby12c80352012-03-05 14:52:26 +0100481 info->tport.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100483 if (info->tport.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 local_irq_restore(flags);
485 return;
486 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100487 info->tport.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 local_irq_restore(flags);
489
490 /*
491 * Now we wait for the transmit buffer to clear; and we notify
492 * the line discipline to only process XON/XOFF characters.
493 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100494 shutdown(tty, info);
Alan Cox15648f12008-07-16 21:52:25 +0100495 rs_flush_buffer(tty);
496 tty_ldisc_flush(tty);
Jiri Slaby87758792012-03-05 14:52:24 +0100497 info->tport.tty = NULL;
498 if (info->tport.blocked_open) {
Jiri Slaby799be6f2012-03-05 14:52:25 +0100499 if (info->tport.close_delay)
500 schedule_timeout_interruptible(info->tport.close_delay);
Jiri Slaby87758792012-03-05 14:52:24 +0100501 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100503 info->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby87758792012-03-05 14:52:24 +0100504 wake_up_interruptible(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
508 * rs_wait_until_sent() --- wait until the transmitter is empty
509 */
510static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
511{
512}
513
514
515/*
516 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
517 */
518static void rs_hangup(struct tty_struct *tty)
519{
Jiri Slaby916b7652012-03-05 14:52:20 +0100520 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522#ifdef SIMSERIAL_DEBUG
523 printk("rs_hangup: called\n");
524#endif
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rs_flush_buffer(tty);
Jiri Slaby01bd7302012-03-05 14:52:27 +0100527 if (info->tport.flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return;
Jiri Slaby5e99d542012-03-05 14:52:23 +0100529 shutdown(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jiri Slaby12c80352012-03-05 14:52:26 +0100531 info->tport.count = 0;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100532 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby87758792012-03-05 14:52:24 +0100533 info->tport.tty = NULL;
534 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537
Jiri Slaby5e99d542012-03-05 14:52:23 +0100538static int startup(struct tty_struct *tty, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100540 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned long flags;
542 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long page;
544
545 page = get_zeroed_page(GFP_KERNEL);
546 if (!page)
547 return -ENOMEM;
548
549 local_irq_save(flags);
550
Jiri Slaby01bd7302012-03-05 14:52:27 +0100551 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 free_page(page);
553 goto errout;
554 }
555
Jiri Slaby916b7652012-03-05 14:52:20 +0100556 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 free_page(page);
558 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100559 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100562 printk("startup: ttys%d (irq %d)...", state->line, state->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#endif
564
565 /*
566 * Allocate the IRQ if necessary
567 */
Jiri Slaby964105b2012-03-05 14:52:17 +0100568 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100569 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100570 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100571 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Jiri Slaby5e99d542012-03-05 14:52:23 +0100575 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jiri Slaby916b7652012-03-05 14:52:20 +0100577 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579#if 0
580 /*
581 * Set up serial timers...
582 */
583 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
584 timer_active |= 1 << RS_TIMER;
585#endif
586
587 /*
588 * Set up the tty->alt_speed kludge
589 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100590 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100591 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100592 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100593 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100594 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100595 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100596 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100597 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Jiri Slaby01bd7302012-03-05 14:52:27 +0100599 port->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 local_irq_restore(flags);
601 return 0;
602
603errout:
604 local_irq_restore(flags);
605 return retval;
606}
607
608
609/*
610 * This routine is called whenever a serial port is opened. It
611 * enables interrupts for a serial port, linking in its async structure into
612 * the IRQ chain. It also performs the serial-specific
613 * initialization for the tty structure.
614 */
615static int rs_open(struct tty_struct *tty, struct file * filp)
616{
Jiri Slaby916b7652012-03-05 14:52:20 +0100617 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +0100618 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Jiri Slaby12c80352012-03-05 14:52:26 +0100620 info->tport.count++;
Jiri Slaby87758792012-03-05 14:52:24 +0100621 info->tport.tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +0100622 tty->driver_data = info;
Jiri Slaby87758792012-03-05 14:52:24 +0100623 tty->port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100626 printk("rs_open %s, count = %d\n", tty->name, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#endif
Jiri Slaby01bd7302012-03-05 14:52:27 +0100628 tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * If the port is the middle of closing, bail out now
632 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100633 if (tty_hung_up_p(filp) || (info->tport.flags & ASYNC_CLOSING)) {
634 if (info->tport.flags & ASYNC_CLOSING)
Jiri Slaby87758792012-03-05 14:52:24 +0100635 interruptible_sleep_on(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#ifdef SERIAL_DO_RESTART
Jiri Slaby01bd7302012-03-05 14:52:27 +0100637 return ((info->tport.flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 -EAGAIN : -ERESTARTSYS);
639#else
640 return -EAGAIN;
641#endif
642 }
643
644 /*
645 * Start up serial port
646 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100647 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (retval) {
649 return retval;
650 }
651
652 /*
653 * figure out which console to use (should be one already)
654 */
655 console = console_drivers;
656 while (console) {
657 if ((console->flags & CON_ENABLED) && console->write) break;
658 console = console->next;
659 }
660
661#ifdef SIMSERIAL_DEBUG
662 printk("rs_open ttys%d successful\n", info->line);
663#endif
664 return 0;
665}
666
667/*
668 * /proc fs routines....
669 */
670
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700671static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700673 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700675 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
676 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100677 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
678 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700682static int rs_proc_open(struct inode *inode, struct file *file)
683{
684 return single_open(file, rs_proc_show, NULL);
685}
686
687static const struct file_operations rs_proc_fops = {
688 .owner = THIS_MODULE,
689 .open = rs_proc_open,
690 .read = seq_read,
691 .llseek = seq_lseek,
692 .release = single_release,
693};
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * ---------------------------------------------------------------------
697 * rs_init() and friends
698 *
699 * rs_init() is called at boot-time to initialize the serial driver.
700 * ---------------------------------------------------------------------
701 */
702
703/*
704 * This routine prints out the appropriate serial driver version
705 * number, and identifies which options were configured into this
706 * driver.
707 */
708static inline void show_serial_version(void)
709{
710 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
711 printk(KERN_INFO " no serial options enabled\n");
712}
713
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700714static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .open = rs_open,
716 .close = rs_close,
717 .write = rs_write,
718 .put_char = rs_put_char,
719 .flush_chars = rs_flush_chars,
720 .write_room = rs_write_room,
721 .chars_in_buffer = rs_chars_in_buffer,
722 .flush_buffer = rs_flush_buffer,
723 .ioctl = rs_ioctl,
724 .throttle = rs_throttle,
725 .unthrottle = rs_unthrottle,
726 .send_xchar = rs_send_xchar,
727 .set_termios = rs_set_termios,
728 .stop = rs_stop,
729 .start = rs_start,
730 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700732 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
735/*
736 * The serial driver boot-time initialization code!
737 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100738static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100740 struct serial_state *state;
741 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (!ia64_platform_is("hpsim"))
744 return -ENODEV;
745
Jiri Slaby410235f2012-03-05 14:52:01 +0100746 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!hp_simserial_driver)
748 return -ENOMEM;
749
750 show_serial_version();
751
752 /* Initialize the tty_driver structure */
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 hp_simserial_driver->driver_name = "simserial";
755 hp_simserial_driver->name = "ttyS";
756 hp_simserial_driver->major = TTY_MAJOR;
757 hp_simserial_driver->minor_start = 64;
758 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
759 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
760 hp_simserial_driver->init_termios = tty_std_termios;
761 hp_simserial_driver->init_termios.c_cflag =
762 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
763 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
764 tty_set_operations(hp_simserial_driver, &hp_ops);
765
766 /*
767 * Let's have a little bit of fun !
768 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100769 state = rs_table;
770 tty_port_init(&state->tport);
771 state->tport.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100773 retval = hpsim_get_irq(KEYBOARD_INTR);
774 if (retval < 0) {
775 printk(KERN_ERR "%s: out of interrupt vectors!\n",
776 __func__);
777 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100780 state->irq = retval;
781
782 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100783 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100784
785 retval = tty_register_driver(hp_simserial_driver);
786 if (retval) {
787 printk(KERN_ERR "Couldn't register simserial driver\n");
788 goto err_free_tty;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100792err_free_tty:
793 put_tty_driver(hp_simserial_driver);
794 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797#ifndef MODULE
798__initcall(simrs_init);
799#endif