ec.c 28 KB

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