blob: 81c151b5f0ac5b9543a55f2df00aae844a3684f6 [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/bootmem.h>
11#include <linux/module.h>
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/list.h>
16#include <linux/slab.h>
17#include <linux/types.h>
18#include <linux/wait.h>
19
20#include <asm/ccwdev.h>
21#include <asm/cio.h>
22#include <asm/ebcdic.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020023#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "raw3270.h"
26
Richard Hitted3cb6f2005-10-30 15:00:10 -080027#include <linux/major.h>
28#include <linux/kdev_t.h>
29#include <linux/device.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080030#include <linux/mutex.h>
Richard Hitted3cb6f2005-10-30 15:00:10 -080031
Heiko Carstens2b67fc42007-02-05 21:16:47 +010032static struct class *class3270;
Richard Hitted3cb6f2005-10-30 15:00:10 -080033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* The main 3270 data structure. */
35struct raw3270 {
36 struct list_head list;
37 struct ccw_device *cdev;
38 int minor;
39
40 short model, rows, cols;
41 unsigned long flags;
42
43 struct list_head req_queue; /* Request queue. */
44 struct list_head view_list; /* List of available views. */
45 struct raw3270_view *view; /* Active view. */
46
47 struct timer_list timer; /* Device timer. */
48
49 unsigned char *ascebc; /* ascii -> ebcdic table */
Cornelia Huck7f021ce2007-10-22 12:52:42 +020050 struct device *clttydev; /* 3270-class tty device ptr */
51 struct device *cltubdev; /* 3270-class tub device ptr */
Martin Schwidefsky132fab12006-06-29 14:57:39 +020052
53 struct raw3270_request init_request;
54 unsigned char init_data[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57/* raw3270->flags */
58#define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
59#define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
60#define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
61#define RAW3270_FLAGS_READY 4 /* Device is useable by views */
62#define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
Martin Schwidefsky4b214a02009-06-16 10:30:47 +020063#define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Semaphore to protect global data of raw3270 (devices, views, etc). */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080066static DEFINE_MUTEX(raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* List of 3270 devices. */
Denis Chengc11ca972008-01-26 14:11:13 +010069static LIST_HEAD(raw3270_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Flag to indicate if the driver has been registered. Some operations
73 * like waiting for the end of i/o need to be done differently as long
74 * as the kernel is still starting up (console support).
75 */
76static int raw3270_registered;
77
78/* Module parameters */
79static int tubxcorrect = 0;
80module_param(tubxcorrect, bool, 0);
81
82/*
83 * Wait queue for device init/delete, view delete.
84 */
85DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
86
87/*
88 * Encode array for 12 bit 3270 addresses.
89 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +010090static unsigned char raw3270_ebcgraf[64] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
92 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
93 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
94 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
95 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
96 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
97 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
98 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
99};
100
101void
102raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
103{
104 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
105 cp[0] = (addr >> 8) & 0x3f;
106 cp[1] = addr & 0xff;
107 } else {
108 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
109 cp[1] = raw3270_ebcgraf[addr & 0x3f];
110 }
111}
112
113/*
114 * Allocate a new 3270 ccw request
115 */
116struct raw3270_request *
117raw3270_request_alloc(size_t size)
118{
119 struct raw3270_request *rq;
120
121 /* Allocate request structure */
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800122 rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!rq)
124 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* alloc output buffer. */
127 if (size > 0) {
128 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
129 if (!rq->buffer) {
130 kfree(rq);
131 return ERR_PTR(-ENOMEM);
132 }
133 }
134 rq->size = size;
135 INIT_LIST_HEAD(&rq->list);
136
137 /*
138 * Setup ccw.
139 */
140 rq->ccw.cda = __pa(rq->buffer);
141 rq->ccw.flags = CCW_FLAG_SLI;
142
143 return rq;
144}
145
146#ifdef CONFIG_TN3270_CONSOLE
147/*
148 * Allocate a new 3270 ccw request from bootmem. Only works very
149 * early in the boot process. Only con3270.c should be using this.
150 */
Heiko Carstense62133b2007-07-27 12:29:13 +0200151struct raw3270_request __init *raw3270_request_alloc_bootmem(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct raw3270_request *rq;
154
155 rq = alloc_bootmem_low(sizeof(struct raw3270));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* alloc output buffer. */
Julia Lawall3ca1c992008-07-14 09:59:13 +0200158 if (size > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 rq->buffer = alloc_bootmem_low(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 rq->size = size;
161 INIT_LIST_HEAD(&rq->list);
162
163 /*
164 * Setup ccw.
165 */
166 rq->ccw.cda = __pa(rq->buffer);
167 rq->ccw.flags = CCW_FLAG_SLI;
168
169 return rq;
170}
171#endif
172
173/*
174 * Free 3270 ccw request
175 */
176void
177raw3270_request_free (struct raw3270_request *rq)
178{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800179 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 kfree(rq);
181}
182
183/*
184 * Reset request to initial state.
185 */
186void
187raw3270_request_reset(struct raw3270_request *rq)
188{
189 BUG_ON(!list_empty(&rq->list));
190 rq->ccw.cmd_code = 0;
191 rq->ccw.count = 0;
192 rq->ccw.cda = __pa(rq->buffer);
193 rq->ccw.flags = CCW_FLAG_SLI;
194 rq->rescnt = 0;
195 rq->rc = 0;
196}
197
198/*
199 * Set command code to ccw of a request.
200 */
201void
202raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
203{
204 rq->ccw.cmd_code = cmd;
205}
206
207/*
208 * Add data fragment to output buffer.
209 */
210int
211raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
212{
213 if (size + rq->ccw.count > rq->size)
214 return -E2BIG;
215 memcpy(rq->buffer + rq->ccw.count, data, size);
216 rq->ccw.count += size;
217 return 0;
218}
219
220/*
221 * Set address/length pair to ccw of a request.
222 */
223void
224raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
225{
226 rq->ccw.cda = __pa(data);
227 rq->ccw.count = size;
228}
229
230/*
231 * Set idal buffer to ccw of a request.
232 */
233void
234raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
235{
236 rq->ccw.cda = __pa(ib->data);
237 rq->ccw.count = ib->size;
238 rq->ccw.flags |= CCW_FLAG_IDA;
239}
240
241/*
242 * Stop running ccw.
243 */
244static int
245raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
246{
247 int retries;
248 int rc;
249
250 if (raw3270_request_final(rq))
251 return 0;
252 /* Check if interrupt has already been processed */
253 for (retries = 0; retries < 5; retries++) {
254 if (retries < 2)
255 rc = ccw_device_halt(rp->cdev, (long) rq);
256 else
257 rc = ccw_device_clear(rp->cdev, (long) rq);
258 if (rc == 0)
259 break; /* termination successful */
260 }
261 return rc;
262}
263
264static int
265raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
266{
267 unsigned long flags;
268 int rc;
269
270 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
271 rc = raw3270_halt_io_nolock(rp, rq);
272 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
273 return rc;
274}
275
276/*
277 * Add the request to the request queue, try to start it if the
278 * 3270 device is idle. Return without waiting for end of i/o.
279 */
280static int
281__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
282 struct raw3270_request *rq)
283{
284 rq->view = view;
285 raw3270_get_view(view);
286 if (list_empty(&rp->req_queue) &&
287 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
288 /* No other requests are on the queue. Start this one. */
289 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
290 (unsigned long) rq, 0, 0);
291 if (rq->rc) {
292 raw3270_put_view(view);
293 return rq->rc;
294 }
295 }
296 list_add_tail(&rq->list, &rp->req_queue);
297 return 0;
298}
299
300int
301raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
302{
303 unsigned long flags;
304 struct raw3270 *rp;
305 int rc;
306
307 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
308 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200309 if (!rp || rp->view != view ||
310 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 rc = -EACCES;
312 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
313 rc = -ENODEV;
314 else
315 rc = __raw3270_start(rp, view, rq);
316 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
317 return rc;
318}
319
320int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800321raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
322{
323 struct raw3270 *rp;
324 int rc;
325
326 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200327 if (!rp || rp->view != view ||
328 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800329 rc = -EACCES;
330 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
331 rc = -ENODEV;
332 else
333 rc = __raw3270_start(rp, view, rq);
334 return rc;
335}
336
337int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
339{
340 struct raw3270 *rp;
341
342 rp = view->dev;
343 rq->view = view;
344 raw3270_get_view(view);
345 list_add_tail(&rq->list, &rp->req_queue);
346 return 0;
347}
348
349/*
350 * 3270 interrupt routine, called from the ccw_device layer
351 */
352static void
353raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
354{
355 struct raw3270 *rp;
356 struct raw3270_view *view;
357 struct raw3270_request *rq;
358 int rc;
359
360 rp = (struct raw3270 *) cdev->dev.driver_data;
361 if (!rp)
362 return;
363 rq = (struct raw3270_request *) intparm;
364 view = rq ? rq->view : rp->view;
365
366 if (IS_ERR(irb))
367 rc = RAW3270_IO_RETRY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200368 else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 rq->rc = -EIO;
370 rc = RAW3270_IO_DONE;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200371 } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
372 DEV_STAT_UNIT_EXCEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Handle CE-DE-UE and subsequent UDE */
374 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
375 rc = RAW3270_IO_BUSY;
376 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
377 /* Wait for UDE if busy flag is set. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200378 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
380 /* Got it, now retry. */
381 rc = RAW3270_IO_RETRY;
382 } else
383 rc = RAW3270_IO_BUSY;
384 } else if (view)
385 rc = view->fn->intv(view, rq, irb);
386 else
387 rc = RAW3270_IO_DONE;
388
389 switch (rc) {
390 case RAW3270_IO_DONE:
391 break;
392 case RAW3270_IO_BUSY:
393 /*
394 * Intervention required by the operator. We have to wait
395 * for unsolicited device end.
396 */
397 return;
398 case RAW3270_IO_RETRY:
399 if (!rq)
400 break;
401 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
402 (unsigned long) rq, 0, 0);
403 if (rq->rc == 0)
404 return; /* Sucessfully restarted. */
405 break;
406 case RAW3270_IO_STOP:
407 if (!rq)
408 break;
409 raw3270_halt_io_nolock(rp, rq);
410 rq->rc = -EIO;
411 break;
412 default:
413 BUG();
414 }
415 if (rq) {
416 BUG_ON(list_empty(&rq->list));
417 /* The request completed, remove from queue and do callback. */
418 list_del_init(&rq->list);
419 if (rq->callback)
420 rq->callback(rq, rq->callback_data);
421 /* Do put_device for get_device in raw3270_start. */
422 raw3270_put_view(view);
423 }
424 /*
425 * Try to start each request on request queue until one is
426 * started successful.
427 */
428 while (!list_empty(&rp->req_queue)) {
429 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
430 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
431 (unsigned long) rq, 0, 0);
432 if (rq->rc == 0)
433 break;
434 /* Start failed. Remove request and do callback. */
435 list_del_init(&rq->list);
436 if (rq->callback)
437 rq->callback(rq, rq->callback_data);
438 /* Do put_device for get_device in raw3270_start. */
439 raw3270_put_view(view);
440 }
441}
442
443/*
444 * Size sensing.
445 */
446
447struct raw3270_ua { /* Query Reply structure for Usable Area */
448 struct { /* Usable Area Query Reply Base */
449 short l; /* Length of this structured field */
450 char sfid; /* 0x81 if Query Reply */
451 char qcode; /* 0x81 if Usable Area */
452 char flags0;
453 char flags1;
454 short w; /* Width of usable area */
455 short h; /* Heigth of usavle area */
456 char units; /* 0x00:in; 0x01:mm */
457 int xr;
458 int yr;
459 char aw;
460 char ah;
461 short buffsz; /* Character buffer size, bytes */
462 char xmin;
463 char ymin;
464 char xmax;
465 char ymax;
466 } __attribute__ ((packed)) uab;
467 struct { /* Alternate Usable Area Self-Defining Parameter */
468 char l; /* Length of this Self-Defining Parm */
469 char sdpid; /* 0x02 if Alternate Usable Area */
470 char res;
471 char auaid; /* 0x01 is Id for the A U A */
472 short wauai; /* Width of AUAi */
473 short hauai; /* Height of AUAi */
474 char auaunits; /* 0x00:in, 0x01:mm */
475 int auaxr;
476 int auayr;
477 char awauai;
478 char ahauai;
479 } __attribute__ ((packed)) aua;
480} __attribute__ ((packed));
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static struct diag210 raw3270_init_diag210;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200483static DEFINE_MUTEX(raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485static int
486raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
487 struct irb *irb)
488{
489 /*
490 * Unit-Check Processing:
491 * Expect Command Reject or Intervention Required.
492 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200493 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Request finished abnormally. */
495 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
496 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
497 return RAW3270_IO_BUSY;
498 }
499 }
500 if (rq) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200501 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (irb->ecw[0] & SNS0_CMD_REJECT)
503 rq->rc = -EOPNOTSUPP;
504 else
505 rq->rc = -EIO;
506 } else
507 /* Request finished normally. Copy residual count. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200508 rq->rescnt = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200510 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
512 wake_up(&raw3270_wait_queue);
513 }
514 return RAW3270_IO_DONE;
515}
516
517static struct raw3270_fn raw3270_init_fn = {
518 .intv = raw3270_init_irq
519};
520
521static struct raw3270_view raw3270_init_view = {
522 .fn = &raw3270_init_fn
523};
524
525/*
526 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
527 * Wait for end of request. The request must have been started
528 * with raw3270_start, rc = 0. The device lock may NOT have been
529 * released between calling raw3270_start and raw3270_wait.
530 */
531static void
532raw3270_wake_init(struct raw3270_request *rq, void *data)
533{
534 wake_up((wait_queue_head_t *) data);
535}
536
537/*
538 * Special wait function that can cope with console initialization.
539 */
540static int
541raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
542 struct raw3270_request *rq)
543{
544 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int rc;
546
547#ifdef CONFIG_TN3270_CONSOLE
548 if (raw3270_registered == 0) {
549 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200550 rq->callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rc = __raw3270_start(rp, view, rq);
552 if (rc == 0)
553 while (!raw3270_request_final(rq)) {
554 wait_cons_dev();
555 barrier();
556 }
557 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
558 return rq->rc;
559 }
560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 rq->callback = raw3270_wake_init;
Martin Schwidefsky54ad6412008-05-30 10:03:32 +0200562 rq->callback_data = &raw3270_wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
564 rc = __raw3270_start(rp, view, rq);
565 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
566 if (rc)
567 return rc;
568 /* Now wait for the completion. */
Martin Schwidefsky54ad6412008-05-30 10:03:32 +0200569 rc = wait_event_interruptible(raw3270_wait_queue,
570 raw3270_request_final(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
572 raw3270_halt_io(view->dev, rq);
573 /* No wait for the halt to complete. */
Martin Schwidefsky54ad6412008-05-30 10:03:32 +0200574 wait_event(raw3270_wait_queue, raw3270_request_final(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -ERESTARTSYS;
576 }
577 return rq->rc;
578}
579
580static int
581__raw3270_size_device_vm(struct raw3270 *rp)
582{
583 int rc, model;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200584 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200586 ccw_device_get_id(rp->cdev, &dev_id);
587 raw3270_init_diag210.vrdcdvno = dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 raw3270_init_diag210.vrdclen = sizeof(struct diag210);
589 rc = diag210(&raw3270_init_diag210);
590 if (rc)
591 return rc;
592 model = raw3270_init_diag210.vrdccrmd;
593 switch (model) {
594 case 2:
595 rp->model = model;
596 rp->rows = 24;
597 rp->cols = 80;
598 break;
599 case 3:
600 rp->model = model;
601 rp->rows = 32;
602 rp->cols = 80;
603 break;
604 case 4:
605 rp->model = model;
606 rp->rows = 43;
607 rp->cols = 80;
608 break;
609 case 5:
610 rp->model = model;
611 rp->rows = 27;
612 rp->cols = 132;
613 break;
614 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 rc = -EOPNOTSUPP;
616 break;
617 }
618 return rc;
619}
620
621static int
622__raw3270_size_device(struct raw3270 *rp)
623{
624 static const unsigned char wbuf[] =
625 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
626 struct raw3270_ua *uap;
627 unsigned short count;
628 int rc;
629
630 /*
631 * To determine the size of the 3270 device we need to do:
632 * 1) send a 'read partition' data stream to the device
633 * 2) wait for the attn interrupt that preceeds the query reply
634 * 3) do a read modified to get the query reply
635 * To make things worse we have to cope with intervention
636 * required (3270 device switched to 'stand-by') and command
637 * rejects (old devices that can't do 'read partition').
638 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200639 memset(&rp->init_request, 0, sizeof(rp->init_request));
640 memset(&rp->init_data, 0, 256);
641 /* Store 'read partition' data stream to init_data */
642 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
643 INIT_LIST_HEAD(&rp->init_request.list);
644 rp->init_request.ccw.cmd_code = TC_WRITESF;
645 rp->init_request.ccw.flags = CCW_FLAG_SLI;
646 rp->init_request.ccw.count = sizeof(wbuf);
647 rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200649 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700650 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* Wait for attention interrupt. */
655#ifdef CONFIG_TN3270_CONSOLE
656 if (raw3270_registered == 0) {
657 unsigned long flags;
658
659 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
660 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
661 wait_cons_dev();
662 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
663 } else
664#endif
665 rc = wait_event_interruptible(raw3270_wait_queue,
666 test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
667 if (rc)
668 return rc;
669
670 /*
671 * The device accepted the 'read partition' command. Now
672 * set up a read ccw and issue it.
673 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200674 rp->init_request.ccw.cmd_code = TC_READMOD;
675 rp->init_request.ccw.flags = CCW_FLAG_SLI;
676 rp->init_request.ccw.count = sizeof(rp->init_data);
677 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
678 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (rc)
680 return rc;
681 /* Got a Query Reply */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200682 count = sizeof(rp->init_data) - rp->init_request.rescnt;
683 uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Paranoia check. */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200685 if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return -EOPNOTSUPP;
687 /* Copy rows/columns of default Usable Area */
688 rp->rows = uap->uab.h;
689 rp->cols = uap->uab.w;
690 /* Check for 14 bit addressing */
691 if ((uap->uab.flags0 & 0x0d) == 0x01)
692 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
693 /* Check for Alternate Usable Area */
694 if (uap->uab.l == sizeof(struct raw3270_ua) &&
695 uap->aua.sdpid == 0x02) {
696 rp->rows = uap->aua.hauai;
697 rp->cols = uap->aua.wauai;
698 }
699 return 0;
700}
701
702static int
703raw3270_size_device(struct raw3270 *rp)
704{
705 int rc;
706
Christoph Hellwigd330f932007-05-31 17:38:04 +0200707 mutex_lock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 rp->view = &raw3270_init_view;
709 raw3270_init_view.dev = rp;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700710 if (MACHINE_IS_VM)
711 rc = __raw3270_size_device_vm(rp);
712 else
713 rc = __raw3270_size_device(rp);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200714 raw3270_init_view.dev = NULL;
715 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200716 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (rc == 0) { /* Found something. */
718 /* Try to find a model. */
719 rp->model = 0;
720 if (rp->rows == 24 && rp->cols == 80)
721 rp->model = 2;
722 if (rp->rows == 32 && rp->cols == 80)
723 rp->model = 3;
724 if (rp->rows == 43 && rp->cols == 80)
725 rp->model = 4;
726 if (rp->rows == 27 && rp->cols == 132)
727 rp->model = 5;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700728 } else {
729 /* Couldn't detect size. Use default model 2. */
730 rp->model = 2;
731 rp->rows = 24;
732 rp->cols = 80;
733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 return rc;
736}
737
738static int
739raw3270_reset_device(struct raw3270 *rp)
740{
741 int rc;
742
Christoph Hellwigd330f932007-05-31 17:38:04 +0200743 mutex_lock(&raw3270_init_mutex);
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200744 memset(&rp->init_request, 0, sizeof(rp->init_request));
745 memset(&rp->init_data, 0, sizeof(rp->init_data));
746 /* Store reset data stream to init_data/init_request */
747 rp->init_data[0] = TW_KR;
748 INIT_LIST_HEAD(&rp->init_request.list);
749 rp->init_request.ccw.cmd_code = TC_EWRITEA;
750 rp->init_request.ccw.flags = CCW_FLAG_SLI;
751 rp->init_request.ccw.count = 1;
752 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 rp->view = &raw3270_init_view;
754 raw3270_init_view.dev = rp;
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200755 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200756 raw3270_init_view.dev = NULL;
757 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200758 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return rc;
760}
761
Richard Hitted3cb6f2005-10-30 15:00:10 -0800762int
763raw3270_reset(struct raw3270_view *view)
764{
765 struct raw3270 *rp;
766 int rc;
767
768 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200769 if (!rp || rp->view != view ||
770 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800771 rc = -EACCES;
772 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
773 rc = -ENODEV;
774 else
775 rc = raw3270_reset_device(view->dev);
776 return rc;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/*
780 * Setup new 3270 device.
781 */
782static int
783raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
784{
785 struct list_head *l;
786 struct raw3270 *tmp;
787 int minor;
788
789 memset(rp, 0, sizeof(struct raw3270));
790 /* Copy ebcdic -> ascii translation table. */
791 memcpy(ascebc, _ascebc, 256);
792 if (tubxcorrect) {
793 /* correct brackets and circumflex */
794 ascebc['['] = 0xad;
795 ascebc[']'] = 0xbd;
796 ascebc['^'] = 0xb0;
797 }
798 rp->ascebc = ascebc;
799
800 /* Set defaults. */
801 rp->rows = 24;
802 rp->cols = 80;
803
804 INIT_LIST_HEAD(&rp->req_queue);
805 INIT_LIST_HEAD(&rp->view_list);
806
807 /*
808 * Add device to list and find the smallest unused minor
Richard Hitted3cb6f2005-10-30 15:00:10 -0800809 * number for it. Note: there is no device with minor 0,
810 * see special case for fs3270.c:fs3270_open().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800812 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Keep the list sorted. */
Richard Hitted3cb6f2005-10-30 15:00:10 -0800814 minor = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 rp->minor = -1;
816 list_for_each(l, &raw3270_devices) {
817 tmp = list_entry(l, struct raw3270, list);
818 if (tmp->minor > minor) {
819 rp->minor = minor;
820 __list_add(&rp->list, l->prev, l);
821 break;
822 }
823 minor++;
824 }
Richard Hitted3cb6f2005-10-30 15:00:10 -0800825 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 rp->minor = minor;
827 list_add_tail(&rp->list, &raw3270_devices);
828 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800829 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* No free minor number? Then give up. */
831 if (rp->minor == -1)
832 return -EUSERS;
833 rp->cdev = cdev;
834 cdev->dev.driver_data = rp;
835 cdev->handler = raw3270_irq;
836 return 0;
837}
838
839#ifdef CONFIG_TN3270_CONSOLE
840/*
841 * Setup 3270 device configured as console.
842 */
Heiko Carstense62133b2007-07-27 12:29:13 +0200843struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct raw3270 *rp;
846 char *ascebc;
847 int rc;
848
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200849 rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 ascebc = (char *) alloc_bootmem(256);
851 rc = raw3270_setup_device(cdev, rp, ascebc);
852 if (rc)
853 return ERR_PTR(rc);
854 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
855 rc = raw3270_reset_device(rp);
856 if (rc)
857 return ERR_PTR(rc);
858 rc = raw3270_size_device(rp);
859 if (rc)
860 return ERR_PTR(rc);
861 rc = raw3270_reset_device(rp);
862 if (rc)
863 return ERR_PTR(rc);
864 set_bit(RAW3270_FLAGS_READY, &rp->flags);
865 return rp;
866}
867
868void
869raw3270_wait_cons_dev(struct raw3270 *rp)
870{
871 unsigned long flags;
872
873 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
874 wait_cons_dev();
875 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
876}
877
878#endif
879
880/*
881 * Create a 3270 device structure.
882 */
883static struct raw3270 *
884raw3270_create_device(struct ccw_device *cdev)
885{
886 struct raw3270 *rp;
887 char *ascebc;
888 int rc;
889
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200890 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (!rp)
892 return ERR_PTR(-ENOMEM);
893 ascebc = kmalloc(256, GFP_KERNEL);
894 if (!ascebc) {
895 kfree(rp);
896 return ERR_PTR(-ENOMEM);
897 }
898 rc = raw3270_setup_device(cdev, rp, ascebc);
899 if (rc) {
900 kfree(rp->ascebc);
901 kfree(rp);
902 rp = ERR_PTR(rc);
903 }
904 /* Get reference to ccw_device structure. */
905 get_device(&cdev->dev);
906 return rp;
907}
908
909/*
910 * Activate a view.
911 */
912int
913raw3270_activate_view(struct raw3270_view *view)
914{
915 struct raw3270 *rp;
916 struct raw3270_view *oldview, *nv;
917 unsigned long flags;
918 int rc;
919
920 rp = view->dev;
921 if (!rp)
922 return -ENODEV;
923 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
924 if (rp->view == view)
925 rc = 0;
926 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
927 rc = -ENODEV;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200928 else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
929 rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 else {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200931 oldview = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (rp->view) {
933 oldview = rp->view;
934 oldview->fn->deactivate(oldview);
935 }
936 rp->view = view;
937 rc = view->fn->activate(view);
938 if (rc) {
939 /* Didn't work. Try to reactivate the old view. */
940 rp->view = oldview;
941 if (!oldview || oldview->fn->activate(oldview) != 0) {
942 /* Didn't work as well. Try any other view. */
943 list_for_each_entry(nv, &rp->view_list, list)
944 if (nv != view && nv != oldview) {
945 rp->view = nv;
946 if (nv->fn->activate(nv) == 0)
947 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200948 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 }
951 }
952 }
953 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
954 return rc;
955}
956
957/*
958 * Deactivate current view.
959 */
960void
961raw3270_deactivate_view(struct raw3270_view *view)
962{
963 unsigned long flags;
964 struct raw3270 *rp;
965
966 rp = view->dev;
967 if (!rp)
968 return;
969 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
970 if (rp->view == view) {
971 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200972 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* Move deactivated view to end of list. */
974 list_del_init(&view->list);
975 list_add_tail(&view->list, &rp->view_list);
976 /* Try to activate another view. */
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200977 if (test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
978 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800979 list_for_each_entry(view, &rp->view_list, list) {
980 rp->view = view;
981 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200983 rp->view = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 }
987 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
988}
989
990/*
991 * Add view to device with minor "minor".
992 */
993int
994raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
995{
996 unsigned long flags;
997 struct raw3270 *rp;
998 int rc;
999
Richard Hitted3cb6f2005-10-30 15:00:10 -08001000 if (minor <= 0)
1001 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001002 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 rc = -ENODEV;
1004 list_for_each_entry(rp, &raw3270_devices, list) {
1005 if (rp->minor != minor)
1006 continue;
1007 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1008 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1009 atomic_set(&view->ref_count, 2);
1010 view->dev = rp;
1011 view->fn = fn;
1012 view->model = rp->model;
1013 view->rows = rp->rows;
1014 view->cols = rp->cols;
1015 view->ascebc = rp->ascebc;
1016 spin_lock_init(&view->lock);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001017 list_add(&view->list, &rp->view_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 rc = 0;
1019 }
1020 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1021 break;
1022 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001023 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return rc;
1025}
1026
1027/*
1028 * Find specific view of device with minor "minor".
1029 */
1030struct raw3270_view *
1031raw3270_find_view(struct raw3270_fn *fn, int minor)
1032{
1033 struct raw3270 *rp;
1034 struct raw3270_view *view, *tmp;
1035 unsigned long flags;
1036
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001037 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 view = ERR_PTR(-ENODEV);
1039 list_for_each_entry(rp, &raw3270_devices, list) {
1040 if (rp->minor != minor)
1041 continue;
1042 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1043 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1044 view = ERR_PTR(-ENOENT);
1045 list_for_each_entry(tmp, &rp->view_list, list) {
1046 if (tmp->fn == fn) {
1047 raw3270_get_view(tmp);
1048 view = tmp;
1049 break;
1050 }
1051 }
1052 }
1053 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1054 break;
1055 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001056 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return view;
1058}
1059
1060/*
1061 * Remove view from device and free view structure via call to view->fn->free.
1062 */
1063void
1064raw3270_del_view(struct raw3270_view *view)
1065{
1066 unsigned long flags;
1067 struct raw3270 *rp;
1068 struct raw3270_view *nv;
1069
1070 rp = view->dev;
1071 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1072 if (rp->view == view) {
1073 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001074 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076 list_del_init(&view->list);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001077 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
1078 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Try to activate another view. */
1080 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001081 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rp->view = nv;
1083 break;
1084 }
1085 }
1086 }
1087 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1088 /* Wait for reference counter to drop to zero. */
1089 atomic_dec(&view->ref_count);
1090 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1091 if (view->fn->free)
1092 view->fn->free(view);
1093}
1094
1095/*
1096 * Remove a 3270 device structure.
1097 */
1098static void
1099raw3270_delete_device(struct raw3270 *rp)
1100{
1101 struct ccw_device *cdev;
1102
1103 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001104 mutex_lock(&raw3270_mutex);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001105 if (rp->clttydev && !IS_ERR(rp->clttydev))
Cornelia Huck7f021ce2007-10-22 12:52:42 +02001106 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001107 if (rp->cltubdev && !IS_ERR(rp->cltubdev))
Cornelia Huck7f021ce2007-10-22 12:52:42 +02001108 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001110 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* Disconnect from ccw_device. */
1113 cdev = rp->cdev;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001114 rp->cdev = NULL;
1115 cdev->dev.driver_data = NULL;
1116 cdev->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* Put ccw_device structure. */
1119 put_device(&cdev->dev);
1120
1121 /* Now free raw3270 structure. */
1122 kfree(rp->ascebc);
1123 kfree(rp);
1124}
1125
1126static int
1127raw3270_probe (struct ccw_device *cdev)
1128{
1129 return 0;
1130}
1131
1132/*
1133 * Additional attributes for a 3270 device
1134 */
1135static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001136raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 return snprintf(buf, PAGE_SIZE, "%i\n",
1139 ((struct raw3270 *) dev->driver_data)->model);
1140}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001141static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001144raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 return snprintf(buf, PAGE_SIZE, "%i\n",
1147 ((struct raw3270 *) dev->driver_data)->rows);
1148}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001149static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001152raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 return snprintf(buf, PAGE_SIZE, "%i\n",
1155 ((struct raw3270 *) dev->driver_data)->cols);
1156}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001157static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159static struct attribute * raw3270_attrs[] = {
1160 &dev_attr_model.attr,
1161 &dev_attr_rows.attr,
1162 &dev_attr_columns.attr,
1163 NULL,
1164};
1165
1166static struct attribute_group raw3270_attr_group = {
1167 .attrs = raw3270_attrs,
1168};
1169
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001170static int raw3270_create_attributes(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001172 int rc;
1173
1174 rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1175 if (rc)
1176 goto out;
1177
Greg Kroah-Hartmanea9e42f2008-07-21 20:03:34 -07001178 rp->clttydev = device_create(class3270, &rp->cdev->dev,
1179 MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
1180 "tty%s", dev_name(&rp->cdev->dev));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001181 if (IS_ERR(rp->clttydev)) {
1182 rc = PTR_ERR(rp->clttydev);
1183 goto out_ttydev;
1184 }
1185
Greg Kroah-Hartmanea9e42f2008-07-21 20:03:34 -07001186 rp->cltubdev = device_create(class3270, &rp->cdev->dev,
1187 MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
1188 "tub%s", dev_name(&rp->cdev->dev));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001189 if (!IS_ERR(rp->cltubdev))
1190 goto out;
1191
1192 rc = PTR_ERR(rp->cltubdev);
Cornelia Huck7f021ce2007-10-22 12:52:42 +02001193 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001194
1195out_ttydev:
1196 sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1197out:
1198 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/*
1202 * Notifier for device addition/removal
1203 */
1204struct raw3270_notifier {
1205 struct list_head list;
1206 void (*notifier)(int, int);
1207};
1208
Denis Chengc11ca972008-01-26 14:11:13 +01001209static LIST_HEAD(raw3270_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211int raw3270_register_notifier(void (*notifier)(int, int))
1212{
1213 struct raw3270_notifier *np;
1214 struct raw3270 *rp;
1215
1216 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1217 if (!np)
1218 return -ENOMEM;
1219 np->notifier = notifier;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001220 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 list_add_tail(&np->list, &raw3270_notifier);
1222 list_for_each_entry(rp, &raw3270_devices, list) {
1223 get_device(&rp->cdev->dev);
1224 notifier(rp->minor, 1);
1225 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001226 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return 0;
1228}
1229
1230void raw3270_unregister_notifier(void (*notifier)(int, int))
1231{
1232 struct raw3270_notifier *np;
1233
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001234 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 list_for_each_entry(np, &raw3270_notifier, list)
1236 if (np->notifier == notifier) {
1237 list_del(&np->list);
1238 kfree(np);
1239 break;
1240 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001241 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244/*
1245 * Set 3270 device online.
1246 */
1247static int
1248raw3270_set_online (struct ccw_device *cdev)
1249{
1250 struct raw3270 *rp;
1251 struct raw3270_notifier *np;
1252 int rc;
1253
1254 rp = raw3270_create_device(cdev);
1255 if (IS_ERR(rp))
1256 return PTR_ERR(rp);
1257 rc = raw3270_reset_device(rp);
1258 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001259 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 rc = raw3270_size_device(rp);
1261 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001262 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 rc = raw3270_reset_device(rp);
1264 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001265 goto failure;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001266 rc = raw3270_create_attributes(rp);
1267 if (rc)
1268 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 set_bit(RAW3270_FLAGS_READY, &rp->flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001270 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 list_for_each_entry(np, &raw3270_notifier, list)
1272 np->notifier(rp->minor, 1);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001273 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001275
1276failure:
1277 raw3270_delete_device(rp);
1278 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/*
1282 * Remove 3270 device structure.
1283 */
1284static void
1285raw3270_remove (struct ccw_device *cdev)
1286{
1287 unsigned long flags;
1288 struct raw3270 *rp;
1289 struct raw3270_view *v;
1290 struct raw3270_notifier *np;
1291
1292 rp = cdev->dev.driver_data;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001293 /*
1294 * _remove is the opposite of _probe; it's probe that
1295 * should set up rp. raw3270_remove gets entered for
1296 * devices even if they haven't been varied online.
1297 * Thus, rp may validly be NULL here.
1298 */
1299 if (rp == NULL)
1300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1302
1303 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1304
1305 /* Deactivate current view and remove all views. */
1306 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1307 if (rp->view) {
1308 rp->view->fn->deactivate(rp->view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001309 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311 while (!list_empty(&rp->view_list)) {
1312 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1313 if (v->fn->release)
1314 v->fn->release(v);
1315 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1316 raw3270_del_view(v);
1317 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1318 }
1319 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1320
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001321 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 list_for_each_entry(np, &raw3270_notifier, list)
1323 np->notifier(rp->minor, 0);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001324 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* Reset 3270 device. */
1327 raw3270_reset_device(rp);
1328 /* And finally remove it. */
1329 raw3270_delete_device(rp);
1330}
1331
1332/*
1333 * Set 3270 device offline.
1334 */
1335static int
1336raw3270_set_offline (struct ccw_device *cdev)
1337{
1338 struct raw3270 *rp;
1339
1340 rp = cdev->dev.driver_data;
1341 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1342 return -EBUSY;
1343 raw3270_remove(cdev);
1344 return 0;
1345}
1346
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001347static int raw3270_pm_stop(struct ccw_device *cdev)
1348{
1349 struct raw3270 *rp;
1350 struct raw3270_view *view;
1351 unsigned long flags;
1352
1353 rp = cdev->dev.driver_data;
1354 if (!rp)
1355 return 0;
1356 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1357 if (rp->view)
1358 rp->view->fn->deactivate(rp->view);
1359 if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
1360 /*
1361 * Release tty and fullscreen for all non-console
1362 * devices.
1363 */
1364 list_for_each_entry(view, &rp->view_list, list) {
1365 if (view->fn->release)
1366 view->fn->release(view);
1367 }
1368 }
1369 set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1370 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1371 return 0;
1372}
1373
1374static int raw3270_pm_start(struct ccw_device *cdev)
1375{
1376 struct raw3270 *rp;
1377 unsigned long flags;
1378
1379 rp = cdev->dev.driver_data;
1380 if (!rp)
1381 return 0;
1382 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1383 clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1384 if (rp->view)
1385 rp->view->fn->activate(rp->view);
1386 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1387 return 0;
1388}
1389
1390void raw3270_pm_unfreeze(struct raw3270_view *view)
1391{
1392 struct raw3270 *rp;
1393
1394 rp = view->dev;
1395 if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
1396 ccw_device_force_console();
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399static struct ccw_device_id raw3270_id[] = {
1400 { CCW_DEVICE(0x3270, 0) },
1401 { CCW_DEVICE(0x3271, 0) },
1402 { CCW_DEVICE(0x3272, 0) },
1403 { CCW_DEVICE(0x3273, 0) },
1404 { CCW_DEVICE(0x3274, 0) },
1405 { CCW_DEVICE(0x3275, 0) },
1406 { CCW_DEVICE(0x3276, 0) },
1407 { CCW_DEVICE(0x3277, 0) },
1408 { CCW_DEVICE(0x3278, 0) },
1409 { CCW_DEVICE(0x3279, 0) },
1410 { CCW_DEVICE(0x3174, 0) },
1411 { /* end of list */ },
1412};
1413
1414static struct ccw_driver raw3270_ccw_driver = {
1415 .name = "3270",
1416 .owner = THIS_MODULE,
1417 .ids = raw3270_id,
1418 .probe = &raw3270_probe,
1419 .remove = &raw3270_remove,
1420 .set_online = &raw3270_set_online,
1421 .set_offline = &raw3270_set_offline,
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001422 .freeze = &raw3270_pm_stop,
1423 .thaw = &raw3270_pm_start,
1424 .restore = &raw3270_pm_start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425};
1426
1427static int
1428raw3270_init(void)
1429{
1430 struct raw3270 *rp;
1431 int rc;
1432
1433 if (raw3270_registered)
1434 return 0;
1435 raw3270_registered = 1;
1436 rc = ccw_driver_register(&raw3270_ccw_driver);
1437 if (rc == 0) {
1438 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001439 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001440 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 list_for_each_entry(rp, &raw3270_devices, list) {
1442 get_device(&rp->cdev->dev);
1443 raw3270_create_attributes(rp);
1444 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001445 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 return rc;
1448}
1449
1450static void
1451raw3270_exit(void)
1452{
1453 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001454 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
1457MODULE_LICENSE("GPL");
1458
1459module_init(raw3270_init);
1460module_exit(raw3270_exit);
1461
1462EXPORT_SYMBOL(raw3270_request_alloc);
1463EXPORT_SYMBOL(raw3270_request_free);
1464EXPORT_SYMBOL(raw3270_request_reset);
1465EXPORT_SYMBOL(raw3270_request_set_cmd);
1466EXPORT_SYMBOL(raw3270_request_add_data);
1467EXPORT_SYMBOL(raw3270_request_set_data);
1468EXPORT_SYMBOL(raw3270_request_set_idal);
1469EXPORT_SYMBOL(raw3270_buffer_address);
1470EXPORT_SYMBOL(raw3270_add_view);
1471EXPORT_SYMBOL(raw3270_del_view);
1472EXPORT_SYMBOL(raw3270_find_view);
1473EXPORT_SYMBOL(raw3270_activate_view);
1474EXPORT_SYMBOL(raw3270_deactivate_view);
1475EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001476EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001478EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479EXPORT_SYMBOL(raw3270_register_notifier);
1480EXPORT_SYMBOL(raw3270_unregister_notifier);
1481EXPORT_SYMBOL(raw3270_wait_queue);