blob: 982cf62ab66d7f4d51bda9a77c17d43cc48a4755 [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>
Holger Smolinski2332ce12008-10-10 21:33:27 +020024#include <linux/reboot.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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int line_pos; /* position on the line (for tabs) */
98 char ubuffer[80]; /* copy_from_user buffer */
99};
100
101/* array of 3215 devices structures */
102static struct raw3215_info *raw3215[NR_3215];
103/* spinlock to protect the raw3215 array */
104static DEFINE_SPINLOCK(raw3215_device_lock);
105/* list of free request structures */
106static struct raw3215_req *raw3215_freelist;
107/* spinlock to protect free list */
108static spinlock_t raw3215_freelist_lock;
109
110static struct tty_driver *tty3215_driver;
111
112/*
113 * Get a request structure from the free list
114 */
115static inline struct raw3215_req *
116raw3215_alloc_req(void) {
117 struct raw3215_req *req;
118 unsigned long flags;
119
120 spin_lock_irqsave(&raw3215_freelist_lock, flags);
121 req = raw3215_freelist;
122 raw3215_freelist = req->next;
123 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
124 return req;
125}
126
127/*
128 * Put a request structure back to the free list
129 */
130static inline void
131raw3215_free_req(struct raw3215_req *req) {
132 unsigned long flags;
133
134 if (req->type == RAW3215_FREE)
135 return; /* don't free a free request */
136 req->type = RAW3215_FREE;
137 spin_lock_irqsave(&raw3215_freelist_lock, flags);
138 req->next = raw3215_freelist;
139 raw3215_freelist = req;
140 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
141}
142
143/*
144 * Set up a read request that reads up to 160 byte from the 3215 device.
145 * If there is a queued read request it is used, but that shouldn't happen
146 * because a 3215 terminal won't accept a new read before the old one is
147 * completed.
148 */
149static void
150raw3215_mk_read_req(struct raw3215_info *raw)
151{
152 struct raw3215_req *req;
153 struct ccw1 *ccw;
154
155 /* there can only be ONE read request at a time */
156 req = raw->queued_read;
157 if (req == NULL) {
158 /* no queued read request, use new req structure */
159 req = raw3215_alloc_req();
160 req->type = RAW3215_READ;
161 req->info = raw;
162 raw->queued_read = req;
163 }
164
165 ccw = req->ccws;
166 ccw->cmd_code = 0x0A; /* read inquiry */
167 ccw->flags = 0x20; /* ignore incorrect length */
168 ccw->count = 160;
169 ccw->cda = (__u32) __pa(raw->inbuf);
170}
171
172/*
173 * Set up a write request with the information from the main structure.
174 * A ccw chain is created that writes as much as possible from the output
175 * buffer to the 3215 device. If a queued write exists it is replaced by
176 * the new, probably lengthened request.
177 */
178static void
179raw3215_mk_write_req(struct raw3215_info *raw)
180{
181 struct raw3215_req *req;
182 struct ccw1 *ccw;
183 int len, count, ix, lines;
184
185 if (raw->count <= raw->written)
186 return;
187 /* check if there is a queued write request */
188 req = raw->queued_write;
189 if (req == NULL) {
190 /* no queued write request, use new req structure */
191 req = raw3215_alloc_req();
192 req->type = RAW3215_WRITE;
193 req->info = raw;
194 raw->queued_write = req;
195 } else {
196 raw->written -= req->len;
197 }
198
199 ccw = req->ccws;
200 req->start = (raw->head - raw->count + raw->written) &
201 (RAW3215_BUFFER_SIZE - 1);
202 /*
203 * now we have to count newlines. We can at max accept
204 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
205 * a restriction in VM
206 */
207 lines = 0;
208 ix = req->start;
209 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
210 if (raw->buffer[ix] == 0x15)
211 lines++;
212 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
213 }
214 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
215 if (len > RAW3215_MAX_BYTES)
216 len = RAW3215_MAX_BYTES;
217 req->len = len;
218 raw->written += len;
219
220 /* set the indication if we should try to enlarge this request */
221 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
222
223 ix = req->start;
224 while (len > 0) {
225 if (ccw > req->ccws)
226 ccw[-1].flags |= 0x40; /* use command chaining */
227 ccw->cmd_code = 0x01; /* write, auto carrier return */
228 ccw->flags = 0x20; /* ignore incorrect length ind. */
229 ccw->cda =
230 (__u32) __pa(raw->buffer + ix);
231 count = len;
232 if (ix + count > RAW3215_BUFFER_SIZE)
233 count = RAW3215_BUFFER_SIZE - ix;
234 ccw->count = count;
235 len -= count;
236 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
237 ccw++;
238 }
239 /*
240 * Add a NOP to the channel program. 3215 devices are purely
241 * emulated and its much better to avoid the channel end
242 * interrupt in this case.
243 */
244 if (ccw > req->ccws)
245 ccw[-1].flags |= 0x40; /* use command chaining */
246 ccw->cmd_code = 0x03; /* NOP */
247 ccw->flags = 0;
248 ccw->cda = 0;
249 ccw->count = 1;
250}
251
252/*
253 * Start a read or a write request
254 */
255static void
256raw3215_start_io(struct raw3215_info *raw)
257{
258 struct raw3215_req *req;
259 int res;
260
261 req = raw->queued_read;
262 if (req != NULL &&
263 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
264 /* dequeue request */
265 raw->queued_read = NULL;
266 res = ccw_device_start(raw->cdev, req->ccws,
267 (unsigned long) req, 0, 0);
268 if (res != 0) {
269 /* do_IO failed, put request back to queue */
270 raw->queued_read = req;
271 } else {
272 raw->flags |= RAW3215_WORKING;
273 }
274 }
275 req = raw->queued_write;
276 if (req != NULL &&
277 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
278 /* dequeue request */
279 raw->queued_write = NULL;
280 res = ccw_device_start(raw->cdev, req->ccws,
281 (unsigned long) req, 0, 0);
282 if (res != 0) {
283 /* do_IO failed, put request back to queue */
284 raw->queued_write = req;
285 } else {
286 raw->flags |= RAW3215_WORKING;
287 }
288 }
289}
290
291/*
292 * Function to start a delayed output after RAW3215_TIMEOUT seconds
293 */
294static void
295raw3215_timeout(unsigned long __data)
296{
297 struct raw3215_info *raw = (struct raw3215_info *) __data;
298 unsigned long flags;
299
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100300 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (raw->flags & RAW3215_TIMER_RUNS) {
302 del_timer(&raw->timer);
303 raw->flags &= ~RAW3215_TIMER_RUNS;
304 raw3215_mk_write_req(raw);
305 raw3215_start_io(raw);
306 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100307 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/*
311 * Function to conditionally start an IO. A read is started immediately,
312 * a write is only started immediately if the flush flag is on or the
313 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
314 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
315 */
316static inline void
317raw3215_try_io(struct raw3215_info *raw)
318{
319 if (!(raw->flags & RAW3215_ACTIVE))
320 return;
321 if (raw->queued_read != NULL)
322 raw3215_start_io(raw);
323 else if (raw->queued_write != NULL) {
324 if ((raw->queued_write->delayable == 0) ||
325 (raw->flags & RAW3215_FLUSHING)) {
326 /* execute write requests bigger than minimum size */
327 raw3215_start_io(raw);
328 if (raw->flags & RAW3215_TIMER_RUNS) {
329 del_timer(&raw->timer);
330 raw->flags &= ~RAW3215_TIMER_RUNS;
331 }
332 } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
333 /* delay small writes */
334 init_timer(&raw->timer);
335 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
336 raw->timer.data = (unsigned long) raw;
337 raw->timer.function = raw3215_timeout;
338 add_timer(&raw->timer);
339 raw->flags |= RAW3215_TIMER_RUNS;
340 }
341 }
342}
343
344/*
345 * The bottom half handler routine for 3215 devices. It tries to start
346 * the next IO and wakes up processes waiting on the tty.
347 */
348static void
349raw3215_tasklet(void *data)
350{
351 struct raw3215_info *raw;
352 struct tty_struct *tty;
353 unsigned long flags;
354
355 raw = (struct raw3215_info *) data;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100356 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 raw3215_mk_write_req(raw);
358 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100359 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 tty = raw->tty;
361 if (tty != NULL &&
362 RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
363 tty_wakeup(tty);
364 }
365}
366
367/*
368 * Interrupt routine, called from common io layer
369 */
370static void
371raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
372{
373 struct raw3215_info *raw;
374 struct raw3215_req *req;
375 struct tty_struct *tty;
376 int cstat, dstat;
Heiko Carstens1d030372008-07-14 09:59:44 +0200377 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 raw = cdev->dev.driver_data;
380 req = (struct raw3215_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200381 cstat = irb->scsw.cmd.cstat;
382 dstat = irb->scsw.cmd.dstat;
Martin Schwidefsky26348f72008-07-14 09:59:26 +0200383 if (cstat != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 tasklet_schedule(&raw->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (dstat & 0x01) { /* we got a unit exception */
386 dstat &= ~0x01; /* we can ignore it */
387 }
388 switch (dstat) {
389 case 0x80:
390 if (cstat != 0)
391 break;
392 /* Attention interrupt, someone hit the enter key */
393 raw3215_mk_read_req(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 tasklet_schedule(&raw->tasklet);
395 break;
396 case 0x08:
397 case 0x0C:
398 /* Channel end interrupt. */
399 if ((raw = req->info) == NULL)
400 return; /* That shouldn't happen ... */
401 if (req->type == RAW3215_READ) {
402 /* store residual count, then wait for device end */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200403 req->residual = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 if (dstat == 0x08)
406 break;
407 case 0x04:
408 /* Device end interrupt. */
409 if ((raw = req->info) == NULL)
410 return; /* That shouldn't happen ... */
411 if (req->type == RAW3215_READ && raw->tty != NULL) {
412 unsigned int cchar;
413
414 tty = raw->tty;
415 count = 160 - req->residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 EBCASC(raw->inbuf, count);
417 cchar = ctrlchar_handle(raw->inbuf, count, tty);
418 switch (cchar & CTRLCHAR_MASK) {
419 case CTRLCHAR_SYSRQ:
420 break;
421
422 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800423 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 tty_flip_buffer_push(raw->tty);
425 break;
426
427 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800429 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
430 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
431 /* add the auto \n */
432 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 count++;
434 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800435 count -= 2;
436 tty_insert_flip_string(tty, raw->inbuf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 tty_flip_buffer_push(raw->tty);
438 break;
439 }
440 } else if (req->type == RAW3215_WRITE) {
441 raw->count -= req->len;
442 raw->written -= req->len;
443 }
444 raw->flags &= ~RAW3215_WORKING;
445 raw3215_free_req(req);
446 /* check for empty wait */
447 if (waitqueue_active(&raw->empty_wait) &&
448 raw->queued_write == NULL &&
449 raw->queued_read == NULL) {
450 wake_up_interruptible(&raw->empty_wait);
451 }
452 tasklet_schedule(&raw->tasklet);
453 break;
454 default:
455 /* Strange interrupt, I'll do my best to clean up */
456 if (req != NULL && req->type != RAW3215_FREE) {
457 if (req->type == RAW3215_WRITE) {
458 raw->count -= req->len;
459 raw->written -= req->len;
460 }
461 raw->flags &= ~RAW3215_WORKING;
462 raw3215_free_req(req);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tasklet_schedule(&raw->tasklet);
465 }
466 return;
467}
468
469/*
470 * Wait until length bytes are available int the output buffer.
471 * Has to be called with the s390irq lock held. Can be called
472 * disabled.
473 */
474static void
475raw3215_make_room(struct raw3215_info *raw, unsigned int length)
476{
477 while (RAW3215_BUFFER_SIZE - raw->count < length) {
478 /* there might be a request pending */
479 raw->flags |= RAW3215_FLUSHING;
480 raw3215_mk_write_req(raw);
481 raw3215_try_io(raw);
482 raw->flags &= ~RAW3215_FLUSHING;
483#ifdef CONFIG_TN3215_CONSOLE
484 wait_cons_dev();
485#endif
486 /* Enough room freed up ? */
487 if (RAW3215_BUFFER_SIZE - raw->count >= length)
488 break;
489 /* there might be another cpu waiting for the lock */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100490 spin_unlock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 udelay(100);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100492 spin_lock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494}
495
496/*
497 * String write routine for 3215 devices
498 */
499static void
500raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
501{
502 unsigned long flags;
503 int c, count;
504
505 while (length > 0) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100506 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 count = (length > RAW3215_BUFFER_SIZE) ?
508 RAW3215_BUFFER_SIZE : length;
509 length -= count;
510
511 raw3215_make_room(raw, count);
512
513 /* copy string to output buffer and convert it to EBCDIC */
514 while (1) {
515 c = min_t(int, count,
516 min(RAW3215_BUFFER_SIZE - raw->count,
517 RAW3215_BUFFER_SIZE - raw->head));
518 if (c <= 0)
519 break;
520 memcpy(raw->buffer + raw->head, str, c);
521 ASCEBC(raw->buffer + raw->head, c);
522 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
523 raw->count += c;
524 raw->line_pos += c;
525 str += c;
526 count -= c;
527 }
528 if (!(raw->flags & RAW3215_WORKING)) {
529 raw3215_mk_write_req(raw);
530 /* start or queue request */
531 raw3215_try_io(raw);
532 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100533 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535}
536
537/*
538 * Put character routine for 3215 devices
539 */
540static void
541raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
542{
543 unsigned long flags;
544 unsigned int length, i;
545
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100546 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (ch == '\t') {
548 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
549 raw->line_pos += length;
550 ch = ' ';
551 } else if (ch == '\n') {
552 length = 1;
553 raw->line_pos = 0;
554 } else {
555 length = 1;
556 raw->line_pos++;
557 }
558 raw3215_make_room(raw, length);
559
560 for (i = 0; i < length; i++) {
561 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
562 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
563 raw->count++;
564 }
565 if (!(raw->flags & RAW3215_WORKING)) {
566 raw3215_mk_write_req(raw);
567 /* start or queue request */
568 raw3215_try_io(raw);
569 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100570 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/*
574 * Flush routine, it simply sets the flush flag and tries to start
575 * pending IO.
576 */
577static void
578raw3215_flush_buffer(struct raw3215_info *raw)
579{
580 unsigned long flags;
581
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100582 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (raw->count > 0) {
584 raw->flags |= RAW3215_FLUSHING;
585 raw3215_try_io(raw);
586 raw->flags &= ~RAW3215_FLUSHING;
587 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100588 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
592 * Fire up a 3215 device.
593 */
594static int
595raw3215_startup(struct raw3215_info *raw)
596{
597 unsigned long flags;
598
599 if (raw->flags & RAW3215_ACTIVE)
600 return 0;
601 raw->line_pos = 0;
602 raw->flags |= RAW3215_ACTIVE;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100603 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100605 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 return 0;
608}
609
610/*
611 * Shutdown a 3215 device.
612 */
613static void
614raw3215_shutdown(struct raw3215_info *raw)
615{
616 DECLARE_WAITQUEUE(wait, current);
617 unsigned long flags;
618
619 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
620 return;
621 /* Wait for outstanding requests, then free irq */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100622 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if ((raw->flags & RAW3215_WORKING) ||
624 raw->queued_write != NULL ||
625 raw->queued_read != NULL) {
626 raw->flags |= RAW3215_CLOSING;
627 add_wait_queue(&raw->empty_wait, &wait);
628 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100629 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100631 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 remove_wait_queue(&raw->empty_wait, &wait);
633 set_current_state(TASK_RUNNING);
634 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
635 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100636 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static int
640raw3215_probe (struct ccw_device *cdev)
641{
642 struct raw3215_info *raw;
643 int line;
644
Cornelia Hucka2e53802007-10-12 16:11:52 +0200645 /* Console is special. */
646 if (raw3215[0] && (cdev->dev.driver_data == raw3215[0]))
647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 raw = kmalloc(sizeof(struct raw3215_info) +
649 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
650 if (raw == NULL)
651 return -ENOMEM;
652
653 spin_lock(&raw3215_device_lock);
654 for (line = 0; line < NR_3215; line++) {
655 if (!raw3215[line]) {
656 raw3215[line] = raw;
657 break;
658 }
659 }
660 spin_unlock(&raw3215_device_lock);
661 if (line == NR_3215) {
662 kfree(raw);
663 return -ENODEV;
664 }
665
666 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
668 memset(raw, 0, sizeof(struct raw3215_info));
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800669 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 GFP_KERNEL|GFP_DMA);
671 if (raw->buffer == NULL) {
672 spin_lock(&raw3215_device_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200673 raw3215[line] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 spin_unlock(&raw3215_device_lock);
675 kfree(raw);
676 return -ENOMEM;
677 }
678 tasklet_init(&raw->tasklet,
679 (void (*)(unsigned long)) raw3215_tasklet,
680 (unsigned long) raw);
681 init_waitqueue_head(&raw->empty_wait);
682
683 cdev->dev.driver_data = raw;
684 cdev->handler = raw3215_irq;
685
686 return 0;
687}
688
689static void
690raw3215_remove (struct ccw_device *cdev)
691{
692 struct raw3215_info *raw;
693
694 ccw_device_set_offline(cdev);
695 raw = cdev->dev.driver_data;
696 if (raw) {
697 cdev->dev.driver_data = NULL;
Jesper Juhl17fd6822005-11-07 01:01:30 -0800698 kfree(raw->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(raw);
700 }
701}
702
703static int
704raw3215_set_online (struct ccw_device *cdev)
705{
706 struct raw3215_info *raw;
707
708 raw = cdev->dev.driver_data;
709 if (!raw)
710 return -ENODEV;
711
712 return raw3215_startup(raw);
713}
714
715static int
716raw3215_set_offline (struct ccw_device *cdev)
717{
718 struct raw3215_info *raw;
719
720 raw = cdev->dev.driver_data;
721 if (!raw)
722 return -ENODEV;
723
724 raw3215_shutdown(raw);
725
726 return 0;
727}
728
729static struct ccw_device_id raw3215_id[] = {
730 { CCW_DEVICE(0x3215, 0) },
731 { /* end of list */ },
732};
733
734static struct ccw_driver raw3215_ccw_driver = {
735 .name = "3215",
736 .owner = THIS_MODULE,
737 .ids = raw3215_id,
738 .probe = &raw3215_probe,
739 .remove = &raw3215_remove,
740 .set_online = &raw3215_set_online,
741 .set_offline = &raw3215_set_offline,
742};
743
744#ifdef CONFIG_TN3215_CONSOLE
745/*
746 * Write a string to the 3215 console
747 */
748static void
749con3215_write(struct console *co, const char *str, unsigned int count)
750{
751 struct raw3215_info *raw;
752 int i;
753
754 if (count <= 0)
755 return;
756 raw = raw3215[0]; /* console 3215 is the first one */
757 while (count > 0) {
758 for (i = 0; i < count; i++)
759 if (str[i] == '\t' || str[i] == '\n')
760 break;
761 raw3215_write(raw, str, i);
762 count -= i;
763 str += i;
764 if (count > 0) {
765 raw3215_putchar(raw, *str);
766 count--;
767 str++;
768 }
769 }
770}
771
772static struct tty_driver *con3215_device(struct console *c, int *index)
773{
774 *index = c->index;
775 return tty3215_driver;
776}
777
778/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200779 * panic() calls con3215_flush through a panic_notifier
780 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
782static void
Holger Smolinski2332ce12008-10-10 21:33:27 +0200783con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct raw3215_info *raw;
786 unsigned long flags;
787
788 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100789 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100791 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Holger Smolinski2332ce12008-10-10 21:33:27 +0200794static int con3215_notify(struct notifier_block *self,
795 unsigned long event, void *data)
796{
797 con3215_flush();
798 return NOTIFY_OK;
799}
800
801static struct notifier_block on_panic_nb = {
802 .notifier_call = con3215_notify,
803 .priority = 0,
804};
805
806static struct notifier_block on_reboot_nb = {
807 .notifier_call = con3215_notify,
808 .priority = 0,
809};
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * The console structure for the 3215 console
813 */
814static struct console con3215 = {
815 .name = "ttyS",
816 .write = con3215_write,
817 .device = con3215_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .flags = CON_PRINTBUFFER,
819};
820
821/*
822 * 3215 console initialization code called from console_init().
823 * NOTE: This is called before kmalloc is available.
824 */
825static int __init
826con3215_init(void)
827{
828 struct ccw_device *cdev;
829 struct raw3215_info *raw;
830 struct raw3215_req *req;
831 int i;
832
833 /* Check if 3215 is to be the console */
834 if (!CONSOLE_IS_3215)
835 return -ENODEV;
836
837 /* Set the console mode for VM */
838 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700839 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
840 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
843 /* allocate 3215 request structures */
844 raw3215_freelist = NULL;
845 spin_lock_init(&raw3215_freelist_lock);
846 for (i = 0; i < NR_3215_REQ; i++) {
847 req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
848 req->next = raw3215_freelist;
849 raw3215_freelist = req;
850 }
851
852 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800853 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -ENODEV;
855
856 raw3215[0] = raw = (struct raw3215_info *)
857 alloc_bootmem_low(sizeof(struct raw3215_info));
858 memset(raw, 0, sizeof(struct raw3215_info));
859 raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
860 raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
861 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 cdev->dev.driver_data = raw;
863 cdev->handler = raw3215_irq;
864
865 raw->flags |= RAW3215_FIXED;
866 tasklet_init(&raw->tasklet,
867 (void (*)(unsigned long)) raw3215_tasklet,
868 (unsigned long) raw);
869 init_waitqueue_head(&raw->empty_wait);
870
871 /* Request the console irq */
872 if (raw3215_startup(raw) != 0) {
873 free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
874 free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
875 free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
876 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -ENODEV;
878 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200879 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
880 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 register_console(&con3215);
882 return 0;
883}
884console_initcall(con3215_init);
885#endif
886
887/*
888 * tty3215_open
889 *
890 * This routine is called whenever a 3215 tty is opened.
891 */
892static int
893tty3215_open(struct tty_struct *tty, struct file * filp)
894{
895 struct raw3215_info *raw;
896 int retval, line;
897
898 line = tty->index;
899 if ((line < 0) || (line >= NR_3215))
900 return -ENODEV;
901
902 raw = raw3215[line];
903 if (raw == NULL)
904 return -ENODEV;
905
906 tty->driver_data = raw;
907 raw->tty = tty;
908
909 tty->low_latency = 0; /* don't use bottom half for pushing chars */
910 /*
911 * Start up 3215 device
912 */
913 retval = raw3215_startup(raw);
914 if (retval)
915 return retval;
916
917 return 0;
918}
919
920/*
921 * tty3215_close()
922 *
923 * This routine is called when the 3215 tty is closed. We wait
924 * for the remaining request to be completed. Then we clean up.
925 */
926static void
927tty3215_close(struct tty_struct *tty, struct file * filp)
928{
929 struct raw3215_info *raw;
930
931 raw = (struct raw3215_info *) tty->driver_data;
932 if (raw == NULL || tty->count > 1)
933 return;
934 tty->closing = 1;
935 /* Shutdown the terminal */
936 raw3215_shutdown(raw);
937 tty->closing = 0;
938 raw->tty = NULL;
939}
940
941/*
942 * Returns the amount of free space in the output buffer.
943 */
944static int
945tty3215_write_room(struct tty_struct *tty)
946{
947 struct raw3215_info *raw;
948
949 raw = (struct raw3215_info *) tty->driver_data;
950
951 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
952 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
953 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
954 else
955 return 0;
956}
957
958/*
959 * String write routine for 3215 ttys
960 */
961static int
962tty3215_write(struct tty_struct * tty,
963 const unsigned char *buf, int count)
964{
965 struct raw3215_info *raw;
966
967 if (!tty)
968 return 0;
969 raw = (struct raw3215_info *) tty->driver_data;
970 raw3215_write(raw, buf, count);
971 return count;
972}
973
974/*
975 * Put character routine for 3215 ttys
976 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700977static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978tty3215_put_char(struct tty_struct *tty, unsigned char ch)
979{
980 struct raw3215_info *raw;
981
982 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -0700983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 raw = (struct raw3215_info *) tty->driver_data;
985 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -0700986 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static void
990tty3215_flush_chars(struct tty_struct *tty)
991{
992}
993
994/*
995 * Returns the number of characters in the output buffer
996 */
997static int
998tty3215_chars_in_buffer(struct tty_struct *tty)
999{
1000 struct raw3215_info *raw;
1001
1002 raw = (struct raw3215_info *) tty->driver_data;
1003 return raw->count;
1004}
1005
1006static void
1007tty3215_flush_buffer(struct tty_struct *tty)
1008{
1009 struct raw3215_info *raw;
1010
1011 raw = (struct raw3215_info *) tty->driver_data;
1012 raw3215_flush_buffer(raw);
1013 tty_wakeup(tty);
1014}
1015
1016/*
1017 * Currently we don't have any io controls for 3215 ttys
1018 */
1019static int
1020tty3215_ioctl(struct tty_struct *tty, struct file * file,
1021 unsigned int cmd, unsigned long arg)
1022{
1023 if (tty->flags & (1 << TTY_IO_ERROR))
1024 return -EIO;
1025
1026 switch (cmd) {
1027 default:
1028 return -ENOIOCTLCMD;
1029 }
1030 return 0;
1031}
1032
1033/*
1034 * Disable reading from a 3215 tty
1035 */
1036static void
1037tty3215_throttle(struct tty_struct * tty)
1038{
1039 struct raw3215_info *raw;
1040
1041 raw = (struct raw3215_info *) tty->driver_data;
1042 raw->flags |= RAW3215_THROTTLED;
1043}
1044
1045/*
1046 * Enable reading from a 3215 tty
1047 */
1048static void
1049tty3215_unthrottle(struct tty_struct * tty)
1050{
1051 struct raw3215_info *raw;
1052 unsigned long flags;
1053
1054 raw = (struct raw3215_info *) tty->driver_data;
1055 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001056 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 raw->flags &= ~RAW3215_THROTTLED;
1058 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001059 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061}
1062
1063/*
1064 * Disable writing to a 3215 tty
1065 */
1066static void
1067tty3215_stop(struct tty_struct *tty)
1068{
1069 struct raw3215_info *raw;
1070
1071 raw = (struct raw3215_info *) tty->driver_data;
1072 raw->flags |= RAW3215_STOPPED;
1073}
1074
1075/*
1076 * Enable writing to a 3215 tty
1077 */
1078static void
1079tty3215_start(struct tty_struct *tty)
1080{
1081 struct raw3215_info *raw;
1082 unsigned long flags;
1083
1084 raw = (struct raw3215_info *) tty->driver_data;
1085 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001086 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 raw->flags &= ~RAW3215_STOPPED;
1088 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001089 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091}
1092
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001093static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 .open = tty3215_open,
1095 .close = tty3215_close,
1096 .write = tty3215_write,
1097 .put_char = tty3215_put_char,
1098 .flush_chars = tty3215_flush_chars,
1099 .write_room = tty3215_write_room,
1100 .chars_in_buffer = tty3215_chars_in_buffer,
1101 .flush_buffer = tty3215_flush_buffer,
1102 .ioctl = tty3215_ioctl,
1103 .throttle = tty3215_throttle,
1104 .unthrottle = tty3215_unthrottle,
1105 .stop = tty3215_stop,
1106 .start = tty3215_start,
1107};
1108
1109/*
1110 * 3215 tty registration code called from tty_init().
1111 * Most kernel services (incl. kmalloc) are available at this poimt.
1112 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001113static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114tty3215_init(void)
1115{
1116 struct tty_driver *driver;
1117 int ret;
1118
1119 if (!CONSOLE_IS_3215)
1120 return 0;
1121
1122 driver = alloc_tty_driver(NR_3215);
1123 if (!driver)
1124 return -ENOMEM;
1125
1126 ret = ccw_driver_register(&raw3215_ccw_driver);
1127 if (ret) {
1128 put_tty_driver(driver);
1129 return ret;
1130 }
1131 /*
1132 * Initialize the tty_driver structure
1133 * Entries in tty3215_driver that are NOT initialized:
1134 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1135 */
1136
1137 driver->owner = THIS_MODULE;
1138 driver->driver_name = "tty3215";
1139 driver->name = "ttyS";
1140 driver->major = TTY_MAJOR;
1141 driver->minor_start = 64;
1142 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1143 driver->subtype = SYSTEM_TYPE_TTY;
1144 driver->init_termios = tty_std_termios;
1145 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1146 driver->init_termios.c_oflag = ONLCR | XTABS;
1147 driver->init_termios.c_lflag = ISIG;
1148 driver->flags = TTY_DRIVER_REAL_RAW;
1149 tty_set_operations(driver, &tty3215_ops);
1150 ret = tty_register_driver(driver);
1151 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 put_tty_driver(driver);
1153 return ret;
1154 }
1155 tty3215_driver = driver;
1156 return 0;
1157}
1158
1159static void __exit
1160tty3215_exit(void)
1161{
1162 tty_unregister_driver(tty3215_driver);
1163 put_tty_driver(tty3215_driver);
1164 ccw_driver_unregister(&raw3215_ccw_driver);
1165}
1166
1167module_init(tty3215_init);
1168module_exit(tty3215_exit);