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