ec.c 26 KB

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