sclp_vt220.c 21.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * SCLP VT220 terminal driver.
L
Linus Torvalds 已提交
3
 *
4 5 6
 * Copyright IBM Corp. 2003, 2009
 *
 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16
 */

#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/list.h>
#include <linux/wait.h>
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
A
Alan Cox 已提交
17
#include <linux/tty_flip.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/major.h>
#include <linux/console.h>
#include <linux/kdev_t.h>
#include <linux/interrupt.h>
#include <linux/init.h>
25
#include <linux/reboot.h>
26
#include <linux/slab.h>
27

L
Linus Torvalds 已提交
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 54 55 56 57
#include <asm/uaccess.h>
#include "sclp.h"

#define SCLP_VT220_MAJOR		TTY_MAJOR
#define SCLP_VT220_MINOR		65
#define SCLP_VT220_DRIVER_NAME		"sclp_vt220"
#define SCLP_VT220_DEVICE_NAME		"ttysclp"
#define SCLP_VT220_CONSOLE_NAME		"ttyS"
#define SCLP_VT220_CONSOLE_INDEX	1	/* console=ttyS1 */

/* Representation of a single write request */
struct sclp_vt220_request {
	struct list_head list;
	struct sclp_req sclp_req;
	int retry_count;
};

/* VT220 SCCB */
struct sclp_vt220_sccb {
	struct sccb_header header;
	struct evbuf_header evbuf;
};

#define SCLP_VT220_MAX_CHARS_PER_BUFFER	(PAGE_SIZE - \
					 sizeof(struct sclp_vt220_request) - \
					 sizeof(struct sclp_vt220_sccb))

/* Structures and data needed to register tty driver */
static struct tty_driver *sclp_vt220_driver;

J
Jiri Slaby 已提交
58
static struct tty_port sclp_vt220_port;
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68

/* Lock to protect internal data from concurrent access */
static spinlock_t sclp_vt220_lock;

/* List of empty pages to be used as write request buffers */
static struct list_head sclp_vt220_empty;

/* List of pending requests */
static struct list_head sclp_vt220_outqueue;

69 70 71 72 73
/* Suspend mode flag */
static int sclp_vt220_suspended;

/* Flag that output queue is currently running */
static int sclp_vt220_queue_running;
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85

/* Timer used for delaying write requests to merge subsequent messages into
 * a single buffer */
static struct timer_list sclp_vt220_timer;

/* Pointer to current request buffer which has been partially filled but not
 * yet sent */
static struct sclp_vt220_request *sclp_vt220_current_request;

/* Number of characters in current request buffer */
static int sclp_vt220_buffered_chars;

86 87
/* Counter controlling core driver initialization. */
static int __initdata sclp_vt220_init_count;
L
Linus Torvalds 已提交
88 89 90 91 92 93 94

/* Flag indicating that sclp_vt220_current_request should really
 * have been already queued but wasn't because the SCLP was processing
 * another buffer */
static int sclp_vt220_flush_later;

static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
95 96
static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
				   enum sclp_pm_event sclp_pm_event);
L
Linus Torvalds 已提交
97 98 99 100 101
static int __sclp_vt220_emit(struct sclp_vt220_request *request);
static void sclp_vt220_emit_current(void);

/* Registration structure for our interest in SCLP event buffers */
static struct sclp_register sclp_vt220_register = {
102 103
	.send_mask		= EVTYP_VT220MSG_MASK,
	.receive_mask		= EVTYP_VT220MSG_MASK,
L
Linus Torvalds 已提交
104
	.state_change_fn	= NULL,
105 106
	.receiver_fn		= sclp_vt220_receiver_fn,
	.pm_event_fn		= sclp_vt220_pm_event_fn,
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116
};


/*
 * Put provided request buffer back into queue and check emit pending
 * buffers if necessary.
 */
static void
sclp_vt220_process_queue(struct sclp_vt220_request *request)
{
J
Jiri Slaby 已提交
117
	struct tty_struct *tty;
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	unsigned long flags;
	void *page;

	do {
		/* Put buffer back to list of empty buffers */
		page = request->sclp_req.sccb;
		spin_lock_irqsave(&sclp_vt220_lock, flags);
		/* Move request from outqueue to empty queue */
		list_del(&request->list);
		list_add_tail((struct list_head *) page, &sclp_vt220_empty);
		/* Check if there is a pending buffer on the out queue. */
		request = NULL;
		if (!list_empty(&sclp_vt220_outqueue))
			request = list_entry(sclp_vt220_outqueue.next,
					     struct sclp_vt220_request, list);
133 134 135 136 137
		if (!request || sclp_vt220_suspended) {
			sclp_vt220_queue_running = 0;
			spin_unlock_irqrestore(&sclp_vt220_lock, flags);
			break;
		}
L
Linus Torvalds 已提交
138
		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
139
	} while (__sclp_vt220_emit(request));
L
Linus Torvalds 已提交
140 141 142
	if (request == NULL && sclp_vt220_flush_later)
		sclp_vt220_emit_current();
	/* Check if the tty needs a wake up call */
J
Jiri Slaby 已提交
143 144 145 146
	tty = tty_port_tty_get(&sclp_vt220_port);
	if (tty) {
		tty_wakeup(tty);
		tty_kref_put(tty);
L
Linus Torvalds 已提交
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	}
}

#define SCLP_BUFFER_MAX_RETRY		1

/*
 * Callback through which the result of a write request is reported by the
 * SCLP.
 */
static void
sclp_vt220_callback(struct sclp_req *request, void *data)
{
	struct sclp_vt220_request *vt220_request;
	struct sclp_vt220_sccb *sccb;

	vt220_request = (struct sclp_vt220_request *) data;
	if (request->status == SCLP_REQ_FAILED) {
		sclp_vt220_process_queue(vt220_request);
		return;
	}
	sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;

	/* Check SCLP response code and choose suitable action	*/
	switch (sccb->header.response_code) {
	case 0x0020 :
		break;

	case 0x05f0: /* Target resource in improper state */
		break;

	case 0x0340: /* Contained SCLP equipment check */
		if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
			break;
		/* Remove processed buffers and requeue rest */
		if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
			/* Not all buffers were processed */
			sccb->header.response_code = 0x0000;
			vt220_request->sclp_req.status = SCLP_REQ_FILLED;
			if (sclp_add_request(request) == 0)
				return;
		}
		break;

	case 0x0040: /* SCLP equipment check */
		if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
			break;
		sccb->header.response_code = 0x0000;
		vt220_request->sclp_req.status = SCLP_REQ_FILLED;
		if (sclp_add_request(request) == 0)
			return;
		break;

	default:
		break;
	}
	sclp_vt220_process_queue(vt220_request);
}

/*
 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
 * otherwise.
 */
static int
__sclp_vt220_emit(struct sclp_vt220_request *request)
{
212
	if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
L
Linus Torvalds 已提交
213 214 215
		request->sclp_req.status = SCLP_REQ_FAILED;
		return -EIO;
	}
216
	request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224
	request->sclp_req.status = SCLP_REQ_FILLED;
	request->sclp_req.callback = sclp_vt220_callback;
	request->sclp_req.callback_data = (void *) request;

	return sclp_add_request(&request->sclp_req);
}

/*
225
 * Queue and emit current request.
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234
 */
static void
sclp_vt220_emit_current(void)
{
	unsigned long flags;
	struct sclp_vt220_request *request;
	struct sclp_vt220_sccb *sccb;

	spin_lock_irqsave(&sclp_vt220_lock, flags);
235
	if (sclp_vt220_current_request) {
L
Linus Torvalds 已提交
236 237 238 239
		sccb = (struct sclp_vt220_sccb *) 
				sclp_vt220_current_request->sclp_req.sccb;
		/* Only emit buffers with content */
		if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
240 241
			list_add_tail(&sclp_vt220_current_request->list,
				      &sclp_vt220_outqueue);
L
Linus Torvalds 已提交
242 243 244 245 246 247
			sclp_vt220_current_request = NULL;
			if (timer_pending(&sclp_vt220_timer))
				del_timer(&sclp_vt220_timer);
		}
		sclp_vt220_flush_later = 0;
	}
248 249 250 251 252 253 254 255 256 257 258 259 260
	if (sclp_vt220_queue_running || sclp_vt220_suspended)
		goto out_unlock;
	if (list_empty(&sclp_vt220_outqueue))
		goto out_unlock;
	request = list_first_entry(&sclp_vt220_outqueue,
				   struct sclp_vt220_request, list);
	sclp_vt220_queue_running = 1;
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);

	if (__sclp_vt220_emit(request))
		sclp_vt220_process_queue(request);
	return;
out_unlock:
L
Linus Torvalds 已提交
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
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
}

#define SCLP_NORMAL_WRITE	0x00

/*
 * Helper function to initialize a page with the sclp request structure.
 */
static struct sclp_vt220_request *
sclp_vt220_initialize_page(void *page)
{
	struct sclp_vt220_request *request;
	struct sclp_vt220_sccb *sccb;

	/* Place request structure at end of page */
	request = ((struct sclp_vt220_request *)
			((addr_t) page + PAGE_SIZE)) - 1;
	request->retry_count = 0;
	request->sclp_req.sccb = page;
	/* SCCB goes at start of page */
	sccb = (struct sclp_vt220_sccb *) page;
	memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
	sccb->header.length = sizeof(struct sclp_vt220_sccb);
	sccb->header.function_code = SCLP_NORMAL_WRITE;
	sccb->header.response_code = 0x0000;
286
	sccb->evbuf.type = EVTYP_VT220MSG;
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 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
	sccb->evbuf.length = sizeof(struct evbuf_header);

	return request;
}

static inline unsigned int
sclp_vt220_space_left(struct sclp_vt220_request *request)
{
	struct sclp_vt220_sccb *sccb;
	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
	return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
	       sccb->header.length;
}

static inline unsigned int
sclp_vt220_chars_stored(struct sclp_vt220_request *request)
{
	struct sclp_vt220_sccb *sccb;
	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
	return sccb->evbuf.length - sizeof(struct evbuf_header);
}

/*
 * Add msg to buffer associated with request. Return the number of characters
 * added.
 */
static int
sclp_vt220_add_msg(struct sclp_vt220_request *request,
		   const unsigned char *msg, int count, int convertlf)
{
	struct sclp_vt220_sccb *sccb;
	void *buffer;
	unsigned char c;
	int from;
	int to;

	if (count > sclp_vt220_space_left(request))
		count = sclp_vt220_space_left(request);
	if (count <= 0)
		return 0;

	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
	buffer = (void *) ((addr_t) sccb + sccb->header.length);

	if (convertlf) {
		/* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
		for (from=0, to=0;
		     (from < count) && (to < sclp_vt220_space_left(request));
		     from++) {
			/* Retrieve character */
			c = msg[from];
			/* Perform conversion */
			if (c == 0x0a) {
				if (to + 1 < sclp_vt220_space_left(request)) {
					((unsigned char *) buffer)[to++] = c;
					((unsigned char *) buffer)[to++] = 0x0d;
				} else
					break;

			} else
				((unsigned char *) buffer)[to++] = c;
		}
		sccb->header.length += to;
		sccb->evbuf.length += to;
		return from;
	} else {
		memcpy(buffer, (const void *) msg, count);
		sccb->header.length += count;
		sccb->evbuf.length += count;
		return count;
	}
}

/*
 * Emit buffer after having waited long enough for more data to arrive.
 */
static void
sclp_vt220_timeout(unsigned long data)
{
	sclp_vt220_emit_current();
}

369
#define BUFFER_MAX_DELAY	HZ/20
L
Linus Torvalds 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

/* 
 * Internal implementation of the write function. Write COUNT bytes of data
 * from memory at BUF
 * to the SCLP interface. In case that the data does not fit into the current
 * write buffer, emit the current one and allocate a new one. If there are no
 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
 * is non-zero, the buffer will be scheduled for emitting after a timeout -
 * otherwise the user has to explicitly call the flush function.
 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
 * buffer should be converted to 0x0a 0x0d. After completion, return the number
 * of bytes written.
 */
static int
__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
385
		   int convertlf, int may_fail)
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396
{
	unsigned long flags;
	void *page;
	int written;
	int overall_written;

	if (count <= 0)
		return 0;
	overall_written = 0;
	spin_lock_irqsave(&sclp_vt220_lock, flags);
	do {
397
		/* Create an sclp output buffer if none exists yet */
L
Linus Torvalds 已提交
398 399
		if (sclp_vt220_current_request == NULL) {
			while (list_empty(&sclp_vt220_empty)) {
400
				spin_unlock_irqrestore(&sclp_vt220_lock, flags);
401
				if (may_fail || sclp_vt220_suspended)
402
					goto out;
L
Linus Torvalds 已提交
403
				else
404
					sclp_sync_wait();
L
Linus Torvalds 已提交
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
				spin_lock_irqsave(&sclp_vt220_lock, flags);
			}
			page = (void *) sclp_vt220_empty.next;
			list_del((struct list_head *) page);
			sclp_vt220_current_request =
				sclp_vt220_initialize_page(page);
		}
		/* Try to write the string to the current request buffer */
		written = sclp_vt220_add_msg(sclp_vt220_current_request,
					     buf, count, convertlf);
		overall_written += written;
		if (written == count)
			break;
		/*
		 * Not all characters could be written to the current
		 * output buffer. Emit the buffer, create a new buffer
		 * and then output the rest of the string.
		 */
		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
		sclp_vt220_emit_current();
		spin_lock_irqsave(&sclp_vt220_lock, flags);
		buf += written;
		count -= written;
	} while (count > 0);
	/* Setup timer to output current console buffer after some time */
	if (sclp_vt220_current_request != NULL &&
	    !timer_pending(&sclp_vt220_timer) && do_schedule) {
		sclp_vt220_timer.function = sclp_vt220_timeout;
		sclp_vt220_timer.data = 0UL;
		sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
		add_timer(&sclp_vt220_timer);
	}
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
438
out:
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450
	return overall_written;
}

/*
 * This routine is called by the kernel to write a series of
 * characters to the tty device.  The characters may come from
 * user space or kernel space.  This routine will return the
 * number of characters actually accepted for writing.
 */
static int
sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
451
	return __sclp_vt220_write(buf, count, 1, 0, 1);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463
}

#define SCLP_VT220_SESSION_ENDED	0x01
#define	SCLP_VT220_SESSION_STARTED	0x80
#define SCLP_VT220_SESSION_DATA		0x00

/*
 * Called by the SCLP to report incoming event buffers.
 */
static void
sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
{
J
Jiri Slaby 已提交
464
	struct tty_struct *tty = tty_port_tty_get(&sclp_vt220_port);
L
Linus Torvalds 已提交
465 466 467 468
	char *buffer;
	unsigned int count;

	/* Ignore input if device is not open */
J
Jiri Slaby 已提交
469
	if (tty == NULL)
L
Linus Torvalds 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482
		return;

	buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
	count = evbuf->length - sizeof(struct evbuf_header);

	switch (*buffer) {
	case SCLP_VT220_SESSION_ENDED:
	case SCLP_VT220_SESSION_STARTED:
		break;
	case SCLP_VT220_SESSION_DATA:
		/* Send input to line discipline */
		buffer++;
		count--;
J
Jiri Slaby 已提交
483 484
		tty_insert_flip_string(tty, buffer, count);
		tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
485 486
		break;
	}
J
Jiri Slaby 已提交
487
	tty_kref_put(tty);
L
Linus Torvalds 已提交
488 489 490 491 492 493 494 495 496
}

/*
 * This routine is called when a particular tty device is opened.
 */
static int
sclp_vt220_open(struct tty_struct *tty, struct file *filp)
{
	if (tty->count == 1) {
J
Jiri Slaby 已提交
497
		tty_port_tty_set(&sclp_vt220_port, tty);
L
Linus Torvalds 已提交
498
		tty->low_latency = 0;
499 500 501 502
		if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
			tty->winsize.ws_row = 24;
			tty->winsize.ws_col = 80;
		}
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511 512
	}
	return 0;
}

/*
 * This routine is called when a particular tty device is closed.
 */
static void
sclp_vt220_close(struct tty_struct *tty, struct file *filp)
{
513
	if (tty->count == 1)
J
Jiri Slaby 已提交
514
		tty_port_tty_set(&sclp_vt220_port, NULL);
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522
}

/*
 * This routine is called by the kernel to write a single
 * character to the tty device.  If the kernel uses this routine,
 * it must call the flush_chars() routine (if defined) when it is
 * done stuffing characters into the driver.
 */
523
static int
L
Linus Torvalds 已提交
524 525
sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
{
526
	return __sclp_vt220_write(&ch, 1, 0, 0, 1);
L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534 535
}

/*
 * This routine is called by the kernel after it has written a
 * series of characters to the tty device using put_char().  
 */
static void
sclp_vt220_flush_chars(struct tty_struct *tty)
{
536
	if (!sclp_vt220_queue_running)
L
Linus Torvalds 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
		sclp_vt220_emit_current();
	else
		sclp_vt220_flush_later = 1;
}

/*
 * This routine returns the numbers of characters the tty driver
 * will accept for queuing to be written.  This number is subject
 * to change as output buffers get emptied, or if the output flow
 * control is acted.
 */
static int
sclp_vt220_write_room(struct tty_struct *tty)
{
	unsigned long flags;
	struct list_head *l;
	int count;

	spin_lock_irqsave(&sclp_vt220_lock, flags);
	count = 0;
	if (sclp_vt220_current_request != NULL)
		count = sclp_vt220_space_left(sclp_vt220_current_request);
	list_for_each(l, &sclp_vt220_empty)
		count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
	return count;
}

/*
 * Return number of buffered chars.
 */
static int
sclp_vt220_chars_in_buffer(struct tty_struct *tty)
{
	unsigned long flags;
	struct list_head *l;
	struct sclp_vt220_request *r;
	int count;

	spin_lock_irqsave(&sclp_vt220_lock, flags);
	count = 0;
	if (sclp_vt220_current_request != NULL)
		count = sclp_vt220_chars_stored(sclp_vt220_current_request);
	list_for_each(l, &sclp_vt220_outqueue) {
		r = list_entry(l, struct sclp_vt220_request, list);
		count += sclp_vt220_chars_stored(r);
	}
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
	return count;
}

/*
 * Pass on all buffers to the hardware. Return only when there are no more
 * buffers pending.
 */
static void
sclp_vt220_flush_buffer(struct tty_struct *tty)
{
	sclp_vt220_emit_current();
}

598 599
/* Release allocated pages. */
static void __init __sclp_vt220_free_pages(void)
600 601 602 603 604
{
	struct list_head *page, *p;

	list_for_each_safe(page, p, &sclp_vt220_empty) {
		list_del(page);
605
		free_page((unsigned long) page);
606 607 608
	}
}

609 610 611 612 613 614 615 616 617 618 619 620 621 622
/* Release memory and unregister from sclp core. Controlled by init counting -
 * only the last invoker will actually perform these actions. */
static void __init __sclp_vt220_cleanup(void)
{
	sclp_vt220_init_count--;
	if (sclp_vt220_init_count != 0)
		return;
	sclp_unregister(&sclp_vt220_register);
	__sclp_vt220_free_pages();
}

/* Allocate buffer pages and register with sclp core. Controlled by init
 * counting - only the first invoker will actually perform these actions. */
static int __init __sclp_vt220_init(int num_pages)
L
Linus Torvalds 已提交
623 624 625
{
	void *page;
	int i;
626
	int rc;
L
Linus Torvalds 已提交
627

628 629
	sclp_vt220_init_count++;
	if (sclp_vt220_init_count != 1)
L
Linus Torvalds 已提交
630 631 632 633 634
		return 0;
	spin_lock_init(&sclp_vt220_lock);
	INIT_LIST_HEAD(&sclp_vt220_empty);
	INIT_LIST_HEAD(&sclp_vt220_outqueue);
	init_timer(&sclp_vt220_timer);
J
Jiri Slaby 已提交
635
	tty_port_init(&sclp_vt220_port);
L
Linus Torvalds 已提交
636 637 638 639 640
	sclp_vt220_current_request = NULL;
	sclp_vt220_buffered_chars = 0;
	sclp_vt220_flush_later = 0;

	/* Allocate pages for output buffering */
641
	rc = -ENOMEM;
642
	for (i = 0; i < num_pages; i++) {
643 644
		page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
		if (!page)
645
			goto out;
646
		list_add_tail(page, &sclp_vt220_empty);
L
Linus Torvalds 已提交
647
	}
648
	rc = sclp_register(&sclp_vt220_register);
649
out:
650
	if (rc) {
651 652
		__sclp_vt220_free_pages();
		sclp_vt220_init_count--;
653 654
	}
	return rc;
L
Linus Torvalds 已提交
655 656
}

J
Jeff Dike 已提交
657
static const struct tty_operations sclp_vt220_ops = {
L
Linus Torvalds 已提交
658 659 660 661 662 663 664
	.open = sclp_vt220_open,
	.close = sclp_vt220_close,
	.write = sclp_vt220_write,
	.put_char = sclp_vt220_put_char,
	.flush_chars = sclp_vt220_flush_chars,
	.write_room = sclp_vt220_write_room,
	.chars_in_buffer = sclp_vt220_chars_in_buffer,
665
	.flush_buffer = sclp_vt220_flush_buffer,
L
Linus Torvalds 已提交
666 667 668 669 670
};

/*
 * Register driver with SCLP and Linux and initialize internal tty structures.
 */
671
static int __init sclp_vt220_tty_init(void)
L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679 680
{
	struct tty_driver *driver;
	int rc;

	/* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
	 * symmetry between VM and LPAR systems regarding ttyS1. */
	driver = alloc_tty_driver(1);
	if (!driver)
		return -ENOMEM;
681
	rc = __sclp_vt220_init(MAX_KMEM_PAGES);
682 683
	if (rc)
		goto out_driver;
L
Linus Torvalds 已提交
684 685 686 687 688 689 690 691 692 693 694 695

	driver->driver_name = SCLP_VT220_DRIVER_NAME;
	driver->name = SCLP_VT220_DEVICE_NAME;
	driver->major = SCLP_VT220_MAJOR;
	driver->minor_start = SCLP_VT220_MINOR;
	driver->type = TTY_DRIVER_TYPE_SYSTEM;
	driver->subtype = SYSTEM_TYPE_TTY;
	driver->init_termios = tty_std_termios;
	driver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(driver, &sclp_vt220_ops);

	rc = tty_register_driver(driver);
696
	if (rc)
697
		goto out_init;
L
Linus Torvalds 已提交
698 699 700
	sclp_vt220_driver = driver;
	return 0;

701
out_init:
702
	__sclp_vt220_cleanup();
703 704 705 706 707
out_driver:
	put_tty_driver(driver);
	return rc;
}
__initcall(sclp_vt220_tty_init);
L
Linus Torvalds 已提交
708

709 710 711 712 713 714 715 716
static void __sclp_vt220_flush_buffer(void)
{
	unsigned long flags;

	sclp_vt220_emit_current();
	spin_lock_irqsave(&sclp_vt220_lock, flags);
	if (timer_pending(&sclp_vt220_timer))
		del_timer(&sclp_vt220_timer);
717
	while (sclp_vt220_queue_running) {
718 719 720 721 722 723 724
		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
		sclp_sync_wait();
		spin_lock_irqsave(&sclp_vt220_lock, flags);
	}
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
}

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
/*
 * Resume console: If there are cached messages, emit them.
 */
static void sclp_vt220_resume(void)
{
	unsigned long flags;

	spin_lock_irqsave(&sclp_vt220_lock, flags);
	sclp_vt220_suspended = 0;
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
	sclp_vt220_emit_current();
}

/*
 * Suspend console: Set suspend flag and flush console
 */
static void sclp_vt220_suspend(void)
{
	unsigned long flags;

	spin_lock_irqsave(&sclp_vt220_lock, flags);
	sclp_vt220_suspended = 1;
	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
	__sclp_vt220_flush_buffer();
}

static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
				   enum sclp_pm_event sclp_pm_event)
{
	switch (sclp_pm_event) {
	case SCLP_PM_EVENT_FREEZE:
		sclp_vt220_suspend();
		break;
	case SCLP_PM_EVENT_RESTORE:
	case SCLP_PM_EVENT_THAW:
		sclp_vt220_resume();
		break;
	}
}

M
Michael Holzheu 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
#ifdef CONFIG_SCLP_VT220_CONSOLE

static void
sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
{
	__sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
}

static struct tty_driver *
sclp_vt220_con_device(struct console *c, int *index)
{
	*index = 0;
	return sclp_vt220_driver;
}

780 781 782
static int
sclp_vt220_notify(struct notifier_block *self,
			  unsigned long event, void *data)
L
Linus Torvalds 已提交
783 784
{
	__sclp_vt220_flush_buffer();
785
	return NOTIFY_OK;
L
Linus Torvalds 已提交
786 787
}

788 789 790 791 792 793 794 795 796 797
static struct notifier_block on_panic_nb = {
	.notifier_call = sclp_vt220_notify,
	.priority = 1,
};

static struct notifier_block on_reboot_nb = {
	.notifier_call = sclp_vt220_notify,
	.priority = 1,
};

L
Linus Torvalds 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
/* Structure needed to register with printk */
static struct console sclp_vt220_console =
{
	.name = SCLP_VT220_CONSOLE_NAME,
	.write = sclp_vt220_con_write,
	.device = sclp_vt220_con_device,
	.flags = CON_PRINTBUFFER,
	.index = SCLP_VT220_CONSOLE_INDEX
};

static int __init
sclp_vt220_con_init(void)
{
	int rc;

	if (!CONSOLE_IS_SCLP)
		return 0;
815
	rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
L
Linus Torvalds 已提交
816 817 818
	if (rc)
		return rc;
	/* Attach linux console */
819 820
	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
	register_reboot_notifier(&on_reboot_nb);
L
Linus Torvalds 已提交
821 822 823 824 825 826 827
	register_console(&sclp_vt220_console);
	return 0;
}

console_initcall(sclp_vt220_con_init);
#endif /* CONFIG_SCLP_VT220_CONSOLE */