ec.c 28 KB

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