ec.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v3)
  3. *
  4. * Copyright (C) 2001-2015 Intel Corporation
  5. * Author: 2014, 2015 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_UDELAY_POLL 1000 /* Wait 1ms for EC transaction polling */
  68. #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
  69. * when trying to clear the EC */
  70. enum {
  71. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  72. EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
  73. * OpReg are installed */
  74. EC_FLAGS_STARTED, /* Driver is started */
  75. EC_FLAGS_STOPPED, /* Driver is stopped */
  76. EC_FLAGS_COMMAND_STORM, /* GPE storms occurred to the
  77. * current command processing */
  78. };
  79. #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
  80. #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
  81. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  82. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  83. module_param(ec_delay, uint, 0644);
  84. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  85. /*
  86. * If the number of false interrupts per one transaction exceeds
  87. * this threshold, will think there is a GPE storm happened and
  88. * will disable the GPE for normal transaction.
  89. */
  90. static unsigned int ec_storm_threshold __read_mostly = 8;
  91. module_param(ec_storm_threshold, uint, 0644);
  92. MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
  93. struct acpi_ec_query_handler {
  94. struct list_head node;
  95. acpi_ec_query_func func;
  96. acpi_handle handle;
  97. void *data;
  98. u8 query_bit;
  99. struct kref kref;
  100. };
  101. struct transaction {
  102. const u8 *wdata;
  103. u8 *rdata;
  104. unsigned short irq_count;
  105. u8 command;
  106. u8 wi;
  107. u8 ri;
  108. u8 wlen;
  109. u8 rlen;
  110. u8 flags;
  111. unsigned long timestamp;
  112. };
  113. static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
  114. static void advance_transaction(struct acpi_ec *ec);
  115. struct acpi_ec *boot_ec, *first_ec;
  116. EXPORT_SYMBOL(first_ec);
  117. static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
  118. static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
  119. static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
  120. static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
  121. static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
  122. /* --------------------------------------------------------------------------
  123. * Logging/Debugging
  124. * -------------------------------------------------------------------------- */
  125. /*
  126. * Splitters used by the developers to track the boundary of the EC
  127. * handling processes.
  128. */
  129. #ifdef DEBUG
  130. #define EC_DBG_SEP " "
  131. #define EC_DBG_DRV "+++++"
  132. #define EC_DBG_STM "====="
  133. #define EC_DBG_REQ "*****"
  134. #define EC_DBG_EVT "#####"
  135. #else
  136. #define EC_DBG_SEP ""
  137. #define EC_DBG_DRV
  138. #define EC_DBG_STM
  139. #define EC_DBG_REQ
  140. #define EC_DBG_EVT
  141. #endif
  142. #define ec_log_raw(fmt, ...) \
  143. pr_info(fmt "\n", ##__VA_ARGS__)
  144. #define ec_dbg_raw(fmt, ...) \
  145. pr_debug(fmt "\n", ##__VA_ARGS__)
  146. #define ec_log(filter, fmt, ...) \
  147. ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
  148. #define ec_dbg(filter, fmt, ...) \
  149. ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
  150. #define ec_log_drv(fmt, ...) \
  151. ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__)
  152. #define ec_dbg_drv(fmt, ...) \
  153. ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__)
  154. #define ec_dbg_stm(fmt, ...) \
  155. ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__)
  156. #define ec_dbg_req(fmt, ...) \
  157. ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__)
  158. #define ec_dbg_evt(fmt, ...) \
  159. ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__)
  160. #define ec_dbg_ref(ec, fmt, ...) \
  161. ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
  162. /* --------------------------------------------------------------------------
  163. * Device Flags
  164. * -------------------------------------------------------------------------- */
  165. static bool acpi_ec_started(struct acpi_ec *ec)
  166. {
  167. return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  168. !test_bit(EC_FLAGS_STOPPED, &ec->flags);
  169. }
  170. static bool acpi_ec_flushed(struct acpi_ec *ec)
  171. {
  172. return ec->reference_count == 1;
  173. }
  174. /* --------------------------------------------------------------------------
  175. * EC Registers
  176. * -------------------------------------------------------------------------- */
  177. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  178. {
  179. u8 x = inb(ec->command_addr);
  180. ec_dbg_raw("EC_SC(R) = 0x%2.2x "
  181. "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d",
  182. x,
  183. !!(x & ACPI_EC_FLAG_SCI),
  184. !!(x & ACPI_EC_FLAG_BURST),
  185. !!(x & ACPI_EC_FLAG_CMD),
  186. !!(x & ACPI_EC_FLAG_IBF),
  187. !!(x & ACPI_EC_FLAG_OBF));
  188. return x;
  189. }
  190. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  191. {
  192. u8 x = inb(ec->data_addr);
  193. ec->curr->timestamp = jiffies;
  194. ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x);
  195. return x;
  196. }
  197. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  198. {
  199. ec_dbg_raw("EC_SC(W) = 0x%2.2x", command);
  200. outb(command, ec->command_addr);
  201. ec->curr->timestamp = jiffies;
  202. }
  203. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  204. {
  205. ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data);
  206. outb(data, ec->data_addr);
  207. ec->curr->timestamp = jiffies;
  208. }
  209. #ifdef DEBUG
  210. static const char *acpi_ec_cmd_string(u8 cmd)
  211. {
  212. switch (cmd) {
  213. case 0x80:
  214. return "RD_EC";
  215. case 0x81:
  216. return "WR_EC";
  217. case 0x82:
  218. return "BE_EC";
  219. case 0x83:
  220. return "BD_EC";
  221. case 0x84:
  222. return "QR_EC";
  223. }
  224. return "UNKNOWN";
  225. }
  226. #else
  227. #define acpi_ec_cmd_string(cmd) "UNDEF"
  228. #endif
  229. /* --------------------------------------------------------------------------
  230. * GPE Registers
  231. * -------------------------------------------------------------------------- */
  232. static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
  233. {
  234. acpi_event_status gpe_status = 0;
  235. (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
  236. return (gpe_status & ACPI_EVENT_FLAG_SET) ? true : false;
  237. }
  238. static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
  239. {
  240. if (open)
  241. acpi_enable_gpe(NULL, ec->gpe);
  242. else {
  243. BUG_ON(ec->reference_count < 1);
  244. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  245. }
  246. if (acpi_ec_is_gpe_raised(ec)) {
  247. /*
  248. * On some platforms, EN=1 writes cannot trigger GPE. So
  249. * software need to manually trigger a pseudo GPE event on
  250. * EN=1 writes.
  251. */
  252. ec_dbg_raw("Polling quirk");
  253. advance_transaction(ec);
  254. }
  255. }
  256. static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
  257. {
  258. if (close)
  259. acpi_disable_gpe(NULL, ec->gpe);
  260. else {
  261. BUG_ON(ec->reference_count < 1);
  262. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  263. }
  264. }
  265. static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
  266. {
  267. /*
  268. * GPE STS is a W1C register, which means:
  269. * 1. Software can clear it without worrying about clearing other
  270. * GPEs' STS bits when the hardware sets them in parallel.
  271. * 2. As long as software can ensure only clearing it when it is
  272. * set, hardware won't set it in parallel.
  273. * So software can clear GPE in any contexts.
  274. * Warning: do not move the check into advance_transaction() as the
  275. * EC commands will be sent without GPE raised.
  276. */
  277. if (!acpi_ec_is_gpe_raised(ec))
  278. return;
  279. acpi_clear_gpe(NULL, ec->gpe);
  280. }
  281. /* --------------------------------------------------------------------------
  282. * Transaction Management
  283. * -------------------------------------------------------------------------- */
  284. static void acpi_ec_submit_request(struct acpi_ec *ec)
  285. {
  286. ec->reference_count++;
  287. if (ec->reference_count == 1)
  288. acpi_ec_enable_gpe(ec, true);
  289. }
  290. static void acpi_ec_complete_request(struct acpi_ec *ec)
  291. {
  292. bool flushed = false;
  293. ec->reference_count--;
  294. if (ec->reference_count == 0)
  295. acpi_ec_disable_gpe(ec, true);
  296. flushed = acpi_ec_flushed(ec);
  297. if (flushed)
  298. wake_up(&ec->wait);
  299. }
  300. static void acpi_ec_set_storm(struct acpi_ec *ec, u8 flag)
  301. {
  302. if (!test_bit(flag, &ec->flags)) {
  303. acpi_ec_disable_gpe(ec, false);
  304. ec_dbg_drv("Polling enabled");
  305. set_bit(flag, &ec->flags);
  306. }
  307. }
  308. static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
  309. {
  310. if (test_bit(flag, &ec->flags)) {
  311. clear_bit(flag, &ec->flags);
  312. acpi_ec_enable_gpe(ec, false);
  313. ec_dbg_drv("Polling disabled");
  314. }
  315. }
  316. /*
  317. * acpi_ec_submit_flushable_request() - Increase the reference count unless
  318. * the flush operation is not in
  319. * progress
  320. * @ec: the EC device
  321. *
  322. * This function must be used before taking a new action that should hold
  323. * the reference count. If this function returns false, then the action
  324. * must be discarded or it will prevent the flush operation from being
  325. * completed.
  326. */
  327. static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec)
  328. {
  329. if (!acpi_ec_started(ec))
  330. return false;
  331. acpi_ec_submit_request(ec);
  332. return true;
  333. }
  334. static void acpi_ec_submit_query(struct acpi_ec *ec)
  335. {
  336. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  337. ec_dbg_req("Event started");
  338. schedule_work(&ec->work);
  339. }
  340. }
  341. static void acpi_ec_complete_query(struct acpi_ec *ec)
  342. {
  343. if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
  344. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  345. ec_dbg_req("Event stopped");
  346. }
  347. }
  348. static int ec_transaction_completed(struct acpi_ec *ec)
  349. {
  350. unsigned long flags;
  351. int ret = 0;
  352. spin_lock_irqsave(&ec->lock, flags);
  353. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
  354. ret = 1;
  355. spin_unlock_irqrestore(&ec->lock, flags);
  356. return ret;
  357. }
  358. static void advance_transaction(struct acpi_ec *ec)
  359. {
  360. struct transaction *t;
  361. u8 status;
  362. bool wakeup = false;
  363. ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
  364. smp_processor_id());
  365. /*
  366. * By always clearing STS before handling all indications, we can
  367. * ensure a hardware STS 0->1 change after this clearing can always
  368. * trigger a GPE interrupt.
  369. */
  370. acpi_ec_clear_gpe(ec);
  371. status = acpi_ec_read_status(ec);
  372. t = ec->curr;
  373. if (!t)
  374. goto err;
  375. if (t->flags & ACPI_EC_COMMAND_POLL) {
  376. if (t->wlen > t->wi) {
  377. if ((status & ACPI_EC_FLAG_IBF) == 0)
  378. acpi_ec_write_data(ec, t->wdata[t->wi++]);
  379. else
  380. goto err;
  381. } else if (t->rlen > t->ri) {
  382. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  383. t->rdata[t->ri++] = acpi_ec_read_data(ec);
  384. if (t->rlen == t->ri) {
  385. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  386. if (t->command == ACPI_EC_COMMAND_QUERY)
  387. ec_dbg_req("Command(%s) hardware completion",
  388. acpi_ec_cmd_string(t->command));
  389. wakeup = true;
  390. }
  391. } else
  392. goto err;
  393. } else if (t->wlen == t->wi &&
  394. (status & ACPI_EC_FLAG_IBF) == 0) {
  395. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  396. wakeup = true;
  397. }
  398. goto out;
  399. } else {
  400. if (EC_FLAGS_QUERY_HANDSHAKE &&
  401. !(status & ACPI_EC_FLAG_SCI) &&
  402. (t->command == ACPI_EC_COMMAND_QUERY)) {
  403. t->flags |= ACPI_EC_COMMAND_POLL;
  404. acpi_ec_complete_query(ec);
  405. t->rdata[t->ri++] = 0x00;
  406. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  407. ec_dbg_req("Command(%s) software completion",
  408. acpi_ec_cmd_string(t->command));
  409. wakeup = true;
  410. } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
  411. acpi_ec_write_cmd(ec, t->command);
  412. t->flags |= ACPI_EC_COMMAND_POLL;
  413. acpi_ec_complete_query(ec);
  414. } else
  415. goto err;
  416. goto out;
  417. }
  418. err:
  419. /*
  420. * If SCI bit is set, then don't think it's a false IRQ
  421. * otherwise will take a not handled IRQ as a false one.
  422. */
  423. if (!(status & ACPI_EC_FLAG_SCI)) {
  424. if (in_interrupt() && t) {
  425. if (t->irq_count < ec_storm_threshold)
  426. ++t->irq_count;
  427. /* Allow triggering on 0 threshold */
  428. if (t->irq_count == ec_storm_threshold)
  429. acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
  430. }
  431. }
  432. out:
  433. if (status & ACPI_EC_FLAG_SCI)
  434. acpi_ec_submit_query(ec);
  435. if (wakeup && in_interrupt())
  436. wake_up(&ec->wait);
  437. }
  438. static void start_transaction(struct acpi_ec *ec)
  439. {
  440. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  441. ec->curr->flags = 0;
  442. ec->curr->timestamp = jiffies;
  443. advance_transaction(ec);
  444. }
  445. static int ec_poll(struct acpi_ec *ec)
  446. {
  447. unsigned long flags;
  448. int repeat = 5; /* number of command restarts */
  449. while (repeat--) {
  450. unsigned long delay = jiffies +
  451. msecs_to_jiffies(ec_delay);
  452. unsigned long usecs = ACPI_EC_UDELAY_POLL;
  453. do {
  454. /* don't sleep with disabled interrupts */
  455. if (EC_FLAGS_MSI || irqs_disabled()) {
  456. usecs = ACPI_EC_MSI_UDELAY;
  457. udelay(usecs);
  458. if (ec_transaction_completed(ec))
  459. return 0;
  460. } else {
  461. if (wait_event_timeout(ec->wait,
  462. ec_transaction_completed(ec),
  463. usecs_to_jiffies(usecs)))
  464. return 0;
  465. }
  466. spin_lock_irqsave(&ec->lock, flags);
  467. if (time_after(jiffies,
  468. ec->curr->timestamp +
  469. usecs_to_jiffies(usecs)))
  470. advance_transaction(ec);
  471. spin_unlock_irqrestore(&ec->lock, flags);
  472. } while (time_before(jiffies, delay));
  473. pr_debug("controller reset, restart transaction\n");
  474. spin_lock_irqsave(&ec->lock, flags);
  475. start_transaction(ec);
  476. spin_unlock_irqrestore(&ec->lock, flags);
  477. }
  478. return -ETIME;
  479. }
  480. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  481. struct transaction *t)
  482. {
  483. unsigned long tmp;
  484. int ret = 0;
  485. if (EC_FLAGS_MSI)
  486. udelay(ACPI_EC_MSI_UDELAY);
  487. /* start transaction */
  488. spin_lock_irqsave(&ec->lock, tmp);
  489. /* Enable GPE for command processing (IBF=0/OBF=1) */
  490. if (!acpi_ec_submit_flushable_request(ec)) {
  491. ret = -EINVAL;
  492. goto unlock;
  493. }
  494. ec_dbg_ref(ec, "Increase command");
  495. /* following two actions should be kept atomic */
  496. ec->curr = t;
  497. ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
  498. start_transaction(ec);
  499. spin_unlock_irqrestore(&ec->lock, tmp);
  500. ret = ec_poll(ec);
  501. spin_lock_irqsave(&ec->lock, tmp);
  502. if (t->irq_count == ec_storm_threshold)
  503. acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
  504. ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
  505. ec->curr = NULL;
  506. /* Disable GPE for command processing (IBF=0/OBF=1) */
  507. acpi_ec_complete_request(ec);
  508. ec_dbg_ref(ec, "Decrease command");
  509. unlock:
  510. spin_unlock_irqrestore(&ec->lock, tmp);
  511. return ret;
  512. }
  513. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  514. {
  515. int status;
  516. u32 glk;
  517. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  518. return -EINVAL;
  519. if (t->rdata)
  520. memset(t->rdata, 0, t->rlen);
  521. mutex_lock(&ec->mutex);
  522. if (ec->global_lock) {
  523. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  524. if (ACPI_FAILURE(status)) {
  525. status = -ENODEV;
  526. goto unlock;
  527. }
  528. }
  529. status = acpi_ec_transaction_unlocked(ec, t);
  530. if (test_bit(EC_FLAGS_COMMAND_STORM, &ec->flags))
  531. msleep(1);
  532. if (ec->global_lock)
  533. acpi_release_global_lock(glk);
  534. unlock:
  535. mutex_unlock(&ec->mutex);
  536. return status;
  537. }
  538. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  539. {
  540. u8 d;
  541. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  542. .wdata = NULL, .rdata = &d,
  543. .wlen = 0, .rlen = 1};
  544. return acpi_ec_transaction(ec, &t);
  545. }
  546. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  547. {
  548. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  549. .wdata = NULL, .rdata = NULL,
  550. .wlen = 0, .rlen = 0};
  551. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  552. acpi_ec_transaction(ec, &t) : 0;
  553. }
  554. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
  555. {
  556. int result;
  557. u8 d;
  558. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  559. .wdata = &address, .rdata = &d,
  560. .wlen = 1, .rlen = 1};
  561. result = acpi_ec_transaction(ec, &t);
  562. *data = d;
  563. return result;
  564. }
  565. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  566. {
  567. u8 wdata[2] = { address, data };
  568. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  569. .wdata = wdata, .rdata = NULL,
  570. .wlen = 2, .rlen = 0};
  571. return acpi_ec_transaction(ec, &t);
  572. }
  573. int ec_read(u8 addr, u8 *val)
  574. {
  575. int err;
  576. u8 temp_data;
  577. if (!first_ec)
  578. return -ENODEV;
  579. err = acpi_ec_read(first_ec, addr, &temp_data);
  580. if (!err) {
  581. *val = temp_data;
  582. return 0;
  583. }
  584. return err;
  585. }
  586. EXPORT_SYMBOL(ec_read);
  587. int ec_write(u8 addr, u8 val)
  588. {
  589. int err;
  590. if (!first_ec)
  591. return -ENODEV;
  592. err = acpi_ec_write(first_ec, addr, val);
  593. return err;
  594. }
  595. EXPORT_SYMBOL(ec_write);
  596. int ec_transaction(u8 command,
  597. const u8 *wdata, unsigned wdata_len,
  598. u8 *rdata, unsigned rdata_len)
  599. {
  600. struct transaction t = {.command = command,
  601. .wdata = wdata, .rdata = rdata,
  602. .wlen = wdata_len, .rlen = rdata_len};
  603. if (!first_ec)
  604. return -ENODEV;
  605. return acpi_ec_transaction(first_ec, &t);
  606. }
  607. EXPORT_SYMBOL(ec_transaction);
  608. /* Get the handle to the EC device */
  609. acpi_handle ec_get_handle(void)
  610. {
  611. if (!first_ec)
  612. return NULL;
  613. return first_ec->handle;
  614. }
  615. EXPORT_SYMBOL(ec_get_handle);
  616. /*
  617. * Process _Q events that might have accumulated in the EC.
  618. * Run with locked ec mutex.
  619. */
  620. static void acpi_ec_clear(struct acpi_ec *ec)
  621. {
  622. int i, status;
  623. u8 value = 0;
  624. for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
  625. status = acpi_ec_query(ec, &value);
  626. if (status || !value)
  627. break;
  628. }
  629. if (unlikely(i == ACPI_EC_CLEAR_MAX))
  630. pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
  631. else
  632. pr_info("%d stale EC events cleared\n", i);
  633. }
  634. static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
  635. {
  636. unsigned long flags;
  637. spin_lock_irqsave(&ec->lock, flags);
  638. if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
  639. ec_dbg_drv("Starting EC");
  640. /* Enable GPE for event processing (SCI_EVT=1) */
  641. if (!resuming) {
  642. acpi_ec_submit_request(ec);
  643. ec_dbg_ref(ec, "Increase driver");
  644. }
  645. ec_log_drv("EC started");
  646. }
  647. spin_unlock_irqrestore(&ec->lock, flags);
  648. }
  649. static bool acpi_ec_stopped(struct acpi_ec *ec)
  650. {
  651. unsigned long flags;
  652. bool flushed;
  653. spin_lock_irqsave(&ec->lock, flags);
  654. flushed = acpi_ec_flushed(ec);
  655. spin_unlock_irqrestore(&ec->lock, flags);
  656. return flushed;
  657. }
  658. static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
  659. {
  660. unsigned long flags;
  661. spin_lock_irqsave(&ec->lock, flags);
  662. if (acpi_ec_started(ec)) {
  663. ec_dbg_drv("Stopping EC");
  664. set_bit(EC_FLAGS_STOPPED, &ec->flags);
  665. spin_unlock_irqrestore(&ec->lock, flags);
  666. wait_event(ec->wait, acpi_ec_stopped(ec));
  667. spin_lock_irqsave(&ec->lock, flags);
  668. /* Disable GPE for event processing (SCI_EVT=1) */
  669. if (!suspending) {
  670. acpi_ec_complete_request(ec);
  671. ec_dbg_ref(ec, "Decrease driver");
  672. }
  673. clear_bit(EC_FLAGS_STARTED, &ec->flags);
  674. clear_bit(EC_FLAGS_STOPPED, &ec->flags);
  675. ec_log_drv("EC stopped");
  676. }
  677. spin_unlock_irqrestore(&ec->lock, flags);
  678. }
  679. void acpi_ec_block_transactions(void)
  680. {
  681. struct acpi_ec *ec = first_ec;
  682. if (!ec)
  683. return;
  684. mutex_lock(&ec->mutex);
  685. /* Prevent transactions from being carried out */
  686. acpi_ec_stop(ec, true);
  687. mutex_unlock(&ec->mutex);
  688. }
  689. void acpi_ec_unblock_transactions(void)
  690. {
  691. struct acpi_ec *ec = first_ec;
  692. if (!ec)
  693. return;
  694. /* Allow transactions to be carried out again */
  695. acpi_ec_start(ec, true);
  696. if (EC_FLAGS_CLEAR_ON_RESUME)
  697. acpi_ec_clear(ec);
  698. }
  699. void acpi_ec_unblock_transactions_early(void)
  700. {
  701. /*
  702. * Allow transactions to happen again (this function is called from
  703. * atomic context during wakeup, so we don't need to acquire the mutex).
  704. */
  705. if (first_ec)
  706. acpi_ec_start(first_ec, true);
  707. }
  708. /* --------------------------------------------------------------------------
  709. Event Management
  710. -------------------------------------------------------------------------- */
  711. static struct acpi_ec_query_handler *
  712. acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
  713. {
  714. if (handler)
  715. kref_get(&handler->kref);
  716. return handler;
  717. }
  718. static void acpi_ec_query_handler_release(struct kref *kref)
  719. {
  720. struct acpi_ec_query_handler *handler =
  721. container_of(kref, struct acpi_ec_query_handler, kref);
  722. kfree(handler);
  723. }
  724. static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
  725. {
  726. kref_put(&handler->kref, acpi_ec_query_handler_release);
  727. }
  728. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  729. acpi_handle handle, acpi_ec_query_func func,
  730. void *data)
  731. {
  732. struct acpi_ec_query_handler *handler =
  733. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  734. if (!handler)
  735. return -ENOMEM;
  736. handler->query_bit = query_bit;
  737. handler->handle = handle;
  738. handler->func = func;
  739. handler->data = data;
  740. mutex_lock(&ec->mutex);
  741. kref_init(&handler->kref);
  742. list_add(&handler->node, &ec->list);
  743. mutex_unlock(&ec->mutex);
  744. return 0;
  745. }
  746. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  747. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  748. {
  749. struct acpi_ec_query_handler *handler, *tmp;
  750. LIST_HEAD(free_list);
  751. mutex_lock(&ec->mutex);
  752. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  753. if (query_bit == handler->query_bit) {
  754. list_del_init(&handler->node);
  755. list_add(&handler->node, &free_list);
  756. }
  757. }
  758. mutex_unlock(&ec->mutex);
  759. list_for_each_entry_safe(handler, tmp, &free_list, node)
  760. acpi_ec_put_query_handler(handler);
  761. }
  762. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  763. static void acpi_ec_run(void *cxt)
  764. {
  765. struct acpi_ec_query_handler *handler = cxt;
  766. if (!handler)
  767. return;
  768. ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
  769. if (handler->func)
  770. handler->func(handler->data);
  771. else if (handler->handle)
  772. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  773. ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
  774. acpi_ec_put_query_handler(handler);
  775. }
  776. static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
  777. {
  778. u8 value = 0;
  779. int result;
  780. acpi_status status;
  781. struct acpi_ec_query_handler *handler;
  782. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  783. .wdata = NULL, .rdata = &value,
  784. .wlen = 0, .rlen = 1};
  785. /*
  786. * Query the EC to find out which _Qxx method we need to evaluate.
  787. * Note that successful completion of the query causes the ACPI_EC_SCI
  788. * bit to be cleared (and thus clearing the interrupt source).
  789. */
  790. result = acpi_ec_transaction(ec, &t);
  791. if (result)
  792. return result;
  793. if (data)
  794. *data = value;
  795. if (!value)
  796. return -ENODATA;
  797. mutex_lock(&ec->mutex);
  798. list_for_each_entry(handler, &ec->list, node) {
  799. if (value == handler->query_bit) {
  800. /* have custom handler for this bit */
  801. handler = acpi_ec_get_query_handler(handler);
  802. ec_dbg_evt("Query(0x%02x) scheduled",
  803. handler->query_bit);
  804. status = acpi_os_execute((handler->func) ?
  805. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  806. acpi_ec_run, handler);
  807. if (ACPI_FAILURE(status))
  808. result = -EBUSY;
  809. break;
  810. }
  811. }
  812. mutex_unlock(&ec->mutex);
  813. return result;
  814. }
  815. static void acpi_ec_gpe_poller(struct work_struct *work)
  816. {
  817. struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
  818. acpi_ec_query(ec, NULL);
  819. }
  820. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  821. u32 gpe_number, void *data)
  822. {
  823. unsigned long flags;
  824. struct acpi_ec *ec = data;
  825. spin_lock_irqsave(&ec->lock, flags);
  826. advance_transaction(ec);
  827. spin_unlock_irqrestore(&ec->lock, flags);
  828. return ACPI_INTERRUPT_HANDLED;
  829. }
  830. /* --------------------------------------------------------------------------
  831. * Address Space Management
  832. * -------------------------------------------------------------------------- */
  833. static acpi_status
  834. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  835. u32 bits, u64 *value64,
  836. void *handler_context, void *region_context)
  837. {
  838. struct acpi_ec *ec = handler_context;
  839. int result = 0, i, bytes = bits / 8;
  840. u8 *value = (u8 *)value64;
  841. if ((address > 0xFF) || !value || !handler_context)
  842. return AE_BAD_PARAMETER;
  843. if (function != ACPI_READ && function != ACPI_WRITE)
  844. return AE_BAD_PARAMETER;
  845. if (EC_FLAGS_MSI || bits > 8)
  846. acpi_ec_burst_enable(ec);
  847. for (i = 0; i < bytes; ++i, ++address, ++value)
  848. result = (function == ACPI_READ) ?
  849. acpi_ec_read(ec, address, value) :
  850. acpi_ec_write(ec, address, *value);
  851. if (EC_FLAGS_MSI || bits > 8)
  852. acpi_ec_burst_disable(ec);
  853. switch (result) {
  854. case -EINVAL:
  855. return AE_BAD_PARAMETER;
  856. case -ENODEV:
  857. return AE_NOT_FOUND;
  858. case -ETIME:
  859. return AE_TIME;
  860. default:
  861. return AE_OK;
  862. }
  863. }
  864. /* --------------------------------------------------------------------------
  865. * Driver Interface
  866. * -------------------------------------------------------------------------- */
  867. static acpi_status
  868. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  869. static struct acpi_ec *make_acpi_ec(void)
  870. {
  871. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  872. if (!ec)
  873. return NULL;
  874. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  875. mutex_init(&ec->mutex);
  876. init_waitqueue_head(&ec->wait);
  877. INIT_LIST_HEAD(&ec->list);
  878. spin_lock_init(&ec->lock);
  879. INIT_WORK(&ec->work, acpi_ec_gpe_poller);
  880. return ec;
  881. }
  882. static acpi_status
  883. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  884. void *context, void **return_value)
  885. {
  886. char node_name[5];
  887. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  888. struct acpi_ec *ec = context;
  889. int value = 0;
  890. acpi_status status;
  891. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  892. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
  893. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  894. return AE_OK;
  895. }
  896. static acpi_status
  897. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  898. {
  899. acpi_status status;
  900. unsigned long long tmp = 0;
  901. struct acpi_ec *ec = context;
  902. /* clear addr values, ec_parse_io_ports depend on it */
  903. ec->command_addr = ec->data_addr = 0;
  904. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  905. ec_parse_io_ports, ec);
  906. if (ACPI_FAILURE(status))
  907. return status;
  908. /* Get GPE bit assignment (EC events). */
  909. /* TODO: Add support for _GPE returning a package */
  910. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  911. if (ACPI_FAILURE(status))
  912. return status;
  913. ec->gpe = tmp;
  914. /* Use the global lock for all EC transactions? */
  915. tmp = 0;
  916. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  917. ec->global_lock = tmp;
  918. ec->handle = handle;
  919. return AE_CTRL_TERMINATE;
  920. }
  921. static int ec_install_handlers(struct acpi_ec *ec)
  922. {
  923. acpi_status status;
  924. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  925. return 0;
  926. status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
  927. ACPI_GPE_EDGE_TRIGGERED,
  928. &acpi_ec_gpe_handler, ec);
  929. if (ACPI_FAILURE(status))
  930. return -ENODEV;
  931. acpi_ec_start(ec, false);
  932. status = acpi_install_address_space_handler(ec->handle,
  933. ACPI_ADR_SPACE_EC,
  934. &acpi_ec_space_handler,
  935. NULL, ec);
  936. if (ACPI_FAILURE(status)) {
  937. if (status == AE_NOT_FOUND) {
  938. /*
  939. * Maybe OS fails in evaluating the _REG object.
  940. * The AE_NOT_FOUND error will be ignored and OS
  941. * continue to initialize EC.
  942. */
  943. pr_err("Fail in evaluating the _REG object"
  944. " of EC device. Broken bios is suspected.\n");
  945. } else {
  946. acpi_ec_stop(ec, false);
  947. acpi_remove_gpe_handler(NULL, ec->gpe,
  948. &acpi_ec_gpe_handler);
  949. return -ENODEV;
  950. }
  951. }
  952. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  953. return 0;
  954. }
  955. static void ec_remove_handlers(struct acpi_ec *ec)
  956. {
  957. if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  958. return;
  959. acpi_ec_stop(ec, false);
  960. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  961. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  962. pr_err("failed to remove space handler\n");
  963. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  964. &acpi_ec_gpe_handler)))
  965. pr_err("failed to remove gpe handler\n");
  966. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  967. }
  968. static int acpi_ec_add(struct acpi_device *device)
  969. {
  970. struct acpi_ec *ec = NULL;
  971. int ret;
  972. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  973. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  974. /* Check for boot EC */
  975. if (boot_ec &&
  976. (boot_ec->handle == device->handle ||
  977. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  978. ec = boot_ec;
  979. boot_ec = NULL;
  980. } else {
  981. ec = make_acpi_ec();
  982. if (!ec)
  983. return -ENOMEM;
  984. }
  985. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  986. AE_CTRL_TERMINATE) {
  987. kfree(ec);
  988. return -EINVAL;
  989. }
  990. /* Find and register all query methods */
  991. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  992. acpi_ec_register_query_methods, NULL, ec, NULL);
  993. if (!first_ec)
  994. first_ec = ec;
  995. device->driver_data = ec;
  996. ret = !!request_region(ec->data_addr, 1, "EC data");
  997. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  998. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  999. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  1000. pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  1001. ec->gpe, ec->command_addr, ec->data_addr);
  1002. ret = ec_install_handlers(ec);
  1003. /* Reprobe devices depending on the EC */
  1004. acpi_walk_dep_device_list(ec->handle);
  1005. /* EC is fully operational, allow queries */
  1006. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  1007. /* Clear stale _Q events if hardware might require that */
  1008. if (EC_FLAGS_CLEAR_ON_RESUME)
  1009. acpi_ec_clear(ec);
  1010. return ret;
  1011. }
  1012. static int acpi_ec_remove(struct acpi_device *device)
  1013. {
  1014. struct acpi_ec *ec;
  1015. struct acpi_ec_query_handler *handler, *tmp;
  1016. if (!device)
  1017. return -EINVAL;
  1018. ec = acpi_driver_data(device);
  1019. ec_remove_handlers(ec);
  1020. mutex_lock(&ec->mutex);
  1021. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  1022. list_del(&handler->node);
  1023. kfree(handler);
  1024. }
  1025. mutex_unlock(&ec->mutex);
  1026. release_region(ec->data_addr, 1);
  1027. release_region(ec->command_addr, 1);
  1028. device->driver_data = NULL;
  1029. if (ec == first_ec)
  1030. first_ec = NULL;
  1031. kfree(ec);
  1032. return 0;
  1033. }
  1034. static acpi_status
  1035. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  1036. {
  1037. struct acpi_ec *ec = context;
  1038. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  1039. return AE_OK;
  1040. /*
  1041. * The first address region returned is the data port, and
  1042. * the second address region returned is the status/command
  1043. * port.
  1044. */
  1045. if (ec->data_addr == 0)
  1046. ec->data_addr = resource->data.io.minimum;
  1047. else if (ec->command_addr == 0)
  1048. ec->command_addr = resource->data.io.minimum;
  1049. else
  1050. return AE_CTRL_TERMINATE;
  1051. return AE_OK;
  1052. }
  1053. int __init acpi_boot_ec_enable(void)
  1054. {
  1055. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  1056. return 0;
  1057. if (!ec_install_handlers(boot_ec)) {
  1058. first_ec = boot_ec;
  1059. return 0;
  1060. }
  1061. return -EFAULT;
  1062. }
  1063. static const struct acpi_device_id ec_device_ids[] = {
  1064. {"PNP0C09", 0},
  1065. {"", 0},
  1066. };
  1067. /* Some BIOS do not survive early DSDT scan, skip it */
  1068. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  1069. {
  1070. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  1071. return 0;
  1072. }
  1073. /* ASUStek often supplies us with broken ECDT, validate it */
  1074. static int ec_validate_ecdt(const struct dmi_system_id *id)
  1075. {
  1076. EC_FLAGS_VALIDATE_ECDT = 1;
  1077. return 0;
  1078. }
  1079. /* MSI EC needs special treatment, enable it */
  1080. static int ec_flag_msi(const struct dmi_system_id *id)
  1081. {
  1082. pr_debug("Detected MSI hardware, enabling workarounds.\n");
  1083. EC_FLAGS_MSI = 1;
  1084. EC_FLAGS_VALIDATE_ECDT = 1;
  1085. return 0;
  1086. }
  1087. /*
  1088. * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
  1089. * the GPE storm threshold back to 20
  1090. */
  1091. static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
  1092. {
  1093. pr_debug("Setting the EC GPE storm threshold to 20\n");
  1094. ec_storm_threshold = 20;
  1095. return 0;
  1096. }
  1097. /*
  1098. * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
  1099. * which case, we complete the QR_EC without issuing it to the firmware.
  1100. * https://bugzilla.kernel.org/show_bug.cgi?id=86211
  1101. */
  1102. static int ec_flag_query_handshake(const struct dmi_system_id *id)
  1103. {
  1104. pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
  1105. EC_FLAGS_QUERY_HANDSHAKE = 1;
  1106. return 0;
  1107. }
  1108. /*
  1109. * On some hardware it is necessary to clear events accumulated by the EC during
  1110. * sleep. These ECs stop reporting GPEs until they are manually polled, if too
  1111. * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
  1112. *
  1113. * https://bugzilla.kernel.org/show_bug.cgi?id=44161
  1114. *
  1115. * Ideally, the EC should also be instructed NOT to accumulate events during
  1116. * sleep (which Windows seems to do somehow), but the interface to control this
  1117. * behaviour is not known at this time.
  1118. *
  1119. * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
  1120. * however it is very likely that other Samsung models are affected.
  1121. *
  1122. * On systems which don't accumulate _Q events during sleep, this extra check
  1123. * should be harmless.
  1124. */
  1125. static int ec_clear_on_resume(const struct dmi_system_id *id)
  1126. {
  1127. pr_debug("Detected system needing EC poll on resume.\n");
  1128. EC_FLAGS_CLEAR_ON_RESUME = 1;
  1129. return 0;
  1130. }
  1131. static struct dmi_system_id ec_dmi_table[] __initdata = {
  1132. {
  1133. ec_skip_dsdt_scan, "Compal JFL92", {
  1134. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  1135. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  1136. {
  1137. ec_flag_msi, "MSI hardware", {
  1138. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  1139. {
  1140. ec_flag_msi, "MSI hardware", {
  1141. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  1142. {
  1143. ec_flag_msi, "MSI hardware", {
  1144. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  1145. {
  1146. ec_flag_msi, "MSI hardware", {
  1147. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
  1148. {
  1149. ec_flag_msi, "Quanta hardware", {
  1150. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  1151. DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
  1152. {
  1153. ec_flag_msi, "Quanta hardware", {
  1154. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  1155. DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
  1156. {
  1157. ec_flag_msi, "Clevo W350etq", {
  1158. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
  1159. DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
  1160. {
  1161. ec_validate_ecdt, "ASUS hardware", {
  1162. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  1163. {
  1164. ec_validate_ecdt, "ASUS hardware", {
  1165. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
  1166. {
  1167. ec_enlarge_storm_threshold, "CLEVO hardware", {
  1168. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  1169. DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
  1170. {
  1171. ec_skip_dsdt_scan, "HP Folio 13", {
  1172. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  1173. DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
  1174. {
  1175. ec_validate_ecdt, "ASUS hardware", {
  1176. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
  1177. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
  1178. {
  1179. ec_clear_on_resume, "Samsung hardware", {
  1180. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
  1181. {
  1182. ec_flag_query_handshake, "Acer hardware", {
  1183. DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
  1184. {},
  1185. };
  1186. int __init acpi_ec_ecdt_probe(void)
  1187. {
  1188. acpi_status status;
  1189. struct acpi_ec *saved_ec = NULL;
  1190. struct acpi_table_ecdt *ecdt_ptr;
  1191. boot_ec = make_acpi_ec();
  1192. if (!boot_ec)
  1193. return -ENOMEM;
  1194. /*
  1195. * Generate a boot ec context
  1196. */
  1197. dmi_check_system(ec_dmi_table);
  1198. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1199. (struct acpi_table_header **)&ecdt_ptr);
  1200. if (ACPI_SUCCESS(status)) {
  1201. pr_info("EC description table is found, configuring boot EC\n");
  1202. boot_ec->command_addr = ecdt_ptr->control.address;
  1203. boot_ec->data_addr = ecdt_ptr->data.address;
  1204. boot_ec->gpe = ecdt_ptr->gpe;
  1205. boot_ec->handle = ACPI_ROOT_OBJECT;
  1206. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id,
  1207. &boot_ec->handle);
  1208. /* Don't trust ECDT, which comes from ASUSTek */
  1209. if (!EC_FLAGS_VALIDATE_ECDT)
  1210. goto install;
  1211. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  1212. if (!saved_ec)
  1213. return -ENOMEM;
  1214. /* fall through */
  1215. }
  1216. if (EC_FLAGS_SKIP_DSDT_SCAN) {
  1217. kfree(saved_ec);
  1218. return -ENODEV;
  1219. }
  1220. /* This workaround is needed only on some broken machines,
  1221. * which require early EC, but fail to provide ECDT */
  1222. pr_debug("Look up EC in DSDT\n");
  1223. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  1224. boot_ec, NULL);
  1225. /* Check that acpi_get_devices actually find something */
  1226. if (ACPI_FAILURE(status) || !boot_ec->handle)
  1227. goto error;
  1228. if (saved_ec) {
  1229. /* try to find good ECDT from ASUSTek */
  1230. if (saved_ec->command_addr != boot_ec->command_addr ||
  1231. saved_ec->data_addr != boot_ec->data_addr ||
  1232. saved_ec->gpe != boot_ec->gpe ||
  1233. saved_ec->handle != boot_ec->handle)
  1234. pr_info("ASUSTek keeps feeding us with broken "
  1235. "ECDT tables, which are very hard to workaround. "
  1236. "Trying to use DSDT EC info instead. Please send "
  1237. "output of acpidump to linux-acpi@vger.kernel.org\n");
  1238. kfree(saved_ec);
  1239. saved_ec = NULL;
  1240. } else {
  1241. /* We really need to limit this workaround, the only ASUS,
  1242. * which needs it, has fake EC._INI method, so use it as flag.
  1243. * Keep boot_ec struct as it will be needed soon.
  1244. */
  1245. if (!dmi_name_in_vendors("ASUS") ||
  1246. !acpi_has_method(boot_ec->handle, "_INI"))
  1247. return -ENODEV;
  1248. }
  1249. install:
  1250. if (!ec_install_handlers(boot_ec)) {
  1251. first_ec = boot_ec;
  1252. return 0;
  1253. }
  1254. error:
  1255. kfree(boot_ec);
  1256. kfree(saved_ec);
  1257. boot_ec = NULL;
  1258. return -ENODEV;
  1259. }
  1260. static struct acpi_driver acpi_ec_driver = {
  1261. .name = "ec",
  1262. .class = ACPI_EC_CLASS,
  1263. .ids = ec_device_ids,
  1264. .ops = {
  1265. .add = acpi_ec_add,
  1266. .remove = acpi_ec_remove,
  1267. },
  1268. };
  1269. int __init acpi_ec_init(void)
  1270. {
  1271. int result = 0;
  1272. /* Now register the driver for the EC */
  1273. result = acpi_bus_register_driver(&acpi_ec_driver);
  1274. if (result < 0)
  1275. return -ENODEV;
  1276. return result;
  1277. }
  1278. /* EC driver currently not unloadable */
  1279. #if 0
  1280. static void __exit acpi_ec_exit(void)
  1281. {
  1282. acpi_bus_unregister_driver(&acpi_ec_driver);
  1283. }
  1284. #endif /* 0 */