ec.c 26 KB

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