ec.c 27 KB

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