blob: 2edd5fb6d3dc1e8a4894e0ff5c8557ccdf2de838 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/raw3270.c
3 * IBM/3270 Driver - core functions.
4 *
5 * Author(s):
6 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/bootmem.h>
12#include <linux/module.h>
13#include <linux/err.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/list.h>
17#include <linux/slab.h>
18#include <linux/types.h>
19#include <linux/wait.h>
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
23#include <asm/ebcdic.h>
Michael Holzheu0a87c5c2007-08-22 13:51:40 +020024#include <asm/diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "raw3270.h"
27
Richard Hitted3cb6f2005-10-30 15:00:10 -080028#include <linux/major.h>
29#include <linux/kdev_t.h>
30#include <linux/device.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080031#include <linux/mutex.h>
Richard Hitted3cb6f2005-10-30 15:00:10 -080032
Heiko Carstens2b67fc42007-02-05 21:16:47 +010033static struct class *class3270;
Richard Hitted3cb6f2005-10-30 15:00:10 -080034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* The main 3270 data structure. */
36struct raw3270 {
37 struct list_head list;
38 struct ccw_device *cdev;
39 int minor;
40
41 short model, rows, cols;
42 unsigned long flags;
43
44 struct list_head req_queue; /* Request queue. */
45 struct list_head view_list; /* List of available views. */
46 struct raw3270_view *view; /* Active view. */
47
48 struct timer_list timer; /* Device timer. */
49
50 unsigned char *ascebc; /* ascii -> ebcdic table */
Richard Hitted3cb6f2005-10-30 15:00:10 -080051 struct class_device *clttydev; /* 3270-class tty device ptr */
52 struct class_device *cltubdev; /* 3270-class tub device ptr */
Martin Schwidefsky132fab12006-06-29 14:57:39 +020053
54 struct raw3270_request init_request;
55 unsigned char init_data[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58/* raw3270->flags */
59#define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
60#define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
61#define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
62#define RAW3270_FLAGS_READY 4 /* Device is useable by views */
63#define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
64
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. */
69static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
70
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));
156 if (!rq)
157 return ERR_PTR(-ENOMEM);
158 memset(rq, 0, sizeof(struct raw3270_request));
159
160 /* alloc output buffer. */
161 if (size > 0) {
162 rq->buffer = alloc_bootmem_low(size);
163 if (!rq->buffer) {
164 free_bootmem((unsigned long) rq,
165 sizeof(struct raw3270));
166 return ERR_PTR(-ENOMEM);
167 }
168 }
169 rq->size = size;
170 INIT_LIST_HEAD(&rq->list);
171
172 /*
173 * Setup ccw.
174 */
175 rq->ccw.cda = __pa(rq->buffer);
176 rq->ccw.flags = CCW_FLAG_SLI;
177
178 return rq;
179}
180#endif
181
182/*
183 * Free 3270 ccw request
184 */
185void
186raw3270_request_free (struct raw3270_request *rq)
187{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800188 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 kfree(rq);
190}
191
192/*
193 * Reset request to initial state.
194 */
195void
196raw3270_request_reset(struct raw3270_request *rq)
197{
198 BUG_ON(!list_empty(&rq->list));
199 rq->ccw.cmd_code = 0;
200 rq->ccw.count = 0;
201 rq->ccw.cda = __pa(rq->buffer);
202 rq->ccw.flags = CCW_FLAG_SLI;
203 rq->rescnt = 0;
204 rq->rc = 0;
205}
206
207/*
208 * Set command code to ccw of a request.
209 */
210void
211raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
212{
213 rq->ccw.cmd_code = cmd;
214}
215
216/*
217 * Add data fragment to output buffer.
218 */
219int
220raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
221{
222 if (size + rq->ccw.count > rq->size)
223 return -E2BIG;
224 memcpy(rq->buffer + rq->ccw.count, data, size);
225 rq->ccw.count += size;
226 return 0;
227}
228
229/*
230 * Set address/length pair to ccw of a request.
231 */
232void
233raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
234{
235 rq->ccw.cda = __pa(data);
236 rq->ccw.count = size;
237}
238
239/*
240 * Set idal buffer to ccw of a request.
241 */
242void
243raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
244{
245 rq->ccw.cda = __pa(ib->data);
246 rq->ccw.count = ib->size;
247 rq->ccw.flags |= CCW_FLAG_IDA;
248}
249
250/*
251 * Stop running ccw.
252 */
253static int
254raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
255{
256 int retries;
257 int rc;
258
259 if (raw3270_request_final(rq))
260 return 0;
261 /* Check if interrupt has already been processed */
262 for (retries = 0; retries < 5; retries++) {
263 if (retries < 2)
264 rc = ccw_device_halt(rp->cdev, (long) rq);
265 else
266 rc = ccw_device_clear(rp->cdev, (long) rq);
267 if (rc == 0)
268 break; /* termination successful */
269 }
270 return rc;
271}
272
273static int
274raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
275{
276 unsigned long flags;
277 int rc;
278
279 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
280 rc = raw3270_halt_io_nolock(rp, rq);
281 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
282 return rc;
283}
284
285/*
286 * Add the request to the request queue, try to start it if the
287 * 3270 device is idle. Return without waiting for end of i/o.
288 */
289static int
290__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
291 struct raw3270_request *rq)
292{
293 rq->view = view;
294 raw3270_get_view(view);
295 if (list_empty(&rp->req_queue) &&
296 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
297 /* No other requests are on the queue. Start this one. */
298 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
299 (unsigned long) rq, 0, 0);
300 if (rq->rc) {
301 raw3270_put_view(view);
302 return rq->rc;
303 }
304 }
305 list_add_tail(&rq->list, &rp->req_queue);
306 return 0;
307}
308
309int
310raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
311{
312 unsigned long flags;
313 struct raw3270 *rp;
314 int rc;
315
316 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
317 rp = view->dev;
318 if (!rp || rp->view != view)
319 rc = -EACCES;
320 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
321 rc = -ENODEV;
322 else
323 rc = __raw3270_start(rp, view, rq);
324 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
325 return rc;
326}
327
328int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800329raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
330{
331 struct raw3270 *rp;
332 int rc;
333
334 rp = view->dev;
335 if (!rp || rp->view != view)
336 rc = -EACCES;
337 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
338 rc = -ENODEV;
339 else
340 rc = __raw3270_start(rp, view, rq);
341 return rc;
342}
343
344int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
346{
347 struct raw3270 *rp;
348
349 rp = view->dev;
350 rq->view = view;
351 raw3270_get_view(view);
352 list_add_tail(&rq->list, &rp->req_queue);
353 return 0;
354}
355
356/*
357 * 3270 interrupt routine, called from the ccw_device layer
358 */
359static void
360raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
361{
362 struct raw3270 *rp;
363 struct raw3270_view *view;
364 struct raw3270_request *rq;
365 int rc;
366
367 rp = (struct raw3270 *) cdev->dev.driver_data;
368 if (!rp)
369 return;
370 rq = (struct raw3270_request *) intparm;
371 view = rq ? rq->view : rp->view;
372
373 if (IS_ERR(irb))
374 rc = RAW3270_IO_RETRY;
375 else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
376 rq->rc = -EIO;
377 rc = RAW3270_IO_DONE;
378 } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
379 DEV_STAT_UNIT_EXCEP)) {
380 /* Handle CE-DE-UE and subsequent UDE */
381 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
382 rc = RAW3270_IO_BUSY;
383 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
384 /* Wait for UDE if busy flag is set. */
385 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
386 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
387 /* Got it, now retry. */
388 rc = RAW3270_IO_RETRY;
389 } else
390 rc = RAW3270_IO_BUSY;
391 } else if (view)
392 rc = view->fn->intv(view, rq, irb);
393 else
394 rc = RAW3270_IO_DONE;
395
396 switch (rc) {
397 case RAW3270_IO_DONE:
398 break;
399 case RAW3270_IO_BUSY:
400 /*
401 * Intervention required by the operator. We have to wait
402 * for unsolicited device end.
403 */
404 return;
405 case RAW3270_IO_RETRY:
406 if (!rq)
407 break;
408 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
409 (unsigned long) rq, 0, 0);
410 if (rq->rc == 0)
411 return; /* Sucessfully restarted. */
412 break;
413 case RAW3270_IO_STOP:
414 if (!rq)
415 break;
416 raw3270_halt_io_nolock(rp, rq);
417 rq->rc = -EIO;
418 break;
419 default:
420 BUG();
421 }
422 if (rq) {
423 BUG_ON(list_empty(&rq->list));
424 /* The request completed, remove from queue and do callback. */
425 list_del_init(&rq->list);
426 if (rq->callback)
427 rq->callback(rq, rq->callback_data);
428 /* Do put_device for get_device in raw3270_start. */
429 raw3270_put_view(view);
430 }
431 /*
432 * Try to start each request on request queue until one is
433 * started successful.
434 */
435 while (!list_empty(&rp->req_queue)) {
436 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
437 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
438 (unsigned long) rq, 0, 0);
439 if (rq->rc == 0)
440 break;
441 /* Start failed. Remove request and do callback. */
442 list_del_init(&rq->list);
443 if (rq->callback)
444 rq->callback(rq, rq->callback_data);
445 /* Do put_device for get_device in raw3270_start. */
446 raw3270_put_view(view);
447 }
448}
449
450/*
451 * Size sensing.
452 */
453
454struct raw3270_ua { /* Query Reply structure for Usable Area */
455 struct { /* Usable Area Query Reply Base */
456 short l; /* Length of this structured field */
457 char sfid; /* 0x81 if Query Reply */
458 char qcode; /* 0x81 if Usable Area */
459 char flags0;
460 char flags1;
461 short w; /* Width of usable area */
462 short h; /* Heigth of usavle area */
463 char units; /* 0x00:in; 0x01:mm */
464 int xr;
465 int yr;
466 char aw;
467 char ah;
468 short buffsz; /* Character buffer size, bytes */
469 char xmin;
470 char ymin;
471 char xmax;
472 char ymax;
473 } __attribute__ ((packed)) uab;
474 struct { /* Alternate Usable Area Self-Defining Parameter */
475 char l; /* Length of this Self-Defining Parm */
476 char sdpid; /* 0x02 if Alternate Usable Area */
477 char res;
478 char auaid; /* 0x01 is Id for the A U A */
479 short wauai; /* Width of AUAi */
480 short hauai; /* Height of AUAi */
481 char auaunits; /* 0x00:in, 0x01:mm */
482 int auaxr;
483 int auayr;
484 char awauai;
485 char ahauai;
486 } __attribute__ ((packed)) aua;
487} __attribute__ ((packed));
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static struct diag210 raw3270_init_diag210;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200490static DEFINE_MUTEX(raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492static int
493raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
494 struct irb *irb)
495{
496 /*
497 * Unit-Check Processing:
498 * Expect Command Reject or Intervention Required.
499 */
500 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
501 /* Request finished abnormally. */
502 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
503 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
504 return RAW3270_IO_BUSY;
505 }
506 }
507 if (rq) {
508 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
509 if (irb->ecw[0] & SNS0_CMD_REJECT)
510 rq->rc = -EOPNOTSUPP;
511 else
512 rq->rc = -EIO;
513 } else
514 /* Request finished normally. Copy residual count. */
515 rq->rescnt = irb->scsw.count;
516 }
517 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
518 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
519 wake_up(&raw3270_wait_queue);
520 }
521 return RAW3270_IO_DONE;
522}
523
524static struct raw3270_fn raw3270_init_fn = {
525 .intv = raw3270_init_irq
526};
527
528static struct raw3270_view raw3270_init_view = {
529 .fn = &raw3270_init_fn
530};
531
532/*
533 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
534 * Wait for end of request. The request must have been started
535 * with raw3270_start, rc = 0. The device lock may NOT have been
536 * released between calling raw3270_start and raw3270_wait.
537 */
538static void
539raw3270_wake_init(struct raw3270_request *rq, void *data)
540{
541 wake_up((wait_queue_head_t *) data);
542}
543
544/*
545 * Special wait function that can cope with console initialization.
546 */
547static int
548raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
549 struct raw3270_request *rq)
550{
551 unsigned long flags;
552 wait_queue_head_t wq;
553 int rc;
554
555#ifdef CONFIG_TN3270_CONSOLE
556 if (raw3270_registered == 0) {
557 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200558 rq->callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 rc = __raw3270_start(rp, view, rq);
560 if (rc == 0)
561 while (!raw3270_request_final(rq)) {
562 wait_cons_dev();
563 barrier();
564 }
565 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
566 return rq->rc;
567 }
568#endif
569 init_waitqueue_head(&wq);
570 rq->callback = raw3270_wake_init;
571 rq->callback_data = &wq;
572 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
573 rc = __raw3270_start(rp, view, rq);
574 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
575 if (rc)
576 return rc;
577 /* Now wait for the completion. */
578 rc = wait_event_interruptible(wq, raw3270_request_final(rq));
579 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
580 raw3270_halt_io(view->dev, rq);
581 /* No wait for the halt to complete. */
582 wait_event(wq, raw3270_request_final(rq));
583 return -ERESTARTSYS;
584 }
585 return rq->rc;
586}
587
588static int
589__raw3270_size_device_vm(struct raw3270 *rp)
590{
591 int rc, model;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200592 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200594 ccw_device_get_id(rp->cdev, &dev_id);
595 raw3270_init_diag210.vrdcdvno = dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 raw3270_init_diag210.vrdclen = sizeof(struct diag210);
597 rc = diag210(&raw3270_init_diag210);
598 if (rc)
599 return rc;
600 model = raw3270_init_diag210.vrdccrmd;
601 switch (model) {
602 case 2:
603 rp->model = model;
604 rp->rows = 24;
605 rp->cols = 80;
606 break;
607 case 3:
608 rp->model = model;
609 rp->rows = 32;
610 rp->cols = 80;
611 break;
612 case 4:
613 rp->model = model;
614 rp->rows = 43;
615 rp->cols = 80;
616 break;
617 case 5:
618 rp->model = model;
619 rp->rows = 27;
620 rp->cols = 132;
621 break;
622 default:
623 printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
624 rc = -EOPNOTSUPP;
625 break;
626 }
627 return rc;
628}
629
630static int
631__raw3270_size_device(struct raw3270 *rp)
632{
633 static const unsigned char wbuf[] =
634 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
635 struct raw3270_ua *uap;
636 unsigned short count;
637 int rc;
638
639 /*
640 * To determine the size of the 3270 device we need to do:
641 * 1) send a 'read partition' data stream to the device
642 * 2) wait for the attn interrupt that preceeds the query reply
643 * 3) do a read modified to get the query reply
644 * To make things worse we have to cope with intervention
645 * required (3270 device switched to 'stand-by') and command
646 * rejects (old devices that can't do 'read partition').
647 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200648 memset(&rp->init_request, 0, sizeof(rp->init_request));
649 memset(&rp->init_data, 0, 256);
650 /* Store 'read partition' data stream to init_data */
651 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
652 INIT_LIST_HEAD(&rp->init_request.list);
653 rp->init_request.ccw.cmd_code = TC_WRITESF;
654 rp->init_request.ccw.flags = CCW_FLAG_SLI;
655 rp->init_request.ccw.count = sizeof(wbuf);
656 rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200658 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700659 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* Wait for attention interrupt. */
664#ifdef CONFIG_TN3270_CONSOLE
665 if (raw3270_registered == 0) {
666 unsigned long flags;
667
668 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
669 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
670 wait_cons_dev();
671 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
672 } else
673#endif
674 rc = wait_event_interruptible(raw3270_wait_queue,
675 test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
676 if (rc)
677 return rc;
678
679 /*
680 * The device accepted the 'read partition' command. Now
681 * set up a read ccw and issue it.
682 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200683 rp->init_request.ccw.cmd_code = TC_READMOD;
684 rp->init_request.ccw.flags = CCW_FLAG_SLI;
685 rp->init_request.ccw.count = sizeof(rp->init_data);
686 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
687 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (rc)
689 return rc;
690 /* Got a Query Reply */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200691 count = sizeof(rp->init_data) - rp->init_request.rescnt;
692 uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* Paranoia check. */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200694 if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EOPNOTSUPP;
696 /* Copy rows/columns of default Usable Area */
697 rp->rows = uap->uab.h;
698 rp->cols = uap->uab.w;
699 /* Check for 14 bit addressing */
700 if ((uap->uab.flags0 & 0x0d) == 0x01)
701 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
702 /* Check for Alternate Usable Area */
703 if (uap->uab.l == sizeof(struct raw3270_ua) &&
704 uap->aua.sdpid == 0x02) {
705 rp->rows = uap->aua.hauai;
706 rp->cols = uap->aua.wauai;
707 }
708 return 0;
709}
710
711static int
712raw3270_size_device(struct raw3270 *rp)
713{
714 int rc;
715
Christoph Hellwigd330f932007-05-31 17:38:04 +0200716 mutex_lock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 rp->view = &raw3270_init_view;
718 raw3270_init_view.dev = rp;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700719 if (MACHINE_IS_VM)
720 rc = __raw3270_size_device_vm(rp);
721 else
722 rc = __raw3270_size_device(rp);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200723 raw3270_init_view.dev = NULL;
724 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200725 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (rc == 0) { /* Found something. */
727 /* Try to find a model. */
728 rp->model = 0;
729 if (rp->rows == 24 && rp->cols == 80)
730 rp->model = 2;
731 if (rp->rows == 32 && rp->cols == 80)
732 rp->model = 3;
733 if (rp->rows == 43 && rp->cols == 80)
734 rp->model = 4;
735 if (rp->rows == 27 && rp->cols == 132)
736 rp->model = 5;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700737 } else {
738 /* Couldn't detect size. Use default model 2. */
739 rp->model = 2;
740 rp->rows = 24;
741 rp->cols = 80;
742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 return rc;
745}
746
747static int
748raw3270_reset_device(struct raw3270 *rp)
749{
750 int rc;
751
Christoph Hellwigd330f932007-05-31 17:38:04 +0200752 mutex_lock(&raw3270_init_mutex);
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200753 memset(&rp->init_request, 0, sizeof(rp->init_request));
754 memset(&rp->init_data, 0, sizeof(rp->init_data));
755 /* Store reset data stream to init_data/init_request */
756 rp->init_data[0] = TW_KR;
757 INIT_LIST_HEAD(&rp->init_request.list);
758 rp->init_request.ccw.cmd_code = TC_EWRITEA;
759 rp->init_request.ccw.flags = CCW_FLAG_SLI;
760 rp->init_request.ccw.count = 1;
761 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 rp->view = &raw3270_init_view;
763 raw3270_init_view.dev = rp;
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200764 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200765 raw3270_init_view.dev = NULL;
766 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200767 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return rc;
769}
770
Richard Hitted3cb6f2005-10-30 15:00:10 -0800771int
772raw3270_reset(struct raw3270_view *view)
773{
774 struct raw3270 *rp;
775 int rc;
776
777 rp = view->dev;
778 if (!rp || rp->view != view)
779 rc = -EACCES;
780 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
781 rc = -ENODEV;
782 else
783 rc = raw3270_reset_device(view->dev);
784 return rc;
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
788 * Setup new 3270 device.
789 */
790static int
791raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
792{
793 struct list_head *l;
794 struct raw3270 *tmp;
795 int minor;
796
797 memset(rp, 0, sizeof(struct raw3270));
798 /* Copy ebcdic -> ascii translation table. */
799 memcpy(ascebc, _ascebc, 256);
800 if (tubxcorrect) {
801 /* correct brackets and circumflex */
802 ascebc['['] = 0xad;
803 ascebc[']'] = 0xbd;
804 ascebc['^'] = 0xb0;
805 }
806 rp->ascebc = ascebc;
807
808 /* Set defaults. */
809 rp->rows = 24;
810 rp->cols = 80;
811
812 INIT_LIST_HEAD(&rp->req_queue);
813 INIT_LIST_HEAD(&rp->view_list);
814
815 /*
816 * Add device to list and find the smallest unused minor
Richard Hitted3cb6f2005-10-30 15:00:10 -0800817 * number for it. Note: there is no device with minor 0,
818 * see special case for fs3270.c:fs3270_open().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800820 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Keep the list sorted. */
Richard Hitted3cb6f2005-10-30 15:00:10 -0800822 minor = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 rp->minor = -1;
824 list_for_each(l, &raw3270_devices) {
825 tmp = list_entry(l, struct raw3270, list);
826 if (tmp->minor > minor) {
827 rp->minor = minor;
828 __list_add(&rp->list, l->prev, l);
829 break;
830 }
831 minor++;
832 }
Richard Hitted3cb6f2005-10-30 15:00:10 -0800833 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 rp->minor = minor;
835 list_add_tail(&rp->list, &raw3270_devices);
836 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800837 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* No free minor number? Then give up. */
839 if (rp->minor == -1)
840 return -EUSERS;
841 rp->cdev = cdev;
842 cdev->dev.driver_data = rp;
843 cdev->handler = raw3270_irq;
844 return 0;
845}
846
847#ifdef CONFIG_TN3270_CONSOLE
848/*
849 * Setup 3270 device configured as console.
850 */
Heiko Carstense62133b2007-07-27 12:29:13 +0200851struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct raw3270 *rp;
854 char *ascebc;
855 int rc;
856
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200857 rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ascebc = (char *) alloc_bootmem(256);
859 rc = raw3270_setup_device(cdev, rp, ascebc);
860 if (rc)
861 return ERR_PTR(rc);
862 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
863 rc = raw3270_reset_device(rp);
864 if (rc)
865 return ERR_PTR(rc);
866 rc = raw3270_size_device(rp);
867 if (rc)
868 return ERR_PTR(rc);
869 rc = raw3270_reset_device(rp);
870 if (rc)
871 return ERR_PTR(rc);
872 set_bit(RAW3270_FLAGS_READY, &rp->flags);
873 return rp;
874}
875
876void
877raw3270_wait_cons_dev(struct raw3270 *rp)
878{
879 unsigned long flags;
880
881 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
882 wait_cons_dev();
883 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
884}
885
886#endif
887
888/*
889 * Create a 3270 device structure.
890 */
891static struct raw3270 *
892raw3270_create_device(struct ccw_device *cdev)
893{
894 struct raw3270 *rp;
895 char *ascebc;
896 int rc;
897
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200898 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (!rp)
900 return ERR_PTR(-ENOMEM);
901 ascebc = kmalloc(256, GFP_KERNEL);
902 if (!ascebc) {
903 kfree(rp);
904 return ERR_PTR(-ENOMEM);
905 }
906 rc = raw3270_setup_device(cdev, rp, ascebc);
907 if (rc) {
908 kfree(rp->ascebc);
909 kfree(rp);
910 rp = ERR_PTR(rc);
911 }
912 /* Get reference to ccw_device structure. */
913 get_device(&cdev->dev);
914 return rp;
915}
916
917/*
918 * Activate a view.
919 */
920int
921raw3270_activate_view(struct raw3270_view *view)
922{
923 struct raw3270 *rp;
924 struct raw3270_view *oldview, *nv;
925 unsigned long flags;
926 int rc;
927
928 rp = view->dev;
929 if (!rp)
930 return -ENODEV;
931 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
932 if (rp->view == view)
933 rc = 0;
934 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
935 rc = -ENODEV;
936 else {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200937 oldview = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (rp->view) {
939 oldview = rp->view;
940 oldview->fn->deactivate(oldview);
941 }
942 rp->view = view;
943 rc = view->fn->activate(view);
944 if (rc) {
945 /* Didn't work. Try to reactivate the old view. */
946 rp->view = oldview;
947 if (!oldview || oldview->fn->activate(oldview) != 0) {
948 /* Didn't work as well. Try any other view. */
949 list_for_each_entry(nv, &rp->view_list, list)
950 if (nv != view && nv != oldview) {
951 rp->view = nv;
952 if (nv->fn->activate(nv) == 0)
953 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200954 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 }
957 }
958 }
959 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
960 return rc;
961}
962
963/*
964 * Deactivate current view.
965 */
966void
967raw3270_deactivate_view(struct raw3270_view *view)
968{
969 unsigned long flags;
970 struct raw3270 *rp;
971
972 rp = view->dev;
973 if (!rp)
974 return;
975 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
976 if (rp->view == view) {
977 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200978 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Move deactivated view to end of list. */
980 list_del_init(&view->list);
981 list_add_tail(&view->list, &rp->view_list);
982 /* Try to activate another view. */
983 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800984 list_for_each_entry(view, &rp->view_list, list) {
985 rp->view = view;
986 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200988 rp->view = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 }
992 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
993}
994
995/*
996 * Add view to device with minor "minor".
997 */
998int
999raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
1000{
1001 unsigned long flags;
1002 struct raw3270 *rp;
1003 int rc;
1004
Richard Hitted3cb6f2005-10-30 15:00:10 -08001005 if (minor <= 0)
1006 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001007 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 rc = -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 atomic_set(&view->ref_count, 2);
1015 view->dev = rp;
1016 view->fn = fn;
1017 view->model = rp->model;
1018 view->rows = rp->rows;
1019 view->cols = rp->cols;
1020 view->ascebc = rp->ascebc;
1021 spin_lock_init(&view->lock);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001022 list_add(&view->list, &rp->view_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 rc = 0;
1024 }
1025 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1026 break;
1027 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001028 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return rc;
1030}
1031
1032/*
1033 * Find specific view of device with minor "minor".
1034 */
1035struct raw3270_view *
1036raw3270_find_view(struct raw3270_fn *fn, int minor)
1037{
1038 struct raw3270 *rp;
1039 struct raw3270_view *view, *tmp;
1040 unsigned long flags;
1041
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001042 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 view = ERR_PTR(-ENODEV);
1044 list_for_each_entry(rp, &raw3270_devices, list) {
1045 if (rp->minor != minor)
1046 continue;
1047 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1048 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1049 view = ERR_PTR(-ENOENT);
1050 list_for_each_entry(tmp, &rp->view_list, list) {
1051 if (tmp->fn == fn) {
1052 raw3270_get_view(tmp);
1053 view = tmp;
1054 break;
1055 }
1056 }
1057 }
1058 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1059 break;
1060 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001061 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return view;
1063}
1064
1065/*
1066 * Remove view from device and free view structure via call to view->fn->free.
1067 */
1068void
1069raw3270_del_view(struct raw3270_view *view)
1070{
1071 unsigned long flags;
1072 struct raw3270 *rp;
1073 struct raw3270_view *nv;
1074
1075 rp = view->dev;
1076 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1077 if (rp->view == view) {
1078 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001079 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 list_del_init(&view->list);
1082 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1083 /* Try to activate another view. */
1084 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001085 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 rp->view = nv;
1087 break;
1088 }
1089 }
1090 }
1091 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1092 /* Wait for reference counter to drop to zero. */
1093 atomic_dec(&view->ref_count);
1094 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1095 if (view->fn->free)
1096 view->fn->free(view);
1097}
1098
1099/*
1100 * Remove a 3270 device structure.
1101 */
1102static void
1103raw3270_delete_device(struct raw3270 *rp)
1104{
1105 struct ccw_device *cdev;
1106
1107 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001108 mutex_lock(&raw3270_mutex);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001109 if (rp->clttydev && !IS_ERR(rp->clttydev))
Richard Hitted3cb6f2005-10-30 15:00:10 -08001110 class_device_destroy(class3270,
1111 MKDEV(IBM_TTY3270_MAJOR, rp->minor));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001112 if (rp->cltubdev && !IS_ERR(rp->cltubdev))
Richard Hitted3cb6f2005-10-30 15:00:10 -08001113 class_device_destroy(class3270,
1114 MKDEV(IBM_FS3270_MAJOR, rp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001116 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /* Disconnect from ccw_device. */
1119 cdev = rp->cdev;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001120 rp->cdev = NULL;
1121 cdev->dev.driver_data = NULL;
1122 cdev->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* Put ccw_device structure. */
1125 put_device(&cdev->dev);
1126
1127 /* Now free raw3270 structure. */
1128 kfree(rp->ascebc);
1129 kfree(rp);
1130}
1131
1132static int
1133raw3270_probe (struct ccw_device *cdev)
1134{
1135 return 0;
1136}
1137
1138/*
1139 * Additional attributes for a 3270 device
1140 */
1141static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001142raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 return snprintf(buf, PAGE_SIZE, "%i\n",
1145 ((struct raw3270 *) dev->driver_data)->model);
1146}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001147static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001150raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 return snprintf(buf, PAGE_SIZE, "%i\n",
1153 ((struct raw3270 *) dev->driver_data)->rows);
1154}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001155static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001158raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 return snprintf(buf, PAGE_SIZE, "%i\n",
1161 ((struct raw3270 *) dev->driver_data)->cols);
1162}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001163static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165static struct attribute * raw3270_attrs[] = {
1166 &dev_attr_model.attr,
1167 &dev_attr_rows.attr,
1168 &dev_attr_columns.attr,
1169 NULL,
1170};
1171
1172static struct attribute_group raw3270_attr_group = {
1173 .attrs = raw3270_attrs,
1174};
1175
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001176static int raw3270_create_attributes(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001178 int rc;
1179
1180 rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1181 if (rc)
1182 goto out;
1183
1184 rp->clttydev = class_device_create(class3270, NULL,
1185 MKDEV(IBM_TTY3270_MAJOR, rp->minor),
1186 &rp->cdev->dev, "tty%s",
1187 rp->cdev->dev.bus_id);
1188 if (IS_ERR(rp->clttydev)) {
1189 rc = PTR_ERR(rp->clttydev);
1190 goto out_ttydev;
1191 }
1192
1193 rp->cltubdev = class_device_create(class3270, NULL,
1194 MKDEV(IBM_FS3270_MAJOR, rp->minor),
1195 &rp->cdev->dev, "tub%s",
1196 rp->cdev->dev.bus_id);
1197 if (!IS_ERR(rp->cltubdev))
1198 goto out;
1199
1200 rc = PTR_ERR(rp->cltubdev);
1201 class_device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1202
1203out_ttydev:
1204 sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1205out:
1206 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209/*
1210 * Notifier for device addition/removal
1211 */
1212struct raw3270_notifier {
1213 struct list_head list;
1214 void (*notifier)(int, int);
1215};
1216
1217static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
1218
1219int raw3270_register_notifier(void (*notifier)(int, int))
1220{
1221 struct raw3270_notifier *np;
1222 struct raw3270 *rp;
1223
1224 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1225 if (!np)
1226 return -ENOMEM;
1227 np->notifier = notifier;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001228 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 list_add_tail(&np->list, &raw3270_notifier);
1230 list_for_each_entry(rp, &raw3270_devices, list) {
1231 get_device(&rp->cdev->dev);
1232 notifier(rp->minor, 1);
1233 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001234 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return 0;
1236}
1237
1238void raw3270_unregister_notifier(void (*notifier)(int, int))
1239{
1240 struct raw3270_notifier *np;
1241
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001242 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 list_for_each_entry(np, &raw3270_notifier, list)
1244 if (np->notifier == notifier) {
1245 list_del(&np->list);
1246 kfree(np);
1247 break;
1248 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001249 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252/*
1253 * Set 3270 device online.
1254 */
1255static int
1256raw3270_set_online (struct ccw_device *cdev)
1257{
1258 struct raw3270 *rp;
1259 struct raw3270_notifier *np;
1260 int rc;
1261
1262 rp = raw3270_create_device(cdev);
1263 if (IS_ERR(rp))
1264 return PTR_ERR(rp);
1265 rc = raw3270_reset_device(rp);
1266 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001267 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 rc = raw3270_size_device(rp);
1269 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001270 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 rc = raw3270_reset_device(rp);
1272 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001273 goto failure;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001274 rc = raw3270_create_attributes(rp);
1275 if (rc)
1276 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 set_bit(RAW3270_FLAGS_READY, &rp->flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001278 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 list_for_each_entry(np, &raw3270_notifier, list)
1280 np->notifier(rp->minor, 1);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001281 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001283
1284failure:
1285 raw3270_delete_device(rp);
1286 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/*
1290 * Remove 3270 device structure.
1291 */
1292static void
1293raw3270_remove (struct ccw_device *cdev)
1294{
1295 unsigned long flags;
1296 struct raw3270 *rp;
1297 struct raw3270_view *v;
1298 struct raw3270_notifier *np;
1299
1300 rp = cdev->dev.driver_data;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001301 /*
1302 * _remove is the opposite of _probe; it's probe that
1303 * should set up rp. raw3270_remove gets entered for
1304 * devices even if they haven't been varied online.
1305 * Thus, rp may validly be NULL here.
1306 */
1307 if (rp == NULL)
1308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1310
1311 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1312
1313 /* Deactivate current view and remove all views. */
1314 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1315 if (rp->view) {
1316 rp->view->fn->deactivate(rp->view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001317 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 while (!list_empty(&rp->view_list)) {
1320 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1321 if (v->fn->release)
1322 v->fn->release(v);
1323 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1324 raw3270_del_view(v);
1325 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1326 }
1327 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1328
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001329 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 list_for_each_entry(np, &raw3270_notifier, list)
1331 np->notifier(rp->minor, 0);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001332 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* Reset 3270 device. */
1335 raw3270_reset_device(rp);
1336 /* And finally remove it. */
1337 raw3270_delete_device(rp);
1338}
1339
1340/*
1341 * Set 3270 device offline.
1342 */
1343static int
1344raw3270_set_offline (struct ccw_device *cdev)
1345{
1346 struct raw3270 *rp;
1347
1348 rp = cdev->dev.driver_data;
1349 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1350 return -EBUSY;
1351 raw3270_remove(cdev);
1352 return 0;
1353}
1354
1355static struct ccw_device_id raw3270_id[] = {
1356 { CCW_DEVICE(0x3270, 0) },
1357 { CCW_DEVICE(0x3271, 0) },
1358 { CCW_DEVICE(0x3272, 0) },
1359 { CCW_DEVICE(0x3273, 0) },
1360 { CCW_DEVICE(0x3274, 0) },
1361 { CCW_DEVICE(0x3275, 0) },
1362 { CCW_DEVICE(0x3276, 0) },
1363 { CCW_DEVICE(0x3277, 0) },
1364 { CCW_DEVICE(0x3278, 0) },
1365 { CCW_DEVICE(0x3279, 0) },
1366 { CCW_DEVICE(0x3174, 0) },
1367 { /* end of list */ },
1368};
1369
1370static struct ccw_driver raw3270_ccw_driver = {
1371 .name = "3270",
1372 .owner = THIS_MODULE,
1373 .ids = raw3270_id,
1374 .probe = &raw3270_probe,
1375 .remove = &raw3270_remove,
1376 .set_online = &raw3270_set_online,
1377 .set_offline = &raw3270_set_offline,
1378};
1379
1380static int
1381raw3270_init(void)
1382{
1383 struct raw3270 *rp;
1384 int rc;
1385
1386 if (raw3270_registered)
1387 return 0;
1388 raw3270_registered = 1;
1389 rc = ccw_driver_register(&raw3270_ccw_driver);
1390 if (rc == 0) {
1391 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001392 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001393 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 list_for_each_entry(rp, &raw3270_devices, list) {
1395 get_device(&rp->cdev->dev);
1396 raw3270_create_attributes(rp);
1397 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001398 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400 return rc;
1401}
1402
1403static void
1404raw3270_exit(void)
1405{
1406 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001407 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410MODULE_LICENSE("GPL");
1411
1412module_init(raw3270_init);
1413module_exit(raw3270_exit);
1414
1415EXPORT_SYMBOL(raw3270_request_alloc);
1416EXPORT_SYMBOL(raw3270_request_free);
1417EXPORT_SYMBOL(raw3270_request_reset);
1418EXPORT_SYMBOL(raw3270_request_set_cmd);
1419EXPORT_SYMBOL(raw3270_request_add_data);
1420EXPORT_SYMBOL(raw3270_request_set_data);
1421EXPORT_SYMBOL(raw3270_request_set_idal);
1422EXPORT_SYMBOL(raw3270_buffer_address);
1423EXPORT_SYMBOL(raw3270_add_view);
1424EXPORT_SYMBOL(raw3270_del_view);
1425EXPORT_SYMBOL(raw3270_find_view);
1426EXPORT_SYMBOL(raw3270_activate_view);
1427EXPORT_SYMBOL(raw3270_deactivate_view);
1428EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001429EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001431EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432EXPORT_SYMBOL(raw3270_register_notifier);
1433EXPORT_SYMBOL(raw3270_unregister_notifier);
1434EXPORT_SYMBOL(raw3270_wait_queue);