2 * Windfarm PowerMac thermal control. Core
4 * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * Released under the term of the GNU GPL v2.
9 * This core code tracks the list of sensors & controls, register
10 * clients, and holds the kernel thread used for control.
14 * Add some information about sensor/control type and data format to
15 * sensors/controls, and have the sysfs attribute stuff be moved
16 * generically here instead of hard coded in the platform specific
17 * driver as it us currently
19 * This however requires solving some annoying lifetime issues with
20 * sysfs which doesn't seem to have lifetime rules for struct attribute,
21 * I may have to create full features kobjects for every sensor/control
22 * instead which is a bit of an overkill imho
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/spinlock.h>
30 #include <linux/smp_lock.h>
31 #include <linux/kthread.h>
32 #include <linux/jiffies.h>
33 #include <linux/reboot.h>
34 #include <linux/device.h>
35 #include <linux/platform_device.h>
36 #include <linux/mutex.h>
45 #define DBG(args...) printk(args)
47 #define DBG(args...) do { } while(0)
50 static LIST_HEAD(wf_controls);
51 static LIST_HEAD(wf_sensors);
52 static DEFINE_MUTEX(wf_lock);
53 static struct notifier_block *wf_client_list;
54 static int wf_client_count;
55 static unsigned int wf_overtemp;
56 static unsigned int wf_overtemp_counter;
57 struct task_struct *wf_thread;
60 * Utilities & tick thread
63 static inline void wf_notify(int event, void *param)
65 notifier_call_chain(&wf_client_list, event, param);
68 int wf_critical_overtemp(void)
70 static char * critical_overtemp_path = "/sbin/critical_overtemp";
71 char *argv[] = { critical_overtemp_path, NULL };
72 static char *envp[] = { "HOME=/",
74 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
77 return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
79 EXPORT_SYMBOL_GPL(wf_critical_overtemp);
81 static int wf_thread_func(void *data)
83 unsigned long next, delay;
87 DBG("wf: thread started\n");
89 while(!kthread_should_stop()) {
92 if (time_after_eq(jiffies, next)) {
93 wf_notify(WF_EVENT_TICK, NULL);
95 wf_overtemp_counter++;
96 /* 10 seconds overtemp, notify userland */
97 if (wf_overtemp_counter > 10)
98 wf_critical_overtemp();
99 /* 30 seconds, shutdown */
100 if (wf_overtemp_counter > 30) {
101 printk(KERN_ERR "windfarm: Overtemp "
103 " seconds, shutting down\n");
110 delay = next - jiffies;
112 schedule_timeout_interruptible(delay);
114 /* there should be no signal, but oh well */
115 if (signal_pending(current)) {
116 printk(KERN_WARNING "windfarm: thread got sigl !\n");
121 DBG("wf: thread stopped\n");
126 static void wf_start_thread(void)
128 wf_thread = kthread_run(wf_thread_func, NULL, "kwindfarm");
129 if (IS_ERR(wf_thread)) {
130 printk(KERN_ERR "windfarm: failed to create thread,err %ld\n",
137 static void wf_stop_thread(void)
140 kthread_stop(wf_thread);
148 static void wf_control_release(struct kref *kref)
150 struct wf_control *ct = container_of(kref, struct wf_control, ref);
152 DBG("wf: Deleting control %s\n", ct->name);
154 if (ct->ops && ct->ops->release)
155 ct->ops->release(ct);
160 int wf_register_control(struct wf_control *new_ct)
162 struct wf_control *ct;
164 mutex_lock(&wf_lock);
165 list_for_each_entry(ct, &wf_controls, link) {
166 if (!strcmp(ct->name, new_ct->name)) {
167 printk(KERN_WARNING "windfarm: trying to register"
168 " duplicate control %s\n", ct->name);
169 mutex_unlock(&wf_lock);
173 kref_init(&new_ct->ref);
174 list_add(&new_ct->link, &wf_controls);
176 DBG("wf: Registered control %s\n", new_ct->name);
178 wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
179 mutex_unlock(&wf_lock);
183 EXPORT_SYMBOL_GPL(wf_register_control);
185 void wf_unregister_control(struct wf_control *ct)
187 mutex_lock(&wf_lock);
189 mutex_unlock(&wf_lock);
191 DBG("wf: Unregistered control %s\n", ct->name);
193 kref_put(&ct->ref, wf_control_release);
195 EXPORT_SYMBOL_GPL(wf_unregister_control);
197 struct wf_control * wf_find_control(const char *name)
199 struct wf_control *ct;
201 mutex_lock(&wf_lock);
202 list_for_each_entry(ct, &wf_controls, link) {
203 if (!strcmp(ct->name, name)) {
204 if (wf_get_control(ct))
206 mutex_unlock(&wf_lock);
210 mutex_unlock(&wf_lock);
213 EXPORT_SYMBOL_GPL(wf_find_control);
215 int wf_get_control(struct wf_control *ct)
217 if (!try_module_get(ct->ops->owner))
222 EXPORT_SYMBOL_GPL(wf_get_control);
224 void wf_put_control(struct wf_control *ct)
226 struct module *mod = ct->ops->owner;
227 kref_put(&ct->ref, wf_control_release);
230 EXPORT_SYMBOL_GPL(wf_put_control);
238 static void wf_sensor_release(struct kref *kref)
240 struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
242 DBG("wf: Deleting sensor %s\n", sr->name);
244 if (sr->ops && sr->ops->release)
245 sr->ops->release(sr);
250 int wf_register_sensor(struct wf_sensor *new_sr)
252 struct wf_sensor *sr;
254 mutex_lock(&wf_lock);
255 list_for_each_entry(sr, &wf_sensors, link) {
256 if (!strcmp(sr->name, new_sr->name)) {
257 printk(KERN_WARNING "windfarm: trying to register"
258 " duplicate sensor %s\n", sr->name);
259 mutex_unlock(&wf_lock);
263 kref_init(&new_sr->ref);
264 list_add(&new_sr->link, &wf_sensors);
266 DBG("wf: Registered sensor %s\n", new_sr->name);
268 wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
269 mutex_unlock(&wf_lock);
273 EXPORT_SYMBOL_GPL(wf_register_sensor);
275 void wf_unregister_sensor(struct wf_sensor *sr)
277 mutex_lock(&wf_lock);
279 mutex_unlock(&wf_lock);
281 DBG("wf: Unregistered sensor %s\n", sr->name);
285 EXPORT_SYMBOL_GPL(wf_unregister_sensor);
287 struct wf_sensor * wf_find_sensor(const char *name)
289 struct wf_sensor *sr;
291 mutex_lock(&wf_lock);
292 list_for_each_entry(sr, &wf_sensors, link) {
293 if (!strcmp(sr->name, name)) {
294 if (wf_get_sensor(sr))
296 mutex_unlock(&wf_lock);
300 mutex_unlock(&wf_lock);
303 EXPORT_SYMBOL_GPL(wf_find_sensor);
305 int wf_get_sensor(struct wf_sensor *sr)
307 if (!try_module_get(sr->ops->owner))
312 EXPORT_SYMBOL_GPL(wf_get_sensor);
314 void wf_put_sensor(struct wf_sensor *sr)
316 struct module *mod = sr->ops->owner;
317 kref_put(&sr->ref, wf_sensor_release);
320 EXPORT_SYMBOL_GPL(wf_put_sensor);
324 * Client & notification
327 int wf_register_client(struct notifier_block *nb)
330 struct wf_control *ct;
331 struct wf_sensor *sr;
333 mutex_lock(&wf_lock);
334 rc = notifier_chain_register(&wf_client_list, nb);
338 list_for_each_entry(ct, &wf_controls, link)
339 wf_notify(WF_EVENT_NEW_CONTROL, ct);
340 list_for_each_entry(sr, &wf_sensors, link)
341 wf_notify(WF_EVENT_NEW_SENSOR, sr);
342 if (wf_client_count == 1)
345 mutex_unlock(&wf_lock);
348 EXPORT_SYMBOL_GPL(wf_register_client);
350 int wf_unregister_client(struct notifier_block *nb)
352 mutex_lock(&wf_lock);
353 notifier_chain_unregister(&wf_client_list, nb);
355 if (wf_client_count == 0)
357 mutex_unlock(&wf_lock);
361 EXPORT_SYMBOL_GPL(wf_unregister_client);
363 void wf_set_overtemp(void)
365 mutex_lock(&wf_lock);
367 if (wf_overtemp == 1) {
368 printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
369 wf_overtemp_counter = 0;
370 wf_notify(WF_EVENT_OVERTEMP, NULL);
372 mutex_unlock(&wf_lock);
374 EXPORT_SYMBOL_GPL(wf_set_overtemp);
376 void wf_clear_overtemp(void)
378 mutex_lock(&wf_lock);
379 WARN_ON(wf_overtemp == 0);
380 if (wf_overtemp == 0) {
381 mutex_unlock(&wf_lock);
385 if (wf_overtemp == 0) {
386 printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
387 wf_notify(WF_EVENT_NORMALTEMP, NULL);
389 mutex_unlock(&wf_lock);
391 EXPORT_SYMBOL_GPL(wf_clear_overtemp);
393 int wf_is_overtemp(void)
395 return (wf_overtemp != 0);
397 EXPORT_SYMBOL_GPL(wf_is_overtemp);
399 static struct platform_device wf_platform_device = {
403 static int __init windfarm_core_init(void)
405 DBG("wf: core loaded\n");
407 platform_device_register(&wf_platform_device);
411 static void __exit windfarm_core_exit(void)
413 BUG_ON(wf_client_count != 0);
415 DBG("wf: core unloaded\n");
417 platform_device_unregister(&wf_platform_device);
421 module_init(windfarm_core_init);
422 module_exit(windfarm_core_exit);
424 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
425 MODULE_DESCRIPTION("Core component of PowerMac thermal control");
426 MODULE_LICENSE("GPL");