patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / s390 / char / raw3270.c
1 /*
2  *  drivers/s390/char/raw3270.c
3  *    IBM/3270 Driver - core functions.
4  *
5  *  Author(s):
6  *    Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7  *    Rewritten for 2.5 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/module.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/wait.h>
21
22 #include <asm/ccwdev.h>
23 #include <asm/cio.h>
24 #include <asm/ebcdic.h>
25
26 #include "raw3270.h"
27
28 /* The main 3270 data structure. */
29 struct raw3270 {
30         struct list_head list;
31         struct ccw_device *cdev;
32         int minor;
33
34         short model, rows, cols;
35         unsigned long flags;
36
37         struct list_head req_queue;     /* Request queue. */
38         struct list_head view_list;     /* List of available views. */
39         struct raw3270_view *view;      /* Active view. */
40
41         struct timer_list timer;        /* Device timer. */
42
43         unsigned char *ascebc;          /* ascii -> ebcdic table */
44 };
45
46 /* raw3270->flags */
47 #define RAW3270_FLAGS_14BITADDR 0       /* 14-bit buffer addresses */
48 #define RAW3270_FLAGS_BUSY      1       /* Device busy, leave it alone */
49 #define RAW3270_FLAGS_ATTN      2       /* Device sent an ATTN interrupt */
50 #define RAW3270_FLAGS_READY     4       /* Device is useable by views */
51 #define RAW3270_FLAGS_CONSOLE   8       /* Device is the console. */
52
53 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
54 static DECLARE_MUTEX(raw3270_sem);
55
56 /* List of 3270 devices. */
57 static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
58
59 /*
60  * Flag to indicate if the driver has been registered. Some operations
61  * like waiting for the end of i/o need to be done differently as long
62  * as the kernel is still starting up (console support).
63  */
64 static int raw3270_registered;
65
66 /* Module parameters */
67 static int tubxcorrect = 0;
68 MODULE_PARM(tubxcorrect, "i");
69
70 /*
71  * Wait queue for device init/delete, view delete.
72  */
73 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
74
75 /*
76  * Encode array for 12 bit 3270 addresses.
77  */
78 unsigned char raw3270_ebcgraf[64] =     {
79         0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
80         0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
81         0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
82         0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
83         0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
84         0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
85         0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
86         0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
87 };
88
89 void
90 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
91 {
92         if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
93                 cp[0] = (addr >> 8) & 0x3f;
94                 cp[1] = addr & 0xff;
95         } else {
96                 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
97                 cp[1] = raw3270_ebcgraf[addr & 0x3f];
98         }
99 }
100
101 /*
102  * Allocate a new 3270 ccw request
103  */
104 struct raw3270_request *
105 raw3270_request_alloc(size_t size)
106 {
107         struct raw3270_request *rq;
108
109         /* Allocate request structure */
110         rq = kmalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
111         if (!rq)
112                 return ERR_PTR(-ENOMEM);
113         memset(rq, 0, sizeof(struct raw3270_request));
114
115         /* alloc output buffer. */
116         if (size > 0) {
117                 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
118                 if (!rq->buffer) {
119                         kfree(rq);
120                         return ERR_PTR(-ENOMEM);
121                 }
122         }
123         rq->size = size;
124         INIT_LIST_HEAD(&rq->list);
125
126         /*
127          * Setup ccw.
128          */
129         rq->ccw.cda = __pa(rq->buffer);
130         rq->ccw.flags = CCW_FLAG_SLI;
131
132         return rq;
133 }
134
135 #ifdef CONFIG_TN3270_CONSOLE
136 /*
137  * Allocate a new 3270 ccw request from bootmem. Only works very
138  * early in the boot process. Only con3270.c should be using this.
139  */
140 struct raw3270_request *
141 raw3270_request_alloc_bootmem(size_t size)
142 {
143         struct raw3270_request *rq;
144
145         rq = alloc_bootmem_low(sizeof(struct raw3270));
146         if (!rq)
147                 return ERR_PTR(-ENOMEM);
148         memset(rq, 0, sizeof(struct raw3270_request));
149
150         /* alloc output buffer. */
151         if (size > 0) {
152                 rq->buffer = alloc_bootmem_low(size);
153                 if (!rq->buffer) {
154                         free_bootmem((unsigned long) rq,
155                                      sizeof(struct raw3270));
156                         return ERR_PTR(-ENOMEM);
157                 }
158         }
159         rq->size = size;
160         INIT_LIST_HEAD(&rq->list);
161
162         /*
163          * Setup ccw.
164          */
165         rq->ccw.cda = __pa(rq->buffer);
166         rq->ccw.flags = CCW_FLAG_SLI;
167
168         return rq;
169 }
170 #endif
171
172 /*
173  * Free 3270 ccw request
174  */
175 void
176 raw3270_request_free (struct raw3270_request *rq)
177 {
178         if (rq->buffer)
179                 kfree(rq->buffer);
180         kfree(rq);
181 }
182
183 /*
184  * Reset request to initial state.
185  */
186 void
187 raw3270_request_reset(struct raw3270_request *rq)
188 {
189         BUG_ON(!list_empty(&rq->list));
190         rq->ccw.cmd_code = 0;
191         rq->ccw.count = 0;
192         rq->ccw.cda = __pa(rq->buffer);
193         rq->ccw.flags = CCW_FLAG_SLI;
194         rq->rescnt = 0;
195         rq->rc = 0;
196 }
197
198 /*
199  * Set command code to ccw of a request.
200  */
201 void
202 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
203 {
204         rq->ccw.cmd_code = cmd;
205 }
206
207 /*
208  * Add data fragment to output buffer.
209  */
210 int
211 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
212 {
213         if (size + rq->ccw.count > rq->size)
214                 return -E2BIG;
215         memcpy(rq->buffer + rq->ccw.count, data, size);
216         rq->ccw.count += size;
217         return 0;
218 }
219
220 /*
221  * Set address/length pair to ccw of a request.
222  */
223 void
224 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
225 {
226         rq->ccw.cda = __pa(data);
227         rq->ccw.count = size;
228 }
229
230 /*
231  * Set idal buffer to ccw of a request.
232  */
233 void
234 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
235 {
236         rq->ccw.cda = __pa(ib->data);
237         rq->ccw.count = ib->size;
238         rq->ccw.flags |= CCW_FLAG_IDA;
239 }
240
241 /*
242  * Stop running ccw.
243  */
244 static int
245 raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
246 {
247         int retries;
248         int rc;
249
250         if (raw3270_request_final(rq))
251                 return 0;
252         /* Check if interrupt has already been processed */
253         for (retries = 0; retries < 5; retries++) {
254                 if (retries < 2)
255                         rc = ccw_device_halt(rp->cdev, (long) rq);
256                 else
257                         rc = ccw_device_clear(rp->cdev, (long) rq);
258                 if (rc == 0)
259                         break;          /* termination successful */
260         }
261         return rc;
262 }
263
264 static int
265 raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
266 {
267         unsigned long flags;
268         int rc;
269
270         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
271         rc = raw3270_halt_io_nolock(rp, rq);
272         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
273         return rc;
274 }
275
276 /*
277  * Add the request to the request queue, try to start it if the
278  * 3270 device is idle. Return without waiting for end of i/o.
279  */
280 static int
281 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
282                 struct raw3270_request *rq)
283 {
284         rq->view = view;
285         raw3270_get_view(view);
286         if (list_empty(&rp->req_queue) &&
287             !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
288                 /* No other requests are on the queue. Start this one. */
289                 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
290                                                (unsigned long) rq, 0, 0);
291                 if (rq->rc) {
292                         raw3270_put_view(view);
293                         return rq->rc;
294                 }
295         }
296         list_add_tail(&rq->list, &rp->req_queue);
297         return 0;
298 }
299
300 int
301 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
302 {
303         unsigned long flags;
304         struct raw3270 *rp;
305         int rc;
306
307         spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
308         rp = view->dev;
309         if (!rp || rp->view != view)
310                 rc = -EACCES;
311         else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
312                 rc = -ENODEV;
313         else
314                 rc =  __raw3270_start(rp, view, rq);
315         spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
316         return rc;
317 }
318
319 int
320 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
321 {
322         struct raw3270 *rp;
323
324         rp = view->dev;
325         rq->view = view;
326         raw3270_get_view(view);
327         list_add_tail(&rq->list, &rp->req_queue);
328         return 0;
329 }
330
331 /*
332  * 3270 interrupt routine, called from the ccw_device layer
333  */
334 static void
335 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
336 {
337         struct raw3270 *rp;
338         struct raw3270_view *view;
339         struct raw3270_request *rq;
340         int rc;
341
342         rp = (struct raw3270 *) cdev->dev.driver_data;
343         if (!rp)
344                 return;
345         rq = (struct raw3270_request *) intparm;
346         view = rq ? rq->view : rp->view;
347
348         if (IS_ERR(irb))
349                 rc = RAW3270_IO_RETRY;
350         else if (irb->scsw.dstat ==  (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
351                                       DEV_STAT_UNIT_EXCEP)) {
352                 /* Handle CE-DE-UE and subsequent UDE */
353                 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
354                 rc = RAW3270_IO_BUSY;
355         } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
356                 /* Wait for UDE if busy flag is set. */
357                 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
358                         clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
359                         /* Got it, now retry. */
360                         rc = RAW3270_IO_RETRY;
361                 } else
362                         rc = RAW3270_IO_BUSY;
363         } else if (view)
364                 rc = view->fn->intv(view, rq, irb);
365         else
366                 rc = RAW3270_IO_DONE;
367
368         switch (rc) {
369         case RAW3270_IO_DONE:
370                 break;
371         case RAW3270_IO_BUSY:
372                 /* 
373                  * Intervention required by the operator. We have to wait
374                  * for unsolicited device end.
375                  */
376                 return;
377         case RAW3270_IO_RETRY:
378                 if (!rq)
379                         break;
380                 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
381                                           (unsigned long) rq, 0, 0);
382                 if (rq->rc == 0)
383                         return; /* Sucessfully restarted. */
384                 break;
385         case RAW3270_IO_STOP:
386                 if (!rq)
387                         break;
388                 raw3270_halt_io_nolock(rp, rq);
389                 rq->rc = -EIO;
390                 break;
391         default:
392                 BUG();
393         }
394         if (rq) {
395                 BUG_ON(list_empty(&rq->list));
396                 /* The request completed, remove from queue and do callback. */
397                 list_del_init(&rq->list);
398                 if (rq->callback)
399                         rq->callback(rq, rq->callback_data);
400                 /* Do put_device for get_device in raw3270_start. */
401                 raw3270_put_view(view);
402         }
403         /*
404          * Try to start each request on request queue until one is
405          * started successful.
406          */
407         while (!list_empty(&rp->req_queue)) {
408                 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
409                 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
410                                           (unsigned long) rq, 0, 0);
411                 if (rq->rc == 0)
412                         break;
413                 /* Start failed. Remove request and do callback. */
414                 list_del_init(&rq->list);
415                 if (rq->callback)
416                         rq->callback(rq, rq->callback_data);
417                 /* Do put_device for get_device in raw3270_start. */
418                 raw3270_put_view(view);
419         }
420 }
421
422 /*
423  * Size sensing.
424  */
425
426 struct raw3270_ua {     /* Query Reply structure for Usable Area */
427         struct {        /* Usable Area Query Reply Base */
428                 short l;        /* Length of this structured field */
429                 char  sfid;     /* 0x81 if Query Reply */
430                 char  qcode;    /* 0x81 if Usable Area */
431                 char  flags0;
432                 char  flags1;
433                 short w;        /* Width of usable area */
434                 short h;        /* Heigth of usavle area */
435                 char  units;    /* 0x00:in; 0x01:mm */
436                 int   xr;
437                 int   yr;
438                 char  aw;
439                 char  ah;
440                 short buffsz;   /* Character buffer size, bytes */
441                 char  xmin;
442                 char  ymin;
443                 char  xmax;
444                 char  ymax;
445         } __attribute__ ((packed)) uab;
446         struct {        /* Alternate Usable Area Self-Defining Parameter */
447                 char  l;        /* Length of this Self-Defining Parm */
448                 char  sdpid;    /* 0x02 if Alternate Usable Area */
449                 char  res;
450                 char  auaid;    /* 0x01 is Id for the A U A */
451                 short wauai;    /* Width of AUAi */
452                 short hauai;    /* Height of AUAi */
453                 char  auaunits; /* 0x00:in, 0x01:mm */
454                 int   auaxr;
455                 int   auayr;
456                 char  awauai;
457                 char  ahauai;
458         } __attribute__ ((packed)) aua;
459 } __attribute__ ((packed));
460
461 static unsigned char raw3270_init_data[256];
462 static struct raw3270_request raw3270_init_request;
463 static struct diag210 raw3270_init_diag210;
464 static DECLARE_MUTEX(raw3270_init_sem);
465
466 static int
467 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
468                  struct irb *irb)
469 {
470         /*
471          * Unit-Check Processing:
472          * Expect Command Reject or Intervention Required.
473          */
474         if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
475                 /* Request finished abnormally. */
476                 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
477                         set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
478                         return RAW3270_IO_BUSY;
479                 }
480         }
481         if (rq) {
482                 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
483                         if (irb->ecw[0] & SNS0_CMD_REJECT)
484                                 rq->rc = -EOPNOTSUPP;
485                         else
486                                 rq->rc = -EIO;
487                 } else
488                         /* Request finished normally. Copy residual count. */
489                         rq->rescnt = irb->scsw.count;
490         }
491         if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
492                 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
493                 wake_up(&raw3270_wait_queue);
494         }
495         return RAW3270_IO_DONE;
496 }
497
498 static struct raw3270_fn raw3270_init_fn = {
499         .intv = raw3270_init_irq
500 };
501
502 static struct raw3270_view raw3270_init_view = {
503         .fn = &raw3270_init_fn
504 };
505
506 /*
507  * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
508  * Wait for end of request. The request must have been started
509  * with raw3270_start, rc = 0. The device lock may NOT have been
510  * released between calling raw3270_start and raw3270_wait.
511  */
512 static void
513 raw3270_wake_init(struct raw3270_request *rq, void *data)
514 {
515         wake_up((wait_queue_head_t *) data);
516 }
517
518 /*
519  * Special wait function that can cope with console initialization.
520  */
521 static int
522 raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
523                    struct raw3270_request *rq)
524 {
525         unsigned long flags;
526         wait_queue_head_t wq;
527         int rc;
528
529 #ifdef CONFIG_TN3270_CONSOLE
530         if (raw3270_registered == 0) {
531                 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
532                 rq->callback = 0;
533                 rc = __raw3270_start(rp, view, rq);
534                 if (rc == 0)
535                         while (!raw3270_request_final(rq)) {
536                                 wait_cons_dev();
537                                 barrier();
538                         }
539                 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
540                 return rq->rc;
541         }
542 #endif
543         init_waitqueue_head(&wq);
544         rq->callback = raw3270_wake_init;
545         rq->callback_data = &wq;
546         spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
547         rc = __raw3270_start(rp, view, rq);
548         spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
549         if (rc)
550                 return rc;
551         /* Now wait for the completion. */
552         rc = wait_event_interruptible(wq, raw3270_request_final(rq));
553         if (rc == -ERESTARTSYS) {       /* Interrupted by a signal. */
554                 raw3270_halt_io(view->dev, rq);
555                 return -ERESTARTSYS;
556         }
557         return rq->rc;
558 }
559
560 static int
561 __raw3270_size_device_vm(struct raw3270 *rp)
562 {
563         int rc, model;
564
565         raw3270_init_diag210.vrdcdvno = 
566                 _ccw_device_get_device_number(rp->cdev);
567         raw3270_init_diag210.vrdclen = sizeof(struct diag210);
568         rc = diag210(&raw3270_init_diag210);
569         if (rc)
570                 return rc;
571         model = raw3270_init_diag210.vrdccrmd;
572         switch (model) {
573         case 2:
574                 rp->model = model;
575                 rp->rows = 24;
576                 rp->cols = 80;
577                 break;
578         case 3:
579                 rp->model = model;
580                 rp->rows = 32;
581                 rp->cols = 80;
582                 break;
583         case 4:
584                 rp->model = model;
585                 rp->rows = 43;
586                 rp->cols = 80;
587                 break;
588         case 5:
589                 rp->model = model;
590                 rp->rows = 27;
591                 rp->cols = 132;
592                 break;
593         default:
594                 printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
595                 rc = -EOPNOTSUPP;
596                 break;
597         }
598         return rc;
599 }
600
601 static int
602 __raw3270_size_device(struct raw3270 *rp)
603 {
604         static const unsigned char wbuf[] =
605                 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
606         struct raw3270_ua *uap;
607         unsigned short count;
608         int rc;
609
610         /*
611          * To determine the size of the 3270 device we need to do:
612          * 1) send a 'read partition' data stream to the device
613          * 2) wait for the attn interrupt that preceeds the query reply
614          * 3) do a read modified to get the query reply
615          * To make things worse we have to cope with intervention
616          * required (3270 device switched to 'stand-by') and command
617          * rejects (old devices that can't do 'read partition').
618          */
619         memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
620         memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
621         /* Store 'read partition' data stream to raw3270_init_data */
622         memcpy(raw3270_init_data, wbuf, sizeof(wbuf));
623         INIT_LIST_HEAD(&raw3270_init_request.list);
624         raw3270_init_request.ccw.cmd_code = TC_WRITESF;
625         raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
626         raw3270_init_request.ccw.count = sizeof(wbuf);
627         raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
628
629         rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
630         if (rc) {
631                 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
632                 if (rc == -EOPNOTSUPP && MACHINE_IS_VM)
633                         return __raw3270_size_device_vm(rp);
634                 return rc;
635         }
636
637         /* Wait for attention interrupt. */
638 #ifdef CONFIG_TN3270_CONSOLE
639         if (raw3270_registered == 0) {
640                 unsigned long flags;
641
642                 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
643                 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
644                         wait_cons_dev();
645                 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
646         } else
647 #endif
648                 rc = wait_event_interruptible(raw3270_wait_queue,
649                         test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
650         if (rc)
651                 return rc;
652
653         /*
654          * The device accepted the 'read partition' command. Now
655          * set up a read ccw and issue it.
656          */
657         raw3270_init_request.ccw.cmd_code = TC_READMOD;
658         raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
659         raw3270_init_request.ccw.count = sizeof(raw3270_init_data);
660         raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
661         rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
662         if (rc)
663                 return rc;
664         /* Got a Query Reply */
665         count = sizeof(raw3270_init_data) - raw3270_init_request.rescnt;
666         uap = (struct raw3270_ua *) (raw3270_init_data + 1);
667         /* Paranoia check. */
668         if (raw3270_init_data[0] != 0x88 || uap->uab.qcode != 0x81)
669                 return -EOPNOTSUPP;
670         /* Copy rows/columns of default Usable Area */
671         rp->rows = uap->uab.h;
672         rp->cols = uap->uab.w;
673         /* Check for 14 bit addressing */
674         if ((uap->uab.flags0 & 0x0d) == 0x01)
675                 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
676         /* Check for Alternate Usable Area */
677         if (uap->uab.l == sizeof(struct raw3270_ua) &&
678             uap->aua.sdpid == 0x02) {
679                 rp->rows = uap->aua.hauai;
680                 rp->cols = uap->aua.wauai;
681         }
682         return 0;
683 }
684
685 static int
686 raw3270_size_device(struct raw3270 *rp)
687 {
688         int rc;
689
690         down(&raw3270_init_sem);
691         rp->view = &raw3270_init_view;
692         raw3270_init_view.dev = rp;
693         rc = __raw3270_size_device(rp);
694         raw3270_init_view.dev = 0;
695         rp->view = 0;
696         up(&raw3270_init_sem);
697         if (rc == 0) {  /* Found something. */
698                 /* Try to find a model. */
699                 rp->model = 0;
700                 if (rp->rows == 24 && rp->cols == 80)
701                         rp->model = 2;
702                 if (rp->rows == 32 && rp->cols == 80)
703                         rp->model = 3;
704                 if (rp->rows == 43 && rp->cols == 80)
705                         rp->model = 4;
706                 if (rp->rows == 27 && rp->cols == 132)
707                         rp->model = 5;
708         }
709         return rc;
710 }
711
712 static int
713 raw3270_reset_device(struct raw3270 *rp)
714 {
715         int rc;
716
717         down(&raw3270_init_sem);
718         memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
719         memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
720         /* Store reset data stream to raw3270_init_data/raw3270_init_request */
721         raw3270_init_data[0] = TW_KR;
722         INIT_LIST_HEAD(&raw3270_init_request.list);
723         raw3270_init_request.ccw.cmd_code = TC_EWRITEA;
724         raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
725         raw3270_init_request.ccw.count = 1;
726         raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
727         rp->view = &raw3270_init_view;
728         raw3270_init_view.dev = rp;
729         rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
730         raw3270_init_view.dev = 0;
731         rp->view = 0;
732         up(&raw3270_init_sem);
733         return rc;
734 }
735
736 /*
737  * Setup new 3270 device.
738  */
739 static int
740 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
741 {
742         struct list_head *l;
743         struct raw3270 *tmp;
744         int minor;
745
746         memset(rp, 0, sizeof(struct raw3270));
747         /* Copy ebcdic -> ascii translation table. */
748         memcpy(ascebc, _ascebc, 256);
749         if (tubxcorrect) {
750                 /* correct brackets and circumflex */
751                 ascebc['['] = 0xad;
752                 ascebc[']'] = 0xbd;
753                 ascebc['^'] = 0xb0;
754         }
755         rp->ascebc = ascebc;
756
757         /* Set defaults. */
758         rp->rows = 24;
759         rp->cols = 80;
760
761         INIT_LIST_HEAD(&rp->req_queue);
762         INIT_LIST_HEAD(&rp->view_list);
763
764         /*
765          * Add device to list and find the smallest unused minor
766          * number for it.
767          */
768         down(&raw3270_sem);
769         /* Keep the list sorted. */
770         minor = 0;
771         rp->minor = -1;
772         list_for_each(l, &raw3270_devices) {
773                 tmp = list_entry(l, struct raw3270, list);
774                 if (tmp->minor > minor) {
775                         rp->minor = minor;
776                         __list_add(&rp->list, l->prev, l);
777                         break;
778                 }
779                 minor++;
780         }
781         if (rp->minor == -1 && minor < RAW3270_MAXDEVS) {
782                 rp->minor = minor;
783                 list_add_tail(&rp->list, &raw3270_devices);
784         }
785         up(&raw3270_sem);
786         /* No free minor number? Then give up. */
787         if (rp->minor == -1)
788                 return -EUSERS;
789         rp->cdev = cdev;
790         cdev->dev.driver_data = rp;
791         cdev->handler = raw3270_irq;
792         return 0;
793 }
794
795 #ifdef CONFIG_TN3270_CONSOLE
796 /*
797  * Setup 3270 device configured as console.
798  */
799 struct raw3270 *
800 raw3270_setup_console(struct ccw_device *cdev)
801 {
802         struct raw3270 *rp;
803         char *ascebc;
804         int rc;
805
806         rp = (struct raw3270 *) alloc_bootmem(sizeof(struct raw3270));
807         ascebc = (char *) alloc_bootmem(256);
808         rc = raw3270_setup_device(cdev, rp, ascebc);
809         if (rc)
810                 return ERR_PTR(rc);
811         set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
812         raw3270_reset_device(rp);
813         raw3270_size_device(rp);
814         raw3270_reset_device(rp);
815         set_bit(RAW3270_FLAGS_READY, &rp->flags);
816         return rp;
817 }
818
819 void
820 raw3270_wait_cons_dev(struct raw3270 *rp)
821 {
822         unsigned long flags;
823
824         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
825         wait_cons_dev();
826         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
827 }
828
829 #endif
830
831 /*
832  * Create a 3270 device structure.
833  */
834 static struct raw3270 *
835 raw3270_create_device(struct ccw_device *cdev)
836 {
837         struct raw3270 *rp;
838         char *ascebc;
839         int rc;
840
841         rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL);
842         if (!rp)
843                 return ERR_PTR(-ENOMEM);
844         ascebc = kmalloc(256, GFP_KERNEL);
845         if (!ascebc) {
846                 kfree(rp);
847                 return ERR_PTR(-ENOMEM);
848         }
849         rc = raw3270_setup_device(cdev, rp, ascebc);
850         if (rc) {
851                 kfree(rp->ascebc);
852                 kfree(rp);
853                 rp = ERR_PTR(rc);
854         }
855         /* Get reference to ccw_device structure. */
856         get_device(&cdev->dev);
857         return rp;
858 }
859
860 /*
861  * Activate a view.
862  */
863 int
864 raw3270_activate_view(struct raw3270_view *view)
865 {
866         struct raw3270 *rp;
867         struct raw3270_view *oldview, *nv;
868         unsigned long flags;
869         int rc;
870
871         rp = view->dev;
872         if (!rp)
873                 return -ENODEV;
874         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
875         if (rp->view == view)
876                 rc = 0;
877         else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
878                 rc = -ENODEV;
879         else {
880                 oldview = 0;
881                 if (rp->view) {
882                         oldview = rp->view;
883                         oldview->fn->deactivate(oldview);
884                 }
885                 rp->view = view;
886                 rc = view->fn->activate(view);
887                 if (rc) {
888                         /* Didn't work. Try to reactivate the old view. */
889                         rp->view = oldview;
890                         if (!oldview || oldview->fn->activate(oldview) != 0) {
891                                 /* Didn't work as well. Try any other view. */
892                                 list_for_each_entry(nv, &rp->view_list, list)
893                                         if (nv != view && nv != oldview) {
894                                                 rp->view = nv;
895                                                 if (nv->fn->activate(nv) == 0)
896                                                         break;
897                                                 rp->view = 0;
898                                         }
899                         }
900                 }
901         }
902         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
903         return rc;
904 }
905
906 /*
907  * Deactivate current view.
908  */
909 void
910 raw3270_deactivate_view(struct raw3270_view *view)
911 {
912         unsigned long flags;
913         struct raw3270 *rp;
914
915         rp = view->dev;
916         if (!rp)
917                 return;
918         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
919         if (rp->view == view) {
920                 view->fn->deactivate(view);
921                 rp->view = 0;
922                 /* Move deactivated view to end of list. */
923                 list_del_init(&view->list);
924                 list_add_tail(&view->list, &rp->view_list);
925                 /* Try to activate another view. */
926                 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
927                         list_for_each_entry(view, &rp->view_list, list)
928                                 if (view->fn->activate(view) == 0) {
929                                         rp->view = view;
930                                         break;
931                                 }
932                 }
933         }
934         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
935 }
936
937 /*
938  * Add view to device with minor "minor".
939  */
940 int
941 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
942 {
943         unsigned long flags;
944         struct raw3270 *rp;
945         int rc;
946
947         down(&raw3270_sem);
948         rc = -ENODEV;
949         list_for_each_entry(rp, &raw3270_devices, list) {
950                 if (rp->minor != minor)
951                         continue;
952                 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
953                 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
954                         atomic_set(&view->ref_count, 2);
955                         view->dev = rp;
956                         view->fn = fn;
957                         view->model = rp->model;
958                         view->rows = rp->rows;
959                         view->cols = rp->cols;
960                         view->ascebc = rp->ascebc;
961                         spin_lock_init(&view->lock);
962                         list_add_tail(&view->list, &rp->view_list);
963                         rc = 0;
964                 }
965                 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
966                 break;
967         }
968         up(&raw3270_sem);
969         return rc;
970 }
971
972 /*
973  * Find specific view of device with minor "minor".
974  */
975 struct raw3270_view *
976 raw3270_find_view(struct raw3270_fn *fn, int minor)
977 {
978         struct raw3270 *rp;
979         struct raw3270_view *view, *tmp;
980         unsigned long flags;
981
982         down(&raw3270_sem);
983         view = ERR_PTR(-ENODEV);
984         list_for_each_entry(rp, &raw3270_devices, list) {
985                 if (rp->minor != minor)
986                         continue;
987                 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
988                 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
989                         view = ERR_PTR(-ENOENT);
990                         list_for_each_entry(tmp, &rp->view_list, list) {
991                                 if (tmp->fn == fn) {
992                                         raw3270_get_view(tmp);
993                                         view = tmp;
994                                         break;
995                                 }
996                         }
997                 }
998                 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
999                 break;
1000         }
1001         up(&raw3270_sem);
1002         return view;
1003 }
1004
1005 /*
1006  * Remove view from device and free view structure via call to view->fn->free.
1007  */
1008 void
1009 raw3270_del_view(struct raw3270_view *view)
1010 {
1011         unsigned long flags;
1012         struct raw3270 *rp;
1013         struct raw3270_view *nv;
1014
1015         rp = view->dev;
1016         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1017         if (rp->view == view) {
1018                 view->fn->deactivate(view);
1019                 rp->view = 0;
1020         }
1021         list_del_init(&view->list);
1022         if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1023                 /* Try to activate another view. */
1024                 list_for_each_entry(nv, &rp->view_list, list) {
1025                         if (nv->fn->activate(view) == 0) {
1026                                 rp->view = nv;
1027                                 break;
1028                         }
1029                 }
1030         }
1031         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1032         /* Wait for reference counter to drop to zero. */
1033         atomic_sub(2, &view->ref_count);
1034         wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1035         if (view->fn->free)
1036                 view->fn->free(view);
1037 }
1038
1039 /*
1040  * Remove a 3270 device structure.
1041  */
1042 static void
1043 raw3270_delete_device(struct raw3270 *rp)
1044 {
1045         struct ccw_device *cdev;
1046
1047         /* Remove from device chain. */
1048         down(&raw3270_sem);
1049         list_del_init(&rp->list);
1050         up(&raw3270_sem);
1051
1052         /* Disconnect from ccw_device. */
1053         cdev = rp->cdev;
1054         rp->cdev = 0;
1055         cdev->dev.driver_data = 0;
1056         cdev->handler = 0;
1057
1058         /* Put ccw_device structure. */
1059         put_device(&cdev->dev);
1060
1061         /* Now free raw3270 structure. */
1062         kfree(rp->ascebc);
1063         kfree(rp);
1064 }
1065
1066 static int
1067 raw3270_probe (struct ccw_device *cdev)
1068 {
1069         return 0;
1070 }
1071
1072 /*
1073  * Additional attributes for a 3270 device
1074  */
1075 static ssize_t
1076 raw3270_model_show(struct device *dev, char *buf)
1077 {
1078         return snprintf(buf, PAGE_SIZE, "%i\n",
1079                         ((struct raw3270 *) dev->driver_data)->model);
1080 }
1081 static DEVICE_ATTR(model, 0444, raw3270_model_show, 0);
1082
1083 static ssize_t
1084 raw3270_rows_show(struct device *dev, char *buf)
1085 {
1086         return snprintf(buf, PAGE_SIZE, "%i\n",
1087                         ((struct raw3270 *) dev->driver_data)->rows);
1088 }
1089 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, 0);
1090
1091 static ssize_t
1092 raw3270_columns_show(struct device *dev, char *buf)
1093 {
1094         return snprintf(buf, PAGE_SIZE, "%i\n",
1095                         ((struct raw3270 *) dev->driver_data)->cols);
1096 }
1097 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, 0);
1098
1099 static struct attribute * raw3270_attrs[] = {
1100         &dev_attr_model.attr,
1101         &dev_attr_rows.attr,
1102         &dev_attr_columns.attr,
1103         NULL,
1104 };
1105
1106 static struct attribute_group raw3270_attr_group = {
1107         .attrs = raw3270_attrs,
1108 };
1109
1110 static void
1111 raw3270_create_attributes(struct raw3270 *rp)
1112 {
1113         //FIXME: check return code
1114         sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1115 }
1116
1117 /*
1118  * Notifier for device addition/removal
1119  */
1120 struct raw3270_notifier {
1121         struct list_head list;
1122         void (*notifier)(int, int);
1123 };
1124
1125 static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
1126
1127 int raw3270_register_notifier(void (*notifier)(int, int))
1128 {
1129         struct raw3270_notifier *np;
1130         struct raw3270 *rp;
1131
1132         np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1133         if (!np)
1134                 return -ENOMEM;
1135         np->notifier = notifier;
1136         down(&raw3270_sem);
1137         list_add_tail(&np->list, &raw3270_notifier);
1138         list_for_each_entry(rp, &raw3270_devices, list) {
1139                 get_device(&rp->cdev->dev);
1140                 notifier(rp->minor, 1);
1141         }
1142         up(&raw3270_sem);
1143         return 0;
1144 }
1145
1146 void raw3270_unregister_notifier(void (*notifier)(int, int))
1147 {
1148         struct raw3270_notifier *np;
1149
1150         down(&raw3270_sem);
1151         list_for_each_entry(np, &raw3270_notifier, list)
1152                 if (np->notifier == notifier) {
1153                         list_del(&np->list);
1154                         kfree(np);
1155                         break;
1156                 }
1157         up(&raw3270_sem);
1158 }
1159
1160 /*
1161  * Set 3270 device online.
1162  */
1163 static int
1164 raw3270_set_online (struct ccw_device *cdev)
1165 {
1166         struct raw3270 *rp;
1167         struct raw3270_notifier *np;
1168
1169         rp = raw3270_create_device(cdev);
1170         if (IS_ERR(rp))
1171                 return PTR_ERR(rp);
1172         raw3270_reset_device(rp);
1173         raw3270_size_device(rp);
1174         raw3270_reset_device(rp);
1175         raw3270_create_attributes(rp);
1176         set_bit(RAW3270_FLAGS_READY, &rp->flags);
1177         down(&raw3270_sem);
1178         list_for_each_entry(np, &raw3270_notifier, list)
1179                 np->notifier(rp->minor, 1);
1180         up(&raw3270_sem);
1181         return 0;
1182 }
1183
1184 /*
1185  * Remove 3270 device structure.
1186  */
1187 static void
1188 raw3270_remove (struct ccw_device *cdev)
1189 {
1190         unsigned long flags;
1191         struct raw3270 *rp;
1192         struct raw3270_view *v;
1193         struct raw3270_notifier *np;
1194
1195         rp = cdev->dev.driver_data;
1196         clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1197
1198         sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1199
1200         /* Deactivate current view and remove all views. */
1201         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1202         if (rp->view) {
1203                 rp->view->fn->deactivate(rp->view);
1204                 rp->view = 0;
1205         }
1206         while (!list_empty(&rp->view_list)) {
1207                 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1208                 if (v->fn->release)
1209                         v->fn->release(v);
1210                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1211                 raw3270_del_view(v);
1212                 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1213         }
1214         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1215
1216         down(&raw3270_sem);
1217         list_for_each_entry(np, &raw3270_notifier, list)
1218                 np->notifier(rp->minor, 0);
1219         up(&raw3270_sem);
1220
1221         /* Reset 3270 device. */
1222         raw3270_reset_device(rp);
1223         /* And finally remove it. */
1224         raw3270_delete_device(rp);
1225 }
1226
1227 /*
1228  * Set 3270 device offline.
1229  */
1230 static int
1231 raw3270_set_offline (struct ccw_device *cdev)
1232 {
1233         struct raw3270 *rp;
1234
1235         rp = cdev->dev.driver_data;
1236         if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1237                 return -EBUSY;
1238         raw3270_remove(cdev);
1239         return 0;
1240 }
1241
1242 static struct ccw_device_id raw3270_id[] = {
1243         { CCW_DEVICE(0x3270, 0) },
1244         { CCW_DEVICE(0x3271, 0) },
1245         { CCW_DEVICE(0x3272, 0) },
1246         { CCW_DEVICE(0x3273, 0) },
1247         { CCW_DEVICE(0x3274, 0) },
1248         { CCW_DEVICE(0x3275, 0) },
1249         { CCW_DEVICE(0x3276, 0) },
1250         { CCW_DEVICE(0x3277, 0) },
1251         { CCW_DEVICE(0x3278, 0) },
1252         { CCW_DEVICE(0x3279, 0) },
1253         { CCW_DEVICE(0x3174, 0) },
1254         { /* end of list */ },
1255 };
1256
1257 static struct ccw_driver raw3270_ccw_driver = {
1258         .name           = "3270",
1259         .owner          = THIS_MODULE,
1260         .ids            = raw3270_id,
1261         .probe          = &raw3270_probe,
1262         .remove         = &raw3270_remove,
1263         .set_online     = &raw3270_set_online,
1264         .set_offline    = &raw3270_set_offline,
1265 };
1266
1267 static int
1268 raw3270_init(void)
1269 {
1270         struct raw3270 *rp;
1271         int rc;
1272
1273         if (raw3270_registered)
1274                 return 0;
1275         raw3270_registered = 1;
1276         rc = ccw_driver_register(&raw3270_ccw_driver);
1277         if (rc == 0) {
1278                 /* Create attributes for early (= console) device. */
1279                 down(&raw3270_sem);
1280                 list_for_each_entry(rp, &raw3270_devices, list) {
1281                         get_device(&rp->cdev->dev);
1282                         raw3270_create_attributes(rp);
1283                 }
1284                 up(&raw3270_sem);
1285         }
1286         return rc;
1287 }
1288
1289 static void
1290 raw3270_exit(void)
1291 {
1292         ccw_driver_unregister(&raw3270_ccw_driver);
1293 }
1294
1295 MODULE_LICENSE("GPL");
1296
1297 module_init(raw3270_init);
1298 module_exit(raw3270_exit);
1299
1300 EXPORT_SYMBOL(raw3270_request_alloc);
1301 EXPORT_SYMBOL(raw3270_request_free);
1302 EXPORT_SYMBOL(raw3270_request_reset);
1303 EXPORT_SYMBOL(raw3270_request_set_cmd);
1304 EXPORT_SYMBOL(raw3270_request_add_data);
1305 EXPORT_SYMBOL(raw3270_request_set_data);
1306 EXPORT_SYMBOL(raw3270_request_set_idal);
1307 EXPORT_SYMBOL(raw3270_buffer_address);
1308 EXPORT_SYMBOL(raw3270_add_view);
1309 EXPORT_SYMBOL(raw3270_del_view);
1310 EXPORT_SYMBOL(raw3270_find_view);
1311 EXPORT_SYMBOL(raw3270_activate_view);
1312 EXPORT_SYMBOL(raw3270_deactivate_view);
1313 EXPORT_SYMBOL(raw3270_start);
1314 EXPORT_SYMBOL(raw3270_start_irq);
1315 EXPORT_SYMBOL(raw3270_register_notifier);
1316 EXPORT_SYMBOL(raw3270_unregister_notifier);
1317 EXPORT_SYMBOL(raw3270_wait_queue);