Commit | Line | Data |
---|---|---|
3f86b832 RT |
1 | /* |
2 | * acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $) | |
3 | * | |
4 | * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu> | |
5 | * | |
6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or (at | |
11 | * your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | * | |
22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
23 | */ | |
24 | ||
25 | #include <linux/init.h> | |
26 | #include <linux/module.h> | |
27 | #include <linux/moduleparam.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/proc_fs.h> | |
30 | #include <linux/seq_file.h> | |
31 | #include <asm/uaccess.h> | |
32 | #include <linux/acpi.h> | |
6d15702c | 33 | #include <linux/timer.h> |
3f86b832 RT |
34 | #include <linux/delay.h> |
35 | ||
3f86b832 RT |
36 | #define ACPI_SBS_COMPONENT 0x00080000 |
37 | #define ACPI_SBS_CLASS "sbs" | |
38 | #define ACPI_AC_CLASS "ac_adapter" | |
39 | #define ACPI_BATTERY_CLASS "battery" | |
40 | #define ACPI_SBS_HID "ACPI0002" | |
3f86b832 RT |
41 | #define ACPI_SBS_DEVICE_NAME "Smart Battery System" |
42 | #define ACPI_SBS_FILE_INFO "info" | |
43 | #define ACPI_SBS_FILE_STATE "state" | |
44 | #define ACPI_SBS_FILE_ALARM "alarm" | |
45 | #define ACPI_BATTERY_DIR_NAME "BAT%i" | |
46 | #define ACPI_AC_DIR_NAME "AC0" | |
47 | #define ACPI_SBC_SMBUS_ADDR 0x9 | |
48 | #define ACPI_SBSM_SMBUS_ADDR 0xa | |
49 | #define ACPI_SB_SMBUS_ADDR 0xb | |
50 | #define ACPI_SBS_AC_NOTIFY_STATUS 0x80 | |
51 | #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80 | |
52 | #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81 | |
53 | ||
54 | #define _COMPONENT ACPI_SBS_COMPONENT | |
55 | ||
f52fd66d | 56 | ACPI_MODULE_NAME("sbs"); |
3f86b832 RT |
57 | |
58 | MODULE_AUTHOR("Rich Townsend"); | |
59 | MODULE_DESCRIPTION("Smart Battery System ACPI interface driver"); | |
60 | MODULE_LICENSE("GPL"); | |
61 | ||
6d15702c VL |
62 | #define xmsleep(t) msleep(t) |
63 | ||
64 | #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */ | |
65 | ||
66 | #define ACPI_EC_SMB_STS 0x01 /* status */ | |
67 | #define ACPI_EC_SMB_ADDR 0x02 /* address */ | |
68 | #define ACPI_EC_SMB_CMD 0x03 /* command */ | |
69 | #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */ | |
70 | #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */ | |
71 | ||
72 | #define ACPI_EC_SMB_STS_DONE 0x80 | |
73 | #define ACPI_EC_SMB_STS_STATUS 0x1f | |
74 | ||
75 | #define ACPI_EC_SMB_PRTCL_WRITE 0x00 | |
76 | #define ACPI_EC_SMB_PRTCL_READ 0x01 | |
77 | #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08 | |
78 | #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a | |
79 | ||
80 | #define ACPI_EC_SMB_TRANSACTION_SLEEP 1 | |
81 | #define ACPI_EC_SMB_ACCESS_SLEEP1 1 | |
82 | #define ACPI_EC_SMB_ACCESS_SLEEP2 10 | |
83 | ||
84 | #define DEF_CAPACITY_UNIT 3 | |
85 | #define MAH_CAPACITY_UNIT 1 | |
86 | #define MWH_CAPACITY_UNIT 2 | |
87 | #define CAPACITY_UNIT DEF_CAPACITY_UNIT | |
88 | ||
89 | #define REQUEST_UPDATE_MODE 1 | |
90 | #define QUEUE_UPDATE_MODE 2 | |
91 | ||
92 | #define DATA_TYPE_COMMON 0 | |
93 | #define DATA_TYPE_INFO 1 | |
94 | #define DATA_TYPE_STATE 2 | |
95 | #define DATA_TYPE_ALARM 3 | |
96 | #define DATA_TYPE_AC_STATE 4 | |
97 | ||
98 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); | |
99 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); | |
100 | extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); | |
101 | extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
102 | ||
103 | #define MAX_SBS_BAT 4 | |
104 | #define ACPI_SBS_BLOCK_MAX 32 | |
105 | ||
106 | #define ACPI_SBS_SMBUS_READ 1 | |
107 | #define ACPI_SBS_SMBUS_WRITE 2 | |
108 | ||
109 | #define ACPI_SBS_WORD_DATA 1 | |
110 | #define ACPI_SBS_BLOCK_DATA 2 | |
111 | ||
3f86b832 RT |
112 | static struct semaphore sbs_sem; |
113 | ||
114 | #define UPDATE_MODE QUEUE_UPDATE_MODE | |
115 | /* REQUEST_UPDATE_MODE QUEUE_UPDATE_MODE */ | |
116 | #define UPDATE_INFO_MODE 0 | |
117 | #define UPDATE_TIME 60 | |
118 | #define UPDATE_TIME2 0 | |
119 | ||
120 | static int capacity_mode = CAPACITY_UNIT; | |
121 | static int update_mode = UPDATE_MODE; | |
122 | static int update_info_mode = UPDATE_INFO_MODE; | |
123 | static int update_time = UPDATE_TIME; | |
124 | static int update_time2 = UPDATE_TIME2; | |
125 | ||
3cd5b87d LV |
126 | module_param(capacity_mode, int, 0); |
127 | module_param(update_mode, int, 0); | |
128 | module_param(update_info_mode, int, 0); | |
129 | module_param(update_time, int, 0); | |
130 | module_param(update_time2, int, 0); | |
3f86b832 RT |
131 | |
132 | static int acpi_sbs_add(struct acpi_device *device); | |
133 | static int acpi_sbs_remove(struct acpi_device *device, int type); | |
3f86b832 RT |
134 | static void acpi_sbs_update_queue(void *data); |
135 | ||
136 | static struct acpi_driver acpi_sbs_driver = { | |
c2b6705b | 137 | .name = "sbs", |
3f86b832 RT |
138 | .class = ACPI_SBS_CLASS, |
139 | .ids = ACPI_SBS_HID, | |
140 | .ops = { | |
141 | .add = acpi_sbs_add, | |
142 | .remove = acpi_sbs_remove, | |
143 | }, | |
144 | }; | |
145 | ||
146 | struct acpi_battery_info { | |
147 | int capacity_mode; | |
148 | s16 full_charge_capacity; | |
149 | s16 design_capacity; | |
150 | s16 design_voltage; | |
151 | int vscale; | |
152 | int ipscale; | |
153 | s16 serial_number; | |
6d15702c VL |
154 | char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3]; |
155 | char device_name[ACPI_SBS_BLOCK_MAX + 3]; | |
156 | char device_chemistry[ACPI_SBS_BLOCK_MAX + 3]; | |
3f86b832 RT |
157 | }; |
158 | ||
159 | struct acpi_battery_state { | |
160 | s16 voltage; | |
161 | s16 amperage; | |
162 | s16 remaining_capacity; | |
163 | s16 average_time_to_empty; | |
164 | s16 average_time_to_full; | |
165 | s16 battery_status; | |
166 | }; | |
167 | ||
168 | struct acpi_battery_alarm { | |
169 | s16 remaining_capacity; | |
170 | }; | |
171 | ||
172 | struct acpi_battery { | |
173 | int alive; | |
174 | int battery_present; | |
175 | int id; | |
176 | int init_state; | |
177 | struct acpi_sbs *sbs; | |
178 | struct acpi_battery_info info; | |
179 | struct acpi_battery_state state; | |
180 | struct acpi_battery_alarm alarm; | |
181 | struct proc_dir_entry *battery_entry; | |
182 | }; | |
183 | ||
184 | struct acpi_sbs { | |
185 | acpi_handle handle; | |
6d15702c | 186 | int base; |
3f86b832 | 187 | struct acpi_device *device; |
3f86b832 RT |
188 | int sbsm_present; |
189 | int sbsm_batteries_supported; | |
190 | int ac_present; | |
191 | struct proc_dir_entry *ac_entry; | |
192 | struct acpi_battery battery[MAX_SBS_BAT]; | |
193 | int update_info_mode; | |
194 | int zombie; | |
195 | int update_time; | |
196 | int update_time2; | |
197 | struct timer_list update_timer; | |
198 | }; | |
199 | ||
6d15702c VL |
200 | union sbs_rw_data { |
201 | u16 word; | |
202 | u8 block[ACPI_SBS_BLOCK_MAX + 2]; | |
203 | }; | |
204 | ||
205 | static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr, | |
206 | char read_write, u8 command, int size, | |
207 | union sbs_rw_data *data); | |
3f86b832 RT |
208 | static void acpi_update_delay(struct acpi_sbs *sbs); |
209 | static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type); | |
210 | ||
211 | /* -------------------------------------------------------------------------- | |
212 | SMBus Communication | |
213 | -------------------------------------------------------------------------- */ | |
214 | ||
6d15702c | 215 | static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data) |
3f86b832 | 216 | { |
6d15702c VL |
217 | u8 val; |
218 | int err; | |
3f86b832 | 219 | |
6d15702c VL |
220 | err = ec_read(sbs->base + address, &val); |
221 | if (!err) { | |
222 | *data = val; | |
223 | } | |
224 | xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP); | |
225 | return (err); | |
226 | } | |
3f86b832 | 227 | |
6d15702c VL |
228 | static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data) |
229 | { | |
230 | int err; | |
3f86b832 | 231 | |
6d15702c VL |
232 | err = ec_write(sbs->base + address, data); |
233 | return (err); | |
234 | } | |
3f86b832 | 235 | |
6d15702c VL |
236 | static int |
237 | acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr, | |
238 | char read_write, u8 command, int size, | |
239 | union sbs_rw_data *data) | |
240 | { | |
241 | unsigned char protocol, len = 0, temp[2] = { 0, 0 }; | |
242 | int i; | |
243 | ||
244 | if (read_write == ACPI_SBS_SMBUS_READ) { | |
245 | protocol = ACPI_EC_SMB_PRTCL_READ; | |
246 | } else { | |
247 | protocol = ACPI_EC_SMB_PRTCL_WRITE; | |
248 | } | |
249 | ||
250 | switch (size) { | |
251 | ||
252 | case ACPI_SBS_WORD_DATA: | |
253 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command); | |
254 | if (read_write == ACPI_SBS_SMBUS_WRITE) { | |
255 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word); | |
256 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1, | |
257 | data->word >> 8); | |
258 | } | |
259 | protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA; | |
3f86b832 | 260 | break; |
6d15702c VL |
261 | case ACPI_SBS_BLOCK_DATA: |
262 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command); | |
263 | if (read_write == ACPI_SBS_SMBUS_WRITE) { | |
264 | len = min_t(u8, data->block[0], 32); | |
265 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len); | |
266 | for (i = 0; i < len; i++) | |
267 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i, | |
268 | data->block[i + 1]); | |
269 | } | |
270 | protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA; | |
3f86b832 | 271 | break; |
6d15702c VL |
272 | default: |
273 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, | |
6845118b | 274 | "unsupported transaction %d", size)); |
6d15702c VL |
275 | return (-1); |
276 | } | |
277 | ||
278 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1); | |
279 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol); | |
280 | ||
281 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); | |
282 | ||
283 | if (~temp[0] & ACPI_EC_SMB_STS_DONE) { | |
284 | xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1); | |
285 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); | |
286 | } | |
287 | if (~temp[0] & ACPI_EC_SMB_STS_DONE) { | |
288 | xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2); | |
289 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); | |
290 | } | |
291 | if ((~temp[0] & ACPI_EC_SMB_STS_DONE) | |
292 | || (temp[0] & ACPI_EC_SMB_STS_STATUS)) { | |
293 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, | |
6845118b | 294 | "transaction %d error", size)); |
6d15702c VL |
295 | return (-1); |
296 | } | |
297 | ||
298 | if (read_write == ACPI_SBS_SMBUS_WRITE) { | |
299 | return (0); | |
300 | } | |
301 | ||
302 | switch (size) { | |
303 | ||
304 | case ACPI_SBS_WORD_DATA: | |
305 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp); | |
306 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1); | |
307 | data->word = (temp[1] << 8) | temp[0]; | |
3f86b832 | 308 | break; |
6d15702c VL |
309 | |
310 | case ACPI_SBS_BLOCK_DATA: | |
311 | len = 0; | |
312 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len); | |
313 | len = min_t(u8, len, 32); | |
314 | for (i = 0; i < len; i++) | |
315 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i, | |
316 | data->block + i + 1); | |
317 | data->block[0] = len; | |
3f86b832 RT |
318 | break; |
319 | default: | |
6d15702c | 320 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
6845118b | 321 | "unsupported transaction %d", size)); |
6d15702c | 322 | return (-1); |
3f86b832 | 323 | } |
6d15702c VL |
324 | |
325 | return (0); | |
3f86b832 RT |
326 | } |
327 | ||
328 | static int | |
6d15702c | 329 | acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word) |
3f86b832 | 330 | { |
6d15702c | 331 | union sbs_rw_data data; |
3f86b832 | 332 | int result = 0; |
3f86b832 | 333 | |
6d15702c VL |
334 | result = acpi_ec_sbs_access(sbs, addr, |
335 | ACPI_SBS_SMBUS_READ, func, | |
336 | ACPI_SBS_WORD_DATA, &data); | |
337 | if (result) { | |
6845118b VL |
338 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
339 | "acpi_ec_sbs_access() failed")); | |
6d15702c VL |
340 | } else { |
341 | *word = data.word; | |
3f86b832 RT |
342 | } |
343 | ||
635227ee | 344 | return result; |
3f86b832 RT |
345 | } |
346 | ||
347 | static int | |
6d15702c | 348 | acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str) |
3f86b832 | 349 | { |
6d15702c | 350 | union sbs_rw_data data; |
3f86b832 | 351 | int result = 0; |
3f86b832 | 352 | |
6d15702c VL |
353 | result = acpi_ec_sbs_access(sbs, addr, |
354 | ACPI_SBS_SMBUS_READ, func, | |
355 | ACPI_SBS_BLOCK_DATA, &data); | |
356 | if (result) { | |
6845118b VL |
357 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
358 | "acpi_ec_sbs_access() failed")); | |
6d15702c VL |
359 | } else { |
360 | strncpy(str, (const char *)data.block + 1, data.block[0]); | |
361 | str[data.block[0]] = 0; | |
3f86b832 RT |
362 | } |
363 | ||
635227ee | 364 | return result; |
3f86b832 RT |
365 | } |
366 | ||
367 | static int | |
6d15702c | 368 | acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word) |
3f86b832 | 369 | { |
6d15702c | 370 | union sbs_rw_data data; |
3f86b832 | 371 | int result = 0; |
3f86b832 RT |
372 | |
373 | data.word = word; | |
374 | ||
6d15702c VL |
375 | result = acpi_ec_sbs_access(sbs, addr, |
376 | ACPI_SBS_SMBUS_WRITE, func, | |
377 | ACPI_SBS_WORD_DATA, &data); | |
378 | if (result) { | |
6845118b VL |
379 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
380 | "acpi_ec_sbs_access() failed")); | |
3f86b832 RT |
381 | } |
382 | ||
635227ee | 383 | return result; |
3f86b832 RT |
384 | } |
385 | ||
386 | /* -------------------------------------------------------------------------- | |
387 | Smart Battery System Management | |
388 | -------------------------------------------------------------------------- */ | |
389 | ||
390 | /* Smart Battery */ | |
391 | ||
392 | static int acpi_sbs_generate_event(struct acpi_device *device, | |
393 | int event, int state, char *bid, char *class) | |
394 | { | |
395 | char bid_saved[5]; | |
396 | char class_saved[20]; | |
397 | int result = 0; | |
398 | ||
3f86b832 RT |
399 | strcpy(bid_saved, acpi_device_bid(device)); |
400 | strcpy(class_saved, acpi_device_class(device)); | |
401 | ||
402 | strcpy(acpi_device_bid(device), bid); | |
403 | strcpy(acpi_device_class(device), class); | |
404 | ||
405 | result = acpi_bus_generate_event(device, event, state); | |
406 | ||
407 | strcpy(acpi_device_bid(device), bid_saved); | |
408 | strcpy(acpi_device_class(device), class_saved); | |
409 | ||
635227ee | 410 | return result; |
3f86b832 RT |
411 | } |
412 | ||
413 | static int acpi_battery_get_present(struct acpi_battery *battery) | |
414 | { | |
415 | s16 state; | |
416 | int result = 0; | |
417 | int is_present = 0; | |
418 | ||
6d15702c VL |
419 | result = acpi_sbs_read_word(battery->sbs, |
420 | ACPI_SBSM_SMBUS_ADDR, 0x01, &state); | |
3f86b832 | 421 | if (result) { |
6845118b VL |
422 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
423 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
424 | } |
425 | if (!result) { | |
426 | is_present = (state & 0x000f) & (1 << battery->id); | |
427 | } | |
428 | battery->battery_present = is_present; | |
429 | ||
635227ee | 430 | return result; |
3f86b832 RT |
431 | } |
432 | ||
433 | static int acpi_battery_is_present(struct acpi_battery *battery) | |
434 | { | |
435 | return (battery->battery_present); | |
436 | } | |
437 | ||
438 | static int acpi_ac_is_present(struct acpi_sbs *sbs) | |
439 | { | |
440 | return (sbs->ac_present); | |
441 | } | |
442 | ||
443 | static int acpi_battery_select(struct acpi_battery *battery) | |
444 | { | |
6d15702c | 445 | struct acpi_sbs *sbs = battery->sbs; |
3f86b832 RT |
446 | int result = 0; |
447 | s16 state; | |
448 | int foo; | |
449 | ||
3f86b832 RT |
450 | if (battery->sbs->sbsm_present) { |
451 | ||
452 | /* Take special care not to knobble other nibbles of | |
453 | * state (aka selector_state), since | |
454 | * it causes charging to halt on SBSELs */ | |
455 | ||
456 | result = | |
6d15702c | 457 | acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state); |
3f86b832 | 458 | if (result) { |
6845118b VL |
459 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
460 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
461 | goto end; |
462 | } | |
463 | ||
464 | foo = (state & 0x0fff) | (1 << (battery->id + 12)); | |
465 | result = | |
6d15702c | 466 | acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo); |
3f86b832 | 467 | if (result) { |
6845118b VL |
468 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
469 | "acpi_sbs_write_word() failed")); | |
3f86b832 RT |
470 | goto end; |
471 | } | |
472 | } | |
473 | ||
474 | end: | |
635227ee | 475 | return result; |
3f86b832 RT |
476 | } |
477 | ||
478 | static int acpi_sbsm_get_info(struct acpi_sbs *sbs) | |
479 | { | |
3f86b832 RT |
480 | int result = 0; |
481 | s16 battery_system_info; | |
482 | ||
6d15702c VL |
483 | result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04, |
484 | &battery_system_info); | |
3f86b832 | 485 | if (result) { |
6845118b VL |
486 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
487 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
488 | goto end; |
489 | } | |
490 | ||
491 | sbs->sbsm_batteries_supported = battery_system_info & 0x000f; | |
492 | ||
493 | end: | |
494 | ||
635227ee | 495 | return result; |
3f86b832 RT |
496 | } |
497 | ||
498 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
499 | { | |
6d15702c | 500 | struct acpi_sbs *sbs = battery->sbs; |
3f86b832 RT |
501 | int result = 0; |
502 | s16 battery_mode; | |
503 | s16 specification_info; | |
504 | ||
6d15702c VL |
505 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03, |
506 | &battery_mode); | |
3f86b832 | 507 | if (result) { |
6845118b VL |
508 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
509 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
510 | goto end; |
511 | } | |
512 | battery->info.capacity_mode = (battery_mode & 0x8000) >> 15; | |
513 | ||
6d15702c VL |
514 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10, |
515 | &battery->info.full_charge_capacity); | |
3f86b832 | 516 | if (result) { |
6845118b VL |
517 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
518 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
519 | goto end; |
520 | } | |
521 | ||
6d15702c VL |
522 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18, |
523 | &battery->info.design_capacity); | |
3f86b832 RT |
524 | |
525 | if (result) { | |
526 | goto end; | |
527 | } | |
528 | ||
6d15702c VL |
529 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19, |
530 | &battery->info.design_voltage); | |
3f86b832 | 531 | if (result) { |
6845118b VL |
532 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
533 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
534 | goto end; |
535 | } | |
536 | ||
6d15702c VL |
537 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a, |
538 | &specification_info); | |
3f86b832 | 539 | if (result) { |
6845118b VL |
540 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
541 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
542 | goto end; |
543 | } | |
544 | ||
545 | switch ((specification_info & 0x0f00) >> 8) { | |
546 | case 1: | |
547 | battery->info.vscale = 10; | |
548 | break; | |
549 | case 2: | |
550 | battery->info.vscale = 100; | |
551 | break; | |
552 | case 3: | |
553 | battery->info.vscale = 1000; | |
554 | break; | |
555 | default: | |
556 | battery->info.vscale = 1; | |
557 | } | |
558 | ||
559 | switch ((specification_info & 0xf000) >> 12) { | |
560 | case 1: | |
561 | battery->info.ipscale = 10; | |
562 | break; | |
563 | case 2: | |
564 | battery->info.ipscale = 100; | |
565 | break; | |
566 | case 3: | |
567 | battery->info.ipscale = 1000; | |
568 | break; | |
569 | default: | |
570 | battery->info.ipscale = 1; | |
571 | } | |
572 | ||
6d15702c VL |
573 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c, |
574 | &battery->info.serial_number); | |
3f86b832 RT |
575 | if (result) { |
576 | goto end; | |
577 | } | |
578 | ||
6d15702c VL |
579 | result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20, |
580 | battery->info.manufacturer_name); | |
3f86b832 | 581 | if (result) { |
6845118b VL |
582 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
583 | "acpi_sbs_read_str() failed")); | |
3f86b832 RT |
584 | goto end; |
585 | } | |
586 | ||
6d15702c VL |
587 | result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21, |
588 | battery->info.device_name); | |
3f86b832 | 589 | if (result) { |
6845118b VL |
590 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
591 | "acpi_sbs_read_str() failed")); | |
3f86b832 RT |
592 | goto end; |
593 | } | |
594 | ||
6d15702c VL |
595 | result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22, |
596 | battery->info.device_chemistry); | |
3f86b832 | 597 | if (result) { |
6845118b VL |
598 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
599 | "acpi_sbs_read_str() failed")); | |
3f86b832 RT |
600 | goto end; |
601 | } | |
602 | ||
603 | end: | |
635227ee | 604 | return result; |
3f86b832 RT |
605 | } |
606 | ||
607 | static void acpi_update_delay(struct acpi_sbs *sbs) | |
608 | { | |
3f86b832 RT |
609 | if (sbs->zombie) { |
610 | return; | |
611 | } | |
612 | if (sbs->update_time2 > 0) { | |
613 | msleep(sbs->update_time2 * 1000); | |
614 | } | |
615 | } | |
616 | ||
617 | static int acpi_battery_get_state(struct acpi_battery *battery) | |
618 | { | |
6d15702c | 619 | struct acpi_sbs *sbs = battery->sbs; |
3f86b832 RT |
620 | int result = 0; |
621 | ||
3f86b832 | 622 | acpi_update_delay(battery->sbs); |
6d15702c VL |
623 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09, |
624 | &battery->state.voltage); | |
3f86b832 | 625 | if (result) { |
6845118b VL |
626 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
627 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
628 | goto end; |
629 | } | |
630 | ||
631 | acpi_update_delay(battery->sbs); | |
6d15702c VL |
632 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a, |
633 | &battery->state.amperage); | |
3f86b832 | 634 | if (result) { |
6845118b VL |
635 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
636 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
637 | goto end; |
638 | } | |
639 | ||
640 | acpi_update_delay(battery->sbs); | |
6d15702c VL |
641 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f, |
642 | &battery->state.remaining_capacity); | |
3f86b832 | 643 | if (result) { |
6845118b VL |
644 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
645 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
646 | goto end; |
647 | } | |
648 | ||
649 | acpi_update_delay(battery->sbs); | |
6d15702c VL |
650 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x12, |
651 | &battery->state.average_time_to_empty); | |
3f86b832 | 652 | if (result) { |
6845118b VL |
653 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
654 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
655 | goto end; |
656 | } | |
657 | ||
658 | acpi_update_delay(battery->sbs); | |
6d15702c VL |
659 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x13, |
660 | &battery->state.average_time_to_full); | |
3f86b832 | 661 | if (result) { |
6845118b VL |
662 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
663 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
664 | goto end; |
665 | } | |
666 | ||
667 | acpi_update_delay(battery->sbs); | |
6d15702c VL |
668 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16, |
669 | &battery->state.battery_status); | |
3f86b832 | 670 | if (result) { |
6845118b VL |
671 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
672 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
673 | goto end; |
674 | } | |
675 | ||
676 | acpi_update_delay(battery->sbs); | |
677 | ||
678 | end: | |
635227ee | 679 | return result; |
3f86b832 RT |
680 | } |
681 | ||
682 | static int acpi_battery_get_alarm(struct acpi_battery *battery) | |
683 | { | |
6d15702c | 684 | struct acpi_sbs *sbs = battery->sbs; |
3f86b832 RT |
685 | int result = 0; |
686 | ||
6d15702c VL |
687 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, |
688 | &battery->alarm.remaining_capacity); | |
3f86b832 | 689 | if (result) { |
6845118b VL |
690 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
691 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
692 | goto end; |
693 | } | |
694 | ||
695 | acpi_update_delay(battery->sbs); | |
696 | ||
697 | end: | |
698 | ||
635227ee | 699 | return result; |
3f86b832 RT |
700 | } |
701 | ||
702 | static int acpi_battery_set_alarm(struct acpi_battery *battery, | |
703 | unsigned long alarm) | |
704 | { | |
6d15702c | 705 | struct acpi_sbs *sbs = battery->sbs; |
3f86b832 RT |
706 | int result = 0; |
707 | s16 battery_mode; | |
708 | int foo; | |
709 | ||
3f86b832 RT |
710 | result = acpi_battery_select(battery); |
711 | if (result) { | |
6845118b VL |
712 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
713 | "acpi_battery_select() failed")); | |
3f86b832 RT |
714 | goto end; |
715 | } | |
716 | ||
717 | /* If necessary, enable the alarm */ | |
718 | ||
719 | if (alarm > 0) { | |
720 | result = | |
6d15702c VL |
721 | acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03, |
722 | &battery_mode); | |
3f86b832 | 723 | if (result) { |
6845118b VL |
724 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
725 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
726 | goto end; |
727 | } | |
728 | ||
729 | result = | |
6d15702c VL |
730 | acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, |
731 | battery_mode & 0xbfff); | |
3f86b832 | 732 | if (result) { |
6845118b VL |
733 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
734 | "acpi_sbs_write_word() failed")); | |
3f86b832 RT |
735 | goto end; |
736 | } | |
737 | } | |
738 | ||
739 | foo = alarm / (battery->info.capacity_mode ? 10 : 1); | |
6d15702c | 740 | result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo); |
3f86b832 | 741 | if (result) { |
6845118b VL |
742 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
743 | "acpi_sbs_write_word() failed")); | |
3f86b832 RT |
744 | goto end; |
745 | } | |
746 | ||
747 | end: | |
748 | ||
635227ee | 749 | return result; |
3f86b832 RT |
750 | } |
751 | ||
752 | static int acpi_battery_set_mode(struct acpi_battery *battery) | |
753 | { | |
754 | int result = 0; | |
755 | s16 battery_mode; | |
756 | ||
3f86b832 RT |
757 | if (capacity_mode == DEF_CAPACITY_UNIT) { |
758 | goto end; | |
759 | } | |
760 | ||
6d15702c VL |
761 | result = acpi_sbs_read_word(battery->sbs, |
762 | ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode); | |
3f86b832 | 763 | if (result) { |
6845118b VL |
764 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
765 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
766 | goto end; |
767 | } | |
768 | ||
769 | if (capacity_mode == MAH_CAPACITY_UNIT) { | |
770 | battery_mode &= 0x7fff; | |
771 | } else { | |
772 | battery_mode |= 0x8000; | |
773 | } | |
6d15702c VL |
774 | result = acpi_sbs_write_word(battery->sbs, |
775 | ACPI_SB_SMBUS_ADDR, 0x03, battery_mode); | |
3f86b832 | 776 | if (result) { |
6845118b VL |
777 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
778 | "acpi_sbs_write_word() failed")); | |
3f86b832 RT |
779 | goto end; |
780 | } | |
781 | ||
6d15702c VL |
782 | result = acpi_sbs_read_word(battery->sbs, |
783 | ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode); | |
3f86b832 | 784 | if (result) { |
6845118b VL |
785 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
786 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
787 | goto end; |
788 | } | |
789 | ||
790 | end: | |
635227ee | 791 | return result; |
3f86b832 RT |
792 | } |
793 | ||
794 | static int acpi_battery_init(struct acpi_battery *battery) | |
795 | { | |
796 | int result = 0; | |
797 | ||
3f86b832 RT |
798 | result = acpi_battery_select(battery); |
799 | if (result) { | |
6845118b VL |
800 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
801 | "acpi_battery_init() failed")); | |
3f86b832 RT |
802 | goto end; |
803 | } | |
804 | ||
805 | result = acpi_battery_set_mode(battery); | |
806 | if (result) { | |
6845118b VL |
807 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
808 | "acpi_battery_set_mode() failed")); | |
3f86b832 RT |
809 | goto end; |
810 | } | |
811 | ||
812 | result = acpi_battery_get_info(battery); | |
813 | if (result) { | |
6845118b VL |
814 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
815 | "acpi_battery_get_info() failed")); | |
3f86b832 RT |
816 | goto end; |
817 | } | |
818 | ||
819 | result = acpi_battery_get_state(battery); | |
820 | if (result) { | |
6845118b VL |
821 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
822 | "acpi_battery_get_state() failed")); | |
3f86b832 RT |
823 | goto end; |
824 | } | |
825 | ||
826 | result = acpi_battery_get_alarm(battery); | |
827 | if (result) { | |
6845118b VL |
828 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
829 | "acpi_battery_get_alarm() failed")); | |
3f86b832 RT |
830 | goto end; |
831 | } | |
832 | ||
833 | end: | |
635227ee | 834 | return result; |
3f86b832 RT |
835 | } |
836 | ||
837 | static int acpi_ac_get_present(struct acpi_sbs *sbs) | |
838 | { | |
3f86b832 RT |
839 | int result = 0; |
840 | s16 charger_status; | |
841 | ||
6d15702c VL |
842 | result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13, |
843 | &charger_status); | |
3f86b832 RT |
844 | |
845 | if (result) { | |
6845118b VL |
846 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
847 | "acpi_sbs_read_word() failed")); | |
3f86b832 RT |
848 | goto end; |
849 | } | |
850 | ||
851 | sbs->ac_present = (charger_status & 0x8000) >> 15; | |
852 | ||
853 | end: | |
854 | ||
635227ee | 855 | return result; |
3f86b832 RT |
856 | } |
857 | ||
858 | /* -------------------------------------------------------------------------- | |
859 | FS Interface (/proc/acpi) | |
860 | -------------------------------------------------------------------------- */ | |
861 | ||
862 | /* Generic Routines */ | |
863 | ||
864 | static int | |
865 | acpi_sbs_generic_add_fs(struct proc_dir_entry **dir, | |
866 | struct proc_dir_entry *parent_dir, | |
867 | char *dir_name, | |
868 | struct file_operations *info_fops, | |
869 | struct file_operations *state_fops, | |
870 | struct file_operations *alarm_fops, void *data) | |
871 | { | |
872 | struct proc_dir_entry *entry = NULL; | |
873 | ||
3f86b832 RT |
874 | if (!*dir) { |
875 | *dir = proc_mkdir(dir_name, parent_dir); | |
876 | if (!*dir) { | |
6845118b VL |
877 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
878 | "proc_mkdir() failed")); | |
635227ee | 879 | return -ENODEV; |
3f86b832 RT |
880 | } |
881 | (*dir)->owner = THIS_MODULE; | |
882 | } | |
883 | ||
884 | /* 'info' [R] */ | |
885 | if (info_fops) { | |
886 | entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir); | |
887 | if (!entry) { | |
6845118b VL |
888 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
889 | "create_proc_entry() failed")); | |
3f86b832 RT |
890 | } else { |
891 | entry->proc_fops = info_fops; | |
892 | entry->data = data; | |
893 | entry->owner = THIS_MODULE; | |
894 | } | |
895 | } | |
896 | ||
897 | /* 'state' [R] */ | |
898 | if (state_fops) { | |
899 | entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir); | |
900 | if (!entry) { | |
6845118b VL |
901 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
902 | "create_proc_entry() failed")); | |
3f86b832 RT |
903 | } else { |
904 | entry->proc_fops = state_fops; | |
905 | entry->data = data; | |
906 | entry->owner = THIS_MODULE; | |
907 | } | |
908 | } | |
909 | ||
910 | /* 'alarm' [R/W] */ | |
911 | if (alarm_fops) { | |
912 | entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir); | |
913 | if (!entry) { | |
6845118b VL |
914 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
915 | "create_proc_entry() failed")); | |
3f86b832 RT |
916 | } else { |
917 | entry->proc_fops = alarm_fops; | |
918 | entry->data = data; | |
919 | entry->owner = THIS_MODULE; | |
920 | } | |
921 | } | |
922 | ||
635227ee | 923 | return 0; |
3f86b832 RT |
924 | } |
925 | ||
926 | static void | |
927 | acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir, | |
928 | struct proc_dir_entry *parent_dir) | |
929 | { | |
3f86b832 RT |
930 | |
931 | if (*dir) { | |
932 | remove_proc_entry(ACPI_SBS_FILE_INFO, *dir); | |
933 | remove_proc_entry(ACPI_SBS_FILE_STATE, *dir); | |
934 | remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir); | |
935 | remove_proc_entry((*dir)->name, parent_dir); | |
936 | *dir = NULL; | |
937 | } | |
938 | ||
939 | } | |
940 | ||
941 | /* Smart Battery Interface */ | |
942 | ||
943 | static struct proc_dir_entry *acpi_battery_dir = NULL; | |
944 | ||
945 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) | |
946 | { | |
50dd0969 | 947 | struct acpi_battery *battery = seq->private; |
3f86b832 RT |
948 | int cscale; |
949 | int result = 0; | |
950 | ||
3f86b832 | 951 | if (battery->sbs->zombie) { |
635227ee | 952 | return -ENODEV; |
3f86b832 RT |
953 | } |
954 | ||
955 | down(&sbs_sem); | |
956 | ||
957 | if (update_mode == REQUEST_UPDATE_MODE) { | |
958 | result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO); | |
959 | if (result) { | |
6845118b VL |
960 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
961 | "acpi_sbs_update_run() failed")); | |
3f86b832 RT |
962 | } |
963 | } | |
964 | ||
965 | if (acpi_battery_is_present(battery)) { | |
966 | seq_printf(seq, "present: yes\n"); | |
967 | } else { | |
968 | seq_printf(seq, "present: no\n"); | |
969 | goto end; | |
970 | } | |
971 | ||
972 | if (battery->info.capacity_mode) { | |
973 | cscale = battery->info.vscale * battery->info.ipscale; | |
974 | } else { | |
975 | cscale = battery->info.ipscale; | |
976 | } | |
977 | seq_printf(seq, "design capacity: %i%s", | |
978 | battery->info.design_capacity * cscale, | |
979 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); | |
980 | ||
981 | seq_printf(seq, "last full capacity: %i%s", | |
982 | battery->info.full_charge_capacity * cscale, | |
983 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); | |
984 | ||
985 | seq_printf(seq, "battery technology: rechargeable\n"); | |
986 | ||
987 | seq_printf(seq, "design voltage: %i mV\n", | |
988 | battery->info.design_voltage * battery->info.vscale); | |
989 | ||
990 | seq_printf(seq, "design capacity warning: unknown\n"); | |
991 | seq_printf(seq, "design capacity low: unknown\n"); | |
992 | seq_printf(seq, "capacity granularity 1: unknown\n"); | |
993 | seq_printf(seq, "capacity granularity 2: unknown\n"); | |
994 | ||
995 | seq_printf(seq, "model number: %s\n", | |
996 | battery->info.device_name); | |
997 | ||
998 | seq_printf(seq, "serial number: %i\n", | |
999 | battery->info.serial_number); | |
1000 | ||
1001 | seq_printf(seq, "battery type: %s\n", | |
1002 | battery->info.device_chemistry); | |
1003 | ||
1004 | seq_printf(seq, "OEM info: %s\n", | |
1005 | battery->info.manufacturer_name); | |
1006 | ||
1007 | end: | |
1008 | ||
1009 | up(&sbs_sem); | |
1010 | ||
635227ee | 1011 | return result; |
3f86b832 RT |
1012 | } |
1013 | ||
1014 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) | |
1015 | { | |
1016 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); | |
1017 | } | |
1018 | ||
1019 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) | |
1020 | { | |
1021 | struct acpi_battery *battery = (struct acpi_battery *)seq->private; | |
1022 | int result = 0; | |
1023 | int cscale; | |
1024 | int foo; | |
1025 | ||
3f86b832 | 1026 | if (battery->sbs->zombie) { |
635227ee | 1027 | return -ENODEV; |
3f86b832 RT |
1028 | } |
1029 | ||
1030 | down(&sbs_sem); | |
1031 | ||
1032 | if (update_mode == REQUEST_UPDATE_MODE) { | |
1033 | result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE); | |
1034 | if (result) { | |
6845118b VL |
1035 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1036 | "acpi_sbs_update_run() failed")); | |
3f86b832 RT |
1037 | } |
1038 | } | |
1039 | ||
1040 | if (acpi_battery_is_present(battery)) { | |
1041 | seq_printf(seq, "present: yes\n"); | |
1042 | } else { | |
1043 | seq_printf(seq, "present: no\n"); | |
1044 | goto end; | |
1045 | } | |
1046 | ||
1047 | if (battery->info.capacity_mode) { | |
1048 | cscale = battery->info.vscale * battery->info.ipscale; | |
1049 | } else { | |
1050 | cscale = battery->info.ipscale; | |
1051 | } | |
1052 | ||
1053 | if (battery->state.battery_status & 0x0010) { | |
1054 | seq_printf(seq, "capacity state: critical\n"); | |
1055 | } else { | |
1056 | seq_printf(seq, "capacity state: ok\n"); | |
1057 | } | |
e6d0f562 VL |
1058 | |
1059 | foo = (s16) battery->state.amperage * battery->info.ipscale; | |
1060 | if (battery->info.capacity_mode) { | |
1061 | foo = foo * battery->info.design_voltage / 1000; | |
1062 | } | |
3f86b832 RT |
1063 | if (battery->state.amperage < 0) { |
1064 | seq_printf(seq, "charging state: discharging\n"); | |
e6d0f562 VL |
1065 | seq_printf(seq, "present rate: %d %s\n", |
1066 | -foo, battery->info.capacity_mode ? "mW" : "mA"); | |
3f86b832 RT |
1067 | } else if (battery->state.amperage > 0) { |
1068 | seq_printf(seq, "charging state: charging\n"); | |
e6d0f562 VL |
1069 | seq_printf(seq, "present rate: %d %s\n", |
1070 | foo, battery->info.capacity_mode ? "mW" : "mA"); | |
3f86b832 RT |
1071 | } else { |
1072 | seq_printf(seq, "charging state: charged\n"); | |
1073 | seq_printf(seq, "present rate: 0 %s\n", | |
1074 | battery->info.capacity_mode ? "mW" : "mA"); | |
1075 | } | |
1076 | ||
1077 | seq_printf(seq, "remaining capacity: %i%s", | |
1078 | battery->state.remaining_capacity * cscale, | |
1079 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); | |
1080 | ||
1081 | seq_printf(seq, "present voltage: %i mV\n", | |
1082 | battery->state.voltage * battery->info.vscale); | |
1083 | ||
1084 | end: | |
1085 | ||
1086 | up(&sbs_sem); | |
1087 | ||
635227ee | 1088 | return result; |
3f86b832 RT |
1089 | } |
1090 | ||
1091 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) | |
1092 | { | |
1093 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); | |
1094 | } | |
1095 | ||
1096 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) | |
1097 | { | |
50dd0969 | 1098 | struct acpi_battery *battery = seq->private; |
3f86b832 RT |
1099 | int result = 0; |
1100 | int cscale; | |
1101 | ||
3f86b832 | 1102 | if (battery->sbs->zombie) { |
635227ee | 1103 | return -ENODEV; |
3f86b832 RT |
1104 | } |
1105 | ||
1106 | down(&sbs_sem); | |
1107 | ||
1108 | if (update_mode == REQUEST_UPDATE_MODE) { | |
1109 | result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM); | |
1110 | if (result) { | |
6845118b VL |
1111 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1112 | "acpi_sbs_update_run() failed")); | |
3f86b832 RT |
1113 | } |
1114 | } | |
1115 | ||
1116 | if (!acpi_battery_is_present(battery)) { | |
1117 | seq_printf(seq, "present: no\n"); | |
1118 | goto end; | |
1119 | } | |
1120 | ||
1121 | if (battery->info.capacity_mode) { | |
1122 | cscale = battery->info.vscale * battery->info.ipscale; | |
1123 | } else { | |
1124 | cscale = battery->info.ipscale; | |
1125 | } | |
1126 | ||
1127 | seq_printf(seq, "alarm: "); | |
1128 | if (battery->alarm.remaining_capacity) { | |
1129 | seq_printf(seq, "%i%s", | |
1130 | battery->alarm.remaining_capacity * cscale, | |
1131 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); | |
1132 | } else { | |
1133 | seq_printf(seq, "disabled\n"); | |
1134 | } | |
1135 | ||
1136 | end: | |
1137 | ||
1138 | up(&sbs_sem); | |
1139 | ||
635227ee | 1140 | return result; |
3f86b832 RT |
1141 | } |
1142 | ||
1143 | static ssize_t | |
1144 | acpi_battery_write_alarm(struct file *file, const char __user * buffer, | |
1145 | size_t count, loff_t * ppos) | |
1146 | { | |
50dd0969 JE |
1147 | struct seq_file *seq = file->private_data; |
1148 | struct acpi_battery *battery = seq->private; | |
3f86b832 RT |
1149 | char alarm_string[12] = { '\0' }; |
1150 | int result, old_alarm, new_alarm; | |
1151 | ||
3f86b832 | 1152 | if (battery->sbs->zombie) { |
635227ee | 1153 | return -ENODEV; |
3f86b832 RT |
1154 | } |
1155 | ||
1156 | down(&sbs_sem); | |
1157 | ||
1158 | if (!acpi_battery_is_present(battery)) { | |
1159 | result = -ENODEV; | |
1160 | goto end; | |
1161 | } | |
1162 | ||
1163 | if (count > sizeof(alarm_string) - 1) { | |
1164 | result = -EINVAL; | |
1165 | goto end; | |
1166 | } | |
1167 | ||
1168 | if (copy_from_user(alarm_string, buffer, count)) { | |
1169 | result = -EFAULT; | |
1170 | goto end; | |
1171 | } | |
1172 | ||
1173 | alarm_string[count] = 0; | |
1174 | ||
1175 | old_alarm = battery->alarm.remaining_capacity; | |
1176 | new_alarm = simple_strtoul(alarm_string, NULL, 0); | |
1177 | ||
1178 | result = acpi_battery_set_alarm(battery, new_alarm); | |
1179 | if (result) { | |
6845118b VL |
1180 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1181 | "acpi_battery_set_alarm() failed")); | |
50dd0969 | 1182 | acpi_battery_set_alarm(battery, old_alarm); |
3f86b832 RT |
1183 | goto end; |
1184 | } | |
1185 | result = acpi_battery_get_alarm(battery); | |
1186 | if (result) { | |
6845118b VL |
1187 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1188 | "acpi_battery_get_alarm() failed")); | |
50dd0969 | 1189 | acpi_battery_set_alarm(battery, old_alarm); |
3f86b832 RT |
1190 | goto end; |
1191 | } | |
1192 | ||
1193 | end: | |
1194 | up(&sbs_sem); | |
1195 | ||
1196 | if (result) { | |
635227ee | 1197 | return result; |
3f86b832 | 1198 | } else { |
635227ee | 1199 | return count; |
3f86b832 RT |
1200 | } |
1201 | } | |
1202 | ||
1203 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) | |
1204 | { | |
1205 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); | |
1206 | } | |
1207 | ||
1208 | static struct file_operations acpi_battery_info_fops = { | |
1209 | .open = acpi_battery_info_open_fs, | |
1210 | .read = seq_read, | |
1211 | .llseek = seq_lseek, | |
1212 | .release = single_release, | |
1213 | .owner = THIS_MODULE, | |
1214 | }; | |
1215 | ||
1216 | static struct file_operations acpi_battery_state_fops = { | |
1217 | .open = acpi_battery_state_open_fs, | |
1218 | .read = seq_read, | |
1219 | .llseek = seq_lseek, | |
1220 | .release = single_release, | |
1221 | .owner = THIS_MODULE, | |
1222 | }; | |
1223 | ||
1224 | static struct file_operations acpi_battery_alarm_fops = { | |
1225 | .open = acpi_battery_alarm_open_fs, | |
1226 | .read = seq_read, | |
1227 | .write = acpi_battery_write_alarm, | |
1228 | .llseek = seq_lseek, | |
1229 | .release = single_release, | |
1230 | .owner = THIS_MODULE, | |
1231 | }; | |
1232 | ||
1233 | /* Legacy AC Adapter Interface */ | |
1234 | ||
1235 | static struct proc_dir_entry *acpi_ac_dir = NULL; | |
1236 | ||
1237 | static int acpi_ac_read_state(struct seq_file *seq, void *offset) | |
1238 | { | |
50dd0969 | 1239 | struct acpi_sbs *sbs = seq->private; |
3f86b832 RT |
1240 | int result; |
1241 | ||
3f86b832 | 1242 | if (sbs->zombie) { |
635227ee | 1243 | return -ENODEV; |
3f86b832 RT |
1244 | } |
1245 | ||
1246 | down(&sbs_sem); | |
1247 | ||
1248 | if (update_mode == REQUEST_UPDATE_MODE) { | |
1249 | result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE); | |
1250 | if (result) { | |
6845118b VL |
1251 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1252 | "acpi_sbs_update_run() failed")); | |
3f86b832 RT |
1253 | } |
1254 | } | |
1255 | ||
1256 | seq_printf(seq, "state: %s\n", | |
1257 | sbs->ac_present ? "on-line" : "off-line"); | |
1258 | ||
1259 | up(&sbs_sem); | |
1260 | ||
635227ee | 1261 | return 0; |
3f86b832 RT |
1262 | } |
1263 | ||
1264 | static int acpi_ac_state_open_fs(struct inode *inode, struct file *file) | |
1265 | { | |
1266 | return single_open(file, acpi_ac_read_state, PDE(inode)->data); | |
1267 | } | |
1268 | ||
1269 | static struct file_operations acpi_ac_state_fops = { | |
1270 | .open = acpi_ac_state_open_fs, | |
1271 | .read = seq_read, | |
1272 | .llseek = seq_lseek, | |
1273 | .release = single_release, | |
1274 | .owner = THIS_MODULE, | |
1275 | }; | |
1276 | ||
1277 | /* -------------------------------------------------------------------------- | |
1278 | Driver Interface | |
1279 | -------------------------------------------------------------------------- */ | |
1280 | ||
1281 | /* Smart Battery */ | |
1282 | ||
1283 | static int acpi_battery_add(struct acpi_sbs *sbs, int id) | |
1284 | { | |
1285 | int is_present; | |
1286 | int result; | |
1287 | char dir_name[32]; | |
1288 | struct acpi_battery *battery; | |
1289 | ||
3f86b832 RT |
1290 | battery = &sbs->battery[id]; |
1291 | ||
1292 | battery->alive = 0; | |
1293 | ||
1294 | battery->init_state = 0; | |
1295 | battery->id = id; | |
1296 | battery->sbs = sbs; | |
1297 | ||
1298 | result = acpi_battery_select(battery); | |
1299 | if (result) { | |
6845118b VL |
1300 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1301 | "acpi_battery_select() failed")); | |
3f86b832 RT |
1302 | goto end; |
1303 | } | |
1304 | ||
1305 | result = acpi_battery_get_present(battery); | |
1306 | if (result) { | |
6845118b VL |
1307 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1308 | "acpi_battery_get_present() failed")); | |
3f86b832 RT |
1309 | goto end; |
1310 | } | |
1311 | ||
1312 | is_present = acpi_battery_is_present(battery); | |
1313 | ||
1314 | if (is_present) { | |
1315 | result = acpi_battery_init(battery); | |
1316 | if (result) { | |
6845118b VL |
1317 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1318 | "acpi_battery_init() failed")); | |
3f86b832 RT |
1319 | goto end; |
1320 | } | |
1321 | battery->init_state = 1; | |
1322 | } | |
1323 | ||
50dd0969 | 1324 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
3f86b832 RT |
1325 | |
1326 | result = acpi_sbs_generic_add_fs(&battery->battery_entry, | |
1327 | acpi_battery_dir, | |
1328 | dir_name, | |
1329 | &acpi_battery_info_fops, | |
1330 | &acpi_battery_state_fops, | |
1331 | &acpi_battery_alarm_fops, battery); | |
1332 | if (result) { | |
6845118b VL |
1333 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1334 | "acpi_sbs_generic_add_fs() failed")); | |
3f86b832 RT |
1335 | goto end; |
1336 | } | |
1337 | battery->alive = 1; | |
1338 | ||
1339 | end: | |
635227ee | 1340 | return result; |
3f86b832 RT |
1341 | } |
1342 | ||
1343 | static void acpi_battery_remove(struct acpi_sbs *sbs, int id) | |
1344 | { | |
3f86b832 RT |
1345 | |
1346 | if (sbs->battery[id].battery_entry) { | |
1347 | acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry), | |
1348 | acpi_battery_dir); | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | static int acpi_ac_add(struct acpi_sbs *sbs) | |
1353 | { | |
1354 | int result; | |
1355 | ||
3f86b832 RT |
1356 | result = acpi_ac_get_present(sbs); |
1357 | if (result) { | |
6845118b VL |
1358 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1359 | "acpi_ac_get_present() failed")); | |
3f86b832 RT |
1360 | goto end; |
1361 | } | |
1362 | ||
1363 | result = acpi_sbs_generic_add_fs(&sbs->ac_entry, | |
1364 | acpi_ac_dir, | |
1365 | ACPI_AC_DIR_NAME, | |
1366 | NULL, &acpi_ac_state_fops, NULL, sbs); | |
1367 | if (result) { | |
6845118b VL |
1368 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1369 | "acpi_sbs_generic_add_fs() failed")); | |
3f86b832 RT |
1370 | goto end; |
1371 | } | |
1372 | ||
1373 | end: | |
1374 | ||
635227ee | 1375 | return result; |
3f86b832 RT |
1376 | } |
1377 | ||
1378 | static void acpi_ac_remove(struct acpi_sbs *sbs) | |
1379 | { | |
3f86b832 RT |
1380 | |
1381 | if (sbs->ac_entry) { | |
1382 | acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir); | |
1383 | } | |
1384 | } | |
1385 | ||
1386 | static void acpi_sbs_update_queue_run(unsigned long data) | |
1387 | { | |
3f86b832 RT |
1388 | acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data); |
1389 | } | |
1390 | ||
1391 | static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type) | |
1392 | { | |
1393 | struct acpi_battery *battery; | |
1394 | int result = 0; | |
1395 | int old_ac_present; | |
1396 | int old_battery_present; | |
1397 | int new_ac_present; | |
1398 | int new_battery_present; | |
1399 | int id; | |
1400 | char dir_name[32]; | |
1401 | int do_battery_init, do_ac_init; | |
1402 | s16 old_remaining_capacity; | |
1403 | ||
3f86b832 RT |
1404 | if (sbs->zombie) { |
1405 | goto end; | |
1406 | } | |
1407 | ||
1408 | old_ac_present = acpi_ac_is_present(sbs); | |
1409 | ||
1410 | result = acpi_ac_get_present(sbs); | |
1411 | if (result) { | |
6845118b VL |
1412 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1413 | "acpi_ac_get_present() failed")); | |
3f86b832 RT |
1414 | } |
1415 | ||
1416 | new_ac_present = acpi_ac_is_present(sbs); | |
1417 | ||
1418 | do_ac_init = (old_ac_present != new_ac_present); | |
1419 | ||
1420 | if (data_type == DATA_TYPE_AC_STATE) { | |
1421 | goto end; | |
1422 | } | |
1423 | ||
1424 | for (id = 0; id < MAX_SBS_BAT; id++) { | |
1425 | battery = &sbs->battery[id]; | |
1426 | if (battery->alive == 0) { | |
1427 | continue; | |
1428 | } | |
1429 | ||
1430 | old_remaining_capacity = battery->state.remaining_capacity; | |
1431 | ||
1432 | old_battery_present = acpi_battery_is_present(battery); | |
1433 | ||
1434 | result = acpi_battery_select(battery); | |
1435 | if (result) { | |
6845118b VL |
1436 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1437 | "acpi_battery_select() failed")); | |
3f86b832 RT |
1438 | } |
1439 | if (sbs->zombie) { | |
1440 | goto end; | |
1441 | } | |
1442 | ||
1443 | result = acpi_battery_get_present(battery); | |
1444 | if (result) { | |
6845118b VL |
1445 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1446 | "acpi_battery_get_present() failed")); | |
3f86b832 RT |
1447 | } |
1448 | if (sbs->zombie) { | |
1449 | goto end; | |
1450 | } | |
1451 | ||
1452 | new_battery_present = acpi_battery_is_present(battery); | |
1453 | ||
1454 | do_battery_init = ((old_battery_present != new_battery_present) | |
1455 | && new_battery_present); | |
1456 | ||
1457 | if (sbs->zombie) { | |
1458 | goto end; | |
1459 | } | |
1460 | if (do_ac_init || do_battery_init || | |
1461 | update_info_mode || sbs->update_info_mode) { | |
1462 | if (sbs->update_info_mode) { | |
1463 | sbs->update_info_mode = 0; | |
1464 | } else { | |
1465 | sbs->update_info_mode = 1; | |
1466 | } | |
1467 | result = acpi_battery_init(battery); | |
1468 | if (result) { | |
6845118b VL |
1469 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1470 | "acpi_battery_init() " | |
1471 | "failed")); | |
3f86b832 RT |
1472 | } |
1473 | } | |
1474 | if (data_type == DATA_TYPE_INFO) { | |
1475 | continue; | |
1476 | } | |
1477 | ||
1478 | if (sbs->zombie) { | |
1479 | goto end; | |
1480 | } | |
1481 | if (new_battery_present) { | |
1482 | result = acpi_battery_get_alarm(battery); | |
1483 | if (result) { | |
6845118b VL |
1484 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1485 | "acpi_battery_get_alarm() " | |
1486 | "failed")); | |
3f86b832 RT |
1487 | } |
1488 | if (data_type == DATA_TYPE_ALARM) { | |
1489 | continue; | |
1490 | } | |
1491 | ||
1492 | result = acpi_battery_get_state(battery); | |
1493 | if (result) { | |
6845118b VL |
1494 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1495 | "acpi_battery_get_state() " | |
1496 | "failed")); | |
3f86b832 RT |
1497 | } |
1498 | } | |
1499 | if (sbs->zombie) { | |
1500 | goto end; | |
1501 | } | |
1502 | if (data_type != DATA_TYPE_COMMON) { | |
1503 | continue; | |
1504 | } | |
1505 | ||
1506 | if (old_battery_present != new_battery_present) { | |
50dd0969 | 1507 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
3f86b832 RT |
1508 | result = acpi_sbs_generate_event(sbs->device, |
1509 | ACPI_SBS_BATTERY_NOTIFY_STATUS, | |
1510 | new_battery_present, | |
1511 | dir_name, | |
1512 | ACPI_BATTERY_CLASS); | |
1513 | if (result) { | |
6845118b VL |
1514 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1515 | "acpi_sbs_generate_event() " | |
1516 | "failed")); | |
3f86b832 RT |
1517 | } |
1518 | } | |
1519 | if (old_remaining_capacity != battery->state.remaining_capacity) { | |
50dd0969 | 1520 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
3f86b832 RT |
1521 | result = acpi_sbs_generate_event(sbs->device, |
1522 | ACPI_SBS_BATTERY_NOTIFY_STATUS, | |
1523 | new_battery_present, | |
1524 | dir_name, | |
1525 | ACPI_BATTERY_CLASS); | |
1526 | if (result) { | |
6845118b VL |
1527 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1528 | "acpi_sbs_generate_event() failed")); | |
3f86b832 RT |
1529 | } |
1530 | } | |
1531 | ||
1532 | } | |
1533 | if (sbs->zombie) { | |
1534 | goto end; | |
1535 | } | |
1536 | if (data_type != DATA_TYPE_COMMON) { | |
1537 | goto end; | |
1538 | } | |
1539 | ||
1540 | if (old_ac_present != new_ac_present) { | |
1541 | result = acpi_sbs_generate_event(sbs->device, | |
1542 | ACPI_SBS_AC_NOTIFY_STATUS, | |
1543 | new_ac_present, | |
1544 | ACPI_AC_DIR_NAME, | |
1545 | ACPI_AC_CLASS); | |
1546 | if (result) { | |
6845118b VL |
1547 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1548 | "acpi_sbs_generate_event() failed")); | |
3f86b832 RT |
1549 | } |
1550 | } | |
1551 | ||
1552 | end: | |
635227ee | 1553 | return result; |
3f86b832 RT |
1554 | } |
1555 | ||
1556 | static void acpi_sbs_update_queue(void *data) | |
1557 | { | |
1558 | struct acpi_sbs *sbs = data; | |
1559 | unsigned long delay = -1; | |
1560 | int result; | |
1561 | ||
3f86b832 RT |
1562 | if (sbs->zombie) { |
1563 | goto end; | |
1564 | } | |
1565 | ||
1566 | result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON); | |
1567 | if (result) { | |
6845118b VL |
1568 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1569 | "acpi_sbs_update_run() failed")); | |
3f86b832 RT |
1570 | } |
1571 | ||
1572 | if (sbs->zombie) { | |
1573 | goto end; | |
1574 | } | |
1575 | ||
1576 | if (update_mode == REQUEST_UPDATE_MODE) { | |
1577 | goto end; | |
1578 | } | |
1579 | ||
1580 | delay = jiffies + HZ * update_time; | |
1581 | sbs->update_timer.data = (unsigned long)data; | |
1582 | sbs->update_timer.function = acpi_sbs_update_queue_run; | |
1583 | sbs->update_timer.expires = delay; | |
1584 | add_timer(&sbs->update_timer); | |
1585 | end: | |
1586 | ; | |
1587 | } | |
1588 | ||
1589 | static int acpi_sbs_add(struct acpi_device *device) | |
1590 | { | |
1591 | struct acpi_sbs *sbs = NULL; | |
6d15702c | 1592 | int result; |
3f86b832 | 1593 | unsigned long sbs_obj; |
6d15702c | 1594 | int id; |
3f86b832 | 1595 | acpi_status status = AE_OK; |
6d15702c VL |
1596 | unsigned long val; |
1597 | ||
1598 | status = | |
1599 | acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val); | |
1600 | if (ACPI_FAILURE(status)) { | |
6845118b | 1601 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC")); |
6d15702c VL |
1602 | return -EIO; |
1603 | } | |
3f86b832 | 1604 | |
36bcbec7 | 1605 | sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL); |
3f86b832 | 1606 | if (!sbs) { |
6845118b | 1607 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kmalloc() failed")); |
635227ee | 1608 | return -ENOMEM; |
3f86b832 | 1609 | } |
6d15702c | 1610 | sbs->base = (val & 0xff00ull) >> 8; |
3f86b832 RT |
1611 | |
1612 | sbs->device = device; | |
3f86b832 RT |
1613 | |
1614 | strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME); | |
1615 | strcpy(acpi_device_class(device), ACPI_SBS_CLASS); | |
1616 | acpi_driver_data(device) = sbs; | |
1617 | ||
1618 | sbs->update_time = 0; | |
1619 | sbs->update_time2 = 0; | |
1620 | ||
1621 | result = acpi_ac_add(sbs); | |
1622 | if (result) { | |
6845118b | 1623 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed")); |
3f86b832 RT |
1624 | goto end; |
1625 | } | |
1626 | result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj); | |
1627 | if (ACPI_FAILURE(result)) { | |
6845118b VL |
1628 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1629 | "acpi_evaluate_integer() failed")); | |
3f86b832 RT |
1630 | result = -EIO; |
1631 | goto end; | |
1632 | } | |
1633 | ||
1634 | if (sbs_obj > 0) { | |
1635 | result = acpi_sbsm_get_info(sbs); | |
1636 | if (result) { | |
6845118b VL |
1637 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1638 | "acpi_sbsm_get_info() failed")); | |
3f86b832 RT |
1639 | goto end; |
1640 | } | |
1641 | sbs->sbsm_present = 1; | |
1642 | } | |
1643 | if (sbs->sbsm_present == 0) { | |
1644 | result = acpi_battery_add(sbs, 0); | |
1645 | if (result) { | |
6845118b VL |
1646 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1647 | "acpi_battery_add() failed")); | |
3f86b832 RT |
1648 | goto end; |
1649 | } | |
1650 | } else { | |
1651 | for (id = 0; id < MAX_SBS_BAT; id++) { | |
1652 | if ((sbs->sbsm_batteries_supported & (1 << id))) { | |
1653 | result = acpi_battery_add(sbs, id); | |
1654 | if (result) { | |
6845118b VL |
1655 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1656 | "acpi_battery_add() " | |
1657 | "failed")); | |
3f86b832 RT |
1658 | goto end; |
1659 | } | |
1660 | } | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | sbs->handle = device->handle; | |
1665 | ||
1666 | init_timer(&sbs->update_timer); | |
1667 | if (update_mode == QUEUE_UPDATE_MODE) { | |
1668 | status = acpi_os_execute(OSL_GPE_HANDLER, | |
50dd0969 | 1669 | acpi_sbs_update_queue, sbs); |
3f86b832 | 1670 | if (status != AE_OK) { |
6845118b VL |
1671 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1672 | "acpi_os_execute() failed")); | |
3f86b832 RT |
1673 | } |
1674 | } | |
1675 | sbs->update_time = update_time; | |
1676 | sbs->update_time2 = update_time2; | |
1677 | ||
1678 | printk(KERN_INFO PREFIX "%s [%s]\n", | |
1679 | acpi_device_name(device), acpi_device_bid(device)); | |
1680 | ||
1681 | end: | |
1682 | if (result) { | |
6d15702c | 1683 | acpi_sbs_remove(device, 0); |
3f86b832 RT |
1684 | } |
1685 | ||
635227ee | 1686 | return result; |
3f86b832 RT |
1687 | } |
1688 | ||
1689 | int acpi_sbs_remove(struct acpi_device *device, int type) | |
1690 | { | |
cece9014 | 1691 | struct acpi_sbs *sbs; |
3f86b832 RT |
1692 | int id; |
1693 | ||
963497c1 LV |
1694 | if (!device) { |
1695 | return -EINVAL; | |
1696 | } | |
1697 | ||
1698 | sbs = (struct acpi_sbs *)acpi_driver_data(device); | |
1699 | ||
1700 | if (!sbs) { | |
635227ee | 1701 | return -EINVAL; |
3f86b832 RT |
1702 | } |
1703 | ||
1704 | sbs->zombie = 1; | |
1705 | sbs->update_time = 0; | |
1706 | sbs->update_time2 = 0; | |
1707 | del_timer_sync(&sbs->update_timer); | |
1708 | acpi_os_wait_events_complete(NULL); | |
1709 | del_timer_sync(&sbs->update_timer); | |
1710 | ||
1711 | for (id = 0; id < MAX_SBS_BAT; id++) { | |
1712 | acpi_battery_remove(sbs, id); | |
1713 | } | |
1714 | ||
1715 | acpi_ac_remove(sbs); | |
1716 | ||
6d15702c VL |
1717 | acpi_driver_data(device) = NULL; |
1718 | ||
3f86b832 RT |
1719 | kfree(sbs); |
1720 | ||
635227ee | 1721 | return 0; |
3f86b832 RT |
1722 | } |
1723 | ||
1724 | static int __init acpi_sbs_init(void) | |
1725 | { | |
1726 | int result = 0; | |
1727 | ||
b20d2aeb LB |
1728 | if (acpi_disabled) |
1729 | return -ENODEV; | |
1730 | ||
3f86b832 RT |
1731 | init_MUTEX(&sbs_sem); |
1732 | ||
1733 | if (capacity_mode != DEF_CAPACITY_UNIT | |
1734 | && capacity_mode != MAH_CAPACITY_UNIT | |
1735 | && capacity_mode != MWH_CAPACITY_UNIT) { | |
6845118b VL |
1736 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_sbs_init: " |
1737 | "invalid capacity_mode = %d", capacity_mode)); | |
635227ee | 1738 | return -EINVAL; |
3f86b832 RT |
1739 | } |
1740 | ||
1741 | acpi_ac_dir = acpi_lock_ac_dir(); | |
1742 | if (!acpi_ac_dir) { | |
6845118b VL |
1743 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1744 | "acpi_lock_ac_dir() failed")); | |
635227ee | 1745 | return -ENODEV; |
3f86b832 RT |
1746 | } |
1747 | ||
1748 | acpi_battery_dir = acpi_lock_battery_dir(); | |
1749 | if (!acpi_battery_dir) { | |
6845118b VL |
1750 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1751 | "acpi_lock_battery_dir() failed")); | |
635227ee | 1752 | return -ENODEV; |
3f86b832 RT |
1753 | } |
1754 | ||
1755 | result = acpi_bus_register_driver(&acpi_sbs_driver); | |
1756 | if (result < 0) { | |
6845118b VL |
1757 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
1758 | "acpi_bus_register_driver() failed")); | |
635227ee | 1759 | return -ENODEV; |
3f86b832 RT |
1760 | } |
1761 | ||
635227ee | 1762 | return 0; |
3f86b832 RT |
1763 | } |
1764 | ||
1765 | static void __exit acpi_sbs_exit(void) | |
1766 | { | |
3f86b832 RT |
1767 | |
1768 | acpi_bus_unregister_driver(&acpi_sbs_driver); | |
1769 | ||
1770 | acpi_unlock_ac_dir(acpi_ac_dir); | |
1771 | acpi_ac_dir = NULL; | |
1772 | acpi_unlock_battery_dir(acpi_battery_dir); | |
1773 | acpi_battery_dir = NULL; | |
1774 | ||
635227ee | 1775 | return; |
3f86b832 RT |
1776 | } |
1777 | ||
1778 | module_init(acpi_sbs_init); | |
1779 | module_exit(acpi_sbs_exit); |