blob: 041c65bc7bb17cf0b483ef40511b33ac4b61b900 [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
93/*
94 * Encode array for 12 bit 3270 addresses.
95 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +010096static unsigned char raw3270_ebcgraf[64] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
98 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
99 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
100 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
101 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
102 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
103 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
104 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
105};
106
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100107static inline int raw3270_state_ready(struct raw3270 *rp)
108{
109 return rp->state == RAW3270_STATE_READY;
110}
111
112static inline int raw3270_state_final(struct raw3270 *rp)
113{
114 return rp->state == RAW3270_STATE_INIT ||
115 rp->state == RAW3270_STATE_READY;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118void
119raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
120{
121 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
122 cp[0] = (addr >> 8) & 0x3f;
123 cp[1] = addr & 0xff;
124 } else {
125 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
126 cp[1] = raw3270_ebcgraf[addr & 0x3f];
127 }
128}
129
130/*
131 * Allocate a new 3270 ccw request
132 */
133struct raw3270_request *
134raw3270_request_alloc(size_t size)
135{
136 struct raw3270_request *rq;
137
138 /* Allocate request structure */
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800139 rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!rq)
141 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* alloc output buffer. */
144 if (size > 0) {
145 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
146 if (!rq->buffer) {
147 kfree(rq);
148 return ERR_PTR(-ENOMEM);
149 }
150 }
151 rq->size = size;
152 INIT_LIST_HEAD(&rq->list);
153
154 /*
155 * Setup ccw.
156 */
157 rq->ccw.cda = __pa(rq->buffer);
158 rq->ccw.flags = CCW_FLAG_SLI;
159
160 return rq;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * Free 3270 ccw request
165 */
166void
167raw3270_request_free (struct raw3270_request *rq)
168{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800169 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 kfree(rq);
171}
172
173/*
174 * Reset request to initial state.
175 */
176void
177raw3270_request_reset(struct raw3270_request *rq)
178{
179 BUG_ON(!list_empty(&rq->list));
180 rq->ccw.cmd_code = 0;
181 rq->ccw.count = 0;
182 rq->ccw.cda = __pa(rq->buffer);
183 rq->ccw.flags = CCW_FLAG_SLI;
184 rq->rescnt = 0;
185 rq->rc = 0;
186}
187
188/*
189 * Set command code to ccw of a request.
190 */
191void
192raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
193{
194 rq->ccw.cmd_code = cmd;
195}
196
197/*
198 * Add data fragment to output buffer.
199 */
200int
201raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
202{
203 if (size + rq->ccw.count > rq->size)
204 return -E2BIG;
205 memcpy(rq->buffer + rq->ccw.count, data, size);
206 rq->ccw.count += size;
207 return 0;
208}
209
210/*
211 * Set address/length pair to ccw of a request.
212 */
213void
214raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
215{
216 rq->ccw.cda = __pa(data);
217 rq->ccw.count = size;
218}
219
220/*
221 * Set idal buffer to ccw of a request.
222 */
223void
224raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
225{
226 rq->ccw.cda = __pa(ib->data);
227 rq->ccw.count = ib->size;
228 rq->ccw.flags |= CCW_FLAG_IDA;
229}
230
231/*
232 * Stop running ccw.
233 */
234static int
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100235__raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int retries;
238 int rc;
239
240 if (raw3270_request_final(rq))
241 return 0;
242 /* Check if interrupt has already been processed */
243 for (retries = 0; retries < 5; retries++) {
244 if (retries < 2)
245 rc = ccw_device_halt(rp->cdev, (long) rq);
246 else
247 rc = ccw_device_clear(rp->cdev, (long) rq);
248 if (rc == 0)
249 break; /* termination successful */
250 }
251 return rc;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
255 * Add the request to the request queue, try to start it if the
256 * 3270 device is idle. Return without waiting for end of i/o.
257 */
258static int
259__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
260 struct raw3270_request *rq)
261{
262 rq->view = view;
263 raw3270_get_view(view);
264 if (list_empty(&rp->req_queue) &&
265 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
266 /* No other requests are on the queue. Start this one. */
267 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
268 (unsigned long) rq, 0, 0);
269 if (rq->rc) {
270 raw3270_put_view(view);
271 return rq->rc;
272 }
273 }
274 list_add_tail(&rq->list, &rp->req_queue);
275 return 0;
276}
277
278int
279raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
280{
281 unsigned long flags;
282 struct raw3270 *rp;
283 int rc;
284
285 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
286 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200287 if (!rp || rp->view != view ||
288 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 rc = -EACCES;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100290 else if (!raw3270_state_ready(rp))
291 rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else
293 rc = __raw3270_start(rp, view, rq);
294 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
295 return rc;
296}
297
298int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800299raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
300{
301 struct raw3270 *rp;
302 int rc;
303
304 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200305 if (!rp || rp->view != view ||
306 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800307 rc = -EACCES;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100308 else if (!raw3270_state_ready(rp))
309 rc = -EBUSY;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800310 else
311 rc = __raw3270_start(rp, view, rq);
312 return rc;
313}
314
315int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
317{
318 struct raw3270 *rp;
319
320 rp = view->dev;
321 rq->view = view;
322 raw3270_get_view(view);
323 list_add_tail(&rq->list, &rp->req_queue);
324 return 0;
325}
326
327/*
328 * 3270 interrupt routine, called from the ccw_device layer
329 */
330static void
331raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
332{
333 struct raw3270 *rp;
334 struct raw3270_view *view;
335 struct raw3270_request *rq;
336 int rc;
337
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700338 rp = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!rp)
340 return;
341 rq = (struct raw3270_request *) intparm;
342 view = rq ? rq->view : rp->view;
343
344 if (IS_ERR(irb))
345 rc = RAW3270_IO_RETRY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200346 else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 rq->rc = -EIO;
348 rc = RAW3270_IO_DONE;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200349 } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
350 DEV_STAT_UNIT_EXCEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* Handle CE-DE-UE and subsequent UDE */
352 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
353 rc = RAW3270_IO_BUSY;
354 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
355 /* Wait for UDE if busy flag is set. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200356 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
358 /* Got it, now retry. */
359 rc = RAW3270_IO_RETRY;
360 } else
361 rc = RAW3270_IO_BUSY;
362 } else if (view)
363 rc = view->fn->intv(view, rq, irb);
364 else
365 rc = RAW3270_IO_DONE;
366
367 switch (rc) {
368 case RAW3270_IO_DONE:
369 break;
370 case RAW3270_IO_BUSY:
371 /*
372 * Intervention required by the operator. We have to wait
373 * for unsolicited device end.
374 */
375 return;
376 case RAW3270_IO_RETRY:
377 if (!rq)
378 break;
379 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
380 (unsigned long) rq, 0, 0);
381 if (rq->rc == 0)
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800382 return; /* Successfully restarted. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384 case RAW3270_IO_STOP:
385 if (!rq)
386 break;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100387 __raw3270_halt_io(rp, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 rq->rc = -EIO;
389 break;
390 default:
391 BUG();
392 }
393 if (rq) {
394 BUG_ON(list_empty(&rq->list));
395 /* The request completed, remove from queue and do callback. */
396 list_del_init(&rq->list);
397 if (rq->callback)
398 rq->callback(rq, rq->callback_data);
399 /* Do put_device for get_device in raw3270_start. */
400 raw3270_put_view(view);
401 }
402 /*
403 * Try to start each request on request queue until one is
404 * started successful.
405 */
406 while (!list_empty(&rp->req_queue)) {
407 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
408 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
409 (unsigned long) rq, 0, 0);
410 if (rq->rc == 0)
411 break;
412 /* Start failed. Remove request and do callback. */
413 list_del_init(&rq->list);
414 if (rq->callback)
415 rq->callback(rq, rq->callback_data);
416 /* Do put_device for get_device in raw3270_start. */
417 raw3270_put_view(view);
418 }
419}
420
421/*
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100422 * To determine the size of the 3270 device we need to do:
423 * 1) send a 'read partition' data stream to the device
424 * 2) wait for the attn interrupt that precedes the query reply
425 * 3) do a read modified to get the query reply
426 * To make things worse we have to cope with intervention
427 * required (3270 device switched to 'stand-by') and command
428 * rejects (old devices that can't do 'read partition').
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430struct raw3270_ua { /* Query Reply structure for Usable Area */
431 struct { /* Usable Area Query Reply Base */
432 short l; /* Length of this structured field */
433 char sfid; /* 0x81 if Query Reply */
434 char qcode; /* 0x81 if Usable Area */
435 char flags0;
436 char flags1;
437 short w; /* Width of usable area */
438 short h; /* Heigth of usavle area */
439 char units; /* 0x00:in; 0x01:mm */
440 int xr;
441 int yr;
442 char aw;
443 char ah;
444 short buffsz; /* Character buffer size, bytes */
445 char xmin;
446 char ymin;
447 char xmax;
448 char ymax;
449 } __attribute__ ((packed)) uab;
450 struct { /* Alternate Usable Area Self-Defining Parameter */
451 char l; /* Length of this Self-Defining Parm */
452 char sdpid; /* 0x02 if Alternate Usable Area */
453 char res;
454 char auaid; /* 0x01 is Id for the A U A */
455 short wauai; /* Width of AUAi */
456 short hauai; /* Height of AUAi */
457 char auaunits; /* 0x00:in, 0x01:mm */
458 int auaxr;
459 int auayr;
460 char awauai;
461 char ahauai;
462 } __attribute__ ((packed)) aua;
463} __attribute__ ((packed));
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static void
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100466raw3270_size_device_vm(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int rc, model;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200469 struct ccw_dev_id dev_id;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100470 struct diag210 diag_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200472 ccw_device_get_id(rp->cdev, &dev_id);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100473 diag_data.vrdcdvno = dev_id.devno;
474 diag_data.vrdclen = sizeof(struct diag210);
475 rc = diag210(&diag_data);
476 model = diag_data.vrdccrmd;
477 /* Use default model 2 if the size could not be detected */
478 if (rc || model < 2 || model > 5)
479 model = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 switch (model) {
481 case 2:
482 rp->model = model;
483 rp->rows = 24;
484 rp->cols = 80;
485 break;
486 case 3:
487 rp->model = model;
488 rp->rows = 32;
489 rp->cols = 80;
490 break;
491 case 4:
492 rp->model = model;
493 rp->rows = 43;
494 rp->cols = 80;
495 break;
496 case 5:
497 rp->model = model;
498 rp->rows = 27;
499 rp->cols = 132;
500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100504static void
505raw3270_size_device(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct raw3270_ua *uap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Got a Query Reply */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200510 uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Paranoia check. */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100512 if (rp->init_readmod.rc || rp->init_data[0] != 0x88 ||
513 uap->uab.qcode != 0x81) {
514 /* Couldn't detect size. Use default model 2. */
515 rp->model = 2;
516 rp->rows = 24;
517 rp->cols = 80;
518 return;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* Copy rows/columns of default Usable Area */
521 rp->rows = uap->uab.h;
522 rp->cols = uap->uab.w;
523 /* Check for 14 bit addressing */
524 if ((uap->uab.flags0 & 0x0d) == 0x01)
525 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
526 /* Check for Alternate Usable Area */
527 if (uap->uab.l == sizeof(struct raw3270_ua) &&
528 uap->aua.sdpid == 0x02) {
529 rp->rows = uap->aua.hauai;
530 rp->cols = uap->aua.wauai;
531 }
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100532 /* Try to find a model. */
533 rp->model = 0;
534 if (rp->rows == 24 && rp->cols == 80)
535 rp->model = 2;
536 if (rp->rows == 32 && rp->cols == 80)
537 rp->model = 3;
538 if (rp->rows == 43 && rp->cols == 80)
539 rp->model = 4;
540 if (rp->rows == 27 && rp->cols == 132)
541 rp->model = 5;
542}
543
544static void
545raw3270_size_device_done(struct raw3270 *rp)
546{
547 struct raw3270_view *view;
548
549 rp->view = NULL;
550 rp->state = RAW3270_STATE_READY;
551 /* Notify views about new size */
552 list_for_each_entry(view, &rp->view_list, list)
553 if (view->fn->resize)
554 view->fn->resize(view, rp->model, rp->rows, rp->cols);
555 /* Setup processing done, now activate a view */
556 list_for_each_entry(view, &rp->view_list, list) {
557 rp->view = view;
558 if (view->fn->activate(view) == 0)
559 break;
560 rp->view = NULL;
561 }
562}
563
564static void
565raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
566{
567 struct raw3270 *rp = rq->view->dev;
568
569 raw3270_size_device(rp);
570 raw3270_size_device_done(rp);
571}
572
573static void
574raw3270_read_modified(struct raw3270 *rp)
575{
576 if (rp->state != RAW3270_STATE_W4ATTN)
577 return;
578 /* Use 'read modified' to get the result of a read partition. */
579 memset(&rp->init_readmod, 0, sizeof(rp->init_readmod));
580 memset(&rp->init_data, 0, sizeof(rp->init_data));
581 rp->init_readmod.ccw.cmd_code = TC_READMOD;
582 rp->init_readmod.ccw.flags = CCW_FLAG_SLI;
583 rp->init_readmod.ccw.count = sizeof(rp->init_data);
584 rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data);
585 rp->init_readmod.callback = raw3270_read_modified_cb;
586 rp->state = RAW3270_STATE_READMOD;
587 raw3270_start_irq(&rp->init_view, &rp->init_readmod);
588}
589
590static void
591raw3270_writesf_readpart(struct raw3270 *rp)
592{
593 static const unsigned char wbuf[] =
594 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
595
596 /* Store 'read partition' data stream to init_data */
597 memset(&rp->init_readpart, 0, sizeof(rp->init_readpart));
598 memset(&rp->init_data, 0, sizeof(rp->init_data));
599 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
600 rp->init_readpart.ccw.cmd_code = TC_WRITESF;
601 rp->init_readpart.ccw.flags = CCW_FLAG_SLI;
602 rp->init_readpart.ccw.count = sizeof(wbuf);
603 rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data);
604 rp->state = RAW3270_STATE_W4ATTN;
605 raw3270_start_irq(&rp->init_view, &rp->init_readpart);
606}
607
608/*
609 * Device reset
610 */
611static void
612raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
613{
614 struct raw3270 *rp = rq->view->dev;
615
616 if (rp->state != RAW3270_STATE_RESET)
617 return;
Martin Schwidefskydc3ac5f2013-10-14 16:48:29 +0200618 if (rq->rc) {
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100619 /* Reset command failed. */
620 rp->state = RAW3270_STATE_INIT;
Martin Schwidefsky01a7cfa2013-10-15 13:41:05 +0200621 } else if (MACHINE_IS_VM) {
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100622 raw3270_size_device_vm(rp);
623 raw3270_size_device_done(rp);
624 } else
625 raw3270_writesf_readpart(rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static int
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100629__raw3270_reset_device(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 int rc;
632
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100633 /* Store reset data stream to init_data/init_reset */
634 memset(&rp->init_reset, 0, sizeof(rp->init_reset));
635 memset(&rp->init_data, 0, sizeof(rp->init_data));
636 rp->init_data[0] = TW_KR;
637 rp->init_reset.ccw.cmd_code = TC_EWRITEA;
638 rp->init_reset.ccw.flags = CCW_FLAG_SLI;
639 rp->init_reset.ccw.count = 1;
640 rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data);
641 rp->init_reset.callback = raw3270_reset_device_cb;
642 rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset);
643 if (rc == 0 && rp->state == RAW3270_STATE_INIT)
644 rp->state = RAW3270_STATE_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return rc;
646}
647
648static int
649raw3270_reset_device(struct raw3270 *rp)
650{
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100651 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int rc;
653
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100654 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
655 rc = __raw3270_reset_device(rp);
656 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return rc;
658}
659
Richard Hitted3cb6f2005-10-30 15:00:10 -0800660int
661raw3270_reset(struct raw3270_view *view)
662{
663 struct raw3270 *rp;
664 int rc;
665
666 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200667 if (!rp || rp->view != view ||
668 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800669 rc = -EACCES;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100670 else if (!raw3270_state_ready(rp))
671 rc = -EBUSY;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800672 else
673 rc = raw3270_reset_device(view->dev);
674 return rc;
675}
676
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100677static int
678raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
679 struct irb *irb)
680{
681 struct raw3270 *rp;
682
683 /*
684 * Unit-Check Processing:
685 * Expect Command Reject or Intervention Required.
686 */
687 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
688 /* Request finished abnormally. */
689 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
690 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
691 return RAW3270_IO_BUSY;
692 }
693 }
694 if (rq) {
695 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
696 if (irb->ecw[0] & SNS0_CMD_REJECT)
697 rq->rc = -EOPNOTSUPP;
698 else
699 rq->rc = -EIO;
700 }
701 }
702 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
703 /* Queue read modified after attention interrupt */
704 rp = view->dev;
705 raw3270_read_modified(rp);
706 }
707 return RAW3270_IO_DONE;
708}
709
710static struct raw3270_fn raw3270_init_fn = {
711 .intv = raw3270_init_irq
712};
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/*
715 * Setup new 3270 device.
716 */
717static int
718raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
719{
720 struct list_head *l;
721 struct raw3270 *tmp;
722 int minor;
723
724 memset(rp, 0, sizeof(struct raw3270));
725 /* Copy ebcdic -> ascii translation table. */
726 memcpy(ascebc, _ascebc, 256);
727 if (tubxcorrect) {
728 /* correct brackets and circumflex */
729 ascebc['['] = 0xad;
730 ascebc[']'] = 0xbd;
731 ascebc['^'] = 0xb0;
732 }
733 rp->ascebc = ascebc;
734
735 /* Set defaults. */
736 rp->rows = 24;
737 rp->cols = 80;
738
739 INIT_LIST_HEAD(&rp->req_queue);
740 INIT_LIST_HEAD(&rp->view_list);
741
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100742 rp->init_view.dev = rp;
743 rp->init_view.fn = &raw3270_init_fn;
744 rp->view = &rp->init_view;
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * Add device to list and find the smallest unused minor
Richard Hitted3cb6f2005-10-30 15:00:10 -0800748 * number for it. Note: there is no device with minor 0,
749 * see special case for fs3270.c:fs3270_open().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800751 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Keep the list sorted. */
Richard Hitted3cb6f2005-10-30 15:00:10 -0800753 minor = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 rp->minor = -1;
755 list_for_each(l, &raw3270_devices) {
756 tmp = list_entry(l, struct raw3270, list);
757 if (tmp->minor > minor) {
758 rp->minor = minor;
759 __list_add(&rp->list, l->prev, l);
760 break;
761 }
762 minor++;
763 }
Richard Hitted3cb6f2005-10-30 15:00:10 -0800764 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 rp->minor = minor;
766 list_add_tail(&rp->list, &raw3270_devices);
767 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800768 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* No free minor number? Then give up. */
770 if (rp->minor == -1)
771 return -EUSERS;
772 rp->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700773 dev_set_drvdata(&cdev->dev, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 cdev->handler = raw3270_irq;
775 return 0;
776}
777
778#ifdef CONFIG_TN3270_CONSOLE
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100779/* Tentative definition - see below for actual definition. */
780static struct ccw_driver raw3270_ccw_driver;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/*
783 * Setup 3270 device configured as console.
784 */
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100785struct raw3270 __init *raw3270_setup_console(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100787 struct ccw_device *cdev;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100788 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct raw3270 *rp;
790 char *ascebc;
791 int rc;
792
Sebastian Ott1e532092014-01-27 13:28:10 +0100793 cdev = ccw_device_create_console(&raw3270_ccw_driver);
Sebastian Ott2253e8d2014-01-27 13:26:10 +0100794 if (IS_ERR(cdev))
795 return ERR_CAST(cdev);
796
Heiko Carstens33403dc2009-06-22 12:08:05 +0200797 rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
798 ascebc = kzalloc(256, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 rc = raw3270_setup_device(cdev, rp, ascebc);
800 if (rc)
801 return ERR_PTR(rc);
802 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
Sebastian Ott1e532092014-01-27 13:28:10 +0100803
804 rc = ccw_device_enable_console(cdev);
805 if (rc) {
806 ccw_device_destroy_console(cdev);
807 return ERR_PTR(rc);
808 }
809
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100810 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
811 do {
812 __raw3270_reset_device(rp);
813 while (!raw3270_state_final(rp)) {
Sebastian Ott188561a2013-04-13 12:53:21 +0200814 ccw_device_wait_idle(rp->cdev);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100815 barrier();
816 }
817 } while (rp->state != RAW3270_STATE_READY);
818 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return rp;
820}
821
822void
823raw3270_wait_cons_dev(struct raw3270 *rp)
824{
825 unsigned long flags;
826
827 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Sebastian Ott188561a2013-04-13 12:53:21 +0200828 ccw_device_wait_idle(rp->cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
830}
831
832#endif
833
834/*
835 * Create a 3270 device structure.
836 */
837static struct raw3270 *
838raw3270_create_device(struct ccw_device *cdev)
839{
840 struct raw3270 *rp;
841 char *ascebc;
842 int rc;
843
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200844 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!rp)
846 return ERR_PTR(-ENOMEM);
847 ascebc = kmalloc(256, GFP_KERNEL);
848 if (!ascebc) {
849 kfree(rp);
850 return ERR_PTR(-ENOMEM);
851 }
852 rc = raw3270_setup_device(cdev, rp, ascebc);
853 if (rc) {
854 kfree(rp->ascebc);
855 kfree(rp);
856 rp = ERR_PTR(rc);
857 }
858 /* Get reference to ccw_device structure. */
859 get_device(&cdev->dev);
860 return rp;
861}
862
863/*
864 * Activate a view.
865 */
866int
867raw3270_activate_view(struct raw3270_view *view)
868{
869 struct raw3270 *rp;
870 struct raw3270_view *oldview, *nv;
871 unsigned long flags;
872 int rc;
873
874 rp = view->dev;
875 if (!rp)
876 return -ENODEV;
877 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
878 if (rp->view == view)
879 rc = 0;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100880 else if (!raw3270_state_ready(rp))
881 rc = -EBUSY;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200882 else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
883 rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 else {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200885 oldview = NULL;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100886 if (rp->view && rp->view->fn->deactivate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 oldview = rp->view;
888 oldview->fn->deactivate(oldview);
889 }
890 rp->view = view;
891 rc = view->fn->activate(view);
892 if (rc) {
893 /* Didn't work. Try to reactivate the old view. */
894 rp->view = oldview;
895 if (!oldview || oldview->fn->activate(oldview) != 0) {
896 /* Didn't work as well. Try any other view. */
897 list_for_each_entry(nv, &rp->view_list, list)
898 if (nv != view && nv != oldview) {
899 rp->view = nv;
900 if (nv->fn->activate(nv) == 0)
901 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200902 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 }
905 }
906 }
907 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
908 return rc;
909}
910
911/*
912 * Deactivate current view.
913 */
914void
915raw3270_deactivate_view(struct raw3270_view *view)
916{
917 unsigned long flags;
918 struct raw3270 *rp;
919
920 rp = view->dev;
921 if (!rp)
922 return;
923 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
924 if (rp->view == view) {
925 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200926 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* Move deactivated view to end of list. */
928 list_del_init(&view->list);
929 list_add_tail(&view->list, &rp->view_list);
930 /* Try to activate another view. */
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100931 if (raw3270_state_ready(rp) &&
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200932 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800933 list_for_each_entry(view, &rp->view_list, list) {
934 rp->view = view;
935 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200937 rp->view = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 }
941 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
942}
943
944/*
945 * Add view to device with minor "minor".
946 */
947int
948raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
949{
950 unsigned long flags;
951 struct raw3270 *rp;
952 int rc;
953
Richard Hitted3cb6f2005-10-30 15:00:10 -0800954 if (minor <= 0)
955 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800956 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 rc = -ENODEV;
958 list_for_each_entry(rp, &raw3270_devices, list) {
959 if (rp->minor != minor)
960 continue;
961 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100962 atomic_set(&view->ref_count, 2);
963 view->dev = rp;
964 view->fn = fn;
965 view->model = rp->model;
966 view->rows = rp->rows;
967 view->cols = rp->cols;
968 view->ascebc = rp->ascebc;
969 spin_lock_init(&view->lock);
970 list_add(&view->list, &rp->view_list);
971 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
973 break;
974 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800975 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return rc;
977}
978
979/*
980 * Find specific view of device with minor "minor".
981 */
982struct raw3270_view *
983raw3270_find_view(struct raw3270_fn *fn, int minor)
984{
985 struct raw3270 *rp;
986 struct raw3270_view *view, *tmp;
987 unsigned long flags;
988
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800989 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 view = ERR_PTR(-ENODEV);
991 list_for_each_entry(rp, &raw3270_devices, list) {
992 if (rp->minor != minor)
993 continue;
994 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +0100995 list_for_each_entry(tmp, &rp->view_list, list) {
996 if (tmp->fn == fn) {
997 raw3270_get_view(tmp);
998 view = tmp;
999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 }
1002 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1003 break;
1004 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001005 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return view;
1007}
1008
1009/*
1010 * Remove view from device and free view structure via call to view->fn->free.
1011 */
1012void
1013raw3270_del_view(struct raw3270_view *view)
1014{
1015 unsigned long flags;
1016 struct raw3270 *rp;
1017 struct raw3270_view *nv;
1018
1019 rp = view->dev;
1020 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1021 if (rp->view == view) {
1022 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001023 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 list_del_init(&view->list);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001026 if (!rp->view && raw3270_state_ready(rp) &&
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001027 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* Try to activate another view. */
1029 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001030 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 rp->view = nv;
1032 break;
1033 }
1034 }
1035 }
1036 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1037 /* Wait for reference counter to drop to zero. */
1038 atomic_dec(&view->ref_count);
1039 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1040 if (view->fn->free)
1041 view->fn->free(view);
1042}
1043
1044/*
1045 * Remove a 3270 device structure.
1046 */
1047static void
1048raw3270_delete_device(struct raw3270 *rp)
1049{
1050 struct ccw_device *cdev;
1051
1052 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001053 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001055 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /* Disconnect from ccw_device. */
1058 cdev = rp->cdev;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001059 rp->cdev = NULL;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001060 dev_set_drvdata(&cdev->dev, NULL);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001061 cdev->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* Put ccw_device structure. */
1064 put_device(&cdev->dev);
1065
1066 /* Now free raw3270 structure. */
1067 kfree(rp->ascebc);
1068 kfree(rp);
1069}
1070
1071static int
1072raw3270_probe (struct ccw_device *cdev)
1073{
1074 return 0;
1075}
1076
1077/*
1078 * Additional attributes for a 3270 device
1079 */
1080static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001081raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001084 ((struct raw3270 *) dev_get_drvdata(dev))->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001086static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001089raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001092 ((struct raw3270 *) dev_get_drvdata(dev))->rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001094static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001097raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001100 ((struct raw3270 *) dev_get_drvdata(dev))->cols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001102static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104static struct attribute * raw3270_attrs[] = {
1105 &dev_attr_model.attr,
1106 &dev_attr_rows.attr,
1107 &dev_attr_columns.attr,
1108 NULL,
1109};
1110
1111static struct attribute_group raw3270_attr_group = {
1112 .attrs = raw3270_attrs,
1113};
1114
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001115static int raw3270_create_attributes(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001117 return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/*
1121 * Notifier for device addition/removal
1122 */
Denis Chengc11ca972008-01-26 14:11:13 +01001123static LIST_HEAD(raw3270_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001125int raw3270_register_notifier(struct raw3270_notifier *notifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct raw3270 *rp;
1128
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001129 mutex_lock(&raw3270_mutex);
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001130 list_add_tail(&notifier->list, &raw3270_notifier);
1131 list_for_each_entry(rp, &raw3270_devices, list)
1132 notifier->create(rp->minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001133 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135}
1136
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001137void raw3270_unregister_notifier(struct raw3270_notifier *notifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001139 struct raw3270 *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001141 mutex_lock(&raw3270_mutex);
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001142 list_for_each_entry(rp, &raw3270_devices, list)
1143 notifier->destroy(rp->minor);
1144 list_del(&notifier->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001145 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/*
1149 * Set 3270 device online.
1150 */
1151static int
1152raw3270_set_online (struct ccw_device *cdev)
1153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct raw3270_notifier *np;
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001155 struct raw3270 *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int rc;
1157
1158 rp = raw3270_create_device(cdev);
1159 if (IS_ERR(rp))
1160 return PTR_ERR(rp);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001161 rc = raw3270_create_attributes(rp);
1162 if (rc)
1163 goto failure;
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001164 raw3270_reset_device(rp);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001165 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 list_for_each_entry(np, &raw3270_notifier, list)
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001167 np->create(rp->minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001168 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001170
1171failure:
1172 raw3270_delete_device(rp);
1173 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176/*
1177 * Remove 3270 device structure.
1178 */
1179static void
1180raw3270_remove (struct ccw_device *cdev)
1181{
1182 unsigned long flags;
1183 struct raw3270 *rp;
1184 struct raw3270_view *v;
1185 struct raw3270_notifier *np;
1186
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001187 rp = dev_get_drvdata(&cdev->dev);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001188 /*
1189 * _remove is the opposite of _probe; it's probe that
1190 * should set up rp. raw3270_remove gets entered for
1191 * devices even if they haven't been varied online.
1192 * Thus, rp may validly be NULL here.
1193 */
1194 if (rp == NULL)
1195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1198
1199 /* Deactivate current view and remove all views. */
1200 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1201 if (rp->view) {
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001202 if (rp->view->fn->deactivate)
1203 rp->view->fn->deactivate(rp->view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001204 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 while (!list_empty(&rp->view_list)) {
1207 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1208 if (v->fn->release)
1209 v->fn->release(v);
1210 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1211 raw3270_del_view(v);
1212 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1213 }
1214 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1215
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001216 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 list_for_each_entry(np, &raw3270_notifier, list)
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001218 np->destroy(rp->minor);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001219 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* Reset 3270 device. */
1222 raw3270_reset_device(rp);
1223 /* And finally remove it. */
1224 raw3270_delete_device(rp);
1225}
1226
1227/*
1228 * Set 3270 device offline.
1229 */
1230static int
1231raw3270_set_offline (struct ccw_device *cdev)
1232{
1233 struct raw3270 *rp;
1234
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001235 rp = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1237 return -EBUSY;
1238 raw3270_remove(cdev);
1239 return 0;
1240}
1241
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001242static int raw3270_pm_stop(struct ccw_device *cdev)
1243{
1244 struct raw3270 *rp;
1245 struct raw3270_view *view;
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);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001252 if (rp->view && rp->view->fn->deactivate)
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001253 rp->view->fn->deactivate(rp->view);
1254 if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
1255 /*
1256 * Release tty and fullscreen for all non-console
1257 * devices.
1258 */
1259 list_for_each_entry(view, &rp->view_list, list) {
1260 if (view->fn->release)
1261 view->fn->release(view);
1262 }
1263 }
1264 set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1265 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1266 return 0;
1267}
1268
1269static int raw3270_pm_start(struct ccw_device *cdev)
1270{
1271 struct raw3270 *rp;
1272 unsigned long flags;
1273
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001274 rp = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001275 if (!rp)
1276 return 0;
1277 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1278 clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01001279 if (rp->view && rp->view->fn->activate)
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001280 rp->view->fn->activate(rp->view);
1281 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1282 return 0;
1283}
1284
1285void raw3270_pm_unfreeze(struct raw3270_view *view)
1286{
Sebastian Ott34483ca2009-10-06 10:33:56 +02001287#ifdef CONFIG_TN3270_CONSOLE
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001288 struct raw3270 *rp;
1289
1290 rp = view->dev;
1291 if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Sebastian Ottf10ccca2013-04-13 12:56:51 +02001292 ccw_device_force_console(rp->cdev);
Sebastian Ott34483ca2009-10-06 10:33:56 +02001293#endif
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296static struct ccw_device_id raw3270_id[] = {
1297 { CCW_DEVICE(0x3270, 0) },
1298 { CCW_DEVICE(0x3271, 0) },
1299 { CCW_DEVICE(0x3272, 0) },
1300 { CCW_DEVICE(0x3273, 0) },
1301 { CCW_DEVICE(0x3274, 0) },
1302 { CCW_DEVICE(0x3275, 0) },
1303 { CCW_DEVICE(0x3276, 0) },
1304 { CCW_DEVICE(0x3277, 0) },
1305 { CCW_DEVICE(0x3278, 0) },
1306 { CCW_DEVICE(0x3279, 0) },
1307 { CCW_DEVICE(0x3174, 0) },
1308 { /* end of list */ },
1309};
1310
1311static struct ccw_driver raw3270_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +01001312 .driver = {
1313 .name = "3270",
1314 .owner = THIS_MODULE,
1315 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 .ids = raw3270_id,
1317 .probe = &raw3270_probe,
1318 .remove = &raw3270_remove,
1319 .set_online = &raw3270_set_online,
1320 .set_offline = &raw3270_set_offline,
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001321 .freeze = &raw3270_pm_stop,
1322 .thaw = &raw3270_pm_start,
1323 .restore = &raw3270_pm_start,
Heiko Carstens420f42e2013-01-02 15:18:18 +01001324 .int_class = IRQIO_C70,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325};
1326
1327static int
1328raw3270_init(void)
1329{
1330 struct raw3270 *rp;
1331 int rc;
1332
1333 if (raw3270_registered)
1334 return 0;
1335 raw3270_registered = 1;
1336 rc = ccw_driver_register(&raw3270_ccw_driver);
1337 if (rc == 0) {
1338 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001339 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001340 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 list_for_each_entry(rp, &raw3270_devices, list) {
1342 get_device(&rp->cdev->dev);
1343 raw3270_create_attributes(rp);
1344 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001345 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 return rc;
1348}
1349
1350static void
1351raw3270_exit(void)
1352{
1353 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001354 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357MODULE_LICENSE("GPL");
1358
1359module_init(raw3270_init);
1360module_exit(raw3270_exit);
1361
Martin Schwidefskyc95571e2013-01-08 15:31:11 +01001362EXPORT_SYMBOL(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363EXPORT_SYMBOL(raw3270_request_alloc);
1364EXPORT_SYMBOL(raw3270_request_free);
1365EXPORT_SYMBOL(raw3270_request_reset);
1366EXPORT_SYMBOL(raw3270_request_set_cmd);
1367EXPORT_SYMBOL(raw3270_request_add_data);
1368EXPORT_SYMBOL(raw3270_request_set_data);
1369EXPORT_SYMBOL(raw3270_request_set_idal);
1370EXPORT_SYMBOL(raw3270_buffer_address);
1371EXPORT_SYMBOL(raw3270_add_view);
1372EXPORT_SYMBOL(raw3270_del_view);
1373EXPORT_SYMBOL(raw3270_find_view);
1374EXPORT_SYMBOL(raw3270_activate_view);
1375EXPORT_SYMBOL(raw3270_deactivate_view);
1376EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001377EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001379EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380EXPORT_SYMBOL(raw3270_register_notifier);
1381EXPORT_SYMBOL(raw3270_unregister_notifier);
1382EXPORT_SYMBOL(raw3270_wait_queue);