ec.c 27 KB

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