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; |
| 68 | tty->low_latency = 0; |
| 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 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 110 | struct tty_struct *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | unsigned long flags; |
| 112 | void *page; |
| 113 | |
| 114 | do { |
| 115 | page = sclp_unmake_buffer(buffer); |
| 116 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 117 | /* Remove buffer from outqueue */ |
| 118 | list_del(&buffer->list); |
| 119 | sclp_tty_buffer_count--; |
| 120 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 121 | /* Check if there is a pending buffer on the out queue. */ |
| 122 | buffer = NULL; |
| 123 | if (!list_empty(&sclp_tty_outqueue)) |
| 124 | buffer = list_entry(sclp_tty_outqueue.next, |
| 125 | struct sclp_buffer, list); |
| 126 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 127 | } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* check if the tty needs a wake up call */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 129 | tty = tty_port_tty_get(&sclp_port); |
| 130 | if (tty != NULL) { |
| 131 | tty_wakeup(tty); |
| 132 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | static inline void |
| 137 | __sclp_ttybuf_emit(struct sclp_buffer *buffer) |
| 138 | { |
| 139 | unsigned long flags; |
| 140 | int count; |
| 141 | int rc; |
| 142 | |
| 143 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 144 | list_add_tail(&buffer->list, &sclp_tty_outqueue); |
| 145 | count = sclp_tty_buffer_count++; |
| 146 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 147 | if (count) |
| 148 | return; |
| 149 | rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback); |
| 150 | if (rc) |
| 151 | sclp_ttybuf_callback(buffer, rc); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * When this routine is called from the timer then we flush the |
| 156 | * temporary write buffer. |
| 157 | */ |
| 158 | static void |
| 159 | sclp_tty_timeout(unsigned long data) |
| 160 | { |
| 161 | unsigned long flags; |
| 162 | struct sclp_buffer *buf; |
| 163 | |
| 164 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 165 | buf = sclp_ttybuf; |
| 166 | sclp_ttybuf = NULL; |
| 167 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 168 | |
| 169 | if (buf != NULL) { |
| 170 | __sclp_ttybuf_emit(buf); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Write a string to the sclp tty. |
| 176 | */ |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 177 | 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] | 178 | { |
| 179 | unsigned long flags; |
| 180 | void *page; |
| 181 | int written; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 182 | int overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct sclp_buffer *buf; |
| 184 | |
| 185 | if (count <= 0) |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 186 | return 0; |
| 187 | overall_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 189 | do { |
| 190 | /* Create a sclp output buffer if none exists yet */ |
| 191 | if (sclp_ttybuf == NULL) { |
| 192 | while (list_empty(&sclp_tty_pages)) { |
| 193 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 194 | if (may_fail) |
| 195 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | else |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 197 | sclp_sync_wait(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 199 | } |
| 200 | page = sclp_tty_pages.next; |
| 201 | list_del((struct list_head *) page); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 202 | sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns, |
| 203 | SPACES_PER_TAB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | /* try to write the string to the current output buffer */ |
| 206 | written = sclp_write(sclp_ttybuf, str, count); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 207 | overall_written += written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (written == count) |
| 209 | break; |
| 210 | /* |
| 211 | * Not all characters could be written to the current |
| 212 | * output buffer. Emit the buffer, create a new buffer |
| 213 | * and then output the rest of the string. |
| 214 | */ |
| 215 | buf = sclp_ttybuf; |
| 216 | sclp_ttybuf = NULL; |
| 217 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 218 | __sclp_ttybuf_emit(buf); |
| 219 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 220 | str += written; |
| 221 | count -= written; |
| 222 | } while (count > 0); |
| 223 | /* Setup timer to output current console buffer after 1/10 second */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 224 | if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) && |
| 225 | !timer_pending(&sclp_tty_timer)) { |
| 226 | init_timer(&sclp_tty_timer); |
| 227 | sclp_tty_timer.function = sclp_tty_timeout; |
| 228 | sclp_tty_timer.data = 0UL; |
| 229 | sclp_tty_timer.expires = jiffies + HZ/10; |
| 230 | add_timer(&sclp_tty_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 233 | out: |
| 234 | return overall_written; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
| 238 | * This routine is called by the kernel to write a series of characters to the |
| 239 | * tty device. The characters may come from user space or kernel space. This |
| 240 | * routine will return the number of characters actually accepted for writing. |
| 241 | */ |
| 242 | static int |
| 243 | sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 244 | { |
| 245 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 246 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | sclp_tty_chars_count = 0; |
| 248 | } |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 249 | return sclp_tty_write_string(buf, count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * This routine is called by the kernel to write a single character to the tty |
| 254 | * device. If the kernel uses this routine, it must call the flush_chars() |
| 255 | * routine (if defined) when it is done stuffing characters into the driver. |
| 256 | * |
| 257 | * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. |
| 258 | * If the given character is a '\n' the contents of the SCLP write buffer |
| 259 | * - including previous characters from sclp_tty_put_char() and strings from |
| 260 | * sclp_write() without final '\n' - will be written. |
| 261 | */ |
Alan Cox | 9e7c9a1 | 2008-04-30 00:54:00 -0700 | [diff] [blame] | 262 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) |
| 264 | { |
| 265 | sclp_tty_chars[sclp_tty_chars_count++] = ch; |
| 266 | if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 267 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | sclp_tty_chars_count = 0; |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 269 | } |
| 270 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * This routine is called by the kernel after it has written a series of |
| 275 | * characters to the tty device using put_char(). |
| 276 | */ |
| 277 | static void |
| 278 | sclp_tty_flush_chars(struct tty_struct *tty) |
| 279 | { |
| 280 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 281 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | sclp_tty_chars_count = 0; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * This routine returns the number of characters in the write buffer of the |
| 288 | * SCLP driver. The provided number includes all characters that are stored |
| 289 | * in the SCCB (will be written next time the SCLP is not busy) as well as |
| 290 | * characters in the write buffer (will not be written as long as there is a |
| 291 | * final line feed missing). |
| 292 | */ |
| 293 | static int |
| 294 | sclp_tty_chars_in_buffer(struct tty_struct *tty) |
| 295 | { |
| 296 | unsigned long flags; |
| 297 | struct list_head *l; |
| 298 | struct sclp_buffer *t; |
| 299 | int count; |
| 300 | |
| 301 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 302 | count = 0; |
| 303 | if (sclp_ttybuf != NULL) |
| 304 | count = sclp_chars_in_buffer(sclp_ttybuf); |
| 305 | list_for_each(l, &sclp_tty_outqueue) { |
| 306 | t = list_entry(l, struct sclp_buffer, list); |
| 307 | count += sclp_chars_in_buffer(t); |
| 308 | } |
| 309 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 310 | return count; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * removes all content from buffers of low level driver |
| 315 | */ |
| 316 | static void |
| 317 | sclp_tty_flush_buffer(struct tty_struct *tty) |
| 318 | { |
| 319 | if (sclp_tty_chars_count > 0) { |
Heiko Carstens | 5e34599 | 2008-07-14 09:59:46 +0200 | [diff] [blame] | 320 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | sclp_tty_chars_count = 0; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * push input to tty |
| 327 | */ |
| 328 | static void |
| 329 | sclp_tty_input(unsigned char* buf, unsigned int count) |
| 330 | { |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 331 | struct tty_struct *tty = tty_port_tty_get(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | unsigned int cchar; |
| 333 | |
| 334 | /* |
| 335 | * If this tty driver is currently closed |
| 336 | * then throw the received input away. |
| 337 | */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 338 | if (tty == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 340 | cchar = ctrlchar_handle(buf, count, tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | switch (cchar & CTRLCHAR_MASK) { |
| 342 | case CTRLCHAR_SYSRQ: |
| 343 | break; |
| 344 | case CTRLCHAR_CTRL: |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 345 | tty_insert_flip_char(tty, cchar, TTY_NORMAL); |
| 346 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | break; |
| 348 | case CTRLCHAR_NONE: |
| 349 | /* send (normal) input to line discipline */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (count < 2 || |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 351 | (strncmp((const char *) buf + count - 2, "^n", 2) && |
| 352 | strncmp((const char *) buf + count - 2, "\252n", 2))) { |
| 353 | /* add the auto \n */ |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 354 | tty_insert_flip_string(tty, buf, count); |
| 355 | tty_insert_flip_char(tty, '\n', TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } else |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 357 | tty_insert_flip_string(tty, buf, count - 2); |
| 358 | tty_flip_buffer_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | break; |
| 360 | } |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 361 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* |
| 365 | * get a EBCDIC string in upper/lower case, |
| 366 | * find out characters in lower/upper case separated by a special character, |
| 367 | * modifiy original string, |
| 368 | * returns length of resulting string |
| 369 | */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 370 | static int sclp_switch_cases(unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
| 372 | unsigned char *ip, *op; |
| 373 | int toggle; |
| 374 | |
| 375 | /* initially changing case is off */ |
| 376 | toggle = 0; |
| 377 | ip = op = buf; |
| 378 | while (count-- > 0) { |
| 379 | /* compare with special character */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 380 | if (*ip == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | /* followed by another special character? */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 382 | if (count && ip[1] == CASE_DELIMITER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* |
| 384 | * ... then put a single copy of the special |
| 385 | * character to the output string |
| 386 | */ |
| 387 | *op++ = *ip++; |
| 388 | count--; |
| 389 | } else |
| 390 | /* |
| 391 | * ... special character follower by a normal |
| 392 | * character toggles the case change behaviour |
| 393 | */ |
| 394 | toggle = ~toggle; |
| 395 | /* skip special character */ |
| 396 | ip++; |
| 397 | } else |
| 398 | /* not the special character */ |
| 399 | if (toggle) |
| 400 | /* but case switching is on */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 401 | if (sclp_tty_tolower) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | /* switch to uppercase */ |
| 403 | *op++ = _ebc_toupper[(int) *ip++]; |
| 404 | else |
| 405 | /* switch to lowercase */ |
| 406 | *op++ = _ebc_tolower[(int) *ip++]; |
| 407 | else |
| 408 | /* no case switching, copy the character */ |
| 409 | *op++ = *ip++; |
| 410 | } |
| 411 | /* return length of reformatted string. */ |
| 412 | return op - buf; |
| 413 | } |
| 414 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 415 | static void sclp_get_input(struct gds_subvector *sv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 417 | unsigned char *str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | int count; |
| 419 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 420 | str = (unsigned char *) (sv + 1); |
| 421 | count = sv->length - sizeof(*sv); |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 422 | if (sclp_tty_tolower) |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 423 | EBC_TOLOWER(str, count); |
| 424 | count = sclp_switch_cases(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* convert EBCDIC to ASCII (modify original input in SCCB) */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 426 | sclp_ebcasc_str(str, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* transfer input to high level driver */ |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 429 | sclp_tty_input(str, count); |
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_selfdeftextmsg(struct gds_subvector *sv) |
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 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 436 | end = (void *) sv + sv->length; |
| 437 | for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length) |
| 438 | if (sv->key == 0x30) |
| 439 | sclp_get_input(sv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 442 | static inline void sclp_eval_textcmd(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 444 | struct gds_subvector *sv; |
| 445 | void *end; |
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 | end = (void *) v + v->length; |
| 448 | for (sv = (struct gds_subvector *) (v + 1); |
| 449 | (void *) sv < end; sv = (void *) sv + sv->length) |
| 450 | if (sv->key == GDS_KEY_SELFDEFTEXTMSG) |
| 451 | sclp_eval_selfdeftextmsg(sv); |
| 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 455 | static inline void sclp_eval_cpmsu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 457 | void *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 459 | end = (void *) v + v->length; |
| 460 | for (v = v + 1; (void *) v < end; v = (void *) v + v->length) |
| 461 | if (v->gds_id == GDS_ID_TEXTCMD) |
| 462 | sclp_eval_textcmd(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 466 | static inline void sclp_eval_mdsmu(struct gds_vector *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 468 | v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU); |
| 469 | if (v) |
| 470 | sclp_eval_cpmsu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 473 | static void sclp_tty_receiver(struct evbuf_header *evbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 475 | struct gds_vector *v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Martin Schwidefsky | 30c2df5 | 2011-05-23 10:24:42 +0200 | [diff] [blame] | 477 | v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length, |
| 478 | GDS_ID_MDSMU); |
| 479 | if (v) |
| 480 | sclp_eval_mdsmu(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static void |
| 484 | sclp_tty_state_change(struct sclp_register *reg) |
| 485 | { |
| 486 | } |
| 487 | |
| 488 | static struct sclp_register sclp_input_event = |
| 489 | { |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame] | 490 | .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | .state_change_fn = sclp_tty_state_change, |
| 492 | .receiver_fn = sclp_tty_receiver |
| 493 | }; |
| 494 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 495 | static const struct tty_operations sclp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | .open = sclp_tty_open, |
| 497 | .close = sclp_tty_close, |
| 498 | .write = sclp_tty_write, |
| 499 | .put_char = sclp_tty_put_char, |
| 500 | .flush_chars = sclp_tty_flush_chars, |
| 501 | .write_room = sclp_tty_write_room, |
| 502 | .chars_in_buffer = sclp_tty_chars_in_buffer, |
| 503 | .flush_buffer = sclp_tty_flush_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | }; |
| 505 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 506 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | sclp_tty_init(void) |
| 508 | { |
| 509 | struct tty_driver *driver; |
| 510 | void *page; |
| 511 | int i; |
| 512 | int rc; |
| 513 | |
| 514 | if (!CONSOLE_IS_SCLP) |
| 515 | return 0; |
| 516 | driver = alloc_tty_driver(1); |
| 517 | if (!driver) |
| 518 | return -ENOMEM; |
| 519 | |
| 520 | rc = sclp_rw_init(); |
| 521 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | put_tty_driver(driver); |
| 523 | return rc; |
| 524 | } |
| 525 | /* Allocate pages for output buffering */ |
| 526 | INIT_LIST_HEAD(&sclp_tty_pages); |
| 527 | for (i = 0; i < MAX_KMEM_PAGES; i++) { |
| 528 | page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 529 | if (page == NULL) { |
| 530 | put_tty_driver(driver); |
| 531 | return -ENOMEM; |
| 532 | } |
| 533 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 534 | } |
| 535 | INIT_LIST_HEAD(&sclp_tty_outqueue); |
| 536 | spin_lock_init(&sclp_tty_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | init_timer(&sclp_tty_timer); |
| 538 | sclp_ttybuf = NULL; |
| 539 | sclp_tty_buffer_count = 0; |
| 540 | if (MACHINE_IS_VM) { |
| 541 | /* |
| 542 | * save 4 characters for the CPU number |
| 543 | * written at start of each line by VM/CP |
| 544 | */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 545 | sclp_tty_columns = 76; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | /* case input lines to lowercase */ |
Heiko Carstens | 095761d | 2008-07-14 09:59:45 +0200 | [diff] [blame] | 547 | sclp_tty_tolower = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | sclp_tty_chars_count = 0; |
Jiri Slaby | 0ea63da | 2012-04-02 13:54:12 +0200 | [diff] [blame] | 550 | tty_port_init(&sclp_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | rc = sclp_register(&sclp_input_event); |
| 553 | if (rc) { |
| 554 | put_tty_driver(driver); |
| 555 | return rc; |
| 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | driver->driver_name = "sclp_line"; |
| 559 | driver->name = "sclp_line"; |
| 560 | driver->major = TTY_MAJOR; |
| 561 | driver->minor_start = 64; |
| 562 | driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 563 | driver->subtype = SYSTEM_TYPE_TTY; |
| 564 | driver->init_termios = tty_std_termios; |
| 565 | driver->init_termios.c_iflag = IGNBRK | IGNPAR; |
| 566 | driver->init_termios.c_oflag = ONLCR | XTABS; |
| 567 | driver->init_termios.c_lflag = ISIG | ECHO; |
| 568 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 569 | tty_set_operations(driver, &sclp_ops); |
Jiri Slaby | b19e2ca | 2012-08-07 21:47:51 +0200 | [diff] [blame] | 570 | tty_port_link_device(&sclp_port, driver, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |