ec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 = 0;
  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. while (bits - i > 0) {
  497. if (function == ACPI_READ) {
  498. result = acpi_ec_read(ec, address, &temp);
  499. (*value) |= ((acpi_integer)temp) << i;
  500. } else {
  501. temp = 0xff & ((*value) >> i);
  502. result = acpi_ec_write(ec, address, temp);
  503. }
  504. i += 8;
  505. ++address;
  506. }
  507. switch (result) {
  508. case -EINVAL:
  509. return AE_BAD_PARAMETER;
  510. break;
  511. case -ENODEV:
  512. return AE_NOT_FOUND;
  513. break;
  514. case -ETIME:
  515. return AE_TIME;
  516. break;
  517. default:
  518. return AE_OK;
  519. }
  520. }
  521. /* --------------------------------------------------------------------------
  522. FS Interface (/proc)
  523. -------------------------------------------------------------------------- */
  524. static struct proc_dir_entry *acpi_ec_dir;
  525. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  526. {
  527. struct acpi_ec *ec = seq->private;
  528. if (!ec)
  529. goto end;
  530. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  531. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  532. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  533. seq_printf(seq, "use global lock:\t%s\n",
  534. ec->global_lock ? "yes" : "no");
  535. end:
  536. return 0;
  537. }
  538. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  539. {
  540. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  541. }
  542. static struct file_operations acpi_ec_info_ops = {
  543. .open = acpi_ec_info_open_fs,
  544. .read = seq_read,
  545. .llseek = seq_lseek,
  546. .release = single_release,
  547. .owner = THIS_MODULE,
  548. };
  549. static int acpi_ec_add_fs(struct acpi_device *device)
  550. {
  551. struct proc_dir_entry *entry = NULL;
  552. if (!acpi_device_dir(device)) {
  553. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  554. acpi_ec_dir);
  555. if (!acpi_device_dir(device))
  556. return -ENODEV;
  557. }
  558. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  559. acpi_device_dir(device));
  560. if (!entry)
  561. return -ENODEV;
  562. else {
  563. entry->proc_fops = &acpi_ec_info_ops;
  564. entry->data = acpi_driver_data(device);
  565. entry->owner = THIS_MODULE;
  566. }
  567. return 0;
  568. }
  569. static int acpi_ec_remove_fs(struct acpi_device *device)
  570. {
  571. if (acpi_device_dir(device)) {
  572. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  573. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  574. acpi_device_dir(device) = NULL;
  575. }
  576. return 0;
  577. }
  578. /* --------------------------------------------------------------------------
  579. Driver Interface
  580. -------------------------------------------------------------------------- */
  581. static acpi_status
  582. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  583. static struct acpi_ec *make_acpi_ec(void)
  584. {
  585. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  586. if (!ec)
  587. return NULL;
  588. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  589. mutex_init(&ec->lock);
  590. init_waitqueue_head(&ec->wait);
  591. INIT_LIST_HEAD(&ec->list);
  592. return ec;
  593. }
  594. static acpi_status
  595. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  596. void *context, void **return_value)
  597. {
  598. struct acpi_namespace_node *node = handle;
  599. struct acpi_ec *ec = context;
  600. int value = 0;
  601. if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
  602. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  603. }
  604. return AE_OK;
  605. }
  606. static acpi_status
  607. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  608. {
  609. acpi_status status;
  610. struct acpi_ec *ec = context;
  611. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  612. ec_parse_io_ports, ec);
  613. if (ACPI_FAILURE(status))
  614. return status;
  615. /* Get GPE bit assignment (EC events). */
  616. /* TODO: Add support for _GPE returning a package */
  617. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
  618. if (ACPI_FAILURE(status))
  619. return status;
  620. /* Find and register all query methods */
  621. acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
  622. acpi_ec_register_query_methods, ec, NULL);
  623. /* Use the global lock for all EC transactions? */
  624. acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
  625. ec->handle = handle;
  626. return AE_CTRL_TERMINATE;
  627. }
  628. static void ec_remove_handlers(struct acpi_ec *ec)
  629. {
  630. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  631. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  632. pr_err(PREFIX "failed to remove space handler\n");
  633. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  634. &acpi_ec_gpe_handler)))
  635. pr_err(PREFIX "failed to remove gpe handler\n");
  636. ec->handlers_installed = 0;
  637. }
  638. static int acpi_ec_add(struct acpi_device *device)
  639. {
  640. struct acpi_ec *ec = NULL;
  641. if (!device)
  642. return -EINVAL;
  643. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  644. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  645. /* Check for boot EC */
  646. if (boot_ec) {
  647. if (boot_ec->handle == device->handle) {
  648. /* Pre-loaded EC from DSDT, just move pointer */
  649. ec = boot_ec;
  650. boot_ec = NULL;
  651. goto end;
  652. } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
  653. /* ECDT-based EC, time to shut it down */
  654. ec_remove_handlers(boot_ec);
  655. kfree(boot_ec);
  656. first_ec = boot_ec = NULL;
  657. }
  658. }
  659. ec = make_acpi_ec();
  660. if (!ec)
  661. return -ENOMEM;
  662. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  663. AE_CTRL_TERMINATE) {
  664. kfree(ec);
  665. return -EINVAL;
  666. }
  667. ec->handle = device->handle;
  668. end:
  669. if (!first_ec)
  670. first_ec = ec;
  671. acpi_driver_data(device) = ec;
  672. acpi_ec_add_fs(device);
  673. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  674. ec->gpe, ec->command_addr, ec->data_addr);
  675. pr_info(PREFIX "driver started in %s mode\n",
  676. (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
  677. return 0;
  678. }
  679. static int acpi_ec_remove(struct acpi_device *device, int type)
  680. {
  681. struct acpi_ec *ec;
  682. struct acpi_ec_query_handler *handler, *tmp;
  683. if (!device)
  684. return -EINVAL;
  685. ec = acpi_driver_data(device);
  686. mutex_lock(&ec->lock);
  687. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  688. list_del(&handler->node);
  689. kfree(handler);
  690. }
  691. mutex_unlock(&ec->lock);
  692. acpi_ec_remove_fs(device);
  693. acpi_driver_data(device) = NULL;
  694. if (ec == first_ec)
  695. first_ec = NULL;
  696. kfree(ec);
  697. return 0;
  698. }
  699. static acpi_status
  700. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  701. {
  702. struct acpi_ec *ec = context;
  703. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  704. return AE_OK;
  705. /*
  706. * The first address region returned is the data port, and
  707. * the second address region returned is the status/command
  708. * port.
  709. */
  710. if (ec->data_addr == 0)
  711. ec->data_addr = resource->data.io.minimum;
  712. else if (ec->command_addr == 0)
  713. ec->command_addr = resource->data.io.minimum;
  714. else
  715. return AE_CTRL_TERMINATE;
  716. return AE_OK;
  717. }
  718. static int ec_install_handlers(struct acpi_ec *ec)
  719. {
  720. acpi_status status;
  721. if (ec->handlers_installed)
  722. return 0;
  723. status = acpi_install_gpe_handler(NULL, ec->gpe,
  724. ACPI_GPE_EDGE_TRIGGERED,
  725. &acpi_ec_gpe_handler, ec);
  726. if (ACPI_FAILURE(status))
  727. return -ENODEV;
  728. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  729. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  730. status = acpi_install_address_space_handler(ec->handle,
  731. ACPI_ADR_SPACE_EC,
  732. &acpi_ec_space_handler,
  733. &acpi_ec_space_setup, ec);
  734. if (ACPI_FAILURE(status)) {
  735. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  736. return -ENODEV;
  737. }
  738. ec->handlers_installed = 1;
  739. return 0;
  740. }
  741. static int acpi_ec_start(struct acpi_device *device)
  742. {
  743. struct acpi_ec *ec;
  744. int ret = 0;
  745. if (!device)
  746. return -EINVAL;
  747. ec = acpi_driver_data(device);
  748. if (!ec)
  749. return -EINVAL;
  750. ret = ec_install_handlers(ec);
  751. /* EC is fully operational, allow queries */
  752. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  753. return ret;
  754. }
  755. static int acpi_ec_stop(struct acpi_device *device, int type)
  756. {
  757. struct acpi_ec *ec;
  758. if (!device)
  759. return -EINVAL;
  760. ec = acpi_driver_data(device);
  761. if (!ec)
  762. return -EINVAL;
  763. ec_remove_handlers(ec);
  764. return 0;
  765. }
  766. int __init acpi_boot_ec_enable(void)
  767. {
  768. if (!boot_ec || boot_ec->handlers_installed)
  769. return 0;
  770. if (!ec_install_handlers(boot_ec)) {
  771. first_ec = boot_ec;
  772. return 0;
  773. }
  774. return -EFAULT;
  775. }
  776. int __init acpi_ec_ecdt_probe(void)
  777. {
  778. int ret;
  779. acpi_status status;
  780. struct acpi_table_ecdt *ecdt_ptr;
  781. boot_ec = make_acpi_ec();
  782. if (!boot_ec)
  783. return -ENOMEM;
  784. /*
  785. * Generate a boot ec context
  786. */
  787. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  788. (struct acpi_table_header **)&ecdt_ptr);
  789. if (ACPI_SUCCESS(status)) {
  790. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  791. boot_ec->command_addr = ecdt_ptr->control.address;
  792. boot_ec->data_addr = ecdt_ptr->data.address;
  793. boot_ec->gpe = ecdt_ptr->gpe;
  794. boot_ec->handle = ACPI_ROOT_OBJECT;
  795. } else {
  796. /* This workaround is needed only on some broken machines,
  797. * which require early EC, but fail to provide ECDT */
  798. acpi_handle x;
  799. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  800. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  801. boot_ec, NULL);
  802. /* Check that acpi_get_devices actually find something */
  803. if (ACPI_FAILURE(status) || !boot_ec->handle)
  804. goto error;
  805. /* We really need to limit this workaround, the only ASUS,
  806. * which needs it, has fake EC._INI method, so use it as flag.
  807. * Keep boot_ec struct as it will be needed soon.
  808. */
  809. if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
  810. return -ENODEV;
  811. }
  812. ret = ec_install_handlers(boot_ec);
  813. if (!ret) {
  814. first_ec = boot_ec;
  815. return 0;
  816. }
  817. error:
  818. kfree(boot_ec);
  819. boot_ec = NULL;
  820. return -ENODEV;
  821. }
  822. static int __init acpi_ec_init(void)
  823. {
  824. int result = 0;
  825. if (acpi_disabled)
  826. return 0;
  827. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  828. if (!acpi_ec_dir)
  829. return -ENODEV;
  830. /* Now register the driver for the EC */
  831. result = acpi_bus_register_driver(&acpi_ec_driver);
  832. if (result < 0) {
  833. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  834. return -ENODEV;
  835. }
  836. return result;
  837. }
  838. subsys_initcall(acpi_ec_init);
  839. /* EC driver currently not unloadable */
  840. #if 0
  841. static void __exit acpi_ec_exit(void)
  842. {
  843. acpi_bus_unregister_driver(&acpi_ec_driver);
  844. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  845. return;
  846. }
  847. #endif /* 0 */