blob: e66a75b3822cbf24f09c0824c87b7f0c81089749 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/sclp_tty.c
3 * SCLP line mode terminal driver.
4 *
5 * S390 version
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/kmod.h>
13#include <linux/tty.h>
14#include <linux/tty_driver.h>
Alan Cox33f0f882006-01-09 20:54:13 -080015#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21
22#include "ctrlchar.h"
23#include "sclp.h"
24#include "sclp_rw.h"
25#include "sclp_tty.h"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
28 * size of a buffer that collects single characters coming in
29 * via sclp_tty_put_char()
30 */
31#define SCLP_TTY_BUF_SIZE 512
32
33/*
34 * There is exactly one SCLP terminal, so we can keep things simple
35 * and allocate all variables statically.
36 */
37
38/* Lock to guard over changes to global variables. */
39static spinlock_t sclp_tty_lock;
40/* List of free pages that can be used for console output buffering. */
41static struct list_head sclp_tty_pages;
42/* List of full struct sclp_buffer structures ready for output. */
43static struct list_head sclp_tty_outqueue;
44/* Counter how many buffers are emitted. */
45static int sclp_tty_buffer_count;
46/* Pointer to current console buffer. */
47static struct sclp_buffer *sclp_ttybuf;
48/* Timer for delayed output of console messages. */
49static struct timer_list sclp_tty_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jiri Slaby0ea63da2012-04-02 13:54:12 +020051static struct tty_port sclp_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
53static unsigned short int sclp_tty_chars_count;
54
55struct tty_driver *sclp_tty_driver;
56
Heiko Carstens095761d2008-07-14 09:59:45 +020057static int sclp_tty_tolower;
58static int sclp_tty_columns = 80;
59
60#define SPACES_PER_TAB 8
61#define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* This routine is called whenever we try to open a SCLP terminal. */
64static int
65sclp_tty_open(struct tty_struct *tty, struct file *filp)
66{
Jiri Slaby0ea63da2012-04-02 13:54:12 +020067 tty_port_tty_set(&sclp_port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 tty->driver_data = NULL;
69 tty->low_latency = 0;
70 return 0;
71}
72
73/* This routine is called when the SCLP terminal is closed. */
74static void
75sclp_tty_close(struct tty_struct *tty, struct file *filp)
76{
77 if (tty->count > 1)
78 return;
Jiri Slaby0ea63da2012-04-02 13:54:12 +020079 tty_port_tty_set(&sclp_port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * This routine returns the numbers of characters the tty driver
84 * will accept for queuing to be written. This number is subject
85 * to change as output buffers get emptied, or if the output flow
86 * control is acted. This is not an exact number because not every
87 * character needs the same space in the sccb. The worst case is
88 * a string of newlines. Every newlines creates a new mto which
89 * needs 8 bytes.
90 */
91static int
92sclp_tty_write_room (struct tty_struct *tty)
93{
94 unsigned long flags;
95 struct list_head *l;
96 int count;
97
98 spin_lock_irqsave(&sclp_tty_lock, flags);
99 count = 0;
100 if (sclp_ttybuf != NULL)
101 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
102 list_for_each(l, &sclp_tty_pages)
103 count += NR_EMPTY_MTO_PER_SCCB;
104 spin_unlock_irqrestore(&sclp_tty_lock, flags);
105 return count;
106}
107
108static void
109sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
110{
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200111 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned long flags;
113 void *page;
114
115 do {
116 page = sclp_unmake_buffer(buffer);
117 spin_lock_irqsave(&sclp_tty_lock, flags);
118 /* Remove buffer from outqueue */
119 list_del(&buffer->list);
120 sclp_tty_buffer_count--;
121 list_add_tail((struct list_head *) page, &sclp_tty_pages);
122 /* Check if there is a pending buffer on the out queue. */
123 buffer = NULL;
124 if (!list_empty(&sclp_tty_outqueue))
125 buffer = list_entry(sclp_tty_outqueue.next,
126 struct sclp_buffer, list);
127 spin_unlock_irqrestore(&sclp_tty_lock, flags);
128 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* check if the tty needs a wake up call */
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200130 tty = tty_port_tty_get(&sclp_port);
131 if (tty != NULL) {
132 tty_wakeup(tty);
133 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135}
136
137static inline void
138__sclp_ttybuf_emit(struct sclp_buffer *buffer)
139{
140 unsigned long flags;
141 int count;
142 int rc;
143
144 spin_lock_irqsave(&sclp_tty_lock, flags);
145 list_add_tail(&buffer->list, &sclp_tty_outqueue);
146 count = sclp_tty_buffer_count++;
147 spin_unlock_irqrestore(&sclp_tty_lock, flags);
148 if (count)
149 return;
150 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
151 if (rc)
152 sclp_ttybuf_callback(buffer, rc);
153}
154
155/*
156 * When this routine is called from the timer then we flush the
157 * temporary write buffer.
158 */
159static void
160sclp_tty_timeout(unsigned long data)
161{
162 unsigned long flags;
163 struct sclp_buffer *buf;
164
165 spin_lock_irqsave(&sclp_tty_lock, flags);
166 buf = sclp_ttybuf;
167 sclp_ttybuf = NULL;
168 spin_unlock_irqrestore(&sclp_tty_lock, flags);
169
170 if (buf != NULL) {
171 __sclp_ttybuf_emit(buf);
172 }
173}
174
175/*
176 * Write a string to the sclp tty.
177 */
Heiko Carstens5e345992008-07-14 09:59:46 +0200178static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned long flags;
181 void *page;
182 int written;
Heiko Carstens5e345992008-07-14 09:59:46 +0200183 int overall_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct sclp_buffer *buf;
185
186 if (count <= 0)
Heiko Carstens5e345992008-07-14 09:59:46 +0200187 return 0;
188 overall_written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 spin_lock_irqsave(&sclp_tty_lock, flags);
190 do {
191 /* Create a sclp output buffer if none exists yet */
192 if (sclp_ttybuf == NULL) {
193 while (list_empty(&sclp_tty_pages)) {
194 spin_unlock_irqrestore(&sclp_tty_lock, flags);
Heiko Carstens5e345992008-07-14 09:59:46 +0200195 if (may_fail)
196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 else
Heiko Carstens5e345992008-07-14 09:59:46 +0200198 sclp_sync_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_lock_irqsave(&sclp_tty_lock, flags);
200 }
201 page = sclp_tty_pages.next;
202 list_del((struct list_head *) page);
Heiko Carstens095761d2008-07-14 09:59:45 +0200203 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
204 SPACES_PER_TAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 /* try to write the string to the current output buffer */
207 written = sclp_write(sclp_ttybuf, str, count);
Heiko Carstens5e345992008-07-14 09:59:46 +0200208 overall_written += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (written == count)
210 break;
211 /*
212 * Not all characters could be written to the current
213 * output buffer. Emit the buffer, create a new buffer
214 * and then output the rest of the string.
215 */
216 buf = sclp_ttybuf;
217 sclp_ttybuf = NULL;
218 spin_unlock_irqrestore(&sclp_tty_lock, flags);
219 __sclp_ttybuf_emit(buf);
220 spin_lock_irqsave(&sclp_tty_lock, flags);
221 str += written;
222 count -= written;
223 } while (count > 0);
224 /* Setup timer to output current console buffer after 1/10 second */
Heiko Carstens095761d2008-07-14 09:59:45 +0200225 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
226 !timer_pending(&sclp_tty_timer)) {
227 init_timer(&sclp_tty_timer);
228 sclp_tty_timer.function = sclp_tty_timeout;
229 sclp_tty_timer.data = 0UL;
230 sclp_tty_timer.expires = jiffies + HZ/10;
231 add_timer(&sclp_tty_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 spin_unlock_irqrestore(&sclp_tty_lock, flags);
Heiko Carstens5e345992008-07-14 09:59:46 +0200234out:
235 return overall_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/*
239 * This routine is called by the kernel to write a series of characters to the
240 * tty device. The characters may come from user space or kernel space. This
241 * routine will return the number of characters actually accepted for writing.
242 */
243static int
244sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
245{
246 if (sclp_tty_chars_count > 0) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200247 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sclp_tty_chars_count = 0;
249 }
Heiko Carstens5e345992008-07-14 09:59:46 +0200250 return sclp_tty_write_string(buf, count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * This routine is called by the kernel to write a single character to the tty
255 * device. If the kernel uses this routine, it must call the flush_chars()
256 * routine (if defined) when it is done stuffing characters into the driver.
257 *
258 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
259 * If the given character is a '\n' the contents of the SCLP write buffer
260 * - including previous characters from sclp_tty_put_char() and strings from
261 * sclp_write() without final '\n' - will be written.
262 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700263static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
265{
266 sclp_tty_chars[sclp_tty_chars_count++] = ch;
267 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200268 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 sclp_tty_chars_count = 0;
Heiko Carstens5e345992008-07-14 09:59:46 +0200270 }
271 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/*
275 * This routine is called by the kernel after it has written a series of
276 * characters to the tty device using put_char().
277 */
278static void
279sclp_tty_flush_chars(struct tty_struct *tty)
280{
281 if (sclp_tty_chars_count > 0) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200282 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sclp_tty_chars_count = 0;
284 }
285}
286
287/*
288 * This routine returns the number of characters in the write buffer of the
289 * SCLP driver. The provided number includes all characters that are stored
290 * in the SCCB (will be written next time the SCLP is not busy) as well as
291 * characters in the write buffer (will not be written as long as there is a
292 * final line feed missing).
293 */
294static int
295sclp_tty_chars_in_buffer(struct tty_struct *tty)
296{
297 unsigned long flags;
298 struct list_head *l;
299 struct sclp_buffer *t;
300 int count;
301
302 spin_lock_irqsave(&sclp_tty_lock, flags);
303 count = 0;
304 if (sclp_ttybuf != NULL)
305 count = sclp_chars_in_buffer(sclp_ttybuf);
306 list_for_each(l, &sclp_tty_outqueue) {
307 t = list_entry(l, struct sclp_buffer, list);
308 count += sclp_chars_in_buffer(t);
309 }
310 spin_unlock_irqrestore(&sclp_tty_lock, flags);
311 return count;
312}
313
314/*
315 * removes all content from buffers of low level driver
316 */
317static void
318sclp_tty_flush_buffer(struct tty_struct *tty)
319{
320 if (sclp_tty_chars_count > 0) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200321 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sclp_tty_chars_count = 0;
323 }
324}
325
326/*
327 * push input to tty
328 */
329static void
330sclp_tty_input(unsigned char* buf, unsigned int count)
331{
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200332 struct tty_struct *tty = tty_port_tty_get(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned int cchar;
334
335 /*
336 * If this tty driver is currently closed
337 * then throw the received input away.
338 */
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200339 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return;
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200341 cchar = ctrlchar_handle(buf, count, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 switch (cchar & CTRLCHAR_MASK) {
343 case CTRLCHAR_SYSRQ:
344 break;
345 case CTRLCHAR_CTRL:
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200346 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
347 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 case CTRLCHAR_NONE:
350 /* send (normal) input to line discipline */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800352 (strncmp((const char *) buf + count - 2, "^n", 2) &&
353 strncmp((const char *) buf + count - 2, "\252n", 2))) {
354 /* add the auto \n */
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200355 tty_insert_flip_string(tty, buf, count);
356 tty_insert_flip_char(tty, '\n', TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 } else
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200358 tty_insert_flip_string(tty, buf, count - 2);
359 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200362 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/*
366 * get a EBCDIC string in upper/lower case,
367 * find out characters in lower/upper case separated by a special character,
368 * modifiy original string,
369 * returns length of resulting string
370 */
Heiko Carstens095761d2008-07-14 09:59:45 +0200371static int sclp_switch_cases(unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 unsigned char *ip, *op;
374 int toggle;
375
376 /* initially changing case is off */
377 toggle = 0;
378 ip = op = buf;
379 while (count-- > 0) {
380 /* compare with special character */
Heiko Carstens095761d2008-07-14 09:59:45 +0200381 if (*ip == CASE_DELIMITER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* followed by another special character? */
Heiko Carstens095761d2008-07-14 09:59:45 +0200383 if (count && ip[1] == CASE_DELIMITER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
385 * ... then put a single copy of the special
386 * character to the output string
387 */
388 *op++ = *ip++;
389 count--;
390 } else
391 /*
392 * ... special character follower by a normal
393 * character toggles the case change behaviour
394 */
395 toggle = ~toggle;
396 /* skip special character */
397 ip++;
398 } else
399 /* not the special character */
400 if (toggle)
401 /* but case switching is on */
Heiko Carstens095761d2008-07-14 09:59:45 +0200402 if (sclp_tty_tolower)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* switch to uppercase */
404 *op++ = _ebc_toupper[(int) *ip++];
405 else
406 /* switch to lowercase */
407 *op++ = _ebc_tolower[(int) *ip++];
408 else
409 /* no case switching, copy the character */
410 *op++ = *ip++;
411 }
412 /* return length of reformatted string. */
413 return op - buf;
414}
415
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200416static void sclp_get_input(struct gds_subvector *sv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200418 unsigned char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int count;
420
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200421 str = (unsigned char *) (sv + 1);
422 count = sv->length - sizeof(*sv);
Heiko Carstens095761d2008-07-14 09:59:45 +0200423 if (sclp_tty_tolower)
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200424 EBC_TOLOWER(str, count);
425 count = sclp_switch_cases(str, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* convert EBCDIC to ASCII (modify original input in SCCB) */
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200427 sclp_ebcasc_str(str, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* transfer input to high level driver */
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200430 sclp_tty_input(str, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200433static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200435 void *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200437 end = (void *) sv + sv->length;
438 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
439 if (sv->key == 0x30)
440 sclp_get_input(sv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200443static inline void sclp_eval_textcmd(struct gds_vector *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200445 struct gds_subvector *sv;
446 void *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200448 end = (void *) v + v->length;
449 for (sv = (struct gds_subvector *) (v + 1);
450 (void *) sv < end; sv = (void *) sv + sv->length)
451 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
452 sclp_eval_selfdeftextmsg(sv);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200456static inline void sclp_eval_cpmsu(struct gds_vector *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200458 void *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200460 end = (void *) v + v->length;
461 for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
462 if (v->gds_id == GDS_ID_TEXTCMD)
463 sclp_eval_textcmd(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200467static inline void sclp_eval_mdsmu(struct gds_vector *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200469 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
470 if (v)
471 sclp_eval_cpmsu(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200474static void sclp_tty_receiver(struct evbuf_header *evbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200476 struct gds_vector *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200478 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
479 GDS_ID_MDSMU);
480 if (v)
481 sclp_eval_mdsmu(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static void
485sclp_tty_state_change(struct sclp_register *reg)
486{
487}
488
489static struct sclp_register sclp_input_event =
490{
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200491 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .state_change_fn = sclp_tty_state_change,
493 .receiver_fn = sclp_tty_receiver
494};
495
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700496static const struct tty_operations sclp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .open = sclp_tty_open,
498 .close = sclp_tty_close,
499 .write = sclp_tty_write,
500 .put_char = sclp_tty_put_char,
501 .flush_chars = sclp_tty_flush_chars,
502 .write_room = sclp_tty_write_room,
503 .chars_in_buffer = sclp_tty_chars_in_buffer,
504 .flush_buffer = sclp_tty_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505};
506
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100507static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508sclp_tty_init(void)
509{
510 struct tty_driver *driver;
511 void *page;
512 int i;
513 int rc;
514
515 if (!CONSOLE_IS_SCLP)
516 return 0;
517 driver = alloc_tty_driver(1);
518 if (!driver)
519 return -ENOMEM;
520
521 rc = sclp_rw_init();
522 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 put_tty_driver(driver);
524 return rc;
525 }
526 /* Allocate pages for output buffering */
527 INIT_LIST_HEAD(&sclp_tty_pages);
528 for (i = 0; i < MAX_KMEM_PAGES; i++) {
529 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
530 if (page == NULL) {
531 put_tty_driver(driver);
532 return -ENOMEM;
533 }
534 list_add_tail((struct list_head *) page, &sclp_tty_pages);
535 }
536 INIT_LIST_HEAD(&sclp_tty_outqueue);
537 spin_lock_init(&sclp_tty_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 init_timer(&sclp_tty_timer);
539 sclp_ttybuf = NULL;
540 sclp_tty_buffer_count = 0;
541 if (MACHINE_IS_VM) {
542 /*
543 * save 4 characters for the CPU number
544 * written at start of each line by VM/CP
545 */
Heiko Carstens095761d2008-07-14 09:59:45 +0200546 sclp_tty_columns = 76;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* case input lines to lowercase */
Heiko Carstens095761d2008-07-14 09:59:45 +0200548 sclp_tty_tolower = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 sclp_tty_chars_count = 0;
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200551 tty_port_init(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 rc = sclp_register(&sclp_input_event);
554 if (rc) {
555 put_tty_driver(driver);
556 return rc;
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 driver->driver_name = "sclp_line";
560 driver->name = "sclp_line";
561 driver->major = TTY_MAJOR;
562 driver->minor_start = 64;
563 driver->type = TTY_DRIVER_TYPE_SYSTEM;
564 driver->subtype = SYSTEM_TYPE_TTY;
565 driver->init_termios = tty_std_termios;
566 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
567 driver->init_termios.c_oflag = ONLCR | XTABS;
568 driver->init_termios.c_lflag = ISIG | ECHO;
569 driver->flags = TTY_DRIVER_REAL_RAW;
570 tty_set_operations(driver, &sclp_ops);
571 rc = tty_register_driver(driver);
572 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 put_tty_driver(driver);
574 return rc;
575 }
576 sclp_tty_driver = driver;
577 return 0;
578}
579module_init(sclp_tty_init);