sclp_vt220.c revision 62b7494209495847269a6ce0504cbefd23d42eb1
1/*
2 * SCLP VT220 terminal driver.
3 *
4 * Copyright IBM Corp. 2003, 2009
5 *
6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
7 */
8
9#include <linux/module.h>
10#include <linux/spinlock.h>
11#include <linux/list.h>
12#include <linux/wait.h>
13#include <linux/timer.h>
14#include <linux/kernel.h>
15#include <linux/tty.h>
16#include <linux/tty_driver.h>
17#include <linux/tty_flip.h>
18#include <linux/errno.h>
19#include <linux/mm.h>
20#include <linux/major.h>
21#include <linux/console.h>
22#include <linux/kdev_t.h>
23#include <linux/bootmem.h>
24#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/reboot.h>
27
28#include <asm/uaccess.h>
29#include "sclp.h"
30
31#define SCLP_VT220_MAJOR		TTY_MAJOR
32#define SCLP_VT220_MINOR		65
33#define SCLP_VT220_DRIVER_NAME		"sclp_vt220"
34#define SCLP_VT220_DEVICE_NAME		"ttysclp"
35#define SCLP_VT220_CONSOLE_NAME		"ttyS"
36#define SCLP_VT220_CONSOLE_INDEX	1	/* console=ttyS1 */
37#define SCLP_VT220_BUF_SIZE		80
38
39/* Representation of a single write request */
40struct sclp_vt220_request {
41	struct list_head list;
42	struct sclp_req sclp_req;
43	int retry_count;
44};
45
46/* VT220 SCCB */
47struct sclp_vt220_sccb {
48	struct sccb_header header;
49	struct evbuf_header evbuf;
50};
51
52#define SCLP_VT220_MAX_CHARS_PER_BUFFER	(PAGE_SIZE - \
53					 sizeof(struct sclp_vt220_request) - \
54					 sizeof(struct sclp_vt220_sccb))
55
56/* Structures and data needed to register tty driver */
57static struct tty_driver *sclp_vt220_driver;
58
59/* The tty_struct that the kernel associated with us */
60static struct tty_struct *sclp_vt220_tty;
61
62/* Lock to protect internal data from concurrent access */
63static spinlock_t sclp_vt220_lock;
64
65/* List of empty pages to be used as write request buffers */
66static struct list_head sclp_vt220_empty;
67
68/* List of pending requests */
69static struct list_head sclp_vt220_outqueue;
70
71/* Suspend mode flag */
72static int sclp_vt220_suspended;
73
74/* Flag that output queue is currently running */
75static int sclp_vt220_queue_running;
76
77/* Timer used for delaying write requests to merge subsequent messages into
78 * a single buffer */
79static struct timer_list sclp_vt220_timer;
80
81/* Pointer to current request buffer which has been partially filled but not
82 * yet sent */
83static struct sclp_vt220_request *sclp_vt220_current_request;
84
85/* Number of characters in current request buffer */
86static int sclp_vt220_buffered_chars;
87
88/* Counter controlling core driver initialization. */
89static int __initdata sclp_vt220_init_count;
90
91/* Flag indicating that sclp_vt220_current_request should really
92 * have been already queued but wasn't because the SCLP was processing
93 * another buffer */
94static int sclp_vt220_flush_later;
95
96static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
97static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
98				   enum sclp_pm_event sclp_pm_event);
99static int __sclp_vt220_emit(struct sclp_vt220_request *request);
100static void sclp_vt220_emit_current(void);
101
102/* Registration structure for our interest in SCLP event buffers */
103static struct sclp_register sclp_vt220_register = {
104	.send_mask		= EVTYP_VT220MSG_MASK,
105	.receive_mask		= EVTYP_VT220MSG_MASK,
106	.state_change_fn	= NULL,
107	.receiver_fn		= sclp_vt220_receiver_fn,
108	.pm_event_fn		= sclp_vt220_pm_event_fn,
109};
110
111
112/*
113 * Put provided request buffer back into queue and check emit pending
114 * buffers if necessary.
115 */
116static void
117sclp_vt220_process_queue(struct sclp_vt220_request *request)
118{
119	unsigned long flags;
120	void *page;
121
122	do {
123		/* Put buffer back to list of empty buffers */
124		page = request->sclp_req.sccb;
125		spin_lock_irqsave(&sclp_vt220_lock, flags);
126		/* Move request from outqueue to empty queue */
127		list_del(&request->list);
128		list_add_tail((struct list_head *) page, &sclp_vt220_empty);
129		/* Check if there is a pending buffer on the out queue. */
130		request = NULL;
131		if (!list_empty(&sclp_vt220_outqueue))
132			request = list_entry(sclp_vt220_outqueue.next,
133					     struct sclp_vt220_request, list);
134		if (!request || sclp_vt220_suspended) {
135			sclp_vt220_queue_running = 0;
136			spin_unlock_irqrestore(&sclp_vt220_lock, flags);
137			break;
138		}
139		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
140	} while (__sclp_vt220_emit(request));
141	if (request == NULL && sclp_vt220_flush_later)
142		sclp_vt220_emit_current();
143	/* Check if the tty needs a wake up call */
144	if (sclp_vt220_tty != NULL) {
145		tty_wakeup(sclp_vt220_tty);
146	}
147}
148
149#define SCLP_BUFFER_MAX_RETRY		1
150
151/*
152 * Callback through which the result of a write request is reported by the
153 * SCLP.
154 */
155static void
156sclp_vt220_callback(struct sclp_req *request, void *data)
157{
158	struct sclp_vt220_request *vt220_request;
159	struct sclp_vt220_sccb *sccb;
160
161	vt220_request = (struct sclp_vt220_request *) data;
162	if (request->status == SCLP_REQ_FAILED) {
163		sclp_vt220_process_queue(vt220_request);
164		return;
165	}
166	sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
167
168	/* Check SCLP response code and choose suitable action	*/
169	switch (sccb->header.response_code) {
170	case 0x0020 :
171		break;
172
173	case 0x05f0: /* Target resource in improper state */
174		break;
175
176	case 0x0340: /* Contained SCLP equipment check */
177		if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
178			break;
179		/* Remove processed buffers and requeue rest */
180		if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
181			/* Not all buffers were processed */
182			sccb->header.response_code = 0x0000;
183			vt220_request->sclp_req.status = SCLP_REQ_FILLED;
184			if (sclp_add_request(request) == 0)
185				return;
186		}
187		break;
188
189	case 0x0040: /* SCLP equipment check */
190		if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
191			break;
192		sccb->header.response_code = 0x0000;
193		vt220_request->sclp_req.status = SCLP_REQ_FILLED;
194		if (sclp_add_request(request) == 0)
195			return;
196		break;
197
198	default:
199		break;
200	}
201	sclp_vt220_process_queue(vt220_request);
202}
203
204/*
205 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
206 * otherwise.
207 */
208static int
209__sclp_vt220_emit(struct sclp_vt220_request *request)
210{
211	if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
212		request->sclp_req.status = SCLP_REQ_FAILED;
213		return -EIO;
214	}
215	request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
216	request->sclp_req.status = SCLP_REQ_FILLED;
217	request->sclp_req.callback = sclp_vt220_callback;
218	request->sclp_req.callback_data = (void *) request;
219
220	return sclp_add_request(&request->sclp_req);
221}
222
223/*
224 * Queue and emit current request.
225 */
226static void
227sclp_vt220_emit_current(void)
228{
229	unsigned long flags;
230	struct sclp_vt220_request *request;
231	struct sclp_vt220_sccb *sccb;
232
233	spin_lock_irqsave(&sclp_vt220_lock, flags);
234	if (sclp_vt220_current_request) {
235		sccb = (struct sclp_vt220_sccb *)
236				sclp_vt220_current_request->sclp_req.sccb;
237		/* Only emit buffers with content */
238		if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
239			list_add_tail(&sclp_vt220_current_request->list,
240				      &sclp_vt220_outqueue);
241			sclp_vt220_current_request = NULL;
242			if (timer_pending(&sclp_vt220_timer))
243				del_timer(&sclp_vt220_timer);
244		}
245		sclp_vt220_flush_later = 0;
246	}
247	if (sclp_vt220_queue_running || sclp_vt220_suspended)
248		goto out_unlock;
249	if (list_empty(&sclp_vt220_outqueue))
250		goto out_unlock;
251	request = list_first_entry(&sclp_vt220_outqueue,
252				   struct sclp_vt220_request, list);
253	sclp_vt220_queue_running = 1;
254	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
255
256	if (__sclp_vt220_emit(request))
257		sclp_vt220_process_queue(request);
258	return;
259out_unlock:
260	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
261}
262
263#define SCLP_NORMAL_WRITE	0x00
264
265/*
266 * Helper function to initialize a page with the sclp request structure.
267 */
268static struct sclp_vt220_request *
269sclp_vt220_initialize_page(void *page)
270{
271	struct sclp_vt220_request *request;
272	struct sclp_vt220_sccb *sccb;
273
274	/* Place request structure at end of page */
275	request = ((struct sclp_vt220_request *)
276			((addr_t) page + PAGE_SIZE)) - 1;
277	request->retry_count = 0;
278	request->sclp_req.sccb = page;
279	/* SCCB goes at start of page */
280	sccb = (struct sclp_vt220_sccb *) page;
281	memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
282	sccb->header.length = sizeof(struct sclp_vt220_sccb);
283	sccb->header.function_code = SCLP_NORMAL_WRITE;
284	sccb->header.response_code = 0x0000;
285	sccb->evbuf.type = EVTYP_VT220MSG;
286	sccb->evbuf.length = sizeof(struct evbuf_header);
287
288	return request;
289}
290
291static inline unsigned int
292sclp_vt220_space_left(struct sclp_vt220_request *request)
293{
294	struct sclp_vt220_sccb *sccb;
295	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
296	return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
297	       sccb->header.length;
298}
299
300static inline unsigned int
301sclp_vt220_chars_stored(struct sclp_vt220_request *request)
302{
303	struct sclp_vt220_sccb *sccb;
304	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
305	return sccb->evbuf.length - sizeof(struct evbuf_header);
306}
307
308/*
309 * Add msg to buffer associated with request. Return the number of characters
310 * added.
311 */
312static int
313sclp_vt220_add_msg(struct sclp_vt220_request *request,
314		   const unsigned char *msg, int count, int convertlf)
315{
316	struct sclp_vt220_sccb *sccb;
317	void *buffer;
318	unsigned char c;
319	int from;
320	int to;
321
322	if (count > sclp_vt220_space_left(request))
323		count = sclp_vt220_space_left(request);
324	if (count <= 0)
325		return 0;
326
327	sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
328	buffer = (void *) ((addr_t) sccb + sccb->header.length);
329
330	if (convertlf) {
331		/* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
332		for (from=0, to=0;
333		     (from < count) && (to < sclp_vt220_space_left(request));
334		     from++) {
335			/* Retrieve character */
336			c = msg[from];
337			/* Perform conversion */
338			if (c == 0x0a) {
339				if (to + 1 < sclp_vt220_space_left(request)) {
340					((unsigned char *) buffer)[to++] = c;
341					((unsigned char *) buffer)[to++] = 0x0d;
342				} else
343					break;
344
345			} else
346				((unsigned char *) buffer)[to++] = c;
347		}
348		sccb->header.length += to;
349		sccb->evbuf.length += to;
350		return from;
351	} else {
352		memcpy(buffer, (const void *) msg, count);
353		sccb->header.length += count;
354		sccb->evbuf.length += count;
355		return count;
356	}
357}
358
359/*
360 * Emit buffer after having waited long enough for more data to arrive.
361 */
362static void
363sclp_vt220_timeout(unsigned long data)
364{
365	sclp_vt220_emit_current();
366}
367
368#define BUFFER_MAX_DELAY	HZ/20
369
370/*
371 * Internal implementation of the write function. Write COUNT bytes of data
372 * from memory at BUF
373 * to the SCLP interface. In case that the data does not fit into the current
374 * write buffer, emit the current one and allocate a new one. If there are no
375 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
376 * is non-zero, the buffer will be scheduled for emitting after a timeout -
377 * otherwise the user has to explicitly call the flush function.
378 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
379 * buffer should be converted to 0x0a 0x0d. After completion, return the number
380 * of bytes written.
381 */
382static int
383__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
384		   int convertlf, int may_fail)
385{
386	unsigned long flags;
387	void *page;
388	int written;
389	int overall_written;
390
391	if (count <= 0)
392		return 0;
393	overall_written = 0;
394	spin_lock_irqsave(&sclp_vt220_lock, flags);
395	do {
396		/* Create an sclp output buffer if none exists yet */
397		if (sclp_vt220_current_request == NULL) {
398			while (list_empty(&sclp_vt220_empty)) {
399				spin_unlock_irqrestore(&sclp_vt220_lock, flags);
400				if (may_fail || sclp_vt220_suspended)
401					goto out;
402				else
403					sclp_sync_wait();
404				spin_lock_irqsave(&sclp_vt220_lock, flags);
405			}
406			page = (void *) sclp_vt220_empty.next;
407			list_del((struct list_head *) page);
408			sclp_vt220_current_request =
409				sclp_vt220_initialize_page(page);
410		}
411		/* Try to write the string to the current request buffer */
412		written = sclp_vt220_add_msg(sclp_vt220_current_request,
413					     buf, count, convertlf);
414		overall_written += written;
415		if (written == count)
416			break;
417		/*
418		 * Not all characters could be written to the current
419		 * output buffer. Emit the buffer, create a new buffer
420		 * and then output the rest of the string.
421		 */
422		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
423		sclp_vt220_emit_current();
424		spin_lock_irqsave(&sclp_vt220_lock, flags);
425		buf += written;
426		count -= written;
427	} while (count > 0);
428	/* Setup timer to output current console buffer after some time */
429	if (sclp_vt220_current_request != NULL &&
430	    !timer_pending(&sclp_vt220_timer) && do_schedule) {
431		sclp_vt220_timer.function = sclp_vt220_timeout;
432		sclp_vt220_timer.data = 0UL;
433		sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
434		add_timer(&sclp_vt220_timer);
435	}
436	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
437out:
438	return overall_written;
439}
440
441/*
442 * This routine is called by the kernel to write a series of
443 * characters to the tty device.  The characters may come from
444 * user space or kernel space.  This routine will return the
445 * number of characters actually accepted for writing.
446 */
447static int
448sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
449{
450	return __sclp_vt220_write(buf, count, 1, 0, 1);
451}
452
453#define SCLP_VT220_SESSION_ENDED	0x01
454#define	SCLP_VT220_SESSION_STARTED	0x80
455#define SCLP_VT220_SESSION_DATA		0x00
456
457/*
458 * Called by the SCLP to report incoming event buffers.
459 */
460static void
461sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
462{
463	char *buffer;
464	unsigned int count;
465
466	/* Ignore input if device is not open */
467	if (sclp_vt220_tty == NULL)
468		return;
469
470	buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
471	count = evbuf->length - sizeof(struct evbuf_header);
472
473	switch (*buffer) {
474	case SCLP_VT220_SESSION_ENDED:
475	case SCLP_VT220_SESSION_STARTED:
476		break;
477	case SCLP_VT220_SESSION_DATA:
478		/* Send input to line discipline */
479		buffer++;
480		count--;
481		tty_insert_flip_string(sclp_vt220_tty, buffer, count);
482		tty_flip_buffer_push(sclp_vt220_tty);
483		break;
484	}
485}
486
487/*
488 * This routine is called when a particular tty device is opened.
489 */
490static int
491sclp_vt220_open(struct tty_struct *tty, struct file *filp)
492{
493	if (tty->count == 1) {
494		sclp_vt220_tty = tty;
495		tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
496		if (tty->driver_data == NULL)
497			return -ENOMEM;
498		tty->low_latency = 0;
499	}
500	return 0;
501}
502
503/*
504 * This routine is called when a particular tty device is closed.
505 */
506static void
507sclp_vt220_close(struct tty_struct *tty, struct file *filp)
508{
509	if (tty->count == 1) {
510		sclp_vt220_tty = NULL;
511		kfree(tty->driver_data);
512		tty->driver_data = NULL;
513	}
514}
515
516/*
517 * This routine is called by the kernel to write a single
518 * character to the tty device.  If the kernel uses this routine,
519 * it must call the flush_chars() routine (if defined) when it is
520 * done stuffing characters into the driver.
521 */
522static int
523sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
524{
525	return __sclp_vt220_write(&ch, 1, 0, 0, 1);
526}
527
528/*
529 * This routine is called by the kernel after it has written a
530 * series of characters to the tty device using put_char().
531 */
532static void
533sclp_vt220_flush_chars(struct tty_struct *tty)
534{
535	if (!sclp_vt220_queue_running)
536		sclp_vt220_emit_current();
537	else
538		sclp_vt220_flush_later = 1;
539}
540
541/*
542 * This routine returns the numbers of characters the tty driver
543 * will accept for queuing to be written.  This number is subject
544 * to change as output buffers get emptied, or if the output flow
545 * control is acted.
546 */
547static int
548sclp_vt220_write_room(struct tty_struct *tty)
549{
550	unsigned long flags;
551	struct list_head *l;
552	int count;
553
554	spin_lock_irqsave(&sclp_vt220_lock, flags);
555	count = 0;
556	if (sclp_vt220_current_request != NULL)
557		count = sclp_vt220_space_left(sclp_vt220_current_request);
558	list_for_each(l, &sclp_vt220_empty)
559		count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
560	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
561	return count;
562}
563
564/*
565 * Return number of buffered chars.
566 */
567static int
568sclp_vt220_chars_in_buffer(struct tty_struct *tty)
569{
570	unsigned long flags;
571	struct list_head *l;
572	struct sclp_vt220_request *r;
573	int count;
574
575	spin_lock_irqsave(&sclp_vt220_lock, flags);
576	count = 0;
577	if (sclp_vt220_current_request != NULL)
578		count = sclp_vt220_chars_stored(sclp_vt220_current_request);
579	list_for_each(l, &sclp_vt220_outqueue) {
580		r = list_entry(l, struct sclp_vt220_request, list);
581		count += sclp_vt220_chars_stored(r);
582	}
583	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
584	return count;
585}
586
587/*
588 * Pass on all buffers to the hardware. Return only when there are no more
589 * buffers pending.
590 */
591static void
592sclp_vt220_flush_buffer(struct tty_struct *tty)
593{
594	sclp_vt220_emit_current();
595}
596
597/* Release allocated pages. */
598static void __init __sclp_vt220_free_pages(void)
599{
600	struct list_head *page, *p;
601
602	list_for_each_safe(page, p, &sclp_vt220_empty) {
603		list_del(page);
604		if (slab_is_available())
605			free_page((unsigned long) page);
606		else
607			free_bootmem((unsigned long) page, PAGE_SIZE);
608	}
609}
610
611/* Release memory and unregister from sclp core. Controlled by init counting -
612 * only the last invoker will actually perform these actions. */
613static void __init __sclp_vt220_cleanup(void)
614{
615	sclp_vt220_init_count--;
616	if (sclp_vt220_init_count != 0)
617		return;
618	sclp_unregister(&sclp_vt220_register);
619	__sclp_vt220_free_pages();
620}
621
622/* Allocate buffer pages and register with sclp core. Controlled by init
623 * counting - only the first invoker will actually perform these actions. */
624static int __init __sclp_vt220_init(int num_pages)
625{
626	void *page;
627	int i;
628	int rc;
629
630	sclp_vt220_init_count++;
631	if (sclp_vt220_init_count != 1)
632		return 0;
633	spin_lock_init(&sclp_vt220_lock);
634	INIT_LIST_HEAD(&sclp_vt220_empty);
635	INIT_LIST_HEAD(&sclp_vt220_outqueue);
636	init_timer(&sclp_vt220_timer);
637	sclp_vt220_current_request = NULL;
638	sclp_vt220_buffered_chars = 0;
639	sclp_vt220_tty = NULL;
640	sclp_vt220_flush_later = 0;
641
642	/* Allocate pages for output buffering */
643	for (i = 0; i < num_pages; i++) {
644		if (slab_is_available())
645			page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
646		else
647			page = alloc_bootmem_low_pages(PAGE_SIZE);
648		if (!page) {
649			rc = -ENOMEM;
650			goto out;
651		}
652		list_add_tail((struct list_head *) page, &sclp_vt220_empty);
653	}
654	rc = sclp_register(&sclp_vt220_register);
655out:
656	if (rc) {
657		__sclp_vt220_free_pages();
658		sclp_vt220_init_count--;
659	}
660	return rc;
661}
662
663static const struct tty_operations sclp_vt220_ops = {
664	.open = sclp_vt220_open,
665	.close = sclp_vt220_close,
666	.write = sclp_vt220_write,
667	.put_char = sclp_vt220_put_char,
668	.flush_chars = sclp_vt220_flush_chars,
669	.write_room = sclp_vt220_write_room,
670	.chars_in_buffer = sclp_vt220_chars_in_buffer,
671	.flush_buffer = sclp_vt220_flush_buffer,
672};
673
674/*
675 * Register driver with SCLP and Linux and initialize internal tty structures.
676 */
677static int __init sclp_vt220_tty_init(void)
678{
679	struct tty_driver *driver;
680	int rc;
681
682	/* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
683	 * symmetry between VM and LPAR systems regarding ttyS1. */
684	driver = alloc_tty_driver(1);
685	if (!driver)
686		return -ENOMEM;
687	rc = __sclp_vt220_init(MAX_KMEM_PAGES);
688	if (rc)
689		goto out_driver;
690
691	driver->owner = THIS_MODULE;
692	driver->driver_name = SCLP_VT220_DRIVER_NAME;
693	driver->name = SCLP_VT220_DEVICE_NAME;
694	driver->major = SCLP_VT220_MAJOR;
695	driver->minor_start = SCLP_VT220_MINOR;
696	driver->type = TTY_DRIVER_TYPE_SYSTEM;
697	driver->subtype = SYSTEM_TYPE_TTY;
698	driver->init_termios = tty_std_termios;
699	driver->flags = TTY_DRIVER_REAL_RAW;
700	tty_set_operations(driver, &sclp_vt220_ops);
701
702	rc = tty_register_driver(driver);
703	if (rc)
704		goto out_init;
705	sclp_vt220_driver = driver;
706	return 0;
707
708out_init:
709	__sclp_vt220_cleanup();
710out_driver:
711	put_tty_driver(driver);
712	return rc;
713}
714__initcall(sclp_vt220_tty_init);
715
716#ifdef CONFIG_SCLP_VT220_CONSOLE
717
718static void
719sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
720{
721	__sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
722}
723
724static struct tty_driver *
725sclp_vt220_con_device(struct console *c, int *index)
726{
727	*index = 0;
728	return sclp_vt220_driver;
729}
730
731static void __sclp_vt220_flush_buffer(void)
732{
733	unsigned long flags;
734
735	sclp_vt220_emit_current();
736	spin_lock_irqsave(&sclp_vt220_lock, flags);
737	if (timer_pending(&sclp_vt220_timer))
738		del_timer(&sclp_vt220_timer);
739	while (sclp_vt220_queue_running) {
740		spin_unlock_irqrestore(&sclp_vt220_lock, flags);
741		sclp_sync_wait();
742		spin_lock_irqsave(&sclp_vt220_lock, flags);
743	}
744	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
745}
746
747/*
748 * Resume console: If there are cached messages, emit them.
749 */
750static void sclp_vt220_resume(void)
751{
752	unsigned long flags;
753
754	spin_lock_irqsave(&sclp_vt220_lock, flags);
755	sclp_vt220_suspended = 0;
756	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
757	sclp_vt220_emit_current();
758}
759
760/*
761 * Suspend console: Set suspend flag and flush console
762 */
763static void sclp_vt220_suspend(void)
764{
765	unsigned long flags;
766
767	spin_lock_irqsave(&sclp_vt220_lock, flags);
768	sclp_vt220_suspended = 1;
769	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
770	__sclp_vt220_flush_buffer();
771}
772
773static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
774				   enum sclp_pm_event sclp_pm_event)
775{
776	switch (sclp_pm_event) {
777	case SCLP_PM_EVENT_FREEZE:
778		sclp_vt220_suspend();
779		break;
780	case SCLP_PM_EVENT_RESTORE:
781	case SCLP_PM_EVENT_THAW:
782		sclp_vt220_resume();
783		break;
784	}
785}
786
787static int
788sclp_vt220_notify(struct notifier_block *self,
789			  unsigned long event, void *data)
790{
791	__sclp_vt220_flush_buffer();
792	return NOTIFY_OK;
793}
794
795static struct notifier_block on_panic_nb = {
796	.notifier_call = sclp_vt220_notify,
797	.priority = 1,
798};
799
800static struct notifier_block on_reboot_nb = {
801	.notifier_call = sclp_vt220_notify,
802	.priority = 1,
803};
804
805/* Structure needed to register with printk */
806static struct console sclp_vt220_console =
807{
808	.name = SCLP_VT220_CONSOLE_NAME,
809	.write = sclp_vt220_con_write,
810	.device = sclp_vt220_con_device,
811	.flags = CON_PRINTBUFFER,
812	.index = SCLP_VT220_CONSOLE_INDEX
813};
814
815static int __init
816sclp_vt220_con_init(void)
817{
818	int rc;
819
820	if (!CONSOLE_IS_SCLP)
821		return 0;
822	rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
823	if (rc)
824		return rc;
825	/* Attach linux console */
826	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
827	register_reboot_notifier(&on_reboot_nb);
828	register_console(&sclp_vt220_console);
829	return 0;
830}
831
832console_initcall(sclp_vt220_con_init);
833#endif /* CONFIG_SCLP_VT220_CONSOLE */
834
835