sbs.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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", 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", 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", 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_EXCEPTION((AE_INFO, AE_ERROR,
  289. "acpi_ec_sbs_access() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  305. "acpi_ec_sbs_access() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  323. "acpi_ec_sbs_access() failed"));
  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_EXCEPTION((AE_INFO, AE_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_EXCEPTION((AE_INFO, AE_ERROR,
  385. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  393. "acpi_sbs_write_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  408. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  425. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  433. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  445. "acpi_sbs_read_word() failed"));
  446. goto end;
  447. }
  448. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
  449. &specification_info);
  450. if (result) {
  451. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  452. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  490. "acpi_sbs_read_str() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  497. "acpi_sbs_read_str() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  504. "acpi_sbs_read_str() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  528. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  536. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  544. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  552. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  560. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  568. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  583. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  600. "acpi_battery_select() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  610. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  618. "acpi_sbs_write_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  626. "acpi_sbs_write_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  643. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  655. "acpi_sbs_write_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  662. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  674. "acpi_battery_init() failed"));
  675. goto end;
  676. }
  677. result = acpi_battery_set_mode(battery);
  678. if (result) {
  679. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  680. "acpi_battery_set_mode() failed"));
  681. goto end;
  682. }
  683. result = acpi_battery_get_info(battery);
  684. if (result) {
  685. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  686. "acpi_battery_get_info() failed"));
  687. goto end;
  688. }
  689. result = acpi_battery_get_state(battery);
  690. if (result) {
  691. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  692. "acpi_battery_get_state() failed"));
  693. goto end;
  694. }
  695. result = acpi_battery_get_alarm(battery);
  696. if (result) {
  697. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  698. "acpi_battery_get_alarm() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  712. "acpi_sbs_read_word() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  736. "proc_mkdir() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  746. "create_proc_entry() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  758. "create_proc_entry() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  770. "create_proc_entry() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  806. "acpi_sbs_update_run() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  863. "acpi_sbs_update_run() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  925. "acpi_sbs_update_run() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  979. "acpi_battery_set_alarm() failed"));
  980. acpi_battery_set_alarm(battery, old_alarm);
  981. goto end;
  982. }
  983. result = acpi_battery_get_alarm(battery);
  984. if (result) {
  985. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  986. "acpi_battery_get_alarm() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1038. "acpi_sbs_update_run() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1075. "acpi_battery_select() failed"));
  1076. goto end;
  1077. }
  1078. result = acpi_battery_get_present(battery);
  1079. if (result) {
  1080. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1081. "acpi_battery_get_present() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1089. "acpi_battery_init() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1103. "acpi_sbs_generic_add_fs() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1123. "acpi_ac_get_present() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1132. "acpi_sbs_generic_add_fs() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1167. "acpi_ac_get_present() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1184. "acpi_battery_select() failed"));
  1185. }
  1186. if (sbs->zombie) {
  1187. goto end;
  1188. }
  1189. result = acpi_battery_get_present(battery);
  1190. if (result) {
  1191. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1192. "acpi_battery_get_present() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1213. "acpi_battery_init() "
  1214. "failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1227. "acpi_battery_get_alarm() "
  1228. "failed"));
  1229. }
  1230. if (data_type == DATA_TYPE_ALARM) {
  1231. continue;
  1232. }
  1233. result = acpi_battery_get_state(battery);
  1234. if (result) {
  1235. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1236. "acpi_battery_get_state() "
  1237. "failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1255. "acpi_sbs_generate_event() "
  1256. "failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1268. "acpi_sbs_generate_event() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1286. "acpi_sbs_generate_event() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1303. "acpi_sbs_update_run() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
  1331. return -EIO;
  1332. }
  1333. sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
  1334. if (!sbs) {
  1335. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kmalloc() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
  1348. goto end;
  1349. }
  1350. result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
  1351. if (ACPI_FAILURE(result)) {
  1352. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1353. "acpi_evaluate_integer() failed"));
  1354. result = -EIO;
  1355. goto end;
  1356. }
  1357. if (sbs_obj > 0) {
  1358. result = acpi_sbsm_get_info(sbs);
  1359. if (result) {
  1360. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1361. "acpi_sbsm_get_info() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1370. "acpi_battery_add() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1379. "acpi_battery_add() "
  1380. "failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR,
  1393. "acpi_os_execute() failed"));
  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_EXCEPTION((AE_INFO, AE_ERROR, "acpi_sbs_init: "
  1441. "invalid capacity_mode = %d", capacity_mode));
  1442. return -EINVAL;
  1443. }
  1444. acpi_ac_dir = acpi_lock_ac_dir();
  1445. if (!acpi_ac_dir) {
  1446. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1447. "acpi_lock_ac_dir() failed"));
  1448. return -ENODEV;
  1449. }
  1450. acpi_battery_dir = acpi_lock_battery_dir();
  1451. if (!acpi_battery_dir) {
  1452. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1453. "acpi_lock_battery_dir() failed"));
  1454. return -ENODEV;
  1455. }
  1456. result = acpi_bus_register_driver(&acpi_sbs_driver);
  1457. if (result < 0) {
  1458. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1459. "acpi_bus_register_driver() failed"));
  1460. return -ENODEV;
  1461. }
  1462. return 0;
  1463. }
  1464. static void __exit acpi_sbs_exit(void)
  1465. {
  1466. acpi_bus_unregister_driver(&acpi_sbs_driver);
  1467. acpi_unlock_ac_dir(acpi_ac_dir);
  1468. acpi_ac_dir = NULL;
  1469. acpi_unlock_battery_dir(acpi_battery_dir);
  1470. acpi_battery_dir = NULL;
  1471. return;
  1472. }
  1473. module_init(acpi_sbs_init);
  1474. module_exit(acpi_sbs_exit);