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