ec.c 28 KB

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