ec.c 28 KB

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