serial.c 10.8 KB
Newer Older
1 2 3
/*
 *  linux/kernel/serial.c
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5 6
 */

7 8 9 10 11
/*
 *	serial.c
 *
 * This module implements the rs232 io functions
 *	void rs_write(struct tty_struct * queue);
12
 *	long rs_init(long);
13 14 15
 * and all interrupts pertaining to serial IO.
 */

16 17
#include <linux/errno.h>
#include <linux/signal.h>
18
#include <linux/sched.h>
19
#include <linux/timer.h>
20 21
#include <linux/tty.h>

22 23
#include <asm/system.h>
#include <asm/io.h>
24
#include <asm/segment.h>
25

26
#define WAKEUP_CHARS (3*TTY_BUF_SIZE/4)
27

28 29 30 31 32 33
struct serial_struct serial_table[NR_SERIALS] = {
	{ PORT_UNKNOWN, 0, 0x3F8, 4, NULL},
	{ PORT_UNKNOWN, 1, 0x2F8, 3, NULL},
	{ PORT_UNKNOWN, 2, 0x3E8, 4, NULL},
	{ PORT_UNKNOWN, 3, 0x2E8, 3, NULL},
};
34

35 36 37 38 39 40 41 42 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
void send_break(unsigned int line)
{
	unsigned short port;
	struct serial_struct * info;

	if (line >= NR_SERIALS)
		return;
	info = serial_table + line;
	if (!(port = info->port))
		return;
	port += 3;
	current->state = TASK_INTERRUPTIBLE;
	current->timeout = jiffies + 25;
	outb_p(inb_p(port) | 0x40,port);
	schedule();
	outb_p(inb_p(port) & 0xbf,port);
}

/*
 * There are several races here: we avoid most of them by disabling timer_active
 * for the crucial part of the process.. That's a good idea anyway.
 *
 * The problem is that we have to output characters /both/ from interrupts
 * and from the normal write: the latter to be sure the interrupts start up
 * again. With serial lines, the interrupts can happen so often that the
 * races actually are noticeable.
 */
static void send_intr(struct serial_struct * info)
{
	unsigned short port = info->port;
	unsigned int timer = SER1_TIMEOUT + info->line;
	struct tty_queue * queue = info->tty->write_q;
	int c, i = 0;

	if (info->tty->stopped) return;

	timer_active &= ~(1 << timer);
	while (inb_p(info->port+5) & 0x20) {
		if (queue->tail == queue->head)
			goto end_send;
		c = queue->buf[queue->tail];
		queue->tail++;
		queue->tail &= TTY_BUF_SIZE-1;
  		outb(c,port);
		if ((info->type != PORT_16550A) || (++i >= 14) || info->tty->stopped)
			break;
	}
	timer_table[timer].expires = jiffies + 10;
	timer_active |= 1 << timer;
end_send:
	if (LEFT(queue) > WAKEUP_CHARS)
		wake_up(&queue->proc_list);
}

static void receive_intr(struct serial_struct * info)
{
	unsigned short port = info->port;
	struct tty_queue * queue = info->tty->read_q;
	int head = queue->head;
	int maxhead = (queue->tail-1) & (TTY_BUF_SIZE-1);

	timer_active &= ~((1<<SER1_TIMER)<<info->line);
	do {
		queue->buf[head] = inb(port);
		if (head != maxhead) {
			head++;
			head &= TTY_BUF_SIZE-1;
		}
	} while (inb(port+5) & 1);
	queue->head = head;
	timer_active |= (1<<SER1_TIMER)<<info->line;
}

static void line_status_intr(struct serial_struct * info)
{
	unsigned char status = inb(info->port+5);

/*	printk("line status: %02x\n",status); */
}

static void modem_status_intr(struct serial_struct * info)
{
	unsigned char status = inb(info->port+6);

	if (!(info->tty->termios.c_cflag & CLOCAL)) {
		if ((status & 0x88) == 0x08 && info->tty->pgrp > 0)
			kill_pg(info->tty->pgrp,SIGHUP,1);

		if (info->tty->termios.c_cflag & CRTSCTS)
			info->tty->stopped = !(status & 0x10);

		if (!info->tty->stopped)
			send_intr(info);
	}
}

static void (*jmp_table[4])(struct serial_struct *) = {
	modem_status_intr,
	send_intr,
	receive_intr,
	line_status_intr
};

static void check_tty(struct serial_struct * info)
{
	unsigned char ident;

	if (!info || !info->tty || !info->port)
		return;
	while (1) {
		ident = inb(info->port+2) & 7;
		if (ident & 1)
			return;
		ident >>= 1;
		if (ident > 3)
			return;
		jmp_table[ident](info);
	}
}

/*
 * Again, we disable interrupts to be sure there aren't any races:
 * see send_intr for details.
 */
static inline void do_rs_write(struct serial_struct * info)
{
	if (!info->tty || !info->port)
		return;
	if (!info->tty->write_q || EMPTY(info->tty->write_q))
		return;
	cli();
	send_intr(info);
	sti();
}

/*
 * IRQ routines: one per line
 */
static void com1_IRQ(int unused)
{
	check_tty(serial_table+0);
}

static void com2_IRQ(int unused)
{
	check_tty(serial_table+1);
}

static void com3_IRQ(int unused)
{
	check_tty(serial_table+2);
}

static void com4_IRQ(int unused)
{
	check_tty(serial_table+3);
}

/*
 * Receive timer routines: one per line
 */
196 197
static void com1_timer(void)
{
198
	TTY_READ_FLUSH(tty_table+64);
199 200 201 202
}

static void com2_timer(void)
{
203
	TTY_READ_FLUSH(tty_table+65);
204 205
}

206 207
static void com3_timer(void)
{
208
	TTY_READ_FLUSH(tty_table+66);
209 210 211 212
}

static void com4_timer(void)
{
213
	TTY_READ_FLUSH(tty_table+67);
214 215
}

216 217 218
/*
 * Send timeout routines: one per line
 */
219 220
static void com1_timeout(void)
{
221
	do_rs_write(serial_table);
222 223 224 225
}

static void com2_timeout(void)
{
226
	do_rs_write(serial_table + 1);
227 228
}

229 230
static void com3_timeout(void)
{
231
	do_rs_write(serial_table + 2);
232 233 234 235
}

static void com4_timeout(void)
{
236
	do_rs_write(serial_table + 3);
237 238
}

239
static void init(struct serial_struct * info)
240
{
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	unsigned char status1, status2, scratch;
	unsigned short port = info->port;

	if (inb(port+5) == 0xff) {
		info->type = PORT_UNKNOWN;
		return;
	}
	
	scratch = inb(port+7);
	outb_p(0xa5, port+7);
	status1 = inb(port+7);
	outb_p(0x5a, port+7);
	status2 = inb(port+7);
	if (status1 == 0xa5 && status2 == 0x5a) {
		outb_p(scratch, port+7);
		outb_p(0x01, port+2);
		scratch = inb(port+2) >> 6;
		switch (scratch) {
			case 0:
				info->type = PORT_16450;
				break;
			case 1:
				info->type = PORT_UNKNOWN;
				break;
			case 2:
				info->type = PORT_16550;
				outb_p(0x00, port+2);
				break;
			case 3:
				info->type = PORT_16550A;
				outb_p(0xc7, port+2);
				break;
		}
	} else
		info->type = PORT_8250;
276
	outb_p(0x80,port+3);	/* set DLAB of line control reg */
277
	outb_p(0x30,port);	/* LS of divisor (48 -> 2400 bps) */
278 279
	outb_p(0x00,port+1);	/* MS of divisor */
	outb_p(0x03,port+3);	/* reset DLAB */
280
	outb_p(0x00,port+4);	/* reset DTR,RTS, OUT_2 */
281
	outb_p(0x00,port+1);	/* disable all intrs */
282 283 284
	(void)inb(port);	/* read data port to reset things (?) */
}

285
void serial_close(unsigned line, struct file * filp)
286
{
287 288
	struct serial_struct * info;
	int irq;
289

290
	if (line >= NR_SERIALS)
291
		return;
292 293
	info = serial_table + line;
	if (!info->port)
294
		return;
295 296 297 298 299 300 301 302 303 304 305
	outb(0x00,info->port+4);	/* reset DTR, RTS, */
	irq = info->irq;
	if (irq == 2)
		irq = 9;
	free_irq(irq);
}

static void startup(unsigned short port)
{
	int i;

306
	outb_p(0x03,port+3);	/* reset DLAB */
307 308 309 310 311 312 313 314 315 316 317 318
	outb_p(0x0b,port+4);	/* set DTR,RTS, OUT_2 */
	outb_p(0x0f,port+1);	/* enable all intrs */
	inb_p(port+2);
	inb_p(port+6);
	inb_p(port+2);
	inb_p(port+5);
	for (i = 0; i < 16 ; i++) {
		inb_p(port+0);
		if (!(inb_p(port+5) & 1))
			break;
	}
	inb_p(port+2);
Linus Torvalds's avatar
Linus Torvalds committed
319
	inb_p(port+5);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 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 434 435 436 437 438 439 440 441 442
}

void change_speed(unsigned int line)
{
	struct serial_struct * info;
	unsigned short port,quot;
	unsigned cflag,cval;
	static unsigned short quotient[] = {
		0, 2304, 1536, 1047, 857,
		768, 576, 384, 192, 96,
		64, 48, 24, 12, 6, 3
	};

	if (line >= NR_SERIALS)
		return;
	info = serial_table + line;
	cflag = info->tty->termios.c_cflag;
	if (!(port = info->port))
		return;
	quot = quotient[cflag & CBAUD];
	if (!quot)
		outb(0x00,port+4);
	else if (!inb(port+4))
		startup(port);
/* byte size and parity */
	cval = cflag & (CSIZE | CSTOPB);
	cval >>= 4;
	if (cflag & PARENB)
		cval |= 8;
	if (!(cflag & PARODD))
		cval |= 16;
	cli();
	outb_p(cval | 0x80,port+3);	/* set DLAB */
	outb_p(quot & 0xff,port);	/* LS of divisor */
	outb_p(quot >> 8,port+1);	/* MS of divisor */
	outb(cval,port+3);		/* reset DLAB */
	sti();
}

static void (*serial_handler[NR_SERIALS])(int) = {
	com1_IRQ,com2_IRQ,com3_IRQ,com4_IRQ
};

/*
 * this routine enables interrupts on 'line', and disables them for any
 * other serial line that shared the same IRQ. Braindamaged AT hardware.
 */
int serial_open(unsigned line, struct file * filp)
{
	struct serial_struct * info;
	int irq,retval;
	unsigned short port;
	struct sigaction sa;

	sa.sa_handler = serial_handler[line];
	sa.sa_flags = SA_INTERRUPT;
	sa.sa_mask = 0;
	sa.sa_restorer = NULL;
	if (line >= NR_SERIALS)
		return -ENODEV;
	info = serial_table + line;
	if (!(port = info->port))
		return -ENODEV;
	irq = info->irq;
	if (irq == 2)
		irq = 9;
	if (retval = irqaction(irq,&sa))
		return retval;
	startup(port);
	return 0;
}

int get_serial_info(unsigned int line, struct serial_struct * info)
{
	if (line >= NR_SERIALS)
		return -ENODEV;
	if (!info)
		return -EFAULT;
	memcpy_tofs(info,serial_table+line,sizeof(*info));
	return 0;
}

int set_serial_info(unsigned int line, struct serial_struct * info)
{
	struct serial_struct tmp;
	unsigned new_port;
	unsigned irq,new_irq;
	int retval;
	void (*handler)(int) = serial_handler[line];

	if (!suser())
		return -EPERM;
	if (line >= NR_SERIALS)
		return -ENODEV;
	if (!info)
		return -EFAULT;
	memcpy_fromfs(&tmp,info,sizeof(tmp));
	info = serial_table + line;
	if (!(new_port = tmp.port))
		new_port = info->port;
	if (!(new_irq = tmp.irq))
		new_irq = info->irq;
	if (new_irq > 15 || new_port > 0xffff)
		return -EINVAL;
	if (new_irq == 2)
		new_irq = 9;
	irq = info->irq;
	if (irq == 2)
		irq = 9;
	if (irq != new_irq) {
		retval = request_irq(new_irq,handler);
		if (retval)
			return retval;
		info->irq = new_irq;
		free_irq(irq);
	}
	cli();
	if (new_port != info->port) {
		outb(0x00,info->port+4);	/* reset DTR, RTS, */
		info->port = new_port;
		init(info);
		startup(new_port);
	}
443
	sti();
444
	return 0;
445 446
}

447
long rs_init(long kmem_start)
448
{
449 450 451
	int i;
	struct serial_struct * info;

452 453 454 455 456
/* SERx_TIMER timers are used for receiving: timeout is always 0 (immediate) */
	timer_table[SER1_TIMER].fn = com1_timer;
	timer_table[SER1_TIMER].expires = 0;
	timer_table[SER2_TIMER].fn = com2_timer;
	timer_table[SER2_TIMER].expires = 0;
457 458 459 460
	timer_table[SER3_TIMER].fn = com3_timer;
	timer_table[SER3_TIMER].expires = 0;
	timer_table[SER4_TIMER].fn = com4_timer;
	timer_table[SER4_TIMER].expires = 0;
461 462 463 464 465
/* SERx_TIMEOUT timers are used for writing: prevent serial lockups */
	timer_table[SER1_TIMEOUT].fn = com1_timeout;
	timer_table[SER1_TIMEOUT].expires = 0;
	timer_table[SER2_TIMEOUT].fn = com2_timeout;
	timer_table[SER2_TIMEOUT].expires = 0;
466 467 468 469
	timer_table[SER3_TIMEOUT].fn = com3_timeout;
	timer_table[SER3_TIMEOUT].expires = 0;
	timer_table[SER4_TIMEOUT].fn = com4_timeout;
	timer_table[SER4_TIMEOUT].expires = 0;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	for (i = 0, info = serial_table; i < NR_SERIALS; i++,info++) {
		info->tty = (tty_table+64) + i;
		init(info);
		if (info->type == PORT_UNKNOWN)
			continue;
		printk("serial port at 0x%04x (irq = %d)",info->port,info->irq);
		switch (info->type) {
			case PORT_8250:
				printk(" is a 8250\n");
				break;
			case PORT_16450:
				printk(" is a 16450\n");
				break;
			case PORT_16550:
				printk(" is a 16550\n");
				break;
			case PORT_16550A:
				printk(" is a 16550A\n");
				break;
			default:
				printk("\n");
				break;
		}
	}
	return kmem_start;
495 496 497 498 499 500 501 502 503 504 505
}

/*
 * This routine gets called when tty_write has put something into
 * the write_queue. It must check wheter the queue is empty, and
 * set the interrupt register accordingly
 *
 *	void _rs_write(struct tty_struct * tty);
 */
void rs_write(struct tty_struct * tty)
{
506 507 508
	int line = tty - tty_table - 64;

	do_rs_write(serial_table+line);
509
}