xref: /openbmc/linux/drivers/acpi/sbs.c (revision c21b37f6)
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>
33 #include <linux/timer.h>
34 #include <linux/jiffies.h>
35 #include <linux/delay.h>
36 
37 #define ACPI_SBS_COMPONENT		0x00080000
38 #define ACPI_SBS_CLASS			"sbs"
39 #define ACPI_AC_CLASS			"ac_adapter"
40 #define ACPI_BATTERY_CLASS		"battery"
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 
56 ACPI_MODULE_NAME("sbs");
57 
58 MODULE_AUTHOR("Rich Townsend");
59 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
60 MODULE_LICENSE("GPL");
61 
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 
112 #define	UPDATE_DELAY	10
113 
114 /* 0 - every time, > 0 - by update_time */
115 static unsigned int update_time = 120;
116 
117 static unsigned int capacity_mode = CAPACITY_UNIT;
118 
119 module_param(update_time, uint, 0644);
120 module_param(capacity_mode, uint, 0444);
121 
122 static int acpi_sbs_add(struct acpi_device *device);
123 static int acpi_sbs_remove(struct acpi_device *device, int type);
124 static int acpi_sbs_resume(struct acpi_device *device);
125 
126 static const struct acpi_device_id sbs_device_ids[] = {
127 	{"ACPI0001", 0},
128 	{"ACPI0005", 0},
129 	{"", 0},
130 };
131 MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
132 
133 static struct acpi_driver acpi_sbs_driver = {
134 	.name = "sbs",
135 	.class = ACPI_SBS_CLASS,
136 	.ids = sbs_device_ids,
137 	.ops = {
138 		.add = acpi_sbs_add,
139 		.remove = acpi_sbs_remove,
140 		.resume = acpi_sbs_resume,
141 		},
142 };
143 
144 struct acpi_ac {
145 	int ac_present;
146 };
147 
148 struct acpi_battery_info {
149 	int capacity_mode;
150 	s16 full_charge_capacity;
151 	s16 design_capacity;
152 	s16 design_voltage;
153 	int vscale;
154 	int ipscale;
155 	s16 serial_number;
156 	char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
157 	char device_name[ACPI_SBS_BLOCK_MAX + 3];
158 	char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
159 };
160 
161 struct acpi_battery_state {
162 	s16 voltage;
163 	s16 amperage;
164 	s16 remaining_capacity;
165 	s16 battery_state;
166 };
167 
168 struct acpi_battery_alarm {
169 	s16 remaining_capacity;
170 };
171 
172 struct acpi_battery {
173 	int alive;
174 	int id;
175 	int init_state;
176 	int battery_present;
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 	int base;
186 	struct acpi_device *device;
187 	struct mutex mutex;
188 	int sbsm_present;
189 	int sbsm_batteries_supported;
190 	struct proc_dir_entry *ac_entry;
191 	struct acpi_ac ac;
192 	struct acpi_battery battery[MAX_SBS_BAT];
193 	int zombie;
194 	struct timer_list update_timer;
195 	int run_cnt;
196 	int update_proc_flg;
197 };
198 
199 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
200 static void acpi_sbs_update_time(void *data);
201 
202 union sbs_rw_data {
203 	u16 word;
204 	u8 block[ACPI_SBS_BLOCK_MAX + 2];
205 };
206 
207 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
208 			      char read_write, u8 command, int size,
209 			      union sbs_rw_data *data);
210 
211 /* --------------------------------------------------------------------------
212                                SMBus Communication
213    -------------------------------------------------------------------------- */
214 
215 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
216 {
217 	u8 val;
218 	int err;
219 
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 }
227 
228 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
229 {
230 	int err;
231 
232 	err = ec_write(sbs->base + address, data);
233 	return (err);
234 }
235 
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;
260 		break;
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;
271 		break;
272 	default:
273 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
274 				"unsupported transaction %d", size));
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,
294 				"transaction %d error", size));
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];
308 		break;
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;
318 		break;
319 	default:
320 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
321 				"unsupported transaction %d", size));
322 		return (-1);
323 	}
324 
325 	return (0);
326 }
327 
328 static int
329 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
330 {
331 	union sbs_rw_data data;
332 	int result = 0;
333 
334 	result = acpi_ec_sbs_access(sbs, addr,
335 				    ACPI_SBS_SMBUS_READ, func,
336 				    ACPI_SBS_WORD_DATA, &data);
337 	if (result) {
338 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
339 				"acpi_ec_sbs_access() failed"));
340 	} else {
341 		*word = data.word;
342 	}
343 
344 	return result;
345 }
346 
347 static int
348 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
349 {
350 	union sbs_rw_data data;
351 	int result = 0;
352 
353 	result = acpi_ec_sbs_access(sbs, addr,
354 				    ACPI_SBS_SMBUS_READ, func,
355 				    ACPI_SBS_BLOCK_DATA, &data);
356 	if (result) {
357 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
358 				"acpi_ec_sbs_access() failed"));
359 	} else {
360 		strncpy(str, (const char *)data.block + 1, data.block[0]);
361 		str[data.block[0]] = 0;
362 	}
363 
364 	return result;
365 }
366 
367 static int
368 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
369 {
370 	union sbs_rw_data data;
371 	int result = 0;
372 
373 	data.word = word;
374 
375 	result = acpi_ec_sbs_access(sbs, addr,
376 				    ACPI_SBS_SMBUS_WRITE, func,
377 				    ACPI_SBS_WORD_DATA, &data);
378 	if (result) {
379 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
380 				"acpi_ec_sbs_access() failed"));
381 	}
382 
383 	return result;
384 }
385 
386 static int sbs_zombie(struct acpi_sbs *sbs)
387 {
388 	return (sbs->zombie);
389 }
390 
391 static int sbs_mutex_lock(struct acpi_sbs *sbs)
392 {
393 	if (sbs_zombie(sbs)) {
394 		return -ENODEV;
395 	}
396 	mutex_lock(&sbs->mutex);
397 	return 0;
398 }
399 
400 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
401 {
402 	mutex_unlock(&sbs->mutex);
403 }
404 
405 /* --------------------------------------------------------------------------
406                             Smart Battery System Management
407    -------------------------------------------------------------------------- */
408 
409 static int acpi_check_update_proc(struct acpi_sbs *sbs)
410 {
411 	acpi_status status = AE_OK;
412 
413 	if (update_time == 0) {
414 		sbs->update_proc_flg = 0;
415 		return 0;
416 	}
417 	if (sbs->update_proc_flg == 0) {
418 		status = acpi_os_execute(OSL_GPE_HANDLER,
419 					 acpi_sbs_update_time, sbs);
420 		if (status != AE_OK) {
421 			ACPI_EXCEPTION((AE_INFO, status,
422 					"acpi_os_execute() failed"));
423 			return 1;
424 		}
425 		sbs->update_proc_flg = 1;
426 	}
427 	return 0;
428 }
429 
430 static int acpi_sbs_generate_event(struct acpi_device *device,
431 				   int event, int state, char *bid, char *class)
432 {
433 	char bid_saved[5];
434 	char class_saved[20];
435 	int result = 0;
436 
437 	strcpy(bid_saved, acpi_device_bid(device));
438 	strcpy(class_saved, acpi_device_class(device));
439 
440 	strcpy(acpi_device_bid(device), bid);
441 	strcpy(acpi_device_class(device), class);
442 
443 	result = acpi_bus_generate_event(device, event, state);
444 
445 	strcpy(acpi_device_bid(device), bid_saved);
446 	strcpy(acpi_device_class(device), class_saved);
447 
448 	return result;
449 }
450 
451 static int acpi_battery_get_present(struct acpi_battery *battery)
452 {
453 	s16 state;
454 	int result = 0;
455 	int is_present = 0;
456 
457 	result = acpi_sbs_read_word(battery->sbs,
458 				    ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
459 	if (result) {
460 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
461 				"acpi_sbs_read_word() failed"));
462 	}
463 	if (!result) {
464 		is_present = (state & 0x000f) & (1 << battery->id);
465 	}
466 	battery->battery_present = is_present;
467 
468 	return result;
469 }
470 
471 static int acpi_battery_select(struct acpi_battery *battery)
472 {
473 	struct acpi_sbs *sbs = battery->sbs;
474 	int result = 0;
475 	s16 state;
476 	int foo;
477 
478 	if (sbs->sbsm_present) {
479 
480 		/* Take special care not to knobble other nibbles of
481 		 * state (aka selector_state), since
482 		 * it causes charging to halt on SBSELs */
483 
484 		result =
485 		    acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
486 		if (result) {
487 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
488 					"acpi_sbs_read_word() failed"));
489 			goto end;
490 		}
491 
492 		foo = (state & 0x0fff) | (1 << (battery->id + 12));
493 		result =
494 		    acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
495 		if (result) {
496 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
497 					"acpi_sbs_write_word() failed"));
498 			goto end;
499 		}
500 	}
501 
502       end:
503 	return result;
504 }
505 
506 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
507 {
508 	int result = 0;
509 	s16 battery_system_info;
510 
511 	result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
512 				    &battery_system_info);
513 	if (result) {
514 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
515 				"acpi_sbs_read_word() failed"));
516 		goto end;
517 	}
518 	sbs->sbsm_present = 1;
519 	sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
520 
521       end:
522 
523 	return result;
524 }
525 
526 static int acpi_battery_get_info(struct acpi_battery *battery)
527 {
528 	struct acpi_sbs *sbs = battery->sbs;
529 	int result = 0;
530 	s16 battery_mode;
531 	s16 specification_info;
532 
533 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
534 				    &battery_mode);
535 	if (result) {
536 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
537 				"acpi_sbs_read_word() failed"));
538 		goto end;
539 	}
540 	battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
541 
542 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
543 				    &battery->info.full_charge_capacity);
544 	if (result) {
545 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
546 				"acpi_sbs_read_word() failed"));
547 		goto end;
548 	}
549 
550 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
551 				    &battery->info.design_capacity);
552 
553 	if (result) {
554 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
555 				"acpi_sbs_read_word() failed"));
556 		goto end;
557 	}
558 
559 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
560 				    &battery->info.design_voltage);
561 	if (result) {
562 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
563 				"acpi_sbs_read_word() failed"));
564 		goto end;
565 	}
566 
567 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
568 				    &specification_info);
569 	if (result) {
570 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
571 				"acpi_sbs_read_word() failed"));
572 		goto end;
573 	}
574 
575 	switch ((specification_info & 0x0f00) >> 8) {
576 	case 1:
577 		battery->info.vscale = 10;
578 		break;
579 	case 2:
580 		battery->info.vscale = 100;
581 		break;
582 	case 3:
583 		battery->info.vscale = 1000;
584 		break;
585 	default:
586 		battery->info.vscale = 1;
587 	}
588 
589 	switch ((specification_info & 0xf000) >> 12) {
590 	case 1:
591 		battery->info.ipscale = 10;
592 		break;
593 	case 2:
594 		battery->info.ipscale = 100;
595 		break;
596 	case 3:
597 		battery->info.ipscale = 1000;
598 		break;
599 	default:
600 		battery->info.ipscale = 1;
601 	}
602 
603 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
604 				    &battery->info.serial_number);
605 	if (result) {
606 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
607 				"acpi_sbs_read_word() failed"));
608 		goto end;
609 	}
610 
611 	result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
612 				   battery->info.manufacturer_name);
613 	if (result) {
614 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
615 				"acpi_sbs_read_str() failed"));
616 		goto end;
617 	}
618 
619 	result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
620 				   battery->info.device_name);
621 	if (result) {
622 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
623 				"acpi_sbs_read_str() failed"));
624 		goto end;
625 	}
626 
627 	result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
628 				   battery->info.device_chemistry);
629 	if (result) {
630 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
631 				"acpi_sbs_read_str() failed"));
632 		goto end;
633 	}
634 
635       end:
636 	return result;
637 }
638 
639 static int acpi_battery_get_state(struct acpi_battery *battery)
640 {
641 	struct acpi_sbs *sbs = battery->sbs;
642 	int result = 0;
643 
644 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
645 				    &battery->state.voltage);
646 	if (result) {
647 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
648 				"acpi_sbs_read_word() failed"));
649 		goto end;
650 	}
651 
652 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
653 				    &battery->state.amperage);
654 	if (result) {
655 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
656 				"acpi_sbs_read_word() failed"));
657 		goto end;
658 	}
659 
660 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
661 				    &battery->state.remaining_capacity);
662 	if (result) {
663 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
664 				"acpi_sbs_read_word() failed"));
665 		goto end;
666 	}
667 
668 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
669 				    &battery->state.battery_state);
670 	if (result) {
671 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
672 				"acpi_sbs_read_word() failed"));
673 		goto end;
674 	}
675 
676       end:
677 	return result;
678 }
679 
680 static int acpi_battery_get_alarm(struct acpi_battery *battery)
681 {
682 	struct acpi_sbs *sbs = battery->sbs;
683 	int result = 0;
684 
685 	result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
686 				    &battery->alarm.remaining_capacity);
687 	if (result) {
688 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
689 				"acpi_sbs_read_word() failed"));
690 		goto end;
691 	}
692 
693       end:
694 
695 	return result;
696 }
697 
698 static int acpi_battery_set_alarm(struct acpi_battery *battery,
699 				  unsigned long alarm)
700 {
701 	struct acpi_sbs *sbs = battery->sbs;
702 	int result = 0;
703 	s16 battery_mode;
704 	int foo;
705 
706 	result = acpi_battery_select(battery);
707 	if (result) {
708 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
709 				"acpi_battery_select() failed"));
710 		goto end;
711 	}
712 
713 	/* If necessary, enable the alarm */
714 
715 	if (alarm > 0) {
716 		result =
717 		    acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
718 				       &battery_mode);
719 		if (result) {
720 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
721 					"acpi_sbs_read_word() failed"));
722 			goto end;
723 		}
724 
725 		result =
726 		    acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
727 					battery_mode & 0xbfff);
728 		if (result) {
729 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
730 					"acpi_sbs_write_word() failed"));
731 			goto end;
732 		}
733 	}
734 
735 	foo = alarm / (battery->info.capacity_mode ? 10 : 1);
736 	result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
737 	if (result) {
738 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
739 				"acpi_sbs_write_word() failed"));
740 		goto end;
741 	}
742 
743       end:
744 
745 	return result;
746 }
747 
748 static int acpi_battery_set_mode(struct acpi_battery *battery)
749 {
750 	struct acpi_sbs *sbs = battery->sbs;
751 	int result = 0;
752 	s16 battery_mode;
753 
754 	if (capacity_mode == DEF_CAPACITY_UNIT) {
755 		goto end;
756 	}
757 
758 	result = acpi_sbs_read_word(sbs,
759 				    ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
760 	if (result) {
761 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
762 				"acpi_sbs_read_word() failed"));
763 		goto end;
764 	}
765 
766 	if (capacity_mode == MAH_CAPACITY_UNIT) {
767 		battery_mode &= 0x7fff;
768 	} else {
769 		battery_mode |= 0x8000;
770 	}
771 	result = acpi_sbs_write_word(sbs,
772 				     ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
773 	if (result) {
774 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
775 				"acpi_sbs_write_word() failed"));
776 		goto end;
777 	}
778 
779 	result = acpi_sbs_read_word(sbs,
780 				    ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
781 	if (result) {
782 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
783 				"acpi_sbs_read_word() failed"));
784 		goto end;
785 	}
786 
787       end:
788 	return result;
789 }
790 
791 static int acpi_battery_init(struct acpi_battery *battery)
792 {
793 	int result = 0;
794 
795 	result = acpi_battery_select(battery);
796 	if (result) {
797 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
798 				"acpi_battery_select() failed"));
799 		goto end;
800 	}
801 
802 	result = acpi_battery_set_mode(battery);
803 	if (result) {
804 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
805 				"acpi_battery_set_mode() failed"));
806 		goto end;
807 	}
808 
809 	result = acpi_battery_get_info(battery);
810 	if (result) {
811 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
812 				"acpi_battery_get_info() failed"));
813 		goto end;
814 	}
815 
816 	result = acpi_battery_get_state(battery);
817 	if (result) {
818 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
819 				"acpi_battery_get_state() failed"));
820 		goto end;
821 	}
822 
823 	result = acpi_battery_get_alarm(battery);
824 	if (result) {
825 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
826 				"acpi_battery_get_alarm() failed"));
827 		goto end;
828 	}
829 
830       end:
831 	return result;
832 }
833 
834 static int acpi_ac_get_present(struct acpi_sbs *sbs)
835 {
836 	int result = 0;
837 	s16 charger_status;
838 
839 	result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
840 				    &charger_status);
841 
842 	if (result) {
843 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
844 				"acpi_sbs_read_word() failed"));
845 		goto end;
846 	}
847 
848 	sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
849 
850       end:
851 
852 	return result;
853 }
854 
855 /* --------------------------------------------------------------------------
856                               FS Interface (/proc/acpi)
857    -------------------------------------------------------------------------- */
858 
859 /* Generic Routines */
860 
861 static int
862 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
863 			struct proc_dir_entry *parent_dir,
864 			char *dir_name,
865 			struct file_operations *info_fops,
866 			struct file_operations *state_fops,
867 			struct file_operations *alarm_fops, void *data)
868 {
869 	struct proc_dir_entry *entry = NULL;
870 
871 	if (!*dir) {
872 		*dir = proc_mkdir(dir_name, parent_dir);
873 		if (!*dir) {
874 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
875 					"proc_mkdir() failed"));
876 			return -ENODEV;
877 		}
878 		(*dir)->owner = THIS_MODULE;
879 	}
880 
881 	/* 'info' [R] */
882 	if (info_fops) {
883 		entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
884 		if (!entry) {
885 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
886 					"create_proc_entry() failed"));
887 		} else {
888 			entry->proc_fops = info_fops;
889 			entry->data = data;
890 			entry->owner = THIS_MODULE;
891 		}
892 	}
893 
894 	/* 'state' [R] */
895 	if (state_fops) {
896 		entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
897 		if (!entry) {
898 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
899 					"create_proc_entry() failed"));
900 		} else {
901 			entry->proc_fops = state_fops;
902 			entry->data = data;
903 			entry->owner = THIS_MODULE;
904 		}
905 	}
906 
907 	/* 'alarm' [R/W] */
908 	if (alarm_fops) {
909 		entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
910 		if (!entry) {
911 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
912 					"create_proc_entry() failed"));
913 		} else {
914 			entry->proc_fops = alarm_fops;
915 			entry->data = data;
916 			entry->owner = THIS_MODULE;
917 		}
918 	}
919 
920 	return 0;
921 }
922 
923 static void
924 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
925 			   struct proc_dir_entry *parent_dir)
926 {
927 
928 	if (*dir) {
929 		remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
930 		remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
931 		remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
932 		remove_proc_entry((*dir)->name, parent_dir);
933 		*dir = NULL;
934 	}
935 
936 }
937 
938 /* Smart Battery Interface */
939 
940 static struct proc_dir_entry *acpi_battery_dir = NULL;
941 
942 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
943 {
944 	struct acpi_battery *battery = seq->private;
945 	struct acpi_sbs *sbs = battery->sbs;
946 	int cscale;
947 	int result = 0;
948 
949 	if (sbs_mutex_lock(sbs)) {
950 		return -ENODEV;
951 	}
952 
953 	result = acpi_check_update_proc(sbs);
954 	if (result)
955 		goto end;
956 
957 	if (update_time == 0) {
958 		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
959 		if (result) {
960 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
961 					"acpi_sbs_update_run() failed"));
962 		}
963 	}
964 
965 	if (battery->battery_present) {
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\n",
978 		   battery->info.design_capacity * cscale,
979 		   battery->info.capacity_mode ? "0 mWh" : " mAh");
980 
981 	seq_printf(seq, "last full capacity:      %i%s\n",
982 		   battery->info.full_charge_capacity * cscale,
983 		   battery->info.capacity_mode ? "0 mWh" : " mAh");
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 	sbs_mutex_unlock(sbs);
1010 
1011 	return result;
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 = seq->private;
1022 	struct acpi_sbs *sbs = battery->sbs;
1023 	int result = 0;
1024 	int cscale;
1025 	int foo;
1026 
1027 	if (sbs_mutex_lock(sbs)) {
1028 		return -ENODEV;
1029 	}
1030 
1031 	result = acpi_check_update_proc(sbs);
1032 	if (result)
1033 		goto end;
1034 
1035 	if (update_time == 0) {
1036 		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1037 		if (result) {
1038 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1039 					"acpi_sbs_update_run() failed"));
1040 		}
1041 	}
1042 
1043 	if (battery->battery_present) {
1044 		seq_printf(seq, "present:                 yes\n");
1045 	} else {
1046 		seq_printf(seq, "present:                 no\n");
1047 		goto end;
1048 	}
1049 
1050 	if (battery->info.capacity_mode) {
1051 		cscale = battery->info.vscale * battery->info.ipscale;
1052 	} else {
1053 		cscale = battery->info.ipscale;
1054 	}
1055 
1056 	if (battery->state.battery_state & 0x0010) {
1057 		seq_printf(seq, "capacity state:          critical\n");
1058 	} else {
1059 		seq_printf(seq, "capacity state:          ok\n");
1060 	}
1061 
1062 	foo = (s16) battery->state.amperage * battery->info.ipscale;
1063 	if (battery->info.capacity_mode) {
1064 		foo = foo * battery->info.design_voltage / 1000;
1065 	}
1066 	if (battery->state.amperage < 0) {
1067 		seq_printf(seq, "charging state:          discharging\n");
1068 		seq_printf(seq, "present rate:            %d %s\n",
1069 			   -foo, battery->info.capacity_mode ? "mW" : "mA");
1070 	} else if (battery->state.amperage > 0) {
1071 		seq_printf(seq, "charging state:          charging\n");
1072 		seq_printf(seq, "present rate:            %d %s\n",
1073 			   foo, battery->info.capacity_mode ? "mW" : "mA");
1074 	} else {
1075 		seq_printf(seq, "charging state:          charged\n");
1076 		seq_printf(seq, "present rate:            0 %s\n",
1077 			   battery->info.capacity_mode ? "mW" : "mA");
1078 	}
1079 
1080 	seq_printf(seq, "remaining capacity:      %i%s\n",
1081 		   battery->state.remaining_capacity * cscale,
1082 		   battery->info.capacity_mode ? "0 mWh" : " mAh");
1083 
1084 	seq_printf(seq, "present voltage:         %i mV\n",
1085 		   battery->state.voltage * battery->info.vscale);
1086 
1087       end:
1088 
1089 	sbs_mutex_unlock(sbs);
1090 
1091 	return result;
1092 }
1093 
1094 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1095 {
1096 	return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1097 }
1098 
1099 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1100 {
1101 	struct acpi_battery *battery = seq->private;
1102 	struct acpi_sbs *sbs = battery->sbs;
1103 	int result = 0;
1104 	int cscale;
1105 
1106 	if (sbs_mutex_lock(sbs)) {
1107 		return -ENODEV;
1108 	}
1109 
1110 	result = acpi_check_update_proc(sbs);
1111 	if (result)
1112 		goto end;
1113 
1114 	if (update_time == 0) {
1115 		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1116 		if (result) {
1117 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1118 					"acpi_sbs_update_run() failed"));
1119 		}
1120 	}
1121 
1122 	if (!battery->battery_present) {
1123 		seq_printf(seq, "present:                 no\n");
1124 		goto end;
1125 	}
1126 
1127 	if (battery->info.capacity_mode) {
1128 		cscale = battery->info.vscale * battery->info.ipscale;
1129 	} else {
1130 		cscale = battery->info.ipscale;
1131 	}
1132 
1133 	seq_printf(seq, "alarm:                   ");
1134 	if (battery->alarm.remaining_capacity) {
1135 		seq_printf(seq, "%i%s\n",
1136 			   battery->alarm.remaining_capacity * cscale,
1137 			   battery->info.capacity_mode ? "0 mWh" : " mAh");
1138 	} else {
1139 		seq_printf(seq, "disabled\n");
1140 	}
1141 
1142       end:
1143 
1144 	sbs_mutex_unlock(sbs);
1145 
1146 	return result;
1147 }
1148 
1149 static ssize_t
1150 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1151 			 size_t count, loff_t * ppos)
1152 {
1153 	struct seq_file *seq = file->private_data;
1154 	struct acpi_battery *battery = seq->private;
1155 	struct acpi_sbs *sbs = battery->sbs;
1156 	char alarm_string[12] = { '\0' };
1157 	int result, old_alarm, new_alarm;
1158 
1159 	if (sbs_mutex_lock(sbs)) {
1160 		return -ENODEV;
1161 	}
1162 
1163 	result = acpi_check_update_proc(sbs);
1164 	if (result)
1165 		goto end;
1166 
1167 	if (!battery->battery_present) {
1168 		result = -ENODEV;
1169 		goto end;
1170 	}
1171 
1172 	if (count > sizeof(alarm_string) - 1) {
1173 		result = -EINVAL;
1174 		goto end;
1175 	}
1176 
1177 	if (copy_from_user(alarm_string, buffer, count)) {
1178 		result = -EFAULT;
1179 		goto end;
1180 	}
1181 
1182 	alarm_string[count] = 0;
1183 
1184 	old_alarm = battery->alarm.remaining_capacity;
1185 	new_alarm = simple_strtoul(alarm_string, NULL, 0);
1186 
1187 	result = acpi_battery_set_alarm(battery, new_alarm);
1188 	if (result) {
1189 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1190 				"acpi_battery_set_alarm() failed"));
1191 		acpi_battery_set_alarm(battery, old_alarm);
1192 		goto end;
1193 	}
1194 	result = acpi_battery_get_alarm(battery);
1195 	if (result) {
1196 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1197 				"acpi_battery_get_alarm() failed"));
1198 		acpi_battery_set_alarm(battery, old_alarm);
1199 		goto end;
1200 	}
1201 
1202       end:
1203 	sbs_mutex_unlock(sbs);
1204 
1205 	if (result) {
1206 		return result;
1207 	} else {
1208 		return count;
1209 	}
1210 }
1211 
1212 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1213 {
1214 	return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1215 }
1216 
1217 static struct file_operations acpi_battery_info_fops = {
1218 	.open = acpi_battery_info_open_fs,
1219 	.read = seq_read,
1220 	.llseek = seq_lseek,
1221 	.release = single_release,
1222 	.owner = THIS_MODULE,
1223 };
1224 
1225 static struct file_operations acpi_battery_state_fops = {
1226 	.open = acpi_battery_state_open_fs,
1227 	.read = seq_read,
1228 	.llseek = seq_lseek,
1229 	.release = single_release,
1230 	.owner = THIS_MODULE,
1231 };
1232 
1233 static struct file_operations acpi_battery_alarm_fops = {
1234 	.open = acpi_battery_alarm_open_fs,
1235 	.read = seq_read,
1236 	.write = acpi_battery_write_alarm,
1237 	.llseek = seq_lseek,
1238 	.release = single_release,
1239 	.owner = THIS_MODULE,
1240 };
1241 
1242 /* Legacy AC Adapter Interface */
1243 
1244 static struct proc_dir_entry *acpi_ac_dir = NULL;
1245 
1246 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1247 {
1248 	struct acpi_sbs *sbs = seq->private;
1249 	int result;
1250 
1251 	if (sbs_mutex_lock(sbs)) {
1252 		return -ENODEV;
1253 	}
1254 
1255 	if (update_time == 0) {
1256 		result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1257 		if (result) {
1258 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1259 					"acpi_sbs_update_run() failed"));
1260 		}
1261 	}
1262 
1263 	seq_printf(seq, "state:                   %s\n",
1264 		   sbs->ac.ac_present ? "on-line" : "off-line");
1265 
1266 	sbs_mutex_unlock(sbs);
1267 
1268 	return 0;
1269 }
1270 
1271 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1272 {
1273 	return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1274 }
1275 
1276 static struct file_operations acpi_ac_state_fops = {
1277 	.open = acpi_ac_state_open_fs,
1278 	.read = seq_read,
1279 	.llseek = seq_lseek,
1280 	.release = single_release,
1281 	.owner = THIS_MODULE,
1282 };
1283 
1284 /* --------------------------------------------------------------------------
1285                                  Driver Interface
1286    -------------------------------------------------------------------------- */
1287 
1288 /* Smart Battery */
1289 
1290 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1291 {
1292 	int is_present;
1293 	int result;
1294 	char dir_name[32];
1295 	struct acpi_battery *battery;
1296 
1297 	battery = &sbs->battery[id];
1298 
1299 	battery->alive = 0;
1300 
1301 	battery->init_state = 0;
1302 	battery->id = id;
1303 	battery->sbs = sbs;
1304 
1305 	result = acpi_battery_select(battery);
1306 	if (result) {
1307 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1308 				"acpi_battery_select() failed"));
1309 		goto end;
1310 	}
1311 
1312 	result = acpi_battery_get_present(battery);
1313 	if (result) {
1314 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1315 				"acpi_battery_get_present() failed"));
1316 		goto end;
1317 	}
1318 
1319 	is_present = battery->battery_present;
1320 
1321 	if (is_present) {
1322 		result = acpi_battery_init(battery);
1323 		if (result) {
1324 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1325 					"acpi_battery_init() failed"));
1326 			goto end;
1327 		}
1328 		battery->init_state = 1;
1329 	}
1330 
1331 	sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1332 
1333 	result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1334 					 acpi_battery_dir,
1335 					 dir_name,
1336 					 &acpi_battery_info_fops,
1337 					 &acpi_battery_state_fops,
1338 					 &acpi_battery_alarm_fops, battery);
1339 	if (result) {
1340 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1341 				"acpi_sbs_generic_add_fs() failed"));
1342 		goto end;
1343 	}
1344 	battery->alive = 1;
1345 
1346 	printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1347 	       ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1348 	       sbs->battery->battery_present ? "present" : "absent");
1349 
1350       end:
1351 	return result;
1352 }
1353 
1354 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1355 {
1356 
1357 	if (sbs->battery[id].battery_entry) {
1358 		acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1359 					   acpi_battery_dir);
1360 	}
1361 }
1362 
1363 static int acpi_ac_add(struct acpi_sbs *sbs)
1364 {
1365 	int result;
1366 
1367 	result = acpi_ac_get_present(sbs);
1368 	if (result) {
1369 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1370 				"acpi_ac_get_present() failed"));
1371 		goto end;
1372 	}
1373 
1374 	result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1375 					 acpi_ac_dir,
1376 					 ACPI_AC_DIR_NAME,
1377 					 NULL, &acpi_ac_state_fops, NULL, sbs);
1378 	if (result) {
1379 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1380 				"acpi_sbs_generic_add_fs() failed"));
1381 		goto end;
1382 	}
1383 
1384 	printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1385 	       ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1386 	       ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1387 
1388       end:
1389 
1390 	return result;
1391 }
1392 
1393 static void acpi_ac_remove(struct acpi_sbs *sbs)
1394 {
1395 
1396 	if (sbs->ac_entry) {
1397 		acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1398 	}
1399 }
1400 
1401 static void acpi_sbs_update_time_run(unsigned long data)
1402 {
1403 	acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1404 }
1405 
1406 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1407 {
1408 	struct acpi_battery *battery;
1409 	int result = 0, cnt;
1410 	int old_ac_present = -1;
1411 	int old_battery_present = -1;
1412 	int new_ac_present = -1;
1413 	int new_battery_present = -1;
1414 	int id_min = 0, id_max = MAX_SBS_BAT - 1;
1415 	char dir_name[32];
1416 	int do_battery_init = 0, do_ac_init = 0;
1417 	int old_remaining_capacity = 0;
1418 	int update_ac = 1, update_battery = 1;
1419 	int up_tm = update_time;
1420 
1421 	if (sbs_zombie(sbs)) {
1422 		goto end;
1423 	}
1424 
1425 	if (id >= 0) {
1426 		id_min = id_max = id;
1427 	}
1428 
1429 	if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1430 		cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1431 		if (sbs->run_cnt % cnt != 0) {
1432 			update_battery = 0;
1433 		}
1434 	}
1435 
1436 	sbs->run_cnt++;
1437 
1438 	if (!update_ac && !update_battery) {
1439 		goto end;
1440 	}
1441 
1442 	old_ac_present = sbs->ac.ac_present;
1443 
1444 	result = acpi_ac_get_present(sbs);
1445 	if (result) {
1446 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1447 				"acpi_ac_get_present() failed"));
1448 	}
1449 
1450 	new_ac_present = sbs->ac.ac_present;
1451 
1452 	do_ac_init = (old_ac_present != new_ac_present);
1453 	if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
1454 		do_ac_init = 1;
1455 	}
1456 
1457 	if (do_ac_init) {
1458 		result = acpi_sbs_generate_event(sbs->device,
1459 						 ACPI_SBS_AC_NOTIFY_STATUS,
1460 						 new_ac_present,
1461 						 ACPI_AC_DIR_NAME,
1462 						 ACPI_AC_CLASS);
1463 		if (result) {
1464 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1465 					"acpi_sbs_generate_event() failed"));
1466 		}
1467 	}
1468 
1469 	if (data_type == DATA_TYPE_COMMON) {
1470 		if (!do_ac_init && !update_battery) {
1471 			goto end;
1472 		}
1473 	}
1474 
1475 	if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1476 		goto end;
1477 	}
1478 
1479 	for (id = id_min; id <= id_max; id++) {
1480 		battery = &sbs->battery[id];
1481 		if (battery->alive == 0) {
1482 			continue;
1483 		}
1484 
1485 		old_remaining_capacity = battery->state.remaining_capacity;
1486 
1487 		old_battery_present = battery->battery_present;
1488 
1489 		result = acpi_battery_select(battery);
1490 		if (result) {
1491 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1492 					"acpi_battery_select() failed"));
1493 		}
1494 
1495 		result = acpi_battery_get_present(battery);
1496 		if (result) {
1497 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1498 					"acpi_battery_get_present() failed"));
1499 		}
1500 
1501 		new_battery_present = battery->battery_present;
1502 
1503 		do_battery_init = ((old_battery_present != new_battery_present)
1504 				   && new_battery_present);
1505 		if (!new_battery_present)
1506 			goto event;
1507 		if (do_ac_init || do_battery_init) {
1508 			result = acpi_battery_init(battery);
1509 			if (result) {
1510 				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1511 						"acpi_battery_init() "
1512 						"failed"));
1513 			}
1514 		}
1515 		if (sbs_zombie(sbs)) {
1516 			goto end;
1517 		}
1518 
1519 		if ((data_type == DATA_TYPE_COMMON
1520 		     || data_type == DATA_TYPE_INFO)
1521 		    && new_battery_present) {
1522 			result = acpi_battery_get_info(battery);
1523 			if (result) {
1524 				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1525 						"acpi_battery_get_info() failed"));
1526 			}
1527 		}
1528 		if (data_type == DATA_TYPE_INFO) {
1529 			continue;
1530 		}
1531 		if (sbs_zombie(sbs)) {
1532 			goto end;
1533 		}
1534 
1535 		if ((data_type == DATA_TYPE_COMMON
1536 		     || data_type == DATA_TYPE_STATE)
1537 		    && new_battery_present) {
1538 			result = acpi_battery_get_state(battery);
1539 			if (result) {
1540 				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1541 						"acpi_battery_get_state() failed"));
1542 			}
1543 		}
1544 		if (data_type == DATA_TYPE_STATE) {
1545 			goto event;
1546 		}
1547 		if (sbs_zombie(sbs)) {
1548 			goto end;
1549 		}
1550 
1551 		if ((data_type == DATA_TYPE_COMMON
1552 		     || data_type == DATA_TYPE_ALARM)
1553 		    && new_battery_present) {
1554 			result = acpi_battery_get_alarm(battery);
1555 			if (result) {
1556 				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1557 						"acpi_battery_get_alarm() "
1558 						"failed"));
1559 			}
1560 		}
1561 		if (data_type == DATA_TYPE_ALARM) {
1562 			continue;
1563 		}
1564 		if (sbs_zombie(sbs)) {
1565 			goto end;
1566 		}
1567 
1568 	      event:
1569 
1570 		if (old_battery_present != new_battery_present || do_ac_init ||
1571 		    old_remaining_capacity !=
1572 		    battery->state.remaining_capacity) {
1573 			sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1574 			result = acpi_sbs_generate_event(sbs->device,
1575 							 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1576 							 new_battery_present,
1577 							 dir_name,
1578 							 ACPI_BATTERY_CLASS);
1579 			if (result) {
1580 				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1581 						"acpi_sbs_generate_event() "
1582 						"failed"));
1583 			}
1584 		}
1585 	}
1586 
1587       end:
1588 
1589 	return result;
1590 }
1591 
1592 static void acpi_sbs_update_time(void *data)
1593 {
1594 	struct acpi_sbs *sbs = data;
1595 	unsigned long delay = -1;
1596 	int result;
1597 	unsigned int up_tm = update_time;
1598 
1599 	if (sbs_mutex_lock(sbs))
1600 		return;
1601 
1602 	result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1603 	if (result) {
1604 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1605 				"acpi_sbs_update_run() failed"));
1606 	}
1607 
1608 	if (sbs_zombie(sbs)) {
1609 		goto end;
1610 	}
1611 
1612 	if (!up_tm) {
1613 		if (timer_pending(&sbs->update_timer))
1614 			del_timer(&sbs->update_timer);
1615 	} else {
1616 		delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1617 		delay = jiffies + HZ * delay;
1618 		if (timer_pending(&sbs->update_timer)) {
1619 			mod_timer(&sbs->update_timer, delay);
1620 		} else {
1621 			sbs->update_timer.data = (unsigned long)data;
1622 			sbs->update_timer.function = acpi_sbs_update_time_run;
1623 			sbs->update_timer.expires = delay;
1624 			add_timer(&sbs->update_timer);
1625 		}
1626 	}
1627 
1628       end:
1629 
1630 	sbs_mutex_unlock(sbs);
1631 }
1632 
1633 static int acpi_sbs_add(struct acpi_device *device)
1634 {
1635 	struct acpi_sbs *sbs = NULL;
1636 	int result = 0, remove_result = 0;
1637 	int id;
1638 	acpi_status status = AE_OK;
1639 	unsigned long val;
1640 
1641 	status =
1642 	    acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
1643 	if (ACPI_FAILURE(status)) {
1644 		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1645 		return -EIO;
1646 	}
1647 
1648 	sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1649 	if (!sbs) {
1650 		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
1651 		result = -ENOMEM;
1652 		goto end;
1653 	}
1654 
1655 	mutex_init(&sbs->mutex);
1656 
1657 	sbs_mutex_lock(sbs);
1658 
1659 	sbs->base = 0xff & (val >> 8);
1660 	sbs->device = device;
1661 
1662 	strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1663 	strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1664 	acpi_driver_data(device) = sbs;
1665 
1666 	result = acpi_ac_add(sbs);
1667 	if (result) {
1668 		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1669 		goto end;
1670 	}
1671 
1672 	acpi_sbsm_get_info(sbs);
1673 
1674 	if (!sbs->sbsm_present) {
1675 		result = acpi_battery_add(sbs, 0);
1676 		if (result) {
1677 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1678 					"acpi_battery_add() failed"));
1679 			goto end;
1680 		}
1681 	} else {
1682 		for (id = 0; id < MAX_SBS_BAT; id++) {
1683 			if ((sbs->sbsm_batteries_supported & (1 << id))) {
1684 				result = acpi_battery_add(sbs, id);
1685 				if (result) {
1686 					ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1687 							"acpi_battery_add() failed"));
1688 					goto end;
1689 				}
1690 			}
1691 		}
1692 	}
1693 
1694 	init_timer(&sbs->update_timer);
1695 	result = acpi_check_update_proc(sbs);
1696 	if (result)
1697 		goto end;
1698 
1699       end:
1700 
1701 	sbs_mutex_unlock(sbs);
1702 
1703 	if (result) {
1704 		remove_result = acpi_sbs_remove(device, 0);
1705 		if (remove_result) {
1706 			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1707 					"acpi_sbs_remove() failed"));
1708 		}
1709 	}
1710 
1711 	return result;
1712 }
1713 
1714 static int acpi_sbs_remove(struct acpi_device *device, int type)
1715 {
1716 	struct acpi_sbs *sbs;
1717 	int id;
1718 
1719 	if (!device) {
1720 		return -EINVAL;
1721 	}
1722 
1723 	sbs = acpi_driver_data(device);
1724 	if (!sbs) {
1725 		return -EINVAL;
1726 	}
1727 
1728 	sbs_mutex_lock(sbs);
1729 
1730 	sbs->zombie = 1;
1731 	del_timer_sync(&sbs->update_timer);
1732 	acpi_os_wait_events_complete(NULL);
1733 	del_timer_sync(&sbs->update_timer);
1734 
1735 	for (id = 0; id < MAX_SBS_BAT; id++) {
1736 		acpi_battery_remove(sbs, id);
1737 	}
1738 
1739 	acpi_ac_remove(sbs);
1740 
1741 	sbs_mutex_unlock(sbs);
1742 
1743 	mutex_destroy(&sbs->mutex);
1744 
1745 	kfree(sbs);
1746 
1747 	return 0;
1748 }
1749 
1750 static void acpi_sbs_rmdirs(void)
1751 {
1752 	if (acpi_ac_dir) {
1753 		acpi_unlock_ac_dir(acpi_ac_dir);
1754 		acpi_ac_dir = NULL;
1755 	}
1756 	if (acpi_battery_dir) {
1757 		acpi_unlock_battery_dir(acpi_battery_dir);
1758 		acpi_battery_dir = NULL;
1759 	}
1760 }
1761 
1762 static int acpi_sbs_resume(struct acpi_device *device)
1763 {
1764 	struct acpi_sbs *sbs;
1765 
1766 	if (!device)
1767 		return -EINVAL;
1768 
1769 	sbs = device->driver_data;
1770 
1771 	sbs->run_cnt = 0;
1772 
1773 	return 0;
1774 }
1775 
1776 static int __init acpi_sbs_init(void)
1777 {
1778 	int result = 0;
1779 
1780 	if (acpi_disabled)
1781 		return -ENODEV;
1782 
1783 	if (capacity_mode != DEF_CAPACITY_UNIT
1784 	    && capacity_mode != MAH_CAPACITY_UNIT
1785 	    && capacity_mode != MWH_CAPACITY_UNIT) {
1786 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1787 				"invalid capacity_mode = %d", capacity_mode));
1788 		return -EINVAL;
1789 	}
1790 
1791 	acpi_ac_dir = acpi_lock_ac_dir();
1792 	if (!acpi_ac_dir) {
1793 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1794 				"acpi_lock_ac_dir() failed"));
1795 		return -ENODEV;
1796 	}
1797 
1798 	acpi_battery_dir = acpi_lock_battery_dir();
1799 	if (!acpi_battery_dir) {
1800 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1801 				"acpi_lock_battery_dir() failed"));
1802 		acpi_sbs_rmdirs();
1803 		return -ENODEV;
1804 	}
1805 
1806 	result = acpi_bus_register_driver(&acpi_sbs_driver);
1807 	if (result < 0) {
1808 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1809 				"acpi_bus_register_driver() failed"));
1810 		acpi_sbs_rmdirs();
1811 		return -ENODEV;
1812 	}
1813 
1814 	return 0;
1815 }
1816 
1817 static void __exit acpi_sbs_exit(void)
1818 {
1819 	acpi_bus_unregister_driver(&acpi_sbs_driver);
1820 
1821 	acpi_sbs_rmdirs();
1822 
1823 	return;
1824 }
1825 
1826 module_init(acpi_sbs_init);
1827 module_exit(acpi_sbs_exit);
1828