blob: 4f2f81b16cfa277dfebd324513a60cdf400d3c1e [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>
24
25#include "raw3270.h"
26
Richard Hitted3cb6f2005-10-30 15:00:10 -080027#include <linux/major.h>
28#include <linux/kdev_t.h>
29#include <linux/device.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080030#include <linux/mutex.h>
Richard Hitted3cb6f2005-10-30 15:00:10 -080031
Heiko Carstens2b67fc42007-02-05 21:16:47 +010032static struct class *class3270;
Richard Hitted3cb6f2005-10-30 15:00:10 -080033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* The main 3270 data structure. */
35struct raw3270 {
36 struct list_head list;
37 struct ccw_device *cdev;
38 int minor;
39
40 short model, rows, cols;
41 unsigned long flags;
42
43 struct list_head req_queue; /* Request queue. */
44 struct list_head view_list; /* List of available views. */
45 struct raw3270_view *view; /* Active view. */
46
47 struct timer_list timer; /* Device timer. */
48
49 unsigned char *ascebc; /* ascii -> ebcdic table */
Richard Hitted3cb6f2005-10-30 15:00:10 -080050 struct class_device *clttydev; /* 3270-class tty device ptr */
51 struct class_device *cltubdev; /* 3270-class tub device ptr */
Martin Schwidefsky132fab12006-06-29 14:57:39 +020052
53 struct raw3270_request init_request;
54 unsigned char init_data[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57/* raw3270->flags */
58#define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
59#define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
60#define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
61#define RAW3270_FLAGS_READY 4 /* Device is useable by views */
62#define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
63
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. */
68static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
69
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 */
78static int tubxcorrect = 0;
79module_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
145#ifdef CONFIG_TN3270_CONSOLE
146/*
147 * Allocate a new 3270 ccw request from bootmem. Only works very
148 * early in the boot process. Only con3270.c should be using this.
149 */
Heiko Carstense62133b2007-07-27 12:29:13 +0200150struct raw3270_request __init *raw3270_request_alloc_bootmem(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct raw3270_request *rq;
153
154 rq = alloc_bootmem_low(sizeof(struct raw3270));
155 if (!rq)
156 return ERR_PTR(-ENOMEM);
157 memset(rq, 0, sizeof(struct raw3270_request));
158
159 /* alloc output buffer. */
160 if (size > 0) {
161 rq->buffer = alloc_bootmem_low(size);
162 if (!rq->buffer) {
163 free_bootmem((unsigned long) rq,
164 sizeof(struct raw3270));
165 return ERR_PTR(-ENOMEM);
166 }
167 }
168 rq->size = size;
169 INIT_LIST_HEAD(&rq->list);
170
171 /*
172 * Setup ccw.
173 */
174 rq->ccw.cda = __pa(rq->buffer);
175 rq->ccw.flags = CCW_FLAG_SLI;
176
177 return rq;
178}
179#endif
180
181/*
182 * Free 3270 ccw request
183 */
184void
185raw3270_request_free (struct raw3270_request *rq)
186{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800187 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(rq);
189}
190
191/*
192 * Reset request to initial state.
193 */
194void
195raw3270_request_reset(struct raw3270_request *rq)
196{
197 BUG_ON(!list_empty(&rq->list));
198 rq->ccw.cmd_code = 0;
199 rq->ccw.count = 0;
200 rq->ccw.cda = __pa(rq->buffer);
201 rq->ccw.flags = CCW_FLAG_SLI;
202 rq->rescnt = 0;
203 rq->rc = 0;
204}
205
206/*
207 * Set command code to ccw of a request.
208 */
209void
210raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
211{
212 rq->ccw.cmd_code = cmd;
213}
214
215/*
216 * Add data fragment to output buffer.
217 */
218int
219raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
220{
221 if (size + rq->ccw.count > rq->size)
222 return -E2BIG;
223 memcpy(rq->buffer + rq->ccw.count, data, size);
224 rq->ccw.count += size;
225 return 0;
226}
227
228/*
229 * Set address/length pair to ccw of a request.
230 */
231void
232raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
233{
234 rq->ccw.cda = __pa(data);
235 rq->ccw.count = size;
236}
237
238/*
239 * Set idal buffer to ccw of a request.
240 */
241void
242raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
243{
244 rq->ccw.cda = __pa(ib->data);
245 rq->ccw.count = ib->size;
246 rq->ccw.flags |= CCW_FLAG_IDA;
247}
248
249/*
250 * Stop running ccw.
251 */
252static int
253raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
254{
255 int retries;
256 int rc;
257
258 if (raw3270_request_final(rq))
259 return 0;
260 /* Check if interrupt has already been processed */
261 for (retries = 0; retries < 5; retries++) {
262 if (retries < 2)
263 rc = ccw_device_halt(rp->cdev, (long) rq);
264 else
265 rc = ccw_device_clear(rp->cdev, (long) rq);
266 if (rc == 0)
267 break; /* termination successful */
268 }
269 return rc;
270}
271
272static int
273raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
274{
275 unsigned long flags;
276 int rc;
277
278 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
279 rc = raw3270_halt_io_nolock(rp, rq);
280 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
281 return rc;
282}
283
284/*
285 * Add the request to the request queue, try to start it if the
286 * 3270 device is idle. Return without waiting for end of i/o.
287 */
288static int
289__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
290 struct raw3270_request *rq)
291{
292 rq->view = view;
293 raw3270_get_view(view);
294 if (list_empty(&rp->req_queue) &&
295 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
296 /* No other requests are on the queue. Start this one. */
297 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
298 (unsigned long) rq, 0, 0);
299 if (rq->rc) {
300 raw3270_put_view(view);
301 return rq->rc;
302 }
303 }
304 list_add_tail(&rq->list, &rp->req_queue);
305 return 0;
306}
307
308int
309raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
310{
311 unsigned long flags;
312 struct raw3270 *rp;
313 int rc;
314
315 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
316 rp = view->dev;
317 if (!rp || rp->view != view)
318 rc = -EACCES;
319 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
320 rc = -ENODEV;
321 else
322 rc = __raw3270_start(rp, view, rq);
323 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
324 return rc;
325}
326
327int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800328raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
329{
330 struct raw3270 *rp;
331 int rc;
332
333 rp = view->dev;
334 if (!rp || rp->view != view)
335 rc = -EACCES;
336 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
337 rc = -ENODEV;
338 else
339 rc = __raw3270_start(rp, view, rq);
340 return rc;
341}
342
343int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
345{
346 struct raw3270 *rp;
347
348 rp = view->dev;
349 rq->view = view;
350 raw3270_get_view(view);
351 list_add_tail(&rq->list, &rp->req_queue);
352 return 0;
353}
354
355/*
356 * 3270 interrupt routine, called from the ccw_device layer
357 */
358static void
359raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
360{
361 struct raw3270 *rp;
362 struct raw3270_view *view;
363 struct raw3270_request *rq;
364 int rc;
365
366 rp = (struct raw3270 *) cdev->dev.driver_data;
367 if (!rp)
368 return;
369 rq = (struct raw3270_request *) intparm;
370 view = rq ? rq->view : rp->view;
371
372 if (IS_ERR(irb))
373 rc = RAW3270_IO_RETRY;
374 else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
375 rq->rc = -EIO;
376 rc = RAW3270_IO_DONE;
377 } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
378 DEV_STAT_UNIT_EXCEP)) {
379 /* Handle CE-DE-UE and subsequent UDE */
380 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
381 rc = RAW3270_IO_BUSY;
382 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
383 /* Wait for UDE if busy flag is set. */
384 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
385 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
386 /* Got it, now retry. */
387 rc = RAW3270_IO_RETRY;
388 } else
389 rc = RAW3270_IO_BUSY;
390 } else if (view)
391 rc = view->fn->intv(view, rq, irb);
392 else
393 rc = RAW3270_IO_DONE;
394
395 switch (rc) {
396 case RAW3270_IO_DONE:
397 break;
398 case RAW3270_IO_BUSY:
399 /*
400 * Intervention required by the operator. We have to wait
401 * for unsolicited device end.
402 */
403 return;
404 case RAW3270_IO_RETRY:
405 if (!rq)
406 break;
407 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
408 (unsigned long) rq, 0, 0);
409 if (rq->rc == 0)
410 return; /* Sucessfully restarted. */
411 break;
412 case RAW3270_IO_STOP:
413 if (!rq)
414 break;
415 raw3270_halt_io_nolock(rp, rq);
416 rq->rc = -EIO;
417 break;
418 default:
419 BUG();
420 }
421 if (rq) {
422 BUG_ON(list_empty(&rq->list));
423 /* The request completed, remove from queue and do callback. */
424 list_del_init(&rq->list);
425 if (rq->callback)
426 rq->callback(rq, rq->callback_data);
427 /* Do put_device for get_device in raw3270_start. */
428 raw3270_put_view(view);
429 }
430 /*
431 * Try to start each request on request queue until one is
432 * started successful.
433 */
434 while (!list_empty(&rp->req_queue)) {
435 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
436 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
437 (unsigned long) rq, 0, 0);
438 if (rq->rc == 0)
439 break;
440 /* Start failed. Remove request and do callback. */
441 list_del_init(&rq->list);
442 if (rq->callback)
443 rq->callback(rq, rq->callback_data);
444 /* Do put_device for get_device in raw3270_start. */
445 raw3270_put_view(view);
446 }
447}
448
449/*
450 * Size sensing.
451 */
452
453struct raw3270_ua { /* Query Reply structure for Usable Area */
454 struct { /* Usable Area Query Reply Base */
455 short l; /* Length of this structured field */
456 char sfid; /* 0x81 if Query Reply */
457 char qcode; /* 0x81 if Usable Area */
458 char flags0;
459 char flags1;
460 short w; /* Width of usable area */
461 short h; /* Heigth of usavle area */
462 char units; /* 0x00:in; 0x01:mm */
463 int xr;
464 int yr;
465 char aw;
466 char ah;
467 short buffsz; /* Character buffer size, bytes */
468 char xmin;
469 char ymin;
470 char xmax;
471 char ymax;
472 } __attribute__ ((packed)) uab;
473 struct { /* Alternate Usable Area Self-Defining Parameter */
474 char l; /* Length of this Self-Defining Parm */
475 char sdpid; /* 0x02 if Alternate Usable Area */
476 char res;
477 char auaid; /* 0x01 is Id for the A U A */
478 short wauai; /* Width of AUAi */
479 short hauai; /* Height of AUAi */
480 char auaunits; /* 0x00:in, 0x01:mm */
481 int auaxr;
482 int auayr;
483 char awauai;
484 char ahauai;
485 } __attribute__ ((packed)) aua;
486} __attribute__ ((packed));
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static struct diag210 raw3270_init_diag210;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200489static DEFINE_MUTEX(raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491static int
492raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
493 struct irb *irb)
494{
495 /*
496 * Unit-Check Processing:
497 * Expect Command Reject or Intervention Required.
498 */
499 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
500 /* Request finished abnormally. */
501 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
502 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
503 return RAW3270_IO_BUSY;
504 }
505 }
506 if (rq) {
507 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
508 if (irb->ecw[0] & SNS0_CMD_REJECT)
509 rq->rc = -EOPNOTSUPP;
510 else
511 rq->rc = -EIO;
512 } else
513 /* Request finished normally. Copy residual count. */
514 rq->rescnt = irb->scsw.count;
515 }
516 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
517 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
518 wake_up(&raw3270_wait_queue);
519 }
520 return RAW3270_IO_DONE;
521}
522
523static struct raw3270_fn raw3270_init_fn = {
524 .intv = raw3270_init_irq
525};
526
527static struct raw3270_view raw3270_init_view = {
528 .fn = &raw3270_init_fn
529};
530
531/*
532 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
533 * Wait for end of request. The request must have been started
534 * with raw3270_start, rc = 0. The device lock may NOT have been
535 * released between calling raw3270_start and raw3270_wait.
536 */
537static void
538raw3270_wake_init(struct raw3270_request *rq, void *data)
539{
540 wake_up((wait_queue_head_t *) data);
541}
542
543/*
544 * Special wait function that can cope with console initialization.
545 */
546static int
547raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
548 struct raw3270_request *rq)
549{
550 unsigned long flags;
551 wait_queue_head_t wq;
552 int rc;
553
554#ifdef CONFIG_TN3270_CONSOLE
555 if (raw3270_registered == 0) {
556 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200557 rq->callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rc = __raw3270_start(rp, view, rq);
559 if (rc == 0)
560 while (!raw3270_request_final(rq)) {
561 wait_cons_dev();
562 barrier();
563 }
564 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
565 return rq->rc;
566 }
567#endif
568 init_waitqueue_head(&wq);
569 rq->callback = raw3270_wake_init;
570 rq->callback_data = &wq;
571 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
572 rc = __raw3270_start(rp, view, rq);
573 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
574 if (rc)
575 return rc;
576 /* Now wait for the completion. */
577 rc = wait_event_interruptible(wq, raw3270_request_final(rq));
578 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
579 raw3270_halt_io(view->dev, rq);
580 /* No wait for the halt to complete. */
581 wait_event(wq, raw3270_request_final(rq));
582 return -ERESTARTSYS;
583 }
584 return rq->rc;
585}
586
587static int
588__raw3270_size_device_vm(struct raw3270 *rp)
589{
590 int rc, model;
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200591 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Cornelia Huck9a92fe42007-05-10 15:45:42 +0200593 ccw_device_get_id(rp->cdev, &dev_id);
594 raw3270_init_diag210.vrdcdvno = dev_id.devno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 raw3270_init_diag210.vrdclen = sizeof(struct diag210);
596 rc = diag210(&raw3270_init_diag210);
597 if (rc)
598 return rc;
599 model = raw3270_init_diag210.vrdccrmd;
600 switch (model) {
601 case 2:
602 rp->model = model;
603 rp->rows = 24;
604 rp->cols = 80;
605 break;
606 case 3:
607 rp->model = model;
608 rp->rows = 32;
609 rp->cols = 80;
610 break;
611 case 4:
612 rp->model = model;
613 rp->rows = 43;
614 rp->cols = 80;
615 break;
616 case 5:
617 rp->model = model;
618 rp->rows = 27;
619 rp->cols = 132;
620 break;
621 default:
622 printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
623 rc = -EOPNOTSUPP;
624 break;
625 }
626 return rc;
627}
628
629static int
630__raw3270_size_device(struct raw3270 *rp)
631{
632 static const unsigned char wbuf[] =
633 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
634 struct raw3270_ua *uap;
635 unsigned short count;
636 int rc;
637
638 /*
639 * To determine the size of the 3270 device we need to do:
640 * 1) send a 'read partition' data stream to the device
641 * 2) wait for the attn interrupt that preceeds the query reply
642 * 3) do a read modified to get the query reply
643 * To make things worse we have to cope with intervention
644 * required (3270 device switched to 'stand-by') and command
645 * rejects (old devices that can't do 'read partition').
646 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200647 memset(&rp->init_request, 0, sizeof(rp->init_request));
648 memset(&rp->init_data, 0, 256);
649 /* Store 'read partition' data stream to init_data */
650 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
651 INIT_LIST_HEAD(&rp->init_request.list);
652 rp->init_request.ccw.cmd_code = TC_WRITESF;
653 rp->init_request.ccw.flags = CCW_FLAG_SLI;
654 rp->init_request.ccw.count = sizeof(wbuf);
655 rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200657 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700658 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* Wait for attention interrupt. */
663#ifdef CONFIG_TN3270_CONSOLE
664 if (raw3270_registered == 0) {
665 unsigned long flags;
666
667 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
668 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
669 wait_cons_dev();
670 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
671 } else
672#endif
673 rc = wait_event_interruptible(raw3270_wait_queue,
674 test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
675 if (rc)
676 return rc;
677
678 /*
679 * The device accepted the 'read partition' command. Now
680 * set up a read ccw and issue it.
681 */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200682 rp->init_request.ccw.cmd_code = TC_READMOD;
683 rp->init_request.ccw.flags = CCW_FLAG_SLI;
684 rp->init_request.ccw.count = sizeof(rp->init_data);
685 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
686 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (rc)
688 return rc;
689 /* Got a Query Reply */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200690 count = sizeof(rp->init_data) - rp->init_request.rescnt;
691 uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* Paranoia check. */
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200693 if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EOPNOTSUPP;
695 /* Copy rows/columns of default Usable Area */
696 rp->rows = uap->uab.h;
697 rp->cols = uap->uab.w;
698 /* Check for 14 bit addressing */
699 if ((uap->uab.flags0 & 0x0d) == 0x01)
700 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
701 /* Check for Alternate Usable Area */
702 if (uap->uab.l == sizeof(struct raw3270_ua) &&
703 uap->aua.sdpid == 0x02) {
704 rp->rows = uap->aua.hauai;
705 rp->cols = uap->aua.wauai;
706 }
707 return 0;
708}
709
710static int
711raw3270_size_device(struct raw3270 *rp)
712{
713 int rc;
714
Christoph Hellwigd330f932007-05-31 17:38:04 +0200715 mutex_lock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 rp->view = &raw3270_init_view;
717 raw3270_init_view.dev = rp;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700718 if (MACHINE_IS_VM)
719 rc = __raw3270_size_device_vm(rp);
720 else
721 rc = __raw3270_size_device(rp);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200722 raw3270_init_view.dev = NULL;
723 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200724 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (rc == 0) { /* Found something. */
726 /* Try to find a model. */
727 rp->model = 0;
728 if (rp->rows == 24 && rp->cols == 80)
729 rp->model = 2;
730 if (rp->rows == 32 && rp->cols == 80)
731 rp->model = 3;
732 if (rp->rows == 43 && rp->cols == 80)
733 rp->model = 4;
734 if (rp->rows == 27 && rp->cols == 132)
735 rp->model = 5;
Martin Schwidefsky3863e722005-09-03 15:58:06 -0700736 } else {
737 /* Couldn't detect size. Use default model 2. */
738 rp->model = 2;
739 rp->rows = 24;
740 rp->cols = 80;
741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 return rc;
744}
745
746static int
747raw3270_reset_device(struct raw3270 *rp)
748{
749 int rc;
750
Christoph Hellwigd330f932007-05-31 17:38:04 +0200751 mutex_lock(&raw3270_init_mutex);
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200752 memset(&rp->init_request, 0, sizeof(rp->init_request));
753 memset(&rp->init_data, 0, sizeof(rp->init_data));
754 /* Store reset data stream to init_data/init_request */
755 rp->init_data[0] = TW_KR;
756 INIT_LIST_HEAD(&rp->init_request.list);
757 rp->init_request.ccw.cmd_code = TC_EWRITEA;
758 rp->init_request.ccw.flags = CCW_FLAG_SLI;
759 rp->init_request.ccw.count = 1;
760 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 rp->view = &raw3270_init_view;
762 raw3270_init_view.dev = rp;
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200763 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200764 raw3270_init_view.dev = NULL;
765 rp->view = NULL;
Christoph Hellwigd330f932007-05-31 17:38:04 +0200766 mutex_unlock(&raw3270_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return rc;
768}
769
Richard Hitted3cb6f2005-10-30 15:00:10 -0800770int
771raw3270_reset(struct raw3270_view *view)
772{
773 struct raw3270 *rp;
774 int rc;
775
776 rp = view->dev;
777 if (!rp || rp->view != view)
778 rc = -EACCES;
779 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
780 rc = -ENODEV;
781 else
782 rc = raw3270_reset_device(view->dev);
783 return rc;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*
787 * Setup new 3270 device.
788 */
789static int
790raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
791{
792 struct list_head *l;
793 struct raw3270 *tmp;
794 int minor;
795
796 memset(rp, 0, sizeof(struct raw3270));
797 /* Copy ebcdic -> ascii translation table. */
798 memcpy(ascebc, _ascebc, 256);
799 if (tubxcorrect) {
800 /* correct brackets and circumflex */
801 ascebc['['] = 0xad;
802 ascebc[']'] = 0xbd;
803 ascebc['^'] = 0xb0;
804 }
805 rp->ascebc = ascebc;
806
807 /* Set defaults. */
808 rp->rows = 24;
809 rp->cols = 80;
810
811 INIT_LIST_HEAD(&rp->req_queue);
812 INIT_LIST_HEAD(&rp->view_list);
813
814 /*
815 * Add device to list and find the smallest unused minor
Richard Hitted3cb6f2005-10-30 15:00:10 -0800816 * number for it. Note: there is no device with minor 0,
817 * see special case for fs3270.c:fs3270_open().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800819 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* Keep the list sorted. */
Richard Hitted3cb6f2005-10-30 15:00:10 -0800821 minor = RAW3270_FIRSTMINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 rp->minor = -1;
823 list_for_each(l, &raw3270_devices) {
824 tmp = list_entry(l, struct raw3270, list);
825 if (tmp->minor > minor) {
826 rp->minor = minor;
827 __list_add(&rp->list, l->prev, l);
828 break;
829 }
830 minor++;
831 }
Richard Hitted3cb6f2005-10-30 15:00:10 -0800832 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 rp->minor = minor;
834 list_add_tail(&rp->list, &raw3270_devices);
835 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800836 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* No free minor number? Then give up. */
838 if (rp->minor == -1)
839 return -EUSERS;
840 rp->cdev = cdev;
841 cdev->dev.driver_data = rp;
842 cdev->handler = raw3270_irq;
843 return 0;
844}
845
846#ifdef CONFIG_TN3270_CONSOLE
847/*
848 * Setup 3270 device configured as console.
849 */
Heiko Carstense62133b2007-07-27 12:29:13 +0200850struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct raw3270 *rp;
853 char *ascebc;
854 int rc;
855
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200856 rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 ascebc = (char *) alloc_bootmem(256);
858 rc = raw3270_setup_device(cdev, rp, ascebc);
859 if (rc)
860 return ERR_PTR(rc);
861 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
862 rc = raw3270_reset_device(rp);
863 if (rc)
864 return ERR_PTR(rc);
865 rc = raw3270_size_device(rp);
866 if (rc)
867 return ERR_PTR(rc);
868 rc = raw3270_reset_device(rp);
869 if (rc)
870 return ERR_PTR(rc);
871 set_bit(RAW3270_FLAGS_READY, &rp->flags);
872 return rp;
873}
874
875void
876raw3270_wait_cons_dev(struct raw3270 *rp)
877{
878 unsigned long flags;
879
880 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
881 wait_cons_dev();
882 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
883}
884
885#endif
886
887/*
888 * Create a 3270 device structure.
889 */
890static struct raw3270 *
891raw3270_create_device(struct ccw_device *cdev)
892{
893 struct raw3270 *rp;
894 char *ascebc;
895 int rc;
896
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200897 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!rp)
899 return ERR_PTR(-ENOMEM);
900 ascebc = kmalloc(256, GFP_KERNEL);
901 if (!ascebc) {
902 kfree(rp);
903 return ERR_PTR(-ENOMEM);
904 }
905 rc = raw3270_setup_device(cdev, rp, ascebc);
906 if (rc) {
907 kfree(rp->ascebc);
908 kfree(rp);
909 rp = ERR_PTR(rc);
910 }
911 /* Get reference to ccw_device structure. */
912 get_device(&cdev->dev);
913 return rp;
914}
915
916/*
917 * Activate a view.
918 */
919int
920raw3270_activate_view(struct raw3270_view *view)
921{
922 struct raw3270 *rp;
923 struct raw3270_view *oldview, *nv;
924 unsigned long flags;
925 int rc;
926
927 rp = view->dev;
928 if (!rp)
929 return -ENODEV;
930 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
931 if (rp->view == view)
932 rc = 0;
933 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
934 rc = -ENODEV;
935 else {
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200936 oldview = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (rp->view) {
938 oldview = rp->view;
939 oldview->fn->deactivate(oldview);
940 }
941 rp->view = view;
942 rc = view->fn->activate(view);
943 if (rc) {
944 /* Didn't work. Try to reactivate the old view. */
945 rp->view = oldview;
946 if (!oldview || oldview->fn->activate(oldview) != 0) {
947 /* Didn't work as well. Try any other view. */
948 list_for_each_entry(nv, &rp->view_list, list)
949 if (nv != view && nv != oldview) {
950 rp->view = nv;
951 if (nv->fn->activate(nv) == 0)
952 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200953 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 }
956 }
957 }
958 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
959 return rc;
960}
961
962/*
963 * Deactivate current view.
964 */
965void
966raw3270_deactivate_view(struct raw3270_view *view)
967{
968 unsigned long flags;
969 struct raw3270 *rp;
970
971 rp = view->dev;
972 if (!rp)
973 return;
974 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
975 if (rp->view == view) {
976 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200977 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* Move deactivated view to end of list. */
979 list_del_init(&view->list);
980 list_add_tail(&view->list, &rp->view_list);
981 /* Try to activate another view. */
982 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800983 list_for_each_entry(view, &rp->view_list, list) {
984 rp->view = view;
985 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 break;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200987 rp->view = NULL;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 }
991 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
992}
993
994/*
995 * Add view to device with minor "minor".
996 */
997int
998raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
999{
1000 unsigned long flags;
1001 struct raw3270 *rp;
1002 int rc;
1003
Richard Hitted3cb6f2005-10-30 15:00:10 -08001004 if (minor <= 0)
1005 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001006 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 rc = -ENODEV;
1008 list_for_each_entry(rp, &raw3270_devices, list) {
1009 if (rp->minor != minor)
1010 continue;
1011 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1012 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1013 atomic_set(&view->ref_count, 2);
1014 view->dev = rp;
1015 view->fn = fn;
1016 view->model = rp->model;
1017 view->rows = rp->rows;
1018 view->cols = rp->cols;
1019 view->ascebc = rp->ascebc;
1020 spin_lock_init(&view->lock);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001021 list_add(&view->list, &rp->view_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 rc = 0;
1023 }
1024 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1025 break;
1026 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001027 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return rc;
1029}
1030
1031/*
1032 * Find specific view of device with minor "minor".
1033 */
1034struct raw3270_view *
1035raw3270_find_view(struct raw3270_fn *fn, int minor)
1036{
1037 struct raw3270 *rp;
1038 struct raw3270_view *view, *tmp;
1039 unsigned long flags;
1040
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001041 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 view = ERR_PTR(-ENODEV);
1043 list_for_each_entry(rp, &raw3270_devices, list) {
1044 if (rp->minor != minor)
1045 continue;
1046 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1047 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1048 view = ERR_PTR(-ENOENT);
1049 list_for_each_entry(tmp, &rp->view_list, list) {
1050 if (tmp->fn == fn) {
1051 raw3270_get_view(tmp);
1052 view = tmp;
1053 break;
1054 }
1055 }
1056 }
1057 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1058 break;
1059 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001060 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return view;
1062}
1063
1064/*
1065 * Remove view from device and free view structure via call to view->fn->free.
1066 */
1067void
1068raw3270_del_view(struct raw3270_view *view)
1069{
1070 unsigned long flags;
1071 struct raw3270 *rp;
1072 struct raw3270_view *nv;
1073
1074 rp = view->dev;
1075 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1076 if (rp->view == view) {
1077 view->fn->deactivate(view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001078 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 list_del_init(&view->list);
1081 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1082 /* Try to activate another view. */
1083 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001084 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 rp->view = nv;
1086 break;
1087 }
1088 }
1089 }
1090 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1091 /* Wait for reference counter to drop to zero. */
1092 atomic_dec(&view->ref_count);
1093 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1094 if (view->fn->free)
1095 view->fn->free(view);
1096}
1097
1098/*
1099 * Remove a 3270 device structure.
1100 */
1101static void
1102raw3270_delete_device(struct raw3270 *rp)
1103{
1104 struct ccw_device *cdev;
1105
1106 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001107 mutex_lock(&raw3270_mutex);
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001108 if (rp->clttydev && !IS_ERR(rp->clttydev))
Richard Hitted3cb6f2005-10-30 15:00:10 -08001109 class_device_destroy(class3270,
1110 MKDEV(IBM_TTY3270_MAJOR, rp->minor));
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001111 if (rp->cltubdev && !IS_ERR(rp->cltubdev))
Richard Hitted3cb6f2005-10-30 15:00:10 -08001112 class_device_destroy(class3270,
1113 MKDEV(IBM_FS3270_MAJOR, rp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001115 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /* Disconnect from ccw_device. */
1118 cdev = rp->cdev;
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001119 rp->cdev = NULL;
1120 cdev->dev.driver_data = NULL;
1121 cdev->handler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /* Put ccw_device structure. */
1124 put_device(&cdev->dev);
1125
1126 /* Now free raw3270 structure. */
1127 kfree(rp->ascebc);
1128 kfree(rp);
1129}
1130
1131static int
1132raw3270_probe (struct ccw_device *cdev)
1133{
1134 return 0;
1135}
1136
1137/*
1138 * Additional attributes for a 3270 device
1139 */
1140static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001141raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 return snprintf(buf, PAGE_SIZE, "%i\n",
1144 ((struct raw3270 *) dev->driver_data)->model);
1145}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001146static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001149raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 return snprintf(buf, PAGE_SIZE, "%i\n",
1152 ((struct raw3270 *) dev->driver_data)->rows);
1153}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001154static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001157raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 return snprintf(buf, PAGE_SIZE, "%i\n",
1160 ((struct raw3270 *) dev->driver_data)->cols);
1161}
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001162static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164static struct attribute * raw3270_attrs[] = {
1165 &dev_attr_model.attr,
1166 &dev_attr_rows.attr,
1167 &dev_attr_columns.attr,
1168 NULL,
1169};
1170
1171static struct attribute_group raw3270_attr_group = {
1172 .attrs = raw3270_attrs,
1173};
1174
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001175static int raw3270_create_attributes(struct raw3270 *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001177 int rc;
1178
1179 rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1180 if (rc)
1181 goto out;
1182
1183 rp->clttydev = class_device_create(class3270, NULL,
1184 MKDEV(IBM_TTY3270_MAJOR, rp->minor),
1185 &rp->cdev->dev, "tty%s",
1186 rp->cdev->dev.bus_id);
1187 if (IS_ERR(rp->clttydev)) {
1188 rc = PTR_ERR(rp->clttydev);
1189 goto out_ttydev;
1190 }
1191
1192 rp->cltubdev = class_device_create(class3270, NULL,
1193 MKDEV(IBM_FS3270_MAJOR, rp->minor),
1194 &rp->cdev->dev, "tub%s",
1195 rp->cdev->dev.bus_id);
1196 if (!IS_ERR(rp->cltubdev))
1197 goto out;
1198
1199 rc = PTR_ERR(rp->cltubdev);
1200 class_device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1201
1202out_ttydev:
1203 sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1204out:
1205 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208/*
1209 * Notifier for device addition/removal
1210 */
1211struct raw3270_notifier {
1212 struct list_head list;
1213 void (*notifier)(int, int);
1214};
1215
1216static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
1217
1218int raw3270_register_notifier(void (*notifier)(int, int))
1219{
1220 struct raw3270_notifier *np;
1221 struct raw3270 *rp;
1222
1223 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1224 if (!np)
1225 return -ENOMEM;
1226 np->notifier = notifier;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001227 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 list_add_tail(&np->list, &raw3270_notifier);
1229 list_for_each_entry(rp, &raw3270_devices, list) {
1230 get_device(&rp->cdev->dev);
1231 notifier(rp->minor, 1);
1232 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001233 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return 0;
1235}
1236
1237void raw3270_unregister_notifier(void (*notifier)(int, int))
1238{
1239 struct raw3270_notifier *np;
1240
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001241 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 list_for_each_entry(np, &raw3270_notifier, list)
1243 if (np->notifier == notifier) {
1244 list_del(&np->list);
1245 kfree(np);
1246 break;
1247 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001248 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/*
1252 * Set 3270 device online.
1253 */
1254static int
1255raw3270_set_online (struct ccw_device *cdev)
1256{
1257 struct raw3270 *rp;
1258 struct raw3270_notifier *np;
1259 int rc;
1260
1261 rp = raw3270_create_device(cdev);
1262 if (IS_ERR(rp))
1263 return PTR_ERR(rp);
1264 rc = raw3270_reset_device(rp);
1265 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001266 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 rc = raw3270_size_device(rp);
1268 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001269 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 rc = raw3270_reset_device(rp);
1271 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001272 goto failure;
Heiko Carstensd7cf0d52006-07-18 13:46:58 +02001273 rc = raw3270_create_attributes(rp);
1274 if (rc)
1275 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 set_bit(RAW3270_FLAGS_READY, &rp->flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001277 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 list_for_each_entry(np, &raw3270_notifier, list)
1279 np->notifier(rp->minor, 1);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001280 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001282
1283failure:
1284 raw3270_delete_device(rp);
1285 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288/*
1289 * Remove 3270 device structure.
1290 */
1291static void
1292raw3270_remove (struct ccw_device *cdev)
1293{
1294 unsigned long flags;
1295 struct raw3270 *rp;
1296 struct raw3270_view *v;
1297 struct raw3270_notifier *np;
1298
1299 rp = cdev->dev.driver_data;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001300 /*
1301 * _remove is the opposite of _probe; it's probe that
1302 * should set up rp. raw3270_remove gets entered for
1303 * devices even if they haven't been varied online.
1304 * Thus, rp may validly be NULL here.
1305 */
1306 if (rp == NULL)
1307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1309
1310 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1311
1312 /* Deactivate current view and remove all views. */
1313 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1314 if (rp->view) {
1315 rp->view->fn->deactivate(rp->view);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001316 rp->view = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 while (!list_empty(&rp->view_list)) {
1319 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1320 if (v->fn->release)
1321 v->fn->release(v);
1322 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1323 raw3270_del_view(v);
1324 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1325 }
1326 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1327
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001328 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 list_for_each_entry(np, &raw3270_notifier, list)
1330 np->notifier(rp->minor, 0);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001331 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* Reset 3270 device. */
1334 raw3270_reset_device(rp);
1335 /* And finally remove it. */
1336 raw3270_delete_device(rp);
1337}
1338
1339/*
1340 * Set 3270 device offline.
1341 */
1342static int
1343raw3270_set_offline (struct ccw_device *cdev)
1344{
1345 struct raw3270 *rp;
1346
1347 rp = cdev->dev.driver_data;
1348 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1349 return -EBUSY;
1350 raw3270_remove(cdev);
1351 return 0;
1352}
1353
1354static struct ccw_device_id raw3270_id[] = {
1355 { CCW_DEVICE(0x3270, 0) },
1356 { CCW_DEVICE(0x3271, 0) },
1357 { CCW_DEVICE(0x3272, 0) },
1358 { CCW_DEVICE(0x3273, 0) },
1359 { CCW_DEVICE(0x3274, 0) },
1360 { CCW_DEVICE(0x3275, 0) },
1361 { CCW_DEVICE(0x3276, 0) },
1362 { CCW_DEVICE(0x3277, 0) },
1363 { CCW_DEVICE(0x3278, 0) },
1364 { CCW_DEVICE(0x3279, 0) },
1365 { CCW_DEVICE(0x3174, 0) },
1366 { /* end of list */ },
1367};
1368
1369static struct ccw_driver raw3270_ccw_driver = {
1370 .name = "3270",
1371 .owner = THIS_MODULE,
1372 .ids = raw3270_id,
1373 .probe = &raw3270_probe,
1374 .remove = &raw3270_remove,
1375 .set_online = &raw3270_set_online,
1376 .set_offline = &raw3270_set_offline,
1377};
1378
1379static int
1380raw3270_init(void)
1381{
1382 struct raw3270 *rp;
1383 int rc;
1384
1385 if (raw3270_registered)
1386 return 0;
1387 raw3270_registered = 1;
1388 rc = ccw_driver_register(&raw3270_ccw_driver);
1389 if (rc == 0) {
1390 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001391 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001392 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 list_for_each_entry(rp, &raw3270_devices, list) {
1394 get_device(&rp->cdev->dev);
1395 raw3270_create_attributes(rp);
1396 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001397 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 return rc;
1400}
1401
1402static void
1403raw3270_exit(void)
1404{
1405 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001406 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
1409MODULE_LICENSE("GPL");
1410
1411module_init(raw3270_init);
1412module_exit(raw3270_exit);
1413
1414EXPORT_SYMBOL(raw3270_request_alloc);
1415EXPORT_SYMBOL(raw3270_request_free);
1416EXPORT_SYMBOL(raw3270_request_reset);
1417EXPORT_SYMBOL(raw3270_request_set_cmd);
1418EXPORT_SYMBOL(raw3270_request_add_data);
1419EXPORT_SYMBOL(raw3270_request_set_data);
1420EXPORT_SYMBOL(raw3270_request_set_idal);
1421EXPORT_SYMBOL(raw3270_buffer_address);
1422EXPORT_SYMBOL(raw3270_add_view);
1423EXPORT_SYMBOL(raw3270_del_view);
1424EXPORT_SYMBOL(raw3270_find_view);
1425EXPORT_SYMBOL(raw3270_activate_view);
1426EXPORT_SYMBOL(raw3270_deactivate_view);
1427EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001428EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001430EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431EXPORT_SYMBOL(raw3270_register_notifier);
1432EXPORT_SYMBOL(raw3270_unregister_notifier);
1433EXPORT_SYMBOL(raw3270_wait_queue);