sbs.c 41 KB

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