2 * 3215 line mode terminal driver.
4 * Copyright IBM Corp. 1999, 2009
5 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * Aug-2000: Added tab support
9 * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kdev_t.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/vt_kern.h>
18 #include <linux/init.h>
19 #include <linux/console.h>
20 #include <linux/interrupt.h>
21 #include <linux/err.h>
22 #include <linux/reboot.h>
23 #include <linux/slab.h>
24 #include <asm/ccwdev.h>
27 #include <asm/ebcdic.h>
28 #include <asm/uaccess.h>
29 #include <asm/delay.h>
30 #include <asm/cpcmd.h>
31 #include <asm/setup.h>
36 #define NR_3215_REQ (4*NR_3215)
37 #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
38 #define RAW3215_INBUF_SIZE 256 /* input buffer size */
39 #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
40 #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
41 #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
42 #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
43 #define RAW3215_NR_CCWS 3
44 #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
46 #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
47 #define RAW3215_ACTIVE 2 /* set if the device is in use */
48 #define RAW3215_WORKING 4 /* set if a request is being worked on */
49 #define RAW3215_THROTTLED 8 /* set if reading is disabled */
50 #define RAW3215_STOPPED 16 /* set if writing is disabled */
51 #define RAW3215_CLOSING 32 /* set while in close process */
52 #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
53 #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
54 #define RAW3215_FROZEN 256 /* set if 3215 is frozen for suspend */
56 #define TAB_STOP_SIZE 8 /* tab stop size */
59 * Request types for a 3215 device
62 RAW3215_FREE
, RAW3215_READ
, RAW3215_WRITE
66 * Request structure for a 3215 device
69 enum raw3215_type type
; /* type of the request */
70 int start
, len
; /* start index & len in output buffer */
71 int delayable
; /* indication to wait for more data */
72 int residual
; /* residual count for read request */
73 struct ccw1 ccws
[RAW3215_NR_CCWS
]; /* space for the channel program */
74 struct raw3215_info
*info
; /* pointer to main structure */
75 struct raw3215_req
*next
; /* pointer to next request */
76 } __attribute__ ((aligned(8)));
79 struct ccw_device
*cdev
; /* device for tty driver */
80 spinlock_t
*lock
; /* pointer to irq lock */
81 int flags
; /* state flags */
82 char *buffer
; /* pointer to output buffer */
83 char *inbuf
; /* pointer to input buffer */
84 int head
; /* first free byte in output buffer */
85 int count
; /* number of bytes in output buffer */
86 int written
; /* number of bytes in write requests */
87 struct tty_struct
*tty
; /* pointer to tty structure if present */
88 struct raw3215_req
*queued_read
; /* pointer to queued read requests */
89 struct raw3215_req
*queued_write
;/* pointer to queued write requests */
90 wait_queue_head_t empty_wait
; /* wait queue for flushing */
91 struct timer_list timer
; /* timer for delayed output */
92 int line_pos
; /* position on the line (for tabs) */
93 char ubuffer
[80]; /* copy_from_user buffer */
96 /* array of 3215 devices structures */
97 static struct raw3215_info
*raw3215
[NR_3215
];
98 /* spinlock to protect the raw3215 array */
99 static DEFINE_SPINLOCK(raw3215_device_lock
);
100 /* list of free request structures */
101 static struct raw3215_req
*raw3215_freelist
;
102 /* spinlock to protect free list */
103 static spinlock_t raw3215_freelist_lock
;
105 static struct tty_driver
*tty3215_driver
;
108 * Get a request structure from the free list
110 static inline struct raw3215_req
*raw3215_alloc_req(void)
112 struct raw3215_req
*req
;
115 spin_lock_irqsave(&raw3215_freelist_lock
, flags
);
116 req
= raw3215_freelist
;
117 raw3215_freelist
= req
->next
;
118 spin_unlock_irqrestore(&raw3215_freelist_lock
, flags
);
123 * Put a request structure back to the free list
125 static inline void raw3215_free_req(struct raw3215_req
*req
)
129 if (req
->type
== RAW3215_FREE
)
130 return; /* don't free a free request */
131 req
->type
= RAW3215_FREE
;
132 spin_lock_irqsave(&raw3215_freelist_lock
, flags
);
133 req
->next
= raw3215_freelist
;
134 raw3215_freelist
= req
;
135 spin_unlock_irqrestore(&raw3215_freelist_lock
, flags
);
139 * Set up a read request that reads up to 160 byte from the 3215 device.
140 * If there is a queued read request it is used, but that shouldn't happen
141 * because a 3215 terminal won't accept a new read before the old one is
144 static void raw3215_mk_read_req(struct raw3215_info
*raw
)
146 struct raw3215_req
*req
;
149 /* there can only be ONE read request at a time */
150 req
= raw
->queued_read
;
152 /* no queued read request, use new req structure */
153 req
= raw3215_alloc_req();
154 req
->type
= RAW3215_READ
;
156 raw
->queued_read
= req
;
160 ccw
->cmd_code
= 0x0A; /* read inquiry */
161 ccw
->flags
= 0x20; /* ignore incorrect length */
163 ccw
->cda
= (__u32
) __pa(raw
->inbuf
);
167 * Set up a write request with the information from the main structure.
168 * A ccw chain is created that writes as much as possible from the output
169 * buffer to the 3215 device. If a queued write exists it is replaced by
170 * the new, probably lengthened request.
172 static void raw3215_mk_write_req(struct raw3215_info
*raw
)
174 struct raw3215_req
*req
;
176 int len
, count
, ix
, lines
;
178 if (raw
->count
<= raw
->written
)
180 /* check if there is a queued write request */
181 req
= raw
->queued_write
;
183 /* no queued write request, use new req structure */
184 req
= raw3215_alloc_req();
185 req
->type
= RAW3215_WRITE
;
187 raw
->queued_write
= req
;
189 raw
->written
-= req
->len
;
193 req
->start
= (raw
->head
- raw
->count
+ raw
->written
) &
194 (RAW3215_BUFFER_SIZE
- 1);
196 * now we have to count newlines. We can at max accept
197 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
198 * a restriction in VM
202 while (lines
< RAW3215_MAX_NEWLINE
&& ix
!= raw
->head
) {
203 if (raw
->buffer
[ix
] == 0x15)
205 ix
= (ix
+ 1) & (RAW3215_BUFFER_SIZE
- 1);
207 len
= ((ix
- 1 - req
->start
) & (RAW3215_BUFFER_SIZE
- 1)) + 1;
208 if (len
> RAW3215_MAX_BYTES
)
209 len
= RAW3215_MAX_BYTES
;
213 /* set the indication if we should try to enlarge this request */
214 req
->delayable
= (ix
== raw
->head
) && (len
< RAW3215_MIN_WRITE
);
219 ccw
[-1].flags
|= 0x40; /* use command chaining */
220 ccw
->cmd_code
= 0x01; /* write, auto carrier return */
221 ccw
->flags
= 0x20; /* ignore incorrect length ind. */
223 (__u32
) __pa(raw
->buffer
+ ix
);
225 if (ix
+ count
> RAW3215_BUFFER_SIZE
)
226 count
= RAW3215_BUFFER_SIZE
- ix
;
229 ix
= (ix
+ count
) & (RAW3215_BUFFER_SIZE
- 1);
233 * Add a NOP to the channel program. 3215 devices are purely
234 * emulated and its much better to avoid the channel end
235 * interrupt in this case.
238 ccw
[-1].flags
|= 0x40; /* use command chaining */
239 ccw
->cmd_code
= 0x03; /* NOP */
246 * Start a read or a write request
248 static void raw3215_start_io(struct raw3215_info
*raw
)
250 struct raw3215_req
*req
;
253 req
= raw
->queued_read
;
255 !(raw
->flags
& (RAW3215_WORKING
| RAW3215_THROTTLED
))) {
256 /* dequeue request */
257 raw
->queued_read
= NULL
;
258 res
= ccw_device_start(raw
->cdev
, req
->ccws
,
259 (unsigned long) req
, 0, 0);
261 /* do_IO failed, put request back to queue */
262 raw
->queued_read
= req
;
264 raw
->flags
|= RAW3215_WORKING
;
267 req
= raw
->queued_write
;
269 !(raw
->flags
& (RAW3215_WORKING
| RAW3215_STOPPED
))) {
270 /* dequeue request */
271 raw
->queued_write
= NULL
;
272 res
= ccw_device_start(raw
->cdev
, req
->ccws
,
273 (unsigned long) req
, 0, 0);
275 /* do_IO failed, put request back to queue */
276 raw
->queued_write
= req
;
278 raw
->flags
|= RAW3215_WORKING
;
284 * Function to start a delayed output after RAW3215_TIMEOUT seconds
286 static void raw3215_timeout(unsigned long __data
)
288 struct raw3215_info
*raw
= (struct raw3215_info
*) __data
;
291 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
292 if (raw
->flags
& RAW3215_TIMER_RUNS
) {
293 del_timer(&raw
->timer
);
294 raw
->flags
&= ~RAW3215_TIMER_RUNS
;
295 if (!(raw
->flags
& RAW3215_FROZEN
)) {
296 raw3215_mk_write_req(raw
);
297 raw3215_start_io(raw
);
300 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
304 * Function to conditionally start an IO. A read is started immediately,
305 * a write is only started immediately if the flush flag is on or the
306 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
307 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
309 static inline void raw3215_try_io(struct raw3215_info
*raw
)
311 if (!(raw
->flags
& RAW3215_ACTIVE
) || (raw
->flags
& RAW3215_FROZEN
))
313 if (raw
->queued_read
!= NULL
)
314 raw3215_start_io(raw
);
315 else if (raw
->queued_write
!= NULL
) {
316 if ((raw
->queued_write
->delayable
== 0) ||
317 (raw
->flags
& RAW3215_FLUSHING
)) {
318 /* execute write requests bigger than minimum size */
319 raw3215_start_io(raw
);
320 if (raw
->flags
& RAW3215_TIMER_RUNS
) {
321 del_timer(&raw
->timer
);
322 raw
->flags
&= ~RAW3215_TIMER_RUNS
;
324 } else if (!(raw
->flags
& RAW3215_TIMER_RUNS
)) {
325 /* delay small writes */
326 init_timer(&raw
->timer
);
327 raw
->timer
.expires
= RAW3215_TIMEOUT
+ jiffies
;
328 raw
->timer
.data
= (unsigned long) raw
;
329 raw
->timer
.function
= raw3215_timeout
;
330 add_timer(&raw
->timer
);
331 raw
->flags
|= RAW3215_TIMER_RUNS
;
337 * Try to start the next IO and wake up processes waiting on the tty.
339 static void raw3215_next_io(struct raw3215_info
*raw
)
341 struct tty_struct
*tty
;
343 raw3215_mk_write_req(raw
);
347 RAW3215_BUFFER_SIZE
- raw
->count
>= RAW3215_MIN_SPACE
) {
353 * Interrupt routine, called from common io layer
355 static void raw3215_irq(struct ccw_device
*cdev
, unsigned long intparm
,
358 struct raw3215_info
*raw
;
359 struct raw3215_req
*req
;
360 struct tty_struct
*tty
;
364 raw
= dev_get_drvdata(&cdev
->dev
);
365 req
= (struct raw3215_req
*) intparm
;
366 cstat
= irb
->scsw
.cmd
.cstat
;
367 dstat
= irb
->scsw
.cmd
.dstat
;
369 raw3215_next_io(raw
);
370 if (dstat
& 0x01) { /* we got a unit exception */
371 dstat
&= ~0x01; /* we can ignore it */
377 /* Attention interrupt, someone hit the enter key */
378 raw3215_mk_read_req(raw
);
379 raw3215_next_io(raw
);
383 /* Channel end interrupt. */
384 if ((raw
= req
->info
) == NULL
)
385 return; /* That shouldn't happen ... */
386 if (req
->type
== RAW3215_READ
) {
387 /* store residual count, then wait for device end */
388 req
->residual
= irb
->scsw
.cmd
.count
;
393 /* Device end interrupt. */
394 if ((raw
= req
->info
) == NULL
)
395 return; /* That shouldn't happen ... */
396 if (req
->type
== RAW3215_READ
&& raw
->tty
!= NULL
) {
400 count
= 160 - req
->residual
;
401 EBCASC(raw
->inbuf
, count
);
402 cchar
= ctrlchar_handle(raw
->inbuf
, count
, tty
);
403 switch (cchar
& CTRLCHAR_MASK
) {
408 tty_insert_flip_char(tty
, cchar
, TTY_NORMAL
);
409 tty_flip_buffer_push(raw
->tty
);
414 (strncmp(raw
->inbuf
+count
-2, "\252n", 2) &&
415 strncmp(raw
->inbuf
+count
-2, "^n", 2)) ) {
416 /* add the auto \n */
417 raw
->inbuf
[count
] = '\n';
421 tty_insert_flip_string(tty
, raw
->inbuf
, count
);
422 tty_flip_buffer_push(raw
->tty
);
425 } else if (req
->type
== RAW3215_WRITE
) {
426 raw
->count
-= req
->len
;
427 raw
->written
-= req
->len
;
429 raw
->flags
&= ~RAW3215_WORKING
;
430 raw3215_free_req(req
);
431 /* check for empty wait */
432 if (waitqueue_active(&raw
->empty_wait
) &&
433 raw
->queued_write
== NULL
&&
434 raw
->queued_read
== NULL
) {
435 wake_up_interruptible(&raw
->empty_wait
);
437 raw3215_next_io(raw
);
440 /* Strange interrupt, I'll do my best to clean up */
441 if (req
!= NULL
&& req
->type
!= RAW3215_FREE
) {
442 if (req
->type
== RAW3215_WRITE
) {
443 raw
->count
-= req
->len
;
444 raw
->written
-= req
->len
;
446 raw
->flags
&= ~RAW3215_WORKING
;
447 raw3215_free_req(req
);
449 raw3215_next_io(raw
);
455 * Drop the oldest line from the output buffer.
457 static void raw3215_drop_line(struct raw3215_info
*raw
)
462 BUG_ON(raw
->written
!= 0);
463 ix
= (raw
->head
- raw
->count
) & (RAW3215_BUFFER_SIZE
- 1);
464 while (raw
->count
> 0) {
465 ch
= raw
->buffer
[ix
];
466 ix
= (ix
+ 1) & (RAW3215_BUFFER_SIZE
- 1);
475 * Wait until length bytes are available int the output buffer.
476 * Has to be called with the s390irq lock held. Can be called
479 static void raw3215_make_room(struct raw3215_info
*raw
, unsigned int length
)
481 while (RAW3215_BUFFER_SIZE
- raw
->count
< length
) {
482 /* While console is frozen for suspend we have no other
483 * choice but to drop message from the buffer to make
484 * room for even more messages. */
485 if (raw
->flags
& RAW3215_FROZEN
) {
486 raw3215_drop_line(raw
);
489 /* there might be a request pending */
490 raw
->flags
|= RAW3215_FLUSHING
;
491 raw3215_mk_write_req(raw
);
493 raw
->flags
&= ~RAW3215_FLUSHING
;
494 #ifdef CONFIG_TN3215_CONSOLE
497 /* Enough room freed up ? */
498 if (RAW3215_BUFFER_SIZE
- raw
->count
>= length
)
500 /* there might be another cpu waiting for the lock */
501 spin_unlock(get_ccwdev_lock(raw
->cdev
));
503 spin_lock(get_ccwdev_lock(raw
->cdev
));
508 * String write routine for 3215 devices
510 static void raw3215_write(struct raw3215_info
*raw
, const char *str
,
517 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
518 count
= (length
> RAW3215_BUFFER_SIZE
) ?
519 RAW3215_BUFFER_SIZE
: length
;
522 raw3215_make_room(raw
, count
);
524 /* copy string to output buffer and convert it to EBCDIC */
526 c
= min_t(int, count
,
527 min(RAW3215_BUFFER_SIZE
- raw
->count
,
528 RAW3215_BUFFER_SIZE
- raw
->head
));
531 memcpy(raw
->buffer
+ raw
->head
, str
, c
);
532 ASCEBC(raw
->buffer
+ raw
->head
, c
);
533 raw
->head
= (raw
->head
+ c
) & (RAW3215_BUFFER_SIZE
- 1);
539 if (!(raw
->flags
& RAW3215_WORKING
)) {
540 raw3215_mk_write_req(raw
);
541 /* start or queue request */
544 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
549 * Put character routine for 3215 devices
551 static void raw3215_putchar(struct raw3215_info
*raw
, unsigned char ch
)
554 unsigned int length
, i
;
556 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
558 length
= TAB_STOP_SIZE
- (raw
->line_pos
%TAB_STOP_SIZE
);
559 raw
->line_pos
+= length
;
561 } else if (ch
== '\n') {
568 raw3215_make_room(raw
, length
);
570 for (i
= 0; i
< length
; i
++) {
571 raw
->buffer
[raw
->head
] = (char) _ascebc
[(int) ch
];
572 raw
->head
= (raw
->head
+ 1) & (RAW3215_BUFFER_SIZE
- 1);
575 if (!(raw
->flags
& RAW3215_WORKING
)) {
576 raw3215_mk_write_req(raw
);
577 /* start or queue request */
580 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
584 * Flush routine, it simply sets the flush flag and tries to start
587 static void raw3215_flush_buffer(struct raw3215_info
*raw
)
591 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
592 if (raw
->count
> 0) {
593 raw
->flags
|= RAW3215_FLUSHING
;
595 raw
->flags
&= ~RAW3215_FLUSHING
;
597 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
601 * Fire up a 3215 device.
603 static int raw3215_startup(struct raw3215_info
*raw
)
607 if (raw
->flags
& RAW3215_ACTIVE
)
610 raw
->flags
|= RAW3215_ACTIVE
;
611 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
613 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
619 * Shutdown a 3215 device.
621 static void raw3215_shutdown(struct raw3215_info
*raw
)
623 DECLARE_WAITQUEUE(wait
, current
);
626 if (!(raw
->flags
& RAW3215_ACTIVE
) || (raw
->flags
& RAW3215_FIXED
))
628 /* Wait for outstanding requests, then free irq */
629 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
630 if ((raw
->flags
& RAW3215_WORKING
) ||
631 raw
->queued_write
!= NULL
||
632 raw
->queued_read
!= NULL
) {
633 raw
->flags
|= RAW3215_CLOSING
;
634 add_wait_queue(&raw
->empty_wait
, &wait
);
635 set_current_state(TASK_INTERRUPTIBLE
);
636 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
638 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
639 remove_wait_queue(&raw
->empty_wait
, &wait
);
640 set_current_state(TASK_RUNNING
);
641 raw
->flags
&= ~(RAW3215_ACTIVE
| RAW3215_CLOSING
);
643 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
646 static int raw3215_probe (struct ccw_device
*cdev
)
648 struct raw3215_info
*raw
;
651 /* Console is special. */
652 if (raw3215
[0] && (raw3215
[0] == dev_get_drvdata(&cdev
->dev
)))
654 raw
= kmalloc(sizeof(struct raw3215_info
) +
655 RAW3215_INBUF_SIZE
, GFP_KERNEL
|GFP_DMA
);
659 spin_lock(&raw3215_device_lock
);
660 for (line
= 0; line
< NR_3215
; line
++) {
661 if (!raw3215
[line
]) {
666 spin_unlock(&raw3215_device_lock
);
667 if (line
== NR_3215
) {
673 raw
->inbuf
= (char *) raw
+ sizeof(struct raw3215_info
);
674 memset(raw
, 0, sizeof(struct raw3215_info
));
675 raw
->buffer
= kmalloc(RAW3215_BUFFER_SIZE
,
677 if (raw
->buffer
== NULL
) {
678 spin_lock(&raw3215_device_lock
);
679 raw3215
[line
] = NULL
;
680 spin_unlock(&raw3215_device_lock
);
684 init_waitqueue_head(&raw
->empty_wait
);
686 dev_set_drvdata(&cdev
->dev
, raw
);
687 cdev
->handler
= raw3215_irq
;
692 static void raw3215_remove (struct ccw_device
*cdev
)
694 struct raw3215_info
*raw
;
696 ccw_device_set_offline(cdev
);
697 raw
= dev_get_drvdata(&cdev
->dev
);
699 dev_set_drvdata(&cdev
->dev
, NULL
);
705 static int raw3215_set_online (struct ccw_device
*cdev
)
707 struct raw3215_info
*raw
;
709 raw
= dev_get_drvdata(&cdev
->dev
);
713 return raw3215_startup(raw
);
716 static int raw3215_set_offline (struct ccw_device
*cdev
)
718 struct raw3215_info
*raw
;
720 raw
= dev_get_drvdata(&cdev
->dev
);
724 raw3215_shutdown(raw
);
729 static int raw3215_pm_stop(struct ccw_device
*cdev
)
731 struct raw3215_info
*raw
;
734 /* Empty the output buffer, then prevent new I/O. */
735 raw
= dev_get_drvdata(&cdev
->dev
);
736 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
737 raw3215_make_room(raw
, RAW3215_BUFFER_SIZE
);
738 raw
->flags
|= RAW3215_FROZEN
;
739 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
743 static int raw3215_pm_start(struct ccw_device
*cdev
)
745 struct raw3215_info
*raw
;
748 /* Allow I/O again and flush output buffer. */
749 raw
= dev_get_drvdata(&cdev
->dev
);
750 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
751 raw
->flags
&= ~RAW3215_FROZEN
;
752 raw
->flags
|= RAW3215_FLUSHING
;
754 raw
->flags
&= ~RAW3215_FLUSHING
;
755 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
759 static struct ccw_device_id raw3215_id
[] = {
760 { CCW_DEVICE(0x3215, 0) },
761 { /* end of list */ },
764 static struct ccw_driver raw3215_ccw_driver
= {
766 .owner
= THIS_MODULE
,
768 .probe
= &raw3215_probe
,
769 .remove
= &raw3215_remove
,
770 .set_online
= &raw3215_set_online
,
771 .set_offline
= &raw3215_set_offline
,
772 .freeze
= &raw3215_pm_stop
,
773 .thaw
= &raw3215_pm_start
,
774 .restore
= &raw3215_pm_start
,
777 #ifdef CONFIG_TN3215_CONSOLE
779 * Write a string to the 3215 console
781 static void con3215_write(struct console
*co
, const char *str
,
784 struct raw3215_info
*raw
;
789 raw
= raw3215
[0]; /* console 3215 is the first one */
791 for (i
= 0; i
< count
; i
++)
792 if (str
[i
] == '\t' || str
[i
] == '\n')
794 raw3215_write(raw
, str
, i
);
798 raw3215_putchar(raw
, *str
);
805 static struct tty_driver
*con3215_device(struct console
*c
, int *index
)
808 return tty3215_driver
;
812 * panic() calls con3215_flush through a panic_notifier
813 * before the system enters a disabled, endless loop.
815 static void con3215_flush(void)
817 struct raw3215_info
*raw
;
820 raw
= raw3215
[0]; /* console 3215 is the first one */
821 if (raw
->flags
& RAW3215_FROZEN
)
822 /* The console is still frozen for suspend. */
823 if (ccw_device_force_console())
824 /* Forcing didn't work, no panic message .. */
826 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
827 raw3215_make_room(raw
, RAW3215_BUFFER_SIZE
);
828 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
831 static int con3215_notify(struct notifier_block
*self
,
832 unsigned long event
, void *data
)
838 static struct notifier_block on_panic_nb
= {
839 .notifier_call
= con3215_notify
,
843 static struct notifier_block on_reboot_nb
= {
844 .notifier_call
= con3215_notify
,
849 * The console structure for the 3215 console
851 static struct console con3215
= {
853 .write
= con3215_write
,
854 .device
= con3215_device
,
855 .flags
= CON_PRINTBUFFER
,
859 * 3215 console initialization code called from console_init().
860 * NOTE: This is called before kmalloc is available.
862 static int __init
con3215_init(void)
864 struct ccw_device
*cdev
;
865 struct raw3215_info
*raw
;
866 struct raw3215_req
*req
;
869 /* Check if 3215 is to be the console */
870 if (!CONSOLE_IS_3215
)
873 /* Set the console mode for VM */
875 cpcmd("TERM CONMODE 3215", NULL
, 0, NULL
);
876 cpcmd("TERM AUTOCR OFF", NULL
, 0, NULL
);
879 /* allocate 3215 request structures */
880 raw3215_freelist
= NULL
;
881 spin_lock_init(&raw3215_freelist_lock
);
882 for (i
= 0; i
< NR_3215_REQ
; i
++) {
883 req
= kzalloc(sizeof(struct raw3215_req
), GFP_KERNEL
| GFP_DMA
);
884 req
->next
= raw3215_freelist
;
885 raw3215_freelist
= req
;
888 cdev
= ccw_device_probe_console();
892 raw3215
[0] = raw
= (struct raw3215_info
*)
893 kzalloc(sizeof(struct raw3215_info
), GFP_KERNEL
| GFP_DMA
);
894 raw
->buffer
= kzalloc(RAW3215_BUFFER_SIZE
, GFP_KERNEL
| GFP_DMA
);
895 raw
->inbuf
= kzalloc(RAW3215_INBUF_SIZE
, GFP_KERNEL
| GFP_DMA
);
897 dev_set_drvdata(&cdev
->dev
, raw
);
898 cdev
->handler
= raw3215_irq
;
900 raw
->flags
|= RAW3215_FIXED
;
901 init_waitqueue_head(&raw
->empty_wait
);
903 /* Request the console irq */
904 if (raw3215_startup(raw
) != 0) {
911 atomic_notifier_chain_register(&panic_notifier_list
, &on_panic_nb
);
912 register_reboot_notifier(&on_reboot_nb
);
913 register_console(&con3215
);
916 console_initcall(con3215_init
);
922 * This routine is called whenever a 3215 tty is opened.
924 static int tty3215_open(struct tty_struct
*tty
, struct file
* filp
)
926 struct raw3215_info
*raw
;
930 if ((line
< 0) || (line
>= NR_3215
))
937 tty
->driver_data
= raw
;
940 tty
->low_latency
= 0; /* don't use bottom half for pushing chars */
942 * Start up 3215 device
944 retval
= raw3215_startup(raw
);
954 * This routine is called when the 3215 tty is closed. We wait
955 * for the remaining request to be completed. Then we clean up.
957 static void tty3215_close(struct tty_struct
*tty
, struct file
* filp
)
959 struct raw3215_info
*raw
;
961 raw
= (struct raw3215_info
*) tty
->driver_data
;
962 if (raw
== NULL
|| tty
->count
> 1)
965 /* Shutdown the terminal */
966 raw3215_shutdown(raw
);
972 * Returns the amount of free space in the output buffer.
974 static int tty3215_write_room(struct tty_struct
*tty
)
976 struct raw3215_info
*raw
;
978 raw
= (struct raw3215_info
*) tty
->driver_data
;
980 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
981 if ((RAW3215_BUFFER_SIZE
- raw
->count
- TAB_STOP_SIZE
) >= 0)
982 return RAW3215_BUFFER_SIZE
- raw
->count
- TAB_STOP_SIZE
;
988 * String write routine for 3215 ttys
990 static int tty3215_write(struct tty_struct
* tty
,
991 const unsigned char *buf
, int count
)
993 struct raw3215_info
*raw
;
997 raw
= (struct raw3215_info
*) tty
->driver_data
;
998 raw3215_write(raw
, buf
, count
);
1003 * Put character routine for 3215 ttys
1005 static int tty3215_put_char(struct tty_struct
*tty
, unsigned char ch
)
1007 struct raw3215_info
*raw
;
1011 raw
= (struct raw3215_info
*) tty
->driver_data
;
1012 raw3215_putchar(raw
, ch
);
1016 static void tty3215_flush_chars(struct tty_struct
*tty
)
1021 * Returns the number of characters in the output buffer
1023 static int tty3215_chars_in_buffer(struct tty_struct
*tty
)
1025 struct raw3215_info
*raw
;
1027 raw
= (struct raw3215_info
*) tty
->driver_data
;
1031 static void tty3215_flush_buffer(struct tty_struct
*tty
)
1033 struct raw3215_info
*raw
;
1035 raw
= (struct raw3215_info
*) tty
->driver_data
;
1036 raw3215_flush_buffer(raw
);
1041 * Currently we don't have any io controls for 3215 ttys
1043 static int tty3215_ioctl(struct tty_struct
*tty
, struct file
* file
,
1044 unsigned int cmd
, unsigned long arg
)
1046 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1051 return -ENOIOCTLCMD
;
1057 * Disable reading from a 3215 tty
1059 static void tty3215_throttle(struct tty_struct
* tty
)
1061 struct raw3215_info
*raw
;
1063 raw
= (struct raw3215_info
*) tty
->driver_data
;
1064 raw
->flags
|= RAW3215_THROTTLED
;
1068 * Enable reading from a 3215 tty
1070 static void tty3215_unthrottle(struct tty_struct
* tty
)
1072 struct raw3215_info
*raw
;
1073 unsigned long flags
;
1075 raw
= (struct raw3215_info
*) tty
->driver_data
;
1076 if (raw
->flags
& RAW3215_THROTTLED
) {
1077 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
1078 raw
->flags
&= ~RAW3215_THROTTLED
;
1079 raw3215_try_io(raw
);
1080 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
1085 * Disable writing to a 3215 tty
1087 static void tty3215_stop(struct tty_struct
*tty
)
1089 struct raw3215_info
*raw
;
1091 raw
= (struct raw3215_info
*) tty
->driver_data
;
1092 raw
->flags
|= RAW3215_STOPPED
;
1096 * Enable writing to a 3215 tty
1098 static void tty3215_start(struct tty_struct
*tty
)
1100 struct raw3215_info
*raw
;
1101 unsigned long flags
;
1103 raw
= (struct raw3215_info
*) tty
->driver_data
;
1104 if (raw
->flags
& RAW3215_STOPPED
) {
1105 spin_lock_irqsave(get_ccwdev_lock(raw
->cdev
), flags
);
1106 raw
->flags
&= ~RAW3215_STOPPED
;
1107 raw3215_try_io(raw
);
1108 spin_unlock_irqrestore(get_ccwdev_lock(raw
->cdev
), flags
);
1112 static const struct tty_operations tty3215_ops
= {
1113 .open
= tty3215_open
,
1114 .close
= tty3215_close
,
1115 .write
= tty3215_write
,
1116 .put_char
= tty3215_put_char
,
1117 .flush_chars
= tty3215_flush_chars
,
1118 .write_room
= tty3215_write_room
,
1119 .chars_in_buffer
= tty3215_chars_in_buffer
,
1120 .flush_buffer
= tty3215_flush_buffer
,
1121 .ioctl
= tty3215_ioctl
,
1122 .throttle
= tty3215_throttle
,
1123 .unthrottle
= tty3215_unthrottle
,
1124 .stop
= tty3215_stop
,
1125 .start
= tty3215_start
,
1129 * 3215 tty registration code called from tty_init().
1130 * Most kernel services (incl. kmalloc) are available at this poimt.
1132 static int __init
tty3215_init(void)
1134 struct tty_driver
*driver
;
1137 if (!CONSOLE_IS_3215
)
1140 driver
= alloc_tty_driver(NR_3215
);
1144 ret
= ccw_driver_register(&raw3215_ccw_driver
);
1146 put_tty_driver(driver
);
1150 * Initialize the tty_driver structure
1151 * Entries in tty3215_driver that are NOT initialized:
1152 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1155 driver
->owner
= THIS_MODULE
;
1156 driver
->driver_name
= "tty3215";
1157 driver
->name
= "ttyS";
1158 driver
->major
= TTY_MAJOR
;
1159 driver
->minor_start
= 64;
1160 driver
->type
= TTY_DRIVER_TYPE_SYSTEM
;
1161 driver
->subtype
= SYSTEM_TYPE_TTY
;
1162 driver
->init_termios
= tty_std_termios
;
1163 driver
->init_termios
.c_iflag
= IGNBRK
| IGNPAR
;
1164 driver
->init_termios
.c_oflag
= ONLCR
| XTABS
;
1165 driver
->init_termios
.c_lflag
= ISIG
;
1166 driver
->flags
= TTY_DRIVER_REAL_RAW
;
1167 tty_set_operations(driver
, &tty3215_ops
);
1168 ret
= tty_register_driver(driver
);
1170 put_tty_driver(driver
);
1173 tty3215_driver
= driver
;
1177 static void __exit
tty3215_exit(void)
1179 tty_unregister_driver(tty3215_driver
);
1180 put_tty_driver(tty3215_driver
);
1181 ccw_driver_unregister(&raw3215_ccw_driver
);
1184 module_init(tty3215_init
);
1185 module_exit(tty3215_exit
);