ec.c 27 KB

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