nfsd: make laundromat network namespace aware
[linux-2.6/libata-dev.git] / drivers / leds / ledtrig-cpu.c
blobb312056da14d3c5f9ef602a455d8bbc80fc8c125
1 /*
2 * ledtrig-cpu.c - LED trigger based on CPU activity
4 * This LED trigger will be registered for each possible CPU and named as
5 * cpu0, cpu1, cpu2, cpu3, etc.
7 * It can be bound to any LED just like other triggers using either a
8 * board file or via sysfs interface.
10 * An API named ledtrig_cpu is exported for any user, who want to add CPU
11 * activity indication in their code
13 * Copyright 2011 Linus Walleij <linus.walleij@linaro.org>
14 * Copyright 2011 - 2012 Bryan Wu <bryan.wu@canonical.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/percpu.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/rwsem.h>
29 #include "leds.h"
31 #define MAX_NAME_LEN 8
33 struct led_trigger_cpu {
34 char name[MAX_NAME_LEN];
35 struct led_trigger *_trig;
36 struct mutex lock;
37 int lock_is_inited;
40 static DEFINE_PER_CPU(struct led_trigger_cpu, cpu_trig);
42 /**
43 * ledtrig_cpu - emit a CPU event as a trigger
44 * @evt: CPU event to be emitted
46 * Emit a CPU event on a CPU core, which will trigger a
47 * binded LED to turn on or turn off.
49 void ledtrig_cpu(enum cpu_led_event ledevt)
51 struct led_trigger_cpu *trig = &__get_cpu_var(cpu_trig);
53 /* mutex lock should be initialized before calling mutex_call() */
54 if (!trig->lock_is_inited)
55 return;
57 mutex_lock(&trig->lock);
59 /* Locate the correct CPU LED */
60 switch (ledevt) {
61 case CPU_LED_IDLE_END:
62 case CPU_LED_START:
63 /* Will turn the LED on, max brightness */
64 led_trigger_event(trig->_trig, LED_FULL);
65 break;
67 case CPU_LED_IDLE_START:
68 case CPU_LED_STOP:
69 case CPU_LED_HALTED:
70 /* Will turn the LED off */
71 led_trigger_event(trig->_trig, LED_OFF);
72 break;
74 default:
75 /* Will leave the LED as it is */
76 break;
79 mutex_unlock(&trig->lock);
81 EXPORT_SYMBOL(ledtrig_cpu);
83 static int ledtrig_cpu_syscore_suspend(void)
85 ledtrig_cpu(CPU_LED_STOP);
86 return 0;
89 static void ledtrig_cpu_syscore_resume(void)
91 ledtrig_cpu(CPU_LED_START);
94 static void ledtrig_cpu_syscore_shutdown(void)
96 ledtrig_cpu(CPU_LED_HALTED);
99 static struct syscore_ops ledtrig_cpu_syscore_ops = {
100 .shutdown = ledtrig_cpu_syscore_shutdown,
101 .suspend = ledtrig_cpu_syscore_suspend,
102 .resume = ledtrig_cpu_syscore_resume,
105 static int __init ledtrig_cpu_init(void)
107 int cpu;
109 /* Supports up to 9999 cpu cores */
110 BUILD_BUG_ON(CONFIG_NR_CPUS > 9999);
113 * Registering CPU led trigger for each CPU core here
114 * ignores CPU hotplug, but after this CPU hotplug works
115 * fine with this trigger.
117 for_each_possible_cpu(cpu) {
118 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
120 mutex_init(&trig->lock);
122 snprintf(trig->name, MAX_NAME_LEN, "cpu%d", cpu);
124 mutex_lock(&trig->lock);
125 led_trigger_register_simple(trig->name, &trig->_trig);
126 trig->lock_is_inited = 1;
127 mutex_unlock(&trig->lock);
130 register_syscore_ops(&ledtrig_cpu_syscore_ops);
132 pr_info("ledtrig-cpu: registered to indicate activity on CPUs\n");
134 return 0;
136 module_init(ledtrig_cpu_init);
138 static void __exit ledtrig_cpu_exit(void)
140 int cpu;
142 for_each_possible_cpu(cpu) {
143 struct led_trigger_cpu *trig = &per_cpu(cpu_trig, cpu);
145 mutex_lock(&trig->lock);
147 led_trigger_unregister_simple(trig->_trig);
148 trig->_trig = NULL;
149 memset(trig->name, 0, MAX_NAME_LEN);
150 trig->lock_is_inited = 0;
152 mutex_unlock(&trig->lock);
153 mutex_destroy(&trig->lock);
156 unregister_syscore_ops(&ledtrig_cpu_syscore_ops);
158 module_exit(ledtrig_cpu_exit);
160 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
161 MODULE_AUTHOR("Bryan Wu <bryan.wu@canonical.com>");
162 MODULE_DESCRIPTION("CPU LED trigger");
163 MODULE_LICENSE("GPL");