ec.c 28 KB

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