blob: a2da898ce90fd942ef272202ebc407eedbe40d10 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02002 * IBM/3270 Driver - core functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02004 * Author(s):
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Copyright IBM Corp. 2003, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/list.h>
15#include <linux/slab.h>
16#include <linux/types.h>
17#include <linux/wait.h>
18
19#include <asm/ccwdev.h>
20#include <asm/cio.h>
21#include <asm/ebcdic.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020022#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "raw3270.h"
25
Richard Hitted3cb6f2005-10-30 15:00:10 -080026#include <linux/major.h>
27#include <linux/kdev_t.h>
28#include <linux/device.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080029#include <linux/mutex.h>
Richard Hitted3cb6f2005-10-30 15:00:10 -080030
Martin Schwidefskyc95571e2013-01-08 15:31:11 +010031struct class *class3270;
Richard Hitted3cb6f2005-10-30 15:00:10 -080032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* The main 3270 data structure. */
34struct raw3270 {
35 struct list_head list;
36 struct ccw_device *cdev;
37 int minor;
38
39 short model, rows, cols;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +010040 unsigned int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 unsigned long flags;
42
43 struct list_head req_queue; /* Request queue. */
44 struct list_head view_list; /* List of available views. */
45 struct raw3270_view *view; /* Active view. */
46
47 struct timer_list timer; /* Device timer. */
48
49 unsigned char *ascebc; /* ascii -> ebcdic table */
Martin Schwidefsky132fab12006-06-29 14:57:39 +020050
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +010051 struct raw3270_view init_view;
52 struct raw3270_request init_reset;
53 struct raw3270_request init_readpart;
54 struct raw3270_request init_readmod;
Martin Schwidefsky132fab12006-06-29 14:57:39 +020055 unsigned char init_data[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +010058/* raw3270->state */
59#define RAW3270_STATE_INIT 0 /* Initial state */
60#define RAW3270_STATE_RESET 1 /* Reset command is pending */
61#define RAW3270_STATE_W4ATTN 2 /* Wait for attention interrupt */
62#define RAW3270_STATE_READMOD 3 /* Read partition is pending */
63#define RAW3270_STATE_READY 4 /* Device is usable by views */
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* raw3270->flags */
66#define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
67#define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +010068#define RAW3270_FLAGS_CONSOLE 2 /* Device is the console. */
69#define RAW3270_FLAGS_FROZEN 3 /* set if 3270 is frozen for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* Semaphore to protect global data of raw3270 (devices, views, etc). */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080072static DEFINE_MUTEX(raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* List of 3270 devices. */
Denis Chengc11ca972008-01-26 14:11:13 +010075static LIST_HEAD(raw3270_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Flag to indicate if the driver has been registered. Some operations
79 * like waiting for the end of i/o need to be done differently as long
80 * as the kernel is still starting up (console support).
81 */
82static int raw3270_registered;
83
84/* Module parameters */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103085static bool tubxcorrect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086module_param(tubxcorrect, bool, 0);
87
88/*
89 * Wait queue for device init/delete, view delete.
90 */
91DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
92
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +020093static void __raw3270_disconnect(struct raw3270 *rp);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Encode array for 12 bit 3270 addresses.
97 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +010098static unsigned char raw3270_ebcgraf[64] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
100 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
101 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
102 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
103 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
104 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
105 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
106 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
107};
108
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100109static inline int raw3270_state_ready(struct raw3270 *rp)
110{
111 return rp->state == RAW3270_STATE_READY;
112}
113
114static inline int raw3270_state_final(struct raw3270 *rp)
115{
116 return rp->state == RAW3270_STATE_INIT ||
117 rp->state == RAW3270_STATE_READY;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120void
121raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
122{
123 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
124 cp[0] = (addr >> 8) & 0x3f;
125 cp[1] = addr & 0xff;
126 } else {
127 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
128 cp[1] = raw3270_ebcgraf[addr & 0x3f];
129 }
130}
131
132/*
133 * Allocate a new 3270 ccw request
134 */
135struct raw3270_request *
136raw3270_request_alloc(size_t size)
137{
138 struct raw3270_request *rq;
139
140 /* Allocate request structure */
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800141 rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!rq)
143 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* alloc output buffer. */
146 if (size > 0) {
147 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
148 if (!rq->buffer) {
149 kfree(rq);
150 return ERR_PTR(-ENOMEM);
151 }
152 }
153 rq->size = size;
154 INIT_LIST_HEAD(&rq->list);
155
156 /*
157 * Setup ccw.
158 */
159 rq->ccw.cda = __pa(rq->buffer);
160 rq->ccw.flags = CCW_FLAG_SLI;
161
162 return rq;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * Free 3270 ccw request
167 */
168void
169raw3270_request_free (struct raw3270_request *rq)
170{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800171 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree(rq);
173}
174
175/*
176 * Reset request to initial state.
177 */
178void
179raw3270_request_reset(struct raw3270_request *rq)
180{
181 BUG_ON(!list_empty(&rq->list));
182 rq->ccw.cmd_code = 0;
183 rq->ccw.count = 0;
184 rq->ccw.cda = __pa(rq->buffer);
185 rq->ccw.flags = CCW_FLAG_SLI;
186 rq->rescnt = 0;
187 rq->rc = 0;
188}
189
190/*
191 * Set command code to ccw of a request.
192 */
193void
194raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
195{
196 rq->ccw.cmd_code = cmd;
197}
198
199/*
200 * Add data fragment to output buffer.
201 */
202int
203raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
204{
205 if (size + rq->ccw.count > rq->size)
206 return -E2BIG;
207 memcpy(rq->buffer + rq->ccw.count, data, size);
208 rq->ccw.count += size;
209 return 0;
210}
211
212/*
213 * Set address/length pair to ccw of a request.
214 */
215void
216raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
217{
218 rq->ccw.cda = __pa(data);
219 rq->ccw.count = size;
220}
221
222/*
223 * Set idal buffer to ccw of a request.
224 */
225void
226raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
227{
228 rq->ccw.cda = __pa(ib->data);
229 rq->ccw.count = ib->size;
230 rq->ccw.flags |= CCW_FLAG_IDA;
231}
232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Add the request to the request queue, try to start it if the
235 * 3270 device is idle. Return without waiting for end of i/o.
236 */
237static int
238__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
239 struct raw3270_request *rq)
240{
241 rq->view = view;
242 raw3270_get_view(view);
243 if (list_empty(&rp->req_queue) &&
244 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
245 /* No other requests are on the queue. Start this one. */
246 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
247 (unsigned long) rq, 0, 0);
248 if (rq->rc) {
249 raw3270_put_view(view);
250 return rq->rc;
251 }
252 }
253 list_add_tail(&rq->list, &rp->req_queue);
254 return 0;
255}
256
257int
Martin Schwidefsky233faec2014-03-21 09:25:24 +0100258raw3270_view_active(struct raw3270_view *view)
259{
260 struct raw3270 *rp = view->dev;
261
262 return rp && rp->view == view &&
263 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
264}
265
266int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
268{
269 unsigned long flags;
270 struct raw3270 *rp;
271 int rc;
272
273 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
274 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200275 if (!rp || rp->view != view ||
276 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 rc = -EACCES;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100278 else if (!raw3270_state_ready(rp))
279 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 else
281 rc = __raw3270_start(rp, view, rq);
282 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
283 return rc;
284}
285
286int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800287raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
288{
289 struct raw3270 *rp;
290 int rc;
291
292 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200293 if (!rp || rp->view != view ||
294 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800295 rc = -EACCES;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100296 else if (!raw3270_state_ready(rp))
297 rc = -EBUSY;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800298 else
299 rc = __raw3270_start(rp, view, rq);
300 return rc;
301}
302
303int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
305{
306 struct raw3270 *rp;
307
308 rp = view->dev;
309 rq->view = view;
310 raw3270_get_view(view);
311 list_add_tail(&rq->list, &rp->req_queue);
312 return 0;
313}
314
315/*
316 * 3270 interrupt routine, called from the ccw_device layer
317 */
318static void
319raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
320{
321 struct raw3270 *rp;
322 struct raw3270_view *view;
323 struct raw3270_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700325 rp = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!rp)
327 return;
328 rq = (struct raw3270_request *) intparm;
329 view = rq ? rq->view : rp->view;
330
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200331 if (!IS_ERR(irb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Handle CE-DE-UE and subsequent UDE */
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200333 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200335 if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END |
336 DEV_STAT_DEV_END |
337 DEV_STAT_UNIT_EXCEP))
338 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
339 /* Handle disconnected devices */
340 if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200341 (irb->ecw[0] & SNS0_INTERVENTION_REQ)) {
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200342 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200343 if (rp->state > RAW3270_STATE_RESET)
344 __raw3270_disconnect(rp);
345 }
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200346 /* Call interrupt handler of the view */
347 if (view)
348 view->fn->intv(view, rq, irb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200350
351 if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags))
352 /* Device busy, do not start I/O */
353 return;
354
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200355 if (rq && !list_empty(&rq->list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* The request completed, remove from queue and do callback. */
357 list_del_init(&rq->list);
358 if (rq->callback)
359 rq->callback(rq, rq->callback_data);
360 /* Do put_device for get_device in raw3270_start. */
361 raw3270_put_view(view);
362 }
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Try to start each request on request queue until one is
366 * started successful.
367 */
368 while (!list_empty(&rp->req_queue)) {
369 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
370 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
371 (unsigned long) rq, 0, 0);
372 if (rq->rc == 0)
373 break;
374 /* Start failed. Remove request and do callback. */
375 list_del_init(&rq->list);
376 if (rq->callback)
377 rq->callback(rq, rq->callback_data);
378 /* Do put_device for get_device in raw3270_start. */
379 raw3270_put_view(view);
380 }
381}
382
383/*
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100384 * To determine the size of the 3270 device we need to do:
385 * 1) send a 'read partition' data stream to the device
386 * 2) wait for the attn interrupt that precedes the query reply
387 * 3) do a read modified to get the query reply
388 * To make things worse we have to cope with intervention
389 * required (3270 device switched to 'stand-by') and command
390 * rejects (old devices that can't do 'read partition').
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392struct raw3270_ua { /* Query Reply structure for Usable Area */
393 struct { /* Usable Area Query Reply Base */
394 short l; /* Length of this structured field */
395 char sfid; /* 0x81 if Query Reply */
396 char qcode; /* 0x81 if Usable Area */
397 char flags0;
398 char flags1;
399 short w; /* Width of usable area */
400 short h; /* Heigth of usavle area */
401 char units; /* 0x00:in; 0x01:mm */
402 int xr;
403 int yr;
404 char aw;
405 char ah;
406 short buffsz; /* Character buffer size, bytes */
407 char xmin;
408 char ymin;
409 char xmax;
410 char ymax;
411 } __attribute__ ((packed)) uab;
412 struct { /* Alternate Usable Area Self-Defining Parameter */
413 char l; /* Length of this Self-Defining Parm */
414 char sdpid; /* 0x02 if Alternate Usable Area */
415 char res;
416 char auaid; /* 0x01 is Id for the A U A */
417 short wauai; /* Width of AUAi */
418 short hauai; /* Height of AUAi */
419 char auaunits; /* 0x00:in, 0x01:mm */
420 int auaxr;
421 int auayr;
422 char awauai;
423 char ahauai;
424 } __attribute__ ((packed)) aua;
425} __attribute__ ((packed));
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100428raw3270_size_device_vm(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 int rc, model;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200431 struct ccw_dev_id dev_id;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100432 struct diag210 diag_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200434 ccw_device_get_id(rp->cdev, &dev_id);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100435 diag_data.vrdcdvno = dev_id.devno;
436 diag_data.vrdclen = sizeof(struct diag210);
437 rc = diag210(&diag_data);
438 model = diag_data.vrdccrmd;
439 /* Use default model 2 if the size could not be detected */
440 if (rc || model < 2 || model > 5)
441 model = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 switch (model) {
443 case 2:
444 rp->model = model;
445 rp->rows = 24;
446 rp->cols = 80;
447 break;
448 case 3:
449 rp->model = model;
450 rp->rows = 32;
451 rp->cols = 80;
452 break;
453 case 4:
454 rp->model = model;
455 rp->rows = 43;
456 rp->cols = 80;
457 break;
458 case 5:
459 rp->model = model;
460 rp->rows = 27;
461 rp->cols = 132;
462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100466static void
467raw3270_size_device(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct raw3270_ua *uap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Got a Query Reply */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200472 uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* Paranoia check. */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100474 if (rp->init_readmod.rc || rp->init_data[0] != 0x88 ||
475 uap->uab.qcode != 0x81) {
476 /* Couldn't detect size. Use default model 2. */
477 rp->model = 2;
478 rp->rows = 24;
479 rp->cols = 80;
480 return;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Copy rows/columns of default Usable Area */
483 rp->rows = uap->uab.h;
484 rp->cols = uap->uab.w;
485 /* Check for 14 bit addressing */
486 if ((uap->uab.flags0 & 0x0d) == 0x01)
487 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
488 /* Check for Alternate Usable Area */
489 if (uap->uab.l == sizeof(struct raw3270_ua) &&
490 uap->aua.sdpid == 0x02) {
491 rp->rows = uap->aua.hauai;
492 rp->cols = uap->aua.wauai;
493 }
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100494 /* Try to find a model. */
495 rp->model = 0;
496 if (rp->rows == 24 && rp->cols == 80)
497 rp->model = 2;
498 if (rp->rows == 32 && rp->cols == 80)
499 rp->model = 3;
500 if (rp->rows == 43 && rp->cols == 80)
501 rp->model = 4;
502 if (rp->rows == 27 && rp->cols == 132)
503 rp->model = 5;
504}
505
506static void
507raw3270_size_device_done(struct raw3270 *rp)
508{
509 struct raw3270_view *view;
510
511 rp->view = NULL;
512 rp->state = RAW3270_STATE_READY;
513 /* Notify views about new size */
514 list_for_each_entry(view, &rp->view_list, list)
515 if (view->fn->resize)
516 view->fn->resize(view, rp->model, rp->rows, rp->cols);
517 /* Setup processing done, now activate a view */
518 list_for_each_entry(view, &rp->view_list, list) {
519 rp->view = view;
520 if (view->fn->activate(view) == 0)
521 break;
522 rp->view = NULL;
523 }
524}
525
526static void
527raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
528{
529 struct raw3270 *rp = rq->view->dev;
530
531 raw3270_size_device(rp);
532 raw3270_size_device_done(rp);
533}
534
535static void
536raw3270_read_modified(struct raw3270 *rp)
537{
538 if (rp->state != RAW3270_STATE_W4ATTN)
539 return;
540 /* Use 'read modified' to get the result of a read partition. */
541 memset(&rp->init_readmod, 0, sizeof(rp->init_readmod));
542 memset(&rp->init_data, 0, sizeof(rp->init_data));
543 rp->init_readmod.ccw.cmd_code = TC_READMOD;
544 rp->init_readmod.ccw.flags = CCW_FLAG_SLI;
545 rp->init_readmod.ccw.count = sizeof(rp->init_data);
546 rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data);
547 rp->init_readmod.callback = raw3270_read_modified_cb;
548 rp->state = RAW3270_STATE_READMOD;
549 raw3270_start_irq(&rp->init_view, &rp->init_readmod);
550}
551
552static void
553raw3270_writesf_readpart(struct raw3270 *rp)
554{
555 static const unsigned char wbuf[] =
556 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
557
558 /* Store 'read partition' data stream to init_data */
559 memset(&rp->init_readpart, 0, sizeof(rp->init_readpart));
560 memset(&rp->init_data, 0, sizeof(rp->init_data));
561 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
562 rp->init_readpart.ccw.cmd_code = TC_WRITESF;
563 rp->init_readpart.ccw.flags = CCW_FLAG_SLI;
564 rp->init_readpart.ccw.count = sizeof(wbuf);
565 rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data);
566 rp->state = RAW3270_STATE_W4ATTN;
567 raw3270_start_irq(&rp->init_view, &rp->init_readpart);
568}
569
570/*
571 * Device reset
572 */
573static void
574raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
575{
576 struct raw3270 *rp = rq->view->dev;
577
578 if (rp->state != RAW3270_STATE_RESET)
579 return;
Martin Schwidefskydc3ac5f2013-10-14 16:48:29 +0200580 if (rq->rc) {
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100581 /* Reset command failed. */
582 rp->state = RAW3270_STATE_INIT;
Martin Schwidefsky01a7cfa2013-10-15 13:41:05 +0200583 } else if (MACHINE_IS_VM) {
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100584 raw3270_size_device_vm(rp);
585 raw3270_size_device_done(rp);
586 } else
587 raw3270_writesf_readpart(rp);
Martin Schwidefskybd1cb5d2014-03-21 15:24:27 +0100588 memset(&rp->init_reset, 0, sizeof(rp->init_reset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591static int
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100592__raw3270_reset_device(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 int rc;
595
Martin Schwidefskybd1cb5d2014-03-21 15:24:27 +0100596 /* Check if reset is already pending */
597 if (rp->init_reset.view)
598 return -EBUSY;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100599 /* Store reset data stream to init_data/init_reset */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100600 rp->init_data[0] = TW_KR;
601 rp->init_reset.ccw.cmd_code = TC_EWRITEA;
602 rp->init_reset.ccw.flags = CCW_FLAG_SLI;
603 rp->init_reset.ccw.count = 1;
604 rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data);
605 rp->init_reset.callback = raw3270_reset_device_cb;
606 rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset);
607 if (rc == 0 && rp->state == RAW3270_STATE_INIT)
608 rp->state = RAW3270_STATE_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return rc;
610}
611
612static int
613raw3270_reset_device(struct raw3270 *rp)
614{
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100615 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int rc;
617
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100618 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
619 rc = __raw3270_reset_device(rp);
620 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return rc;
622}
623
Richard Hitted3cb6f2005-10-30 15:00:10 -0800624int
625raw3270_reset(struct raw3270_view *view)
626{
627 struct raw3270 *rp;
628 int rc;
629
630 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200631 if (!rp || rp->view != view ||
632 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800633 rc = -EACCES;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100634 else if (!raw3270_state_ready(rp))
635 rc = -EBUSY;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800636 else
637 rc = raw3270_reset_device(view->dev);
638 return rc;
639}
640
Martin Schwidefsky8340ab62016-05-02 14:53:29 +0200641static void
Martin Schwidefsky7e36eff2016-05-02 15:07:00 +0200642__raw3270_disconnect(struct raw3270 *rp)
643{
644 struct raw3270_request *rq;
645 struct raw3270_view *view;
646
647 rp->state = RAW3270_STATE_INIT;
648 rp->view = &rp->init_view;
649 /* Cancel all queued requests */
650 while (!list_empty(&rp->req_queue)) {
651 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
652 view = rq->view;
653 rq->rc = -EACCES;
654 list_del_init(&rq->list);
655 if (rq->callback)
656 rq->callback(rq, rq->callback_data);
657 raw3270_put_view(view);
658 }
659 /* Start from scratch */
660 __raw3270_reset_device(rp);
661}
662
663static void
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100664raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
665 struct irb *irb)
666{
667 struct raw3270 *rp;
668
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100669 if (rq) {
670 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
671 if (irb->ecw[0] & SNS0_CMD_REJECT)
672 rq->rc = -EOPNOTSUPP;
673 else
674 rq->rc = -EIO;
675 }
676 }
677 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
678 /* Queue read modified after attention interrupt */
679 rp = view->dev;
680 raw3270_read_modified(rp);
681 }
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100682}
683
684static struct raw3270_fn raw3270_init_fn = {
685 .intv = raw3270_init_irq
686};
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/*
689 * Setup new 3270 device.
690 */
691static int
692raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
693{
694 struct list_head *l;
695 struct raw3270 *tmp;
696 int minor;
697
698 memset(rp, 0, sizeof(struct raw3270));
699 /* Copy ebcdic -> ascii translation table. */
700 memcpy(ascebc, _ascebc, 256);
701 if (tubxcorrect) {
702 /* correct brackets and circumflex */
703 ascebc['['] = 0xad;
704 ascebc[']'] = 0xbd;
705 ascebc['^'] = 0xb0;
706 }
707 rp->ascebc = ascebc;
708
709 /* Set defaults. */
710 rp->rows = 24;
711 rp->cols = 80;
712
713 INIT_LIST_HEAD(&rp->req_queue);
714 INIT_LIST_HEAD(&rp->view_list);
715
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100716 rp->init_view.dev = rp;
717 rp->init_view.fn = &raw3270_init_fn;
718 rp->view = &rp->init_view;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /*
721 * Add device to list and find the smallest unused minor
Richard Hitted3cb6f2005-10-30 15:00:10 -0800722 * number for it. Note: there is no device with minor 0,
723 * see special case for fs3270.c:fs3270_open().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800725 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Keep the list sorted. */
Richard Hitted3cb6f2005-10-30 15:00:10 -0800727 minor = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 rp->minor = -1;
729 list_for_each(l, &raw3270_devices) {
730 tmp = list_entry(l, struct raw3270, list);
731 if (tmp->minor > minor) {
732 rp->minor = minor;
733 __list_add(&rp->list, l->prev, l);
734 break;
735 }
736 minor++;
737 }
Richard Hitted3cb6f2005-10-30 15:00:10 -0800738 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 rp->minor = minor;
740 list_add_tail(&rp->list, &raw3270_devices);
741 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800742 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* No free minor number? Then give up. */
744 if (rp->minor == -1)
745 return -EUSERS;
746 rp->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700747 dev_set_drvdata(&cdev->dev, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 cdev->handler = raw3270_irq;
749 return 0;
750}
751
752#ifdef CONFIG_TN3270_CONSOLE
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100753/* Tentative definition - see below for actual definition. */
754static struct ccw_driver raw3270_ccw_driver;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Setup 3270 device configured as console.
758 */
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100759struct raw3270 __init *raw3270_setup_console(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100761 struct ccw_device *cdev;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100762 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct raw3270 *rp;
764 char *ascebc;
765 int rc;
766
Sebastian Ott1e532092014-01-27 13:28:10 +0100767 cdev = ccw_device_create_console(&raw3270_ccw_driver);
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100768 if (IS_ERR(cdev))
769 return ERR_CAST(cdev);
770
Heiko Carstens33403dc2009-06-22 12:08:05 +0200771 rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
772 ascebc = kzalloc(256, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 rc = raw3270_setup_device(cdev, rp, ascebc);
774 if (rc)
775 return ERR_PTR(rc);
776 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
Sebastian Ott1e532092014-01-27 13:28:10 +0100777
778 rc = ccw_device_enable_console(cdev);
779 if (rc) {
780 ccw_device_destroy_console(cdev);
781 return ERR_PTR(rc);
782 }
783
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100784 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
785 do {
786 __raw3270_reset_device(rp);
787 while (!raw3270_state_final(rp)) {
Sebastian Ott188561a2013-04-13 12:53:21 +0200788 ccw_device_wait_idle(rp->cdev);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100789 barrier();
790 }
791 } while (rp->state != RAW3270_STATE_READY);
792 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return rp;
794}
795
796void
797raw3270_wait_cons_dev(struct raw3270 *rp)
798{
799 unsigned long flags;
800
801 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Sebastian Ott188561a2013-04-13 12:53:21 +0200802 ccw_device_wait_idle(rp->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
804}
805
806#endif
807
808/*
809 * Create a 3270 device structure.
810 */
811static struct raw3270 *
812raw3270_create_device(struct ccw_device *cdev)
813{
814 struct raw3270 *rp;
815 char *ascebc;
816 int rc;
817
Martin Schwidefskybd1cb5d2014-03-21 15:24:27 +0100818 rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!rp)
820 return ERR_PTR(-ENOMEM);
821 ascebc = kmalloc(256, GFP_KERNEL);
822 if (!ascebc) {
823 kfree(rp);
824 return ERR_PTR(-ENOMEM);
825 }
826 rc = raw3270_setup_device(cdev, rp, ascebc);
827 if (rc) {
828 kfree(rp->ascebc);
829 kfree(rp);
830 rp = ERR_PTR(rc);
831 }
832 /* Get reference to ccw_device structure. */
833 get_device(&cdev->dev);
834 return rp;
835}
836
837/*
838 * Activate a view.
839 */
840int
841raw3270_activate_view(struct raw3270_view *view)
842{
843 struct raw3270 *rp;
844 struct raw3270_view *oldview, *nv;
845 unsigned long flags;
846 int rc;
847
848 rp = view->dev;
849 if (!rp)
850 return -ENODEV;
851 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
852 if (rp->view == view)
853 rc = 0;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100854 else if (!raw3270_state_ready(rp))
855 rc = -EBUSY;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200856 else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
857 rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 else {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200859 oldview = NULL;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100860 if (rp->view && rp->view->fn->deactivate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 oldview = rp->view;
862 oldview->fn->deactivate(oldview);
863 }
864 rp->view = view;
865 rc = view->fn->activate(view);
866 if (rc) {
867 /* Didn't work. Try to reactivate the old view. */
868 rp->view = oldview;
869 if (!oldview || oldview->fn->activate(oldview) != 0) {
870 /* Didn't work as well. Try any other view. */
871 list_for_each_entry(nv, &rp->view_list, list)
872 if (nv != view && nv != oldview) {
873 rp->view = nv;
874 if (nv->fn->activate(nv) == 0)
875 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200876 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
879 }
880 }
881 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
882 return rc;
883}
884
885/*
886 * Deactivate current view.
887 */
888void
889raw3270_deactivate_view(struct raw3270_view *view)
890{
891 unsigned long flags;
892 struct raw3270 *rp;
893
894 rp = view->dev;
895 if (!rp)
896 return;
897 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
898 if (rp->view == view) {
899 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200900 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* Move deactivated view to end of list. */
902 list_del_init(&view->list);
903 list_add_tail(&view->list, &rp->view_list);
904 /* Try to activate another view. */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100905 if (raw3270_state_ready(rp) &&
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200906 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800907 list_for_each_entry(view, &rp->view_list, list) {
908 rp->view = view;
909 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200911 rp->view = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 }
915 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
916}
917
918/*
919 * Add view to device with minor "minor".
920 */
921int
922raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
923{
924 unsigned long flags;
925 struct raw3270 *rp;
926 int rc;
927
Richard Hitted3cb6f2005-10-30 15:00:10 -0800928 if (minor <= 0)
929 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800930 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 rc = -ENODEV;
932 list_for_each_entry(rp, &raw3270_devices, list) {
933 if (rp->minor != minor)
934 continue;
935 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100936 atomic_set(&view->ref_count, 2);
937 view->dev = rp;
938 view->fn = fn;
939 view->model = rp->model;
940 view->rows = rp->rows;
941 view->cols = rp->cols;
942 view->ascebc = rp->ascebc;
943 spin_lock_init(&view->lock);
944 list_add(&view->list, &rp->view_list);
945 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
947 break;
948 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800949 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return rc;
951}
952
953/*
954 * Find specific view of device with minor "minor".
955 */
956struct raw3270_view *
957raw3270_find_view(struct raw3270_fn *fn, int minor)
958{
959 struct raw3270 *rp;
960 struct raw3270_view *view, *tmp;
961 unsigned long flags;
962
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800963 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 view = ERR_PTR(-ENODEV);
965 list_for_each_entry(rp, &raw3270_devices, list) {
966 if (rp->minor != minor)
967 continue;
968 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100969 list_for_each_entry(tmp, &rp->view_list, list) {
970 if (tmp->fn == fn) {
971 raw3270_get_view(tmp);
972 view = tmp;
973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 }
976 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
977 break;
978 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800979 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return view;
981}
982
983/*
984 * Remove view from device and free view structure via call to view->fn->free.
985 */
986void
987raw3270_del_view(struct raw3270_view *view)
988{
989 unsigned long flags;
990 struct raw3270 *rp;
991 struct raw3270_view *nv;
992
993 rp = view->dev;
994 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
995 if (rp->view == view) {
996 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200997 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 list_del_init(&view->list);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001000 if (!rp->view && raw3270_state_ready(rp) &&
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001001 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Try to activate another view. */
1003 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001004 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 rp->view = nv;
1006 break;
1007 }
1008 }
1009 }
1010 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1011 /* Wait for reference counter to drop to zero. */
1012 atomic_dec(&view->ref_count);
1013 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1014 if (view->fn->free)
1015 view->fn->free(view);
1016}
1017
1018/*
1019 * Remove a 3270 device structure.
1020 */
1021static void
1022raw3270_delete_device(struct raw3270 *rp)
1023{
1024 struct ccw_device *cdev;
1025
1026 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001027 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001029 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /* Disconnect from ccw_device. */
1032 cdev = rp->cdev;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001033 rp->cdev = NULL;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001034 dev_set_drvdata(&cdev->dev, NULL);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001035 cdev->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* Put ccw_device structure. */
1038 put_device(&cdev->dev);
1039
1040 /* Now free raw3270 structure. */
1041 kfree(rp->ascebc);
1042 kfree(rp);
1043}
1044
1045static int
1046raw3270_probe (struct ccw_device *cdev)
1047{
1048 return 0;
1049}
1050
1051/*
1052 * Additional attributes for a 3270 device
1053 */
1054static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001055raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001058 ((struct raw3270 *) dev_get_drvdata(dev))->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001060static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001063raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001066 ((struct raw3270 *) dev_get_drvdata(dev))->rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001068static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001071raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001074 ((struct raw3270 *) dev_get_drvdata(dev))->cols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001076static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078static struct attribute * raw3270_attrs[] = {
1079 &dev_attr_model.attr,
1080 &dev_attr_rows.attr,
1081 &dev_attr_columns.attr,
1082 NULL,
1083};
1084
1085static struct attribute_group raw3270_attr_group = {
1086 .attrs = raw3270_attrs,
1087};
1088
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001089static int raw3270_create_attributes(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001091 return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094/*
1095 * Notifier for device addition/removal
1096 */
Denis Chengc11ca972008-01-26 14:11:13 +01001097static LIST_HEAD(raw3270_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001099int raw3270_register_notifier(struct raw3270_notifier *notifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct raw3270 *rp;
1102
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001103 mutex_lock(&raw3270_mutex);
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001104 list_add_tail(&notifier->list, &raw3270_notifier);
1105 list_for_each_entry(rp, &raw3270_devices, list)
1106 notifier->create(rp->minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001107 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109}
1110
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001111void raw3270_unregister_notifier(struct raw3270_notifier *notifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001113 struct raw3270 *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001115 mutex_lock(&raw3270_mutex);
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001116 list_for_each_entry(rp, &raw3270_devices, list)
1117 notifier->destroy(rp->minor);
1118 list_del(&notifier->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001119 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122/*
1123 * Set 3270 device online.
1124 */
1125static int
1126raw3270_set_online (struct ccw_device *cdev)
1127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct raw3270_notifier *np;
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001129 struct raw3270 *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int rc;
1131
1132 rp = raw3270_create_device(cdev);
1133 if (IS_ERR(rp))
1134 return PTR_ERR(rp);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001135 rc = raw3270_create_attributes(rp);
1136 if (rc)
1137 goto failure;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001138 raw3270_reset_device(rp);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001139 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 list_for_each_entry(np, &raw3270_notifier, list)
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001141 np->create(rp->minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001142 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001144
1145failure:
1146 raw3270_delete_device(rp);
1147 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150/*
1151 * Remove 3270 device structure.
1152 */
1153static void
1154raw3270_remove (struct ccw_device *cdev)
1155{
1156 unsigned long flags;
1157 struct raw3270 *rp;
1158 struct raw3270_view *v;
1159 struct raw3270_notifier *np;
1160
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001161 rp = dev_get_drvdata(&cdev->dev);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001162 /*
1163 * _remove is the opposite of _probe; it's probe that
1164 * should set up rp. raw3270_remove gets entered for
1165 * devices even if they haven't been varied online.
1166 * Thus, rp may validly be NULL here.
1167 */
1168 if (rp == NULL)
1169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1172
1173 /* Deactivate current view and remove all views. */
1174 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1175 if (rp->view) {
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001176 if (rp->view->fn->deactivate)
1177 rp->view->fn->deactivate(rp->view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001178 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 while (!list_empty(&rp->view_list)) {
1181 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1182 if (v->fn->release)
1183 v->fn->release(v);
1184 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1185 raw3270_del_view(v);
1186 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1187 }
1188 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1189
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001190 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 list_for_each_entry(np, &raw3270_notifier, list)
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001192 np->destroy(rp->minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001193 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 /* Reset 3270 device. */
1196 raw3270_reset_device(rp);
1197 /* And finally remove it. */
1198 raw3270_delete_device(rp);
1199}
1200
1201/*
1202 * Set 3270 device offline.
1203 */
1204static int
1205raw3270_set_offline (struct ccw_device *cdev)
1206{
1207 struct raw3270 *rp;
1208
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001209 rp = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1211 return -EBUSY;
1212 raw3270_remove(cdev);
1213 return 0;
1214}
1215
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001216static int raw3270_pm_stop(struct ccw_device *cdev)
1217{
1218 struct raw3270 *rp;
1219 struct raw3270_view *view;
1220 unsigned long flags;
1221
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001222 rp = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001223 if (!rp)
1224 return 0;
1225 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001226 if (rp->view && rp->view->fn->deactivate)
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001227 rp->view->fn->deactivate(rp->view);
1228 if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
1229 /*
1230 * Release tty and fullscreen for all non-console
1231 * devices.
1232 */
1233 list_for_each_entry(view, &rp->view_list, list) {
1234 if (view->fn->release)
1235 view->fn->release(view);
1236 }
1237 }
1238 set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1239 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1240 return 0;
1241}
1242
1243static int raw3270_pm_start(struct ccw_device *cdev)
1244{
1245 struct raw3270 *rp;
1246 unsigned long flags;
1247
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001248 rp = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001249 if (!rp)
1250 return 0;
1251 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1252 clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001253 if (rp->view && rp->view->fn->activate)
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001254 rp->view->fn->activate(rp->view);
1255 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1256 return 0;
1257}
1258
1259void raw3270_pm_unfreeze(struct raw3270_view *view)
1260{
Sebastian Ott34483ca2009-10-06 10:33:56 +02001261#ifdef CONFIG_TN3270_CONSOLE
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001262 struct raw3270 *rp;
1263
1264 rp = view->dev;
1265 if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001266 ccw_device_force_console(rp->cdev);
Sebastian Ott34483ca2009-10-06 10:33:56 +02001267#endif
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static struct ccw_device_id raw3270_id[] = {
1271 { CCW_DEVICE(0x3270, 0) },
1272 { CCW_DEVICE(0x3271, 0) },
1273 { CCW_DEVICE(0x3272, 0) },
1274 { CCW_DEVICE(0x3273, 0) },
1275 { CCW_DEVICE(0x3274, 0) },
1276 { CCW_DEVICE(0x3275, 0) },
1277 { CCW_DEVICE(0x3276, 0) },
1278 { CCW_DEVICE(0x3277, 0) },
1279 { CCW_DEVICE(0x3278, 0) },
1280 { CCW_DEVICE(0x3279, 0) },
1281 { CCW_DEVICE(0x3174, 0) },
1282 { /* end of list */ },
1283};
1284
1285static struct ccw_driver raw3270_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +01001286 .driver = {
1287 .name = "3270",
1288 .owner = THIS_MODULE,
1289 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 .ids = raw3270_id,
1291 .probe = &raw3270_probe,
1292 .remove = &raw3270_remove,
1293 .set_online = &raw3270_set_online,
1294 .set_offline = &raw3270_set_offline,
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001295 .freeze = &raw3270_pm_stop,
1296 .thaw = &raw3270_pm_start,
1297 .restore = &raw3270_pm_start,
Heiko Carstens420f42e2013-01-02 15:18:18 +01001298 .int_class = IRQIO_C70,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299};
1300
1301static int
1302raw3270_init(void)
1303{
1304 struct raw3270 *rp;
1305 int rc;
1306
1307 if (raw3270_registered)
1308 return 0;
1309 raw3270_registered = 1;
1310 rc = ccw_driver_register(&raw3270_ccw_driver);
1311 if (rc == 0) {
1312 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001313 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001314 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 list_for_each_entry(rp, &raw3270_devices, list) {
1316 get_device(&rp->cdev->dev);
1317 raw3270_create_attributes(rp);
1318 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001319 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 return rc;
1322}
1323
1324static void
1325raw3270_exit(void)
1326{
1327 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001328 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331MODULE_LICENSE("GPL");
1332
1333module_init(raw3270_init);
1334module_exit(raw3270_exit);
1335
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001336EXPORT_SYMBOL(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337EXPORT_SYMBOL(raw3270_request_alloc);
1338EXPORT_SYMBOL(raw3270_request_free);
1339EXPORT_SYMBOL(raw3270_request_reset);
1340EXPORT_SYMBOL(raw3270_request_set_cmd);
1341EXPORT_SYMBOL(raw3270_request_add_data);
1342EXPORT_SYMBOL(raw3270_request_set_data);
1343EXPORT_SYMBOL(raw3270_request_set_idal);
1344EXPORT_SYMBOL(raw3270_buffer_address);
1345EXPORT_SYMBOL(raw3270_add_view);
1346EXPORT_SYMBOL(raw3270_del_view);
1347EXPORT_SYMBOL(raw3270_find_view);
1348EXPORT_SYMBOL(raw3270_activate_view);
1349EXPORT_SYMBOL(raw3270_deactivate_view);
1350EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001351EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001353EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354EXPORT_SYMBOL(raw3270_register_notifier);
1355EXPORT_SYMBOL(raw3270_unregister_notifier);
1356EXPORT_SYMBOL(raw3270_wait_queue);