Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / drivers / macintosh / windfarm_core.c
1 /*
2  * Windfarm PowerMac thermal control. Core
3  *
4  * (c) Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
5  *                    <benh@kernel.crashing.org>
6  *
7  * Released under the term of the GNU GPL v2.
8  *
9  * This core code tracks the list of sensors & controls, register
10  * clients, and holds the kernel thread used for control.
11  *
12  * TODO:
13  *
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
18  *
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
23  */
24
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>
37
38 #include "windfarm.h"
39
40 #define VERSION "0.2"
41
42 #undef DEBUG
43
44 #ifdef DEBUG
45 #define DBG(args...)    printk(args)
46 #else
47 #define DBG(args...)    do { } while(0)
48 #endif
49
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;
58
59 /*
60  * Utilities & tick thread
61  */
62
63 static inline void wf_notify(int event, void *param)
64 {
65         notifier_call_chain(&wf_client_list, event, param);
66 }
67
68 int wf_critical_overtemp(void)
69 {
70         static char * critical_overtemp_path = "/sbin/critical_overtemp";
71         char *argv[] = { critical_overtemp_path, NULL };
72         static char *envp[] = { "HOME=/",
73                                 "TERM=linux",
74                                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
75                                 NULL };
76
77         return call_usermodehelper(critical_overtemp_path, argv, envp, 0);
78 }
79 EXPORT_SYMBOL_GPL(wf_critical_overtemp);
80
81 static int wf_thread_func(void *data)
82 {
83         unsigned long next, delay;
84
85         next = jiffies;
86
87         DBG("wf: thread started\n");
88
89         while(!kthread_should_stop()) {
90                 try_to_freeze();
91
92                 if (time_after_eq(jiffies, next)) {
93                         wf_notify(WF_EVENT_TICK, NULL);
94                         if (wf_overtemp) {
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 "
102                                                "for more than 30"
103                                                " seconds, shutting down\n");
104                                         machine_power_off();
105                                 }
106                         }
107                         next += HZ;
108                 }
109
110                 delay = next - jiffies;
111                 if (delay <= HZ)
112                         schedule_timeout_interruptible(delay);
113
114                 /* there should be no signal, but oh well */
115                 if (signal_pending(current)) {
116                         printk(KERN_WARNING "windfarm: thread got sigl !\n");
117                         break;
118                 }
119         }
120
121         DBG("wf: thread stopped\n");
122
123         return 0;
124 }
125
126 static void wf_start_thread(void)
127 {
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",
131                        PTR_ERR(wf_thread));
132                 wf_thread = NULL;
133         }
134 }
135
136
137 static void wf_stop_thread(void)
138 {
139         if (wf_thread)
140                 kthread_stop(wf_thread);
141         wf_thread = NULL;
142 }
143
144 /*
145  * Controls
146  */
147
148 static void wf_control_release(struct kref *kref)
149 {
150         struct wf_control *ct = container_of(kref, struct wf_control, ref);
151
152         DBG("wf: Deleting control %s\n", ct->name);
153
154         if (ct->ops && ct->ops->release)
155                 ct->ops->release(ct);
156         else
157                 kfree(ct);
158 }
159
160 int wf_register_control(struct wf_control *new_ct)
161 {
162         struct wf_control *ct;
163
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);
170                         return -EEXIST;
171                 }
172         }
173         kref_init(&new_ct->ref);
174         list_add(&new_ct->link, &wf_controls);
175
176         DBG("wf: Registered control %s\n", new_ct->name);
177
178         wf_notify(WF_EVENT_NEW_CONTROL, new_ct);
179         mutex_unlock(&wf_lock);
180
181         return 0;
182 }
183 EXPORT_SYMBOL_GPL(wf_register_control);
184
185 void wf_unregister_control(struct wf_control *ct)
186 {
187         mutex_lock(&wf_lock);
188         list_del(&ct->link);
189         mutex_unlock(&wf_lock);
190
191         DBG("wf: Unregistered control %s\n", ct->name);
192
193         kref_put(&ct->ref, wf_control_release);
194 }
195 EXPORT_SYMBOL_GPL(wf_unregister_control);
196
197 struct wf_control * wf_find_control(const char *name)
198 {
199         struct wf_control *ct;
200
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))
205                                 ct = NULL;
206                         mutex_unlock(&wf_lock);
207                         return ct;
208                 }
209         }
210         mutex_unlock(&wf_lock);
211         return NULL;
212 }
213 EXPORT_SYMBOL_GPL(wf_find_control);
214
215 int wf_get_control(struct wf_control *ct)
216 {
217         if (!try_module_get(ct->ops->owner))
218                 return -ENODEV;
219         kref_get(&ct->ref);
220         return 0;
221 }
222 EXPORT_SYMBOL_GPL(wf_get_control);
223
224 void wf_put_control(struct wf_control *ct)
225 {
226         struct module *mod = ct->ops->owner;
227         kref_put(&ct->ref, wf_control_release);
228         module_put(mod);
229 }
230 EXPORT_SYMBOL_GPL(wf_put_control);
231
232
233 /*
234  * Sensors
235  */
236
237
238 static void wf_sensor_release(struct kref *kref)
239 {
240         struct wf_sensor *sr = container_of(kref, struct wf_sensor, ref);
241
242         DBG("wf: Deleting sensor %s\n", sr->name);
243
244         if (sr->ops && sr->ops->release)
245                 sr->ops->release(sr);
246         else
247                 kfree(sr);
248 }
249
250 int wf_register_sensor(struct wf_sensor *new_sr)
251 {
252         struct wf_sensor *sr;
253
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);
260                         return -EEXIST;
261                 }
262         }
263         kref_init(&new_sr->ref);
264         list_add(&new_sr->link, &wf_sensors);
265
266         DBG("wf: Registered sensor %s\n", new_sr->name);
267
268         wf_notify(WF_EVENT_NEW_SENSOR, new_sr);
269         mutex_unlock(&wf_lock);
270
271         return 0;
272 }
273 EXPORT_SYMBOL_GPL(wf_register_sensor);
274
275 void wf_unregister_sensor(struct wf_sensor *sr)
276 {
277         mutex_lock(&wf_lock);
278         list_del(&sr->link);
279         mutex_unlock(&wf_lock);
280
281         DBG("wf: Unregistered sensor %s\n", sr->name);
282
283         wf_put_sensor(sr);
284 }
285 EXPORT_SYMBOL_GPL(wf_unregister_sensor);
286
287 struct wf_sensor * wf_find_sensor(const char *name)
288 {
289         struct wf_sensor *sr;
290
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))
295                                 sr = NULL;
296                         mutex_unlock(&wf_lock);
297                         return sr;
298                 }
299         }
300         mutex_unlock(&wf_lock);
301         return NULL;
302 }
303 EXPORT_SYMBOL_GPL(wf_find_sensor);
304
305 int wf_get_sensor(struct wf_sensor *sr)
306 {
307         if (!try_module_get(sr->ops->owner))
308                 return -ENODEV;
309         kref_get(&sr->ref);
310         return 0;
311 }
312 EXPORT_SYMBOL_GPL(wf_get_sensor);
313
314 void wf_put_sensor(struct wf_sensor *sr)
315 {
316         struct module *mod = sr->ops->owner;
317         kref_put(&sr->ref, wf_sensor_release);
318         module_put(mod);
319 }
320 EXPORT_SYMBOL_GPL(wf_put_sensor);
321
322
323 /*
324  * Client & notification
325  */
326
327 int wf_register_client(struct notifier_block *nb)
328 {
329         int rc;
330         struct wf_control *ct;
331         struct wf_sensor *sr;
332
333         mutex_lock(&wf_lock);
334         rc = notifier_chain_register(&wf_client_list, nb);
335         if (rc != 0)
336                 goto bail;
337         wf_client_count++;
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)
343                 wf_start_thread();
344  bail:
345         mutex_unlock(&wf_lock);
346         return rc;
347 }
348 EXPORT_SYMBOL_GPL(wf_register_client);
349
350 int wf_unregister_client(struct notifier_block *nb)
351 {
352         mutex_lock(&wf_lock);
353         notifier_chain_unregister(&wf_client_list, nb);
354         wf_client_count++;
355         if (wf_client_count == 0)
356                 wf_stop_thread();
357         mutex_unlock(&wf_lock);
358
359         return 0;
360 }
361 EXPORT_SYMBOL_GPL(wf_unregister_client);
362
363 void wf_set_overtemp(void)
364 {
365         mutex_lock(&wf_lock);
366         wf_overtemp++;
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);
371         }
372         mutex_unlock(&wf_lock);
373 }
374 EXPORT_SYMBOL_GPL(wf_set_overtemp);
375
376 void wf_clear_overtemp(void)
377 {
378         mutex_lock(&wf_lock);
379         WARN_ON(wf_overtemp == 0);
380         if (wf_overtemp == 0) {
381                 mutex_unlock(&wf_lock);
382                 return;
383         }
384         wf_overtemp--;
385         if (wf_overtemp == 0) {
386                 printk(KERN_WARNING "windfarm: Overtemp condition cleared !\n");
387                 wf_notify(WF_EVENT_NORMALTEMP, NULL);
388         }
389         mutex_unlock(&wf_lock);
390 }
391 EXPORT_SYMBOL_GPL(wf_clear_overtemp);
392
393 int wf_is_overtemp(void)
394 {
395         return (wf_overtemp != 0);
396 }
397 EXPORT_SYMBOL_GPL(wf_is_overtemp);
398
399 static struct platform_device wf_platform_device = {
400         .name   = "windfarm",
401 };
402
403 static int __init windfarm_core_init(void)
404 {
405         DBG("wf: core loaded\n");
406
407         platform_device_register(&wf_platform_device);
408         return 0;
409 }
410
411 static void __exit windfarm_core_exit(void)
412 {
413         BUG_ON(wf_client_count != 0);
414
415         DBG("wf: core unloaded\n");
416
417         platform_device_unregister(&wf_platform_device);
418 }
419
420
421 module_init(windfarm_core_init);
422 module_exit(windfarm_core_exit);
423
424 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
425 MODULE_DESCRIPTION("Core component of PowerMac thermal control");
426 MODULE_LICENSE("GPL");
427