ec.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. #define ACPI_EC_CLASS "embedded_controller"
  44. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  45. #define ACPI_EC_FILE_INFO "info"
  46. #undef PREFIX
  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_STORM_THRESHOLD 8 /* number of false interrupts
  65. per one transaction */
  66. enum {
  67. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  68. EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
  69. * for status change */
  70. EC_FLAGS_NO_GPE, /* Don't use GPE mode */
  71. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  72. EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
  73. * OpReg are installed */
  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. /* --------------------------------------------------------------------------
  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 gpe_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_wait(struct acpi_ec *ec)
  182. {
  183. if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
  184. msecs_to_jiffies(ACPI_EC_DELAY)))
  185. return 0;
  186. /* try restart command if we get any false interrupts */
  187. if (ec->curr->irq_count &&
  188. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  189. pr_debug(PREFIX "controller reset, restart transaction\n");
  190. start_transaction(ec);
  191. if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
  192. msecs_to_jiffies(ACPI_EC_DELAY)))
  193. return 0;
  194. }
  195. /* missing GPEs, switch back to poll mode */
  196. if (printk_ratelimit())
  197. pr_info(PREFIX "missing confirmations, "
  198. "switch off interrupt mode.\n");
  199. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  200. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  201. return 1;
  202. }
  203. static void acpi_ec_gpe_query(void *ec_cxt);
  204. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  205. {
  206. if (state & ACPI_EC_FLAG_SCI) {
  207. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  208. return acpi_os_execute(OSL_EC_BURST_HANDLER,
  209. acpi_ec_gpe_query, ec);
  210. }
  211. return 0;
  212. }
  213. static void ec_delay(void)
  214. {
  215. /* EC in MSI notebooks don't tolerate delays other than 550 usec */
  216. if (EC_FLAGS_MSI)
  217. udelay(ACPI_EC_DELAY);
  218. else
  219. /* Use shortest sleep available */
  220. msleep(1);
  221. }
  222. static int ec_poll(struct acpi_ec *ec)
  223. {
  224. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  225. udelay(ACPI_EC_CDELAY);
  226. while (time_before(jiffies, delay)) {
  227. gpe_transaction(ec, acpi_ec_read_status(ec));
  228. ec_delay();
  229. if (ec_transaction_done(ec))
  230. return 0;
  231. }
  232. return -ETIME;
  233. }
  234. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  235. struct transaction *t,
  236. int force_poll)
  237. {
  238. unsigned long tmp;
  239. int ret = 0;
  240. pr_debug(PREFIX "transaction start\n");
  241. /* disable GPE during transaction if storm is detected */
  242. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  243. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  244. acpi_disable_gpe(NULL, ec->gpe);
  245. }
  246. if (EC_FLAGS_MSI)
  247. udelay(ACPI_EC_DELAY);
  248. /* start transaction */
  249. spin_lock_irqsave(&ec->curr_lock, tmp);
  250. /* following two actions should be kept atomic */
  251. ec->curr = t;
  252. start_transaction(ec);
  253. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  254. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  255. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  256. /* if we selected poll mode or failed in GPE-mode do a poll loop */
  257. if (force_poll ||
  258. !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
  259. acpi_ec_wait(ec))
  260. ret = ec_poll(ec);
  261. pr_debug(PREFIX "transaction end\n");
  262. spin_lock_irqsave(&ec->curr_lock, tmp);
  263. ec->curr = NULL;
  264. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  265. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  266. /* check if we received SCI during transaction */
  267. ec_check_sci(ec, acpi_ec_read_status(ec));
  268. /* it is safe to enable GPE outside of transaction */
  269. acpi_enable_gpe(NULL, ec->gpe);
  270. } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
  271. t->irq_count > ACPI_EC_STORM_THRESHOLD) {
  272. pr_info(PREFIX "GPE storm detected, "
  273. "transactions will use polling mode\n");
  274. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  275. }
  276. return ret;
  277. }
  278. static int ec_check_ibf0(struct acpi_ec *ec)
  279. {
  280. u8 status = acpi_ec_read_status(ec);
  281. return (status & ACPI_EC_FLAG_IBF) == 0;
  282. }
  283. static int ec_wait_ibf0(struct acpi_ec *ec)
  284. {
  285. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  286. /* interrupt wait manually if GPE mode is not active */
  287. unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
  288. msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
  289. while (time_before(jiffies, delay))
  290. if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
  291. return 0;
  292. return -ETIME;
  293. }
  294. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t,
  295. int force_poll)
  296. {
  297. int status;
  298. u32 glk;
  299. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  300. return -EINVAL;
  301. if (t->rdata)
  302. memset(t->rdata, 0, t->rlen);
  303. mutex_lock(&ec->lock);
  304. if (ec->global_lock) {
  305. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  306. if (ACPI_FAILURE(status)) {
  307. status = -ENODEV;
  308. goto unlock;
  309. }
  310. }
  311. if (ec_wait_ibf0(ec)) {
  312. pr_err(PREFIX "input buffer is not empty, "
  313. "aborting transaction\n");
  314. status = -ETIME;
  315. goto end;
  316. }
  317. status = acpi_ec_transaction_unlocked(ec, t, force_poll);
  318. end:
  319. if (ec->global_lock)
  320. acpi_release_global_lock(glk);
  321. unlock:
  322. mutex_unlock(&ec->lock);
  323. return status;
  324. }
  325. /*
  326. * Note: samsung nv5000 doesn't work with ec burst mode.
  327. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  328. */
  329. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  330. {
  331. u8 d;
  332. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  333. .wdata = NULL, .rdata = &d,
  334. .wlen = 0, .rlen = 1};
  335. return acpi_ec_transaction(ec, &t, 0);
  336. }
  337. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  338. {
  339. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  340. .wdata = NULL, .rdata = NULL,
  341. .wlen = 0, .rlen = 0};
  342. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  343. acpi_ec_transaction(ec, &t, 0) : 0;
  344. }
  345. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  346. {
  347. int result;
  348. u8 d;
  349. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  350. .wdata = &address, .rdata = &d,
  351. .wlen = 1, .rlen = 1};
  352. result = acpi_ec_transaction(ec, &t, 0);
  353. *data = d;
  354. return result;
  355. }
  356. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  357. {
  358. u8 wdata[2] = { address, data };
  359. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  360. .wdata = wdata, .rdata = NULL,
  361. .wlen = 2, .rlen = 0};
  362. return acpi_ec_transaction(ec, &t, 0);
  363. }
  364. /*
  365. * Externally callable EC access functions. For now, assume 1 EC only
  366. */
  367. int ec_burst_enable(void)
  368. {
  369. if (!first_ec)
  370. return -ENODEV;
  371. return acpi_ec_burst_enable(first_ec);
  372. }
  373. EXPORT_SYMBOL(ec_burst_enable);
  374. int ec_burst_disable(void)
  375. {
  376. if (!first_ec)
  377. return -ENODEV;
  378. return acpi_ec_burst_disable(first_ec);
  379. }
  380. EXPORT_SYMBOL(ec_burst_disable);
  381. int ec_read(u8 addr, u8 * val)
  382. {
  383. int err;
  384. u8 temp_data;
  385. if (!first_ec)
  386. return -ENODEV;
  387. err = acpi_ec_read(first_ec, addr, &temp_data);
  388. if (!err) {
  389. *val = temp_data;
  390. return 0;
  391. } else
  392. return err;
  393. }
  394. EXPORT_SYMBOL(ec_read);
  395. int ec_write(u8 addr, u8 val)
  396. {
  397. int err;
  398. if (!first_ec)
  399. return -ENODEV;
  400. err = acpi_ec_write(first_ec, addr, val);
  401. return err;
  402. }
  403. EXPORT_SYMBOL(ec_write);
  404. int ec_transaction(u8 command,
  405. const u8 * wdata, unsigned wdata_len,
  406. u8 * rdata, unsigned rdata_len,
  407. int force_poll)
  408. {
  409. struct transaction t = {.command = command,
  410. .wdata = wdata, .rdata = rdata,
  411. .wlen = wdata_len, .rlen = rdata_len};
  412. if (!first_ec)
  413. return -ENODEV;
  414. return acpi_ec_transaction(first_ec, &t, force_poll);
  415. }
  416. EXPORT_SYMBOL(ec_transaction);
  417. static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
  418. {
  419. int result;
  420. u8 d;
  421. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  422. .wdata = NULL, .rdata = &d,
  423. .wlen = 0, .rlen = 1};
  424. if (!ec || !data)
  425. return -EINVAL;
  426. /*
  427. * Query the EC to find out which _Qxx method we need to evaluate.
  428. * Note that successful completion of the query causes the ACPI_EC_SCI
  429. * bit to be cleared (and thus clearing the interrupt source).
  430. */
  431. result = acpi_ec_transaction(ec, &t, 0);
  432. if (result)
  433. return result;
  434. if (!d)
  435. return -ENODATA;
  436. *data = d;
  437. return 0;
  438. }
  439. /* --------------------------------------------------------------------------
  440. Event Management
  441. -------------------------------------------------------------------------- */
  442. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  443. acpi_handle handle, acpi_ec_query_func func,
  444. void *data)
  445. {
  446. struct acpi_ec_query_handler *handler =
  447. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  448. if (!handler)
  449. return -ENOMEM;
  450. handler->query_bit = query_bit;
  451. handler->handle = handle;
  452. handler->func = func;
  453. handler->data = data;
  454. mutex_lock(&ec->lock);
  455. list_add(&handler->node, &ec->list);
  456. mutex_unlock(&ec->lock);
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  460. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  461. {
  462. struct acpi_ec_query_handler *handler, *tmp;
  463. mutex_lock(&ec->lock);
  464. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  465. if (query_bit == handler->query_bit) {
  466. list_del(&handler->node);
  467. kfree(handler);
  468. }
  469. }
  470. mutex_unlock(&ec->lock);
  471. }
  472. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  473. static void acpi_ec_gpe_query(void *ec_cxt)
  474. {
  475. struct acpi_ec *ec = ec_cxt;
  476. u8 value = 0;
  477. struct acpi_ec_query_handler *handler, copy;
  478. if (!ec || acpi_ec_query(ec, &value))
  479. return;
  480. mutex_lock(&ec->lock);
  481. list_for_each_entry(handler, &ec->list, node) {
  482. if (value == handler->query_bit) {
  483. /* have custom handler for this bit */
  484. memcpy(&copy, handler, sizeof(copy));
  485. mutex_unlock(&ec->lock);
  486. if (copy.func) {
  487. copy.func(copy.data);
  488. } else if (copy.handle) {
  489. acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
  490. }
  491. return;
  492. }
  493. }
  494. mutex_unlock(&ec->lock);
  495. }
  496. static u32 acpi_ec_gpe_handler(void *data)
  497. {
  498. struct acpi_ec *ec = data;
  499. u8 status;
  500. pr_debug(PREFIX "~~~> interrupt\n");
  501. status = acpi_ec_read_status(ec);
  502. if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
  503. gpe_transaction(ec, status);
  504. if (ec_transaction_done(ec) &&
  505. (status & ACPI_EC_FLAG_IBF) == 0)
  506. wake_up(&ec->wait);
  507. }
  508. ec_check_sci(ec, status);
  509. if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
  510. !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
  511. /* this is non-query, must be confirmation */
  512. if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  513. if (printk_ratelimit())
  514. pr_info(PREFIX "non-query interrupt received,"
  515. " switching to interrupt mode\n");
  516. } else {
  517. /* hush, STORM switches the mode every transaction */
  518. pr_debug(PREFIX "non-query interrupt received,"
  519. " switching to interrupt mode\n");
  520. }
  521. set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  522. }
  523. return ACPI_INTERRUPT_HANDLED;
  524. }
  525. /* --------------------------------------------------------------------------
  526. Address Space Management
  527. -------------------------------------------------------------------------- */
  528. static acpi_status
  529. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  530. u32 bits, acpi_integer *value,
  531. void *handler_context, void *region_context)
  532. {
  533. struct acpi_ec *ec = handler_context;
  534. int result = 0, i;
  535. u8 temp = 0;
  536. if ((address > 0xFF) || !value || !handler_context)
  537. return AE_BAD_PARAMETER;
  538. if (function != ACPI_READ && function != ACPI_WRITE)
  539. return AE_BAD_PARAMETER;
  540. if (bits != 8 && acpi_strict)
  541. return AE_BAD_PARAMETER;
  542. acpi_ec_burst_enable(ec);
  543. if (function == ACPI_READ) {
  544. result = acpi_ec_read(ec, address, &temp);
  545. *value = temp;
  546. } else {
  547. temp = 0xff & (*value);
  548. result = acpi_ec_write(ec, address, temp);
  549. }
  550. for (i = 8; unlikely(bits - i > 0); i += 8) {
  551. ++address;
  552. if (function == ACPI_READ) {
  553. result = acpi_ec_read(ec, address, &temp);
  554. (*value) |= ((acpi_integer)temp) << i;
  555. } else {
  556. temp = 0xff & ((*value) >> i);
  557. result = acpi_ec_write(ec, address, temp);
  558. }
  559. }
  560. acpi_ec_burst_disable(ec);
  561. switch (result) {
  562. case -EINVAL:
  563. return AE_BAD_PARAMETER;
  564. break;
  565. case -ENODEV:
  566. return AE_NOT_FOUND;
  567. break;
  568. case -ETIME:
  569. return AE_TIME;
  570. break;
  571. default:
  572. return AE_OK;
  573. }
  574. }
  575. /* --------------------------------------------------------------------------
  576. FS Interface (/proc)
  577. -------------------------------------------------------------------------- */
  578. static struct proc_dir_entry *acpi_ec_dir;
  579. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  580. {
  581. struct acpi_ec *ec = seq->private;
  582. if (!ec)
  583. goto end;
  584. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  585. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  586. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  587. seq_printf(seq, "use global lock:\t%s\n",
  588. ec->global_lock ? "yes" : "no");
  589. end:
  590. return 0;
  591. }
  592. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  593. {
  594. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  595. }
  596. static const struct file_operations acpi_ec_info_ops = {
  597. .open = acpi_ec_info_open_fs,
  598. .read = seq_read,
  599. .llseek = seq_lseek,
  600. .release = single_release,
  601. .owner = THIS_MODULE,
  602. };
  603. static int acpi_ec_add_fs(struct acpi_device *device)
  604. {
  605. struct proc_dir_entry *entry = NULL;
  606. if (!acpi_device_dir(device)) {
  607. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  608. acpi_ec_dir);
  609. if (!acpi_device_dir(device))
  610. return -ENODEV;
  611. }
  612. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  613. acpi_device_dir(device),
  614. &acpi_ec_info_ops, acpi_driver_data(device));
  615. if (!entry)
  616. return -ENODEV;
  617. return 0;
  618. }
  619. static int acpi_ec_remove_fs(struct acpi_device *device)
  620. {
  621. if (acpi_device_dir(device)) {
  622. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  623. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  624. acpi_device_dir(device) = NULL;
  625. }
  626. return 0;
  627. }
  628. /* --------------------------------------------------------------------------
  629. Driver Interface
  630. -------------------------------------------------------------------------- */
  631. static acpi_status
  632. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  633. static struct acpi_ec *make_acpi_ec(void)
  634. {
  635. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  636. if (!ec)
  637. return NULL;
  638. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  639. mutex_init(&ec->lock);
  640. init_waitqueue_head(&ec->wait);
  641. INIT_LIST_HEAD(&ec->list);
  642. spin_lock_init(&ec->curr_lock);
  643. return ec;
  644. }
  645. static acpi_status
  646. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  647. void *context, void **return_value)
  648. {
  649. char node_name[5];
  650. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  651. struct acpi_ec *ec = context;
  652. int value = 0;
  653. acpi_status status;
  654. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  655. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  656. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  657. }
  658. return AE_OK;
  659. }
  660. static acpi_status
  661. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  662. {
  663. acpi_status status;
  664. unsigned long long tmp = 0;
  665. struct acpi_ec *ec = context;
  666. /* clear addr values, ec_parse_io_ports depend on it */
  667. ec->command_addr = ec->data_addr = 0;
  668. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  669. ec_parse_io_ports, ec);
  670. if (ACPI_FAILURE(status))
  671. return status;
  672. /* Get GPE bit assignment (EC events). */
  673. /* TODO: Add support for _GPE returning a package */
  674. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  675. if (ACPI_FAILURE(status))
  676. return status;
  677. ec->gpe = tmp;
  678. /* Use the global lock for all EC transactions? */
  679. tmp = 0;
  680. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  681. ec->global_lock = tmp;
  682. ec->handle = handle;
  683. return AE_CTRL_TERMINATE;
  684. }
  685. static int ec_install_handlers(struct acpi_ec *ec)
  686. {
  687. acpi_status status;
  688. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  689. return 0;
  690. status = acpi_install_gpe_handler(NULL, ec->gpe,
  691. ACPI_GPE_EDGE_TRIGGERED,
  692. &acpi_ec_gpe_handler, ec);
  693. if (ACPI_FAILURE(status))
  694. return -ENODEV;
  695. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  696. acpi_enable_gpe(NULL, ec->gpe);
  697. status = acpi_install_address_space_handler(ec->handle,
  698. ACPI_ADR_SPACE_EC,
  699. &acpi_ec_space_handler,
  700. NULL, ec);
  701. if (ACPI_FAILURE(status)) {
  702. if (status == AE_NOT_FOUND) {
  703. /*
  704. * Maybe OS fails in evaluating the _REG object.
  705. * The AE_NOT_FOUND error will be ignored and OS
  706. * continue to initialize EC.
  707. */
  708. printk(KERN_ERR "Fail in evaluating the _REG object"
  709. " of EC device. Broken bios is suspected.\n");
  710. } else {
  711. acpi_remove_gpe_handler(NULL, ec->gpe,
  712. &acpi_ec_gpe_handler);
  713. return -ENODEV;
  714. }
  715. }
  716. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  717. return 0;
  718. }
  719. static void ec_remove_handlers(struct acpi_ec *ec)
  720. {
  721. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  722. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  723. pr_err(PREFIX "failed to remove space handler\n");
  724. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  725. &acpi_ec_gpe_handler)))
  726. pr_err(PREFIX "failed to remove gpe handler\n");
  727. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  728. }
  729. static int acpi_ec_add(struct acpi_device *device)
  730. {
  731. struct acpi_ec *ec = NULL;
  732. int ret;
  733. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  734. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  735. /* Check for boot EC */
  736. if (boot_ec &&
  737. (boot_ec->handle == device->handle ||
  738. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  739. ec = boot_ec;
  740. boot_ec = NULL;
  741. } else {
  742. ec = make_acpi_ec();
  743. if (!ec)
  744. return -ENOMEM;
  745. }
  746. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  747. AE_CTRL_TERMINATE) {
  748. kfree(ec);
  749. return -EINVAL;
  750. }
  751. ec->handle = device->handle;
  752. /* Find and register all query methods */
  753. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  754. acpi_ec_register_query_methods, ec, NULL);
  755. if (!first_ec)
  756. first_ec = ec;
  757. device->driver_data = ec;
  758. acpi_ec_add_fs(device);
  759. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  760. ec->gpe, ec->command_addr, ec->data_addr);
  761. pr_info(PREFIX "driver started in %s mode\n",
  762. (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
  763. ret = ec_install_handlers(ec);
  764. /* EC is fully operational, allow queries */
  765. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  766. return ret;
  767. }
  768. static int acpi_ec_remove(struct acpi_device *device, int type)
  769. {
  770. struct acpi_ec *ec;
  771. struct acpi_ec_query_handler *handler, *tmp;
  772. if (!device)
  773. return -EINVAL;
  774. ec = acpi_driver_data(device);
  775. mutex_lock(&ec->lock);
  776. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  777. list_del(&handler->node);
  778. kfree(handler);
  779. }
  780. mutex_unlock(&ec->lock);
  781. acpi_ec_remove_fs(device);
  782. device->driver_data = NULL;
  783. if (ec == first_ec)
  784. first_ec = NULL;
  785. kfree(ec);
  786. return 0;
  787. }
  788. static acpi_status
  789. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  790. {
  791. struct acpi_ec *ec = context;
  792. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  793. return AE_OK;
  794. /*
  795. * The first address region returned is the data port, and
  796. * the second address region returned is the status/command
  797. * port.
  798. */
  799. if (ec->data_addr == 0)
  800. ec->data_addr = resource->data.io.minimum;
  801. else if (ec->command_addr == 0)
  802. ec->command_addr = resource->data.io.minimum;
  803. else
  804. return AE_CTRL_TERMINATE;
  805. return AE_OK;
  806. }
  807. static int acpi_ec_stop(struct acpi_device *device, int type)
  808. {
  809. struct acpi_ec *ec;
  810. if (!device)
  811. return -EINVAL;
  812. ec = acpi_driver_data(device);
  813. if (!ec)
  814. return -EINVAL;
  815. ec_remove_handlers(ec);
  816. return 0;
  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. int __init acpi_ec_ecdt_probe(void)
  833. {
  834. acpi_status status;
  835. struct acpi_ec *saved_ec = NULL;
  836. struct acpi_table_ecdt *ecdt_ptr;
  837. boot_ec = make_acpi_ec();
  838. if (!boot_ec)
  839. return -ENOMEM;
  840. /*
  841. * Generate a boot ec context
  842. */
  843. if (dmi_name_in_vendors("Micro-Star") ||
  844. dmi_name_in_vendors("Notebook")) {
  845. pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
  846. EC_FLAGS_MSI = 1;
  847. }
  848. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  849. (struct acpi_table_header **)&ecdt_ptr);
  850. if (ACPI_SUCCESS(status)) {
  851. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  852. boot_ec->command_addr = ecdt_ptr->control.address;
  853. boot_ec->data_addr = ecdt_ptr->data.address;
  854. boot_ec->gpe = ecdt_ptr->gpe;
  855. boot_ec->handle = ACPI_ROOT_OBJECT;
  856. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  857. /* Don't trust ECDT, which comes from ASUSTek */
  858. if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
  859. goto install;
  860. saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  861. if (!saved_ec)
  862. return -ENOMEM;
  863. memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
  864. /* fall through */
  865. }
  866. /* This workaround is needed only on some broken machines,
  867. * which require early EC, but fail to provide ECDT */
  868. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  869. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  870. boot_ec, NULL);
  871. /* Check that acpi_get_devices actually find something */
  872. if (ACPI_FAILURE(status) || !boot_ec->handle)
  873. goto error;
  874. if (saved_ec) {
  875. /* try to find good ECDT from ASUSTek */
  876. if (saved_ec->command_addr != boot_ec->command_addr ||
  877. saved_ec->data_addr != boot_ec->data_addr ||
  878. saved_ec->gpe != boot_ec->gpe ||
  879. saved_ec->handle != boot_ec->handle)
  880. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  881. "ECDT tables, which are very hard to workaround. "
  882. "Trying to use DSDT EC info instead. Please send "
  883. "output of acpidump to linux-acpi@vger.kernel.org\n");
  884. kfree(saved_ec);
  885. saved_ec = NULL;
  886. } else {
  887. /* We really need to limit this workaround, the only ASUS,
  888. * which needs it, has fake EC._INI method, so use it as flag.
  889. * Keep boot_ec struct as it will be needed soon.
  890. */
  891. acpi_handle dummy;
  892. if (!dmi_name_in_vendors("ASUS") ||
  893. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  894. &dummy)))
  895. return -ENODEV;
  896. }
  897. install:
  898. if (!ec_install_handlers(boot_ec)) {
  899. first_ec = boot_ec;
  900. return 0;
  901. }
  902. error:
  903. kfree(boot_ec);
  904. boot_ec = NULL;
  905. return -ENODEV;
  906. }
  907. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  908. {
  909. struct acpi_ec *ec = acpi_driver_data(device);
  910. /* Stop using GPE */
  911. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  912. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  913. acpi_disable_gpe(NULL, ec->gpe);
  914. return 0;
  915. }
  916. static int acpi_ec_resume(struct acpi_device *device)
  917. {
  918. struct acpi_ec *ec = acpi_driver_data(device);
  919. /* Enable use of GPE back */
  920. clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
  921. set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  922. acpi_enable_gpe(NULL, ec->gpe);
  923. return 0;
  924. }
  925. static struct acpi_driver acpi_ec_driver = {
  926. .name = "ec",
  927. .class = ACPI_EC_CLASS,
  928. .ids = ec_device_ids,
  929. .ops = {
  930. .add = acpi_ec_add,
  931. .remove = acpi_ec_remove,
  932. .stop = acpi_ec_stop,
  933. .suspend = acpi_ec_suspend,
  934. .resume = acpi_ec_resume,
  935. },
  936. };
  937. int __init acpi_ec_init(void)
  938. {
  939. int result = 0;
  940. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  941. if (!acpi_ec_dir)
  942. return -ENODEV;
  943. /* Now register the driver for the EC */
  944. result = acpi_bus_register_driver(&acpi_ec_driver);
  945. if (result < 0) {
  946. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  947. return -ENODEV;
  948. }
  949. return result;
  950. }
  951. /* EC driver currently not unloadable */
  952. #if 0
  953. static void __exit acpi_ec_exit(void)
  954. {
  955. acpi_bus_unregister_driver(&acpi_ec_driver);
  956. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  957. return;
  958. }
  959. #endif /* 0 */