sbs.c 40 KB

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