ec.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. msleep(1);
  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_handler(u32 function, acpi_physical_address address,
  462. u32 bits, acpi_integer *value,
  463. void *handler_context, void *region_context)
  464. {
  465. struct acpi_ec *ec = handler_context;
  466. int result = 0, i;
  467. u8 temp = 0;
  468. if ((address > 0xFF) || !value || !handler_context)
  469. return AE_BAD_PARAMETER;
  470. if (function != ACPI_READ && function != ACPI_WRITE)
  471. return AE_BAD_PARAMETER;
  472. if (bits != 8 && acpi_strict)
  473. return AE_BAD_PARAMETER;
  474. acpi_ec_burst_enable(ec);
  475. if (function == ACPI_READ) {
  476. result = acpi_ec_read(ec, address, &temp);
  477. *value = temp;
  478. } else {
  479. temp = 0xff & (*value);
  480. result = acpi_ec_write(ec, address, temp);
  481. }
  482. for (i = 8; unlikely(bits - i > 0); i += 8) {
  483. ++address;
  484. if (function == ACPI_READ) {
  485. result = acpi_ec_read(ec, address, &temp);
  486. (*value) |= ((acpi_integer)temp) << i;
  487. } else {
  488. temp = 0xff & ((*value) >> i);
  489. result = acpi_ec_write(ec, address, temp);
  490. }
  491. }
  492. acpi_ec_burst_disable(ec);
  493. switch (result) {
  494. case -EINVAL:
  495. return AE_BAD_PARAMETER;
  496. break;
  497. case -ENODEV:
  498. return AE_NOT_FOUND;
  499. break;
  500. case -ETIME:
  501. return AE_TIME;
  502. break;
  503. default:
  504. return AE_OK;
  505. }
  506. }
  507. /* --------------------------------------------------------------------------
  508. FS Interface (/proc)
  509. -------------------------------------------------------------------------- */
  510. static struct proc_dir_entry *acpi_ec_dir;
  511. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  512. {
  513. struct acpi_ec *ec = seq->private;
  514. if (!ec)
  515. goto end;
  516. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  517. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  518. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  519. seq_printf(seq, "use global lock:\t%s\n",
  520. ec->global_lock ? "yes" : "no");
  521. end:
  522. return 0;
  523. }
  524. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  525. {
  526. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  527. }
  528. static struct file_operations acpi_ec_info_ops = {
  529. .open = acpi_ec_info_open_fs,
  530. .read = seq_read,
  531. .llseek = seq_lseek,
  532. .release = single_release,
  533. .owner = THIS_MODULE,
  534. };
  535. static int acpi_ec_add_fs(struct acpi_device *device)
  536. {
  537. struct proc_dir_entry *entry = NULL;
  538. if (!acpi_device_dir(device)) {
  539. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  540. acpi_ec_dir);
  541. if (!acpi_device_dir(device))
  542. return -ENODEV;
  543. }
  544. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  545. acpi_device_dir(device),
  546. &acpi_ec_info_ops, acpi_driver_data(device));
  547. if (!entry)
  548. return -ENODEV;
  549. return 0;
  550. }
  551. static int acpi_ec_remove_fs(struct acpi_device *device)
  552. {
  553. if (acpi_device_dir(device)) {
  554. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  555. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  556. acpi_device_dir(device) = NULL;
  557. }
  558. return 0;
  559. }
  560. /* --------------------------------------------------------------------------
  561. Driver Interface
  562. -------------------------------------------------------------------------- */
  563. static acpi_status
  564. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  565. static struct acpi_ec *make_acpi_ec(void)
  566. {
  567. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  568. if (!ec)
  569. return NULL;
  570. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  571. mutex_init(&ec->lock);
  572. init_waitqueue_head(&ec->wait);
  573. INIT_LIST_HEAD(&ec->list);
  574. INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
  575. atomic_set(&ec->irq_count, 0);
  576. return ec;
  577. }
  578. static acpi_status
  579. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  580. void *context, void **return_value)
  581. {
  582. struct acpi_namespace_node *node = handle;
  583. struct acpi_ec *ec = context;
  584. int value = 0;
  585. if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
  586. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  587. }
  588. return AE_OK;
  589. }
  590. static acpi_status
  591. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  592. {
  593. acpi_status status;
  594. struct acpi_ec *ec = context;
  595. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  596. ec_parse_io_ports, ec);
  597. if (ACPI_FAILURE(status))
  598. return status;
  599. /* Get GPE bit assignment (EC events). */
  600. /* TODO: Add support for _GPE returning a package */
  601. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
  602. if (ACPI_FAILURE(status))
  603. return status;
  604. /* Use the global lock for all EC transactions? */
  605. acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
  606. ec->handle = handle;
  607. return AE_CTRL_TERMINATE;
  608. }
  609. static void ec_poll_stop(struct acpi_ec *ec)
  610. {
  611. clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
  612. cancel_delayed_work(&ec->work);
  613. }
  614. static void ec_remove_handlers(struct acpi_ec *ec)
  615. {
  616. ec_poll_stop(ec);
  617. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  618. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  619. pr_err(PREFIX "failed to remove space handler\n");
  620. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  621. &acpi_ec_gpe_handler)))
  622. pr_err(PREFIX "failed to remove gpe handler\n");
  623. ec->handlers_installed = 0;
  624. }
  625. static int acpi_ec_add(struct acpi_device *device)
  626. {
  627. struct acpi_ec *ec = NULL;
  628. if (!device)
  629. return -EINVAL;
  630. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  631. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  632. /* Check for boot EC */
  633. if (boot_ec &&
  634. (boot_ec->handle == device->handle ||
  635. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  636. ec = boot_ec;
  637. boot_ec = NULL;
  638. } else {
  639. ec = make_acpi_ec();
  640. if (!ec)
  641. return -ENOMEM;
  642. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  643. AE_CTRL_TERMINATE) {
  644. kfree(ec);
  645. return -EINVAL;
  646. }
  647. }
  648. ec->handle = device->handle;
  649. /* Find and register all query methods */
  650. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  651. acpi_ec_register_query_methods, ec, NULL);
  652. if (!first_ec)
  653. first_ec = ec;
  654. acpi_driver_data(device) = ec;
  655. acpi_ec_add_fs(device);
  656. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  657. ec->gpe, ec->command_addr, ec->data_addr);
  658. pr_info(PREFIX "driver started in %s mode\n",
  659. (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
  660. return 0;
  661. }
  662. static int acpi_ec_remove(struct acpi_device *device, int type)
  663. {
  664. struct acpi_ec *ec;
  665. struct acpi_ec_query_handler *handler, *tmp;
  666. if (!device)
  667. return -EINVAL;
  668. ec = acpi_driver_data(device);
  669. mutex_lock(&ec->lock);
  670. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  671. list_del(&handler->node);
  672. kfree(handler);
  673. }
  674. mutex_unlock(&ec->lock);
  675. acpi_ec_remove_fs(device);
  676. acpi_driver_data(device) = NULL;
  677. if (ec == first_ec)
  678. first_ec = NULL;
  679. kfree(ec);
  680. return 0;
  681. }
  682. static acpi_status
  683. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  684. {
  685. struct acpi_ec *ec = context;
  686. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  687. return AE_OK;
  688. /*
  689. * The first address region returned is the data port, and
  690. * the second address region returned is the status/command
  691. * port.
  692. */
  693. if (ec->data_addr == 0)
  694. ec->data_addr = resource->data.io.minimum;
  695. else if (ec->command_addr == 0)
  696. ec->command_addr = resource->data.io.minimum;
  697. else
  698. return AE_CTRL_TERMINATE;
  699. return AE_OK;
  700. }
  701. static int ec_install_handlers(struct acpi_ec *ec)
  702. {
  703. acpi_status status;
  704. if (ec->handlers_installed)
  705. return 0;
  706. status = acpi_install_gpe_handler(NULL, ec->gpe,
  707. ACPI_GPE_EDGE_TRIGGERED,
  708. &acpi_ec_gpe_handler, ec);
  709. if (ACPI_FAILURE(status))
  710. return -ENODEV;
  711. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  712. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  713. status = acpi_install_address_space_handler(ec->handle,
  714. ACPI_ADR_SPACE_EC,
  715. &acpi_ec_space_handler,
  716. NULL, ec);
  717. if (ACPI_FAILURE(status)) {
  718. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  719. return -ENODEV;
  720. }
  721. ec->handlers_installed = 1;
  722. return 0;
  723. }
  724. static int acpi_ec_start(struct acpi_device *device)
  725. {
  726. struct acpi_ec *ec;
  727. int ret = 0;
  728. if (!device)
  729. return -EINVAL;
  730. ec = acpi_driver_data(device);
  731. if (!ec)
  732. return -EINVAL;
  733. ret = ec_install_handlers(ec);
  734. /* EC is fully operational, allow queries */
  735. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  736. ec_schedule_ec_poll(ec);
  737. return ret;
  738. }
  739. static int acpi_ec_stop(struct acpi_device *device, int type)
  740. {
  741. struct acpi_ec *ec;
  742. if (!device)
  743. return -EINVAL;
  744. ec = acpi_driver_data(device);
  745. if (!ec)
  746. return -EINVAL;
  747. ec_remove_handlers(ec);
  748. return 0;
  749. }
  750. int __init acpi_boot_ec_enable(void)
  751. {
  752. if (!boot_ec || boot_ec->handlers_installed)
  753. return 0;
  754. if (!ec_install_handlers(boot_ec)) {
  755. first_ec = boot_ec;
  756. return 0;
  757. }
  758. return -EFAULT;
  759. }
  760. static const struct acpi_device_id ec_device_ids[] = {
  761. {"PNP0C09", 0},
  762. {"", 0},
  763. };
  764. int __init acpi_ec_ecdt_probe(void)
  765. {
  766. int ret;
  767. acpi_status status;
  768. struct acpi_table_ecdt *ecdt_ptr;
  769. boot_ec = make_acpi_ec();
  770. if (!boot_ec)
  771. return -ENOMEM;
  772. /*
  773. * Generate a boot ec context
  774. */
  775. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  776. (struct acpi_table_header **)&ecdt_ptr);
  777. if (ACPI_SUCCESS(status)) {
  778. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  779. boot_ec->command_addr = ecdt_ptr->control.address;
  780. boot_ec->data_addr = ecdt_ptr->data.address;
  781. boot_ec->gpe = ecdt_ptr->gpe;
  782. boot_ec->handle = ACPI_ROOT_OBJECT;
  783. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  784. } else {
  785. /* This workaround is needed only on some broken machines,
  786. * which require early EC, but fail to provide ECDT */
  787. acpi_handle x;
  788. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  789. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  790. boot_ec, NULL);
  791. /* Check that acpi_get_devices actually find something */
  792. if (ACPI_FAILURE(status) || !boot_ec->handle)
  793. goto error;
  794. /* We really need to limit this workaround, the only ASUS,
  795. * which needs it, has fake EC._INI method, so use it as flag.
  796. * Keep boot_ec struct as it will be needed soon.
  797. */
  798. if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
  799. return -ENODEV;
  800. }
  801. ret = ec_install_handlers(boot_ec);
  802. if (!ret) {
  803. first_ec = boot_ec;
  804. return 0;
  805. }
  806. error:
  807. kfree(boot_ec);
  808. boot_ec = NULL;
  809. return -ENODEV;
  810. }
  811. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  812. {
  813. struct acpi_ec *ec = acpi_driver_data(device);
  814. /* Stop using GPE */
  815. set_bit(EC_FLAGS_NO_GPE, &ec->flags);
  816. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  817. acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  818. return 0;
  819. }
  820. static int acpi_ec_resume(struct acpi_device *device)
  821. {
  822. struct acpi_ec *ec = acpi_driver_data(device);
  823. /* Enable use of GPE back */
  824. clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
  825. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  826. return 0;
  827. }
  828. static struct acpi_driver acpi_ec_driver = {
  829. .name = "ec",
  830. .class = ACPI_EC_CLASS,
  831. .ids = ec_device_ids,
  832. .ops = {
  833. .add = acpi_ec_add,
  834. .remove = acpi_ec_remove,
  835. .start = acpi_ec_start,
  836. .stop = acpi_ec_stop,
  837. .suspend = acpi_ec_suspend,
  838. .resume = acpi_ec_resume,
  839. },
  840. };
  841. static int __init acpi_ec_init(void)
  842. {
  843. int result = 0;
  844. if (acpi_disabled)
  845. return 0;
  846. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  847. if (!acpi_ec_dir)
  848. return -ENODEV;
  849. /* Now register the driver for the EC */
  850. result = acpi_bus_register_driver(&acpi_ec_driver);
  851. if (result < 0) {
  852. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  853. return -ENODEV;
  854. }
  855. return result;
  856. }
  857. subsys_initcall(acpi_ec_init);
  858. /* EC driver currently not unloadable */
  859. #if 0
  860. static void __exit acpi_ec_exit(void)
  861. {
  862. acpi_bus_unregister_driver(&acpi_ec_driver);
  863. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  864. return;
  865. }
  866. #endif /* 0 */