ec.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v2.1)
  3. *
  4. * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  5. * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  6. * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  7. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  8. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or (at
  15. * your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. */
  28. /* Uncomment next line to get verbose printout */
  29. /* #define DEBUG */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/delay.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/list.h>
  39. #include <linux/spinlock.h>
  40. #include <asm/io.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/acpi_drivers.h>
  43. #include <acpi/actypes.h>
  44. #define ACPI_EC_CLASS "embedded_controller"
  45. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  46. #define ACPI_EC_FILE_INFO "info"
  47. #undef PREFIX
  48. #define PREFIX "ACPI: EC: "
  49. /* EC status register */
  50. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  51. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  52. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  53. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  54. /* EC commands */
  55. enum ec_command {
  56. ACPI_EC_COMMAND_READ = 0x80,
  57. ACPI_EC_COMMAND_WRITE = 0x81,
  58. ACPI_EC_BURST_ENABLE = 0x82,
  59. ACPI_EC_BURST_DISABLE = 0x83,
  60. ACPI_EC_COMMAND_QUERY = 0x84,
  61. };
  62. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  63. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  64. #define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
  65. #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
  66. per one transaction */
  67. enum {
  68. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  69. EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
  70. * for status change */
  71. EC_FLAGS_NO_GPE, /* Don't use GPE mode */
  72. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  73. EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
  74. * OpReg are installed */
  75. };
  76. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  77. /* External interfaces use first EC only, so remember */
  78. typedef int (*acpi_ec_query_func) (void *data);
  79. struct acpi_ec_query_handler {
  80. struct list_head node;
  81. acpi_ec_query_func func;
  82. acpi_handle handle;
  83. void *data;
  84. u8 query_bit;
  85. };
  86. struct transaction {
  87. const u8 *wdata;
  88. u8 *rdata;
  89. unsigned short irq_count;
  90. u8 command;
  91. u8 wi;
  92. u8 ri;
  93. u8 wlen;
  94. u8 rlen;
  95. bool done;
  96. };
  97. static struct acpi_ec {
  98. acpi_handle handle;
  99. unsigned long gpe;
  100. unsigned long command_addr;
  101. unsigned long data_addr;
  102. unsigned long global_lock;
  103. unsigned long flags;
  104. struct mutex lock;
  105. wait_queue_head_t wait;
  106. struct list_head list;
  107. struct transaction *curr;
  108. spinlock_t curr_lock;
  109. } *boot_ec, *first_ec;
  110. /*
  111. * Some Asus system have exchanged ECDT data/command IO addresses.
  112. */
  113. static int print_ecdt_error(const struct dmi_system_id *id)
  114. {
  115. printk(KERN_NOTICE PREFIX "%s detected - "
  116. "ECDT has exchanged control/data I/O address\n",
  117. id->ident);
  118. return 0;
  119. }
  120. static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
  121. {
  122. print_ecdt_error, "Asus L4R", {
  123. DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
  124. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
  125. DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
  126. {
  127. print_ecdt_error, "Asus M6R", {
  128. DMI_MATCH(DMI_BIOS_VERSION, "0207"),
  129. DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
  130. DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
  131. {},
  132. };
  133. /* --------------------------------------------------------------------------
  134. Transaction Management
  135. -------------------------------------------------------------------------- */
  136. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  137. {
  138. u8 x = inb(ec->command_addr);
  139. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  140. return x;
  141. }
  142. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  143. {
  144. u8 x = inb(ec->data_addr);
  145. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  146. return x;
  147. }
  148. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  149. {
  150. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  151. outb(command, ec->command_addr);
  152. }
  153. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  154. {
  155. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  156. outb(data, ec->data_addr);
  157. }
  158. static int ec_transaction_done(struct acpi_ec *ec)
  159. {
  160. unsigned long flags;
  161. int ret = 0;
  162. spin_lock_irqsave(&ec->curr_lock, flags);
  163. if (!ec->curr || ec->curr->done)
  164. ret = 1;
  165. spin_unlock_irqrestore(&ec->curr_lock, flags);
  166. return ret;
  167. }
  168. static void start_transaction(struct acpi_ec *ec)
  169. {
  170. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  171. ec->curr->done = false;
  172. acpi_ec_write_cmd(ec, ec->curr->command);
  173. }
  174. static void gpe_transaction(struct acpi_ec *ec, u8 status)
  175. {
  176. unsigned long flags;
  177. spin_lock_irqsave(&ec->curr_lock, flags);
  178. if (!ec->curr)
  179. goto unlock;
  180. if (ec->curr->wlen > ec->curr->wi) {
  181. if ((status & ACPI_EC_FLAG_IBF) == 0)
  182. acpi_ec_write_data(ec,
  183. ec->curr->wdata[ec->curr->wi++]);
  184. else
  185. goto err;
  186. } else if (ec->curr->rlen > ec->curr->ri) {
  187. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  188. ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
  189. if (ec->curr->rlen == ec->curr->ri)
  190. ec->curr->done = true;
  191. } else
  192. goto err;
  193. } else if (ec->curr->wlen == ec->curr->wi &&
  194. (status & ACPI_EC_FLAG_IBF) == 0)
  195. ec->curr->done = true;
  196. goto unlock;
  197. err:
  198. /* false interrupt, state didn't change */
  199. ++ec->curr->irq_count;
  200. unlock:
  201. spin_unlock_irqrestore(&ec->curr_lock, flags);
  202. }
  203. static int acpi_ec_wait(struct acpi_ec *ec)
  204. {
  205. if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
  206. msecs_to_jiffies(ACPI_EC_DELAY)))
  207. return 0;
  208. /* try restart command if we get any false interrupts */
  209. if (ec->curr->irq_count &&
  210. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  211. pr_debug(PREFIX "controller reset, restart transaction\n");
  212. start_transaction(ec);
  213. if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
  214. msecs_to_jiffies(ACPI_EC_DELAY)))
  215. return 0;
  216. }
  217. /* missing GPEs, switch back to poll mode */
  218. if (printk_ratelimit())
  219. pr_info(PREFIX "missing confirmations, "
  220. "switch off interrupt mode.\n");
  221. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  222. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  223. return 1;
  224. }
  225. static void acpi_ec_gpe_query(void *ec_cxt);
  226. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  227. {
  228. if (state & ACPI_EC_FLAG_SCI) {
  229. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  230. return acpi_os_execute(OSL_EC_BURST_HANDLER,
  231. acpi_ec_gpe_query, ec);
  232. }
  233. return 0;
  234. }
  235. static int ec_poll(struct acpi_ec *ec)
  236. {
  237. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  238. udelay(ACPI_EC_UDELAY);
  239. while (time_before(jiffies, delay)) {
  240. gpe_transaction(ec, acpi_ec_read_status(ec));
  241. udelay(ACPI_EC_UDELAY);
  242. if (ec_transaction_done(ec))
  243. return 0;
  244. }
  245. return -ETIME;
  246. }
  247. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  248. struct transaction *t,
  249. int force_poll)
  250. {
  251. unsigned long tmp;
  252. int ret = 0;
  253. pr_debug(PREFIX "transaction start\n");
  254. /* disable GPE during transaction if storm is detected */
  255. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  256. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  257. acpi_disable_gpe(NULL, ec->gpe);
  258. }
  259. /* start transaction */
  260. spin_lock_irqsave(&ec->curr_lock, tmp);
  261. /* following two actions should be kept atomic */
  262. ec->curr = t;
  263. start_transaction(ec);
  264. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  265. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  266. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  267. /* if we selected poll mode or failed in GPE-mode do a poll loop */
  268. if (force_poll ||
  269. !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
  270. acpi_ec_wait(ec))
  271. ret = ec_poll(ec);
  272. pr_debug(PREFIX "transaction end\n");
  273. spin_lock_irqsave(&ec->curr_lock, tmp);
  274. ec->curr = NULL;
  275. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  276. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  277. /* check if we received SCI during transaction */
  278. ec_check_sci(ec, acpi_ec_read_status(ec));
  279. /* it is safe to enable GPE outside of transaction */
  280. acpi_enable_gpe(NULL, ec->gpe);
  281. } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
  282. t->irq_count > ACPI_EC_STORM_THRESHOLD) {
  283. pr_info(PREFIX "GPE storm detected, "
  284. "transactions will use polling mode\n");
  285. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  286. }
  287. return ret;
  288. }
  289. static int ec_check_ibf0(struct acpi_ec *ec)
  290. {
  291. u8 status = acpi_ec_read_status(ec);
  292. return (status & ACPI_EC_FLAG_IBF) == 0;
  293. }
  294. static int ec_wait_ibf0(struct acpi_ec *ec)
  295. {
  296. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  297. /* interrupt wait manually if GPE mode is not active */
  298. unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
  299. msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
  300. while (time_before(jiffies, delay))
  301. if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
  302. return 0;
  303. return -ETIME;
  304. }
  305. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t,
  306. int force_poll)
  307. {
  308. int status;
  309. u32 glk;
  310. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  311. return -EINVAL;
  312. if (t->rdata)
  313. memset(t->rdata, 0, t->rlen);
  314. mutex_lock(&ec->lock);
  315. if (ec->global_lock) {
  316. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  317. if (ACPI_FAILURE(status)) {
  318. status = -ENODEV;
  319. goto unlock;
  320. }
  321. }
  322. if (ec_wait_ibf0(ec)) {
  323. pr_err(PREFIX "input buffer is not empty, "
  324. "aborting transaction\n");
  325. status = -ETIME;
  326. goto end;
  327. }
  328. status = acpi_ec_transaction_unlocked(ec, t, force_poll);
  329. end:
  330. if (ec->global_lock)
  331. acpi_release_global_lock(glk);
  332. unlock:
  333. mutex_unlock(&ec->lock);
  334. return status;
  335. }
  336. /*
  337. * Note: samsung nv5000 doesn't work with ec burst mode.
  338. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  339. */
  340. int acpi_ec_burst_enable(struct acpi_ec *ec)
  341. {
  342. u8 d;
  343. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  344. .wdata = NULL, .rdata = &d,
  345. .wlen = 0, .rlen = 1};
  346. return acpi_ec_transaction(ec, &t, 0);
  347. }
  348. int acpi_ec_burst_disable(struct acpi_ec *ec)
  349. {
  350. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  351. .wdata = NULL, .rdata = NULL,
  352. .wlen = 0, .rlen = 0};
  353. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  354. acpi_ec_transaction(ec, &t, 0) : 0;
  355. }
  356. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  357. {
  358. int result;
  359. u8 d;
  360. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  361. .wdata = &address, .rdata = &d,
  362. .wlen = 1, .rlen = 1};
  363. result = acpi_ec_transaction(ec, &t, 0);
  364. *data = d;
  365. return result;
  366. }
  367. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  368. {
  369. u8 wdata[2] = { address, data };
  370. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  371. .wdata = wdata, .rdata = NULL,
  372. .wlen = 2, .rlen = 0};
  373. return acpi_ec_transaction(ec, &t, 0);
  374. }
  375. /*
  376. * Externally callable EC access functions. For now, assume 1 EC only
  377. */
  378. int ec_burst_enable(void)
  379. {
  380. if (!first_ec)
  381. return -ENODEV;
  382. return acpi_ec_burst_enable(first_ec);
  383. }
  384. EXPORT_SYMBOL(ec_burst_enable);
  385. int ec_burst_disable(void)
  386. {
  387. if (!first_ec)
  388. return -ENODEV;
  389. return acpi_ec_burst_disable(first_ec);
  390. }
  391. EXPORT_SYMBOL(ec_burst_disable);
  392. int ec_read(u8 addr, u8 * val)
  393. {
  394. int err;
  395. u8 temp_data;
  396. if (!first_ec)
  397. return -ENODEV;
  398. err = acpi_ec_read(first_ec, addr, &temp_data);
  399. if (!err) {
  400. *val = temp_data;
  401. return 0;
  402. } else
  403. return err;
  404. }
  405. EXPORT_SYMBOL(ec_read);
  406. int ec_write(u8 addr, u8 val)
  407. {
  408. int err;
  409. if (!first_ec)
  410. return -ENODEV;
  411. err = acpi_ec_write(first_ec, addr, val);
  412. return err;
  413. }
  414. EXPORT_SYMBOL(ec_write);
  415. int ec_transaction(u8 command,
  416. const u8 * wdata, unsigned wdata_len,
  417. u8 * rdata, unsigned rdata_len,
  418. int force_poll)
  419. {
  420. struct transaction t = {.command = command,
  421. .wdata = wdata, .rdata = rdata,
  422. .wlen = wdata_len, .rlen = rdata_len};
  423. if (!first_ec)
  424. return -ENODEV;
  425. return acpi_ec_transaction(first_ec, &t, force_poll);
  426. }
  427. EXPORT_SYMBOL(ec_transaction);
  428. static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
  429. {
  430. int result;
  431. u8 d;
  432. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  433. .wdata = NULL, .rdata = &d,
  434. .wlen = 0, .rlen = 1};
  435. if (!ec || !data)
  436. return -EINVAL;
  437. /*
  438. * Query the EC to find out which _Qxx method we need to evaluate.
  439. * Note that successful completion of the query causes the ACPI_EC_SCI
  440. * bit to be cleared (and thus clearing the interrupt source).
  441. */
  442. result = acpi_ec_transaction(ec, &t, 0);
  443. if (result)
  444. return result;
  445. if (!d)
  446. return -ENODATA;
  447. *data = d;
  448. return 0;
  449. }
  450. /* --------------------------------------------------------------------------
  451. Event Management
  452. -------------------------------------------------------------------------- */
  453. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  454. acpi_handle handle, acpi_ec_query_func func,
  455. void *data)
  456. {
  457. struct acpi_ec_query_handler *handler =
  458. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  459. if (!handler)
  460. return -ENOMEM;
  461. handler->query_bit = query_bit;
  462. handler->handle = handle;
  463. handler->func = func;
  464. handler->data = data;
  465. mutex_lock(&ec->lock);
  466. list_add(&handler->node, &ec->list);
  467. mutex_unlock(&ec->lock);
  468. return 0;
  469. }
  470. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  471. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  472. {
  473. struct acpi_ec_query_handler *handler, *tmp;
  474. mutex_lock(&ec->lock);
  475. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  476. if (query_bit == handler->query_bit) {
  477. list_del(&handler->node);
  478. kfree(handler);
  479. }
  480. }
  481. mutex_unlock(&ec->lock);
  482. }
  483. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  484. static void acpi_ec_gpe_query(void *ec_cxt)
  485. {
  486. struct acpi_ec *ec = ec_cxt;
  487. u8 value = 0;
  488. struct acpi_ec_query_handler *handler, copy;
  489. if (!ec || acpi_ec_query(ec, &value))
  490. return;
  491. mutex_lock(&ec->lock);
  492. list_for_each_entry(handler, &ec->list, node) {
  493. if (value == handler->query_bit) {
  494. /* have custom handler for this bit */
  495. memcpy(&copy, handler, sizeof(copy));
  496. mutex_unlock(&ec->lock);
  497. if (copy.func) {
  498. copy.func(copy.data);
  499. } else if (copy.handle) {
  500. acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
  501. }
  502. return;
  503. }
  504. }
  505. mutex_unlock(&ec->lock);
  506. }
  507. static u32 acpi_ec_gpe_handler(void *data)
  508. {
  509. struct acpi_ec *ec = data;
  510. u8 status;
  511. pr_debug(PREFIX "~~~> interrupt\n");
  512. status = acpi_ec_read_status(ec);
  513. if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
  514. gpe_transaction(ec, status);
  515. if (ec_transaction_done(ec) &&
  516. (status & ACPI_EC_FLAG_IBF) == 0)
  517. wake_up(&ec->wait);
  518. }
  519. ec_check_sci(ec, status);
  520. if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
  521. !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
  522. /* this is non-query, must be confirmation */
  523. if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  524. if (printk_ratelimit())
  525. pr_info(PREFIX "non-query interrupt received,"
  526. " switching to interrupt mode\n");
  527. } else {
  528. /* hush, STORM switches the mode every transaction */
  529. pr_debug(PREFIX "non-query interrupt received,"
  530. " switching to interrupt mode\n");
  531. }
  532. set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  533. }
  534. return ACPI_INTERRUPT_HANDLED;
  535. }
  536. /* --------------------------------------------------------------------------
  537. Address Space Management
  538. -------------------------------------------------------------------------- */
  539. static acpi_status
  540. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  541. u32 bits, acpi_integer *value,
  542. void *handler_context, void *region_context)
  543. {
  544. struct acpi_ec *ec = handler_context;
  545. int result = 0, i;
  546. u8 temp = 0;
  547. if ((address > 0xFF) || !value || !handler_context)
  548. return AE_BAD_PARAMETER;
  549. if (function != ACPI_READ && function != ACPI_WRITE)
  550. return AE_BAD_PARAMETER;
  551. if (bits != 8 && acpi_strict)
  552. return AE_BAD_PARAMETER;
  553. acpi_ec_burst_enable(ec);
  554. if (function == ACPI_READ) {
  555. result = acpi_ec_read(ec, address, &temp);
  556. *value = temp;
  557. } else {
  558. temp = 0xff & (*value);
  559. result = acpi_ec_write(ec, address, temp);
  560. }
  561. for (i = 8; unlikely(bits - i > 0); i += 8) {
  562. ++address;
  563. if (function == ACPI_READ) {
  564. result = acpi_ec_read(ec, address, &temp);
  565. (*value) |= ((acpi_integer)temp) << i;
  566. } else {
  567. temp = 0xff & ((*value) >> i);
  568. result = acpi_ec_write(ec, address, temp);
  569. }
  570. }
  571. acpi_ec_burst_disable(ec);
  572. switch (result) {
  573. case -EINVAL:
  574. return AE_BAD_PARAMETER;
  575. break;
  576. case -ENODEV:
  577. return AE_NOT_FOUND;
  578. break;
  579. case -ETIME:
  580. return AE_TIME;
  581. break;
  582. default:
  583. return AE_OK;
  584. }
  585. }
  586. /* --------------------------------------------------------------------------
  587. FS Interface (/proc)
  588. -------------------------------------------------------------------------- */
  589. static struct proc_dir_entry *acpi_ec_dir;
  590. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  591. {
  592. struct acpi_ec *ec = seq->private;
  593. if (!ec)
  594. goto end;
  595. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  596. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  597. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  598. seq_printf(seq, "use global lock:\t%s\n",
  599. ec->global_lock ? "yes" : "no");
  600. end:
  601. return 0;
  602. }
  603. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  604. {
  605. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  606. }
  607. static struct file_operations acpi_ec_info_ops = {
  608. .open = acpi_ec_info_open_fs,
  609. .read = seq_read,
  610. .llseek = seq_lseek,
  611. .release = single_release,
  612. .owner = THIS_MODULE,
  613. };
  614. static int acpi_ec_add_fs(struct acpi_device *device)
  615. {
  616. struct proc_dir_entry *entry = NULL;
  617. if (!acpi_device_dir(device)) {
  618. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  619. acpi_ec_dir);
  620. if (!acpi_device_dir(device))
  621. return -ENODEV;
  622. }
  623. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  624. acpi_device_dir(device),
  625. &acpi_ec_info_ops, acpi_driver_data(device));
  626. if (!entry)
  627. return -ENODEV;
  628. return 0;
  629. }
  630. static int acpi_ec_remove_fs(struct acpi_device *device)
  631. {
  632. if (acpi_device_dir(device)) {
  633. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  634. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  635. acpi_device_dir(device) = NULL;
  636. }
  637. return 0;
  638. }
  639. /* --------------------------------------------------------------------------
  640. Driver Interface
  641. -------------------------------------------------------------------------- */
  642. static acpi_status
  643. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  644. static struct acpi_ec *make_acpi_ec(void)
  645. {
  646. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  647. if (!ec)
  648. return NULL;
  649. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  650. mutex_init(&ec->lock);
  651. init_waitqueue_head(&ec->wait);
  652. INIT_LIST_HEAD(&ec->list);
  653. spin_lock_init(&ec->curr_lock);
  654. return ec;
  655. }
  656. static acpi_status
  657. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  658. void *context, void **return_value)
  659. {
  660. struct acpi_namespace_node *node = handle;
  661. struct acpi_ec *ec = context;
  662. int value = 0;
  663. if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
  664. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  665. }
  666. return AE_OK;
  667. }
  668. static acpi_status
  669. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  670. {
  671. acpi_status status;
  672. unsigned long long tmp;
  673. struct acpi_ec *ec = context;
  674. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  675. ec_parse_io_ports, ec);
  676. if (ACPI_FAILURE(status))
  677. return status;
  678. /* Get GPE bit assignment (EC events). */
  679. /* TODO: Add support for _GPE returning a package */
  680. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  681. if (ACPI_FAILURE(status))
  682. return status;
  683. ec->gpe = tmp;
  684. /* Use the global lock for all EC transactions? */
  685. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  686. ec->global_lock = tmp;
  687. ec->handle = handle;
  688. return AE_CTRL_TERMINATE;
  689. }
  690. static void ec_remove_handlers(struct acpi_ec *ec)
  691. {
  692. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  693. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  694. pr_err(PREFIX "failed to remove space handler\n");
  695. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  696. &acpi_ec_gpe_handler)))
  697. pr_err(PREFIX "failed to remove gpe handler\n");
  698. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  699. }
  700. static int acpi_ec_add(struct acpi_device *device)
  701. {
  702. struct acpi_ec *ec = NULL;
  703. if (!device)
  704. return -EINVAL;
  705. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  706. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  707. /* Check for boot EC */
  708. if (boot_ec &&
  709. (boot_ec->handle == device->handle ||
  710. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  711. ec = boot_ec;
  712. boot_ec = NULL;
  713. } else {
  714. ec = make_acpi_ec();
  715. if (!ec)
  716. return -ENOMEM;
  717. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  718. AE_CTRL_TERMINATE) {
  719. kfree(ec);
  720. return -EINVAL;
  721. }
  722. }
  723. ec->handle = device->handle;
  724. /* Find and register all query methods */
  725. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  726. acpi_ec_register_query_methods, ec, NULL);
  727. if (!first_ec)
  728. first_ec = ec;
  729. device->driver_data = ec;
  730. acpi_ec_add_fs(device);
  731. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  732. ec->gpe, ec->command_addr, ec->data_addr);
  733. pr_info(PREFIX "driver started in %s mode\n",
  734. (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
  735. return 0;
  736. }
  737. static int acpi_ec_remove(struct acpi_device *device, int type)
  738. {
  739. struct acpi_ec *ec;
  740. struct acpi_ec_query_handler *handler, *tmp;
  741. if (!device)
  742. return -EINVAL;
  743. ec = acpi_driver_data(device);
  744. mutex_lock(&ec->lock);
  745. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  746. list_del(&handler->node);
  747. kfree(handler);
  748. }
  749. mutex_unlock(&ec->lock);
  750. acpi_ec_remove_fs(device);
  751. device->driver_data = NULL;
  752. if (ec == first_ec)
  753. first_ec = NULL;
  754. kfree(ec);
  755. return 0;
  756. }
  757. static acpi_status
  758. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  759. {
  760. struct acpi_ec *ec = context;
  761. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  762. return AE_OK;
  763. /*
  764. * The first address region returned is the data port, and
  765. * the second address region returned is the status/command
  766. * port.
  767. */
  768. if (ec->data_addr == 0)
  769. ec->data_addr = resource->data.io.minimum;
  770. else if (ec->command_addr == 0)
  771. ec->command_addr = resource->data.io.minimum;
  772. else
  773. return AE_CTRL_TERMINATE;
  774. return AE_OK;
  775. }
  776. static int ec_install_handlers(struct acpi_ec *ec)
  777. {
  778. acpi_status status;
  779. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  780. return 0;
  781. status = acpi_install_gpe_handler(NULL, ec->gpe,
  782. ACPI_GPE_EDGE_TRIGGERED,
  783. &acpi_ec_gpe_handler, ec);
  784. if (ACPI_FAILURE(status))
  785. return -ENODEV;
  786. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  787. acpi_enable_gpe(NULL, ec->gpe);
  788. status = acpi_install_address_space_handler(ec->handle,
  789. ACPI_ADR_SPACE_EC,
  790. &acpi_ec_space_handler,
  791. NULL, ec);
  792. if (ACPI_FAILURE(status)) {
  793. if (status == AE_NOT_FOUND) {
  794. /*
  795. * Maybe OS fails in evaluating the _REG object.
  796. * The AE_NOT_FOUND error will be ignored and OS
  797. * continue to initialize EC.
  798. */
  799. printk(KERN_ERR "Fail in evaluating the _REG object"
  800. " of EC device. Broken bios is suspected.\n");
  801. } else {
  802. acpi_remove_gpe_handler(NULL, ec->gpe,
  803. &acpi_ec_gpe_handler);
  804. return -ENODEV;
  805. }
  806. }
  807. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  808. return 0;
  809. }
  810. static int acpi_ec_start(struct acpi_device *device)
  811. {
  812. struct acpi_ec *ec;
  813. int ret = 0;
  814. if (!device)
  815. return -EINVAL;
  816. ec = acpi_driver_data(device);
  817. if (!ec)
  818. return -EINVAL;
  819. ret = ec_install_handlers(ec);
  820. /* EC is fully operational, allow queries */
  821. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  822. return ret;
  823. }
  824. static int acpi_ec_stop(struct acpi_device *device, int type)
  825. {
  826. struct acpi_ec *ec;
  827. if (!device)
  828. return -EINVAL;
  829. ec = acpi_driver_data(device);
  830. if (!ec)
  831. return -EINVAL;
  832. ec_remove_handlers(ec);
  833. return 0;
  834. }
  835. int __init acpi_boot_ec_enable(void)
  836. {
  837. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  838. return 0;
  839. if (!ec_install_handlers(boot_ec)) {
  840. first_ec = boot_ec;
  841. return 0;
  842. }
  843. return -EFAULT;
  844. }
  845. static const struct acpi_device_id ec_device_ids[] = {
  846. {"PNP0C09", 0},
  847. {"", 0},
  848. };
  849. int __init acpi_ec_ecdt_probe(void)
  850. {
  851. int ret;
  852. acpi_status status;
  853. struct acpi_table_ecdt *ecdt_ptr;
  854. boot_ec = make_acpi_ec();
  855. if (!boot_ec)
  856. return -ENOMEM;
  857. /*
  858. * Generate a boot ec context
  859. */
  860. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  861. (struct acpi_table_header **)&ecdt_ptr);
  862. if (ACPI_SUCCESS(status)) {
  863. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  864. boot_ec->command_addr = ecdt_ptr->control.address;
  865. boot_ec->data_addr = ecdt_ptr->data.address;
  866. if (dmi_check_system(ec_dmi_table)) {
  867. /*
  868. * If the board falls into ec_dmi_table, it means
  869. * that ECDT table gives the incorrect command/status
  870. * & data I/O address. Just fix it.
  871. */
  872. boot_ec->data_addr = ecdt_ptr->control.address;
  873. boot_ec->command_addr = ecdt_ptr->data.address;
  874. }
  875. boot_ec->gpe = ecdt_ptr->gpe;
  876. boot_ec->handle = ACPI_ROOT_OBJECT;
  877. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  878. } else {
  879. /* This workaround is needed only on some broken machines,
  880. * which require early EC, but fail to provide ECDT */
  881. acpi_handle x;
  882. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  883. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  884. boot_ec, NULL);
  885. /* Check that acpi_get_devices actually find something */
  886. if (ACPI_FAILURE(status) || !boot_ec->handle)
  887. goto error;
  888. /* We really need to limit this workaround, the only ASUS,
  889. * which needs it, has fake EC._INI method, so use it as flag.
  890. * Keep boot_ec struct as it will be needed soon.
  891. */
  892. if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
  893. return -ENODEV;
  894. }
  895. ret = ec_install_handlers(boot_ec);
  896. if (!ret) {
  897. first_ec = boot_ec;
  898. return 0;
  899. }
  900. error:
  901. kfree(boot_ec);
  902. boot_ec = NULL;
  903. return -ENODEV;
  904. }
  905. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  906. {
  907. struct acpi_ec *ec = acpi_driver_data(device);
  908. /* Stop using GPE */
  909. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  910. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  911. acpi_disable_gpe(NULL, ec->gpe);
  912. return 0;
  913. }
  914. static int acpi_ec_resume(struct acpi_device *device)
  915. {
  916. struct acpi_ec *ec = acpi_driver_data(device);
  917. /* Enable use of GPE back */
  918. clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
  919. acpi_enable_gpe(NULL, ec->gpe);
  920. return 0;
  921. }
  922. static struct acpi_driver acpi_ec_driver = {
  923. .name = "ec",
  924. .class = ACPI_EC_CLASS,
  925. .ids = ec_device_ids,
  926. .ops = {
  927. .add = acpi_ec_add,
  928. .remove = acpi_ec_remove,
  929. .start = acpi_ec_start,
  930. .stop = acpi_ec_stop,
  931. .suspend = acpi_ec_suspend,
  932. .resume = acpi_ec_resume,
  933. },
  934. };
  935. static int __init acpi_ec_init(void)
  936. {
  937. int result = 0;
  938. if (acpi_disabled)
  939. return 0;
  940. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  941. if (!acpi_ec_dir)
  942. return -ENODEV;
  943. /* Now register the driver for the EC */
  944. result = acpi_bus_register_driver(&acpi_ec_driver);
  945. if (result < 0) {
  946. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  947. return -ENODEV;
  948. }
  949. return result;
  950. }
  951. subsys_initcall(acpi_ec_init);
  952. /* EC driver currently not unloadable */
  953. #if 0
  954. static void __exit acpi_ec_exit(void)
  955. {
  956. acpi_bus_unregister_driver(&acpi_ec_driver);
  957. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  958. return;
  959. }
  960. #endif /* 0 */