ec.c 25 KB

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