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