sbs.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  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_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. old_ac_present = sbs->ac.ac_present;
  1191. result = acpi_ac_get_present(sbs);
  1192. if (result) {
  1193. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1194. "acpi_ac_get_present() failed"));
  1195. }
  1196. new_ac_present = sbs->ac.ac_present;
  1197. do_ac_init = (old_ac_present != new_ac_present);
  1198. if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
  1199. do_ac_init = 1;
  1200. }
  1201. if (do_ac_init) {
  1202. result = acpi_sbs_generate_event(sbs->device,
  1203. ACPI_SBS_AC_NOTIFY_STATUS,
  1204. new_ac_present,
  1205. ACPI_AC_DIR_NAME,
  1206. ACPI_AC_CLASS);
  1207. if (result) {
  1208. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1209. "acpi_sbs_generate_event() failed"));
  1210. }
  1211. }
  1212. if (data_type == DATA_TYPE_COMMON) {
  1213. if (!do_ac_init && !update_battery) {
  1214. goto end;
  1215. }
  1216. }
  1217. if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
  1218. goto end;
  1219. }
  1220. for (id = id_min; id <= id_max; id++) {
  1221. battery = &sbs->battery[id];
  1222. if (battery->alive == 0) {
  1223. continue;
  1224. }
  1225. old_remaining_capacity = battery->state.remaining_capacity;
  1226. old_battery_present = battery->battery_present;
  1227. result = acpi_battery_select(battery);
  1228. if (result) {
  1229. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1230. "acpi_battery_select() failed"));
  1231. }
  1232. result = acpi_battery_get_present(battery);
  1233. if (result) {
  1234. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1235. "acpi_battery_get_present() failed"));
  1236. }
  1237. new_battery_present = battery->battery_present;
  1238. do_battery_init = ((old_battery_present != new_battery_present)
  1239. && new_battery_present);
  1240. if (!new_battery_present)
  1241. goto event;
  1242. if (do_ac_init || do_battery_init) {
  1243. result = acpi_battery_init(battery);
  1244. if (result) {
  1245. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1246. "acpi_battery_init() "
  1247. "failed"));
  1248. }
  1249. }
  1250. if (sbs_zombie(sbs)) {
  1251. goto end;
  1252. }
  1253. if ((data_type == DATA_TYPE_COMMON
  1254. || data_type == DATA_TYPE_INFO)
  1255. && new_battery_present) {
  1256. result = acpi_battery_get_info(battery);
  1257. if (result) {
  1258. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1259. "acpi_battery_get_info() failed"));
  1260. }
  1261. }
  1262. if (data_type == DATA_TYPE_INFO) {
  1263. continue;
  1264. }
  1265. if (sbs_zombie(sbs)) {
  1266. goto end;
  1267. }
  1268. if ((data_type == DATA_TYPE_COMMON
  1269. || data_type == DATA_TYPE_STATE)
  1270. && new_battery_present) {
  1271. result = acpi_battery_get_state(battery);
  1272. if (result) {
  1273. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1274. "acpi_battery_get_state() failed"));
  1275. }
  1276. }
  1277. if (data_type == DATA_TYPE_STATE) {
  1278. goto event;
  1279. }
  1280. if (sbs_zombie(sbs)) {
  1281. goto end;
  1282. }
  1283. if ((data_type == DATA_TYPE_COMMON
  1284. || data_type == DATA_TYPE_ALARM)
  1285. && new_battery_present) {
  1286. result = acpi_battery_get_alarm(battery);
  1287. if (result) {
  1288. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1289. "acpi_battery_get_alarm() "
  1290. "failed"));
  1291. }
  1292. }
  1293. if (data_type == DATA_TYPE_ALARM) {
  1294. continue;
  1295. }
  1296. if (sbs_zombie(sbs)) {
  1297. goto end;
  1298. }
  1299. event:
  1300. if (old_battery_present != new_battery_present || do_ac_init ||
  1301. old_remaining_capacity !=
  1302. battery->state.remaining_capacity) {
  1303. sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
  1304. result = acpi_sbs_generate_event(sbs->device,
  1305. ACPI_SBS_BATTERY_NOTIFY_STATUS,
  1306. new_battery_present,
  1307. dir_name,
  1308. ACPI_BATTERY_CLASS);
  1309. if (result) {
  1310. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1311. "acpi_sbs_generate_event() "
  1312. "failed"));
  1313. }
  1314. }
  1315. }
  1316. end:
  1317. return result;
  1318. }
  1319. static void acpi_sbs_update_time(void *data)
  1320. {
  1321. struct acpi_sbs *sbs = data;
  1322. unsigned long delay = -1;
  1323. int result;
  1324. unsigned int up_tm = update_time;
  1325. if (sbs_mutex_lock(sbs))
  1326. return;
  1327. result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
  1328. if (result) {
  1329. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1330. "acpi_sbs_update_run() failed"));
  1331. }
  1332. if (sbs_zombie(sbs)) {
  1333. goto end;
  1334. }
  1335. if (!up_tm) {
  1336. if (timer_pending(&sbs->update_timer))
  1337. del_timer(&sbs->update_timer);
  1338. } else {
  1339. delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
  1340. delay = jiffies + HZ * delay;
  1341. if (timer_pending(&sbs->update_timer)) {
  1342. mod_timer(&sbs->update_timer, delay);
  1343. } else {
  1344. sbs->update_timer.data = (unsigned long)data;
  1345. sbs->update_timer.function = acpi_sbs_update_time_run;
  1346. sbs->update_timer.expires = delay;
  1347. add_timer(&sbs->update_timer);
  1348. }
  1349. }
  1350. end:
  1351. sbs_mutex_unlock(sbs);
  1352. }
  1353. static int acpi_sbs_add(struct acpi_device *device)
  1354. {
  1355. struct acpi_sbs *sbs = NULL;
  1356. int result = 0, remove_result = 0;
  1357. int id;
  1358. acpi_status status = AE_OK;
  1359. unsigned long val;
  1360. status =
  1361. acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
  1362. if (ACPI_FAILURE(status)) {
  1363. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
  1364. return -EIO;
  1365. }
  1366. sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
  1367. if (!sbs) {
  1368. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
  1369. result = -ENOMEM;
  1370. goto end;
  1371. }
  1372. mutex_init(&sbs->mutex);
  1373. sbs_mutex_lock(sbs);
  1374. sbs->base = 0xff & (val >> 8);
  1375. sbs->device = device;
  1376. strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
  1377. strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
  1378. acpi_driver_data(device) = sbs;
  1379. result = acpi_ac_add(sbs);
  1380. if (result) {
  1381. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
  1382. goto end;
  1383. }
  1384. acpi_sbsm_get_info(sbs);
  1385. if (!sbs->sbsm_present) {
  1386. result = acpi_battery_add(sbs, 0);
  1387. if (result) {
  1388. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1389. "acpi_battery_add() failed"));
  1390. goto end;
  1391. }
  1392. } else {
  1393. for (id = 0; id < MAX_SBS_BAT; id++) {
  1394. if ((sbs->sbsm_batteries_supported & (1 << id))) {
  1395. result = acpi_battery_add(sbs, id);
  1396. if (result) {
  1397. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1398. "acpi_battery_add() failed"));
  1399. goto end;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. init_timer(&sbs->update_timer);
  1405. result = acpi_check_update_proc(sbs);
  1406. if (result)
  1407. goto end;
  1408. end:
  1409. sbs_mutex_unlock(sbs);
  1410. if (result) {
  1411. remove_result = acpi_sbs_remove(device, 0);
  1412. if (remove_result) {
  1413. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1414. "acpi_sbs_remove() failed"));
  1415. }
  1416. }
  1417. return result;
  1418. }
  1419. static int acpi_sbs_remove(struct acpi_device *device, int type)
  1420. {
  1421. struct acpi_sbs *sbs;
  1422. int id;
  1423. if (!device) {
  1424. return -EINVAL;
  1425. }
  1426. sbs = acpi_driver_data(device);
  1427. if (!sbs) {
  1428. return -EINVAL;
  1429. }
  1430. sbs_mutex_lock(sbs);
  1431. sbs->zombie = 1;
  1432. del_timer_sync(&sbs->update_timer);
  1433. acpi_os_wait_events_complete(NULL);
  1434. del_timer_sync(&sbs->update_timer);
  1435. for (id = 0; id < MAX_SBS_BAT; id++) {
  1436. acpi_battery_remove(sbs, id);
  1437. }
  1438. acpi_ac_remove(sbs);
  1439. sbs_mutex_unlock(sbs);
  1440. mutex_destroy(&sbs->mutex);
  1441. kfree(sbs);
  1442. return 0;
  1443. }
  1444. static void acpi_sbs_rmdirs(void)
  1445. {
  1446. if (acpi_ac_dir) {
  1447. acpi_unlock_ac_dir(acpi_ac_dir);
  1448. acpi_ac_dir = NULL;
  1449. }
  1450. if (acpi_battery_dir) {
  1451. acpi_unlock_battery_dir(acpi_battery_dir);
  1452. acpi_battery_dir = NULL;
  1453. }
  1454. }
  1455. static int acpi_sbs_resume(struct acpi_device *device)
  1456. {
  1457. struct acpi_sbs *sbs;
  1458. if (!device)
  1459. return -EINVAL;
  1460. sbs = device->driver_data;
  1461. sbs->run_cnt = 0;
  1462. return 0;
  1463. }
  1464. static int __init acpi_sbs_init(void)
  1465. {
  1466. int result = 0;
  1467. if (acpi_disabled)
  1468. return -ENODEV;
  1469. if (capacity_mode != DEF_CAPACITY_UNIT
  1470. && capacity_mode != MAH_CAPACITY_UNIT
  1471. && capacity_mode != MWH_CAPACITY_UNIT) {
  1472. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1473. "invalid capacity_mode = %d", capacity_mode));
  1474. return -EINVAL;
  1475. }
  1476. acpi_ac_dir = acpi_lock_ac_dir();
  1477. if (!acpi_ac_dir) {
  1478. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1479. "acpi_lock_ac_dir() failed"));
  1480. return -ENODEV;
  1481. }
  1482. acpi_battery_dir = acpi_lock_battery_dir();
  1483. if (!acpi_battery_dir) {
  1484. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1485. "acpi_lock_battery_dir() failed"));
  1486. acpi_sbs_rmdirs();
  1487. return -ENODEV;
  1488. }
  1489. result = acpi_bus_register_driver(&acpi_sbs_driver);
  1490. if (result < 0) {
  1491. ACPI_EXCEPTION((AE_INFO, AE_ERROR,
  1492. "acpi_bus_register_driver() failed"));
  1493. acpi_sbs_rmdirs();
  1494. return -ENODEV;
  1495. }
  1496. return 0;
  1497. }
  1498. static void __exit acpi_sbs_exit(void)
  1499. {
  1500. acpi_bus_unregister_driver(&acpi_sbs_driver);
  1501. acpi_sbs_rmdirs();
  1502. return;
  1503. }
  1504. module_init(acpi_sbs_init);
  1505. module_exit(acpi_sbs_exit);