ec.c 25 KB

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