ec.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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_BLOCKED, /* Transactions are blocked */
  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_BLOCKED, &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. /* It has to be disabled, so that it doesn't trigger. */
  288. acpi_disable_gpe(NULL, ec->gpe);
  289. }
  290. status = acpi_ec_transaction_unlocked(ec, t);
  291. /* check if we received SCI during transaction */
  292. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  293. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  294. msleep(1);
  295. /* It is safe to enable the GPE outside of the transaction. */
  296. acpi_enable_gpe(NULL, ec->gpe);
  297. } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
  298. pr_info(PREFIX "GPE storm detected, "
  299. "transactions will use polling mode\n");
  300. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  301. }
  302. pr_debug(PREFIX "transaction end\n");
  303. end:
  304. if (ec->global_lock)
  305. acpi_release_global_lock(glk);
  306. unlock:
  307. mutex_unlock(&ec->lock);
  308. return status;
  309. }
  310. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  311. {
  312. u8 d;
  313. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  314. .wdata = NULL, .rdata = &d,
  315. .wlen = 0, .rlen = 1};
  316. return acpi_ec_transaction(ec, &t);
  317. }
  318. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  319. {
  320. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  321. .wdata = NULL, .rdata = NULL,
  322. .wlen = 0, .rlen = 0};
  323. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  324. acpi_ec_transaction(ec, &t) : 0;
  325. }
  326. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  327. {
  328. int result;
  329. u8 d;
  330. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  331. .wdata = &address, .rdata = &d,
  332. .wlen = 1, .rlen = 1};
  333. result = acpi_ec_transaction(ec, &t);
  334. *data = d;
  335. return result;
  336. }
  337. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  338. {
  339. u8 wdata[2] = { address, data };
  340. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  341. .wdata = wdata, .rdata = NULL,
  342. .wlen = 2, .rlen = 0};
  343. return acpi_ec_transaction(ec, &t);
  344. }
  345. /*
  346. * Externally callable EC access functions. For now, assume 1 EC only
  347. */
  348. int ec_burst_enable(void)
  349. {
  350. if (!first_ec)
  351. return -ENODEV;
  352. return acpi_ec_burst_enable(first_ec);
  353. }
  354. EXPORT_SYMBOL(ec_burst_enable);
  355. int ec_burst_disable(void)
  356. {
  357. if (!first_ec)
  358. return -ENODEV;
  359. return acpi_ec_burst_disable(first_ec);
  360. }
  361. EXPORT_SYMBOL(ec_burst_disable);
  362. int ec_read(u8 addr, u8 * val)
  363. {
  364. int err;
  365. u8 temp_data;
  366. if (!first_ec)
  367. return -ENODEV;
  368. err = acpi_ec_read(first_ec, addr, &temp_data);
  369. if (!err) {
  370. *val = temp_data;
  371. return 0;
  372. } else
  373. return err;
  374. }
  375. EXPORT_SYMBOL(ec_read);
  376. int ec_write(u8 addr, u8 val)
  377. {
  378. int err;
  379. if (!first_ec)
  380. return -ENODEV;
  381. err = acpi_ec_write(first_ec, addr, val);
  382. return err;
  383. }
  384. EXPORT_SYMBOL(ec_write);
  385. int ec_transaction(u8 command,
  386. const u8 * wdata, unsigned wdata_len,
  387. u8 * rdata, unsigned rdata_len,
  388. int force_poll)
  389. {
  390. struct transaction t = {.command = command,
  391. .wdata = wdata, .rdata = rdata,
  392. .wlen = wdata_len, .rlen = rdata_len};
  393. if (!first_ec)
  394. return -ENODEV;
  395. return acpi_ec_transaction(first_ec, &t);
  396. }
  397. EXPORT_SYMBOL(ec_transaction);
  398. void acpi_ec_block_transactions(void)
  399. {
  400. struct acpi_ec *ec = first_ec;
  401. if (!ec)
  402. return;
  403. mutex_lock(&ec->lock);
  404. /* Prevent transactions from being carried out */
  405. set_bit(EC_FLAGS_BLOCKED, &ec->flags);
  406. mutex_unlock(&ec->lock);
  407. }
  408. void acpi_ec_unblock_transactions(void)
  409. {
  410. struct acpi_ec *ec = first_ec;
  411. if (!ec)
  412. return;
  413. mutex_lock(&ec->lock);
  414. /* Allow transactions to be carried out again */
  415. clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
  416. mutex_unlock(&ec->lock);
  417. }
  418. void acpi_ec_unblock_transactions_early(void)
  419. {
  420. /*
  421. * Allow transactions to happen again (this function is called from
  422. * atomic context during wakeup, so we don't need to acquire the mutex).
  423. */
  424. if (first_ec)
  425. clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
  426. }
  427. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  428. {
  429. int result;
  430. u8 d;
  431. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  432. .wdata = NULL, .rdata = &d,
  433. .wlen = 0, .rlen = 1};
  434. if (!ec || !data)
  435. return -EINVAL;
  436. /*
  437. * Query the EC to find out which _Qxx method we need to evaluate.
  438. * Note that successful completion of the query causes the ACPI_EC_SCI
  439. * bit to be cleared (and thus clearing the interrupt source).
  440. */
  441. result = acpi_ec_transaction_unlocked(ec, &t);
  442. if (result)
  443. return result;
  444. if (!d)
  445. return -ENODATA;
  446. *data = d;
  447. return 0;
  448. }
  449. /* --------------------------------------------------------------------------
  450. Event Management
  451. -------------------------------------------------------------------------- */
  452. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  453. acpi_handle handle, acpi_ec_query_func func,
  454. void *data)
  455. {
  456. struct acpi_ec_query_handler *handler =
  457. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  458. if (!handler)
  459. return -ENOMEM;
  460. handler->query_bit = query_bit;
  461. handler->handle = handle;
  462. handler->func = func;
  463. handler->data = data;
  464. mutex_lock(&ec->lock);
  465. list_add(&handler->node, &ec->list);
  466. mutex_unlock(&ec->lock);
  467. return 0;
  468. }
  469. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  470. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  471. {
  472. struct acpi_ec_query_handler *handler, *tmp;
  473. mutex_lock(&ec->lock);
  474. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  475. if (query_bit == handler->query_bit) {
  476. list_del(&handler->node);
  477. kfree(handler);
  478. }
  479. }
  480. mutex_unlock(&ec->lock);
  481. }
  482. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  483. static void acpi_ec_run(void *cxt)
  484. {
  485. struct acpi_ec_query_handler *handler = cxt;
  486. if (!handler)
  487. return;
  488. pr_debug(PREFIX "start query execution\n");
  489. if (handler->func)
  490. handler->func(handler->data);
  491. else if (handler->handle)
  492. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  493. pr_debug(PREFIX "stop query execution\n");
  494. kfree(handler);
  495. }
  496. static int acpi_ec_sync_query(struct acpi_ec *ec)
  497. {
  498. u8 value = 0;
  499. int status;
  500. struct acpi_ec_query_handler *handler, *copy;
  501. if ((status = acpi_ec_query_unlocked(ec, &value)))
  502. return status;
  503. list_for_each_entry(handler, &ec->list, node) {
  504. if (value == handler->query_bit) {
  505. /* have custom handler for this bit */
  506. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  507. if (!copy)
  508. return -ENOMEM;
  509. memcpy(copy, handler, sizeof(*copy));
  510. pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
  511. return acpi_os_execute((copy->func) ?
  512. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  513. acpi_ec_run, copy);
  514. }
  515. }
  516. return 0;
  517. }
  518. static void acpi_ec_gpe_query(void *ec_cxt)
  519. {
  520. struct acpi_ec *ec = ec_cxt;
  521. if (!ec)
  522. return;
  523. mutex_lock(&ec->lock);
  524. acpi_ec_sync_query(ec);
  525. mutex_unlock(&ec->lock);
  526. }
  527. static void acpi_ec_gpe_query(void *ec_cxt);
  528. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  529. {
  530. if (state & ACPI_EC_FLAG_SCI) {
  531. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  532. pr_debug(PREFIX "push gpe query to the queue\n");
  533. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  534. acpi_ec_gpe_query, ec);
  535. }
  536. }
  537. return 0;
  538. }
  539. static u32 acpi_ec_gpe_handler(void *data)
  540. {
  541. struct acpi_ec *ec = data;
  542. pr_debug(PREFIX "~~~> interrupt\n");
  543. advance_transaction(ec, acpi_ec_read_status(ec));
  544. if (ec_transaction_done(ec) &&
  545. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  546. wake_up(&ec->wait);
  547. ec_check_sci(ec, acpi_ec_read_status(ec));
  548. }
  549. return ACPI_INTERRUPT_HANDLED;
  550. }
  551. /* --------------------------------------------------------------------------
  552. Address Space Management
  553. -------------------------------------------------------------------------- */
  554. static acpi_status
  555. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  556. u32 bits, u64 *value64,
  557. void *handler_context, void *region_context)
  558. {
  559. struct acpi_ec *ec = handler_context;
  560. int result = 0, i, bytes = bits / 8;
  561. u8 *value = (u8 *)value64;
  562. if ((address > 0xFF) || !value || !handler_context)
  563. return AE_BAD_PARAMETER;
  564. if (function != ACPI_READ && function != ACPI_WRITE)
  565. return AE_BAD_PARAMETER;
  566. if (EC_FLAGS_MSI || bits > 8)
  567. acpi_ec_burst_enable(ec);
  568. for (i = 0; i < bytes; ++i, ++address, ++value)
  569. result = (function == ACPI_READ) ?
  570. acpi_ec_read(ec, address, value) :
  571. acpi_ec_write(ec, address, *value);
  572. if (EC_FLAGS_MSI || bits > 8)
  573. acpi_ec_burst_disable(ec);
  574. switch (result) {
  575. case -EINVAL:
  576. return AE_BAD_PARAMETER;
  577. break;
  578. case -ENODEV:
  579. return AE_NOT_FOUND;
  580. break;
  581. case -ETIME:
  582. return AE_TIME;
  583. break;
  584. default:
  585. return AE_OK;
  586. }
  587. }
  588. /* --------------------------------------------------------------------------
  589. FS Interface (/proc)
  590. -------------------------------------------------------------------------- */
  591. static struct proc_dir_entry *acpi_ec_dir;
  592. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  593. {
  594. struct acpi_ec *ec = seq->private;
  595. if (!ec)
  596. goto end;
  597. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  598. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  599. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  600. seq_printf(seq, "use global lock:\t%s\n",
  601. ec->global_lock ? "yes" : "no");
  602. end:
  603. return 0;
  604. }
  605. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  606. {
  607. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  608. }
  609. static const struct file_operations acpi_ec_info_ops = {
  610. .open = acpi_ec_info_open_fs,
  611. .read = seq_read,
  612. .llseek = seq_lseek,
  613. .release = single_release,
  614. .owner = THIS_MODULE,
  615. };
  616. static int acpi_ec_add_fs(struct acpi_device *device)
  617. {
  618. struct proc_dir_entry *entry = NULL;
  619. if (!acpi_device_dir(device)) {
  620. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  621. acpi_ec_dir);
  622. if (!acpi_device_dir(device))
  623. return -ENODEV;
  624. }
  625. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  626. acpi_device_dir(device),
  627. &acpi_ec_info_ops, acpi_driver_data(device));
  628. if (!entry)
  629. return -ENODEV;
  630. return 0;
  631. }
  632. static int acpi_ec_remove_fs(struct acpi_device *device)
  633. {
  634. if (acpi_device_dir(device)) {
  635. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  636. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  637. acpi_device_dir(device) = NULL;
  638. }
  639. return 0;
  640. }
  641. /* --------------------------------------------------------------------------
  642. Driver Interface
  643. -------------------------------------------------------------------------- */
  644. static acpi_status
  645. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  646. static struct acpi_ec *make_acpi_ec(void)
  647. {
  648. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  649. if (!ec)
  650. return NULL;
  651. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  652. mutex_init(&ec->lock);
  653. init_waitqueue_head(&ec->wait);
  654. INIT_LIST_HEAD(&ec->list);
  655. spin_lock_init(&ec->curr_lock);
  656. return ec;
  657. }
  658. static acpi_status
  659. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  660. void *context, void **return_value)
  661. {
  662. char node_name[5];
  663. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  664. struct acpi_ec *ec = context;
  665. int value = 0;
  666. acpi_status status;
  667. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  668. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  669. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  670. }
  671. return AE_OK;
  672. }
  673. static acpi_status
  674. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  675. {
  676. acpi_status status;
  677. unsigned long long tmp = 0;
  678. struct acpi_ec *ec = context;
  679. /* clear addr values, ec_parse_io_ports depend on it */
  680. ec->command_addr = ec->data_addr = 0;
  681. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  682. ec_parse_io_ports, ec);
  683. if (ACPI_FAILURE(status))
  684. return status;
  685. /* Get GPE bit assignment (EC events). */
  686. /* TODO: Add support for _GPE returning a package */
  687. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  688. if (ACPI_FAILURE(status))
  689. return status;
  690. ec->gpe = tmp;
  691. /* Use the global lock for all EC transactions? */
  692. tmp = 0;
  693. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  694. ec->global_lock = tmp;
  695. ec->handle = handle;
  696. return AE_CTRL_TERMINATE;
  697. }
  698. static int ec_install_handlers(struct acpi_ec *ec)
  699. {
  700. acpi_status status;
  701. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  702. return 0;
  703. status = acpi_install_gpe_handler(NULL, ec->gpe,
  704. ACPI_GPE_EDGE_TRIGGERED,
  705. &acpi_ec_gpe_handler, ec);
  706. if (ACPI_FAILURE(status))
  707. return -ENODEV;
  708. acpi_enable_gpe(NULL, ec->gpe);
  709. status = acpi_install_address_space_handler(ec->handle,
  710. ACPI_ADR_SPACE_EC,
  711. &acpi_ec_space_handler,
  712. NULL, ec);
  713. if (ACPI_FAILURE(status)) {
  714. if (status == AE_NOT_FOUND) {
  715. /*
  716. * Maybe OS fails in evaluating the _REG object.
  717. * The AE_NOT_FOUND error will be ignored and OS
  718. * continue to initialize EC.
  719. */
  720. printk(KERN_ERR "Fail in evaluating the _REG object"
  721. " of EC device. Broken bios is suspected.\n");
  722. } else {
  723. acpi_remove_gpe_handler(NULL, ec->gpe,
  724. &acpi_ec_gpe_handler);
  725. acpi_disable_gpe(NULL, ec->gpe);
  726. return -ENODEV;
  727. }
  728. }
  729. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  730. return 0;
  731. }
  732. static void ec_remove_handlers(struct acpi_ec *ec)
  733. {
  734. acpi_disable_gpe(NULL, ec->gpe);
  735. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  736. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  737. pr_err(PREFIX "failed to remove space handler\n");
  738. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  739. &acpi_ec_gpe_handler)))
  740. pr_err(PREFIX "failed to remove gpe handler\n");
  741. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  742. }
  743. static int acpi_ec_add(struct acpi_device *device)
  744. {
  745. struct acpi_ec *ec = NULL;
  746. int ret;
  747. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  748. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  749. /* Check for boot EC */
  750. if (boot_ec &&
  751. (boot_ec->handle == device->handle ||
  752. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  753. ec = boot_ec;
  754. boot_ec = NULL;
  755. } else {
  756. ec = make_acpi_ec();
  757. if (!ec)
  758. return -ENOMEM;
  759. }
  760. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  761. AE_CTRL_TERMINATE) {
  762. kfree(ec);
  763. return -EINVAL;
  764. }
  765. ec->handle = device->handle;
  766. /* Find and register all query methods */
  767. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  768. acpi_ec_register_query_methods, NULL, ec, NULL);
  769. if (!first_ec)
  770. first_ec = ec;
  771. device->driver_data = ec;
  772. acpi_ec_add_fs(device);
  773. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  774. ec->gpe, ec->command_addr, ec->data_addr);
  775. ret = ec_install_handlers(ec);
  776. /* EC is fully operational, allow queries */
  777. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  778. return ret;
  779. }
  780. static int acpi_ec_remove(struct acpi_device *device, int type)
  781. {
  782. struct acpi_ec *ec;
  783. struct acpi_ec_query_handler *handler, *tmp;
  784. if (!device)
  785. return -EINVAL;
  786. ec = acpi_driver_data(device);
  787. ec_remove_handlers(ec);
  788. mutex_lock(&ec->lock);
  789. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  790. list_del(&handler->node);
  791. kfree(handler);
  792. }
  793. mutex_unlock(&ec->lock);
  794. acpi_ec_remove_fs(device);
  795. device->driver_data = NULL;
  796. if (ec == first_ec)
  797. first_ec = NULL;
  798. kfree(ec);
  799. return 0;
  800. }
  801. static acpi_status
  802. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  803. {
  804. struct acpi_ec *ec = context;
  805. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  806. return AE_OK;
  807. /*
  808. * The first address region returned is the data port, and
  809. * the second address region returned is the status/command
  810. * port.
  811. */
  812. if (ec->data_addr == 0)
  813. ec->data_addr = resource->data.io.minimum;
  814. else if (ec->command_addr == 0)
  815. ec->command_addr = resource->data.io.minimum;
  816. else
  817. return AE_CTRL_TERMINATE;
  818. return AE_OK;
  819. }
  820. int __init acpi_boot_ec_enable(void)
  821. {
  822. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  823. return 0;
  824. if (!ec_install_handlers(boot_ec)) {
  825. first_ec = boot_ec;
  826. return 0;
  827. }
  828. return -EFAULT;
  829. }
  830. static const struct acpi_device_id ec_device_ids[] = {
  831. {"PNP0C09", 0},
  832. {"", 0},
  833. };
  834. /* Some BIOS do not survive early DSDT scan, skip it */
  835. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  836. {
  837. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  838. return 0;
  839. }
  840. /* ASUStek often supplies us with broken ECDT, validate it */
  841. static int ec_validate_ecdt(const struct dmi_system_id *id)
  842. {
  843. EC_FLAGS_VALIDATE_ECDT = 1;
  844. return 0;
  845. }
  846. /* MSI EC needs special treatment, enable it */
  847. static int ec_flag_msi(const struct dmi_system_id *id)
  848. {
  849. printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
  850. EC_FLAGS_MSI = 1;
  851. EC_FLAGS_VALIDATE_ECDT = 1;
  852. return 0;
  853. }
  854. static struct dmi_system_id __initdata ec_dmi_table[] = {
  855. {
  856. ec_skip_dsdt_scan, "Compal JFL92", {
  857. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  858. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  859. {
  860. ec_flag_msi, "MSI hardware", {
  861. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  862. {
  863. ec_flag_msi, "MSI hardware", {
  864. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  865. {
  866. ec_flag_msi, "MSI hardware", {
  867. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  868. {
  869. ec_validate_ecdt, "ASUS hardware", {
  870. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  871. {},
  872. };
  873. int __init acpi_ec_ecdt_probe(void)
  874. {
  875. acpi_status status;
  876. struct acpi_ec *saved_ec = NULL;
  877. struct acpi_table_ecdt *ecdt_ptr;
  878. boot_ec = make_acpi_ec();
  879. if (!boot_ec)
  880. return -ENOMEM;
  881. /*
  882. * Generate a boot ec context
  883. */
  884. dmi_check_system(ec_dmi_table);
  885. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  886. (struct acpi_table_header **)&ecdt_ptr);
  887. if (ACPI_SUCCESS(status)) {
  888. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  889. boot_ec->command_addr = ecdt_ptr->control.address;
  890. boot_ec->data_addr = ecdt_ptr->data.address;
  891. boot_ec->gpe = ecdt_ptr->gpe;
  892. boot_ec->handle = ACPI_ROOT_OBJECT;
  893. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  894. /* Don't trust ECDT, which comes from ASUSTek */
  895. if (!EC_FLAGS_VALIDATE_ECDT)
  896. goto install;
  897. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  898. if (!saved_ec)
  899. return -ENOMEM;
  900. /* fall through */
  901. }
  902. if (EC_FLAGS_SKIP_DSDT_SCAN)
  903. return -ENODEV;
  904. /* This workaround is needed only on some broken machines,
  905. * which require early EC, but fail to provide ECDT */
  906. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  907. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  908. boot_ec, NULL);
  909. /* Check that acpi_get_devices actually find something */
  910. if (ACPI_FAILURE(status) || !boot_ec->handle)
  911. goto error;
  912. if (saved_ec) {
  913. /* try to find good ECDT from ASUSTek */
  914. if (saved_ec->command_addr != boot_ec->command_addr ||
  915. saved_ec->data_addr != boot_ec->data_addr ||
  916. saved_ec->gpe != boot_ec->gpe ||
  917. saved_ec->handle != boot_ec->handle)
  918. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  919. "ECDT tables, which are very hard to workaround. "
  920. "Trying to use DSDT EC info instead. Please send "
  921. "output of acpidump to linux-acpi@vger.kernel.org\n");
  922. kfree(saved_ec);
  923. saved_ec = NULL;
  924. } else {
  925. /* We really need to limit this workaround, the only ASUS,
  926. * which needs it, has fake EC._INI method, so use it as flag.
  927. * Keep boot_ec struct as it will be needed soon.
  928. */
  929. acpi_handle dummy;
  930. if (!dmi_name_in_vendors("ASUS") ||
  931. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  932. &dummy)))
  933. return -ENODEV;
  934. }
  935. install:
  936. if (!ec_install_handlers(boot_ec)) {
  937. first_ec = boot_ec;
  938. return 0;
  939. }
  940. error:
  941. kfree(boot_ec);
  942. boot_ec = NULL;
  943. return -ENODEV;
  944. }
  945. static struct acpi_driver acpi_ec_driver = {
  946. .name = "ec",
  947. .class = ACPI_EC_CLASS,
  948. .ids = ec_device_ids,
  949. .ops = {
  950. .add = acpi_ec_add,
  951. .remove = acpi_ec_remove,
  952. },
  953. };
  954. int __init acpi_ec_init(void)
  955. {
  956. int result = 0;
  957. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  958. if (!acpi_ec_dir)
  959. return -ENODEV;
  960. /* Now register the driver for the EC */
  961. result = acpi_bus_register_driver(&acpi_ec_driver);
  962. if (result < 0) {
  963. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  964. return -ENODEV;
  965. }
  966. return result;
  967. }
  968. /* EC driver currently not unloadable */
  969. #if 0
  970. static void __exit acpi_ec_exit(void)
  971. {
  972. acpi_bus_unregister_driver(&acpi_ec_driver);
  973. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  974. return;
  975. }
  976. #endif /* 0 */