blob: 5aff8b684eb21e8557f253c7ddf18d48420435c1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * SCLP line mode terminal driver.
4 *
5 * S390 version
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02006 * Copyright IBM Corp. 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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/kmod.h>
12#include <linux/tty.h>
13#include <linux/tty_driver.h>
Alan Cox33f0f882006-01-09 20:54:13 -080014#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "ctrlchar.h"
22#include "sclp.h"
23#include "sclp_rw.h"
24#include "sclp_tty.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/*
27 * size of a buffer that collects single characters coming in
28 * via sclp_tty_put_char()
29 */
30#define SCLP_TTY_BUF_SIZE 512
31
32/*
33 * There is exactly one SCLP terminal, so we can keep things simple
34 * and allocate all variables statically.
35 */
36
37/* Lock to guard over changes to global variables. */
38static spinlock_t sclp_tty_lock;
39/* List of free pages that can be used for console output buffering. */
40static struct list_head sclp_tty_pages;
41/* List of full struct sclp_buffer structures ready for output. */
42static struct list_head sclp_tty_outqueue;
43/* Counter how many buffers are emitted. */
44static int sclp_tty_buffer_count;
45/* Pointer to current console buffer. */
46static struct sclp_buffer *sclp_ttybuf;
47/* Timer for delayed output of console messages. */
48static struct timer_list sclp_tty_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jiri Slaby0ea63da2012-04-02 13:54:12 +020050static struct tty_port sclp_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
52static unsigned short int sclp_tty_chars_count;
53
54struct tty_driver *sclp_tty_driver;
55
Heiko Carstens095761d2008-07-14 09:59:45 +020056static int sclp_tty_tolower;
57static int sclp_tty_columns = 80;
58
59#define SPACES_PER_TAB 8
60#define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* This routine is called whenever we try to open a SCLP terminal. */
63static int
64sclp_tty_open(struct tty_struct *tty, struct file *filp)
65{
Jiri Slaby0ea63da2012-04-02 13:54:12 +020066 tty_port_tty_set(&sclp_port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 tty->driver_data = NULL;
Jiri Slabyd6c53c0e2013-01-03 15:53:05 +010068 sclp_port.low_latency = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
70}
71
72/* This routine is called when the SCLP terminal is closed. */
73static void
74sclp_tty_close(struct tty_struct *tty, struct file *filp)
75{
76 if (tty->count > 1)
77 return;
Jiri Slaby0ea63da2012-04-02 13:54:12 +020078 tty_port_tty_set(&sclp_port, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * This routine returns the numbers of characters the tty driver
83 * will accept for queuing to be written. This number is subject
84 * to change as output buffers get emptied, or if the output flow
85 * control is acted. This is not an exact number because not every
86 * character needs the same space in the sccb. The worst case is
Martin Schwidefsky18d1a7f2015-10-01 14:11:35 +020087 * a string of newlines. Every newline creates a new message which
88 * needs 82 bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90static int
91sclp_tty_write_room (struct tty_struct *tty)
92{
93 unsigned long flags;
94 struct list_head *l;
95 int count;
96
97 spin_lock_irqsave(&sclp_tty_lock, flags);
98 count = 0;
99 if (sclp_ttybuf != NULL)
Martin Schwidefsky18d1a7f2015-10-01 14:11:35 +0200100 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct msg_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 list_for_each(l, &sclp_tty_pages)
Martin Schwidefsky18d1a7f2015-10-01 14:11:35 +0200102 count += NR_EMPTY_MSG_PER_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 spin_unlock_irqrestore(&sclp_tty_lock, flags);
104 return count;
105}
106
107static void
108sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
109{
110 unsigned long flags;
111 void *page;
112
113 do {
114 page = sclp_unmake_buffer(buffer);
115 spin_lock_irqsave(&sclp_tty_lock, flags);
116 /* Remove buffer from outqueue */
117 list_del(&buffer->list);
118 sclp_tty_buffer_count--;
119 list_add_tail((struct list_head *) page, &sclp_tty_pages);
120 /* Check if there is a pending buffer on the out queue. */
121 buffer = NULL;
122 if (!list_empty(&sclp_tty_outqueue))
123 buffer = list_entry(sclp_tty_outqueue.next,
124 struct sclp_buffer, list);
125 spin_unlock_irqrestore(&sclp_tty_lock, flags);
126 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100127
128 tty_port_tty_wakeup(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static inline void
132__sclp_ttybuf_emit(struct sclp_buffer *buffer)
133{
134 unsigned long flags;
135 int count;
136 int rc;
137
138 spin_lock_irqsave(&sclp_tty_lock, flags);
139 list_add_tail(&buffer->list, &sclp_tty_outqueue);
140 count = sclp_tty_buffer_count++;
141 spin_unlock_irqrestore(&sclp_tty_lock, flags);
142 if (count)
143 return;
144 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
145 if (rc)
146 sclp_ttybuf_callback(buffer, rc);
147}
148
149/*
150 * When this routine is called from the timer then we flush the
151 * temporary write buffer.
152 */
153static void
Kees Cookc9602ee2017-10-16 16:44:30 -0700154sclp_tty_timeout(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 unsigned long flags;
157 struct sclp_buffer *buf;
158
159 spin_lock_irqsave(&sclp_tty_lock, flags);
160 buf = sclp_ttybuf;
161 sclp_ttybuf = NULL;
162 spin_unlock_irqrestore(&sclp_tty_lock, flags);
163
164 if (buf != NULL) {
165 __sclp_ttybuf_emit(buf);
166 }
167}
168
169/*
170 * Write a string to the sclp tty.
171 */
Heiko Carstens5e345992008-07-14 09:59:46 +0200172static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned long flags;
175 void *page;
176 int written;
Heiko Carstens5e345992008-07-14 09:59:46 +0200177 int overall_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct sclp_buffer *buf;
179
180 if (count <= 0)
Heiko Carstens5e345992008-07-14 09:59:46 +0200181 return 0;
182 overall_written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_lock_irqsave(&sclp_tty_lock, flags);
184 do {
185 /* Create a sclp output buffer if none exists yet */
186 if (sclp_ttybuf == NULL) {
187 while (list_empty(&sclp_tty_pages)) {
188 spin_unlock_irqrestore(&sclp_tty_lock, flags);
Heiko Carstens5e345992008-07-14 09:59:46 +0200189 if (may_fail)
190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 else
Heiko Carstens5e345992008-07-14 09:59:46 +0200192 sclp_sync_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 spin_lock_irqsave(&sclp_tty_lock, flags);
194 }
195 page = sclp_tty_pages.next;
196 list_del((struct list_head *) page);
Heiko Carstens095761d2008-07-14 09:59:45 +0200197 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
198 SPACES_PER_TAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 /* try to write the string to the current output buffer */
201 written = sclp_write(sclp_ttybuf, str, count);
Heiko Carstens5e345992008-07-14 09:59:46 +0200202 overall_written += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (written == count)
204 break;
205 /*
206 * Not all characters could be written to the current
207 * output buffer. Emit the buffer, create a new buffer
208 * and then output the rest of the string.
209 */
210 buf = sclp_ttybuf;
211 sclp_ttybuf = NULL;
212 spin_unlock_irqrestore(&sclp_tty_lock, flags);
213 __sclp_ttybuf_emit(buf);
214 spin_lock_irqsave(&sclp_tty_lock, flags);
215 str += written;
216 count -= written;
217 } while (count > 0);
218 /* Setup timer to output current console buffer after 1/10 second */
Heiko Carstens095761d2008-07-14 09:59:45 +0200219 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
220 !timer_pending(&sclp_tty_timer)) {
Himanshu Jha8179c7b2017-09-24 17:30:14 +0530221 mod_timer(&sclp_tty_timer, jiffies + HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 spin_unlock_irqrestore(&sclp_tty_lock, flags);
Heiko Carstens5e345992008-07-14 09:59:46 +0200224out:
225 return overall_written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/*
229 * This routine is called by the kernel to write a series of characters to the
230 * tty device. The characters may come from user space or kernel space. This
231 * routine will return the number of characters actually accepted for writing.
232 */
233static int
234sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
235{
236 if (sclp_tty_chars_count > 0) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200237 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 sclp_tty_chars_count = 0;
239 }
Heiko Carstens5e345992008-07-14 09:59:46 +0200240 return sclp_tty_write_string(buf, count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/*
244 * This routine is called by the kernel to write a single character to the tty
245 * device. If the kernel uses this routine, it must call the flush_chars()
246 * routine (if defined) when it is done stuffing characters into the driver.
247 *
248 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
249 * If the given character is a '\n' the contents of the SCLP write buffer
250 * - including previous characters from sclp_tty_put_char() and strings from
251 * sclp_write() without final '\n' - will be written.
252 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700253static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
255{
256 sclp_tty_chars[sclp_tty_chars_count++] = ch;
257 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200258 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sclp_tty_chars_count = 0;
Heiko Carstens5e345992008-07-14 09:59:46 +0200260 }
261 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
265 * This routine is called by the kernel after it has written a series of
266 * characters to the tty device using put_char().
267 */
268static void
269sclp_tty_flush_chars(struct tty_struct *tty)
270{
271 if (sclp_tty_chars_count > 0) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200272 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 sclp_tty_chars_count = 0;
274 }
275}
276
277/*
278 * This routine returns the number of characters in the write buffer of the
279 * SCLP driver. The provided number includes all characters that are stored
280 * in the SCCB (will be written next time the SCLP is not busy) as well as
281 * characters in the write buffer (will not be written as long as there is a
282 * final line feed missing).
283 */
284static int
285sclp_tty_chars_in_buffer(struct tty_struct *tty)
286{
287 unsigned long flags;
288 struct list_head *l;
289 struct sclp_buffer *t;
290 int count;
291
292 spin_lock_irqsave(&sclp_tty_lock, flags);
293 count = 0;
294 if (sclp_ttybuf != NULL)
295 count = sclp_chars_in_buffer(sclp_ttybuf);
296 list_for_each(l, &sclp_tty_outqueue) {
297 t = list_entry(l, struct sclp_buffer, list);
298 count += sclp_chars_in_buffer(t);
299 }
300 spin_unlock_irqrestore(&sclp_tty_lock, flags);
301 return count;
302}
303
304/*
305 * removes all content from buffers of low level driver
306 */
307static void
308sclp_tty_flush_buffer(struct tty_struct *tty)
309{
310 if (sclp_tty_chars_count > 0) {
Heiko Carstens5e345992008-07-14 09:59:46 +0200311 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 sclp_tty_chars_count = 0;
313 }
314}
315
316/*
317 * push input to tty
318 */
319static void
320sclp_tty_input(unsigned char* buf, unsigned int count)
321{
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200322 struct tty_struct *tty = tty_port_tty_get(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned int cchar;
324
325 /*
326 * If this tty driver is currently closed
327 * then throw the received input away.
328 */
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200329 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return;
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200331 cchar = ctrlchar_handle(buf, count, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 switch (cchar & CTRLCHAR_MASK) {
333 case CTRLCHAR_SYSRQ:
334 break;
335 case CTRLCHAR_CTRL:
Jiri Slaby92a19f92013-01-03 15:53:03 +0100336 tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100337 tty_flip_buffer_push(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
339 case CTRLCHAR_NONE:
340 /* send (normal) input to line discipline */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800342 (strncmp((const char *) buf + count - 2, "^n", 2) &&
343 strncmp((const char *) buf + count - 2, "\252n", 2))) {
344 /* add the auto \n */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100345 tty_insert_flip_string(&sclp_port, buf, count);
Jiri Slaby92a19f92013-01-03 15:53:03 +0100346 tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100348 tty_insert_flip_string(&sclp_port, buf, count - 2);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100349 tty_flip_buffer_push(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 }
Jiri Slaby0ea63da2012-04-02 13:54:12 +0200352 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/*
356 * get a EBCDIC string in upper/lower case,
357 * find out characters in lower/upper case separated by a special character,
358 * modifiy original string,
359 * returns length of resulting string
360 */
Heiko Carstens095761d2008-07-14 09:59:45 +0200361static int sclp_switch_cases(unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned char *ip, *op;
364 int toggle;
365
366 /* initially changing case is off */
367 toggle = 0;
368 ip = op = buf;
369 while (count-- > 0) {
370 /* compare with special character */
Heiko Carstens095761d2008-07-14 09:59:45 +0200371 if (*ip == CASE_DELIMITER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* followed by another special character? */
Heiko Carstens095761d2008-07-14 09:59:45 +0200373 if (count && ip[1] == CASE_DELIMITER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * ... then put a single copy of the special
376 * character to the output string
377 */
378 *op++ = *ip++;
379 count--;
380 } else
381 /*
382 * ... special character follower by a normal
383 * character toggles the case change behaviour
384 */
385 toggle = ~toggle;
386 /* skip special character */
387 ip++;
388 } else
389 /* not the special character */
390 if (toggle)
391 /* but case switching is on */
Heiko Carstens095761d2008-07-14 09:59:45 +0200392 if (sclp_tty_tolower)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* switch to uppercase */
394 *op++ = _ebc_toupper[(int) *ip++];
395 else
396 /* switch to lowercase */
397 *op++ = _ebc_tolower[(int) *ip++];
398 else
399 /* no case switching, copy the character */
400 *op++ = *ip++;
401 }
402 /* return length of reformatted string. */
403 return op - buf;
404}
405
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200406static void sclp_get_input(struct gds_subvector *sv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200408 unsigned char *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int count;
410
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200411 str = (unsigned char *) (sv + 1);
412 count = sv->length - sizeof(*sv);
Heiko Carstens095761d2008-07-14 09:59:45 +0200413 if (sclp_tty_tolower)
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200414 EBC_TOLOWER(str, count);
415 count = sclp_switch_cases(str, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* convert EBCDIC to ASCII (modify original input in SCCB) */
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200417 sclp_ebcasc_str(str, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* transfer input to high level driver */
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200420 sclp_tty_input(str, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200423static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200425 void *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200427 end = (void *) sv + sv->length;
428 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
429 if (sv->key == 0x30)
430 sclp_get_input(sv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200433static inline void sclp_eval_textcmd(struct gds_vector *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200435 struct gds_subvector *sv;
436 void *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200438 end = (void *) v + v->length;
439 for (sv = (struct gds_subvector *) (v + 1);
440 (void *) sv < end; sv = (void *) sv + sv->length)
441 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
442 sclp_eval_selfdeftextmsg(sv);
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200446static inline void sclp_eval_cpmsu(struct gds_vector *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200448 void *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200450 end = (void *) v + v->length;
451 for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
452 if (v->gds_id == GDS_ID_TEXTCMD)
453 sclp_eval_textcmd(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200457static inline void sclp_eval_mdsmu(struct gds_vector *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200459 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
460 if (v)
461 sclp_eval_cpmsu(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200464static void sclp_tty_receiver(struct evbuf_header *evbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200466 struct gds_vector *v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Martin Schwidefsky30c2df52011-05-23 10:24:42 +0200468 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
469 GDS_ID_MDSMU);
470 if (v)
471 sclp_eval_mdsmu(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474static void
475sclp_tty_state_change(struct sclp_register *reg)
476{
477}
478
479static struct sclp_register sclp_input_event =
480{
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200481 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .state_change_fn = sclp_tty_state_change,
483 .receiver_fn = sclp_tty_receiver
484};
485
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700486static const struct tty_operations sclp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 .open = sclp_tty_open,
488 .close = sclp_tty_close,
489 .write = sclp_tty_write,
490 .put_char = sclp_tty_put_char,
491 .flush_chars = sclp_tty_flush_chars,
492 .write_room = sclp_tty_write_room,
493 .chars_in_buffer = sclp_tty_chars_in_buffer,
494 .flush_buffer = sclp_tty_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495};
496
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100497static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498sclp_tty_init(void)
499{
500 struct tty_driver *driver;
501 void *page;
502 int i;
503 int rc;
504
Christian Borntraeger936b2162018-02-21 11:54:07 +0000505 /* z/VM multiplexes the line mode output on the 32xx screen */
506 if (MACHINE_IS_VM && !CONSOLE_IS_SCLP)
507 return 0;
508 if (!sclp.has_linemode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510 driver = alloc_tty_driver(1);
511 if (!driver)
512 return -ENOMEM;
513
514 rc = sclp_rw_init();
515 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 put_tty_driver(driver);
517 return rc;
518 }
519 /* Allocate pages for output buffering */
520 INIT_LIST_HEAD(&sclp_tty_pages);
521 for (i = 0; i < MAX_KMEM_PAGES; i++) {
522 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
523 if (page == NULL) {
524 put_tty_driver(driver);
525 return -ENOMEM;
526 }
527 list_add_tail((struct list_head *) page, &sclp_tty_pages);
528 }
529 INIT_LIST_HEAD(&sclp_tty_outqueue);
530 spin_lock_init(&sclp_tty_lock);
Kees Cookc9602ee2017-10-16 16:44:30 -0700531 timer_setup(&sclp_tty_timer, sclp_tty_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 sclp_ttybuf = NULL;
533 sclp_tty_buffer_count = 0;
534 if (MACHINE_IS_VM) {
535 /*
536 * save 4 characters for the CPU number
537 * written at start of each line by VM/CP
538 */
Heiko Carstens095761d2008-07-14 09:59:45 +0200539 sclp_tty_columns = 76;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* case input lines to lowercase */
Heiko Carstens095761d2008-07-14 09:59:45 +0200541 sclp_tty_tolower = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 sclp_tty_chars_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 rc = sclp_register(&sclp_input_event);
546 if (rc) {
547 put_tty_driver(driver);
548 return rc;
549 }
550
Jiri Slaby191c5f12012-11-15 09:49:56 +0100551 tty_port_init(&sclp_port);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 driver->driver_name = "sclp_line";
554 driver->name = "sclp_line";
555 driver->major = TTY_MAJOR;
556 driver->minor_start = 64;
557 driver->type = TTY_DRIVER_TYPE_SYSTEM;
558 driver->subtype = SYSTEM_TYPE_TTY;
559 driver->init_termios = tty_std_termios;
560 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
Martin Schwidefskyb8a2bbd2014-08-13 14:24:58 +0200561 driver->init_termios.c_oflag = ONLCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 driver->init_termios.c_lflag = ISIG | ECHO;
563 driver->flags = TTY_DRIVER_REAL_RAW;
564 tty_set_operations(driver, &sclp_ops);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200565 tty_port_link_device(&sclp_port, driver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rc = tty_register_driver(driver);
567 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 put_tty_driver(driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100569 tty_port_destroy(&sclp_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return rc;
571 }
572 sclp_tty_driver = driver;
573 return 0;
574}
Paul Gortmaker238b9282016-10-30 16:37:27 -0400575device_initcall(sclp_tty_init);