blob: c65c49d31e7f065a6eac2dfad62919839cb70c80 [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
52/*
53 * This has been extracted from asm/serial.h. We need one eventually but
54 * I don't know exactly what we're going to put in it so just fake one
55 * for now.
56 */
57#define BASE_BAUD ( 1843200 / 16 )
58
59#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
60
61/*
62 * Most of the values here are meaningless to this particular driver.
63 * However some values must be preserved for the code (leveraged from serial.c
64 * to work correctly).
65 * port must not be 0
66 * type must not be UNKNOWN
67 * So I picked arbitrary (guess from where?) values instead
68 */
69static struct serial_state rs_table[NR_PORTS]={
70 /* UART CLK PORT IRQ FLAGS */
Jiri Slaby9c8efec2012-03-05 14:52:12 +010071 { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 } /* ttyS0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74/*
75 * Just for the fun of it !
76 */
77static struct serial_uart_config uart_config[] = {
78 { "unknown", 1, 0 },
79 { "8250", 1, 0 },
80 { "16450", 1, 0 },
81 { "16550", 1, 0 },
82 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
83 { "cirrus", 1, 0 },
84 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
85 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
86 UART_STARTECH },
87 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
Al Virocfa7fd72006-10-10 22:46:17 +010088 { NULL, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
91struct tty_driver *hp_simserial_driver;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static struct console *console;
94
95static unsigned char *tmp_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97extern struct console *console_drivers; /* from kernel/printk.c */
98
99/*
100 * ------------------------------------------------------------
101 * rs_stop() and rs_start()
102 *
103 * This routines are called before setting or resetting tty->stopped.
104 * They enable or disable transmitter interrupts, as necessary.
105 * ------------------------------------------------------------
106 */
107static void rs_stop(struct tty_struct *tty)
108{
109#ifdef SIMSERIAL_DEBUG
110 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
111 tty->stopped, tty->hw_stopped, tty->flow_stopped);
112#endif
113
114}
115
116static void rs_start(struct tty_struct *tty)
117{
viro@ZenIV.linux.org.uke72225d2005-09-07 23:23:50 +0100118#ifdef SIMSERIAL_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
120 tty->stopped, tty->hw_stopped, tty->flow_stopped);
121#endif
122}
123
Al Viro5dcded12006-10-08 14:59:19 +0100124static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 unsigned char ch;
127 static unsigned char seen_esc = 0;
128
129 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
130 if ( ch == 27 && seen_esc == 0 ) {
131 seen_esc = 1;
132 continue;
133 } else {
134 if ( seen_esc==1 && ch == 'O' ) {
135 seen_esc = 2;
136 continue;
137 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700138 if ( ch == 'P' ) /* F1 */
139 show_state();
140#ifdef CONFIG_MAGIC_SYSRQ
141 if ( ch == 'S' ) { /* F4 */
142 do
143 ch = ia64_ssc(0, 0, 0, 0,
144 SSC_GETCHAR);
145 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700146 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 seen_esc = 0;
150 continue;
151 }
152 }
153 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Andreas Schwabd50f5c52006-01-13 23:46:38 +0100155 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 tty_flip_buffer_push(tty);
159}
160
161/*
162 * This is the serial driver's interrupt routine for a single port
163 */
Al Viro5dcded12006-10-08 14:59:19 +0100164static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Jiri Slaby916b7652012-03-05 14:52:20 +0100166 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jiri Slaby87758792012-03-05 14:52:24 +0100168 if (!info->tport.tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
170 return IRQ_NONE;
171 }
172 /*
173 * pretty simple in our case, because we only get interrupts
174 * on inbound traffic
175 */
Jiri Slaby87758792012-03-05 14:52:24 +0100176 receive_chars(info->tport.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return IRQ_HANDLED;
178}
179
180/*
181 * -------------------------------------------------------------------
182 * Here ends the serial interrupt routines.
183 * -------------------------------------------------------------------
184 */
185
Alan Coxf34d7a52008-04-30 00:54:13 -0700186static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Jiri Slaby916b7652012-03-05 14:52:20 +0100188 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long flags;
190
Alan Coxf34d7a52008-04-30 00:54:13 -0700191 if (!tty || !info->xmit.buf)
192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 local_irq_save(flags);
195 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
196 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 info->xmit.buf[info->xmit.head] = ch;
200 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
201 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700202 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Jiri Slaby5e99d542012-03-05 14:52:23 +0100205static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
206 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 int count;
209 unsigned long flags;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 local_irq_save(flags);
212
213 if (info->x_char) {
214 char c = info->x_char;
215
216 console->write(console, &c, 1);
217
Jiri Slaby916b7652012-03-05 14:52:20 +0100218 info->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 info->x_char = 0;
220
221 goto out;
222 }
223
Jiri Slaby5e99d542012-03-05 14:52:23 +0100224 if (info->xmit.head == info->xmit.tail || tty->stopped ||
225 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#ifdef SIMSERIAL_DEBUG
227 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100228 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#endif
230 goto out;
231 }
232 /*
233 * We removed the loop and try to do it in to chunks. We need
234 * 2 operations maximum because it's a ring buffer.
235 *
236 * First from current to tail if possible.
237 * Then from the beginning of the buffer until necessary
238 */
239
240 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
241 SERIAL_XMIT_SIZE - info->xmit.tail);
242 console->write(console, info->xmit.buf+info->xmit.tail, count);
243
244 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
245
246 /*
247 * We have more at the beginning of the buffer
248 */
249 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
250 if (count) {
251 console->write(console, info->xmit.buf, count);
252 info->xmit.tail += count;
253 }
254out:
255 local_irq_restore(flags);
256}
257
258static void rs_flush_chars(struct tty_struct *tty)
259{
Jiri Slaby916b7652012-03-05 14:52:20 +0100260 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
263 !info->xmit.buf)
264 return;
265
Jiri Slaby5e99d542012-03-05 14:52:23 +0100266 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270static int rs_write(struct tty_struct * tty,
271 const unsigned char *buf, int count)
272{
Jiri Slaby916b7652012-03-05 14:52:20 +0100273 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned long flags;
276
277 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
278
279 local_irq_save(flags);
280 while (1) {
281 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
282 if (count < c)
283 c = count;
284 if (c <= 0) {
285 break;
286 }
287 memcpy(info->xmit.buf + info->xmit.head, buf, c);
288 info->xmit.head = ((info->xmit.head + c) &
289 (SERIAL_XMIT_SIZE-1));
290 buf += c;
291 count -= c;
292 ret += c;
293 }
294 local_irq_restore(flags);
295 /*
296 * Hey, we transmit directly from here in our case
297 */
298 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
299 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100300 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 return ret;
303}
304
305static int rs_write_room(struct tty_struct *tty)
306{
Jiri Slaby916b7652012-03-05 14:52:20 +0100307 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
310}
311
312static int rs_chars_in_buffer(struct tty_struct *tty)
313{
Jiri Slaby916b7652012-03-05 14:52:20 +0100314 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
317}
318
319static void rs_flush_buffer(struct tty_struct *tty)
320{
Jiri Slaby916b7652012-03-05 14:52:20 +0100321 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned long flags;
323
324 local_irq_save(flags);
325 info->xmit.head = info->xmit.tail = 0;
326 local_irq_restore(flags);
327
Alan Cox15648f12008-07-16 21:52:25 +0100328 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/*
332 * This function is used to send a high-priority XON/XOFF character to
333 * the device
334 */
335static void rs_send_xchar(struct tty_struct *tty, char ch)
336{
Jiri Slaby916b7652012-03-05 14:52:20 +0100337 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 info->x_char = ch;
340 if (ch) {
341 /*
342 * I guess we could call console->write() directly but
343 * let's do that for now.
344 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100345 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347}
348
349/*
350 * ------------------------------------------------------------
351 * rs_throttle()
352 *
353 * This routine is called by the upper-layer tty layer to signal that
354 * incoming characters should be throttled.
355 * ------------------------------------------------------------
356 */
357static void rs_throttle(struct tty_struct * tty)
358{
359 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
360
361 printk(KERN_INFO "simrs_throttle called\n");
362}
363
364static void rs_unthrottle(struct tty_struct * tty)
365{
Jiri Slaby916b7652012-03-05 14:52:20 +0100366 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (I_IXOFF(tty)) {
369 if (info->x_char)
370 info->x_char = 0;
371 else
372 rs_send_xchar(tty, START_CHAR(tty));
373 }
374 printk(KERN_INFO "simrs_unthrottle called\n");
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Luck, Tony10e82f62011-02-22 11:47:04 -0800378static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
381 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100382 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (tty->flags & (1 << TTY_IO_ERROR))
384 return -EIO;
385 }
386
387 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 case TIOCGSERIAL:
389 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
390 return 0;
391 case TIOCSSERIAL:
392 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
393 return 0;
394 case TIOCSERCONFIG:
395 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
396 return -EINVAL;
397
398 case TIOCSERGETLSR: /* Get line status register */
399 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
400 return -EINVAL;
401
402 case TIOCSERGSTRUCT:
403 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
404#if 0
405 if (copy_to_user((struct async_struct *) arg,
406 info, sizeof(struct async_struct)))
407 return -EFAULT;
408#endif
409 return 0;
410
411 /*
412 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
413 * - mask passed in arg for lines of interest
414 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
415 * Caller should use TIOCGICOUNT to see which one it was
416 */
417 case TIOCMIWAIT:
418 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 case TIOCSERGWILD:
421 case TIOCSERSWILD:
422 /* "setserial -W" is called in Debian boot */
423 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
424 return 0;
425
426 default:
427 return -ENOIOCTLCMD;
428 }
429 return 0;
430}
431
432#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
433
Tony Luckf889a262006-12-12 10:47:36 -0800434static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Handle turning off CRTSCTS */
437 if ((old_termios->c_cflag & CRTSCTS) &&
438 !(tty->termios->c_cflag & CRTSCTS)) {
439 tty->hw_stopped = 0;
440 rs_start(tty);
441 }
442}
443/*
444 * This routine will shutdown a serial port; interrupts are disabled, and
445 * DTR is dropped if the hangup on close termio flag is on.
446 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100447static void shutdown(struct tty_struct *tty, struct serial_state *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jiri Slaby01bd7302012-03-05 14:52:27 +0100451 if (!(info->tport.flags & ASYNC_INITIALIZED))
Jiri Slaby979b6d82012-03-05 14:52:15 +0100452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100455 printk("Shutting down serial port %d (irq %d)...\n", info->line,
456 info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
458
459 local_irq_save(flags);
460 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100461 if (info->irq)
462 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (info->xmit.buf) {
465 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100466 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
Jiri Slaby5e99d542012-03-05 14:52:23 +0100469 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jiri Slaby01bd7302012-03-05 14:52:27 +0100471 info->tport.flags &= ~ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 local_irq_restore(flags);
474}
475
476/*
477 * ------------------------------------------------------------
478 * rs_close()
479 *
480 * This routine is called when the serial port gets closed. First, we
481 * wait for the last remaining data to be sent. Then, we unlink its
482 * async structure from the interrupt chain if necessary, and we free
483 * that IRQ if nothing is left in the chain.
484 * ------------------------------------------------------------
485 */
486static void rs_close(struct tty_struct *tty, struct file * filp)
487{
Jiri Slaby916b7652012-03-05 14:52:20 +0100488 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unsigned long flags;
490
Jiri Slaby916b7652012-03-05 14:52:20 +0100491 if (!info)
492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 local_irq_save(flags);
495 if (tty_hung_up_p(filp)) {
496#ifdef SIMSERIAL_DEBUG
497 printk("rs_close: hung_up\n");
498#endif
499 local_irq_restore(flags);
500 return;
501 }
502#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100503 printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#endif
Jiri Slaby12c80352012-03-05 14:52:26 +0100505 if ((tty->count == 1) && (info->tport.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Uh, oh. tty->count is 1, which means that the tty
Jiri Slaby12c80352012-03-05 14:52:26 +0100508 * structure will be freed. info->tport.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * be one in these conditions. If it's greater than
510 * one, we've got real problems, since it means the
511 * serial port won't be shutdown.
512 */
513 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby12c80352012-03-05 14:52:26 +0100514 "info->tport.count is %d\n", info->tport.count);
515 info->tport.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100517 if (--info->tport.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
Jiri Slaby12c80352012-03-05 14:52:26 +0100519 info->line, info->tport.count);
520 info->tport.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Jiri Slaby12c80352012-03-05 14:52:26 +0100522 if (info->tport.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 local_irq_restore(flags);
524 return;
525 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100526 info->tport.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 local_irq_restore(flags);
528
529 /*
530 * Now we wait for the transmit buffer to clear; and we notify
531 * the line discipline to only process XON/XOFF characters.
532 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100533 shutdown(tty, info);
Alan Cox15648f12008-07-16 21:52:25 +0100534 rs_flush_buffer(tty);
535 tty_ldisc_flush(tty);
Jiri Slaby87758792012-03-05 14:52:24 +0100536 info->tport.tty = NULL;
537 if (info->tport.blocked_open) {
Jiri Slaby799be6f2012-03-05 14:52:25 +0100538 if (info->tport.close_delay)
539 schedule_timeout_interruptible(info->tport.close_delay);
Jiri Slaby87758792012-03-05 14:52:24 +0100540 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Jiri Slaby01bd7302012-03-05 14:52:27 +0100542 info->tport.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slaby87758792012-03-05 14:52:24 +0100543 wake_up_interruptible(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
547 * rs_wait_until_sent() --- wait until the transmitter is empty
548 */
549static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
550{
551}
552
553
554/*
555 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
556 */
557static void rs_hangup(struct tty_struct *tty)
558{
Jiri Slaby916b7652012-03-05 14:52:20 +0100559 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561#ifdef SIMSERIAL_DEBUG
562 printk("rs_hangup: called\n");
563#endif
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rs_flush_buffer(tty);
Jiri Slaby01bd7302012-03-05 14:52:27 +0100566 if (info->tport.flags & ASYNC_CLOSING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return;
Jiri Slaby5e99d542012-03-05 14:52:23 +0100568 shutdown(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jiri Slaby12c80352012-03-05 14:52:26 +0100570 info->tport.count = 0;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100571 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby87758792012-03-05 14:52:24 +0100572 info->tport.tty = NULL;
573 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576
Jiri Slaby5e99d542012-03-05 14:52:23 +0100577static int startup(struct tty_struct *tty, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jiri Slaby01bd7302012-03-05 14:52:27 +0100579 struct tty_port *port = &state->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unsigned long flags;
581 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned long page;
583
584 page = get_zeroed_page(GFP_KERNEL);
585 if (!page)
586 return -ENOMEM;
587
588 local_irq_save(flags);
589
Jiri Slaby01bd7302012-03-05 14:52:27 +0100590 if (port->flags & ASYNC_INITIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 free_page(page);
592 goto errout;
593 }
594
595 if (!state->port || !state->type) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100596 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 free_page(page);
598 goto errout;
599 }
Jiri Slaby916b7652012-03-05 14:52:20 +0100600 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 free_page(page);
602 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100603 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605#ifdef SIMSERIAL_DEBUG
Jiri Slaby916b7652012-03-05 14:52:20 +0100606 printk("startup: ttys%d (irq %d)...", state->line, state->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
608
609 /*
610 * Allocate the IRQ if necessary
611 */
Jiri Slaby964105b2012-03-05 14:52:17 +0100612 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100613 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100614 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100615 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
Jiri Slaby5e99d542012-03-05 14:52:23 +0100619 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jiri Slaby916b7652012-03-05 14:52:20 +0100621 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623#if 0
624 /*
625 * Set up serial timers...
626 */
627 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
628 timer_active |= 1 << RS_TIMER;
629#endif
630
631 /*
632 * Set up the tty->alt_speed kludge
633 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100634 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100635 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100636 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100637 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100638 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100639 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100640 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100641 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Jiri Slaby01bd7302012-03-05 14:52:27 +0100643 port->flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 local_irq_restore(flags);
645 return 0;
646
647errout:
648 local_irq_restore(flags);
649 return retval;
650}
651
652
653/*
654 * This routine is called whenever a serial port is opened. It
655 * enables interrupts for a serial port, linking in its async structure into
656 * the IRQ chain. It also performs the serial-specific
657 * initialization for the tty structure.
658 */
659static int rs_open(struct tty_struct *tty, struct file * filp)
660{
Jiri Slaby916b7652012-03-05 14:52:20 +0100661 struct serial_state *info = rs_table + tty->index;
Jiri Slaby410235f2012-03-05 14:52:01 +0100662 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned long page;
664
Jiri Slaby12c80352012-03-05 14:52:26 +0100665 info->tport.count++;
Jiri Slaby87758792012-03-05 14:52:24 +0100666 info->tport.tty = tty;
Jiri Slaby916b7652012-03-05 14:52:20 +0100667 tty->driver_data = info;
Jiri Slaby87758792012-03-05 14:52:24 +0100668 tty->port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670#ifdef SIMSERIAL_DEBUG
Jiri Slaby12c80352012-03-05 14:52:26 +0100671 printk("rs_open %s, count = %d\n", tty->name, info->tport.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#endif
Jiri Slaby01bd7302012-03-05 14:52:27 +0100673 tty->low_latency = (info->tport.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (!tmp_buf) {
676 page = get_zeroed_page(GFP_KERNEL);
677 if (!page)
678 return -ENOMEM;
679 if (tmp_buf)
680 free_page(page);
681 else
682 tmp_buf = (unsigned char *) page;
683 }
684
685 /*
686 * If the port is the middle of closing, bail out now
687 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100688 if (tty_hung_up_p(filp) || (info->tport.flags & ASYNC_CLOSING)) {
689 if (info->tport.flags & ASYNC_CLOSING)
Jiri Slaby87758792012-03-05 14:52:24 +0100690 interruptible_sleep_on(&info->tport.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691#ifdef SERIAL_DO_RESTART
Jiri Slaby01bd7302012-03-05 14:52:27 +0100692 return ((info->tport.flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 -EAGAIN : -ERESTARTSYS);
694#else
695 return -EAGAIN;
696#endif
697 }
698
699 /*
700 * Start up serial port
701 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100702 retval = startup(tty, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (retval) {
704 return retval;
705 }
706
707 /*
708 * figure out which console to use (should be one already)
709 */
710 console = console_drivers;
711 while (console) {
712 if ((console->flags & CON_ENABLED) && console->write) break;
713 console = console->next;
714 }
715
716#ifdef SIMSERIAL_DEBUG
717 printk("rs_open ttys%d successful\n", info->line);
718#endif
719 return 0;
720}
721
722/*
723 * /proc fs routines....
724 */
725
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700726static inline void line_info(struct seq_file *m, struct serial_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700728 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 state->line, uart_config[state->type].name,
730 state->port, state->irq);
731}
732
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700733static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700735 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700737 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
738 for (i = 0; i < NR_PORTS; i++)
739 line_info(m, &rs_table[i]);
740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700743static int rs_proc_open(struct inode *inode, struct file *file)
744{
745 return single_open(file, rs_proc_show, NULL);
746}
747
748static const struct file_operations rs_proc_fops = {
749 .owner = THIS_MODULE,
750 .open = rs_proc_open,
751 .read = seq_read,
752 .llseek = seq_lseek,
753 .release = single_release,
754};
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * ---------------------------------------------------------------------
758 * rs_init() and friends
759 *
760 * rs_init() is called at boot-time to initialize the serial driver.
761 * ---------------------------------------------------------------------
762 */
763
764/*
765 * This routine prints out the appropriate serial driver version
766 * number, and identifies which options were configured into this
767 * driver.
768 */
769static inline void show_serial_version(void)
770{
771 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
772 printk(KERN_INFO " no serial options enabled\n");
773}
774
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700775static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .open = rs_open,
777 .close = rs_close,
778 .write = rs_write,
779 .put_char = rs_put_char,
780 .flush_chars = rs_flush_chars,
781 .write_room = rs_write_room,
782 .chars_in_buffer = rs_chars_in_buffer,
783 .flush_buffer = rs_flush_buffer,
784 .ioctl = rs_ioctl,
785 .throttle = rs_throttle,
786 .unthrottle = rs_unthrottle,
787 .send_xchar = rs_send_xchar,
788 .set_termios = rs_set_termios,
789 .stop = rs_stop,
790 .start = rs_start,
791 .hangup = rs_hangup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 .wait_until_sent = rs_wait_until_sent,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700793 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794};
795
796/*
797 * The serial driver boot-time initialization code!
798 */
799static int __init
800simrs_init (void)
801{
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700802 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct serial_state *state;
804
805 if (!ia64_platform_is("hpsim"))
806 return -ENODEV;
807
Jiri Slaby410235f2012-03-05 14:52:01 +0100808 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!hp_simserial_driver)
810 return -ENOMEM;
811
812 show_serial_version();
813
814 /* Initialize the tty_driver structure */
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 hp_simserial_driver->driver_name = "simserial";
817 hp_simserial_driver->name = "ttyS";
818 hp_simserial_driver->major = TTY_MAJOR;
819 hp_simserial_driver->minor_start = 64;
820 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
821 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
822 hp_simserial_driver->init_termios = tty_std_termios;
823 hp_simserial_driver->init_termios.c_cflag =
824 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
825 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
826 tty_set_operations(hp_simserial_driver, &hp_ops);
827
828 /*
829 * Let's have a little bit of fun !
830 */
831 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
Jiri Slaby87758792012-03-05 14:52:24 +0100832 tty_port_init(&state->tport);
Jiri Slaby799be6f2012-03-05 14:52:25 +0100833 state->tport.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (state->type == PORT_UNKNOWN) continue;
836
837 if (!state->irq) {
Jiri Slaby6efb6b72012-03-08 21:01:18 +0100838 if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700839 panic("%s: out of interrupt vectors!\n",
Harvey Harrisond4ed8082008-03-04 15:15:00 -0800840 __func__);
Kenji Kaneshige3b5cc092005-07-10 21:49:00 -0700841 state->irq = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843
844 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
845 state->line,
846 state->port, state->irq,
847 uart_config[state->type].name);
848 }
849
850 if (tty_register_driver(hp_simserial_driver))
851 panic("Couldn't register simserial driver\n");
852
853 return 0;
854}
855
856#ifndef MODULE
857__initcall(simrs_init);
858#endif