[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / s390 / char / raw3270.c
blob1026f2bc318565334daa386321f592f125691010
1 /*
2 * drivers/s390/char/raw3270.c
3 * IBM/3270 Driver - core functions.
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 */
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>
22 #include <asm/ccwdev.h>
23 #include <asm/cio.h>
24 #include <asm/ebcdic.h>
26 #include "raw3270.h"
28 #include <linux/major.h>
29 #include <linux/kdev_t.h>
30 #include <linux/device.h>
32 struct class *class3270;
34 /* The main 3270 data structure. */
35 struct raw3270 {
36 struct list_head list;
37 struct ccw_device *cdev;
38 int minor;
40 short model, rows, cols;
41 unsigned long flags;
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. */
47 struct timer_list timer; /* Device timer. */
49 unsigned char *ascebc; /* ascii -> ebcdic table */
50 struct class_device *clttydev; /* 3270-class tty device ptr */
51 struct class_device *cltubdev; /* 3270-class tub device ptr */
54 /* raw3270->flags */
55 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
56 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
57 #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
58 #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
59 #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
61 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
62 static DECLARE_MUTEX(raw3270_sem);
64 /* List of 3270 devices. */
65 static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
68 * Flag to indicate if the driver has been registered. Some operations
69 * like waiting for the end of i/o need to be done differently as long
70 * as the kernel is still starting up (console support).
72 static int raw3270_registered;
74 /* Module parameters */
75 static int tubxcorrect = 0;
76 module_param(tubxcorrect, bool, 0);
79 * Wait queue for device init/delete, view delete.
81 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
84 * Encode array for 12 bit 3270 addresses.
86 unsigned char raw3270_ebcgraf[64] = {
87 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
88 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
89 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
90 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
91 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
92 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
93 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
94 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
97 void
98 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
100 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
101 cp[0] = (addr >> 8) & 0x3f;
102 cp[1] = addr & 0xff;
103 } else {
104 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
105 cp[1] = raw3270_ebcgraf[addr & 0x3f];
110 * Allocate a new 3270 ccw request
112 struct raw3270_request *
113 raw3270_request_alloc(size_t size)
115 struct raw3270_request *rq;
117 /* Allocate request structure */
118 rq = kmalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
119 if (!rq)
120 return ERR_PTR(-ENOMEM);
121 memset(rq, 0, sizeof(struct raw3270_request));
123 /* alloc output buffer. */
124 if (size > 0) {
125 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
126 if (!rq->buffer) {
127 kfree(rq);
128 return ERR_PTR(-ENOMEM);
131 rq->size = size;
132 INIT_LIST_HEAD(&rq->list);
135 * Setup ccw.
137 rq->ccw.cda = __pa(rq->buffer);
138 rq->ccw.flags = CCW_FLAG_SLI;
140 return rq;
143 #ifdef CONFIG_TN3270_CONSOLE
145 * Allocate a new 3270 ccw request from bootmem. Only works very
146 * early in the boot process. Only con3270.c should be using this.
148 struct raw3270_request *
149 raw3270_request_alloc_bootmem(size_t size)
151 struct raw3270_request *rq;
153 rq = alloc_bootmem_low(sizeof(struct raw3270));
154 if (!rq)
155 return ERR_PTR(-ENOMEM);
156 memset(rq, 0, sizeof(struct raw3270_request));
158 /* alloc output buffer. */
159 if (size > 0) {
160 rq->buffer = alloc_bootmem_low(size);
161 if (!rq->buffer) {
162 free_bootmem((unsigned long) rq,
163 sizeof(struct raw3270));
164 return ERR_PTR(-ENOMEM);
167 rq->size = size;
168 INIT_LIST_HEAD(&rq->list);
171 * Setup ccw.
173 rq->ccw.cda = __pa(rq->buffer);
174 rq->ccw.flags = CCW_FLAG_SLI;
176 return rq;
178 #endif
181 * Free 3270 ccw request
183 void
184 raw3270_request_free (struct raw3270_request *rq)
186 kfree(rq->buffer);
187 kfree(rq);
191 * Reset request to initial state.
193 void
194 raw3270_request_reset(struct raw3270_request *rq)
196 BUG_ON(!list_empty(&rq->list));
197 rq->ccw.cmd_code = 0;
198 rq->ccw.count = 0;
199 rq->ccw.cda = __pa(rq->buffer);
200 rq->ccw.flags = CCW_FLAG_SLI;
201 rq->rescnt = 0;
202 rq->rc = 0;
206 * Set command code to ccw of a request.
208 void
209 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
211 rq->ccw.cmd_code = cmd;
215 * Add data fragment to output buffer.
218 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
220 if (size + rq->ccw.count > rq->size)
221 return -E2BIG;
222 memcpy(rq->buffer + rq->ccw.count, data, size);
223 rq->ccw.count += size;
224 return 0;
228 * Set address/length pair to ccw of a request.
230 void
231 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
233 rq->ccw.cda = __pa(data);
234 rq->ccw.count = size;
238 * Set idal buffer to ccw of a request.
240 void
241 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
243 rq->ccw.cda = __pa(ib->data);
244 rq->ccw.count = ib->size;
245 rq->ccw.flags |= CCW_FLAG_IDA;
249 * Stop running ccw.
251 static int
252 raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
254 int retries;
255 int rc;
257 if (raw3270_request_final(rq))
258 return 0;
259 /* Check if interrupt has already been processed */
260 for (retries = 0; retries < 5; retries++) {
261 if (retries < 2)
262 rc = ccw_device_halt(rp->cdev, (long) rq);
263 else
264 rc = ccw_device_clear(rp->cdev, (long) rq);
265 if (rc == 0)
266 break; /* termination successful */
268 return rc;
271 static int
272 raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
274 unsigned long flags;
275 int rc;
277 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
278 rc = raw3270_halt_io_nolock(rp, rq);
279 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
280 return rc;
284 * Add the request to the request queue, try to start it if the
285 * 3270 device is idle. Return without waiting for end of i/o.
287 static int
288 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
289 struct raw3270_request *rq)
291 rq->view = view;
292 raw3270_get_view(view);
293 if (list_empty(&rp->req_queue) &&
294 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
295 /* No other requests are on the queue. Start this one. */
296 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
297 (unsigned long) rq, 0, 0);
298 if (rq->rc) {
299 raw3270_put_view(view);
300 return rq->rc;
303 list_add_tail(&rq->list, &rp->req_queue);
304 return 0;
308 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
310 unsigned long flags;
311 struct raw3270 *rp;
312 int rc;
314 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
315 rp = view->dev;
316 if (!rp || rp->view != view)
317 rc = -EACCES;
318 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
319 rc = -ENODEV;
320 else
321 rc = __raw3270_start(rp, view, rq);
322 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
323 return rc;
327 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
329 struct raw3270 *rp;
330 int rc;
332 rp = view->dev;
333 if (!rp || rp->view != view)
334 rc = -EACCES;
335 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
336 rc = -ENODEV;
337 else
338 rc = __raw3270_start(rp, view, rq);
339 return rc;
343 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
345 struct raw3270 *rp;
347 rp = view->dev;
348 rq->view = view;
349 raw3270_get_view(view);
350 list_add_tail(&rq->list, &rp->req_queue);
351 return 0;
355 * 3270 interrupt routine, called from the ccw_device layer
357 static void
358 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
360 struct raw3270 *rp;
361 struct raw3270_view *view;
362 struct raw3270_request *rq;
363 int rc;
365 rp = (struct raw3270 *) cdev->dev.driver_data;
366 if (!rp)
367 return;
368 rq = (struct raw3270_request *) intparm;
369 view = rq ? rq->view : rp->view;
371 if (IS_ERR(irb))
372 rc = RAW3270_IO_RETRY;
373 else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
374 rq->rc = -EIO;
375 rc = RAW3270_IO_DONE;
376 } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
377 DEV_STAT_UNIT_EXCEP)) {
378 /* Handle CE-DE-UE and subsequent UDE */
379 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
380 rc = RAW3270_IO_BUSY;
381 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
382 /* Wait for UDE if busy flag is set. */
383 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
384 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
385 /* Got it, now retry. */
386 rc = RAW3270_IO_RETRY;
387 } else
388 rc = RAW3270_IO_BUSY;
389 } else if (view)
390 rc = view->fn->intv(view, rq, irb);
391 else
392 rc = RAW3270_IO_DONE;
394 switch (rc) {
395 case RAW3270_IO_DONE:
396 break;
397 case RAW3270_IO_BUSY:
399 * Intervention required by the operator. We have to wait
400 * for unsolicited device end.
402 return;
403 case RAW3270_IO_RETRY:
404 if (!rq)
405 break;
406 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
407 (unsigned long) rq, 0, 0);
408 if (rq->rc == 0)
409 return; /* Sucessfully restarted. */
410 break;
411 case RAW3270_IO_STOP:
412 if (!rq)
413 break;
414 raw3270_halt_io_nolock(rp, rq);
415 rq->rc = -EIO;
416 break;
417 default:
418 BUG();
420 if (rq) {
421 BUG_ON(list_empty(&rq->list));
422 /* The request completed, remove from queue and do callback. */
423 list_del_init(&rq->list);
424 if (rq->callback)
425 rq->callback(rq, rq->callback_data);
426 /* Do put_device for get_device in raw3270_start. */
427 raw3270_put_view(view);
430 * Try to start each request on request queue until one is
431 * started successful.
433 while (!list_empty(&rp->req_queue)) {
434 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
435 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
436 (unsigned long) rq, 0, 0);
437 if (rq->rc == 0)
438 break;
439 /* Start failed. Remove request and do callback. */
440 list_del_init(&rq->list);
441 if (rq->callback)
442 rq->callback(rq, rq->callback_data);
443 /* Do put_device for get_device in raw3270_start. */
444 raw3270_put_view(view);
449 * Size sensing.
452 struct raw3270_ua { /* Query Reply structure for Usable Area */
453 struct { /* Usable Area Query Reply Base */
454 short l; /* Length of this structured field */
455 char sfid; /* 0x81 if Query Reply */
456 char qcode; /* 0x81 if Usable Area */
457 char flags0;
458 char flags1;
459 short w; /* Width of usable area */
460 short h; /* Heigth of usavle area */
461 char units; /* 0x00:in; 0x01:mm */
462 int xr;
463 int yr;
464 char aw;
465 char ah;
466 short buffsz; /* Character buffer size, bytes */
467 char xmin;
468 char ymin;
469 char xmax;
470 char ymax;
471 } __attribute__ ((packed)) uab;
472 struct { /* Alternate Usable Area Self-Defining Parameter */
473 char l; /* Length of this Self-Defining Parm */
474 char sdpid; /* 0x02 if Alternate Usable Area */
475 char res;
476 char auaid; /* 0x01 is Id for the A U A */
477 short wauai; /* Width of AUAi */
478 short hauai; /* Height of AUAi */
479 char auaunits; /* 0x00:in, 0x01:mm */
480 int auaxr;
481 int auayr;
482 char awauai;
483 char ahauai;
484 } __attribute__ ((packed)) aua;
485 } __attribute__ ((packed));
487 static unsigned char raw3270_init_data[256];
488 static struct raw3270_request raw3270_init_request;
489 static struct diag210 raw3270_init_diag210;
490 static DECLARE_MUTEX(raw3270_init_sem);
492 static int
493 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
494 struct irb *irb)
497 * Unit-Check Processing:
498 * Expect Command Reject or Intervention Required.
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;
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;
517 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
518 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
519 wake_up(&raw3270_wait_queue);
521 return RAW3270_IO_DONE;
524 static struct raw3270_fn raw3270_init_fn = {
525 .intv = raw3270_init_irq
528 static struct raw3270_view raw3270_init_view = {
529 .fn = &raw3270_init_fn
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.
538 static void
539 raw3270_wake_init(struct raw3270_request *rq, void *data)
541 wake_up((wait_queue_head_t *) data);
545 * Special wait function that can cope with console initialization.
547 static int
548 raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
549 struct raw3270_request *rq)
551 unsigned long flags;
552 wait_queue_head_t wq;
553 int rc;
555 #ifdef CONFIG_TN3270_CONSOLE
556 if (raw3270_registered == 0) {
557 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
558 rq->callback = 0;
559 rc = __raw3270_start(rp, view, rq);
560 if (rc == 0)
561 while (!raw3270_request_final(rq)) {
562 wait_cons_dev();
563 barrier();
565 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
566 return rq->rc;
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;
585 return rq->rc;
588 static int
589 __raw3270_size_device_vm(struct raw3270 *rp)
591 int rc, model;
593 raw3270_init_diag210.vrdcdvno =
594 _ccw_device_get_device_number(rp->cdev);
595 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;
626 return rc;
629 static int
630 __raw3270_size_device(struct raw3270 *rp)
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;
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').
647 memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
648 memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
649 /* Store 'read partition' data stream to raw3270_init_data */
650 memcpy(raw3270_init_data, wbuf, sizeof(wbuf));
651 INIT_LIST_HEAD(&raw3270_init_request.list);
652 raw3270_init_request.ccw.cmd_code = TC_WRITESF;
653 raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
654 raw3270_init_request.ccw.count = sizeof(wbuf);
655 raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
657 rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
658 if (rc)
659 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
660 return rc;
662 /* Wait for attention interrupt. */
663 #ifdef CONFIG_TN3270_CONSOLE
664 if (raw3270_registered == 0) {
665 unsigned long flags;
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;
679 * The device accepted the 'read partition' command. Now
680 * set up a read ccw and issue it.
682 raw3270_init_request.ccw.cmd_code = TC_READMOD;
683 raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
684 raw3270_init_request.ccw.count = sizeof(raw3270_init_data);
685 raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
686 rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
687 if (rc)
688 return rc;
689 /* Got a Query Reply */
690 count = sizeof(raw3270_init_data) - raw3270_init_request.rescnt;
691 uap = (struct raw3270_ua *) (raw3270_init_data + 1);
692 /* Paranoia check. */
693 if (raw3270_init_data[0] != 0x88 || uap->uab.qcode != 0x81)
694 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;
707 return 0;
710 static int
711 raw3270_size_device(struct raw3270 *rp)
713 int rc;
715 down(&raw3270_init_sem);
716 rp->view = &raw3270_init_view;
717 raw3270_init_view.dev = rp;
718 if (MACHINE_IS_VM)
719 rc = __raw3270_size_device_vm(rp);
720 else
721 rc = __raw3270_size_device(rp);
722 raw3270_init_view.dev = 0;
723 rp->view = 0;
724 up(&raw3270_init_sem);
725 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;
736 } 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;
743 return rc;
746 static int
747 raw3270_reset_device(struct raw3270 *rp)
749 int rc;
751 down(&raw3270_init_sem);
752 memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
753 memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
754 /* Store reset data stream to raw3270_init_data/raw3270_init_request */
755 raw3270_init_data[0] = TW_KR;
756 INIT_LIST_HEAD(&raw3270_init_request.list);
757 raw3270_init_request.ccw.cmd_code = TC_EWRITEA;
758 raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
759 raw3270_init_request.ccw.count = 1;
760 raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
761 rp->view = &raw3270_init_view;
762 raw3270_init_view.dev = rp;
763 rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
764 raw3270_init_view.dev = 0;
765 rp->view = 0;
766 up(&raw3270_init_sem);
767 return rc;
771 raw3270_reset(struct raw3270_view *view)
773 struct raw3270 *rp;
774 int rc;
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;
787 * Setup new 3270 device.
789 static int
790 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
792 struct list_head *l;
793 struct raw3270 *tmp;
794 int minor;
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;
805 rp->ascebc = ascebc;
807 /* Set defaults. */
808 rp->rows = 24;
809 rp->cols = 80;
811 INIT_LIST_HEAD(&rp->req_queue);
812 INIT_LIST_HEAD(&rp->view_list);
815 * Add device to list and find the smallest unused minor
816 * number for it. Note: there is no device with minor 0,
817 * see special case for fs3270.c:fs3270_open().
819 down(&raw3270_sem);
820 /* Keep the list sorted. */
821 minor = RAW3270_FIRSTMINOR;
822 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;
830 minor++;
832 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
833 rp->minor = minor;
834 list_add_tail(&rp->list, &raw3270_devices);
836 up(&raw3270_sem);
837 /* 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;
846 #ifdef CONFIG_TN3270_CONSOLE
848 * Setup 3270 device configured as console.
850 struct raw3270 *
851 raw3270_setup_console(struct ccw_device *cdev)
853 struct raw3270 *rp;
854 char *ascebc;
855 int rc;
857 rp = (struct raw3270 *) alloc_bootmem(sizeof(struct raw3270));
858 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;
876 void
877 raw3270_wait_cons_dev(struct raw3270 *rp)
879 unsigned long flags;
881 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
882 wait_cons_dev();
883 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
886 #endif
889 * Create a 3270 device structure.
891 static struct raw3270 *
892 raw3270_create_device(struct ccw_device *cdev)
894 struct raw3270 *rp;
895 char *ascebc;
896 int rc;
898 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL);
899 if (!rp)
900 return ERR_PTR(-ENOMEM);
901 ascebc = kmalloc(256, GFP_KERNEL);
902 if (!ascebc) {
903 kfree(rp);
904 return ERR_PTR(-ENOMEM);
906 rc = raw3270_setup_device(cdev, rp, ascebc);
907 if (rc) {
908 kfree(rp->ascebc);
909 kfree(rp);
910 rp = ERR_PTR(rc);
912 /* Get reference to ccw_device structure. */
913 get_device(&cdev->dev);
914 return rp;
918 * Activate a view.
921 raw3270_activate_view(struct raw3270_view *view)
923 struct raw3270 *rp;
924 struct raw3270_view *oldview, *nv;
925 unsigned long flags;
926 int rc;
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 {
937 oldview = 0;
938 if (rp->view) {
939 oldview = rp->view;
940 oldview->fn->deactivate(oldview);
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;
954 rp->view = 0;
959 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
960 return rc;
964 * Deactivate current view.
966 void
967 raw3270_deactivate_view(struct raw3270_view *view)
969 unsigned long flags;
970 struct raw3270 *rp;
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);
978 rp->view = 0;
979 /* 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)) {
984 list_for_each_entry(view, &rp->view_list, list) {
985 rp->view = view;
986 if (view->fn->activate(view) == 0)
987 break;
988 rp->view = 0;
992 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
996 * Add view to device with minor "minor".
999 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
1001 unsigned long flags;
1002 struct raw3270 *rp;
1003 int rc;
1005 if (minor <= 0)
1006 return -ENODEV;
1007 down(&raw3270_sem);
1008 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);
1022 list_add(&view->list, &rp->view_list);
1023 rc = 0;
1025 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1026 break;
1028 up(&raw3270_sem);
1029 return rc;
1033 * Find specific view of device with minor "minor".
1035 struct raw3270_view *
1036 raw3270_find_view(struct raw3270_fn *fn, int minor)
1038 struct raw3270 *rp;
1039 struct raw3270_view *view, *tmp;
1040 unsigned long flags;
1042 down(&raw3270_sem);
1043 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;
1058 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1059 break;
1061 up(&raw3270_sem);
1062 return view;
1066 * Remove view from device and free view structure via call to view->fn->free.
1068 void
1069 raw3270_del_view(struct raw3270_view *view)
1071 unsigned long flags;
1072 struct raw3270 *rp;
1073 struct raw3270_view *nv;
1075 rp = view->dev;
1076 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1077 if (rp->view == view) {
1078 view->fn->deactivate(view);
1079 rp->view = 0;
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) {
1085 if (nv->fn->activate(nv) == 0) {
1086 rp->view = nv;
1087 break;
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);
1100 * Remove a 3270 device structure.
1102 static void
1103 raw3270_delete_device(struct raw3270 *rp)
1105 struct ccw_device *cdev;
1107 /* Remove from device chain. */
1108 down(&raw3270_sem);
1109 if (rp->clttydev)
1110 class_device_destroy(class3270,
1111 MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1112 if (rp->cltubdev)
1113 class_device_destroy(class3270,
1114 MKDEV(IBM_FS3270_MAJOR, rp->minor));
1115 list_del_init(&rp->list);
1116 up(&raw3270_sem);
1118 /* Disconnect from ccw_device. */
1119 cdev = rp->cdev;
1120 rp->cdev = 0;
1121 cdev->dev.driver_data = 0;
1122 cdev->handler = 0;
1124 /* Put ccw_device structure. */
1125 put_device(&cdev->dev);
1127 /* Now free raw3270 structure. */
1128 kfree(rp->ascebc);
1129 kfree(rp);
1132 static int
1133 raw3270_probe (struct ccw_device *cdev)
1135 return 0;
1139 * Additional attributes for a 3270 device
1141 static ssize_t
1142 raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
1144 return snprintf(buf, PAGE_SIZE, "%i\n",
1145 ((struct raw3270 *) dev->driver_data)->model);
1147 static DEVICE_ATTR(model, 0444, raw3270_model_show, 0);
1149 static ssize_t
1150 raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
1152 return snprintf(buf, PAGE_SIZE, "%i\n",
1153 ((struct raw3270 *) dev->driver_data)->rows);
1155 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, 0);
1157 static ssize_t
1158 raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
1160 return snprintf(buf, PAGE_SIZE, "%i\n",
1161 ((struct raw3270 *) dev->driver_data)->cols);
1163 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, 0);
1165 static struct attribute * raw3270_attrs[] = {
1166 &dev_attr_model.attr,
1167 &dev_attr_rows.attr,
1168 &dev_attr_columns.attr,
1169 NULL,
1172 static struct attribute_group raw3270_attr_group = {
1173 .attrs = raw3270_attrs,
1176 static void
1177 raw3270_create_attributes(struct raw3270 *rp)
1179 //FIXME: check return code
1180 sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1181 rp->clttydev =
1182 class_device_create(class3270, NULL,
1183 MKDEV(IBM_TTY3270_MAJOR, rp->minor),
1184 &rp->cdev->dev, "tty%s",
1185 rp->cdev->dev.bus_id);
1186 rp->cltubdev =
1187 class_device_create(class3270, NULL,
1188 MKDEV(IBM_FS3270_MAJOR, rp->minor),
1189 &rp->cdev->dev, "tub%s",
1190 rp->cdev->dev.bus_id);
1194 * Notifier for device addition/removal
1196 struct raw3270_notifier {
1197 struct list_head list;
1198 void (*notifier)(int, int);
1201 static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
1203 int raw3270_register_notifier(void (*notifier)(int, int))
1205 struct raw3270_notifier *np;
1206 struct raw3270 *rp;
1208 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1209 if (!np)
1210 return -ENOMEM;
1211 np->notifier = notifier;
1212 down(&raw3270_sem);
1213 list_add_tail(&np->list, &raw3270_notifier);
1214 list_for_each_entry(rp, &raw3270_devices, list) {
1215 get_device(&rp->cdev->dev);
1216 notifier(rp->minor, 1);
1218 up(&raw3270_sem);
1219 return 0;
1222 void raw3270_unregister_notifier(void (*notifier)(int, int))
1224 struct raw3270_notifier *np;
1226 down(&raw3270_sem);
1227 list_for_each_entry(np, &raw3270_notifier, list)
1228 if (np->notifier == notifier) {
1229 list_del(&np->list);
1230 kfree(np);
1231 break;
1233 up(&raw3270_sem);
1237 * Set 3270 device online.
1239 static int
1240 raw3270_set_online (struct ccw_device *cdev)
1242 struct raw3270 *rp;
1243 struct raw3270_notifier *np;
1244 int rc;
1246 rp = raw3270_create_device(cdev);
1247 if (IS_ERR(rp))
1248 return PTR_ERR(rp);
1249 rc = raw3270_reset_device(rp);
1250 if (rc)
1251 goto failure;
1252 rc = raw3270_size_device(rp);
1253 if (rc)
1254 goto failure;
1255 rc = raw3270_reset_device(rp);
1256 if (rc)
1257 goto failure;
1258 raw3270_create_attributes(rp);
1259 set_bit(RAW3270_FLAGS_READY, &rp->flags);
1260 down(&raw3270_sem);
1261 list_for_each_entry(np, &raw3270_notifier, list)
1262 np->notifier(rp->minor, 1);
1263 up(&raw3270_sem);
1264 return 0;
1266 failure:
1267 raw3270_delete_device(rp);
1268 return rc;
1272 * Remove 3270 device structure.
1274 static void
1275 raw3270_remove (struct ccw_device *cdev)
1277 unsigned long flags;
1278 struct raw3270 *rp;
1279 struct raw3270_view *v;
1280 struct raw3270_notifier *np;
1282 rp = cdev->dev.driver_data;
1284 * _remove is the opposite of _probe; it's probe that
1285 * should set up rp. raw3270_remove gets entered for
1286 * devices even if they haven't been varied online.
1287 * Thus, rp may validly be NULL here.
1289 if (rp == NULL)
1290 return;
1291 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1293 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1295 /* Deactivate current view and remove all views. */
1296 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1297 if (rp->view) {
1298 rp->view->fn->deactivate(rp->view);
1299 rp->view = 0;
1301 while (!list_empty(&rp->view_list)) {
1302 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1303 if (v->fn->release)
1304 v->fn->release(v);
1305 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1306 raw3270_del_view(v);
1307 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1309 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1311 down(&raw3270_sem);
1312 list_for_each_entry(np, &raw3270_notifier, list)
1313 np->notifier(rp->minor, 0);
1314 up(&raw3270_sem);
1316 /* Reset 3270 device. */
1317 raw3270_reset_device(rp);
1318 /* And finally remove it. */
1319 raw3270_delete_device(rp);
1323 * Set 3270 device offline.
1325 static int
1326 raw3270_set_offline (struct ccw_device *cdev)
1328 struct raw3270 *rp;
1330 rp = cdev->dev.driver_data;
1331 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1332 return -EBUSY;
1333 raw3270_remove(cdev);
1334 return 0;
1337 static struct ccw_device_id raw3270_id[] = {
1338 { CCW_DEVICE(0x3270, 0) },
1339 { CCW_DEVICE(0x3271, 0) },
1340 { CCW_DEVICE(0x3272, 0) },
1341 { CCW_DEVICE(0x3273, 0) },
1342 { CCW_DEVICE(0x3274, 0) },
1343 { CCW_DEVICE(0x3275, 0) },
1344 { CCW_DEVICE(0x3276, 0) },
1345 { CCW_DEVICE(0x3277, 0) },
1346 { CCW_DEVICE(0x3278, 0) },
1347 { CCW_DEVICE(0x3279, 0) },
1348 { CCW_DEVICE(0x3174, 0) },
1349 { /* end of list */ },
1352 static struct ccw_driver raw3270_ccw_driver = {
1353 .name = "3270",
1354 .owner = THIS_MODULE,
1355 .ids = raw3270_id,
1356 .probe = &raw3270_probe,
1357 .remove = &raw3270_remove,
1358 .set_online = &raw3270_set_online,
1359 .set_offline = &raw3270_set_offline,
1362 static int
1363 raw3270_init(void)
1365 struct raw3270 *rp;
1366 int rc;
1368 if (raw3270_registered)
1369 return 0;
1370 raw3270_registered = 1;
1371 rc = ccw_driver_register(&raw3270_ccw_driver);
1372 if (rc == 0) {
1373 /* Create attributes for early (= console) device. */
1374 down(&raw3270_sem);
1375 class3270 = class_create(THIS_MODULE, "3270");
1376 list_for_each_entry(rp, &raw3270_devices, list) {
1377 get_device(&rp->cdev->dev);
1378 raw3270_create_attributes(rp);
1380 up(&raw3270_sem);
1382 return rc;
1385 static void
1386 raw3270_exit(void)
1388 ccw_driver_unregister(&raw3270_ccw_driver);
1389 class_destroy(class3270);
1392 MODULE_LICENSE("GPL");
1394 module_init(raw3270_init);
1395 module_exit(raw3270_exit);
1397 EXPORT_SYMBOL(raw3270_request_alloc);
1398 EXPORT_SYMBOL(raw3270_request_free);
1399 EXPORT_SYMBOL(raw3270_request_reset);
1400 EXPORT_SYMBOL(raw3270_request_set_cmd);
1401 EXPORT_SYMBOL(raw3270_request_add_data);
1402 EXPORT_SYMBOL(raw3270_request_set_data);
1403 EXPORT_SYMBOL(raw3270_request_set_idal);
1404 EXPORT_SYMBOL(raw3270_buffer_address);
1405 EXPORT_SYMBOL(raw3270_add_view);
1406 EXPORT_SYMBOL(raw3270_del_view);
1407 EXPORT_SYMBOL(raw3270_find_view);
1408 EXPORT_SYMBOL(raw3270_activate_view);
1409 EXPORT_SYMBOL(raw3270_deactivate_view);
1410 EXPORT_SYMBOL(raw3270_start);
1411 EXPORT_SYMBOL(raw3270_start_locked);
1412 EXPORT_SYMBOL(raw3270_start_irq);
1413 EXPORT_SYMBOL(raw3270_reset);
1414 EXPORT_SYMBOL(raw3270_register_notifier);
1415 EXPORT_SYMBOL(raw3270_unregister_notifier);
1416 EXPORT_SYMBOL(raw3270_wait_queue);