ec.c 26 KB

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