blob: 9ab06e0dad408abdc2b5f66b33b559ec8ec23000 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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/*
Heiko Carstens408aec32008-10-10 21:33:28 +0200344 * Try to start the next IO and wake up processes waiting on the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Heiko Carstens408aec32008-10-10 21:33:28 +0200346static void raw3215_next_io(struct raw3215_info *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 raw3215_mk_write_req(raw);
351 raw3215_try_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 tty = raw->tty;
353 if (tty != NULL &&
354 RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
355 tty_wakeup(tty);
356 }
357}
358
359/*
360 * Interrupt routine, called from common io layer
361 */
362static void
363raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
364{
365 struct raw3215_info *raw;
366 struct raw3215_req *req;
367 struct tty_struct *tty;
368 int cstat, dstat;
Heiko Carstens1d030372008-07-14 09:59:44 +0200369 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 raw = cdev->dev.driver_data;
372 req = (struct raw3215_req *) intparm;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200373 cstat = irb->scsw.cmd.cstat;
374 dstat = irb->scsw.cmd.dstat;
Martin Schwidefsky26348f72008-07-14 09:59:26 +0200375 if (cstat != 0)
Heiko Carstens408aec32008-10-10 21:33:28 +0200376 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (dstat & 0x01) { /* we got a unit exception */
378 dstat &= ~0x01; /* we can ignore it */
379 }
380 switch (dstat) {
381 case 0x80:
382 if (cstat != 0)
383 break;
384 /* Attention interrupt, someone hit the enter key */
385 raw3215_mk_read_req(raw);
Heiko Carstens408aec32008-10-10 21:33:28 +0200386 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388 case 0x08:
389 case 0x0C:
390 /* Channel end interrupt. */
391 if ((raw = req->info) == NULL)
392 return; /* That shouldn't happen ... */
393 if (req->type == RAW3215_READ) {
394 /* store residual count, then wait for device end */
Peter Oberparleiter23d805b2008-07-14 09:58:50 +0200395 req->residual = irb->scsw.cmd.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 if (dstat == 0x08)
398 break;
399 case 0x04:
400 /* Device end interrupt. */
401 if ((raw = req->info) == NULL)
402 return; /* That shouldn't happen ... */
403 if (req->type == RAW3215_READ && raw->tty != NULL) {
404 unsigned int cchar;
405
406 tty = raw->tty;
407 count = 160 - req->residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 EBCASC(raw->inbuf, count);
409 cchar = ctrlchar_handle(raw->inbuf, count, tty);
410 switch (cchar & CTRLCHAR_MASK) {
411 case CTRLCHAR_SYSRQ:
412 break;
413
414 case CTRLCHAR_CTRL:
Alan Cox33f0f882006-01-09 20:54:13 -0800415 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 tty_flip_buffer_push(raw->tty);
417 break;
418
419 case CTRLCHAR_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (count < 2 ||
Alan Cox33f0f882006-01-09 20:54:13 -0800421 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
422 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
423 /* add the auto \n */
424 raw->inbuf[count] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 count++;
426 } else
Alan Cox33f0f882006-01-09 20:54:13 -0800427 count -= 2;
428 tty_insert_flip_string(tty, raw->inbuf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 tty_flip_buffer_push(raw->tty);
430 break;
431 }
432 } else if (req->type == RAW3215_WRITE) {
433 raw->count -= req->len;
434 raw->written -= req->len;
435 }
436 raw->flags &= ~RAW3215_WORKING;
437 raw3215_free_req(req);
438 /* check for empty wait */
439 if (waitqueue_active(&raw->empty_wait) &&
440 raw->queued_write == NULL &&
441 raw->queued_read == NULL) {
442 wake_up_interruptible(&raw->empty_wait);
443 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200444 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446 default:
447 /* Strange interrupt, I'll do my best to clean up */
448 if (req != NULL && req->type != RAW3215_FREE) {
449 if (req->type == RAW3215_WRITE) {
450 raw->count -= req->len;
451 raw->written -= req->len;
452 }
453 raw->flags &= ~RAW3215_WORKING;
454 raw3215_free_req(req);
455 }
Heiko Carstens408aec32008-10-10 21:33:28 +0200456 raw3215_next_io(raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 return;
459}
460
461/*
462 * Wait until length bytes are available int the output buffer.
463 * Has to be called with the s390irq lock held. Can be called
464 * disabled.
465 */
466static void
467raw3215_make_room(struct raw3215_info *raw, unsigned int length)
468{
469 while (RAW3215_BUFFER_SIZE - raw->count < length) {
470 /* there might be a request pending */
471 raw->flags |= RAW3215_FLUSHING;
472 raw3215_mk_write_req(raw);
473 raw3215_try_io(raw);
474 raw->flags &= ~RAW3215_FLUSHING;
475#ifdef CONFIG_TN3215_CONSOLE
476 wait_cons_dev();
477#endif
478 /* Enough room freed up ? */
479 if (RAW3215_BUFFER_SIZE - raw->count >= length)
480 break;
481 /* there might be another cpu waiting for the lock */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100482 spin_unlock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 udelay(100);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100484 spin_lock(get_ccwdev_lock(raw->cdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486}
487
488/*
489 * String write routine for 3215 devices
490 */
491static void
492raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
493{
494 unsigned long flags;
495 int c, count;
496
497 while (length > 0) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100498 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 count = (length > RAW3215_BUFFER_SIZE) ?
500 RAW3215_BUFFER_SIZE : length;
501 length -= count;
502
503 raw3215_make_room(raw, count);
504
505 /* copy string to output buffer and convert it to EBCDIC */
506 while (1) {
507 c = min_t(int, count,
508 min(RAW3215_BUFFER_SIZE - raw->count,
509 RAW3215_BUFFER_SIZE - raw->head));
510 if (c <= 0)
511 break;
512 memcpy(raw->buffer + raw->head, str, c);
513 ASCEBC(raw->buffer + raw->head, c);
514 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
515 raw->count += c;
516 raw->line_pos += c;
517 str += c;
518 count -= c;
519 }
520 if (!(raw->flags & RAW3215_WORKING)) {
521 raw3215_mk_write_req(raw);
522 /* start or queue request */
523 raw3215_try_io(raw);
524 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100525 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527}
528
529/*
530 * Put character routine for 3215 devices
531 */
532static void
533raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
534{
535 unsigned long flags;
536 unsigned int length, i;
537
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100538 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ch == '\t') {
540 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
541 raw->line_pos += length;
542 ch = ' ';
543 } else if (ch == '\n') {
544 length = 1;
545 raw->line_pos = 0;
546 } else {
547 length = 1;
548 raw->line_pos++;
549 }
550 raw3215_make_room(raw, length);
551
552 for (i = 0; i < length; i++) {
553 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
554 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
555 raw->count++;
556 }
557 if (!(raw->flags & RAW3215_WORKING)) {
558 raw3215_mk_write_req(raw);
559 /* start or queue request */
560 raw3215_try_io(raw);
561 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100562 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * Flush routine, it simply sets the flush flag and tries to start
567 * pending IO.
568 */
569static void
570raw3215_flush_buffer(struct raw3215_info *raw)
571{
572 unsigned long flags;
573
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100574 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (raw->count > 0) {
576 raw->flags |= RAW3215_FLUSHING;
577 raw3215_try_io(raw);
578 raw->flags &= ~RAW3215_FLUSHING;
579 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100580 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583/*
584 * Fire up a 3215 device.
585 */
586static int
587raw3215_startup(struct raw3215_info *raw)
588{
589 unsigned long flags;
590
591 if (raw->flags & RAW3215_ACTIVE)
592 return 0;
593 raw->line_pos = 0;
594 raw->flags |= RAW3215_ACTIVE;
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100595 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100597 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 return 0;
600}
601
602/*
603 * Shutdown a 3215 device.
604 */
605static void
606raw3215_shutdown(struct raw3215_info *raw)
607{
608 DECLARE_WAITQUEUE(wait, current);
609 unsigned long flags;
610
611 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
612 return;
613 /* Wait for outstanding requests, then free irq */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100614 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if ((raw->flags & RAW3215_WORKING) ||
616 raw->queued_write != NULL ||
617 raw->queued_read != NULL) {
618 raw->flags |= RAW3215_CLOSING;
619 add_wait_queue(&raw->empty_wait, &wait);
620 set_current_state(TASK_INTERRUPTIBLE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100621 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 schedule();
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100623 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 remove_wait_queue(&raw->empty_wait, &wait);
625 set_current_state(TASK_RUNNING);
626 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
627 }
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100628 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static int
632raw3215_probe (struct ccw_device *cdev)
633{
634 struct raw3215_info *raw;
635 int line;
636
Cornelia Hucka2e53802007-10-12 16:11:52 +0200637 /* Console is special. */
638 if (raw3215[0] && (cdev->dev.driver_data == raw3215[0]))
639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 raw = kmalloc(sizeof(struct raw3215_info) +
641 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
642 if (raw == NULL)
643 return -ENOMEM;
644
645 spin_lock(&raw3215_device_lock);
646 for (line = 0; line < NR_3215; line++) {
647 if (!raw3215[line]) {
648 raw3215[line] = raw;
649 break;
650 }
651 }
652 spin_unlock(&raw3215_device_lock);
653 if (line == NR_3215) {
654 kfree(raw);
655 return -ENODEV;
656 }
657
658 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
660 memset(raw, 0, sizeof(struct raw3215_info));
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800661 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 GFP_KERNEL|GFP_DMA);
663 if (raw->buffer == NULL) {
664 spin_lock(&raw3215_device_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200665 raw3215[line] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_unlock(&raw3215_device_lock);
667 kfree(raw);
668 return -ENOMEM;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 init_waitqueue_head(&raw->empty_wait);
671
672 cdev->dev.driver_data = raw;
673 cdev->handler = raw3215_irq;
674
675 return 0;
676}
677
678static void
679raw3215_remove (struct ccw_device *cdev)
680{
681 struct raw3215_info *raw;
682
683 ccw_device_set_offline(cdev);
684 raw = cdev->dev.driver_data;
685 if (raw) {
686 cdev->dev.driver_data = NULL;
Jesper Juhl17fd6822005-11-07 01:01:30 -0800687 kfree(raw->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 kfree(raw);
689 }
690}
691
692static int
693raw3215_set_online (struct ccw_device *cdev)
694{
695 struct raw3215_info *raw;
696
697 raw = cdev->dev.driver_data;
698 if (!raw)
699 return -ENODEV;
700
701 return raw3215_startup(raw);
702}
703
704static int
705raw3215_set_offline (struct ccw_device *cdev)
706{
707 struct raw3215_info *raw;
708
709 raw = cdev->dev.driver_data;
710 if (!raw)
711 return -ENODEV;
712
713 raw3215_shutdown(raw);
714
715 return 0;
716}
717
718static struct ccw_device_id raw3215_id[] = {
719 { CCW_DEVICE(0x3215, 0) },
720 { /* end of list */ },
721};
722
723static struct ccw_driver raw3215_ccw_driver = {
724 .name = "3215",
725 .owner = THIS_MODULE,
726 .ids = raw3215_id,
727 .probe = &raw3215_probe,
728 .remove = &raw3215_remove,
729 .set_online = &raw3215_set_online,
730 .set_offline = &raw3215_set_offline,
731};
732
733#ifdef CONFIG_TN3215_CONSOLE
734/*
735 * Write a string to the 3215 console
736 */
737static void
738con3215_write(struct console *co, const char *str, unsigned int count)
739{
740 struct raw3215_info *raw;
741 int i;
742
743 if (count <= 0)
744 return;
745 raw = raw3215[0]; /* console 3215 is the first one */
746 while (count > 0) {
747 for (i = 0; i < count; i++)
748 if (str[i] == '\t' || str[i] == '\n')
749 break;
750 raw3215_write(raw, str, i);
751 count -= i;
752 str += i;
753 if (count > 0) {
754 raw3215_putchar(raw, *str);
755 count--;
756 str++;
757 }
758 }
759}
760
761static struct tty_driver *con3215_device(struct console *c, int *index)
762{
763 *index = c->index;
764 return tty3215_driver;
765}
766
767/*
Holger Smolinski2332ce12008-10-10 21:33:27 +0200768 * panic() calls con3215_flush through a panic_notifier
769 * before the system enters a disabled, endless loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
771static void
Holger Smolinski2332ce12008-10-10 21:33:27 +0200772con3215_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct raw3215_info *raw;
775 unsigned long flags;
776
777 raw = raw3215[0]; /* console 3215 is the first one */
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100778 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +0100780 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Holger Smolinski2332ce12008-10-10 21:33:27 +0200783static int con3215_notify(struct notifier_block *self,
784 unsigned long event, void *data)
785{
786 con3215_flush();
787 return NOTIFY_OK;
788}
789
790static struct notifier_block on_panic_nb = {
791 .notifier_call = con3215_notify,
792 .priority = 0,
793};
794
795static struct notifier_block on_reboot_nb = {
796 .notifier_call = con3215_notify,
797 .priority = 0,
798};
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 .flags = CON_PRINTBUFFER,
808};
809
810/*
811 * 3215 console initialization code called from console_init().
812 * NOTE: This is called before kmalloc is available.
813 */
814static int __init
815con3215_init(void)
816{
817 struct ccw_device *cdev;
818 struct raw3215_info *raw;
819 struct raw3215_req *req;
820 int i;
821
822 /* Check if 3215 is to be the console */
823 if (!CONSOLE_IS_3215)
824 return -ENODEV;
825
826 /* Set the console mode for VM */
827 if (MACHINE_IS_VM) {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700828 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
829 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 /* allocate 3215 request structures */
833 raw3215_freelist = NULL;
834 spin_lock_init(&raw3215_freelist_lock);
835 for (i = 0; i < NR_3215_REQ; i++) {
836 req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
837 req->next = raw3215_freelist;
838 raw3215_freelist = req;
839 }
840
841 cdev = ccw_device_probe_console();
Peter Oberparleiter600b5d12006-02-01 03:06:35 -0800842 if (IS_ERR(cdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -ENODEV;
844
845 raw3215[0] = raw = (struct raw3215_info *)
846 alloc_bootmem_low(sizeof(struct raw3215_info));
847 memset(raw, 0, sizeof(struct raw3215_info));
848 raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
849 raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
850 raw->cdev = cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 cdev->dev.driver_data = raw;
852 cdev->handler = raw3215_irq;
853
854 raw->flags |= RAW3215_FIXED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 init_waitqueue_head(&raw->empty_wait);
856
857 /* Request the console irq */
858 if (raw3215_startup(raw) != 0) {
859 free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
860 free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
861 free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
862 raw3215[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return -ENODEV;
864 }
Holger Smolinski2332ce12008-10-10 21:33:27 +0200865 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
866 register_reboot_notifier(&on_reboot_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 register_console(&con3215);
868 return 0;
869}
870console_initcall(con3215_init);
871#endif
872
873/*
874 * tty3215_open
875 *
876 * This routine is called whenever a 3215 tty is opened.
877 */
878static int
879tty3215_open(struct tty_struct *tty, struct file * filp)
880{
881 struct raw3215_info *raw;
882 int retval, line;
883
884 line = tty->index;
885 if ((line < 0) || (line >= NR_3215))
886 return -ENODEV;
887
888 raw = raw3215[line];
889 if (raw == NULL)
890 return -ENODEV;
891
892 tty->driver_data = raw;
893 raw->tty = tty;
894
895 tty->low_latency = 0; /* don't use bottom half for pushing chars */
896 /*
897 * Start up 3215 device
898 */
899 retval = raw3215_startup(raw);
900 if (retval)
901 return retval;
902
903 return 0;
904}
905
906/*
907 * tty3215_close()
908 *
909 * This routine is called when the 3215 tty is closed. We wait
910 * for the remaining request to be completed. Then we clean up.
911 */
912static void
913tty3215_close(struct tty_struct *tty, struct file * filp)
914{
915 struct raw3215_info *raw;
916
917 raw = (struct raw3215_info *) tty->driver_data;
918 if (raw == NULL || tty->count > 1)
919 return;
920 tty->closing = 1;
921 /* Shutdown the terminal */
922 raw3215_shutdown(raw);
923 tty->closing = 0;
924 raw->tty = NULL;
925}
926
927/*
928 * Returns the amount of free space in the output buffer.
929 */
930static int
931tty3215_write_room(struct tty_struct *tty)
932{
933 struct raw3215_info *raw;
934
935 raw = (struct raw3215_info *) tty->driver_data;
936
937 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
938 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
939 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
940 else
941 return 0;
942}
943
944/*
945 * String write routine for 3215 ttys
946 */
947static int
948tty3215_write(struct tty_struct * tty,
949 const unsigned char *buf, int count)
950{
951 struct raw3215_info *raw;
952
953 if (!tty)
954 return 0;
955 raw = (struct raw3215_info *) tty->driver_data;
956 raw3215_write(raw, buf, count);
957 return count;
958}
959
960/*
961 * Put character routine for 3215 ttys
962 */
Alan Cox9e7c9a12008-04-30 00:54:00 -0700963static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964tty3215_put_char(struct tty_struct *tty, unsigned char ch)
965{
966 struct raw3215_info *raw;
967
968 if (!tty)
Alan Cox9e7c9a12008-04-30 00:54:00 -0700969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 raw = (struct raw3215_info *) tty->driver_data;
971 raw3215_putchar(raw, ch);
Alan Cox9e7c9a12008-04-30 00:54:00 -0700972 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975static void
976tty3215_flush_chars(struct tty_struct *tty)
977{
978}
979
980/*
981 * Returns the number of characters in the output buffer
982 */
983static int
984tty3215_chars_in_buffer(struct tty_struct *tty)
985{
986 struct raw3215_info *raw;
987
988 raw = (struct raw3215_info *) tty->driver_data;
989 return raw->count;
990}
991
992static void
993tty3215_flush_buffer(struct tty_struct *tty)
994{
995 struct raw3215_info *raw;
996
997 raw = (struct raw3215_info *) tty->driver_data;
998 raw3215_flush_buffer(raw);
999 tty_wakeup(tty);
1000}
1001
1002/*
1003 * Currently we don't have any io controls for 3215 ttys
1004 */
1005static int
1006tty3215_ioctl(struct tty_struct *tty, struct file * file,
1007 unsigned int cmd, unsigned long arg)
1008{
1009 if (tty->flags & (1 << TTY_IO_ERROR))
1010 return -EIO;
1011
1012 switch (cmd) {
1013 default:
1014 return -ENOIOCTLCMD;
1015 }
1016 return 0;
1017}
1018
1019/*
1020 * Disable reading from a 3215 tty
1021 */
1022static void
1023tty3215_throttle(struct tty_struct * tty)
1024{
1025 struct raw3215_info *raw;
1026
1027 raw = (struct raw3215_info *) tty->driver_data;
1028 raw->flags |= RAW3215_THROTTLED;
1029}
1030
1031/*
1032 * Enable reading from a 3215 tty
1033 */
1034static void
1035tty3215_unthrottle(struct tty_struct * tty)
1036{
1037 struct raw3215_info *raw;
1038 unsigned long flags;
1039
1040 raw = (struct raw3215_info *) tty->driver_data;
1041 if (raw->flags & RAW3215_THROTTLED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001042 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 raw->flags &= ~RAW3215_THROTTLED;
1044 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001045 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047}
1048
1049/*
1050 * Disable writing to a 3215 tty
1051 */
1052static void
1053tty3215_stop(struct tty_struct *tty)
1054{
1055 struct raw3215_info *raw;
1056
1057 raw = (struct raw3215_info *) tty->driver_data;
1058 raw->flags |= RAW3215_STOPPED;
1059}
1060
1061/*
1062 * Enable writing to a 3215 tty
1063 */
1064static void
1065tty3215_start(struct tty_struct *tty)
1066{
1067 struct raw3215_info *raw;
1068 unsigned long flags;
1069
1070 raw = (struct raw3215_info *) tty->driver_data;
1071 if (raw->flags & RAW3215_STOPPED) {
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001072 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 raw->flags &= ~RAW3215_STOPPED;
1074 raw3215_try_io(raw);
Martin Schwidefsky520a4e32006-12-04 15:40:07 +01001075 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077}
1078
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001079static const struct tty_operations tty3215_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 .open = tty3215_open,
1081 .close = tty3215_close,
1082 .write = tty3215_write,
1083 .put_char = tty3215_put_char,
1084 .flush_chars = tty3215_flush_chars,
1085 .write_room = tty3215_write_room,
1086 .chars_in_buffer = tty3215_chars_in_buffer,
1087 .flush_buffer = tty3215_flush_buffer,
1088 .ioctl = tty3215_ioctl,
1089 .throttle = tty3215_throttle,
1090 .unthrottle = tty3215_unthrottle,
1091 .stop = tty3215_stop,
1092 .start = tty3215_start,
1093};
1094
1095/*
1096 * 3215 tty registration code called from tty_init().
1097 * Most kernel services (incl. kmalloc) are available at this poimt.
1098 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001099static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100tty3215_init(void)
1101{
1102 struct tty_driver *driver;
1103 int ret;
1104
1105 if (!CONSOLE_IS_3215)
1106 return 0;
1107
1108 driver = alloc_tty_driver(NR_3215);
1109 if (!driver)
1110 return -ENOMEM;
1111
1112 ret = ccw_driver_register(&raw3215_ccw_driver);
1113 if (ret) {
1114 put_tty_driver(driver);
1115 return ret;
1116 }
1117 /*
1118 * Initialize the tty_driver structure
1119 * Entries in tty3215_driver that are NOT initialized:
1120 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1121 */
1122
1123 driver->owner = THIS_MODULE;
1124 driver->driver_name = "tty3215";
1125 driver->name = "ttyS";
1126 driver->major = TTY_MAJOR;
1127 driver->minor_start = 64;
1128 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1129 driver->subtype = SYSTEM_TYPE_TTY;
1130 driver->init_termios = tty_std_termios;
1131 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1132 driver->init_termios.c_oflag = ONLCR | XTABS;
1133 driver->init_termios.c_lflag = ISIG;
1134 driver->flags = TTY_DRIVER_REAL_RAW;
1135 tty_set_operations(driver, &tty3215_ops);
1136 ret = tty_register_driver(driver);
1137 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 put_tty_driver(driver);
1139 return ret;
1140 }
1141 tty3215_driver = driver;
1142 return 0;
1143}
1144
1145static void __exit
1146tty3215_exit(void)
1147{
1148 tty_unregister_driver(tty3215_driver);
1149 put_tty_driver(tty3215_driver);
1150 ccw_driver_unregister(&raw3215_ccw_driver);
1151}
1152
1153module_init(tty3215_init);
1154module_exit(tty3215_exit);