blob: 3c1314b7391b0fe5141982210c366dfb902875e0 [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
11#include <linux/config.h>
12#include <linux/bootmem.h>
13#include <linux/module.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/list.h>
18#include <linux/slab.h>
19#include <linux/types.h>
20#include <linux/wait.h>
21
22#include <asm/ccwdev.h>
23#include <asm/cio.h>
24#include <asm/ebcdic.h>
25
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
33struct class *class3270;
34
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 */
90unsigned char raw3270_ebcgraf[64] = {
91 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 */
151struct raw3270_request *
152raw3270_request_alloc_bootmem(size_t size)
153{
154 struct raw3270_request *rq;
155
156 rq = alloc_bootmem_low(sizeof(struct raw3270));
157 if (!rq)
158 return ERR_PTR(-ENOMEM);
159 memset(rq, 0, sizeof(struct raw3270_request));
160
161 /* alloc output buffer. */
162 if (size > 0) {
163 rq->buffer = alloc_bootmem_low(size);
164 if (!rq->buffer) {
165 free_bootmem((unsigned long) rq,
166 sizeof(struct raw3270));
167 return ERR_PTR(-ENOMEM);
168 }
169 }
170 rq->size = size;
171 INIT_LIST_HEAD(&rq->list);
172
173 /*
174 * Setup ccw.
175 */
176 rq->ccw.cda = __pa(rq->buffer);
177 rq->ccw.flags = CCW_FLAG_SLI;
178
179 return rq;
180}
181#endif
182
183/*
184 * Free 3270 ccw request
185 */
186void
187raw3270_request_free (struct raw3270_request *rq)
188{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800189 kfree(rq->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kfree(rq);
191}
192
193/*
194 * Reset request to initial state.
195 */
196void
197raw3270_request_reset(struct raw3270_request *rq)
198{
199 BUG_ON(!list_empty(&rq->list));
200 rq->ccw.cmd_code = 0;
201 rq->ccw.count = 0;
202 rq->ccw.cda = __pa(rq->buffer);
203 rq->ccw.flags = CCW_FLAG_SLI;
204 rq->rescnt = 0;
205 rq->rc = 0;
206}
207
208/*
209 * Set command code to ccw of a request.
210 */
211void
212raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
213{
214 rq->ccw.cmd_code = cmd;
215}
216
217/*
218 * Add data fragment to output buffer.
219 */
220int
221raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
222{
223 if (size + rq->ccw.count > rq->size)
224 return -E2BIG;
225 memcpy(rq->buffer + rq->ccw.count, data, size);
226 rq->ccw.count += size;
227 return 0;
228}
229
230/*
231 * Set address/length pair to ccw of a request.
232 */
233void
234raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
235{
236 rq->ccw.cda = __pa(data);
237 rq->ccw.count = size;
238}
239
240/*
241 * Set idal buffer to ccw of a request.
242 */
243void
244raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
245{
246 rq->ccw.cda = __pa(ib->data);
247 rq->ccw.count = ib->size;
248 rq->ccw.flags |= CCW_FLAG_IDA;
249}
250
251/*
252 * Stop running ccw.
253 */
254static int
255raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
256{
257 int retries;
258 int rc;
259
260 if (raw3270_request_final(rq))
261 return 0;
262 /* Check if interrupt has already been processed */
263 for (retries = 0; retries < 5; retries++) {
264 if (retries < 2)
265 rc = ccw_device_halt(rp->cdev, (long) rq);
266 else
267 rc = ccw_device_clear(rp->cdev, (long) rq);
268 if (rc == 0)
269 break; /* termination successful */
270 }
271 return rc;
272}
273
274static int
275raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
276{
277 unsigned long flags;
278 int rc;
279
280 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
281 rc = raw3270_halt_io_nolock(rp, rq);
282 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
283 return rc;
284}
285
286/*
287 * Add the request to the request queue, try to start it if the
288 * 3270 device is idle. Return without waiting for end of i/o.
289 */
290static int
291__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
292 struct raw3270_request *rq)
293{
294 rq->view = view;
295 raw3270_get_view(view);
296 if (list_empty(&rp->req_queue) &&
297 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
298 /* No other requests are on the queue. Start this one. */
299 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
300 (unsigned long) rq, 0, 0);
301 if (rq->rc) {
302 raw3270_put_view(view);
303 return rq->rc;
304 }
305 }
306 list_add_tail(&rq->list, &rp->req_queue);
307 return 0;
308}
309
310int
311raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
312{
313 unsigned long flags;
314 struct raw3270 *rp;
315 int rc;
316
317 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
318 rp = view->dev;
319 if (!rp || rp->view != view)
320 rc = -EACCES;
321 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
322 rc = -ENODEV;
323 else
324 rc = __raw3270_start(rp, view, rq);
325 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
326 return rc;
327}
328
329int
Richard Hitted3cb6f2005-10-30 15:00:10 -0800330raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
331{
332 struct raw3270 *rp;
333 int rc;
334
335 rp = view->dev;
336 if (!rp || rp->view != view)
337 rc = -EACCES;
338 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
339 rc = -ENODEV;
340 else
341 rc = __raw3270_start(rp, view, rq);
342 return rc;
343}
344
345int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
347{
348 struct raw3270 *rp;
349
350 rp = view->dev;
351 rq->view = view;
352 raw3270_get_view(view);
353 list_add_tail(&rq->list, &rp->req_queue);
354 return 0;
355}
356
357/*
358 * 3270 interrupt routine, called from the ccw_device layer
359 */
360static void
361raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
362{
363 struct raw3270 *rp;
364 struct raw3270_view *view;
365 struct raw3270_request *rq;
366 int rc;
367
368 rp = (struct raw3270 *) cdev->dev.driver_data;
369 if (!rp)
370 return;
371 rq = (struct raw3270_request *) intparm;
372 view = rq ? rq->view : rp->view;
373
374 if (IS_ERR(irb))
375 rc = RAW3270_IO_RETRY;
376 else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
377 rq->rc = -EIO;
378 rc = RAW3270_IO_DONE;
379 } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
380 DEV_STAT_UNIT_EXCEP)) {
381 /* Handle CE-DE-UE and subsequent UDE */
382 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
383 rc = RAW3270_IO_BUSY;
384 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
385 /* Wait for UDE if busy flag is set. */
386 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
387 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
388 /* Got it, now retry. */
389 rc = RAW3270_IO_RETRY;
390 } else
391 rc = RAW3270_IO_BUSY;
392 } else if (view)
393 rc = view->fn->intv(view, rq, irb);
394 else
395 rc = RAW3270_IO_DONE;
396
397 switch (rc) {
398 case RAW3270_IO_DONE:
399 break;
400 case RAW3270_IO_BUSY:
401 /*
402 * Intervention required by the operator. We have to wait
403 * for unsolicited device end.
404 */
405 return;
406 case RAW3270_IO_RETRY:
407 if (!rq)
408 break;
409 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
410 (unsigned long) rq, 0, 0);
411 if (rq->rc == 0)
412 return; /* Sucessfully restarted. */
413 break;
414 case RAW3270_IO_STOP:
415 if (!rq)
416 break;
417 raw3270_halt_io_nolock(rp, rq);
418 rq->rc = -EIO;
419 break;
420 default:
421 BUG();
422 }
423 if (rq) {
424 BUG_ON(list_empty(&rq->list));
425 /* The request completed, remove from queue and do callback. */
426 list_del_init(&rq->list);
427 if (rq->callback)
428 rq->callback(rq, rq->callback_data);
429 /* Do put_device for get_device in raw3270_start. */
430 raw3270_put_view(view);
431 }
432 /*
433 * Try to start each request on request queue until one is
434 * started successful.
435 */
436 while (!list_empty(&rp->req_queue)) {
437 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
438 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
439 (unsigned long) rq, 0, 0);
440 if (rq->rc == 0)
441 break;
442 /* Start failed. Remove request and do callback. */
443 list_del_init(&rq->list);
444 if (rq->callback)
445 rq->callback(rq, rq->callback_data);
446 /* Do put_device for get_device in raw3270_start. */
447 raw3270_put_view(view);
448 }
449}
450
451/*
452 * Size sensing.
453 */
454
455struct raw3270_ua { /* Query Reply structure for Usable Area */
456 struct { /* Usable Area Query Reply Base */
457 short l; /* Length of this structured field */
458 char sfid; /* 0x81 if Query Reply */
459 char qcode; /* 0x81 if Usable Area */
460 char flags0;
461 char flags1;
462 short w; /* Width of usable area */
463 short h; /* Heigth of usavle area */
464 char units; /* 0x00:in; 0x01:mm */
465 int xr;
466 int yr;
467 char aw;
468 char ah;
469 short buffsz; /* Character buffer size, bytes */
470 char xmin;
471 char ymin;
472 char xmax;
473 char ymax;
474 } __attribute__ ((packed)) uab;
475 struct { /* Alternate Usable Area Self-Defining Parameter */
476 char l; /* Length of this Self-Defining Parm */
477 char sdpid; /* 0x02 if Alternate Usable Area */
478 char res;
479 char auaid; /* 0x01 is Id for the A U A */
480 short wauai; /* Width of AUAi */
481 short hauai; /* Height of AUAi */
482 char auaunits; /* 0x00:in, 0x01:mm */
483 int auaxr;
484 int auayr;
485 char awauai;
486 char ahauai;
487 } __attribute__ ((packed)) aua;
488} __attribute__ ((packed));
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static struct diag210 raw3270_init_diag210;
491static DECLARE_MUTEX(raw3270_init_sem);
492
493static int
494raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
495 struct irb *irb)
496{
497 /*
498 * Unit-Check Processing:
499 * Expect Command Reject or Intervention Required.
500 */
501 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
502 /* Request finished abnormally. */
503 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
504 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
505 return RAW3270_IO_BUSY;
506 }
507 }
508 if (rq) {
509 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
510 if (irb->ecw[0] & SNS0_CMD_REJECT)
511 rq->rc = -EOPNOTSUPP;
512 else
513 rq->rc = -EIO;
514 } else
515 /* Request finished normally. Copy residual count. */
516 rq->rescnt = irb->scsw.count;
517 }
518 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
519 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
520 wake_up(&raw3270_wait_queue);
521 }
522 return RAW3270_IO_DONE;
523}
524
525static struct raw3270_fn raw3270_init_fn = {
526 .intv = raw3270_init_irq
527};
528
529static struct raw3270_view raw3270_init_view = {
530 .fn = &raw3270_init_fn
531};
532
533/*
534 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
535 * Wait for end of request. The request must have been started
536 * with raw3270_start, rc = 0. The device lock may NOT have been
537 * released between calling raw3270_start and raw3270_wait.
538 */
539static void
540raw3270_wake_init(struct raw3270_request *rq, void *data)
541{
542 wake_up((wait_queue_head_t *) data);
543}
544
545/*
546 * Special wait function that can cope with console initialization.
547 */
548static int
549raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
550 struct raw3270_request *rq)
551{
552 unsigned long flags;
553 wait_queue_head_t wq;
554 int rc;
555
556#ifdef CONFIG_TN3270_CONSOLE
557 if (raw3270_registered == 0) {
558 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
559 rq->callback = 0;
560 rc = __raw3270_start(rp, view, rq);
561 if (rc == 0)
562 while (!raw3270_request_final(rq)) {
563 wait_cons_dev();
564 barrier();
565 }
566 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
567 return rq->rc;
568 }
569#endif
570 init_waitqueue_head(&wq);
571 rq->callback = raw3270_wake_init;
572 rq->callback_data = &wq;
573 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
574 rc = __raw3270_start(rp, view, rq);
575 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
576 if (rc)
577 return rc;
578 /* Now wait for the completion. */
579 rc = wait_event_interruptible(wq, raw3270_request_final(rq));
580 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
581 raw3270_halt_io(view->dev, rq);
582 /* No wait for the halt to complete. */
583 wait_event(wq, raw3270_request_final(rq));
584 return -ERESTARTSYS;
585 }
586 return rq->rc;
587}
588
589static int
590__raw3270_size_device_vm(struct raw3270 *rp)
591{
592 int rc, model;
593
594 raw3270_init_diag210.vrdcdvno =
595 _ccw_device_get_device_number(rp->cdev);
596 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
716 down(&raw3270_init_sem);
717 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 raw3270_init_view.dev = 0;
724 rp->view = 0;
725 up(&raw3270_init_sem);
726 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
752 down(&raw3270_init_sem);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 raw3270_init_view.dev = 0;
766 rp->view = 0;
767 up(&raw3270_init_sem);
768 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 */
851struct raw3270 *
852raw3270_setup_console(struct ccw_device *cdev)
853{
854 struct raw3270 *rp;
855 char *ascebc;
856 int rc;
857
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200858 rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 ascebc = (char *) alloc_bootmem(256);
860 rc = raw3270_setup_device(cdev, rp, ascebc);
861 if (rc)
862 return ERR_PTR(rc);
863 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
864 rc = raw3270_reset_device(rp);
865 if (rc)
866 return ERR_PTR(rc);
867 rc = raw3270_size_device(rp);
868 if (rc)
869 return ERR_PTR(rc);
870 rc = raw3270_reset_device(rp);
871 if (rc)
872 return ERR_PTR(rc);
873 set_bit(RAW3270_FLAGS_READY, &rp->flags);
874 return rp;
875}
876
877void
878raw3270_wait_cons_dev(struct raw3270 *rp)
879{
880 unsigned long flags;
881
882 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
883 wait_cons_dev();
884 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
885}
886
887#endif
888
889/*
890 * Create a 3270 device structure.
891 */
892static struct raw3270 *
893raw3270_create_device(struct ccw_device *cdev)
894{
895 struct raw3270 *rp;
896 char *ascebc;
897 int rc;
898
Martin Schwidefsky132fab12006-06-29 14:57:39 +0200899 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!rp)
901 return ERR_PTR(-ENOMEM);
902 ascebc = kmalloc(256, GFP_KERNEL);
903 if (!ascebc) {
904 kfree(rp);
905 return ERR_PTR(-ENOMEM);
906 }
907 rc = raw3270_setup_device(cdev, rp, ascebc);
908 if (rc) {
909 kfree(rp->ascebc);
910 kfree(rp);
911 rp = ERR_PTR(rc);
912 }
913 /* Get reference to ccw_device structure. */
914 get_device(&cdev->dev);
915 return rp;
916}
917
918/*
919 * Activate a view.
920 */
921int
922raw3270_activate_view(struct raw3270_view *view)
923{
924 struct raw3270 *rp;
925 struct raw3270_view *oldview, *nv;
926 unsigned long flags;
927 int rc;
928
929 rp = view->dev;
930 if (!rp)
931 return -ENODEV;
932 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
933 if (rp->view == view)
934 rc = 0;
935 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
936 rc = -ENODEV;
937 else {
938 oldview = 0;
939 if (rp->view) {
940 oldview = rp->view;
941 oldview->fn->deactivate(oldview);
942 }
943 rp->view = view;
944 rc = view->fn->activate(view);
945 if (rc) {
946 /* Didn't work. Try to reactivate the old view. */
947 rp->view = oldview;
948 if (!oldview || oldview->fn->activate(oldview) != 0) {
949 /* Didn't work as well. Try any other view. */
950 list_for_each_entry(nv, &rp->view_list, list)
951 if (nv != view && nv != oldview) {
952 rp->view = nv;
953 if (nv->fn->activate(nv) == 0)
954 break;
955 rp->view = 0;
956 }
957 }
958 }
959 }
960 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
961 return rc;
962}
963
964/*
965 * Deactivate current view.
966 */
967void
968raw3270_deactivate_view(struct raw3270_view *view)
969{
970 unsigned long flags;
971 struct raw3270 *rp;
972
973 rp = view->dev;
974 if (!rp)
975 return;
976 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
977 if (rp->view == view) {
978 view->fn->deactivate(view);
979 rp->view = 0;
980 /* Move deactivated view to end of list. */
981 list_del_init(&view->list);
982 list_add_tail(&view->list, &rp->view_list);
983 /* Try to activate another view. */
984 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
Richard Hitted3cb6f2005-10-30 15:00:10 -0800985 list_for_each_entry(view, &rp->view_list, list) {
986 rp->view = view;
987 if (view->fn->activate(view) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
Richard Hitted3cb6f2005-10-30 15:00:10 -0800989 rp->view = 0;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 }
993 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
994}
995
996/*
997 * Add view to device with minor "minor".
998 */
999int
1000raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
1001{
1002 unsigned long flags;
1003 struct raw3270 *rp;
1004 int rc;
1005
Richard Hitted3cb6f2005-10-30 15:00:10 -08001006 if (minor <= 0)
1007 return -ENODEV;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001008 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 rc = -ENODEV;
1010 list_for_each_entry(rp, &raw3270_devices, list) {
1011 if (rp->minor != minor)
1012 continue;
1013 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1014 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1015 atomic_set(&view->ref_count, 2);
1016 view->dev = rp;
1017 view->fn = fn;
1018 view->model = rp->model;
1019 view->rows = rp->rows;
1020 view->cols = rp->cols;
1021 view->ascebc = rp->ascebc;
1022 spin_lock_init(&view->lock);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001023 list_add(&view->list, &rp->view_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 rc = 0;
1025 }
1026 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1027 break;
1028 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001029 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return rc;
1031}
1032
1033/*
1034 * Find specific view of device with minor "minor".
1035 */
1036struct raw3270_view *
1037raw3270_find_view(struct raw3270_fn *fn, int minor)
1038{
1039 struct raw3270 *rp;
1040 struct raw3270_view *view, *tmp;
1041 unsigned long flags;
1042
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001043 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 view = ERR_PTR(-ENODEV);
1045 list_for_each_entry(rp, &raw3270_devices, list) {
1046 if (rp->minor != minor)
1047 continue;
1048 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1049 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1050 view = ERR_PTR(-ENOENT);
1051 list_for_each_entry(tmp, &rp->view_list, list) {
1052 if (tmp->fn == fn) {
1053 raw3270_get_view(tmp);
1054 view = tmp;
1055 break;
1056 }
1057 }
1058 }
1059 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1060 break;
1061 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001062 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return view;
1064}
1065
1066/*
1067 * Remove view from device and free view structure via call to view->fn->free.
1068 */
1069void
1070raw3270_del_view(struct raw3270_view *view)
1071{
1072 unsigned long flags;
1073 struct raw3270 *rp;
1074 struct raw3270_view *nv;
1075
1076 rp = view->dev;
1077 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1078 if (rp->view == view) {
1079 view->fn->deactivate(view);
1080 rp->view = 0;
1081 }
1082 list_del_init(&view->list);
1083 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1084 /* Try to activate another view. */
1085 list_for_each_entry(nv, &rp->view_list, list) {
Richard Hitted3cb6f2005-10-30 15:00:10 -08001086 if (nv->fn->activate(nv) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 rp->view = nv;
1088 break;
1089 }
1090 }
1091 }
1092 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1093 /* Wait for reference counter to drop to zero. */
1094 atomic_dec(&view->ref_count);
1095 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1096 if (view->fn->free)
1097 view->fn->free(view);
1098}
1099
1100/*
1101 * Remove a 3270 device structure.
1102 */
1103static void
1104raw3270_delete_device(struct raw3270 *rp)
1105{
1106 struct ccw_device *cdev;
1107
1108 /* Remove from device chain. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001109 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001110 if (rp->clttydev)
1111 class_device_destroy(class3270,
1112 MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1113 if (rp->cltubdev)
1114 class_device_destroy(class3270,
1115 MKDEV(IBM_FS3270_MAJOR, rp->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 list_del_init(&rp->list);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001117 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /* Disconnect from ccw_device. */
1120 cdev = rp->cdev;
1121 rp->cdev = 0;
1122 cdev->dev.driver_data = 0;
1123 cdev->handler = 0;
1124
1125 /* Put ccw_device structure. */
1126 put_device(&cdev->dev);
1127
1128 /* Now free raw3270 structure. */
1129 kfree(rp->ascebc);
1130 kfree(rp);
1131}
1132
1133static int
1134raw3270_probe (struct ccw_device *cdev)
1135{
1136 return 0;
1137}
1138
1139/*
1140 * Additional attributes for a 3270 device
1141 */
1142static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001143raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 return snprintf(buf, PAGE_SIZE, "%i\n",
1146 ((struct raw3270 *) dev->driver_data)->model);
1147}
1148static DEVICE_ATTR(model, 0444, raw3270_model_show, 0);
1149
1150static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001151raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 return snprintf(buf, PAGE_SIZE, "%i\n",
1154 ((struct raw3270 *) dev->driver_data)->rows);
1155}
1156static DEVICE_ATTR(rows, 0444, raw3270_rows_show, 0);
1157
1158static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -04001159raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 return snprintf(buf, PAGE_SIZE, "%i\n",
1162 ((struct raw3270 *) dev->driver_data)->cols);
1163}
1164static DEVICE_ATTR(columns, 0444, raw3270_columns_show, 0);
1165
1166static struct attribute * raw3270_attrs[] = {
1167 &dev_attr_model.attr,
1168 &dev_attr_rows.attr,
1169 &dev_attr_columns.attr,
1170 NULL,
1171};
1172
1173static struct attribute_group raw3270_attr_group = {
1174 .attrs = raw3270_attrs,
1175};
1176
1177static void
1178raw3270_create_attributes(struct raw3270 *rp)
1179{
1180 //FIXME: check return code
1181 sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001182 rp->clttydev =
Martin Schwidefskyfc71fe42005-11-18 01:11:03 -08001183 class_device_create(class3270, NULL,
Richard Hitted3cb6f2005-10-30 15:00:10 -08001184 MKDEV(IBM_TTY3270_MAJOR, rp->minor),
1185 &rp->cdev->dev, "tty%s",
1186 rp->cdev->dev.bus_id);
1187 rp->cltubdev =
Martin Schwidefskyfc71fe42005-11-18 01:11:03 -08001188 class_device_create(class3270, NULL,
Richard Hitted3cb6f2005-10-30 15:00:10 -08001189 MKDEV(IBM_FS3270_MAJOR, rp->minor),
1190 &rp->cdev->dev, "tub%s",
1191 rp->cdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194/*
1195 * Notifier for device addition/removal
1196 */
1197struct raw3270_notifier {
1198 struct list_head list;
1199 void (*notifier)(int, int);
1200};
1201
1202static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
1203
1204int raw3270_register_notifier(void (*notifier)(int, int))
1205{
1206 struct raw3270_notifier *np;
1207 struct raw3270 *rp;
1208
1209 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1210 if (!np)
1211 return -ENOMEM;
1212 np->notifier = notifier;
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001213 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 list_add_tail(&np->list, &raw3270_notifier);
1215 list_for_each_entry(rp, &raw3270_devices, list) {
1216 get_device(&rp->cdev->dev);
1217 notifier(rp->minor, 1);
1218 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001219 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return 0;
1221}
1222
1223void raw3270_unregister_notifier(void (*notifier)(int, int))
1224{
1225 struct raw3270_notifier *np;
1226
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001227 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 list_for_each_entry(np, &raw3270_notifier, list)
1229 if (np->notifier == notifier) {
1230 list_del(&np->list);
1231 kfree(np);
1232 break;
1233 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001234 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237/*
1238 * Set 3270 device online.
1239 */
1240static int
1241raw3270_set_online (struct ccw_device *cdev)
1242{
1243 struct raw3270 *rp;
1244 struct raw3270_notifier *np;
1245 int rc;
1246
1247 rp = raw3270_create_device(cdev);
1248 if (IS_ERR(rp))
1249 return PTR_ERR(rp);
1250 rc = raw3270_reset_device(rp);
1251 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001252 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 rc = raw3270_size_device(rp);
1254 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001255 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 rc = raw3270_reset_device(rp);
1257 if (rc)
Richard Hitted3cb6f2005-10-30 15:00:10 -08001258 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 raw3270_create_attributes(rp);
1260 set_bit(RAW3270_FLAGS_READY, &rp->flags);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001261 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 list_for_each_entry(np, &raw3270_notifier, list)
1263 np->notifier(rp->minor, 1);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001264 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001266
1267failure:
1268 raw3270_delete_device(rp);
1269 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272/*
1273 * Remove 3270 device structure.
1274 */
1275static void
1276raw3270_remove (struct ccw_device *cdev)
1277{
1278 unsigned long flags;
1279 struct raw3270 *rp;
1280 struct raw3270_view *v;
1281 struct raw3270_notifier *np;
1282
1283 rp = cdev->dev.driver_data;
Richard Hitted3cb6f2005-10-30 15:00:10 -08001284 /*
1285 * _remove is the opposite of _probe; it's probe that
1286 * should set up rp. raw3270_remove gets entered for
1287 * devices even if they haven't been varied online.
1288 * Thus, rp may validly be NULL here.
1289 */
1290 if (rp == NULL)
1291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1293
1294 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1295
1296 /* Deactivate current view and remove all views. */
1297 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1298 if (rp->view) {
1299 rp->view->fn->deactivate(rp->view);
1300 rp->view = 0;
1301 }
1302 while (!list_empty(&rp->view_list)) {
1303 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1304 if (v->fn->release)
1305 v->fn->release(v);
1306 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1307 raw3270_del_view(v);
1308 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1309 }
1310 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1311
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001312 mutex_lock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 list_for_each_entry(np, &raw3270_notifier, list)
1314 np->notifier(rp->minor, 0);
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001315 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* Reset 3270 device. */
1318 raw3270_reset_device(rp);
1319 /* And finally remove it. */
1320 raw3270_delete_device(rp);
1321}
1322
1323/*
1324 * Set 3270 device offline.
1325 */
1326static int
1327raw3270_set_offline (struct ccw_device *cdev)
1328{
1329 struct raw3270 *rp;
1330
1331 rp = cdev->dev.driver_data;
1332 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1333 return -EBUSY;
1334 raw3270_remove(cdev);
1335 return 0;
1336}
1337
1338static struct ccw_device_id raw3270_id[] = {
1339 { CCW_DEVICE(0x3270, 0) },
1340 { CCW_DEVICE(0x3271, 0) },
1341 { CCW_DEVICE(0x3272, 0) },
1342 { CCW_DEVICE(0x3273, 0) },
1343 { CCW_DEVICE(0x3274, 0) },
1344 { CCW_DEVICE(0x3275, 0) },
1345 { CCW_DEVICE(0x3276, 0) },
1346 { CCW_DEVICE(0x3277, 0) },
1347 { CCW_DEVICE(0x3278, 0) },
1348 { CCW_DEVICE(0x3279, 0) },
1349 { CCW_DEVICE(0x3174, 0) },
1350 { /* end of list */ },
1351};
1352
1353static struct ccw_driver raw3270_ccw_driver = {
1354 .name = "3270",
1355 .owner = THIS_MODULE,
1356 .ids = raw3270_id,
1357 .probe = &raw3270_probe,
1358 .remove = &raw3270_remove,
1359 .set_online = &raw3270_set_online,
1360 .set_offline = &raw3270_set_offline,
1361};
1362
1363static int
1364raw3270_init(void)
1365{
1366 struct raw3270 *rp;
1367 int rc;
1368
1369 if (raw3270_registered)
1370 return 0;
1371 raw3270_registered = 1;
1372 rc = ccw_driver_register(&raw3270_ccw_driver);
1373 if (rc == 0) {
1374 /* Create attributes for early (= console) device. */
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001375 mutex_lock(&raw3270_mutex);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001376 class3270 = class_create(THIS_MODULE, "3270");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 list_for_each_entry(rp, &raw3270_devices, list) {
1378 get_device(&rp->cdev->dev);
1379 raw3270_create_attributes(rp);
1380 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001381 mutex_unlock(&raw3270_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 return rc;
1384}
1385
1386static void
1387raw3270_exit(void)
1388{
1389 ccw_driver_unregister(&raw3270_ccw_driver);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001390 class_destroy(class3270);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393MODULE_LICENSE("GPL");
1394
1395module_init(raw3270_init);
1396module_exit(raw3270_exit);
1397
1398EXPORT_SYMBOL(raw3270_request_alloc);
1399EXPORT_SYMBOL(raw3270_request_free);
1400EXPORT_SYMBOL(raw3270_request_reset);
1401EXPORT_SYMBOL(raw3270_request_set_cmd);
1402EXPORT_SYMBOL(raw3270_request_add_data);
1403EXPORT_SYMBOL(raw3270_request_set_data);
1404EXPORT_SYMBOL(raw3270_request_set_idal);
1405EXPORT_SYMBOL(raw3270_buffer_address);
1406EXPORT_SYMBOL(raw3270_add_view);
1407EXPORT_SYMBOL(raw3270_del_view);
1408EXPORT_SYMBOL(raw3270_find_view);
1409EXPORT_SYMBOL(raw3270_activate_view);
1410EXPORT_SYMBOL(raw3270_deactivate_view);
1411EXPORT_SYMBOL(raw3270_start);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001412EXPORT_SYMBOL(raw3270_start_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413EXPORT_SYMBOL(raw3270_start_irq);
Richard Hitted3cb6f2005-10-30 15:00:10 -08001414EXPORT_SYMBOL(raw3270_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415EXPORT_SYMBOL(raw3270_register_notifier);
1416EXPORT_SYMBOL(raw3270_unregister_notifier);
1417EXPORT_SYMBOL(raw3270_wait_queue);