ec.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v2.1)
  3. *
  4. * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  5. * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  6. * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  7. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  8. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or (at
  15. * your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. */
  28. /* Uncomment next line to get verbose printout */
  29. /* #define DEBUG */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/delay.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/list.h>
  39. #include <linux/spinlock.h>
  40. #include <asm/io.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/acpi_drivers.h>
  43. #define ACPI_EC_CLASS "embedded_controller"
  44. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  45. #define ACPI_EC_FILE_INFO "info"
  46. #undef PREFIX
  47. #define PREFIX "ACPI: EC: "
  48. /* EC status register */
  49. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  50. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  51. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  52. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  53. /* EC commands */
  54. enum ec_command {
  55. ACPI_EC_COMMAND_READ = 0x80,
  56. ACPI_EC_COMMAND_WRITE = 0x81,
  57. ACPI_EC_BURST_ENABLE = 0x82,
  58. ACPI_EC_BURST_DISABLE = 0x83,
  59. ACPI_EC_COMMAND_QUERY = 0x84,
  60. };
  61. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  62. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  63. #define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
  64. #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
  65. per one transaction */
  66. enum {
  67. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  68. EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
  69. * for status change */
  70. EC_FLAGS_NO_GPE, /* Don't use GPE mode */
  71. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  72. EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
  73. * OpReg are installed */
  74. };
  75. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  76. /* External interfaces use first EC only, so remember */
  77. typedef int (*acpi_ec_query_func) (void *data);
  78. struct acpi_ec_query_handler {
  79. struct list_head node;
  80. acpi_ec_query_func func;
  81. acpi_handle handle;
  82. void *data;
  83. u8 query_bit;
  84. };
  85. struct transaction {
  86. const u8 *wdata;
  87. u8 *rdata;
  88. unsigned short irq_count;
  89. u8 command;
  90. u8 wi;
  91. u8 ri;
  92. u8 wlen;
  93. u8 rlen;
  94. bool done;
  95. };
  96. static struct acpi_ec {
  97. acpi_handle handle;
  98. unsigned long gpe;
  99. unsigned long command_addr;
  100. unsigned long data_addr;
  101. unsigned long global_lock;
  102. unsigned long flags;
  103. struct mutex lock;
  104. wait_queue_head_t wait;
  105. struct list_head list;
  106. struct transaction *curr;
  107. spinlock_t curr_lock;
  108. } *boot_ec, *first_ec;
  109. /*
  110. * Some Asus system have exchanged ECDT data/command IO addresses.
  111. */
  112. static int print_ecdt_error(const struct dmi_system_id *id)
  113. {
  114. printk(KERN_NOTICE PREFIX "%s detected - "
  115. "ECDT has exchanged control/data I/O address\n",
  116. id->ident);
  117. return 0;
  118. }
  119. static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
  120. {
  121. print_ecdt_error, "Asus L4R", {
  122. DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
  123. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
  124. DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
  125. {
  126. print_ecdt_error, "Asus M6R", {
  127. DMI_MATCH(DMI_BIOS_VERSION, "0207"),
  128. DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
  129. DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
  130. {},
  131. };
  132. /* --------------------------------------------------------------------------
  133. Transaction Management
  134. -------------------------------------------------------------------------- */
  135. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  136. {
  137. u8 x = inb(ec->command_addr);
  138. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  139. return x;
  140. }
  141. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  142. {
  143. u8 x = inb(ec->data_addr);
  144. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  145. return x;
  146. }
  147. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  148. {
  149. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  150. outb(command, ec->command_addr);
  151. }
  152. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  153. {
  154. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  155. outb(data, ec->data_addr);
  156. }
  157. static int ec_transaction_done(struct acpi_ec *ec)
  158. {
  159. unsigned long flags;
  160. int ret = 0;
  161. spin_lock_irqsave(&ec->curr_lock, flags);
  162. if (!ec->curr || ec->curr->done)
  163. ret = 1;
  164. spin_unlock_irqrestore(&ec->curr_lock, flags);
  165. return ret;
  166. }
  167. static void start_transaction(struct acpi_ec *ec)
  168. {
  169. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  170. ec->curr->done = false;
  171. acpi_ec_write_cmd(ec, ec->curr->command);
  172. }
  173. static void gpe_transaction(struct acpi_ec *ec, u8 status)
  174. {
  175. unsigned long flags;
  176. spin_lock_irqsave(&ec->curr_lock, flags);
  177. if (!ec->curr)
  178. goto unlock;
  179. if (ec->curr->wlen > ec->curr->wi) {
  180. if ((status & ACPI_EC_FLAG_IBF) == 0)
  181. acpi_ec_write_data(ec,
  182. ec->curr->wdata[ec->curr->wi++]);
  183. else
  184. goto err;
  185. } else if (ec->curr->rlen > ec->curr->ri) {
  186. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  187. ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
  188. if (ec->curr->rlen == ec->curr->ri)
  189. ec->curr->done = true;
  190. } else
  191. goto err;
  192. } else if (ec->curr->wlen == ec->curr->wi &&
  193. (status & ACPI_EC_FLAG_IBF) == 0)
  194. ec->curr->done = true;
  195. goto unlock;
  196. err:
  197. /* false interrupt, state didn't change */
  198. if (in_interrupt())
  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. char node_name[5];
  661. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  662. struct acpi_ec *ec = context;
  663. int value = 0;
  664. acpi_status status;
  665. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  666. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  667. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  668. }
  669. return AE_OK;
  670. }
  671. static acpi_status
  672. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  673. {
  674. acpi_status status;
  675. unsigned long long tmp = 0;
  676. struct acpi_ec *ec = context;
  677. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  678. ec_parse_io_ports, ec);
  679. if (ACPI_FAILURE(status))
  680. return status;
  681. /* Get GPE bit assignment (EC events). */
  682. /* TODO: Add support for _GPE returning a package */
  683. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  684. if (ACPI_FAILURE(status))
  685. return status;
  686. ec->gpe = tmp;
  687. /* Use the global lock for all EC transactions? */
  688. tmp = 0;
  689. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  690. ec->global_lock = tmp;
  691. ec->handle = handle;
  692. return AE_CTRL_TERMINATE;
  693. }
  694. static void ec_remove_handlers(struct acpi_ec *ec)
  695. {
  696. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  697. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  698. pr_err(PREFIX "failed to remove space handler\n");
  699. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  700. &acpi_ec_gpe_handler)))
  701. pr_err(PREFIX "failed to remove gpe handler\n");
  702. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  703. }
  704. static int acpi_ec_add(struct acpi_device *device)
  705. {
  706. struct acpi_ec *ec = NULL;
  707. if (!device)
  708. return -EINVAL;
  709. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  710. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  711. /* Check for boot EC */
  712. if (boot_ec &&
  713. (boot_ec->handle == device->handle ||
  714. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  715. ec = boot_ec;
  716. boot_ec = NULL;
  717. } else {
  718. ec = make_acpi_ec();
  719. if (!ec)
  720. return -ENOMEM;
  721. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  722. AE_CTRL_TERMINATE) {
  723. kfree(ec);
  724. return -EINVAL;
  725. }
  726. }
  727. ec->handle = device->handle;
  728. /* Find and register all query methods */
  729. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  730. acpi_ec_register_query_methods, ec, NULL);
  731. if (!first_ec)
  732. first_ec = ec;
  733. device->driver_data = ec;
  734. acpi_ec_add_fs(device);
  735. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  736. ec->gpe, ec->command_addr, ec->data_addr);
  737. pr_info(PREFIX "driver started in %s mode\n",
  738. (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
  739. return 0;
  740. }
  741. static int acpi_ec_remove(struct acpi_device *device, int type)
  742. {
  743. struct acpi_ec *ec;
  744. struct acpi_ec_query_handler *handler, *tmp;
  745. if (!device)
  746. return -EINVAL;
  747. ec = acpi_driver_data(device);
  748. mutex_lock(&ec->lock);
  749. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  750. list_del(&handler->node);
  751. kfree(handler);
  752. }
  753. mutex_unlock(&ec->lock);
  754. acpi_ec_remove_fs(device);
  755. device->driver_data = NULL;
  756. if (ec == first_ec)
  757. first_ec = NULL;
  758. kfree(ec);
  759. return 0;
  760. }
  761. static acpi_status
  762. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  763. {
  764. struct acpi_ec *ec = context;
  765. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  766. return AE_OK;
  767. /*
  768. * The first address region returned is the data port, and
  769. * the second address region returned is the status/command
  770. * port.
  771. */
  772. if (ec->data_addr == 0)
  773. ec->data_addr = resource->data.io.minimum;
  774. else if (ec->command_addr == 0)
  775. ec->command_addr = resource->data.io.minimum;
  776. else
  777. return AE_CTRL_TERMINATE;
  778. return AE_OK;
  779. }
  780. static int ec_install_handlers(struct acpi_ec *ec)
  781. {
  782. acpi_status status;
  783. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  784. return 0;
  785. status = acpi_install_gpe_handler(NULL, ec->gpe,
  786. ACPI_GPE_EDGE_TRIGGERED,
  787. &acpi_ec_gpe_handler, ec);
  788. if (ACPI_FAILURE(status))
  789. return -ENODEV;
  790. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  791. acpi_enable_gpe(NULL, ec->gpe);
  792. status = acpi_install_address_space_handler(ec->handle,
  793. ACPI_ADR_SPACE_EC,
  794. &acpi_ec_space_handler,
  795. NULL, ec);
  796. if (ACPI_FAILURE(status)) {
  797. if (status == AE_NOT_FOUND) {
  798. /*
  799. * Maybe OS fails in evaluating the _REG object.
  800. * The AE_NOT_FOUND error will be ignored and OS
  801. * continue to initialize EC.
  802. */
  803. printk(KERN_ERR "Fail in evaluating the _REG object"
  804. " of EC device. Broken bios is suspected.\n");
  805. } else {
  806. acpi_remove_gpe_handler(NULL, ec->gpe,
  807. &acpi_ec_gpe_handler);
  808. return -ENODEV;
  809. }
  810. }
  811. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  812. return 0;
  813. }
  814. static int acpi_ec_start(struct acpi_device *device)
  815. {
  816. struct acpi_ec *ec;
  817. int ret = 0;
  818. if (!device)
  819. return -EINVAL;
  820. ec = acpi_driver_data(device);
  821. if (!ec)
  822. return -EINVAL;
  823. ret = ec_install_handlers(ec);
  824. /* EC is fully operational, allow queries */
  825. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  826. return ret;
  827. }
  828. static int acpi_ec_stop(struct acpi_device *device, int type)
  829. {
  830. struct acpi_ec *ec;
  831. if (!device)
  832. return -EINVAL;
  833. ec = acpi_driver_data(device);
  834. if (!ec)
  835. return -EINVAL;
  836. ec_remove_handlers(ec);
  837. return 0;
  838. }
  839. int __init acpi_boot_ec_enable(void)
  840. {
  841. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  842. return 0;
  843. if (!ec_install_handlers(boot_ec)) {
  844. first_ec = boot_ec;
  845. return 0;
  846. }
  847. return -EFAULT;
  848. }
  849. static const struct acpi_device_id ec_device_ids[] = {
  850. {"PNP0C09", 0},
  851. {"", 0},
  852. };
  853. int __init acpi_ec_ecdt_probe(void)
  854. {
  855. acpi_status status;
  856. struct acpi_table_ecdt *ecdt_ptr;
  857. acpi_handle dummy;
  858. boot_ec = make_acpi_ec();
  859. if (!boot_ec)
  860. return -ENOMEM;
  861. /*
  862. * Generate a boot ec context
  863. */
  864. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  865. (struct acpi_table_header **)&ecdt_ptr);
  866. if (ACPI_SUCCESS(status)) {
  867. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  868. boot_ec->command_addr = ecdt_ptr->control.address;
  869. boot_ec->data_addr = ecdt_ptr->data.address;
  870. if (dmi_check_system(ec_dmi_table)) {
  871. /*
  872. * If the board falls into ec_dmi_table, it means
  873. * that ECDT table gives the incorrect command/status
  874. * & data I/O address. Just fix it.
  875. */
  876. boot_ec->data_addr = ecdt_ptr->control.address;
  877. boot_ec->command_addr = ecdt_ptr->data.address;
  878. }
  879. boot_ec->gpe = ecdt_ptr->gpe;
  880. boot_ec->handle = ACPI_ROOT_OBJECT;
  881. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  882. /* Add some basic check against completely broken table */
  883. if (boot_ec->data_addr != boot_ec->command_addr)
  884. goto install;
  885. /* fall through */
  886. }
  887. /* This workaround is needed only on some broken machines,
  888. * which require early EC, but fail to provide ECDT */
  889. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  890. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  891. boot_ec, NULL);
  892. /* Check that acpi_get_devices actually find something */
  893. if (ACPI_FAILURE(status) || !boot_ec->handle)
  894. goto error;
  895. /* We really need to limit this workaround, the only ASUS,
  896. * which needs it, has fake EC._INI method, so use it as flag.
  897. * Keep boot_ec struct as it will be needed soon.
  898. */
  899. if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &dummy)))
  900. return -ENODEV;
  901. install:
  902. if (!ec_install_handlers(boot_ec)) {
  903. first_ec = boot_ec;
  904. return 0;
  905. }
  906. error:
  907. kfree(boot_ec);
  908. boot_ec = NULL;
  909. return -ENODEV;
  910. }
  911. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  912. {
  913. struct acpi_ec *ec = acpi_driver_data(device);
  914. /* Stop using GPE */
  915. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  916. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  917. acpi_disable_gpe(NULL, ec->gpe);
  918. return 0;
  919. }
  920. static int acpi_ec_resume(struct acpi_device *device)
  921. {
  922. struct acpi_ec *ec = acpi_driver_data(device);
  923. /* Enable use of GPE back */
  924. clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
  925. acpi_enable_gpe(NULL, ec->gpe);
  926. return 0;
  927. }
  928. static struct acpi_driver acpi_ec_driver = {
  929. .name = "ec",
  930. .class = ACPI_EC_CLASS,
  931. .ids = ec_device_ids,
  932. .ops = {
  933. .add = acpi_ec_add,
  934. .remove = acpi_ec_remove,
  935. .start = acpi_ec_start,
  936. .stop = acpi_ec_stop,
  937. .suspend = acpi_ec_suspend,
  938. .resume = acpi_ec_resume,
  939. },
  940. };
  941. static int __init acpi_ec_init(void)
  942. {
  943. int result = 0;
  944. if (acpi_disabled)
  945. return 0;
  946. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  947. if (!acpi_ec_dir)
  948. return -ENODEV;
  949. /* Now register the driver for the EC */
  950. result = acpi_bus_register_driver(&acpi_ec_driver);
  951. if (result < 0) {
  952. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  953. return -ENODEV;
  954. }
  955. return result;
  956. }
  957. subsys_initcall(acpi_ec_init);
  958. /* EC driver currently not unloadable */
  959. #if 0
  960. static void __exit acpi_ec_exit(void)
  961. {
  962. acpi_bus_unregister_driver(&acpi_ec_driver);
  963. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  964. return;
  965. }
  966. #endif /* 0 */