sbs.c 41 KB

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