blob: f3b8bb84faf295013c03c1d4e539ee967015fa2a [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
Heiko Carstens2b67fc42007-02-05 21:16:47 +010031static struct 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;
40 unsigned long flags;
41
42 struct list_head req_queue; /* Request queue. */
43 struct list_head view_list; /* List of available views. */
44 struct raw3270_view *view; /* Active view. */
45
46 struct timer_list timer; /* Device timer. */
47
48 unsigned char *ascebc; /* ascii -> ebcdic table */
Cornelia Huck7f021ce2007-10-22 12:52:42 +020049 struct device *clttydev; /* 3270-class tty device ptr */
50 struct device *cltubdev; /* 3270-class tub device ptr */
Martin Schwidefsky132fab12006-06-29 14:57:39 +020051
52 struct raw3270_request init_request;
53 unsigned char init_data[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
56/* raw3270->flags */
57#define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
58#define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
59#define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
60#define RAW3270_FLAGS_READY 4 /* Device is useable by views */
61#define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
Martin Schwidefsky4b214a02009-06-16 10:30:47 +020062#define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* Semaphore to protect global data of raw3270 (devices, views, etc). */
Ingo Molnar14cc3e22006-03-26 01:37:14 -080065static DEFINE_MUTEX(raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* List of 3270 devices. */
Denis Chengc11ca972008-01-26 14:11:13 +010068static LIST_HEAD(raw3270_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * Flag to indicate if the driver has been registered. Some operations
72 * like waiting for the end of i/o need to be done differently as long
73 * as the kernel is still starting up (console support).
74 */
75static int raw3270_registered;
76
77/* Module parameters */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103078static bool tubxcorrect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079module_param(tubxcorrect, bool, 0);
80
81/*
82 * Wait queue for device init/delete, view delete.
83 */
84DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
85
86/*
87 * Encode array for 12 bit 3270 addresses.
88 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +010089static unsigned char raw3270_ebcgraf[64] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
91 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
92 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
93 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
94 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
95 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
96 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
97 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
98};
99
100void
101raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
102{
103 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
104 cp[0] = (addr >> 8) & 0x3f;
105 cp[1] = addr & 0xff;
106 } else {
107 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
108 cp[1] = raw3270_ebcgraf[addr & 0x3f];
109 }
110}
111
112/*
113 * Allocate a new 3270 ccw request
114 */
115struct raw3270_request *
116raw3270_request_alloc(size_t size)
117{
118 struct raw3270_request *rq;
119
120 /* Allocate request structure */
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800121 rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!rq)
123 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* alloc output buffer. */
126 if (size > 0) {
127 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
128 if (!rq->buffer) {
129 kfree(rq);
130 return ERR_PTR(-ENOMEM);
131 }
132 }
133 rq->size = size;
134 INIT_LIST_HEAD(&rq->list);
135
136 /*
137 * Setup ccw.
138 */
139 rq->ccw.cda = __pa(rq->buffer);
140 rq->ccw.flags = CCW_FLAG_SLI;
141
142 return rq;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * Free 3270 ccw request
147 */
148void
149raw3270_request_free (struct raw3270_request *rq)
150{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800151 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 kfree(rq);
153}
154
155/*
156 * Reset request to initial state.
157 */
158void
159raw3270_request_reset(struct raw3270_request *rq)
160{
161 BUG_ON(!list_empty(&rq->list));
162 rq->ccw.cmd_code = 0;
163 rq->ccw.count = 0;
164 rq->ccw.cda = __pa(rq->buffer);
165 rq->ccw.flags = CCW_FLAG_SLI;
166 rq->rescnt = 0;
167 rq->rc = 0;
168}
169
170/*
171 * Set command code to ccw of a request.
172 */
173void
174raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
175{
176 rq->ccw.cmd_code = cmd;
177}
178
179/*
180 * Add data fragment to output buffer.
181 */
182int
183raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
184{
185 if (size + rq->ccw.count > rq->size)
186 return -E2BIG;
187 memcpy(rq->buffer + rq->ccw.count, data, size);
188 rq->ccw.count += size;
189 return 0;
190}
191
192/*
193 * Set address/length pair to ccw of a request.
194 */
195void
196raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
197{
198 rq->ccw.cda = __pa(data);
199 rq->ccw.count = size;
200}
201
202/*
203 * Set idal buffer to ccw of a request.
204 */
205void
206raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
207{
208 rq->ccw.cda = __pa(ib->data);
209 rq->ccw.count = ib->size;
210 rq->ccw.flags |= CCW_FLAG_IDA;
211}
212
213/*
214 * Stop running ccw.
215 */
216static int
217raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
218{
219 int retries;
220 int rc;
221
222 if (raw3270_request_final(rq))
223 return 0;
224 /* Check if interrupt has already been processed */
225 for (retries = 0; retries < 5; retries++) {
226 if (retries < 2)
227 rc = ccw_device_halt(rp->cdev, (long) rq);
228 else
229 rc = ccw_device_clear(rp->cdev, (long) rq);
230 if (rc == 0)
231 break; /* termination successful */
232 }
233 return rc;
234}
235
236static int
237raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
238{
239 unsigned long flags;
240 int rc;
241
242 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
243 rc = raw3270_halt_io_nolock(rp, rq);
244 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
245 return rc;
246}
247
248/*
249 * Add the request to the request queue, try to start it if the
250 * 3270 device is idle. Return without waiting for end of i/o.
251 */
252static int
253__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
254 struct raw3270_request *rq)
255{
256 rq->view = view;
257 raw3270_get_view(view);
258 if (list_empty(&rp->req_queue) &&
259 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
260 /* No other requests are on the queue. Start this one. */
261 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
262 (unsigned long) rq, 0, 0);
263 if (rq->rc) {
264 raw3270_put_view(view);
265 return rq->rc;
266 }
267 }
268 list_add_tail(&rq->list, &rp->req_queue);
269 return 0;
270}
271
272int
273raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
274{
275 unsigned long flags;
276 struct raw3270 *rp;
277 int rc;
278
279 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
280 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200281 if (!rp || rp->view != view ||
282 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 rc = -EACCES;
284 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
285 rc = -ENODEV;
286 else
287 rc = __raw3270_start(rp, view, rq);
288 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
289 return rc;
290}
291
292int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800293raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
294{
295 struct raw3270 *rp;
296 int rc;
297
298 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200299 if (!rp || rp->view != view ||
300 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800301 rc = -EACCES;
302 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
303 rc = -ENODEV;
304 else
305 rc = __raw3270_start(rp, view, rq);
306 return rc;
307}
308
309int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
311{
312 struct raw3270 *rp;
313
314 rp = view->dev;
315 rq->view = view;
316 raw3270_get_view(view);
317 list_add_tail(&rq->list, &rp->req_queue);
318 return 0;
319}
320
321/*
322 * 3270 interrupt routine, called from the ccw_device layer
323 */
324static void
325raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
326{
327 struct raw3270 *rp;
328 struct raw3270_view *view;
329 struct raw3270_request *rq;
330 int rc;
331
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700332 rp = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!rp)
334 return;
335 rq = (struct raw3270_request *) intparm;
336 view = rq ? rq->view : rp->view;
337
338 if (IS_ERR(irb))
339 rc = RAW3270_IO_RETRY;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200340 else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 rq->rc = -EIO;
342 rc = RAW3270_IO_DONE;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200343 } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
344 DEV_STAT_UNIT_EXCEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* Handle CE-DE-UE and subsequent UDE */
346 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
347 rc = RAW3270_IO_BUSY;
348 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
349 /* Wait for UDE if busy flag is set. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200350 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
352 /* Got it, now retry. */
353 rc = RAW3270_IO_RETRY;
354 } else
355 rc = RAW3270_IO_BUSY;
356 } else if (view)
357 rc = view->fn->intv(view, rq, irb);
358 else
359 rc = RAW3270_IO_DONE;
360
361 switch (rc) {
362 case RAW3270_IO_DONE:
363 break;
364 case RAW3270_IO_BUSY:
365 /*
366 * Intervention required by the operator. We have to wait
367 * for unsolicited device end.
368 */
369 return;
370 case RAW3270_IO_RETRY:
371 if (!rq)
372 break;
373 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
374 (unsigned long) rq, 0, 0);
375 if (rq->rc == 0)
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800376 return; /* Successfully restarted. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378 case RAW3270_IO_STOP:
379 if (!rq)
380 break;
381 raw3270_halt_io_nolock(rp, rq);
382 rq->rc = -EIO;
383 break;
384 default:
385 BUG();
386 }
387 if (rq) {
388 BUG_ON(list_empty(&rq->list));
389 /* The request completed, remove from queue and do callback. */
390 list_del_init(&rq->list);
391 if (rq->callback)
392 rq->callback(rq, rq->callback_data);
393 /* Do put_device for get_device in raw3270_start. */
394 raw3270_put_view(view);
395 }
396 /*
397 * Try to start each request on request queue until one is
398 * started successful.
399 */
400 while (!list_empty(&rp->req_queue)) {
401 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
402 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
403 (unsigned long) rq, 0, 0);
404 if (rq->rc == 0)
405 break;
406 /* Start failed. Remove request and do callback. */
407 list_del_init(&rq->list);
408 if (rq->callback)
409 rq->callback(rq, rq->callback_data);
410 /* Do put_device for get_device in raw3270_start. */
411 raw3270_put_view(view);
412 }
413}
414
415/*
416 * Size sensing.
417 */
418
419struct raw3270_ua { /* Query Reply structure for Usable Area */
420 struct { /* Usable Area Query Reply Base */
421 short l; /* Length of this structured field */
422 char sfid; /* 0x81 if Query Reply */
423 char qcode; /* 0x81 if Usable Area */
424 char flags0;
425 char flags1;
426 short w; /* Width of usable area */
427 short h; /* Heigth of usavle area */
428 char units; /* 0x00:in; 0x01:mm */
429 int xr;
430 int yr;
431 char aw;
432 char ah;
433 short buffsz; /* Character buffer size, bytes */
434 char xmin;
435 char ymin;
436 char xmax;
437 char ymax;
438 } __attribute__ ((packed)) uab;
439 struct { /* Alternate Usable Area Self-Defining Parameter */
440 char l; /* Length of this Self-Defining Parm */
441 char sdpid; /* 0x02 if Alternate Usable Area */
442 char res;
443 char auaid; /* 0x01 is Id for the A U A */
444 short wauai; /* Width of AUAi */
445 short hauai; /* Height of AUAi */
446 char auaunits; /* 0x00:in, 0x01:mm */
447 int auaxr;
448 int auayr;
449 char awauai;
450 char ahauai;
451 } __attribute__ ((packed)) aua;
452} __attribute__ ((packed));
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static struct diag210 raw3270_init_diag210;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200455static DEFINE_MUTEX(raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457static int
458raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
459 struct irb *irb)
460{
461 /*
462 * Unit-Check Processing:
463 * Expect Command Reject or Intervention Required.
464 */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200465 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Request finished abnormally. */
467 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
468 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
469 return RAW3270_IO_BUSY;
470 }
471 }
472 if (rq) {
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200473 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (irb->ecw[0] & SNS0_CMD_REJECT)
475 rq->rc = -EOPNOTSUPP;
476 else
477 rq->rc = -EIO;
478 } else
479 /* Request finished normally. Copy residual count. */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200480 rq->rescnt = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200482 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
484 wake_up(&raw3270_wait_queue);
485 }
486 return RAW3270_IO_DONE;
487}
488
489static struct raw3270_fn raw3270_init_fn = {
490 .intv = raw3270_init_irq
491};
492
493static struct raw3270_view raw3270_init_view = {
494 .fn = &raw3270_init_fn
495};
496
497/*
498 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
499 * Wait for end of request. The request must have been started
500 * with raw3270_start, rc = 0. The device lock may NOT have been
501 * released between calling raw3270_start and raw3270_wait.
502 */
503static void
504raw3270_wake_init(struct raw3270_request *rq, void *data)
505{
506 wake_up((wait_queue_head_t *) data);
507}
508
509/*
510 * Special wait function that can cope with console initialization.
511 */
512static int
513raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
514 struct raw3270_request *rq)
515{
516 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int rc;
518
519#ifdef CONFIG_TN3270_CONSOLE
520 if (raw3270_registered == 0) {
521 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200522 rq->callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rc = __raw3270_start(rp, view, rq);
524 if (rc == 0)
525 while (!raw3270_request_final(rq)) {
526 wait_cons_dev();
527 barrier();
528 }
529 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
530 return rq->rc;
531 }
532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 rq->callback = raw3270_wake_init;
Martin Schwidefsky54ad6412008-05-30 10:03:32 +0200534 rq->callback_data = &raw3270_wait_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
536 rc = __raw3270_start(rp, view, rq);
537 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
538 if (rc)
539 return rc;
540 /* Now wait for the completion. */
Martin Schwidefsky54ad6412008-05-30 10:03:32 +0200541 rc = wait_event_interruptible(raw3270_wait_queue,
542 raw3270_request_final(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
544 raw3270_halt_io(view->dev, rq);
545 /* No wait for the halt to complete. */
Martin Schwidefsky54ad6412008-05-30 10:03:32 +0200546 wait_event(raw3270_wait_queue, raw3270_request_final(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ERESTARTSYS;
548 }
549 return rq->rc;
550}
551
552static int
553__raw3270_size_device_vm(struct raw3270 *rp)
554{
555 int rc, model;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200556 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200558 ccw_device_get_id(rp->cdev, &dev_id);
559 raw3270_init_diag210.vrdcdvno = dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 raw3270_init_diag210.vrdclen = sizeof(struct diag210);
561 rc = diag210(&raw3270_init_diag210);
562 if (rc)
563 return rc;
564 model = raw3270_init_diag210.vrdccrmd;
565 switch (model) {
566 case 2:
567 rp->model = model;
568 rp->rows = 24;
569 rp->cols = 80;
570 break;
571 case 3:
572 rp->model = model;
573 rp->rows = 32;
574 rp->cols = 80;
575 break;
576 case 4:
577 rp->model = model;
578 rp->rows = 43;
579 rp->cols = 80;
580 break;
581 case 5:
582 rp->model = model;
583 rp->rows = 27;
584 rp->cols = 132;
585 break;
586 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 rc = -EOPNOTSUPP;
588 break;
589 }
590 return rc;
591}
592
593static int
594__raw3270_size_device(struct raw3270 *rp)
595{
596 static const unsigned char wbuf[] =
597 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
598 struct raw3270_ua *uap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int rc;
600
601 /*
602 * To determine the size of the 3270 device we need to do:
603 * 1) send a 'read partition' data stream to the device
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 * 2) wait for the attn interrupt that precedes the query reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * 3) do a read modified to get the query reply
606 * To make things worse we have to cope with intervention
607 * required (3270 device switched to 'stand-by') and command
608 * rejects (old devices that can't do 'read partition').
609 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200610 memset(&rp->init_request, 0, sizeof(rp->init_request));
611 memset(&rp->init_data, 0, 256);
612 /* Store 'read partition' data stream to init_data */
613 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
614 INIT_LIST_HEAD(&rp->init_request.list);
615 rp->init_request.ccw.cmd_code = TC_WRITESF;
616 rp->init_request.ccw.flags = CCW_FLAG_SLI;
617 rp->init_request.ccw.count = sizeof(wbuf);
618 rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200620 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700621 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* Wait for attention interrupt. */
626#ifdef CONFIG_TN3270_CONSOLE
627 if (raw3270_registered == 0) {
628 unsigned long flags;
629
630 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
631 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
632 wait_cons_dev();
633 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
634 } else
635#endif
636 rc = wait_event_interruptible(raw3270_wait_queue,
637 test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
638 if (rc)
639 return rc;
640
641 /*
642 * The device accepted the 'read partition' command. Now
643 * set up a read ccw and issue it.
644 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200645 rp->init_request.ccw.cmd_code = TC_READMOD;
646 rp->init_request.ccw.flags = CCW_FLAG_SLI;
647 rp->init_request.ccw.count = sizeof(rp->init_data);
648 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
649 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (rc)
651 return rc;
652 /* Got a Query Reply */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200653 uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Paranoia check. */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200655 if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -EOPNOTSUPP;
657 /* Copy rows/columns of default Usable Area */
658 rp->rows = uap->uab.h;
659 rp->cols = uap->uab.w;
660 /* Check for 14 bit addressing */
661 if ((uap->uab.flags0 & 0x0d) == 0x01)
662 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
663 /* Check for Alternate Usable Area */
664 if (uap->uab.l == sizeof(struct raw3270_ua) &&
665 uap->aua.sdpid == 0x02) {
666 rp->rows = uap->aua.hauai;
667 rp->cols = uap->aua.wauai;
668 }
669 return 0;
670}
671
672static int
673raw3270_size_device(struct raw3270 *rp)
674{
675 int rc;
676
Christoph Hellwigd330f932007-05-31 17:38:04 +0200677 mutex_lock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 rp->view = &raw3270_init_view;
679 raw3270_init_view.dev = rp;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700680 if (MACHINE_IS_VM)
681 rc = __raw3270_size_device_vm(rp);
682 else
683 rc = __raw3270_size_device(rp);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200684 raw3270_init_view.dev = NULL;
685 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200686 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (rc == 0) { /* Found something. */
688 /* Try to find a model. */
689 rp->model = 0;
690 if (rp->rows == 24 && rp->cols == 80)
691 rp->model = 2;
692 if (rp->rows == 32 && rp->cols == 80)
693 rp->model = 3;
694 if (rp->rows == 43 && rp->cols == 80)
695 rp->model = 4;
696 if (rp->rows == 27 && rp->cols == 132)
697 rp->model = 5;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700698 } else {
699 /* Couldn't detect size. Use default model 2. */
700 rp->model = 2;
701 rp->rows = 24;
702 rp->cols = 80;
703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 return rc;
706}
707
708static int
709raw3270_reset_device(struct raw3270 *rp)
710{
711 int rc;
712
Christoph Hellwigd330f932007-05-31 17:38:04 +0200713 mutex_lock(&raw3270_init_mutex);
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200714 memset(&rp->init_request, 0, sizeof(rp->init_request));
715 memset(&rp->init_data, 0, sizeof(rp->init_data));
716 /* Store reset data stream to init_data/init_request */
717 rp->init_data[0] = TW_KR;
718 INIT_LIST_HEAD(&rp->init_request.list);
719 rp->init_request.ccw.cmd_code = TC_EWRITEA;
720 rp->init_request.ccw.flags = CCW_FLAG_SLI;
721 rp->init_request.ccw.count = 1;
722 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 rp->view = &raw3270_init_view;
724 raw3270_init_view.dev = rp;
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200725 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200726 raw3270_init_view.dev = NULL;
727 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200728 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return rc;
730}
731
Richard Hitted3cb6f2005-10-30 15:00:10 -0800732int
733raw3270_reset(struct raw3270_view *view)
734{
735 struct raw3270 *rp;
736 int rc;
737
738 rp = view->dev;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200739 if (!rp || rp->view != view ||
740 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
Richard Hitted3cb6f2005-10-30 15:00:10 -0800741 rc = -EACCES;
742 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
743 rc = -ENODEV;
744 else
745 rc = raw3270_reset_device(view->dev);
746 return rc;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/*
750 * Setup new 3270 device.
751 */
752static int
753raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
754{
755 struct list_head *l;
756 struct raw3270 *tmp;
757 int minor;
758
759 memset(rp, 0, sizeof(struct raw3270));
760 /* Copy ebcdic -> ascii translation table. */
761 memcpy(ascebc, _ascebc, 256);
762 if (tubxcorrect) {
763 /* correct brackets and circumflex */
764 ascebc['['] = 0xad;
765 ascebc[']'] = 0xbd;
766 ascebc['^'] = 0xb0;
767 }
768 rp->ascebc = ascebc;
769
770 /* Set defaults. */
771 rp->rows = 24;
772 rp->cols = 80;
773
774 INIT_LIST_HEAD(&rp->req_queue);
775 INIT_LIST_HEAD(&rp->view_list);
776
777 /*
778 * Add device to list and find the smallest unused minor
Richard Hitted3cb6f2005-10-30 15:00:10 -0800779 * number for it. Note: there is no device with minor 0,
780 * see special case for fs3270.c:fs3270_open().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800782 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* Keep the list sorted. */
Richard Hitted3cb6f2005-10-30 15:00:10 -0800784 minor = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 rp->minor = -1;
786 list_for_each(l, &raw3270_devices) {
787 tmp = list_entry(l, struct raw3270, list);
788 if (tmp->minor > minor) {
789 rp->minor = minor;
790 __list_add(&rp->list, l->prev, l);
791 break;
792 }
793 minor++;
794 }
Richard Hitted3cb6f2005-10-30 15:00:10 -0800795 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 rp->minor = minor;
797 list_add_tail(&rp->list, &raw3270_devices);
798 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800799 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* No free minor number? Then give up. */
801 if (rp->minor == -1)
802 return -EUSERS;
803 rp->cdev = cdev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -0700804 dev_set_drvdata(&cdev->dev, rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 cdev->handler = raw3270_irq;
806 return 0;
807}
808
809#ifdef CONFIG_TN3270_CONSOLE
810/*
811 * Setup 3270 device configured as console.
812 */
Heiko Carstense62133b2007-07-27 12:29:13 +0200813struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct raw3270 *rp;
816 char *ascebc;
817 int rc;
818
Heiko Carstens33403dc2009-06-22 12:08:05 +0200819 rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
820 ascebc = kzalloc(256, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 rc = raw3270_setup_device(cdev, rp, ascebc);
822 if (rc)
823 return ERR_PTR(rc);
824 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
825 rc = raw3270_reset_device(rp);
826 if (rc)
827 return ERR_PTR(rc);
828 rc = raw3270_size_device(rp);
829 if (rc)
830 return ERR_PTR(rc);
831 rc = raw3270_reset_device(rp);
832 if (rc)
833 return ERR_PTR(rc);
834 set_bit(RAW3270_FLAGS_READY, &rp->flags);
835 return rp;
836}
837
838void
839raw3270_wait_cons_dev(struct raw3270 *rp)
840{
841 unsigned long flags;
842
843 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
844 wait_cons_dev();
845 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
846}
847
848#endif
849
850/*
851 * Create a 3270 device structure.
852 */
853static struct raw3270 *
854raw3270_create_device(struct ccw_device *cdev)
855{
856 struct raw3270 *rp;
857 char *ascebc;
858 int rc;
859
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200860 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!rp)
862 return ERR_PTR(-ENOMEM);
863 ascebc = kmalloc(256, GFP_KERNEL);
864 if (!ascebc) {
865 kfree(rp);
866 return ERR_PTR(-ENOMEM);
867 }
868 rc = raw3270_setup_device(cdev, rp, ascebc);
869 if (rc) {
870 kfree(rp->ascebc);
871 kfree(rp);
872 rp = ERR_PTR(rc);
873 }
874 /* Get reference to ccw_device structure. */
875 get_device(&cdev->dev);
876 return rp;
877}
878
879/*
880 * Activate a view.
881 */
882int
883raw3270_activate_view(struct raw3270_view *view)
884{
885 struct raw3270 *rp;
886 struct raw3270_view *oldview, *nv;
887 unsigned long flags;
888 int rc;
889
890 rp = view->dev;
891 if (!rp)
892 return -ENODEV;
893 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
894 if (rp->view == view)
895 rc = 0;
896 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
897 rc = -ENODEV;
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200898 else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
899 rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 else {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200901 oldview = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (rp->view) {
903 oldview = rp->view;
904 oldview->fn->deactivate(oldview);
905 }
906 rp->view = view;
907 rc = view->fn->activate(view);
908 if (rc) {
909 /* Didn't work. Try to reactivate the old view. */
910 rp->view = oldview;
911 if (!oldview || oldview->fn->activate(oldview) != 0) {
912 /* Didn't work as well. Try any other view. */
913 list_for_each_entry(nv, &rp->view_list, list)
914 if (nv != view && nv != oldview) {
915 rp->view = nv;
916 if (nv->fn->activate(nv) == 0)
917 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200918 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 }
921 }
922 }
923 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
924 return rc;
925}
926
927/*
928 * Deactivate current view.
929 */
930void
931raw3270_deactivate_view(struct raw3270_view *view)
932{
933 unsigned long flags;
934 struct raw3270 *rp;
935
936 rp = view->dev;
937 if (!rp)
938 return;
939 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
940 if (rp->view == view) {
941 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200942 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* Move deactivated view to end of list. */
944 list_del_init(&view->list);
945 list_add_tail(&view->list, &rp->view_list);
946 /* Try to activate another view. */
Martin Schwidefsky4b214a02009-06-16 10:30:47 +0200947 if (test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
948 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800949 list_for_each_entry(view, &rp->view_list, list) {
950 rp->view = view;
951 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200953 rp->view = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 }
957 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
958}
959
960/*
961 * Add view to device with minor "minor".
962 */
963int
964raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
965{
966 unsigned long flags;
967 struct raw3270 *rp;
968 int rc;
969
Richard Hitted3cb6f2005-10-30 15:00:10 -0800970 if (minor <= 0)
971 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800972 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 rc = -ENODEV;
974 list_for_each_entry(rp, &raw3270_devices, list) {
975 if (rp->minor != minor)
976 continue;
977 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
978 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
979 atomic_set(&view->ref_count, 2);
980 view->dev = rp;
981 view->fn = fn;
982 view->model = rp->model;
983 view->rows = rp->rows;
984 view->cols = rp->cols;
985 view->ascebc = rp->ascebc;
986 spin_lock_init(&view->lock);
Richard Hitted3cb6f2005-10-30 15:00:10 -0800987 list_add(&view->list, &rp->view_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 rc = 0;
989 }
990 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
991 break;
992 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800993 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return rc;
995}
996
997/*
998 * Find specific view of device with minor "minor".
999 */
1000struct raw3270_view *
1001raw3270_find_view(struct raw3270_fn *fn, int minor)
1002{
1003 struct raw3270 *rp;
1004 struct raw3270_view *view, *tmp;
1005 unsigned long flags;
1006
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001007 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 view = ERR_PTR(-ENODEV);
1009 list_for_each_entry(rp, &raw3270_devices, list) {
1010 if (rp->minor != minor)
1011 continue;
1012 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1013 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1014 view = ERR_PTR(-ENOENT);
1015 list_for_each_entry(tmp, &rp->view_list, list) {
1016 if (tmp->fn == fn) {
1017 raw3270_get_view(tmp);
1018 view = tmp;
1019 break;
1020 }
1021 }
1022 }
1023 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1024 break;
1025 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001026 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return view;
1028}
1029
1030/*
1031 * Remove view from device and free view structure via call to view->fn->free.
1032 */
1033void
1034raw3270_del_view(struct raw3270_view *view)
1035{
1036 unsigned long flags;
1037 struct raw3270 *rp;
1038 struct raw3270_view *nv;
1039
1040 rp = view->dev;
1041 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1042 if (rp->view == view) {
1043 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001044 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 list_del_init(&view->list);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001047 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
1048 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Try to activate another view. */
1050 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001051 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 rp->view = nv;
1053 break;
1054 }
1055 }
1056 }
1057 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1058 /* Wait for reference counter to drop to zero. */
1059 atomic_dec(&view->ref_count);
1060 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1061 if (view->fn->free)
1062 view->fn->free(view);
1063}
1064
1065/*
1066 * Remove a 3270 device structure.
1067 */
1068static void
1069raw3270_delete_device(struct raw3270 *rp)
1070{
1071 struct ccw_device *cdev;
1072
1073 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001074 mutex_lock(&raw3270_mutex);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001075 if (rp->clttydev && !IS_ERR(rp->clttydev))
Cornelia Huck7f021ce2007-10-22 12:52:42 +02001076 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001077 if (rp->cltubdev && !IS_ERR(rp->cltubdev))
Cornelia Huck7f021ce2007-10-22 12:52:42 +02001078 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001080 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* Disconnect from ccw_device. */
1083 cdev = rp->cdev;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001084 rp->cdev = NULL;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001085 dev_set_drvdata(&cdev->dev, NULL);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001086 cdev->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 /* Put ccw_device structure. */
1089 put_device(&cdev->dev);
1090
1091 /* Now free raw3270 structure. */
1092 kfree(rp->ascebc);
1093 kfree(rp);
1094}
1095
1096static int
1097raw3270_probe (struct ccw_device *cdev)
1098{
1099 return 0;
1100}
1101
1102/*
1103 * Additional attributes for a 3270 device
1104 */
1105static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001106raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001109 ((struct raw3270 *) dev_get_drvdata(dev))->model);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001111static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001114raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001117 ((struct raw3270 *) dev_get_drvdata(dev))->rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001119static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001122raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 return snprintf(buf, PAGE_SIZE, "%i\n",
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001125 ((struct raw3270 *) dev_get_drvdata(dev))->cols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001127static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129static struct attribute * raw3270_attrs[] = {
1130 &dev_attr_model.attr,
1131 &dev_attr_rows.attr,
1132 &dev_attr_columns.attr,
1133 NULL,
1134};
1135
1136static struct attribute_group raw3270_attr_group = {
1137 .attrs = raw3270_attrs,
1138};
1139
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001140static int raw3270_create_attributes(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001142 int rc;
1143
1144 rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1145 if (rc)
1146 goto out;
1147
Greg Kroah-Hartmanea9e42f2008-07-21 20:03:34 -07001148 rp->clttydev = device_create(class3270, &rp->cdev->dev,
1149 MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
1150 "tty%s", dev_name(&rp->cdev->dev));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001151 if (IS_ERR(rp->clttydev)) {
1152 rc = PTR_ERR(rp->clttydev);
1153 goto out_ttydev;
1154 }
1155
Greg Kroah-Hartmanea9e42f2008-07-21 20:03:34 -07001156 rp->cltubdev = device_create(class3270, &rp->cdev->dev,
1157 MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
1158 "tub%s", dev_name(&rp->cdev->dev));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001159 if (!IS_ERR(rp->cltubdev))
1160 goto out;
1161
1162 rc = PTR_ERR(rp->cltubdev);
Cornelia Huck7f021ce2007-10-22 12:52:42 +02001163 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001164
1165out_ttydev:
1166 sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1167out:
1168 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171/*
1172 * Notifier for device addition/removal
1173 */
1174struct raw3270_notifier {
1175 struct list_head list;
1176 void (*notifier)(int, int);
1177};
1178
Denis Chengc11ca972008-01-26 14:11:13 +01001179static LIST_HEAD(raw3270_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181int raw3270_register_notifier(void (*notifier)(int, int))
1182{
1183 struct raw3270_notifier *np;
1184 struct raw3270 *rp;
1185
1186 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1187 if (!np)
1188 return -ENOMEM;
1189 np->notifier = notifier;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001190 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 list_add_tail(&np->list, &raw3270_notifier);
1192 list_for_each_entry(rp, &raw3270_devices, list) {
1193 get_device(&rp->cdev->dev);
1194 notifier(rp->minor, 1);
1195 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001196 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198}
1199
1200void raw3270_unregister_notifier(void (*notifier)(int, int))
1201{
1202 struct raw3270_notifier *np;
1203
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001204 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 list_for_each_entry(np, &raw3270_notifier, list)
1206 if (np->notifier == notifier) {
1207 list_del(&np->list);
1208 kfree(np);
1209 break;
1210 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001211 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214/*
1215 * Set 3270 device online.
1216 */
1217static int
1218raw3270_set_online (struct ccw_device *cdev)
1219{
1220 struct raw3270 *rp;
1221 struct raw3270_notifier *np;
1222 int rc;
1223
1224 rp = raw3270_create_device(cdev);
1225 if (IS_ERR(rp))
1226 return PTR_ERR(rp);
1227 rc = raw3270_reset_device(rp);
1228 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001229 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 rc = raw3270_size_device(rp);
1231 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001232 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 rc = raw3270_reset_device(rp);
1234 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001235 goto failure;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001236 rc = raw3270_create_attributes(rp);
1237 if (rc)
1238 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 set_bit(RAW3270_FLAGS_READY, &rp->flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001240 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 list_for_each_entry(np, &raw3270_notifier, list)
1242 np->notifier(rp->minor, 1);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001243 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001245
1246failure:
1247 raw3270_delete_device(rp);
1248 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * Remove 3270 device structure.
1253 */
1254static void
1255raw3270_remove (struct ccw_device *cdev)
1256{
1257 unsigned long flags;
1258 struct raw3270 *rp;
1259 struct raw3270_view *v;
1260 struct raw3270_notifier *np;
1261
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001262 rp = dev_get_drvdata(&cdev->dev);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001263 /*
1264 * _remove is the opposite of _probe; it's probe that
1265 * should set up rp. raw3270_remove gets entered for
1266 * devices even if they haven't been varied online.
1267 * Thus, rp may validly be NULL here.
1268 */
1269 if (rp == NULL)
1270 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1272
1273 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1274
1275 /* Deactivate current view and remove all views. */
1276 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1277 if (rp->view) {
1278 rp->view->fn->deactivate(rp->view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001279 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 while (!list_empty(&rp->view_list)) {
1282 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1283 if (v->fn->release)
1284 v->fn->release(v);
1285 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1286 raw3270_del_view(v);
1287 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1288 }
1289 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1290
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001291 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 list_for_each_entry(np, &raw3270_notifier, list)
1293 np->notifier(rp->minor, 0);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001294 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* Reset 3270 device. */
1297 raw3270_reset_device(rp);
1298 /* And finally remove it. */
1299 raw3270_delete_device(rp);
1300}
1301
1302/*
1303 * Set 3270 device offline.
1304 */
1305static int
1306raw3270_set_offline (struct ccw_device *cdev)
1307{
1308 struct raw3270 *rp;
1309
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001310 rp = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1312 return -EBUSY;
1313 raw3270_remove(cdev);
1314 return 0;
1315}
1316
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001317static int raw3270_pm_stop(struct ccw_device *cdev)
1318{
1319 struct raw3270 *rp;
1320 struct raw3270_view *view;
1321 unsigned long flags;
1322
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001323 rp = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001324 if (!rp)
1325 return 0;
1326 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1327 if (rp->view)
1328 rp->view->fn->deactivate(rp->view);
1329 if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
1330 /*
1331 * Release tty and fullscreen for all non-console
1332 * devices.
1333 */
1334 list_for_each_entry(view, &rp->view_list, list) {
1335 if (view->fn->release)
1336 view->fn->release(view);
1337 }
1338 }
1339 set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1340 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1341 return 0;
1342}
1343
1344static int raw3270_pm_start(struct ccw_device *cdev)
1345{
1346 struct raw3270 *rp;
1347 unsigned long flags;
1348
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001349 rp = dev_get_drvdata(&cdev->dev);
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001350 if (!rp)
1351 return 0;
1352 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1353 clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1354 if (rp->view)
1355 rp->view->fn->activate(rp->view);
1356 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1357 return 0;
1358}
1359
1360void raw3270_pm_unfreeze(struct raw3270_view *view)
1361{
Sebastian Ott34483ca2009-10-06 10:33:56 +02001362#ifdef CONFIG_TN3270_CONSOLE
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001363 struct raw3270 *rp;
1364
1365 rp = view->dev;
1366 if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
1367 ccw_device_force_console();
Sebastian Ott34483ca2009-10-06 10:33:56 +02001368#endif
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371static struct ccw_device_id raw3270_id[] = {
1372 { CCW_DEVICE(0x3270, 0) },
1373 { CCW_DEVICE(0x3271, 0) },
1374 { CCW_DEVICE(0x3272, 0) },
1375 { CCW_DEVICE(0x3273, 0) },
1376 { CCW_DEVICE(0x3274, 0) },
1377 { CCW_DEVICE(0x3275, 0) },
1378 { CCW_DEVICE(0x3276, 0) },
1379 { CCW_DEVICE(0x3277, 0) },
1380 { CCW_DEVICE(0x3278, 0) },
1381 { CCW_DEVICE(0x3279, 0) },
1382 { CCW_DEVICE(0x3174, 0) },
1383 { /* end of list */ },
1384};
1385
1386static struct ccw_driver raw3270_ccw_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +01001387 .driver = {
1388 .name = "3270",
1389 .owner = THIS_MODULE,
1390 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 .ids = raw3270_id,
1392 .probe = &raw3270_probe,
1393 .remove = &raw3270_remove,
1394 .set_online = &raw3270_set_online,
1395 .set_offline = &raw3270_set_offline,
Martin Schwidefsky4b214a02009-06-16 10:30:47 +02001396 .freeze = &raw3270_pm_stop,
1397 .thaw = &raw3270_pm_start,
1398 .restore = &raw3270_pm_start,
Peter Oberparleiterde400d62011-10-30 15:16:04 +01001399 .int_class = IOINT_C70,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400};
1401
1402static int
1403raw3270_init(void)
1404{
1405 struct raw3270 *rp;
1406 int rc;
1407
1408 if (raw3270_registered)
1409 return 0;
1410 raw3270_registered = 1;
1411 rc = ccw_driver_register(&raw3270_ccw_driver);
1412 if (rc == 0) {
1413 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001414 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001415 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 list_for_each_entry(rp, &raw3270_devices, list) {
1417 get_device(&rp->cdev->dev);
1418 raw3270_create_attributes(rp);
1419 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001420 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422 return rc;
1423}
1424
1425static void
1426raw3270_exit(void)
1427{
1428 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001429 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432MODULE_LICENSE("GPL");
1433
1434module_init(raw3270_init);
1435module_exit(raw3270_exit);
1436
1437EXPORT_SYMBOL(raw3270_request_alloc);
1438EXPORT_SYMBOL(raw3270_request_free);
1439EXPORT_SYMBOL(raw3270_request_reset);
1440EXPORT_SYMBOL(raw3270_request_set_cmd);
1441EXPORT_SYMBOL(raw3270_request_add_data);
1442EXPORT_SYMBOL(raw3270_request_set_data);
1443EXPORT_SYMBOL(raw3270_request_set_idal);
1444EXPORT_SYMBOL(raw3270_buffer_address);
1445EXPORT_SYMBOL(raw3270_add_view);
1446EXPORT_SYMBOL(raw3270_del_view);
1447EXPORT_SYMBOL(raw3270_find_view);
1448EXPORT_SYMBOL(raw3270_activate_view);
1449EXPORT_SYMBOL(raw3270_deactivate_view);
1450EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001451EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001453EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454EXPORT_SYMBOL(raw3270_register_notifier);
1455EXPORT_SYMBOL(raw3270_unregister_notifier);
1456EXPORT_SYMBOL(raw3270_wait_queue);