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/wait.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <asm/uaccess.h> |
| 22 | |
| 23 | #include "ctrlchar.h" |
| 24 | #include "sclp.h" |
| 25 | #include "sclp_rw.h" |
| 26 | #include "sclp_tty.h" |
| 27 | |
| 28 | #define SCLP_TTY_PRINT_HEADER "sclp tty driver: " |
| 29 | |
| 30 | /* |
| 31 | * size of a buffer that collects single characters coming in |
| 32 | * via sclp_tty_put_char() |
| 33 | */ |
| 34 | #define SCLP_TTY_BUF_SIZE 512 |
| 35 | |
| 36 | /* |
| 37 | * There is exactly one SCLP terminal, so we can keep things simple |
| 38 | * and allocate all variables statically. |
| 39 | */ |
| 40 | |
| 41 | /* Lock to guard over changes to global variables. */ |
| 42 | static spinlock_t sclp_tty_lock; |
| 43 | /* List of free pages that can be used for console output buffering. */ |
| 44 | static struct list_head sclp_tty_pages; |
| 45 | /* List of full struct sclp_buffer structures ready for output. */ |
| 46 | static struct list_head sclp_tty_outqueue; |
| 47 | /* Counter how many buffers are emitted. */ |
| 48 | static int sclp_tty_buffer_count; |
| 49 | /* Pointer to current console buffer. */ |
| 50 | static struct sclp_buffer *sclp_ttybuf; |
| 51 | /* Timer for delayed output of console messages. */ |
| 52 | static struct timer_list sclp_tty_timer; |
| 53 | /* Waitqueue to wait for buffers to get empty. */ |
| 54 | static wait_queue_head_t sclp_tty_waitq; |
| 55 | |
| 56 | static struct tty_struct *sclp_tty; |
| 57 | static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE]; |
| 58 | static unsigned short int sclp_tty_chars_count; |
| 59 | |
| 60 | struct tty_driver *sclp_tty_driver; |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static struct sclp_ioctls sclp_ioctls; |
| 63 | static struct sclp_ioctls sclp_ioctls_init = |
| 64 | { |
| 65 | 8, /* 1 hor. tab. = 8 spaces */ |
| 66 | 0, /* no echo of input by this driver */ |
| 67 | 80, /* 80 characters/line */ |
| 68 | 1, /* write after 1/10 s without final new line */ |
| 69 | MAX_KMEM_PAGES, /* quick fix: avoid __alloc_pages */ |
| 70 | MAX_KMEM_PAGES, /* take 32/64 pages from kernel memory, */ |
| 71 | 0, /* do not convert to lower case */ |
| 72 | 0x6c /* to seprate upper and lower case */ |
| 73 | /* ('%' in EBCDIC) */ |
| 74 | }; |
| 75 | |
| 76 | /* This routine is called whenever we try to open a SCLP terminal. */ |
| 77 | static int |
| 78 | sclp_tty_open(struct tty_struct *tty, struct file *filp) |
| 79 | { |
| 80 | sclp_tty = tty; |
| 81 | tty->driver_data = NULL; |
| 82 | tty->low_latency = 0; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* This routine is called when the SCLP terminal is closed. */ |
| 87 | static void |
| 88 | sclp_tty_close(struct tty_struct *tty, struct file *filp) |
| 89 | { |
| 90 | if (tty->count > 1) |
| 91 | return; |
| 92 | sclp_tty = NULL; |
| 93 | } |
| 94 | |
| 95 | /* execute commands to control the i/o behaviour of the SCLP tty at runtime */ |
| 96 | static int |
| 97 | sclp_tty_ioctl(struct tty_struct *tty, struct file * file, |
| 98 | unsigned int cmd, unsigned long arg) |
| 99 | { |
| 100 | unsigned long flags; |
| 101 | unsigned int obuf; |
| 102 | int check; |
| 103 | int rc; |
| 104 | |
| 105 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 106 | return -EIO; |
| 107 | rc = 0; |
| 108 | check = 0; |
| 109 | switch (cmd) { |
| 110 | case TIOCSCLPSHTAB: |
| 111 | /* set width of horizontal tab */ |
| 112 | if (get_user(sclp_ioctls.htab, (unsigned short __user *) arg)) |
| 113 | rc = -EFAULT; |
| 114 | else |
| 115 | check = 1; |
| 116 | break; |
| 117 | case TIOCSCLPGHTAB: |
| 118 | /* get width of horizontal tab */ |
| 119 | if (put_user(sclp_ioctls.htab, (unsigned short __user *) arg)) |
| 120 | rc = -EFAULT; |
| 121 | break; |
| 122 | case TIOCSCLPSECHO: |
| 123 | /* enable/disable echo of input */ |
| 124 | if (get_user(sclp_ioctls.echo, (unsigned char __user *) arg)) |
| 125 | rc = -EFAULT; |
| 126 | break; |
| 127 | case TIOCSCLPGECHO: |
| 128 | /* Is echo of input enabled ? */ |
| 129 | if (put_user(sclp_ioctls.echo, (unsigned char __user *) arg)) |
| 130 | rc = -EFAULT; |
| 131 | break; |
| 132 | case TIOCSCLPSCOLS: |
| 133 | /* set number of columns for output */ |
| 134 | if (get_user(sclp_ioctls.columns, (unsigned short __user *) arg)) |
| 135 | rc = -EFAULT; |
| 136 | else |
| 137 | check = 1; |
| 138 | break; |
| 139 | case TIOCSCLPGCOLS: |
| 140 | /* get number of columns for output */ |
| 141 | if (put_user(sclp_ioctls.columns, (unsigned short __user *) arg)) |
| 142 | rc = -EFAULT; |
| 143 | break; |
| 144 | case TIOCSCLPSNL: |
| 145 | /* enable/disable writing without final new line character */ |
| 146 | if (get_user(sclp_ioctls.final_nl, (signed char __user *) arg)) |
| 147 | rc = -EFAULT; |
| 148 | break; |
| 149 | case TIOCSCLPGNL: |
| 150 | /* Is writing without final new line character enabled ? */ |
| 151 | if (put_user(sclp_ioctls.final_nl, (signed char __user *) arg)) |
| 152 | rc = -EFAULT; |
| 153 | break; |
| 154 | case TIOCSCLPSOBUF: |
| 155 | /* |
| 156 | * set the maximum buffers size for output, will be rounded |
| 157 | * up to next 4kB boundary and stored as number of SCCBs |
| 158 | * (4kB Buffers) limitation: 256 x 4kB |
| 159 | */ |
| 160 | if (get_user(obuf, (unsigned int __user *) arg) == 0) { |
| 161 | if (obuf & 0xFFF) |
| 162 | sclp_ioctls.max_sccb = (obuf >> 12) + 1; |
| 163 | else |
| 164 | sclp_ioctls.max_sccb = (obuf >> 12); |
| 165 | } else |
| 166 | rc = -EFAULT; |
| 167 | break; |
| 168 | case TIOCSCLPGOBUF: |
| 169 | /* get the maximum buffers size for output */ |
| 170 | obuf = sclp_ioctls.max_sccb << 12; |
| 171 | if (put_user(obuf, (unsigned int __user *) arg)) |
| 172 | rc = -EFAULT; |
| 173 | break; |
| 174 | case TIOCSCLPGKBUF: |
| 175 | /* get the number of buffers got from kernel at startup */ |
| 176 | if (put_user(sclp_ioctls.kmem_sccb, (unsigned short __user *) arg)) |
| 177 | rc = -EFAULT; |
| 178 | break; |
| 179 | case TIOCSCLPSCASE: |
| 180 | /* enable/disable conversion from upper to lower case */ |
| 181 | if (get_user(sclp_ioctls.tolower, (unsigned char __user *) arg)) |
| 182 | rc = -EFAULT; |
| 183 | break; |
| 184 | case TIOCSCLPGCASE: |
| 185 | /* Is conversion from upper to lower case of input enabled? */ |
| 186 | if (put_user(sclp_ioctls.tolower, (unsigned char __user *) arg)) |
| 187 | rc = -EFAULT; |
| 188 | break; |
| 189 | case TIOCSCLPSDELIM: |
| 190 | /* |
| 191 | * set special character used for separating upper and |
| 192 | * lower case, 0x00 disables this feature |
| 193 | */ |
| 194 | if (get_user(sclp_ioctls.delim, (unsigned char __user *) arg)) |
| 195 | rc = -EFAULT; |
| 196 | break; |
| 197 | case TIOCSCLPGDELIM: |
| 198 | /* |
| 199 | * get special character used for separating upper and |
| 200 | * lower case, 0x00 disables this feature |
| 201 | */ |
| 202 | if (put_user(sclp_ioctls.delim, (unsigned char __user *) arg)) |
| 203 | rc = -EFAULT; |
| 204 | break; |
| 205 | case TIOCSCLPSINIT: |
| 206 | /* set initial (default) sclp ioctls */ |
| 207 | sclp_ioctls = sclp_ioctls_init; |
| 208 | check = 1; |
| 209 | break; |
| 210 | default: |
| 211 | rc = -ENOIOCTLCMD; |
| 212 | break; |
| 213 | } |
| 214 | if (check) { |
| 215 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 216 | if (sclp_ttybuf != NULL) { |
| 217 | sclp_set_htab(sclp_ttybuf, sclp_ioctls.htab); |
| 218 | sclp_set_columns(sclp_ttybuf, sclp_ioctls.columns); |
| 219 | } |
| 220 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 221 | } |
| 222 | return rc; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * This routine returns the numbers of characters the tty driver |
| 227 | * will accept for queuing to be written. This number is subject |
| 228 | * to change as output buffers get emptied, or if the output flow |
| 229 | * control is acted. This is not an exact number because not every |
| 230 | * character needs the same space in the sccb. The worst case is |
| 231 | * a string of newlines. Every newlines creates a new mto which |
| 232 | * needs 8 bytes. |
| 233 | */ |
| 234 | static int |
| 235 | sclp_tty_write_room (struct tty_struct *tty) |
| 236 | { |
| 237 | unsigned long flags; |
| 238 | struct list_head *l; |
| 239 | int count; |
| 240 | |
| 241 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 242 | count = 0; |
| 243 | if (sclp_ttybuf != NULL) |
| 244 | count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto); |
| 245 | list_for_each(l, &sclp_tty_pages) |
| 246 | count += NR_EMPTY_MTO_PER_SCCB; |
| 247 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 248 | return count; |
| 249 | } |
| 250 | |
| 251 | static void |
| 252 | sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc) |
| 253 | { |
| 254 | unsigned long flags; |
| 255 | void *page; |
| 256 | |
| 257 | do { |
| 258 | page = sclp_unmake_buffer(buffer); |
| 259 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 260 | /* Remove buffer from outqueue */ |
| 261 | list_del(&buffer->list); |
| 262 | sclp_tty_buffer_count--; |
| 263 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 264 | /* Check if there is a pending buffer on the out queue. */ |
| 265 | buffer = NULL; |
| 266 | if (!list_empty(&sclp_tty_outqueue)) |
| 267 | buffer = list_entry(sclp_tty_outqueue.next, |
| 268 | struct sclp_buffer, list); |
| 269 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 270 | } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback)); |
| 271 | wake_up(&sclp_tty_waitq); |
| 272 | /* check if the tty needs a wake up call */ |
| 273 | if (sclp_tty != NULL) { |
| 274 | tty_wakeup(sclp_tty); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static inline void |
| 279 | __sclp_ttybuf_emit(struct sclp_buffer *buffer) |
| 280 | { |
| 281 | unsigned long flags; |
| 282 | int count; |
| 283 | int rc; |
| 284 | |
| 285 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 286 | list_add_tail(&buffer->list, &sclp_tty_outqueue); |
| 287 | count = sclp_tty_buffer_count++; |
| 288 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 289 | if (count) |
| 290 | return; |
| 291 | rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback); |
| 292 | if (rc) |
| 293 | sclp_ttybuf_callback(buffer, rc); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * When this routine is called from the timer then we flush the |
| 298 | * temporary write buffer. |
| 299 | */ |
| 300 | static void |
| 301 | sclp_tty_timeout(unsigned long data) |
| 302 | { |
| 303 | unsigned long flags; |
| 304 | struct sclp_buffer *buf; |
| 305 | |
| 306 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 307 | buf = sclp_ttybuf; |
| 308 | sclp_ttybuf = NULL; |
| 309 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 310 | |
| 311 | if (buf != NULL) { |
| 312 | __sclp_ttybuf_emit(buf); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Write a string to the sclp tty. |
| 318 | */ |
| 319 | static void |
| 320 | sclp_tty_write_string(const unsigned char *str, int count) |
| 321 | { |
| 322 | unsigned long flags; |
| 323 | void *page; |
| 324 | int written; |
| 325 | struct sclp_buffer *buf; |
| 326 | |
| 327 | if (count <= 0) |
| 328 | return; |
| 329 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 330 | do { |
| 331 | /* Create a sclp output buffer if none exists yet */ |
| 332 | if (sclp_ttybuf == NULL) { |
| 333 | while (list_empty(&sclp_tty_pages)) { |
| 334 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 335 | if (in_interrupt()) |
| 336 | sclp_sync_wait(); |
| 337 | else |
| 338 | wait_event(sclp_tty_waitq, |
| 339 | !list_empty(&sclp_tty_pages)); |
| 340 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 341 | } |
| 342 | page = sclp_tty_pages.next; |
| 343 | list_del((struct list_head *) page); |
| 344 | sclp_ttybuf = sclp_make_buffer(page, |
| 345 | sclp_ioctls.columns, |
| 346 | sclp_ioctls.htab); |
| 347 | } |
| 348 | /* try to write the string to the current output buffer */ |
| 349 | written = sclp_write(sclp_ttybuf, str, count); |
| 350 | if (written == count) |
| 351 | break; |
| 352 | /* |
| 353 | * Not all characters could be written to the current |
| 354 | * output buffer. Emit the buffer, create a new buffer |
| 355 | * and then output the rest of the string. |
| 356 | */ |
| 357 | buf = sclp_ttybuf; |
| 358 | sclp_ttybuf = NULL; |
| 359 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 360 | __sclp_ttybuf_emit(buf); |
| 361 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 362 | str += written; |
| 363 | count -= written; |
| 364 | } while (count > 0); |
| 365 | /* Setup timer to output current console buffer after 1/10 second */ |
| 366 | if (sclp_ioctls.final_nl) { |
| 367 | if (sclp_ttybuf != NULL && |
| 368 | sclp_chars_in_buffer(sclp_ttybuf) != 0 && |
| 369 | !timer_pending(&sclp_tty_timer)) { |
| 370 | init_timer(&sclp_tty_timer); |
| 371 | sclp_tty_timer.function = sclp_tty_timeout; |
| 372 | sclp_tty_timer.data = 0UL; |
| 373 | sclp_tty_timer.expires = jiffies + HZ/10; |
| 374 | add_timer(&sclp_tty_timer); |
| 375 | } |
| 376 | } else { |
| 377 | if (sclp_ttybuf != NULL && |
| 378 | sclp_chars_in_buffer(sclp_ttybuf) != 0) { |
| 379 | buf = sclp_ttybuf; |
| 380 | sclp_ttybuf = NULL; |
| 381 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 382 | __sclp_ttybuf_emit(buf); |
| 383 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 384 | } |
| 385 | } |
| 386 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * This routine is called by the kernel to write a series of characters to the |
| 391 | * tty device. The characters may come from user space or kernel space. This |
| 392 | * routine will return the number of characters actually accepted for writing. |
| 393 | */ |
| 394 | static int |
| 395 | sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 396 | { |
| 397 | if (sclp_tty_chars_count > 0) { |
| 398 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); |
| 399 | sclp_tty_chars_count = 0; |
| 400 | } |
| 401 | sclp_tty_write_string(buf, count); |
| 402 | return count; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * This routine is called by the kernel to write a single character to the tty |
| 407 | * device. If the kernel uses this routine, it must call the flush_chars() |
| 408 | * routine (if defined) when it is done stuffing characters into the driver. |
| 409 | * |
| 410 | * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. |
| 411 | * If the given character is a '\n' the contents of the SCLP write buffer |
| 412 | * - including previous characters from sclp_tty_put_char() and strings from |
| 413 | * sclp_write() without final '\n' - will be written. |
| 414 | */ |
| 415 | static void |
| 416 | sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) |
| 417 | { |
| 418 | sclp_tty_chars[sclp_tty_chars_count++] = ch; |
| 419 | if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { |
| 420 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); |
| 421 | sclp_tty_chars_count = 0; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * This routine is called by the kernel after it has written a series of |
| 427 | * characters to the tty device using put_char(). |
| 428 | */ |
| 429 | static void |
| 430 | sclp_tty_flush_chars(struct tty_struct *tty) |
| 431 | { |
| 432 | if (sclp_tty_chars_count > 0) { |
| 433 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); |
| 434 | sclp_tty_chars_count = 0; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * This routine returns the number of characters in the write buffer of the |
| 440 | * SCLP driver. The provided number includes all characters that are stored |
| 441 | * in the SCCB (will be written next time the SCLP is not busy) as well as |
| 442 | * characters in the write buffer (will not be written as long as there is a |
| 443 | * final line feed missing). |
| 444 | */ |
| 445 | static int |
| 446 | sclp_tty_chars_in_buffer(struct tty_struct *tty) |
| 447 | { |
| 448 | unsigned long flags; |
| 449 | struct list_head *l; |
| 450 | struct sclp_buffer *t; |
| 451 | int count; |
| 452 | |
| 453 | spin_lock_irqsave(&sclp_tty_lock, flags); |
| 454 | count = 0; |
| 455 | if (sclp_ttybuf != NULL) |
| 456 | count = sclp_chars_in_buffer(sclp_ttybuf); |
| 457 | list_for_each(l, &sclp_tty_outqueue) { |
| 458 | t = list_entry(l, struct sclp_buffer, list); |
| 459 | count += sclp_chars_in_buffer(t); |
| 460 | } |
| 461 | spin_unlock_irqrestore(&sclp_tty_lock, flags); |
| 462 | return count; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * removes all content from buffers of low level driver |
| 467 | */ |
| 468 | static void |
| 469 | sclp_tty_flush_buffer(struct tty_struct *tty) |
| 470 | { |
| 471 | if (sclp_tty_chars_count > 0) { |
| 472 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); |
| 473 | sclp_tty_chars_count = 0; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * push input to tty |
| 479 | */ |
| 480 | static void |
| 481 | sclp_tty_input(unsigned char* buf, unsigned int count) |
| 482 | { |
| 483 | unsigned int cchar; |
| 484 | |
| 485 | /* |
| 486 | * If this tty driver is currently closed |
| 487 | * then throw the received input away. |
| 488 | */ |
| 489 | if (sclp_tty == NULL) |
| 490 | return; |
| 491 | cchar = ctrlchar_handle(buf, count, sclp_tty); |
| 492 | switch (cchar & CTRLCHAR_MASK) { |
| 493 | case CTRLCHAR_SYSRQ: |
| 494 | break; |
| 495 | case CTRLCHAR_CTRL: |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 496 | tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | tty_flip_buffer_push(sclp_tty); |
| 498 | break; |
| 499 | case CTRLCHAR_NONE: |
| 500 | /* send (normal) input to line discipline */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (count < 2 || |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 502 | (strncmp((const char *) buf + count - 2, "^n", 2) && |
| 503 | strncmp((const char *) buf + count - 2, "\252n", 2))) { |
| 504 | /* add the auto \n */ |
| 505 | tty_insert_flip_string(sclp_tty, buf, count); |
| 506 | tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } else |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 508 | tty_insert_flip_string(sclp_tty, buf, count - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | tty_flip_buffer_push(sclp_tty); |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * get a EBCDIC string in upper/lower case, |
| 516 | * find out characters in lower/upper case separated by a special character, |
| 517 | * modifiy original string, |
| 518 | * returns length of resulting string |
| 519 | */ |
| 520 | static int |
| 521 | sclp_switch_cases(unsigned char *buf, int count, |
| 522 | unsigned char delim, int tolower) |
| 523 | { |
| 524 | unsigned char *ip, *op; |
| 525 | int toggle; |
| 526 | |
| 527 | /* initially changing case is off */ |
| 528 | toggle = 0; |
| 529 | ip = op = buf; |
| 530 | while (count-- > 0) { |
| 531 | /* compare with special character */ |
| 532 | if (*ip == delim) { |
| 533 | /* followed by another special character? */ |
| 534 | if (count && ip[1] == delim) { |
| 535 | /* |
| 536 | * ... then put a single copy of the special |
| 537 | * character to the output string |
| 538 | */ |
| 539 | *op++ = *ip++; |
| 540 | count--; |
| 541 | } else |
| 542 | /* |
| 543 | * ... special character follower by a normal |
| 544 | * character toggles the case change behaviour |
| 545 | */ |
| 546 | toggle = ~toggle; |
| 547 | /* skip special character */ |
| 548 | ip++; |
| 549 | } else |
| 550 | /* not the special character */ |
| 551 | if (toggle) |
| 552 | /* but case switching is on */ |
| 553 | if (tolower) |
| 554 | /* switch to uppercase */ |
| 555 | *op++ = _ebc_toupper[(int) *ip++]; |
| 556 | else |
| 557 | /* switch to lowercase */ |
| 558 | *op++ = _ebc_tolower[(int) *ip++]; |
| 559 | else |
| 560 | /* no case switching, copy the character */ |
| 561 | *op++ = *ip++; |
| 562 | } |
| 563 | /* return length of reformatted string. */ |
| 564 | return op - buf; |
| 565 | } |
| 566 | |
| 567 | static void |
| 568 | sclp_get_input(unsigned char *start, unsigned char *end) |
| 569 | { |
| 570 | int count; |
| 571 | |
| 572 | count = end - start; |
| 573 | /* |
| 574 | * if set in ioctl convert EBCDIC to lower case |
| 575 | * (modify original input in SCCB) |
| 576 | */ |
| 577 | if (sclp_ioctls.tolower) |
| 578 | EBC_TOLOWER(start, count); |
| 579 | |
| 580 | /* |
| 581 | * if set in ioctl find out characters in lower or upper case |
| 582 | * (depends on current case) separated by a special character, |
| 583 | * works on EBCDIC |
| 584 | */ |
| 585 | if (sclp_ioctls.delim) |
| 586 | count = sclp_switch_cases(start, count, |
| 587 | sclp_ioctls.delim, |
| 588 | sclp_ioctls.tolower); |
| 589 | |
| 590 | /* convert EBCDIC to ASCII (modify original input in SCCB) */ |
| 591 | sclp_ebcasc_str(start, count); |
| 592 | |
| 593 | /* if set in ioctl write operators input to console */ |
| 594 | if (sclp_ioctls.echo) |
| 595 | sclp_tty_write(sclp_tty, start, count); |
| 596 | |
| 597 | /* transfer input to high level driver */ |
| 598 | sclp_tty_input(start, count); |
| 599 | } |
| 600 | |
| 601 | static inline struct gds_vector * |
| 602 | find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id) |
| 603 | { |
| 604 | struct gds_vector *vec; |
| 605 | |
| 606 | for (vec = start; vec < end; vec = (void *) vec + vec->length) |
| 607 | if (vec->gds_id == id) |
| 608 | return vec; |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | static inline struct gds_subvector * |
| 613 | find_gds_subvector(struct gds_subvector *start, |
| 614 | struct gds_subvector *end, u8 key) |
| 615 | { |
| 616 | struct gds_subvector *subvec; |
| 617 | |
| 618 | for (subvec = start; subvec < end; |
| 619 | subvec = (void *) subvec + subvec->length) |
| 620 | if (subvec->key == key) |
| 621 | return subvec; |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | static inline void |
| 626 | sclp_eval_selfdeftextmsg(struct gds_subvector *start, |
| 627 | struct gds_subvector *end) |
| 628 | { |
| 629 | struct gds_subvector *subvec; |
| 630 | |
| 631 | subvec = start; |
| 632 | while (subvec < end) { |
| 633 | subvec = find_gds_subvector(subvec, end, 0x30); |
| 634 | if (!subvec) |
| 635 | break; |
| 636 | sclp_get_input((unsigned char *)(subvec + 1), |
| 637 | (unsigned char *) subvec + subvec->length); |
| 638 | subvec = (void *) subvec + subvec->length; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | static inline void |
| 643 | sclp_eval_textcmd(struct gds_subvector *start, |
| 644 | struct gds_subvector *end) |
| 645 | { |
| 646 | struct gds_subvector *subvec; |
| 647 | |
| 648 | subvec = start; |
| 649 | while (subvec < end) { |
| 650 | subvec = find_gds_subvector(subvec, end, |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame^] | 651 | GDS_KEY_SELFDEFTEXTMSG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (!subvec) |
| 653 | break; |
| 654 | sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1), |
| 655 | (void *)subvec + subvec->length); |
| 656 | subvec = (void *) subvec + subvec->length; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | static inline void |
| 661 | sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end) |
| 662 | { |
| 663 | struct gds_vector *vec; |
| 664 | |
| 665 | vec = start; |
| 666 | while (vec < end) { |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame^] | 667 | vec = find_gds_vector(vec, end, GDS_ID_TEXTCMD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (!vec) |
| 669 | break; |
| 670 | sclp_eval_textcmd((struct gds_subvector *)(vec + 1), |
| 671 | (void *) vec + vec->length); |
| 672 | vec = (void *) vec + vec->length; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | |
| 677 | static inline void |
| 678 | sclp_eval_mdsmu(struct gds_vector *start, void *end) |
| 679 | { |
| 680 | struct gds_vector *vec; |
| 681 | |
| 682 | vec = find_gds_vector(start, end, GDS_ID_CPMSU); |
| 683 | if (vec) |
| 684 | sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length); |
| 685 | } |
| 686 | |
| 687 | static void |
| 688 | sclp_tty_receiver(struct evbuf_header *evbuf) |
| 689 | { |
| 690 | struct gds_vector *start, *end, *vec; |
| 691 | |
| 692 | start = (struct gds_vector *)(evbuf + 1); |
| 693 | end = (void *) evbuf + evbuf->length; |
| 694 | vec = find_gds_vector(start, end, GDS_ID_MDSMU); |
| 695 | if (vec) |
| 696 | sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length); |
| 697 | } |
| 698 | |
| 699 | static void |
| 700 | sclp_tty_state_change(struct sclp_register *reg) |
| 701 | { |
| 702 | } |
| 703 | |
| 704 | static struct sclp_register sclp_input_event = |
| 705 | { |
Stefan Haberland | 6d4740c | 2007-04-27 16:01:53 +0200 | [diff] [blame^] | 706 | .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | .state_change_fn = sclp_tty_state_change, |
| 708 | .receiver_fn = sclp_tty_receiver |
| 709 | }; |
| 710 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 711 | static const struct tty_operations sclp_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | .open = sclp_tty_open, |
| 713 | .close = sclp_tty_close, |
| 714 | .write = sclp_tty_write, |
| 715 | .put_char = sclp_tty_put_char, |
| 716 | .flush_chars = sclp_tty_flush_chars, |
| 717 | .write_room = sclp_tty_write_room, |
| 718 | .chars_in_buffer = sclp_tty_chars_in_buffer, |
| 719 | .flush_buffer = sclp_tty_flush_buffer, |
| 720 | .ioctl = sclp_tty_ioctl, |
| 721 | }; |
| 722 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 723 | static int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | sclp_tty_init(void) |
| 725 | { |
| 726 | struct tty_driver *driver; |
| 727 | void *page; |
| 728 | int i; |
| 729 | int rc; |
| 730 | |
| 731 | if (!CONSOLE_IS_SCLP) |
| 732 | return 0; |
| 733 | driver = alloc_tty_driver(1); |
| 734 | if (!driver) |
| 735 | return -ENOMEM; |
| 736 | |
| 737 | rc = sclp_rw_init(); |
| 738 | if (rc) { |
| 739 | printk(KERN_ERR SCLP_TTY_PRINT_HEADER |
| 740 | "could not register tty - " |
| 741 | "sclp_rw_init returned %d\n", rc); |
| 742 | put_tty_driver(driver); |
| 743 | return rc; |
| 744 | } |
| 745 | /* Allocate pages for output buffering */ |
| 746 | INIT_LIST_HEAD(&sclp_tty_pages); |
| 747 | for (i = 0; i < MAX_KMEM_PAGES; i++) { |
| 748 | page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
| 749 | if (page == NULL) { |
| 750 | put_tty_driver(driver); |
| 751 | return -ENOMEM; |
| 752 | } |
| 753 | list_add_tail((struct list_head *) page, &sclp_tty_pages); |
| 754 | } |
| 755 | INIT_LIST_HEAD(&sclp_tty_outqueue); |
| 756 | spin_lock_init(&sclp_tty_lock); |
| 757 | init_waitqueue_head(&sclp_tty_waitq); |
| 758 | init_timer(&sclp_tty_timer); |
| 759 | sclp_ttybuf = NULL; |
| 760 | sclp_tty_buffer_count = 0; |
| 761 | if (MACHINE_IS_VM) { |
| 762 | /* |
| 763 | * save 4 characters for the CPU number |
| 764 | * written at start of each line by VM/CP |
| 765 | */ |
| 766 | sclp_ioctls_init.columns = 76; |
| 767 | /* case input lines to lowercase */ |
| 768 | sclp_ioctls_init.tolower = 1; |
| 769 | } |
| 770 | sclp_ioctls = sclp_ioctls_init; |
| 771 | sclp_tty_chars_count = 0; |
| 772 | sclp_tty = NULL; |
| 773 | |
| 774 | rc = sclp_register(&sclp_input_event); |
| 775 | if (rc) { |
| 776 | put_tty_driver(driver); |
| 777 | return rc; |
| 778 | } |
| 779 | |
| 780 | driver->owner = THIS_MODULE; |
| 781 | driver->driver_name = "sclp_line"; |
| 782 | driver->name = "sclp_line"; |
| 783 | driver->major = TTY_MAJOR; |
| 784 | driver->minor_start = 64; |
| 785 | driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 786 | driver->subtype = SYSTEM_TYPE_TTY; |
| 787 | driver->init_termios = tty_std_termios; |
| 788 | driver->init_termios.c_iflag = IGNBRK | IGNPAR; |
| 789 | driver->init_termios.c_oflag = ONLCR | XTABS; |
| 790 | driver->init_termios.c_lflag = ISIG | ECHO; |
| 791 | driver->flags = TTY_DRIVER_REAL_RAW; |
| 792 | tty_set_operations(driver, &sclp_ops); |
| 793 | rc = tty_register_driver(driver); |
| 794 | if (rc) { |
| 795 | printk(KERN_ERR SCLP_TTY_PRINT_HEADER |
| 796 | "could not register tty - " |
| 797 | "tty_register_driver returned %d\n", rc); |
| 798 | put_tty_driver(driver); |
| 799 | return rc; |
| 800 | } |
| 801 | sclp_tty_driver = driver; |
| 802 | return 0; |
| 803 | } |
| 804 | module_init(sclp_tty_init); |