ec.c 27 KB

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