blob: 45df0f427864ca9998f347a6c3f6eac2e0953acf [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
Jiri Slaby916b7652012-03-05 14:52:20 +0100179 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 info->x_char = 0;
181
182 goto out;
183 }
184
Jiri Slaby5e99d542012-03-05 14:52:23 +0100185 if (info->xmit.head == info->xmit.tail || tty->stopped ||
186 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#ifdef SIMSERIAL_DEBUG
188 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100189 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191 goto out;
192 }
193 /*
194 * We removed the loop and try to do it in to chunks. We need
195 * 2 operations maximum because it's a ring buffer.
196 *
197 * First from current to tail if possible.
198 * Then from the beginning of the buffer until necessary
199 */
200
201 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
202 SERIAL_XMIT_SIZE - info->xmit.tail);
203 console->write(console, info->xmit.buf+info->xmit.tail, count);
204
205 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
206
207 /*
208 * We have more at the beginning of the buffer
209 */
210 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
211 if (count) {
212 console->write(console, info->xmit.buf, count);
213 info->xmit.tail += count;
214 }
215out:
216 local_irq_restore(flags);
217}
218
219static void rs_flush_chars(struct tty_struct *tty)
220{
Jiri Slaby916b7652012-03-05 14:52:20 +0100221 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
224 !info->xmit.buf)
225 return;
226
Jiri Slaby5e99d542012-03-05 14:52:23 +0100227 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230
231static int rs_write(struct tty_struct * tty,
232 const unsigned char *buf, int count)
233{
Jiri Slaby916b7652012-03-05 14:52:20 +0100234 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 unsigned long flags;
237
Jiri Slabyd88405d2012-03-05 14:52:29 +0100238 if (!tty || !info->xmit.buf)
239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 local_irq_save(flags);
242 while (1) {
243 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
244 if (count < c)
245 c = count;
246 if (c <= 0) {
247 break;
248 }
249 memcpy(info->xmit.buf + info->xmit.head, buf, c);
250 info->xmit.head = ((info->xmit.head + c) &
251 (SERIAL_XMIT_SIZE-1));
252 buf += c;
253 count -= c;
254 ret += c;
255 }
256 local_irq_restore(flags);
257 /*
258 * Hey, we transmit directly from here in our case
259 */
260 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
261 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100262 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 return ret;
265}
266
267static int rs_write_room(struct tty_struct *tty)
268{
Jiri Slaby916b7652012-03-05 14:52:20 +0100269 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
272}
273
274static int rs_chars_in_buffer(struct tty_struct *tty)
275{
Jiri Slaby916b7652012-03-05 14:52:20 +0100276 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
279}
280
281static void rs_flush_buffer(struct tty_struct *tty)
282{
Jiri Slaby916b7652012-03-05 14:52:20 +0100283 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned long flags;
285
286 local_irq_save(flags);
287 info->xmit.head = info->xmit.tail = 0;
288 local_irq_restore(flags);
289
Alan Cox15648f12008-07-16 21:52:25 +0100290 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * This function is used to send a high-priority XON/XOFF character to
295 * the device
296 */
297static void rs_send_xchar(struct tty_struct *tty, char ch)
298{
Jiri Slaby916b7652012-03-05 14:52:20 +0100299 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 info->x_char = ch;
302 if (ch) {
303 /*
304 * I guess we could call console->write() directly but
305 * let's do that for now.
306 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100307 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309}
310
311/*
312 * ------------------------------------------------------------
313 * rs_throttle()
314 *
315 * This routine is called by the upper-layer tty layer to signal that
316 * incoming characters should be throttled.
317 * ------------------------------------------------------------
318 */
319static void rs_throttle(struct tty_struct * tty)
320{
321 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
322
323 printk(KERN_INFO "simrs_throttle called\n");
324}
325
326static void rs_unthrottle(struct tty_struct * tty)
327{
Jiri Slaby916b7652012-03-05 14:52:20 +0100328 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (I_IXOFF(tty)) {
331 if (info->x_char)
332 info->x_char = 0;
333 else
334 rs_send_xchar(tty, START_CHAR(tty));
335 }
336 printk(KERN_INFO "simrs_unthrottle called\n");
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Luck, Tony10e82f62011-02-22 11:47:04 -0800340static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
343 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100344 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (tty->flags & (1 << TTY_IO_ERROR))
346 return -EIO;
347 }
348
349 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 case TIOCGSERIAL:
351 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
352 return 0;
353 case TIOCSSERIAL:
354 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
355 return 0;
356 case TIOCSERCONFIG:
357 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
358 return -EINVAL;
359
360 case TIOCSERGETLSR: /* Get line status register */
361 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
362 return -EINVAL;
363
364 case TIOCSERGSTRUCT:
365 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
366#if 0
367 if (copy_to_user((struct async_struct *) arg,
368 info, sizeof(struct async_struct)))
369 return -EFAULT;
370#endif
371 return 0;
372
373 /*
374 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
375 * - mask passed in arg for lines of interest
376 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
377 * Caller should use TIOCGICOUNT to see which one it was
378 */
379 case TIOCMIWAIT:
380 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 case TIOCSERGWILD:
383 case TIOCSERSWILD:
384 /* "setserial -W" is called in Debian boot */
385 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
386 return 0;
387
388 default:
389 return -ENOIOCTLCMD;
390 }
391 return 0;
392}
393
394#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
395
Tony Luckf889a262006-12-12 10:47:36 -0800396static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Handle turning off CRTSCTS */
399 if ((old_termios->c_cflag & CRTSCTS) &&
400 !(tty->termios->c_cflag & CRTSCTS)) {
401 tty->hw_stopped = 0;
402 rs_start(tty);
403 }
404}
405/*
406 * This routine will shutdown a serial port; interrupts are disabled, and
407 * DTR is dropped if the hangup on close termio flag is on.
408 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100409static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jiri Slaby01bd7302012-03-05 14:52:27 +0100413 if (!(info->tport.flags & ASYNC_INITIALIZED))
Jiri Slaby979b6d82012-03-05 14:52:15 +0100414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100417 printk("Shutting down serial port %d (irq %d)...\n", info->line,
418 info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#endif
420
421 local_irq_save(flags);
422 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100423 if (info->irq)
424 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (info->xmit.buf) {
427 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100428 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Jiri Slaby5e99d542012-03-05 14:52:23 +0100431 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jiri Slaby01bd7302012-03-05 14:52:27 +0100433 info->tport.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 local_irq_restore(flags);
436}
437
438/*
439 * ------------------------------------------------------------
440 * rs_close()
441 *
442 * This routine is called when the serial port gets closed. First, we
443 * wait for the last remaining data to be sent. Then, we unlink its
444 * async structure from the interrupt chain if necessary, and we free
445 * that IRQ if nothing is left in the chain.
446 * ------------------------------------------------------------
447 */
448static void rs_close(struct tty_struct *tty, struct file * filp)
449{
Jiri Slaby916b7652012-03-05 14:52:20 +0100450 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 unsigned long flags;
452
Jiri Slaby916b7652012-03-05 14:52:20 +0100453 if (!info)
454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 local_irq_save(flags);
457 if (tty_hung_up_p(filp)) {
458#ifdef SIMSERIAL_DEBUG
459 printk("rs_close: hung_up\n");
460#endif
461 local_irq_restore(flags);
462 return;
463 }
464#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100465 printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#endif
Jiri Slaby12c80352012-03-05 14:52:26 +0100467 if ((tty->count == 1) && (info->tport.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Uh, oh. tty->count is 1, which means that the tty
Jiri Slaby12c80352012-03-05 14:52:26 +0100470 * structure will be freed. info->tport.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * be one in these conditions. If it's greater than
472 * one, we've got real problems, since it means the
473 * serial port won't be shutdown.
474 */
475 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby12c80352012-03-05 14:52:26 +0100476 "info->tport.count is %d\n", info->tport.count);
477 info->tport.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100479 if (--info->tport.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +0100481 info->line, info->tport.count);
482 info->tport.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100484 if (info->tport.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 local_irq_restore(flags);
486 return;
487 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100488 info->tport.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 local_irq_restore(flags);
490
491 /*
492 * Now we wait for the transmit buffer to clear; and we notify
493 * the line discipline to only process XON/XOFF characters.
494 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100495 shutdown(tty, info);
Alan Cox15648f12008-07-16 21:52:25 +0100496 rs_flush_buffer(tty);
497 tty_ldisc_flush(tty);
Jiri Slaby87758792012-03-05 14:52:24 +0100498 info->tport.tty = NULL;
499 if (info->tport.blocked_open) {
Jiri Slaby799be6f2012-03-05 14:52:25 +0100500 if (info->tport.close_delay)
501 schedule_timeout_interruptible(info->tport.close_delay);
Jiri Slaby87758792012-03-05 14:52:24 +0100502 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100504 info->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby87758792012-03-05 14:52:24 +0100505 wake_up_interruptible(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/*
509 * rs_wait_until_sent() --- wait until the transmitter is empty
510 */
511static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
512{
513}
514
515
516/*
517 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
518 */
519static void rs_hangup(struct tty_struct *tty)
520{
Jiri Slaby916b7652012-03-05 14:52:20 +0100521 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523#ifdef SIMSERIAL_DEBUG
524 printk("rs_hangup: called\n");
525#endif
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rs_flush_buffer(tty);
Jiri Slaby01bd7302012-03-05 14:52:27 +0100528 if (info->tport.flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return;
Jiri Slaby5e99d542012-03-05 14:52:23 +0100530 shutdown(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jiri Slaby12c80352012-03-05 14:52:26 +0100532 info->tport.count = 0;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100533 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby87758792012-03-05 14:52:24 +0100534 info->tport.tty = NULL;
535 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538
Jiri Slaby5e99d542012-03-05 14:52:23 +0100539static int startup(struct tty_struct *tty, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100541 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long flags;
543 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unsigned long page;
545
546 page = get_zeroed_page(GFP_KERNEL);
547 if (!page)
548 return -ENOMEM;
549
550 local_irq_save(flags);
551
Jiri Slaby01bd7302012-03-05 14:52:27 +0100552 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 free_page(page);
554 goto errout;
555 }
556
Jiri Slaby916b7652012-03-05 14:52:20 +0100557 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 free_page(page);
559 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100560 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100563 printk("startup: ttys%d (irq %d)...", state->line, state->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#endif
565
566 /*
567 * Allocate the IRQ if necessary
568 */
Jiri Slaby964105b2012-03-05 14:52:17 +0100569 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100570 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100571 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100572 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Jiri Slaby5e99d542012-03-05 14:52:23 +0100576 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jiri Slaby916b7652012-03-05 14:52:20 +0100578 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580#if 0
581 /*
582 * Set up serial timers...
583 */
584 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
585 timer_active |= 1 << RS_TIMER;
586#endif
587
588 /*
589 * Set up the tty->alt_speed kludge
590 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100591 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100592 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100593 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100594 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100595 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100596 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100597 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100598 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Jiri Slaby01bd7302012-03-05 14:52:27 +0100600 port->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 local_irq_restore(flags);
602 return 0;
603
604errout:
605 local_irq_restore(flags);
606 return retval;
607}
608
609
610/*
611 * This routine is called whenever a serial port is opened. It
612 * enables interrupts for a serial port, linking in its async structure into
613 * the IRQ chain. It also performs the serial-specific
614 * initialization for the tty structure.
615 */
616static int rs_open(struct tty_struct *tty, struct file * filp)
617{
Jiri Slaby916b7652012-03-05 14:52:20 +0100618 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +0100619 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jiri Slaby12c80352012-03-05 14:52:26 +0100621 info->tport.count++;
Jiri Slaby87758792012-03-05 14:52:24 +0100622 info->tport.tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +0100623 tty->driver_data = info;
Jiri Slaby87758792012-03-05 14:52:24 +0100624 tty->port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100627 printk("rs_open %s, count = %d\n", tty->name, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#endif
Jiri Slaby01bd7302012-03-05 14:52:27 +0100629 tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * If the port is the middle of closing, bail out now
633 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100634 if (tty_hung_up_p(filp) || (info->tport.flags & ASYNC_CLOSING)) {
635 if (info->tport.flags & ASYNC_CLOSING)
Jiri Slaby87758792012-03-05 14:52:24 +0100636 interruptible_sleep_on(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637#ifdef SERIAL_DO_RESTART
Jiri Slaby01bd7302012-03-05 14:52:27 +0100638 return ((info->tport.flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 -EAGAIN : -ERESTARTSYS);
640#else
641 return -EAGAIN;
642#endif
643 }
644
645 /*
646 * Start up serial port
647 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100648 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (retval) {
650 return retval;
651 }
652
653 /*
654 * figure out which console to use (should be one already)
655 */
656 console = console_drivers;
657 while (console) {
658 if ((console->flags & CON_ENABLED) && console->write) break;
659 console = console->next;
660 }
661
662#ifdef SIMSERIAL_DEBUG
663 printk("rs_open ttys%d successful\n", info->line);
664#endif
665 return 0;
666}
667
668/*
669 * /proc fs routines....
670 */
671
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700672static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100674 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
675 state->line, state->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700678static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700680 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700682 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
683 for (i = 0; i < NR_PORTS; i++)
684 line_info(m, &rs_table[i]);
685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700688static int rs_proc_open(struct inode *inode, struct file *file)
689{
690 return single_open(file, rs_proc_show, NULL);
691}
692
693static const struct file_operations rs_proc_fops = {
694 .owner = THIS_MODULE,
695 .open = rs_proc_open,
696 .read = seq_read,
697 .llseek = seq_lseek,
698 .release = single_release,
699};
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701/*
702 * ---------------------------------------------------------------------
703 * rs_init() and friends
704 *
705 * rs_init() is called at boot-time to initialize the serial driver.
706 * ---------------------------------------------------------------------
707 */
708
709/*
710 * This routine prints out the appropriate serial driver version
711 * number, and identifies which options were configured into this
712 * driver.
713 */
714static inline void show_serial_version(void)
715{
716 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
717 printk(KERN_INFO " no serial options enabled\n");
718}
719
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700720static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 .open = rs_open,
722 .close = rs_close,
723 .write = rs_write,
724 .put_char = rs_put_char,
725 .flush_chars = rs_flush_chars,
726 .write_room = rs_write_room,
727 .chars_in_buffer = rs_chars_in_buffer,
728 .flush_buffer = rs_flush_buffer,
729 .ioctl = rs_ioctl,
730 .throttle = rs_throttle,
731 .unthrottle = rs_unthrottle,
732 .send_xchar = rs_send_xchar,
733 .set_termios = rs_set_termios,
734 .stop = rs_stop,
735 .start = rs_start,
736 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700738 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739};
740
741/*
742 * The serial driver boot-time initialization code!
743 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100744static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100746 struct serial_state *state;
747 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (!ia64_platform_is("hpsim"))
750 return -ENODEV;
751
Jiri Slaby410235f2012-03-05 14:52:01 +0100752 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!hp_simserial_driver)
754 return -ENOMEM;
755
756 show_serial_version();
757
758 /* Initialize the tty_driver structure */
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 hp_simserial_driver->driver_name = "simserial";
761 hp_simserial_driver->name = "ttyS";
762 hp_simserial_driver->major = TTY_MAJOR;
763 hp_simserial_driver->minor_start = 64;
764 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
765 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
766 hp_simserial_driver->init_termios = tty_std_termios;
767 hp_simserial_driver->init_termios.c_cflag =
768 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
769 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
770 tty_set_operations(hp_simserial_driver, &hp_ops);
771
772 /*
773 * Let's have a little bit of fun !
774 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100775 state = rs_table;
776 tty_port_init(&state->tport);
777 state->tport.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100779 retval = hpsim_get_irq(KEYBOARD_INTR);
780 if (retval < 0) {
781 printk(KERN_ERR "%s: out of interrupt vectors!\n",
782 __func__);
783 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100786 state->irq = retval;
787
788 /* the port is imaginary */
789 printk(KERN_INFO "ttyS%d at 0x03f8 (irq = %d) is a 16550\n",
790 state->line, state->irq);
791
792 retval = tty_register_driver(hp_simserial_driver);
793 if (retval) {
794 printk(KERN_ERR "Couldn't register simserial driver\n");
795 goto err_free_tty;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100799err_free_tty:
800 put_tty_driver(hp_simserial_driver);
801 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804#ifndef MODULE
805__initcall(simrs_init);
806#endif