ec.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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/interrupt.h>
  36. #include <linux/list.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/slab.h>
  39. #include <asm/io.h>
  40. #include <acpi/acpi_bus.h>
  41. #include <acpi/acpi_drivers.h>
  42. #include <linux/dmi.h>
  43. #include "internal.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_MSI_UDELAY 550 /* Wait 550us for MSI EC */
  65. enum {
  66. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  67. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  68. EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
  69. * OpReg are installed */
  70. EC_FLAGS_BLOCKED, /* Transactions are blocked */
  71. };
  72. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  73. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  74. module_param(ec_delay, uint, 0644);
  75. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  76. /*
  77. * If the number of false interrupts per one transaction exceeds
  78. * this threshold, will think there is a GPE storm happened and
  79. * will disable the GPE for normal transaction.
  80. */
  81. static unsigned int ec_storm_threshold __read_mostly = 8;
  82. module_param(ec_storm_threshold, uint, 0644);
  83. MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
  84. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  85. /* External interfaces use first EC only, so remember */
  86. typedef int (*acpi_ec_query_func) (void *data);
  87. struct acpi_ec_query_handler {
  88. struct list_head node;
  89. acpi_ec_query_func func;
  90. acpi_handle handle;
  91. void *data;
  92. u8 query_bit;
  93. };
  94. struct transaction {
  95. const u8 *wdata;
  96. u8 *rdata;
  97. unsigned short irq_count;
  98. u8 command;
  99. u8 wi;
  100. u8 ri;
  101. u8 wlen;
  102. u8 rlen;
  103. bool done;
  104. };
  105. struct acpi_ec *boot_ec, *first_ec;
  106. EXPORT_SYMBOL(first_ec);
  107. static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
  108. static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
  109. static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
  110. /* --------------------------------------------------------------------------
  111. Transaction Management
  112. -------------------------------------------------------------------------- */
  113. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  114. {
  115. u8 x = inb(ec->command_addr);
  116. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  117. return x;
  118. }
  119. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  120. {
  121. u8 x = inb(ec->data_addr);
  122. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  123. return x;
  124. }
  125. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  126. {
  127. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  128. outb(command, ec->command_addr);
  129. }
  130. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  131. {
  132. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  133. outb(data, ec->data_addr);
  134. }
  135. static int ec_transaction_done(struct acpi_ec *ec)
  136. {
  137. unsigned long flags;
  138. int ret = 0;
  139. spin_lock_irqsave(&ec->curr_lock, flags);
  140. if (!ec->curr || ec->curr->done)
  141. ret = 1;
  142. spin_unlock_irqrestore(&ec->curr_lock, flags);
  143. return ret;
  144. }
  145. static void start_transaction(struct acpi_ec *ec)
  146. {
  147. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  148. ec->curr->done = false;
  149. acpi_ec_write_cmd(ec, ec->curr->command);
  150. }
  151. static void advance_transaction(struct acpi_ec *ec, u8 status)
  152. {
  153. unsigned long flags;
  154. spin_lock_irqsave(&ec->curr_lock, flags);
  155. if (!ec->curr)
  156. goto unlock;
  157. if (ec->curr->wlen > ec->curr->wi) {
  158. if ((status & ACPI_EC_FLAG_IBF) == 0)
  159. acpi_ec_write_data(ec,
  160. ec->curr->wdata[ec->curr->wi++]);
  161. else
  162. goto err;
  163. } else if (ec->curr->rlen > ec->curr->ri) {
  164. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  165. ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
  166. if (ec->curr->rlen == ec->curr->ri)
  167. ec->curr->done = true;
  168. } else
  169. goto err;
  170. } else if (ec->curr->wlen == ec->curr->wi &&
  171. (status & ACPI_EC_FLAG_IBF) == 0)
  172. ec->curr->done = true;
  173. goto unlock;
  174. err:
  175. /* false interrupt, state didn't change */
  176. if (in_interrupt())
  177. ++ec->curr->irq_count;
  178. unlock:
  179. spin_unlock_irqrestore(&ec->curr_lock, flags);
  180. }
  181. static int acpi_ec_sync_query(struct acpi_ec *ec);
  182. static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
  183. {
  184. if (state & ACPI_EC_FLAG_SCI) {
  185. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  186. return acpi_ec_sync_query(ec);
  187. }
  188. return 0;
  189. }
  190. static int ec_poll(struct acpi_ec *ec)
  191. {
  192. unsigned long flags;
  193. int repeat = 2; /* number of command restarts */
  194. while (repeat--) {
  195. unsigned long delay = jiffies +
  196. msecs_to_jiffies(ec_delay);
  197. do {
  198. /* don't sleep with disabled interrupts */
  199. if (EC_FLAGS_MSI || irqs_disabled()) {
  200. udelay(ACPI_EC_MSI_UDELAY);
  201. if (ec_transaction_done(ec))
  202. return 0;
  203. } else {
  204. if (wait_event_timeout(ec->wait,
  205. ec_transaction_done(ec),
  206. msecs_to_jiffies(1)))
  207. return 0;
  208. }
  209. advance_transaction(ec, acpi_ec_read_status(ec));
  210. } while (time_before(jiffies, delay));
  211. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
  212. break;
  213. pr_debug(PREFIX "controller reset, restart transaction\n");
  214. spin_lock_irqsave(&ec->curr_lock, flags);
  215. start_transaction(ec);
  216. spin_unlock_irqrestore(&ec->curr_lock, flags);
  217. }
  218. return -ETIME;
  219. }
  220. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  221. struct transaction *t)
  222. {
  223. unsigned long tmp;
  224. int ret = 0;
  225. if (EC_FLAGS_MSI)
  226. udelay(ACPI_EC_MSI_UDELAY);
  227. /* start transaction */
  228. spin_lock_irqsave(&ec->curr_lock, tmp);
  229. /* following two actions should be kept atomic */
  230. ec->curr = t;
  231. start_transaction(ec);
  232. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  233. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  234. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  235. ret = ec_poll(ec);
  236. spin_lock_irqsave(&ec->curr_lock, tmp);
  237. ec->curr = NULL;
  238. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  239. return ret;
  240. }
  241. static int ec_check_ibf0(struct acpi_ec *ec)
  242. {
  243. u8 status = acpi_ec_read_status(ec);
  244. return (status & ACPI_EC_FLAG_IBF) == 0;
  245. }
  246. static int ec_wait_ibf0(struct acpi_ec *ec)
  247. {
  248. unsigned long delay = jiffies + msecs_to_jiffies(ec_delay);
  249. /* interrupt wait manually if GPE mode is not active */
  250. while (time_before(jiffies, delay))
  251. if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
  252. msecs_to_jiffies(1)))
  253. return 0;
  254. return -ETIME;
  255. }
  256. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  257. {
  258. int status;
  259. u32 glk;
  260. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  261. return -EINVAL;
  262. if (t->rdata)
  263. memset(t->rdata, 0, t->rlen);
  264. mutex_lock(&ec->lock);
  265. if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
  266. status = -EINVAL;
  267. goto unlock;
  268. }
  269. if (ec->global_lock) {
  270. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  271. if (ACPI_FAILURE(status)) {
  272. status = -ENODEV;
  273. goto unlock;
  274. }
  275. }
  276. if (ec_wait_ibf0(ec)) {
  277. pr_err(PREFIX "input buffer is not empty, "
  278. "aborting transaction\n");
  279. status = -ETIME;
  280. goto end;
  281. }
  282. pr_debug(PREFIX "transaction start\n");
  283. /* disable GPE during transaction if storm is detected */
  284. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  285. /* It has to be disabled, so that it doesn't trigger. */
  286. acpi_disable_gpe(NULL, ec->gpe);
  287. }
  288. status = acpi_ec_transaction_unlocked(ec, t);
  289. /* check if we received SCI during transaction */
  290. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  291. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  292. msleep(1);
  293. /* It is safe to enable the GPE outside of the transaction. */
  294. acpi_enable_gpe(NULL, ec->gpe);
  295. } else if (t->irq_count > ec_storm_threshold) {
  296. pr_info(PREFIX "GPE storm detected, "
  297. "transactions will use polling mode\n");
  298. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  299. }
  300. pr_debug(PREFIX "transaction end\n");
  301. end:
  302. if (ec->global_lock)
  303. acpi_release_global_lock(glk);
  304. unlock:
  305. mutex_unlock(&ec->lock);
  306. return status;
  307. }
  308. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  309. {
  310. u8 d;
  311. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  312. .wdata = NULL, .rdata = &d,
  313. .wlen = 0, .rlen = 1};
  314. return acpi_ec_transaction(ec, &t);
  315. }
  316. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  317. {
  318. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  319. .wdata = NULL, .rdata = NULL,
  320. .wlen = 0, .rlen = 0};
  321. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  322. acpi_ec_transaction(ec, &t) : 0;
  323. }
  324. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  325. {
  326. int result;
  327. u8 d;
  328. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  329. .wdata = &address, .rdata = &d,
  330. .wlen = 1, .rlen = 1};
  331. result = acpi_ec_transaction(ec, &t);
  332. *data = d;
  333. return result;
  334. }
  335. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  336. {
  337. u8 wdata[2] = { address, data };
  338. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  339. .wdata = wdata, .rdata = NULL,
  340. .wlen = 2, .rlen = 0};
  341. return acpi_ec_transaction(ec, &t);
  342. }
  343. /*
  344. * Externally callable EC access functions. For now, assume 1 EC only
  345. */
  346. int ec_burst_enable(void)
  347. {
  348. if (!first_ec)
  349. return -ENODEV;
  350. return acpi_ec_burst_enable(first_ec);
  351. }
  352. EXPORT_SYMBOL(ec_burst_enable);
  353. int ec_burst_disable(void)
  354. {
  355. if (!first_ec)
  356. return -ENODEV;
  357. return acpi_ec_burst_disable(first_ec);
  358. }
  359. EXPORT_SYMBOL(ec_burst_disable);
  360. int ec_read(u8 addr, u8 * val)
  361. {
  362. int err;
  363. u8 temp_data;
  364. if (!first_ec)
  365. return -ENODEV;
  366. err = acpi_ec_read(first_ec, addr, &temp_data);
  367. if (!err) {
  368. *val = temp_data;
  369. return 0;
  370. } else
  371. return err;
  372. }
  373. EXPORT_SYMBOL(ec_read);
  374. int ec_write(u8 addr, u8 val)
  375. {
  376. int err;
  377. if (!first_ec)
  378. return -ENODEV;
  379. err = acpi_ec_write(first_ec, addr, val);
  380. return err;
  381. }
  382. EXPORT_SYMBOL(ec_write);
  383. int ec_transaction(u8 command,
  384. const u8 * wdata, unsigned wdata_len,
  385. u8 * rdata, unsigned rdata_len)
  386. {
  387. struct transaction t = {.command = command,
  388. .wdata = wdata, .rdata = rdata,
  389. .wlen = wdata_len, .rlen = rdata_len};
  390. if (!first_ec)
  391. return -ENODEV;
  392. return acpi_ec_transaction(first_ec, &t);
  393. }
  394. EXPORT_SYMBOL(ec_transaction);
  395. /* Get the handle to the EC device */
  396. acpi_handle ec_get_handle(void)
  397. {
  398. if (!first_ec)
  399. return NULL;
  400. return first_ec->handle;
  401. }
  402. EXPORT_SYMBOL(ec_get_handle);
  403. void acpi_ec_block_transactions(void)
  404. {
  405. struct acpi_ec *ec = first_ec;
  406. if (!ec)
  407. return;
  408. mutex_lock(&ec->lock);
  409. /* Prevent transactions from being carried out */
  410. set_bit(EC_FLAGS_BLOCKED, &ec->flags);
  411. mutex_unlock(&ec->lock);
  412. }
  413. void acpi_ec_unblock_transactions(void)
  414. {
  415. struct acpi_ec *ec = first_ec;
  416. if (!ec)
  417. return;
  418. mutex_lock(&ec->lock);
  419. /* Allow transactions to be carried out again */
  420. clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
  421. mutex_unlock(&ec->lock);
  422. }
  423. void acpi_ec_unblock_transactions_early(void)
  424. {
  425. /*
  426. * Allow transactions to happen again (this function is called from
  427. * atomic context during wakeup, so we don't need to acquire the mutex).
  428. */
  429. if (first_ec)
  430. clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
  431. }
  432. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  433. {
  434. int result;
  435. u8 d;
  436. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  437. .wdata = NULL, .rdata = &d,
  438. .wlen = 0, .rlen = 1};
  439. if (!ec || !data)
  440. return -EINVAL;
  441. /*
  442. * Query the EC to find out which _Qxx method we need to evaluate.
  443. * Note that successful completion of the query causes the ACPI_EC_SCI
  444. * bit to be cleared (and thus clearing the interrupt source).
  445. */
  446. result = acpi_ec_transaction_unlocked(ec, &t);
  447. if (result)
  448. return result;
  449. if (!d)
  450. return -ENODATA;
  451. *data = d;
  452. return 0;
  453. }
  454. /* --------------------------------------------------------------------------
  455. Event Management
  456. -------------------------------------------------------------------------- */
  457. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  458. acpi_handle handle, acpi_ec_query_func func,
  459. void *data)
  460. {
  461. struct acpi_ec_query_handler *handler =
  462. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  463. if (!handler)
  464. return -ENOMEM;
  465. handler->query_bit = query_bit;
  466. handler->handle = handle;
  467. handler->func = func;
  468. handler->data = data;
  469. mutex_lock(&ec->lock);
  470. list_add(&handler->node, &ec->list);
  471. mutex_unlock(&ec->lock);
  472. return 0;
  473. }
  474. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  475. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  476. {
  477. struct acpi_ec_query_handler *handler, *tmp;
  478. mutex_lock(&ec->lock);
  479. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  480. if (query_bit == handler->query_bit) {
  481. list_del(&handler->node);
  482. kfree(handler);
  483. }
  484. }
  485. mutex_unlock(&ec->lock);
  486. }
  487. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  488. static void acpi_ec_run(void *cxt)
  489. {
  490. struct acpi_ec_query_handler *handler = cxt;
  491. if (!handler)
  492. return;
  493. pr_debug(PREFIX "start query execution\n");
  494. if (handler->func)
  495. handler->func(handler->data);
  496. else if (handler->handle)
  497. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  498. pr_debug(PREFIX "stop query execution\n");
  499. kfree(handler);
  500. }
  501. static int acpi_ec_sync_query(struct acpi_ec *ec)
  502. {
  503. u8 value = 0;
  504. int status;
  505. struct acpi_ec_query_handler *handler, *copy;
  506. if ((status = acpi_ec_query_unlocked(ec, &value)))
  507. return status;
  508. list_for_each_entry(handler, &ec->list, node) {
  509. if (value == handler->query_bit) {
  510. /* have custom handler for this bit */
  511. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  512. if (!copy)
  513. return -ENOMEM;
  514. memcpy(copy, handler, sizeof(*copy));
  515. pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
  516. return acpi_os_execute((copy->func) ?
  517. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  518. acpi_ec_run, copy);
  519. }
  520. }
  521. return 0;
  522. }
  523. static void acpi_ec_gpe_query(void *ec_cxt)
  524. {
  525. struct acpi_ec *ec = ec_cxt;
  526. if (!ec)
  527. return;
  528. mutex_lock(&ec->lock);
  529. acpi_ec_sync_query(ec);
  530. mutex_unlock(&ec->lock);
  531. }
  532. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  533. {
  534. if (state & ACPI_EC_FLAG_SCI) {
  535. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  536. pr_debug(PREFIX "push gpe query to the queue\n");
  537. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  538. acpi_ec_gpe_query, ec);
  539. }
  540. }
  541. return 0;
  542. }
  543. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  544. u32 gpe_number, void *data)
  545. {
  546. struct acpi_ec *ec = data;
  547. pr_debug(PREFIX "~~~> interrupt\n");
  548. advance_transaction(ec, acpi_ec_read_status(ec));
  549. if (ec_transaction_done(ec) &&
  550. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  551. wake_up(&ec->wait);
  552. ec_check_sci(ec, acpi_ec_read_status(ec));
  553. }
  554. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  555. }
  556. /* --------------------------------------------------------------------------
  557. Address Space Management
  558. -------------------------------------------------------------------------- */
  559. static acpi_status
  560. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  561. u32 bits, u64 *value64,
  562. void *handler_context, void *region_context)
  563. {
  564. struct acpi_ec *ec = handler_context;
  565. int result = 0, i, bytes = bits / 8;
  566. u8 *value = (u8 *)value64;
  567. if ((address > 0xFF) || !value || !handler_context)
  568. return AE_BAD_PARAMETER;
  569. if (function != ACPI_READ && function != ACPI_WRITE)
  570. return AE_BAD_PARAMETER;
  571. if (EC_FLAGS_MSI || bits > 8)
  572. acpi_ec_burst_enable(ec);
  573. for (i = 0; i < bytes; ++i, ++address, ++value)
  574. result = (function == ACPI_READ) ?
  575. acpi_ec_read(ec, address, value) :
  576. acpi_ec_write(ec, address, *value);
  577. if (EC_FLAGS_MSI || bits > 8)
  578. acpi_ec_burst_disable(ec);
  579. switch (result) {
  580. case -EINVAL:
  581. return AE_BAD_PARAMETER;
  582. break;
  583. case -ENODEV:
  584. return AE_NOT_FOUND;
  585. break;
  586. case -ETIME:
  587. return AE_TIME;
  588. break;
  589. default:
  590. return AE_OK;
  591. }
  592. }
  593. /* --------------------------------------------------------------------------
  594. Driver Interface
  595. -------------------------------------------------------------------------- */
  596. static acpi_status
  597. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  598. static struct acpi_ec *make_acpi_ec(void)
  599. {
  600. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  601. if (!ec)
  602. return NULL;
  603. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  604. mutex_init(&ec->lock);
  605. init_waitqueue_head(&ec->wait);
  606. INIT_LIST_HEAD(&ec->list);
  607. spin_lock_init(&ec->curr_lock);
  608. return ec;
  609. }
  610. static acpi_status
  611. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  612. void *context, void **return_value)
  613. {
  614. char node_name[5];
  615. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  616. struct acpi_ec *ec = context;
  617. int value = 0;
  618. acpi_status status;
  619. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  620. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  621. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  622. }
  623. return AE_OK;
  624. }
  625. static acpi_status
  626. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  627. {
  628. acpi_status status;
  629. unsigned long long tmp = 0;
  630. struct acpi_ec *ec = context;
  631. /* clear addr values, ec_parse_io_ports depend on it */
  632. ec->command_addr = ec->data_addr = 0;
  633. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  634. ec_parse_io_ports, ec);
  635. if (ACPI_FAILURE(status))
  636. return status;
  637. /* Get GPE bit assignment (EC events). */
  638. /* TODO: Add support for _GPE returning a package */
  639. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  640. if (ACPI_FAILURE(status))
  641. return status;
  642. ec->gpe = tmp;
  643. /* Use the global lock for all EC transactions? */
  644. tmp = 0;
  645. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  646. ec->global_lock = tmp;
  647. ec->handle = handle;
  648. return AE_CTRL_TERMINATE;
  649. }
  650. static int ec_install_handlers(struct acpi_ec *ec)
  651. {
  652. acpi_status status;
  653. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  654. return 0;
  655. status = acpi_install_gpe_handler(NULL, ec->gpe,
  656. ACPI_GPE_EDGE_TRIGGERED,
  657. &acpi_ec_gpe_handler, ec);
  658. if (ACPI_FAILURE(status))
  659. return -ENODEV;
  660. acpi_enable_gpe(NULL, ec->gpe);
  661. status = acpi_install_address_space_handler(ec->handle,
  662. ACPI_ADR_SPACE_EC,
  663. &acpi_ec_space_handler,
  664. NULL, ec);
  665. if (ACPI_FAILURE(status)) {
  666. if (status == AE_NOT_FOUND) {
  667. /*
  668. * Maybe OS fails in evaluating the _REG object.
  669. * The AE_NOT_FOUND error will be ignored and OS
  670. * continue to initialize EC.
  671. */
  672. printk(KERN_ERR "Fail in evaluating the _REG object"
  673. " of EC device. Broken bios is suspected.\n");
  674. } else {
  675. acpi_remove_gpe_handler(NULL, ec->gpe,
  676. &acpi_ec_gpe_handler);
  677. acpi_disable_gpe(NULL, ec->gpe);
  678. return -ENODEV;
  679. }
  680. }
  681. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  682. return 0;
  683. }
  684. static void ec_remove_handlers(struct acpi_ec *ec)
  685. {
  686. acpi_disable_gpe(NULL, ec->gpe);
  687. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  688. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  689. pr_err(PREFIX "failed to remove space handler\n");
  690. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  691. &acpi_ec_gpe_handler)))
  692. pr_err(PREFIX "failed to remove gpe handler\n");
  693. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  694. }
  695. static int acpi_ec_add(struct acpi_device *device)
  696. {
  697. struct acpi_ec *ec = NULL;
  698. int ret;
  699. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  700. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  701. /* Check for boot EC */
  702. if (boot_ec &&
  703. (boot_ec->handle == device->handle ||
  704. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  705. ec = boot_ec;
  706. boot_ec = NULL;
  707. } else {
  708. ec = make_acpi_ec();
  709. if (!ec)
  710. return -ENOMEM;
  711. }
  712. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  713. AE_CTRL_TERMINATE) {
  714. kfree(ec);
  715. return -EINVAL;
  716. }
  717. /* Find and register all query methods */
  718. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  719. acpi_ec_register_query_methods, NULL, ec, NULL);
  720. if (!first_ec)
  721. first_ec = ec;
  722. device->driver_data = ec;
  723. ret = !!request_region(ec->data_addr, 1, "EC data");
  724. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  725. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  726. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  727. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  728. ec->gpe, ec->command_addr, ec->data_addr);
  729. ret = ec_install_handlers(ec);
  730. /* EC is fully operational, allow queries */
  731. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  732. return ret;
  733. }
  734. static int acpi_ec_remove(struct acpi_device *device, int type)
  735. {
  736. struct acpi_ec *ec;
  737. struct acpi_ec_query_handler *handler, *tmp;
  738. if (!device)
  739. return -EINVAL;
  740. ec = acpi_driver_data(device);
  741. ec_remove_handlers(ec);
  742. mutex_lock(&ec->lock);
  743. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  744. list_del(&handler->node);
  745. kfree(handler);
  746. }
  747. mutex_unlock(&ec->lock);
  748. release_region(ec->data_addr, 1);
  749. release_region(ec->command_addr, 1);
  750. device->driver_data = NULL;
  751. if (ec == first_ec)
  752. first_ec = NULL;
  753. kfree(ec);
  754. return 0;
  755. }
  756. static acpi_status
  757. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  758. {
  759. struct acpi_ec *ec = context;
  760. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  761. return AE_OK;
  762. /*
  763. * The first address region returned is the data port, and
  764. * the second address region returned is the status/command
  765. * port.
  766. */
  767. if (ec->data_addr == 0)
  768. ec->data_addr = resource->data.io.minimum;
  769. else if (ec->command_addr == 0)
  770. ec->command_addr = resource->data.io.minimum;
  771. else
  772. return AE_CTRL_TERMINATE;
  773. return AE_OK;
  774. }
  775. int __init acpi_boot_ec_enable(void)
  776. {
  777. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  778. return 0;
  779. if (!ec_install_handlers(boot_ec)) {
  780. first_ec = boot_ec;
  781. return 0;
  782. }
  783. return -EFAULT;
  784. }
  785. static const struct acpi_device_id ec_device_ids[] = {
  786. {"PNP0C09", 0},
  787. {"", 0},
  788. };
  789. /* Some BIOS do not survive early DSDT scan, skip it */
  790. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  791. {
  792. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  793. return 0;
  794. }
  795. /* ASUStek often supplies us with broken ECDT, validate it */
  796. static int ec_validate_ecdt(const struct dmi_system_id *id)
  797. {
  798. EC_FLAGS_VALIDATE_ECDT = 1;
  799. return 0;
  800. }
  801. /* MSI EC needs special treatment, enable it */
  802. static int ec_flag_msi(const struct dmi_system_id *id)
  803. {
  804. printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
  805. EC_FLAGS_MSI = 1;
  806. EC_FLAGS_VALIDATE_ECDT = 1;
  807. return 0;
  808. }
  809. /*
  810. * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
  811. * the GPE storm threshold back to 20
  812. */
  813. static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
  814. {
  815. pr_debug("Setting the EC GPE storm threshold to 20\n");
  816. ec_storm_threshold = 20;
  817. return 0;
  818. }
  819. static struct dmi_system_id __initdata ec_dmi_table[] = {
  820. {
  821. ec_skip_dsdt_scan, "Compal JFL92", {
  822. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  823. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  824. {
  825. ec_flag_msi, "MSI hardware", {
  826. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  827. {
  828. ec_flag_msi, "MSI hardware", {
  829. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  830. {
  831. ec_flag_msi, "MSI hardware", {
  832. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  833. {
  834. ec_flag_msi, "MSI hardware", {
  835. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
  836. {
  837. ec_flag_msi, "Quanta hardware", {
  838. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  839. DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
  840. {
  841. ec_flag_msi, "Quanta hardware", {
  842. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  843. DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
  844. {
  845. ec_validate_ecdt, "ASUS hardware", {
  846. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  847. {
  848. ec_validate_ecdt, "ASUS hardware", {
  849. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
  850. {
  851. ec_enlarge_storm_threshold, "CLEVO hardware", {
  852. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  853. DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
  854. {},
  855. };
  856. int __init acpi_ec_ecdt_probe(void)
  857. {
  858. acpi_status status;
  859. struct acpi_ec *saved_ec = NULL;
  860. struct acpi_table_ecdt *ecdt_ptr;
  861. boot_ec = make_acpi_ec();
  862. if (!boot_ec)
  863. return -ENOMEM;
  864. /*
  865. * Generate a boot ec context
  866. */
  867. dmi_check_system(ec_dmi_table);
  868. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  869. (struct acpi_table_header **)&ecdt_ptr);
  870. if (ACPI_SUCCESS(status)) {
  871. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  872. boot_ec->command_addr = ecdt_ptr->control.address;
  873. boot_ec->data_addr = ecdt_ptr->data.address;
  874. boot_ec->gpe = ecdt_ptr->gpe;
  875. boot_ec->handle = ACPI_ROOT_OBJECT;
  876. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  877. /* Don't trust ECDT, which comes from ASUSTek */
  878. if (!EC_FLAGS_VALIDATE_ECDT)
  879. goto install;
  880. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  881. if (!saved_ec)
  882. return -ENOMEM;
  883. /* fall through */
  884. }
  885. if (EC_FLAGS_SKIP_DSDT_SCAN)
  886. return -ENODEV;
  887. /* This workaround is needed only on some broken machines,
  888. * which require early EC, but fail to provide ECDT */
  889. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  890. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  891. boot_ec, NULL);
  892. /* Check that acpi_get_devices actually find something */
  893. if (ACPI_FAILURE(status) || !boot_ec->handle)
  894. goto error;
  895. if (saved_ec) {
  896. /* try to find good ECDT from ASUSTek */
  897. if (saved_ec->command_addr != boot_ec->command_addr ||
  898. saved_ec->data_addr != boot_ec->data_addr ||
  899. saved_ec->gpe != boot_ec->gpe ||
  900. saved_ec->handle != boot_ec->handle)
  901. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  902. "ECDT tables, which are very hard to workaround. "
  903. "Trying to use DSDT EC info instead. Please send "
  904. "output of acpidump to linux-acpi@vger.kernel.org\n");
  905. kfree(saved_ec);
  906. saved_ec = NULL;
  907. } else {
  908. /* We really need to limit this workaround, the only ASUS,
  909. * which needs it, has fake EC._INI method, so use it as flag.
  910. * Keep boot_ec struct as it will be needed soon.
  911. */
  912. acpi_handle dummy;
  913. if (!dmi_name_in_vendors("ASUS") ||
  914. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  915. &dummy)))
  916. return -ENODEV;
  917. }
  918. install:
  919. if (!ec_install_handlers(boot_ec)) {
  920. first_ec = boot_ec;
  921. return 0;
  922. }
  923. error:
  924. kfree(boot_ec);
  925. boot_ec = NULL;
  926. return -ENODEV;
  927. }
  928. static struct acpi_driver acpi_ec_driver = {
  929. .name = "ec",
  930. .class = ACPI_EC_CLASS,
  931. .ids = ec_device_ids,
  932. .ops = {
  933. .add = acpi_ec_add,
  934. .remove = acpi_ec_remove,
  935. },
  936. };
  937. int __init acpi_ec_init(void)
  938. {
  939. int result = 0;
  940. /* Now register the driver for the EC */
  941. result = acpi_bus_register_driver(&acpi_ec_driver);
  942. if (result < 0)
  943. return -ENODEV;
  944. return result;
  945. }
  946. /* EC driver currently not unloadable */
  947. #if 0
  948. static void __exit acpi_ec_exit(void)
  949. {
  950. acpi_bus_unregister_driver(&acpi_ec_driver);
  951. return;
  952. }
  953. #endif /* 0 */