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>
44 #define DBG(args...) printk(args)
46 #define DBG(args...) do { } while(0)
49 static LIST_HEAD(wf_controls);
50 static LIST_HEAD(wf_sensors);
51 static DECLARE_MUTEX(wf_lock);
52 static struct notifier_block *wf_client_list;
53 static int wf_client_count;
54 static unsigned int wf_overtemp;
55 static unsigned int wf_overtemp_counter;
56 struct task_struct *wf_thread;
59 * Utilities & tick thread
62 static inline void wf_notify(int event, void *param)
64 notifier_call_chain(&wf_client_list, event, param);
67 int wf_critical_overtemp(void)
69 static char * critical_overtemp_path = "/sbin/critical_overtemp";
70 char *argv[] = { critical_overtemp_path, NULL };
71 static char *envp[] = { "HOME=/",
73 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
76 return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
78 EXPORT_SYMBOL_GPL(wf_critical_overtemp);
80 static int wf_thread_func(void *data)
82 unsigned long next, delay;
86 DBG("wf: thread started\n");
88 while(!kthread_should_stop()) {
91 if (time_after_eq(jiffies, next)) {
92 wf_notify(WF_EVENT_TICK, NULL);
94 wf_overtemp_counter++;
95 /* 10 seconds overtemp, notify userland */
96 if (wf_overtemp_counter > 10)
97 wf_critical_overtemp();
98 /* 30 seconds, shutdown */
99 if (wf_overtemp_counter > 30) {
100 printk(KERN_ERR "windfarm: Overtemp "
102 " seconds, shutting down\n");
109 delay = next - jiffies;
111 schedule_timeout_interruptible(delay);
113 /* there should be no signal, but oh well */
114 if (signal_pending(current)) {
115 printk(KERN_WARNING "windfarm: thread got sigl !\n");
120 DBG("wf: thread stopped\n");
125 static void wf_start_thread(void)
127 wf_thread = kthread_run(wf_thread_func, NULL, "kwindfarm");
128 if (IS_ERR(wf_thread)) {
129 printk(KERN_ERR "windfarm: failed to create thread,err %ld\n",
136 static void wf_stop_thread(void)
139 kthread_stop(wf_thread);
147 static void wf_control_release(struct kref *kref)
149 struct wf_control *ct = container_of(kref, struct wf_control, ref);
151 DBG("wf: Deleting control %s\n", ct->name);
153 if (ct->ops && ct->ops->release)
154 ct->ops->release(ct);
159 int wf_register_control(struct wf_control *new_ct)
161 struct wf_control *ct;
164 list_for_each_entry(ct, &wf_controls, link) {
165 if (!strcmp(ct->name, new_ct->name)) {
166 printk(KERN_WARNING "windfarm: trying to register"
167 " duplicate control %s\n", ct->name);
172 kref_init(&new_ct->ref);
173 list_add(&new_ct->link, &wf_controls);
175 DBG("wf: Registered control %s\n", new_ct->name);
177 wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
182 EXPORT_SYMBOL_GPL(wf_register_control);
184 void wf_unregister_control(struct wf_control *ct)
190 DBG("wf: Unregistered control %s\n", ct->name);
192 kref_put(&ct->ref, wf_control_release);
194 EXPORT_SYMBOL_GPL(wf_unregister_control);
196 struct wf_control * wf_find_control(const char *name)
198 struct wf_control *ct;
201 list_for_each_entry(ct, &wf_controls, link) {
202 if (!strcmp(ct->name, name)) {
203 if (wf_get_control(ct))
212 EXPORT_SYMBOL_GPL(wf_find_control);
214 int wf_get_control(struct wf_control *ct)
216 if (!try_module_get(ct->ops->owner))
221 EXPORT_SYMBOL_GPL(wf_get_control);
223 void wf_put_control(struct wf_control *ct)
225 struct module *mod = ct->ops->owner;
226 kref_put(&ct->ref, wf_control_release);
229 EXPORT_SYMBOL_GPL(wf_put_control);
237 static void wf_sensor_release(struct kref *kref)
239 struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
241 DBG("wf: Deleting sensor %s\n", sr->name);
243 if (sr->ops && sr->ops->release)
244 sr->ops->release(sr);
249 int wf_register_sensor(struct wf_sensor *new_sr)
251 struct wf_sensor *sr;
254 list_for_each_entry(sr, &wf_sensors, link) {
255 if (!strcmp(sr->name, new_sr->name)) {
256 printk(KERN_WARNING "windfarm: trying to register"
257 " duplicate sensor %s\n", sr->name);
262 kref_init(&new_sr->ref);
263 list_add(&new_sr->link, &wf_sensors);
265 DBG("wf: Registered sensor %s\n", new_sr->name);
267 wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
272 EXPORT_SYMBOL_GPL(wf_register_sensor);
274 void wf_unregister_sensor(struct wf_sensor *sr)
280 DBG("wf: Unregistered sensor %s\n", sr->name);
284 EXPORT_SYMBOL_GPL(wf_unregister_sensor);
286 struct wf_sensor * wf_find_sensor(const char *name)
288 struct wf_sensor *sr;
291 list_for_each_entry(sr, &wf_sensors, link) {
292 if (!strcmp(sr->name, name)) {
293 if (wf_get_sensor(sr))
302 EXPORT_SYMBOL_GPL(wf_find_sensor);
304 int wf_get_sensor(struct wf_sensor *sr)
306 if (!try_module_get(sr->ops->owner))
311 EXPORT_SYMBOL_GPL(wf_get_sensor);
313 void wf_put_sensor(struct wf_sensor *sr)
315 struct module *mod = sr->ops->owner;
316 kref_put(&sr->ref, wf_sensor_release);
319 EXPORT_SYMBOL_GPL(wf_put_sensor);
323 * Client & notification
326 int wf_register_client(struct notifier_block *nb)
329 struct wf_control *ct;
330 struct wf_sensor *sr;
333 rc = notifier_chain_register(&wf_client_list, nb);
337 list_for_each_entry(ct, &wf_controls, link)
338 wf_notify(WF_EVENT_NEW_CONTROL, ct);
339 list_for_each_entry(sr, &wf_sensors, link)
340 wf_notify(WF_EVENT_NEW_SENSOR, sr);
341 if (wf_client_count == 1)
347 EXPORT_SYMBOL_GPL(wf_register_client);
349 int wf_unregister_client(struct notifier_block *nb)
352 notifier_chain_unregister(&wf_client_list, nb);
354 if (wf_client_count == 0)
360 EXPORT_SYMBOL_GPL(wf_unregister_client);
362 void wf_set_overtemp(void)
366 if (wf_overtemp == 1) {
367 printk(KERN_WARNING "windfarm: Overtemp condition detected !\n");
368 wf_overtemp_counter = 0;
369 wf_notify(WF_EVENT_OVERTEMP, NULL);
373 EXPORT_SYMBOL_GPL(wf_set_overtemp);
375 void wf_clear_overtemp(void)
378 WARN_ON(wf_overtemp == 0);
379 if (wf_overtemp == 0) {
384 if (wf_overtemp == 0) {
385 printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
386 wf_notify(WF_EVENT_NORMALTEMP, NULL);
390 EXPORT_SYMBOL_GPL(wf_clear_overtemp);
392 int wf_is_overtemp(void)
394 return (wf_overtemp != 0);
396 EXPORT_SYMBOL_GPL(wf_is_overtemp);
398 static struct platform_device wf_platform_device = {
402 static int __init windfarm_core_init(void)
404 DBG("wf: core loaded\n");
406 platform_device_register(&wf_platform_device);
410 static void __exit windfarm_core_exit(void)
412 BUG_ON(wf_client_count != 0);
414 DBG("wf: core unloaded\n");
416 platform_device_unregister(&wf_platform_device);
420 module_init(windfarm_core_init);
421 module_exit(windfarm_core_exit);
423 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
424 MODULE_DESCRIPTION("Core component of PowerMac thermal control");
425 MODULE_LICENSE("GPL");