ec.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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_FROZEN, /* Transactions are suspended */
  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_FROZEN, &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_suspend_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_FROZEN, &ec->flags);
  413. mutex_unlock(&ec->lock);
  414. }
  415. void acpi_ec_resume_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_FROZEN, &ec->flags);
  423. mutex_unlock(&ec->lock);
  424. }
  425. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  426. {
  427. int result;
  428. u8 d;
  429. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  430. .wdata = NULL, .rdata = &d,
  431. .wlen = 0, .rlen = 1};
  432. if (!ec || !data)
  433. return -EINVAL;
  434. /*
  435. * Query the EC to find out which _Qxx method we need to evaluate.
  436. * Note that successful completion of the query causes the ACPI_EC_SCI
  437. * bit to be cleared (and thus clearing the interrupt source).
  438. */
  439. result = acpi_ec_transaction_unlocked(ec, &t);
  440. if (result)
  441. return result;
  442. if (!d)
  443. return -ENODATA;
  444. *data = d;
  445. return 0;
  446. }
  447. /* --------------------------------------------------------------------------
  448. Event Management
  449. -------------------------------------------------------------------------- */
  450. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  451. acpi_handle handle, acpi_ec_query_func func,
  452. void *data)
  453. {
  454. struct acpi_ec_query_handler *handler =
  455. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  456. if (!handler)
  457. return -ENOMEM;
  458. handler->query_bit = query_bit;
  459. handler->handle = handle;
  460. handler->func = func;
  461. handler->data = data;
  462. mutex_lock(&ec->lock);
  463. list_add(&handler->node, &ec->list);
  464. mutex_unlock(&ec->lock);
  465. return 0;
  466. }
  467. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  468. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  469. {
  470. struct acpi_ec_query_handler *handler, *tmp;
  471. mutex_lock(&ec->lock);
  472. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  473. if (query_bit == handler->query_bit) {
  474. list_del(&handler->node);
  475. kfree(handler);
  476. }
  477. }
  478. mutex_unlock(&ec->lock);
  479. }
  480. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  481. static void acpi_ec_run(void *cxt)
  482. {
  483. struct acpi_ec_query_handler *handler = cxt;
  484. if (!handler)
  485. return;
  486. pr_debug(PREFIX "start query execution\n");
  487. if (handler->func)
  488. handler->func(handler->data);
  489. else if (handler->handle)
  490. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  491. pr_debug(PREFIX "stop query execution\n");
  492. kfree(handler);
  493. }
  494. static int acpi_ec_sync_query(struct acpi_ec *ec)
  495. {
  496. u8 value = 0;
  497. int status;
  498. struct acpi_ec_query_handler *handler, *copy;
  499. if ((status = acpi_ec_query_unlocked(ec, &value)))
  500. return status;
  501. list_for_each_entry(handler, &ec->list, node) {
  502. if (value == handler->query_bit) {
  503. /* have custom handler for this bit */
  504. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  505. if (!copy)
  506. return -ENOMEM;
  507. memcpy(copy, handler, sizeof(*copy));
  508. pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
  509. return acpi_os_execute((copy->func) ?
  510. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  511. acpi_ec_run, copy);
  512. }
  513. }
  514. return 0;
  515. }
  516. static void acpi_ec_gpe_query(void *ec_cxt)
  517. {
  518. struct acpi_ec *ec = ec_cxt;
  519. if (!ec)
  520. return;
  521. mutex_lock(&ec->lock);
  522. acpi_ec_sync_query(ec);
  523. mutex_unlock(&ec->lock);
  524. }
  525. static void acpi_ec_gpe_query(void *ec_cxt);
  526. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  527. {
  528. if (state & ACPI_EC_FLAG_SCI) {
  529. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  530. pr_debug(PREFIX "push gpe query to the queue\n");
  531. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  532. acpi_ec_gpe_query, ec);
  533. }
  534. }
  535. return 0;
  536. }
  537. static u32 acpi_ec_gpe_handler(void *data)
  538. {
  539. struct acpi_ec *ec = data;
  540. pr_debug(PREFIX "~~~> interrupt\n");
  541. advance_transaction(ec, acpi_ec_read_status(ec));
  542. if (ec_transaction_done(ec) &&
  543. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  544. wake_up(&ec->wait);
  545. ec_check_sci(ec, acpi_ec_read_status(ec));
  546. }
  547. return ACPI_INTERRUPT_HANDLED;
  548. }
  549. /* --------------------------------------------------------------------------
  550. Address Space Management
  551. -------------------------------------------------------------------------- */
  552. static acpi_status
  553. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  554. u32 bits, u64 *value64,
  555. void *handler_context, void *region_context)
  556. {
  557. struct acpi_ec *ec = handler_context;
  558. int result = 0, i, bytes = bits / 8;
  559. u8 *value = (u8 *)value64;
  560. if ((address > 0xFF) || !value || !handler_context)
  561. return AE_BAD_PARAMETER;
  562. if (function != ACPI_READ && function != ACPI_WRITE)
  563. return AE_BAD_PARAMETER;
  564. if (EC_FLAGS_MSI || bits > 8)
  565. acpi_ec_burst_enable(ec);
  566. for (i = 0; i < bytes; ++i, ++address, ++value)
  567. result = (function == ACPI_READ) ?
  568. acpi_ec_read(ec, address, value) :
  569. acpi_ec_write(ec, address, *value);
  570. if (EC_FLAGS_MSI || bits > 8)
  571. acpi_ec_burst_disable(ec);
  572. switch (result) {
  573. case -EINVAL:
  574. return AE_BAD_PARAMETER;
  575. break;
  576. case -ENODEV:
  577. return AE_NOT_FOUND;
  578. break;
  579. case -ETIME:
  580. return AE_TIME;
  581. break;
  582. default:
  583. return AE_OK;
  584. }
  585. }
  586. /* --------------------------------------------------------------------------
  587. FS Interface (/proc)
  588. -------------------------------------------------------------------------- */
  589. static struct proc_dir_entry *acpi_ec_dir;
  590. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  591. {
  592. struct acpi_ec *ec = seq->private;
  593. if (!ec)
  594. goto end;
  595. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  596. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  597. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  598. seq_printf(seq, "use global lock:\t%s\n",
  599. ec->global_lock ? "yes" : "no");
  600. end:
  601. return 0;
  602. }
  603. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  604. {
  605. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  606. }
  607. static const struct file_operations acpi_ec_info_ops = {
  608. .open = acpi_ec_info_open_fs,
  609. .read = seq_read,
  610. .llseek = seq_lseek,
  611. .release = single_release,
  612. .owner = THIS_MODULE,
  613. };
  614. static int acpi_ec_add_fs(struct acpi_device *device)
  615. {
  616. struct proc_dir_entry *entry = NULL;
  617. if (!acpi_device_dir(device)) {
  618. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  619. acpi_ec_dir);
  620. if (!acpi_device_dir(device))
  621. return -ENODEV;
  622. }
  623. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  624. acpi_device_dir(device),
  625. &acpi_ec_info_ops, acpi_driver_data(device));
  626. if (!entry)
  627. return -ENODEV;
  628. return 0;
  629. }
  630. static int acpi_ec_remove_fs(struct acpi_device *device)
  631. {
  632. if (acpi_device_dir(device)) {
  633. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  634. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  635. acpi_device_dir(device) = NULL;
  636. }
  637. return 0;
  638. }
  639. /* --------------------------------------------------------------------------
  640. Driver Interface
  641. -------------------------------------------------------------------------- */
  642. static acpi_status
  643. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  644. static struct acpi_ec *make_acpi_ec(void)
  645. {
  646. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  647. if (!ec)
  648. return NULL;
  649. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  650. mutex_init(&ec->lock);
  651. init_waitqueue_head(&ec->wait);
  652. INIT_LIST_HEAD(&ec->list);
  653. spin_lock_init(&ec->curr_lock);
  654. return ec;
  655. }
  656. static acpi_status
  657. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  658. void *context, void **return_value)
  659. {
  660. char node_name[5];
  661. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  662. struct acpi_ec *ec = context;
  663. int value = 0;
  664. acpi_status status;
  665. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  666. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  667. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  668. }
  669. return AE_OK;
  670. }
  671. static acpi_status
  672. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  673. {
  674. acpi_status status;
  675. unsigned long long tmp = 0;
  676. struct acpi_ec *ec = context;
  677. /* clear addr values, ec_parse_io_ports depend on it */
  678. ec->command_addr = ec->data_addr = 0;
  679. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  680. ec_parse_io_ports, ec);
  681. if (ACPI_FAILURE(status))
  682. return status;
  683. /* Get GPE bit assignment (EC events). */
  684. /* TODO: Add support for _GPE returning a package */
  685. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  686. if (ACPI_FAILURE(status))
  687. return status;
  688. ec->gpe = tmp;
  689. /* Use the global lock for all EC transactions? */
  690. tmp = 0;
  691. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  692. ec->global_lock = tmp;
  693. ec->handle = handle;
  694. return AE_CTRL_TERMINATE;
  695. }
  696. static int ec_install_handlers(struct acpi_ec *ec)
  697. {
  698. acpi_status status;
  699. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  700. return 0;
  701. status = acpi_install_gpe_handler(NULL, ec->gpe,
  702. ACPI_GPE_EDGE_TRIGGERED,
  703. &acpi_ec_gpe_handler, ec);
  704. if (ACPI_FAILURE(status))
  705. return -ENODEV;
  706. acpi_enable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  707. status = acpi_install_address_space_handler(ec->handle,
  708. ACPI_ADR_SPACE_EC,
  709. &acpi_ec_space_handler,
  710. NULL, ec);
  711. if (ACPI_FAILURE(status)) {
  712. if (status == AE_NOT_FOUND) {
  713. /*
  714. * Maybe OS fails in evaluating the _REG object.
  715. * The AE_NOT_FOUND error will be ignored and OS
  716. * continue to initialize EC.
  717. */
  718. printk(KERN_ERR "Fail in evaluating the _REG object"
  719. " of EC device. Broken bios is suspected.\n");
  720. } else {
  721. acpi_remove_gpe_handler(NULL, ec->gpe,
  722. &acpi_ec_gpe_handler);
  723. acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  724. return -ENODEV;
  725. }
  726. }
  727. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  728. return 0;
  729. }
  730. static void ec_remove_handlers(struct acpi_ec *ec)
  731. {
  732. acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  733. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  734. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  735. pr_err(PREFIX "failed to remove space handler\n");
  736. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  737. &acpi_ec_gpe_handler)))
  738. pr_err(PREFIX "failed to remove gpe handler\n");
  739. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  740. }
  741. static int acpi_ec_add(struct acpi_device *device)
  742. {
  743. struct acpi_ec *ec = NULL;
  744. int ret;
  745. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  746. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  747. /* Check for boot EC */
  748. if (boot_ec &&
  749. (boot_ec->handle == device->handle ||
  750. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  751. ec = boot_ec;
  752. boot_ec = NULL;
  753. } else {
  754. ec = make_acpi_ec();
  755. if (!ec)
  756. return -ENOMEM;
  757. }
  758. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  759. AE_CTRL_TERMINATE) {
  760. kfree(ec);
  761. return -EINVAL;
  762. }
  763. ec->handle = device->handle;
  764. /* Find and register all query methods */
  765. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  766. acpi_ec_register_query_methods, NULL, ec, NULL);
  767. if (!first_ec)
  768. first_ec = ec;
  769. device->driver_data = ec;
  770. acpi_ec_add_fs(device);
  771. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  772. ec->gpe, ec->command_addr, ec->data_addr);
  773. ret = ec_install_handlers(ec);
  774. /* EC is fully operational, allow queries */
  775. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  776. return ret;
  777. }
  778. static int acpi_ec_remove(struct acpi_device *device, int type)
  779. {
  780. struct acpi_ec *ec;
  781. struct acpi_ec_query_handler *handler, *tmp;
  782. if (!device)
  783. return -EINVAL;
  784. ec = acpi_driver_data(device);
  785. ec_remove_handlers(ec);
  786. mutex_lock(&ec->lock);
  787. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  788. list_del(&handler->node);
  789. kfree(handler);
  790. }
  791. mutex_unlock(&ec->lock);
  792. acpi_ec_remove_fs(device);
  793. device->driver_data = NULL;
  794. if (ec == first_ec)
  795. first_ec = NULL;
  796. kfree(ec);
  797. return 0;
  798. }
  799. static acpi_status
  800. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  801. {
  802. struct acpi_ec *ec = context;
  803. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  804. return AE_OK;
  805. /*
  806. * The first address region returned is the data port, and
  807. * the second address region returned is the status/command
  808. * port.
  809. */
  810. if (ec->data_addr == 0)
  811. ec->data_addr = resource->data.io.minimum;
  812. else if (ec->command_addr == 0)
  813. ec->command_addr = resource->data.io.minimum;
  814. else
  815. return AE_CTRL_TERMINATE;
  816. return AE_OK;
  817. }
  818. int __init acpi_boot_ec_enable(void)
  819. {
  820. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  821. return 0;
  822. if (!ec_install_handlers(boot_ec)) {
  823. first_ec = boot_ec;
  824. return 0;
  825. }
  826. return -EFAULT;
  827. }
  828. static const struct acpi_device_id ec_device_ids[] = {
  829. {"PNP0C09", 0},
  830. {"", 0},
  831. };
  832. /* Some BIOS do not survive early DSDT scan, skip it */
  833. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  834. {
  835. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  836. return 0;
  837. }
  838. /* ASUStek often supplies us with broken ECDT, validate it */
  839. static int ec_validate_ecdt(const struct dmi_system_id *id)
  840. {
  841. EC_FLAGS_VALIDATE_ECDT = 1;
  842. return 0;
  843. }
  844. /* MSI EC needs special treatment, enable it */
  845. static int ec_flag_msi(const struct dmi_system_id *id)
  846. {
  847. printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
  848. EC_FLAGS_MSI = 1;
  849. EC_FLAGS_VALIDATE_ECDT = 1;
  850. return 0;
  851. }
  852. static struct dmi_system_id __initdata ec_dmi_table[] = {
  853. {
  854. ec_skip_dsdt_scan, "Compal JFL92", {
  855. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  856. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  857. {
  858. ec_flag_msi, "MSI hardware", {
  859. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  860. {
  861. ec_flag_msi, "MSI hardware", {
  862. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  863. {
  864. ec_flag_msi, "MSI hardware", {
  865. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  866. {
  867. ec_validate_ecdt, "ASUS hardware", {
  868. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  869. {},
  870. };
  871. int __init acpi_ec_ecdt_probe(void)
  872. {
  873. acpi_status status;
  874. struct acpi_ec *saved_ec = NULL;
  875. struct acpi_table_ecdt *ecdt_ptr;
  876. boot_ec = make_acpi_ec();
  877. if (!boot_ec)
  878. return -ENOMEM;
  879. /*
  880. * Generate a boot ec context
  881. */
  882. dmi_check_system(ec_dmi_table);
  883. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  884. (struct acpi_table_header **)&ecdt_ptr);
  885. if (ACPI_SUCCESS(status)) {
  886. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  887. boot_ec->command_addr = ecdt_ptr->control.address;
  888. boot_ec->data_addr = ecdt_ptr->data.address;
  889. boot_ec->gpe = ecdt_ptr->gpe;
  890. boot_ec->handle = ACPI_ROOT_OBJECT;
  891. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  892. /* Don't trust ECDT, which comes from ASUSTek */
  893. if (!EC_FLAGS_VALIDATE_ECDT)
  894. goto install;
  895. saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  896. if (!saved_ec)
  897. return -ENOMEM;
  898. memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
  899. /* fall through */
  900. }
  901. if (EC_FLAGS_SKIP_DSDT_SCAN)
  902. return -ENODEV;
  903. /* This workaround is needed only on some broken machines,
  904. * which require early EC, but fail to provide ECDT */
  905. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  906. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  907. boot_ec, NULL);
  908. /* Check that acpi_get_devices actually find something */
  909. if (ACPI_FAILURE(status) || !boot_ec->handle)
  910. goto error;
  911. if (saved_ec) {
  912. /* try to find good ECDT from ASUSTek */
  913. if (saved_ec->command_addr != boot_ec->command_addr ||
  914. saved_ec->data_addr != boot_ec->data_addr ||
  915. saved_ec->gpe != boot_ec->gpe ||
  916. saved_ec->handle != boot_ec->handle)
  917. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  918. "ECDT tables, which are very hard to workaround. "
  919. "Trying to use DSDT EC info instead. Please send "
  920. "output of acpidump to linux-acpi@vger.kernel.org\n");
  921. kfree(saved_ec);
  922. saved_ec = NULL;
  923. } else {
  924. /* We really need to limit this workaround, the only ASUS,
  925. * which needs it, has fake EC._INI method, so use it as flag.
  926. * Keep boot_ec struct as it will be needed soon.
  927. */
  928. acpi_handle dummy;
  929. if (!dmi_name_in_vendors("ASUS") ||
  930. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  931. &dummy)))
  932. return -ENODEV;
  933. }
  934. install:
  935. if (!ec_install_handlers(boot_ec)) {
  936. first_ec = boot_ec;
  937. return 0;
  938. }
  939. error:
  940. kfree(boot_ec);
  941. boot_ec = NULL;
  942. return -ENODEV;
  943. }
  944. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  945. {
  946. struct acpi_ec *ec = acpi_driver_data(device);
  947. /* Stop using the GPE, but keep it reference counted. */
  948. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  949. return 0;
  950. }
  951. static int acpi_ec_resume(struct acpi_device *device)
  952. {
  953. struct acpi_ec *ec = acpi_driver_data(device);
  954. /* Enable the GPE again, but don't reference count it once more. */
  955. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  956. return 0;
  957. }
  958. static struct acpi_driver acpi_ec_driver = {
  959. .name = "ec",
  960. .class = ACPI_EC_CLASS,
  961. .ids = ec_device_ids,
  962. .ops = {
  963. .add = acpi_ec_add,
  964. .remove = acpi_ec_remove,
  965. .suspend = acpi_ec_suspend,
  966. .resume = acpi_ec_resume,
  967. },
  968. };
  969. int __init acpi_ec_init(void)
  970. {
  971. int result = 0;
  972. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  973. if (!acpi_ec_dir)
  974. return -ENODEV;
  975. /* Now register the driver for the EC */
  976. result = acpi_bus_register_driver(&acpi_ec_driver);
  977. if (result < 0) {
  978. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  979. return -ENODEV;
  980. }
  981. return result;
  982. }
  983. /* EC driver currently not unloadable */
  984. #if 0
  985. static void __exit acpi_ec_exit(void)
  986. {
  987. acpi_bus_unregister_driver(&acpi_ec_driver);
  988. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  989. return;
  990. }
  991. #endif /* 0 */