ec.c 28 KB

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