raw3270.c 33.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 *  drivers/s390/char/raw3270.c
 *    IBM/3270 Driver - core functions.
 *
 *  Author(s):
 *    Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
 *    Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
 *	-- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
 */

#include <linux/bootmem.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/wait.h>

#include <asm/ccwdev.h>
#include <asm/cio.h>
#include <asm/ebcdic.h>
24
#include <asm/diag.h>
Linus Torvalds's avatar
Linus Torvalds committed
25
26
27

#include "raw3270.h"

28
29
30
#include <linux/major.h>
#include <linux/kdev_t.h>
#include <linux/device.h>
31
#include <linux/mutex.h>
32

33
static struct class *class3270;
34

Linus Torvalds's avatar
Linus Torvalds committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* The main 3270 data structure. */
struct raw3270 {
	struct list_head list;
	struct ccw_device *cdev;
	int minor;

	short model, rows, cols;
	unsigned long flags;

	struct list_head req_queue;	/* Request queue. */
	struct list_head view_list;	/* List of available views. */
	struct raw3270_view *view;	/* Active view. */

	struct timer_list timer;	/* Device timer. */

	unsigned char *ascebc;		/* ascii -> ebcdic table */
51
52
	struct device *clttydev;	/* 3270-class tty device ptr */
	struct device *cltubdev;	/* 3270-class tub device ptr */
53
54
55

	struct raw3270_request init_request;
	unsigned char init_data[256];
Linus Torvalds's avatar
Linus Torvalds committed
56
57
58
59
60
61
62
63
64
65
};

/* raw3270->flags */
#define RAW3270_FLAGS_14BITADDR	0	/* 14-bit buffer addresses */
#define RAW3270_FLAGS_BUSY	1	/* Device busy, leave it alone */
#define RAW3270_FLAGS_ATTN	2	/* Device sent an ATTN interrupt */
#define RAW3270_FLAGS_READY	4	/* Device is useable by views */
#define RAW3270_FLAGS_CONSOLE	8	/* Device is the console. */

/* Semaphore to protect global data of raw3270 (devices, views, etc). */
66
static DEFINE_MUTEX(raw3270_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
67
68

/* List of 3270 devices. */
69
static LIST_HEAD(raw3270_devices);
Linus Torvalds's avatar
Linus Torvalds committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

/*
 * Flag to indicate if the driver has been registered. Some operations
 * like waiting for the end of i/o need to be done differently as long
 * as the kernel is still starting up (console support).
 */
static int raw3270_registered;

/* Module parameters */
static int tubxcorrect = 0;
module_param(tubxcorrect, bool, 0);

/*
 * Wait queue for device init/delete, view delete.
 */
DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);

/*
 * Encode array for 12 bit 3270 addresses.
 */
90
static unsigned char raw3270_ebcgraf[64] =	{
Linus Torvalds's avatar
Linus Torvalds committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
	0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
	0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
	0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
	0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
	0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
	0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
};

void
raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
{
	if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
		cp[0] = (addr >> 8) & 0x3f;
		cp[1] = addr & 0xff;
	} else {
		cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
		cp[1] = raw3270_ebcgraf[addr & 0x3f];
	}
}

/*
 * Allocate a new 3270 ccw request
 */
struct raw3270_request *
raw3270_request_alloc(size_t size)
{
	struct raw3270_request *rq;

	/* Allocate request structure */
122
	rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
Linus Torvalds's avatar
Linus Torvalds committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	if (!rq)
		return ERR_PTR(-ENOMEM);

	/* alloc output buffer. */
	if (size > 0) {
		rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
		if (!rq->buffer) {
			kfree(rq);
			return ERR_PTR(-ENOMEM);
		}
	}
	rq->size = size;
	INIT_LIST_HEAD(&rq->list);

	/*
	 * Setup ccw.
	 */
	rq->ccw.cda = __pa(rq->buffer);
	rq->ccw.flags = CCW_FLAG_SLI;

	return rq;
}

#ifdef CONFIG_TN3270_CONSOLE
/*
 * Allocate a new 3270 ccw request from bootmem. Only works very
 * early in the boot process. Only con3270.c should be using this.
 */
151
struct raw3270_request __init *raw3270_request_alloc_bootmem(size_t size)
Linus Torvalds's avatar
Linus Torvalds committed
152
153
154
155
156
157
{
	struct raw3270_request *rq;

	rq = alloc_bootmem_low(sizeof(struct raw3270));

	/* alloc output buffer. */
158
	if (size > 0)
Linus Torvalds's avatar
Linus Torvalds committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
		rq->buffer = alloc_bootmem_low(size);
	rq->size = size;
	INIT_LIST_HEAD(&rq->list);

	/*
	 * Setup ccw.
	 */
	rq->ccw.cda = __pa(rq->buffer);
	rq->ccw.flags = CCW_FLAG_SLI;

	return rq;
}
#endif

/*
 * Free 3270 ccw request
 */
void
raw3270_request_free (struct raw3270_request *rq)
{
179
	kfree(rq->buffer);
Linus Torvalds's avatar
Linus Torvalds committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
	kfree(rq);
}

/*
 * Reset request to initial state.
 */
void
raw3270_request_reset(struct raw3270_request *rq)
{
	BUG_ON(!list_empty(&rq->list));
	rq->ccw.cmd_code = 0;
	rq->ccw.count = 0;
	rq->ccw.cda = __pa(rq->buffer);
	rq->ccw.flags = CCW_FLAG_SLI;
	rq->rescnt = 0;
	rq->rc = 0;
}

/*
 * Set command code to ccw of a request.
 */
void
raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
{
	rq->ccw.cmd_code = cmd;
}

/*
 * Add data fragment to output buffer.
 */
int
raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
{
	if (size + rq->ccw.count > rq->size)
		return -E2BIG;
	memcpy(rq->buffer + rq->ccw.count, data, size);
	rq->ccw.count += size;
	return 0;
}

/*
 * Set address/length pair to ccw of a request.
 */
void
raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
{
	rq->ccw.cda = __pa(data);
	rq->ccw.count = size;
}

/*
 * Set idal buffer to ccw of a request.
 */
void
raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
{
	rq->ccw.cda = __pa(ib->data);
	rq->ccw.count = ib->size;
	rq->ccw.flags |= CCW_FLAG_IDA;
}

/*
 * Stop running ccw.
 */
static int
raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
{
	int retries;
	int rc;

	if (raw3270_request_final(rq))
		return 0;
	/* Check if interrupt has already been processed */
	for (retries = 0; retries < 5; retries++) {
		if (retries < 2)
			rc = ccw_device_halt(rp->cdev, (long) rq);
		else
			rc = ccw_device_clear(rp->cdev, (long) rq);
		if (rc == 0)
			break;		/* termination successful */
	}
	return rc;
}

static int
raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
{
	unsigned long flags;
	int rc;

	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
	rc = raw3270_halt_io_nolock(rp, rq);
	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
	return rc;
}

/*
 * Add the request to the request queue, try to start it if the
 * 3270 device is idle. Return without waiting for end of i/o.
 */
static int
__raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
		struct raw3270_request *rq)
{
	rq->view = view;
	raw3270_get_view(view);
	if (list_empty(&rp->req_queue) &&
	    !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
		/* No other requests are on the queue. Start this one. */
		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
					       (unsigned long) rq, 0, 0);
		if (rq->rc) {
			raw3270_put_view(view);
			return rq->rc;
		}
	}
	list_add_tail(&rq->list, &rp->req_queue);
	return 0;
}

int
raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
{
	unsigned long flags;
	struct raw3270 *rp;
	int rc;

	spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
	rp = view->dev;
	if (!rp || rp->view != view)
		rc = -EACCES;
	else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
		rc = -ENODEV;
	else
		rc =  __raw3270_start(rp, view, rq);
	spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
	return rc;
}

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
int
raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
{
	struct raw3270 *rp;
	int rc;

	rp = view->dev;
	if (!rp || rp->view != view)
		rc = -EACCES;
	else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
		rc = -ENODEV;
	else
		rc =  __raw3270_start(rp, view, rq);
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
int
raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
{
	struct raw3270 *rp;

	rp = view->dev;
	rq->view = view;
	raw3270_get_view(view);
	list_add_tail(&rq->list, &rp->req_queue);
	return 0;
}

/*
 * 3270 interrupt routine, called from the ccw_device layer
 */
static void
raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
{
	struct raw3270 *rp;
	struct raw3270_view *view;
	struct raw3270_request *rq;
	int rc;

	rp = (struct raw3270 *) cdev->dev.driver_data;
	if (!rp)
		return;
	rq = (struct raw3270_request *) intparm;
	view = rq ? rq->view : rp->view;

	if (IS_ERR(irb))
		rc = RAW3270_IO_RETRY;
366
	else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
Linus Torvalds's avatar
Linus Torvalds committed
367
368
		rq->rc = -EIO;
		rc = RAW3270_IO_DONE;
369
370
	} else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
					   DEV_STAT_UNIT_EXCEP)) {
Linus Torvalds's avatar
Linus Torvalds committed
371
372
373
374
375
		/* Handle CE-DE-UE and subsequent UDE */
		set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
		rc = RAW3270_IO_BUSY;
	} else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
		/* Wait for UDE if busy flag is set. */
376
		if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
Linus Torvalds's avatar
Linus Torvalds committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
			clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
			/* Got it, now retry. */
			rc = RAW3270_IO_RETRY;
		} else
			rc = RAW3270_IO_BUSY;
	} else if (view)
		rc = view->fn->intv(view, rq, irb);
	else
		rc = RAW3270_IO_DONE;

	switch (rc) {
	case RAW3270_IO_DONE:
		break;
	case RAW3270_IO_BUSY:
		/* 
		 * Intervention required by the operator. We have to wait
		 * for unsolicited device end.
		 */
		return;
	case RAW3270_IO_RETRY:
		if (!rq)
			break;
		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
					  (unsigned long) rq, 0, 0);
		if (rq->rc == 0)
			return;	/* Sucessfully restarted. */
		break;
	case RAW3270_IO_STOP:
		if (!rq)
			break;
		raw3270_halt_io_nolock(rp, rq);
		rq->rc = -EIO;
		break;
	default:
		BUG();
	}
	if (rq) {
		BUG_ON(list_empty(&rq->list));
		/* The request completed, remove from queue and do callback. */
		list_del_init(&rq->list);
		if (rq->callback)
			rq->callback(rq, rq->callback_data);
		/* Do put_device for get_device in raw3270_start. */
		raw3270_put_view(view);
	}
	/*
	 * Try to start each request on request queue until one is
	 * started successful.
	 */
	while (!list_empty(&rp->req_queue)) {
		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
					  (unsigned long) rq, 0, 0);
		if (rq->rc == 0)
			break;
		/* Start failed. Remove request and do callback. */
		list_del_init(&rq->list);
		if (rq->callback)
			rq->callback(rq, rq->callback_data);
		/* Do put_device for get_device in raw3270_start. */
		raw3270_put_view(view);
	}
}

/*
 * Size sensing.
 */

struct raw3270_ua {	/* Query Reply structure for Usable Area */
	struct {	/* Usable Area Query Reply Base */
		short l;	/* Length of this structured field */
		char  sfid;	/* 0x81 if Query Reply */
		char  qcode;	/* 0x81 if Usable Area */
		char  flags0;
		char  flags1;
		short w;	/* Width of usable area */
		short h;	/* Heigth of usavle area */
		char  units;	/* 0x00:in; 0x01:mm */
		int   xr;
		int   yr;
		char  aw;
		char  ah;
		short buffsz;	/* Character buffer size, bytes */
		char  xmin;
		char  ymin;
		char  xmax;
		char  ymax;
	} __attribute__ ((packed)) uab;
	struct {	/* Alternate Usable Area Self-Defining Parameter */
		char  l;	/* Length of this Self-Defining Parm */
		char  sdpid;	/* 0x02 if Alternate Usable Area */
		char  res;
		char  auaid;	/* 0x01 is Id for the A U A */
		short wauai;	/* Width of AUAi */
		short hauai;	/* Height of AUAi */
		char  auaunits;	/* 0x00:in, 0x01:mm */
		int   auaxr;
		int   auayr;
		char  awauai;
		char  ahauai;
	} __attribute__ ((packed)) aua;
} __attribute__ ((packed));

static struct diag210 raw3270_init_diag210;
481
static DEFINE_MUTEX(raw3270_init_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
482
483
484
485
486
487
488
489
490

static int
raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
		 struct irb *irb)
{
	/*
	 * Unit-Check Processing:
	 * Expect Command Reject or Intervention Required.
	 */
491
	if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
Linus Torvalds's avatar
Linus Torvalds committed
492
493
494
495
496
497
498
		/* Request finished abnormally. */
		if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
			set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
			return RAW3270_IO_BUSY;
		}
	}
	if (rq) {
499
		if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
Linus Torvalds's avatar
Linus Torvalds committed
500
501
502
503
504
505
			if (irb->ecw[0] & SNS0_CMD_REJECT)
				rq->rc = -EOPNOTSUPP;
			else
				rq->rc = -EIO;
		} else
			/* Request finished normally. Copy residual count. */
506
			rq->rescnt = irb->scsw.cmd.count;
Linus Torvalds's avatar
Linus Torvalds committed
507
	}
508
	if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
Linus Torvalds's avatar
Linus Torvalds committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
		set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
		wake_up(&raw3270_wait_queue);
	}
	return RAW3270_IO_DONE;
}

static struct raw3270_fn raw3270_init_fn = {
	.intv = raw3270_init_irq
};

static struct raw3270_view raw3270_init_view = {
	.fn = &raw3270_init_fn
};

/*
 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
 * Wait for end of request. The request must have been started
 * with raw3270_start, rc = 0. The device lock may NOT have been
 * released between calling raw3270_start and raw3270_wait.
 */
static void
raw3270_wake_init(struct raw3270_request *rq, void *data)
{
	wake_up((wait_queue_head_t *) data);
}

/*
 * Special wait function that can cope with console initialization.
 */
static int
raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
		   struct raw3270_request *rq)
{
	unsigned long flags;
	int rc;

#ifdef CONFIG_TN3270_CONSOLE
	if (raw3270_registered == 0) {
		spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
Heiko Carstens's avatar
Heiko Carstens committed
548
		rq->callback = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
549
550
551
552
553
554
555
556
557
558
559
		rc = __raw3270_start(rp, view, rq);
		if (rc == 0)
			while (!raw3270_request_final(rq)) {
				wait_cons_dev();
				barrier();
			}
		spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
		return rq->rc;
	}
#endif
	rq->callback = raw3270_wake_init;
560
	rq->callback_data = &raw3270_wait_queue;
Linus Torvalds's avatar
Linus Torvalds committed
561
562
563
564
565
566
	spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
	rc = __raw3270_start(rp, view, rq);
	spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
	if (rc)
		return rc;
	/* Now wait for the completion. */
567
568
	rc = wait_event_interruptible(raw3270_wait_queue,
				      raw3270_request_final(rq));
Linus Torvalds's avatar
Linus Torvalds committed
569
570
571
	if (rc == -ERESTARTSYS) {	/* Interrupted by a signal. */
		raw3270_halt_io(view->dev, rq);
		/* No wait for the halt to complete. */
572
		wait_event(raw3270_wait_queue, raw3270_request_final(rq));
Linus Torvalds's avatar
Linus Torvalds committed
573
574
575
576
577
578
579
580
581
		return -ERESTARTSYS;
	}
	return rq->rc;
}

static int
__raw3270_size_device_vm(struct raw3270 *rp)
{
	int rc, model;
582
	struct ccw_dev_id dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
583

584
585
	ccw_device_get_id(rp->cdev, &dev_id);
	raw3270_init_diag210.vrdcdvno = dev_id.devno;
Linus Torvalds's avatar
Linus Torvalds committed
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
	raw3270_init_diag210.vrdclen = sizeof(struct diag210);
	rc = diag210(&raw3270_init_diag210);
	if (rc)
		return rc;
	model = raw3270_init_diag210.vrdccrmd;
	switch (model) {
	case 2:
		rp->model = model;
		rp->rows = 24;
		rp->cols = 80;
		break;
	case 3:
		rp->model = model;
		rp->rows = 32;
		rp->cols = 80;
		break;
	case 4:
		rp->model = model;
		rp->rows = 43;
		rp->cols = 80;
		break;
	case 5:
		rp->model = model;
		rp->rows = 27;
		rp->cols = 132;
		break;
	default:
		printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
		rc = -EOPNOTSUPP;
		break;
	}
	return rc;
}

static int
__raw3270_size_device(struct raw3270 *rp)
{
	static const unsigned char wbuf[] =
		{ 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
	struct raw3270_ua *uap;
	unsigned short count;
	int rc;

	/*
	 * To determine the size of the 3270 device we need to do:
	 * 1) send a 'read partition' data stream to the device
	 * 2) wait for the attn interrupt that preceeds the query reply
	 * 3) do a read modified to get the query reply
	 * To make things worse we have to cope with intervention
	 * required (3270 device switched to 'stand-by') and command
	 * rejects (old devices that can't do 'read partition').
	 */
638
639
640
641
642
643
644
645
646
647
648
	memset(&rp->init_request, 0, sizeof(rp->init_request));
	memset(&rp->init_data, 0, 256);
	/* Store 'read partition' data stream to init_data */
	memcpy(&rp->init_data, wbuf, sizeof(wbuf));
	INIT_LIST_HEAD(&rp->init_request.list);
	rp->init_request.ccw.cmd_code = TC_WRITESF;
	rp->init_request.ccw.flags = CCW_FLAG_SLI;
	rp->init_request.ccw.count = sizeof(wbuf);
	rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);

	rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
649
	if (rc)
Linus Torvalds's avatar
Linus Torvalds committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
		/* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
		return rc;

	/* Wait for attention interrupt. */
#ifdef CONFIG_TN3270_CONSOLE
	if (raw3270_registered == 0) {
		unsigned long flags;

		spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
		while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
			wait_cons_dev();
		spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
	} else
#endif
		rc = wait_event_interruptible(raw3270_wait_queue,
			test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
	if (rc)
		return rc;

	/*
	 * The device accepted the 'read partition' command. Now
	 * set up a read ccw and issue it.
	 */
673
674
675
676
677
	rp->init_request.ccw.cmd_code = TC_READMOD;
	rp->init_request.ccw.flags = CCW_FLAG_SLI;
	rp->init_request.ccw.count = sizeof(rp->init_data);
	rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
	rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Linus Torvalds's avatar
Linus Torvalds committed
678
679
680
	if (rc)
		return rc;
	/* Got a Query Reply */
681
682
	count = sizeof(rp->init_data) - rp->init_request.rescnt;
	uap = (struct raw3270_ua *) (rp->init_data + 1);
Linus Torvalds's avatar
Linus Torvalds committed
683
	/* Paranoia check. */
684
	if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
Linus Torvalds's avatar
Linus Torvalds committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
		return -EOPNOTSUPP;
	/* Copy rows/columns of default Usable Area */
	rp->rows = uap->uab.h;
	rp->cols = uap->uab.w;
	/* Check for 14 bit addressing */
	if ((uap->uab.flags0 & 0x0d) == 0x01)
		set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
	/* Check for Alternate Usable Area */
	if (uap->uab.l == sizeof(struct raw3270_ua) &&
	    uap->aua.sdpid == 0x02) {
		rp->rows = uap->aua.hauai;
		rp->cols = uap->aua.wauai;
	}
	return 0;
}

static int
raw3270_size_device(struct raw3270 *rp)
{
	int rc;

706
	mutex_lock(&raw3270_init_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
707
708
	rp->view = &raw3270_init_view;
	raw3270_init_view.dev = rp;
709
710
711
712
	if (MACHINE_IS_VM)
		rc = __raw3270_size_device_vm(rp);
	else
		rc = __raw3270_size_device(rp);
Heiko Carstens's avatar
Heiko Carstens committed
713
714
	raw3270_init_view.dev = NULL;
	rp->view = NULL;
715
	mutex_unlock(&raw3270_init_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
716
717
718
719
720
721
722
723
724
725
726
	if (rc == 0) {	/* Found something. */
		/* Try to find a model. */
		rp->model = 0;
		if (rp->rows == 24 && rp->cols == 80)
			rp->model = 2;
		if (rp->rows == 32 && rp->cols == 80)
			rp->model = 3;
		if (rp->rows == 43 && rp->cols == 80)
			rp->model = 4;
		if (rp->rows == 27 && rp->cols == 132)
			rp->model = 5;
727
728
729
730
731
732
	} else {
		/* Couldn't detect size. Use default model 2. */
		rp->model = 2;
		rp->rows = 24;
		rp->cols = 80;
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
733
734
735
736
737
738
739
740
741
	}
	return rc;
}

static int
raw3270_reset_device(struct raw3270 *rp)
{
	int rc;

742
	mutex_lock(&raw3270_init_mutex);
743
744
745
746
747
748
749
750
751
	memset(&rp->init_request, 0, sizeof(rp->init_request));
	memset(&rp->init_data, 0, sizeof(rp->init_data));
	/* Store reset data stream to init_data/init_request */
	rp->init_data[0] = TW_KR;
	INIT_LIST_HEAD(&rp->init_request.list);
	rp->init_request.ccw.cmd_code = TC_EWRITEA;
	rp->init_request.ccw.flags = CCW_FLAG_SLI;
	rp->init_request.ccw.count = 1;
	rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
Linus Torvalds's avatar
Linus Torvalds committed
752
753
	rp->view = &raw3270_init_view;
	raw3270_init_view.dev = rp;
754
	rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
Heiko Carstens's avatar
Heiko Carstens committed
755
756
	raw3270_init_view.dev = NULL;
	rp->view = NULL;
757
	mutex_unlock(&raw3270_init_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
758
759
760
	return rc;
}

761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
int
raw3270_reset(struct raw3270_view *view)
{
	struct raw3270 *rp;
	int rc;

	rp = view->dev;
	if (!rp || rp->view != view)
		rc = -EACCES;
	else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
		rc = -ENODEV;
	else
		rc = raw3270_reset_device(view->dev);
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/*
 * Setup new 3270 device.
 */
static int
raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
{
	struct list_head *l;
	struct raw3270 *tmp;
	int minor;

	memset(rp, 0, sizeof(struct raw3270));
	/* Copy ebcdic -> ascii translation table. */
	memcpy(ascebc, _ascebc, 256);
	if (tubxcorrect) {
		/* correct brackets and circumflex */
		ascebc['['] = 0xad;
		ascebc[']'] = 0xbd;
		ascebc['^'] = 0xb0;
	}
	rp->ascebc = ascebc;

	/* Set defaults. */
	rp->rows = 24;
	rp->cols = 80;

	INIT_LIST_HEAD(&rp->req_queue);
	INIT_LIST_HEAD(&rp->view_list);

	/*
	 * Add device to list and find the smallest unused minor
807
808
	 * number for it. Note: there is no device with minor 0,
	 * see special case for fs3270.c:fs3270_open().
Linus Torvalds's avatar
Linus Torvalds committed
809
	 */
810
	mutex_lock(&raw3270_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
811
	/* Keep the list sorted. */
812
	minor = RAW3270_FIRSTMINOR;
Linus Torvalds's avatar
Linus Torvalds committed
813
814
815
816
817
818
819
820
821
822
	rp->minor = -1;
	list_for_each(l, &raw3270_devices) {
		tmp = list_entry(l, struct raw3270, list);
		if (tmp->minor > minor) {
			rp->minor = minor;
			__list_add(&rp->list, l->prev, l);
			break;
		}
		minor++;
	}
823
	if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
Linus Torvalds's avatar
Linus Torvalds committed
824
825
826
		rp->minor = minor;
		list_add_tail(&rp->list, &raw3270_devices);
	}
827
	mutex_unlock(&raw3270_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
828
829
830
831
832
833
834
835
836
837
838
839
840
	/* No free minor number? Then give up. */
	if (rp->minor == -1)
		return -EUSERS;
	rp->cdev = cdev;
	cdev->dev.driver_data = rp;
	cdev->handler = raw3270_irq;
	return 0;
}

#ifdef CONFIG_TN3270_CONSOLE
/*
 * Setup 3270 device configured as console.
 */
841
struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
Linus Torvalds's avatar
Linus Torvalds committed
842
843
844
845
846
{
	struct raw3270 *rp;
	char *ascebc;
	int rc;

847
	rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
Linus Torvalds's avatar
Linus Torvalds committed
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
	ascebc = (char *) alloc_bootmem(256);
	rc = raw3270_setup_device(cdev, rp, ascebc);
	if (rc)
		return ERR_PTR(rc);
	set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
	rc = raw3270_reset_device(rp);
	if (rc)
		return ERR_PTR(rc);
	rc = raw3270_size_device(rp);
	if (rc)
		return ERR_PTR(rc);
	rc = raw3270_reset_device(rp);
	if (rc)
		return ERR_PTR(rc);
	set_bit(RAW3270_FLAGS_READY, &rp->flags);
	return rp;
}

void
raw3270_wait_cons_dev(struct raw3270 *rp)
{
	unsigned long flags;

	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
	wait_cons_dev();
	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
}

#endif

/*
 * Create a 3270 device structure.
 */
static struct raw3270 *
raw3270_create_device(struct ccw_device *cdev)
{
	struct raw3270 *rp;
	char *ascebc;
	int rc;

888
	rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
Linus Torvalds's avatar
Linus Torvalds committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
	if (!rp)
		return ERR_PTR(-ENOMEM);
	ascebc = kmalloc(256, GFP_KERNEL);
	if (!ascebc) {
		kfree(rp);
		return ERR_PTR(-ENOMEM);
	}
	rc = raw3270_setup_device(cdev, rp, ascebc);
	if (rc) {
		kfree(rp->ascebc);
		kfree(rp);
		rp = ERR_PTR(rc);
	}
	/* Get reference to ccw_device structure. */
	get_device(&cdev->dev);
	return rp;
}

/*
 * Activate a view.
 */
int
raw3270_activate_view(struct raw3270_view *view)
{
	struct raw3270 *rp;
	struct raw3270_view *oldview, *nv;
	unsigned long flags;
	int rc;

	rp = view->dev;
	if (!rp)
		return -ENODEV;
	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
	if (rp->view == view)
		rc = 0;
	else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
		rc = -ENODEV;
	else {
Heiko Carstens's avatar
Heiko Carstens committed
927
		oldview = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
		if (rp->view) {
			oldview = rp->view;
			oldview->fn->deactivate(oldview);
		}
		rp->view = view;
		rc = view->fn->activate(view);
		if (rc) {
			/* Didn't work. Try to reactivate the old view. */
			rp->view = oldview;
			if (!oldview || oldview->fn->activate(oldview) != 0) {
				/* Didn't work as well. Try any other view. */
				list_for_each_entry(nv, &rp->view_list, list)
					if (nv != view && nv != oldview) {
						rp->view = nv;
						if (nv->fn->activate(nv) == 0)
							break;
Heiko Carstens's avatar
Heiko Carstens committed
944
						rp->view = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
					}
			}
		}
	}
	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
	return rc;
}

/*
 * Deactivate current view.
 */
void
raw3270_deactivate_view(struct raw3270_view *view)
{
	unsigned long flags;
	struct raw3270 *rp;

	rp = view->dev;
	if (!rp)
		return;
	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
	if (rp->view == view) {
		view->fn->deactivate(view);
Heiko Carstens's avatar
Heiko Carstens committed
968
		rp->view = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
969
970
971
972
973
		/* Move deactivated view to end of list. */
		list_del_init(&view->list);
		list_add_tail(&view->list, &rp->view_list);
		/* Try to activate another view. */
		if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
974
975
976
			list_for_each_entry(view, &rp->view_list, list) {
				rp->view = view;
				if (view->fn->activate(view) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
977
					break;
Heiko Carstens's avatar
Heiko Carstens committed
978
				rp->view = NULL;
979
			}
Linus Torvalds's avatar
Linus Torvalds committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
		}
	}
	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
}

/*
 * Add view to device with minor "minor".
 */
int
raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
{
	unsigned long flags;
	struct raw3270 *rp;
	int rc;

995
996
	if (minor <= 0)
		return -ENODEV;
997
	mutex_lock(&raw3270_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
998
999
1000
	rc = -ENODEV;
	list_for_each_entry(rp, &raw3270_devices, list) {
		if (rp->minor != minor)