sbs.c 28 KB

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