ec.c 27 KB

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