Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * SCLP line mode terminal driver. |
| 3 | * |
| 4 | * S390 version |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 1999 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Author(s): Martin Peschke <mpeschke@de.ibm.com> |
| 7 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 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 | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/uaccess.h> |
| 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. */ |
| 38 | static spinlock_t sclp_tty_lock; |
| 39 | /* List of free pages that can be used for console output buffering. */ |
| 40 | static struct list_head sclp_tty_pages; |
| 41 | /* List of full struct sclp_buffer structures ready for output. */ |
| 42 | static struct list_head sclp_tty_outqueue; |
| 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; |
| 57 | static 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 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; |
Jiri Slaby | d6c53c0 | 2013-01-03 15:53:05 +0100 | [diff] [blame] | 68 | sclp_port.low_latency = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* This routine is called when the SCLP terminal is closed. */ |
| 73 | static void |
| 74 | sclp_tty_close(struct tty_struct *tty, struct file *filp) |
| 75 | { |
| 76 | if (tty->count > 1) |
| 77 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 78 | tty_port_tty_set(&sclp_port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
| 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 |
| 87 | * a string of newlines. Every newlines creates a new mto which |
| 88 | * needs 8 bytes. |
| 89 | */ |
| 90 | static int |
| 91 | sclp_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) |
| 100 | count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto); |
| 101 | list_for_each(l, &sclp_tty_pages) |
| 102 | count += NR_EMPTY_MTO_PER_SCCB; |
| 103 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 104 | return count; |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | sclp_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 Slaby | 6aad04f | 2013-03-07 13:12:29 +0100 | [diff] [blame] | 127 | |
| 128 | tty_port_tty_wakeup(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static 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 | */ |
| 153 | static void |
| 154 | sclp_tty_timeout(unsigned long data) |
| 155 | { |
| 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 Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 172 | 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] | 173 | { |
| 174 | unsigned long flags; |
| 175 | void *page; |
| 176 | int written; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 177 | int overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | struct sclp_buffer *buf; |
| 179 | |
| 180 | if (count <= 0) |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 181 | return 0; |
| 182 | overall_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | 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 Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 189 | if (may_fail) |
| 190 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | else |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 192 | sclp_sync_wait(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 194 | } |
| 195 | page = sclp_tty_pages.next; |
| 196 | list_del((struct list_head *) page); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 197 | sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns, |
| 198 | SPACES_PER_TAB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | /* try to write the string to the current output buffer */ |
| 201 | written = sclp_write(sclp_ttybuf, str, count); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 202 | overall_written += written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 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 Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 219 | if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) && |
| 220 | !timer_pending(&sclp_tty_timer)) { |
| 221 | init_timer(&sclp_tty_timer); |
| 222 | sclp_tty_timer.function = sclp_tty_timeout; |
| 223 | sclp_tty_timer.data = 0UL; |
| 224 | sclp_tty_timer.expires = jiffies + HZ/10; |
| 225 | add_timer(&sclp_tty_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 228 | out: |
| 229 | return overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This routine is called by the kernel to write a series of characters to the |
| 234 | * tty device. The characters may come from user space or kernel space. This |
| 235 | * routine will return the number of characters actually accepted for writing. |
| 236 | */ |
| 237 | static int |
| 238 | sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 239 | { |
| 240 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 241 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | sclp_tty_chars_count = 0; |
| 243 | } |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 244 | return sclp_tty_write_string(buf, count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * This routine is called by the kernel to write a single character to the tty |
| 249 | * device. If the kernel uses this routine, it must call the flush_chars() |
| 250 | * routine (if defined) when it is done stuffing characters into the driver. |
| 251 | * |
| 252 | * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. |
| 253 | * If the given character is a '\n' the contents of the SCLP write buffer |
| 254 | * - including previous characters from sclp_tty_put_char() and strings from |
| 255 | * sclp_write() without final '\n' - will be written. |
| 256 | */ |
Alan Cox | 9e7c9a1 | 2008-04-30 00:54:00 -0700 | [diff] [blame] | 257 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) |
| 259 | { |
| 260 | sclp_tty_chars[sclp_tty_chars_count++] = ch; |
| 261 | if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 262 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | sclp_tty_chars_count = 0; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 264 | } |
| 265 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * This routine is called by the kernel after it has written a series of |
| 270 | * characters to the tty device using put_char(). |
| 271 | */ |
| 272 | static void |
| 273 | sclp_tty_flush_chars(struct tty_struct *tty) |
| 274 | { |
| 275 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 276 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | sclp_tty_chars_count = 0; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * This routine returns the number of characters in the write buffer of the |
| 283 | * SCLP driver. The provided number includes all characters that are stored |
| 284 | * in the SCCB (will be written next time the SCLP is not busy) as well as |
| 285 | * characters in the write buffer (will not be written as long as there is a |
| 286 | * final line feed missing). |
| 287 | */ |
| 288 | static int |
| 289 | sclp_tty_chars_in_buffer(struct tty_struct *tty) |
| 290 | { |
| 291 | unsigned long flags; |
| 292 | struct list_head *l; |
| 293 | struct sclp_buffer *t; |
| 294 | int count; |
| 295 | |
| 296 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 297 | count = 0; |
| 298 | if (sclp_ttybuf != NULL) |
| 299 | count = sclp_chars_in_buffer(sclp_ttybuf); |
| 300 | list_for_each(l, &sclp_tty_outqueue) { |
| 301 | t = list_entry(l, struct sclp_buffer, list); |
| 302 | count += sclp_chars_in_buffer(t); |
| 303 | } |
| 304 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 305 | return count; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * removes all content from buffers of low level driver |
| 310 | */ |
| 311 | static void |
| 312 | sclp_tty_flush_buffer(struct tty_struct *tty) |
| 313 | { |
| 314 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 315 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | sclp_tty_chars_count = 0; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * push input to tty |
| 322 | */ |
| 323 | static void |
| 324 | sclp_tty_input(unsigned char* buf, unsigned int count) |
| 325 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 326 | struct tty_struct *tty = tty_port_tty_get(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | unsigned int cchar; |
| 328 | |
| 329 | /* |
| 330 | * If this tty driver is currently closed |
| 331 | * then throw the received input away. |
| 332 | */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 333 | if (tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 335 | cchar = ctrlchar_handle(buf, count, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | switch (cchar & CTRLCHAR_MASK) { |
| 337 | case CTRLCHAR_SYSRQ: |
| 338 | break; |
| 339 | case CTRLCHAR_CTRL: |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 340 | tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 341 | tty_flip_buffer_push(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | break; |
| 343 | case CTRLCHAR_NONE: |
| 344 | /* send (normal) input to line discipline */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (count < 2 || |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 346 | (strncmp((const char *) buf + count - 2, "^n", 2) && |
| 347 | strncmp((const char *) buf + count - 2, "\252n", 2))) { |
| 348 | /* add the auto \n */ |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 349 | tty_insert_flip_string(&sclp_port, buf, count); |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 350 | tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } else |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 352 | tty_insert_flip_string(&sclp_port, buf, count - 2); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 353 | tty_flip_buffer_push(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | break; |
| 355 | } |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 356 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * get a EBCDIC string in upper/lower case, |
| 361 | * find out characters in lower/upper case separated by a special character, |
| 362 | * modifiy original string, |
| 363 | * returns length of resulting string |
| 364 | */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 365 | static int sclp_switch_cases(unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
| 367 | unsigned char *ip, *op; |
| 368 | int toggle; |
| 369 | |
| 370 | /* initially changing case is off */ |
| 371 | toggle = 0; |
| 372 | ip = op = buf; |
| 373 | while (count-- > 0) { |
| 374 | /* compare with special character */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 375 | if (*ip == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /* followed by another special character? */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 377 | if (count && ip[1] == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | /* |
| 379 | * ... then put a single copy of the special |
| 380 | * character to the output string |
| 381 | */ |
| 382 | *op++ = *ip++; |
| 383 | count--; |
| 384 | } else |
| 385 | /* |
| 386 | * ... special character follower by a normal |
| 387 | * character toggles the case change behaviour |
| 388 | */ |
| 389 | toggle = ~toggle; |
| 390 | /* skip special character */ |
| 391 | ip++; |
| 392 | } else |
| 393 | /* not the special character */ |
| 394 | if (toggle) |
| 395 | /* but case switching is on */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 396 | if (sclp_tty_tolower) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | /* switch to uppercase */ |
| 398 | *op++ = _ebc_toupper[(int) *ip++]; |
| 399 | else |
| 400 | /* switch to lowercase */ |
| 401 | *op++ = _ebc_tolower[(int) *ip++]; |
| 402 | else |
| 403 | /* no case switching, copy the character */ |
| 404 | *op++ = *ip++; |
| 405 | } |
| 406 | /* return length of reformatted string. */ |
| 407 | return op - buf; |
| 408 | } |
| 409 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 410 | static void sclp_get_input(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 412 | unsigned char *str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | int count; |
| 414 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 415 | str = (unsigned char *) (sv + 1); |
| 416 | count = sv->length - sizeof(*sv); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 417 | if (sclp_tty_tolower) |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 418 | EBC_TOLOWER(str, count); |
| 419 | count = sclp_switch_cases(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* convert EBCDIC to ASCII (modify original input in SCCB) */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 421 | sclp_ebcasc_str(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | /* transfer input to high level driver */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 424 | sclp_tty_input(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 427 | static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 429 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 431 | end = (void *) sv + sv->length; |
| 432 | for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length) |
| 433 | if (sv->key == 0x30) |
| 434 | sclp_get_input(sv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 437 | static inline void sclp_eval_textcmd(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 439 | struct gds_subvector *sv; |
| 440 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 442 | end = (void *) v + v->length; |
| 443 | for (sv = (struct gds_subvector *) (v + 1); |
| 444 | (void *) sv < end; sv = (void *) sv + sv->length) |
| 445 | if (sv->key == GDS_KEY_SELFDEFTEXTMSG) |
| 446 | sclp_eval_selfdeftextmsg(sv); |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 450 | static inline void sclp_eval_cpmsu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 452 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 454 | end = (void *) v + v->length; |
| 455 | for (v = v + 1; (void *) v < end; v = (void *) v + v->length) |
| 456 | if (v->gds_id == GDS_ID_TEXTCMD) |
| 457 | sclp_eval_textcmd(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 461 | static inline void sclp_eval_mdsmu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 463 | v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU); |
| 464 | if (v) |
| 465 | sclp_eval_cpmsu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 468 | static void sclp_tty_receiver(struct evbuf_header *evbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 470 | struct gds_vector *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 472 | v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length, |
| 473 | GDS_ID_MDSMU); |
| 474 | if (v) |
| 475 | sclp_eval_mdsmu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static void |
| 479 | sclp_tty_state_change(struct sclp_register *reg) |
| 480 | { |
| 481 | } |
| 482 | |
| 483 | static struct sclp_register sclp_input_event = |
| 484 | { |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame] | 485 | .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | .state_change_fn = sclp_tty_state_change, |
| 487 | .receiver_fn = sclp_tty_receiver |
| 488 | }; |
| 489 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 490 | static const struct tty_operations sclp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | .open = sclp_tty_open, |
| 492 | .close = sclp_tty_close, |
| 493 | .write = sclp_tty_write, |
| 494 | .put_char = sclp_tty_put_char, |
| 495 | .flush_chars = sclp_tty_flush_chars, |
| 496 | .write_room = sclp_tty_write_room, |
| 497 | .chars_in_buffer = sclp_tty_chars_in_buffer, |
| 498 | .flush_buffer = sclp_tty_flush_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | }; |
| 500 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 501 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | sclp_tty_init(void) |
| 503 | { |
| 504 | struct tty_driver *driver; |
| 505 | void *page; |
| 506 | int i; |
| 507 | int rc; |
| 508 | |
| 509 | if (!CONSOLE_IS_SCLP) |
| 510 | return 0; |
| 511 | driver = alloc_tty_driver(1); |
| 512 | if (!driver) |
| 513 | return -ENOMEM; |
| 514 | |
| 515 | rc = sclp_rw_init(); |
| 516 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | put_tty_driver(driver); |
| 518 | return rc; |
| 519 | } |
| 520 | /* Allocate pages for output buffering */ |
| 521 | INIT_LIST_HEAD(&sclp_tty_pages); |
| 522 | for (i = 0; i < MAX_KMEM_PAGES; i++) { |
| 523 | page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 524 | if (page == NULL) { |
| 525 | put_tty_driver(driver); |
| 526 | return -ENOMEM; |
| 527 | } |
| 528 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 529 | } |
| 530 | INIT_LIST_HEAD(&sclp_tty_outqueue); |
| 531 | spin_lock_init(&sclp_tty_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | init_timer(&sclp_tty_timer); |
| 533 | sclp_ttybuf = NULL; |
| 534 | sclp_tty_buffer_count = 0; |
| 535 | if (MACHINE_IS_VM) { |
| 536 | /* |
| 537 | * save 4 characters for the CPU number |
| 538 | * written at start of each line by VM/CP |
| 539 | */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 540 | sclp_tty_columns = 76; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* case input lines to lowercase */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 542 | sclp_tty_tolower = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | sclp_tty_chars_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | rc = sclp_register(&sclp_input_event); |
| 547 | if (rc) { |
| 548 | put_tty_driver(driver); |
| 549 | return rc; |
| 550 | } |
| 551 | |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 552 | tty_port_init(&sclp_port); |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | driver->driver_name = "sclp_line"; |
| 555 | driver->name = "sclp_line"; |
| 556 | driver->major = TTY_MAJOR; |
| 557 | driver->minor_start = 64; |
| 558 | driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 559 | driver->subtype = SYSTEM_TYPE_TTY; |
| 560 | driver->init_termios = tty_std_termios; |
| 561 | driver->init_termios.c_iflag = IGNBRK | IGNPAR; |
Martin Schwidefsky | b8a2bbd | 2014-08-13 14:24:58 +0200 | [diff] [blame] | 562 | driver->init_termios.c_oflag = ONLCR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | driver->init_termios.c_lflag = ISIG | ECHO; |
| 564 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 565 | tty_set_operations(driver, &sclp_ops); |
Jiri Slaby | b19e2ca | 2012-08-07 21:47:51 +0200 | [diff] [blame] | 566 | tty_port_link_device(&sclp_port, driver, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | rc = tty_register_driver(driver); |
| 568 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | put_tty_driver(driver); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 570 | tty_port_destroy(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return rc; |
| 572 | } |
| 573 | sclp_tty_driver = driver; |
| 574 | return 0; |
| 575 | } |
| 576 | module_init(sclp_tty_init); |