ec.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v2.1)
  3. *
  4. * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  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 printout */
  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 <linux/spinlock.h>
  40. #include <asm/io.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/acpi_drivers.h>
  43. #include <linux/dmi.h>
  44. #define ACPI_EC_CLASS "embedded_controller"
  45. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  46. #define ACPI_EC_FILE_INFO "info"
  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. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  62. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  63. #define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
  64. #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
  65. #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
  66. per one transaction */
  67. enum {
  68. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  69. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  70. EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
  71. * OpReg are installed */
  72. };
  73. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  74. /* External interfaces use first EC only, so remember */
  75. typedef int (*acpi_ec_query_func) (void *data);
  76. struct acpi_ec_query_handler {
  77. struct list_head node;
  78. acpi_ec_query_func func;
  79. acpi_handle handle;
  80. void *data;
  81. u8 query_bit;
  82. };
  83. struct transaction {
  84. const u8 *wdata;
  85. u8 *rdata;
  86. unsigned short irq_count;
  87. u8 command;
  88. u8 wi;
  89. u8 ri;
  90. u8 wlen;
  91. u8 rlen;
  92. bool done;
  93. };
  94. static struct acpi_ec {
  95. acpi_handle handle;
  96. unsigned long gpe;
  97. unsigned long command_addr;
  98. unsigned long data_addr;
  99. unsigned long global_lock;
  100. unsigned long flags;
  101. struct mutex lock;
  102. wait_queue_head_t wait;
  103. struct list_head list;
  104. struct transaction *curr;
  105. spinlock_t curr_lock;
  106. } *boot_ec, *first_ec;
  107. static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
  108. static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
  109. static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
  110. /* --------------------------------------------------------------------------
  111. Transaction Management
  112. -------------------------------------------------------------------------- */
  113. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  114. {
  115. u8 x = inb(ec->command_addr);
  116. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  117. return x;
  118. }
  119. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  120. {
  121. u8 x = inb(ec->data_addr);
  122. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  123. return x;
  124. }
  125. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  126. {
  127. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  128. outb(command, ec->command_addr);
  129. }
  130. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  131. {
  132. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  133. outb(data, ec->data_addr);
  134. }
  135. static int ec_transaction_done(struct acpi_ec *ec)
  136. {
  137. unsigned long flags;
  138. int ret = 0;
  139. spin_lock_irqsave(&ec->curr_lock, flags);
  140. if (!ec->curr || ec->curr->done)
  141. ret = 1;
  142. spin_unlock_irqrestore(&ec->curr_lock, flags);
  143. return ret;
  144. }
  145. static void start_transaction(struct acpi_ec *ec)
  146. {
  147. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  148. ec->curr->done = false;
  149. acpi_ec_write_cmd(ec, ec->curr->command);
  150. }
  151. static void advance_transaction(struct acpi_ec *ec, u8 status)
  152. {
  153. unsigned long flags;
  154. spin_lock_irqsave(&ec->curr_lock, flags);
  155. if (!ec->curr)
  156. goto unlock;
  157. if (ec->curr->wlen > ec->curr->wi) {
  158. if ((status & ACPI_EC_FLAG_IBF) == 0)
  159. acpi_ec_write_data(ec,
  160. ec->curr->wdata[ec->curr->wi++]);
  161. else
  162. goto err;
  163. } else if (ec->curr->rlen > ec->curr->ri) {
  164. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  165. ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
  166. if (ec->curr->rlen == ec->curr->ri)
  167. ec->curr->done = true;
  168. } else
  169. goto err;
  170. } else if (ec->curr->wlen == ec->curr->wi &&
  171. (status & ACPI_EC_FLAG_IBF) == 0)
  172. ec->curr->done = true;
  173. goto unlock;
  174. err:
  175. /* false interrupt, state didn't change */
  176. if (in_interrupt())
  177. ++ec->curr->irq_count;
  178. unlock:
  179. spin_unlock_irqrestore(&ec->curr_lock, flags);
  180. }
  181. static int acpi_ec_sync_query(struct acpi_ec *ec);
  182. static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
  183. {
  184. if (state & ACPI_EC_FLAG_SCI) {
  185. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  186. return acpi_ec_sync_query(ec);
  187. }
  188. return 0;
  189. }
  190. static int ec_poll(struct acpi_ec *ec)
  191. {
  192. unsigned long flags;
  193. int repeat = 2; /* number of command restarts */
  194. while (repeat--) {
  195. unsigned long delay = jiffies +
  196. msecs_to_jiffies(ACPI_EC_DELAY);
  197. do {
  198. /* don't sleep with disabled interrupts */
  199. if (EC_FLAGS_MSI || irqs_disabled()) {
  200. udelay(ACPI_EC_MSI_UDELAY);
  201. if (ec_transaction_done(ec))
  202. return 0;
  203. } else {
  204. if (wait_event_timeout(ec->wait,
  205. ec_transaction_done(ec),
  206. msecs_to_jiffies(1)))
  207. return 0;
  208. }
  209. advance_transaction(ec, acpi_ec_read_status(ec));
  210. } while (time_before(jiffies, delay));
  211. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
  212. break;
  213. pr_debug(PREFIX "controller reset, restart transaction\n");
  214. spin_lock_irqsave(&ec->curr_lock, flags);
  215. start_transaction(ec);
  216. spin_unlock_irqrestore(&ec->curr_lock, flags);
  217. }
  218. return -ETIME;
  219. }
  220. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  221. struct transaction *t)
  222. {
  223. unsigned long tmp;
  224. int ret = 0;
  225. if (EC_FLAGS_MSI)
  226. udelay(ACPI_EC_MSI_UDELAY);
  227. /* start transaction */
  228. spin_lock_irqsave(&ec->curr_lock, tmp);
  229. /* following two actions should be kept atomic */
  230. ec->curr = t;
  231. start_transaction(ec);
  232. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  233. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  234. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  235. ret = ec_poll(ec);
  236. spin_lock_irqsave(&ec->curr_lock, tmp);
  237. ec->curr = NULL;
  238. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  239. return ret;
  240. }
  241. static int ec_check_ibf0(struct acpi_ec *ec)
  242. {
  243. u8 status = acpi_ec_read_status(ec);
  244. return (status & ACPI_EC_FLAG_IBF) == 0;
  245. }
  246. static int ec_wait_ibf0(struct acpi_ec *ec)
  247. {
  248. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  249. /* interrupt wait manually if GPE mode is not active */
  250. while (time_before(jiffies, delay))
  251. if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
  252. msecs_to_jiffies(1)))
  253. return 0;
  254. return -ETIME;
  255. }
  256. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  257. {
  258. int status;
  259. u32 glk;
  260. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  261. return -EINVAL;
  262. if (t->rdata)
  263. memset(t->rdata, 0, t->rlen);
  264. mutex_lock(&ec->lock);
  265. if (ec->global_lock) {
  266. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  267. if (ACPI_FAILURE(status)) {
  268. status = -ENODEV;
  269. goto unlock;
  270. }
  271. }
  272. if (ec_wait_ibf0(ec)) {
  273. pr_err(PREFIX "input buffer is not empty, "
  274. "aborting transaction\n");
  275. status = -ETIME;
  276. goto end;
  277. }
  278. pr_debug(PREFIX "transaction start\n");
  279. /* disable GPE during transaction if storm is detected */
  280. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  281. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  282. }
  283. status = acpi_ec_transaction_unlocked(ec, t);
  284. /* check if we received SCI during transaction */
  285. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  286. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  287. msleep(1);
  288. /* it is safe to enable GPE outside of transaction */
  289. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  290. } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
  291. pr_info(PREFIX "GPE storm detected, "
  292. "transactions will use polling mode\n");
  293. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  294. }
  295. pr_debug(PREFIX "transaction end\n");
  296. end:
  297. if (ec->global_lock)
  298. acpi_release_global_lock(glk);
  299. unlock:
  300. mutex_unlock(&ec->lock);
  301. return status;
  302. }
  303. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  304. {
  305. u8 d;
  306. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  307. .wdata = NULL, .rdata = &d,
  308. .wlen = 0, .rlen = 1};
  309. return acpi_ec_transaction(ec, &t);
  310. }
  311. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  312. {
  313. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  314. .wdata = NULL, .rdata = NULL,
  315. .wlen = 0, .rlen = 0};
  316. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  317. acpi_ec_transaction(ec, &t) : 0;
  318. }
  319. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  320. {
  321. int result;
  322. u8 d;
  323. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  324. .wdata = &address, .rdata = &d,
  325. .wlen = 1, .rlen = 1};
  326. result = acpi_ec_transaction(ec, &t);
  327. *data = d;
  328. return result;
  329. }
  330. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  331. {
  332. u8 wdata[2] = { address, data };
  333. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  334. .wdata = wdata, .rdata = NULL,
  335. .wlen = 2, .rlen = 0};
  336. return acpi_ec_transaction(ec, &t);
  337. }
  338. /*
  339. * Externally callable EC access functions. For now, assume 1 EC only
  340. */
  341. int ec_burst_enable(void)
  342. {
  343. if (!first_ec)
  344. return -ENODEV;
  345. return acpi_ec_burst_enable(first_ec);
  346. }
  347. EXPORT_SYMBOL(ec_burst_enable);
  348. int ec_burst_disable(void)
  349. {
  350. if (!first_ec)
  351. return -ENODEV;
  352. return acpi_ec_burst_disable(first_ec);
  353. }
  354. EXPORT_SYMBOL(ec_burst_disable);
  355. int ec_read(u8 addr, u8 * val)
  356. {
  357. int err;
  358. u8 temp_data;
  359. if (!first_ec)
  360. return -ENODEV;
  361. err = acpi_ec_read(first_ec, addr, &temp_data);
  362. if (!err) {
  363. *val = temp_data;
  364. return 0;
  365. } else
  366. return err;
  367. }
  368. EXPORT_SYMBOL(ec_read);
  369. int ec_write(u8 addr, u8 val)
  370. {
  371. int err;
  372. if (!first_ec)
  373. return -ENODEV;
  374. err = acpi_ec_write(first_ec, addr, val);
  375. return err;
  376. }
  377. EXPORT_SYMBOL(ec_write);
  378. int ec_transaction(u8 command,
  379. const u8 * wdata, unsigned wdata_len,
  380. u8 * rdata, unsigned rdata_len,
  381. int force_poll)
  382. {
  383. struct transaction t = {.command = command,
  384. .wdata = wdata, .rdata = rdata,
  385. .wlen = wdata_len, .rlen = rdata_len};
  386. if (!first_ec)
  387. return -ENODEV;
  388. return acpi_ec_transaction(first_ec, &t);
  389. }
  390. EXPORT_SYMBOL(ec_transaction);
  391. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  392. {
  393. int result;
  394. u8 d;
  395. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  396. .wdata = NULL, .rdata = &d,
  397. .wlen = 0, .rlen = 1};
  398. if (!ec || !data)
  399. return -EINVAL;
  400. /*
  401. * Query the EC to find out which _Qxx method we need to evaluate.
  402. * Note that successful completion of the query causes the ACPI_EC_SCI
  403. * bit to be cleared (and thus clearing the interrupt source).
  404. */
  405. result = acpi_ec_transaction_unlocked(ec, &t);
  406. if (result)
  407. return result;
  408. if (!d)
  409. return -ENODATA;
  410. *data = d;
  411. return 0;
  412. }
  413. /* --------------------------------------------------------------------------
  414. Event Management
  415. -------------------------------------------------------------------------- */
  416. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  417. acpi_handle handle, acpi_ec_query_func func,
  418. void *data)
  419. {
  420. struct acpi_ec_query_handler *handler =
  421. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  422. if (!handler)
  423. return -ENOMEM;
  424. handler->query_bit = query_bit;
  425. handler->handle = handle;
  426. handler->func = func;
  427. handler->data = data;
  428. mutex_lock(&ec->lock);
  429. list_add(&handler->node, &ec->list);
  430. mutex_unlock(&ec->lock);
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  434. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  435. {
  436. struct acpi_ec_query_handler *handler, *tmp;
  437. mutex_lock(&ec->lock);
  438. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  439. if (query_bit == handler->query_bit) {
  440. list_del(&handler->node);
  441. kfree(handler);
  442. }
  443. }
  444. mutex_unlock(&ec->lock);
  445. }
  446. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  447. static void acpi_ec_run(void *cxt)
  448. {
  449. struct acpi_ec_query_handler *handler = cxt;
  450. if (!handler)
  451. return;
  452. pr_debug(PREFIX "start query execution\n");
  453. if (handler->func)
  454. handler->func(handler->data);
  455. else if (handler->handle)
  456. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  457. pr_debug(PREFIX "stop query execution\n");
  458. kfree(handler);
  459. }
  460. static int acpi_ec_sync_query(struct acpi_ec *ec)
  461. {
  462. u8 value = 0;
  463. int status;
  464. struct acpi_ec_query_handler *handler, *copy;
  465. if ((status = acpi_ec_query_unlocked(ec, &value)))
  466. return status;
  467. list_for_each_entry(handler, &ec->list, node) {
  468. if (value == handler->query_bit) {
  469. /* have custom handler for this bit */
  470. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  471. if (!copy)
  472. return -ENOMEM;
  473. memcpy(copy, handler, sizeof(*copy));
  474. pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
  475. return acpi_os_execute((copy->func) ?
  476. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  477. acpi_ec_run, copy);
  478. }
  479. }
  480. return 0;
  481. }
  482. static void acpi_ec_gpe_query(void *ec_cxt)
  483. {
  484. struct acpi_ec *ec = ec_cxt;
  485. if (!ec)
  486. return;
  487. mutex_lock(&ec->lock);
  488. acpi_ec_sync_query(ec);
  489. mutex_unlock(&ec->lock);
  490. }
  491. static void acpi_ec_gpe_query(void *ec_cxt);
  492. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  493. {
  494. if (state & ACPI_EC_FLAG_SCI) {
  495. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  496. pr_debug(PREFIX "push gpe query to the queue\n");
  497. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  498. acpi_ec_gpe_query, ec);
  499. }
  500. }
  501. return 0;
  502. }
  503. static u32 acpi_ec_gpe_handler(void *data)
  504. {
  505. struct acpi_ec *ec = data;
  506. pr_debug(PREFIX "~~~> interrupt\n");
  507. advance_transaction(ec, acpi_ec_read_status(ec));
  508. if (ec_transaction_done(ec) &&
  509. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
  510. wake_up(&ec->wait);
  511. ec_check_sci(ec, acpi_ec_read_status(ec));
  512. }
  513. return ACPI_INTERRUPT_HANDLED;
  514. }
  515. /* --------------------------------------------------------------------------
  516. Address Space Management
  517. -------------------------------------------------------------------------- */
  518. static acpi_status
  519. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  520. u32 bits, acpi_integer *value,
  521. void *handler_context, void *region_context)
  522. {
  523. struct acpi_ec *ec = handler_context;
  524. int result = 0, i;
  525. u8 temp = 0;
  526. if ((address > 0xFF) || !value || !handler_context)
  527. return AE_BAD_PARAMETER;
  528. if (function != ACPI_READ && function != ACPI_WRITE)
  529. return AE_BAD_PARAMETER;
  530. if (bits != 8 && acpi_strict)
  531. return AE_BAD_PARAMETER;
  532. if (EC_FLAGS_MSI)
  533. acpi_ec_burst_enable(ec);
  534. if (function == ACPI_READ) {
  535. result = acpi_ec_read(ec, address, &temp);
  536. *value = temp;
  537. } else {
  538. temp = 0xff & (*value);
  539. result = acpi_ec_write(ec, address, temp);
  540. }
  541. for (i = 8; unlikely(bits - i > 0); i += 8) {
  542. ++address;
  543. if (function == ACPI_READ) {
  544. result = acpi_ec_read(ec, address, &temp);
  545. (*value) |= ((acpi_integer)temp) << i;
  546. } else {
  547. temp = 0xff & ((*value) >> i);
  548. result = acpi_ec_write(ec, address, temp);
  549. }
  550. }
  551. if (EC_FLAGS_MSI)
  552. acpi_ec_burst_disable(ec);
  553. switch (result) {
  554. case -EINVAL:
  555. return AE_BAD_PARAMETER;
  556. break;
  557. case -ENODEV:
  558. return AE_NOT_FOUND;
  559. break;
  560. case -ETIME:
  561. return AE_TIME;
  562. break;
  563. default:
  564. return AE_OK;
  565. }
  566. }
  567. /* --------------------------------------------------------------------------
  568. FS Interface (/proc)
  569. -------------------------------------------------------------------------- */
  570. static struct proc_dir_entry *acpi_ec_dir;
  571. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  572. {
  573. struct acpi_ec *ec = seq->private;
  574. if (!ec)
  575. goto end;
  576. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  577. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  578. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  579. seq_printf(seq, "use global lock:\t%s\n",
  580. ec->global_lock ? "yes" : "no");
  581. end:
  582. return 0;
  583. }
  584. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  585. {
  586. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  587. }
  588. static const struct file_operations acpi_ec_info_ops = {
  589. .open = acpi_ec_info_open_fs,
  590. .read = seq_read,
  591. .llseek = seq_lseek,
  592. .release = single_release,
  593. .owner = THIS_MODULE,
  594. };
  595. static int acpi_ec_add_fs(struct acpi_device *device)
  596. {
  597. struct proc_dir_entry *entry = NULL;
  598. if (!acpi_device_dir(device)) {
  599. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  600. acpi_ec_dir);
  601. if (!acpi_device_dir(device))
  602. return -ENODEV;
  603. }
  604. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  605. acpi_device_dir(device),
  606. &acpi_ec_info_ops, acpi_driver_data(device));
  607. if (!entry)
  608. return -ENODEV;
  609. return 0;
  610. }
  611. static int acpi_ec_remove_fs(struct acpi_device *device)
  612. {
  613. if (acpi_device_dir(device)) {
  614. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  615. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  616. acpi_device_dir(device) = NULL;
  617. }
  618. return 0;
  619. }
  620. /* --------------------------------------------------------------------------
  621. Driver Interface
  622. -------------------------------------------------------------------------- */
  623. static acpi_status
  624. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  625. static struct acpi_ec *make_acpi_ec(void)
  626. {
  627. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  628. if (!ec)
  629. return NULL;
  630. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  631. mutex_init(&ec->lock);
  632. init_waitqueue_head(&ec->wait);
  633. INIT_LIST_HEAD(&ec->list);
  634. spin_lock_init(&ec->curr_lock);
  635. return ec;
  636. }
  637. static acpi_status
  638. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  639. void *context, void **return_value)
  640. {
  641. char node_name[5];
  642. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  643. struct acpi_ec *ec = context;
  644. int value = 0;
  645. acpi_status status;
  646. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  647. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  648. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  649. }
  650. return AE_OK;
  651. }
  652. static acpi_status
  653. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  654. {
  655. acpi_status status;
  656. unsigned long long tmp = 0;
  657. struct acpi_ec *ec = context;
  658. /* clear addr values, ec_parse_io_ports depend on it */
  659. ec->command_addr = ec->data_addr = 0;
  660. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  661. ec_parse_io_ports, ec);
  662. if (ACPI_FAILURE(status))
  663. return status;
  664. /* Get GPE bit assignment (EC events). */
  665. /* TODO: Add support for _GPE returning a package */
  666. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  667. if (ACPI_FAILURE(status))
  668. return status;
  669. ec->gpe = tmp;
  670. /* Use the global lock for all EC transactions? */
  671. tmp = 0;
  672. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  673. ec->global_lock = tmp;
  674. ec->handle = handle;
  675. return AE_CTRL_TERMINATE;
  676. }
  677. static int ec_install_handlers(struct acpi_ec *ec)
  678. {
  679. acpi_status status;
  680. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  681. return 0;
  682. status = acpi_install_gpe_handler(NULL, ec->gpe,
  683. ACPI_GPE_EDGE_TRIGGERED,
  684. &acpi_ec_gpe_handler, ec);
  685. if (ACPI_FAILURE(status))
  686. return -ENODEV;
  687. acpi_enable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  688. status = acpi_install_address_space_handler(ec->handle,
  689. ACPI_ADR_SPACE_EC,
  690. &acpi_ec_space_handler,
  691. NULL, ec);
  692. if (ACPI_FAILURE(status)) {
  693. if (status == AE_NOT_FOUND) {
  694. /*
  695. * Maybe OS fails in evaluating the _REG object.
  696. * The AE_NOT_FOUND error will be ignored and OS
  697. * continue to initialize EC.
  698. */
  699. printk(KERN_ERR "Fail in evaluating the _REG object"
  700. " of EC device. Broken bios is suspected.\n");
  701. } else {
  702. acpi_remove_gpe_handler(NULL, ec->gpe,
  703. &acpi_ec_gpe_handler);
  704. acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  705. return -ENODEV;
  706. }
  707. }
  708. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  709. return 0;
  710. }
  711. static void ec_remove_handlers(struct acpi_ec *ec)
  712. {
  713. acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  714. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  715. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  716. pr_err(PREFIX "failed to remove space handler\n");
  717. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  718. &acpi_ec_gpe_handler)))
  719. pr_err(PREFIX "failed to remove gpe handler\n");
  720. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  721. }
  722. static int acpi_ec_add(struct acpi_device *device)
  723. {
  724. struct acpi_ec *ec = NULL;
  725. int ret;
  726. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  727. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  728. /* Check for boot EC */
  729. if (boot_ec &&
  730. (boot_ec->handle == device->handle ||
  731. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  732. ec = boot_ec;
  733. boot_ec = NULL;
  734. } else {
  735. ec = make_acpi_ec();
  736. if (!ec)
  737. return -ENOMEM;
  738. }
  739. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  740. AE_CTRL_TERMINATE) {
  741. kfree(ec);
  742. return -EINVAL;
  743. }
  744. ec->handle = device->handle;
  745. /* Find and register all query methods */
  746. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  747. acpi_ec_register_query_methods, NULL, ec, NULL);
  748. if (!first_ec)
  749. first_ec = ec;
  750. device->driver_data = ec;
  751. acpi_ec_add_fs(device);
  752. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  753. ec->gpe, ec->command_addr, ec->data_addr);
  754. ret = ec_install_handlers(ec);
  755. /* EC is fully operational, allow queries */
  756. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  757. return ret;
  758. }
  759. static int acpi_ec_remove(struct acpi_device *device, int type)
  760. {
  761. struct acpi_ec *ec;
  762. struct acpi_ec_query_handler *handler, *tmp;
  763. if (!device)
  764. return -EINVAL;
  765. ec = acpi_driver_data(device);
  766. ec_remove_handlers(ec);
  767. mutex_lock(&ec->lock);
  768. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  769. list_del(&handler->node);
  770. kfree(handler);
  771. }
  772. mutex_unlock(&ec->lock);
  773. acpi_ec_remove_fs(device);
  774. device->driver_data = NULL;
  775. if (ec == first_ec)
  776. first_ec = NULL;
  777. kfree(ec);
  778. return 0;
  779. }
  780. static acpi_status
  781. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  782. {
  783. struct acpi_ec *ec = context;
  784. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  785. return AE_OK;
  786. /*
  787. * The first address region returned is the data port, and
  788. * the second address region returned is the status/command
  789. * port.
  790. */
  791. if (ec->data_addr == 0)
  792. ec->data_addr = resource->data.io.minimum;
  793. else if (ec->command_addr == 0)
  794. ec->command_addr = resource->data.io.minimum;
  795. else
  796. return AE_CTRL_TERMINATE;
  797. return AE_OK;
  798. }
  799. int __init acpi_boot_ec_enable(void)
  800. {
  801. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  802. return 0;
  803. if (!ec_install_handlers(boot_ec)) {
  804. first_ec = boot_ec;
  805. return 0;
  806. }
  807. return -EFAULT;
  808. }
  809. static const struct acpi_device_id ec_device_ids[] = {
  810. {"PNP0C09", 0},
  811. {"", 0},
  812. };
  813. /* Some BIOS do not survive early DSDT scan, skip it */
  814. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  815. {
  816. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  817. return 0;
  818. }
  819. /* ASUStek often supplies us with broken ECDT, validate it */
  820. static int ec_validate_ecdt(const struct dmi_system_id *id)
  821. {
  822. EC_FLAGS_VALIDATE_ECDT = 1;
  823. return 0;
  824. }
  825. /* MSI EC needs special treatment, enable it */
  826. static int ec_flag_msi(const struct dmi_system_id *id)
  827. {
  828. printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
  829. EC_FLAGS_MSI = 1;
  830. EC_FLAGS_VALIDATE_ECDT = 1;
  831. return 0;
  832. }
  833. static struct dmi_system_id __initdata ec_dmi_table[] = {
  834. {
  835. ec_skip_dsdt_scan, "Compal JFL92", {
  836. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  837. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  838. {
  839. ec_flag_msi, "MSI hardware", {
  840. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  841. {
  842. ec_flag_msi, "MSI hardware", {
  843. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  844. {
  845. ec_flag_msi, "MSI hardware", {
  846. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  847. {
  848. ec_validate_ecdt, "ASUS hardware", {
  849. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  850. {},
  851. };
  852. int __init acpi_ec_ecdt_probe(void)
  853. {
  854. acpi_status status;
  855. struct acpi_ec *saved_ec = NULL;
  856. struct acpi_table_ecdt *ecdt_ptr;
  857. boot_ec = make_acpi_ec();
  858. if (!boot_ec)
  859. return -ENOMEM;
  860. /*
  861. * Generate a boot ec context
  862. */
  863. dmi_check_system(ec_dmi_table);
  864. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  865. (struct acpi_table_header **)&ecdt_ptr);
  866. if (ACPI_SUCCESS(status)) {
  867. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  868. boot_ec->command_addr = ecdt_ptr->control.address;
  869. boot_ec->data_addr = ecdt_ptr->data.address;
  870. boot_ec->gpe = ecdt_ptr->gpe;
  871. boot_ec->handle = ACPI_ROOT_OBJECT;
  872. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  873. /* Don't trust ECDT, which comes from ASUSTek */
  874. if (!EC_FLAGS_VALIDATE_ECDT)
  875. goto install;
  876. saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  877. if (!saved_ec)
  878. return -ENOMEM;
  879. memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
  880. /* fall through */
  881. }
  882. if (EC_FLAGS_SKIP_DSDT_SCAN)
  883. return -ENODEV;
  884. /* This workaround is needed only on some broken machines,
  885. * which require early EC, but fail to provide ECDT */
  886. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  887. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  888. boot_ec, NULL);
  889. /* Check that acpi_get_devices actually find something */
  890. if (ACPI_FAILURE(status) || !boot_ec->handle)
  891. goto error;
  892. if (saved_ec) {
  893. /* try to find good ECDT from ASUSTek */
  894. if (saved_ec->command_addr != boot_ec->command_addr ||
  895. saved_ec->data_addr != boot_ec->data_addr ||
  896. saved_ec->gpe != boot_ec->gpe ||
  897. saved_ec->handle != boot_ec->handle)
  898. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  899. "ECDT tables, which are very hard to workaround. "
  900. "Trying to use DSDT EC info instead. Please send "
  901. "output of acpidump to linux-acpi@vger.kernel.org\n");
  902. kfree(saved_ec);
  903. saved_ec = NULL;
  904. } else {
  905. /* We really need to limit this workaround, the only ASUS,
  906. * which needs it, has fake EC._INI method, so use it as flag.
  907. * Keep boot_ec struct as it will be needed soon.
  908. */
  909. acpi_handle dummy;
  910. if (!dmi_name_in_vendors("ASUS") ||
  911. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  912. &dummy)))
  913. return -ENODEV;
  914. }
  915. install:
  916. if (!ec_install_handlers(boot_ec)) {
  917. first_ec = boot_ec;
  918. return 0;
  919. }
  920. error:
  921. kfree(boot_ec);
  922. boot_ec = NULL;
  923. return -ENODEV;
  924. }
  925. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  926. {
  927. struct acpi_ec *ec = acpi_driver_data(device);
  928. /* Stop using GPE */
  929. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  930. return 0;
  931. }
  932. static int acpi_ec_resume(struct acpi_device *device)
  933. {
  934. struct acpi_ec *ec = acpi_driver_data(device);
  935. /* Enable use of GPE back */
  936. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  937. return 0;
  938. }
  939. static struct acpi_driver acpi_ec_driver = {
  940. .name = "ec",
  941. .class = ACPI_EC_CLASS,
  942. .ids = ec_device_ids,
  943. .ops = {
  944. .add = acpi_ec_add,
  945. .remove = acpi_ec_remove,
  946. .suspend = acpi_ec_suspend,
  947. .resume = acpi_ec_resume,
  948. },
  949. };
  950. int __init acpi_ec_init(void)
  951. {
  952. int result = 0;
  953. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  954. if (!acpi_ec_dir)
  955. return -ENODEV;
  956. /* Now register the driver for the EC */
  957. result = acpi_bus_register_driver(&acpi_ec_driver);
  958. if (result < 0) {
  959. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  960. return -ENODEV;
  961. }
  962. return result;
  963. }
  964. /* EC driver currently not unloadable */
  965. #if 0
  966. static void __exit acpi_ec_exit(void)
  967. {
  968. acpi_bus_unregister_driver(&acpi_ec_driver);
  969. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  970. return;
  971. }
  972. #endif /* 0 */