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