ec.c 26 KB

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