ec.c 29 KB

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