2 * SCLP line mode console driver
4 * Copyright IBM Corp. 1999, 2009
5 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 #include <linux/kmod.h>
10 #include <linux/console.h>
11 #include <linux/init.h>
12 #include <linux/timer.h>
13 #include <linux/jiffies.h>
14 #include <linux/termios.h>
15 #include <linux/err.h>
16 #include <linux/reboot.h>
22 #define sclp_console_major 4 /* TTYAUX_MAJOR */
23 #define sclp_console_minor 64
24 #define sclp_console_name "ttyS"
26 /* Lock to guard over changes to global variables */
27 static spinlock_t sclp_con_lock
;
28 /* List of free pages that can be used for console output buffering */
29 static struct list_head sclp_con_pages
;
30 /* List of full struct sclp_buffer structures ready for output */
31 static struct list_head sclp_con_outqueue
;
32 /* Pointer to current console buffer */
33 static struct sclp_buffer
*sclp_conbuf
;
34 /* Timer for delayed output of console messages */
35 static struct timer_list sclp_con_timer
;
36 /* Suspend mode flag */
37 static int sclp_con_suspended
;
38 /* Flag that output queue is currently running */
39 static int sclp_con_queue_running
;
41 /* Output format for console messages */
42 static unsigned short sclp_con_columns
;
43 static unsigned short sclp_con_width_htab
;
46 sclp_conbuf_callback(struct sclp_buffer
*buffer
, int rc
)
52 page
= sclp_unmake_buffer(buffer
);
53 spin_lock_irqsave(&sclp_con_lock
, flags
);
55 /* Remove buffer from outqueue */
56 list_del(&buffer
->list
);
57 list_add_tail((struct list_head
*) page
, &sclp_con_pages
);
59 /* Check if there is a pending buffer on the out queue. */
61 if (!list_empty(&sclp_con_outqueue
))
62 buffer
= list_first_entry(&sclp_con_outqueue
,
63 struct sclp_buffer
, list
);
64 if (!buffer
|| sclp_con_suspended
) {
65 sclp_con_queue_running
= 0;
66 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
69 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
70 } while (sclp_emit_buffer(buffer
, sclp_conbuf_callback
));
74 * Finalize and emit first pending buffer.
76 static void sclp_conbuf_emit(void)
78 struct sclp_buffer
* buffer
;
82 spin_lock_irqsave(&sclp_con_lock
, flags
);
84 list_add_tail(&sclp_conbuf
->list
, &sclp_con_outqueue
);
86 if (sclp_con_queue_running
|| sclp_con_suspended
)
88 if (list_empty(&sclp_con_outqueue
))
90 buffer
= list_first_entry(&sclp_con_outqueue
, struct sclp_buffer
,
92 sclp_con_queue_running
= 1;
93 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
95 rc
= sclp_emit_buffer(buffer
, sclp_conbuf_callback
);
97 sclp_conbuf_callback(buffer
, rc
);
100 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
104 * Wait until out queue is empty
106 static void sclp_console_sync_queue(void)
110 spin_lock_irqsave(&sclp_con_lock
, flags
);
111 if (timer_pending(&sclp_con_timer
))
112 del_timer(&sclp_con_timer
);
113 while (sclp_con_queue_running
) {
114 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
116 spin_lock_irqsave(&sclp_con_lock
, flags
);
118 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
122 * When this routine is called from the timer then we flush the
123 * temporary write buffer without further waiting on a final new line.
126 sclp_console_timeout(unsigned long data
)
132 * Writes the given message to S390 system console
135 sclp_console_write(struct console
*console
, const char *message
,
144 spin_lock_irqsave(&sclp_con_lock
, flags
);
146 * process escape characters, write message into buffer,
147 * send buffer to SCLP
150 /* make sure we have a console output buffer */
151 if (sclp_conbuf
== NULL
) {
152 while (list_empty(&sclp_con_pages
)) {
153 if (sclp_con_suspended
)
155 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
157 spin_lock_irqsave(&sclp_con_lock
, flags
);
159 page
= sclp_con_pages
.next
;
160 list_del((struct list_head
*) page
);
161 sclp_conbuf
= sclp_make_buffer(page
, sclp_con_columns
,
162 sclp_con_width_htab
);
164 /* try to write the string to the current output buffer */
165 written
= sclp_write(sclp_conbuf
, (const unsigned char *)
167 if (written
== count
)
170 * Not all characters could be written to the current
171 * output buffer. Emit the buffer, create a new buffer
172 * and then output the rest of the string.
174 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
176 spin_lock_irqsave(&sclp_con_lock
, flags
);
180 /* Setup timer to output current console buffer after 1/10 second */
181 if (sclp_conbuf
!= NULL
&& sclp_chars_in_buffer(sclp_conbuf
) != 0 &&
182 !timer_pending(&sclp_con_timer
)) {
183 init_timer(&sclp_con_timer
);
184 sclp_con_timer
.function
= sclp_console_timeout
;
185 sclp_con_timer
.data
= 0UL;
186 sclp_con_timer
.expires
= jiffies
+ HZ
/10;
187 add_timer(&sclp_con_timer
);
190 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
193 static struct tty_driver
*
194 sclp_console_device(struct console
*c
, int *index
)
197 return sclp_tty_driver
;
201 * Make sure that all buffers will be flushed to the SCLP.
204 sclp_console_flush(void)
207 sclp_console_sync_queue();
211 * Resume console: If there are cached messages, emit them.
213 static void sclp_console_resume(void)
217 spin_lock_irqsave(&sclp_con_lock
, flags
);
218 sclp_con_suspended
= 0;
219 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
224 * Suspend console: Set suspend flag and flush console
226 static void sclp_console_suspend(void)
230 spin_lock_irqsave(&sclp_con_lock
, flags
);
231 sclp_con_suspended
= 1;
232 spin_unlock_irqrestore(&sclp_con_lock
, flags
);
233 sclp_console_flush();
236 static int sclp_console_notify(struct notifier_block
*self
,
237 unsigned long event
, void *data
)
239 sclp_console_flush();
243 static struct notifier_block on_panic_nb
= {
244 .notifier_call
= sclp_console_notify
,
245 .priority
= SCLP_PANIC_PRIO_CLIENT
,
248 static struct notifier_block on_reboot_nb
= {
249 .notifier_call
= sclp_console_notify
,
254 * used to register the SCLP console to the kernel and to
255 * give printk necessary information
257 static struct console sclp_console
=
259 .name
= sclp_console_name
,
260 .write
= sclp_console_write
,
261 .device
= sclp_console_device
,
262 .flags
= CON_PRINTBUFFER
,
263 .index
= 0 /* ttyS0 */
267 * This function is called for SCLP suspend and resume events.
269 void sclp_console_pm_event(enum sclp_pm_event sclp_pm_event
)
271 switch (sclp_pm_event
) {
272 case SCLP_PM_EVENT_FREEZE
:
273 sclp_console_suspend();
275 case SCLP_PM_EVENT_RESTORE
:
276 case SCLP_PM_EVENT_THAW
:
277 sclp_console_resume();
283 * called by console_init() in drivers/char/tty_io.c at boot-time.
286 sclp_console_init(void)
292 if (!CONSOLE_IS_SCLP
)
297 /* Allocate pages for output buffering */
298 INIT_LIST_HEAD(&sclp_con_pages
);
299 for (i
= 0; i
< MAX_CONSOLE_PAGES
; i
++) {
300 page
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
301 list_add_tail(page
, &sclp_con_pages
);
303 INIT_LIST_HEAD(&sclp_con_outqueue
);
304 spin_lock_init(&sclp_con_lock
);
306 init_timer(&sclp_con_timer
);
308 /* Set output format */
311 * save 4 characters for the CPU number
312 * written at start of each line by VM/CP
314 sclp_con_columns
= 76;
316 sclp_con_columns
= 80;
317 sclp_con_width_htab
= 8;
319 /* enable printk-access to this driver */
320 atomic_notifier_chain_register(&panic_notifier_list
, &on_panic_nb
);
321 register_reboot_notifier(&on_reboot_nb
);
322 register_console(&sclp_console
);
326 console_initcall(sclp_console_init
);