NBD: allow hung network I/O to be cancelled
[linux-2.6] / drivers / power / power_supply_leds.c
1 /*
2  *  LEDs triggers for power supply class
3  *
4  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
5  *  Copyright © 2004  Szabolcs Gyurko
6  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
7  *
8  *  Modified: 2004, Oct     Szabolcs Gyurko
9  *
10  *  You may use this code as per GPL version 2
11  */
12
13 #include <linux/power_supply.h>
14
15 /* Battery specific LEDs triggers. */
16
17 static void power_supply_update_bat_leds(struct power_supply *psy)
18 {
19         union power_supply_propval status;
20
21         if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
22                 return;
23
24         dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, status.intval);
25
26         switch (status.intval) {
27         case POWER_SUPPLY_STATUS_FULL:
28                 led_trigger_event(psy->charging_full_trig, LED_FULL);
29                 led_trigger_event(psy->charging_trig, LED_OFF);
30                 led_trigger_event(psy->full_trig, LED_FULL);
31                 break;
32         case POWER_SUPPLY_STATUS_CHARGING:
33                 led_trigger_event(psy->charging_full_trig, LED_FULL);
34                 led_trigger_event(psy->charging_trig, LED_FULL);
35                 led_trigger_event(psy->full_trig, LED_OFF);
36                 break;
37         default:
38                 led_trigger_event(psy->charging_full_trig, LED_OFF);
39                 led_trigger_event(psy->charging_trig, LED_OFF);
40                 led_trigger_event(psy->full_trig, LED_OFF);
41                 break;
42         }
43 }
44
45 static int power_supply_create_bat_triggers(struct power_supply *psy)
46 {
47         int rc = 0;
48
49         psy->charging_full_trig_name = kmalloc(strlen(psy->name) +
50                                   sizeof("-charging-or-full"), GFP_KERNEL);
51         if (!psy->charging_full_trig_name)
52                 goto charging_full_failed;
53
54         psy->charging_trig_name = kmalloc(strlen(psy->name) +
55                                           sizeof("-charging"), GFP_KERNEL);
56         if (!psy->charging_trig_name)
57                 goto charging_failed;
58
59         psy->full_trig_name = kmalloc(strlen(psy->name) +
60                                       sizeof("-full"), GFP_KERNEL);
61         if (!psy->full_trig_name)
62                 goto full_failed;
63
64         strcpy(psy->charging_full_trig_name, psy->name);
65         strcat(psy->charging_full_trig_name, "-charging-or-full");
66         strcpy(psy->charging_trig_name, psy->name);
67         strcat(psy->charging_trig_name, "-charging");
68         strcpy(psy->full_trig_name, psy->name);
69         strcat(psy->full_trig_name, "-full");
70
71         led_trigger_register_simple(psy->charging_full_trig_name,
72                                     &psy->charging_full_trig);
73         led_trigger_register_simple(psy->charging_trig_name,
74                                     &psy->charging_trig);
75         led_trigger_register_simple(psy->full_trig_name,
76                                     &psy->full_trig);
77
78         goto success;
79
80 full_failed:
81         kfree(psy->charging_trig_name);
82 charging_failed:
83         kfree(psy->charging_full_trig_name);
84 charging_full_failed:
85         rc = -ENOMEM;
86 success:
87         return rc;
88 }
89
90 static void power_supply_remove_bat_triggers(struct power_supply *psy)
91 {
92         led_trigger_unregister_simple(psy->charging_full_trig);
93         led_trigger_unregister_simple(psy->charging_trig);
94         led_trigger_unregister_simple(psy->full_trig);
95         kfree(psy->full_trig_name);
96         kfree(psy->charging_trig_name);
97         kfree(psy->charging_full_trig_name);
98 }
99
100 /* Generated power specific LEDs triggers. */
101
102 static void power_supply_update_gen_leds(struct power_supply *psy)
103 {
104         union power_supply_propval online;
105
106         if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
107                 return;
108
109         dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval);
110
111         if (online.intval)
112                 led_trigger_event(psy->online_trig, LED_FULL);
113         else
114                 led_trigger_event(psy->online_trig, LED_OFF);
115 }
116
117 static int power_supply_create_gen_triggers(struct power_supply *psy)
118 {
119         int rc = 0;
120
121         psy->online_trig_name = kmalloc(strlen(psy->name) + sizeof("-online"),
122                                         GFP_KERNEL);
123         if (!psy->online_trig_name)
124                 goto online_failed;
125
126         strcpy(psy->online_trig_name, psy->name);
127         strcat(psy->online_trig_name, "-online");
128
129         led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
130
131         goto success;
132
133 online_failed:
134         rc = -ENOMEM;
135 success:
136         return rc;
137 }
138
139 static void power_supply_remove_gen_triggers(struct power_supply *psy)
140 {
141         led_trigger_unregister_simple(psy->online_trig);
142         kfree(psy->online_trig_name);
143 }
144
145 /* Choice what triggers to create&update. */
146
147 void power_supply_update_leds(struct power_supply *psy)
148 {
149         if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
150                 power_supply_update_bat_leds(psy);
151         else
152                 power_supply_update_gen_leds(psy);
153 }
154
155 int power_supply_create_triggers(struct power_supply *psy)
156 {
157         if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
158                 return power_supply_create_bat_triggers(psy);
159         return power_supply_create_gen_triggers(psy);
160 }
161
162 void power_supply_remove_triggers(struct power_supply *psy)
163 {
164         if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
165                 power_supply_remove_bat_triggers(psy);
166         else
167                 power_supply_remove_gen_triggers(psy);
168 }