Merge git://git.infradead.org/~dwmw2/hdroneline
[linux-2.6] / drivers / s390 / char / sclp_con.c
1 /*
2  *  drivers/s390/char/sclp_con.c
3  *    SCLP line mode console driver
4  *
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  */
10
11 #include <linux/kmod.h>
12 #include <linux/console.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/jiffies.h>
16 #include <linux/bootmem.h>
17 #include <linux/err.h>
18
19 #include "sclp.h"
20 #include "sclp_rw.h"
21 #include "sclp_tty.h"
22
23 #define SCLP_CON_PRINT_HEADER "sclp console driver: "
24
25 #define sclp_console_major 4            /* TTYAUX_MAJOR */
26 #define sclp_console_minor 64
27 #define sclp_console_name  "ttyS"
28
29 /* Lock to guard over changes to global variables */
30 static spinlock_t sclp_con_lock;
31 /* List of free pages that can be used for console output buffering */
32 static struct list_head sclp_con_pages;
33 /* List of full struct sclp_buffer structures ready for output */
34 static struct list_head sclp_con_outqueue;
35 /* Counter how many buffers are emitted (max 1) and how many */
36 /* are on the output queue. */
37 static int sclp_con_buffer_count;
38 /* Pointer to current console buffer */
39 static struct sclp_buffer *sclp_conbuf;
40 /* Timer for delayed output of console messages */
41 static struct timer_list sclp_con_timer;
42
43 /* Output format for console messages */
44 static unsigned short sclp_con_columns;
45 static unsigned short sclp_con_width_htab;
46
47 static void
48 sclp_conbuf_callback(struct sclp_buffer *buffer, int rc)
49 {
50         unsigned long flags;
51         void *page;
52
53         do {
54                 page = sclp_unmake_buffer(buffer);
55                 spin_lock_irqsave(&sclp_con_lock, flags);
56                 /* Remove buffer from outqueue */
57                 list_del(&buffer->list);
58                 sclp_con_buffer_count--;
59                 list_add_tail((struct list_head *) page, &sclp_con_pages);
60                 /* Check if there is a pending buffer on the out queue. */
61                 buffer = NULL;
62                 if (!list_empty(&sclp_con_outqueue))
63                         buffer = list_entry(sclp_con_outqueue.next,
64                                             struct sclp_buffer, list);
65                 spin_unlock_irqrestore(&sclp_con_lock, flags);
66         } while (buffer && sclp_emit_buffer(buffer, sclp_conbuf_callback));
67 }
68
69 static inline void
70 sclp_conbuf_emit(void)
71 {
72         struct sclp_buffer* buffer;
73         unsigned long flags;
74         int count;
75         int rc;
76
77         spin_lock_irqsave(&sclp_con_lock, flags);
78         buffer = sclp_conbuf;
79         sclp_conbuf = NULL;
80         if (buffer == NULL) {
81                 spin_unlock_irqrestore(&sclp_con_lock, flags);
82                 return;
83         }
84         list_add_tail(&buffer->list, &sclp_con_outqueue);
85         count = sclp_con_buffer_count++;
86         spin_unlock_irqrestore(&sclp_con_lock, flags);
87         if (count)
88                 return;
89         rc = sclp_emit_buffer(buffer, sclp_conbuf_callback);
90         if (rc)
91                 sclp_conbuf_callback(buffer, rc);
92 }
93
94 /*
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.
97  */
98 static void
99 sclp_console_timeout(unsigned long data)
100 {
101         sclp_conbuf_emit();
102 }
103
104 /*
105  * Writes the given message to S390 system console
106  */
107 static void
108 sclp_console_write(struct console *console, const char *message,
109                    unsigned int count)
110 {
111         unsigned long flags;
112         void *page;
113         int written;
114
115         if (count == 0)
116                 return;
117         spin_lock_irqsave(&sclp_con_lock, flags);
118         /*
119          * process escape characters, write message into buffer,
120          * send buffer to SCLP
121          */
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);
129                         }
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);
134                 }
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;
140                 /*
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.
144                  */
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);
159         }
160         spin_unlock_irqrestore(&sclp_con_lock, flags);
161 }
162
163 static struct tty_driver *
164 sclp_console_device(struct console *c, int *index)
165 {
166         *index = c->index;
167         return sclp_tty_driver;
168 }
169
170 /*
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.
174  */
175 static void
176 sclp_console_unblank(void)
177 {
178         unsigned long flags;
179
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);
188         }
189         spin_unlock_irqrestore(&sclp_con_lock, flags);
190 }
191
192 /*
193  * used to register the SCLP console to the kernel and to
194  * give printk necessary information
195  */
196 static struct console sclp_console =
197 {
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 */
204 };
205
206 /*
207  * called by console_init() in drivers/char/tty_io.c at boot-time.
208  */
209 static int __init
210 sclp_console_init(void)
211 {
212         void *page;
213         int i;
214         int rc;
215
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);
228         }
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);
234
235         /* Set output format */
236         if (MACHINE_IS_VM)
237                 /*
238                  * save 4 characters for the CPU number
239                  * written at start of each line by VM/CP
240                  */
241                 sclp_con_columns = 76;
242         else
243                 sclp_con_columns = 80;
244         sclp_con_width_htab = 8;
245
246         /* enable printk-access to this driver */
247         register_console(&sclp_console);
248         return 0;
249 }
250
251 console_initcall(sclp_console_init);