1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
23#include <linux/console.h>
24#include <linux/init.h>
25#include <linux/jiffies.h>
26#include <linux/nmi.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
34#include <linux/syscalls.h>
35
36#include <asm/uaccess.h>
37
38
39
40
41void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
42{
43}
44
45#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
46
47
48#define DEFAULT_MESSAGE_LOGLEVEL 4
49
50
51#define MINIMUM_CONSOLE_LOGLEVEL 1
52#define DEFAULT_CONSOLE_LOGLEVEL 7
53
54DECLARE_WAIT_QUEUE_HEAD(log_wait);
55
56int console_printk[4] = {
57 DEFAULT_CONSOLE_LOGLEVEL,
58 DEFAULT_MESSAGE_LOGLEVEL,
59 MINIMUM_CONSOLE_LOGLEVEL,
60 DEFAULT_CONSOLE_LOGLEVEL,
61};
62
63
64
65
66
67int oops_in_progress;
68EXPORT_SYMBOL(oops_in_progress);
69
70
71
72
73
74
75static DECLARE_MUTEX(console_sem);
76struct console *console_drivers;
77EXPORT_SYMBOL_GPL(console_drivers);
78
79
80
81
82
83
84
85
86
87static int console_locked, console_suspended;
88
89
90
91
92
93
94static DEFINE_SPINLOCK(logbuf_lock);
95
96#define LOG_BUF_MASK (log_buf_len-1)
97#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
98
99
100
101
102
103static unsigned log_start;
104static unsigned con_start;
105static unsigned log_end;
106
107
108
109
110struct console_cmdline
111{
112 char name[8];
113 int index;
114 char *options;
115#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
116 char *brl_options;
117#endif
118};
119
120#define MAX_CMDLINECONSOLES 8
121
122static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
123static int selected_console = -1;
124static int preferred_console = -1;
125int console_set_on_cmdline;
126EXPORT_SYMBOL(console_set_on_cmdline);
127
128
129static int console_may_schedule;
130
131#ifdef CONFIG_PRINTK
132
133static char __log_buf[__LOG_BUF_LEN];
134static char *log_buf = __log_buf;
135static int log_buf_len = __LOG_BUF_LEN;
136static unsigned logged_chars;
137
138static int __init log_buf_len_setup(char *str)
139{
140 unsigned size = memparse(str, &str);
141 unsigned long flags;
142
143 if (size)
144 size = roundup_pow_of_two(size);
145 if (size > log_buf_len) {
146 unsigned start, dest_idx, offset;
147 char *new_log_buf;
148
149 new_log_buf = alloc_bootmem(size);
150 if (!new_log_buf) {
151 printk(KERN_WARNING "log_buf_len: allocation failed\n");
152 goto out;
153 }
154
155 spin_lock_irqsave(&logbuf_lock, flags);
156 log_buf_len = size;
157 log_buf = new_log_buf;
158
159 offset = start = min(con_start, log_start);
160 dest_idx = 0;
161 while (start != log_end) {
162 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
163 start++;
164 dest_idx++;
165 }
166 log_start -= offset;
167 con_start -= offset;
168 log_end -= offset;
169 spin_unlock_irqrestore(&logbuf_lock, flags);
170
171 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
172 }
173out:
174 return 1;
175}
176
177__setup("log_buf_len=", log_buf_len_setup);
178
179#ifdef CONFIG_BOOT_PRINTK_DELAY
180
181static unsigned int boot_delay;
182static unsigned long long printk_delay_msec;
183
184static int __init boot_delay_setup(char *str)
185{
186 unsigned long lpj;
187 unsigned long long loops_per_msec;
188
189 lpj = preset_lpj ? preset_lpj : 1000000;
190 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
191
192 get_option(&str, &boot_delay);
193 if (boot_delay > 10 * 1000)
194 boot_delay = 0;
195
196 printk_delay_msec = loops_per_msec;
197 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
198 "HZ: %d, printk_delay_msec: %llu\n",
199 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
200 return 1;
201}
202__setup("boot_delay=", boot_delay_setup);
203
204static void boot_delay_msec(void)
205{
206 unsigned long long k;
207 unsigned long timeout;
208
209 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
210 return;
211
212 k = (unsigned long long)printk_delay_msec * boot_delay;
213
214 timeout = jiffies + msecs_to_jiffies(boot_delay);
215 while (k) {
216 k--;
217 cpu_relax();
218
219
220
221
222
223 if (time_after(jiffies, timeout))
224 break;
225 touch_nmi_watchdog();
226 }
227}
228#else
229static inline void boot_delay_msec(void)
230{
231}
232#endif
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249int do_syslog(int type, char __user *buf, int len)
250{
251 unsigned i, j, limit, count;
252 int do_clear = 0;
253 char c;
254 int error = 0;
255
256 error = security_syslog(type);
257 if (error)
258 return error;
259
260 switch (type) {
261 case 0:
262 break;
263 case 1:
264 break;
265 case 2:
266 error = -EINVAL;
267 if (!buf || len < 0)
268 goto out;
269 error = 0;
270 if (!len)
271 goto out;
272 if (!access_ok(VERIFY_WRITE, buf, len)) {
273 error = -EFAULT;
274 goto out;
275 }
276 error = wait_event_interruptible(log_wait,
277 (log_start - log_end));
278 if (error)
279 goto out;
280 i = 0;
281 spin_lock_irq(&logbuf_lock);
282 while (!error && (log_start != log_end) && i < len) {
283 c = LOG_BUF(log_start);
284 log_start++;
285 spin_unlock_irq(&logbuf_lock);
286 error = __put_user(c,buf);
287 buf++;
288 i++;
289 cond_resched();
290 spin_lock_irq(&logbuf_lock);
291 }
292 spin_unlock_irq(&logbuf_lock);
293 if (!error)
294 error = i;
295 break;
296 case 4:
297 do_clear = 1;
298
299 case 3:
300 error = -EINVAL;
301 if (!buf || len < 0)
302 goto out;
303 error = 0;
304 if (!len)
305 goto out;
306 if (!access_ok(VERIFY_WRITE, buf, len)) {
307 error = -EFAULT;
308 goto out;
309 }
310 count = len;
311 if (count > log_buf_len)
312 count = log_buf_len;
313 spin_lock_irq(&logbuf_lock);
314 if (count > logged_chars)
315 count = logged_chars;
316 if (do_clear)
317 logged_chars = 0;
318 limit = log_end;
319
320
321
322
323
324
325 for (i = 0; i < count && !error; i++) {
326 j = limit-1-i;
327 if (j + log_buf_len < log_end)
328 break;
329 c = LOG_BUF(j);
330 spin_unlock_irq(&logbuf_lock);
331 error = __put_user(c,&buf[count-1-i]);
332 cond_resched();
333 spin_lock_irq(&logbuf_lock);
334 }
335 spin_unlock_irq(&logbuf_lock);
336 if (error)
337 break;
338 error = i;
339 if (i != count) {
340 int offset = count-error;
341
342 for (i = 0; i < error; i++) {
343 if (__get_user(c,&buf[i+offset]) ||
344 __put_user(c,&buf[i])) {
345 error = -EFAULT;
346 break;
347 }
348 cond_resched();
349 }
350 }
351 break;
352 case 5:
353 logged_chars = 0;
354 break;
355 case 6:
356 console_loglevel = minimum_console_loglevel;
357 break;
358 case 7:
359 console_loglevel = default_console_loglevel;
360 break;
361 case 8:
362 error = -EINVAL;
363 if (len < 1 || len > 8)
364 goto out;
365 if (len < minimum_console_loglevel)
366 len = minimum_console_loglevel;
367 console_loglevel = len;
368 error = 0;
369 break;
370 case 9:
371 error = log_end - log_start;
372 break;
373 case 10:
374 error = log_buf_len;
375 break;
376 default:
377 error = -EINVAL;
378 break;
379 }
380out:
381 return error;
382}
383
384SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
385{
386 return do_syslog(type, buf, len);
387}
388
389
390
391
392static void __call_console_drivers(unsigned start, unsigned end)
393{
394 struct console *con;
395
396 for (con = console_drivers; con; con = con->next) {
397 if ((con->flags & CON_ENABLED) && con->write &&
398 (cpu_online(smp_processor_id()) ||
399 (con->flags & CON_ANYTIME)))
400 con->write(con, &LOG_BUF(start), end - start);
401 }
402}
403
404static int __read_mostly ignore_loglevel;
405
406static int __init ignore_loglevel_setup(char *str)
407{
408 ignore_loglevel = 1;
409 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
410
411 return 0;
412}
413
414early_param("ignore_loglevel", ignore_loglevel_setup);
415
416
417
418
419static void _call_console_drivers(unsigned start,
420 unsigned end, int msg_log_level)
421{
422 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
423 console_drivers && start != end) {
424 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
425
426 __call_console_drivers(start & LOG_BUF_MASK,
427 log_buf_len);
428 __call_console_drivers(0, end & LOG_BUF_MASK);
429 } else {
430 __call_console_drivers(start, end);
431 }
432 }
433}
434
435
436
437
438
439
440static void call_console_drivers(unsigned start, unsigned end)
441{
442 unsigned cur_index, start_print;
443 static int msg_level = -1;
444
445 BUG_ON(((int)(start - end)) > 0);
446
447 cur_index = start;
448 start_print = start;
449 while (cur_index != end) {
450 if (msg_level < 0 && ((end - cur_index) > 2) &&
451 LOG_BUF(cur_index + 0) == '<' &&
452 LOG_BUF(cur_index + 1) >= '0' &&
453 LOG_BUF(cur_index + 1) <= '7' &&
454 LOG_BUF(cur_index + 2) == '>') {
455 msg_level = LOG_BUF(cur_index + 1) - '0';
456 cur_index += 3;
457 start_print = cur_index;
458 }
459 while (cur_index != end) {
460 char c = LOG_BUF(cur_index);
461
462 cur_index++;
463 if (c == '\n') {
464 if (msg_level < 0) {
465
466
467
468
469
470
471 msg_level = default_message_loglevel;
472 }
473 _call_console_drivers(start_print, cur_index, msg_level);
474 msg_level = -1;
475 start_print = cur_index;
476 break;
477 }
478 }
479 }
480 _call_console_drivers(start_print, end, msg_level);
481}
482
483static void emit_log_char(char c)
484{
485 LOG_BUF(log_end) = c;
486 log_end++;
487 if (log_end - log_start > log_buf_len)
488 log_start = log_end - log_buf_len;
489 if (log_end - con_start > log_buf_len)
490 con_start = log_end - log_buf_len;
491 if (logged_chars < log_buf_len)
492 logged_chars++;
493}
494
495
496
497
498
499
500static void zap_locks(void)
501{
502 static unsigned long oops_timestamp;
503
504 if (time_after_eq(jiffies, oops_timestamp) &&
505 !time_after(jiffies, oops_timestamp + 30 * HZ))
506 return;
507
508 oops_timestamp = jiffies;
509
510
511 spin_lock_init(&logbuf_lock);
512
513 init_MUTEX(&console_sem);
514}
515
516#if defined(CONFIG_PRINTK_TIME)
517static int printk_time = 1;
518#else
519static int printk_time = 0;
520#endif
521module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
522
523
524static int have_callable_console(void)
525{
526 struct console *con;
527
528 for (con = console_drivers; con; con = con->next)
529 if (con->flags & CON_ANYTIME)
530 return 1;
531
532 return 0;
533}
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557asmlinkage int printk(const char *fmt, ...)
558{
559 va_list args;
560 int r;
561
562 va_start(args, fmt);
563 r = vprintk(fmt, args);
564 va_end(args);
565
566 return r;
567}
568
569
570static volatile unsigned int printk_cpu = UINT_MAX;
571
572
573
574
575
576
577
578
579
580static inline int can_use_console(unsigned int cpu)
581{
582 return cpu_online(cpu) || have_callable_console();
583}
584
585
586
587
588
589
590
591
592
593
594
595static int acquire_console_semaphore_for_printk(unsigned int cpu)
596{
597 int retval = 0;
598
599 if (!try_acquire_console_sem()) {
600 retval = 1;
601
602
603
604
605
606
607
608 if (!can_use_console(cpu)) {
609 console_locked = 0;
610 up(&console_sem);
611 retval = 0;
612 }
613 }
614 printk_cpu = UINT_MAX;
615 spin_unlock(&logbuf_lock);
616 return retval;
617}
618static const char recursion_bug_msg [] =
619 KERN_CRIT "BUG: recent printk recursion!\n";
620static int recursion_bug;
621static int new_text_line = 1;
622static char printk_buf[1024];
623
624asmlinkage int vprintk(const char *fmt, va_list args)
625{
626 int printed_len = 0;
627 int current_log_level = default_message_loglevel;
628 unsigned long flags;
629 int this_cpu;
630 char *p;
631
632 boot_delay_msec();
633
634 preempt_disable();
635
636 raw_local_irq_save(flags);
637 this_cpu = smp_processor_id();
638
639
640
641
642 if (unlikely(printk_cpu == this_cpu)) {
643
644
645
646
647
648
649
650 if (!oops_in_progress) {
651 recursion_bug = 1;
652 goto out_restore_irqs;
653 }
654 zap_locks();
655 }
656
657 lockdep_off();
658 spin_lock(&logbuf_lock);
659 printk_cpu = this_cpu;
660
661 if (recursion_bug) {
662 recursion_bug = 0;
663 strcpy(printk_buf, recursion_bug_msg);
664 printed_len = strlen(recursion_bug_msg);
665 }
666
667 printed_len += vscnprintf(printk_buf + printed_len,
668 sizeof(printk_buf) - printed_len, fmt, args);
669
670
671
672
673
674
675 for (p = printk_buf; *p; p++) {
676 if (new_text_line) {
677
678 if (p[0] == '<' && p[1] >= '0' && p[1] <= '7' &&
679 p[2] == '>') {
680 current_log_level = p[1] - '0';
681 p += 3;
682 printed_len -= 3;
683 }
684
685
686 emit_log_char('<');
687 emit_log_char(current_log_level + '0');
688 emit_log_char('>');
689 printed_len += 3;
690 new_text_line = 0;
691
692 if (printk_time) {
693
694 char tbuf[50], *tp;
695 unsigned tlen;
696 unsigned long long t;
697 unsigned long nanosec_rem;
698
699 t = cpu_clock(printk_cpu);
700 nanosec_rem = do_div(t, 1000000000);
701 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
702 (unsigned long) t,
703 nanosec_rem / 1000);
704
705 for (tp = tbuf; tp < tbuf + tlen; tp++)
706 emit_log_char(*tp);
707 printed_len += tlen;
708 }
709
710 if (!*p)
711 break;
712 }
713
714 emit_log_char(*p);
715 if (*p == '\n')
716 new_text_line = 1;
717 }
718
719
720
721
722
723
724
725
726
727
728
729 if (acquire_console_semaphore_for_printk(this_cpu))
730 release_console_sem();
731
732 lockdep_on();
733out_restore_irqs:
734 raw_local_irq_restore(flags);
735
736 preempt_enable();
737 return printed_len;
738}
739EXPORT_SYMBOL(printk);
740EXPORT_SYMBOL(vprintk);
741
742#else
743
744static void call_console_drivers(unsigned start, unsigned end)
745{
746}
747
748#endif
749
750static int __add_preferred_console(char *name, int idx, char *options,
751 char *brl_options)
752{
753 struct console_cmdline *c;
754 int i;
755
756
757
758
759
760 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
761 if (strcmp(console_cmdline[i].name, name) == 0 &&
762 console_cmdline[i].index == idx) {
763 if (!brl_options)
764 selected_console = i;
765 return 0;
766 }
767 if (i == MAX_CMDLINECONSOLES)
768 return -E2BIG;
769 if (!brl_options)
770 selected_console = i;
771 c = &console_cmdline[i];
772 strlcpy(c->name, name, sizeof(c->name));
773 c->options = options;
774#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
775 c->brl_options = brl_options;
776#endif
777 c->index = idx;
778 return 0;
779}
780
781
782
783static int __init console_setup(char *str)
784{
785 char buf[sizeof(console_cmdline[0].name) + 4];
786 char *s, *options, *brl_options = NULL;
787 int idx;
788
789#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
790 if (!memcmp(str, "brl,", 4)) {
791 brl_options = "";
792 str += 4;
793 } else if (!memcmp(str, "brl=", 4)) {
794 brl_options = str + 4;
795 str = strchr(brl_options, ',');
796 if (!str) {
797 printk(KERN_ERR "need port name after brl=\n");
798 return 1;
799 }
800 *(str++) = 0;
801 }
802#endif
803
804
805
806
807 if (str[0] >= '0' && str[0] <= '9') {
808 strcpy(buf, "ttyS");
809 strncpy(buf + 4, str, sizeof(buf) - 5);
810 } else {
811 strncpy(buf, str, sizeof(buf) - 1);
812 }
813 buf[sizeof(buf) - 1] = 0;
814 if ((options = strchr(str, ',')) != NULL)
815 *(options++) = 0;
816#ifdef __sparc__
817 if (!strcmp(str, "ttya"))
818 strcpy(buf, "ttyS0");
819 if (!strcmp(str, "ttyb"))
820 strcpy(buf, "ttyS1");
821#endif
822 for (s = buf; *s; s++)
823 if ((*s >= '0' && *s <= '9') || *s == ',')
824 break;
825 idx = simple_strtoul(s, NULL, 10);
826 *s = 0;
827
828 __add_preferred_console(buf, idx, options, brl_options);
829 console_set_on_cmdline = 1;
830 return 1;
831}
832__setup("console=", console_setup);
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847int add_preferred_console(char *name, int idx, char *options)
848{
849 return __add_preferred_console(name, idx, options, NULL);
850}
851
852int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
853{
854 struct console_cmdline *c;
855 int i;
856
857 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
858 if (strcmp(console_cmdline[i].name, name) == 0 &&
859 console_cmdline[i].index == idx) {
860 c = &console_cmdline[i];
861 strlcpy(c->name, name_new, sizeof(c->name));
862 c->name[sizeof(c->name) - 1] = 0;
863 c->options = options;
864 c->index = idx_new;
865 return i;
866 }
867
868 return -1;
869}
870
871int console_suspend_enabled = 1;
872EXPORT_SYMBOL(console_suspend_enabled);
873
874static int __init console_suspend_disable(char *str)
875{
876 console_suspend_enabled = 0;
877 return 1;
878}
879__setup("no_console_suspend", console_suspend_disable);
880
881
882
883
884
885
886void suspend_console(void)
887{
888 if (!console_suspend_enabled)
889 return;
890 printk("Suspending console(s) (use no_console_suspend to debug)\n");
891 acquire_console_sem();
892 console_suspended = 1;
893 up(&console_sem);
894}
895
896void resume_console(void)
897{
898 if (!console_suspend_enabled)
899 return;
900 down(&console_sem);
901 console_suspended = 0;
902 release_console_sem();
903}
904
905
906
907
908
909
910
911
912
913void acquire_console_sem(void)
914{
915 BUG_ON(in_interrupt());
916 down(&console_sem);
917 if (console_suspended)
918 return;
919 console_locked = 1;
920 console_may_schedule = 1;
921}
922EXPORT_SYMBOL(acquire_console_sem);
923
924int try_acquire_console_sem(void)
925{
926 if (down_trylock(&console_sem))
927 return -1;
928 if (console_suspended) {
929 up(&console_sem);
930 return -1;
931 }
932 console_locked = 1;
933 console_may_schedule = 0;
934 return 0;
935}
936EXPORT_SYMBOL(try_acquire_console_sem);
937
938int is_console_locked(void)
939{
940 return console_locked;
941}
942
943static DEFINE_PER_CPU(int, printk_pending);
944
945void printk_tick(void)
946{
947 if (__get_cpu_var(printk_pending)) {
948 __get_cpu_var(printk_pending) = 0;
949 wake_up_interruptible(&log_wait);
950 }
951}
952
953int printk_needs_cpu(int cpu)
954{
955 return per_cpu(printk_pending, cpu);
956}
957
958void wake_up_klogd(void)
959{
960 if (waitqueue_active(&log_wait))
961 __raw_get_cpu_var(printk_pending) = 1;
962}
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978void release_console_sem(void)
979{
980 unsigned long flags;
981 unsigned _con_start, _log_end;
982 unsigned wake_klogd = 0;
983
984 if (console_suspended) {
985 up(&console_sem);
986 return;
987 }
988
989 console_may_schedule = 0;
990
991 for ( ; ; ) {
992 spin_lock_irqsave(&logbuf_lock, flags);
993 wake_klogd |= log_start - log_end;
994 if (con_start == log_end)
995 break;
996 _con_start = con_start;
997 _log_end = log_end;
998 con_start = log_end;
999 spin_unlock(&logbuf_lock);
1000 stop_critical_timings();
1001 call_console_drivers(_con_start, _log_end);
1002 start_critical_timings();
1003 local_irq_restore(flags);
1004 }
1005 console_locked = 0;
1006 up(&console_sem);
1007 spin_unlock_irqrestore(&logbuf_lock, flags);
1008 if (wake_klogd)
1009 wake_up_klogd();
1010}
1011EXPORT_SYMBOL(release_console_sem);
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022void __sched console_conditional_schedule(void)
1023{
1024 if (console_may_schedule)
1025 cond_resched();
1026}
1027EXPORT_SYMBOL(console_conditional_schedule);
1028
1029void console_print(const char *s)
1030{
1031 printk(KERN_EMERG "%s", s);
1032}
1033EXPORT_SYMBOL(console_print);
1034
1035void console_unblank(void)
1036{
1037 struct console *c;
1038
1039
1040
1041
1042
1043 if (oops_in_progress) {
1044 if (down_trylock(&console_sem) != 0)
1045 return;
1046 } else
1047 acquire_console_sem();
1048
1049 console_locked = 1;
1050 console_may_schedule = 0;
1051 for (c = console_drivers; c != NULL; c = c->next)
1052 if ((c->flags & CON_ENABLED) && c->unblank)
1053 c->unblank();
1054 release_console_sem();
1055}
1056
1057
1058
1059
1060struct tty_driver *console_device(int *index)
1061{
1062 struct console *c;
1063 struct tty_driver *driver = NULL;
1064
1065 acquire_console_sem();
1066 for (c = console_drivers; c != NULL; c = c->next) {
1067 if (!c->device)
1068 continue;
1069 driver = c->device(c, index);
1070 if (driver)
1071 break;
1072 }
1073 release_console_sem();
1074 return driver;
1075}
1076
1077
1078
1079
1080
1081
1082void console_stop(struct console *console)
1083{
1084 acquire_console_sem();
1085 console->flags &= ~CON_ENABLED;
1086 release_console_sem();
1087}
1088EXPORT_SYMBOL(console_stop);
1089
1090void console_start(struct console *console)
1091{
1092 acquire_console_sem();
1093 console->flags |= CON_ENABLED;
1094 release_console_sem();
1095}
1096EXPORT_SYMBOL(console_start);
1097
1098
1099
1100
1101
1102
1103
1104void register_console(struct console *console)
1105{
1106 int i;
1107 unsigned long flags;
1108 struct console *bootconsole = NULL;
1109
1110 if (console_drivers) {
1111 if (console->flags & CON_BOOT)
1112 return;
1113 if (console_drivers->flags & CON_BOOT)
1114 bootconsole = console_drivers;
1115 }
1116
1117 if (preferred_console < 0 || bootconsole || !console_drivers)
1118 preferred_console = selected_console;
1119
1120 if (console->early_setup)
1121 console->early_setup();
1122
1123
1124
1125
1126
1127
1128 if (preferred_console < 0) {
1129 if (console->index < 0)
1130 console->index = 0;
1131 if (console->setup == NULL ||
1132 console->setup(console, NULL) == 0) {
1133 console->flags |= CON_ENABLED;
1134 if (console->device) {
1135 console->flags |= CON_CONSDEV;
1136 preferred_console = 0;
1137 }
1138 }
1139 }
1140
1141
1142
1143
1144
1145 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1146 i++) {
1147 if (strcmp(console_cmdline[i].name, console->name) != 0)
1148 continue;
1149 if (console->index >= 0 &&
1150 console->index != console_cmdline[i].index)
1151 continue;
1152 if (console->index < 0)
1153 console->index = console_cmdline[i].index;
1154#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1155 if (console_cmdline[i].brl_options) {
1156 console->flags |= CON_BRL;
1157 braille_register_console(console,
1158 console_cmdline[i].index,
1159 console_cmdline[i].options,
1160 console_cmdline[i].brl_options);
1161 return;
1162 }
1163#endif
1164 if (console->setup &&
1165 console->setup(console, console_cmdline[i].options) != 0)
1166 break;
1167 console->flags |= CON_ENABLED;
1168 console->index = console_cmdline[i].index;
1169 if (i == selected_console) {
1170 console->flags |= CON_CONSDEV;
1171 preferred_console = selected_console;
1172 }
1173 break;
1174 }
1175
1176 if (!(console->flags & CON_ENABLED))
1177 return;
1178
1179 if (bootconsole && (console->flags & CON_CONSDEV)) {
1180 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1181 bootconsole->name, bootconsole->index,
1182 console->name, console->index);
1183 unregister_console(bootconsole);
1184 console->flags &= ~CON_PRINTBUFFER;
1185 } else {
1186 printk(KERN_INFO "console [%s%d] enabled\n",
1187 console->name, console->index);
1188 }
1189
1190
1191
1192
1193
1194 acquire_console_sem();
1195 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1196 console->next = console_drivers;
1197 console_drivers = console;
1198 if (console->next)
1199 console->next->flags &= ~CON_CONSDEV;
1200 } else {
1201 console->next = console_drivers->next;
1202 console_drivers->next = console;
1203 }
1204 if (console->flags & CON_PRINTBUFFER) {
1205
1206
1207
1208
1209 spin_lock_irqsave(&logbuf_lock, flags);
1210 con_start = log_start;
1211 spin_unlock_irqrestore(&logbuf_lock, flags);
1212 }
1213 release_console_sem();
1214}
1215EXPORT_SYMBOL(register_console);
1216
1217int unregister_console(struct console *console)
1218{
1219 struct console *a, *b;
1220 int res = 1;
1221
1222#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1223 if (console->flags & CON_BRL)
1224 return braille_unregister_console(console);
1225#endif
1226
1227 acquire_console_sem();
1228 if (console_drivers == console) {
1229 console_drivers=console->next;
1230 res = 0;
1231 } else if (console_drivers) {
1232 for (a=console_drivers->next, b=console_drivers ;
1233 a; b=a, a=b->next) {
1234 if (a == console) {
1235 b->next = a->next;
1236 res = 0;
1237 break;
1238 }
1239 }
1240 }
1241
1242
1243
1244
1245
1246 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1247 console_drivers->flags |= CON_CONSDEV;
1248
1249 release_console_sem();
1250 return res;
1251}
1252EXPORT_SYMBOL(unregister_console);
1253
1254static int __init disable_boot_consoles(void)
1255{
1256 if (console_drivers != NULL) {
1257 if (console_drivers->flags & CON_BOOT) {
1258 printk(KERN_INFO "turn off boot console %s%d\n",
1259 console_drivers->name, console_drivers->index);
1260 return unregister_console(console_drivers);
1261 }
1262 }
1263 return 0;
1264}
1265late_initcall(disable_boot_consoles);
1266
1267#if defined CONFIG_PRINTK
1268
1269
1270
1271
1272
1273
1274
1275DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1276
1277int printk_ratelimit(void)
1278{
1279 return __ratelimit(&printk_ratelimit_state);
1280}
1281EXPORT_SYMBOL(printk_ratelimit);
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1293 unsigned int interval_msecs)
1294{
1295 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1296 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1297 return true;
1298 }
1299 return false;
1300}
1301EXPORT_SYMBOL(printk_timed_ratelimit);
1302#endif
1303