sbs.c 29 KB

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