blob: 3351dea11ff0ed61ec3968e9fd07460ef59ca9ca [file] [log] [blame]
Rich Townsend3f86b832006-07-01 11:36:54 -04001/*
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +04002 * sbs.c - ACPI Smart Battery System Driver ($Revision: 2.0 $)
Rich Townsend3f86b832006-07-01 11:36:54 -04003 *
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +04004 * Copyright (c) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (c) 2005-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
Rich Townsend3f86b832006-07-01 11:36:54 -04006 * 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#include <linux/proc_fs.h>
32#include <linux/seq_file.h>
33#include <asm/uaccess.h>
34#include <linux/acpi.h>
Vladimir Lebedev6d157022007-03-19 17:45:50 +030035#include <linux/timer.h>
Vladimir Lebedev72206232007-03-19 17:45:50 +030036#include <linux/jiffies.h>
Rich Townsend3f86b832006-07-01 11:36:54 -040037#include <linux/delay.h>
38
Alexey Starikovskiy91087df2007-09-26 19:43:28 +040039#include "sbshc.h"
40
Rich Townsend3f86b832006-07-01 11:36:54 -040041#define ACPI_SBS_COMPONENT 0x00080000
42#define ACPI_SBS_CLASS "sbs"
43#define ACPI_AC_CLASS "ac_adapter"
44#define ACPI_BATTERY_CLASS "battery"
Rich Townsend3f86b832006-07-01 11:36:54 -040045#define ACPI_SBS_DEVICE_NAME "Smart Battery System"
46#define ACPI_SBS_FILE_INFO "info"
47#define ACPI_SBS_FILE_STATE "state"
48#define ACPI_SBS_FILE_ALARM "alarm"
49#define ACPI_BATTERY_DIR_NAME "BAT%i"
50#define ACPI_AC_DIR_NAME "AC0"
Rich Townsend3f86b832006-07-01 11:36:54 -040051
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040052enum acpi_sbs_device_addr {
53 ACPI_SBS_CHARGER = 0x9,
54 ACPI_SBS_MANAGER = 0xa,
55 ACPI_SBS_BATTERY = 0xb,
56};
57
58#define ACPI_SBS_NOTIFY_STATUS 0x80
59#define ACPI_SBS_NOTIFY_INFO 0x81
Rich Townsend3f86b832006-07-01 11:36:54 -040060
Len Brownf52fd662007-02-12 22:42:12 -050061ACPI_MODULE_NAME("sbs");
Rich Townsend3f86b832006-07-01 11:36:54 -040062
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040063MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Rich Townsend3f86b832006-07-01 11:36:54 -040064MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
65MODULE_LICENSE("GPL");
66
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040067static unsigned int cache_time = 1000;
68module_param(cache_time, uint, 0644);
69MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedev6d157022007-03-19 17:45:50 +030070
71extern struct proc_dir_entry *acpi_lock_ac_dir(void);
72extern struct proc_dir_entry *acpi_lock_battery_dir(void);
73extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
74extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
75
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040076#define MAX_SBS_BAT 4
Vladimir Lebedev6d157022007-03-19 17:45:50 +030077#define ACPI_SBS_BLOCK_MAX 32
78
Rich Townsend3f86b832006-07-01 11:36:54 -040079static int acpi_sbs_add(struct acpi_device *device);
80static int acpi_sbs_remove(struct acpi_device *device, int type);
Vladimir Lebedev72206232007-03-19 17:45:50 +030081static int acpi_sbs_resume(struct acpi_device *device);
Rich Townsend3f86b832006-07-01 11:36:54 -040082
Thomas Renninger1ba90e32007-07-23 14:44:41 +020083static const struct acpi_device_id sbs_device_ids[] = {
Alexey Starikovskiy91087df2007-09-26 19:43:28 +040084 {"ACPI0002", 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +020085 {"", 0},
86};
87MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
88
Rich Townsend3f86b832006-07-01 11:36:54 -040089static struct acpi_driver acpi_sbs_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050090 .name = "sbs",
Rich Townsend3f86b832006-07-01 11:36:54 -040091 .class = ACPI_SBS_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020092 .ids = sbs_device_ids,
Rich Townsend3f86b832006-07-01 11:36:54 -040093 .ops = {
94 .add = acpi_sbs_add,
95 .remove = acpi_sbs_remove,
Vladimir Lebedev72206232007-03-19 17:45:50 +030096 .resume = acpi_sbs_resume,
Rich Townsend3f86b832006-07-01 11:36:54 -040097 },
98};
99
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400100struct acpi_battery {
101 struct acpi_sbs *sbs;
102 struct proc_dir_entry *proc_entry;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400103 unsigned long update_time;
104 char name[8];
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400105 char manufacturer_name[ACPI_SBS_BLOCK_MAX];
106 char device_name[ACPI_SBS_BLOCK_MAX];
107 char device_chemistry[ACPI_SBS_BLOCK_MAX];
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400108 u32 alarm_capacity;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400109 u16 full_charge_capacity;
110 u16 design_capacity;
111 u16 design_voltage;
112 u16 serial_number;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400113 u16 cycle_count;
114 u16 temp_now;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400115 u16 voltage_now;
116 s16 current_now;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400117 s16 current_avg;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400118 u16 capacity_now;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400119 u16 state_of_charge;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400120 u16 state;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400121 u16 mode;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400122 u16 spec;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400123 u8 id;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400124 u8 present:1;
Rich Townsend3f86b832006-07-01 11:36:54 -0400125};
126
127struct acpi_sbs {
Rich Townsend3f86b832006-07-01 11:36:54 -0400128 struct acpi_device *device;
Alexey Starikovskiy91087df2007-09-26 19:43:28 +0400129 struct acpi_smb_hc *hc;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400130 struct mutex lock;
131 struct proc_dir_entry *charger_entry;
Rich Townsend3f86b832006-07-01 11:36:54 -0400132 struct acpi_battery battery[MAX_SBS_BAT];
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400133 u8 batteries_supported:4;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400134 u8 manager_present:1;
135 u8 charger_present:1;
Rich Townsend3f86b832006-07-01 11:36:54 -0400136};
137
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400138static inline int battery_scale(int log)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300139{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400140 int scale = 1;
141 while (log--)
142 scale *= 10;
143 return scale;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300144}
145
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400146static inline int acpi_battery_vscale(struct acpi_battery *battery)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300147{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400148 return battery_scale((battery->spec & 0x0f00) >> 8);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300149}
150
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400151static inline int acpi_battery_ipscale(struct acpi_battery *battery)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300152{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400153 return battery_scale((battery->spec & 0xf000) >> 12);
154}
155
156static inline int acpi_battery_mode(struct acpi_battery *battery)
157{
158 return (battery->mode & 0x8000);
159}
160
161static inline int acpi_battery_scale(struct acpi_battery *battery)
162{
163 return (acpi_battery_mode(battery) ? 10 : 1) *
164 acpi_battery_ipscale(battery);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300165}
166
Rich Townsend3f86b832006-07-01 11:36:54 -0400167/* --------------------------------------------------------------------------
168 Smart Battery System Management
169 -------------------------------------------------------------------------- */
170
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400171struct acpi_battery_reader {
172 u8 command; /* command for battery */
173 u8 mode; /* word or block? */
174 size_t offset; /* offset inside struct acpi_sbs_battery */
175};
Vladimir Lebedev72206232007-03-19 17:45:50 +0300176
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400177static struct acpi_battery_reader info_readers[] = {
178 {0x01, SMBUS_READ_WORD, offsetof(struct acpi_battery, alarm_capacity)},
179 {0x03, SMBUS_READ_WORD, offsetof(struct acpi_battery, mode)},
180 {0x10, SMBUS_READ_WORD, offsetof(struct acpi_battery, full_charge_capacity)},
181 {0x17, SMBUS_READ_WORD, offsetof(struct acpi_battery, cycle_count)},
182 {0x18, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_capacity)},
183 {0x19, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_voltage)},
184 {0x1a, SMBUS_READ_WORD, offsetof(struct acpi_battery, spec)},
185 {0x1c, SMBUS_READ_WORD, offsetof(struct acpi_battery, serial_number)},
186 {0x20, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, manufacturer_name)},
187 {0x21, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_name)},
188 {0x22, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_chemistry)},
189};
Rich Townsend3f86b832006-07-01 11:36:54 -0400190
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400191static struct acpi_battery_reader state_readers[] = {
192 {0x08, SMBUS_READ_WORD, offsetof(struct acpi_battery, temp_now)},
193 {0x09, SMBUS_READ_WORD, offsetof(struct acpi_battery, voltage_now)},
194 {0x0a, SMBUS_READ_WORD, offsetof(struct acpi_battery, current_now)},
195 {0x0b, SMBUS_READ_WORD, offsetof(struct acpi_battery, current_avg)},
196 {0x0f, SMBUS_READ_WORD, offsetof(struct acpi_battery, capacity_now)},
197 {0x0e, SMBUS_READ_WORD, offsetof(struct acpi_battery, state_of_charge)},
198 {0x16, SMBUS_READ_WORD, offsetof(struct acpi_battery, state)},
199};
Rich Townsend3f86b832006-07-01 11:36:54 -0400200
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400201static int acpi_manager_get_info(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400202{
Rich Townsend3f86b832006-07-01 11:36:54 -0400203 int result = 0;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400204 u16 battery_system_info;
Rich Townsend3f86b832006-07-01 11:36:54 -0400205
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400206 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
207 0x04, (u8 *) & battery_system_info);
208 if (!result)
209 sbs->batteries_supported = battery_system_info & 0x000f;
Len Brown635227e2006-07-01 16:48:23 -0400210 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400211}
212
213static int acpi_battery_get_info(struct acpi_battery *battery)
214{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400215 int i, result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400216
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400217 for (i = 0; i < ARRAY_SIZE(info_readers); ++i) {
218 result = acpi_smbus_read(battery->sbs->hc, info_readers[i].mode,
219 ACPI_SBS_BATTERY, info_readers[i].command,
220 (u8 *) battery + info_readers[i].offset);
221 if (result)
222 break;
Rich Townsend3f86b832006-07-01 11:36:54 -0400223 }
Len Brown635227e2006-07-01 16:48:23 -0400224 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400225}
226
Rich Townsend3f86b832006-07-01 11:36:54 -0400227static int acpi_battery_get_state(struct acpi_battery *battery)
228{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400229 int i, result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400230
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400231 if (time_before(jiffies, battery->update_time +
232 msecs_to_jiffies(cache_time)))
233 return 0;
234 for (i = 0; i < ARRAY_SIZE(state_readers); ++i) {
235 result = acpi_smbus_read(battery->sbs->hc,
236 state_readers[i].mode,
237 ACPI_SBS_BATTERY,
238 state_readers[i].command,
239 (u8 *)battery +
240 state_readers[i].offset);
241 if (result)
242 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400243 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400244 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400245 battery->update_time = jiffies;
Len Brown635227e2006-07-01 16:48:23 -0400246 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400247}
248
249static int acpi_battery_get_alarm(struct acpi_battery *battery)
250{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400251 return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
252 ACPI_SBS_BATTERY, 0x01,
253 (u8 *) & battery->alarm_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400254}
255
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400256static int acpi_battery_set_alarm(struct acpi_battery *battery)
Rich Townsend3f86b832006-07-01 11:36:54 -0400257{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400258 struct acpi_sbs *sbs = battery->sbs;
259 u16 value;
260 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400261
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400262 if (sbs->manager_present) {
263 acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
264 0x01, (u8 *)&value);
265 value &= 0x0fff;
266 value |= 1 << (battery->id + 12);
267 acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_MANAGER,
268 0x01, (u8 *)&value, 2);
Rich Townsend3f86b832006-07-01 11:36:54 -0400269 }
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400270 value = battery->alarm_capacity / (acpi_battery_mode(battery) ? 10 : 1);
271 return acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY,
272 0x01, (u8 *)&value, 2);
Rich Townsend3f86b832006-07-01 11:36:54 -0400273}
274
275static int acpi_ac_get_present(struct acpi_sbs *sbs)
276{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400277 int result;
278 u16 status;
Rich Townsend3f86b832006-07-01 11:36:54 -0400279
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400280 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER,
281 0x13, (u8 *) & status);
282 if (!result)
283 sbs->charger_present = (status >> 15) & 0x1;
Len Brown635227e2006-07-01 16:48:23 -0400284 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400285}
286
287/* --------------------------------------------------------------------------
288 FS Interface (/proc/acpi)
289 -------------------------------------------------------------------------- */
290
291/* Generic Routines */
292
293static int
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400294acpi_sbs_add_fs(struct proc_dir_entry **dir,
Rich Townsend3f86b832006-07-01 11:36:54 -0400295 struct proc_dir_entry *parent_dir,
296 char *dir_name,
297 struct file_operations *info_fops,
298 struct file_operations *state_fops,
299 struct file_operations *alarm_fops, void *data)
300{
301 struct proc_dir_entry *entry = NULL;
302
Rich Townsend3f86b832006-07-01 11:36:54 -0400303 if (!*dir) {
304 *dir = proc_mkdir(dir_name, parent_dir);
305 if (!*dir) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300306 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
307 "proc_mkdir() failed"));
Len Brown635227e2006-07-01 16:48:23 -0400308 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400309 }
310 (*dir)->owner = THIS_MODULE;
311 }
312
313 /* 'info' [R] */
314 if (info_fops) {
315 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
316 if (!entry) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300317 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
318 "create_proc_entry() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400319 } else {
320 entry->proc_fops = info_fops;
321 entry->data = data;
322 entry->owner = THIS_MODULE;
323 }
324 }
325
326 /* 'state' [R] */
327 if (state_fops) {
328 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
329 if (!entry) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300330 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
331 "create_proc_entry() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400332 } else {
333 entry->proc_fops = state_fops;
334 entry->data = data;
335 entry->owner = THIS_MODULE;
336 }
337 }
338
339 /* 'alarm' [R/W] */
340 if (alarm_fops) {
341 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
342 if (!entry) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300343 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
344 "create_proc_entry() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400345 } else {
346 entry->proc_fops = alarm_fops;
347 entry->data = data;
348 entry->owner = THIS_MODULE;
349 }
350 }
351
Len Brown635227e2006-07-01 16:48:23 -0400352 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400353}
354
355static void
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400356acpi_sbs_remove_fs(struct proc_dir_entry **dir,
Rich Townsend3f86b832006-07-01 11:36:54 -0400357 struct proc_dir_entry *parent_dir)
358{
Rich Townsend3f86b832006-07-01 11:36:54 -0400359
360 if (*dir) {
361 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
362 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
363 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
364 remove_proc_entry((*dir)->name, parent_dir);
365 *dir = NULL;
366 }
367
368}
369
370/* Smart Battery Interface */
371
372static struct proc_dir_entry *acpi_battery_dir = NULL;
373
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400374static inline char *acpi_battery_units(struct acpi_battery *battery)
375{
376 return acpi_battery_mode(battery) ? " mWh" : " mAh";
377}
378
379
Rich Townsend3f86b832006-07-01 11:36:54 -0400380static int acpi_battery_read_info(struct seq_file *seq, void *offset)
381{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200382 struct acpi_battery *battery = seq->private;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300383 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400384 int result = 0;
385
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400386 mutex_lock(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400387
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400388 seq_printf(seq, "present: %s\n",
389 (battery->present) ? "yes" : "no");
390 if (!battery->present)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300391 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400392
Vladimir Lebedev72206232007-03-19 17:45:50 +0300393 seq_printf(seq, "design capacity: %i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400394 battery->design_capacity * acpi_battery_scale(battery),
395 acpi_battery_units(battery));
Vladimir Lebedev72206232007-03-19 17:45:50 +0300396 seq_printf(seq, "last full capacity: %i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400397 battery->full_charge_capacity * acpi_battery_scale(battery),
398 acpi_battery_units(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400399 seq_printf(seq, "battery technology: rechargeable\n");
Rich Townsend3f86b832006-07-01 11:36:54 -0400400 seq_printf(seq, "design voltage: %i mV\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400401 battery->design_voltage * acpi_battery_vscale(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400402 seq_printf(seq, "design capacity warning: unknown\n");
403 seq_printf(seq, "design capacity low: unknown\n");
404 seq_printf(seq, "capacity granularity 1: unknown\n");
405 seq_printf(seq, "capacity granularity 2: unknown\n");
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400406 seq_printf(seq, "model number: %s\n", battery->device_name);
Rich Townsend3f86b832006-07-01 11:36:54 -0400407 seq_printf(seq, "serial number: %i\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400408 battery->serial_number);
Rich Townsend3f86b832006-07-01 11:36:54 -0400409 seq_printf(seq, "battery type: %s\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400410 battery->device_chemistry);
Rich Townsend3f86b832006-07-01 11:36:54 -0400411 seq_printf(seq, "OEM info: %s\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400412 battery->manufacturer_name);
Rich Townsend3f86b832006-07-01 11:36:54 -0400413 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400414 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400415 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400416}
417
418static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
419{
420 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
421}
422
423static int acpi_battery_read_state(struct seq_file *seq, void *offset)
424{
Vladimir Lebedev72206232007-03-19 17:45:50 +0300425 struct acpi_battery *battery = seq->private;
426 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400427 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400428
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400429 mutex_lock(&sbs->lock);
430 seq_printf(seq, "present: %s\n",
431 (battery->present) ? "yes" : "no");
432 if (!battery->present)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300433 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400434
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400435 acpi_battery_get_state(battery);
436 seq_printf(seq, "capacity state: %s\n",
437 (battery->state & 0x0010) ? "critical" : "ok");
438 seq_printf(seq, "charging state: %s\n",
439 (battery->current_now < 0) ? "discharging" :
440 ((battery->current_now > 0) ? "charging" : "charged"));
441 seq_printf(seq, "present rate: %d mA\n",
442 abs(battery->current_now) * acpi_battery_ipscale(battery));
Vladimir Lebedev72206232007-03-19 17:45:50 +0300443 seq_printf(seq, "remaining capacity: %i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400444 battery->capacity_now * acpi_battery_scale(battery),
445 acpi_battery_units(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400446 seq_printf(seq, "present voltage: %i mV\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400447 battery->voltage_now * acpi_battery_vscale(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400448
449 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400450 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400451 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400452}
453
454static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
455{
456 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
457}
458
459static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
460{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200461 struct acpi_battery *battery = seq->private;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300462 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400463 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400464
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400465 mutex_lock(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400466
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400467 if (!battery->present) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400468 seq_printf(seq, "present: no\n");
469 goto end;
470 }
471
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400472 acpi_battery_get_alarm(battery);
Rich Townsend3f86b832006-07-01 11:36:54 -0400473 seq_printf(seq, "alarm: ");
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400474 if (battery->alarm_capacity)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300475 seq_printf(seq, "%i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400476 battery->alarm_capacity *
477 acpi_battery_scale(battery),
478 acpi_battery_units(battery));
479 else
Rich Townsend3f86b832006-07-01 11:36:54 -0400480 seq_printf(seq, "disabled\n");
Rich Townsend3f86b832006-07-01 11:36:54 -0400481 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400482 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400483 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400484}
485
486static ssize_t
487acpi_battery_write_alarm(struct file *file, const char __user * buffer,
488 size_t count, loff_t * ppos)
489{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200490 struct seq_file *seq = file->private_data;
491 struct acpi_battery *battery = seq->private;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300492 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400493 char alarm_string[12] = { '\0' };
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400494 int result = 0;
495 mutex_lock(&sbs->lock);
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400496 if (!battery->present) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400497 result = -ENODEV;
498 goto end;
499 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400500 if (count > sizeof(alarm_string) - 1) {
501 result = -EINVAL;
502 goto end;
503 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400504 if (copy_from_user(alarm_string, buffer, count)) {
505 result = -EFAULT;
506 goto end;
507 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400508 alarm_string[count] = 0;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400509 battery->alarm_capacity = simple_strtoul(alarm_string, NULL, 0);
510 acpi_battery_set_alarm(battery);
Rich Townsend3f86b832006-07-01 11:36:54 -0400511 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400512 mutex_unlock(&sbs->lock);
513 if (result)
Len Brown635227e2006-07-01 16:48:23 -0400514 return result;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400515 return count;
Rich Townsend3f86b832006-07-01 11:36:54 -0400516}
517
518static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
519{
520 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
521}
522
523static struct file_operations acpi_battery_info_fops = {
524 .open = acpi_battery_info_open_fs,
525 .read = seq_read,
526 .llseek = seq_lseek,
527 .release = single_release,
528 .owner = THIS_MODULE,
529};
530
531static struct file_operations acpi_battery_state_fops = {
532 .open = acpi_battery_state_open_fs,
533 .read = seq_read,
534 .llseek = seq_lseek,
535 .release = single_release,
536 .owner = THIS_MODULE,
537};
538
539static struct file_operations acpi_battery_alarm_fops = {
540 .open = acpi_battery_alarm_open_fs,
541 .read = seq_read,
542 .write = acpi_battery_write_alarm,
543 .llseek = seq_lseek,
544 .release = single_release,
545 .owner = THIS_MODULE,
546};
547
548/* Legacy AC Adapter Interface */
549
550static struct proc_dir_entry *acpi_ac_dir = NULL;
551
552static int acpi_ac_read_state(struct seq_file *seq, void *offset)
553{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400554
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200555 struct acpi_sbs *sbs = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -0400556
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400557 mutex_lock(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400558
559 seq_printf(seq, "state: %s\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400560 sbs->charger_present ? "on-line" : "off-line");
Rich Townsend3f86b832006-07-01 11:36:54 -0400561
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400562 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400563 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400564}
565
566static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
567{
568 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
569}
570
571static struct file_operations acpi_ac_state_fops = {
572 .open = acpi_ac_state_open_fs,
573 .read = seq_read,
574 .llseek = seq_lseek,
575 .release = single_release,
576 .owner = THIS_MODULE,
577};
578
579/* --------------------------------------------------------------------------
580 Driver Interface
581 -------------------------------------------------------------------------- */
582
583/* Smart Battery */
584
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400585static int acpi_battery_read(struct acpi_battery *battery)
586{
587 int result = 0, saved_present = battery->present;
588 u16 state;
589
590 if (battery->sbs->manager_present) {
591 result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
592 ACPI_SBS_MANAGER, 0x01, (u8 *)&state);
593 if (!result)
594 battery->present = state & (1 << battery->id);
595 state &= 0x0fff;
596 state |= 1 << (battery->id + 12);
597 acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD,
598 ACPI_SBS_MANAGER, 0x01, (u8 *)&state, 2);
599 } else if (battery->id == 0)
600 battery->present = 1;
601 if (result || !battery->present)
602 return result;
603
604 if (saved_present != battery->present) {
605 battery->update_time = 0;
606 result = acpi_battery_get_info(battery);
607 if (result)
608 return result;
609 }
610 result = acpi_battery_get_state(battery);
611 return result;
612}
613
Rich Townsend3f86b832006-07-01 11:36:54 -0400614static int acpi_battery_add(struct acpi_sbs *sbs, int id)
615{
Rich Townsend3f86b832006-07-01 11:36:54 -0400616 int result;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400617 struct acpi_battery *battery = &sbs->battery[id];
Rich Townsend3f86b832006-07-01 11:36:54 -0400618 battery->id = id;
619 battery->sbs = sbs;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400620 battery->update_time = 0;
621 result = acpi_battery_read(battery);
622 if (result)
623 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400624
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400625 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id);
626 acpi_sbs_add_fs(&battery->proc_entry, acpi_battery_dir,
627 battery->name, &acpi_battery_info_fops,
628 &acpi_battery_state_fops, &acpi_battery_alarm_fops,
629 battery);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300630 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400631 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
632 battery->name, sbs->battery->present ? "present" : "absent");
Len Brown635227e2006-07-01 16:48:23 -0400633 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400634}
635
636static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
637{
Rich Townsend3f86b832006-07-01 11:36:54 -0400638
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400639 if (sbs->battery[id].proc_entry) {
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400640 acpi_sbs_remove_fs(&(sbs->battery[id].proc_entry),
641 acpi_battery_dir);
Rich Townsend3f86b832006-07-01 11:36:54 -0400642 }
643}
644
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400645static int acpi_charger_add(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400646{
647 int result;
648
Rich Townsend3f86b832006-07-01 11:36:54 -0400649 result = acpi_ac_get_present(sbs);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400650 if (result)
Rich Townsend3f86b832006-07-01 11:36:54 -0400651 goto end;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400652 result = acpi_sbs_add_fs(&sbs->charger_entry, acpi_ac_dir,
653 ACPI_AC_DIR_NAME, NULL,
654 &acpi_ac_state_fops, NULL, sbs);
655 if (result)
Rich Townsend3f86b832006-07-01 11:36:54 -0400656 goto end;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300657 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
658 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400659 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
Rich Townsend3f86b832006-07-01 11:36:54 -0400660 end:
Len Brown635227e2006-07-01 16:48:23 -0400661 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400662}
663
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400664static void acpi_charger_remove(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400665{
Rich Townsend3f86b832006-07-01 11:36:54 -0400666
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400667 if (sbs->charger_entry)
668 acpi_sbs_remove_fs(&sbs->charger_entry, acpi_ac_dir);
669}
670
671void acpi_sbs_callback(void *context)
672{
673 int id;
674 struct acpi_sbs *sbs = context;
675 struct acpi_battery *bat;
676 u8 saved_charger_state = sbs->charger_present;
677 u8 saved_battery_state;
678 acpi_ac_get_present(sbs);
679 if (sbs->charger_present != saved_charger_state) {
680 acpi_bus_generate_proc_event4(ACPI_AC_CLASS, ACPI_AC_DIR_NAME,
681 ACPI_SBS_NOTIFY_STATUS,
682 sbs->charger_present);
683 }
684 if (sbs->manager_present) {
685 for (id = 0; id < MAX_SBS_BAT; ++id) {
686 if (!(sbs->batteries_supported & (1 << id)))
687 continue;
688 bat = &sbs->battery[id];
689 saved_battery_state = bat->present;
690 acpi_battery_read(bat);
691 if (saved_battery_state == bat->present)
692 continue;
693 acpi_bus_generate_proc_event4(ACPI_BATTERY_CLASS,
694 bat->name,
695 ACPI_SBS_NOTIFY_STATUS,
696 bat->present);
697 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400698 }
699}
700
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400701static int acpi_sbs_remove(struct acpi_device *device, int type);
Rich Townsend3f86b832006-07-01 11:36:54 -0400702
703static int acpi_sbs_add(struct acpi_device *device)
704{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400705 struct acpi_sbs *sbs;
706 int result = 0;
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300707 int id;
Rich Townsend3f86b832006-07-01 11:36:54 -0400708
Burman Yan36bcbec2006-12-19 12:56:11 -0800709 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -0400710 if (!sbs) {
Vladimir Lebedev72206232007-03-19 17:45:50 +0300711 result = -ENOMEM;
712 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400713 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400714
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400715 mutex_init(&sbs->lock);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300716
Alexey Starikovskiy91087df2007-09-26 19:43:28 +0400717 sbs->hc = acpi_driver_data(device->parent);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400718 sbs->device = device;
Rich Townsend3f86b832006-07-01 11:36:54 -0400719 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
720 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
721 acpi_driver_data(device) = sbs;
722
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400723 result = acpi_charger_add(sbs);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300724 if (result)
725 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400726
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400727 result = acpi_manager_get_info(sbs);
728 if (!result) {
729 sbs->manager_present = 1;
730 for (id = 0; id < MAX_SBS_BAT; ++id)
731 if ((sbs->batteries_supported & (1 << id)))
732 acpi_battery_add(sbs, id);
733 } else
734 acpi_battery_add(sbs, 0);
735 acpi_smbus_register_callback(sbs->hc, acpi_sbs_callback, sbs);
Rich Townsend3f86b832006-07-01 11:36:54 -0400736 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400737 if (result)
738 acpi_sbs_remove(device, 0);
Len Brown635227e2006-07-01 16:48:23 -0400739 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400740}
741
Vladimir Lebedev72206232007-03-19 17:45:50 +0300742static int acpi_sbs_remove(struct acpi_device *device, int type)
Rich Townsend3f86b832006-07-01 11:36:54 -0400743{
Len Browncece9012006-12-16 01:04:27 -0500744 struct acpi_sbs *sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400745 int id;
746
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400747 if (!device)
Lebedev, Vladimir P963497c2006-09-05 19:49:13 +0400748 return -EINVAL;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300749 sbs = acpi_driver_data(device);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400750 if (!sbs)
Len Brown635227e2006-07-01 16:48:23 -0400751 return -EINVAL;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400752 mutex_lock(&sbs->lock);
753 acpi_smbus_unregister_callback(sbs->hc);
754 for (id = 0; id < MAX_SBS_BAT; ++id)
Rich Townsend3f86b832006-07-01 11:36:54 -0400755 acpi_battery_remove(sbs, id);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400756 acpi_charger_remove(sbs);
757 mutex_unlock(&sbs->lock);
758 mutex_destroy(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400759 kfree(sbs);
Len Brown635227e2006-07-01 16:48:23 -0400760 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400761}
762
Vladimir Lebedev72206232007-03-19 17:45:50 +0300763static void acpi_sbs_rmdirs(void)
764{
765 if (acpi_ac_dir) {
766 acpi_unlock_ac_dir(acpi_ac_dir);
767 acpi_ac_dir = NULL;
768 }
769 if (acpi_battery_dir) {
770 acpi_unlock_battery_dir(acpi_battery_dir);
771 acpi_battery_dir = NULL;
772 }
773}
774
775static int acpi_sbs_resume(struct acpi_device *device)
776{
777 struct acpi_sbs *sbs;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300778 if (!device)
779 return -EINVAL;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300780 sbs = device->driver_data;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400781 acpi_sbs_callback(sbs);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300782 return 0;
783}
784
Rich Townsend3f86b832006-07-01 11:36:54 -0400785static int __init acpi_sbs_init(void)
786{
787 int result = 0;
788
Len Brownb20d2ae2006-08-15 23:21:37 -0400789 if (acpi_disabled)
790 return -ENODEV;
791
Rich Townsend3f86b832006-07-01 11:36:54 -0400792 acpi_ac_dir = acpi_lock_ac_dir();
793 if (!acpi_ac_dir) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300794 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
795 "acpi_lock_ac_dir() failed"));
Len Brown635227e2006-07-01 16:48:23 -0400796 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400797 }
798
799 acpi_battery_dir = acpi_lock_battery_dir();
800 if (!acpi_battery_dir) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300801 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
802 "acpi_lock_battery_dir() failed"));
Vladimir Lebedev72206232007-03-19 17:45:50 +0300803 acpi_sbs_rmdirs();
Len Brown635227e2006-07-01 16:48:23 -0400804 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400805 }
806
807 result = acpi_bus_register_driver(&acpi_sbs_driver);
808 if (result < 0) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300809 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
810 "acpi_bus_register_driver() failed"));
Vladimir Lebedev72206232007-03-19 17:45:50 +0300811 acpi_sbs_rmdirs();
Len Brown635227e2006-07-01 16:48:23 -0400812 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400813 }
814
Len Brown635227e2006-07-01 16:48:23 -0400815 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400816}
817
818static void __exit acpi_sbs_exit(void)
819{
Rich Townsend3f86b832006-07-01 11:36:54 -0400820 acpi_bus_unregister_driver(&acpi_sbs_driver);
821
Vladimir Lebedev72206232007-03-19 17:45:50 +0300822 acpi_sbs_rmdirs();
Rich Townsend3f86b832006-07-01 11:36:54 -0400823
Len Brown635227e2006-07-01 16:48:23 -0400824 return;
Rich Townsend3f86b832006-07-01 11:36:54 -0400825}
826
827module_init(acpi_sbs_init);
828module_exit(acpi_sbs_exit);