]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/char/fs3270.c
Merge tag 'mmc-v4.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / char / fs3270.c
CommitLineData
6f05e69e 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
4b214a0c 3 * IBM/3270 Driver - fullscreen driver.
1da177e4 4 *
4b214a0c
MS
5 * Author(s):
6 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7 * Rewritten for 2.5/2.6 by Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * Copyright IBM Corp. 2003, 2009
1da177e4
LT
9 */
10
1da177e4
LT
11#include <linux/bootmem.h>
12#include <linux/console.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
048cd4e5 15#include <linux/compat.h>
3f07c014 16#include <linux/sched/signal.h>
3a4c5d59 17#include <linux/module.h>
1da177e4 18#include <linux/list.h>
5a0e3ad6 19#include <linux/slab.h>
1da177e4
LT
20#include <linux/types.h>
21
16e1a577 22#include <asm/compat.h>
1da177e4
LT
23#include <asm/ccwdev.h>
24#include <asm/cio.h>
1da177e4
LT
25#include <asm/ebcdic.h>
26#include <asm/idals.h>
27
28#include "raw3270.h"
29#include "ctrlchar.h"
30
2b67fc46 31static struct raw3270_fn fs3270_fn;
1da177e4
LT
32
33struct fs3270 {
34 struct raw3270_view view;
782237a2 35 struct pid *fs_pid; /* Pid of controlling program. */
1da177e4
LT
36 int read_command; /* ccw command to use for reads. */
37 int write_command; /* ccw command to use for writes. */
38 int attention; /* Got attention. */
ed3cb6f0
RH
39 int active; /* Fullscreen view is active. */
40 struct raw3270_request *init; /* single init request. */
41 wait_queue_head_t wait; /* Init & attention wait queue. */
42 struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */
43 size_t rdbuf_size; /* size of data returned by RDBUF */
1da177e4
LT
44};
45
5cbb5f57
MS
46static DEFINE_MUTEX(fs3270_mutex);
47
1da177e4
LT
48static void
49fs3270_wake_up(struct raw3270_request *rq, void *data)
50{
51 wake_up((wait_queue_head_t *) data);
52}
53
ed3cb6f0
RH
54static inline int
55fs3270_working(struct fs3270 *fp)
56{
57 /*
58 * The fullscreen view is in working order if the view
59 * has been activated AND the initial request is finished.
60 */
61 return fp->active && raw3270_request_final(fp->init);
62}
63
1da177e4
LT
64static int
65fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
66{
ed3cb6f0 67 struct fs3270 *fp;
1da177e4
LT
68 int rc;
69
ed3cb6f0 70 fp = (struct fs3270 *) view;
1da177e4 71 rq->callback = fs3270_wake_up;
ed3cb6f0
RH
72 rq->callback_data = &fp->wait;
73
74 do {
75 if (!fs3270_working(fp)) {
76 /* Fullscreen view isn't ready yet. */
77 rc = wait_event_interruptible(fp->wait,
78 fs3270_working(fp));
79 if (rc != 0)
80 break;
81 }
82 rc = raw3270_start(view, rq);
83 if (rc == 0) {
af901ca1 84 /* Started successfully. Now wait for completion. */
ed3cb6f0
RH
85 wait_event(fp->wait, raw3270_request_final(rq));
86 }
87 } while (rc == -EACCES);
88 return rc;
1da177e4
LT
89}
90
ed3cb6f0
RH
91/*
92 * Switch to the fullscreen view.
93 */
1da177e4
LT
94static void
95fs3270_reset_callback(struct raw3270_request *rq, void *data)
96{
ed3cb6f0
RH
97 struct fs3270 *fp;
98
99 fp = (struct fs3270 *) rq->view;
1da177e4 100 raw3270_request_reset(rq);
ed3cb6f0
RH
101 wake_up(&fp->wait);
102}
103
104static void
105fs3270_restore_callback(struct raw3270_request *rq, void *data)
106{
107 struct fs3270 *fp;
108
109 fp = (struct fs3270 *) rq->view;
110 if (rq->rc != 0 || rq->rescnt != 0) {
111 if (fp->fs_pid)
782237a2 112 kill_pid(fp->fs_pid, SIGHUP, 1);
ed3cb6f0
RH
113 }
114 fp->rdbuf_size = 0;
115 raw3270_request_reset(rq);
116 wake_up(&fp->wait);
1da177e4
LT
117}
118
1da177e4
LT
119static int
120fs3270_activate(struct raw3270_view *view)
121{
122 struct fs3270 *fp;
ed3cb6f0
RH
123 char *cp;
124 int rc;
1da177e4
LT
125
126 fp = (struct fs3270 *) view;
ed3cb6f0
RH
127
128 /* If an old init command is still running just return. */
129 if (!raw3270_request_final(fp->init))
130 return 0;
131
132 if (fp->rdbuf_size == 0) {
133 /* No saved buffer. Just clear the screen. */
134 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
135 fp->init->callback = fs3270_reset_callback;
136 } else {
137 /* Restore fullscreen buffer saved by fs3270_deactivate. */
138 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
139 raw3270_request_set_idal(fp->init, fp->rdbuf);
140 fp->init->ccw.count = fp->rdbuf_size;
141 cp = fp->rdbuf->data[0];
142 cp[0] = TW_KR;
143 cp[1] = TO_SBA;
144 cp[2] = cp[6];
145 cp[3] = cp[7];
146 cp[4] = TO_IC;
147 cp[5] = TO_SBA;
148 cp[6] = 0x40;
149 cp[7] = 0x40;
150 fp->init->rescnt = 0;
151 fp->init->callback = fs3270_restore_callback;
152 }
153 rc = fp->init->rc = raw3270_start_locked(view, fp->init);
154 if (rc)
155 fp->init->callback(fp->init, NULL);
156 else
157 fp->active = 1;
158 return rc;
1da177e4
LT
159}
160
161/*
162 * Shutdown fullscreen view.
163 */
ed3cb6f0
RH
164static void
165fs3270_save_callback(struct raw3270_request *rq, void *data)
166{
167 struct fs3270 *fp;
168
169 fp = (struct fs3270 *) rq->view;
170
171 /* Correct idal buffer element 0 address. */
172 fp->rdbuf->data[0] -= 5;
173 fp->rdbuf->size += 5;
174
175 /*
176 * If the rdbuf command failed or the idal buffer is
177 * to small for the amount of data returned by the
178 * rdbuf command, then we have no choice but to send
179 * a SIGHUP to the application.
180 */
181 if (rq->rc != 0 || rq->rescnt == 0) {
182 if (fp->fs_pid)
782237a2 183 kill_pid(fp->fs_pid, SIGHUP, 1);
ed3cb6f0
RH
184 fp->rdbuf_size = 0;
185 } else
186 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
187 raw3270_request_reset(rq);
188 wake_up(&fp->wait);
189}
190
1da177e4
LT
191static void
192fs3270_deactivate(struct raw3270_view *view)
193{
1da177e4
LT
194 struct fs3270 *fp;
195
196 fp = (struct fs3270 *) view;
ed3cb6f0
RH
197 fp->active = 0;
198
199 /* If an old init command is still running just return. */
200 if (!raw3270_request_final(fp->init))
201 return;
202
203 /* Prepare read-buffer request. */
204 raw3270_request_set_cmd(fp->init, TC_RDBUF);
205 /*
206 * Hackish: skip first 5 bytes of the idal buffer to make
207 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
208 * in the activation command.
209 */
210 fp->rdbuf->data[0] += 5;
211 fp->rdbuf->size -= 5;
212 raw3270_request_set_idal(fp->init, fp->rdbuf);
213 fp->init->rescnt = 0;
214 fp->init->callback = fs3270_save_callback;
215
216 /* Start I/O to read in the 3270 buffer. */
217 fp->init->rc = raw3270_start_locked(view, fp->init);
218 if (fp->init->rc)
219 fp->init->callback(fp->init, NULL);
1da177e4
LT
220}
221
8340ab60 222static void
1da177e4
LT
223fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
224{
225 /* Handle ATTN. Set indication and wake waiters for attention. */
23d805b6 226 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
1da177e4 227 fp->attention = 1;
ed3cb6f0 228 wake_up(&fp->wait);
1da177e4
LT
229 }
230
231 if (rq) {
23d805b6 232 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
1da177e4
LT
233 rq->rc = -EIO;
234 else
235 /* Normal end. Copy residual count. */
23d805b6 236 rq->rescnt = irb->scsw.cmd.count;
1da177e4 237 }
1da177e4
LT
238}
239
240/*
241 * Process reads from fullscreen 3270.
242 */
243static ssize_t
d2c993d8 244fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
1da177e4
LT
245{
246 struct fs3270 *fp;
247 struct raw3270_request *rq;
248 struct idal_buffer *ib;
ed3cb6f0 249 ssize_t rc;
1da177e4
LT
250
251 if (count == 0 || count > 65535)
252 return -EINVAL;
253 fp = filp->private_data;
254 if (!fp)
255 return -ENODEV;
256 ib = idal_buffer_alloc(count, 0);
ed3cb6f0 257 if (IS_ERR(ib))
1da177e4
LT
258 return -ENOMEM;
259 rq = raw3270_request_alloc(0);
260 if (!IS_ERR(rq)) {
261 if (fp->read_command == 0 && fp->write_command != 0)
262 fp->read_command = 6;
263 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
264 raw3270_request_set_idal(rq, ib);
ed3cb6f0
RH
265 rc = wait_event_interruptible(fp->wait, fp->attention);
266 fp->attention = 0;
267 if (rc == 0) {
268 rc = fs3270_do_io(&fp->view, rq);
269 if (rc == 0) {
270 count -= rq->rescnt;
271 if (idal_buffer_to_user(ib, data, count) != 0)
272 rc = -EFAULT;
273 else
274 rc = count;
275
276 }
277 }
1da177e4
LT
278 raw3270_request_free(rq);
279 } else
280 rc = PTR_ERR(rq);
281 idal_buffer_free(ib);
282 return rc;
283}
284
285/*
286 * Process writes to fullscreen 3270.
287 */
288static ssize_t
d2c993d8 289fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
1da177e4
LT
290{
291 struct fs3270 *fp;
292 struct raw3270_request *rq;
293 struct idal_buffer *ib;
294 int write_command;
ed3cb6f0 295 ssize_t rc;
1da177e4
LT
296
297 fp = filp->private_data;
298 if (!fp)
299 return -ENODEV;
300 ib = idal_buffer_alloc(count, 0);
ed3cb6f0 301 if (IS_ERR(ib))
1da177e4
LT
302 return -ENOMEM;
303 rq = raw3270_request_alloc(0);
304 if (!IS_ERR(rq)) {
305 if (idal_buffer_from_user(ib, data, count) == 0) {
306 write_command = fp->write_command ? : 1;
307 if (write_command == 5)
308 write_command = 13;
309 raw3270_request_set_cmd(rq, write_command);
310 raw3270_request_set_idal(rq, ib);
311 rc = fs3270_do_io(&fp->view, rq);
ed3cb6f0
RH
312 if (rc == 0)
313 rc = count - rq->rescnt;
1da177e4
LT
314 } else
315 rc = -EFAULT;
316 raw3270_request_free(rq);
317 } else
318 rc = PTR_ERR(rq);
319 idal_buffer_free(ib);
320 return rc;
321}
322
323/*
324 * process ioctl commands for the tube driver
325 */
0f75e00c
CH
326static long
327fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 328{
16e1a577 329 char __user *argp;
1da177e4
LT
330 struct fs3270 *fp;
331 struct raw3270_iocb iocb;
332 int rc;
333
334 fp = filp->private_data;
335 if (!fp)
336 return -ENODEV;
16e1a577
HC
337 if (is_compat_task())
338 argp = compat_ptr(arg);
339 else
340 argp = (char __user *)arg;
1da177e4 341 rc = 0;
5cbb5f57 342 mutex_lock(&fs3270_mutex);
1da177e4
LT
343 switch (cmd) {
344 case TUBICMD:
345 fp->read_command = arg;
346 break;
347 case TUBOCMD:
348 fp->write_command = arg;
349 break;
350 case TUBGETI:
16e1a577 351 rc = put_user(fp->read_command, argp);
1da177e4
LT
352 break;
353 case TUBGETO:
16e1a577 354 rc = put_user(fp->write_command, argp);
1da177e4
LT
355 break;
356 case TUBGETMOD:
357 iocb.model = fp->view.model;
358 iocb.line_cnt = fp->view.rows;
359 iocb.col_cnt = fp->view.cols;
360 iocb.pf_cnt = 24;
361 iocb.re_cnt = 20;
362 iocb.map = 0;
16e1a577 363 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
1da177e4
LT
364 rc = -EFAULT;
365 break;
366 }
5cbb5f57 367 mutex_unlock(&fs3270_mutex);
1da177e4
LT
368 return rc;
369}
370
371/*
ed3cb6f0 372 * Allocate fs3270 structure.
1da177e4
LT
373 */
374static struct fs3270 *
375fs3270_alloc_view(void)
376{
377 struct fs3270 *fp;
378
88abaab4 379 fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
1da177e4
LT
380 if (!fp)
381 return ERR_PTR(-ENOMEM);
ed3cb6f0
RH
382 fp->init = raw3270_request_alloc(0);
383 if (IS_ERR(fp->init)) {
1da177e4
LT
384 kfree(fp);
385 return ERR_PTR(-ENOMEM);
386 }
387 return fp;
388}
389
390/*
ed3cb6f0 391 * Free fs3270 structure.
1da177e4
LT
392 */
393static void
394fs3270_free_view(struct raw3270_view *view)
395{
ed3cb6f0
RH
396 struct fs3270 *fp;
397
398 fp = (struct fs3270 *) view;
399 if (fp->rdbuf)
400 idal_buffer_free(fp->rdbuf);
401 raw3270_request_free(((struct fs3270 *) view)->init);
1da177e4
LT
402 kfree(view);
403}
404
405/*
406 * Unlink fs3270 data structure from filp.
407 */
408static void
409fs3270_release(struct raw3270_view *view)
410{
4b214a0c
MS
411 struct fs3270 *fp;
412
413 fp = (struct fs3270 *) view;
414 if (fp->fs_pid)
415 kill_pid(fp->fs_pid, SIGHUP, 1);
1da177e4
LT
416}
417
418/* View to a 3270 device. Can be console, tty or fullscreen. */
2b67fc46 419static struct raw3270_fn fs3270_fn = {
1da177e4
LT
420 .activate = fs3270_activate,
421 .deactivate = fs3270_deactivate,
422 .intv = (void *) fs3270_irq,
423 .release = fs3270_release,
424 .free = fs3270_free_view
425};
426
427/*
428 * This routine is called whenever a 3270 fullscreen device is opened.
429 */
430static int
431fs3270_open(struct inode *inode, struct file *filp)
432{
433 struct fs3270 *fp;
ed3cb6f0 434 struct idal_buffer *ib;
a18992d4 435 int minor, rc = 0;
1da177e4 436
496ad9aa 437 if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR)
1da177e4 438 return -ENODEV;
496ad9aa 439 minor = iminor(file_inode(filp));
ed3cb6f0
RH
440 /* Check for minor 0 multiplexer. */
441 if (minor == 0) {
a90610e5 442 struct tty_struct *tty = get_current_tty();
24ec839c 443 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
452a00d2 444 tty_kref_put(tty);
a18992d4 445 return -ENODEV;
24ec839c 446 }
c95571e6 447 minor = tty->index;
452a00d2 448 tty_kref_put(tty);
ed3cb6f0 449 }
5cbb5f57 450 mutex_lock(&fs3270_mutex);
1da177e4
LT
451 /* Check if some other program is already using fullscreen mode. */
452 fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
453 if (!IS_ERR(fp)) {
454 raw3270_put_view(&fp->view);
764a4a8e
JC
455 rc = -EBUSY;
456 goto out;
1da177e4
LT
457 }
458 /* Allocate fullscreen view structure. */
459 fp = fs3270_alloc_view();
764a4a8e
JC
460 if (IS_ERR(fp)) {
461 rc = PTR_ERR(fp);
462 goto out;
463 }
1da177e4 464
ed3cb6f0 465 init_waitqueue_head(&fp->wait);
782237a2 466 fp->fs_pid = get_pid(task_pid(current));
1da177e4
LT
467 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
468 if (rc) {
469 fs3270_free_view(&fp->view);
764a4a8e 470 goto out;
1da177e4
LT
471 }
472
ed3cb6f0
RH
473 /* Allocate idal-buffer. */
474 ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
475 if (IS_ERR(ib)) {
476 raw3270_put_view(&fp->view);
477 raw3270_del_view(&fp->view);
2b31001d 478 rc = PTR_ERR(ib);
764a4a8e 479 goto out;
ed3cb6f0
RH
480 }
481 fp->rdbuf = ib;
482
1da177e4
LT
483 rc = raw3270_activate_view(&fp->view);
484 if (rc) {
ed3cb6f0 485 raw3270_put_view(&fp->view);
1da177e4 486 raw3270_del_view(&fp->view);
764a4a8e 487 goto out;
1da177e4 488 }
58ea91c0 489 nonseekable_open(inode, filp);
1da177e4 490 filp->private_data = fp;
764a4a8e 491out:
5cbb5f57 492 mutex_unlock(&fs3270_mutex);
a18992d4 493 return rc;
1da177e4
LT
494}
495
496/*
497 * This routine is called when the 3270 tty is closed. We wait
498 * for the remaining request to be completed. Then we clean up.
499 */
500static int
501fs3270_close(struct inode *inode, struct file *filp)
502{
503 struct fs3270 *fp;
504
505 fp = filp->private_data;
d2c993d8 506 filp->private_data = NULL;
ed3cb6f0 507 if (fp) {
782237a2
CLG
508 put_pid(fp->fs_pid);
509 fp->fs_pid = NULL;
ed3cb6f0
RH
510 raw3270_reset(&fp->view);
511 raw3270_put_view(&fp->view);
1da177e4 512 raw3270_del_view(&fp->view);
ed3cb6f0 513 }
1da177e4
LT
514 return 0;
515}
516
d54b1fdb 517static const struct file_operations fs3270_fops = {
0f75e00c
CH
518 .owner = THIS_MODULE, /* owner */
519 .read = fs3270_read, /* read */
520 .write = fs3270_write, /* write */
521 .unlocked_ioctl = fs3270_ioctl, /* ioctl */
522 .compat_ioctl = fs3270_ioctl, /* ioctl */
16e1a577
HC
523 .open = fs3270_open, /* open */
524 .release = fs3270_close, /* release */
6038f373 525 .llseek = no_llseek,
1da177e4
LT
526};
527
63df41d6 528static void fs3270_create_cb(int minor)
c95571e6
MS
529{
530 __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops);
531 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor),
532 NULL, "3270/tub%d", minor);
533}
534
63df41d6 535static void fs3270_destroy_cb(int minor)
c95571e6
MS
536{
537 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, minor));
538 __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub");
539}
540
63df41d6 541static struct raw3270_notifier fs3270_notifier =
c95571e6
MS
542{
543 .create = fs3270_create_cb,
544 .destroy = fs3270_destroy_cb,
545};
546
1da177e4
LT
547/*
548 * 3270 fullscreen driver initialization.
549 */
550static int __init
551fs3270_init(void)
552{
553 int rc;
554
c95571e6 555 rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops);
a26182ed 556 if (rc)
1da177e4 557 return rc;
c95571e6
MS
558 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0),
559 NULL, "3270/tub");
560 raw3270_register_notifier(&fs3270_notifier);
1da177e4
LT
561 return 0;
562}
563
564static void __exit
565fs3270_exit(void)
566{
c95571e6 567 raw3270_unregister_notifier(&fs3270_notifier);
d1e61fe4 568 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, 0));
c95571e6 569 __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270");
1da177e4
LT
570}
571
572MODULE_LICENSE("GPL");
573MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
574
575module_init(fs3270_init);
576module_exit(fs3270_exit);