ec.c 28 KB

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