Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * SCLP line mode terminal driver. |
| 4 | * |
| 5 | * S390 version |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 6 | * Copyright IBM Corp. 1999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Martin Peschke <mpeschke@de.ibm.com> |
| 8 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kmod.h> |
| 12 | #include <linux/tty.h> |
| 13 | #include <linux/tty_driver.h> |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 14 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include "ctrlchar.h" |
| 22 | #include "sclp.h" |
| 23 | #include "sclp_rw.h" |
| 24 | #include "sclp_tty.h" |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* |
| 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. */ |
Vineeth Vijayan | 7dd8ed0 | 2021-03-30 20:46:57 +0200 | [diff] [blame] | 38 | static DEFINE_SPINLOCK(sclp_tty_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* List of free pages that can be used for console output buffering. */ |
Vineeth Vijayan | 8bc00c0 | 2021-03-31 15:43:40 +0200 | [diff] [blame] | 40 | static LIST_HEAD(sclp_tty_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* List of full struct sclp_buffer structures ready for output. */ |
Vineeth Vijayan | 8bc00c0 | 2021-03-31 15:43:40 +0200 | [diff] [blame] | 42 | static LIST_HEAD(sclp_tty_outqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* Counter how many buffers are emitted. */ |
| 44 | static int sclp_tty_buffer_count; |
| 45 | /* Pointer to current console buffer. */ |
| 46 | static struct sclp_buffer *sclp_ttybuf; |
| 47 | /* Timer for delayed output of console messages. */ |
| 48 | static struct timer_list sclp_tty_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 50 | static struct tty_port sclp_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE]; |
| 52 | static unsigned short int sclp_tty_chars_count; |
| 53 | |
| 54 | struct tty_driver *sclp_tty_driver; |
| 55 | |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 56 | static int sclp_tty_tolower; |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 57 | |
Peter Oberparleiter | 98ce70b | 2021-03-02 17:28:12 +0100 | [diff] [blame] | 58 | #define SCLP_TTY_COLUMNS 320 |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 59 | #define SPACES_PER_TAB 8 |
| 60 | #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* This routine is called whenever we try to open a SCLP terminal. */ |
| 63 | static int |
| 64 | sclp_tty_open(struct tty_struct *tty, struct file *filp) |
| 65 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 66 | tty_port_tty_set(&sclp_port, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | tty->driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* This routine is called when the SCLP terminal is closed. */ |
| 72 | static void |
| 73 | sclp_tty_close(struct tty_struct *tty, struct file *filp) |
| 74 | { |
| 75 | if (tty->count > 1) |
| 76 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 77 | tty_port_tty_set(&sclp_port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 81 | * This routine returns the numbers of characters the tty driver |
| 82 | * will accept for queuing to be written. This number is subject |
| 83 | * to change as output buffers get emptied, or if the output flow |
| 84 | * control is acted. This is not an exact number because not every |
| 85 | * character needs the same space in the sccb. The worst case is |
Martin Schwidefsky | 18d1a7f | 2015-10-01 14:11:35 +0200 | [diff] [blame] | 86 | * a string of newlines. Every newline creates a new message which |
| 87 | * needs 82 bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | */ |
| 89 | static int |
| 90 | sclp_tty_write_room (struct tty_struct *tty) |
| 91 | { |
| 92 | unsigned long flags; |
| 93 | struct list_head *l; |
| 94 | int count; |
| 95 | |
| 96 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 97 | count = 0; |
| 98 | if (sclp_ttybuf != NULL) |
Martin Schwidefsky | 18d1a7f | 2015-10-01 14:11:35 +0200 | [diff] [blame] | 99 | count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct msg_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | list_for_each(l, &sclp_tty_pages) |
Martin Schwidefsky | 18d1a7f | 2015-10-01 14:11:35 +0200 | [diff] [blame] | 101 | count += NR_EMPTY_MSG_PER_SCCB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 103 | return count; |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc) |
| 108 | { |
| 109 | unsigned long flags; |
| 110 | void *page; |
| 111 | |
| 112 | do { |
| 113 | page = sclp_unmake_buffer(buffer); |
| 114 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 115 | /* Remove buffer from outqueue */ |
| 116 | list_del(&buffer->list); |
| 117 | sclp_tty_buffer_count--; |
| 118 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 119 | /* Check if there is a pending buffer on the out queue. */ |
| 120 | buffer = NULL; |
| 121 | if (!list_empty(&sclp_tty_outqueue)) |
| 122 | buffer = list_entry(sclp_tty_outqueue.next, |
| 123 | struct sclp_buffer, list); |
| 124 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 125 | } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback)); |
Jiri Slaby | 6aad04f | 2013-03-07 13:12:29 +0100 | [diff] [blame] | 126 | |
| 127 | tty_port_tty_wakeup(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static inline void |
| 131 | __sclp_ttybuf_emit(struct sclp_buffer *buffer) |
| 132 | { |
| 133 | unsigned long flags; |
| 134 | int count; |
| 135 | int rc; |
| 136 | |
| 137 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 138 | list_add_tail(&buffer->list, &sclp_tty_outqueue); |
| 139 | count = sclp_tty_buffer_count++; |
| 140 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 141 | if (count) |
| 142 | return; |
| 143 | rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback); |
| 144 | if (rc) |
| 145 | sclp_ttybuf_callback(buffer, rc); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * When this routine is called from the timer then we flush the |
| 150 | * temporary write buffer. |
| 151 | */ |
| 152 | static void |
Kees Cook | c9602ee | 2017-10-16 16:44:30 -0700 | [diff] [blame] | 153 | sclp_tty_timeout(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | unsigned long flags; |
| 156 | struct sclp_buffer *buf; |
| 157 | |
| 158 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 159 | buf = sclp_ttybuf; |
| 160 | sclp_ttybuf = NULL; |
| 161 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 162 | |
| 163 | if (buf != NULL) { |
| 164 | __sclp_ttybuf_emit(buf); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Write a string to the sclp tty. |
| 170 | */ |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 171 | static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | unsigned long flags; |
| 174 | void *page; |
| 175 | int written; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 176 | int overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | struct sclp_buffer *buf; |
| 178 | |
| 179 | if (count <= 0) |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 180 | return 0; |
| 181 | overall_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 183 | do { |
| 184 | /* Create a sclp output buffer if none exists yet */ |
| 185 | if (sclp_ttybuf == NULL) { |
| 186 | while (list_empty(&sclp_tty_pages)) { |
| 187 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 188 | if (may_fail) |
| 189 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | else |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 191 | sclp_sync_wait(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 193 | } |
| 194 | page = sclp_tty_pages.next; |
| 195 | list_del((struct list_head *) page); |
Peter Oberparleiter | 98ce70b | 2021-03-02 17:28:12 +0100 | [diff] [blame] | 196 | sclp_ttybuf = sclp_make_buffer(page, SCLP_TTY_COLUMNS, |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 197 | SPACES_PER_TAB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | /* try to write the string to the current output buffer */ |
| 200 | written = sclp_write(sclp_ttybuf, str, count); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 201 | overall_written += written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (written == count) |
| 203 | break; |
| 204 | /* |
| 205 | * Not all characters could be written to the current |
| 206 | * output buffer. Emit the buffer, create a new buffer |
| 207 | * and then output the rest of the string. |
| 208 | */ |
| 209 | buf = sclp_ttybuf; |
| 210 | sclp_ttybuf = NULL; |
| 211 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 212 | __sclp_ttybuf_emit(buf); |
| 213 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 214 | str += written; |
| 215 | count -= written; |
| 216 | } while (count > 0); |
| 217 | /* Setup timer to output current console buffer after 1/10 second */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 218 | if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) && |
| 219 | !timer_pending(&sclp_tty_timer)) { |
Himanshu Jha | 8179c7b | 2017-09-24 17:30:14 +0530 | [diff] [blame] | 220 | mod_timer(&sclp_tty_timer, jiffies + HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 223 | out: |
| 224 | return overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * This routine is called by the kernel to write a series of characters to the |
| 229 | * tty device. The characters may come from user space or kernel space. This |
| 230 | * routine will return the number of characters actually accepted for writing. |
| 231 | */ |
| 232 | static int |
| 233 | sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 234 | { |
| 235 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 236 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | sclp_tty_chars_count = 0; |
| 238 | } |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 239 | return sclp_tty_write_string(buf, count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * This routine is called by the kernel to write a single character to the tty |
| 244 | * device. If the kernel uses this routine, it must call the flush_chars() |
| 245 | * routine (if defined) when it is done stuffing characters into the driver. |
| 246 | * |
| 247 | * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. |
| 248 | * If the given character is a '\n' the contents of the SCLP write buffer |
| 249 | * - including previous characters from sclp_tty_put_char() and strings from |
| 250 | * sclp_write() without final '\n' - will be written. |
| 251 | */ |
Alan Cox | 9e7c9a1 | 2008-04-30 00:54:00 -0700 | [diff] [blame] | 252 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) |
| 254 | { |
| 255 | sclp_tty_chars[sclp_tty_chars_count++] = ch; |
| 256 | if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 257 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | sclp_tty_chars_count = 0; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 259 | } |
| 260 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * This routine is called by the kernel after it has written a series of |
| 265 | * characters to the tty device using put_char(). |
| 266 | */ |
| 267 | static void |
| 268 | sclp_tty_flush_chars(struct tty_struct *tty) |
| 269 | { |
| 270 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 271 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | sclp_tty_chars_count = 0; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * This routine returns the number of characters in the write buffer of the |
| 278 | * SCLP driver. The provided number includes all characters that are stored |
| 279 | * in the SCCB (will be written next time the SCLP is not busy) as well as |
| 280 | * characters in the write buffer (will not be written as long as there is a |
| 281 | * final line feed missing). |
| 282 | */ |
| 283 | static int |
| 284 | sclp_tty_chars_in_buffer(struct tty_struct *tty) |
| 285 | { |
| 286 | unsigned long flags; |
| 287 | struct list_head *l; |
| 288 | struct sclp_buffer *t; |
| 289 | int count; |
| 290 | |
| 291 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 292 | count = 0; |
| 293 | if (sclp_ttybuf != NULL) |
| 294 | count = sclp_chars_in_buffer(sclp_ttybuf); |
| 295 | list_for_each(l, &sclp_tty_outqueue) { |
| 296 | t = list_entry(l, struct sclp_buffer, list); |
| 297 | count += sclp_chars_in_buffer(t); |
| 298 | } |
| 299 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 300 | return count; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * removes all content from buffers of low level driver |
| 305 | */ |
| 306 | static void |
| 307 | sclp_tty_flush_buffer(struct tty_struct *tty) |
| 308 | { |
| 309 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 310 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | sclp_tty_chars_count = 0; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * push input to tty |
| 317 | */ |
| 318 | static void |
| 319 | sclp_tty_input(unsigned char* buf, unsigned int count) |
| 320 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 321 | struct tty_struct *tty = tty_port_tty_get(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | unsigned int cchar; |
| 323 | |
| 324 | /* |
| 325 | * If this tty driver is currently closed |
| 326 | * then throw the received input away. |
| 327 | */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 328 | if (tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 330 | cchar = ctrlchar_handle(buf, count, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | switch (cchar & CTRLCHAR_MASK) { |
| 332 | case CTRLCHAR_SYSRQ: |
| 333 | break; |
| 334 | case CTRLCHAR_CTRL: |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 335 | tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 336 | tty_flip_buffer_push(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | break; |
| 338 | case CTRLCHAR_NONE: |
| 339 | /* send (normal) input to line discipline */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | if (count < 2 || |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 341 | (strncmp((const char *) buf + count - 2, "^n", 2) && |
| 342 | strncmp((const char *) buf + count - 2, "\252n", 2))) { |
| 343 | /* add the auto \n */ |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 344 | tty_insert_flip_string(&sclp_port, buf, count); |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 345 | tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } else |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 347 | tty_insert_flip_string(&sclp_port, buf, count - 2); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 348 | tty_flip_buffer_push(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | break; |
| 350 | } |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 351 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * get a EBCDIC string in upper/lower case, |
| 356 | * find out characters in lower/upper case separated by a special character, |
| 357 | * modifiy original string, |
| 358 | * returns length of resulting string |
| 359 | */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 360 | static int sclp_switch_cases(unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | unsigned char *ip, *op; |
| 363 | int toggle; |
| 364 | |
| 365 | /* initially changing case is off */ |
| 366 | toggle = 0; |
| 367 | ip = op = buf; |
| 368 | while (count-- > 0) { |
| 369 | /* compare with special character */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 370 | if (*ip == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | /* followed by another special character? */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 372 | if (count && ip[1] == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* |
| 374 | * ... then put a single copy of the special |
| 375 | * character to the output string |
| 376 | */ |
| 377 | *op++ = *ip++; |
| 378 | count--; |
| 379 | } else |
| 380 | /* |
| 381 | * ... special character follower by a normal |
| 382 | * character toggles the case change behaviour |
| 383 | */ |
| 384 | toggle = ~toggle; |
| 385 | /* skip special character */ |
| 386 | ip++; |
| 387 | } else |
| 388 | /* not the special character */ |
| 389 | if (toggle) |
| 390 | /* but case switching is on */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 391 | if (sclp_tty_tolower) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* switch to uppercase */ |
| 393 | *op++ = _ebc_toupper[(int) *ip++]; |
| 394 | else |
| 395 | /* switch to lowercase */ |
| 396 | *op++ = _ebc_tolower[(int) *ip++]; |
| 397 | else |
| 398 | /* no case switching, copy the character */ |
| 399 | *op++ = *ip++; |
| 400 | } |
| 401 | /* return length of reformatted string. */ |
| 402 | return op - buf; |
| 403 | } |
| 404 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 405 | static void sclp_get_input(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 407 | unsigned char *str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | int count; |
| 409 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 410 | str = (unsigned char *) (sv + 1); |
| 411 | count = sv->length - sizeof(*sv); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 412 | if (sclp_tty_tolower) |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 413 | EBC_TOLOWER(str, count); |
| 414 | count = sclp_switch_cases(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* convert EBCDIC to ASCII (modify original input in SCCB) */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 416 | sclp_ebcasc_str(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* transfer input to high level driver */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 419 | sclp_tty_input(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 422 | static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 424 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 426 | end = (void *) sv + sv->length; |
| 427 | for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length) |
| 428 | if (sv->key == 0x30) |
| 429 | sclp_get_input(sv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 432 | static inline void sclp_eval_textcmd(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 434 | struct gds_subvector *sv; |
| 435 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 437 | end = (void *) v + v->length; |
| 438 | for (sv = (struct gds_subvector *) (v + 1); |
| 439 | (void *) sv < end; sv = (void *) sv + sv->length) |
| 440 | if (sv->key == GDS_KEY_SELFDEFTEXTMSG) |
| 441 | sclp_eval_selfdeftextmsg(sv); |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 445 | static inline void sclp_eval_cpmsu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 447 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 449 | end = (void *) v + v->length; |
| 450 | for (v = v + 1; (void *) v < end; v = (void *) v + v->length) |
| 451 | if (v->gds_id == GDS_ID_TEXTCMD) |
| 452 | sclp_eval_textcmd(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 456 | static inline void sclp_eval_mdsmu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 458 | v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU); |
| 459 | if (v) |
| 460 | sclp_eval_cpmsu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 463 | static void sclp_tty_receiver(struct evbuf_header *evbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 465 | struct gds_vector *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 467 | v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length, |
| 468 | GDS_ID_MDSMU); |
| 469 | if (v) |
| 470 | sclp_eval_mdsmu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void |
| 474 | sclp_tty_state_change(struct sclp_register *reg) |
| 475 | { |
| 476 | } |
| 477 | |
| 478 | static struct sclp_register sclp_input_event = |
| 479 | { |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame] | 480 | .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | .state_change_fn = sclp_tty_state_change, |
| 482 | .receiver_fn = sclp_tty_receiver |
| 483 | }; |
| 484 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 485 | static const struct tty_operations sclp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | .open = sclp_tty_open, |
| 487 | .close = sclp_tty_close, |
| 488 | .write = sclp_tty_write, |
| 489 | .put_char = sclp_tty_put_char, |
| 490 | .flush_chars = sclp_tty_flush_chars, |
| 491 | .write_room = sclp_tty_write_room, |
| 492 | .chars_in_buffer = sclp_tty_chars_in_buffer, |
| 493 | .flush_buffer = sclp_tty_flush_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 496 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | sclp_tty_init(void) |
| 498 | { |
| 499 | struct tty_driver *driver; |
| 500 | void *page; |
| 501 | int i; |
| 502 | int rc; |
| 503 | |
Christian Borntraeger | 936b216 | 2018-02-21 11:54:07 +0000 | [diff] [blame] | 504 | /* z/VM multiplexes the line mode output on the 32xx screen */ |
| 505 | if (MACHINE_IS_VM && !CONSOLE_IS_SCLP) |
| 506 | return 0; |
| 507 | if (!sclp.has_linemode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return 0; |
| 509 | driver = alloc_tty_driver(1); |
| 510 | if (!driver) |
| 511 | return -ENOMEM; |
| 512 | |
| 513 | rc = sclp_rw_init(); |
| 514 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | put_tty_driver(driver); |
| 516 | return rc; |
| 517 | } |
| 518 | /* Allocate pages for output buffering */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | for (i = 0; i < MAX_KMEM_PAGES; i++) { |
| 520 | page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 521 | if (page == NULL) { |
| 522 | put_tty_driver(driver); |
| 523 | return -ENOMEM; |
| 524 | } |
| 525 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 526 | } |
Kees Cook | c9602ee | 2017-10-16 16:44:30 -0700 | [diff] [blame] | 527 | timer_setup(&sclp_tty_timer, sclp_tty_timeout, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | sclp_ttybuf = NULL; |
| 529 | sclp_tty_buffer_count = 0; |
| 530 | if (MACHINE_IS_VM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* case input lines to lowercase */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 532 | sclp_tty_tolower = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | sclp_tty_chars_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | rc = sclp_register(&sclp_input_event); |
| 537 | if (rc) { |
| 538 | put_tty_driver(driver); |
| 539 | return rc; |
| 540 | } |
| 541 | |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 542 | tty_port_init(&sclp_port); |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | driver->driver_name = "sclp_line"; |
| 545 | driver->name = "sclp_line"; |
| 546 | driver->major = TTY_MAJOR; |
| 547 | driver->minor_start = 64; |
| 548 | driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 549 | driver->subtype = SYSTEM_TYPE_TTY; |
| 550 | driver->init_termios = tty_std_termios; |
| 551 | driver->init_termios.c_iflag = IGNBRK | IGNPAR; |
Martin Schwidefsky | b8a2bbd | 2014-08-13 14:24:58 +0200 | [diff] [blame] | 552 | driver->init_termios.c_oflag = ONLCR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | driver->init_termios.c_lflag = ISIG | ECHO; |
| 554 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 555 | tty_set_operations(driver, &sclp_ops); |
Jiri Slaby | b19e2ca | 2012-08-07 21:47:51 +0200 | [diff] [blame] | 556 | tty_port_link_device(&sclp_port, driver, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | rc = tty_register_driver(driver); |
| 558 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | put_tty_driver(driver); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 560 | tty_port_destroy(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return rc; |
| 562 | } |
| 563 | sclp_tty_driver = driver; |
| 564 | return 0; |
| 565 | } |
Paul Gortmaker | 238b928 | 2016-10-30 16:37:27 -0400 | [diff] [blame] | 566 | device_initcall(sclp_tty_init); |