ec.c 29 KB

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