xref: /openbmc/linux/drivers/s390/char/con3215.c (revision 84b102f5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * 3215 line mode terminal driver.
4  *
5  * Copyright IBM Corp. 1999, 2009
6  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7  *
8  * Updated:
9  *  Aug-2000: Added tab support
10  *	      Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
11  */
12 
13 #include <linux/types.h>
14 #include <linux/kdev_t.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/vt_kern.h>
18 #include <linux/init.h>
19 #include <linux/console.h>
20 #include <linux/interrupt.h>
21 #include <linux/err.h>
22 #include <linux/reboot.h>
23 #include <linux/serial.h> /* ASYNC_* flags */
24 #include <linux/slab.h>
25 #include <asm/ccwdev.h>
26 #include <asm/cio.h>
27 #include <asm/io.h>
28 #include <asm/ebcdic.h>
29 #include <linux/uaccess.h>
30 #include <asm/delay.h>
31 #include <asm/cpcmd.h>
32 #include <asm/setup.h>
33 
34 #include "ctrlchar.h"
35 
36 #define NR_3215		    1
37 #define NR_3215_REQ	    (4*NR_3215)
38 #define RAW3215_BUFFER_SIZE 65536     /* output buffer size */
39 #define RAW3215_INBUF_SIZE  256	      /* input buffer size */
40 #define RAW3215_MIN_SPACE   128	      /* minimum free space for wakeup */
41 #define RAW3215_MIN_WRITE   1024      /* min. length for immediate output */
42 #define RAW3215_MAX_BYTES   3968      /* max. bytes to write with one ssch */
43 #define RAW3215_MAX_NEWLINE 50	      /* max. lines to write with one ssch */
44 #define RAW3215_NR_CCWS	    3
45 #define RAW3215_TIMEOUT	    HZ/10     /* time for delayed output */
46 
47 #define RAW3215_FIXED	    1	      /* 3215 console device is not be freed */
48 #define RAW3215_WORKING	    4	      /* set if a request is being worked on */
49 #define RAW3215_THROTTLED   8	      /* set if reading is disabled */
50 #define RAW3215_STOPPED	    16	      /* set if writing is disabled */
51 #define RAW3215_TIMER_RUNS  64	      /* set if the output delay timer is on */
52 #define RAW3215_FLUSHING    128	      /* set to flush buffer (no delay) */
53 
54 #define TAB_STOP_SIZE	    8	      /* tab stop size */
55 
56 /*
57  * Request types for a 3215 device
58  */
59 enum raw3215_type {
60 	RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
61 };
62 
63 /*
64  * Request structure for a 3215 device
65  */
66 struct raw3215_req {
67 	enum raw3215_type type;	      /* type of the request */
68 	int start, len;		      /* start index & len in output buffer */
69 	int delayable;		      /* indication to wait for more data */
70 	int residual;		      /* residual count for read request */
71 	struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
72 	struct raw3215_info *info;    /* pointer to main structure */
73 	struct raw3215_req *next;     /* pointer to next request */
74 } __attribute__ ((aligned(8)));
75 
76 struct raw3215_info {
77 	struct tty_port port;
78 	struct ccw_device *cdev;      /* device for tty driver */
79 	spinlock_t *lock;	      /* pointer to irq lock */
80 	int flags;		      /* state flags */
81 	char *buffer;		      /* pointer to output buffer */
82 	char *inbuf;		      /* pointer to input buffer */
83 	int head;		      /* first free byte in output buffer */
84 	int count;		      /* number of bytes in output buffer */
85 	int written;		      /* number of bytes in write requests */
86 	struct raw3215_req *queued_read; /* pointer to queued read requests */
87 	struct raw3215_req *queued_write;/* pointer to queued write requests */
88 	struct tasklet_struct tlet;   /* tasklet to invoke tty_wakeup */
89 	wait_queue_head_t empty_wait; /* wait queue for flushing */
90 	struct timer_list timer;      /* timer for delayed output */
91 	int line_pos;		      /* position on the line (for tabs) */
92 	char ubuffer[80];	      /* copy_from_user buffer */
93 };
94 
95 /* array of 3215 devices structures */
96 static struct raw3215_info *raw3215[NR_3215];
97 /* spinlock to protect the raw3215 array */
98 static DEFINE_SPINLOCK(raw3215_device_lock);
99 /* list of free request structures */
100 static struct raw3215_req *raw3215_freelist;
101 /* spinlock to protect free list */
102 static spinlock_t raw3215_freelist_lock;
103 
104 static struct tty_driver *tty3215_driver;
105 
106 /*
107  * Get a request structure from the free list
108  */
109 static inline struct raw3215_req *raw3215_alloc_req(void)
110 {
111 	struct raw3215_req *req;
112 	unsigned long flags;
113 
114 	spin_lock_irqsave(&raw3215_freelist_lock, flags);
115 	req = raw3215_freelist;
116 	raw3215_freelist = req->next;
117 	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
118 	return req;
119 }
120 
121 /*
122  * Put a request structure back to the free list
123  */
124 static inline void raw3215_free_req(struct raw3215_req *req)
125 {
126 	unsigned long flags;
127 
128 	if (req->type == RAW3215_FREE)
129 		return;		/* don't free a free request */
130 	req->type = RAW3215_FREE;
131 	spin_lock_irqsave(&raw3215_freelist_lock, flags);
132 	req->next = raw3215_freelist;
133 	raw3215_freelist = req;
134 	spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
135 }
136 
137 /*
138  * Set up a read request that reads up to 160 byte from the 3215 device.
139  * If there is a queued read request it is used, but that shouldn't happen
140  * because a 3215 terminal won't accept a new read before the old one is
141  * completed.
142  */
143 static void raw3215_mk_read_req(struct raw3215_info *raw)
144 {
145 	struct raw3215_req *req;
146 	struct ccw1 *ccw;
147 
148 	/* there can only be ONE read request at a time */
149 	req = raw->queued_read;
150 	if (req == NULL) {
151 		/* no queued read request, use new req structure */
152 		req = raw3215_alloc_req();
153 		req->type = RAW3215_READ;
154 		req->info = raw;
155 		raw->queued_read = req;
156 	}
157 
158 	ccw = req->ccws;
159 	ccw->cmd_code = 0x0A; /* read inquiry */
160 	ccw->flags = 0x20;    /* ignore incorrect length */
161 	ccw->count = 160;
162 	ccw->cda = (__u32) __pa(raw->inbuf);
163 }
164 
165 /*
166  * Set up a write request with the information from the main structure.
167  * A ccw chain is created that writes as much as possible from the output
168  * buffer to the 3215 device. If a queued write exists it is replaced by
169  * the new, probably lengthened request.
170  */
171 static void raw3215_mk_write_req(struct raw3215_info *raw)
172 {
173 	struct raw3215_req *req;
174 	struct ccw1 *ccw;
175 	int len, count, ix, lines;
176 
177 	if (raw->count <= raw->written)
178 		return;
179 	/* check if there is a queued write request */
180 	req = raw->queued_write;
181 	if (req == NULL) {
182 		/* no queued write request, use new req structure */
183 		req = raw3215_alloc_req();
184 		req->type = RAW3215_WRITE;
185 		req->info = raw;
186 		raw->queued_write = req;
187 	} else {
188 		raw->written -= req->len;
189 	}
190 
191 	ccw = req->ccws;
192 	req->start = (raw->head - raw->count + raw->written) &
193 		     (RAW3215_BUFFER_SIZE - 1);
194 	/*
195 	 * now we have to count newlines. We can at max accept
196 	 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
197 	 * a restriction in VM
198 	 */
199 	lines = 0;
200 	ix = req->start;
201 	while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
202 		if (raw->buffer[ix] == 0x15)
203 			lines++;
204 		ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
205 	}
206 	len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
207 	if (len > RAW3215_MAX_BYTES)
208 		len = RAW3215_MAX_BYTES;
209 	req->len = len;
210 	raw->written += len;
211 
212 	/* set the indication if we should try to enlarge this request */
213 	req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
214 
215 	ix = req->start;
216 	while (len > 0) {
217 		if (ccw > req->ccws)
218 			ccw[-1].flags |= 0x40; /* use command chaining */
219 		ccw->cmd_code = 0x01; /* write, auto carrier return */
220 		ccw->flags = 0x20;    /* ignore incorrect length ind.  */
221 		ccw->cda =
222 			(__u32) __pa(raw->buffer + ix);
223 		count = len;
224 		if (ix + count > RAW3215_BUFFER_SIZE)
225 			count = RAW3215_BUFFER_SIZE - ix;
226 		ccw->count = count;
227 		len -= count;
228 		ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
229 		ccw++;
230 	}
231 	/*
232 	 * Add a NOP to the channel program. 3215 devices are purely
233 	 * emulated and its much better to avoid the channel end
234 	 * interrupt in this case.
235 	 */
236 	if (ccw > req->ccws)
237 		ccw[-1].flags |= 0x40; /* use command chaining */
238 	ccw->cmd_code = 0x03; /* NOP */
239 	ccw->flags = 0;
240 	ccw->cda = 0;
241 	ccw->count = 1;
242 }
243 
244 /*
245  * Start a read or a write request
246  */
247 static void raw3215_start_io(struct raw3215_info *raw)
248 {
249 	struct raw3215_req *req;
250 	int res;
251 
252 	req = raw->queued_read;
253 	if (req != NULL &&
254 	    !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
255 		/* dequeue request */
256 		raw->queued_read = NULL;
257 		res = ccw_device_start(raw->cdev, req->ccws,
258 				       (unsigned long) req, 0, 0);
259 		if (res != 0) {
260 			/* do_IO failed, put request back to queue */
261 			raw->queued_read = req;
262 		} else {
263 			raw->flags |= RAW3215_WORKING;
264 		}
265 	}
266 	req = raw->queued_write;
267 	if (req != NULL &&
268 	    !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
269 		/* dequeue request */
270 		raw->queued_write = NULL;
271 		res = ccw_device_start(raw->cdev, req->ccws,
272 				       (unsigned long) req, 0, 0);
273 		if (res != 0) {
274 			/* do_IO failed, put request back to queue */
275 			raw->queued_write = req;
276 		} else {
277 			raw->flags |= RAW3215_WORKING;
278 		}
279 	}
280 }
281 
282 /*
283  * Function to start a delayed output after RAW3215_TIMEOUT seconds
284  */
285 static void raw3215_timeout(struct timer_list *t)
286 {
287 	struct raw3215_info *raw = from_timer(raw, t, timer);
288 	unsigned long flags;
289 
290 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
291 	raw->flags &= ~RAW3215_TIMER_RUNS;
292 	raw3215_mk_write_req(raw);
293 	raw3215_start_io(raw);
294 	if ((raw->queued_read || raw->queued_write) &&
295 	    !(raw->flags & RAW3215_WORKING) &&
296 	    !(raw->flags & RAW3215_TIMER_RUNS)) {
297 		raw->timer.expires = RAW3215_TIMEOUT + jiffies;
298 		add_timer(&raw->timer);
299 		raw->flags |= RAW3215_TIMER_RUNS;
300 	}
301 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
302 }
303 
304 /*
305  * Function to conditionally start an IO. A read is started immediately,
306  * a write is only started immediately if the flush flag is on or the
307  * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
308  * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
309  */
310 static inline void raw3215_try_io(struct raw3215_info *raw)
311 {
312 	if (!tty_port_initialized(&raw->port))
313 		return;
314 	if (raw->queued_read != NULL)
315 		raw3215_start_io(raw);
316 	else if (raw->queued_write != NULL) {
317 		if ((raw->queued_write->delayable == 0) ||
318 		    (raw->flags & RAW3215_FLUSHING)) {
319 			/* execute write requests bigger than minimum size */
320 			raw3215_start_io(raw);
321 		}
322 	}
323 	if ((raw->queued_read || raw->queued_write) &&
324 	    !(raw->flags & RAW3215_WORKING) &&
325 	    !(raw->flags & RAW3215_TIMER_RUNS)) {
326 		raw->timer.expires = RAW3215_TIMEOUT + jiffies;
327 		add_timer(&raw->timer);
328 		raw->flags |= RAW3215_TIMER_RUNS;
329 	}
330 }
331 
332 /*
333  * Call tty_wakeup from tasklet context
334  */
335 static void raw3215_wakeup(unsigned long data)
336 {
337 	struct raw3215_info *raw = (struct raw3215_info *) data;
338 	struct tty_struct *tty;
339 
340 	tty = tty_port_tty_get(&raw->port);
341 	if (tty) {
342 		tty_wakeup(tty);
343 		tty_kref_put(tty);
344 	}
345 }
346 
347 /*
348  * Try to start the next IO and wake up processes waiting on the tty.
349  */
350 static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
351 {
352 	raw3215_mk_write_req(raw);
353 	raw3215_try_io(raw);
354 	if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
355 		tasklet_schedule(&raw->tlet);
356 }
357 
358 /*
359  * Interrupt routine, called from common io layer
360  */
361 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
362 			struct irb *irb)
363 {
364 	struct raw3215_info *raw;
365 	struct raw3215_req *req;
366 	struct tty_struct *tty;
367 	int cstat, dstat;
368 	int count;
369 
370 	raw = dev_get_drvdata(&cdev->dev);
371 	req = (struct raw3215_req *) intparm;
372 	tty = tty_port_tty_get(&raw->port);
373 	cstat = irb->scsw.cmd.cstat;
374 	dstat = irb->scsw.cmd.dstat;
375 	if (cstat != 0)
376 		raw3215_next_io(raw, tty);
377 	if (dstat & 0x01) { /* we got a unit exception */
378 		dstat &= ~0x01;	 /* we can ignore it */
379 	}
380 	switch (dstat) {
381 	case 0x80:
382 		if (cstat != 0)
383 			break;
384 		/* Attention interrupt, someone hit the enter key */
385 		raw3215_mk_read_req(raw);
386 		raw3215_next_io(raw, tty);
387 		break;
388 	case 0x08:
389 	case 0x0C:
390 		/* Channel end interrupt. */
391 		if ((raw = req->info) == NULL)
392 			goto put_tty;	     /* That shouldn't happen ... */
393 		if (req->type == RAW3215_READ) {
394 			/* store residual count, then wait for device end */
395 			req->residual = irb->scsw.cmd.count;
396 		}
397 		if (dstat == 0x08)
398 			break;
399 		fallthrough;
400 	case 0x04:
401 		/* Device end interrupt. */
402 		if ((raw = req->info) == NULL)
403 			goto put_tty;	     /* That shouldn't happen ... */
404 		if (req->type == RAW3215_READ && tty != NULL) {
405 			unsigned int cchar;
406 
407 			count = 160 - req->residual;
408 			EBCASC(raw->inbuf, count);
409 			cchar = ctrlchar_handle(raw->inbuf, count, tty);
410 			switch (cchar & CTRLCHAR_MASK) {
411 			case CTRLCHAR_SYSRQ:
412 				break;
413 
414 			case CTRLCHAR_CTRL:
415 				tty_insert_flip_char(&raw->port, cchar,
416 						TTY_NORMAL);
417 				tty_flip_buffer_push(&raw->port);
418 				break;
419 
420 			case CTRLCHAR_NONE:
421 				if (count < 2 ||
422 				    (strncmp(raw->inbuf+count-2, "\252n", 2) &&
423 				     strncmp(raw->inbuf+count-2, "^n", 2)) ) {
424 					/* add the auto \n */
425 					raw->inbuf[count] = '\n';
426 					count++;
427 				} else
428 					count -= 2;
429 				tty_insert_flip_string(&raw->port, raw->inbuf,
430 						count);
431 				tty_flip_buffer_push(&raw->port);
432 				break;
433 			}
434 		} else if (req->type == RAW3215_WRITE) {
435 			raw->count -= req->len;
436 			raw->written -= req->len;
437 		}
438 		raw->flags &= ~RAW3215_WORKING;
439 		raw3215_free_req(req);
440 		/* check for empty wait */
441 		if (waitqueue_active(&raw->empty_wait) &&
442 		    raw->queued_write == NULL &&
443 		    raw->queued_read == NULL) {
444 			wake_up_interruptible(&raw->empty_wait);
445 		}
446 		raw3215_next_io(raw, tty);
447 		break;
448 	default:
449 		/* Strange interrupt, I'll do my best to clean up */
450 		if (req != NULL && req->type != RAW3215_FREE) {
451 			if (req->type == RAW3215_WRITE) {
452 				raw->count -= req->len;
453 				raw->written -= req->len;
454 			}
455 			raw->flags &= ~RAW3215_WORKING;
456 			raw3215_free_req(req);
457 		}
458 		raw3215_next_io(raw, tty);
459 	}
460 put_tty:
461 	tty_kref_put(tty);
462 }
463 
464 /*
465  * Wait until length bytes are available int the output buffer.
466  * Has to be called with the s390irq lock held. Can be called
467  * disabled.
468  */
469 static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
470 {
471 	while (RAW3215_BUFFER_SIZE - raw->count < length) {
472 		/* there might be a request pending */
473 		raw->flags |= RAW3215_FLUSHING;
474 		raw3215_mk_write_req(raw);
475 		raw3215_try_io(raw);
476 		raw->flags &= ~RAW3215_FLUSHING;
477 #ifdef CONFIG_TN3215_CONSOLE
478 		ccw_device_wait_idle(raw->cdev);
479 #endif
480 		/* Enough room freed up ? */
481 		if (RAW3215_BUFFER_SIZE - raw->count >= length)
482 			break;
483 		/* there might be another cpu waiting for the lock */
484 		spin_unlock(get_ccwdev_lock(raw->cdev));
485 		udelay(100);
486 		spin_lock(get_ccwdev_lock(raw->cdev));
487 	}
488 }
489 
490 /*
491  * String write routine for 3215 devices
492  */
493 static void raw3215_write(struct raw3215_info *raw, const char *str,
494 			  unsigned int length)
495 {
496 	unsigned long flags;
497 	int c, count;
498 
499 	while (length > 0) {
500 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
501 		count = (length > RAW3215_BUFFER_SIZE) ?
502 					     RAW3215_BUFFER_SIZE : length;
503 		length -= count;
504 
505 		raw3215_make_room(raw, count);
506 
507 		/* copy string to output buffer and convert it to EBCDIC */
508 		while (1) {
509 			c = min_t(int, count,
510 				  min(RAW3215_BUFFER_SIZE - raw->count,
511 				      RAW3215_BUFFER_SIZE - raw->head));
512 			if (c <= 0)
513 				break;
514 			memcpy(raw->buffer + raw->head, str, c);
515 			ASCEBC(raw->buffer + raw->head, c);
516 			raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
517 			raw->count += c;
518 			raw->line_pos += c;
519 			str += c;
520 			count -= c;
521 		}
522 		if (!(raw->flags & RAW3215_WORKING)) {
523 			raw3215_mk_write_req(raw);
524 			/* start or queue request */
525 			raw3215_try_io(raw);
526 		}
527 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
528 	}
529 }
530 
531 /*
532  * Put character routine for 3215 devices
533  */
534 static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
535 {
536 	unsigned long flags;
537 	unsigned int length, i;
538 
539 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
540 	if (ch == '\t') {
541 		length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
542 		raw->line_pos += length;
543 		ch = ' ';
544 	} else if (ch == '\n') {
545 		length = 1;
546 		raw->line_pos = 0;
547 	} else {
548 		length = 1;
549 		raw->line_pos++;
550 	}
551 	raw3215_make_room(raw, length);
552 
553 	for (i = 0; i < length; i++) {
554 		raw->buffer[raw->head] = (char) _ascebc[(int) ch];
555 		raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
556 		raw->count++;
557 	}
558 	if (!(raw->flags & RAW3215_WORKING)) {
559 		raw3215_mk_write_req(raw);
560 		/* start or queue request */
561 		raw3215_try_io(raw);
562 	}
563 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
564 }
565 
566 /*
567  * Flush routine, it simply sets the flush flag and tries to start
568  * pending IO.
569  */
570 static void raw3215_flush_buffer(struct raw3215_info *raw)
571 {
572 	unsigned long flags;
573 
574 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
575 	if (raw->count > 0) {
576 		raw->flags |= RAW3215_FLUSHING;
577 		raw3215_try_io(raw);
578 		raw->flags &= ~RAW3215_FLUSHING;
579 	}
580 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
581 }
582 
583 /*
584  * Fire up a 3215 device.
585  */
586 static int raw3215_startup(struct raw3215_info *raw)
587 {
588 	unsigned long flags;
589 
590 	if (tty_port_initialized(&raw->port))
591 		return 0;
592 	raw->line_pos = 0;
593 	tty_port_set_initialized(&raw->port, 1);
594 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
595 	raw3215_try_io(raw);
596 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
597 
598 	return 0;
599 }
600 
601 /*
602  * Shutdown a 3215 device.
603  */
604 static void raw3215_shutdown(struct raw3215_info *raw)
605 {
606 	DECLARE_WAITQUEUE(wait, current);
607 	unsigned long flags;
608 
609 	if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
610 		return;
611 	/* Wait for outstanding requests, then free irq */
612 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
613 	if ((raw->flags & RAW3215_WORKING) ||
614 	    raw->queued_write != NULL ||
615 	    raw->queued_read != NULL) {
616 		add_wait_queue(&raw->empty_wait, &wait);
617 		set_current_state(TASK_INTERRUPTIBLE);
618 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
619 		schedule();
620 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
621 		remove_wait_queue(&raw->empty_wait, &wait);
622 		set_current_state(TASK_RUNNING);
623 		tty_port_set_initialized(&raw->port, 1);
624 	}
625 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
626 }
627 
628 static struct raw3215_info *raw3215_alloc_info(void)
629 {
630 	struct raw3215_info *info;
631 
632 	info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
633 	if (!info)
634 		return NULL;
635 
636 	info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
637 	info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
638 	if (!info->buffer || !info->inbuf) {
639 		kfree(info->inbuf);
640 		kfree(info->buffer);
641 		kfree(info);
642 		return NULL;
643 	}
644 
645 	timer_setup(&info->timer, raw3215_timeout, 0);
646 	init_waitqueue_head(&info->empty_wait);
647 	tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
648 	tty_port_init(&info->port);
649 
650 	return info;
651 }
652 
653 static void raw3215_free_info(struct raw3215_info *raw)
654 {
655 	kfree(raw->inbuf);
656 	kfree(raw->buffer);
657 	tty_port_destroy(&raw->port);
658 	kfree(raw);
659 }
660 
661 static int raw3215_probe (struct ccw_device *cdev)
662 {
663 	struct raw3215_info *raw;
664 	int line;
665 
666 	/* Console is special. */
667 	if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
668 		return 0;
669 
670 	raw = raw3215_alloc_info();
671 	if (raw == NULL)
672 		return -ENOMEM;
673 
674 	raw->cdev = cdev;
675 	dev_set_drvdata(&cdev->dev, raw);
676 	cdev->handler = raw3215_irq;
677 
678 	spin_lock(&raw3215_device_lock);
679 	for (line = 0; line < NR_3215; line++) {
680 		if (!raw3215[line]) {
681 			raw3215[line] = raw;
682 			break;
683 		}
684 	}
685 	spin_unlock(&raw3215_device_lock);
686 	if (line == NR_3215) {
687 		raw3215_free_info(raw);
688 		return -ENODEV;
689 	}
690 
691 	return 0;
692 }
693 
694 static void raw3215_remove (struct ccw_device *cdev)
695 {
696 	struct raw3215_info *raw;
697 	unsigned int line;
698 
699 	ccw_device_set_offline(cdev);
700 	raw = dev_get_drvdata(&cdev->dev);
701 	if (raw) {
702 		spin_lock(&raw3215_device_lock);
703 		for (line = 0; line < NR_3215; line++)
704 			if (raw3215[line] == raw)
705 				break;
706 		raw3215[line] = NULL;
707 		spin_unlock(&raw3215_device_lock);
708 		dev_set_drvdata(&cdev->dev, NULL);
709 		raw3215_free_info(raw);
710 	}
711 }
712 
713 static int raw3215_set_online (struct ccw_device *cdev)
714 {
715 	struct raw3215_info *raw;
716 
717 	raw = dev_get_drvdata(&cdev->dev);
718 	if (!raw)
719 		return -ENODEV;
720 
721 	return raw3215_startup(raw);
722 }
723 
724 static int raw3215_set_offline (struct ccw_device *cdev)
725 {
726 	struct raw3215_info *raw;
727 
728 	raw = dev_get_drvdata(&cdev->dev);
729 	if (!raw)
730 		return -ENODEV;
731 
732 	raw3215_shutdown(raw);
733 
734 	return 0;
735 }
736 
737 static struct ccw_device_id raw3215_id[] = {
738 	{ CCW_DEVICE(0x3215, 0) },
739 	{ /* end of list */ },
740 };
741 
742 static struct ccw_driver raw3215_ccw_driver = {
743 	.driver = {
744 		.name	= "3215",
745 		.owner	= THIS_MODULE,
746 	},
747 	.ids		= raw3215_id,
748 	.probe		= &raw3215_probe,
749 	.remove		= &raw3215_remove,
750 	.set_online	= &raw3215_set_online,
751 	.set_offline	= &raw3215_set_offline,
752 	.int_class	= IRQIO_C15,
753 };
754 
755 #ifdef CONFIG_TN3215_CONSOLE
756 /*
757  * Write a string to the 3215 console
758  */
759 static void con3215_write(struct console *co, const char *str,
760 			  unsigned int count)
761 {
762 	struct raw3215_info *raw;
763 	int i;
764 
765 	if (count <= 0)
766 		return;
767 	raw = raw3215[0];	/* console 3215 is the first one */
768 	while (count > 0) {
769 		for (i = 0; i < count; i++)
770 			if (str[i] == '\t' || str[i] == '\n')
771 				break;
772 		raw3215_write(raw, str, i);
773 		count -= i;
774 		str += i;
775 		if (count > 0) {
776 			raw3215_putchar(raw, *str);
777 			count--;
778 			str++;
779 		}
780 	}
781 }
782 
783 static struct tty_driver *con3215_device(struct console *c, int *index)
784 {
785 	*index = c->index;
786 	return tty3215_driver;
787 }
788 
789 /*
790  * panic() calls con3215_flush through a panic_notifier
791  * before the system enters a disabled, endless loop.
792  */
793 static void con3215_flush(void)
794 {
795 	struct raw3215_info *raw;
796 	unsigned long flags;
797 
798 	raw = raw3215[0];  /* console 3215 is the first one */
799 	spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
800 	raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
801 	spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
802 }
803 
804 static int con3215_notify(struct notifier_block *self,
805 			  unsigned long event, void *data)
806 {
807 	con3215_flush();
808 	return NOTIFY_OK;
809 }
810 
811 static struct notifier_block on_panic_nb = {
812 	.notifier_call = con3215_notify,
813 	.priority = 0,
814 };
815 
816 static struct notifier_block on_reboot_nb = {
817 	.notifier_call = con3215_notify,
818 	.priority = 0,
819 };
820 
821 /*
822  *  The console structure for the 3215 console
823  */
824 static struct console con3215 = {
825 	.name	 = "ttyS",
826 	.write	 = con3215_write,
827 	.device	 = con3215_device,
828 	.flags	 = CON_PRINTBUFFER,
829 };
830 
831 /*
832  * 3215 console initialization code called from console_init().
833  */
834 static int __init con3215_init(void)
835 {
836 	struct ccw_device *cdev;
837 	struct raw3215_info *raw;
838 	struct raw3215_req *req;
839 	int i;
840 
841 	/* Check if 3215 is to be the console */
842 	if (!CONSOLE_IS_3215)
843 		return -ENODEV;
844 
845 	/* Set the console mode for VM */
846 	if (MACHINE_IS_VM) {
847 		cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
848 		cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
849 	}
850 
851 	/* allocate 3215 request structures */
852 	raw3215_freelist = NULL;
853 	spin_lock_init(&raw3215_freelist_lock);
854 	for (i = 0; i < NR_3215_REQ; i++) {
855 		req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
856 		if (!req)
857 			return -ENOMEM;
858 		req->next = raw3215_freelist;
859 		raw3215_freelist = req;
860 	}
861 
862 	cdev = ccw_device_create_console(&raw3215_ccw_driver);
863 	if (IS_ERR(cdev))
864 		return -ENODEV;
865 
866 	raw3215[0] = raw = raw3215_alloc_info();
867 	raw->cdev = cdev;
868 	dev_set_drvdata(&cdev->dev, raw);
869 	cdev->handler = raw3215_irq;
870 
871 	raw->flags |= RAW3215_FIXED;
872 	if (ccw_device_enable_console(cdev)) {
873 		ccw_device_destroy_console(cdev);
874 		raw3215_free_info(raw);
875 		raw3215[0] = NULL;
876 		return -ENODEV;
877 	}
878 
879 	/* Request the console irq */
880 	if (raw3215_startup(raw) != 0) {
881 		raw3215_free_info(raw);
882 		raw3215[0] = NULL;
883 		return -ENODEV;
884 	}
885 	atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
886 	register_reboot_notifier(&on_reboot_nb);
887 	register_console(&con3215);
888 	return 0;
889 }
890 console_initcall(con3215_init);
891 #endif
892 
893 static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
894 {
895 	struct raw3215_info *raw;
896 
897 	raw = raw3215[tty->index];
898 	if (raw == NULL)
899 		return -ENODEV;
900 
901 	tty->driver_data = raw;
902 
903 	return tty_port_install(&raw->port, driver, tty);
904 }
905 
906 /*
907  * tty3215_open
908  *
909  * This routine is called whenever a 3215 tty is opened.
910  */
911 static int tty3215_open(struct tty_struct *tty, struct file * filp)
912 {
913 	struct raw3215_info *raw = tty->driver_data;
914 
915 	tty_port_tty_set(&raw->port, tty);
916 
917 	raw->port.low_latency = 0; /* don't use bottom half for pushing chars */
918 	/*
919 	 * Start up 3215 device
920 	 */
921 	return raw3215_startup(raw);
922 }
923 
924 /*
925  * tty3215_close()
926  *
927  * This routine is called when the 3215 tty is closed. We wait
928  * for the remaining request to be completed. Then we clean up.
929  */
930 static void tty3215_close(struct tty_struct *tty, struct file * filp)
931 {
932 	struct raw3215_info *raw;
933 
934 	raw = (struct raw3215_info *) tty->driver_data;
935 	if (raw == NULL || tty->count > 1)
936 		return;
937 	tty->closing = 1;
938 	/* Shutdown the terminal */
939 	raw3215_shutdown(raw);
940 	tasklet_kill(&raw->tlet);
941 	tty->closing = 0;
942 	tty_port_tty_set(&raw->port, NULL);
943 }
944 
945 /*
946  * Returns the amount of free space in the output buffer.
947  */
948 static int tty3215_write_room(struct tty_struct *tty)
949 {
950 	struct raw3215_info *raw;
951 
952 	raw = (struct raw3215_info *) tty->driver_data;
953 
954 	/* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
955 	if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
956 		return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
957 	else
958 		return 0;
959 }
960 
961 /*
962  * String write routine for 3215 ttys
963  */
964 static int tty3215_write(struct tty_struct * tty,
965 			 const unsigned char *buf, int count)
966 {
967 	struct raw3215_info *raw;
968 	int i, written;
969 
970 	if (!tty)
971 		return 0;
972 	raw = (struct raw3215_info *) tty->driver_data;
973 	written = count;
974 	while (count > 0) {
975 		for (i = 0; i < count; i++)
976 			if (buf[i] == '\t' || buf[i] == '\n')
977 				break;
978 		raw3215_write(raw, buf, i);
979 		count -= i;
980 		buf += i;
981 		if (count > 0) {
982 			raw3215_putchar(raw, *buf);
983 			count--;
984 			buf++;
985 		}
986 	}
987 	return written;
988 }
989 
990 /*
991  * Put character routine for 3215 ttys
992  */
993 static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
994 {
995 	struct raw3215_info *raw;
996 
997 	if (!tty)
998 		return 0;
999 	raw = (struct raw3215_info *) tty->driver_data;
1000 	raw3215_putchar(raw, ch);
1001 	return 1;
1002 }
1003 
1004 static void tty3215_flush_chars(struct tty_struct *tty)
1005 {
1006 }
1007 
1008 /*
1009  * Returns the number of characters in the output buffer
1010  */
1011 static int tty3215_chars_in_buffer(struct tty_struct *tty)
1012 {
1013 	struct raw3215_info *raw;
1014 
1015 	raw = (struct raw3215_info *) tty->driver_data;
1016 	return raw->count;
1017 }
1018 
1019 static void tty3215_flush_buffer(struct tty_struct *tty)
1020 {
1021 	struct raw3215_info *raw;
1022 
1023 	raw = (struct raw3215_info *) tty->driver_data;
1024 	raw3215_flush_buffer(raw);
1025 	tty_wakeup(tty);
1026 }
1027 
1028 /*
1029  * Disable reading from a 3215 tty
1030  */
1031 static void tty3215_throttle(struct tty_struct * tty)
1032 {
1033 	struct raw3215_info *raw;
1034 
1035 	raw = (struct raw3215_info *) tty->driver_data;
1036 	raw->flags |= RAW3215_THROTTLED;
1037 }
1038 
1039 /*
1040  * Enable reading from a 3215 tty
1041  */
1042 static void tty3215_unthrottle(struct tty_struct * tty)
1043 {
1044 	struct raw3215_info *raw;
1045 	unsigned long flags;
1046 
1047 	raw = (struct raw3215_info *) tty->driver_data;
1048 	if (raw->flags & RAW3215_THROTTLED) {
1049 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1050 		raw->flags &= ~RAW3215_THROTTLED;
1051 		raw3215_try_io(raw);
1052 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1053 	}
1054 }
1055 
1056 /*
1057  * Disable writing to a 3215 tty
1058  */
1059 static void tty3215_stop(struct tty_struct *tty)
1060 {
1061 	struct raw3215_info *raw;
1062 
1063 	raw = (struct raw3215_info *) tty->driver_data;
1064 	raw->flags |= RAW3215_STOPPED;
1065 }
1066 
1067 /*
1068  * Enable writing to a 3215 tty
1069  */
1070 static void tty3215_start(struct tty_struct *tty)
1071 {
1072 	struct raw3215_info *raw;
1073 	unsigned long flags;
1074 
1075 	raw = (struct raw3215_info *) tty->driver_data;
1076 	if (raw->flags & RAW3215_STOPPED) {
1077 		spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1078 		raw->flags &= ~RAW3215_STOPPED;
1079 		raw3215_try_io(raw);
1080 		spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1081 	}
1082 }
1083 
1084 static const struct tty_operations tty3215_ops = {
1085 	.install = tty3215_install,
1086 	.open = tty3215_open,
1087 	.close = tty3215_close,
1088 	.write = tty3215_write,
1089 	.put_char = tty3215_put_char,
1090 	.flush_chars = tty3215_flush_chars,
1091 	.write_room = tty3215_write_room,
1092 	.chars_in_buffer = tty3215_chars_in_buffer,
1093 	.flush_buffer = tty3215_flush_buffer,
1094 	.throttle = tty3215_throttle,
1095 	.unthrottle = tty3215_unthrottle,
1096 	.stop = tty3215_stop,
1097 	.start = tty3215_start,
1098 };
1099 
1100 /*
1101  * 3215 tty registration code called from tty_init().
1102  * Most kernel services (incl. kmalloc) are available at this poimt.
1103  */
1104 static int __init tty3215_init(void)
1105 {
1106 	struct tty_driver *driver;
1107 	int ret;
1108 
1109 	if (!CONSOLE_IS_3215)
1110 		return 0;
1111 
1112 	driver = alloc_tty_driver(NR_3215);
1113 	if (!driver)
1114 		return -ENOMEM;
1115 
1116 	ret = ccw_driver_register(&raw3215_ccw_driver);
1117 	if (ret) {
1118 		put_tty_driver(driver);
1119 		return ret;
1120 	}
1121 	/*
1122 	 * Initialize the tty_driver structure
1123 	 * Entries in tty3215_driver that are NOT initialized:
1124 	 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1125 	 */
1126 
1127 	driver->driver_name = "tty3215";
1128 	driver->name = "ttyS";
1129 	driver->major = TTY_MAJOR;
1130 	driver->minor_start = 64;
1131 	driver->type = TTY_DRIVER_TYPE_SYSTEM;
1132 	driver->subtype = SYSTEM_TYPE_TTY;
1133 	driver->init_termios = tty_std_termios;
1134 	driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1135 	driver->init_termios.c_oflag = ONLCR;
1136 	driver->init_termios.c_lflag = ISIG;
1137 	driver->flags = TTY_DRIVER_REAL_RAW;
1138 	tty_set_operations(driver, &tty3215_ops);
1139 	ret = tty_register_driver(driver);
1140 	if (ret) {
1141 		put_tty_driver(driver);
1142 		return ret;
1143 	}
1144 	tty3215_driver = driver;
1145 	return 0;
1146 }
1147 device_initcall(tty3215_init);
1148