ec.c 28 KB

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