sbs.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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_event(device, event, state);
  373. strcpy(acpi_device_bid(device), bid_saved);
  374. strcpy(acpi_device_class(device), class_saved);
  375. return result;
  376. }
  377. static int acpi_battery_get_present(struct acpi_battery *battery)
  378. {
  379. s16 state;
  380. int result = 0;
  381. int is_present = 0;
  382. result = acpi_sbs_read_word(battery->sbs,
  383. ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
  384. if (result) {
  385. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  386. "acpi_sbs_read_word() failed"));
  387. }
  388. if (!result) {
  389. is_present = (state & 0x000f) & (1 << battery->id);
  390. }
  391. battery->battery_present = is_present;
  392. return result;
  393. }
  394. static int acpi_battery_select(struct acpi_battery *battery)
  395. {
  396. struct acpi_sbs *sbs = battery->sbs;
  397. int result = 0;
  398. s16 state;
  399. int foo;
  400. if (sbs->sbsm_present) {
  401. /* Take special care not to knobble other nibbles of
  402. * state (aka selector_state), since
  403. * it causes charging to halt on SBSELs */
  404. result =
  405. acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
  406. if (result) {
  407. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  408. "acpi_sbs_read_word() failed"));
  409. goto end;
  410. }
  411. foo = (state & 0x0fff) | (1 << (battery->id + 12));
  412. result =
  413. acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
  414. if (result) {
  415. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  416. "acpi_sbs_write_word() failed"));
  417. goto end;
  418. }
  419. }
  420. end:
  421. return result;
  422. }
  423. static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
  424. {
  425. int result = 0;
  426. s16 battery_system_info;
  427. result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
  428. &battery_system_info);
  429. if (result) {
  430. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  431. "acpi_sbs_read_word() failed"));
  432. goto end;
  433. }
  434. sbs->sbsm_present = 1;
  435. sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
  436. end:
  437. return result;
  438. }
  439. static int acpi_battery_get_info(struct acpi_battery *battery)
  440. {
  441. struct acpi_sbs *sbs = battery->sbs;
  442. int result = 0;
  443. s16 battery_mode;
  444. s16 specification_info;
  445. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
  446. &battery_mode);
  447. if (result) {
  448. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  449. "acpi_sbs_read_word() failed"));
  450. goto end;
  451. }
  452. battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
  453. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
  454. &battery->info.full_charge_capacity);
  455. if (result) {
  456. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  457. "acpi_sbs_read_word() failed"));
  458. goto end;
  459. }
  460. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
  461. &battery->info.design_capacity);
  462. if (result) {
  463. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  464. "acpi_sbs_read_word() failed"));
  465. goto end;
  466. }
  467. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
  468. &battery->info.design_voltage);
  469. if (result) {
  470. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  471. "acpi_sbs_read_word() failed"));
  472. goto end;
  473. }
  474. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
  475. &specification_info);
  476. if (result) {
  477. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  478. "acpi_sbs_read_word() failed"));
  479. goto end;
  480. }
  481. switch ((specification_info & 0x0f00) >> 8) {
  482. case 1:
  483. battery->info.vscale = 10;
  484. break;
  485. case 2:
  486. battery->info.vscale = 100;
  487. break;
  488. case 3:
  489. battery->info.vscale = 1000;
  490. break;
  491. default:
  492. battery->info.vscale = 1;
  493. }
  494. switch ((specification_info & 0xf000) >> 12) {
  495. case 1:
  496. battery->info.ipscale = 10;
  497. break;
  498. case 2:
  499. battery->info.ipscale = 100;
  500. break;
  501. case 3:
  502. battery->info.ipscale = 1000;
  503. break;
  504. default:
  505. battery->info.ipscale = 1;
  506. }
  507. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
  508. &battery->info.serial_number);
  509. if (result) {
  510. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  511. "acpi_sbs_read_word() failed"));
  512. goto end;
  513. }
  514. result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
  515. battery->info.manufacturer_name);
  516. if (result) {
  517. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  518. "acpi_sbs_read_str() failed"));
  519. goto end;
  520. }
  521. result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
  522. battery->info.device_name);
  523. if (result) {
  524. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  525. "acpi_sbs_read_str() failed"));
  526. goto end;
  527. }
  528. result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
  529. battery->info.device_chemistry);
  530. if (result) {
  531. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  532. "acpi_sbs_read_str() failed"));
  533. goto end;
  534. }
  535. end:
  536. return result;
  537. }
  538. static int acpi_battery_get_state(struct acpi_battery *battery)
  539. {
  540. struct acpi_sbs *sbs = battery->sbs;
  541. int result = 0;
  542. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
  543. &battery->state.voltage);
  544. if (result) {
  545. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  546. "acpi_sbs_read_word() failed"));
  547. goto end;
  548. }
  549. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
  550. &battery->state.amperage);
  551. if (result) {
  552. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  553. "acpi_sbs_read_word() failed"));
  554. goto end;
  555. }
  556. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
  557. &battery->state.remaining_capacity);
  558. if (result) {
  559. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  560. "acpi_sbs_read_word() failed"));
  561. goto end;
  562. }
  563. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
  564. &battery->state.battery_state);
  565. if (result) {
  566. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  567. "acpi_sbs_read_word() failed"));
  568. goto end;
  569. }
  570. end:
  571. return result;
  572. }
  573. static int acpi_battery_get_alarm(struct acpi_battery *battery)
  574. {
  575. struct acpi_sbs *sbs = battery->sbs;
  576. int result = 0;
  577. result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
  578. &battery->alarm.remaining_capacity);
  579. if (result) {
  580. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  581. "acpi_sbs_read_word() failed"));
  582. goto end;
  583. }
  584. end:
  585. return result;
  586. }
  587. static int acpi_battery_set_alarm(struct acpi_battery *battery,
  588. unsigned long alarm)
  589. {
  590. struct acpi_sbs *sbs = battery->sbs;
  591. int result = 0;
  592. s16 battery_mode;
  593. int foo;
  594. result = acpi_battery_select(battery);
  595. if (result) {
  596. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  597. "acpi_battery_select() failed"));
  598. goto end;
  599. }
  600. /* If necessary, enable the alarm */
  601. if (alarm > 0) {
  602. result =
  603. acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
  604. &battery_mode);
  605. if (result) {
  606. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  607. "acpi_sbs_read_word() failed"));
  608. goto end;
  609. }
  610. result =
  611. acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
  612. battery_mode & 0xbfff);
  613. if (result) {
  614. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  615. "acpi_sbs_write_word() failed"));
  616. goto end;
  617. }
  618. }
  619. foo = alarm / (battery->info.capacity_mode ? 10 : 1);
  620. result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
  621. if (result) {
  622. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  623. "acpi_sbs_write_word() failed"));
  624. goto end;
  625. }
  626. end:
  627. return result;
  628. }
  629. static int acpi_battery_set_mode(struct acpi_battery *battery)
  630. {
  631. struct acpi_sbs *sbs = battery->sbs;
  632. int result = 0;
  633. s16 battery_mode;
  634. if (capacity_mode == DEF_CAPACITY_UNIT) {
  635. goto end;
  636. }
  637. result = acpi_sbs_read_word(sbs,
  638. ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
  639. if (result) {
  640. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  641. "acpi_sbs_read_word() failed"));
  642. goto end;
  643. }
  644. if (capacity_mode == MAH_CAPACITY_UNIT) {
  645. battery_mode &= 0x7fff;
  646. } else {
  647. battery_mode |= 0x8000;
  648. }
  649. result = acpi_sbs_write_word(sbs,
  650. ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
  651. if (result) {
  652. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  653. "acpi_sbs_write_word() failed"));
  654. goto end;
  655. }
  656. result = acpi_sbs_read_word(sbs,
  657. ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
  658. if (result) {
  659. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  660. "acpi_sbs_read_word() failed"));
  661. goto end;
  662. }
  663. end:
  664. return result;
  665. }
  666. static int acpi_battery_init(struct acpi_battery *battery)
  667. {
  668. int result = 0;
  669. result = acpi_battery_select(battery);
  670. if (result) {
  671. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  672. "acpi_battery_select() failed"));
  673. goto end;
  674. }
  675. result = acpi_battery_set_mode(battery);
  676. if (result) {
  677. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  678. "acpi_battery_set_mode() failed"));
  679. goto end;
  680. }
  681. result = acpi_battery_get_info(battery);
  682. if (result) {
  683. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  684. "acpi_battery_get_info() failed"));
  685. goto end;
  686. }
  687. result = acpi_battery_get_state(battery);
  688. if (result) {
  689. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  690. "acpi_battery_get_state() failed"));
  691. goto end;
  692. }
  693. result = acpi_battery_get_alarm(battery);
  694. if (result) {
  695. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  696. "acpi_battery_get_alarm() failed"));
  697. goto end;
  698. }
  699. end:
  700. return result;
  701. }
  702. static int acpi_ac_get_present(struct acpi_sbs *sbs)
  703. {
  704. int result = 0;
  705. s16 charger_status;
  706. result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
  707. &charger_status);
  708. if (result) {
  709. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  710. "acpi_sbs_read_word() failed"));
  711. goto end;
  712. }
  713. sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
  714. end:
  715. return result;
  716. }
  717. /* --------------------------------------------------------------------------
  718. FS Interface (/proc/acpi)
  719. -------------------------------------------------------------------------- */
  720. /* Generic Routines */
  721. static int
  722. acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
  723. struct proc_dir_entry *parent_dir,
  724. char *dir_name,
  725. struct file_operations *info_fops,
  726. struct file_operations *state_fops,
  727. struct file_operations *alarm_fops, void *data)
  728. {
  729. struct proc_dir_entry *entry = NULL;
  730. if (!*dir) {
  731. *dir = proc_mkdir(dir_name, parent_dir);
  732. if (!*dir) {
  733. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  734. "proc_mkdir() failed"));
  735. return -ENODEV;
  736. }
  737. (*dir)->owner = THIS_MODULE;
  738. }
  739. /* 'info' [R] */
  740. if (info_fops) {
  741. entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
  742. if (!entry) {
  743. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  744. "create_proc_entry() failed"));
  745. } else {
  746. entry->proc_fops = info_fops;
  747. entry->data = data;
  748. entry->owner = THIS_MODULE;
  749. }
  750. }
  751. /* 'state' [R] */
  752. if (state_fops) {
  753. entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
  754. if (!entry) {
  755. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  756. "create_proc_entry() failed"));
  757. } else {
  758. entry->proc_fops = state_fops;
  759. entry->data = data;
  760. entry->owner = THIS_MODULE;
  761. }
  762. }
  763. /* 'alarm' [R/W] */
  764. if (alarm_fops) {
  765. entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
  766. if (!entry) {
  767. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  768. "create_proc_entry() failed"));
  769. } else {
  770. entry->proc_fops = alarm_fops;
  771. entry->data = data;
  772. entry->owner = THIS_MODULE;
  773. }
  774. }
  775. return 0;
  776. }
  777. static void
  778. acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
  779. struct proc_dir_entry *parent_dir)
  780. {
  781. if (*dir) {
  782. remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
  783. remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
  784. remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
  785. remove_proc_entry((*dir)->name, parent_dir);
  786. *dir = NULL;
  787. }
  788. }
  789. /* Smart Battery Interface */
  790. static struct proc_dir_entry *acpi_battery_dir = NULL;
  791. static int acpi_battery_read_info(struct seq_file *seq, void *offset)
  792. {
  793. struct acpi_battery *battery = seq->private;
  794. struct acpi_sbs *sbs = battery->sbs;
  795. int cscale;
  796. int result = 0;
  797. if (sbs_mutex_lock(sbs)) {
  798. return -ENODEV;
  799. }
  800. result = acpi_check_update_proc(sbs);
  801. if (result)
  802. goto end;
  803. if (update_time == 0) {
  804. result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
  805. if (result) {
  806. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  807. "acpi_sbs_update_run() failed"));
  808. }
  809. }
  810. if (battery->battery_present) {
  811. seq_printf(seq, "present: yes\n");
  812. } else {
  813. seq_printf(seq, "present: no\n");
  814. goto end;
  815. }
  816. if (battery->info.capacity_mode) {
  817. cscale = battery->info.vscale * battery->info.ipscale;
  818. } else {
  819. cscale = battery->info.ipscale;
  820. }
  821. seq_printf(seq, "design capacity: %i%s\n",
  822. battery->info.design_capacity * cscale,
  823. battery->info.capacity_mode ? "0 mWh" : " mAh");
  824. seq_printf(seq, "last full capacity: %i%s\n",
  825. battery->info.full_charge_capacity * cscale,
  826. battery->info.capacity_mode ? "0 mWh" : " mAh");
  827. seq_printf(seq, "battery technology: rechargeable\n");
  828. seq_printf(seq, "design voltage: %i mV\n",
  829. battery->info.design_voltage * battery->info.vscale);
  830. seq_printf(seq, "design capacity warning: unknown\n");
  831. seq_printf(seq, "design capacity low: unknown\n");
  832. seq_printf(seq, "capacity granularity 1: unknown\n");
  833. seq_printf(seq, "capacity granularity 2: unknown\n");
  834. seq_printf(seq, "model number: %s\n",
  835. battery->info.device_name);
  836. seq_printf(seq, "serial number: %i\n",
  837. battery->info.serial_number);
  838. seq_printf(seq, "battery type: %s\n",
  839. battery->info.device_chemistry);
  840. seq_printf(seq, "OEM info: %s\n",
  841. battery->info.manufacturer_name);
  842. end:
  843. sbs_mutex_unlock(sbs);
  844. return result;
  845. }
  846. static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
  847. {
  848. return single_open(file, acpi_battery_read_info, PDE(inode)->data);
  849. }
  850. static int acpi_battery_read_state(struct seq_file *seq, void *offset)
  851. {
  852. struct acpi_battery *battery = seq->private;
  853. struct acpi_sbs *sbs = battery->sbs;
  854. int result = 0;
  855. int cscale;
  856. int foo;
  857. if (sbs_mutex_lock(sbs)) {
  858. return -ENODEV;
  859. }
  860. result = acpi_check_update_proc(sbs);
  861. if (result)
  862. goto end;
  863. if (update_time == 0) {
  864. result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
  865. if (result) {
  866. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  867. "acpi_sbs_update_run() failed"));
  868. }
  869. }
  870. if (battery->battery_present) {
  871. seq_printf(seq, "present: yes\n");
  872. } else {
  873. seq_printf(seq, "present: no\n");
  874. goto end;
  875. }
  876. if (battery->info.capacity_mode) {
  877. cscale = battery->info.vscale * battery->info.ipscale;
  878. } else {
  879. cscale = battery->info.ipscale;
  880. }
  881. if (battery->state.battery_state & 0x0010) {
  882. seq_printf(seq, "capacity state: critical\n");
  883. } else {
  884. seq_printf(seq, "capacity state: ok\n");
  885. }
  886. foo = (s16) battery->state.amperage * battery->info.ipscale;
  887. if (battery->info.capacity_mode) {
  888. foo = foo * battery->info.design_voltage / 1000;
  889. }
  890. if (battery->state.amperage < 0) {
  891. seq_printf(seq, "charging state: discharging\n");
  892. seq_printf(seq, "present rate: %d %s\n",
  893. -foo, battery->info.capacity_mode ? "mW" : "mA");
  894. } else if (battery->state.amperage > 0) {
  895. seq_printf(seq, "charging state: charging\n");
  896. seq_printf(seq, "present rate: %d %s\n",
  897. foo, battery->info.capacity_mode ? "mW" : "mA");
  898. } else {
  899. seq_printf(seq, "charging state: charged\n");
  900. seq_printf(seq, "present rate: 0 %s\n",
  901. battery->info.capacity_mode ? "mW" : "mA");
  902. }
  903. seq_printf(seq, "remaining capacity: %i%s\n",
  904. battery->state.remaining_capacity * cscale,
  905. battery->info.capacity_mode ? "0 mWh" : " mAh");
  906. seq_printf(seq, "present voltage: %i mV\n",
  907. battery->state.voltage * battery->info.vscale);
  908. end:
  909. sbs_mutex_unlock(sbs);
  910. return result;
  911. }
  912. static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
  913. {
  914. return single_open(file, acpi_battery_read_state, PDE(inode)->data);
  915. }
  916. static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
  917. {
  918. struct acpi_battery *battery = seq->private;
  919. struct acpi_sbs *sbs = battery->sbs;
  920. int result = 0;
  921. int cscale;
  922. if (sbs_mutex_lock(sbs)) {
  923. return -ENODEV;
  924. }
  925. result = acpi_check_update_proc(sbs);
  926. if (result)
  927. goto end;
  928. if (update_time == 0) {
  929. result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
  930. if (result) {
  931. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  932. "acpi_sbs_update_run() failed"));
  933. }
  934. }
  935. if (!battery->battery_present) {
  936. seq_printf(seq, "present: no\n");
  937. goto end;
  938. }
  939. if (battery->info.capacity_mode) {
  940. cscale = battery->info.vscale * battery->info.ipscale;
  941. } else {
  942. cscale = battery->info.ipscale;
  943. }
  944. seq_printf(seq, "alarm: ");
  945. if (battery->alarm.remaining_capacity) {
  946. seq_printf(seq, "%i%s\n",
  947. battery->alarm.remaining_capacity * cscale,
  948. battery->info.capacity_mode ? "0 mWh" : " mAh");
  949. } else {
  950. seq_printf(seq, "disabled\n");
  951. }
  952. end:
  953. sbs_mutex_unlock(sbs);
  954. return result;
  955. }
  956. static ssize_t
  957. acpi_battery_write_alarm(struct file *file, const char __user * buffer,
  958. size_t count, loff_t * ppos)
  959. {
  960. struct seq_file *seq = file->private_data;
  961. struct acpi_battery *battery = seq->private;
  962. struct acpi_sbs *sbs = battery->sbs;
  963. char alarm_string[12] = { '\0' };
  964. int result, old_alarm, new_alarm;
  965. if (sbs_mutex_lock(sbs)) {
  966. return -ENODEV;
  967. }
  968. result = acpi_check_update_proc(sbs);
  969. if (result)
  970. goto end;
  971. if (!battery->battery_present) {
  972. result = -ENODEV;
  973. goto end;
  974. }
  975. if (count > sizeof(alarm_string) - 1) {
  976. result = -EINVAL;
  977. goto end;
  978. }
  979. if (copy_from_user(alarm_string, buffer, count)) {
  980. result = -EFAULT;
  981. goto end;
  982. }
  983. alarm_string[count] = 0;
  984. old_alarm = battery->alarm.remaining_capacity;
  985. new_alarm = simple_strtoul(alarm_string, NULL, 0);
  986. result = acpi_battery_set_alarm(battery, new_alarm);
  987. if (result) {
  988. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  989. "acpi_battery_set_alarm() failed"));
  990. acpi_battery_set_alarm(battery, old_alarm);
  991. goto end;
  992. }
  993. result = acpi_battery_get_alarm(battery);
  994. if (result) {
  995. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  996. "acpi_battery_get_alarm() failed"));
  997. acpi_battery_set_alarm(battery, old_alarm);
  998. goto end;
  999. }
  1000. end:
  1001. sbs_mutex_unlock(sbs);
  1002. if (result) {
  1003. return result;
  1004. } else {
  1005. return count;
  1006. }
  1007. }
  1008. static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
  1009. {
  1010. return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
  1011. }
  1012. static struct file_operations acpi_battery_info_fops = {
  1013. .open = acpi_battery_info_open_fs,
  1014. .read = seq_read,
  1015. .llseek = seq_lseek,
  1016. .release = single_release,
  1017. .owner = THIS_MODULE,
  1018. };
  1019. static struct file_operations acpi_battery_state_fops = {
  1020. .open = acpi_battery_state_open_fs,
  1021. .read = seq_read,
  1022. .llseek = seq_lseek,
  1023. .release = single_release,
  1024. .owner = THIS_MODULE,
  1025. };
  1026. static struct file_operations acpi_battery_alarm_fops = {
  1027. .open = acpi_battery_alarm_open_fs,
  1028. .read = seq_read,
  1029. .write = acpi_battery_write_alarm,
  1030. .llseek = seq_lseek,
  1031. .release = single_release,
  1032. .owner = THIS_MODULE,
  1033. };
  1034. /* Legacy AC Adapter Interface */
  1035. static struct proc_dir_entry *acpi_ac_dir = NULL;
  1036. static int acpi_ac_read_state(struct seq_file *seq, void *offset)
  1037. {
  1038. struct acpi_sbs *sbs = seq->private;
  1039. int result;
  1040. if (sbs_mutex_lock(sbs)) {
  1041. return -ENODEV;
  1042. }
  1043. if (update_time == 0) {
  1044. result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
  1045. if (result) {
  1046. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1047. "acpi_sbs_update_run() failed"));
  1048. }
  1049. }
  1050. seq_printf(seq, "state: %s\n",
  1051. sbs->ac.ac_present ? "on-line" : "off-line");
  1052. sbs_mutex_unlock(sbs);
  1053. return 0;
  1054. }
  1055. static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
  1056. {
  1057. return single_open(file, acpi_ac_read_state, PDE(inode)->data);
  1058. }
  1059. static struct file_operations acpi_ac_state_fops = {
  1060. .open = acpi_ac_state_open_fs,
  1061. .read = seq_read,
  1062. .llseek = seq_lseek,
  1063. .release = single_release,
  1064. .owner = THIS_MODULE,
  1065. };
  1066. /* --------------------------------------------------------------------------
  1067. Driver Interface
  1068. -------------------------------------------------------------------------- */
  1069. /* Smart Battery */
  1070. static int acpi_battery_add(struct acpi_sbs *sbs, int id)
  1071. {
  1072. int is_present;
  1073. int result;
  1074. char dir_name[32];
  1075. struct acpi_battery *battery;
  1076. battery = &sbs->battery[id];
  1077. battery->alive = 0;
  1078. battery->init_state = 0;
  1079. battery->id = id;
  1080. battery->sbs = sbs;
  1081. result = acpi_battery_select(battery);
  1082. if (result) {
  1083. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1084. "acpi_battery_select() failed"));
  1085. goto end;
  1086. }
  1087. result = acpi_battery_get_present(battery);
  1088. if (result) {
  1089. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1090. "acpi_battery_get_present() failed"));
  1091. goto end;
  1092. }
  1093. is_present = battery->battery_present;
  1094. if (is_present) {
  1095. result = acpi_battery_init(battery);
  1096. if (result) {
  1097. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1098. "acpi_battery_init() failed"));
  1099. goto end;
  1100. }
  1101. battery->init_state = 1;
  1102. }
  1103. sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
  1104. result = acpi_sbs_generic_add_fs(&battery->battery_entry,
  1105. acpi_battery_dir,
  1106. dir_name,
  1107. &acpi_battery_info_fops,
  1108. &acpi_battery_state_fops,
  1109. &acpi_battery_alarm_fops, battery);
  1110. if (result) {
  1111. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1112. "acpi_sbs_generic_add_fs() failed"));
  1113. goto end;
  1114. }
  1115. battery->alive = 1;
  1116. printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
  1117. ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
  1118. sbs->battery->battery_present ? "present" : "absent");
  1119. end:
  1120. return result;
  1121. }
  1122. static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
  1123. {
  1124. if (sbs->battery[id].battery_entry) {
  1125. acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
  1126. acpi_battery_dir);
  1127. }
  1128. }
  1129. static int acpi_ac_add(struct acpi_sbs *sbs)
  1130. {
  1131. int result;
  1132. result = acpi_ac_get_present(sbs);
  1133. if (result) {
  1134. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1135. "acpi_ac_get_present() failed"));
  1136. goto end;
  1137. }
  1138. result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
  1139. acpi_ac_dir,
  1140. ACPI_AC_DIR_NAME,
  1141. NULL, &acpi_ac_state_fops, NULL, sbs);
  1142. if (result) {
  1143. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1144. "acpi_sbs_generic_add_fs() failed"));
  1145. goto end;
  1146. }
  1147. printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
  1148. ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
  1149. ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
  1150. end:
  1151. return result;
  1152. }
  1153. static void acpi_ac_remove(struct acpi_sbs *sbs)
  1154. {
  1155. if (sbs->ac_entry) {
  1156. acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
  1157. }
  1158. }
  1159. static void acpi_sbs_update_time_run(unsigned long data)
  1160. {
  1161. acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
  1162. }
  1163. static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
  1164. {
  1165. struct acpi_battery *battery;
  1166. int result = 0, cnt;
  1167. int old_ac_present = -1;
  1168. int old_battery_present = -1;
  1169. int new_ac_present = -1;
  1170. int new_battery_present = -1;
  1171. int id_min = 0, id_max = MAX_SBS_BAT - 1;
  1172. char dir_name[32];
  1173. int do_battery_init = 0, do_ac_init = 0;
  1174. int old_remaining_capacity = 0;
  1175. int update_ac = 1, update_battery = 1;
  1176. int up_tm = update_time;
  1177. if (sbs_zombie(sbs)) {
  1178. goto end;
  1179. }
  1180. if (id >= 0) {
  1181. id_min = id_max = id;
  1182. }
  1183. if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
  1184. cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
  1185. if (sbs->run_cnt % cnt != 0) {
  1186. update_battery = 0;
  1187. }
  1188. }
  1189. sbs->run_cnt++;
  1190. if (!update_ac && !update_battery) {
  1191. goto end;
  1192. }
  1193. old_ac_present = sbs->ac.ac_present;
  1194. result = acpi_ac_get_present(sbs);
  1195. if (result) {
  1196. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1197. "acpi_ac_get_present() failed"));
  1198. }
  1199. new_ac_present = sbs->ac.ac_present;
  1200. do_ac_init = (old_ac_present != new_ac_present);
  1201. if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
  1202. do_ac_init = 1;
  1203. }
  1204. if (do_ac_init) {
  1205. result = acpi_sbs_generate_event(sbs->device,
  1206. ACPI_SBS_AC_NOTIFY_STATUS,
  1207. new_ac_present,
  1208. ACPI_AC_DIR_NAME,
  1209. ACPI_AC_CLASS);
  1210. if (result) {
  1211. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1212. "acpi_sbs_generate_event() failed"));
  1213. }
  1214. }
  1215. if (data_type == DATA_TYPE_COMMON) {
  1216. if (!do_ac_init && !update_battery) {
  1217. goto end;
  1218. }
  1219. }
  1220. if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
  1221. goto end;
  1222. }
  1223. for (id = id_min; id <= id_max; id++) {
  1224. battery = &sbs->battery[id];
  1225. if (battery->alive == 0) {
  1226. continue;
  1227. }
  1228. old_remaining_capacity = battery->state.remaining_capacity;
  1229. old_battery_present = battery->battery_present;
  1230. result = acpi_battery_select(battery);
  1231. if (result) {
  1232. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1233. "acpi_battery_select() failed"));
  1234. }
  1235. result = acpi_battery_get_present(battery);
  1236. if (result) {
  1237. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1238. "acpi_battery_get_present() failed"));
  1239. }
  1240. new_battery_present = battery->battery_present;
  1241. do_battery_init = ((old_battery_present != new_battery_present)
  1242. && new_battery_present);
  1243. if (!new_battery_present)
  1244. goto event;
  1245. if (do_ac_init || do_battery_init) {
  1246. result = acpi_battery_init(battery);
  1247. if (result) {
  1248. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1249. "acpi_battery_init() "
  1250. "failed"));
  1251. }
  1252. }
  1253. if (sbs_zombie(sbs)) {
  1254. goto end;
  1255. }
  1256. if ((data_type == DATA_TYPE_COMMON
  1257. || data_type == DATA_TYPE_INFO)
  1258. && new_battery_present) {
  1259. result = acpi_battery_get_info(battery);
  1260. if (result) {
  1261. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1262. "acpi_battery_get_info() failed"));
  1263. }
  1264. }
  1265. if (data_type == DATA_TYPE_INFO) {
  1266. continue;
  1267. }
  1268. if (sbs_zombie(sbs)) {
  1269. goto end;
  1270. }
  1271. if ((data_type == DATA_TYPE_COMMON
  1272. || data_type == DATA_TYPE_STATE)
  1273. && new_battery_present) {
  1274. result = acpi_battery_get_state(battery);
  1275. if (result) {
  1276. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1277. "acpi_battery_get_state() failed"));
  1278. }
  1279. }
  1280. if (data_type == DATA_TYPE_STATE) {
  1281. goto event;
  1282. }
  1283. if (sbs_zombie(sbs)) {
  1284. goto end;
  1285. }
  1286. if ((data_type == DATA_TYPE_COMMON
  1287. || data_type == DATA_TYPE_ALARM)
  1288. && new_battery_present) {
  1289. result = acpi_battery_get_alarm(battery);
  1290. if (result) {
  1291. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1292. "acpi_battery_get_alarm() "
  1293. "failed"));
  1294. }
  1295. }
  1296. if (data_type == DATA_TYPE_ALARM) {
  1297. continue;
  1298. }
  1299. if (sbs_zombie(sbs)) {
  1300. goto end;
  1301. }
  1302. event:
  1303. if (old_battery_present != new_battery_present || do_ac_init ||
  1304. old_remaining_capacity !=
  1305. battery->state.remaining_capacity) {
  1306. sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
  1307. result = acpi_sbs_generate_event(sbs->device,
  1308. ACPI_SBS_BATTERY_NOTIFY_STATUS,
  1309. new_battery_present,
  1310. dir_name,
  1311. ACPI_BATTERY_CLASS);
  1312. if (result) {
  1313. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1314. "acpi_sbs_generate_event() "
  1315. "failed"));
  1316. }
  1317. }
  1318. }
  1319. end:
  1320. return result;
  1321. }
  1322. static void acpi_sbs_update_time(void *data)
  1323. {
  1324. struct acpi_sbs *sbs = data;
  1325. unsigned long delay = -1;
  1326. int result;
  1327. unsigned int up_tm = update_time;
  1328. if (sbs_mutex_lock(sbs))
  1329. return;
  1330. result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
  1331. if (result) {
  1332. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1333. "acpi_sbs_update_run() failed"));
  1334. }
  1335. if (sbs_zombie(sbs)) {
  1336. goto end;
  1337. }
  1338. if (!up_tm) {
  1339. if (timer_pending(&sbs->update_timer))
  1340. del_timer(&sbs->update_timer);
  1341. } else {
  1342. delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
  1343. delay = jiffies + HZ * delay;
  1344. if (timer_pending(&sbs->update_timer)) {
  1345. mod_timer(&sbs->update_timer, delay);
  1346. } else {
  1347. sbs->update_timer.data = (unsigned long)data;
  1348. sbs->update_timer.function = acpi_sbs_update_time_run;
  1349. sbs->update_timer.expires = delay;
  1350. add_timer(&sbs->update_timer);
  1351. }
  1352. }
  1353. end:
  1354. sbs_mutex_unlock(sbs);
  1355. }
  1356. static int acpi_sbs_add(struct acpi_device *device)
  1357. {
  1358. struct acpi_sbs *sbs = NULL;
  1359. int result = 0, remove_result = 0;
  1360. int id;
  1361. acpi_status status = AE_OK;
  1362. unsigned long val;
  1363. status =
  1364. acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
  1365. if (ACPI_FAILURE(status)) {
  1366. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
  1367. return -EIO;
  1368. }
  1369. sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
  1370. if (!sbs) {
  1371. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
  1372. result = -ENOMEM;
  1373. goto end;
  1374. }
  1375. mutex_init(&sbs->mutex);
  1376. sbs_mutex_lock(sbs);
  1377. sbs->base = 0xff & (val >> 8);
  1378. sbs->device = device;
  1379. strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
  1380. strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
  1381. acpi_driver_data(device) = sbs;
  1382. result = acpi_ac_add(sbs);
  1383. if (result) {
  1384. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
  1385. goto end;
  1386. }
  1387. acpi_sbsm_get_info(sbs);
  1388. if (!sbs->sbsm_present) {
  1389. result = acpi_battery_add(sbs, 0);
  1390. if (result) {
  1391. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1392. "acpi_battery_add() failed"));
  1393. goto end;
  1394. }
  1395. } else {
  1396. for (id = 0; id < MAX_SBS_BAT; id++) {
  1397. if ((sbs->sbsm_batteries_supported & (1 << id))) {
  1398. result = acpi_battery_add(sbs, id);
  1399. if (result) {
  1400. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1401. "acpi_battery_add() failed"));
  1402. goto end;
  1403. }
  1404. }
  1405. }
  1406. }
  1407. init_timer(&sbs->update_timer);
  1408. result = acpi_check_update_proc(sbs);
  1409. if (result)
  1410. goto end;
  1411. end:
  1412. sbs_mutex_unlock(sbs);
  1413. if (result) {
  1414. remove_result = acpi_sbs_remove(device, 0);
  1415. if (remove_result) {
  1416. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1417. "acpi_sbs_remove() failed"));
  1418. }
  1419. }
  1420. return result;
  1421. }
  1422. static int acpi_sbs_remove(struct acpi_device *device, int type)
  1423. {
  1424. struct acpi_sbs *sbs;
  1425. int id;
  1426. if (!device) {
  1427. return -EINVAL;
  1428. }
  1429. sbs = acpi_driver_data(device);
  1430. if (!sbs) {
  1431. return -EINVAL;
  1432. }
  1433. sbs_mutex_lock(sbs);
  1434. sbs->zombie = 1;
  1435. del_timer_sync(&sbs->update_timer);
  1436. acpi_os_wait_events_complete(NULL);
  1437. del_timer_sync(&sbs->update_timer);
  1438. for (id = 0; id < MAX_SBS_BAT; id++) {
  1439. acpi_battery_remove(sbs, id);
  1440. }
  1441. acpi_ac_remove(sbs);
  1442. sbs_mutex_unlock(sbs);
  1443. mutex_destroy(&sbs->mutex);
  1444. kfree(sbs);
  1445. return 0;
  1446. }
  1447. static void acpi_sbs_rmdirs(void)
  1448. {
  1449. if (acpi_ac_dir) {
  1450. acpi_unlock_ac_dir(acpi_ac_dir);
  1451. acpi_ac_dir = NULL;
  1452. }
  1453. if (acpi_battery_dir) {
  1454. acpi_unlock_battery_dir(acpi_battery_dir);
  1455. acpi_battery_dir = NULL;
  1456. }
  1457. }
  1458. static int acpi_sbs_resume(struct acpi_device *device)
  1459. {
  1460. struct acpi_sbs *sbs;
  1461. if (!device)
  1462. return -EINVAL;
  1463. sbs = device->driver_data;
  1464. sbs->run_cnt = 0;
  1465. return 0;
  1466. }
  1467. static int __init acpi_sbs_init(void)
  1468. {
  1469. int result = 0;
  1470. if (acpi_disabled)
  1471. return -ENODEV;
  1472. if (capacity_mode != DEF_CAPACITY_UNIT
  1473. && capacity_mode != MAH_CAPACITY_UNIT
  1474. && capacity_mode != MWH_CAPACITY_UNIT) {
  1475. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1476. "invalid capacity_mode = %d", capacity_mode));
  1477. return -EINVAL;
  1478. }
  1479. acpi_ac_dir = acpi_lock_ac_dir();
  1480. if (!acpi_ac_dir) {
  1481. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1482. "acpi_lock_ac_dir() failed"));
  1483. return -ENODEV;
  1484. }
  1485. acpi_battery_dir = acpi_lock_battery_dir();
  1486. if (!acpi_battery_dir) {
  1487. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1488. "acpi_lock_battery_dir() failed"));
  1489. acpi_sbs_rmdirs();
  1490. return -ENODEV;
  1491. }
  1492. result = acpi_bus_register_driver(&acpi_sbs_driver);
  1493. if (result < 0) {
  1494. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1495. "acpi_bus_register_driver() failed"));
  1496. acpi_sbs_rmdirs();
  1497. return -ENODEV;
  1498. }
  1499. return 0;
  1500. }
  1501. static void __exit acpi_sbs_exit(void)
  1502. {
  1503. acpi_bus_unregister_driver(&acpi_sbs_driver);
  1504. acpi_sbs_rmdirs();
  1505. return;
  1506. }
  1507. module_init(acpi_sbs_init);
  1508. module_exit(acpi_sbs_exit);