blob: e71298158f9eaa83a0531269a7da5c83c0c1d14a [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 Carstens3a4c5d52011-07-30 09:25:15 +020014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18
Heiko Carstens16e1a572010-01-13 20:44:32 +010019#include <asm/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/ccwdev.h>
21#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/ebcdic.h>
23#include <asm/idals.h>
24
25#include "raw3270.h"
26#include "ctrlchar.h"
27
Heiko Carstens2b67fc42007-02-05 21:16:47 +010028static struct raw3270_fn fs3270_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30struct fs3270 {
31 struct raw3270_view view;
Cedric Le Goater782237a2006-10-02 02:17:28 -070032 struct pid *fs_pid; /* Pid of controlling program. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int read_command; /* ccw command to use for reads. */
34 int write_command; /* ccw command to use for writes. */
35 int attention; /* Got attention. */
Richard Hitted3cb6f2005-10-30 15:00:10 -080036 int active; /* Fullscreen view is active. */
37 struct raw3270_request *init; /* single init request. */
38 wait_queue_head_t wait; /* Init & attention wait queue. */
39 struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */
40 size_t rdbuf_size; /* size of data returned by RDBUF */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +010043static DEFINE_MUTEX(fs3270_mutex);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static void
46fs3270_wake_up(struct raw3270_request *rq, void *data)
47{
48 wake_up((wait_queue_head_t *) data);
49}
50
Richard Hitted3cb6f2005-10-30 15:00:10 -080051static inline int
52fs3270_working(struct fs3270 *fp)
53{
54 /*
55 * The fullscreen view is in working order if the view
56 * has been activated AND the initial request is finished.
57 */
58 return fp->active && raw3270_request_final(fp->init);
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int
62fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
63{
Richard Hitted3cb6f2005-10-30 15:00:10 -080064 struct fs3270 *fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int rc;
66
Richard Hitted3cb6f2005-10-30 15:00:10 -080067 fp = (struct fs3270 *) view;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 rq->callback = fs3270_wake_up;
Richard Hitted3cb6f2005-10-30 15:00:10 -080069 rq->callback_data = &fp->wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Richard Hitted3cb6f2005-10-30 15:00:10 -080071 do {
72 if (!fs3270_working(fp)) {
73 /* Fullscreen view isn't ready yet. */
74 rc = wait_event_interruptible(fp->wait,
75 fs3270_working(fp));
76 if (rc != 0)
77 break;
78 }
79 rc = raw3270_start(view, rq);
80 if (rc == 0) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020081 /* Started successfully. Now wait for completion. */
Richard Hitted3cb6f2005-10-30 15:00:10 -080082 wait_event(fp->wait, raw3270_request_final(rq));
83 }
84 } while (rc == -EACCES);
85 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * Switch to the fullscreen view.
90 */
Richard Hitted3cb6f2005-10-30 15:00:10 -080091static void
92fs3270_reset_callback(struct raw3270_request *rq, void *data)
93{
94 struct fs3270 *fp;
95
96 fp = (struct fs3270 *) rq->view;
97 raw3270_request_reset(rq);
98 wake_up(&fp->wait);
99}
100
101static void
102fs3270_restore_callback(struct raw3270_request *rq, void *data)
103{
104 struct fs3270 *fp;
105
106 fp = (struct fs3270 *) rq->view;
107 if (rq->rc != 0 || rq->rescnt != 0) {
108 if (fp->fs_pid)
Cedric Le Goater782237a2006-10-02 02:17:28 -0700109 kill_pid(fp->fs_pid, SIGHUP, 1);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800110 }
111 fp->rdbuf_size = 0;
112 raw3270_request_reset(rq);
113 wake_up(&fp->wait);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int
117fs3270_activate(struct raw3270_view *view)
118{
119 struct fs3270 *fp;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800120 char *cp;
121 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 fp = (struct fs3270 *) view;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800124
125 /* If an old init command is still running just return. */
126 if (!raw3270_request_final(fp->init))
127 return 0;
128
129 if (fp->rdbuf_size == 0) {
130 /* No saved buffer. Just clear the screen. */
131 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
132 fp->init->callback = fs3270_reset_callback;
133 } else {
134 /* Restore fullscreen buffer saved by fs3270_deactivate. */
135 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
136 raw3270_request_set_idal(fp->init, fp->rdbuf);
137 fp->init->ccw.count = fp->rdbuf_size;
138 cp = fp->rdbuf->data[0];
139 cp[0] = TW_KR;
140 cp[1] = TO_SBA;
141 cp[2] = cp[6];
142 cp[3] = cp[7];
143 cp[4] = TO_IC;
144 cp[5] = TO_SBA;
145 cp[6] = 0x40;
146 cp[7] = 0x40;
147 fp->init->rescnt = 0;
148 fp->init->callback = fs3270_restore_callback;
149 }
150 rc = fp->init->rc = raw3270_start_locked(view, fp->init);
151 if (rc)
152 fp->init->callback(fp->init, NULL);
153 else
154 fp->active = 1;
155 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
159 * Shutdown fullscreen view.
160 */
161static void
Richard Hitted3cb6f2005-10-30 15:00:10 -0800162fs3270_save_callback(struct raw3270_request *rq, void *data)
163{
164 struct fs3270 *fp;
165
166 fp = (struct fs3270 *) rq->view;
167
168 /* Correct idal buffer element 0 address. */
169 fp->rdbuf->data[0] -= 5;
170 fp->rdbuf->size += 5;
171
172 /*
173 * If the rdbuf command failed or the idal buffer is
174 * to small for the amount of data returned by the
175 * rdbuf command, then we have no choice but to send
176 * a SIGHUP to the application.
177 */
178 if (rq->rc != 0 || rq->rescnt == 0) {
179 if (fp->fs_pid)
Cedric Le Goater782237a2006-10-02 02:17:28 -0700180 kill_pid(fp->fs_pid, SIGHUP, 1);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800181 fp->rdbuf_size = 0;
182 } else
183 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
184 raw3270_request_reset(rq);
185 wake_up(&fp->wait);
186}
187
188static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189fs3270_deactivate(struct raw3270_view *view)
190{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct fs3270 *fp;
192
193 fp = (struct fs3270 *) view;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800194 fp->active = 0;
195
196 /* If an old init command is still running just return. */
197 if (!raw3270_request_final(fp->init))
198 return;
199
200 /* Prepare read-buffer request. */
201 raw3270_request_set_cmd(fp->init, TC_RDBUF);
202 /*
203 * Hackish: skip first 5 bytes of the idal buffer to make
204 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
205 * in the activation command.
206 */
207 fp->rdbuf->data[0] += 5;
208 fp->rdbuf->size -= 5;
209 raw3270_request_set_idal(fp->init, fp->rdbuf);
210 fp->init->rescnt = 0;
211 fp->init->callback = fs3270_save_callback;
212
213 /* Start I/O to read in the 3270 buffer. */
214 fp->init->rc = raw3270_start_locked(view, fp->init);
215 if (fp->init->rc)
216 fp->init->callback(fp->init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static int
220fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
221{
222 /* Handle ATTN. Set indication and wake waiters for attention. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200223 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 fp->attention = 1;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800225 wake_up(&fp->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
228 if (rq) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200229 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 rq->rc = -EIO;
231 else
232 /* Normal end. Copy residual count. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200233 rq->rescnt = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 return RAW3270_IO_DONE;
236}
237
238/*
239 * Process reads from fullscreen 3270.
240 */
241static ssize_t
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200242fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct fs3270 *fp;
245 struct raw3270_request *rq;
246 struct idal_buffer *ib;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800247 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (count == 0 || count > 65535)
250 return -EINVAL;
251 fp = filp->private_data;
252 if (!fp)
253 return -ENODEV;
254 ib = idal_buffer_alloc(count, 0);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800255 if (IS_ERR(ib))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -ENOMEM;
257 rq = raw3270_request_alloc(0);
258 if (!IS_ERR(rq)) {
259 if (fp->read_command == 0 && fp->write_command != 0)
260 fp->read_command = 6;
261 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
262 raw3270_request_set_idal(rq, ib);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800263 rc = wait_event_interruptible(fp->wait, fp->attention);
264 fp->attention = 0;
265 if (rc == 0) {
266 rc = fs3270_do_io(&fp->view, rq);
267 if (rc == 0) {
268 count -= rq->rescnt;
269 if (idal_buffer_to_user(ib, data, count) != 0)
270 rc = -EFAULT;
271 else
272 rc = count;
273
274 }
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 raw3270_request_free(rq);
277 } else
278 rc = PTR_ERR(rq);
279 idal_buffer_free(ib);
280 return rc;
281}
282
283/*
284 * Process writes to fullscreen 3270.
285 */
286static ssize_t
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200287fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct fs3270 *fp;
290 struct raw3270_request *rq;
291 struct idal_buffer *ib;
292 int write_command;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800293 ssize_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 fp = filp->private_data;
296 if (!fp)
297 return -ENODEV;
298 ib = idal_buffer_alloc(count, 0);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800299 if (IS_ERR(ib))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -ENOMEM;
301 rq = raw3270_request_alloc(0);
302 if (!IS_ERR(rq)) {
303 if (idal_buffer_from_user(ib, data, count) == 0) {
304 write_command = fp->write_command ? : 1;
305 if (write_command == 5)
306 write_command = 13;
307 raw3270_request_set_cmd(rq, write_command);
308 raw3270_request_set_idal(rq, ib);
309 rc = fs3270_do_io(&fp->view, rq);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800310 if (rc == 0)
311 rc = count - rq->rescnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else
313 rc = -EFAULT;
314 raw3270_request_free(rq);
315 } else
316 rc = PTR_ERR(rq);
317 idal_buffer_free(ib);
318 return rc;
319}
320
321/*
322 * process ioctl commands for the tube driver
323 */
Christoph Hellwig0f75e002006-01-09 20:52:04 -0800324static long
325fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Heiko Carstens16e1a572010-01-13 20:44:32 +0100327 char __user *argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct fs3270 *fp;
329 struct raw3270_iocb iocb;
330 int rc;
331
332 fp = filp->private_data;
333 if (!fp)
334 return -ENODEV;
Heiko Carstens16e1a572010-01-13 20:44:32 +0100335 if (is_compat_task())
336 argp = compat_ptr(arg);
337 else
338 argp = (char __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 rc = 0;
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100340 mutex_lock(&fs3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 switch (cmd) {
342 case TUBICMD:
343 fp->read_command = arg;
344 break;
345 case TUBOCMD:
346 fp->write_command = arg;
347 break;
348 case TUBGETI:
Heiko Carstens16e1a572010-01-13 20:44:32 +0100349 rc = put_user(fp->read_command, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 case TUBGETO:
Heiko Carstens16e1a572010-01-13 20:44:32 +0100352 rc = put_user(fp->write_command, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 case TUBGETMOD:
355 iocb.model = fp->view.model;
356 iocb.line_cnt = fp->view.rows;
357 iocb.col_cnt = fp->view.cols;
358 iocb.pf_cnt = 24;
359 iocb.re_cnt = 20;
360 iocb.map = 0;
Heiko Carstens16e1a572010-01-13 20:44:32 +0100361 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 rc = -EFAULT;
363 break;
364 }
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100365 mutex_unlock(&fs3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return rc;
367}
368
369/*
Richard Hitted3cb6f2005-10-30 15:00:10 -0800370 * Allocate fs3270 structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
372static struct fs3270 *
373fs3270_alloc_view(void)
374{
375 struct fs3270 *fp;
376
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800377 fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!fp)
379 return ERR_PTR(-ENOMEM);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800380 fp->init = raw3270_request_alloc(0);
381 if (IS_ERR(fp->init)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(fp);
383 return ERR_PTR(-ENOMEM);
384 }
385 return fp;
386}
387
388/*
Richard Hitted3cb6f2005-10-30 15:00:10 -0800389 * Free fs3270 structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
391static void
392fs3270_free_view(struct raw3270_view *view)
393{
Richard Hitted3cb6f2005-10-30 15:00:10 -0800394 struct fs3270 *fp;
395
396 fp = (struct fs3270 *) view;
397 if (fp->rdbuf)
398 idal_buffer_free(fp->rdbuf);
399 raw3270_request_free(((struct fs3270 *) view)->init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree(view);
401}
402
403/*
404 * Unlink fs3270 data structure from filp.
405 */
406static void
407fs3270_release(struct raw3270_view *view)
408{
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200409 struct fs3270 *fp;
410
411 fp = (struct fs3270 *) view;
412 if (fp->fs_pid)
413 kill_pid(fp->fs_pid, SIGHUP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416/* View to a 3270 device. Can be console, tty or fullscreen. */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100417static struct raw3270_fn fs3270_fn = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .activate = fs3270_activate,
419 .deactivate = fs3270_deactivate,
420 .intv = (void *) fs3270_irq,
421 .release = fs3270_release,
422 .free = fs3270_free_view
423};
424
425/*
426 * This routine is called whenever a 3270 fullscreen device is opened.
427 */
428static int
429fs3270_open(struct inode *inode, struct file *filp)
430{
431 struct fs3270 *fp;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800432 struct idal_buffer *ib;
Alan Coxa18992d2008-10-13 10:46:09 +0100433 int minor, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Josef Sipek49522c92006-12-08 02:37:34 -0800435 if (imajor(filp->f_path.dentry->d_inode) != IBM_FS3270_MAJOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -ENODEV;
Josef Sipek49522c92006-12-08 02:37:34 -0800437 minor = iminor(filp->f_path.dentry->d_inode);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800438 /* Check for minor 0 multiplexer. */
439 if (minor == 0) {
Alan Coxa90610e2008-10-13 10:45:52 +0100440 struct tty_struct *tty = get_current_tty();
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800441 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
Alan Cox452a00d2008-10-13 10:39:13 +0100442 tty_kref_put(tty);
Alan Coxa18992d2008-10-13 10:46:09 +0100443 return -ENODEV;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800444 }
445 minor = tty->index + RAW3270_FIRSTMINOR;
Alan Cox452a00d2008-10-13 10:39:13 +0100446 tty_kref_put(tty);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800447 }
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100448 mutex_lock(&fs3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Check if some other program is already using fullscreen mode. */
450 fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
451 if (!IS_ERR(fp)) {
452 raw3270_put_view(&fp->view);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600453 rc = -EBUSY;
454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 /* Allocate fullscreen view structure. */
457 fp = fs3270_alloc_view();
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600458 if (IS_ERR(fp)) {
459 rc = PTR_ERR(fp);
460 goto out;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Richard Hitted3cb6f2005-10-30 15:00:10 -0800463 init_waitqueue_head(&fp->wait);
Cedric Le Goater782237a2006-10-02 02:17:28 -0700464 fp->fs_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
466 if (rc) {
467 fs3270_free_view(&fp->view);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Richard Hitted3cb6f2005-10-30 15:00:10 -0800471 /* Allocate idal-buffer. */
472 ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
473 if (IS_ERR(ib)) {
474 raw3270_put_view(&fp->view);
475 raw3270_del_view(&fp->view);
Roel Kluin2b310012009-12-18 17:43:19 +0100476 rc = PTR_ERR(ib);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600477 goto out;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800478 }
479 fp->rdbuf = ib;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 rc = raw3270_activate_view(&fp->view);
482 if (rc) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800483 raw3270_put_view(&fp->view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 raw3270_del_view(&fp->view);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200487 nonseekable_open(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 filp->private_data = fp;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600489out:
Martin Schwidefsky5cbb5f52009-12-07 12:52:20 +0100490 mutex_unlock(&fs3270_mutex);
Alan Coxa18992d2008-10-13 10:46:09 +0100491 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/*
495 * This routine is called when the 3270 tty is closed. We wait
496 * for the remaining request to be completed. Then we clean up.
497 */
498static int
499fs3270_close(struct inode *inode, struct file *filp)
500{
501 struct fs3270 *fp;
502
503 fp = filp->private_data;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200504 filp->private_data = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800505 if (fp) {
Cedric Le Goater782237a2006-10-02 02:17:28 -0700506 put_pid(fp->fs_pid);
507 fp->fs_pid = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800508 raw3270_reset(&fp->view);
509 raw3270_put_view(&fp->view);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 raw3270_del_view(&fp->view);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513}
514
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800515static const struct file_operations fs3270_fops = {
Christoph Hellwig0f75e002006-01-09 20:52:04 -0800516 .owner = THIS_MODULE, /* owner */
517 .read = fs3270_read, /* read */
518 .write = fs3270_write, /* write */
519 .unlocked_ioctl = fs3270_ioctl, /* ioctl */
520 .compat_ioctl = fs3270_ioctl, /* ioctl */
Heiko Carstens16e1a572010-01-13 20:44:32 +0100521 .open = fs3270_open, /* open */
522 .release = fs3270_close, /* release */
Arnd Bergmann6038f372010-08-15 18:52:59 +0200523 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524};
525
526/*
527 * 3270 fullscreen driver initialization.
528 */
529static int __init
530fs3270_init(void)
531{
532 int rc;
533
534 rc = register_chrdev(IBM_FS3270_MAJOR, "fs3270", &fs3270_fops);
Martin Schwidefskya26182e2008-07-14 09:59:25 +0200535 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538}
539
540static void __exit
541fs3270_exit(void)
542{
543 unregister_chrdev(IBM_FS3270_MAJOR, "fs3270");
544}
545
546MODULE_LICENSE("GPL");
547MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
548
549module_init(fs3270_init);
550module_exit(fs3270_exit);