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