]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/power/power_supply_core.c
ixgbe: move GSO segments and byte count processing into ixgbe_tx_map
[net-next-2.6.git] / drivers / power / power_supply_core.c
CommitLineData
4a11b59d
AV
1/*
2 * Universal power supply monitor 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/module.h>
14#include <linux/types.h>
15#include <linux/init.h>
5f487cd3 16#include <linux/slab.h>
4a11b59d
AV
17#include <linux/device.h>
18#include <linux/err.h>
19#include <linux/power_supply.h>
20#include "power_supply.h"
21
ff3417e7 22/* exported for the APM Power driver, APM emulation */
4a11b59d 23struct class *power_supply_class;
ff3417e7 24EXPORT_SYMBOL_GPL(power_supply_class);
4a11b59d 25
5f487cd3
AV
26static struct device_type power_supply_dev_type;
27
443cad92
DY
28static int __power_supply_changed_work(struct device *dev, void *data)
29{
30 struct power_supply *psy = (struct power_supply *)data;
31 struct power_supply *pst = dev_get_drvdata(dev);
32 int i;
33
34 for (i = 0; i < psy->num_supplicants; i++)
35 if (!strcmp(psy->supplied_to[i], pst->name)) {
36 if (pst->external_power_changed)
37 pst->external_power_changed(pst);
38 }
39 return 0;
40}
41
4a11b59d
AV
42static void power_supply_changed_work(struct work_struct *work)
43{
44 struct power_supply *psy = container_of(work, struct power_supply,
45 changed_work);
4a11b59d 46
0cddc0a9 47 dev_dbg(psy->dev, "%s\n", __func__);
4a11b59d 48
93562b53 49 class_for_each_device(power_supply_class, NULL, psy,
443cad92 50 __power_supply_changed_work);
4a11b59d
AV
51
52 power_supply_update_leds(psy);
53
54 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
4a11b59d
AV
55}
56
57void power_supply_changed(struct power_supply *psy)
58{
0cddc0a9 59 dev_dbg(psy->dev, "%s\n", __func__);
4a11b59d
AV
60
61 schedule_work(&psy->changed_work);
4a11b59d 62}
ff3417e7 63EXPORT_SYMBOL_GPL(power_supply_changed);
4a11b59d 64
443cad92 65static int __power_supply_am_i_supplied(struct device *dev, void *data)
4a11b59d
AV
66{
67 union power_supply_propval ret = {0,};
443cad92
DY
68 struct power_supply *psy = (struct power_supply *)data;
69 struct power_supply *epsy = dev_get_drvdata(dev);
70 int i;
71
72 for (i = 0; i < epsy->num_supplicants; i++) {
73 if (!strcmp(epsy->supplied_to[i], psy->name)) {
74 if (epsy->get_property(epsy,
75 POWER_SUPPLY_PROP_ONLINE, &ret))
76 continue;
77 if (ret.intval)
78 return ret.intval;
4a11b59d
AV
79 }
80 }
443cad92
DY
81 return 0;
82}
83
84int power_supply_am_i_supplied(struct power_supply *psy)
85{
86 int error;
87
93562b53 88 error = class_for_each_device(power_supply_class, NULL, psy,
443cad92 89 __power_supply_am_i_supplied);
4a11b59d 90
0cddc0a9 91 dev_dbg(psy->dev, "%s %d\n", __func__, error);
4a11b59d 92
443cad92 93 return error;
4a11b59d 94}
ff3417e7 95EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
4a11b59d 96
942ed161
MG
97static int __power_supply_is_system_supplied(struct device *dev, void *data)
98{
99 union power_supply_propval ret = {0,};
100 struct power_supply *psy = dev_get_drvdata(dev);
101
102 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
103 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
104 return 0;
105 if (ret.intval)
106 return ret.intval;
107 }
108 return 0;
109}
110
111int power_supply_is_system_supplied(void)
112{
113 int error;
114
115 error = class_for_each_device(power_supply_class, NULL, NULL,
116 __power_supply_is_system_supplied);
117
118 return error;
119}
ff3417e7 120EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
942ed161 121
e5f5ccb6
DM
122int power_supply_set_battery_charged(struct power_supply *psy)
123{
124 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
125 psy->set_charged(psy);
126 return 0;
127 }
128
129 return -EINVAL;
130}
131EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
132
133static int power_supply_match_device_by_name(struct device *dev, void *data)
134{
135 const char *name = data;
136 struct power_supply *psy = dev_get_drvdata(dev);
137
138 return strcmp(psy->name, name) == 0;
139}
140
141struct power_supply *power_supply_get_by_name(char *name)
142{
143 struct device *dev = class_find_device(power_supply_class, NULL, name,
144 power_supply_match_device_by_name);
145
146 return dev ? dev_get_drvdata(dev) : NULL;
147}
148EXPORT_SYMBOL_GPL(power_supply_get_by_name);
149
5f487cd3
AV
150static void power_supply_dev_release(struct device *dev)
151{
152 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
153 kfree(dev);
154}
155
4a11b59d
AV
156int power_supply_register(struct device *parent, struct power_supply *psy)
157{
5f487cd3
AV
158 struct device *dev;
159 int rc;
4a11b59d 160
5f487cd3
AV
161 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
162 if (!dev)
163 return -ENOMEM;
4a11b59d 164
5f487cd3 165 device_initialize(dev);
4a11b59d 166
5f487cd3
AV
167 dev->class = power_supply_class;
168 dev->type = &power_supply_dev_type;
169 dev->parent = parent;
170 dev->release = power_supply_dev_release;
171 dev_set_drvdata(dev, psy);
172 psy->dev = dev;
173
174 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
175 if (rc)
176 goto kobject_set_name_failed;
177
178 rc = device_add(dev);
4a11b59d 179 if (rc)
5f487cd3
AV
180 goto device_add_failed;
181
182 INIT_WORK(&psy->changed_work, power_supply_changed_work);
4a11b59d
AV
183
184 rc = power_supply_create_triggers(psy);
185 if (rc)
186 goto create_triggers_failed;
187
188 power_supply_changed(psy);
189
190 goto success;
191
192create_triggers_failed:
4a11b59d 193 device_unregister(psy->dev);
5f487cd3
AV
194kobject_set_name_failed:
195device_add_failed:
196 kfree(dev);
4a11b59d
AV
197success:
198 return rc;
199}
ff3417e7 200EXPORT_SYMBOL_GPL(power_supply_register);
4a11b59d
AV
201
202void power_supply_unregister(struct power_supply *psy)
203{
204 flush_scheduled_work();
205 power_supply_remove_triggers(psy);
4a11b59d 206 device_unregister(psy->dev);
4a11b59d 207}
ff3417e7 208EXPORT_SYMBOL_GPL(power_supply_unregister);
4a11b59d
AV
209
210static int __init power_supply_class_init(void)
211{
212 power_supply_class = class_create(THIS_MODULE, "power_supply");
213
214 if (IS_ERR(power_supply_class))
215 return PTR_ERR(power_supply_class);
216
217 power_supply_class->dev_uevent = power_supply_uevent;
5f487cd3 218 power_supply_init_attrs(&power_supply_dev_type);
4a11b59d
AV
219
220 return 0;
221}
222
223static void __exit power_supply_class_exit(void)
224{
225 class_destroy(power_supply_class);
4a11b59d
AV
226}
227
4a11b59d
AV
228subsys_initcall(power_supply_class_init);
229module_exit(power_supply_class_exit);
230
231MODULE_DESCRIPTION("Universal power supply monitor class");
232MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
233 "Szabolcs Gyurko, "
234 "Anton Vorontsov <cbou@mail.ru>");
235MODULE_LICENSE("GPL");