Merge branch 'firewire-kernel-streaming' of git://git.alsa-project.org/alsa-kprivate
[firewire-audio.git] / drivers / s390 / char / con3215.c
blob3fb4335d491d7aaf114769f026d13a4c8cbdcab5
1 /*
2 * 3215 line mode terminal driver.
4 * Copyright IBM Corp. 1999, 2009
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Updated:
8 * Aug-2000: Added tab support
9 * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
12 #include <linux/kernel_stat.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kdev_t.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/vt_kern.h>
19 #include <linux/init.h>
20 #include <linux/console.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/reboot.h>
24 #include <linux/slab.h>
25 #include <asm/ccwdev.h>
26 #include <asm/cio.h>
27 #include <asm/io.h>
28 #include <asm/ebcdic.h>
29 #include <asm/uaccess.h>
30 #include <asm/delay.h>
31 #include <asm/cpcmd.h>
32 #include <asm/setup.h>
34 #include "ctrlchar.h"
36 #define NR_3215 1
37 #define NR_3215_REQ (4*NR_3215)
38 #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
39 #define RAW3215_INBUF_SIZE 256 /* input buffer size */
40 #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
41 #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
42 #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
43 #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
44 #define RAW3215_NR_CCWS 3
45 #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
47 #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
48 #define RAW3215_ACTIVE 2 /* set if the device is in use */
49 #define RAW3215_WORKING 4 /* set if a request is being worked on */
50 #define RAW3215_THROTTLED 8 /* set if reading is disabled */
51 #define RAW3215_STOPPED 16 /* set if writing is disabled */
52 #define RAW3215_CLOSING 32 /* set while in close process */
53 #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
54 #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
55 #define RAW3215_FROZEN 256 /* set if 3215 is frozen for suspend */
57 #define TAB_STOP_SIZE 8 /* tab stop size */
60 * Request types for a 3215 device
62 enum raw3215_type {
63 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
67 * Request structure for a 3215 device
69 struct raw3215_req {
70 enum raw3215_type type; /* type of the request */
71 int start, len; /* start index & len in output buffer */
72 int delayable; /* indication to wait for more data */
73 int residual; /* residual count for read request */
74 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
75 struct raw3215_info *info; /* pointer to main structure */
76 struct raw3215_req *next; /* pointer to next request */
77 } __attribute__ ((aligned(8)));
79 struct raw3215_info {
80 struct ccw_device *cdev; /* device for tty driver */
81 spinlock_t *lock; /* pointer to irq lock */
82 int flags; /* state flags */
83 char *buffer; /* pointer to output buffer */
84 char *inbuf; /* pointer to input buffer */
85 int head; /* first free byte in output buffer */
86 int count; /* number of bytes in output buffer */
87 int written; /* number of bytes in write requests */
88 struct tty_struct *tty; /* pointer to tty structure if present */
89 struct raw3215_req *queued_read; /* pointer to queued read requests */
90 struct raw3215_req *queued_write;/* pointer to queued write requests */
91 wait_queue_head_t empty_wait; /* wait queue for flushing */
92 struct timer_list timer; /* timer for delayed output */
93 int line_pos; /* position on the line (for tabs) */
94 char ubuffer[80]; /* copy_from_user buffer */
97 /* array of 3215 devices structures */
98 static struct raw3215_info *raw3215[NR_3215];
99 /* spinlock to protect the raw3215 array */
100 static DEFINE_SPINLOCK(raw3215_device_lock);
101 /* list of free request structures */
102 static struct raw3215_req *raw3215_freelist;
103 /* spinlock to protect free list */
104 static spinlock_t raw3215_freelist_lock;
106 static struct tty_driver *tty3215_driver;
109 * Get a request structure from the free list
111 static inline struct raw3215_req *raw3215_alloc_req(void)
113 struct raw3215_req *req;
114 unsigned long flags;
116 spin_lock_irqsave(&raw3215_freelist_lock, flags);
117 req = raw3215_freelist;
118 raw3215_freelist = req->next;
119 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
120 return req;
124 * Put a request structure back to the free list
126 static inline void raw3215_free_req(struct raw3215_req *req)
128 unsigned long flags;
130 if (req->type == RAW3215_FREE)
131 return; /* don't free a free request */
132 req->type = RAW3215_FREE;
133 spin_lock_irqsave(&raw3215_freelist_lock, flags);
134 req->next = raw3215_freelist;
135 raw3215_freelist = req;
136 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
140 * Set up a read request that reads up to 160 byte from the 3215 device.
141 * If there is a queued read request it is used, but that shouldn't happen
142 * because a 3215 terminal won't accept a new read before the old one is
143 * completed.
145 static void raw3215_mk_read_req(struct raw3215_info *raw)
147 struct raw3215_req *req;
148 struct ccw1 *ccw;
150 /* there can only be ONE read request at a time */
151 req = raw->queued_read;
152 if (req == NULL) {
153 /* no queued read request, use new req structure */
154 req = raw3215_alloc_req();
155 req->type = RAW3215_READ;
156 req->info = raw;
157 raw->queued_read = req;
160 ccw = req->ccws;
161 ccw->cmd_code = 0x0A; /* read inquiry */
162 ccw->flags = 0x20; /* ignore incorrect length */
163 ccw->count = 160;
164 ccw->cda = (__u32) __pa(raw->inbuf);
168 * Set up a write request with the information from the main structure.
169 * A ccw chain is created that writes as much as possible from the output
170 * buffer to the 3215 device. If a queued write exists it is replaced by
171 * the new, probably lengthened request.
173 static void raw3215_mk_write_req(struct raw3215_info *raw)
175 struct raw3215_req *req;
176 struct ccw1 *ccw;
177 int len, count, ix, lines;
179 if (raw->count <= raw->written)
180 return;
181 /* check if there is a queued write request */
182 req = raw->queued_write;
183 if (req == NULL) {
184 /* no queued write request, use new req structure */
185 req = raw3215_alloc_req();
186 req->type = RAW3215_WRITE;
187 req->info = raw;
188 raw->queued_write = req;
189 } else {
190 raw->written -= req->len;
193 ccw = req->ccws;
194 req->start = (raw->head - raw->count + raw->written) &
195 (RAW3215_BUFFER_SIZE - 1);
197 * now we have to count newlines. We can at max accept
198 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
199 * a restriction in VM
201 lines = 0;
202 ix = req->start;
203 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
204 if (raw->buffer[ix] == 0x15)
205 lines++;
206 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
208 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
209 if (len > RAW3215_MAX_BYTES)
210 len = RAW3215_MAX_BYTES;
211 req->len = len;
212 raw->written += len;
214 /* set the indication if we should try to enlarge this request */
215 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
217 ix = req->start;
218 while (len > 0) {
219 if (ccw > req->ccws)
220 ccw[-1].flags |= 0x40; /* use command chaining */
221 ccw->cmd_code = 0x01; /* write, auto carrier return */
222 ccw->flags = 0x20; /* ignore incorrect length ind. */
223 ccw->cda =
224 (__u32) __pa(raw->buffer + ix);
225 count = len;
226 if (ix + count > RAW3215_BUFFER_SIZE)
227 count = RAW3215_BUFFER_SIZE - ix;
228 ccw->count = count;
229 len -= count;
230 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
231 ccw++;
234 * Add a NOP to the channel program. 3215 devices are purely
235 * emulated and its much better to avoid the channel end
236 * interrupt in this case.
238 if (ccw > req->ccws)
239 ccw[-1].flags |= 0x40; /* use command chaining */
240 ccw->cmd_code = 0x03; /* NOP */
241 ccw->flags = 0;
242 ccw->cda = 0;
243 ccw->count = 1;
247 * Start a read or a write request
249 static void raw3215_start_io(struct raw3215_info *raw)
251 struct raw3215_req *req;
252 int res;
254 req = raw->queued_read;
255 if (req != NULL &&
256 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
257 /* dequeue request */
258 raw->queued_read = NULL;
259 res = ccw_device_start(raw->cdev, req->ccws,
260 (unsigned long) req, 0, 0);
261 if (res != 0) {
262 /* do_IO failed, put request back to queue */
263 raw->queued_read = req;
264 } else {
265 raw->flags |= RAW3215_WORKING;
268 req = raw->queued_write;
269 if (req != NULL &&
270 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
271 /* dequeue request */
272 raw->queued_write = NULL;
273 res = ccw_device_start(raw->cdev, req->ccws,
274 (unsigned long) req, 0, 0);
275 if (res != 0) {
276 /* do_IO failed, put request back to queue */
277 raw->queued_write = req;
278 } else {
279 raw->flags |= RAW3215_WORKING;
285 * Function to start a delayed output after RAW3215_TIMEOUT seconds
287 static void raw3215_timeout(unsigned long __data)
289 struct raw3215_info *raw = (struct raw3215_info *) __data;
290 unsigned long flags;
292 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
293 if (raw->flags & RAW3215_TIMER_RUNS) {
294 del_timer(&raw->timer);
295 raw->flags &= ~RAW3215_TIMER_RUNS;
296 if (!(raw->flags & RAW3215_FROZEN)) {
297 raw3215_mk_write_req(raw);
298 raw3215_start_io(raw);
301 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
305 * Function to conditionally start an IO. A read is started immediately,
306 * a write is only started immediately if the flush flag is on or the
307 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
308 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
310 static inline void raw3215_try_io(struct raw3215_info *raw)
312 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FROZEN))
313 return;
314 if (raw->queued_read != NULL)
315 raw3215_start_io(raw);
316 else if (raw->queued_write != NULL) {
317 if ((raw->queued_write->delayable == 0) ||
318 (raw->flags & RAW3215_FLUSHING)) {
319 /* execute write requests bigger than minimum size */
320 raw3215_start_io(raw);
321 if (raw->flags & RAW3215_TIMER_RUNS) {
322 del_timer(&raw->timer);
323 raw->flags &= ~RAW3215_TIMER_RUNS;
325 } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
326 /* delay small writes */
327 init_timer(&raw->timer);
328 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
329 raw->timer.data = (unsigned long) raw;
330 raw->timer.function = raw3215_timeout;
331 add_timer(&raw->timer);
332 raw->flags |= RAW3215_TIMER_RUNS;
338 * Try to start the next IO and wake up processes waiting on the tty.
340 static void raw3215_next_io(struct raw3215_info *raw)
342 struct tty_struct *tty;
344 raw3215_mk_write_req(raw);
345 raw3215_try_io(raw);
346 tty = raw->tty;
347 if (tty != NULL &&
348 RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
349 tty_wakeup(tty);
354 * Interrupt routine, called from common io layer
356 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
357 struct irb *irb)
359 struct raw3215_info *raw;
360 struct raw3215_req *req;
361 struct tty_struct *tty;
362 int cstat, dstat;
363 int count;
365 kstat_cpu(smp_processor_id()).irqs[IOINT_C15]++;
366 raw = dev_get_drvdata(&cdev->dev);
367 req = (struct raw3215_req *) intparm;
368 cstat = irb->scsw.cmd.cstat;
369 dstat = irb->scsw.cmd.dstat;
370 if (cstat != 0)
371 raw3215_next_io(raw);
372 if (dstat & 0x01) { /* we got a unit exception */
373 dstat &= ~0x01; /* we can ignore it */
375 switch (dstat) {
376 case 0x80:
377 if (cstat != 0)
378 break;
379 /* Attention interrupt, someone hit the enter key */
380 raw3215_mk_read_req(raw);
381 raw3215_next_io(raw);
382 break;
383 case 0x08:
384 case 0x0C:
385 /* Channel end interrupt. */
386 if ((raw = req->info) == NULL)
387 return; /* That shouldn't happen ... */
388 if (req->type == RAW3215_READ) {
389 /* store residual count, then wait for device end */
390 req->residual = irb->scsw.cmd.count;
392 if (dstat == 0x08)
393 break;
394 case 0x04:
395 /* Device end interrupt. */
396 if ((raw = req->info) == NULL)
397 return; /* That shouldn't happen ... */
398 if (req->type == RAW3215_READ && raw->tty != NULL) {
399 unsigned int cchar;
401 tty = raw->tty;
402 count = 160 - req->residual;
403 EBCASC(raw->inbuf, count);
404 cchar = ctrlchar_handle(raw->inbuf, count, tty);
405 switch (cchar & CTRLCHAR_MASK) {
406 case CTRLCHAR_SYSRQ:
407 break;
409 case CTRLCHAR_CTRL:
410 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
411 tty_flip_buffer_push(raw->tty);
412 break;
414 case CTRLCHAR_NONE:
415 if (count < 2 ||
416 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
417 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
418 /* add the auto \n */
419 raw->inbuf[count] = '\n';
420 count++;
421 } else
422 count -= 2;
423 tty_insert_flip_string(tty, raw->inbuf, count);
424 tty_flip_buffer_push(raw->tty);
425 break;
427 } else if (req->type == RAW3215_WRITE) {
428 raw->count -= req->len;
429 raw->written -= req->len;
431 raw->flags &= ~RAW3215_WORKING;
432 raw3215_free_req(req);
433 /* check for empty wait */
434 if (waitqueue_active(&raw->empty_wait) &&
435 raw->queued_write == NULL &&
436 raw->queued_read == NULL) {
437 wake_up_interruptible(&raw->empty_wait);
439 raw3215_next_io(raw);
440 break;
441 default:
442 /* Strange interrupt, I'll do my best to clean up */
443 if (req != NULL && req->type != RAW3215_FREE) {
444 if (req->type == RAW3215_WRITE) {
445 raw->count -= req->len;
446 raw->written -= req->len;
448 raw->flags &= ~RAW3215_WORKING;
449 raw3215_free_req(req);
451 raw3215_next_io(raw);
453 return;
457 * Drop the oldest line from the output buffer.
459 static void raw3215_drop_line(struct raw3215_info *raw)
461 int ix;
462 char ch;
464 BUG_ON(raw->written != 0);
465 ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
466 while (raw->count > 0) {
467 ch = raw->buffer[ix];
468 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
469 raw->count--;
470 if (ch == 0x15)
471 break;
473 raw->head = ix;
477 * Wait until length bytes are available int the output buffer.
478 * Has to be called with the s390irq lock held. Can be called
479 * disabled.
481 static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
483 while (RAW3215_BUFFER_SIZE - raw->count < length) {
484 /* While console is frozen for suspend we have no other
485 * choice but to drop message from the buffer to make
486 * room for even more messages. */
487 if (raw->flags & RAW3215_FROZEN) {
488 raw3215_drop_line(raw);
489 continue;
491 /* there might be a request pending */
492 raw->flags |= RAW3215_FLUSHING;
493 raw3215_mk_write_req(raw);
494 raw3215_try_io(raw);
495 raw->flags &= ~RAW3215_FLUSHING;
496 #ifdef CONFIG_TN3215_CONSOLE
497 wait_cons_dev();
498 #endif
499 /* Enough room freed up ? */
500 if (RAW3215_BUFFER_SIZE - raw->count >= length)
501 break;
502 /* there might be another cpu waiting for the lock */
503 spin_unlock(get_ccwdev_lock(raw->cdev));
504 udelay(100);
505 spin_lock(get_ccwdev_lock(raw->cdev));
510 * String write routine for 3215 devices
512 static void raw3215_write(struct raw3215_info *raw, const char *str,
513 unsigned int length)
515 unsigned long flags;
516 int c, count;
518 while (length > 0) {
519 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
520 count = (length > RAW3215_BUFFER_SIZE) ?
521 RAW3215_BUFFER_SIZE : length;
522 length -= count;
524 raw3215_make_room(raw, count);
526 /* copy string to output buffer and convert it to EBCDIC */
527 while (1) {
528 c = min_t(int, count,
529 min(RAW3215_BUFFER_SIZE - raw->count,
530 RAW3215_BUFFER_SIZE - raw->head));
531 if (c <= 0)
532 break;
533 memcpy(raw->buffer + raw->head, str, c);
534 ASCEBC(raw->buffer + raw->head, c);
535 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
536 raw->count += c;
537 raw->line_pos += c;
538 str += c;
539 count -= c;
541 if (!(raw->flags & RAW3215_WORKING)) {
542 raw3215_mk_write_req(raw);
543 /* start or queue request */
544 raw3215_try_io(raw);
546 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
551 * Put character routine for 3215 devices
553 static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
555 unsigned long flags;
556 unsigned int length, i;
558 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
559 if (ch == '\t') {
560 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
561 raw->line_pos += length;
562 ch = ' ';
563 } else if (ch == '\n') {
564 length = 1;
565 raw->line_pos = 0;
566 } else {
567 length = 1;
568 raw->line_pos++;
570 raw3215_make_room(raw, length);
572 for (i = 0; i < length; i++) {
573 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
574 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
575 raw->count++;
577 if (!(raw->flags & RAW3215_WORKING)) {
578 raw3215_mk_write_req(raw);
579 /* start or queue request */
580 raw3215_try_io(raw);
582 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
586 * Flush routine, it simply sets the flush flag and tries to start
587 * pending IO.
589 static void raw3215_flush_buffer(struct raw3215_info *raw)
591 unsigned long flags;
593 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
594 if (raw->count > 0) {
595 raw->flags |= RAW3215_FLUSHING;
596 raw3215_try_io(raw);
597 raw->flags &= ~RAW3215_FLUSHING;
599 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
603 * Fire up a 3215 device.
605 static int raw3215_startup(struct raw3215_info *raw)
607 unsigned long flags;
609 if (raw->flags & RAW3215_ACTIVE)
610 return 0;
611 raw->line_pos = 0;
612 raw->flags |= RAW3215_ACTIVE;
613 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
614 raw3215_try_io(raw);
615 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
617 return 0;
621 * Shutdown a 3215 device.
623 static void raw3215_shutdown(struct raw3215_info *raw)
625 DECLARE_WAITQUEUE(wait, current);
626 unsigned long flags;
628 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
629 return;
630 /* Wait for outstanding requests, then free irq */
631 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
632 if ((raw->flags & RAW3215_WORKING) ||
633 raw->queued_write != NULL ||
634 raw->queued_read != NULL) {
635 raw->flags |= RAW3215_CLOSING;
636 add_wait_queue(&raw->empty_wait, &wait);
637 set_current_state(TASK_INTERRUPTIBLE);
638 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
639 schedule();
640 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
641 remove_wait_queue(&raw->empty_wait, &wait);
642 set_current_state(TASK_RUNNING);
643 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
645 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
648 static int raw3215_probe (struct ccw_device *cdev)
650 struct raw3215_info *raw;
651 int line;
653 /* Console is special. */
654 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
655 return 0;
656 raw = kmalloc(sizeof(struct raw3215_info) +
657 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
658 if (raw == NULL)
659 return -ENOMEM;
661 spin_lock(&raw3215_device_lock);
662 for (line = 0; line < NR_3215; line++) {
663 if (!raw3215[line]) {
664 raw3215[line] = raw;
665 break;
668 spin_unlock(&raw3215_device_lock);
669 if (line == NR_3215) {
670 kfree(raw);
671 return -ENODEV;
674 raw->cdev = cdev;
675 raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
676 memset(raw, 0, sizeof(struct raw3215_info));
677 raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
678 GFP_KERNEL|GFP_DMA);
679 if (raw->buffer == NULL) {
680 spin_lock(&raw3215_device_lock);
681 raw3215[line] = NULL;
682 spin_unlock(&raw3215_device_lock);
683 kfree(raw);
684 return -ENOMEM;
686 init_waitqueue_head(&raw->empty_wait);
688 dev_set_drvdata(&cdev->dev, raw);
689 cdev->handler = raw3215_irq;
691 return 0;
694 static void raw3215_remove (struct ccw_device *cdev)
696 struct raw3215_info *raw;
698 ccw_device_set_offline(cdev);
699 raw = dev_get_drvdata(&cdev->dev);
700 if (raw) {
701 dev_set_drvdata(&cdev->dev, NULL);
702 kfree(raw->buffer);
703 kfree(raw);
707 static int raw3215_set_online (struct ccw_device *cdev)
709 struct raw3215_info *raw;
711 raw = dev_get_drvdata(&cdev->dev);
712 if (!raw)
713 return -ENODEV;
715 return raw3215_startup(raw);
718 static int raw3215_set_offline (struct ccw_device *cdev)
720 struct raw3215_info *raw;
722 raw = dev_get_drvdata(&cdev->dev);
723 if (!raw)
724 return -ENODEV;
726 raw3215_shutdown(raw);
728 return 0;
731 static int raw3215_pm_stop(struct ccw_device *cdev)
733 struct raw3215_info *raw;
734 unsigned long flags;
736 /* Empty the output buffer, then prevent new I/O. */
737 raw = dev_get_drvdata(&cdev->dev);
738 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
739 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
740 raw->flags |= RAW3215_FROZEN;
741 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
742 return 0;
745 static int raw3215_pm_start(struct ccw_device *cdev)
747 struct raw3215_info *raw;
748 unsigned long flags;
750 /* Allow I/O again and flush output buffer. */
751 raw = dev_get_drvdata(&cdev->dev);
752 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
753 raw->flags &= ~RAW3215_FROZEN;
754 raw->flags |= RAW3215_FLUSHING;
755 raw3215_try_io(raw);
756 raw->flags &= ~RAW3215_FLUSHING;
757 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
758 return 0;
761 static struct ccw_device_id raw3215_id[] = {
762 { CCW_DEVICE(0x3215, 0) },
763 { /* end of list */ },
766 static struct ccw_driver raw3215_ccw_driver = {
767 .name = "3215",
768 .owner = THIS_MODULE,
769 .ids = raw3215_id,
770 .probe = &raw3215_probe,
771 .remove = &raw3215_remove,
772 .set_online = &raw3215_set_online,
773 .set_offline = &raw3215_set_offline,
774 .freeze = &raw3215_pm_stop,
775 .thaw = &raw3215_pm_start,
776 .restore = &raw3215_pm_start,
779 #ifdef CONFIG_TN3215_CONSOLE
781 * Write a string to the 3215 console
783 static void con3215_write(struct console *co, const char *str,
784 unsigned int count)
786 struct raw3215_info *raw;
787 int i;
789 if (count <= 0)
790 return;
791 raw = raw3215[0]; /* console 3215 is the first one */
792 while (count > 0) {
793 for (i = 0; i < count; i++)
794 if (str[i] == '\t' || str[i] == '\n')
795 break;
796 raw3215_write(raw, str, i);
797 count -= i;
798 str += i;
799 if (count > 0) {
800 raw3215_putchar(raw, *str);
801 count--;
802 str++;
807 static struct tty_driver *con3215_device(struct console *c, int *index)
809 *index = c->index;
810 return tty3215_driver;
814 * panic() calls con3215_flush through a panic_notifier
815 * before the system enters a disabled, endless loop.
817 static void con3215_flush(void)
819 struct raw3215_info *raw;
820 unsigned long flags;
822 raw = raw3215[0]; /* console 3215 is the first one */
823 if (raw->flags & RAW3215_FROZEN)
824 /* The console is still frozen for suspend. */
825 if (ccw_device_force_console())
826 /* Forcing didn't work, no panic message .. */
827 return;
828 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
829 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
830 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
833 static int con3215_notify(struct notifier_block *self,
834 unsigned long event, void *data)
836 con3215_flush();
837 return NOTIFY_OK;
840 static struct notifier_block on_panic_nb = {
841 .notifier_call = con3215_notify,
842 .priority = 0,
845 static struct notifier_block on_reboot_nb = {
846 .notifier_call = con3215_notify,
847 .priority = 0,
851 * The console structure for the 3215 console
853 static struct console con3215 = {
854 .name = "ttyS",
855 .write = con3215_write,
856 .device = con3215_device,
857 .flags = CON_PRINTBUFFER,
861 * 3215 console initialization code called from console_init().
863 static int __init con3215_init(void)
865 struct ccw_device *cdev;
866 struct raw3215_info *raw;
867 struct raw3215_req *req;
868 int i;
870 /* Check if 3215 is to be the console */
871 if (!CONSOLE_IS_3215)
872 return -ENODEV;
874 /* Set the console mode for VM */
875 if (MACHINE_IS_VM) {
876 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
877 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
880 /* allocate 3215 request structures */
881 raw3215_freelist = NULL;
882 spin_lock_init(&raw3215_freelist_lock);
883 for (i = 0; i < NR_3215_REQ; i++) {
884 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
885 req->next = raw3215_freelist;
886 raw3215_freelist = req;
889 cdev = ccw_device_probe_console();
890 if (IS_ERR(cdev))
891 return -ENODEV;
893 raw3215[0] = raw = (struct raw3215_info *)
894 kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
895 raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
896 raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
897 raw->cdev = cdev;
898 dev_set_drvdata(&cdev->dev, raw);
899 cdev->handler = raw3215_irq;
901 raw->flags |= RAW3215_FIXED;
902 init_waitqueue_head(&raw->empty_wait);
904 /* Request the console irq */
905 if (raw3215_startup(raw) != 0) {
906 kfree(raw->inbuf);
907 kfree(raw->buffer);
908 kfree(raw);
909 raw3215[0] = NULL;
910 return -ENODEV;
912 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
913 register_reboot_notifier(&on_reboot_nb);
914 register_console(&con3215);
915 return 0;
917 console_initcall(con3215_init);
918 #endif
921 * tty3215_open
923 * This routine is called whenever a 3215 tty is opened.
925 static int tty3215_open(struct tty_struct *tty, struct file * filp)
927 struct raw3215_info *raw;
928 int retval, line;
930 line = tty->index;
931 if ((line < 0) || (line >= NR_3215))
932 return -ENODEV;
934 raw = raw3215[line];
935 if (raw == NULL)
936 return -ENODEV;
938 tty->driver_data = raw;
939 raw->tty = tty;
941 tty->low_latency = 0; /* don't use bottom half for pushing chars */
943 * Start up 3215 device
945 retval = raw3215_startup(raw);
946 if (retval)
947 return retval;
949 return 0;
953 * tty3215_close()
955 * This routine is called when the 3215 tty is closed. We wait
956 * for the remaining request to be completed. Then we clean up.
958 static void tty3215_close(struct tty_struct *tty, struct file * filp)
960 struct raw3215_info *raw;
962 raw = (struct raw3215_info *) tty->driver_data;
963 if (raw == NULL || tty->count > 1)
964 return;
965 tty->closing = 1;
966 /* Shutdown the terminal */
967 raw3215_shutdown(raw);
968 tty->closing = 0;
969 raw->tty = NULL;
973 * Returns the amount of free space in the output buffer.
975 static int tty3215_write_room(struct tty_struct *tty)
977 struct raw3215_info *raw;
979 raw = (struct raw3215_info *) tty->driver_data;
981 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
982 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
983 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
984 else
985 return 0;
989 * String write routine for 3215 ttys
991 static int tty3215_write(struct tty_struct * tty,
992 const unsigned char *buf, int count)
994 struct raw3215_info *raw;
996 if (!tty)
997 return 0;
998 raw = (struct raw3215_info *) tty->driver_data;
999 raw3215_write(raw, buf, count);
1000 return count;
1004 * Put character routine for 3215 ttys
1006 static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
1008 struct raw3215_info *raw;
1010 if (!tty)
1011 return 0;
1012 raw = (struct raw3215_info *) tty->driver_data;
1013 raw3215_putchar(raw, ch);
1014 return 1;
1017 static void tty3215_flush_chars(struct tty_struct *tty)
1022 * Returns the number of characters in the output buffer
1024 static int tty3215_chars_in_buffer(struct tty_struct *tty)
1026 struct raw3215_info *raw;
1028 raw = (struct raw3215_info *) tty->driver_data;
1029 return raw->count;
1032 static void tty3215_flush_buffer(struct tty_struct *tty)
1034 struct raw3215_info *raw;
1036 raw = (struct raw3215_info *) tty->driver_data;
1037 raw3215_flush_buffer(raw);
1038 tty_wakeup(tty);
1042 * Disable reading from a 3215 tty
1044 static void tty3215_throttle(struct tty_struct * tty)
1046 struct raw3215_info *raw;
1048 raw = (struct raw3215_info *) tty->driver_data;
1049 raw->flags |= RAW3215_THROTTLED;
1053 * Enable reading from a 3215 tty
1055 static void tty3215_unthrottle(struct tty_struct * tty)
1057 struct raw3215_info *raw;
1058 unsigned long flags;
1060 raw = (struct raw3215_info *) tty->driver_data;
1061 if (raw->flags & RAW3215_THROTTLED) {
1062 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1063 raw->flags &= ~RAW3215_THROTTLED;
1064 raw3215_try_io(raw);
1065 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1070 * Disable writing to a 3215 tty
1072 static void tty3215_stop(struct tty_struct *tty)
1074 struct raw3215_info *raw;
1076 raw = (struct raw3215_info *) tty->driver_data;
1077 raw->flags |= RAW3215_STOPPED;
1081 * Enable writing to a 3215 tty
1083 static void tty3215_start(struct tty_struct *tty)
1085 struct raw3215_info *raw;
1086 unsigned long flags;
1088 raw = (struct raw3215_info *) tty->driver_data;
1089 if (raw->flags & RAW3215_STOPPED) {
1090 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1091 raw->flags &= ~RAW3215_STOPPED;
1092 raw3215_try_io(raw);
1093 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1097 static const struct tty_operations tty3215_ops = {
1098 .open = tty3215_open,
1099 .close = tty3215_close,
1100 .write = tty3215_write,
1101 .put_char = tty3215_put_char,
1102 .flush_chars = tty3215_flush_chars,
1103 .write_room = tty3215_write_room,
1104 .chars_in_buffer = tty3215_chars_in_buffer,
1105 .flush_buffer = tty3215_flush_buffer,
1106 .throttle = tty3215_throttle,
1107 .unthrottle = tty3215_unthrottle,
1108 .stop = tty3215_stop,
1109 .start = tty3215_start,
1113 * 3215 tty registration code called from tty_init().
1114 * Most kernel services (incl. kmalloc) are available at this poimt.
1116 static int __init tty3215_init(void)
1118 struct tty_driver *driver;
1119 int ret;
1121 if (!CONSOLE_IS_3215)
1122 return 0;
1124 driver = alloc_tty_driver(NR_3215);
1125 if (!driver)
1126 return -ENOMEM;
1128 ret = ccw_driver_register(&raw3215_ccw_driver);
1129 if (ret) {
1130 put_tty_driver(driver);
1131 return ret;
1134 * Initialize the tty_driver structure
1135 * Entries in tty3215_driver that are NOT initialized:
1136 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1139 driver->owner = THIS_MODULE;
1140 driver->driver_name = "tty3215";
1141 driver->name = "ttyS";
1142 driver->major = TTY_MAJOR;
1143 driver->minor_start = 64;
1144 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1145 driver->subtype = SYSTEM_TYPE_TTY;
1146 driver->init_termios = tty_std_termios;
1147 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1148 driver->init_termios.c_oflag = ONLCR | XTABS;
1149 driver->init_termios.c_lflag = ISIG;
1150 driver->flags = TTY_DRIVER_REAL_RAW;
1151 tty_set_operations(driver, &tty3215_ops);
1152 ret = tty_register_driver(driver);
1153 if (ret) {
1154 put_tty_driver(driver);
1155 return ret;
1157 tty3215_driver = driver;
1158 return 0;
1161 static void __exit tty3215_exit(void)
1163 tty_unregister_driver(tty3215_driver);
1164 put_tty_driver(tty3215_driver);
1165 ccw_driver_unregister(&raw3215_ccw_driver);
1168 module_init(tty3215_init);
1169 module_exit(tty3215_exit);