drm/nouveau: consume the return of large GSP message
[drm/drm-misc.git] / drivers / s390 / char / raw3270.c
blobba3d7114b34fe59f0c1e4e9fc89a92bab036608d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * IBM/3270 Driver - core functions.
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 */
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>
20 #include <asm/ccwdev.h>
21 #include <asm/cio.h>
22 #include <asm/ebcdic.h>
23 #include <asm/diag.h>
25 #include "raw3270.h"
27 #include <linux/major.h>
28 #include <linux/kdev_t.h>
29 #include <linux/device.h>
30 #include <linux/mutex.h>
32 const struct class class3270 = {
33 .name = "3270",
35 EXPORT_SYMBOL(class3270);
37 /* The main 3270 data structure. */
38 struct raw3270 {
39 struct list_head list;
40 struct ccw_device *cdev;
41 int minor;
43 int model, rows, cols;
44 int old_model, old_rows, old_cols;
45 unsigned int state;
46 unsigned long flags;
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. */
52 struct timer_list timer; /* Device timer. */
54 unsigned char *ascebc; /* ascii -> ebcdic table */
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;
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 */
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. */
76 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
77 static DEFINE_MUTEX(raw3270_mutex);
79 /* List of 3270 devices. */
80 static LIST_HEAD(raw3270_devices);
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).
87 static int raw3270_registered;
89 /* Module parameters */
90 static bool tubxcorrect;
91 module_param(tubxcorrect, bool, 0);
94 * Wait queue for device init/delete, view delete.
96 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
97 EXPORT_SYMBOL(raw3270_wait_queue);
99 static void __raw3270_disconnect(struct raw3270 *rp);
102 * Encode array for 12 bit 3270 addresses.
104 static 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
115 static inline int raw3270_state_ready(struct raw3270 *rp)
117 return rp->state == RAW3270_STATE_READY;
120 void raw3270_buffer_address(struct raw3270 *rp, char *cp, int x, int y)
122 int addr;
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];
137 EXPORT_SYMBOL(raw3270_buffer_address);
140 * Allocate a new 3270 ccw request
142 struct raw3270_request *raw3270_request_alloc(size_t size)
144 struct raw3270_request *rq;
146 /* Allocate request structure */
147 rq = kzalloc(sizeof(*rq), GFP_KERNEL | GFP_DMA);
148 if (!rq)
149 return ERR_PTR(-ENOMEM);
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);
159 rq->size = size;
160 INIT_LIST_HEAD(&rq->list);
163 * Setup ccw.
165 if (rq->buffer)
166 rq->ccw.cda = virt_to_dma32(rq->buffer);
167 rq->ccw.flags = CCW_FLAG_SLI;
169 return rq;
171 EXPORT_SYMBOL(raw3270_request_alloc);
174 * Free 3270 ccw request
176 void raw3270_request_free(struct raw3270_request *rq)
178 kfree(rq->buffer);
179 kfree(rq);
181 EXPORT_SYMBOL(raw3270_request_free);
184 * Reset request to initial state.
186 int raw3270_request_reset(struct raw3270_request *rq)
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;
199 EXPORT_SYMBOL(raw3270_request_reset);
202 * Set command code to ccw of a request.
204 void raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
206 rq->ccw.cmd_code = cmd;
208 EXPORT_SYMBOL(raw3270_request_set_cmd);
211 * Add data fragment to output buffer.
213 int raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
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;
221 EXPORT_SYMBOL(raw3270_request_add_data);
224 * Set address/length pair to ccw of a request.
226 void raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
228 rq->ccw.cda = virt_to_dma32(data);
229 rq->ccw.count = size;
231 EXPORT_SYMBOL(raw3270_request_set_data);
234 * Set idal buffer to ccw of a request.
236 void raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
238 rq->ccw.cda = virt_to_dma32(ib->data);
239 rq->ccw.count = ib->size;
240 rq->ccw.flags |= CCW_FLAG_IDA;
242 EXPORT_SYMBOL(raw3270_request_set_idal);
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.
248 static int __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
249 struct raw3270_request *rq)
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;
263 list_add_tail(&rq->list, &rp->req_queue);
264 return 0;
267 int raw3270_view_active(struct raw3270_view *view)
269 struct raw3270 *rp = view->dev;
271 return rp && rp->view == view;
274 int raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
276 unsigned long flags;
277 struct raw3270 *rp;
278 int rc;
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;
291 EXPORT_SYMBOL(raw3270_start);
293 int raw3270_start_request(struct raw3270_view *view, struct raw3270_request *rq,
294 int cmd, void *data, size_t len)
296 int rc;
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);
307 EXPORT_SYMBOL(raw3270_start_request);
309 int raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
311 struct raw3270 *rp;
312 int rc;
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;
323 EXPORT_SYMBOL(raw3270_start_locked);
325 int raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
327 struct raw3270 *rp;
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;
335 EXPORT_SYMBOL(raw3270_start_irq);
338 * 3270 interrupt routine, called from the ccw_device layer
340 static void raw3270_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
342 struct raw3270 *rp;
343 struct raw3270_view *view;
344 struct raw3270_request *rq;
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;
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);
367 /* Call interrupt handler of the view */
368 if (view)
369 view->fn->intv(view, rq, irb);
372 if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags))
373 /* Device busy, do not start I/O */
374 return;
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);
386 * Try to start each request on request queue until one is
387 * started successful.
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);
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').
413 struct 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;
448 static void raw3270_size_device_vm(struct raw3270 *rp)
450 int rc, model;
451 struct ccw_dev_id dev_id;
452 struct diag210 diag_data;
453 struct diag8c diag8c_data;
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;
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;
497 static void raw3270_size_device(struct raw3270 *rp, char *init_data)
499 struct raw3270_ua *uap;
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;
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;
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;
535 static void raw3270_resize_work(struct work_struct *work)
537 struct raw3270 *rp = container_of(work, struct raw3270, resize_work);
538 struct raw3270_view *view;
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);
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;
558 static void raw3270_size_device_done(struct raw3270 *rp)
560 rp->view = NULL;
561 rp->state = RAW3270_STATE_READY;
562 schedule_work(&rp->resize_work);
565 void raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
567 struct raw3270 *rp = rq->view->dev;
569 raw3270_size_device(rp, data);
570 raw3270_size_device_done(rp);
572 EXPORT_SYMBOL(raw3270_read_modified_cb);
574 static void raw3270_read_modified(struct raw3270 *rp)
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);
591 static void raw3270_writesf_readpart(struct raw3270 *rp)
593 static const unsigned char wbuf[] = {
594 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81
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);
610 * Device reset
612 static void raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
614 struct raw3270 *rp = rq->view->dev;
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);
627 memset(&rp->init_reset, 0, sizeof(rp->init_reset));
630 static int __raw3270_reset_device(struct raw3270 *rp)
632 int rc;
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;
650 static int raw3270_reset_device(struct raw3270 *rp)
652 unsigned long flags;
653 int rc;
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;
661 int raw3270_reset(struct raw3270_view *view)
663 struct raw3270 *rp;
664 int rc;
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;
675 EXPORT_SYMBOL(raw3270_reset);
677 static void __raw3270_disconnect(struct raw3270 *rp)
679 struct raw3270_request *rq;
680 struct raw3270_view *view;
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);
694 /* Start from scratch */
695 __raw3270_reset_device(rp);
698 static void raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
699 struct irb *irb)
701 struct raw3270 *rp;
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;
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);
718 static struct raw3270_fn raw3270_init_fn = {
719 .intv = raw3270_init_irq
723 * Setup new 3270 device.
725 static int raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp,
726 char *ascebc)
728 struct list_head *l;
729 struct raw3270 *tmp;
730 int minor;
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;
741 rp->ascebc = ascebc;
743 /* Set defaults. */
744 rp->rows = 24;
745 rp->cols = 80;
746 rp->old_rows = rp->rows;
747 rp->old_cols = rp->cols;
749 INIT_LIST_HEAD(&rp->req_queue);
750 INIT_LIST_HEAD(&rp->view_list);
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);
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().
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;
773 minor++;
775 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
776 rp->minor = minor;
777 list_add_tail(&rp->list, &raw3270_devices);
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;
789 #ifdef CONFIG_TN3270_CONSOLE
790 /* Tentative definition - see below for actual definition. */
791 static struct ccw_driver raw3270_ccw_driver;
793 static inline int raw3270_state_final(struct raw3270 *rp)
795 return rp->state == RAW3270_STATE_INIT ||
796 rp->state == RAW3270_STATE_READY;
800 * Setup 3270 device configured as console.
802 struct raw3270 __init *raw3270_setup_console(void)
804 struct ccw_device *cdev;
805 unsigned long flags;
806 struct raw3270 *rp;
807 char *ascebc;
808 int rc;
810 cdev = ccw_device_create_console(&raw3270_ccw_driver);
811 if (IS_ERR(cdev))
812 return ERR_CAST(cdev);
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);
821 rc = ccw_device_enable_console(cdev);
822 if (rc) {
823 ccw_device_destroy_console(cdev);
824 return ERR_PTR(rc);
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();
834 } while (rp->state != RAW3270_STATE_READY);
835 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
836 return rp;
839 void raw3270_wait_cons_dev(struct raw3270 *rp)
841 unsigned long flags;
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);
848 #endif
851 * Create a 3270 device structure.
853 static struct raw3270 *raw3270_create_device(struct ccw_device *cdev)
855 struct raw3270 *rp;
856 char *ascebc;
857 int rc;
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);
867 rc = raw3270_setup_device(cdev, rp, ascebc);
868 if (rc) {
869 kfree(rp->ascebc);
870 kfree(rp);
871 rp = ERR_PTR(rc);
873 /* Get reference to ccw_device structure. */
874 get_device(&cdev->dev);
875 return rp;
879 * This helper just validates that it is safe to activate a
880 * view in the panic() context, due to locking restrictions.
882 int raw3270_view_lock_unavailable(struct raw3270_view *view)
884 struct raw3270 *rp = view->dev;
886 if (!rp)
887 return -ENODEV;
888 if (spin_is_locked(get_ccwdev_lock(rp->cdev)))
889 return -EBUSY;
890 return 0;
893 static int raw3270_assign_activate_view(struct raw3270 *rp, struct raw3270_view *view)
895 rp->view = view;
896 return view->fn->activate(view);
899 static int __raw3270_activate_view(struct raw3270 *rp, struct raw3270_view *view)
901 struct raw3270_view *oldview = NULL, *nv;
902 int rc;
904 if (rp->view == view)
905 return 0;
907 if (!raw3270_state_ready(rp))
908 return -EBUSY;
910 if (rp->view && rp->view->fn->deactivate) {
911 oldview = rp->view;
912 oldview->fn->deactivate(oldview);
915 rc = raw3270_assign_activate_view(rp, view);
916 if (!rc)
917 return 0;
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;
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;
935 return rc;
939 * Activate a view.
941 int raw3270_activate_view(struct raw3270_view *view)
943 struct raw3270 *rp;
944 unsigned long flags;
945 int rc;
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;
955 EXPORT_SYMBOL(raw3270_activate_view);
958 * Deactivate current view.
960 void raw3270_deactivate_view(struct raw3270_view *view)
962 unsigned long flags;
963 struct raw3270 *rp;
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;
985 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
987 EXPORT_SYMBOL(raw3270_deactivate_view);
990 * Add view to device with minor "minor".
992 int raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn,
993 int minor, int subclass)
995 unsigned long flags;
996 struct raw3270 *rp;
997 int rc;
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;
1021 mutex_unlock(&raw3270_mutex);
1022 return rc;
1024 EXPORT_SYMBOL(raw3270_add_view);
1027 * Find specific view of device with minor "minor".
1029 struct raw3270_view *raw3270_find_view(struct raw3270_fn *fn, int minor)
1031 struct raw3270 *rp;
1032 struct raw3270_view *view, *tmp;
1033 unsigned long flags;
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;
1048 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1049 break;
1051 mutex_unlock(&raw3270_mutex);
1052 return view;
1054 EXPORT_SYMBOL(raw3270_find_view);
1057 * Remove view from device and free view structure via call to view->fn->free.
1059 void raw3270_del_view(struct raw3270_view *view)
1061 unsigned long flags;
1062 struct raw3270 *rp;
1063 struct raw3270_view *nv;
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;
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;
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);
1088 EXPORT_SYMBOL(raw3270_del_view);
1091 * Remove a 3270 device structure.
1093 static void raw3270_delete_device(struct raw3270 *rp)
1095 struct ccw_device *cdev;
1097 /* Remove from device chain. */
1098 mutex_lock(&raw3270_mutex);
1099 list_del_init(&rp->list);
1100 mutex_unlock(&raw3270_mutex);
1102 /* Disconnect from ccw_device. */
1103 cdev = rp->cdev;
1104 rp->cdev = NULL;
1105 dev_set_drvdata(&cdev->dev, NULL);
1106 cdev->handler = NULL;
1108 /* Put ccw_device structure. */
1109 put_device(&cdev->dev);
1111 /* Now free raw3270 structure. */
1112 kfree(rp->ascebc);
1113 kfree(rp);
1116 static int raw3270_probe(struct ccw_device *cdev)
1118 return 0;
1122 * Additional attributes for a 3270 device
1124 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1125 char *buf)
1127 return sysfs_emit(buf, "%i\n",
1128 ((struct raw3270 *)dev_get_drvdata(dev))->model);
1130 static DEVICE_ATTR_RO(model);
1132 static ssize_t rows_show(struct device *dev, struct device_attribute *attr,
1133 char *buf)
1135 return sysfs_emit(buf, "%i\n",
1136 ((struct raw3270 *)dev_get_drvdata(dev))->rows);
1138 static DEVICE_ATTR_RO(rows);
1140 static ssize_t
1141 columns_show(struct device *dev, struct device_attribute *attr,
1142 char *buf)
1144 return sysfs_emit(buf, "%i\n",
1145 ((struct raw3270 *)dev_get_drvdata(dev))->cols);
1147 static DEVICE_ATTR_RO(columns);
1149 static struct attribute *raw3270_attrs[] = {
1150 &dev_attr_model.attr,
1151 &dev_attr_rows.attr,
1152 &dev_attr_columns.attr,
1153 NULL,
1156 static const struct attribute_group raw3270_attr_group = {
1157 .attrs = raw3270_attrs,
1160 static int raw3270_create_attributes(struct raw3270 *rp)
1162 return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1166 * Notifier for device addition/removal
1168 static LIST_HEAD(raw3270_notifier);
1170 int raw3270_register_notifier(struct raw3270_notifier *notifier)
1172 struct raw3270 *rp;
1174 mutex_lock(&raw3270_mutex);
1175 list_add_tail(&notifier->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;
1181 EXPORT_SYMBOL(raw3270_register_notifier);
1183 void raw3270_unregister_notifier(struct raw3270_notifier *notifier)
1185 struct raw3270 *rp;
1187 mutex_lock(&raw3270_mutex);
1188 list_for_each_entry(rp, &raw3270_devices, list)
1189 notifier->destroy(rp->minor);
1190 list_del(&notifier->list);
1191 mutex_unlock(&raw3270_mutex);
1193 EXPORT_SYMBOL(raw3270_unregister_notifier);
1196 * Set 3270 device online.
1198 static int raw3270_set_online(struct ccw_device *cdev)
1200 struct raw3270_notifier *np;
1201 struct raw3270 *rp;
1202 int rc;
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;
1217 failure:
1218 raw3270_delete_device(rp);
1219 return rc;
1223 * Remove 3270 device structure.
1225 static void raw3270_remove(struct ccw_device *cdev)
1227 unsigned long flags;
1228 struct raw3270 *rp;
1229 struct raw3270_view *v;
1230 struct raw3270_notifier *np;
1232 rp = dev_get_drvdata(&cdev->dev);
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.
1239 if (!rp)
1240 return;
1242 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
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;
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);
1259 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1261 mutex_lock(&raw3270_mutex);
1262 list_for_each_entry(np, &raw3270_notifier, list)
1263 np->destroy(rp->minor);
1264 mutex_unlock(&raw3270_mutex);
1266 /* Reset 3270 device. */
1267 raw3270_reset_device(rp);
1268 /* And finally remove it. */
1269 raw3270_delete_device(rp);
1273 * Set 3270 device offline.
1275 static int raw3270_set_offline(struct ccw_device *cdev)
1277 struct raw3270 *rp;
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;
1286 static 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 */ },
1301 static struct ccw_driver raw3270_ccw_driver = {
1302 .driver = {
1303 .name = "3270",
1304 .owner = THIS_MODULE,
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,
1314 static int raw3270_init(void)
1316 struct raw3270 *rp;
1317 int rc;
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);
1334 mutex_unlock(&raw3270_mutex);
1335 return 0;
1338 static void raw3270_exit(void)
1340 ccw_driver_unregister(&raw3270_ccw_driver);
1341 class_unregister(&class3270);
1344 MODULE_DESCRIPTION("IBM/3270 Driver - core functions");
1345 MODULE_LICENSE("GPL");
1347 module_init(raw3270_init);
1348 module_exit(raw3270_exit);