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