ec.c 28 KB

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