ec.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v2.0)
  3. *
  4. * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  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 print outs*/
  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 <asm/io.h>
  40. #include <acpi/acpi_bus.h>
  41. #include <acpi/acpi_drivers.h>
  42. #include <acpi/actypes.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. /* EC events */
  62. enum ec_event {
  63. ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
  64. ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
  65. };
  66. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  67. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  68. #define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
  69. enum {
  70. EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
  71. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  72. EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
  73. EC_FLAGS_NO_GPE, /* Don't use GPE mode */
  74. EC_FLAGS_RESCHEDULE_POLL /* Re-schedule poll */
  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. static struct acpi_ec {
  87. acpi_handle handle;
  88. unsigned long gpe;
  89. unsigned long command_addr;
  90. unsigned long data_addr;
  91. unsigned long global_lock;
  92. unsigned long flags;
  93. struct mutex lock;
  94. wait_queue_head_t wait;
  95. struct list_head list;
  96. struct delayed_work work;
  97. atomic_t irq_count;
  98. u8 handlers_installed;
  99. } *boot_ec, *first_ec;
  100. /*
  101. * Some Asus system have exchanged ECDT data/command IO addresses.
  102. */
  103. static int print_ecdt_error(const struct dmi_system_id *id)
  104. {
  105. printk(KERN_NOTICE PREFIX "%s detected - "
  106. "ECDT has exchanged control/data I/O address\n",
  107. id->ident);
  108. return 0;
  109. }
  110. static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
  111. {
  112. print_ecdt_error, "Asus L4R", {
  113. DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
  114. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
  115. DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
  116. {
  117. print_ecdt_error, "Asus M6R", {
  118. DMI_MATCH(DMI_BIOS_VERSION, "0207"),
  119. DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
  120. DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
  121. {},
  122. };
  123. /* --------------------------------------------------------------------------
  124. Transaction Management
  125. -------------------------------------------------------------------------- */
  126. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  127. {
  128. u8 x = inb(ec->command_addr);
  129. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  130. return x;
  131. }
  132. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  133. {
  134. u8 x = inb(ec->data_addr);
  135. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  136. return inb(ec->data_addr);
  137. }
  138. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  139. {
  140. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  141. outb(command, ec->command_addr);
  142. }
  143. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  144. {
  145. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  146. outb(data, ec->data_addr);
  147. }
  148. static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
  149. {
  150. if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
  151. return 0;
  152. if (event == ACPI_EC_EVENT_OBF_1) {
  153. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
  154. return 1;
  155. } else if (event == ACPI_EC_EVENT_IBF_0) {
  156. if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. static void ec_schedule_ec_poll(struct acpi_ec *ec)
  162. {
  163. if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
  164. schedule_delayed_work(&ec->work,
  165. msecs_to_jiffies(ACPI_EC_DELAY));
  166. }
  167. static void ec_switch_to_poll_mode(struct acpi_ec *ec)
  168. {
  169. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  170. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  171. acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  172. set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
  173. }
  174. static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
  175. {
  176. atomic_set(&ec->irq_count, 0);
  177. if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
  178. likely(!force_poll)) {
  179. if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
  180. msecs_to_jiffies(ACPI_EC_DELAY)))
  181. return 0;
  182. clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  183. if (acpi_ec_check_status(ec, event)) {
  184. /* missing GPEs, switch back to poll mode */
  185. if (printk_ratelimit())
  186. pr_info(PREFIX "missing confirmations, "
  187. "switch off interrupt mode.\n");
  188. ec_switch_to_poll_mode(ec);
  189. ec_schedule_ec_poll(ec);
  190. return 0;
  191. }
  192. } else {
  193. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  194. clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  195. while (time_before(jiffies, delay)) {
  196. if (acpi_ec_check_status(ec, event))
  197. return 0;
  198. msleep(1);
  199. }
  200. if (acpi_ec_check_status(ec,event))
  201. return 0;
  202. }
  203. pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
  204. acpi_ec_read_status(ec),
  205. (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
  206. return -ETIME;
  207. }
  208. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
  209. const u8 * wdata, unsigned wdata_len,
  210. u8 * rdata, unsigned rdata_len,
  211. int force_poll)
  212. {
  213. int result = 0;
  214. set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  215. pr_debug(PREFIX "transaction start\n");
  216. acpi_ec_write_cmd(ec, command);
  217. for (; wdata_len > 0; --wdata_len) {
  218. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
  219. if (result) {
  220. pr_err(PREFIX
  221. "write_cmd timeout, command = %d\n", command);
  222. goto end;
  223. }
  224. set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  225. acpi_ec_write_data(ec, *(wdata++));
  226. }
  227. if (!rdata_len) {
  228. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
  229. if (result) {
  230. pr_err(PREFIX
  231. "finish-write timeout, command = %d\n", command);
  232. goto end;
  233. }
  234. } else if (command == ACPI_EC_COMMAND_QUERY)
  235. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  236. for (; rdata_len > 0; --rdata_len) {
  237. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
  238. if (result) {
  239. pr_err(PREFIX "read timeout, command = %d\n", command);
  240. goto end;
  241. }
  242. /* Don't expect GPE after last read */
  243. if (rdata_len > 1)
  244. set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  245. *(rdata++) = acpi_ec_read_data(ec);
  246. }
  247. end:
  248. pr_debug(PREFIX "transaction end\n");
  249. return result;
  250. }
  251. static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
  252. const u8 * wdata, unsigned wdata_len,
  253. u8 * rdata, unsigned rdata_len,
  254. int force_poll)
  255. {
  256. int status;
  257. u32 glk;
  258. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  259. return -EINVAL;
  260. if (rdata)
  261. memset(rdata, 0, rdata_len);
  262. mutex_lock(&ec->lock);
  263. if (ec->global_lock) {
  264. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  265. if (ACPI_FAILURE(status)) {
  266. mutex_unlock(&ec->lock);
  267. return -ENODEV;
  268. }
  269. }
  270. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
  271. if (status) {
  272. pr_err(PREFIX "input buffer is not empty, "
  273. "aborting transaction\n");
  274. goto end;
  275. }
  276. status = acpi_ec_transaction_unlocked(ec, command,
  277. wdata, wdata_len,
  278. rdata, rdata_len,
  279. force_poll);
  280. end:
  281. if (ec->global_lock)
  282. acpi_release_global_lock(glk);
  283. mutex_unlock(&ec->lock);
  284. return status;
  285. }
  286. /*
  287. * Note: samsung nv5000 doesn't work with ec burst mode.
  288. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  289. */
  290. int acpi_ec_burst_enable(struct acpi_ec *ec)
  291. {
  292. u8 d;
  293. return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
  294. }
  295. int acpi_ec_burst_disable(struct acpi_ec *ec)
  296. {
  297. return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
  298. }
  299. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  300. {
  301. int result;
  302. u8 d;
  303. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
  304. &address, 1, &d, 1, 0);
  305. *data = d;
  306. return result;
  307. }
  308. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  309. {
  310. u8 wdata[2] = { address, data };
  311. return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
  312. wdata, 2, NULL, 0, 0);
  313. }
  314. /*
  315. * Externally callable EC access functions. For now, assume 1 EC only
  316. */
  317. int ec_burst_enable(void)
  318. {
  319. if (!first_ec)
  320. return -ENODEV;
  321. return acpi_ec_burst_enable(first_ec);
  322. }
  323. EXPORT_SYMBOL(ec_burst_enable);
  324. int ec_burst_disable(void)
  325. {
  326. if (!first_ec)
  327. return -ENODEV;
  328. return acpi_ec_burst_disable(first_ec);
  329. }
  330. EXPORT_SYMBOL(ec_burst_disable);
  331. int ec_read(u8 addr, u8 * val)
  332. {
  333. int err;
  334. u8 temp_data;
  335. if (!first_ec)
  336. return -ENODEV;
  337. err = acpi_ec_read(first_ec, addr, &temp_data);
  338. if (!err) {
  339. *val = temp_data;
  340. return 0;
  341. } else
  342. return err;
  343. }
  344. EXPORT_SYMBOL(ec_read);
  345. int ec_write(u8 addr, u8 val)
  346. {
  347. int err;
  348. if (!first_ec)
  349. return -ENODEV;
  350. err = acpi_ec_write(first_ec, addr, val);
  351. return err;
  352. }
  353. EXPORT_SYMBOL(ec_write);
  354. int ec_transaction(u8 command,
  355. const u8 * wdata, unsigned wdata_len,
  356. u8 * rdata, unsigned rdata_len,
  357. int force_poll)
  358. {
  359. if (!first_ec)
  360. return -ENODEV;
  361. return acpi_ec_transaction(first_ec, command, wdata,
  362. wdata_len, rdata, rdata_len,
  363. force_poll);
  364. }
  365. EXPORT_SYMBOL(ec_transaction);
  366. static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
  367. {
  368. int result;
  369. u8 d;
  370. if (!ec || !data)
  371. return -EINVAL;
  372. /*
  373. * Query the EC to find out which _Qxx method we need to evaluate.
  374. * Note that successful completion of the query causes the ACPI_EC_SCI
  375. * bit to be cleared (and thus clearing the interrupt source).
  376. */
  377. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
  378. if (result)
  379. return result;
  380. if (!d)
  381. return -ENODATA;
  382. *data = d;
  383. return 0;
  384. }
  385. /* --------------------------------------------------------------------------
  386. Event Management
  387. -------------------------------------------------------------------------- */
  388. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  389. acpi_handle handle, acpi_ec_query_func func,
  390. void *data)
  391. {
  392. struct acpi_ec_query_handler *handler =
  393. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  394. if (!handler)
  395. return -ENOMEM;
  396. handler->query_bit = query_bit;
  397. handler->handle = handle;
  398. handler->func = func;
  399. handler->data = data;
  400. mutex_lock(&ec->lock);
  401. list_add(&handler->node, &ec->list);
  402. mutex_unlock(&ec->lock);
  403. return 0;
  404. }
  405. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  406. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  407. {
  408. struct acpi_ec_query_handler *handler, *tmp;
  409. mutex_lock(&ec->lock);
  410. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  411. if (query_bit == handler->query_bit) {
  412. list_del(&handler->node);
  413. kfree(handler);
  414. }
  415. }
  416. mutex_unlock(&ec->lock);
  417. }
  418. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  419. static void acpi_ec_gpe_query(void *ec_cxt)
  420. {
  421. struct acpi_ec *ec = ec_cxt;
  422. u8 value = 0;
  423. struct acpi_ec_query_handler *handler, copy;
  424. if (!ec || acpi_ec_query(ec, &value))
  425. return;
  426. mutex_lock(&ec->lock);
  427. list_for_each_entry(handler, &ec->list, node) {
  428. if (value == handler->query_bit) {
  429. /* have custom handler for this bit */
  430. memcpy(&copy, handler, sizeof(copy));
  431. mutex_unlock(&ec->lock);
  432. if (copy.func) {
  433. copy.func(copy.data);
  434. } else if (copy.handle) {
  435. acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
  436. }
  437. return;
  438. }
  439. }
  440. mutex_unlock(&ec->lock);
  441. }
  442. static u32 acpi_ec_gpe_handler(void *data)
  443. {
  444. acpi_status status = AE_OK;
  445. struct acpi_ec *ec = data;
  446. u8 state = acpi_ec_read_status(ec);
  447. pr_debug(PREFIX "~~~> interrupt\n");
  448. atomic_inc(&ec->irq_count);
  449. if (atomic_read(&ec->irq_count) > 5) {
  450. pr_err(PREFIX "GPE storm detected, disabling EC GPE\n");
  451. ec_switch_to_poll_mode(ec);
  452. goto end;
  453. }
  454. clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  455. if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
  456. wake_up(&ec->wait);
  457. if (state & ACPI_EC_FLAG_SCI) {
  458. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  459. status = acpi_os_execute(OSL_EC_BURST_HANDLER,
  460. acpi_ec_gpe_query, ec);
  461. } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
  462. !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
  463. in_interrupt()) {
  464. /* this is non-query, must be confirmation */
  465. if (printk_ratelimit())
  466. pr_info(PREFIX "non-query interrupt received,"
  467. " switching to interrupt mode\n");
  468. set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  469. clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
  470. }
  471. end:
  472. ec_schedule_ec_poll(ec);
  473. return ACPI_SUCCESS(status) ?
  474. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  475. }
  476. static void do_ec_poll(struct work_struct *work)
  477. {
  478. struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
  479. atomic_set(&ec->irq_count, 0);
  480. (void)acpi_ec_gpe_handler(ec);
  481. }
  482. /* --------------------------------------------------------------------------
  483. Address Space Management
  484. -------------------------------------------------------------------------- */
  485. static acpi_status
  486. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  487. u32 bits, acpi_integer *value,
  488. void *handler_context, void *region_context)
  489. {
  490. struct acpi_ec *ec = handler_context;
  491. int result = 0, i;
  492. u8 temp = 0;
  493. if ((address > 0xFF) || !value || !handler_context)
  494. return AE_BAD_PARAMETER;
  495. if (function != ACPI_READ && function != ACPI_WRITE)
  496. return AE_BAD_PARAMETER;
  497. if (bits != 8 && acpi_strict)
  498. return AE_BAD_PARAMETER;
  499. acpi_ec_burst_enable(ec);
  500. if (function == ACPI_READ) {
  501. result = acpi_ec_read(ec, address, &temp);
  502. *value = temp;
  503. } else {
  504. temp = 0xff & (*value);
  505. result = acpi_ec_write(ec, address, temp);
  506. }
  507. for (i = 8; unlikely(bits - i > 0); i += 8) {
  508. ++address;
  509. if (function == ACPI_READ) {
  510. result = acpi_ec_read(ec, address, &temp);
  511. (*value) |= ((acpi_integer)temp) << i;
  512. } else {
  513. temp = 0xff & ((*value) >> i);
  514. result = acpi_ec_write(ec, address, temp);
  515. }
  516. }
  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 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. INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
  600. atomic_set(&ec->irq_count, 0);
  601. return ec;
  602. }
  603. static acpi_status
  604. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  605. void *context, void **return_value)
  606. {
  607. struct acpi_namespace_node *node = handle;
  608. struct acpi_ec *ec = context;
  609. int value = 0;
  610. if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
  611. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  612. }
  613. return AE_OK;
  614. }
  615. static acpi_status
  616. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  617. {
  618. acpi_status status;
  619. struct acpi_ec *ec = context;
  620. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  621. ec_parse_io_ports, ec);
  622. if (ACPI_FAILURE(status))
  623. return status;
  624. /* Get GPE bit assignment (EC events). */
  625. /* TODO: Add support for _GPE returning a package */
  626. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
  627. if (ACPI_FAILURE(status))
  628. return status;
  629. /* Use the global lock for all EC transactions? */
  630. acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
  631. ec->handle = handle;
  632. return AE_CTRL_TERMINATE;
  633. }
  634. static void ec_poll_stop(struct acpi_ec *ec)
  635. {
  636. clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
  637. cancel_delayed_work(&ec->work);
  638. }
  639. static void ec_remove_handlers(struct acpi_ec *ec)
  640. {
  641. ec_poll_stop(ec);
  642. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  643. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  644. pr_err(PREFIX "failed to remove space handler\n");
  645. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  646. &acpi_ec_gpe_handler)))
  647. pr_err(PREFIX "failed to remove gpe handler\n");
  648. ec->handlers_installed = 0;
  649. }
  650. static int acpi_ec_add(struct acpi_device *device)
  651. {
  652. struct acpi_ec *ec = NULL;
  653. if (!device)
  654. return -EINVAL;
  655. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  656. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  657. /* Check for boot EC */
  658. if (boot_ec &&
  659. (boot_ec->handle == device->handle ||
  660. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  661. ec = boot_ec;
  662. boot_ec = NULL;
  663. } else {
  664. ec = make_acpi_ec();
  665. if (!ec)
  666. return -ENOMEM;
  667. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  668. AE_CTRL_TERMINATE) {
  669. kfree(ec);
  670. return -EINVAL;
  671. }
  672. }
  673. ec->handle = device->handle;
  674. /* Find and register all query methods */
  675. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  676. acpi_ec_register_query_methods, ec, NULL);
  677. if (!first_ec)
  678. first_ec = ec;
  679. acpi_driver_data(device) = ec;
  680. acpi_ec_add_fs(device);
  681. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  682. ec->gpe, ec->command_addr, ec->data_addr);
  683. pr_info(PREFIX "driver started in %s mode\n",
  684. (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
  685. return 0;
  686. }
  687. static int acpi_ec_remove(struct acpi_device *device, int type)
  688. {
  689. struct acpi_ec *ec;
  690. struct acpi_ec_query_handler *handler, *tmp;
  691. if (!device)
  692. return -EINVAL;
  693. ec = acpi_driver_data(device);
  694. mutex_lock(&ec->lock);
  695. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  696. list_del(&handler->node);
  697. kfree(handler);
  698. }
  699. mutex_unlock(&ec->lock);
  700. acpi_ec_remove_fs(device);
  701. acpi_driver_data(device) = NULL;
  702. if (ec == first_ec)
  703. first_ec = NULL;
  704. kfree(ec);
  705. return 0;
  706. }
  707. static acpi_status
  708. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  709. {
  710. struct acpi_ec *ec = context;
  711. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  712. return AE_OK;
  713. /*
  714. * The first address region returned is the data port, and
  715. * the second address region returned is the status/command
  716. * port.
  717. */
  718. if (ec->data_addr == 0)
  719. ec->data_addr = resource->data.io.minimum;
  720. else if (ec->command_addr == 0)
  721. ec->command_addr = resource->data.io.minimum;
  722. else
  723. return AE_CTRL_TERMINATE;
  724. return AE_OK;
  725. }
  726. static int ec_install_handlers(struct acpi_ec *ec)
  727. {
  728. acpi_status status;
  729. if (ec->handlers_installed)
  730. return 0;
  731. status = acpi_install_gpe_handler(NULL, ec->gpe,
  732. ACPI_GPE_EDGE_TRIGGERED,
  733. &acpi_ec_gpe_handler, ec);
  734. if (ACPI_FAILURE(status))
  735. return -ENODEV;
  736. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  737. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  738. status = acpi_install_address_space_handler(ec->handle,
  739. ACPI_ADR_SPACE_EC,
  740. &acpi_ec_space_handler,
  741. NULL, ec);
  742. if (ACPI_FAILURE(status)) {
  743. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  744. return -ENODEV;
  745. }
  746. ec->handlers_installed = 1;
  747. return 0;
  748. }
  749. static int acpi_ec_start(struct acpi_device *device)
  750. {
  751. struct acpi_ec *ec;
  752. int ret = 0;
  753. if (!device)
  754. return -EINVAL;
  755. ec = acpi_driver_data(device);
  756. if (!ec)
  757. return -EINVAL;
  758. ret = ec_install_handlers(ec);
  759. /* EC is fully operational, allow queries */
  760. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  761. ec_schedule_ec_poll(ec);
  762. return ret;
  763. }
  764. static int acpi_ec_stop(struct acpi_device *device, int type)
  765. {
  766. struct acpi_ec *ec;
  767. if (!device)
  768. return -EINVAL;
  769. ec = acpi_driver_data(device);
  770. if (!ec)
  771. return -EINVAL;
  772. ec_remove_handlers(ec);
  773. return 0;
  774. }
  775. int __init acpi_boot_ec_enable(void)
  776. {
  777. if (!boot_ec || boot_ec->handlers_installed)
  778. return 0;
  779. if (!ec_install_handlers(boot_ec)) {
  780. first_ec = boot_ec;
  781. return 0;
  782. }
  783. return -EFAULT;
  784. }
  785. static const struct acpi_device_id ec_device_ids[] = {
  786. {"PNP0C09", 0},
  787. {"", 0},
  788. };
  789. int __init acpi_ec_ecdt_probe(void)
  790. {
  791. int ret;
  792. acpi_status status;
  793. struct acpi_table_ecdt *ecdt_ptr;
  794. boot_ec = make_acpi_ec();
  795. if (!boot_ec)
  796. return -ENOMEM;
  797. /*
  798. * Generate a boot ec context
  799. */
  800. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  801. (struct acpi_table_header **)&ecdt_ptr);
  802. if (ACPI_SUCCESS(status)) {
  803. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  804. boot_ec->command_addr = ecdt_ptr->control.address;
  805. boot_ec->data_addr = ecdt_ptr->data.address;
  806. if (dmi_check_system(ec_dmi_table)) {
  807. /*
  808. * If the board falls into ec_dmi_table, it means
  809. * that ECDT table gives the incorrect command/status
  810. * & data I/O address. Just fix it.
  811. */
  812. boot_ec->data_addr = ecdt_ptr->control.address;
  813. boot_ec->command_addr = ecdt_ptr->data.address;
  814. }
  815. boot_ec->gpe = ecdt_ptr->gpe;
  816. boot_ec->handle = ACPI_ROOT_OBJECT;
  817. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  818. } else {
  819. /* This workaround is needed only on some broken machines,
  820. * which require early EC, but fail to provide ECDT */
  821. acpi_handle x;
  822. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  823. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  824. boot_ec, NULL);
  825. /* Check that acpi_get_devices actually find something */
  826. if (ACPI_FAILURE(status) || !boot_ec->handle)
  827. goto error;
  828. /* We really need to limit this workaround, the only ASUS,
  829. * which needs it, has fake EC._INI method, so use it as flag.
  830. * Keep boot_ec struct as it will be needed soon.
  831. */
  832. if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
  833. return -ENODEV;
  834. }
  835. ret = ec_install_handlers(boot_ec);
  836. if (!ret) {
  837. first_ec = boot_ec;
  838. return 0;
  839. }
  840. error:
  841. kfree(boot_ec);
  842. boot_ec = NULL;
  843. return -ENODEV;
  844. }
  845. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  846. {
  847. struct acpi_ec *ec = acpi_driver_data(device);
  848. /* Stop using GPE */
  849. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  850. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  851. acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  852. return 0;
  853. }
  854. static int acpi_ec_resume(struct acpi_device *device)
  855. {
  856. struct acpi_ec *ec = acpi_driver_data(device);
  857. /* Enable use of GPE back */
  858. clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
  859. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  860. return 0;
  861. }
  862. static struct acpi_driver acpi_ec_driver = {
  863. .name = "ec",
  864. .class = ACPI_EC_CLASS,
  865. .ids = ec_device_ids,
  866. .ops = {
  867. .add = acpi_ec_add,
  868. .remove = acpi_ec_remove,
  869. .start = acpi_ec_start,
  870. .stop = acpi_ec_stop,
  871. .suspend = acpi_ec_suspend,
  872. .resume = acpi_ec_resume,
  873. },
  874. };
  875. static int __init acpi_ec_init(void)
  876. {
  877. int result = 0;
  878. if (acpi_disabled)
  879. return 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. subsys_initcall(acpi_ec_init);
  892. /* EC driver currently not unloadable */
  893. #if 0
  894. static void __exit acpi_ec_exit(void)
  895. {
  896. acpi_bus_unregister_driver(&acpi_ec_driver);
  897. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  898. return;
  899. }
  900. #endif /* 0 */