ec.c 27 KB

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