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