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