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