ec.c 28 KB

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