ec.c 29 KB

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