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 | * IBM/3270 Driver - tty functions. |
| 3 | * |
| 4 | * Author(s): |
| 5 | * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global) |
| 6 | * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com> |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 7 | * -- Copyright IBM Corp. 2003 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kdev_t.h> |
| 13 | #include <linux/tty.h> |
| 14 | #include <linux/vt_kern.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/console.h> |
| 17 | #include <linux/interrupt.h> |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 18 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/bootmem.h> |
Arnd Bergmann | 9d4bfd4 | 2009-12-07 12:52:13 +0100 | [diff] [blame] | 22 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/ccwdev.h> |
| 25 | #include <asm/cio.h> |
| 26 | #include <asm/ebcdic.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "raw3270.h" |
Heiko Carstens | 364c855 | 2007-10-12 16:11:35 +0200 | [diff] [blame] | 30 | #include "tty3270.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "keyboard.h" |
| 32 | |
| 33 | #define TTY3270_CHAR_BUF_SIZE 256 |
| 34 | #define TTY3270_OUTPUT_BUFFER_SIZE 1024 |
| 35 | #define TTY3270_STRING_PAGES 5 |
| 36 | |
| 37 | struct tty_driver *tty3270_driver; |
| 38 | static int tty3270_max_index; |
| 39 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 40 | static struct raw3270_fn tty3270_fn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | struct tty3270_cell { |
| 43 | unsigned char character; |
| 44 | unsigned char highlight; |
| 45 | unsigned char f_color; |
| 46 | }; |
| 47 | |
| 48 | struct tty3270_line { |
| 49 | struct tty3270_cell *cells; |
| 50 | int len; |
| 51 | }; |
| 52 | |
| 53 | #define ESCAPE_NPAR 8 |
| 54 | |
| 55 | /* |
| 56 | * The main tty view data structure. |
| 57 | * FIXME: |
| 58 | * 1) describe line orientation & lines list concept against screen |
| 59 | * 2) describe conversion of screen to lines |
| 60 | * 3) describe line format. |
| 61 | */ |
| 62 | struct tty3270 { |
| 63 | struct raw3270_view view; |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 64 | struct tty_port port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | void **freemem_pages; /* Array of pages used for freemem. */ |
| 66 | struct list_head freemem; /* List of free memory for strings. */ |
| 67 | |
| 68 | /* Output stuff. */ |
| 69 | struct list_head lines; /* List of lines. */ |
| 70 | struct list_head update; /* List of lines to update. */ |
| 71 | unsigned char wcc; /* Write control character. */ |
| 72 | int nr_lines; /* # lines in list. */ |
| 73 | int nr_up; /* # lines up in history. */ |
| 74 | unsigned long update_flags; /* Update indication bits. */ |
| 75 | struct string *status; /* Lower right of display. */ |
| 76 | struct raw3270_request *write; /* Single write request. */ |
| 77 | struct timer_list timer; /* Output delay timer. */ |
| 78 | |
| 79 | /* Current tty screen. */ |
| 80 | unsigned int cx, cy; /* Current output position. */ |
| 81 | unsigned int highlight; /* Blink/reverse/underscore */ |
| 82 | unsigned int f_color; /* Foreground color */ |
| 83 | struct tty3270_line *screen; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 84 | unsigned int n_model, n_cols, n_rows; /* New model & size */ |
| 85 | struct work_struct resize_work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* Input stuff. */ |
| 88 | struct string *prompt; /* Output string for input area. */ |
| 89 | struct string *input; /* Input string for read request. */ |
| 90 | struct raw3270_request *read; /* Single read request. */ |
| 91 | struct raw3270_request *kreset; /* Single keyboard reset request. */ |
| 92 | unsigned char inattr; /* Visible/invisible input. */ |
| 93 | int throttle, attn; /* tty throttle/unthrottle. */ |
| 94 | struct tasklet_struct readlet; /* Tasklet to issue read request. */ |
| 95 | struct kbd_data *kbd; /* key_maps stuff. */ |
| 96 | |
| 97 | /* Escape sequence parsing. */ |
| 98 | int esc_state, esc_ques, esc_npar; |
| 99 | int esc_par[ESCAPE_NPAR]; |
| 100 | unsigned int saved_cx, saved_cy; |
| 101 | unsigned int saved_highlight, saved_f_color; |
| 102 | |
| 103 | /* Command recalling. */ |
| 104 | struct list_head rcl_lines; /* List of recallable lines. */ |
| 105 | struct list_head *rcl_walk; /* Point in rcl_lines list. */ |
| 106 | int rcl_nr, rcl_max; /* Number/max number of rcl_lines. */ |
| 107 | |
| 108 | /* Character array for put_char/flush_chars. */ |
| 109 | unsigned int char_count; |
| 110 | char char_buf[TTY3270_CHAR_BUF_SIZE]; |
| 111 | }; |
| 112 | |
| 113 | /* tty3270->update_flags. See tty3270_update for details. */ |
| 114 | #define TTY_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */ |
| 115 | #define TTY_UPDATE_LIST 2 /* Update lines in tty3270->update. */ |
| 116 | #define TTY_UPDATE_INPUT 4 /* Update input line. */ |
| 117 | #define TTY_UPDATE_STATUS 8 /* Update status line. */ |
Martin Schwidefsky | 205d7ab | 2009-06-12 10:26:31 +0200 | [diff] [blame] | 118 | #define TTY_UPDATE_ALL 16 /* Recreate screen. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | static void tty3270_update(struct tty3270 *); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 121 | static void tty3270_resize_work(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Setup timeout for a device. On timeout trigger an update. |
| 125 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 126 | static void tty3270_set_timer(struct tty3270 *tp, int expires) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Martin Schwidefsky | 03439e7 | 2013-12-04 14:29:11 +0100 | [diff] [blame] | 128 | mod_timer(&tp->timer, jiffies + expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * The input line are the two last lines of the screen. |
| 133 | */ |
| 134 | static void |
| 135 | tty3270_update_prompt(struct tty3270 *tp, char *input, int count) |
| 136 | { |
| 137 | struct string *line; |
| 138 | unsigned int off; |
| 139 | |
| 140 | line = tp->prompt; |
| 141 | if (count != 0) |
| 142 | line->string[5] = TF_INMDT; |
| 143 | else |
| 144 | line->string[5] = tp->inattr; |
| 145 | if (count > tp->view.cols * 2 - 11) |
| 146 | count = tp->view.cols * 2 - 11; |
| 147 | memcpy(line->string + 6, input, count); |
| 148 | line->string[6 + count] = TO_IC; |
| 149 | /* Clear to end of input line. */ |
| 150 | if (count < tp->view.cols * 2 - 11) { |
| 151 | line->string[7 + count] = TO_RA; |
| 152 | line->string[10 + count] = 0; |
| 153 | off = tp->view.cols * tp->view.rows - 9; |
| 154 | raw3270_buffer_address(tp->view.dev, line->string+count+8, off); |
| 155 | line->len = 11 + count; |
| 156 | } else |
| 157 | line->len = 7 + count; |
| 158 | tp->update_flags |= TTY_UPDATE_INPUT; |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | tty3270_create_prompt(struct tty3270 *tp) |
| 163 | { |
| 164 | static const unsigned char blueprint[] = |
| 165 | { TO_SBA, 0, 0, 0x6e, TO_SF, TF_INPUT, |
| 166 | /* empty input string */ |
| 167 | TO_IC, TO_RA, 0, 0, 0 }; |
| 168 | struct string *line; |
| 169 | unsigned int offset; |
| 170 | |
| 171 | line = alloc_string(&tp->freemem, |
| 172 | sizeof(blueprint) + tp->view.cols * 2 - 9); |
| 173 | tp->prompt = line; |
| 174 | tp->inattr = TF_INPUT; |
| 175 | /* Copy blueprint to status line */ |
| 176 | memcpy(line->string, blueprint, sizeof(blueprint)); |
| 177 | line->len = sizeof(blueprint); |
| 178 | /* Set output offsets. */ |
| 179 | offset = tp->view.cols * (tp->view.rows - 2); |
| 180 | raw3270_buffer_address(tp->view.dev, line->string + 1, offset); |
| 181 | offset = tp->view.cols * tp->view.rows - 9; |
| 182 | raw3270_buffer_address(tp->view.dev, line->string + 8, offset); |
| 183 | |
| 184 | /* Allocate input string for reading. */ |
| 185 | tp->input = alloc_string(&tp->freemem, tp->view.cols * 2 - 9 + 6); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * The status line is the last line of the screen. It shows the string |
| 190 | * "Running"/"Holding" in the lower right corner of the screen. |
| 191 | */ |
| 192 | static void |
| 193 | tty3270_update_status(struct tty3270 * tp) |
| 194 | { |
| 195 | char *str; |
| 196 | |
| 197 | str = (tp->nr_up != 0) ? "History" : "Running"; |
| 198 | memcpy(tp->status->string + 8, str, 7); |
| 199 | codepage_convert(tp->view.ascebc, tp->status->string + 8, 7); |
| 200 | tp->update_flags |= TTY_UPDATE_STATUS; |
| 201 | } |
| 202 | |
| 203 | static void |
| 204 | tty3270_create_status(struct tty3270 * tp) |
| 205 | { |
| 206 | static const unsigned char blueprint[] = |
| 207 | { TO_SBA, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, TAC_GREEN, |
| 208 | 0, 0, 0, 0, 0, 0, 0, TO_SF, TF_LOG, TO_SA, TAT_COLOR, |
| 209 | TAC_RESET }; |
| 210 | struct string *line; |
| 211 | unsigned int offset; |
| 212 | |
| 213 | line = alloc_string(&tp->freemem,sizeof(blueprint)); |
| 214 | tp->status = line; |
| 215 | /* Copy blueprint to status line */ |
| 216 | memcpy(line->string, blueprint, sizeof(blueprint)); |
| 217 | /* Set address to start of status string (= last 9 characters). */ |
| 218 | offset = tp->view.cols * tp->view.rows - 9; |
| 219 | raw3270_buffer_address(tp->view.dev, line->string + 1, offset); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Set output offsets to 3270 datastream fragment of a tty string. |
| 224 | * (TO_SBA offset at the start and TO_RA offset at the end of the string) |
| 225 | */ |
| 226 | static void |
| 227 | tty3270_update_string(struct tty3270 *tp, struct string *line, int nr) |
| 228 | { |
| 229 | unsigned char *cp; |
| 230 | |
| 231 | raw3270_buffer_address(tp->view.dev, line->string + 1, |
| 232 | tp->view.cols * nr); |
| 233 | cp = line->string + line->len - 4; |
| 234 | if (*cp == TO_RA) |
| 235 | raw3270_buffer_address(tp->view.dev, cp + 1, |
| 236 | tp->view.cols * (nr + 1)); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Rebuild update list to print all lines. |
| 241 | */ |
| 242 | static void |
| 243 | tty3270_rebuild_update(struct tty3270 *tp) |
| 244 | { |
| 245 | struct string *s, *n; |
| 246 | int line, nr_up; |
| 247 | |
| 248 | /* |
| 249 | * Throw away update list and create a new one, |
| 250 | * containing all lines that will fit on the screen. |
| 251 | */ |
| 252 | list_for_each_entry_safe(s, n, &tp->update, update) |
| 253 | list_del_init(&s->update); |
| 254 | line = tp->view.rows - 3; |
| 255 | nr_up = tp->nr_up; |
| 256 | list_for_each_entry_reverse(s, &tp->lines, list) { |
| 257 | if (nr_up > 0) { |
| 258 | nr_up--; |
| 259 | continue; |
| 260 | } |
| 261 | tty3270_update_string(tp, s, line); |
| 262 | list_add(&s->update, &tp->update); |
| 263 | if (--line < 0) |
| 264 | break; |
| 265 | } |
| 266 | tp->update_flags |= TTY_UPDATE_LIST; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Alloc string for size bytes. If there is not enough room in |
| 271 | * freemem, free strings until there is room. |
| 272 | */ |
| 273 | static struct string * |
| 274 | tty3270_alloc_string(struct tty3270 *tp, size_t size) |
| 275 | { |
| 276 | struct string *s, *n; |
| 277 | |
| 278 | s = alloc_string(&tp->freemem, size); |
| 279 | if (s) |
| 280 | return s; |
| 281 | list_for_each_entry_safe(s, n, &tp->lines, list) { |
| 282 | BUG_ON(tp->nr_lines <= tp->view.rows - 2); |
| 283 | list_del(&s->list); |
| 284 | if (!list_empty(&s->update)) |
| 285 | list_del(&s->update); |
| 286 | tp->nr_lines--; |
| 287 | if (free_string(&tp->freemem, s) >= size) |
| 288 | break; |
| 289 | } |
| 290 | s = alloc_string(&tp->freemem, size); |
| 291 | BUG_ON(!s); |
| 292 | if (tp->nr_up != 0 && |
| 293 | tp->nr_up + tp->view.rows - 2 >= tp->nr_lines) { |
| 294 | tp->nr_up = tp->nr_lines - tp->view.rows + 2; |
| 295 | tty3270_rebuild_update(tp); |
| 296 | tty3270_update_status(tp); |
| 297 | } |
| 298 | return s; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Add an empty line to the list. |
| 303 | */ |
| 304 | static void |
| 305 | tty3270_blank_line(struct tty3270 *tp) |
| 306 | { |
| 307 | static const unsigned char blueprint[] = |
| 308 | { TO_SBA, 0, 0, TO_SA, TAT_EXTHI, TAX_RESET, |
| 309 | TO_SA, TAT_COLOR, TAC_RESET, TO_RA, 0, 0, 0 }; |
| 310 | struct string *s; |
| 311 | |
| 312 | s = tty3270_alloc_string(tp, sizeof(blueprint)); |
| 313 | memcpy(s->string, blueprint, sizeof(blueprint)); |
| 314 | s->len = sizeof(blueprint); |
| 315 | list_add_tail(&s->list, &tp->lines); |
| 316 | tp->nr_lines++; |
| 317 | if (tp->nr_up != 0) |
| 318 | tp->nr_up++; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Write request completion callback. |
| 323 | */ |
| 324 | static void |
| 325 | tty3270_write_callback(struct raw3270_request *rq, void *data) |
| 326 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 327 | struct tty3270 *tp = container_of(rq->view, struct tty3270, view); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (rq->rc != 0) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 330 | /* Write wasn't successful. Refresh all. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | tp->update_flags = TTY_UPDATE_ALL; |
| 332 | tty3270_set_timer(tp, 1); |
| 333 | } |
| 334 | raw3270_request_reset(rq); |
| 335 | xchg(&tp->write, rq); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Update 3270 display. |
| 340 | */ |
| 341 | static void |
| 342 | tty3270_update(struct tty3270 *tp) |
| 343 | { |
| 344 | static char invalid_sba[2] = { 0xff, 0xff }; |
| 345 | struct raw3270_request *wrq; |
| 346 | unsigned long updated; |
| 347 | struct string *s, *n; |
| 348 | char *sba, *str; |
| 349 | int rc, len; |
| 350 | |
| 351 | wrq = xchg(&tp->write, 0); |
| 352 | if (!wrq) { |
| 353 | tty3270_set_timer(tp, 1); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | spin_lock(&tp->view.lock); |
| 358 | updated = 0; |
Martin Schwidefsky | 205d7ab | 2009-06-12 10:26:31 +0200 | [diff] [blame] | 359 | if (tp->update_flags & TTY_UPDATE_ALL) { |
| 360 | tty3270_rebuild_update(tp); |
| 361 | tty3270_update_status(tp); |
| 362 | tp->update_flags = TTY_UPDATE_ERASE | TTY_UPDATE_LIST | |
| 363 | TTY_UPDATE_INPUT | TTY_UPDATE_STATUS; |
| 364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (tp->update_flags & TTY_UPDATE_ERASE) { |
| 366 | /* Use erase write alternate to erase display. */ |
| 367 | raw3270_request_set_cmd(wrq, TC_EWRITEA); |
| 368 | updated |= TTY_UPDATE_ERASE; |
| 369 | } else |
| 370 | raw3270_request_set_cmd(wrq, TC_WRITE); |
| 371 | |
| 372 | raw3270_request_add_data(wrq, &tp->wcc, 1); |
| 373 | tp->wcc = TW_NONE; |
| 374 | |
| 375 | /* |
| 376 | * Update status line. |
| 377 | */ |
| 378 | if (tp->update_flags & TTY_UPDATE_STATUS) |
| 379 | if (raw3270_request_add_data(wrq, tp->status->string, |
| 380 | tp->status->len) == 0) |
| 381 | updated |= TTY_UPDATE_STATUS; |
| 382 | |
| 383 | /* |
| 384 | * Write input line. |
| 385 | */ |
| 386 | if (tp->update_flags & TTY_UPDATE_INPUT) |
| 387 | if (raw3270_request_add_data(wrq, tp->prompt->string, |
| 388 | tp->prompt->len) == 0) |
| 389 | updated |= TTY_UPDATE_INPUT; |
| 390 | |
| 391 | sba = invalid_sba; |
| 392 | |
| 393 | if (tp->update_flags & TTY_UPDATE_LIST) { |
| 394 | /* Write strings in the update list to the screen. */ |
| 395 | list_for_each_entry_safe(s, n, &tp->update, update) { |
| 396 | str = s->string; |
| 397 | len = s->len; |
| 398 | /* |
| 399 | * Skip TO_SBA at the start of the string if the |
| 400 | * last output position matches the start address |
| 401 | * of this line. |
| 402 | */ |
| 403 | if (s->string[1] == sba[0] && s->string[2] == sba[1]) |
| 404 | str += 3, len -= 3; |
| 405 | if (raw3270_request_add_data(wrq, str, len) != 0) |
| 406 | break; |
| 407 | list_del_init(&s->update); |
Martin Schwidefsky | 2e63a3a | 2016-04-28 16:32:13 +0200 | [diff] [blame^] | 408 | if (s->string[s->len - 4] == TO_RA) |
| 409 | sba = s->string + s->len - 3; |
| 410 | else |
| 411 | sba = invalid_sba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | if (list_empty(&tp->update)) |
| 414 | updated |= TTY_UPDATE_LIST; |
| 415 | } |
| 416 | wrq->callback = tty3270_write_callback; |
| 417 | rc = raw3270_start(&tp->view, wrq); |
| 418 | if (rc == 0) { |
| 419 | tp->update_flags &= ~updated; |
| 420 | if (tp->update_flags) |
| 421 | tty3270_set_timer(tp, 1); |
| 422 | } else { |
| 423 | raw3270_request_reset(wrq); |
| 424 | xchg(&tp->write, wrq); |
| 425 | } |
| 426 | spin_unlock(&tp->view.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Command recalling. |
| 431 | */ |
| 432 | static void |
| 433 | tty3270_rcl_add(struct tty3270 *tp, char *input, int len) |
| 434 | { |
| 435 | struct string *s; |
| 436 | |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 437 | tp->rcl_walk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (len <= 0) |
| 439 | return; |
| 440 | if (tp->rcl_nr >= tp->rcl_max) { |
| 441 | s = list_entry(tp->rcl_lines.next, struct string, list); |
| 442 | list_del(&s->list); |
| 443 | free_string(&tp->freemem, s); |
| 444 | tp->rcl_nr--; |
| 445 | } |
| 446 | s = tty3270_alloc_string(tp, len); |
| 447 | memcpy(s->string, input, len); |
| 448 | list_add_tail(&s->list, &tp->rcl_lines); |
| 449 | tp->rcl_nr++; |
| 450 | } |
| 451 | |
| 452 | static void |
| 453 | tty3270_rcl_backward(struct kbd_data *kbd) |
| 454 | { |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 455 | struct tty3270 *tp = container_of(kbd->port, struct tty3270, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | struct string *s; |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | spin_lock_bh(&tp->view.lock); |
| 459 | if (tp->inattr == TF_INPUT) { |
| 460 | if (tp->rcl_walk && tp->rcl_walk->prev != &tp->rcl_lines) |
| 461 | tp->rcl_walk = tp->rcl_walk->prev; |
| 462 | else if (!list_empty(&tp->rcl_lines)) |
| 463 | tp->rcl_walk = tp->rcl_lines.prev; |
| 464 | s = tp->rcl_walk ? |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 465 | list_entry(tp->rcl_walk, struct string, list) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (tp->rcl_walk) { |
| 467 | s = list_entry(tp->rcl_walk, struct string, list); |
| 468 | tty3270_update_prompt(tp, s->string, s->len); |
| 469 | } else |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 470 | tty3270_update_prompt(tp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | tty3270_set_timer(tp, 1); |
| 472 | } |
| 473 | spin_unlock_bh(&tp->view.lock); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Deactivate tty view. |
| 478 | */ |
| 479 | static void |
| 480 | tty3270_exit_tty(struct kbd_data *kbd) |
| 481 | { |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 482 | struct tty3270 *tp = container_of(kbd->port, struct tty3270, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | raw3270_deactivate_view(&tp->view); |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Scroll forward in history. |
| 489 | */ |
| 490 | static void |
| 491 | tty3270_scroll_forward(struct kbd_data *kbd) |
| 492 | { |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 493 | struct tty3270 *tp = container_of(kbd->port, struct tty3270, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | int nr_up; |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | spin_lock_bh(&tp->view.lock); |
| 497 | nr_up = tp->nr_up - tp->view.rows + 2; |
| 498 | if (nr_up < 0) |
| 499 | nr_up = 0; |
| 500 | if (nr_up != tp->nr_up) { |
| 501 | tp->nr_up = nr_up; |
| 502 | tty3270_rebuild_update(tp); |
| 503 | tty3270_update_status(tp); |
| 504 | tty3270_set_timer(tp, 1); |
| 505 | } |
| 506 | spin_unlock_bh(&tp->view.lock); |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Scroll backward in history. |
| 511 | */ |
| 512 | static void |
| 513 | tty3270_scroll_backward(struct kbd_data *kbd) |
| 514 | { |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 515 | struct tty3270 *tp = container_of(kbd->port, struct tty3270, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | int nr_up; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | spin_lock_bh(&tp->view.lock); |
| 519 | nr_up = tp->nr_up + tp->view.rows - 2; |
| 520 | if (nr_up + tp->view.rows - 2 > tp->nr_lines) |
| 521 | nr_up = tp->nr_lines - tp->view.rows + 2; |
| 522 | if (nr_up != tp->nr_up) { |
| 523 | tp->nr_up = nr_up; |
| 524 | tty3270_rebuild_update(tp); |
| 525 | tty3270_update_status(tp); |
| 526 | tty3270_set_timer(tp, 1); |
| 527 | } |
| 528 | spin_unlock_bh(&tp->view.lock); |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Pass input line to tty. |
| 533 | */ |
| 534 | static void |
| 535 | tty3270_read_tasklet(struct raw3270_request *rrq) |
| 536 | { |
| 537 | static char kreset_data = TW_KR; |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 538 | struct tty3270 *tp = container_of(rrq->view, struct tty3270, view); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | char *input; |
| 540 | int len; |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | spin_lock_bh(&tp->view.lock); |
| 543 | /* |
| 544 | * Two AID keys are special: For 0x7d (enter) the input line |
| 545 | * has to be emitted to the tty and for 0x6d the screen |
| 546 | * needs to be redrawn. |
| 547 | */ |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 548 | input = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | len = 0; |
| 550 | if (tp->input->string[0] == 0x7d) { |
| 551 | /* Enter: write input to tty. */ |
| 552 | input = tp->input->string + 6; |
| 553 | len = tp->input->len - 6 - rrq->rescnt; |
| 554 | if (tp->inattr != TF_INPUTN) |
| 555 | tty3270_rcl_add(tp, input, len); |
| 556 | if (tp->nr_up > 0) { |
| 557 | tp->nr_up = 0; |
| 558 | tty3270_rebuild_update(tp); |
| 559 | tty3270_update_status(tp); |
| 560 | } |
| 561 | /* Clear input area. */ |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 562 | tty3270_update_prompt(tp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | tty3270_set_timer(tp, 1); |
| 564 | } else if (tp->input->string[0] == 0x6d) { |
| 565 | /* Display has been cleared. Redraw. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | tp->update_flags = TTY_UPDATE_ALL; |
| 567 | tty3270_set_timer(tp, 1); |
| 568 | } |
| 569 | spin_unlock_bh(&tp->view.lock); |
| 570 | |
| 571 | /* Start keyboard reset command. */ |
| 572 | raw3270_request_reset(tp->kreset); |
| 573 | raw3270_request_set_cmd(tp->kreset, TC_WRITE); |
| 574 | raw3270_request_add_data(tp->kreset, &kreset_data, 1); |
| 575 | raw3270_start(&tp->view, tp->kreset); |
| 576 | |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 577 | while (len-- > 0) |
| 578 | kbd_keycode(tp->kbd, *input++); |
| 579 | /* Emit keycode for AID byte. */ |
| 580 | kbd_keycode(tp->kbd, 256 + tp->input->string[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | raw3270_request_reset(rrq); |
| 583 | xchg(&tp->read, rrq); |
| 584 | raw3270_put_view(&tp->view); |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Read request completion callback. |
| 589 | */ |
| 590 | static void |
| 591 | tty3270_read_callback(struct raw3270_request *rq, void *data) |
| 592 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 593 | struct tty3270 *tp = container_of(rq->view, struct tty3270, view); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | raw3270_get_view(rq->view); |
| 595 | /* Schedule tasklet to pass input to tty. */ |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 596 | tasklet_schedule(&tp->readlet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Issue a read request. Call with device lock. |
| 601 | */ |
| 602 | static void |
| 603 | tty3270_issue_read(struct tty3270 *tp, int lock) |
| 604 | { |
| 605 | struct raw3270_request *rrq; |
| 606 | int rc; |
| 607 | |
| 608 | rrq = xchg(&tp->read, 0); |
| 609 | if (!rrq) |
| 610 | /* Read already scheduled. */ |
| 611 | return; |
| 612 | rrq->callback = tty3270_read_callback; |
| 613 | rrq->callback_data = tp; |
| 614 | raw3270_request_set_cmd(rrq, TC_READMOD); |
| 615 | raw3270_request_set_data(rrq, tp->input->string, tp->input->len); |
| 616 | /* Issue the read modified request. */ |
| 617 | if (lock) { |
| 618 | rc = raw3270_start(&tp->view, rrq); |
| 619 | } else |
| 620 | rc = raw3270_start_irq(&tp->view, rrq); |
| 621 | if (rc) { |
| 622 | raw3270_request_reset(rrq); |
| 623 | xchg(&tp->read, rrq); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Switch to the tty view. |
| 629 | */ |
| 630 | static int |
| 631 | tty3270_activate(struct raw3270_view *view) |
| 632 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 633 | struct tty3270 *tp = container_of(view, struct tty3270, view); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | tp->update_flags = TTY_UPDATE_ALL; |
| 636 | tty3270_set_timer(tp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static void |
| 641 | tty3270_deactivate(struct raw3270_view *view) |
| 642 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 643 | struct tty3270 *tp = container_of(view, struct tty3270, view); |
Martin Schwidefsky | 205d7ab | 2009-06-12 10:26:31 +0200 | [diff] [blame] | 644 | |
Martin Schwidefsky | 205d7ab | 2009-06-12 10:26:31 +0200 | [diff] [blame] | 645 | del_timer(&tp->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static int |
| 649 | tty3270_irq(struct tty3270 *tp, struct raw3270_request *rq, struct irb *irb) |
| 650 | { |
| 651 | /* Handle ATTN. Schedule tasklet to read aid. */ |
Peter Oberparleiter | 23d805b6 | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 652 | if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (!tp->throttle) |
| 654 | tty3270_issue_read(tp, 0); |
| 655 | else |
| 656 | tp->attn = 1; |
| 657 | } |
| 658 | |
| 659 | if (rq) { |
Peter Oberparleiter | 23d805b6 | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 660 | if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | rq->rc = -EIO; |
| 662 | else |
| 663 | /* Normal end. Copy residual count. */ |
Peter Oberparleiter | 23d805b6 | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 664 | rq->rescnt = irb->scsw.cmd.count; |
Martin Schwidefsky | 05bfd70 | 2015-08-19 16:50:10 +0200 | [diff] [blame] | 665 | } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) { |
| 666 | /* Interrupt without an outstanding request -> update all */ |
| 667 | tp->update_flags = TTY_UPDATE_ALL; |
| 668 | tty3270_set_timer(tp, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | return RAW3270_IO_DONE; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Allocate tty3270 structure. |
| 675 | */ |
| 676 | static struct tty3270 * |
| 677 | tty3270_alloc_view(void) |
| 678 | { |
| 679 | struct tty3270 *tp; |
| 680 | int pages; |
| 681 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 682 | tp = kzalloc(sizeof(struct tty3270), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (!tp) |
| 684 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | tp->freemem_pages = |
| 686 | kmalloc(sizeof(void *) * TTY3270_STRING_PAGES, GFP_KERNEL); |
| 687 | if (!tp->freemem_pages) |
| 688 | goto out_tp; |
| 689 | INIT_LIST_HEAD(&tp->freemem); |
Jiri Slaby | 9d2ae23 | 2012-04-02 13:54:15 +0200 | [diff] [blame] | 690 | INIT_LIST_HEAD(&tp->lines); |
| 691 | INIT_LIST_HEAD(&tp->update); |
| 692 | INIT_LIST_HEAD(&tp->rcl_lines); |
| 693 | tp->rcl_max = 20; |
Jiri Slaby | 9d2ae23 | 2012-04-02 13:54:15 +0200 | [diff] [blame] | 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) { |
| 696 | tp->freemem_pages[pages] = (void *) |
| 697 | __get_free_pages(GFP_KERNEL|GFP_DMA, 0); |
| 698 | if (!tp->freemem_pages[pages]) |
| 699 | goto out_pages; |
| 700 | add_string_memory(&tp->freemem, |
| 701 | tp->freemem_pages[pages], PAGE_SIZE); |
| 702 | } |
| 703 | tp->write = raw3270_request_alloc(TTY3270_OUTPUT_BUFFER_SIZE); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 704 | if (IS_ERR(tp->write)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | goto out_pages; |
| 706 | tp->read = raw3270_request_alloc(0); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 707 | if (IS_ERR(tp->read)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | goto out_write; |
| 709 | tp->kreset = raw3270_request_alloc(1); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 710 | if (IS_ERR(tp->kreset)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | goto out_read; |
| 712 | tp->kbd = kbd_alloc(); |
| 713 | if (!tp->kbd) |
| 714 | goto out_reset; |
Martin Schwidefsky | 57985d7 | 2013-01-08 15:20:05 +0100 | [diff] [blame] | 715 | |
| 716 | tty_port_init(&tp->port); |
| 717 | setup_timer(&tp->timer, (void (*)(unsigned long)) tty3270_update, |
| 718 | (unsigned long) tp); |
| 719 | tasklet_init(&tp->readlet, |
| 720 | (void (*)(unsigned long)) tty3270_read_tasklet, |
| 721 | (unsigned long) tp->read); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 722 | INIT_WORK(&tp->resize_work, tty3270_resize_work); |
Martin Schwidefsky | 57985d7 | 2013-01-08 15:20:05 +0100 | [diff] [blame] | 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return tp; |
| 725 | |
| 726 | out_reset: |
| 727 | raw3270_request_free(tp->kreset); |
| 728 | out_read: |
| 729 | raw3270_request_free(tp->read); |
| 730 | out_write: |
| 731 | raw3270_request_free(tp->write); |
| 732 | out_pages: |
| 733 | while (pages--) |
| 734 | free_pages((unsigned long) tp->freemem_pages[pages], 0); |
| 735 | kfree(tp->freemem_pages); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 736 | tty_port_destroy(&tp->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | out_tp: |
| 738 | kfree(tp); |
| 739 | out_err: |
| 740 | return ERR_PTR(-ENOMEM); |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Free tty3270 structure. |
| 745 | */ |
| 746 | static void |
| 747 | tty3270_free_view(struct tty3270 *tp) |
| 748 | { |
| 749 | int pages; |
| 750 | |
| 751 | kbd_free(tp->kbd); |
| 752 | raw3270_request_free(tp->kreset); |
| 753 | raw3270_request_free(tp->read); |
| 754 | raw3270_request_free(tp->write); |
| 755 | for (pages = 0; pages < TTY3270_STRING_PAGES; pages++) |
| 756 | free_pages((unsigned long) tp->freemem_pages[pages], 0); |
| 757 | kfree(tp->freemem_pages); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 758 | tty_port_destroy(&tp->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | kfree(tp); |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Allocate tty3270 screen. |
| 764 | */ |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 765 | static struct tty3270_line * |
| 766 | tty3270_alloc_screen(unsigned int rows, unsigned int cols) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 768 | struct tty3270_line *screen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | unsigned long size; |
| 770 | int lines; |
| 771 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 772 | size = sizeof(struct tty3270_line) * (rows - 2); |
| 773 | screen = kzalloc(size, GFP_KERNEL); |
| 774 | if (!screen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | goto out_err; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 776 | for (lines = 0; lines < rows - 2; lines++) { |
| 777 | size = sizeof(struct tty3270_cell) * cols; |
| 778 | screen[lines].cells = kzalloc(size, GFP_KERNEL); |
| 779 | if (!screen[lines].cells) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | goto out_screen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 782 | return screen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | out_screen: |
| 784 | while (lines--) |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 785 | kfree(screen[lines].cells); |
| 786 | kfree(screen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | out_err: |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 788 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Free tty3270 screen. |
| 793 | */ |
| 794 | static void |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 795 | tty3270_free_screen(struct tty3270_line *screen, unsigned int rows) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
| 797 | int lines; |
| 798 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 799 | for (lines = 0; lines < rows - 2; lines++) |
| 800 | kfree(screen[lines].cells); |
| 801 | kfree(screen); |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Resize tty3270 screen |
| 806 | */ |
| 807 | static void tty3270_resize_work(struct work_struct *work) |
| 808 | { |
| 809 | struct tty3270 *tp = container_of(work, struct tty3270, resize_work); |
| 810 | struct tty3270_line *screen, *oscreen; |
| 811 | struct tty_struct *tty; |
| 812 | unsigned int orows; |
| 813 | struct winsize ws; |
| 814 | |
| 815 | screen = tty3270_alloc_screen(tp->n_rows, tp->n_cols); |
Wei Yongjun | 8f0ba63 | 2013-09-11 06:49:20 +0800 | [diff] [blame] | 816 | if (IS_ERR(screen)) |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 817 | return; |
| 818 | /* Switch to new output size */ |
| 819 | spin_lock_bh(&tp->view.lock); |
| 820 | oscreen = tp->screen; |
| 821 | orows = tp->view.rows; |
| 822 | tp->view.model = tp->n_model; |
| 823 | tp->view.rows = tp->n_rows; |
| 824 | tp->view.cols = tp->n_cols; |
| 825 | tp->screen = screen; |
| 826 | free_string(&tp->freemem, tp->prompt); |
| 827 | free_string(&tp->freemem, tp->status); |
| 828 | tty3270_create_prompt(tp); |
| 829 | tty3270_create_status(tp); |
| 830 | tp->nr_up = 0; |
| 831 | while (tp->nr_lines < tp->view.rows - 2) |
| 832 | tty3270_blank_line(tp); |
| 833 | tp->update_flags = TTY_UPDATE_ALL; |
| 834 | spin_unlock_bh(&tp->view.lock); |
| 835 | tty3270_free_screen(oscreen, orows); |
| 836 | tty3270_set_timer(tp, 1); |
| 837 | /* Informat tty layer about new size */ |
| 838 | tty = tty_port_tty_get(&tp->port); |
| 839 | if (!tty) |
| 840 | return; |
| 841 | ws.ws_row = tp->view.rows - 2; |
| 842 | ws.ws_col = tp->view.cols; |
| 843 | tty_do_resize(tty, &ws); |
Martin Schwidefsky | 5ff04fe | 2016-04-27 14:32:06 +0200 | [diff] [blame] | 844 | tty_kref_put(tty); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static void |
| 848 | tty3270_resize(struct raw3270_view *view, int model, int rows, int cols) |
| 849 | { |
| 850 | struct tty3270 *tp = container_of(view, struct tty3270, view); |
| 851 | |
| 852 | tp->n_model = model; |
| 853 | tp->n_rows = rows; |
| 854 | tp->n_cols = cols; |
| 855 | schedule_work(&tp->resize_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Unlink tty3270 data structure from tty. |
| 860 | */ |
| 861 | static void |
| 862 | tty3270_release(struct raw3270_view *view) |
| 863 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 864 | struct tty3270 *tp = container_of(view, struct tty3270, view); |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 865 | struct tty_struct *tty = tty_port_tty_get(&tp->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | if (tty) { |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 868 | tty->driver_data = NULL; |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 869 | tty_port_tty_set(&tp->port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | tty_hangup(tty); |
| 871 | raw3270_put_view(&tp->view); |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 872 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Free tty3270 data structure |
| 878 | */ |
| 879 | static void |
| 880 | tty3270_free(struct raw3270_view *view) |
| 881 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 882 | struct tty3270 *tp = container_of(view, struct tty3270, view); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 883 | |
Martin Schwidefsky | 03439e7 | 2013-12-04 14:29:11 +0100 | [diff] [blame] | 884 | del_timer_sync(&tp->timer); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 885 | tty3270_free_screen(tp->screen, tp->view.rows); |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 886 | tty3270_free_view(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* |
| 890 | * Delayed freeing of tty3270 views. |
| 891 | */ |
| 892 | static void |
| 893 | tty3270_del_views(void) |
| 894 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | int i; |
| 896 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 897 | for (i = RAW3270_FIRSTMINOR; i <= tty3270_max_index; i++) { |
| 898 | struct raw3270_view *view = raw3270_find_view(&tty3270_fn, i); |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 899 | if (!IS_ERR(view)) |
| 900 | raw3270_del_view(view); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 904 | static struct raw3270_fn tty3270_fn = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | .activate = tty3270_activate, |
| 906 | .deactivate = tty3270_deactivate, |
| 907 | .intv = (void *) tty3270_irq, |
| 908 | .release = tty3270_release, |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 909 | .free = tty3270_free, |
| 910 | .resize = tty3270_resize |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | /* |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 914 | * This routine is called whenever a 3270 tty is opened first time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | */ |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 916 | static int tty3270_install(struct tty_driver *driver, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 918 | struct raw3270_view *view; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | struct tty3270 *tp; |
| 920 | int i, rc; |
| 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | /* Check if the tty3270 is already there. */ |
Martin Schwidefsky | 1fcbba3 | 2013-03-21 13:07:18 +0100 | [diff] [blame] | 923 | view = raw3270_find_view(&tty3270_fn, tty->index + RAW3270_FIRSTMINOR); |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 924 | if (!IS_ERR(view)) { |
| 925 | tp = container_of(view, struct tty3270, view); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | tty->driver_data = tp; |
| 927 | tty->winsize.ws_row = tp->view.rows - 2; |
| 928 | tty->winsize.ws_col = tp->view.cols; |
Jiri Slaby | d6c53c0e | 2013-01-03 15:53:05 +0100 | [diff] [blame] | 929 | tp->port.low_latency = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | tp->inattr = TF_INPUT; |
Martin Schwidefsky | b512353 | 2016-05-02 13:49:28 +0200 | [diff] [blame] | 931 | goto port_install; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
Martin Schwidefsky | 1fcbba3 | 2013-03-21 13:07:18 +0100 | [diff] [blame] | 933 | if (tty3270_max_index < tty->index + 1) |
| 934 | tty3270_max_index = tty->index + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
| 936 | /* Allocate tty3270 structure on first open. */ |
| 937 | tp = tty3270_alloc_view(); |
| 938 | if (IS_ERR(tp)) |
| 939 | return PTR_ERR(tp); |
| 940 | |
Martin Schwidefsky | 1fcbba3 | 2013-03-21 13:07:18 +0100 | [diff] [blame] | 941 | rc = raw3270_add_view(&tp->view, &tty3270_fn, |
| 942 | tty->index + RAW3270_FIRSTMINOR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | if (rc) { |
| 944 | tty3270_free_view(tp); |
| 945 | return rc; |
| 946 | } |
| 947 | |
Martin Schwidefsky | 36d9f4d | 2013-12-18 14:36:18 +0100 | [diff] [blame] | 948 | tp->screen = tty3270_alloc_screen(tp->view.rows, tp->view.cols); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 949 | if (IS_ERR(tp->screen)) { |
| 950 | rc = PTR_ERR(tp->screen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | raw3270_put_view(&tp->view); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 952 | raw3270_del_view(&tp->view); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 953 | tty3270_free_view(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | return rc; |
| 955 | } |
| 956 | |
Jiri Slaby | d6c53c0e | 2013-01-03 15:53:05 +0100 | [diff] [blame] | 957 | tp->port.low_latency = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | tty->winsize.ws_row = tp->view.rows - 2; |
| 959 | tty->winsize.ws_col = tp->view.cols; |
| 960 | |
| 961 | tty3270_create_prompt(tp); |
| 962 | tty3270_create_status(tp); |
| 963 | tty3270_update_status(tp); |
| 964 | |
| 965 | /* Create blank line for every line in the tty output area. */ |
| 966 | for (i = 0; i < tp->view.rows - 2; i++) |
| 967 | tty3270_blank_line(tp); |
| 968 | |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 969 | tp->kbd->port = &tp->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | tp->kbd->fn_handler[KVAL(K_INCRCONSOLE)] = tty3270_exit_tty; |
| 971 | tp->kbd->fn_handler[KVAL(K_SCROLLBACK)] = tty3270_scroll_backward; |
| 972 | tp->kbd->fn_handler[KVAL(K_SCROLLFORW)] = tty3270_scroll_forward; |
| 973 | tp->kbd->fn_handler[KVAL(K_CONS)] = tty3270_rcl_backward; |
| 974 | kbd_ascebc(tp->kbd, tp->view.ascebc); |
| 975 | |
| 976 | raw3270_activate_view(&tp->view); |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 977 | |
Martin Schwidefsky | b512353 | 2016-05-02 13:49:28 +0200 | [diff] [blame] | 978 | port_install: |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 979 | rc = tty_port_install(&tp->port, driver, tty); |
| 980 | if (rc) { |
| 981 | raw3270_put_view(&tp->view); |
| 982 | return rc; |
| 983 | } |
| 984 | |
| 985 | tty->driver_data = tp; |
| 986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | /* |
Martin Schwidefsky | 736c9fd | 2013-01-08 15:15:12 +0100 | [diff] [blame] | 991 | * This routine is called whenever a 3270 tty is opened. |
| 992 | */ |
| 993 | static int |
| 994 | tty3270_open(struct tty_struct *tty, struct file *filp) |
| 995 | { |
| 996 | struct tty3270 *tp = tty->driver_data; |
| 997 | struct tty_port *port = &tp->port; |
| 998 | |
| 999 | port->count++; |
| 1000 | tty_port_tty_set(port, tty); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | * This routine is called when the 3270 tty is closed. We wait |
| 1006 | * for the remaining request to be completed. Then we clean up. |
| 1007 | */ |
| 1008 | static void |
| 1009 | tty3270_close(struct tty_struct *tty, struct file * filp) |
| 1010 | { |
Jiri Slaby | 881e18f | 2012-04-02 13:54:16 +0200 | [diff] [blame] | 1011 | struct tty3270 *tp = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
| 1013 | if (tty->count > 1) |
| 1014 | return; |
Martin Schwidefsky | b512353 | 2016-05-02 13:49:28 +0200 | [diff] [blame] | 1015 | if (tp) |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 1016 | tty_port_tty_set(&tp->port, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 1019 | static void tty3270_cleanup(struct tty_struct *tty) |
| 1020 | { |
| 1021 | struct tty3270 *tp = tty->driver_data; |
| 1022 | |
Martin Schwidefsky | b512353 | 2016-05-02 13:49:28 +0200 | [diff] [blame] | 1023 | if (tp) { |
| 1024 | tty->driver_data = NULL; |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 1025 | raw3270_put_view(&tp->view); |
Martin Schwidefsky | b512353 | 2016-05-02 13:49:28 +0200 | [diff] [blame] | 1026 | } |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | /* |
| 1030 | * We always have room. |
| 1031 | */ |
| 1032 | static int |
| 1033 | tty3270_write_room(struct tty_struct *tty) |
| 1034 | { |
| 1035 | return INT_MAX; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * Insert character into the screen at the current position with the |
| 1040 | * current color and highlight. This function does NOT do cursor movement. |
| 1041 | */ |
Heiko Carstens | 74c76c8 | 2008-05-07 09:22:58 +0200 | [diff] [blame] | 1042 | static void tty3270_put_character(struct tty3270 *tp, char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { |
| 1044 | struct tty3270_line *line; |
| 1045 | struct tty3270_cell *cell; |
| 1046 | |
| 1047 | line = tp->screen + tp->cy; |
| 1048 | if (line->len <= tp->cx) { |
| 1049 | while (line->len < tp->cx) { |
| 1050 | cell = line->cells + line->len; |
| 1051 | cell->character = tp->view.ascebc[' ']; |
| 1052 | cell->highlight = tp->highlight; |
| 1053 | cell->f_color = tp->f_color; |
| 1054 | line->len++; |
| 1055 | } |
| 1056 | line->len++; |
| 1057 | } |
| 1058 | cell = line->cells + tp->cx; |
| 1059 | cell->character = tp->view.ascebc[(unsigned int) ch]; |
| 1060 | cell->highlight = tp->highlight; |
| 1061 | cell->f_color = tp->f_color; |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * Convert a tty3270_line to a 3270 data fragment usable for output. |
| 1066 | */ |
| 1067 | static void |
| 1068 | tty3270_convert_line(struct tty3270 *tp, int line_nr) |
| 1069 | { |
| 1070 | struct tty3270_line *line; |
| 1071 | struct tty3270_cell *cell; |
| 1072 | struct string *s, *n; |
| 1073 | unsigned char highlight; |
| 1074 | unsigned char f_color; |
| 1075 | char *cp; |
| 1076 | int flen, i; |
| 1077 | |
| 1078 | /* Determine how long the fragment will be. */ |
| 1079 | flen = 3; /* Prefix (TO_SBA). */ |
| 1080 | line = tp->screen + line_nr; |
| 1081 | flen += line->len; |
| 1082 | highlight = TAX_RESET; |
| 1083 | f_color = TAC_RESET; |
| 1084 | for (i = 0, cell = line->cells; i < line->len; i++, cell++) { |
| 1085 | if (cell->highlight != highlight) { |
| 1086 | flen += 3; /* TO_SA to switch highlight. */ |
| 1087 | highlight = cell->highlight; |
| 1088 | } |
| 1089 | if (cell->f_color != f_color) { |
| 1090 | flen += 3; /* TO_SA to switch color. */ |
| 1091 | f_color = cell->f_color; |
| 1092 | } |
| 1093 | } |
| 1094 | if (highlight != TAX_RESET) |
| 1095 | flen += 3; /* TO_SA to reset hightlight. */ |
| 1096 | if (f_color != TAC_RESET) |
| 1097 | flen += 3; /* TO_SA to reset color. */ |
| 1098 | if (line->len < tp->view.cols) |
| 1099 | flen += 4; /* Postfix (TO_RA). */ |
| 1100 | |
| 1101 | /* Find the line in the list. */ |
| 1102 | i = tp->view.rows - 2 - line_nr; |
| 1103 | list_for_each_entry_reverse(s, &tp->lines, list) |
| 1104 | if (--i <= 0) |
| 1105 | break; |
| 1106 | /* |
| 1107 | * Check if the line needs to get reallocated. |
| 1108 | */ |
| 1109 | if (s->len != flen) { |
| 1110 | /* Reallocate string. */ |
| 1111 | n = tty3270_alloc_string(tp, flen); |
| 1112 | list_add(&n->list, &s->list); |
| 1113 | list_del_init(&s->list); |
| 1114 | if (!list_empty(&s->update)) |
| 1115 | list_del_init(&s->update); |
| 1116 | free_string(&tp->freemem, s); |
| 1117 | s = n; |
| 1118 | } |
| 1119 | |
| 1120 | /* Write 3270 data fragment. */ |
| 1121 | cp = s->string; |
| 1122 | *cp++ = TO_SBA; |
| 1123 | *cp++ = 0; |
| 1124 | *cp++ = 0; |
| 1125 | |
| 1126 | highlight = TAX_RESET; |
| 1127 | f_color = TAC_RESET; |
| 1128 | for (i = 0, cell = line->cells; i < line->len; i++, cell++) { |
| 1129 | if (cell->highlight != highlight) { |
| 1130 | *cp++ = TO_SA; |
| 1131 | *cp++ = TAT_EXTHI; |
| 1132 | *cp++ = cell->highlight; |
| 1133 | highlight = cell->highlight; |
| 1134 | } |
| 1135 | if (cell->f_color != f_color) { |
| 1136 | *cp++ = TO_SA; |
| 1137 | *cp++ = TAT_COLOR; |
| 1138 | *cp++ = cell->f_color; |
| 1139 | f_color = cell->f_color; |
| 1140 | } |
| 1141 | *cp++ = cell->character; |
| 1142 | } |
| 1143 | if (highlight != TAX_RESET) { |
| 1144 | *cp++ = TO_SA; |
| 1145 | *cp++ = TAT_EXTHI; |
| 1146 | *cp++ = TAX_RESET; |
| 1147 | } |
| 1148 | if (f_color != TAC_RESET) { |
| 1149 | *cp++ = TO_SA; |
| 1150 | *cp++ = TAT_COLOR; |
| 1151 | *cp++ = TAC_RESET; |
| 1152 | } |
| 1153 | if (line->len < tp->view.cols) { |
| 1154 | *cp++ = TO_RA; |
| 1155 | *cp++ = 0; |
| 1156 | *cp++ = 0; |
| 1157 | *cp++ = 0; |
| 1158 | } |
| 1159 | |
| 1160 | if (tp->nr_up + line_nr < tp->view.rows - 2) { |
| 1161 | /* Line is currently visible on screen. */ |
| 1162 | tty3270_update_string(tp, s, line_nr); |
| 1163 | /* Add line to update list. */ |
| 1164 | if (list_empty(&s->update)) { |
| 1165 | list_add_tail(&s->update, &tp->update); |
| 1166 | tp->update_flags |= TTY_UPDATE_LIST; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * Do carriage return. |
| 1173 | */ |
| 1174 | static void |
| 1175 | tty3270_cr(struct tty3270 *tp) |
| 1176 | { |
| 1177 | tp->cx = 0; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Do line feed. |
| 1182 | */ |
| 1183 | static void |
| 1184 | tty3270_lf(struct tty3270 *tp) |
| 1185 | { |
| 1186 | struct tty3270_line temp; |
| 1187 | int i; |
| 1188 | |
| 1189 | tty3270_convert_line(tp, tp->cy); |
| 1190 | if (tp->cy < tp->view.rows - 3) { |
| 1191 | tp->cy++; |
| 1192 | return; |
| 1193 | } |
| 1194 | /* Last line just filled up. Add new, blank line. */ |
| 1195 | tty3270_blank_line(tp); |
| 1196 | temp = tp->screen[0]; |
| 1197 | temp.len = 0; |
| 1198 | for (i = 0; i < tp->view.rows - 3; i++) |
| 1199 | tp->screen[i] = tp->screen[i+1]; |
| 1200 | tp->screen[tp->view.rows - 3] = temp; |
| 1201 | tty3270_rebuild_update(tp); |
| 1202 | } |
| 1203 | |
| 1204 | static void |
| 1205 | tty3270_ri(struct tty3270 *tp) |
| 1206 | { |
| 1207 | if (tp->cy > 0) { |
| 1208 | tty3270_convert_line(tp, tp->cy); |
| 1209 | tp->cy--; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * Insert characters at current position. |
| 1215 | */ |
| 1216 | static void |
| 1217 | tty3270_insert_characters(struct tty3270 *tp, int n) |
| 1218 | { |
| 1219 | struct tty3270_line *line; |
| 1220 | int k; |
| 1221 | |
| 1222 | line = tp->screen + tp->cy; |
| 1223 | while (line->len < tp->cx) { |
| 1224 | line->cells[line->len].character = tp->view.ascebc[' ']; |
| 1225 | line->cells[line->len].highlight = TAX_RESET; |
| 1226 | line->cells[line->len].f_color = TAC_RESET; |
| 1227 | line->len++; |
| 1228 | } |
| 1229 | if (n > tp->view.cols - tp->cx) |
| 1230 | n = tp->view.cols - tp->cx; |
| 1231 | k = min_t(int, line->len - tp->cx, tp->view.cols - tp->cx - n); |
| 1232 | while (k--) |
| 1233 | line->cells[tp->cx + n + k] = line->cells[tp->cx + k]; |
| 1234 | line->len += n; |
| 1235 | if (line->len > tp->view.cols) |
| 1236 | line->len = tp->view.cols; |
| 1237 | while (n-- > 0) { |
| 1238 | line->cells[tp->cx + n].character = tp->view.ascebc[' ']; |
| 1239 | line->cells[tp->cx + n].highlight = tp->highlight; |
| 1240 | line->cells[tp->cx + n].f_color = tp->f_color; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * Delete characters at current position. |
| 1246 | */ |
| 1247 | static void |
| 1248 | tty3270_delete_characters(struct tty3270 *tp, int n) |
| 1249 | { |
| 1250 | struct tty3270_line *line; |
| 1251 | int i; |
| 1252 | |
| 1253 | line = tp->screen + tp->cy; |
| 1254 | if (line->len <= tp->cx) |
| 1255 | return; |
| 1256 | if (line->len - tp->cx <= n) { |
| 1257 | line->len = tp->cx; |
| 1258 | return; |
| 1259 | } |
| 1260 | for (i = tp->cx; i + n < line->len; i++) |
| 1261 | line->cells[i] = line->cells[i + n]; |
| 1262 | line->len -= n; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Erase characters at current position. |
| 1267 | */ |
| 1268 | static void |
| 1269 | tty3270_erase_characters(struct tty3270 *tp, int n) |
| 1270 | { |
| 1271 | struct tty3270_line *line; |
| 1272 | struct tty3270_cell *cell; |
| 1273 | |
| 1274 | line = tp->screen + tp->cy; |
| 1275 | while (line->len > tp->cx && n-- > 0) { |
| 1276 | cell = line->cells + tp->cx++; |
| 1277 | cell->character = ' '; |
| 1278 | cell->highlight = TAX_RESET; |
| 1279 | cell->f_color = TAC_RESET; |
| 1280 | } |
| 1281 | tp->cx += n; |
| 1282 | tp->cx = min_t(int, tp->cx, tp->view.cols - 1); |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Erase line, 3 different cases: |
| 1287 | * Esc [ 0 K Erase from current position to end of line inclusive |
| 1288 | * Esc [ 1 K Erase from beginning of line to current position inclusive |
| 1289 | * Esc [ 2 K Erase entire line (without moving cursor) |
| 1290 | */ |
| 1291 | static void |
| 1292 | tty3270_erase_line(struct tty3270 *tp, int mode) |
| 1293 | { |
| 1294 | struct tty3270_line *line; |
| 1295 | struct tty3270_cell *cell; |
| 1296 | int i; |
| 1297 | |
| 1298 | line = tp->screen + tp->cy; |
| 1299 | if (mode == 0) |
| 1300 | line->len = tp->cx; |
| 1301 | else if (mode == 1) { |
| 1302 | for (i = 0; i < tp->cx; i++) { |
| 1303 | cell = line->cells + i; |
| 1304 | cell->character = ' '; |
| 1305 | cell->highlight = TAX_RESET; |
| 1306 | cell->f_color = TAC_RESET; |
| 1307 | } |
| 1308 | if (line->len <= tp->cx) |
| 1309 | line->len = tp->cx + 1; |
| 1310 | } else if (mode == 2) |
| 1311 | line->len = 0; |
| 1312 | tty3270_convert_line(tp, tp->cy); |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * Erase display, 3 different cases: |
| 1317 | * Esc [ 0 J Erase from current position to bottom of screen inclusive |
| 1318 | * Esc [ 1 J Erase from top of screen to current position inclusive |
| 1319 | * Esc [ 2 J Erase entire screen (without moving the cursor) |
| 1320 | */ |
| 1321 | static void |
| 1322 | tty3270_erase_display(struct tty3270 *tp, int mode) |
| 1323 | { |
| 1324 | int i; |
| 1325 | |
| 1326 | if (mode == 0) { |
| 1327 | tty3270_erase_line(tp, 0); |
| 1328 | for (i = tp->cy + 1; i < tp->view.rows - 2; i++) { |
| 1329 | tp->screen[i].len = 0; |
| 1330 | tty3270_convert_line(tp, i); |
| 1331 | } |
| 1332 | } else if (mode == 1) { |
| 1333 | for (i = 0; i < tp->cy; i++) { |
| 1334 | tp->screen[i].len = 0; |
| 1335 | tty3270_convert_line(tp, i); |
| 1336 | } |
| 1337 | tty3270_erase_line(tp, 1); |
| 1338 | } else if (mode == 2) { |
| 1339 | for (i = 0; i < tp->view.rows - 2; i++) { |
| 1340 | tp->screen[i].len = 0; |
| 1341 | tty3270_convert_line(tp, i); |
| 1342 | } |
| 1343 | } |
| 1344 | tty3270_rebuild_update(tp); |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * Set attributes found in an escape sequence. |
| 1349 | * Esc [ <attr> ; <attr> ; ... m |
| 1350 | */ |
| 1351 | static void |
| 1352 | tty3270_set_attributes(struct tty3270 *tp) |
| 1353 | { |
| 1354 | static unsigned char f_colors[] = { |
| 1355 | TAC_DEFAULT, TAC_RED, TAC_GREEN, TAC_YELLOW, TAC_BLUE, |
| 1356 | TAC_PINK, TAC_TURQ, TAC_WHITE, 0, TAC_DEFAULT |
| 1357 | }; |
| 1358 | int i, attr; |
| 1359 | |
| 1360 | for (i = 0; i <= tp->esc_npar; i++) { |
| 1361 | attr = tp->esc_par[i]; |
| 1362 | switch (attr) { |
| 1363 | case 0: /* Reset */ |
| 1364 | tp->highlight = TAX_RESET; |
| 1365 | tp->f_color = TAC_RESET; |
| 1366 | break; |
| 1367 | /* Highlight. */ |
| 1368 | case 4: /* Start underlining. */ |
| 1369 | tp->highlight = TAX_UNDER; |
| 1370 | break; |
| 1371 | case 5: /* Start blink. */ |
| 1372 | tp->highlight = TAX_BLINK; |
| 1373 | break; |
| 1374 | case 7: /* Start reverse. */ |
| 1375 | tp->highlight = TAX_REVER; |
| 1376 | break; |
| 1377 | case 24: /* End underlining */ |
| 1378 | if (tp->highlight == TAX_UNDER) |
| 1379 | tp->highlight = TAX_RESET; |
| 1380 | break; |
| 1381 | case 25: /* End blink. */ |
| 1382 | if (tp->highlight == TAX_BLINK) |
| 1383 | tp->highlight = TAX_RESET; |
| 1384 | break; |
| 1385 | case 27: /* End reverse. */ |
| 1386 | if (tp->highlight == TAX_REVER) |
| 1387 | tp->highlight = TAX_RESET; |
| 1388 | break; |
| 1389 | /* Foreground color. */ |
| 1390 | case 30: /* Black */ |
| 1391 | case 31: /* Red */ |
| 1392 | case 32: /* Green */ |
| 1393 | case 33: /* Yellow */ |
| 1394 | case 34: /* Blue */ |
| 1395 | case 35: /* Magenta */ |
| 1396 | case 36: /* Cyan */ |
| 1397 | case 37: /* White */ |
| 1398 | case 39: /* Black */ |
| 1399 | tp->f_color = f_colors[attr - 30]; |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | static inline int |
| 1406 | tty3270_getpar(struct tty3270 *tp, int ix) |
| 1407 | { |
| 1408 | return (tp->esc_par[ix] > 0) ? tp->esc_par[ix] : 1; |
| 1409 | } |
| 1410 | |
| 1411 | static void |
| 1412 | tty3270_goto_xy(struct tty3270 *tp, int cx, int cy) |
| 1413 | { |
Heiko Carstens | 364c855 | 2007-10-12 16:11:35 +0200 | [diff] [blame] | 1414 | int max_cx = max(0, cx); |
| 1415 | int max_cy = max(0, cy); |
| 1416 | |
| 1417 | tp->cx = min_t(int, tp->view.cols - 1, max_cx); |
| 1418 | cy = min_t(int, tp->view.rows - 3, max_cy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (cy != tp->cy) { |
| 1420 | tty3270_convert_line(tp, tp->cy); |
| 1421 | tp->cy = cy; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * Process escape sequences. Known sequences: |
| 1427 | * Esc 7 Save Cursor Position |
| 1428 | * Esc 8 Restore Cursor Position |
| 1429 | * Esc [ Pn ; Pn ; .. m Set attributes |
| 1430 | * Esc [ Pn ; Pn H Cursor Position |
| 1431 | * Esc [ Pn ; Pn f Cursor Position |
| 1432 | * Esc [ Pn A Cursor Up |
| 1433 | * Esc [ Pn B Cursor Down |
| 1434 | * Esc [ Pn C Cursor Forward |
| 1435 | * Esc [ Pn D Cursor Backward |
| 1436 | * Esc [ Pn G Cursor Horizontal Absolute |
| 1437 | * Esc [ Pn X Erase Characters |
| 1438 | * Esc [ Ps J Erase in Display |
| 1439 | * Esc [ Ps K Erase in Line |
| 1440 | * // FIXME: add all the new ones. |
| 1441 | * |
| 1442 | * Pn is a numeric parameter, a string of zero or more decimal digits. |
| 1443 | * Ps is a selective parameter. |
| 1444 | */ |
| 1445 | static void |
| 1446 | tty3270_escape_sequence(struct tty3270 *tp, char ch) |
| 1447 | { |
| 1448 | enum { ESnormal, ESesc, ESsquare, ESgetpars }; |
| 1449 | |
| 1450 | if (tp->esc_state == ESnormal) { |
| 1451 | if (ch == 0x1b) |
| 1452 | /* Starting new escape sequence. */ |
| 1453 | tp->esc_state = ESesc; |
| 1454 | return; |
| 1455 | } |
| 1456 | if (tp->esc_state == ESesc) { |
| 1457 | tp->esc_state = ESnormal; |
| 1458 | switch (ch) { |
| 1459 | case '[': |
| 1460 | tp->esc_state = ESsquare; |
| 1461 | break; |
| 1462 | case 'E': |
| 1463 | tty3270_cr(tp); |
| 1464 | tty3270_lf(tp); |
| 1465 | break; |
| 1466 | case 'M': |
| 1467 | tty3270_ri(tp); |
| 1468 | break; |
| 1469 | case 'D': |
| 1470 | tty3270_lf(tp); |
| 1471 | break; |
| 1472 | case 'Z': /* Respond ID. */ |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 1473 | kbd_puts_queue(&tp->port, "\033[?6c"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | break; |
| 1475 | case '7': /* Save cursor position. */ |
| 1476 | tp->saved_cx = tp->cx; |
| 1477 | tp->saved_cy = tp->cy; |
| 1478 | tp->saved_highlight = tp->highlight; |
| 1479 | tp->saved_f_color = tp->f_color; |
| 1480 | break; |
| 1481 | case '8': /* Restore cursor position. */ |
| 1482 | tty3270_convert_line(tp, tp->cy); |
| 1483 | tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy); |
| 1484 | tp->highlight = tp->saved_highlight; |
| 1485 | tp->f_color = tp->saved_f_color; |
| 1486 | break; |
| 1487 | case 'c': /* Reset terminal. */ |
| 1488 | tp->cx = tp->saved_cx = 0; |
| 1489 | tp->cy = tp->saved_cy = 0; |
| 1490 | tp->highlight = tp->saved_highlight = TAX_RESET; |
| 1491 | tp->f_color = tp->saved_f_color = TAC_RESET; |
| 1492 | tty3270_erase_display(tp, 2); |
| 1493 | break; |
| 1494 | } |
| 1495 | return; |
| 1496 | } |
| 1497 | if (tp->esc_state == ESsquare) { |
| 1498 | tp->esc_state = ESgetpars; |
| 1499 | memset(tp->esc_par, 0, sizeof(tp->esc_par)); |
| 1500 | tp->esc_npar = 0; |
| 1501 | tp->esc_ques = (ch == '?'); |
| 1502 | if (tp->esc_ques) |
| 1503 | return; |
| 1504 | } |
| 1505 | if (tp->esc_state == ESgetpars) { |
| 1506 | if (ch == ';' && tp->esc_npar < ESCAPE_NPAR - 1) { |
| 1507 | tp->esc_npar++; |
| 1508 | return; |
| 1509 | } |
| 1510 | if (ch >= '0' && ch <= '9') { |
| 1511 | tp->esc_par[tp->esc_npar] *= 10; |
| 1512 | tp->esc_par[tp->esc_npar] += ch - '0'; |
| 1513 | return; |
| 1514 | } |
| 1515 | } |
| 1516 | tp->esc_state = ESnormal; |
| 1517 | if (ch == 'n' && !tp->esc_ques) { |
| 1518 | if (tp->esc_par[0] == 5) /* Status report. */ |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 1519 | kbd_puts_queue(&tp->port, "\033[0n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | else if (tp->esc_par[0] == 6) { /* Cursor report. */ |
| 1521 | char buf[40]; |
| 1522 | sprintf(buf, "\033[%d;%dR", tp->cy + 1, tp->cx + 1); |
Jiri Slaby | ba186e7 | 2012-04-02 13:54:18 +0200 | [diff] [blame] | 1523 | kbd_puts_queue(&tp->port, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
| 1525 | return; |
| 1526 | } |
| 1527 | if (tp->esc_ques) |
| 1528 | return; |
| 1529 | switch (ch) { |
| 1530 | case 'm': |
| 1531 | tty3270_set_attributes(tp); |
| 1532 | break; |
| 1533 | case 'H': /* Set cursor position. */ |
| 1534 | case 'f': |
| 1535 | tty3270_goto_xy(tp, tty3270_getpar(tp, 1) - 1, |
| 1536 | tty3270_getpar(tp, 0) - 1); |
| 1537 | break; |
| 1538 | case 'd': /* Set y position. */ |
| 1539 | tty3270_goto_xy(tp, tp->cx, tty3270_getpar(tp, 0) - 1); |
| 1540 | break; |
| 1541 | case 'A': /* Cursor up. */ |
| 1542 | case 'F': |
| 1543 | tty3270_goto_xy(tp, tp->cx, tp->cy - tty3270_getpar(tp, 0)); |
| 1544 | break; |
| 1545 | case 'B': /* Cursor down. */ |
| 1546 | case 'e': |
| 1547 | case 'E': |
| 1548 | tty3270_goto_xy(tp, tp->cx, tp->cy + tty3270_getpar(tp, 0)); |
| 1549 | break; |
| 1550 | case 'C': /* Cursor forward. */ |
| 1551 | case 'a': |
| 1552 | tty3270_goto_xy(tp, tp->cx + tty3270_getpar(tp, 0), tp->cy); |
| 1553 | break; |
| 1554 | case 'D': /* Cursor backward. */ |
| 1555 | tty3270_goto_xy(tp, tp->cx - tty3270_getpar(tp, 0), tp->cy); |
| 1556 | break; |
| 1557 | case 'G': /* Set x position. */ |
| 1558 | case '`': |
| 1559 | tty3270_goto_xy(tp, tty3270_getpar(tp, 0), tp->cy); |
| 1560 | break; |
| 1561 | case 'X': /* Erase Characters. */ |
| 1562 | tty3270_erase_characters(tp, tty3270_getpar(tp, 0)); |
| 1563 | break; |
| 1564 | case 'J': /* Erase display. */ |
| 1565 | tty3270_erase_display(tp, tp->esc_par[0]); |
| 1566 | break; |
| 1567 | case 'K': /* Erase line. */ |
| 1568 | tty3270_erase_line(tp, tp->esc_par[0]); |
| 1569 | break; |
| 1570 | case 'P': /* Delete characters. */ |
| 1571 | tty3270_delete_characters(tp, tty3270_getpar(tp, 0)); |
| 1572 | break; |
| 1573 | case '@': /* Insert characters. */ |
| 1574 | tty3270_insert_characters(tp, tty3270_getpar(tp, 0)); |
| 1575 | break; |
| 1576 | case 's': /* Save cursor position. */ |
| 1577 | tp->saved_cx = tp->cx; |
| 1578 | tp->saved_cy = tp->cy; |
| 1579 | tp->saved_highlight = tp->highlight; |
| 1580 | tp->saved_f_color = tp->f_color; |
| 1581 | break; |
| 1582 | case 'u': /* Restore cursor position. */ |
| 1583 | tty3270_convert_line(tp, tp->cy); |
| 1584 | tty3270_goto_xy(tp, tp->saved_cx, tp->saved_cy); |
| 1585 | tp->highlight = tp->saved_highlight; |
| 1586 | tp->f_color = tp->saved_f_color; |
| 1587 | break; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | /* |
| 1592 | * String write routine for 3270 ttys |
| 1593 | */ |
| 1594 | static void |
Jiri Slaby | 20acdfa | 2012-04-02 13:54:17 +0200 | [diff] [blame] | 1595 | tty3270_do_write(struct tty3270 *tp, struct tty_struct *tty, |
| 1596 | const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | { |
| 1598 | int i_msg, i; |
| 1599 | |
| 1600 | spin_lock_bh(&tp->view.lock); |
Jiri Slaby | 20acdfa | 2012-04-02 13:54:17 +0200 | [diff] [blame] | 1601 | for (i_msg = 0; !tty->stopped && i_msg < count; i_msg++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | if (tp->esc_state != 0) { |
| 1603 | /* Continue escape sequence. */ |
| 1604 | tty3270_escape_sequence(tp, buf[i_msg]); |
| 1605 | continue; |
| 1606 | } |
| 1607 | |
| 1608 | switch (buf[i_msg]) { |
| 1609 | case 0x07: /* '\a' -- Alarm */ |
| 1610 | tp->wcc |= TW_PLUSALARM; |
| 1611 | break; |
| 1612 | case 0x08: /* Backspace. */ |
| 1613 | if (tp->cx > 0) { |
| 1614 | tp->cx--; |
| 1615 | tty3270_put_character(tp, ' '); |
| 1616 | } |
| 1617 | break; |
| 1618 | case 0x09: /* '\t' -- Tabulate */ |
| 1619 | for (i = tp->cx % 8; i < 8; i++) { |
| 1620 | if (tp->cx >= tp->view.cols) { |
| 1621 | tty3270_cr(tp); |
| 1622 | tty3270_lf(tp); |
| 1623 | break; |
| 1624 | } |
| 1625 | tty3270_put_character(tp, ' '); |
| 1626 | tp->cx++; |
| 1627 | } |
| 1628 | break; |
| 1629 | case 0x0a: /* '\n' -- New Line */ |
| 1630 | tty3270_cr(tp); |
| 1631 | tty3270_lf(tp); |
| 1632 | break; |
| 1633 | case 0x0c: /* '\f' -- Form Feed */ |
| 1634 | tty3270_erase_display(tp, 2); |
| 1635 | tp->cx = tp->cy = 0; |
| 1636 | break; |
| 1637 | case 0x0d: /* '\r' -- Carriage Return */ |
| 1638 | tp->cx = 0; |
| 1639 | break; |
| 1640 | case 0x0f: /* SuSE "exit alternate mode" */ |
| 1641 | break; |
| 1642 | case 0x1b: /* Start escape sequence. */ |
| 1643 | tty3270_escape_sequence(tp, buf[i_msg]); |
| 1644 | break; |
| 1645 | default: /* Insert normal character. */ |
| 1646 | if (tp->cx >= tp->view.cols) { |
| 1647 | tty3270_cr(tp); |
| 1648 | tty3270_lf(tp); |
| 1649 | } |
| 1650 | tty3270_put_character(tp, buf[i_msg]); |
| 1651 | tp->cx++; |
| 1652 | break; |
| 1653 | } |
| 1654 | } |
| 1655 | /* Convert current line to 3270 data fragment. */ |
| 1656 | tty3270_convert_line(tp, tp->cy); |
| 1657 | |
| 1658 | /* Setup timer to update display after 1/10 second */ |
| 1659 | if (!timer_pending(&tp->timer)) |
| 1660 | tty3270_set_timer(tp, HZ/10); |
| 1661 | |
| 1662 | spin_unlock_bh(&tp->view.lock); |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * String write routine for 3270 ttys |
| 1667 | */ |
| 1668 | static int |
| 1669 | tty3270_write(struct tty_struct * tty, |
| 1670 | const unsigned char *buf, int count) |
| 1671 | { |
| 1672 | struct tty3270 *tp; |
| 1673 | |
| 1674 | tp = tty->driver_data; |
| 1675 | if (!tp) |
| 1676 | return 0; |
| 1677 | if (tp->char_count > 0) { |
Jiri Slaby | 20acdfa | 2012-04-02 13:54:17 +0200 | [diff] [blame] | 1678 | tty3270_do_write(tp, tty, tp->char_buf, tp->char_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | tp->char_count = 0; |
| 1680 | } |
Jiri Slaby | 20acdfa | 2012-04-02 13:54:17 +0200 | [diff] [blame] | 1681 | tty3270_do_write(tp, tty, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | return count; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
| 1686 | * Put single characters to the ttys character buffer |
| 1687 | */ |
Heiko Carstens | 74c76c8 | 2008-05-07 09:22:58 +0200 | [diff] [blame] | 1688 | static int tty3270_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | { |
| 1690 | struct tty3270 *tp; |
| 1691 | |
| 1692 | tp = tty->driver_data; |
Heiko Carstens | 74c76c8 | 2008-05-07 09:22:58 +0200 | [diff] [blame] | 1693 | if (!tp || tp->char_count >= TTY3270_CHAR_BUF_SIZE) |
| 1694 | return 0; |
| 1695 | tp->char_buf[tp->char_count++] = ch; |
| 1696 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | * Flush all characters from the ttys characeter buffer put there |
| 1701 | * by tty3270_put_char. |
| 1702 | */ |
| 1703 | static void |
| 1704 | tty3270_flush_chars(struct tty_struct *tty) |
| 1705 | { |
| 1706 | struct tty3270 *tp; |
| 1707 | |
| 1708 | tp = tty->driver_data; |
| 1709 | if (!tp) |
| 1710 | return; |
| 1711 | if (tp->char_count > 0) { |
Jiri Slaby | 20acdfa | 2012-04-02 13:54:17 +0200 | [diff] [blame] | 1712 | tty3270_do_write(tp, tty, tp->char_buf, tp->char_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | tp->char_count = 0; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * Returns the number of characters in the output buffer. This is |
| 1719 | * used in tty_wait_until_sent to wait until all characters have |
| 1720 | * appeared on the screen. |
| 1721 | */ |
| 1722 | static int |
| 1723 | tty3270_chars_in_buffer(struct tty_struct *tty) |
| 1724 | { |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static void |
| 1729 | tty3270_flush_buffer(struct tty_struct *tty) |
| 1730 | { |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Check for visible/invisible input switches |
| 1735 | */ |
| 1736 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1737 | tty3270_set_termios(struct tty_struct *tty, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | { |
| 1739 | struct tty3270 *tp; |
| 1740 | int new; |
| 1741 | |
| 1742 | tp = tty->driver_data; |
| 1743 | if (!tp) |
| 1744 | return; |
| 1745 | spin_lock_bh(&tp->view.lock); |
| 1746 | if (L_ICANON(tty)) { |
| 1747 | new = L_ECHO(tty) ? TF_INPUT: TF_INPUTN; |
| 1748 | if (new != tp->inattr) { |
| 1749 | tp->inattr = new; |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1750 | tty3270_update_prompt(tp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | tty3270_set_timer(tp, 1); |
| 1752 | } |
| 1753 | } |
| 1754 | spin_unlock_bh(&tp->view.lock); |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Disable reading from a 3270 tty |
| 1759 | */ |
| 1760 | static void |
| 1761 | tty3270_throttle(struct tty_struct * tty) |
| 1762 | { |
| 1763 | struct tty3270 *tp; |
| 1764 | |
| 1765 | tp = tty->driver_data; |
| 1766 | if (!tp) |
| 1767 | return; |
| 1768 | tp->throttle = 1; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
| 1772 | * Enable reading from a 3270 tty |
| 1773 | */ |
| 1774 | static void |
| 1775 | tty3270_unthrottle(struct tty_struct * tty) |
| 1776 | { |
| 1777 | struct tty3270 *tp; |
| 1778 | |
| 1779 | tp = tty->driver_data; |
| 1780 | if (!tp) |
| 1781 | return; |
| 1782 | tp->throttle = 0; |
| 1783 | if (tp->attn) |
| 1784 | tty3270_issue_read(tp, 1); |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Hang up the tty device. |
| 1789 | */ |
| 1790 | static void |
| 1791 | tty3270_hangup(struct tty_struct *tty) |
| 1792 | { |
| 1793 | // FIXME: implement |
| 1794 | } |
| 1795 | |
| 1796 | static void |
| 1797 | tty3270_wait_until_sent(struct tty_struct *tty, int timeout) |
| 1798 | { |
| 1799 | } |
| 1800 | |
Heiko Carstens | 65c56e0 | 2011-02-25 14:28:30 +0100 | [diff] [blame] | 1801 | static int tty3270_ioctl(struct tty_struct *tty, unsigned int cmd, |
| 1802 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | { |
| 1804 | struct tty3270 *tp; |
| 1805 | |
| 1806 | tp = tty->driver_data; |
| 1807 | if (!tp) |
| 1808 | return -ENODEV; |
| 1809 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 1810 | return -EIO; |
Heiko Carstens | 65c56e0 | 2011-02-25 14:28:30 +0100 | [diff] [blame] | 1811 | return kbd_ioctl(tp->kbd, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Arnd Bergmann | 9d4bfd4 | 2009-12-07 12:52:13 +0100 | [diff] [blame] | 1814 | #ifdef CONFIG_COMPAT |
Heiko Carstens | 65c56e0 | 2011-02-25 14:28:30 +0100 | [diff] [blame] | 1815 | static long tty3270_compat_ioctl(struct tty_struct *tty, |
| 1816 | unsigned int cmd, unsigned long arg) |
Arnd Bergmann | 9d4bfd4 | 2009-12-07 12:52:13 +0100 | [diff] [blame] | 1817 | { |
| 1818 | struct tty3270 *tp; |
| 1819 | |
| 1820 | tp = tty->driver_data; |
| 1821 | if (!tp) |
| 1822 | return -ENODEV; |
| 1823 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 1824 | return -EIO; |
Heiko Carstens | 65c56e0 | 2011-02-25 14:28:30 +0100 | [diff] [blame] | 1825 | return kbd_ioctl(tp->kbd, cmd, (unsigned long)compat_ptr(arg)); |
Arnd Bergmann | 9d4bfd4 | 2009-12-07 12:52:13 +0100 | [diff] [blame] | 1826 | } |
| 1827 | #endif |
| 1828 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1829 | static const struct tty_operations tty3270_ops = { |
Jiri Slaby | 20cda6f | 2012-08-07 21:48:03 +0200 | [diff] [blame] | 1830 | .install = tty3270_install, |
| 1831 | .cleanup = tty3270_cleanup, |
Martin Schwidefsky | 736c9fd | 2013-01-08 15:15:12 +0100 | [diff] [blame] | 1832 | .open = tty3270_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | .close = tty3270_close, |
| 1834 | .write = tty3270_write, |
| 1835 | .put_char = tty3270_put_char, |
| 1836 | .flush_chars = tty3270_flush_chars, |
| 1837 | .write_room = tty3270_write_room, |
| 1838 | .chars_in_buffer = tty3270_chars_in_buffer, |
| 1839 | .flush_buffer = tty3270_flush_buffer, |
| 1840 | .throttle = tty3270_throttle, |
| 1841 | .unthrottle = tty3270_unthrottle, |
| 1842 | .hangup = tty3270_hangup, |
| 1843 | .wait_until_sent = tty3270_wait_until_sent, |
| 1844 | .ioctl = tty3270_ioctl, |
Arnd Bergmann | 9d4bfd4 | 2009-12-07 12:52:13 +0100 | [diff] [blame] | 1845 | #ifdef CONFIG_COMPAT |
| 1846 | .compat_ioctl = tty3270_compat_ioctl, |
| 1847 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | .set_termios = tty3270_set_termios |
| 1849 | }; |
| 1850 | |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 1851 | static void tty3270_create_cb(int minor) |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1852 | { |
Martin Schwidefsky | 1fcbba3 | 2013-03-21 13:07:18 +0100 | [diff] [blame] | 1853 | tty_register_device(tty3270_driver, minor - RAW3270_FIRSTMINOR, NULL); |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 1856 | static void tty3270_destroy_cb(int minor) |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1857 | { |
Martin Schwidefsky | 1fcbba3 | 2013-03-21 13:07:18 +0100 | [diff] [blame] | 1858 | tty_unregister_device(tty3270_driver, minor - RAW3270_FIRSTMINOR); |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1859 | } |
| 1860 | |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 1861 | static struct raw3270_notifier tty3270_notifier = |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1862 | { |
| 1863 | .create = tty3270_create_cb, |
| 1864 | .destroy = tty3270_destroy_cb, |
| 1865 | }; |
| 1866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | /* |
| 1868 | * 3270 tty registration code called from tty_init(). |
| 1869 | * Most kernel services (incl. kmalloc) are available at this poimt. |
| 1870 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 1871 | static int __init tty3270_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | { |
| 1873 | struct tty_driver *driver; |
| 1874 | int ret; |
| 1875 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1876 | driver = tty_alloc_driver(RAW3270_MAXDEVS, |
| 1877 | TTY_DRIVER_REAL_RAW | |
| 1878 | TTY_DRIVER_DYNAMIC_DEV | |
| 1879 | TTY_DRIVER_RESET_TERMIOS); |
| 1880 | if (IS_ERR(driver)) |
| 1881 | return PTR_ERR(driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
| 1883 | /* |
| 1884 | * Initialize the tty_driver structure |
| 1885 | * Entries in tty3270_driver that are NOT initialized: |
| 1886 | * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc |
| 1887 | */ |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1888 | driver->driver_name = "tty3270"; |
| 1889 | driver->name = "3270/tty"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | driver->major = IBM_TTY3270_MAJOR; |
Martin Schwidefsky | 1fcbba3 | 2013-03-21 13:07:18 +0100 | [diff] [blame] | 1891 | driver->minor_start = RAW3270_FIRSTMINOR; |
| 1892 | driver->name_base = RAW3270_FIRSTMINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 1894 | driver->subtype = SYSTEM_TYPE_TTY; |
| 1895 | driver->init_termios = tty_std_termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | tty_set_operations(driver, &tty3270_ops); |
| 1897 | ret = tty_register_driver(driver); |
| 1898 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | put_tty_driver(driver); |
| 1900 | return ret; |
| 1901 | } |
| 1902 | tty3270_driver = driver; |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1903 | raw3270_register_notifier(&tty3270_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | return 0; |
| 1905 | } |
| 1906 | |
| 1907 | static void __exit |
| 1908 | tty3270_exit(void) |
| 1909 | { |
| 1910 | struct tty_driver *driver; |
| 1911 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1912 | raw3270_unregister_notifier(&tty3270_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | driver = tty3270_driver; |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1914 | tty3270_driver = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | tty_unregister_driver(driver); |
Jiri Slaby | 2312e4f | 2012-08-07 21:47:41 +0200 | [diff] [blame] | 1916 | put_tty_driver(driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | tty3270_del_views(); |
| 1918 | } |
| 1919 | |
| 1920 | MODULE_LICENSE("GPL"); |
| 1921 | MODULE_ALIAS_CHARDEV_MAJOR(IBM_TTY3270_MAJOR); |
| 1922 | |
| 1923 | module_init(tty3270_init); |
| 1924 | module_exit(tty3270_exit); |