ec.c 28 KB

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