sbs.c 41 KB

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