Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/kmod.h> |
| 13 | #include <linux/tty.h> |
| 14 | #include <linux/tty_driver.h> |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 15 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/err.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* |
| 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. */ |
| 39 | static spinlock_t sclp_tty_lock; |
| 40 | /* List of free pages that can be used for console output buffering. */ |
| 41 | static struct list_head sclp_tty_pages; |
| 42 | /* List of full struct sclp_buffer structures ready for output. */ |
| 43 | static struct list_head sclp_tty_outqueue; |
| 44 | /* Counter how many buffers are emitted. */ |
| 45 | static int sclp_tty_buffer_count; |
| 46 | /* Pointer to current console buffer. */ |
| 47 | static struct sclp_buffer *sclp_ttybuf; |
| 48 | /* Timer for delayed output of console messages. */ |
| 49 | static struct timer_list sclp_tty_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 51 | static struct tty_port sclp_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE]; |
| 53 | static unsigned short int sclp_tty_chars_count; |
| 54 | |
| 55 | struct tty_driver *sclp_tty_driver; |
| 56 | |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 57 | static int sclp_tty_tolower; |
| 58 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* This routine is called whenever we try to open a SCLP terminal. */ |
| 64 | static int |
| 65 | sclp_tty_open(struct tty_struct *tty, struct file *filp) |
| 66 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 67 | tty_port_tty_set(&sclp_port, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | 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. */ |
| 74 | static void |
| 75 | sclp_tty_close(struct tty_struct *tty, struct file *filp) |
| 76 | { |
| 77 | if (tty->count > 1) |
| 78 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 79 | tty_port_tty_set(&sclp_port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* |
| 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 | */ |
| 91 | static int |
| 92 | sclp_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 | |
| 108 | static void |
| 109 | sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc) |
| 110 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 111 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /* check if the tty needs a wake up call */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 130 | tty = tty_port_tty_get(&sclp_port); |
| 131 | if (tty != NULL) { |
| 132 | tty_wakeup(tty); |
| 133 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | static 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 | */ |
| 159 | static void |
| 160 | sclp_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 Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 178 | 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] | 179 | { |
| 180 | unsigned long flags; |
| 181 | void *page; |
| 182 | int written; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 183 | int overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | struct sclp_buffer *buf; |
| 185 | |
| 186 | if (count <= 0) |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | overall_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | 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 Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 195 | if (may_fail) |
| 196 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | else |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 198 | sclp_sync_wait(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 200 | } |
| 201 | page = sclp_tty_pages.next; |
| 202 | list_del((struct list_head *) page); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 203 | sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns, |
| 204 | SPACES_PER_TAB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | /* try to write the string to the current output buffer */ |
| 207 | written = sclp_write(sclp_ttybuf, str, count); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 208 | overall_written += written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | 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 Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 225 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 234 | out: |
| 235 | return overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 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 | */ |
| 243 | static int |
| 244 | sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 245 | { |
| 246 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 247 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | sclp_tty_chars_count = 0; |
| 249 | } |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 250 | return sclp_tty_write_string(buf, count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 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 Cox | 9e7c9a1 | 2008-04-30 00:54:00 -0700 | [diff] [blame] | 263 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | sclp_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 Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 268 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | sclp_tty_chars_count = 0; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 270 | } |
| 271 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 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 | */ |
| 278 | static void |
| 279 | sclp_tty_flush_chars(struct tty_struct *tty) |
| 280 | { |
| 281 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 282 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 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 | */ |
| 294 | static int |
| 295 | sclp_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 | */ |
| 317 | static void |
| 318 | sclp_tty_flush_buffer(struct tty_struct *tty) |
| 319 | { |
| 320 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 321 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | sclp_tty_chars_count = 0; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * push input to tty |
| 328 | */ |
| 329 | static void |
| 330 | sclp_tty_input(unsigned char* buf, unsigned int count) |
| 331 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 332 | struct tty_struct *tty = tty_port_tty_get(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | unsigned int cchar; |
| 334 | |
| 335 | /* |
| 336 | * If this tty driver is currently closed |
| 337 | * then throw the received input away. |
| 338 | */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 339 | if (tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 341 | cchar = ctrlchar_handle(buf, count, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | switch (cchar & CTRLCHAR_MASK) { |
| 343 | case CTRLCHAR_SYSRQ: |
| 344 | break; |
| 345 | case CTRLCHAR_CTRL: |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 346 | tty_insert_flip_char(tty, cchar, TTY_NORMAL); |
| 347 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | break; |
| 349 | case CTRLCHAR_NONE: |
| 350 | /* send (normal) input to line discipline */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (count < 2 || |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 352 | (strncmp((const char *) buf + count - 2, "^n", 2) && |
| 353 | strncmp((const char *) buf + count - 2, "\252n", 2))) { |
| 354 | /* add the auto \n */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 355 | tty_insert_flip_string(tty, buf, count); |
| 356 | tty_insert_flip_char(tty, '\n', TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } else |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 358 | tty_insert_flip_string(tty, buf, count - 2); |
| 359 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | break; |
| 361 | } |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 362 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| 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 Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 371 | static int sclp_switch_cases(unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 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 Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 381 | if (*ip == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | /* followed by another special character? */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 383 | if (count && ip[1] == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* |
| 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 Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 402 | if (sclp_tty_tolower) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | /* 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 Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 416 | static void sclp_get_input(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 418 | unsigned char *str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | int count; |
| 420 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 421 | str = (unsigned char *) (sv + 1); |
| 422 | count = sv->length - sizeof(*sv); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 423 | if (sclp_tty_tolower) |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 424 | EBC_TOLOWER(str, count); |
| 425 | count = sclp_switch_cases(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /* convert EBCDIC to ASCII (modify original input in SCCB) */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 427 | sclp_ebcasc_str(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | /* transfer input to high level driver */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 430 | sclp_tty_input(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 433 | static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 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 *) 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 443 | static inline void sclp_eval_textcmd(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 445 | struct gds_subvector *sv; |
| 446 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 448 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 456 | static inline void sclp_eval_cpmsu(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 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 460 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 467 | static inline void sclp_eval_mdsmu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 469 | v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU); |
| 470 | if (v) |
| 471 | sclp_eval_cpmsu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 474 | static void sclp_tty_receiver(struct evbuf_header *evbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 476 | struct gds_vector *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 478 | v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length, |
| 479 | GDS_ID_MDSMU); |
| 480 | if (v) |
| 481 | sclp_eval_mdsmu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void |
| 485 | sclp_tty_state_change(struct sclp_register *reg) |
| 486 | { |
| 487 | } |
| 488 | |
| 489 | static struct sclp_register sclp_input_event = |
| 490 | { |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame] | 491 | .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | .state_change_fn = sclp_tty_state_change, |
| 493 | .receiver_fn = sclp_tty_receiver |
| 494 | }; |
| 495 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 496 | static const struct tty_operations sclp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | }; |
| 506 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 507 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | sclp_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | 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 Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 546 | sclp_tty_columns = 76; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | /* case input lines to lowercase */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 548 | sclp_tty_tolower = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | sclp_tty_chars_count = 0; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame^] | 551 | tty_port_init(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | rc = sclp_register(&sclp_input_event); |
| 554 | if (rc) { |
| 555 | put_tty_driver(driver); |
| 556 | return rc; |
| 557 | } |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | put_tty_driver(driver); |
| 574 | return rc; |
| 575 | } |
| 576 | sclp_tty_driver = driver; |
| 577 | return 0; |
| 578 | } |
| 579 | module_init(sclp_tty_init); |