blob: 911704571b9c1a862f4d8819bb89ad8f0f4c000f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02002 * IBM/3270 Driver - fullscreen driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02004 * Author(s):
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5/2.6 by Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Copyright IBM Corp. 2003, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/bootmem.h>
11#include <linux/console.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010014#include <linux/compat.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/types.h>
19
Heiko Carstens16e1a572010-01-13 20:44:32 +010020#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/ccwdev.h>
22#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/ebcdic.h>
24#include <asm/idals.h>
25
26#include "raw3270.h"
27#include "ctrlchar.h"
28
Heiko Carstens2b67fc42007-02-05 21:16:47 +010029static struct raw3270_fn fs3270_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct fs3270 {
32 struct raw3270_view view;
Cedric Le Goater782237a2006-10-02 02:17:28 -070033 struct pid *fs_pid; /* Pid of controlling program. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int read_command; /* ccw command to use for reads. */
35 int write_command; /* ccw command to use for writes. */
36 int attention; /* Got attention. */
Richard Hitted3cb6f2005-10-30 15:00:10 -080037 int active; /* Fullscreen view is active. */
38 struct raw3270_request *init; /* single init request. */
39 wait_queue_head_t wait; /* Init & attention wait queue. */
40 struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */
41 size_t rdbuf_size; /* size of data returned by RDBUF */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +010044static DEFINE_MUTEX(fs3270_mutex);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void
47fs3270_wake_up(struct raw3270_request *rq, void *data)
48{
49 wake_up((wait_queue_head_t *) data);
50}
51
Richard Hitted3cb6f2005-10-30 15:00:10 -080052static inline int
53fs3270_working(struct fs3270 *fp)
54{
55 /*
56 * The fullscreen view is in working order if the view
57 * has been activated AND the initial request is finished.
58 */
59 return fp->active && raw3270_request_final(fp->init);
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int
63fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
64{
Richard Hitted3cb6f2005-10-30 15:00:10 -080065 struct fs3270 *fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int rc;
67
Richard Hitted3cb6f2005-10-30 15:00:10 -080068 fp = (struct fs3270 *) view;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 rq->callback = fs3270_wake_up;
Richard Hitted3cb6f2005-10-30 15:00:10 -080070 rq->callback_data = &fp->wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Richard Hitted3cb6f2005-10-30 15:00:10 -080072 do {
73 if (!fs3270_working(fp)) {
74 /* Fullscreen view isn't ready yet. */
75 rc = wait_event_interruptible(fp->wait,
76 fs3270_working(fp));
77 if (rc != 0)
78 break;
79 }
80 rc = raw3270_start(view, rq);
81 if (rc == 0) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020082 /* Started successfully. Now wait for completion. */
Richard Hitted3cb6f2005-10-30 15:00:10 -080083 wait_event(fp->wait, raw3270_request_final(rq));
84 }
85 } while (rc == -EACCES);
86 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * Switch to the fullscreen view.
91 */
Richard Hitted3cb6f2005-10-30 15:00:10 -080092static void
93fs3270_reset_callback(struct raw3270_request *rq, void *data)
94{
95 struct fs3270 *fp;
96
97 fp = (struct fs3270 *) rq->view;
98 raw3270_request_reset(rq);
99 wake_up(&fp->wait);
100}
101
102static void
103fs3270_restore_callback(struct raw3270_request *rq, void *data)
104{
105 struct fs3270 *fp;
106
107 fp = (struct fs3270 *) rq->view;
108 if (rq->rc != 0 || rq->rescnt != 0) {
109 if (fp->fs_pid)
Cedric Le Goater782237a2006-10-02 02:17:28 -0700110 kill_pid(fp->fs_pid, SIGHUP, 1);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800111 }
112 fp->rdbuf_size = 0;
113 raw3270_request_reset(rq);
114 wake_up(&fp->wait);
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int
118fs3270_activate(struct raw3270_view *view)
119{
120 struct fs3270 *fp;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800121 char *cp;
122 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 fp = (struct fs3270 *) view;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800125
126 /* If an old init command is still running just return. */
127 if (!raw3270_request_final(fp->init))
128 return 0;
129
130 if (fp->rdbuf_size == 0) {
131 /* No saved buffer. Just clear the screen. */
132 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
133 fp->init->callback = fs3270_reset_callback;
134 } else {
135 /* Restore fullscreen buffer saved by fs3270_deactivate. */
136 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
137 raw3270_request_set_idal(fp->init, fp->rdbuf);
138 fp->init->ccw.count = fp->rdbuf_size;
139 cp = fp->rdbuf->data[0];
140 cp[0] = TW_KR;
141 cp[1] = TO_SBA;
142 cp[2] = cp[6];
143 cp[3] = cp[7];
144 cp[4] = TO_IC;
145 cp[5] = TO_SBA;
146 cp[6] = 0x40;
147 cp[7] = 0x40;
148 fp->init->rescnt = 0;
149 fp->init->callback = fs3270_restore_callback;
150 }
151 rc = fp->init->rc = raw3270_start_locked(view, fp->init);
152 if (rc)
153 fp->init->callback(fp->init, NULL);
154 else
155 fp->active = 1;
156 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/*
160 * Shutdown fullscreen view.
161 */
162static void
Richard Hitted3cb6f2005-10-30 15:00:10 -0800163fs3270_save_callback(struct raw3270_request *rq, void *data)
164{
165 struct fs3270 *fp;
166
167 fp = (struct fs3270 *) rq->view;
168
169 /* Correct idal buffer element 0 address. */
170 fp->rdbuf->data[0] -= 5;
171 fp->rdbuf->size += 5;
172
173 /*
174 * If the rdbuf command failed or the idal buffer is
175 * to small for the amount of data returned by the
176 * rdbuf command, then we have no choice but to send
177 * a SIGHUP to the application.
178 */
179 if (rq->rc != 0 || rq->rescnt == 0) {
180 if (fp->fs_pid)
Cedric Le Goater782237a2006-10-02 02:17:28 -0700181 kill_pid(fp->fs_pid, SIGHUP, 1);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800182 fp->rdbuf_size = 0;
183 } else
184 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
185 raw3270_request_reset(rq);
186 wake_up(&fp->wait);
187}
188
189static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190fs3270_deactivate(struct raw3270_view *view)
191{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct fs3270 *fp;
193
194 fp = (struct fs3270 *) view;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800195 fp->active = 0;
196
197 /* If an old init command is still running just return. */
198 if (!raw3270_request_final(fp->init))
199 return;
200
201 /* Prepare read-buffer request. */
202 raw3270_request_set_cmd(fp->init, TC_RDBUF);
203 /*
204 * Hackish: skip first 5 bytes of the idal buffer to make
205 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
206 * in the activation command.
207 */
208 fp->rdbuf->data[0] += 5;
209 fp->rdbuf->size -= 5;
210 raw3270_request_set_idal(fp->init, fp->rdbuf);
211 fp->init->rescnt = 0;
212 fp->init->callback = fs3270_save_callback;
213
214 /* Start I/O to read in the 3270 buffer. */
215 fp->init->rc = raw3270_start_locked(view, fp->init);
216 if (fp->init->rc)
217 fp->init->callback(fp->init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220static int
221fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
222{
223 /* Handle ATTN. Set indication and wake waiters for attention. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200224 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 fp->attention = 1;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800226 wake_up(&fp->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 if (rq) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200230 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 rq->rc = -EIO;
232 else
233 /* Normal end. Copy residual count. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200234 rq->rescnt = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 return RAW3270_IO_DONE;
237}
238
239/*
240 * Process reads from fullscreen 3270.
241 */
242static ssize_t
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200243fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct fs3270 *fp;
246 struct raw3270_request *rq;
247 struct idal_buffer *ib;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800248 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (count == 0 || count > 65535)
251 return -EINVAL;
252 fp = filp->private_data;
253 if (!fp)
254 return -ENODEV;
255 ib = idal_buffer_alloc(count, 0);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800256 if (IS_ERR(ib))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -ENOMEM;
258 rq = raw3270_request_alloc(0);
259 if (!IS_ERR(rq)) {
260 if (fp->read_command == 0 && fp->write_command != 0)
261 fp->read_command = 6;
262 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
263 raw3270_request_set_idal(rq, ib);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800264 rc = wait_event_interruptible(fp->wait, fp->attention);
265 fp->attention = 0;
266 if (rc == 0) {
267 rc = fs3270_do_io(&fp->view, rq);
268 if (rc == 0) {
269 count -= rq->rescnt;
270 if (idal_buffer_to_user(ib, data, count) != 0)
271 rc = -EFAULT;
272 else
273 rc = count;
274
275 }
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 raw3270_request_free(rq);
278 } else
279 rc = PTR_ERR(rq);
280 idal_buffer_free(ib);
281 return rc;
282}
283
284/*
285 * Process writes to fullscreen 3270.
286 */
287static ssize_t
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200288fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct fs3270 *fp;
291 struct raw3270_request *rq;
292 struct idal_buffer *ib;
293 int write_command;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800294 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 fp = filp->private_data;
297 if (!fp)
298 return -ENODEV;
299 ib = idal_buffer_alloc(count, 0);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800300 if (IS_ERR(ib))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -ENOMEM;
302 rq = raw3270_request_alloc(0);
303 if (!IS_ERR(rq)) {
304 if (idal_buffer_from_user(ib, data, count) == 0) {
305 write_command = fp->write_command ? : 1;
306 if (write_command == 5)
307 write_command = 13;
308 raw3270_request_set_cmd(rq, write_command);
309 raw3270_request_set_idal(rq, ib);
310 rc = fs3270_do_io(&fp->view, rq);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800311 if (rc == 0)
312 rc = count - rq->rescnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 } else
314 rc = -EFAULT;
315 raw3270_request_free(rq);
316 } else
317 rc = PTR_ERR(rq);
318 idal_buffer_free(ib);
319 return rc;
320}
321
322/*
323 * process ioctl commands for the tube driver
324 */
Christoph Hellwig0f75e002006-01-09 20:52:04 -0800325static long
326fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Heiko Carstens16e1a572010-01-13 20:44:32 +0100328 char __user *argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct fs3270 *fp;
330 struct raw3270_iocb iocb;
331 int rc;
332
333 fp = filp->private_data;
334 if (!fp)
335 return -ENODEV;
Heiko Carstens16e1a572010-01-13 20:44:32 +0100336 if (is_compat_task())
337 argp = compat_ptr(arg);
338 else
339 argp = (char __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 rc = 0;
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100341 mutex_lock(&fs3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 switch (cmd) {
343 case TUBICMD:
344 fp->read_command = arg;
345 break;
346 case TUBOCMD:
347 fp->write_command = arg;
348 break;
349 case TUBGETI:
Heiko Carstens16e1a572010-01-13 20:44:32 +0100350 rc = put_user(fp->read_command, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352 case TUBGETO:
Heiko Carstens16e1a572010-01-13 20:44:32 +0100353 rc = put_user(fp->write_command, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break;
355 case TUBGETMOD:
356 iocb.model = fp->view.model;
357 iocb.line_cnt = fp->view.rows;
358 iocb.col_cnt = fp->view.cols;
359 iocb.pf_cnt = 24;
360 iocb.re_cnt = 20;
361 iocb.map = 0;
Heiko Carstens16e1a572010-01-13 20:44:32 +0100362 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 rc = -EFAULT;
364 break;
365 }
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100366 mutex_unlock(&fs3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return rc;
368}
369
370/*
Richard Hitted3cb6f2005-10-30 15:00:10 -0800371 * Allocate fs3270 structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373static struct fs3270 *
374fs3270_alloc_view(void)
375{
376 struct fs3270 *fp;
377
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800378 fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!fp)
380 return ERR_PTR(-ENOMEM);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800381 fp->init = raw3270_request_alloc(0);
382 if (IS_ERR(fp->init)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 kfree(fp);
384 return ERR_PTR(-ENOMEM);
385 }
386 return fp;
387}
388
389/*
Richard Hitted3cb6f2005-10-30 15:00:10 -0800390 * Free fs3270 structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392static void
393fs3270_free_view(struct raw3270_view *view)
394{
Richard Hitted3cb6f2005-10-30 15:00:10 -0800395 struct fs3270 *fp;
396
397 fp = (struct fs3270 *) view;
398 if (fp->rdbuf)
399 idal_buffer_free(fp->rdbuf);
400 raw3270_request_free(((struct fs3270 *) view)->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 kfree(view);
402}
403
404/*
405 * Unlink fs3270 data structure from filp.
406 */
407static void
408fs3270_release(struct raw3270_view *view)
409{
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200410 struct fs3270 *fp;
411
412 fp = (struct fs3270 *) view;
413 if (fp->fs_pid)
414 kill_pid(fp->fs_pid, SIGHUP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/* View to a 3270 device. Can be console, tty or fullscreen. */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100418static struct raw3270_fn fs3270_fn = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 .activate = fs3270_activate,
420 .deactivate = fs3270_deactivate,
421 .intv = (void *) fs3270_irq,
422 .release = fs3270_release,
423 .free = fs3270_free_view
424};
425
426/*
427 * This routine is called whenever a 3270 fullscreen device is opened.
428 */
429static int
430fs3270_open(struct inode *inode, struct file *filp)
431{
432 struct fs3270 *fp;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800433 struct idal_buffer *ib;
Alan Coxa18992d2008-10-13 10:46:09 +0100434 int minor, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Josef Sipek49522c92006-12-08 02:37:34 -0800436 if (imajor(filp->f_path.dentry->d_inode) != IBM_FS3270_MAJOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -ENODEV;
Josef Sipek49522c92006-12-08 02:37:34 -0800438 minor = iminor(filp->f_path.dentry->d_inode);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800439 /* Check for minor 0 multiplexer. */
440 if (minor == 0) {
Alan Coxa90610e2008-10-13 10:45:52 +0100441 struct tty_struct *tty = get_current_tty();
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800442 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
Alan Cox452a00d2008-10-13 10:39:13 +0100443 tty_kref_put(tty);
Alan Coxa18992d2008-10-13 10:46:09 +0100444 return -ENODEV;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800445 }
446 minor = tty->index + RAW3270_FIRSTMINOR;
Alan Cox452a00d2008-10-13 10:39:13 +0100447 tty_kref_put(tty);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800448 }
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100449 mutex_lock(&fs3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Check if some other program is already using fullscreen mode. */
451 fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
452 if (!IS_ERR(fp)) {
453 raw3270_put_view(&fp->view);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600454 rc = -EBUSY;
455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 /* Allocate fullscreen view structure. */
458 fp = fs3270_alloc_view();
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600459 if (IS_ERR(fp)) {
460 rc = PTR_ERR(fp);
461 goto out;
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Richard Hitted3cb6f2005-10-30 15:00:10 -0800464 init_waitqueue_head(&fp->wait);
Cedric Le Goater782237a2006-10-02 02:17:28 -0700465 fp->fs_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
467 if (rc) {
468 fs3270_free_view(&fp->view);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
Richard Hitted3cb6f2005-10-30 15:00:10 -0800472 /* Allocate idal-buffer. */
473 ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
474 if (IS_ERR(ib)) {
475 raw3270_put_view(&fp->view);
476 raw3270_del_view(&fp->view);
Roel Kluin2b310012009-12-18 17:43:19 +0100477 rc = PTR_ERR(ib);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600478 goto out;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800479 }
480 fp->rdbuf = ib;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 rc = raw3270_activate_view(&fp->view);
483 if (rc) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800484 raw3270_put_view(&fp->view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 raw3270_del_view(&fp->view);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200488 nonseekable_open(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 filp->private_data = fp;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600490out:
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100491 mutex_unlock(&fs3270_mutex);
Alan Coxa18992d2008-10-13 10:46:09 +0100492 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495/*
496 * This routine is called when the 3270 tty is closed. We wait
497 * for the remaining request to be completed. Then we clean up.
498 */
499static int
500fs3270_close(struct inode *inode, struct file *filp)
501{
502 struct fs3270 *fp;
503
504 fp = filp->private_data;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200505 filp->private_data = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800506 if (fp) {
Cedric Le Goater782237a2006-10-02 02:17:28 -0700507 put_pid(fp->fs_pid);
508 fp->fs_pid = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800509 raw3270_reset(&fp->view);
510 raw3270_put_view(&fp->view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 raw3270_del_view(&fp->view);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
514}
515
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800516static const struct file_operations fs3270_fops = {
Christoph Hellwig0f75e002006-01-09 20:52:04 -0800517 .owner = THIS_MODULE, /* owner */
518 .read = fs3270_read, /* read */
519 .write = fs3270_write, /* write */
520 .unlocked_ioctl = fs3270_ioctl, /* ioctl */
521 .compat_ioctl = fs3270_ioctl, /* ioctl */
Heiko Carstens16e1a572010-01-13 20:44:32 +0100522 .open = fs3270_open, /* open */
523 .release = fs3270_close, /* release */
Arnd Bergmann6038f372010-08-15 18:52:59 +0200524 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525};
526
527/*
528 * 3270 fullscreen driver initialization.
529 */
530static int __init
531fs3270_init(void)
532{
533 int rc;
534
535 rc = register_chrdev(IBM_FS3270_MAJOR, "fs3270", &fs3270_fops);
Martin Schwidefskya26182e2008-07-14 09:59:25 +0200536 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539}
540
541static void __exit
542fs3270_exit(void)
543{
544 unregister_chrdev(IBM_FS3270_MAJOR, "fs3270");
545}
546
547MODULE_LICENSE("GPL");
548MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
549
550module_init(fs3270_init);
551module_exit(fs3270_exit);