blob: 4b632504c65ca9b0ebb88f0c0553e39f1dd966cc [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int line_pos; /* position on the line (for tabs) */
97 char ubuffer[80]; /* copy_from_user buffer */
98};
99
100/* array of 3215 devices structures */
101static struct raw3215_info *raw3215[NR_3215];
102/* spinlock to protect the raw3215 array */
103static DEFINE_SPINLOCK(raw3215_device_lock);
104/* list of free request structures */
105static struct raw3215_req *raw3215_freelist;
106/* spinlock to protect free list */
107static spinlock_t raw3215_freelist_lock;
108
109static struct tty_driver *tty3215_driver;
110
111/*
112 * Get a request structure from the free list
113 */
114static inline struct raw3215_req *
115raw3215_alloc_req(void) {
116 struct raw3215_req *req;
117 unsigned long flags;
118
119 spin_lock_irqsave(&raw3215_freelist_lock, flags);
120 req = raw3215_freelist;
121 raw3215_freelist = req->next;
122 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
123 return req;
124}
125
126/*
127 * Put a request structure back to the free list
128 */
129static inline void
130raw3215_free_req(struct raw3215_req *req) {
131 unsigned long flags;
132
133 if (req->type == RAW3215_FREE)
134 return; /* don't free a free request */
135 req->type = RAW3215_FREE;
136 spin_lock_irqsave(&raw3215_freelist_lock, flags);
137 req->next = raw3215_freelist;
138 raw3215_freelist = req;
139 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
140}
141
142/*
143 * Set up a read request that reads up to 160 byte from the 3215 device.
144 * If there is a queued read request it is used, but that shouldn't happen
145 * because a 3215 terminal won't accept a new read before the old one is
146 * completed.
147 */
148static void
149raw3215_mk_read_req(struct raw3215_info *raw)
150{
151 struct raw3215_req *req;
152 struct ccw1 *ccw;
153
154 /* there can only be ONE read request at a time */
155 req = raw->queued_read;
156 if (req == NULL) {
157 /* no queued read request, use new req structure */
158 req = raw3215_alloc_req();
159 req->type = RAW3215_READ;
160 req->info = raw;
161 raw->queued_read = req;
162 }
163
164 ccw = req->ccws;
165 ccw->cmd_code = 0x0A; /* read inquiry */
166 ccw->flags = 0x20; /* ignore incorrect length */
167 ccw->count = 160;
168 ccw->cda = (__u32) __pa(raw->inbuf);
169}
170
171/*
172 * Set up a write request with the information from the main structure.
173 * A ccw chain is created that writes as much as possible from the output
174 * buffer to the 3215 device. If a queued write exists it is replaced by
175 * the new, probably lengthened request.
176 */
177static void
178raw3215_mk_write_req(struct raw3215_info *raw)
179{
180 struct raw3215_req *req;
181 struct ccw1 *ccw;
182 int len, count, ix, lines;
183
184 if (raw->count <= raw->written)
185 return;
186 /* check if there is a queued write request */
187 req = raw->queued_write;
188 if (req == NULL) {
189 /* no queued write request, use new req structure */
190 req = raw3215_alloc_req();
191 req->type = RAW3215_WRITE;
192 req->info = raw;
193 raw->queued_write = req;
194 } else {
195 raw->written -= req->len;
196 }
197
198 ccw = req->ccws;
199 req->start = (raw->head - raw->count + raw->written) &
200 (RAW3215_BUFFER_SIZE - 1);
201 /*
202 * now we have to count newlines. We can at max accept
203 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
204 * a restriction in VM
205 */
206 lines = 0;
207 ix = req->start;
208 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
209 if (raw->buffer[ix] == 0x15)
210 lines++;
211 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
212 }
213 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
214 if (len > RAW3215_MAX_BYTES)
215 len = RAW3215_MAX_BYTES;
216 req->len = len;
217 raw->written += len;
218
219 /* set the indication if we should try to enlarge this request */
220 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
221
222 ix = req->start;
223 while (len > 0) {
224 if (ccw > req->ccws)
225 ccw[-1].flags |= 0x40; /* use command chaining */
226 ccw->cmd_code = 0x01; /* write, auto carrier return */
227 ccw->flags = 0x20; /* ignore incorrect length ind. */
228 ccw->cda =
229 (__u32) __pa(raw->buffer + ix);
230 count = len;
231 if (ix + count > RAW3215_BUFFER_SIZE)
232 count = RAW3215_BUFFER_SIZE - ix;
233 ccw->count = count;
234 len -= count;
235 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
236 ccw++;
237 }
238 /*
239 * Add a NOP to the channel program. 3215 devices are purely
240 * emulated and its much better to avoid the channel end
241 * interrupt in this case.
242 */
243 if (ccw > req->ccws)
244 ccw[-1].flags |= 0x40; /* use command chaining */
245 ccw->cmd_code = 0x03; /* NOP */
246 ccw->flags = 0;
247 ccw->cda = 0;
248 ccw->count = 1;
249}
250
251/*
252 * Start a read or a write request
253 */
254static void
255raw3215_start_io(struct raw3215_info *raw)
256{
257 struct raw3215_req *req;
258 int res;
259
260 req = raw->queued_read;
261 if (req != NULL &&
262 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
263 /* dequeue request */
264 raw->queued_read = NULL;
265 res = ccw_device_start(raw->cdev, req->ccws,
266 (unsigned long) req, 0, 0);
267 if (res != 0) {
268 /* do_IO failed, put request back to queue */
269 raw->queued_read = req;
270 } else {
271 raw->flags |= RAW3215_WORKING;
272 }
273 }
274 req = raw->queued_write;
275 if (req != NULL &&
276 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
277 /* dequeue request */
278 raw->queued_write = NULL;
279 res = ccw_device_start(raw->cdev, req->ccws,
280 (unsigned long) req, 0, 0);
281 if (res != 0) {
282 /* do_IO failed, put request back to queue */
283 raw->queued_write = req;
284 } else {
285 raw->flags |= RAW3215_WORKING;
286 }
287 }
288}
289
290/*
291 * Function to start a delayed output after RAW3215_TIMEOUT seconds
292 */
293static void
294raw3215_timeout(unsigned long __data)
295{
296 struct raw3215_info *raw = (struct raw3215_info *) __data;
297 unsigned long flags;
298
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100299 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (raw->flags & RAW3215_TIMER_RUNS) {
301 del_timer(&raw->timer);
302 raw->flags &= ~RAW3215_TIMER_RUNS;
303 raw3215_mk_write_req(raw);
304 raw3215_start_io(raw);
305 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100306 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
310 * Function to conditionally start an IO. A read is started immediately,
311 * a write is only started immediately if the flush flag is on or the
312 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
313 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
314 */
315static inline void
316raw3215_try_io(struct raw3215_info *raw)
317{
318 if (!(raw->flags & RAW3215_ACTIVE))
319 return;
320 if (raw->queued_read != NULL)
321 raw3215_start_io(raw);
322 else if (raw->queued_write != NULL) {
323 if ((raw->queued_write->delayable == 0) ||
324 (raw->flags & RAW3215_FLUSHING)) {
325 /* execute write requests bigger than minimum size */
326 raw3215_start_io(raw);
327 if (raw->flags & RAW3215_TIMER_RUNS) {
328 del_timer(&raw->timer);
329 raw->flags &= ~RAW3215_TIMER_RUNS;
330 }
331 } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
332 /* delay small writes */
333 init_timer(&raw->timer);
334 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
335 raw->timer.data = (unsigned long) raw;
336 raw->timer.function = raw3215_timeout;
337 add_timer(&raw->timer);
338 raw->flags |= RAW3215_TIMER_RUNS;
339 }
340 }
341}
342
343/*
344 * The bottom half handler routine for 3215 devices. It tries to start
345 * the next IO and wakes up processes waiting on the tty.
346 */
347static void
348raw3215_tasklet(void *data)
349{
350 struct raw3215_info *raw;
351 struct tty_struct *tty;
352 unsigned long flags;
353
354 raw = (struct raw3215_info *) data;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100355 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 raw3215_mk_write_req(raw);
357 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100358 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 tty = raw->tty;
360 if (tty != NULL &&
361 RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
362 tty_wakeup(tty);
363 }
364}
365
366/*
367 * Interrupt routine, called from common io layer
368 */
369static void
370raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
371{
372 struct raw3215_info *raw;
373 struct raw3215_req *req;
374 struct tty_struct *tty;
375 int cstat, dstat;
376 int count, slen;
377
378 raw = cdev->dev.driver_data;
379 req = (struct raw3215_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200380 cstat = irb->scsw.cmd.cstat;
381 dstat = irb->scsw.cmd.dstat;
Martin Schwidefsky26348f72008-07-14 09:59:26 +0200382 if (cstat != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 tasklet_schedule(&raw->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (dstat & 0x01) { /* we got a unit exception */
385 dstat &= ~0x01; /* we can ignore it */
386 }
387 switch (dstat) {
388 case 0x80:
389 if (cstat != 0)
390 break;
391 /* Attention interrupt, someone hit the enter key */
392 raw3215_mk_read_req(raw);
393 if (MACHINE_IS_P390)
394 memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);
395 tasklet_schedule(&raw->tasklet);
396 break;
397 case 0x08:
398 case 0x0C:
399 /* Channel end interrupt. */
400 if ((raw = req->info) == NULL)
401 return; /* That shouldn't happen ... */
402 if (req->type == RAW3215_READ) {
403 /* store residual count, then wait for device end */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200404 req->residual = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 if (dstat == 0x08)
407 break;
408 case 0x04:
409 /* Device end interrupt. */
410 if ((raw = req->info) == NULL)
411 return; /* That shouldn't happen ... */
412 if (req->type == RAW3215_READ && raw->tty != NULL) {
413 unsigned int cchar;
414
415 tty = raw->tty;
416 count = 160 - req->residual;
417 if (MACHINE_IS_P390) {
418 slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);
419 if (count > slen)
420 count = slen;
421 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 EBCASC(raw->inbuf, count);
423 cchar = ctrlchar_handle(raw->inbuf, count, tty);
424 switch (cchar & CTRLCHAR_MASK) {
425 case CTRLCHAR_SYSRQ:
426 break;
427
428 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800429 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 tty_flip_buffer_push(raw->tty);
431 break;
432
433 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800435 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
436 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
437 /* add the auto \n */
438 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 count++;
440 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800441 count -= 2;
442 tty_insert_flip_string(tty, raw->inbuf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 tty_flip_buffer_push(raw->tty);
444 break;
445 }
446 } else if (req->type == RAW3215_WRITE) {
447 raw->count -= req->len;
448 raw->written -= req->len;
449 }
450 raw->flags &= ~RAW3215_WORKING;
451 raw3215_free_req(req);
452 /* check for empty wait */
453 if (waitqueue_active(&raw->empty_wait) &&
454 raw->queued_write == NULL &&
455 raw->queued_read == NULL) {
456 wake_up_interruptible(&raw->empty_wait);
457 }
458 tasklet_schedule(&raw->tasklet);
459 break;
460 default:
461 /* Strange interrupt, I'll do my best to clean up */
462 if (req != NULL && req->type != RAW3215_FREE) {
463 if (req->type == RAW3215_WRITE) {
464 raw->count -= req->len;
465 raw->written -= req->len;
466 }
467 raw->flags &= ~RAW3215_WORKING;
468 raw3215_free_req(req);
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 tasklet_schedule(&raw->tasklet);
471 }
472 return;
473}
474
475/*
476 * Wait until length bytes are available int the output buffer.
477 * Has to be called with the s390irq lock held. Can be called
478 * disabled.
479 */
480static void
481raw3215_make_room(struct raw3215_info *raw, unsigned int length)
482{
483 while (RAW3215_BUFFER_SIZE - raw->count < length) {
484 /* there might be a request pending */
485 raw->flags |= RAW3215_FLUSHING;
486 raw3215_mk_write_req(raw);
487 raw3215_try_io(raw);
488 raw->flags &= ~RAW3215_FLUSHING;
489#ifdef CONFIG_TN3215_CONSOLE
490 wait_cons_dev();
491#endif
492 /* Enough room freed up ? */
493 if (RAW3215_BUFFER_SIZE - raw->count >= length)
494 break;
495 /* there might be another cpu waiting for the lock */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100496 spin_unlock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 udelay(100);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100498 spin_lock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500}
501
502/*
503 * String write routine for 3215 devices
504 */
505static void
506raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
507{
508 unsigned long flags;
509 int c, count;
510
511 while (length > 0) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100512 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 count = (length > RAW3215_BUFFER_SIZE) ?
514 RAW3215_BUFFER_SIZE : length;
515 length -= count;
516
517 raw3215_make_room(raw, count);
518
519 /* copy string to output buffer and convert it to EBCDIC */
520 while (1) {
521 c = min_t(int, count,
522 min(RAW3215_BUFFER_SIZE - raw->count,
523 RAW3215_BUFFER_SIZE - raw->head));
524 if (c <= 0)
525 break;
526 memcpy(raw->buffer + raw->head, str, c);
527 ASCEBC(raw->buffer + raw->head, c);
528 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
529 raw->count += c;
530 raw->line_pos += c;
531 str += c;
532 count -= c;
533 }
534 if (!(raw->flags & RAW3215_WORKING)) {
535 raw3215_mk_write_req(raw);
536 /* start or queue request */
537 raw3215_try_io(raw);
538 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100539 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541}
542
543/*
544 * Put character routine for 3215 devices
545 */
546static void
547raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
548{
549 unsigned long flags;
550 unsigned int length, i;
551
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100552 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (ch == '\t') {
554 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
555 raw->line_pos += length;
556 ch = ' ';
557 } else if (ch == '\n') {
558 length = 1;
559 raw->line_pos = 0;
560 } else {
561 length = 1;
562 raw->line_pos++;
563 }
564 raw3215_make_room(raw, length);
565
566 for (i = 0; i < length; i++) {
567 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
568 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
569 raw->count++;
570 }
571 if (!(raw->flags & RAW3215_WORKING)) {
572 raw3215_mk_write_req(raw);
573 /* start or queue request */
574 raw3215_try_io(raw);
575 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100576 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/*
580 * Flush routine, it simply sets the flush flag and tries to start
581 * pending IO.
582 */
583static void
584raw3215_flush_buffer(struct raw3215_info *raw)
585{
586 unsigned long flags;
587
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100588 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (raw->count > 0) {
590 raw->flags |= RAW3215_FLUSHING;
591 raw3215_try_io(raw);
592 raw->flags &= ~RAW3215_FLUSHING;
593 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100594 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * Fire up a 3215 device.
599 */
600static int
601raw3215_startup(struct raw3215_info *raw)
602{
603 unsigned long flags;
604
605 if (raw->flags & RAW3215_ACTIVE)
606 return 0;
607 raw->line_pos = 0;
608 raw->flags |= RAW3215_ACTIVE;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100609 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100611 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 return 0;
614}
615
616/*
617 * Shutdown a 3215 device.
618 */
619static void
620raw3215_shutdown(struct raw3215_info *raw)
621{
622 DECLARE_WAITQUEUE(wait, current);
623 unsigned long flags;
624
625 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
626 return;
627 /* Wait for outstanding requests, then free irq */
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 if ((raw->flags & RAW3215_WORKING) ||
630 raw->queued_write != NULL ||
631 raw->queued_read != NULL) {
632 raw->flags |= RAW3215_CLOSING;
633 add_wait_queue(&raw->empty_wait, &wait);
634 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100635 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100637 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 remove_wait_queue(&raw->empty_wait, &wait);
639 set_current_state(TASK_RUNNING);
640 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
641 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100642 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645static int
646raw3215_probe (struct ccw_device *cdev)
647{
648 struct raw3215_info *raw;
649 int line;
650
Cornelia Hucka2e53802007-10-12 16:11:52 +0200651 /* Console is special. */
652 if (raw3215[0] && (cdev->dev.driver_data == raw3215[0]))
653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 raw = kmalloc(sizeof(struct raw3215_info) +
655 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
656 if (raw == NULL)
657 return -ENOMEM;
658
659 spin_lock(&raw3215_device_lock);
660 for (line = 0; line < NR_3215; line++) {
661 if (!raw3215[line]) {
662 raw3215[line] = raw;
663 break;
664 }
665 }
666 spin_unlock(&raw3215_device_lock);
667 if (line == NR_3215) {
668 kfree(raw);
669 return -ENODEV;
670 }
671
672 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
674 memset(raw, 0, sizeof(struct raw3215_info));
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800675 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 GFP_KERNEL|GFP_DMA);
677 if (raw->buffer == NULL) {
678 spin_lock(&raw3215_device_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200679 raw3215[line] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_unlock(&raw3215_device_lock);
681 kfree(raw);
682 return -ENOMEM;
683 }
684 tasklet_init(&raw->tasklet,
685 (void (*)(unsigned long)) raw3215_tasklet,
686 (unsigned long) raw);
687 init_waitqueue_head(&raw->empty_wait);
688
689 cdev->dev.driver_data = raw;
690 cdev->handler = raw3215_irq;
691
692 return 0;
693}
694
695static void
696raw3215_remove (struct ccw_device *cdev)
697{
698 struct raw3215_info *raw;
699
700 ccw_device_set_offline(cdev);
701 raw = cdev->dev.driver_data;
702 if (raw) {
703 cdev->dev.driver_data = NULL;
Jesper Juhl17fd6822005-11-07 01:01:30 -0800704 kfree(raw->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kfree(raw);
706 }
707}
708
709static int
710raw3215_set_online (struct ccw_device *cdev)
711{
712 struct raw3215_info *raw;
713
714 raw = cdev->dev.driver_data;
715 if (!raw)
716 return -ENODEV;
717
718 return raw3215_startup(raw);
719}
720
721static int
722raw3215_set_offline (struct ccw_device *cdev)
723{
724 struct raw3215_info *raw;
725
726 raw = cdev->dev.driver_data;
727 if (!raw)
728 return -ENODEV;
729
730 raw3215_shutdown(raw);
731
732 return 0;
733}
734
735static struct ccw_device_id raw3215_id[] = {
736 { CCW_DEVICE(0x3215, 0) },
737 { /* end of list */ },
738};
739
740static struct ccw_driver raw3215_ccw_driver = {
741 .name = "3215",
742 .owner = THIS_MODULE,
743 .ids = raw3215_id,
744 .probe = &raw3215_probe,
745 .remove = &raw3215_remove,
746 .set_online = &raw3215_set_online,
747 .set_offline = &raw3215_set_offline,
748};
749
750#ifdef CONFIG_TN3215_CONSOLE
751/*
752 * Write a string to the 3215 console
753 */
754static void
755con3215_write(struct console *co, const char *str, unsigned int count)
756{
757 struct raw3215_info *raw;
758 int i;
759
760 if (count <= 0)
761 return;
762 raw = raw3215[0]; /* console 3215 is the first one */
763 while (count > 0) {
764 for (i = 0; i < count; i++)
765 if (str[i] == '\t' || str[i] == '\n')
766 break;
767 raw3215_write(raw, str, i);
768 count -= i;
769 str += i;
770 if (count > 0) {
771 raw3215_putchar(raw, *str);
772 count--;
773 str++;
774 }
775 }
776}
777
778static struct tty_driver *con3215_device(struct console *c, int *index)
779{
780 *index = c->index;
781 return tty3215_driver;
782}
783
784/*
785 * panic() calls console_unblank before the system enters a
786 * disabled, endless loop.
787 */
788static void
789con3215_unblank(void)
790{
791 struct raw3215_info *raw;
792 unsigned long flags;
793
794 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100795 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100797 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * The console structure for the 3215 console
802 */
803static struct console con3215 = {
804 .name = "ttyS",
805 .write = con3215_write,
806 .device = con3215_device,
807 .unblank = con3215_unblank,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .flags = CON_PRINTBUFFER,
809};
810
811/*
812 * 3215 console initialization code called from console_init().
813 * NOTE: This is called before kmalloc is available.
814 */
815static int __init
816con3215_init(void)
817{
818 struct ccw_device *cdev;
819 struct raw3215_info *raw;
820 struct raw3215_req *req;
821 int i;
822
823 /* Check if 3215 is to be the console */
824 if (!CONSOLE_IS_3215)
825 return -ENODEV;
826
827 /* Set the console mode for VM */
828 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700829 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
830 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 /* allocate 3215 request structures */
834 raw3215_freelist = NULL;
835 spin_lock_init(&raw3215_freelist_lock);
836 for (i = 0; i < NR_3215_REQ; i++) {
837 req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
838 req->next = raw3215_freelist;
839 raw3215_freelist = req;
840 }
841
842 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800843 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -ENODEV;
845
846 raw3215[0] = raw = (struct raw3215_info *)
847 alloc_bootmem_low(sizeof(struct raw3215_info));
848 memset(raw, 0, sizeof(struct raw3215_info));
849 raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
850 raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
851 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 cdev->dev.driver_data = raw;
853 cdev->handler = raw3215_irq;
854
855 raw->flags |= RAW3215_FIXED;
856 tasklet_init(&raw->tasklet,
857 (void (*)(unsigned long)) raw3215_tasklet,
858 (unsigned long) raw);
859 init_waitqueue_head(&raw->empty_wait);
860
861 /* Request the console irq */
862 if (raw3215_startup(raw) != 0) {
863 free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
864 free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
865 free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
866 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -ENODEV;
868 }
869 register_console(&con3215);
870 return 0;
871}
872console_initcall(con3215_init);
873#endif
874
875/*
876 * tty3215_open
877 *
878 * This routine is called whenever a 3215 tty is opened.
879 */
880static int
881tty3215_open(struct tty_struct *tty, struct file * filp)
882{
883 struct raw3215_info *raw;
884 int retval, line;
885
886 line = tty->index;
887 if ((line < 0) || (line >= NR_3215))
888 return -ENODEV;
889
890 raw = raw3215[line];
891 if (raw == NULL)
892 return -ENODEV;
893
894 tty->driver_data = raw;
895 raw->tty = tty;
896
897 tty->low_latency = 0; /* don't use bottom half for pushing chars */
898 /*
899 * Start up 3215 device
900 */
901 retval = raw3215_startup(raw);
902 if (retval)
903 return retval;
904
905 return 0;
906}
907
908/*
909 * tty3215_close()
910 *
911 * This routine is called when the 3215 tty is closed. We wait
912 * for the remaining request to be completed. Then we clean up.
913 */
914static void
915tty3215_close(struct tty_struct *tty, struct file * filp)
916{
917 struct raw3215_info *raw;
918
919 raw = (struct raw3215_info *) tty->driver_data;
920 if (raw == NULL || tty->count > 1)
921 return;
922 tty->closing = 1;
923 /* Shutdown the terminal */
924 raw3215_shutdown(raw);
925 tty->closing = 0;
926 raw->tty = NULL;
927}
928
929/*
930 * Returns the amount of free space in the output buffer.
931 */
932static int
933tty3215_write_room(struct tty_struct *tty)
934{
935 struct raw3215_info *raw;
936
937 raw = (struct raw3215_info *) tty->driver_data;
938
939 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
940 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
941 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
942 else
943 return 0;
944}
945
946/*
947 * String write routine for 3215 ttys
948 */
949static int
950tty3215_write(struct tty_struct * tty,
951 const unsigned char *buf, int count)
952{
953 struct raw3215_info *raw;
954
955 if (!tty)
956 return 0;
957 raw = (struct raw3215_info *) tty->driver_data;
958 raw3215_write(raw, buf, count);
959 return count;
960}
961
962/*
963 * Put character routine for 3215 ttys
964 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700965static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966tty3215_put_char(struct tty_struct *tty, unsigned char ch)
967{
968 struct raw3215_info *raw;
969
970 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -0700971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 raw = (struct raw3215_info *) tty->driver_data;
973 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -0700974 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977static void
978tty3215_flush_chars(struct tty_struct *tty)
979{
980}
981
982/*
983 * Returns the number of characters in the output buffer
984 */
985static int
986tty3215_chars_in_buffer(struct tty_struct *tty)
987{
988 struct raw3215_info *raw;
989
990 raw = (struct raw3215_info *) tty->driver_data;
991 return raw->count;
992}
993
994static void
995tty3215_flush_buffer(struct tty_struct *tty)
996{
997 struct raw3215_info *raw;
998
999 raw = (struct raw3215_info *) tty->driver_data;
1000 raw3215_flush_buffer(raw);
1001 tty_wakeup(tty);
1002}
1003
1004/*
1005 * Currently we don't have any io controls for 3215 ttys
1006 */
1007static int
1008tty3215_ioctl(struct tty_struct *tty, struct file * file,
1009 unsigned int cmd, unsigned long arg)
1010{
1011 if (tty->flags & (1 << TTY_IO_ERROR))
1012 return -EIO;
1013
1014 switch (cmd) {
1015 default:
1016 return -ENOIOCTLCMD;
1017 }
1018 return 0;
1019}
1020
1021/*
1022 * Disable reading from a 3215 tty
1023 */
1024static void
1025tty3215_throttle(struct tty_struct * tty)
1026{
1027 struct raw3215_info *raw;
1028
1029 raw = (struct raw3215_info *) tty->driver_data;
1030 raw->flags |= RAW3215_THROTTLED;
1031}
1032
1033/*
1034 * Enable reading from a 3215 tty
1035 */
1036static void
1037tty3215_unthrottle(struct tty_struct * tty)
1038{
1039 struct raw3215_info *raw;
1040 unsigned long flags;
1041
1042 raw = (struct raw3215_info *) tty->driver_data;
1043 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001044 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 raw->flags &= ~RAW3215_THROTTLED;
1046 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001047 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049}
1050
1051/*
1052 * Disable writing to a 3215 tty
1053 */
1054static void
1055tty3215_stop(struct tty_struct *tty)
1056{
1057 struct raw3215_info *raw;
1058
1059 raw = (struct raw3215_info *) tty->driver_data;
1060 raw->flags |= RAW3215_STOPPED;
1061}
1062
1063/*
1064 * Enable writing to a 3215 tty
1065 */
1066static void
1067tty3215_start(struct tty_struct *tty)
1068{
1069 struct raw3215_info *raw;
1070 unsigned long flags;
1071
1072 raw = (struct raw3215_info *) tty->driver_data;
1073 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001074 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 raw->flags &= ~RAW3215_STOPPED;
1076 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001077 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079}
1080
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001081static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 .open = tty3215_open,
1083 .close = tty3215_close,
1084 .write = tty3215_write,
1085 .put_char = tty3215_put_char,
1086 .flush_chars = tty3215_flush_chars,
1087 .write_room = tty3215_write_room,
1088 .chars_in_buffer = tty3215_chars_in_buffer,
1089 .flush_buffer = tty3215_flush_buffer,
1090 .ioctl = tty3215_ioctl,
1091 .throttle = tty3215_throttle,
1092 .unthrottle = tty3215_unthrottle,
1093 .stop = tty3215_stop,
1094 .start = tty3215_start,
1095};
1096
1097/*
1098 * 3215 tty registration code called from tty_init().
1099 * Most kernel services (incl. kmalloc) are available at this poimt.
1100 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001101static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102tty3215_init(void)
1103{
1104 struct tty_driver *driver;
1105 int ret;
1106
1107 if (!CONSOLE_IS_3215)
1108 return 0;
1109
1110 driver = alloc_tty_driver(NR_3215);
1111 if (!driver)
1112 return -ENOMEM;
1113
1114 ret = ccw_driver_register(&raw3215_ccw_driver);
1115 if (ret) {
1116 put_tty_driver(driver);
1117 return ret;
1118 }
1119 /*
1120 * Initialize the tty_driver structure
1121 * Entries in tty3215_driver that are NOT initialized:
1122 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1123 */
1124
1125 driver->owner = THIS_MODULE;
1126 driver->driver_name = "tty3215";
1127 driver->name = "ttyS";
1128 driver->major = TTY_MAJOR;
1129 driver->minor_start = 64;
1130 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1131 driver->subtype = SYSTEM_TYPE_TTY;
1132 driver->init_termios = tty_std_termios;
1133 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1134 driver->init_termios.c_oflag = ONLCR | XTABS;
1135 driver->init_termios.c_lflag = ISIG;
1136 driver->flags = TTY_DRIVER_REAL_RAW;
1137 tty_set_operations(driver, &tty3215_ops);
1138 ret = tty_register_driver(driver);
1139 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 put_tty_driver(driver);
1141 return ret;
1142 }
1143 tty3215_driver = driver;
1144 return 0;
1145}
1146
1147static void __exit
1148tty3215_exit(void)
1149{
1150 tty_unregister_driver(tty3215_driver);
1151 put_tty_driver(tty3215_driver);
1152 ccw_driver_unregister(&raw3215_ccw_driver);
1153}
1154
1155module_init(tty3215_init);
1156module_exit(tty3215_exit);