ec.c 29 KB

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