ec.c 25 KB

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