simserial.c 20.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Simulated Serial Driver (fake serial)
 *
 * This driver is mostly used for bringup purposes and will go away.
 * It has a strong dependency on the system console. All outputs
 * are rerouted to the same facility as the one used by printk which, in our
 * case means sys_sim.c console (goes via the simulator). The code hereafter
 * is completely leveraged from the serial.c driver.
 *
 * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
 *	Stephane Eranian <eranian@hpl.hp.com>
 *	David Mosberger-Tang <davidm@hpl.hp.com>
 *
 * 02/04/00 D. Mosberger	Merged in serial.c bug fixes in rs_close().
 * 02/25/00 D. Mosberger	Synced up with 2.3.99pre-5 version of serial.c.
 * 07/30/02 D. Mosberger	Replace sti()/cli() with explicit spinlocks & local irq masking
 */

#include <linux/init.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
27
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
28
#include <linux/slab.h>
29
#include <linux/capability.h>
L
Linus Torvalds 已提交
30 31 32 33
#include <linux/console.h>
#include <linux/module.h>
#include <linux/serial.h>
#include <linux/serialP.h>
34
#include <linux/sysrq.h>
L
Linus Torvalds 已提交
35 36

#include <asm/irq.h>
J
Jiri Slaby 已提交
37
#include <asm/hpsim.h>
L
Linus Torvalds 已提交
38 39 40
#include <asm/hw_irq.h>
#include <asm/uaccess.h>

J
Jiri Slaby 已提交
41 42
#include "hpsim_ssc.h"

L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#undef SIMSERIAL_DEBUG	/* define this to get some debug information */

#define KEYBOARD_INTR	3	/* must match with simulator! */

#define NR_PORTS	1	/* only one port for now */

static char *serial_name = "SimSerial driver";
static char *serial_version = "0.6";

/*
 * This has been extracted from asm/serial.h. We need one eventually but
 * I don't know exactly what we're going to put in it so just fake one
 * for now.
 */
#define BASE_BAUD ( 1843200 / 16 )

#define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)

/*
 * Most of the values here are meaningless to this particular driver.
 * However some values must be preserved for the code (leveraged from serial.c
 * to work correctly).
 * port must not be 0
 * type must not be UNKNOWN
 * So I picked arbitrary (guess from where?) values instead
 */
static struct serial_state rs_table[NR_PORTS]={
  /* UART CLK   PORT IRQ     FLAGS        */
71
  { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 }  /* ttyS0 */
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
};

/*
 * Just for the fun of it !
 */
static struct serial_uart_config uart_config[] = {
	{ "unknown", 1, 0 },
	{ "8250", 1, 0 },
	{ "16450", 1, 0 },
	{ "16550", 1, 0 },
	{ "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
	{ "cirrus", 1, 0 },
	{ "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
	{ "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
		  UART_STARTECH },
	{ "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
A
Al Viro 已提交
88
	{ NULL, 0}
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
};

struct tty_driver *hp_simserial_driver;

static struct console *console;

static unsigned char *tmp_buf;

extern struct console *console_drivers; /* from kernel/printk.c */

/*
 * ------------------------------------------------------------
 * rs_stop() and rs_start()
 *
 * This routines are called before setting or resetting tty->stopped.
 * They enable or disable transmitter interrupts, as necessary.
 * ------------------------------------------------------------
 */
static void rs_stop(struct tty_struct *tty)
{
#ifdef SIMSERIAL_DEBUG
	printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
		tty->stopped, tty->hw_stopped, tty->flow_stopped);
#endif

}

static void rs_start(struct tty_struct *tty)
{
118
#ifdef SIMSERIAL_DEBUG
L
Linus Torvalds 已提交
119 120 121 122 123
	printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
		tty->stopped, tty->hw_stopped, tty->flow_stopped);
#endif
}

A
Al Viro 已提交
124
static  void receive_chars(struct tty_struct *tty)
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
{
	unsigned char ch;
	static unsigned char seen_esc = 0;

	while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
		if ( ch == 27 && seen_esc == 0 ) {
			seen_esc = 1;
			continue;
		} else {
			if ( seen_esc==1 && ch == 'O' ) {
				seen_esc = 2;
				continue;
			} else if ( seen_esc == 2 ) {
138 139 140 141 142 143 144 145
				if ( ch == 'P' ) /* F1 */
					show_state();
#ifdef CONFIG_MAGIC_SYSRQ
				if ( ch == 'S' ) { /* F4 */
					do
						ch = ia64_ssc(0, 0, 0, 0,
							      SSC_GETCHAR);
					while (!ch);
146
					handle_sysrq(ch);
147
				}
L
Linus Torvalds 已提交
148 149 150 151 152 153 154
#endif
				seen_esc = 0;
				continue;
			}
		}
		seen_esc = 0;

155 156
		if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
			break;
L
Linus Torvalds 已提交
157 158 159 160 161 162 163
	}
	tty_flip_buffer_push(tty);
}

/*
 * This is the serial driver's interrupt routine for a single port
 */
A
Al Viro 已提交
164
static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
L
Linus Torvalds 已提交
165
{
166
	struct serial_state *info = dev_id;
L
Linus Torvalds 已提交
167

168
	if (!info->tport.tty) {
L
Linus Torvalds 已提交
169 170 171 172 173 174 175
		printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
		return IRQ_NONE;
	}
	/*
	 * pretty simple in our case, because we only get interrupts
	 * on inbound traffic
	 */
176
	receive_chars(info->tport.tty);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185
	return IRQ_HANDLED;
}

/*
 * -------------------------------------------------------------------
 * Here ends the serial interrupt routines.
 * -------------------------------------------------------------------
 */

A
Alan Cox 已提交
186
static int rs_put_char(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
187
{
188
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
189 190
	unsigned long flags;

A
Alan Cox 已提交
191 192
	if (!tty || !info->xmit.buf)
		return 0;
L
Linus Torvalds 已提交
193 194 195 196

	local_irq_save(flags);
	if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
		local_irq_restore(flags);
A
Alan Cox 已提交
197
		return 0;
L
Linus Torvalds 已提交
198 199 200 201
	}
	info->xmit.buf[info->xmit.head] = ch;
	info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
	local_irq_restore(flags);
A
Alan Cox 已提交
202
	return 1;
L
Linus Torvalds 已提交
203 204
}

205 206
static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
		int *intr_done)
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217
{
	int count;
	unsigned long flags;

	local_irq_save(flags);

	if (info->x_char) {
		char c = info->x_char;

		console->write(console, &c, 1);

218
		info->icount.tx++;
L
Linus Torvalds 已提交
219 220 221 222 223
		info->x_char = 0;

		goto out;
	}

224 225
	if (info->xmit.head == info->xmit.tail || tty->stopped ||
			tty->hw_stopped) {
L
Linus Torvalds 已提交
226 227
#ifdef SIMSERIAL_DEBUG
		printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
228
		       info->xmit.head, info->xmit.tail, tty->stopped);
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#endif
		goto out;
	}
	/*
	 * We removed the loop and try to do it in to chunks. We need
	 * 2 operations maximum because it's a ring buffer.
	 *
	 * First from current to tail if possible.
	 * Then from the beginning of the buffer until necessary
	 */

	count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
		    SERIAL_XMIT_SIZE - info->xmit.tail);
	console->write(console, info->xmit.buf+info->xmit.tail, count);

	info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);

	/*
	 * We have more at the beginning of the buffer
	 */
	count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
	if (count) {
		console->write(console, info->xmit.buf, count);
		info->xmit.tail += count;
	}
out:
	local_irq_restore(flags);
}

static void rs_flush_chars(struct tty_struct *tty)
{
260
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
261 262 263 264 265

	if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
	    !info->xmit.buf)
		return;

266
	transmit_chars(tty, info, NULL);
L
Linus Torvalds 已提交
267 268 269 270 271 272
}


static int rs_write(struct tty_struct * tty,
		    const unsigned char *buf, int count)
{
273
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	int	c, ret = 0;
	unsigned long flags;

	if (!tty || !info->xmit.buf || !tmp_buf) return 0;

	local_irq_save(flags);
	while (1) {
		c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
		if (count < c)
			c = count;
		if (c <= 0) {
			break;
		}
		memcpy(info->xmit.buf + info->xmit.head, buf, c);
		info->xmit.head = ((info->xmit.head + c) &
				   (SERIAL_XMIT_SIZE-1));
		buf += c;
		count -= c;
		ret += c;
	}
	local_irq_restore(flags);
	/*
	 * Hey, we transmit directly from here in our case
	 */
	if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
	    && !tty->stopped && !tty->hw_stopped) {
300
		transmit_chars(tty, info, NULL);
L
Linus Torvalds 已提交
301 302 303 304 305 306
	}
	return ret;
}

static int rs_write_room(struct tty_struct *tty)
{
307
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
308 309 310 311 312 313

	return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
}

static int rs_chars_in_buffer(struct tty_struct *tty)
{
314
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
315 316 317 318 319 320

	return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
}

static void rs_flush_buffer(struct tty_struct *tty)
{
321
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
322 323 324 325 326 327
	unsigned long flags;

	local_irq_save(flags);
	info->xmit.head = info->xmit.tail = 0;
	local_irq_restore(flags);

A
Alan Cox 已提交
328
	tty_wakeup(tty);
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336
}

/*
 * This function is used to send a high-priority XON/XOFF character to
 * the device
 */
static void rs_send_xchar(struct tty_struct *tty, char ch)
{
337
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
338 339 340 341 342 343 344

	info->x_char = ch;
	if (ch) {
		/*
		 * I guess we could call console->write() directly but
		 * let's do that for now.
		 */
345
		transmit_chars(tty, info, NULL);
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	}
}

/*
 * ------------------------------------------------------------
 * rs_throttle()
 *
 * This routine is called by the upper-layer tty layer to signal that
 * incoming characters should be throttled.
 * ------------------------------------------------------------
 */
static void rs_throttle(struct tty_struct * tty)
{
	if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));

	printk(KERN_INFO "simrs_throttle called\n");
}

static void rs_unthrottle(struct tty_struct * tty)
{
366
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375 376 377

	if (I_IXOFF(tty)) {
		if (info->x_char)
			info->x_char = 0;
		else
			rs_send_xchar(tty, START_CHAR(tty));
	}
	printk(KERN_INFO "simrs_unthrottle called\n");
}


378
static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
379 380 381
{
	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
382
	    (cmd != TIOCMIWAIT)) {
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
		if (tty->flags & (1 << TTY_IO_ERROR))
		    return -EIO;
	}

	switch (cmd) {
		case TIOCGSERIAL:
			printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
			return 0;
		case TIOCSSERIAL:
			printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
			return 0;
		case TIOCSERCONFIG:
			printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
			return -EINVAL;

		case TIOCSERGETLSR: /* Get line status register */
			printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
			return  -EINVAL;

		case TIOCSERGSTRUCT:
			printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
#if 0
			if (copy_to_user((struct async_struct *) arg,
					 info, sizeof(struct async_struct)))
				return -EFAULT;
#endif
			return 0;

		/*
		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
		 * - mask passed in arg for lines of interest
		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
		 * Caller should use TIOCGICOUNT to see which one it was
		 */
		case TIOCMIWAIT:
			printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
			return 0;
		case TIOCSERGWILD:
		case TIOCSERSWILD:
			/* "setserial -W" is called in Debian boot */
			printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
			return 0;

		default:
			return -ENOIOCTLCMD;
		}
	return 0;
}

#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))

434
static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446
{
	/* Handle turning off CRTSCTS */
	if ((old_termios->c_cflag & CRTSCTS) &&
	    !(tty->termios->c_cflag & CRTSCTS)) {
		tty->hw_stopped = 0;
		rs_start(tty);
	}
}
/*
 * This routine will shutdown a serial port; interrupts are disabled, and
 * DTR is dropped if the hangup on close termio flag is on.
 */
447
static void shutdown(struct tty_struct *tty, struct serial_state *info)
L
Linus Torvalds 已提交
448 449 450
{
	unsigned long	flags;

451
	if (!(info->flags & ASYNC_INITIALIZED))
452
		return;
L
Linus Torvalds 已提交
453 454

#ifdef SIMSERIAL_DEBUG
455 456
	printk("Shutting down serial port %d (irq %d)...\n", info->line,
	       info->irq);
L
Linus Torvalds 已提交
457 458 459 460
#endif

	local_irq_save(flags);
	{
461 462
		if (info->irq)
			free_irq(info->irq, info);
L
Linus Torvalds 已提交
463 464 465

		if (info->xmit.buf) {
			free_page((unsigned long) info->xmit.buf);
A
Al Viro 已提交
466
			info->xmit.buf = NULL;
L
Linus Torvalds 已提交
467 468
		}

469
		set_bit(TTY_IO_ERROR, &tty->flags);
L
Linus Torvalds 已提交
470

471
		info->flags &= ~ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	}
	local_irq_restore(flags);
}

/*
 * ------------------------------------------------------------
 * rs_close()
 *
 * This routine is called when the serial port gets closed.  First, we
 * wait for the last remaining data to be sent.  Then, we unlink its
 * async structure from the interrupt chain if necessary, and we free
 * that IRQ if nothing is left in the chain.
 * ------------------------------------------------------------
 */
static void rs_close(struct tty_struct *tty, struct file * filp)
{
488
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
489 490
	unsigned long flags;

491 492
	if (!info)
		return;
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501 502

	local_irq_save(flags);
	if (tty_hung_up_p(filp)) {
#ifdef SIMSERIAL_DEBUG
		printk("rs_close: hung_up\n");
#endif
		local_irq_restore(flags);
		return;
	}
#ifdef SIMSERIAL_DEBUG
503
	printk("rs_close ttys%d, count = %d\n", info->line, info->count);
L
Linus Torvalds 已提交
504
#endif
505
	if ((tty->count == 1) && (info->count != 1)) {
L
Linus Torvalds 已提交
506 507
		/*
		 * Uh, oh.  tty->count is 1, which means that the tty
508
		 * structure will be freed.  info->count should always
L
Linus Torvalds 已提交
509 510 511 512 513
		 * be one in these conditions.  If it's greater than
		 * one, we've got real problems, since it means the
		 * serial port won't be shutdown.
		 */
		printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
514 515
		       "info->count is %d\n", info->count);
		info->count = 1;
L
Linus Torvalds 已提交
516
	}
517
	if (--info->count < 0) {
L
Linus Torvalds 已提交
518
		printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
519 520
		       info->line, info->count);
		info->count = 0;
L
Linus Torvalds 已提交
521
	}
522
	if (info->count) {
L
Linus Torvalds 已提交
523 524 525
		local_irq_restore(flags);
		return;
	}
526
	info->flags |= ASYNC_CLOSING;
L
Linus Torvalds 已提交
527 528 529 530 531 532
	local_irq_restore(flags);

	/*
	 * Now we wait for the transmit buffer to clear; and we notify
	 * the line discipline to only process XON/XOFF characters.
	 */
533
	shutdown(tty, info);
A
Alan Cox 已提交
534 535
	rs_flush_buffer(tty);
	tty_ldisc_flush(tty);
536 537
	info->tport.tty = NULL;
	if (info->tport.blocked_open) {
538 539
		if (info->tport.close_delay)
			schedule_timeout_interruptible(info->tport.close_delay);
540
		wake_up_interruptible(&info->tport.open_wait);
L
Linus Torvalds 已提交
541
	}
542
	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
543
	wake_up_interruptible(&info->tport.close_wait);
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
}

/*
 * rs_wait_until_sent() --- wait until the transmitter is empty
 */
static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
{
}


/*
 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
 */
static void rs_hangup(struct tty_struct *tty)
{
559
	struct serial_state *info = tty->driver_data;
L
Linus Torvalds 已提交
560 561 562 563 564 565

#ifdef SIMSERIAL_DEBUG
	printk("rs_hangup: called\n");
#endif

	rs_flush_buffer(tty);
566
	if (info->flags & ASYNC_CLOSING)
L
Linus Torvalds 已提交
567
		return;
568
	shutdown(tty, info);
L
Linus Torvalds 已提交
569

570 571
	info->count = 0;
	info->flags &= ~ASYNC_NORMAL_ACTIVE;
572 573
	info->tport.tty = NULL;
	wake_up_interruptible(&info->tport.open_wait);
L
Linus Torvalds 已提交
574 575 576
}


577
static int startup(struct tty_struct *tty, struct serial_state *state)
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586 587 588
{
	unsigned long flags;
	int	retval=0;
	unsigned long page;

	page = get_zeroed_page(GFP_KERNEL);
	if (!page)
		return -ENOMEM;

	local_irq_save(flags);

589
	if (state->flags & ASYNC_INITIALIZED) {
L
Linus Torvalds 已提交
590 591 592 593 594
		free_page(page);
		goto errout;
	}

	if (!state->port || !state->type) {
595
		set_bit(TTY_IO_ERROR, &tty->flags);
L
Linus Torvalds 已提交
596 597 598
		free_page(page);
		goto errout;
	}
599
	if (state->xmit.buf)
L
Linus Torvalds 已提交
600 601
		free_page(page);
	else
602
		state->xmit.buf = (unsigned char *) page;
L
Linus Torvalds 已提交
603 604

#ifdef SIMSERIAL_DEBUG
605
	printk("startup: ttys%d (irq %d)...", state->line, state->irq);
L
Linus Torvalds 已提交
606 607 608 609 610
#endif

	/*
	 * Allocate the IRQ if necessary
	 */
611
	if (state->irq) {
J
Jiri Slaby 已提交
612
		retval = request_irq(state->irq, rs_interrupt_single, 0,
613
				"simserial", state);
614
		if (retval)
L
Linus Torvalds 已提交
615 616 617
			goto errout;
	}

618
	clear_bit(TTY_IO_ERROR, &tty->flags);
L
Linus Torvalds 已提交
619

620
	state->xmit.head = state->xmit.tail = 0;
L
Linus Torvalds 已提交
621 622 623 624 625 626 627 628 629 630 631 632

#if 0
	/*
	 * Set up serial timers...
	 */
	timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
	timer_active |= 1 << RS_TIMER;
#endif

	/*
	 * Set up the tty->alt_speed kludge
	 */
633 634 635 636 637 638 639 640
	if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
		tty->alt_speed = 57600;
	if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
		tty->alt_speed = 115200;
	if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
		tty->alt_speed = 230400;
	if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
		tty->alt_speed = 460800;
L
Linus Torvalds 已提交
641

642
	state->flags |= ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
	local_irq_restore(flags);
	return 0;

errout:
	local_irq_restore(flags);
	return retval;
}


/*
 * This routine is called whenever a serial port is opened.  It
 * enables interrupts for a serial port, linking in its async structure into
 * the IRQ chain.   It also performs the serial-specific
 * initialization for the tty structure.
 */
static int rs_open(struct tty_struct *tty, struct file * filp)
{
660
	struct serial_state *info = rs_table + tty->index;
661
	int			retval;
L
Linus Torvalds 已提交
662 663
	unsigned long		page;

664
	info->count++;
665
	info->tport.tty = tty;
666
	tty->driver_data = info;
667
	tty->port = &info->tport;
L
Linus Torvalds 已提交
668 669

#ifdef SIMSERIAL_DEBUG
670
	printk("rs_open %s, count = %d\n", tty->name, info->count);
L
Linus Torvalds 已提交
671
#endif
672
	tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
L
Linus Torvalds 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686

	if (!tmp_buf) {
		page = get_zeroed_page(GFP_KERNEL);
		if (!page)
			return -ENOMEM;
		if (tmp_buf)
			free_page(page);
		else
			tmp_buf = (unsigned char *) page;
	}

	/*
	 * If the port is the middle of closing, bail out now
	 */
687 688
	if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
		if (info->flags & ASYNC_CLOSING)
689
			interruptible_sleep_on(&info->tport.close_wait);
L
Linus Torvalds 已提交
690
#ifdef SERIAL_DO_RESTART
691
		return ((info->flags & ASYNC_HUP_NOTIFY) ?
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700
			-EAGAIN : -ERESTARTSYS);
#else
		return -EAGAIN;
#endif
	}

	/*
	 * Start up serial port
	 */
701
	retval = startup(tty, info);
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	if (retval) {
		return retval;
	}

	/*
	 * figure out which console to use (should be one already)
	 */
	console = console_drivers;
	while (console) {
		if ((console->flags & CON_ENABLED) && console->write) break;
		console = console->next;
	}

#ifdef SIMSERIAL_DEBUG
	printk("rs_open ttys%d successful\n", info->line);
#endif
	return 0;
}

/*
 * /proc fs routines....
 */

725
static inline void line_info(struct seq_file *m, struct serial_state *state)
L
Linus Torvalds 已提交
726
{
727
	seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
L
Linus Torvalds 已提交
728 729 730 731
		       state->line, uart_config[state->type].name,
		       state->port, state->irq);
}

732
static int rs_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
733
{
734 735 736 737 738 739
	int i;

	seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
	for (i = 0; i < NR_PORTS; i++)
		line_info(m, &rs_table[i]);
	return 0;
L
Linus Torvalds 已提交
740 741
}

742 743 744 745 746 747 748 749 750 751 752 753 754
static int rs_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, rs_proc_show, NULL);
}

static const struct file_operations rs_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= rs_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
/*
 * ---------------------------------------------------------------------
 * rs_init() and friends
 *
 * rs_init() is called at boot-time to initialize the serial driver.
 * ---------------------------------------------------------------------
 */

/*
 * This routine prints out the appropriate serial driver version
 * number, and identifies which options were configured into this
 * driver.
 */
static inline void show_serial_version(void)
{
	printk(KERN_INFO "%s version %s with", serial_name, serial_version);
	printk(KERN_INFO " no serial options enabled\n");
}

J
Jeff Dike 已提交
774
static const struct tty_operations hp_ops = {
L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	.open = rs_open,
	.close = rs_close,
	.write = rs_write,
	.put_char = rs_put_char,
	.flush_chars = rs_flush_chars,
	.write_room = rs_write_room,
	.chars_in_buffer = rs_chars_in_buffer,
	.flush_buffer = rs_flush_buffer,
	.ioctl = rs_ioctl,
	.throttle = rs_throttle,
	.unthrottle = rs_unthrottle,
	.send_xchar = rs_send_xchar,
	.set_termios = rs_set_termios,
	.stop = rs_stop,
	.start = rs_start,
	.hangup = rs_hangup,
	.wait_until_sent = rs_wait_until_sent,
792
	.proc_fops = &rs_proc_fops,
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800
};

/*
 * The serial driver boot-time initialization code!
 */
static int __init
simrs_init (void)
{
801
	int			i, rc;
L
Linus Torvalds 已提交
802 803 804 805 806
	struct serial_state	*state;

	if (!ia64_platform_is("hpsim"))
		return -ENODEV;

807
	hp_simserial_driver = alloc_tty_driver(NR_PORTS);
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	if (!hp_simserial_driver)
		return -ENOMEM;

	show_serial_version();

	/* Initialize the tty_driver structure */

	hp_simserial_driver->driver_name = "simserial";
	hp_simserial_driver->name = "ttyS";
	hp_simserial_driver->major = TTY_MAJOR;
	hp_simserial_driver->minor_start = 64;
	hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
	hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
	hp_simserial_driver->init_termios = tty_std_termios;
	hp_simserial_driver->init_termios.c_cflag =
		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(hp_simserial_driver, &hp_ops);

	/*
	 * Let's have a little bit of fun !
	 */
	for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
831
		tty_port_init(&state->tport);
832
		state->tport.close_delay = 0; /* XXX really 0? */
L
Linus Torvalds 已提交
833 834 835 836

		if (state->type == PORT_UNKNOWN) continue;

		if (!state->irq) {
837
			if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
838
				panic("%s: out of interrupt vectors!\n",
839
				      __func__);
840
			state->irq = rc;
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
		}

		printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
		       state->line,
		       state->port, state->irq,
		       uart_config[state->type].name);
	}

	if (tty_register_driver(hp_simserial_driver))
		panic("Couldn't register simserial driver\n");

	return 0;
}

#ifndef MODULE
__initcall(simrs_init);
#endif