ec.c 28 KB

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