ec.c 27 KB

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