ec.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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/interrupt.h>
  36. #include <linux/list.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/slab.h>
  39. #include <asm/io.h>
  40. #include <acpi/acpi_bus.h>
  41. #include <acpi/acpi_drivers.h>
  42. #include <linux/dmi.h>
  43. #include "internal.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_CDELAY 10 /* Wait 10us before polling EC */
  65. #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
  66. #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
  67. per one transaction */
  68. enum {
  69. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  70. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  71. EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
  72. * OpReg are installed */
  73. EC_FLAGS_BLOCKED, /* Transactions are blocked */
  74. };
  75. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  76. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  77. module_param(ec_delay, uint, 0644);
  78. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  79. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  80. /* External interfaces use first EC only, so remember */
  81. typedef int (*acpi_ec_query_func) (void *data);
  82. struct acpi_ec_query_handler {
  83. struct list_head node;
  84. acpi_ec_query_func func;
  85. acpi_handle handle;
  86. void *data;
  87. u8 query_bit;
  88. };
  89. struct transaction {
  90. const u8 *wdata;
  91. u8 *rdata;
  92. unsigned short irq_count;
  93. u8 command;
  94. u8 wi;
  95. u8 ri;
  96. u8 wlen;
  97. u8 rlen;
  98. bool done;
  99. };
  100. struct acpi_ec *boot_ec, *first_ec;
  101. EXPORT_SYMBOL(first_ec);
  102. static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
  103. static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
  104. static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
  105. /* --------------------------------------------------------------------------
  106. Transaction Management
  107. -------------------------------------------------------------------------- */
  108. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  109. {
  110. u8 x = inb(ec->command_addr);
  111. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  112. return x;
  113. }
  114. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  115. {
  116. u8 x = inb(ec->data_addr);
  117. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  118. return x;
  119. }
  120. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  121. {
  122. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  123. outb(command, ec->command_addr);
  124. }
  125. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  126. {
  127. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  128. outb(data, ec->data_addr);
  129. }
  130. static int ec_transaction_done(struct acpi_ec *ec)
  131. {
  132. unsigned long flags;
  133. int ret = 0;
  134. spin_lock_irqsave(&ec->curr_lock, flags);
  135. if (!ec->curr || ec->curr->done)
  136. ret = 1;
  137. spin_unlock_irqrestore(&ec->curr_lock, flags);
  138. return ret;
  139. }
  140. static void start_transaction(struct acpi_ec *ec)
  141. {
  142. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  143. ec->curr->done = false;
  144. acpi_ec_write_cmd(ec, ec->curr->command);
  145. }
  146. static void advance_transaction(struct acpi_ec *ec, u8 status)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&ec->curr_lock, flags);
  150. if (!ec->curr)
  151. goto unlock;
  152. if (ec->curr->wlen > ec->curr->wi) {
  153. if ((status & ACPI_EC_FLAG_IBF) == 0)
  154. acpi_ec_write_data(ec,
  155. ec->curr->wdata[ec->curr->wi++]);
  156. else
  157. goto err;
  158. } else if (ec->curr->rlen > ec->curr->ri) {
  159. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  160. ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
  161. if (ec->curr->rlen == ec->curr->ri)
  162. ec->curr->done = true;
  163. } else
  164. goto err;
  165. } else if (ec->curr->wlen == ec->curr->wi &&
  166. (status & ACPI_EC_FLAG_IBF) == 0)
  167. ec->curr->done = true;
  168. goto unlock;
  169. err:
  170. /* false interrupt, state didn't change */
  171. if (in_interrupt())
  172. ++ec->curr->irq_count;
  173. unlock:
  174. spin_unlock_irqrestore(&ec->curr_lock, flags);
  175. }
  176. static int acpi_ec_sync_query(struct acpi_ec *ec);
  177. static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
  178. {
  179. if (state & ACPI_EC_FLAG_SCI) {
  180. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  181. return acpi_ec_sync_query(ec);
  182. }
  183. return 0;
  184. }
  185. static int ec_poll(struct acpi_ec *ec)
  186. {
  187. unsigned long flags;
  188. int repeat = 2; /* number of command restarts */
  189. while (repeat--) {
  190. unsigned long delay = jiffies +
  191. msecs_to_jiffies(ec_delay);
  192. do {
  193. /* don't sleep with disabled interrupts */
  194. if (EC_FLAGS_MSI || irqs_disabled()) {
  195. udelay(ACPI_EC_MSI_UDELAY);
  196. if (ec_transaction_done(ec))
  197. return 0;
  198. } else {
  199. if (wait_event_timeout(ec->wait,
  200. ec_transaction_done(ec),
  201. msecs_to_jiffies(1)))
  202. return 0;
  203. }
  204. advance_transaction(ec, acpi_ec_read_status(ec));
  205. } while (time_before(jiffies, delay));
  206. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
  207. break;
  208. pr_debug(PREFIX "controller reset, restart transaction\n");
  209. spin_lock_irqsave(&ec->curr_lock, flags);
  210. start_transaction(ec);
  211. spin_unlock_irqrestore(&ec->curr_lock, flags);
  212. }
  213. return -ETIME;
  214. }
  215. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  216. struct transaction *t)
  217. {
  218. unsigned long tmp;
  219. int ret = 0;
  220. if (EC_FLAGS_MSI)
  221. udelay(ACPI_EC_MSI_UDELAY);
  222. /* start transaction */
  223. spin_lock_irqsave(&ec->curr_lock, tmp);
  224. /* following two actions should be kept atomic */
  225. ec->curr = t;
  226. start_transaction(ec);
  227. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  228. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  229. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  230. ret = ec_poll(ec);
  231. spin_lock_irqsave(&ec->curr_lock, tmp);
  232. ec->curr = NULL;
  233. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  234. return ret;
  235. }
  236. static int ec_check_ibf0(struct acpi_ec *ec)
  237. {
  238. u8 status = acpi_ec_read_status(ec);
  239. return (status & ACPI_EC_FLAG_IBF) == 0;
  240. }
  241. static int ec_wait_ibf0(struct acpi_ec *ec)
  242. {
  243. unsigned long delay = jiffies + msecs_to_jiffies(ec_delay);
  244. /* interrupt wait manually if GPE mode is not active */
  245. while (time_before(jiffies, delay))
  246. if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
  247. msecs_to_jiffies(1)))
  248. return 0;
  249. return -ETIME;
  250. }
  251. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  252. {
  253. int status;
  254. u32 glk;
  255. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  256. return -EINVAL;
  257. if (t->rdata)
  258. memset(t->rdata, 0, t->rlen);
  259. mutex_lock(&ec->lock);
  260. if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
  261. status = -EINVAL;
  262. goto unlock;
  263. }
  264. if (ec->global_lock) {
  265. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  266. if (ACPI_FAILURE(status)) {
  267. status = -ENODEV;
  268. goto unlock;
  269. }
  270. }
  271. if (ec_wait_ibf0(ec)) {
  272. pr_err(PREFIX "input buffer is not empty, "
  273. "aborting transaction\n");
  274. status = -ETIME;
  275. goto end;
  276. }
  277. pr_debug(PREFIX "transaction start\n");
  278. /* disable GPE during transaction if storm is detected */
  279. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  280. /* It has to be disabled, so that it doesn't trigger. */
  281. acpi_disable_gpe(NULL, ec->gpe);
  282. }
  283. status = acpi_ec_transaction_unlocked(ec, t);
  284. /* check if we received SCI during transaction */
  285. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  286. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  287. msleep(1);
  288. /* It is safe to enable the GPE outside of the transaction. */
  289. acpi_enable_gpe(NULL, ec->gpe);
  290. } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
  291. pr_info(PREFIX "GPE storm detected, "
  292. "transactions will use polling mode\n");
  293. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  294. }
  295. pr_debug(PREFIX "transaction end\n");
  296. end:
  297. if (ec->global_lock)
  298. acpi_release_global_lock(glk);
  299. unlock:
  300. mutex_unlock(&ec->lock);
  301. return status;
  302. }
  303. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  304. {
  305. u8 d;
  306. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  307. .wdata = NULL, .rdata = &d,
  308. .wlen = 0, .rlen = 1};
  309. return acpi_ec_transaction(ec, &t);
  310. }
  311. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  312. {
  313. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  314. .wdata = NULL, .rdata = NULL,
  315. .wlen = 0, .rlen = 0};
  316. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  317. acpi_ec_transaction(ec, &t) : 0;
  318. }
  319. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  320. {
  321. int result;
  322. u8 d;
  323. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  324. .wdata = &address, .rdata = &d,
  325. .wlen = 1, .rlen = 1};
  326. result = acpi_ec_transaction(ec, &t);
  327. *data = d;
  328. return result;
  329. }
  330. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  331. {
  332. u8 wdata[2] = { address, data };
  333. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  334. .wdata = wdata, .rdata = NULL,
  335. .wlen = 2, .rlen = 0};
  336. return acpi_ec_transaction(ec, &t);
  337. }
  338. /*
  339. * Externally callable EC access functions. For now, assume 1 EC only
  340. */
  341. int ec_burst_enable(void)
  342. {
  343. if (!first_ec)
  344. return -ENODEV;
  345. return acpi_ec_burst_enable(first_ec);
  346. }
  347. EXPORT_SYMBOL(ec_burst_enable);
  348. int ec_burst_disable(void)
  349. {
  350. if (!first_ec)
  351. return -ENODEV;
  352. return acpi_ec_burst_disable(first_ec);
  353. }
  354. EXPORT_SYMBOL(ec_burst_disable);
  355. int ec_read(u8 addr, u8 * val)
  356. {
  357. int err;
  358. u8 temp_data;
  359. if (!first_ec)
  360. return -ENODEV;
  361. err = acpi_ec_read(first_ec, addr, &temp_data);
  362. if (!err) {
  363. *val = temp_data;
  364. return 0;
  365. } else
  366. return err;
  367. }
  368. EXPORT_SYMBOL(ec_read);
  369. int ec_write(u8 addr, u8 val)
  370. {
  371. int err;
  372. if (!first_ec)
  373. return -ENODEV;
  374. err = acpi_ec_write(first_ec, addr, val);
  375. return err;
  376. }
  377. EXPORT_SYMBOL(ec_write);
  378. int ec_transaction(u8 command,
  379. const u8 * wdata, unsigned wdata_len,
  380. u8 * rdata, unsigned rdata_len,
  381. int force_poll)
  382. {
  383. struct transaction t = {.command = command,
  384. .wdata = wdata, .rdata = rdata,
  385. .wlen = wdata_len, .rlen = rdata_len};
  386. if (!first_ec)
  387. return -ENODEV;
  388. return acpi_ec_transaction(first_ec, &t);
  389. }
  390. EXPORT_SYMBOL(ec_transaction);
  391. void acpi_ec_block_transactions(void)
  392. {
  393. struct acpi_ec *ec = first_ec;
  394. if (!ec)
  395. return;
  396. mutex_lock(&ec->lock);
  397. /* Prevent transactions from being carried out */
  398. set_bit(EC_FLAGS_BLOCKED, &ec->flags);
  399. mutex_unlock(&ec->lock);
  400. }
  401. void acpi_ec_unblock_transactions(void)
  402. {
  403. struct acpi_ec *ec = first_ec;
  404. if (!ec)
  405. return;
  406. mutex_lock(&ec->lock);
  407. /* Allow transactions to be carried out again */
  408. clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
  409. mutex_unlock(&ec->lock);
  410. }
  411. void acpi_ec_unblock_transactions_early(void)
  412. {
  413. /*
  414. * Allow transactions to happen again (this function is called from
  415. * atomic context during wakeup, so we don't need to acquire the mutex).
  416. */
  417. if (first_ec)
  418. clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
  419. }
  420. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  421. {
  422. int result;
  423. u8 d;
  424. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  425. .wdata = NULL, .rdata = &d,
  426. .wlen = 0, .rlen = 1};
  427. if (!ec || !data)
  428. return -EINVAL;
  429. /*
  430. * Query the EC to find out which _Qxx method we need to evaluate.
  431. * Note that successful completion of the query causes the ACPI_EC_SCI
  432. * bit to be cleared (and thus clearing the interrupt source).
  433. */
  434. result = acpi_ec_transaction_unlocked(ec, &t);
  435. if (result)
  436. return result;
  437. if (!d)
  438. return -ENODATA;
  439. *data = d;
  440. return 0;
  441. }
  442. /* --------------------------------------------------------------------------
  443. Event Management
  444. -------------------------------------------------------------------------- */
  445. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  446. acpi_handle handle, acpi_ec_query_func func,
  447. void *data)
  448. {
  449. struct acpi_ec_query_handler *handler =
  450. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  451. if (!handler)
  452. return -ENOMEM;
  453. handler->query_bit = query_bit;
  454. handler->handle = handle;
  455. handler->func = func;
  456. handler->data = data;
  457. mutex_lock(&ec->lock);
  458. list_add(&handler->node, &ec->list);
  459. mutex_unlock(&ec->lock);
  460. return 0;
  461. }
  462. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  463. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  464. {
  465. struct acpi_ec_query_handler *handler, *tmp;
  466. mutex_lock(&ec->lock);
  467. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  468. if (query_bit == handler->query_bit) {
  469. list_del(&handler->node);
  470. kfree(handler);
  471. }
  472. }
  473. mutex_unlock(&ec->lock);
  474. }
  475. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  476. static void acpi_ec_run(void *cxt)
  477. {
  478. struct acpi_ec_query_handler *handler = cxt;
  479. if (!handler)
  480. return;
  481. pr_debug(PREFIX "start query execution\n");
  482. if (handler->func)
  483. handler->func(handler->data);
  484. else if (handler->handle)
  485. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  486. pr_debug(PREFIX "stop query execution\n");
  487. kfree(handler);
  488. }
  489. static int acpi_ec_sync_query(struct acpi_ec *ec)
  490. {
  491. u8 value = 0;
  492. int status;
  493. struct acpi_ec_query_handler *handler, *copy;
  494. if ((status = acpi_ec_query_unlocked(ec, &value)))
  495. return status;
  496. list_for_each_entry(handler, &ec->list, node) {
  497. if (value == handler->query_bit) {
  498. /* have custom handler for this bit */
  499. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  500. if (!copy)
  501. return -ENOMEM;
  502. memcpy(copy, handler, sizeof(*copy));
  503. pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
  504. return acpi_os_execute((copy->func) ?
  505. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  506. acpi_ec_run, copy);
  507. }
  508. }
  509. return 0;
  510. }
  511. static void acpi_ec_gpe_query(void *ec_cxt)
  512. {
  513. struct acpi_ec *ec = ec_cxt;
  514. if (!ec)
  515. return;
  516. mutex_lock(&ec->lock);
  517. acpi_ec_sync_query(ec);
  518. mutex_unlock(&ec->lock);
  519. }
  520. static void acpi_ec_gpe_query(void *ec_cxt);
  521. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  522. {
  523. if (state & ACPI_EC_FLAG_SCI) {
  524. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  525. pr_debug(PREFIX "push gpe query to the queue\n");
  526. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  527. acpi_ec_gpe_query, ec);
  528. }
  529. }
  530. return 0;
  531. }
  532. static u32 acpi_ec_gpe_handler(void *data)
  533. {
  534. struct acpi_ec *ec = data;
  535. pr_debug(PREFIX "~~~> interrupt\n");
  536. advance_transaction(ec, acpi_ec_read_status(ec));
  537. if (ec_transaction_done(ec) &&
  538. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  539. wake_up(&ec->wait);
  540. ec_check_sci(ec, acpi_ec_read_status(ec));
  541. }
  542. return ACPI_INTERRUPT_HANDLED;
  543. }
  544. /* --------------------------------------------------------------------------
  545. Address Space Management
  546. -------------------------------------------------------------------------- */
  547. static acpi_status
  548. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  549. u32 bits, u64 *value64,
  550. void *handler_context, void *region_context)
  551. {
  552. struct acpi_ec *ec = handler_context;
  553. int result = 0, i, bytes = bits / 8;
  554. u8 *value = (u8 *)value64;
  555. if ((address > 0xFF) || !value || !handler_context)
  556. return AE_BAD_PARAMETER;
  557. if (function != ACPI_READ && function != ACPI_WRITE)
  558. return AE_BAD_PARAMETER;
  559. if (EC_FLAGS_MSI || bits > 8)
  560. acpi_ec_burst_enable(ec);
  561. for (i = 0; i < bytes; ++i, ++address, ++value)
  562. result = (function == ACPI_READ) ?
  563. acpi_ec_read(ec, address, value) :
  564. acpi_ec_write(ec, address, *value);
  565. if (EC_FLAGS_MSI || bits > 8)
  566. acpi_ec_burst_disable(ec);
  567. switch (result) {
  568. case -EINVAL:
  569. return AE_BAD_PARAMETER;
  570. break;
  571. case -ENODEV:
  572. return AE_NOT_FOUND;
  573. break;
  574. case -ETIME:
  575. return AE_TIME;
  576. break;
  577. default:
  578. return AE_OK;
  579. }
  580. }
  581. /* --------------------------------------------------------------------------
  582. Driver Interface
  583. -------------------------------------------------------------------------- */
  584. static acpi_status
  585. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  586. static struct acpi_ec *make_acpi_ec(void)
  587. {
  588. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  589. if (!ec)
  590. return NULL;
  591. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  592. mutex_init(&ec->lock);
  593. init_waitqueue_head(&ec->wait);
  594. INIT_LIST_HEAD(&ec->list);
  595. spin_lock_init(&ec->curr_lock);
  596. return ec;
  597. }
  598. static acpi_status
  599. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  600. void *context, void **return_value)
  601. {
  602. char node_name[5];
  603. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  604. struct acpi_ec *ec = context;
  605. int value = 0;
  606. acpi_status status;
  607. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  608. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  609. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  610. }
  611. return AE_OK;
  612. }
  613. static acpi_status
  614. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  615. {
  616. acpi_status status;
  617. unsigned long long tmp = 0;
  618. struct acpi_ec *ec = context;
  619. /* clear addr values, ec_parse_io_ports depend on it */
  620. ec->command_addr = ec->data_addr = 0;
  621. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  622. ec_parse_io_ports, ec);
  623. if (ACPI_FAILURE(status))
  624. return status;
  625. /* Get GPE bit assignment (EC events). */
  626. /* TODO: Add support for _GPE returning a package */
  627. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  628. if (ACPI_FAILURE(status))
  629. return status;
  630. ec->gpe = tmp;
  631. /* Use the global lock for all EC transactions? */
  632. tmp = 0;
  633. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  634. ec->global_lock = tmp;
  635. ec->handle = handle;
  636. return AE_CTRL_TERMINATE;
  637. }
  638. static int ec_install_handlers(struct acpi_ec *ec)
  639. {
  640. acpi_status status;
  641. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  642. return 0;
  643. status = acpi_install_gpe_handler(NULL, ec->gpe,
  644. ACPI_GPE_EDGE_TRIGGERED,
  645. &acpi_ec_gpe_handler, ec);
  646. if (ACPI_FAILURE(status))
  647. return -ENODEV;
  648. acpi_enable_gpe(NULL, ec->gpe);
  649. status = acpi_install_address_space_handler(ec->handle,
  650. ACPI_ADR_SPACE_EC,
  651. &acpi_ec_space_handler,
  652. NULL, ec);
  653. if (ACPI_FAILURE(status)) {
  654. if (status == AE_NOT_FOUND) {
  655. /*
  656. * Maybe OS fails in evaluating the _REG object.
  657. * The AE_NOT_FOUND error will be ignored and OS
  658. * continue to initialize EC.
  659. */
  660. printk(KERN_ERR "Fail in evaluating the _REG object"
  661. " of EC device. Broken bios is suspected.\n");
  662. } else {
  663. acpi_remove_gpe_handler(NULL, ec->gpe,
  664. &acpi_ec_gpe_handler);
  665. acpi_disable_gpe(NULL, ec->gpe);
  666. return -ENODEV;
  667. }
  668. }
  669. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  670. return 0;
  671. }
  672. static void ec_remove_handlers(struct acpi_ec *ec)
  673. {
  674. acpi_disable_gpe(NULL, ec->gpe);
  675. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  676. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  677. pr_err(PREFIX "failed to remove space handler\n");
  678. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  679. &acpi_ec_gpe_handler)))
  680. pr_err(PREFIX "failed to remove gpe handler\n");
  681. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  682. }
  683. static int acpi_ec_add(struct acpi_device *device)
  684. {
  685. struct acpi_ec *ec = NULL;
  686. int ret;
  687. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  688. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  689. /* Check for boot EC */
  690. if (boot_ec &&
  691. (boot_ec->handle == device->handle ||
  692. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  693. ec = boot_ec;
  694. boot_ec = NULL;
  695. } else {
  696. ec = make_acpi_ec();
  697. if (!ec)
  698. return -ENOMEM;
  699. }
  700. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  701. AE_CTRL_TERMINATE) {
  702. kfree(ec);
  703. return -EINVAL;
  704. }
  705. ec->handle = device->handle;
  706. /* Find and register all query methods */
  707. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  708. acpi_ec_register_query_methods, NULL, ec, NULL);
  709. if (!first_ec)
  710. first_ec = ec;
  711. device->driver_data = ec;
  712. WARN(!request_region(ec->data_addr, 1, "EC data"),
  713. "Could not request EC data io port 0x%lx", ec->data_addr);
  714. WARN(!request_region(ec->command_addr, 1, "EC cmd"),
  715. "Could not request EC cmd io port 0x%lx", ec->command_addr);
  716. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  717. ec->gpe, ec->command_addr, ec->data_addr);
  718. ret = ec_install_handlers(ec);
  719. /* EC is fully operational, allow queries */
  720. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  721. return ret;
  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. ec_remove_handlers(ec);
  731. mutex_lock(&ec->lock);
  732. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  733. list_del(&handler->node);
  734. kfree(handler);
  735. }
  736. mutex_unlock(&ec->lock);
  737. release_region(ec->data_addr, 1);
  738. release_region(ec->command_addr, 1);
  739. device->driver_data = NULL;
  740. if (ec == first_ec)
  741. first_ec = NULL;
  742. kfree(ec);
  743. return 0;
  744. }
  745. static acpi_status
  746. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  747. {
  748. struct acpi_ec *ec = context;
  749. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  750. return AE_OK;
  751. /*
  752. * The first address region returned is the data port, and
  753. * the second address region returned is the status/command
  754. * port.
  755. */
  756. if (ec->data_addr == 0)
  757. ec->data_addr = resource->data.io.minimum;
  758. else if (ec->command_addr == 0)
  759. ec->command_addr = resource->data.io.minimum;
  760. else
  761. return AE_CTRL_TERMINATE;
  762. return AE_OK;
  763. }
  764. int __init acpi_boot_ec_enable(void)
  765. {
  766. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  767. return 0;
  768. if (!ec_install_handlers(boot_ec)) {
  769. first_ec = boot_ec;
  770. return 0;
  771. }
  772. return -EFAULT;
  773. }
  774. static const struct acpi_device_id ec_device_ids[] = {
  775. {"PNP0C09", 0},
  776. {"", 0},
  777. };
  778. /* Some BIOS do not survive early DSDT scan, skip it */
  779. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  780. {
  781. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  782. return 0;
  783. }
  784. /* ASUStek often supplies us with broken ECDT, validate it */
  785. static int ec_validate_ecdt(const struct dmi_system_id *id)
  786. {
  787. EC_FLAGS_VALIDATE_ECDT = 1;
  788. return 0;
  789. }
  790. /* MSI EC needs special treatment, enable it */
  791. static int ec_flag_msi(const struct dmi_system_id *id)
  792. {
  793. printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
  794. EC_FLAGS_MSI = 1;
  795. EC_FLAGS_VALIDATE_ECDT = 1;
  796. return 0;
  797. }
  798. static struct dmi_system_id __initdata ec_dmi_table[] = {
  799. {
  800. ec_skip_dsdt_scan, "Compal JFL92", {
  801. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  802. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  803. {
  804. ec_flag_msi, "MSI hardware", {
  805. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  806. {
  807. ec_flag_msi, "MSI hardware", {
  808. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  809. {
  810. ec_flag_msi, "MSI hardware", {
  811. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  812. {
  813. ec_flag_msi, "MSI hardware", {
  814. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
  815. {
  816. ec_validate_ecdt, "ASUS hardware", {
  817. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  818. {},
  819. };
  820. int __init acpi_ec_ecdt_probe(void)
  821. {
  822. acpi_status status;
  823. struct acpi_ec *saved_ec = NULL;
  824. struct acpi_table_ecdt *ecdt_ptr;
  825. boot_ec = make_acpi_ec();
  826. if (!boot_ec)
  827. return -ENOMEM;
  828. /*
  829. * Generate a boot ec context
  830. */
  831. dmi_check_system(ec_dmi_table);
  832. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  833. (struct acpi_table_header **)&ecdt_ptr);
  834. if (ACPI_SUCCESS(status)) {
  835. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  836. boot_ec->command_addr = ecdt_ptr->control.address;
  837. boot_ec->data_addr = ecdt_ptr->data.address;
  838. boot_ec->gpe = ecdt_ptr->gpe;
  839. boot_ec->handle = ACPI_ROOT_OBJECT;
  840. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  841. /* Don't trust ECDT, which comes from ASUSTek */
  842. if (!EC_FLAGS_VALIDATE_ECDT)
  843. goto install;
  844. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  845. if (!saved_ec)
  846. return -ENOMEM;
  847. /* fall through */
  848. }
  849. if (EC_FLAGS_SKIP_DSDT_SCAN)
  850. return -ENODEV;
  851. /* This workaround is needed only on some broken machines,
  852. * which require early EC, but fail to provide ECDT */
  853. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  854. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  855. boot_ec, NULL);
  856. /* Check that acpi_get_devices actually find something */
  857. if (ACPI_FAILURE(status) || !boot_ec->handle)
  858. goto error;
  859. if (saved_ec) {
  860. /* try to find good ECDT from ASUSTek */
  861. if (saved_ec->command_addr != boot_ec->command_addr ||
  862. saved_ec->data_addr != boot_ec->data_addr ||
  863. saved_ec->gpe != boot_ec->gpe ||
  864. saved_ec->handle != boot_ec->handle)
  865. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  866. "ECDT tables, which are very hard to workaround. "
  867. "Trying to use DSDT EC info instead. Please send "
  868. "output of acpidump to linux-acpi@vger.kernel.org\n");
  869. kfree(saved_ec);
  870. saved_ec = NULL;
  871. } else {
  872. /* We really need to limit this workaround, the only ASUS,
  873. * which needs it, has fake EC._INI method, so use it as flag.
  874. * Keep boot_ec struct as it will be needed soon.
  875. */
  876. acpi_handle dummy;
  877. if (!dmi_name_in_vendors("ASUS") ||
  878. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  879. &dummy)))
  880. return -ENODEV;
  881. }
  882. install:
  883. if (!ec_install_handlers(boot_ec)) {
  884. first_ec = boot_ec;
  885. return 0;
  886. }
  887. error:
  888. kfree(boot_ec);
  889. boot_ec = NULL;
  890. return -ENODEV;
  891. }
  892. static struct acpi_driver acpi_ec_driver = {
  893. .name = "ec",
  894. .class = ACPI_EC_CLASS,
  895. .ids = ec_device_ids,
  896. .ops = {
  897. .add = acpi_ec_add,
  898. .remove = acpi_ec_remove,
  899. },
  900. };
  901. int __init acpi_ec_init(void)
  902. {
  903. int result = 0;
  904. /* Now register the driver for the EC */
  905. result = acpi_bus_register_driver(&acpi_ec_driver);
  906. if (result < 0)
  907. return -ENODEV;
  908. return result;
  909. }
  910. /* EC driver currently not unloadable */
  911. #if 0
  912. static void __exit acpi_ec_exit(void)
  913. {
  914. acpi_bus_unregister_driver(&acpi_ec_driver);
  915. return;
  916. }
  917. #endif /* 0 */