ec.c 29 KB

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