ec.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v2.2)
  3. *
  4. * Copyright (C) 2001-2014 Intel Corporation
  5. * Author: 2014 Lv Zheng <lv.zheng@intel.com>
  6. * 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  7. * 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  8. * 2004 Luming Yu <luming.yu@intel.com>
  9. * 2001, 2002 Andy Grover <andrew.grover@intel.com>
  10. * 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  11. * Copyright (C) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  12. *
  13. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  28. *
  29. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. */
  31. /* Uncomment next line to get verbose printout */
  32. /* #define DEBUG */
  33. #define pr_fmt(fmt) "ACPI : EC: " fmt
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/types.h>
  38. #include <linux/delay.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/list.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/slab.h>
  43. #include <linux/acpi.h>
  44. #include <linux/dmi.h>
  45. #include <asm/io.h>
  46. #include "internal.h"
  47. #define ACPI_EC_CLASS "embedded_controller"
  48. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  49. #define ACPI_EC_FILE_INFO "info"
  50. /* EC status register */
  51. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  52. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  53. #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
  54. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  55. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  56. /* EC commands */
  57. enum ec_command {
  58. ACPI_EC_COMMAND_READ = 0x80,
  59. ACPI_EC_COMMAND_WRITE = 0x81,
  60. ACPI_EC_BURST_ENABLE = 0x82,
  61. ACPI_EC_BURST_DISABLE = 0x83,
  62. ACPI_EC_COMMAND_QUERY = 0x84,
  63. };
  64. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  65. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  66. #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
  67. #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
  68. * when trying to clear the EC */
  69. enum {
  70. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  71. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  72. EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
  73. * OpReg are installed */
  74. EC_FLAGS_BLOCKED, /* Transactions are blocked */
  75. };
  76. #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
  77. #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
  78. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  79. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  80. module_param(ec_delay, uint, 0644);
  81. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  82. /*
  83. * If the number of false interrupts per one transaction exceeds
  84. * this threshold, will think there is a GPE storm happened and
  85. * will disable the GPE for normal transaction.
  86. */
  87. static unsigned int ec_storm_threshold __read_mostly = 8;
  88. module_param(ec_storm_threshold, uint, 0644);
  89. MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
  90. struct acpi_ec_query_handler {
  91. struct list_head node;
  92. acpi_ec_query_func func;
  93. acpi_handle handle;
  94. void *data;
  95. u8 query_bit;
  96. struct kref kref;
  97. };
  98. struct transaction {
  99. const u8 *wdata;
  100. u8 *rdata;
  101. unsigned short irq_count;
  102. u8 command;
  103. u8 wi;
  104. u8 ri;
  105. u8 wlen;
  106. u8 rlen;
  107. u8 flags;
  108. };
  109. struct acpi_ec *boot_ec, *first_ec;
  110. EXPORT_SYMBOL(first_ec);
  111. static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
  112. static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
  113. static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
  114. static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
  115. static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
  116. /* --------------------------------------------------------------------------
  117. * Transaction Management
  118. * -------------------------------------------------------------------------- */
  119. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  120. {
  121. u8 x = inb(ec->command_addr);
  122. pr_debug("EC_SC(R) = 0x%2.2x "
  123. "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d\n",
  124. x,
  125. !!(x & ACPI_EC_FLAG_SCI),
  126. !!(x & ACPI_EC_FLAG_BURST),
  127. !!(x & ACPI_EC_FLAG_CMD),
  128. !!(x & ACPI_EC_FLAG_IBF),
  129. !!(x & ACPI_EC_FLAG_OBF));
  130. return x;
  131. }
  132. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  133. {
  134. u8 x = inb(ec->data_addr);
  135. pr_debug("EC_DATA(R) = 0x%2.2x\n", x);
  136. return x;
  137. }
  138. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  139. {
  140. pr_debug("EC_SC(W) = 0x%2.2x\n", command);
  141. outb(command, ec->command_addr);
  142. }
  143. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  144. {
  145. pr_debug("EC_DATA(W) = 0x%2.2x\n", data);
  146. outb(data, ec->data_addr);
  147. }
  148. #ifdef DEBUG
  149. static const char *acpi_ec_cmd_string(u8 cmd)
  150. {
  151. switch (cmd) {
  152. case 0x80:
  153. return "RD_EC";
  154. case 0x81:
  155. return "WR_EC";
  156. case 0x82:
  157. return "BE_EC";
  158. case 0x83:
  159. return "BD_EC";
  160. case 0x84:
  161. return "QR_EC";
  162. }
  163. return "UNKNOWN";
  164. }
  165. #else
  166. #define acpi_ec_cmd_string(cmd) "UNDEF"
  167. #endif
  168. static int ec_transaction_completed(struct acpi_ec *ec)
  169. {
  170. unsigned long flags;
  171. int ret = 0;
  172. spin_lock_irqsave(&ec->lock, flags);
  173. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
  174. ret = 1;
  175. spin_unlock_irqrestore(&ec->lock, flags);
  176. return ret;
  177. }
  178. static void advance_transaction(struct acpi_ec *ec)
  179. {
  180. struct transaction *t;
  181. u8 status;
  182. bool wakeup = false;
  183. pr_debug("===== %s (%d) =====\n",
  184. in_interrupt() ? "IRQ" : "TASK", smp_processor_id());
  185. status = acpi_ec_read_status(ec);
  186. t = ec->curr;
  187. if (!t)
  188. goto err;
  189. if (t->flags & ACPI_EC_COMMAND_POLL) {
  190. if (t->wlen > t->wi) {
  191. if ((status & ACPI_EC_FLAG_IBF) == 0)
  192. acpi_ec_write_data(ec, t->wdata[t->wi++]);
  193. else
  194. goto err;
  195. } else if (t->rlen > t->ri) {
  196. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  197. t->rdata[t->ri++] = acpi_ec_read_data(ec);
  198. if (t->rlen == t->ri) {
  199. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  200. if (t->command == ACPI_EC_COMMAND_QUERY)
  201. pr_debug("***** Command(%s) hardware completion *****\n",
  202. acpi_ec_cmd_string(t->command));
  203. wakeup = true;
  204. }
  205. } else
  206. goto err;
  207. } else if (t->wlen == t->wi &&
  208. (status & ACPI_EC_FLAG_IBF) == 0) {
  209. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  210. wakeup = true;
  211. }
  212. goto out;
  213. } else {
  214. if (EC_FLAGS_QUERY_HANDSHAKE &&
  215. !(status & ACPI_EC_FLAG_SCI) &&
  216. (t->command == ACPI_EC_COMMAND_QUERY)) {
  217. t->flags |= ACPI_EC_COMMAND_POLL;
  218. t->rdata[t->ri++] = 0x00;
  219. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  220. pr_debug("***** Command(%s) software completion *****\n",
  221. acpi_ec_cmd_string(t->command));
  222. wakeup = true;
  223. } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
  224. acpi_ec_write_cmd(ec, t->command);
  225. t->flags |= ACPI_EC_COMMAND_POLL;
  226. } else
  227. goto err;
  228. goto out;
  229. }
  230. err:
  231. /*
  232. * If SCI bit is set, then don't think it's a false IRQ
  233. * otherwise will take a not handled IRQ as a false one.
  234. */
  235. if (!(status & ACPI_EC_FLAG_SCI)) {
  236. if (in_interrupt() && t)
  237. ++t->irq_count;
  238. }
  239. out:
  240. if (wakeup && in_interrupt())
  241. wake_up(&ec->wait);
  242. }
  243. static void start_transaction(struct acpi_ec *ec)
  244. {
  245. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  246. ec->curr->flags = 0;
  247. advance_transaction(ec);
  248. }
  249. static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data);
  250. static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
  251. {
  252. if (state & ACPI_EC_FLAG_SCI) {
  253. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  254. return acpi_ec_sync_query(ec, NULL);
  255. }
  256. return 0;
  257. }
  258. static int ec_poll(struct acpi_ec *ec)
  259. {
  260. unsigned long flags;
  261. int repeat = 5; /* number of command restarts */
  262. while (repeat--) {
  263. unsigned long delay = jiffies +
  264. msecs_to_jiffies(ec_delay);
  265. do {
  266. /* don't sleep with disabled interrupts */
  267. if (EC_FLAGS_MSI || irqs_disabled()) {
  268. udelay(ACPI_EC_MSI_UDELAY);
  269. if (ec_transaction_completed(ec))
  270. return 0;
  271. } else {
  272. if (wait_event_timeout(ec->wait,
  273. ec_transaction_completed(ec),
  274. msecs_to_jiffies(1)))
  275. return 0;
  276. }
  277. spin_lock_irqsave(&ec->lock, flags);
  278. advance_transaction(ec);
  279. spin_unlock_irqrestore(&ec->lock, flags);
  280. } while (time_before(jiffies, delay));
  281. pr_debug("controller reset, restart transaction\n");
  282. spin_lock_irqsave(&ec->lock, flags);
  283. start_transaction(ec);
  284. spin_unlock_irqrestore(&ec->lock, flags);
  285. }
  286. return -ETIME;
  287. }
  288. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  289. struct transaction *t)
  290. {
  291. unsigned long tmp;
  292. int ret = 0;
  293. if (EC_FLAGS_MSI)
  294. udelay(ACPI_EC_MSI_UDELAY);
  295. /* start transaction */
  296. spin_lock_irqsave(&ec->lock, tmp);
  297. /* following two actions should be kept atomic */
  298. ec->curr = t;
  299. pr_debug("***** Command(%s) started *****\n",
  300. acpi_ec_cmd_string(t->command));
  301. start_transaction(ec);
  302. if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
  303. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  304. pr_debug("***** Event stopped *****\n");
  305. }
  306. spin_unlock_irqrestore(&ec->lock, tmp);
  307. ret = ec_poll(ec);
  308. spin_lock_irqsave(&ec->lock, tmp);
  309. pr_debug("***** Command(%s) stopped *****\n",
  310. acpi_ec_cmd_string(t->command));
  311. ec->curr = NULL;
  312. spin_unlock_irqrestore(&ec->lock, tmp);
  313. return ret;
  314. }
  315. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  316. {
  317. int status;
  318. u32 glk;
  319. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  320. return -EINVAL;
  321. if (t->rdata)
  322. memset(t->rdata, 0, t->rlen);
  323. mutex_lock(&ec->mutex);
  324. if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
  325. status = -EINVAL;
  326. goto unlock;
  327. }
  328. if (ec->global_lock) {
  329. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  330. if (ACPI_FAILURE(status)) {
  331. status = -ENODEV;
  332. goto unlock;
  333. }
  334. }
  335. /* disable GPE during transaction if storm is detected */
  336. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  337. /* It has to be disabled, so that it doesn't trigger. */
  338. acpi_disable_gpe(NULL, ec->gpe);
  339. }
  340. status = acpi_ec_transaction_unlocked(ec, t);
  341. /* check if we received SCI during transaction */
  342. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  343. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  344. msleep(1);
  345. /* It is safe to enable the GPE outside of the transaction. */
  346. acpi_enable_gpe(NULL, ec->gpe);
  347. } else if (t->irq_count > ec_storm_threshold) {
  348. pr_info("GPE storm detected(%d GPEs), "
  349. "transactions will use polling mode\n",
  350. t->irq_count);
  351. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  352. }
  353. if (ec->global_lock)
  354. acpi_release_global_lock(glk);
  355. unlock:
  356. mutex_unlock(&ec->mutex);
  357. return status;
  358. }
  359. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  360. {
  361. u8 d;
  362. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  363. .wdata = NULL, .rdata = &d,
  364. .wlen = 0, .rlen = 1};
  365. return acpi_ec_transaction(ec, &t);
  366. }
  367. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  368. {
  369. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  370. .wdata = NULL, .rdata = NULL,
  371. .wlen = 0, .rlen = 0};
  372. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  373. acpi_ec_transaction(ec, &t) : 0;
  374. }
  375. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
  376. {
  377. int result;
  378. u8 d;
  379. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  380. .wdata = &address, .rdata = &d,
  381. .wlen = 1, .rlen = 1};
  382. result = acpi_ec_transaction(ec, &t);
  383. *data = d;
  384. return result;
  385. }
  386. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  387. {
  388. u8 wdata[2] = { address, data };
  389. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  390. .wdata = wdata, .rdata = NULL,
  391. .wlen = 2, .rlen = 0};
  392. return acpi_ec_transaction(ec, &t);
  393. }
  394. int ec_read(u8 addr, u8 *val)
  395. {
  396. int err;
  397. u8 temp_data;
  398. if (!first_ec)
  399. return -ENODEV;
  400. err = acpi_ec_read(first_ec, addr, &temp_data);
  401. if (!err) {
  402. *val = temp_data;
  403. return 0;
  404. }
  405. return err;
  406. }
  407. EXPORT_SYMBOL(ec_read);
  408. int ec_write(u8 addr, u8 val)
  409. {
  410. int err;
  411. if (!first_ec)
  412. return -ENODEV;
  413. err = acpi_ec_write(first_ec, addr, val);
  414. return err;
  415. }
  416. EXPORT_SYMBOL(ec_write);
  417. int ec_transaction(u8 command,
  418. const u8 *wdata, unsigned wdata_len,
  419. u8 *rdata, unsigned rdata_len)
  420. {
  421. struct transaction t = {.command = command,
  422. .wdata = wdata, .rdata = rdata,
  423. .wlen = wdata_len, .rlen = rdata_len};
  424. if (!first_ec)
  425. return -ENODEV;
  426. return acpi_ec_transaction(first_ec, &t);
  427. }
  428. EXPORT_SYMBOL(ec_transaction);
  429. /* Get the handle to the EC device */
  430. acpi_handle ec_get_handle(void)
  431. {
  432. if (!first_ec)
  433. return NULL;
  434. return first_ec->handle;
  435. }
  436. EXPORT_SYMBOL(ec_get_handle);
  437. /*
  438. * Process _Q events that might have accumulated in the EC.
  439. * Run with locked ec mutex.
  440. */
  441. static void acpi_ec_clear(struct acpi_ec *ec)
  442. {
  443. int i, status;
  444. u8 value = 0;
  445. for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
  446. status = acpi_ec_sync_query(ec, &value);
  447. if (status || !value)
  448. break;
  449. }
  450. if (unlikely(i == ACPI_EC_CLEAR_MAX))
  451. pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
  452. else
  453. pr_info("%d stale EC events cleared\n", i);
  454. }
  455. void acpi_ec_block_transactions(void)
  456. {
  457. struct acpi_ec *ec = first_ec;
  458. if (!ec)
  459. return;
  460. mutex_lock(&ec->mutex);
  461. /* Prevent transactions from being carried out */
  462. set_bit(EC_FLAGS_BLOCKED, &ec->flags);
  463. mutex_unlock(&ec->mutex);
  464. }
  465. void acpi_ec_unblock_transactions(void)
  466. {
  467. struct acpi_ec *ec = first_ec;
  468. if (!ec)
  469. return;
  470. mutex_lock(&ec->mutex);
  471. /* Allow transactions to be carried out again */
  472. clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
  473. if (EC_FLAGS_CLEAR_ON_RESUME)
  474. acpi_ec_clear(ec);
  475. mutex_unlock(&ec->mutex);
  476. }
  477. void acpi_ec_unblock_transactions_early(void)
  478. {
  479. /*
  480. * Allow transactions to happen again (this function is called from
  481. * atomic context during wakeup, so we don't need to acquire the mutex).
  482. */
  483. if (first_ec)
  484. clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
  485. }
  486. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 *data)
  487. {
  488. int result;
  489. u8 d;
  490. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  491. .wdata = NULL, .rdata = &d,
  492. .wlen = 0, .rlen = 1};
  493. if (!ec || !data)
  494. return -EINVAL;
  495. /*
  496. * Query the EC to find out which _Qxx method we need to evaluate.
  497. * Note that successful completion of the query causes the ACPI_EC_SCI
  498. * bit to be cleared (and thus clearing the interrupt source).
  499. */
  500. result = acpi_ec_transaction_unlocked(ec, &t);
  501. if (result)
  502. return result;
  503. if (!d)
  504. return -ENODATA;
  505. *data = d;
  506. return 0;
  507. }
  508. /* --------------------------------------------------------------------------
  509. Event Management
  510. -------------------------------------------------------------------------- */
  511. static struct acpi_ec_query_handler *
  512. acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
  513. {
  514. if (handler)
  515. kref_get(&handler->kref);
  516. return handler;
  517. }
  518. static void acpi_ec_query_handler_release(struct kref *kref)
  519. {
  520. struct acpi_ec_query_handler *handler =
  521. container_of(kref, struct acpi_ec_query_handler, kref);
  522. kfree(handler);
  523. }
  524. static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
  525. {
  526. kref_put(&handler->kref, acpi_ec_query_handler_release);
  527. }
  528. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  529. acpi_handle handle, acpi_ec_query_func func,
  530. void *data)
  531. {
  532. struct acpi_ec_query_handler *handler =
  533. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  534. if (!handler)
  535. return -ENOMEM;
  536. handler->query_bit = query_bit;
  537. handler->handle = handle;
  538. handler->func = func;
  539. handler->data = data;
  540. mutex_lock(&ec->mutex);
  541. kref_init(&handler->kref);
  542. list_add(&handler->node, &ec->list);
  543. mutex_unlock(&ec->mutex);
  544. return 0;
  545. }
  546. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  547. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  548. {
  549. struct acpi_ec_query_handler *handler, *tmp;
  550. LIST_HEAD(free_list);
  551. mutex_lock(&ec->mutex);
  552. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  553. if (query_bit == handler->query_bit) {
  554. list_del_init(&handler->node);
  555. list_add(&handler->node, &free_list);
  556. }
  557. }
  558. mutex_unlock(&ec->mutex);
  559. list_for_each_entry(handler, &free_list, node)
  560. acpi_ec_put_query_handler(handler);
  561. }
  562. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  563. static void acpi_ec_run(void *cxt)
  564. {
  565. struct acpi_ec_query_handler *handler = cxt;
  566. if (!handler)
  567. return;
  568. pr_debug("##### Query(0x%02x) started #####\n", handler->query_bit);
  569. if (handler->func)
  570. handler->func(handler->data);
  571. else if (handler->handle)
  572. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  573. pr_debug("##### Query(0x%02x) stopped #####\n", handler->query_bit);
  574. acpi_ec_put_query_handler(handler);
  575. }
  576. static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data)
  577. {
  578. u8 value = 0;
  579. int status;
  580. struct acpi_ec_query_handler *handler;
  581. status = acpi_ec_query_unlocked(ec, &value);
  582. if (data)
  583. *data = value;
  584. if (status)
  585. return status;
  586. list_for_each_entry(handler, &ec->list, node) {
  587. if (value == handler->query_bit) {
  588. /* have custom handler for this bit */
  589. handler = acpi_ec_get_query_handler(handler);
  590. pr_debug("##### Query(0x%02x) scheduled #####\n",
  591. handler->query_bit);
  592. return acpi_os_execute((handler->func) ?
  593. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  594. acpi_ec_run, handler);
  595. }
  596. }
  597. return 0;
  598. }
  599. static void acpi_ec_gpe_query(void *ec_cxt)
  600. {
  601. struct acpi_ec *ec = ec_cxt;
  602. if (!ec)
  603. return;
  604. mutex_lock(&ec->mutex);
  605. acpi_ec_sync_query(ec, NULL);
  606. mutex_unlock(&ec->mutex);
  607. }
  608. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  609. {
  610. if (state & ACPI_EC_FLAG_SCI) {
  611. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  612. pr_debug("***** Event started *****\n");
  613. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  614. acpi_ec_gpe_query, ec);
  615. }
  616. }
  617. return 0;
  618. }
  619. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  620. u32 gpe_number, void *data)
  621. {
  622. unsigned long flags;
  623. struct acpi_ec *ec = data;
  624. spin_lock_irqsave(&ec->lock, flags);
  625. advance_transaction(ec);
  626. spin_unlock_irqrestore(&ec->lock, flags);
  627. ec_check_sci(ec, acpi_ec_read_status(ec));
  628. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  629. }
  630. /* --------------------------------------------------------------------------
  631. * Address Space Management
  632. * -------------------------------------------------------------------------- */
  633. static acpi_status
  634. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  635. u32 bits, u64 *value64,
  636. void *handler_context, void *region_context)
  637. {
  638. struct acpi_ec *ec = handler_context;
  639. int result = 0, i, bytes = bits / 8;
  640. u8 *value = (u8 *)value64;
  641. if ((address > 0xFF) || !value || !handler_context)
  642. return AE_BAD_PARAMETER;
  643. if (function != ACPI_READ && function != ACPI_WRITE)
  644. return AE_BAD_PARAMETER;
  645. if (EC_FLAGS_MSI || bits > 8)
  646. acpi_ec_burst_enable(ec);
  647. for (i = 0; i < bytes; ++i, ++address, ++value)
  648. result = (function == ACPI_READ) ?
  649. acpi_ec_read(ec, address, value) :
  650. acpi_ec_write(ec, address, *value);
  651. if (EC_FLAGS_MSI || bits > 8)
  652. acpi_ec_burst_disable(ec);
  653. switch (result) {
  654. case -EINVAL:
  655. return AE_BAD_PARAMETER;
  656. case -ENODEV:
  657. return AE_NOT_FOUND;
  658. case -ETIME:
  659. return AE_TIME;
  660. default:
  661. return AE_OK;
  662. }
  663. }
  664. /* --------------------------------------------------------------------------
  665. * Driver Interface
  666. * -------------------------------------------------------------------------- */
  667. static acpi_status
  668. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  669. static struct acpi_ec *make_acpi_ec(void)
  670. {
  671. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  672. if (!ec)
  673. return NULL;
  674. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  675. mutex_init(&ec->mutex);
  676. init_waitqueue_head(&ec->wait);
  677. INIT_LIST_HEAD(&ec->list);
  678. spin_lock_init(&ec->lock);
  679. return ec;
  680. }
  681. static acpi_status
  682. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  683. void *context, void **return_value)
  684. {
  685. char node_name[5];
  686. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  687. struct acpi_ec *ec = context;
  688. int value = 0;
  689. acpi_status status;
  690. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  691. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
  692. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  693. return AE_OK;
  694. }
  695. static acpi_status
  696. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  697. {
  698. acpi_status status;
  699. unsigned long long tmp = 0;
  700. struct acpi_ec *ec = context;
  701. /* clear addr values, ec_parse_io_ports depend on it */
  702. ec->command_addr = ec->data_addr = 0;
  703. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  704. ec_parse_io_ports, ec);
  705. if (ACPI_FAILURE(status))
  706. return status;
  707. /* Get GPE bit assignment (EC events). */
  708. /* TODO: Add support for _GPE returning a package */
  709. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  710. if (ACPI_FAILURE(status))
  711. return status;
  712. ec->gpe = tmp;
  713. /* Use the global lock for all EC transactions? */
  714. tmp = 0;
  715. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  716. ec->global_lock = tmp;
  717. ec->handle = handle;
  718. return AE_CTRL_TERMINATE;
  719. }
  720. static int ec_install_handlers(struct acpi_ec *ec)
  721. {
  722. acpi_status status;
  723. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  724. return 0;
  725. status = acpi_install_gpe_handler(NULL, ec->gpe,
  726. ACPI_GPE_EDGE_TRIGGERED,
  727. &acpi_ec_gpe_handler, ec);
  728. if (ACPI_FAILURE(status))
  729. return -ENODEV;
  730. acpi_enable_gpe(NULL, ec->gpe);
  731. status = acpi_install_address_space_handler(ec->handle,
  732. ACPI_ADR_SPACE_EC,
  733. &acpi_ec_space_handler,
  734. NULL, ec);
  735. if (ACPI_FAILURE(status)) {
  736. if (status == AE_NOT_FOUND) {
  737. /*
  738. * Maybe OS fails in evaluating the _REG object.
  739. * The AE_NOT_FOUND error will be ignored and OS
  740. * continue to initialize EC.
  741. */
  742. pr_err("Fail in evaluating the _REG object"
  743. " of EC device. Broken bios is suspected.\n");
  744. } else {
  745. acpi_disable_gpe(NULL, ec->gpe);
  746. acpi_remove_gpe_handler(NULL, ec->gpe,
  747. &acpi_ec_gpe_handler);
  748. return -ENODEV;
  749. }
  750. }
  751. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  752. return 0;
  753. }
  754. static void ec_remove_handlers(struct acpi_ec *ec)
  755. {
  756. if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  757. return;
  758. acpi_disable_gpe(NULL, ec->gpe);
  759. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  760. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  761. pr_err("failed to remove space handler\n");
  762. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  763. &acpi_ec_gpe_handler)))
  764. pr_err("failed to remove gpe handler\n");
  765. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  766. }
  767. static int acpi_ec_add(struct acpi_device *device)
  768. {
  769. struct acpi_ec *ec = NULL;
  770. int ret;
  771. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  772. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  773. /* Check for boot EC */
  774. if (boot_ec &&
  775. (boot_ec->handle == device->handle ||
  776. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  777. ec = boot_ec;
  778. boot_ec = NULL;
  779. } else {
  780. ec = make_acpi_ec();
  781. if (!ec)
  782. return -ENOMEM;
  783. }
  784. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  785. AE_CTRL_TERMINATE) {
  786. kfree(ec);
  787. return -EINVAL;
  788. }
  789. /* Find and register all query methods */
  790. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  791. acpi_ec_register_query_methods, NULL, ec, NULL);
  792. if (!first_ec)
  793. first_ec = ec;
  794. device->driver_data = ec;
  795. ret = !!request_region(ec->data_addr, 1, "EC data");
  796. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  797. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  798. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  799. pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  800. ec->gpe, ec->command_addr, ec->data_addr);
  801. ret = ec_install_handlers(ec);
  802. /* EC is fully operational, allow queries */
  803. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  804. /* Clear stale _Q events if hardware might require that */
  805. if (EC_FLAGS_CLEAR_ON_RESUME) {
  806. mutex_lock(&ec->mutex);
  807. acpi_ec_clear(ec);
  808. mutex_unlock(&ec->mutex);
  809. }
  810. return ret;
  811. }
  812. static int acpi_ec_remove(struct acpi_device *device)
  813. {
  814. struct acpi_ec *ec;
  815. struct acpi_ec_query_handler *handler, *tmp;
  816. if (!device)
  817. return -EINVAL;
  818. ec = acpi_driver_data(device);
  819. ec_remove_handlers(ec);
  820. mutex_lock(&ec->mutex);
  821. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  822. list_del(&handler->node);
  823. kfree(handler);
  824. }
  825. mutex_unlock(&ec->mutex);
  826. release_region(ec->data_addr, 1);
  827. release_region(ec->command_addr, 1);
  828. device->driver_data = NULL;
  829. if (ec == first_ec)
  830. first_ec = NULL;
  831. kfree(ec);
  832. return 0;
  833. }
  834. static acpi_status
  835. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  836. {
  837. struct acpi_ec *ec = context;
  838. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  839. return AE_OK;
  840. /*
  841. * The first address region returned is the data port, and
  842. * the second address region returned is the status/command
  843. * port.
  844. */
  845. if (ec->data_addr == 0)
  846. ec->data_addr = resource->data.io.minimum;
  847. else if (ec->command_addr == 0)
  848. ec->command_addr = resource->data.io.minimum;
  849. else
  850. return AE_CTRL_TERMINATE;
  851. return AE_OK;
  852. }
  853. int __init acpi_boot_ec_enable(void)
  854. {
  855. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  856. return 0;
  857. if (!ec_install_handlers(boot_ec)) {
  858. first_ec = boot_ec;
  859. return 0;
  860. }
  861. return -EFAULT;
  862. }
  863. static const struct acpi_device_id ec_device_ids[] = {
  864. {"PNP0C09", 0},
  865. {"", 0},
  866. };
  867. /* Some BIOS do not survive early DSDT scan, skip it */
  868. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  869. {
  870. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  871. return 0;
  872. }
  873. /* ASUStek often supplies us with broken ECDT, validate it */
  874. static int ec_validate_ecdt(const struct dmi_system_id *id)
  875. {
  876. EC_FLAGS_VALIDATE_ECDT = 1;
  877. return 0;
  878. }
  879. /* MSI EC needs special treatment, enable it */
  880. static int ec_flag_msi(const struct dmi_system_id *id)
  881. {
  882. pr_debug("Detected MSI hardware, enabling workarounds.\n");
  883. EC_FLAGS_MSI = 1;
  884. EC_FLAGS_VALIDATE_ECDT = 1;
  885. return 0;
  886. }
  887. /*
  888. * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
  889. * the GPE storm threshold back to 20
  890. */
  891. static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
  892. {
  893. pr_debug("Setting the EC GPE storm threshold to 20\n");
  894. ec_storm_threshold = 20;
  895. return 0;
  896. }
  897. /*
  898. * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
  899. * which case, we complete the QR_EC without issuing it to the firmware.
  900. * https://bugzilla.kernel.org/show_bug.cgi?id=86211
  901. */
  902. static int ec_flag_query_handshake(const struct dmi_system_id *id)
  903. {
  904. pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
  905. EC_FLAGS_QUERY_HANDSHAKE = 1;
  906. return 0;
  907. }
  908. /*
  909. * On some hardware it is necessary to clear events accumulated by the EC during
  910. * sleep. These ECs stop reporting GPEs until they are manually polled, if too
  911. * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
  912. *
  913. * https://bugzilla.kernel.org/show_bug.cgi?id=44161
  914. *
  915. * Ideally, the EC should also be instructed NOT to accumulate events during
  916. * sleep (which Windows seems to do somehow), but the interface to control this
  917. * behaviour is not known at this time.
  918. *
  919. * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
  920. * however it is very likely that other Samsung models are affected.
  921. *
  922. * On systems which don't accumulate _Q events during sleep, this extra check
  923. * should be harmless.
  924. */
  925. static int ec_clear_on_resume(const struct dmi_system_id *id)
  926. {
  927. pr_debug("Detected system needing EC poll on resume.\n");
  928. EC_FLAGS_CLEAR_ON_RESUME = 1;
  929. return 0;
  930. }
  931. static struct dmi_system_id ec_dmi_table[] __initdata = {
  932. {
  933. ec_skip_dsdt_scan, "Compal JFL92", {
  934. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  935. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  936. {
  937. ec_flag_msi, "MSI hardware", {
  938. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  939. {
  940. ec_flag_msi, "MSI hardware", {
  941. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  942. {
  943. ec_flag_msi, "MSI hardware", {
  944. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  945. {
  946. ec_flag_msi, "MSI hardware", {
  947. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
  948. {
  949. ec_flag_msi, "Quanta hardware", {
  950. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  951. DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
  952. {
  953. ec_flag_msi, "Quanta hardware", {
  954. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  955. DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
  956. {
  957. ec_flag_msi, "Clevo W350etq", {
  958. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
  959. DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
  960. {
  961. ec_validate_ecdt, "ASUS hardware", {
  962. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  963. {
  964. ec_validate_ecdt, "ASUS hardware", {
  965. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
  966. {
  967. ec_enlarge_storm_threshold, "CLEVO hardware", {
  968. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  969. DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
  970. {
  971. ec_skip_dsdt_scan, "HP Folio 13", {
  972. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  973. DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
  974. {
  975. ec_validate_ecdt, "ASUS hardware", {
  976. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
  977. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
  978. {
  979. ec_clear_on_resume, "Samsung hardware", {
  980. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
  981. {
  982. ec_flag_query_handshake, "Acer hardware", {
  983. DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
  984. {},
  985. };
  986. int __init acpi_ec_ecdt_probe(void)
  987. {
  988. acpi_status status;
  989. struct acpi_ec *saved_ec = NULL;
  990. struct acpi_table_ecdt *ecdt_ptr;
  991. boot_ec = make_acpi_ec();
  992. if (!boot_ec)
  993. return -ENOMEM;
  994. /*
  995. * Generate a boot ec context
  996. */
  997. dmi_check_system(ec_dmi_table);
  998. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  999. (struct acpi_table_header **)&ecdt_ptr);
  1000. if (ACPI_SUCCESS(status)) {
  1001. pr_info("EC description table is found, configuring boot EC\n");
  1002. boot_ec->command_addr = ecdt_ptr->control.address;
  1003. boot_ec->data_addr = ecdt_ptr->data.address;
  1004. boot_ec->gpe = ecdt_ptr->gpe;
  1005. boot_ec->handle = ACPI_ROOT_OBJECT;
  1006. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id,
  1007. &boot_ec->handle);
  1008. /* Don't trust ECDT, which comes from ASUSTek */
  1009. if (!EC_FLAGS_VALIDATE_ECDT)
  1010. goto install;
  1011. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  1012. if (!saved_ec)
  1013. return -ENOMEM;
  1014. /* fall through */
  1015. }
  1016. if (EC_FLAGS_SKIP_DSDT_SCAN) {
  1017. kfree(saved_ec);
  1018. return -ENODEV;
  1019. }
  1020. /* This workaround is needed only on some broken machines,
  1021. * which require early EC, but fail to provide ECDT */
  1022. pr_debug("Look up EC in DSDT\n");
  1023. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  1024. boot_ec, NULL);
  1025. /* Check that acpi_get_devices actually find something */
  1026. if (ACPI_FAILURE(status) || !boot_ec->handle)
  1027. goto error;
  1028. if (saved_ec) {
  1029. /* try to find good ECDT from ASUSTek */
  1030. if (saved_ec->command_addr != boot_ec->command_addr ||
  1031. saved_ec->data_addr != boot_ec->data_addr ||
  1032. saved_ec->gpe != boot_ec->gpe ||
  1033. saved_ec->handle != boot_ec->handle)
  1034. pr_info("ASUSTek keeps feeding us with broken "
  1035. "ECDT tables, which are very hard to workaround. "
  1036. "Trying to use DSDT EC info instead. Please send "
  1037. "output of acpidump to linux-acpi@vger.kernel.org\n");
  1038. kfree(saved_ec);
  1039. saved_ec = NULL;
  1040. } else {
  1041. /* We really need to limit this workaround, the only ASUS,
  1042. * which needs it, has fake EC._INI method, so use it as flag.
  1043. * Keep boot_ec struct as it will be needed soon.
  1044. */
  1045. if (!dmi_name_in_vendors("ASUS") ||
  1046. !acpi_has_method(boot_ec->handle, "_INI"))
  1047. return -ENODEV;
  1048. }
  1049. install:
  1050. if (!ec_install_handlers(boot_ec)) {
  1051. first_ec = boot_ec;
  1052. return 0;
  1053. }
  1054. error:
  1055. kfree(boot_ec);
  1056. kfree(saved_ec);
  1057. boot_ec = NULL;
  1058. return -ENODEV;
  1059. }
  1060. static struct acpi_driver acpi_ec_driver = {
  1061. .name = "ec",
  1062. .class = ACPI_EC_CLASS,
  1063. .ids = ec_device_ids,
  1064. .ops = {
  1065. .add = acpi_ec_add,
  1066. .remove = acpi_ec_remove,
  1067. },
  1068. };
  1069. int __init acpi_ec_init(void)
  1070. {
  1071. int result = 0;
  1072. /* Now register the driver for the EC */
  1073. result = acpi_bus_register_driver(&acpi_ec_driver);
  1074. if (result < 0)
  1075. return -ENODEV;
  1076. return result;
  1077. }
  1078. /* EC driver currently not unloadable */
  1079. #if 0
  1080. static void __exit acpi_ec_exit(void)
  1081. {
  1082. acpi_bus_unregister_driver(&acpi_ec_driver);
  1083. }
  1084. #endif /* 0 */