ec.c 25 KB

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