ec.c 28 KB

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