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