MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / s390 / char / sclp_con.c
blob5a76bea84aa3676fafedf53cc8ac8d8e347ead58
1 /*
2 * drivers/s390/char/sclp_con.c
3 * SCLP line mode console driver
5 * S390 version
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 */
11 #include <linux/config.h>
12 #include <linux/kmod.h>
13 #include <linux/console.h>
14 #include <linux/init.h>
15 #include <linux/timer.h>
16 #include <linux/jiffies.h>
17 #include <linux/bootmem.h>
18 #include <linux/err.h>
20 #include "sclp.h"
21 #include "sclp_rw.h"
22 #include "sclp_tty.h"
24 #define SCLP_CON_PRINT_HEADER "sclp console driver: "
26 #define sclp_console_major 4 /* TTYAUX_MAJOR */
27 #define sclp_console_minor 64
28 #define sclp_console_name "ttyS"
30 /* Lock to guard over changes to global variables */
31 static spinlock_t sclp_con_lock;
32 /* List of free pages that can be used for console output buffering */
33 static struct list_head sclp_con_pages;
34 /* List of full struct sclp_buffer structures ready for output */
35 static struct list_head sclp_con_outqueue;
36 /* Counter how many buffers are emitted (max 1) and how many */
37 /* are on the output queue. */
38 static int sclp_con_buffer_count;
39 /* Pointer to current console buffer */
40 static struct sclp_buffer *sclp_conbuf;
41 /* Timer for delayed output of console messages */
42 static struct timer_list sclp_con_timer;
44 /* Output format for console messages */
45 static unsigned short sclp_con_columns;
46 static unsigned short sclp_con_width_htab;
48 static void
49 sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
51 unsigned long flags;
52 struct sclp_buffer *next;
53 void *page;
55 /* Ignore return code - because console-writes aren't critical,
56 we do without a sophisticated error recovery mechanism. */
57 page = sclp_unmake_buffer(buffer);
58 spin_lock_irqsave(&sclp_con_lock, flags);
59 /* Remove buffer from outqueue */
60 list_del(&buffer->list);
61 sclp_con_buffer_count--;
62 list_add_tail((struct list_head *) page, &sclp_con_pages);
63 /* Check if there is a pending buffer on the out queue. */
64 next = NULL;
65 if (!list_empty(&sclp_con_outqueue))
66 next = list_entry(sclp_con_outqueue.next,
67 struct sclp_buffer, list);
68 spin_unlock_irqrestore(&sclp_con_lock, flags);
69 if (next != NULL)
70 sclp_emit_buffer(next, sclp_conbuf_callback);
73 static inline void
74 sclp_conbuf_emit(void)
76 struct sclp_buffer* buffer;
77 unsigned long flags;
78 int count;
80 spin_lock_irqsave(&sclp_con_lock, flags);
81 buffer = sclp_conbuf;
82 sclp_conbuf = NULL;
83 if (buffer == NULL) {
84 spin_unlock_irqrestore(&sclp_con_lock, flags);
85 return;
87 list_add_tail(&buffer->list, &sclp_con_outqueue);
88 count = sclp_con_buffer_count++;
89 spin_unlock_irqrestore(&sclp_con_lock, flags);
90 if (count == 0)
91 sclp_emit_buffer(buffer, sclp_conbuf_callback);
95 * When this routine is called from the timer then we flush the
96 * temporary write buffer without further waiting on a final new line.
98 static void
99 sclp_console_timeout(unsigned long data)
101 sclp_conbuf_emit();
105 * Writes the given message to S390 system console
107 static void
108 sclp_console_write(struct console *console, const char *message,
109 unsigned int count)
111 unsigned long flags;
112 void *page;
113 int written;
115 if (count == 0)
116 return;
117 spin_lock_irqsave(&sclp_con_lock, flags);
119 * process escape characters, write message into buffer,
120 * send buffer to SCLP
122 do {
123 /* make sure we have a console output buffer */
124 if (sclp_conbuf == NULL) {
125 while (list_empty(&sclp_con_pages)) {
126 spin_unlock_irqrestore(&sclp_con_lock, flags);
127 sclp_sync_wait();
128 spin_lock_irqsave(&sclp_con_lock, flags);
130 page = sclp_con_pages.next;
131 list_del((struct list_head *) page);
132 sclp_conbuf = sclp_make_buffer(page, sclp_con_columns,
133 sclp_con_width_htab);
135 /* try to write the string to the current output buffer */
136 written = sclp_write(sclp_conbuf, (const unsigned char *)
137 message, count);
138 if (written == count)
139 break;
141 * Not all characters could be written to the current
142 * output buffer. Emit the buffer, create a new buffer
143 * and then output the rest of the string.
145 spin_unlock_irqrestore(&sclp_con_lock, flags);
146 sclp_conbuf_emit();
147 spin_lock_irqsave(&sclp_con_lock, flags);
148 message += written;
149 count -= written;
150 } while (count > 0);
151 /* Setup timer to output current console buffer after 1/10 second */
152 if (sclp_conbuf != NULL && sclp_chars_in_buffer(sclp_conbuf) != 0 &&
153 !timer_pending(&sclp_con_timer)) {
154 init_timer(&sclp_con_timer);
155 sclp_con_timer.function = sclp_console_timeout;
156 sclp_con_timer.data = 0UL;
157 sclp_con_timer.expires = jiffies + HZ/10;
158 add_timer(&sclp_con_timer);
160 spin_unlock_irqrestore(&sclp_con_lock, flags);
163 static struct tty_driver *
164 sclp_console_device(struct console *c, int *index)
166 *index = c->index;
167 return sclp_tty_driver;
171 * This routine is called from panic when the kernel
172 * is going to give up. We have to make sure that all buffers
173 * will be flushed to the SCLP.
175 static void
176 sclp_console_unblank(void)
178 unsigned long flags;
180 sclp_conbuf_emit();
181 spin_lock_irqsave(&sclp_con_lock, flags);
182 if (timer_pending(&sclp_con_timer))
183 del_timer(&sclp_con_timer);
184 while (sclp_con_buffer_count > 0) {
185 spin_unlock_irqrestore(&sclp_con_lock, flags);
186 sclp_sync_wait();
187 spin_lock_irqsave(&sclp_con_lock, flags);
189 spin_unlock_irqrestore(&sclp_con_lock, flags);
193 * used to register the SCLP console to the kernel and to
194 * give printk necessary information
196 static struct console sclp_console =
198 .name = sclp_console_name,
199 .write = sclp_console_write,
200 .device = sclp_console_device,
201 .unblank = sclp_console_unblank,
202 .flags = CON_PRINTBUFFER,
203 .index = 0 /* ttyS0 */
207 * called by console_init() in drivers/char/tty_io.c at boot-time.
209 static int __init
210 sclp_console_init(void)
212 void *page;
213 int i;
214 int rc;
216 if (!CONSOLE_IS_SCLP)
217 return 0;
218 rc = sclp_rw_init();
219 if (rc)
220 return rc;
221 /* Allocate pages for output buffering */
222 INIT_LIST_HEAD(&sclp_con_pages);
223 for (i = 0; i < MAX_CONSOLE_PAGES; i++) {
224 page = alloc_bootmem_low_pages(PAGE_SIZE);
225 if (page == NULL)
226 return -ENOMEM;
227 list_add_tail((struct list_head *) page, &sclp_con_pages);
229 INIT_LIST_HEAD(&sclp_con_outqueue);
230 spin_lock_init(&sclp_con_lock);
231 sclp_con_buffer_count = 0;
232 sclp_conbuf = NULL;
233 init_timer(&sclp_con_timer);
235 /* Set output format */
236 if (MACHINE_IS_VM)
238 * save 4 characters for the CPU number
239 * written at start of each line by VM/CP
241 sclp_con_columns = 76;
242 else
243 sclp_con_columns = 80;
244 sclp_con_width_htab = 8;
246 /* enable printk-access to this driver */
247 register_console(&sclp_console);
248 return 0;
251 console_initcall(sclp_console_init);