ec.c 27 KB

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