ec.c 27 KB

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