ACPI: SBS: Fix retval warning
[deliverable/linux.git] / drivers / acpi / sbs.c
1 /*
2 * sbs.c - ACPI Smart Battery System Driver ($Revision: 2.0 $)
3 *
4 * Copyright (c) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (c) 2005-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/kernel.h>
31
32 #ifdef CONFIG_ACPI_PROCFS
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <asm/uaccess.h>
36 #endif
37
38 #include <linux/acpi.h>
39 #include <linux/timer.h>
40 #include <linux/jiffies.h>
41 #include <linux/delay.h>
42
43 #include <linux/power_supply.h>
44
45 #include "sbshc.h"
46
47 #define ACPI_SBS_CLASS "sbs"
48 #define ACPI_AC_CLASS "ac_adapter"
49 #define ACPI_BATTERY_CLASS "battery"
50 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
51 #define ACPI_SBS_FILE_INFO "info"
52 #define ACPI_SBS_FILE_STATE "state"
53 #define ACPI_SBS_FILE_ALARM "alarm"
54 #define ACPI_BATTERY_DIR_NAME "BAT%i"
55 #define ACPI_AC_DIR_NAME "AC0"
56
57 enum acpi_sbs_device_addr {
58 ACPI_SBS_CHARGER = 0x9,
59 ACPI_SBS_MANAGER = 0xa,
60 ACPI_SBS_BATTERY = 0xb,
61 };
62
63 #define ACPI_SBS_NOTIFY_STATUS 0x80
64 #define ACPI_SBS_NOTIFY_INFO 0x81
65
66 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
67 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
68 MODULE_LICENSE("GPL");
69
70 static unsigned int cache_time = 1000;
71 module_param(cache_time, uint, 0644);
72 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
73
74 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
75 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
77 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
78
79 #define MAX_SBS_BAT 4
80 #define ACPI_SBS_BLOCK_MAX 32
81
82 static const struct acpi_device_id sbs_device_ids[] = {
83 {"ACPI0002", 0},
84 {"", 0},
85 };
86 MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
87
88 struct acpi_battery {
89 struct power_supply bat;
90 struct acpi_sbs *sbs;
91 #ifdef CONFIG_ACPI_PROCFS
92 struct proc_dir_entry *proc_entry;
93 #endif
94 unsigned long update_time;
95 char name[8];
96 char manufacturer_name[ACPI_SBS_BLOCK_MAX];
97 char device_name[ACPI_SBS_BLOCK_MAX];
98 char device_chemistry[ACPI_SBS_BLOCK_MAX];
99 u16 alarm_capacity;
100 u16 full_charge_capacity;
101 u16 design_capacity;
102 u16 design_voltage;
103 u16 serial_number;
104 u16 cycle_count;
105 u16 temp_now;
106 u16 voltage_now;
107 s16 current_now;
108 s16 current_avg;
109 u16 capacity_now;
110 u16 state_of_charge;
111 u16 state;
112 u16 mode;
113 u16 spec;
114 u8 id;
115 u8 present:1;
116 u8 have_sysfs_alarm:1;
117 };
118
119 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
120
121 struct acpi_sbs {
122 struct power_supply charger;
123 struct acpi_device *device;
124 struct acpi_smb_hc *hc;
125 struct mutex lock;
126 #ifdef CONFIG_ACPI_PROCFS
127 struct proc_dir_entry *charger_entry;
128 #endif
129 struct acpi_battery battery[MAX_SBS_BAT];
130 u8 batteries_supported:4;
131 u8 manager_present:1;
132 u8 charger_present:1;
133 };
134
135 #define to_acpi_sbs(x) container_of(x, struct acpi_sbs, charger)
136
137 static inline int battery_scale(int log)
138 {
139 int scale = 1;
140 while (log--)
141 scale *= 10;
142 return scale;
143 }
144
145 static inline int acpi_battery_vscale(struct acpi_battery *battery)
146 {
147 return battery_scale((battery->spec & 0x0f00) >> 8);
148 }
149
150 static inline int acpi_battery_ipscale(struct acpi_battery *battery)
151 {
152 return battery_scale((battery->spec & 0xf000) >> 12);
153 }
154
155 static inline int acpi_battery_mode(struct acpi_battery *battery)
156 {
157 return (battery->mode & 0x8000);
158 }
159
160 static inline int acpi_battery_scale(struct acpi_battery *battery)
161 {
162 return (acpi_battery_mode(battery) ? 10 : 1) *
163 acpi_battery_ipscale(battery);
164 }
165
166 static int sbs_get_ac_property(struct power_supply *psy,
167 enum power_supply_property psp,
168 union power_supply_propval *val)
169 {
170 struct acpi_sbs *sbs = to_acpi_sbs(psy);
171 switch (psp) {
172 case POWER_SUPPLY_PROP_ONLINE:
173 val->intval = sbs->charger_present;
174 break;
175 default:
176 return -EINVAL;
177 }
178 return 0;
179 }
180
181 static int acpi_battery_technology(struct acpi_battery *battery)
182 {
183 if (!strcasecmp("NiCd", battery->device_chemistry))
184 return POWER_SUPPLY_TECHNOLOGY_NiCd;
185 if (!strcasecmp("NiMH", battery->device_chemistry))
186 return POWER_SUPPLY_TECHNOLOGY_NiMH;
187 if (!strcasecmp("LION", battery->device_chemistry))
188 return POWER_SUPPLY_TECHNOLOGY_LION;
189 if (!strcasecmp("LiP", battery->device_chemistry))
190 return POWER_SUPPLY_TECHNOLOGY_LIPO;
191 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
192 }
193
194 static int acpi_sbs_battery_get_property(struct power_supply *psy,
195 enum power_supply_property psp,
196 union power_supply_propval *val)
197 {
198 struct acpi_battery *battery = to_acpi_battery(psy);
199
200 if ((!battery->present) && psp != POWER_SUPPLY_PROP_PRESENT)
201 return -ENODEV;
202 switch (psp) {
203 case POWER_SUPPLY_PROP_STATUS:
204 if (battery->current_now < 0)
205 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
206 else if (battery->current_now > 0)
207 val->intval = POWER_SUPPLY_STATUS_CHARGING;
208 else
209 val->intval = POWER_SUPPLY_STATUS_FULL;
210 break;
211 case POWER_SUPPLY_PROP_PRESENT:
212 val->intval = battery->present;
213 break;
214 case POWER_SUPPLY_PROP_TECHNOLOGY:
215 val->intval = acpi_battery_technology(battery);
216 break;
217 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
218 val->intval = battery->design_voltage *
219 acpi_battery_vscale(battery) * 1000;
220 break;
221 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
222 val->intval = battery->voltage_now *
223 acpi_battery_vscale(battery) * 1000;
224 break;
225 case POWER_SUPPLY_PROP_CURRENT_NOW:
226 val->intval = abs(battery->current_now) *
227 acpi_battery_ipscale(battery) * 1000;
228 break;
229 case POWER_SUPPLY_PROP_CURRENT_AVG:
230 val->intval = abs(battery->current_avg) *
231 acpi_battery_ipscale(battery) * 1000;
232 break;
233 case POWER_SUPPLY_PROP_CAPACITY:
234 val->intval = battery->state_of_charge;
235 break;
236 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
237 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
238 val->intval = battery->design_capacity *
239 acpi_battery_scale(battery) * 1000;
240 break;
241 case POWER_SUPPLY_PROP_CHARGE_FULL:
242 case POWER_SUPPLY_PROP_ENERGY_FULL:
243 val->intval = battery->full_charge_capacity *
244 acpi_battery_scale(battery) * 1000;
245 break;
246 case POWER_SUPPLY_PROP_CHARGE_NOW:
247 case POWER_SUPPLY_PROP_ENERGY_NOW:
248 val->intval = battery->capacity_now *
249 acpi_battery_scale(battery) * 1000;
250 break;
251 case POWER_SUPPLY_PROP_TEMP:
252 val->intval = battery->temp_now - 2730; // dK -> dC
253 break;
254 case POWER_SUPPLY_PROP_MODEL_NAME:
255 val->strval = battery->device_name;
256 break;
257 case POWER_SUPPLY_PROP_MANUFACTURER:
258 val->strval = battery->manufacturer_name;
259 break;
260 default:
261 return -EINVAL;
262 }
263 return 0;
264 }
265
266 static enum power_supply_property sbs_ac_props[] = {
267 POWER_SUPPLY_PROP_ONLINE,
268 };
269
270 static enum power_supply_property sbs_charge_battery_props[] = {
271 POWER_SUPPLY_PROP_STATUS,
272 POWER_SUPPLY_PROP_PRESENT,
273 POWER_SUPPLY_PROP_TECHNOLOGY,
274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW,
276 POWER_SUPPLY_PROP_CURRENT_NOW,
277 POWER_SUPPLY_PROP_CURRENT_AVG,
278 POWER_SUPPLY_PROP_CAPACITY,
279 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
280 POWER_SUPPLY_PROP_CHARGE_FULL,
281 POWER_SUPPLY_PROP_CHARGE_NOW,
282 POWER_SUPPLY_PROP_TEMP,
283 POWER_SUPPLY_PROP_MODEL_NAME,
284 POWER_SUPPLY_PROP_MANUFACTURER,
285 };
286
287 static enum power_supply_property sbs_energy_battery_props[] = {
288 POWER_SUPPLY_PROP_STATUS,
289 POWER_SUPPLY_PROP_PRESENT,
290 POWER_SUPPLY_PROP_TECHNOLOGY,
291 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
292 POWER_SUPPLY_PROP_VOLTAGE_NOW,
293 POWER_SUPPLY_PROP_CURRENT_NOW,
294 POWER_SUPPLY_PROP_CURRENT_AVG,
295 POWER_SUPPLY_PROP_CAPACITY,
296 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
297 POWER_SUPPLY_PROP_ENERGY_FULL,
298 POWER_SUPPLY_PROP_ENERGY_NOW,
299 POWER_SUPPLY_PROP_TEMP,
300 POWER_SUPPLY_PROP_MODEL_NAME,
301 POWER_SUPPLY_PROP_MANUFACTURER,
302 };
303
304 /* --------------------------------------------------------------------------
305 Smart Battery System Management
306 -------------------------------------------------------------------------- */
307
308 struct acpi_battery_reader {
309 u8 command; /* command for battery */
310 u8 mode; /* word or block? */
311 size_t offset; /* offset inside struct acpi_sbs_battery */
312 };
313
314 static struct acpi_battery_reader info_readers[] = {
315 {0x01, SMBUS_READ_WORD, offsetof(struct acpi_battery, alarm_capacity)},
316 {0x03, SMBUS_READ_WORD, offsetof(struct acpi_battery, mode)},
317 {0x10, SMBUS_READ_WORD, offsetof(struct acpi_battery, full_charge_capacity)},
318 {0x17, SMBUS_READ_WORD, offsetof(struct acpi_battery, cycle_count)},
319 {0x18, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_capacity)},
320 {0x19, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_voltage)},
321 {0x1a, SMBUS_READ_WORD, offsetof(struct acpi_battery, spec)},
322 {0x1c, SMBUS_READ_WORD, offsetof(struct acpi_battery, serial_number)},
323 {0x20, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, manufacturer_name)},
324 {0x21, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_name)},
325 {0x22, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_chemistry)},
326 };
327
328 static struct acpi_battery_reader state_readers[] = {
329 {0x08, SMBUS_READ_WORD, offsetof(struct acpi_battery, temp_now)},
330 {0x09, SMBUS_READ_WORD, offsetof(struct acpi_battery, voltage_now)},
331 {0x0a, SMBUS_READ_WORD, offsetof(struct acpi_battery, current_now)},
332 {0x0b, SMBUS_READ_WORD, offsetof(struct acpi_battery, current_avg)},
333 {0x0f, SMBUS_READ_WORD, offsetof(struct acpi_battery, capacity_now)},
334 {0x0e, SMBUS_READ_WORD, offsetof(struct acpi_battery, state_of_charge)},
335 {0x16, SMBUS_READ_WORD, offsetof(struct acpi_battery, state)},
336 };
337
338 static int acpi_manager_get_info(struct acpi_sbs *sbs)
339 {
340 int result = 0;
341 u16 battery_system_info;
342
343 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
344 0x04, (u8 *)&battery_system_info);
345 if (!result)
346 sbs->batteries_supported = battery_system_info & 0x000f;
347 return result;
348 }
349
350 static int acpi_battery_get_info(struct acpi_battery *battery)
351 {
352 int i, result = 0;
353
354 for (i = 0; i < ARRAY_SIZE(info_readers); ++i) {
355 result = acpi_smbus_read(battery->sbs->hc,
356 info_readers[i].mode,
357 ACPI_SBS_BATTERY,
358 info_readers[i].command,
359 (u8 *) battery +
360 info_readers[i].offset);
361 if (result)
362 break;
363 }
364 return result;
365 }
366
367 static int acpi_battery_get_state(struct acpi_battery *battery)
368 {
369 int i, result = 0;
370
371 if (battery->update_time &&
372 time_before(jiffies, battery->update_time +
373 msecs_to_jiffies(cache_time)))
374 return 0;
375 for (i = 0; i < ARRAY_SIZE(state_readers); ++i) {
376 result = acpi_smbus_read(battery->sbs->hc,
377 state_readers[i].mode,
378 ACPI_SBS_BATTERY,
379 state_readers[i].command,
380 (u8 *)battery +
381 state_readers[i].offset);
382 if (result)
383 goto end;
384 }
385 end:
386 battery->update_time = jiffies;
387 return result;
388 }
389
390 static int acpi_battery_get_alarm(struct acpi_battery *battery)
391 {
392 return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
393 ACPI_SBS_BATTERY, 0x01,
394 (u8 *)&battery->alarm_capacity);
395 }
396
397 static int acpi_battery_set_alarm(struct acpi_battery *battery)
398 {
399 struct acpi_sbs *sbs = battery->sbs;
400 u16 value, sel = 1 << (battery->id + 12);
401
402 int ret;
403
404
405 if (sbs->manager_present) {
406 ret = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
407 0x01, (u8 *)&value);
408 if (ret)
409 goto end;
410 if ((value & 0xf000) != sel) {
411 value &= 0x0fff;
412 value |= sel;
413 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD,
414 ACPI_SBS_MANAGER,
415 0x01, (u8 *)&value, 2);
416 if (ret)
417 goto end;
418 }
419 }
420 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY,
421 0x01, (u8 *)&battery->alarm_capacity, 2);
422 end:
423 return ret;
424 }
425
426 static int acpi_ac_get_present(struct acpi_sbs *sbs)
427 {
428 int result;
429 u16 status;
430
431 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER,
432 0x13, (u8 *) & status);
433 if (!result)
434 sbs->charger_present = (status >> 15) & 0x1;
435 return result;
436 }
437
438 static ssize_t acpi_battery_alarm_show(struct device *dev,
439 struct device_attribute *attr,
440 char *buf)
441 {
442 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
443 acpi_battery_get_alarm(battery);
444 return sprintf(buf, "%d\n", battery->alarm_capacity *
445 acpi_battery_scale(battery) * 1000);
446 }
447
448 static ssize_t acpi_battery_alarm_store(struct device *dev,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
451 {
452 unsigned long x;
453 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
454 if (sscanf(buf, "%ld\n", &x) == 1)
455 battery->alarm_capacity = x /
456 (1000 * acpi_battery_scale(battery));
457 if (battery->present)
458 acpi_battery_set_alarm(battery);
459 return count;
460 }
461
462 static struct device_attribute alarm_attr = {
463 .attr = {.name = "alarm", .mode = 0644, .owner = THIS_MODULE},
464 .show = acpi_battery_alarm_show,
465 .store = acpi_battery_alarm_store,
466 };
467
468 /* --------------------------------------------------------------------------
469 FS Interface (/proc/acpi)
470 -------------------------------------------------------------------------- */
471
472 #ifdef CONFIG_ACPI_PROCFS
473 /* Generic Routines */
474 static int
475 acpi_sbs_add_fs(struct proc_dir_entry **dir,
476 struct proc_dir_entry *parent_dir,
477 char *dir_name,
478 struct file_operations *info_fops,
479 struct file_operations *state_fops,
480 struct file_operations *alarm_fops, void *data)
481 {
482 struct proc_dir_entry *entry = NULL;
483
484 if (!*dir) {
485 *dir = proc_mkdir(dir_name, parent_dir);
486 if (!*dir) {
487 return -ENODEV;
488 }
489 (*dir)->owner = THIS_MODULE;
490 }
491
492 /* 'info' [R] */
493 if (info_fops) {
494 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
495 if (entry) {
496 entry->proc_fops = info_fops;
497 entry->data = data;
498 entry->owner = THIS_MODULE;
499 }
500 }
501
502 /* 'state' [R] */
503 if (state_fops) {
504 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
505 if (entry) {
506 entry->proc_fops = state_fops;
507 entry->data = data;
508 entry->owner = THIS_MODULE;
509 }
510 }
511
512 /* 'alarm' [R/W] */
513 if (alarm_fops) {
514 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
515 if (entry) {
516 entry->proc_fops = alarm_fops;
517 entry->data = data;
518 entry->owner = THIS_MODULE;
519 }
520 }
521 return 0;
522 }
523
524 static void
525 acpi_sbs_remove_fs(struct proc_dir_entry **dir,
526 struct proc_dir_entry *parent_dir)
527 {
528 if (*dir) {
529 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
530 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
531 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
532 remove_proc_entry((*dir)->name, parent_dir);
533 *dir = NULL;
534 }
535 }
536
537 /* Smart Battery Interface */
538 static struct proc_dir_entry *acpi_battery_dir = NULL;
539
540 static inline char *acpi_battery_units(struct acpi_battery *battery)
541 {
542 return acpi_battery_mode(battery) ? " mWh" : " mAh";
543 }
544
545
546 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
547 {
548 struct acpi_battery *battery = seq->private;
549 struct acpi_sbs *sbs = battery->sbs;
550 int result = 0;
551
552 mutex_lock(&sbs->lock);
553
554 seq_printf(seq, "present: %s\n",
555 (battery->present) ? "yes" : "no");
556 if (!battery->present)
557 goto end;
558
559 seq_printf(seq, "design capacity: %i%s\n",
560 battery->design_capacity * acpi_battery_scale(battery),
561 acpi_battery_units(battery));
562 seq_printf(seq, "last full capacity: %i%s\n",
563 battery->full_charge_capacity * acpi_battery_scale(battery),
564 acpi_battery_units(battery));
565 seq_printf(seq, "battery technology: rechargeable\n");
566 seq_printf(seq, "design voltage: %i mV\n",
567 battery->design_voltage * acpi_battery_vscale(battery));
568 seq_printf(seq, "design capacity warning: unknown\n");
569 seq_printf(seq, "design capacity low: unknown\n");
570 seq_printf(seq, "capacity granularity 1: unknown\n");
571 seq_printf(seq, "capacity granularity 2: unknown\n");
572 seq_printf(seq, "model number: %s\n", battery->device_name);
573 seq_printf(seq, "serial number: %i\n",
574 battery->serial_number);
575 seq_printf(seq, "battery type: %s\n",
576 battery->device_chemistry);
577 seq_printf(seq, "OEM info: %s\n",
578 battery->manufacturer_name);
579 end:
580 mutex_unlock(&sbs->lock);
581 return result;
582 }
583
584 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
585 {
586 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
587 }
588
589 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
590 {
591 struct acpi_battery *battery = seq->private;
592 struct acpi_sbs *sbs = battery->sbs;
593 int result = 0;
594
595 mutex_lock(&sbs->lock);
596 seq_printf(seq, "present: %s\n",
597 (battery->present) ? "yes" : "no");
598 if (!battery->present)
599 goto end;
600
601 acpi_battery_get_state(battery);
602 seq_printf(seq, "capacity state: %s\n",
603 (battery->state & 0x0010) ? "critical" : "ok");
604 seq_printf(seq, "charging state: %s\n",
605 (battery->current_now < 0) ? "discharging" :
606 ((battery->current_now > 0) ? "charging" : "charged"));
607 seq_printf(seq, "present rate: %d mA\n",
608 abs(battery->current_now) * acpi_battery_ipscale(battery));
609 seq_printf(seq, "remaining capacity: %i%s\n",
610 battery->capacity_now * acpi_battery_scale(battery),
611 acpi_battery_units(battery));
612 seq_printf(seq, "present voltage: %i mV\n",
613 battery->voltage_now * acpi_battery_vscale(battery));
614
615 end:
616 mutex_unlock(&sbs->lock);
617 return result;
618 }
619
620 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
621 {
622 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
623 }
624
625 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
626 {
627 struct acpi_battery *battery = seq->private;
628 struct acpi_sbs *sbs = battery->sbs;
629 int result = 0;
630
631 mutex_lock(&sbs->lock);
632
633 if (!battery->present) {
634 seq_printf(seq, "present: no\n");
635 goto end;
636 }
637
638 acpi_battery_get_alarm(battery);
639 seq_printf(seq, "alarm: ");
640 if (battery->alarm_capacity)
641 seq_printf(seq, "%i%s\n",
642 battery->alarm_capacity *
643 acpi_battery_scale(battery),
644 acpi_battery_units(battery));
645 else
646 seq_printf(seq, "disabled\n");
647 end:
648 mutex_unlock(&sbs->lock);
649 return result;
650 }
651
652 static ssize_t
653 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
654 size_t count, loff_t * ppos)
655 {
656 struct seq_file *seq = file->private_data;
657 struct acpi_battery *battery = seq->private;
658 struct acpi_sbs *sbs = battery->sbs;
659 char alarm_string[12] = { '\0' };
660 int result = 0;
661 mutex_lock(&sbs->lock);
662 if (!battery->present) {
663 result = -ENODEV;
664 goto end;
665 }
666 if (count > sizeof(alarm_string) - 1) {
667 result = -EINVAL;
668 goto end;
669 }
670 if (copy_from_user(alarm_string, buffer, count)) {
671 result = -EFAULT;
672 goto end;
673 }
674 alarm_string[count] = 0;
675 battery->alarm_capacity = simple_strtoul(alarm_string, NULL, 0) /
676 acpi_battery_scale(battery);
677 acpi_battery_set_alarm(battery);
678 end:
679 mutex_unlock(&sbs->lock);
680 if (result)
681 return result;
682 return count;
683 }
684
685 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
686 {
687 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
688 }
689
690 static struct file_operations acpi_battery_info_fops = {
691 .open = acpi_battery_info_open_fs,
692 .read = seq_read,
693 .llseek = seq_lseek,
694 .release = single_release,
695 .owner = THIS_MODULE,
696 };
697
698 static struct file_operations acpi_battery_state_fops = {
699 .open = acpi_battery_state_open_fs,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = single_release,
703 .owner = THIS_MODULE,
704 };
705
706 static struct file_operations acpi_battery_alarm_fops = {
707 .open = acpi_battery_alarm_open_fs,
708 .read = seq_read,
709 .write = acpi_battery_write_alarm,
710 .llseek = seq_lseek,
711 .release = single_release,
712 .owner = THIS_MODULE,
713 };
714
715 /* Legacy AC Adapter Interface */
716
717 static struct proc_dir_entry *acpi_ac_dir = NULL;
718
719 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
720 {
721
722 struct acpi_sbs *sbs = seq->private;
723
724 mutex_lock(&sbs->lock);
725
726 seq_printf(seq, "state: %s\n",
727 sbs->charger_present ? "on-line" : "off-line");
728
729 mutex_unlock(&sbs->lock);
730 return 0;
731 }
732
733 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
734 {
735 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
736 }
737
738 static struct file_operations acpi_ac_state_fops = {
739 .open = acpi_ac_state_open_fs,
740 .read = seq_read,
741 .llseek = seq_lseek,
742 .release = single_release,
743 .owner = THIS_MODULE,
744 };
745
746 #endif
747
748 /* --------------------------------------------------------------------------
749 Driver Interface
750 -------------------------------------------------------------------------- */
751 static int acpi_battery_read(struct acpi_battery *battery)
752 {
753 int result = 0, saved_present = battery->present;
754 u16 state;
755
756 if (battery->sbs->manager_present) {
757 result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
758 ACPI_SBS_MANAGER, 0x01, (u8 *)&state);
759 if (!result)
760 battery->present = state & (1 << battery->id);
761 state &= 0x0fff;
762 state |= 1 << (battery->id + 12);
763 acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD,
764 ACPI_SBS_MANAGER, 0x01, (u8 *)&state, 2);
765 } else if (battery->id == 0)
766 battery->present = 1;
767 if (result || !battery->present)
768 return result;
769
770 if (saved_present != battery->present) {
771 battery->update_time = 0;
772 result = acpi_battery_get_info(battery);
773 if (result)
774 return result;
775 }
776 result = acpi_battery_get_state(battery);
777 return result;
778 }
779
780 /* Smart Battery */
781 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
782 {
783 struct acpi_battery *battery = &sbs->battery[id];
784 int result;
785
786 battery->id = id;
787 battery->sbs = sbs;
788 result = acpi_battery_read(battery);
789 if (result)
790 return result;
791
792 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id);
793 #ifdef CONFIG_ACPI_PROCFS
794 acpi_sbs_add_fs(&battery->proc_entry, acpi_battery_dir,
795 battery->name, &acpi_battery_info_fops,
796 &acpi_battery_state_fops, &acpi_battery_alarm_fops,
797 battery);
798 #endif
799 battery->bat.name = battery->name;
800 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
801 if (!acpi_battery_mode(battery)) {
802 battery->bat.properties = sbs_charge_battery_props;
803 battery->bat.num_properties =
804 ARRAY_SIZE(sbs_charge_battery_props);
805 } else {
806 battery->bat.properties = sbs_energy_battery_props;
807 battery->bat.num_properties =
808 ARRAY_SIZE(sbs_energy_battery_props);
809 }
810 battery->bat.get_property = acpi_sbs_battery_get_property;
811 result = power_supply_register(&sbs->device->dev, &battery->bat);
812 if (result)
813 goto end;
814 result = device_create_file(battery->bat.dev, &alarm_attr);
815 if (result)
816 goto end;
817 battery->have_sysfs_alarm = 1;
818 end:
819 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
820 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
821 battery->name, sbs->battery->present ? "present" : "absent");
822 return result;
823 }
824
825 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
826 {
827 struct acpi_battery *battery = &sbs->battery[id];
828
829 if (battery->bat.dev) {
830 if (battery->have_sysfs_alarm)
831 device_remove_file(battery->bat.dev, &alarm_attr);
832 power_supply_unregister(&battery->bat);
833 }
834 #ifdef CONFIG_ACPI_PROCFS
835 if (battery->proc_entry)
836 acpi_sbs_remove_fs(&battery->proc_entry, acpi_battery_dir);
837 #endif
838 }
839
840 static int acpi_charger_add(struct acpi_sbs *sbs)
841 {
842 int result;
843
844 result = acpi_ac_get_present(sbs);
845 if (result)
846 goto end;
847 #ifdef CONFIG_ACPI_PROCFS
848 result = acpi_sbs_add_fs(&sbs->charger_entry, acpi_ac_dir,
849 ACPI_AC_DIR_NAME, NULL,
850 &acpi_ac_state_fops, NULL, sbs);
851 if (result)
852 goto end;
853 #endif
854 sbs->charger.name = "sbs-charger";
855 sbs->charger.type = POWER_SUPPLY_TYPE_MAINS;
856 sbs->charger.properties = sbs_ac_props;
857 sbs->charger.num_properties = ARRAY_SIZE(sbs_ac_props);
858 sbs->charger.get_property = sbs_get_ac_property;
859 power_supply_register(&sbs->device->dev, &sbs->charger);
860 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
861 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
862 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
863 end:
864 return result;
865 }
866
867 static void acpi_charger_remove(struct acpi_sbs *sbs)
868 {
869 if (sbs->charger.dev)
870 power_supply_unregister(&sbs->charger);
871 #ifdef CONFIG_ACPI_PROCFS
872 if (sbs->charger_entry)
873 acpi_sbs_remove_fs(&sbs->charger_entry, acpi_ac_dir);
874 #endif
875 }
876
877 void acpi_sbs_callback(void *context)
878 {
879 int id;
880 struct acpi_sbs *sbs = context;
881 struct acpi_battery *bat;
882 u8 saved_charger_state = sbs->charger_present;
883 u8 saved_battery_state;
884 acpi_ac_get_present(sbs);
885 if (sbs->charger_present != saved_charger_state) {
886 #ifdef CONFIG_ACPI_PROC_EVENT
887 acpi_bus_generate_proc_event4(ACPI_AC_CLASS, ACPI_AC_DIR_NAME,
888 ACPI_SBS_NOTIFY_STATUS,
889 sbs->charger_present);
890 #endif
891 kobject_uevent(&sbs->charger.dev->kobj, KOBJ_CHANGE);
892 }
893 if (sbs->manager_present) {
894 for (id = 0; id < MAX_SBS_BAT; ++id) {
895 if (!(sbs->batteries_supported & (1 << id)))
896 continue;
897 bat = &sbs->battery[id];
898 saved_battery_state = bat->present;
899 acpi_battery_read(bat);
900 if (saved_battery_state == bat->present)
901 continue;
902 #ifdef CONFIG_ACPI_PROC_EVENT
903 acpi_bus_generate_proc_event4(ACPI_BATTERY_CLASS,
904 bat->name,
905 ACPI_SBS_NOTIFY_STATUS,
906 bat->present);
907 #endif
908 kobject_uevent(&bat->bat.dev->kobj, KOBJ_CHANGE);
909 }
910 }
911 }
912
913 static int acpi_sbs_remove(struct acpi_device *device, int type);
914
915 static int acpi_sbs_add(struct acpi_device *device)
916 {
917 struct acpi_sbs *sbs;
918 int result = 0;
919 int id;
920
921 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
922 if (!sbs) {
923 result = -ENOMEM;
924 goto end;
925 }
926
927 mutex_init(&sbs->lock);
928
929 sbs->hc = acpi_driver_data(device->parent);
930 sbs->device = device;
931 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
932 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
933 acpi_driver_data(device) = sbs;
934
935 result = acpi_charger_add(sbs);
936 if (result)
937 goto end;
938
939 result = acpi_manager_get_info(sbs);
940 if (!result) {
941 sbs->manager_present = 1;
942 for (id = 0; id < MAX_SBS_BAT; ++id)
943 if ((sbs->batteries_supported & (1 << id)))
944 acpi_battery_add(sbs, id);
945 } else
946 acpi_battery_add(sbs, 0);
947 acpi_smbus_register_callback(sbs->hc, acpi_sbs_callback, sbs);
948 end:
949 if (result)
950 acpi_sbs_remove(device, 0);
951 return result;
952 }
953
954 static int acpi_sbs_remove(struct acpi_device *device, int type)
955 {
956 struct acpi_sbs *sbs;
957 int id;
958
959 if (!device)
960 return -EINVAL;
961 sbs = acpi_driver_data(device);
962 if (!sbs)
963 return -EINVAL;
964 mutex_lock(&sbs->lock);
965 acpi_smbus_unregister_callback(sbs->hc);
966 for (id = 0; id < MAX_SBS_BAT; ++id)
967 acpi_battery_remove(sbs, id);
968 acpi_charger_remove(sbs);
969 mutex_unlock(&sbs->lock);
970 mutex_destroy(&sbs->lock);
971 kfree(sbs);
972 return 0;
973 }
974
975 static void acpi_sbs_rmdirs(void)
976 {
977 #ifdef CONFIG_ACPI_PROCFS
978 if (acpi_ac_dir) {
979 acpi_unlock_ac_dir(acpi_ac_dir);
980 acpi_ac_dir = NULL;
981 }
982 if (acpi_battery_dir) {
983 acpi_unlock_battery_dir(acpi_battery_dir);
984 acpi_battery_dir = NULL;
985 }
986 #endif
987 }
988
989 static int acpi_sbs_resume(struct acpi_device *device)
990 {
991 struct acpi_sbs *sbs;
992 if (!device)
993 return -EINVAL;
994 sbs = device->driver_data;
995 acpi_sbs_callback(sbs);
996 return 0;
997 }
998
999 static struct acpi_driver acpi_sbs_driver = {
1000 .name = "sbs",
1001 .class = ACPI_SBS_CLASS,
1002 .ids = sbs_device_ids,
1003 .ops = {
1004 .add = acpi_sbs_add,
1005 .remove = acpi_sbs_remove,
1006 .resume = acpi_sbs_resume,
1007 },
1008 };
1009
1010 static int __init acpi_sbs_init(void)
1011 {
1012 int result = 0;
1013
1014 if (acpi_disabled)
1015 return -ENODEV;
1016 #ifdef CONFIG_ACPI_PROCFS
1017 acpi_ac_dir = acpi_lock_ac_dir();
1018 if (!acpi_ac_dir)
1019 return -ENODEV;
1020 acpi_battery_dir = acpi_lock_battery_dir();
1021 if (!acpi_battery_dir) {
1022 acpi_sbs_rmdirs();
1023 return -ENODEV;
1024 }
1025 #endif
1026 result = acpi_bus_register_driver(&acpi_sbs_driver);
1027 if (result < 0) {
1028 acpi_sbs_rmdirs();
1029 return -ENODEV;
1030 }
1031 return 0;
1032 }
1033
1034 static void __exit acpi_sbs_exit(void)
1035 {
1036 acpi_bus_unregister_driver(&acpi_sbs_driver);
1037 acpi_sbs_rmdirs();
1038 return;
1039 }
1040
1041 module_init(acpi_sbs_init);
1042 module_exit(acpi_sbs_exit);
This page took 0.052076 seconds and 5 git commands to generate.