ec.c 27 KB

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