con3215.c 29.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * 3215 line mode terminal driver.
L
Linus Torvalds 已提交
3
 *
4 5
 * Copyright IBM Corp. 1999, 2009
 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
L
Linus Torvalds 已提交
6
 *
7 8 9
 * Updated:
 *  Aug-2000: Added tab support
 *	      Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
L
Linus Torvalds 已提交
10 11 12 13 14 15
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/tty.h>
A
Alan Cox 已提交
16
#include <linux/tty_flip.h>
L
Linus Torvalds 已提交
17 18 19 20
#include <linux/vt_kern.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/interrupt.h>
21
#include <linux/err.h>
22
#include <linux/reboot.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <linux/slab.h>
#include <asm/ccwdev.h>
#include <asm/cio.h>
#include <asm/io.h>
#include <asm/ebcdic.h>
#include <asm/uaccess.h>
#include <asm/delay.h>
#include <asm/cpcmd.h>
#include <asm/setup.h>

#include "ctrlchar.h"

#define NR_3215		    1
#define NR_3215_REQ	    (4*NR_3215)
#define RAW3215_BUFFER_SIZE 65536     /* output buffer size */
#define RAW3215_INBUF_SIZE  256	      /* input buffer size */
#define RAW3215_MIN_SPACE   128	      /* minimum free space for wakeup */
#define RAW3215_MIN_WRITE   1024      /* min. length for immediate output */
#define RAW3215_MAX_BYTES   3968      /* max. bytes to write with one ssch */
#define RAW3215_MAX_NEWLINE 50	      /* max. lines to write with one ssch */
#define RAW3215_NR_CCWS	    3
#define RAW3215_TIMEOUT	    HZ/10     /* time for delayed output */

#define RAW3215_FIXED	    1	      /* 3215 console device is not be freed */
#define RAW3215_ACTIVE	    2	      /* set if the device is in use */
#define RAW3215_WORKING	    4	      /* set if a request is being worked on */
#define RAW3215_THROTTLED   8	      /* set if reading is disabled */
#define RAW3215_STOPPED	    16	      /* set if writing is disabled */
#define RAW3215_CLOSING	    32	      /* set while in close process */
#define RAW3215_TIMER_RUNS  64	      /* set if the output delay timer is on */
#define RAW3215_FLUSHING    128	      /* set to flush buffer (no delay) */
54
#define RAW3215_FROZEN	    256	      /* set if 3215 is frozen for suspend */
L
Linus Torvalds 已提交
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

#define TAB_STOP_SIZE	    8	      /* tab stop size */

/*
 * Request types for a 3215 device
 */
enum raw3215_type {
	RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
};

/*
 * Request structure for a 3215 device
 */
struct raw3215_req {
	enum raw3215_type type;	      /* type of the request */
	int start, len;		      /* start index & len in output buffer */
	int delayable;		      /* indication to wait for more data */
	int residual;		      /* residual count for read request */
	struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
	struct raw3215_info *info;    /* pointer to main structure */
	struct raw3215_req *next;     /* pointer to next request */
} __attribute__ ((aligned(8)));

struct raw3215_info {
	struct ccw_device *cdev;      /* device for tty driver */
	spinlock_t *lock;	      /* pointer to irq lock */
	int flags;		      /* state flags */
	char *buffer;		      /* pointer to output buffer */
	char *inbuf;		      /* pointer to input buffer */
	int head;		      /* first free byte in output buffer */
	int count;		      /* number of bytes in output buffer */
	int written;		      /* number of bytes in write requests */
	struct tty_struct *tty;	      /* pointer to tty structure if present */
	struct raw3215_req *queued_read; /* pointer to queued read requests */
	struct raw3215_req *queued_write;/* pointer to queued write requests */
90
	struct tasklet_struct tlet;   /* tasklet to invoke tty_wakeup */
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
	wait_queue_head_t empty_wait; /* wait queue for flushing */
	struct timer_list timer;      /* timer for delayed output */
	int line_pos;		      /* position on the line (for tabs) */
	char ubuffer[80];	      /* copy_from_user buffer */
};

/* array of 3215 devices structures */
static struct raw3215_info *raw3215[NR_3215];
/* spinlock to protect the raw3215 array */
static DEFINE_SPINLOCK(raw3215_device_lock);
/* list of free request structures */
static struct raw3215_req *raw3215_freelist;
/* spinlock to protect free list */
static spinlock_t raw3215_freelist_lock;

static struct tty_driver *tty3215_driver;

/*
 * Get a request structure from the free list
 */
111 112
static inline struct raw3215_req *raw3215_alloc_req(void)
{
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
	struct raw3215_req *req;
	unsigned long flags;

	spin_lock_irqsave(&raw3215_freelist_lock, flags);
	req = raw3215_freelist;
	raw3215_freelist = req->next;
	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
	return req;
}

/*
 * Put a request structure back to the free list
 */
126 127
static inline void raw3215_free_req(struct raw3215_req *req)
{
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	unsigned long flags;

	if (req->type == RAW3215_FREE)
		return;		/* don't free a free request */
	req->type = RAW3215_FREE;
	spin_lock_irqsave(&raw3215_freelist_lock, flags);
	req->next = raw3215_freelist;
	raw3215_freelist = req;
	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
}

/*
 * Set up a read request that reads up to 160 byte from the 3215 device.
 * If there is a queued read request it is used, but that shouldn't happen
 * because a 3215 terminal won't accept a new read before the old one is
 * completed.
 */
145
static void raw3215_mk_read_req(struct raw3215_info *raw)
L
Linus Torvalds 已提交
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
{
	struct raw3215_req *req;
	struct ccw1 *ccw;

	/* there can only be ONE read request at a time */
	req = raw->queued_read;
	if (req == NULL) {
		/* no queued read request, use new req structure */
		req = raw3215_alloc_req();
		req->type = RAW3215_READ;
		req->info = raw;
		raw->queued_read = req;
	}

	ccw = req->ccws;
	ccw->cmd_code = 0x0A; /* read inquiry */
	ccw->flags = 0x20;    /* ignore incorrect length */
	ccw->count = 160;
	ccw->cda = (__u32) __pa(raw->inbuf);
}

/*
 * Set up a write request with the information from the main structure.
 * A ccw chain is created that writes as much as possible from the output
 * buffer to the 3215 device. If a queued write exists it is replaced by
 * the new, probably lengthened request.
 */
173
static void raw3215_mk_write_req(struct raw3215_info *raw)
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
{
	struct raw3215_req *req;
	struct ccw1 *ccw;
	int len, count, ix, lines;

	if (raw->count <= raw->written)
		return;
	/* check if there is a queued write request */
	req = raw->queued_write;
	if (req == NULL) {
		/* no queued write request, use new req structure */
		req = raw3215_alloc_req();
		req->type = RAW3215_WRITE;
		req->info = raw;
		raw->queued_write = req;
	} else {
		raw->written -= req->len;
	}

	ccw = req->ccws;
	req->start = (raw->head - raw->count + raw->written) &
		     (RAW3215_BUFFER_SIZE - 1);
	/*
	 * now we have to count newlines. We can at max accept
	 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
	 * a restriction in VM
	 */
	lines = 0;
	ix = req->start;
	while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
		if (raw->buffer[ix] == 0x15)
			lines++;
		ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
	}
	len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
	if (len > RAW3215_MAX_BYTES)
		len = RAW3215_MAX_BYTES;
	req->len = len;
	raw->written += len;

	/* set the indication if we should try to enlarge this request */
	req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);

	ix = req->start;
	while (len > 0) {
		if (ccw > req->ccws)
			ccw[-1].flags |= 0x40; /* use command chaining */
		ccw->cmd_code = 0x01; /* write, auto carrier return */
		ccw->flags = 0x20;    /* ignore incorrect length ind.  */
		ccw->cda =
			(__u32) __pa(raw->buffer + ix);
		count = len;
		if (ix + count > RAW3215_BUFFER_SIZE)
			count = RAW3215_BUFFER_SIZE - ix;
		ccw->count = count;
		len -= count;
		ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
		ccw++;
	}
	/*
	 * Add a NOP to the channel program. 3215 devices are purely
	 * emulated and its much better to avoid the channel end
	 * interrupt in this case.
	 */
	if (ccw > req->ccws)
		ccw[-1].flags |= 0x40; /* use command chaining */
	ccw->cmd_code = 0x03; /* NOP */
	ccw->flags = 0;
	ccw->cda = 0;
	ccw->count = 1;
}

/*
 * Start a read or a write request
 */
249
static void raw3215_start_io(struct raw3215_info *raw)
L
Linus Torvalds 已提交
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 276 277 278 279 280 281 282 283 284 285 286
{
	struct raw3215_req *req;
	int res;

	req = raw->queued_read;
	if (req != NULL &&
	    !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
		/* dequeue request */
		raw->queued_read = NULL;
		res = ccw_device_start(raw->cdev, req->ccws,
				       (unsigned long) req, 0, 0);
		if (res != 0) {
			/* do_IO failed, put request back to queue */
			raw->queued_read = req;
		} else {
			raw->flags |= RAW3215_WORKING;
		}
	}
	req = raw->queued_write;
	if (req != NULL &&
	    !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
		/* dequeue request */
		raw->queued_write = NULL;
		res = ccw_device_start(raw->cdev, req->ccws,
				       (unsigned long) req, 0, 0);
		if (res != 0) {
			/* do_IO failed, put request back to queue */
			raw->queued_write = req;
		} else {
			raw->flags |= RAW3215_WORKING;
		}
	}
}

/*
 * Function to start a delayed output after RAW3215_TIMEOUT seconds
 */
287
static void raw3215_timeout(unsigned long __data)
L
Linus Torvalds 已提交
288 289 290 291
{
	struct raw3215_info *raw = (struct raw3215_info *) __data;
	unsigned long flags;

M
Martin Schwidefsky 已提交
292
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
293 294 295
	if (raw->flags & RAW3215_TIMER_RUNS) {
		del_timer(&raw->timer);
		raw->flags &= ~RAW3215_TIMER_RUNS;
296 297 298 299
		if (!(raw->flags & RAW3215_FROZEN)) {
			raw3215_mk_write_req(raw);
			raw3215_start_io(raw);
		}
L
Linus Torvalds 已提交
300
	}
M
Martin Schwidefsky 已提交
301
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309
}

/*
 * Function to conditionally start an IO. A read is started immediately,
 * a write is only started immediately if the flush flag is on or the
 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
 */
310
static inline void raw3215_try_io(struct raw3215_info *raw)
L
Linus Torvalds 已提交
311
{
312
	if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FROZEN))
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		return;
	if (raw->queued_read != NULL)
		raw3215_start_io(raw);
	else if (raw->queued_write != NULL) {
		if ((raw->queued_write->delayable == 0) ||
		    (raw->flags & RAW3215_FLUSHING)) {
			/* execute write requests bigger than minimum size */
			raw3215_start_io(raw);
			if (raw->flags & RAW3215_TIMER_RUNS) {
				del_timer(&raw->timer);
				raw->flags &= ~RAW3215_TIMER_RUNS;
			}
		} else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
			/* delay small writes */
			init_timer(&raw->timer);
			raw->timer.expires = RAW3215_TIMEOUT + jiffies;
			raw->timer.data = (unsigned long) raw;
			raw->timer.function = raw3215_timeout;
			add_timer(&raw->timer);
			raw->flags |= RAW3215_TIMER_RUNS;
		}
	}
}

337 338 339 340 341 342 343 344 345
/*
 * Call tty_wakeup from tasklet context
 */
static void raw3215_wakeup(unsigned long data)
{
	struct raw3215_info *raw = (struct raw3215_info *) data;
	tty_wakeup(raw->tty);
}

L
Linus Torvalds 已提交
346
/*
H
Heiko Carstens 已提交
347
 * Try to start the next IO and wake up processes waiting on the tty.
L
Linus Torvalds 已提交
348
 */
H
Heiko Carstens 已提交
349
static void raw3215_next_io(struct raw3215_info *raw)
L
Linus Torvalds 已提交
350 351 352
{
	raw3215_mk_write_req(raw);
	raw3215_try_io(raw);
353 354
	if (raw->tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
		tasklet_schedule(&raw->tlet);
L
Linus Torvalds 已提交
355 356 357 358 359
}

/*
 * Interrupt routine, called from common io layer
 */
360 361
static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
			struct irb *irb)
L
Linus Torvalds 已提交
362 363 364 365 366
{
	struct raw3215_info *raw;
	struct raw3215_req *req;
	struct tty_struct *tty;
	int cstat, dstat;
H
Heiko Carstens 已提交
367
	int count;
L
Linus Torvalds 已提交
368

369
	raw = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
370
	req = (struct raw3215_req *) intparm;
371 372
	cstat = irb->scsw.cmd.cstat;
	dstat = irb->scsw.cmd.dstat;
373
	if (cstat != 0)
H
Heiko Carstens 已提交
374
		raw3215_next_io(raw);
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383
	if (dstat & 0x01) { /* we got a unit exception */
		dstat &= ~0x01;	 /* we can ignore it */
	}
	switch (dstat) {
	case 0x80:
		if (cstat != 0)
			break;
		/* Attention interrupt, someone hit the enter key */
		raw3215_mk_read_req(raw);
H
Heiko Carstens 已提交
384
		raw3215_next_io(raw);
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392
		break;
	case 0x08:
	case 0x0C:
		/* Channel end interrupt. */
		if ((raw = req->info) == NULL)
			return;		     /* That shouldn't happen ... */
		if (req->type == RAW3215_READ) {
			/* store residual count, then wait for device end */
393
			req->residual = irb->scsw.cmd.count;
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		}
		if (dstat == 0x08)
			break;
	case 0x04:
		/* Device end interrupt. */
		if ((raw = req->info) == NULL)
			return;		     /* That shouldn't happen ... */
		if (req->type == RAW3215_READ && raw->tty != NULL) {
			unsigned int cchar;

			tty = raw->tty;
			count = 160 - req->residual;
			EBCASC(raw->inbuf, count);
			cchar = ctrlchar_handle(raw->inbuf, count, tty);
			switch (cchar & CTRLCHAR_MASK) {
			case CTRLCHAR_SYSRQ:
				break;

			case CTRLCHAR_CTRL:
A
Alan Cox 已提交
413
				tty_insert_flip_char(tty, cchar, TTY_NORMAL);
L
Linus Torvalds 已提交
414 415 416 417 418
				tty_flip_buffer_push(raw->tty);
				break;

			case CTRLCHAR_NONE:
				if (count < 2 ||
A
Alan Cox 已提交
419 420 421 422
				    (strncmp(raw->inbuf+count-2, "\252n", 2) &&
				     strncmp(raw->inbuf+count-2, "^n", 2)) ) {
					/* add the auto \n */
					raw->inbuf[count] = '\n';
L
Linus Torvalds 已提交
423 424
					count++;
				} else
A
Alan Cox 已提交
425 426
					count -= 2;
				tty_insert_flip_string(tty, raw->inbuf, count);
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
				tty_flip_buffer_push(raw->tty);
				break;
			}
		} else if (req->type == RAW3215_WRITE) {
			raw->count -= req->len;
			raw->written -= req->len;
		}
		raw->flags &= ~RAW3215_WORKING;
		raw3215_free_req(req);
		/* check for empty wait */
		if (waitqueue_active(&raw->empty_wait) &&
		    raw->queued_write == NULL &&
		    raw->queued_read == NULL) {
			wake_up_interruptible(&raw->empty_wait);
		}
H
Heiko Carstens 已提交
442
		raw3215_next_io(raw);
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453
		break;
	default:
		/* Strange interrupt, I'll do my best to clean up */
		if (req != NULL && req->type != RAW3215_FREE) {
			if (req->type == RAW3215_WRITE) {
				raw->count -= req->len;
				raw->written -= req->len;
			}
			raw->flags &= ~RAW3215_WORKING;
			raw3215_free_req(req);
		}
H
Heiko Carstens 已提交
454
		raw3215_next_io(raw);
L
Linus Torvalds 已提交
455 456 457 458
	}
	return;
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
/*
 * Drop the oldest line from the output buffer.
 */
static void raw3215_drop_line(struct raw3215_info *raw)
{
	int ix;
	char ch;

	BUG_ON(raw->written != 0);
	ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
	while (raw->count > 0) {
		ch = raw->buffer[ix];
		ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
		raw->count--;
		if (ch == 0x15)
			break;
	}
	raw->head = ix;
}

L
Linus Torvalds 已提交
479 480 481 482 483
/*
 * Wait until length bytes are available int the output buffer.
 * Has to be called with the s390irq lock held. Can be called
 * disabled.
 */
484
static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
L
Linus Torvalds 已提交
485 486
{
	while (RAW3215_BUFFER_SIZE - raw->count < length) {
487 488 489 490 491 492 493
		/* While console is frozen for suspend we have no other
		 * choice but to drop message from the buffer to make
		 * room for even more messages. */
		if (raw->flags & RAW3215_FROZEN) {
			raw3215_drop_line(raw);
			continue;
		}
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502 503 504 505
		/* there might be a request pending */
		raw->flags |= RAW3215_FLUSHING;
		raw3215_mk_write_req(raw);
		raw3215_try_io(raw);
		raw->flags &= ~RAW3215_FLUSHING;
#ifdef CONFIG_TN3215_CONSOLE
		wait_cons_dev();
#endif
		/* Enough room freed up ? */
		if (RAW3215_BUFFER_SIZE - raw->count >= length)
			break;
		/* there might be another cpu waiting for the lock */
M
Martin Schwidefsky 已提交
506
		spin_unlock(get_ccwdev_lock(raw->cdev));
L
Linus Torvalds 已提交
507
		udelay(100);
M
Martin Schwidefsky 已提交
508
		spin_lock(get_ccwdev_lock(raw->cdev));
L
Linus Torvalds 已提交
509 510 511 512 513 514
	}
}

/*
 * String write routine for 3215 devices
 */
515 516
static void raw3215_write(struct raw3215_info *raw, const char *str,
			  unsigned int length)
L
Linus Torvalds 已提交
517 518 519 520 521
{
	unsigned long flags;
	int c, count;

	while (length > 0) {
M
Martin Schwidefsky 已提交
522
		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
		count = (length > RAW3215_BUFFER_SIZE) ?
					     RAW3215_BUFFER_SIZE : length;
		length -= count;

		raw3215_make_room(raw, count);

		/* copy string to output buffer and convert it to EBCDIC */
		while (1) {
			c = min_t(int, count,
				  min(RAW3215_BUFFER_SIZE - raw->count,
				      RAW3215_BUFFER_SIZE - raw->head));
			if (c <= 0)
				break;
			memcpy(raw->buffer + raw->head, str, c);
			ASCEBC(raw->buffer + raw->head, c);
			raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
			raw->count += c;
			raw->line_pos += c;
			str += c;
			count -= c;
		}
		if (!(raw->flags & RAW3215_WORKING)) {
			raw3215_mk_write_req(raw);
			/* start or queue request */
			raw3215_try_io(raw);
		}
M
Martin Schwidefsky 已提交
549
		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
550 551 552 553 554 555
	}
}

/*
 * Put character routine for 3215 devices
 */
556
static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
L
Linus Torvalds 已提交
557 558 559 560
{
	unsigned long flags;
	unsigned int length, i;

M
Martin Schwidefsky 已提交
561
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	if (ch == '\t') {
		length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
		raw->line_pos += length;
		ch = ' ';
	} else if (ch == '\n') {
		length = 1;
		raw->line_pos = 0;
	} else {
		length = 1;
		raw->line_pos++;
	}
	raw3215_make_room(raw, length);

	for (i = 0; i < length; i++) {
		raw->buffer[raw->head] = (char) _ascebc[(int) ch];
		raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
		raw->count++;
	}
	if (!(raw->flags & RAW3215_WORKING)) {
		raw3215_mk_write_req(raw);
		/* start or queue request */
		raw3215_try_io(raw);
	}
M
Martin Schwidefsky 已提交
585
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
586 587 588 589 590 591
}

/*
 * Flush routine, it simply sets the flush flag and tries to start
 * pending IO.
 */
592
static void raw3215_flush_buffer(struct raw3215_info *raw)
L
Linus Torvalds 已提交
593 594 595
{
	unsigned long flags;

M
Martin Schwidefsky 已提交
596
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
597 598 599 600 601
	if (raw->count > 0) {
		raw->flags |= RAW3215_FLUSHING;
		raw3215_try_io(raw);
		raw->flags &= ~RAW3215_FLUSHING;
	}
M
Martin Schwidefsky 已提交
602
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
603 604 605 606 607
}

/*
 * Fire up a 3215 device.
 */
608
static int raw3215_startup(struct raw3215_info *raw)
L
Linus Torvalds 已提交
609 610 611 612 613 614 615
{
	unsigned long flags;

	if (raw->flags & RAW3215_ACTIVE)
		return 0;
	raw->line_pos = 0;
	raw->flags |= RAW3215_ACTIVE;
M
Martin Schwidefsky 已提交
616
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
617
	raw3215_try_io(raw);
M
Martin Schwidefsky 已提交
618
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
619 620 621 622 623 624 625

	return 0;
}

/*
 * Shutdown a 3215 device.
 */
626
static void raw3215_shutdown(struct raw3215_info *raw)
L
Linus Torvalds 已提交
627 628 629 630 631 632 633
{
	DECLARE_WAITQUEUE(wait, current);
	unsigned long flags;

	if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
		return;
	/* Wait for outstanding requests, then free irq */
M
Martin Schwidefsky 已提交
634
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
635 636 637 638 639 640
	if ((raw->flags & RAW3215_WORKING) ||
	    raw->queued_write != NULL ||
	    raw->queued_read != NULL) {
		raw->flags |= RAW3215_CLOSING;
		add_wait_queue(&raw->empty_wait, &wait);
		set_current_state(TASK_INTERRUPTIBLE);
M
Martin Schwidefsky 已提交
641
		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
642
		schedule();
M
Martin Schwidefsky 已提交
643
		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
644 645 646 647
		remove_wait_queue(&raw->empty_wait, &wait);
		set_current_state(TASK_RUNNING);
		raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
	}
M
Martin Schwidefsky 已提交
648
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
649 650
}

651
static int raw3215_probe (struct ccw_device *cdev)
L
Linus Torvalds 已提交
652 653 654 655
{
	struct raw3215_info *raw;
	int line;

656
	/* Console is special. */
657
	if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
658
		return 0;
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	raw = kmalloc(sizeof(struct raw3215_info) +
		      RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
	if (raw == NULL)
		return -ENOMEM;

	spin_lock(&raw3215_device_lock);
	for (line = 0; line < NR_3215; line++) {
		if (!raw3215[line]) {
			raw3215[line] = raw;
			break;
		}
	}
	spin_unlock(&raw3215_device_lock);
	if (line == NR_3215) {
		kfree(raw);
		return -ENODEV;
	}

	raw->cdev = cdev;
	raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
	memset(raw, 0, sizeof(struct raw3215_info));
680
	raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
L
Linus Torvalds 已提交
681 682 683
				       GFP_KERNEL|GFP_DMA);
	if (raw->buffer == NULL) {
		spin_lock(&raw3215_device_lock);
H
Heiko Carstens 已提交
684
		raw3215[line] = NULL;
L
Linus Torvalds 已提交
685 686 687 688 689
		spin_unlock(&raw3215_device_lock);
		kfree(raw);
		return -ENOMEM;
	}
	init_waitqueue_head(&raw->empty_wait);
690
	tasklet_init(&raw->tlet, raw3215_wakeup, (unsigned long) raw);
L
Linus Torvalds 已提交
691

692
	dev_set_drvdata(&cdev->dev, raw);
L
Linus Torvalds 已提交
693 694 695 696 697
	cdev->handler = raw3215_irq;

	return 0;
}

698
static void raw3215_remove (struct ccw_device *cdev)
L
Linus Torvalds 已提交
699 700 701 702
{
	struct raw3215_info *raw;

	ccw_device_set_offline(cdev);
703
	raw = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
704
	if (raw) {
705
		dev_set_drvdata(&cdev->dev, NULL);
J
Jesper Juhl 已提交
706
		kfree(raw->buffer);
L
Linus Torvalds 已提交
707 708 709 710
		kfree(raw);
	}
}

711
static int raw3215_set_online (struct ccw_device *cdev)
L
Linus Torvalds 已提交
712 713 714
{
	struct raw3215_info *raw;

715
	raw = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
716 717 718 719 720 721
	if (!raw)
		return -ENODEV;

	return raw3215_startup(raw);
}

722
static int raw3215_set_offline (struct ccw_device *cdev)
L
Linus Torvalds 已提交
723 724 725
{
	struct raw3215_info *raw;

726
	raw = dev_get_drvdata(&cdev->dev);
L
Linus Torvalds 已提交
727 728 729 730 731 732 733 734
	if (!raw)
		return -ENODEV;

	raw3215_shutdown(raw);

	return 0;
}

735 736 737 738 739 740
static int raw3215_pm_stop(struct ccw_device *cdev)
{
	struct raw3215_info *raw;
	unsigned long flags;

	/* Empty the output buffer, then prevent new I/O. */
M
Martin Schwidefsky 已提交
741
	raw = dev_get_drvdata(&cdev->dev);
742 743 744 745 746 747 748 749 750 751 752 753 754
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
	raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
	raw->flags |= RAW3215_FROZEN;
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
	return 0;
}

static int raw3215_pm_start(struct ccw_device *cdev)
{
	struct raw3215_info *raw;
	unsigned long flags;

	/* Allow I/O again and flush output buffer. */
M
Martin Schwidefsky 已提交
755
	raw = dev_get_drvdata(&cdev->dev);
756 757 758 759 760 761 762 763 764
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
	raw->flags &= ~RAW3215_FROZEN;
	raw->flags |= RAW3215_FLUSHING;
	raw3215_try_io(raw);
	raw->flags &= ~RAW3215_FLUSHING;
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
	return 0;
}

L
Linus Torvalds 已提交
765 766 767 768 769 770
static struct ccw_device_id raw3215_id[] = {
	{ CCW_DEVICE(0x3215, 0) },
	{ /* end of list */ },
};

static struct ccw_driver raw3215_ccw_driver = {
771 772 773 774
	.driver = {
		.name	= "3215",
		.owner	= THIS_MODULE,
	},
L
Linus Torvalds 已提交
775 776 777 778 779
	.ids		= raw3215_id,
	.probe		= &raw3215_probe,
	.remove		= &raw3215_remove,
	.set_online	= &raw3215_set_online,
	.set_offline	= &raw3215_set_offline,
780 781 782
	.freeze		= &raw3215_pm_stop,
	.thaw		= &raw3215_pm_start,
	.restore	= &raw3215_pm_start,
783
	.int_class	= IOINT_C15,
L
Linus Torvalds 已提交
784 785 786 787 788 789
};

#ifdef CONFIG_TN3215_CONSOLE
/*
 * Write a string to the 3215 console
 */
790 791
static void con3215_write(struct console *co, const char *str,
			  unsigned int count)
L
Linus Torvalds 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
{
	struct raw3215_info *raw;
	int i;

	if (count <= 0)
		return;
	raw = raw3215[0];	/* console 3215 is the first one */
	while (count > 0) {
		for (i = 0; i < count; i++)
			if (str[i] == '\t' || str[i] == '\n')
				break;
		raw3215_write(raw, str, i);
		count -= i;
		str += i;
		if (count > 0) {
			raw3215_putchar(raw, *str);
			count--;
			str++;
		}
	}
}

static struct tty_driver *con3215_device(struct console *c, int *index)
{
	*index = c->index;
	return tty3215_driver;
}

/*
821 822
 * panic() calls con3215_flush through a panic_notifier
 * before the system enters a disabled, endless loop.
L
Linus Torvalds 已提交
823
 */
824
static void con3215_flush(void)
L
Linus Torvalds 已提交
825 826 827 828 829
{
	struct raw3215_info *raw;
	unsigned long flags;

	raw = raw3215[0];  /* console 3215 is the first one */
830 831 832 833 834
	if (raw->flags & RAW3215_FROZEN)
		/* The console is still frozen for suspend. */
		if (ccw_device_force_console())
			/* Forcing didn't work, no panic message .. */
			return;
M
Martin Schwidefsky 已提交
835
	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
836
	raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
M
Martin Schwidefsky 已提交
837
	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
838 839
}

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
static int con3215_notify(struct notifier_block *self,
			  unsigned long event, void *data)
{
	con3215_flush();
	return NOTIFY_OK;
}

static struct notifier_block on_panic_nb = {
	.notifier_call = con3215_notify,
	.priority = 0,
};

static struct notifier_block on_reboot_nb = {
	.notifier_call = con3215_notify,
	.priority = 0,
};

L
Linus Torvalds 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869
/*
 *  The console structure for the 3215 console
 */
static struct console con3215 = {
	.name	 = "ttyS",
	.write	 = con3215_write,
	.device	 = con3215_device,
	.flags	 = CON_PRINTBUFFER,
};

/*
 * 3215 console initialization code called from console_init().
 */
870
static int __init con3215_init(void)
L
Linus Torvalds 已提交
871 872 873 874 875 876 877 878 879 880 881 882
{
	struct ccw_device *cdev;
	struct raw3215_info *raw;
	struct raw3215_req *req;
	int i;

	/* Check if 3215 is to be the console */
	if (!CONSOLE_IS_3215)
		return -ENODEV;

	/* Set the console mode for VM */
	if (MACHINE_IS_VM) {
883 884
		cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
		cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
L
Linus Torvalds 已提交
885 886 887 888 889 890
	}

	/* allocate 3215 request structures */
	raw3215_freelist = NULL;
	spin_lock_init(&raw3215_freelist_lock);
	for (i = 0; i < NR_3215_REQ; i++) {
891
		req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
892 893 894 895 896
		req->next = raw3215_freelist;
		raw3215_freelist = req;
	}

	cdev = ccw_device_probe_console();
897
	if (IS_ERR(cdev))
L
Linus Torvalds 已提交
898 899 900
		return -ENODEV;

	raw3215[0] = raw = (struct raw3215_info *)
901 902 903
		kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
	raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
	raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
904
	raw->cdev = cdev;
905
	dev_set_drvdata(&cdev->dev, raw);
L
Linus Torvalds 已提交
906 907 908 909
	cdev->handler = raw3215_irq;

	raw->flags |= RAW3215_FIXED;
	init_waitqueue_head(&raw->empty_wait);
910
	tasklet_init(&raw->tlet, raw3215_wakeup, (unsigned long) raw);
L
Linus Torvalds 已提交
911 912 913

	/* Request the console irq */
	if (raw3215_startup(raw) != 0) {
914 915 916
		kfree(raw->inbuf);
		kfree(raw->buffer);
		kfree(raw);
L
Linus Torvalds 已提交
917 918 919
		raw3215[0] = NULL;
		return -ENODEV;
	}
920 921
	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
	register_reboot_notifier(&on_reboot_nb);
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930 931 932
	register_console(&con3215);
	return 0;
}
console_initcall(con3215_init);
#endif

/*
 * tty3215_open
 *
 * This routine is called whenever a 3215 tty is opened.
 */
933
static int tty3215_open(struct tty_struct *tty, struct file * filp)
L
Linus Torvalds 已提交
934 935
{
	struct raw3215_info *raw;
936
	int retval;
L
Linus Torvalds 已提交
937

938
	raw = raw3215[tty->index];
L
Linus Torvalds 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	if (raw == NULL)
		return -ENODEV;

	tty->driver_data = raw;
	raw->tty = tty;

	tty->low_latency = 0;  /* don't use bottom half for pushing chars */
	/*
	 * Start up 3215 device
	 */
	retval = raw3215_startup(raw);
	if (retval)
		return retval;

	return 0;
}

/*
 * tty3215_close()
 *
 * This routine is called when the 3215 tty is closed. We wait
 * for the remaining request to be completed. Then we clean up.
 */
962
static void tty3215_close(struct tty_struct *tty, struct file * filp)
L
Linus Torvalds 已提交
963 964 965 966 967 968 969 970 971
{
	struct raw3215_info *raw;

	raw = (struct raw3215_info *) tty->driver_data;
	if (raw == NULL || tty->count > 1)
		return;
	tty->closing = 1;
	/* Shutdown the terminal */
	raw3215_shutdown(raw);
972
	tasklet_kill(&raw->tlet);
L
Linus Torvalds 已提交
973 974 975 976 977 978 979
	tty->closing = 0;
	raw->tty = NULL;
}

/*
 * Returns the amount of free space in the output buffer.
 */
980
static int tty3215_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
{
	struct raw3215_info *raw;

	raw = (struct raw3215_info *) tty->driver_data;

	/* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
	if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
		return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
	else
		return 0;
}

/*
 * String write routine for 3215 ttys
 */
996 997
static int tty3215_write(struct tty_struct * tty,
			 const unsigned char *buf, int count)
L
Linus Torvalds 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
{
	struct raw3215_info *raw;

	if (!tty)
		return 0;
	raw = (struct raw3215_info *) tty->driver_data;
	raw3215_write(raw, buf, count);
	return count;
}

/*
 * Put character routine for 3215 ttys
 */
1011
static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
L
Linus Torvalds 已提交
1012 1013 1014 1015
{
	struct raw3215_info *raw;

	if (!tty)
1016
		return 0;
L
Linus Torvalds 已提交
1017 1018
	raw = (struct raw3215_info *) tty->driver_data;
	raw3215_putchar(raw, ch);
1019
	return 1;
L
Linus Torvalds 已提交
1020 1021
}

1022
static void tty3215_flush_chars(struct tty_struct *tty)
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028
{
}

/*
 * Returns the number of characters in the output buffer
 */
1029
static int tty3215_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036
{
	struct raw3215_info *raw;

	raw = (struct raw3215_info *) tty->driver_data;
	return raw->count;
}

1037
static void tty3215_flush_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
{
	struct raw3215_info *raw;

	raw = (struct raw3215_info *) tty->driver_data;
	raw3215_flush_buffer(raw);
	tty_wakeup(tty);
}

/*
 * Disable reading from a 3215 tty
 */
1049
static void tty3215_throttle(struct tty_struct * tty)
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
{
	struct raw3215_info *raw;

	raw = (struct raw3215_info *) tty->driver_data;
	raw->flags |= RAW3215_THROTTLED;
}

/*
 * Enable reading from a 3215 tty
 */
1060
static void tty3215_unthrottle(struct tty_struct * tty)
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066
{
	struct raw3215_info *raw;
	unsigned long flags;

	raw = (struct raw3215_info *) tty->driver_data;
	if (raw->flags & RAW3215_THROTTLED) {
M
Martin Schwidefsky 已提交
1067
		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
1068 1069
		raw->flags &= ~RAW3215_THROTTLED;
		raw3215_try_io(raw);
M
Martin Schwidefsky 已提交
1070
		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
1071 1072 1073 1074 1075 1076
	}
}

/*
 * Disable writing to a 3215 tty
 */
1077
static void tty3215_stop(struct tty_struct *tty)
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
{
	struct raw3215_info *raw;

	raw = (struct raw3215_info *) tty->driver_data;
	raw->flags |= RAW3215_STOPPED;
}

/*
 * Enable writing to a 3215 tty
 */
1088
static void tty3215_start(struct tty_struct *tty)
L
Linus Torvalds 已提交
1089 1090 1091 1092 1093 1094
{
	struct raw3215_info *raw;
	unsigned long flags;

	raw = (struct raw3215_info *) tty->driver_data;
	if (raw->flags & RAW3215_STOPPED) {
M
Martin Schwidefsky 已提交
1095
		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
1096 1097
		raw->flags &= ~RAW3215_STOPPED;
		raw3215_try_io(raw);
M
Martin Schwidefsky 已提交
1098
		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
L
Linus Torvalds 已提交
1099 1100 1101
	}
}

J
Jeff Dike 已提交
1102
static const struct tty_operations tty3215_ops = {
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	.open = tty3215_open,
	.close = tty3215_close,
	.write = tty3215_write,
	.put_char = tty3215_put_char,
	.flush_chars = tty3215_flush_chars,
	.write_room = tty3215_write_room,
	.chars_in_buffer = tty3215_chars_in_buffer,
	.flush_buffer = tty3215_flush_buffer,
	.throttle = tty3215_throttle,
	.unthrottle = tty3215_unthrottle,
	.stop = tty3215_stop,
	.start = tty3215_start,
};

/*
 * 3215 tty registration code called from tty_init().
 * Most kernel services (incl. kmalloc) are available at this poimt.
 */
1121
static int __init tty3215_init(void)
L
Linus Torvalds 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
{
	struct tty_driver *driver;
	int ret;

	if (!CONSOLE_IS_3215)
		return 0;

	driver = alloc_tty_driver(NR_3215);
	if (!driver)
		return -ENOMEM;

	ret = ccw_driver_register(&raw3215_ccw_driver);
	if (ret) {
		put_tty_driver(driver);
		return ret;
	}
	/*
	 * Initialize the tty_driver structure
	 * Entries in tty3215_driver that are NOT initialized:
	 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
	 */

	driver->driver_name = "tty3215";
	driver->name = "ttyS";
	driver->major = TTY_MAJOR;
	driver->minor_start = 64;
	driver->type = TTY_DRIVER_TYPE_SYSTEM;
	driver->subtype = SYSTEM_TYPE_TTY;
	driver->init_termios = tty_std_termios;
	driver->init_termios.c_iflag = IGNBRK | IGNPAR;
	driver->init_termios.c_oflag = ONLCR | XTABS;
	driver->init_termios.c_lflag = ISIG;
	driver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(driver, &tty3215_ops);
	ret = tty_register_driver(driver);
	if (ret) {
		put_tty_driver(driver);
		return ret;
	}
	tty3215_driver = driver;
	return 0;
}

1165
static void __exit tty3215_exit(void)
L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173
{
	tty_unregister_driver(tty3215_driver);
	put_tty_driver(tty3215_driver);
	ccw_driver_unregister(&raw3215_ccw_driver);
}

module_init(tty3215_init);
module_exit(tty3215_exit);