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