ec.c 26 KB

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