1/*
2 *  Sysfs interface for the universal power supply monitor class
3 *
4 *  Copyright © 2007  David Woodhouse <dwmw2@infradead.org>
5 *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
6 *  Copyright © 2004  Szabolcs Gyurko
7 *  Copyright © 2003  Ian Molton <spyro@f2s.com>
8 *
9 *  Modified: 2004, Oct     Szabolcs Gyurko
10 *
11 *  You may use this code as per GPL version 2
12 */
13
14#include <linux/ctype.h>
15#include <linux/device.h>
16#include <linux/power_supply.h>
17#include <linux/slab.h>
18#include <linux/stat.h>
19
20#include "power_supply.h"
21
22/*
23 * This is because the name "current" breaks the device attr macro.
24 * The "current" word resolves to "(get_current())" so instead of
25 * "current" "(get_current())" appears in the sysfs.
26 *
27 * The source of this definition is the device.h which calls __ATTR
28 * macro in sysfs.h which calls the __stringify macro.
29 *
30 * Only modification that the name is not tried to be resolved
31 * (as a macro let's say).
32 */
33
34#define POWER_SUPPLY_ATTR(_name)					\
35{									\
36	.attr = { .name = #_name },					\
37	.show = power_supply_show_property,				\
38	.store = power_supply_store_property,				\
39}
40
41static struct device_attribute power_supply_attrs[];
42
43static ssize_t power_supply_show_property(struct device *dev,
44					  struct device_attribute *attr,
45					  char *buf) {
46	static char *type_text[] = {
47		"Unknown", "Battery", "UPS", "Mains", "USB",
48		"USB_DCP", "USB_CDP", "USB_ACA"
49	};
50	static char *status_text[] = {
51		"Unknown", "Charging", "Discharging", "Not charging", "Full"
52	};
53	static char *charge_type[] = {
54		"Unknown", "N/A", "Trickle", "Fast"
55	};
56	static char *health_text[] = {
57		"Unknown", "Good", "Overheat", "Dead", "Over voltage",
58		"Unspecified failure", "Cold", "Watchdog timer expire",
59		"Safety timer expire"
60	};
61	static char *technology_text[] = {
62		"Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
63		"LiMn"
64	};
65	static char *capacity_level_text[] = {
66		"Unknown", "Critical", "Low", "Normal", "High", "Full"
67	};
68	static char *scope_text[] = {
69		"Unknown", "System", "Device"
70	};
71	ssize_t ret = 0;
72	struct power_supply *psy = dev_get_drvdata(dev);
73	const ptrdiff_t off = attr - power_supply_attrs;
74	union power_supply_propval value;
75
76	if (off == POWER_SUPPLY_PROP_TYPE) {
77		value.intval = psy->type;
78	} else {
79		ret = psy->get_property(psy, off, &value);
80
81		if (ret < 0) {
82			if (ret == -ENODATA)
83				dev_dbg(dev, "driver has no data for `%s' property\n",
84					attr->attr.name);
85			else if (ret != -ENODEV)
86				dev_err(dev, "driver failed to report `%s' property: %zd\n",
87					attr->attr.name, ret);
88			return ret;
89		}
90	}
91
92	if (off == POWER_SUPPLY_PROP_STATUS)
93		return sprintf(buf, "%s\n", status_text[value.intval]);
94	else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
95		return sprintf(buf, "%s\n", charge_type[value.intval]);
96	else if (off == POWER_SUPPLY_PROP_HEALTH)
97		return sprintf(buf, "%s\n", health_text[value.intval]);
98	else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
99		return sprintf(buf, "%s\n", technology_text[value.intval]);
100	else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
101		return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
102	else if (off == POWER_SUPPLY_PROP_TYPE)
103		return sprintf(buf, "%s\n", type_text[value.intval]);
104	else if (off == POWER_SUPPLY_PROP_SCOPE)
105		return sprintf(buf, "%s\n", scope_text[value.intval]);
106	else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
107		return sprintf(buf, "%s\n", value.strval);
108
109	if (off == POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT)
110		return sprintf(buf, "%lld\n", value.int64val);
111	else
112		return sprintf(buf, "%d\n", value.intval);
113}
114
115static ssize_t power_supply_store_property(struct device *dev,
116					   struct device_attribute *attr,
117					   const char *buf, size_t count) {
118	ssize_t ret;
119	struct power_supply *psy = dev_get_drvdata(dev);
120	const ptrdiff_t off = attr - power_supply_attrs;
121	union power_supply_propval value;
122	long long_val;
123
124	/* TODO: support other types than int */
125	ret = kstrtol(buf, 10, &long_val);
126	if (ret < 0)
127		return ret;
128
129	value.intval = long_val;
130
131	ret = psy->set_property(psy, off, &value);
132	if (ret < 0)
133		return ret;
134
135	return count;
136}
137
138/* Must be in the same order as POWER_SUPPLY_PROP_* */
139static struct device_attribute power_supply_attrs[] = {
140	/* Properties of type `int' */
141	POWER_SUPPLY_ATTR(status),
142	POWER_SUPPLY_ATTR(charge_type),
143	POWER_SUPPLY_ATTR(health),
144	POWER_SUPPLY_ATTR(present),
145	POWER_SUPPLY_ATTR(online),
146	POWER_SUPPLY_ATTR(authentic),
147	POWER_SUPPLY_ATTR(technology),
148	POWER_SUPPLY_ATTR(cycle_count),
149	POWER_SUPPLY_ATTR(voltage_max),
150	POWER_SUPPLY_ATTR(voltage_min),
151	POWER_SUPPLY_ATTR(voltage_max_design),
152	POWER_SUPPLY_ATTR(voltage_min_design),
153	POWER_SUPPLY_ATTR(voltage_now),
154	POWER_SUPPLY_ATTR(voltage_avg),
155	POWER_SUPPLY_ATTR(voltage_ocv),
156	POWER_SUPPLY_ATTR(voltage_boot),
157	POWER_SUPPLY_ATTR(current_max),
158	POWER_SUPPLY_ATTR(current_now),
159	POWER_SUPPLY_ATTR(current_avg),
160	POWER_SUPPLY_ATTR(current_boot),
161	POWER_SUPPLY_ATTR(power_now),
162	POWER_SUPPLY_ATTR(power_avg),
163	POWER_SUPPLY_ATTR(charge_full_design),
164	POWER_SUPPLY_ATTR(charge_empty_design),
165	POWER_SUPPLY_ATTR(charge_full),
166	POWER_SUPPLY_ATTR(charge_empty),
167	POWER_SUPPLY_ATTR(charge_now),
168	POWER_SUPPLY_ATTR(charge_avg),
169	POWER_SUPPLY_ATTR(charge_counter),
170	POWER_SUPPLY_ATTR(constant_charge_current),
171	POWER_SUPPLY_ATTR(constant_charge_current_max),
172	POWER_SUPPLY_ATTR(constant_charge_voltage),
173	POWER_SUPPLY_ATTR(constant_charge_voltage_max),
174	POWER_SUPPLY_ATTR(charge_control_limit),
175	POWER_SUPPLY_ATTR(charge_control_limit_max),
176	POWER_SUPPLY_ATTR(input_current_limit),
177	POWER_SUPPLY_ATTR(energy_full_design),
178	POWER_SUPPLY_ATTR(energy_empty_design),
179	POWER_SUPPLY_ATTR(energy_full),
180	POWER_SUPPLY_ATTR(energy_empty),
181	POWER_SUPPLY_ATTR(energy_now),
182	POWER_SUPPLY_ATTR(energy_avg),
183	POWER_SUPPLY_ATTR(capacity),
184	POWER_SUPPLY_ATTR(capacity_alert_min),
185	POWER_SUPPLY_ATTR(capacity_alert_max),
186	POWER_SUPPLY_ATTR(capacity_level),
187	POWER_SUPPLY_ATTR(temp),
188	POWER_SUPPLY_ATTR(temp_max),
189	POWER_SUPPLY_ATTR(temp_min),
190	POWER_SUPPLY_ATTR(temp_alert_min),
191	POWER_SUPPLY_ATTR(temp_alert_max),
192	POWER_SUPPLY_ATTR(temp_ambient),
193	POWER_SUPPLY_ATTR(temp_ambient_alert_min),
194	POWER_SUPPLY_ATTR(temp_ambient_alert_max),
195	POWER_SUPPLY_ATTR(time_to_empty_now),
196	POWER_SUPPLY_ATTR(time_to_empty_avg),
197	POWER_SUPPLY_ATTR(time_to_full_now),
198	POWER_SUPPLY_ATTR(time_to_full_avg),
199	POWER_SUPPLY_ATTR(type),
200	POWER_SUPPLY_ATTR(scope),
201	POWER_SUPPLY_ATTR(charge_term_current),
202	POWER_SUPPLY_ATTR(calibrate),
203	/* Local extensions */
204	POWER_SUPPLY_ATTR(usb_hc),
205	POWER_SUPPLY_ATTR(usb_otg),
206	POWER_SUPPLY_ATTR(charge_enabled),
207	/* Local extensions of type int64_t */
208	POWER_SUPPLY_ATTR(charge_counter_ext),
209	/* Properties of type `const char *' */
210	POWER_SUPPLY_ATTR(model_name),
211	POWER_SUPPLY_ATTR(manufacturer),
212	POWER_SUPPLY_ATTR(serial_number),
213};
214
215static struct attribute *
216__power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
217
218static umode_t power_supply_attr_is_visible(struct kobject *kobj,
219					   struct attribute *attr,
220					   int attrno)
221{
222	struct device *dev = container_of(kobj, struct device, kobj);
223	struct power_supply *psy = dev_get_drvdata(dev);
224	umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
225	int i;
226
227	if (attrno == POWER_SUPPLY_PROP_TYPE)
228		return mode;
229
230	for (i = 0; i < psy->num_properties; i++) {
231		int property = psy->properties[i];
232
233		if (property == attrno) {
234			if (psy->property_is_writeable &&
235			    psy->property_is_writeable(psy, property) > 0)
236				mode |= S_IWUSR;
237
238			return mode;
239		}
240	}
241
242	return 0;
243}
244
245static struct attribute_group power_supply_attr_group = {
246	.attrs = __power_supply_attrs,
247	.is_visible = power_supply_attr_is_visible,
248};
249
250static const struct attribute_group *power_supply_attr_groups[] = {
251	&power_supply_attr_group,
252	NULL,
253};
254
255void power_supply_init_attrs(struct device_type *dev_type)
256{
257	int i;
258
259	dev_type->groups = power_supply_attr_groups;
260
261	for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
262		__power_supply_attrs[i] = &power_supply_attrs[i].attr;
263}
264
265static char *kstruprdup(const char *str, gfp_t gfp)
266{
267	char *ret, *ustr;
268
269	ustr = ret = kmalloc(strlen(str) + 1, gfp);
270
271	if (!ret)
272		return NULL;
273
274	while (*str)
275		*ustr++ = toupper(*str++);
276
277	*ustr = 0;
278
279	return ret;
280}
281
282int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
283{
284	struct power_supply *psy = dev_get_drvdata(dev);
285	int ret = 0, j;
286	char *prop_buf;
287	char *attrname;
288
289	dev_dbg(dev, "uevent\n");
290
291	if (!psy || !psy->dev) {
292		dev_dbg(dev, "No power supply yet\n");
293		return ret;
294	}
295
296	dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
297
298	ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
299	if (ret)
300		return ret;
301
302	prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
303	if (!prop_buf)
304		return -ENOMEM;
305
306	for (j = 0; j < psy->num_properties; j++) {
307		struct device_attribute *attr;
308		char *line;
309
310		attr = &power_supply_attrs[psy->properties[j]];
311
312		ret = power_supply_show_property(dev, attr, prop_buf);
313		if (ret == -ENODEV || ret == -ENODATA) {
314			/* When a battery is absent, we expect -ENODEV. Don't abort;
315			   send the uevent with at least the the PRESENT=0 property */
316			ret = 0;
317			continue;
318		}
319
320		if (ret < 0)
321			goto out;
322
323		line = strchr(prop_buf, '\n');
324		if (line)
325			*line = 0;
326
327		attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
328		if (!attrname) {
329			ret = -ENOMEM;
330			goto out;
331		}
332
333		dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
334
335		ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
336		kfree(attrname);
337		if (ret)
338			goto out;
339	}
340
341out:
342	free_page((unsigned long)prop_buf);
343
344	return ret;
345}
346