ec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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 void acpi_ec_gpe_query(void *ec_cxt);
  182. static int ec_check_sci(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_os_execute(OSL_EC_BURST_HANDLER,
  187. acpi_ec_gpe_query, ec);
  188. }
  189. return 0;
  190. }
  191. static int ec_poll(struct acpi_ec *ec)
  192. {
  193. unsigned long flags;
  194. int repeat = 2; /* number of command restarts */
  195. while (repeat--) {
  196. unsigned long delay = jiffies +
  197. msecs_to_jiffies(ACPI_EC_DELAY);
  198. do {
  199. /* don't sleep with disabled interrupts */
  200. if (EC_FLAGS_MSI || irqs_disabled()) {
  201. udelay(ACPI_EC_MSI_UDELAY);
  202. if (ec_transaction_done(ec))
  203. return 0;
  204. } else {
  205. if (wait_event_timeout(ec->wait,
  206. ec_transaction_done(ec),
  207. msecs_to_jiffies(1)))
  208. return 0;
  209. }
  210. advance_transaction(ec, acpi_ec_read_status(ec));
  211. } while (time_before(jiffies, delay));
  212. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
  213. break;
  214. pr_debug(PREFIX "controller reset, restart transaction\n");
  215. spin_lock_irqsave(&ec->curr_lock, flags);
  216. start_transaction(ec);
  217. spin_unlock_irqrestore(&ec->curr_lock, flags);
  218. }
  219. return -ETIME;
  220. }
  221. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  222. struct transaction *t)
  223. {
  224. unsigned long tmp;
  225. int ret = 0;
  226. pr_debug(PREFIX "transaction start\n");
  227. /* disable GPE during transaction if storm is detected */
  228. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  229. acpi_disable_gpe(NULL, ec->gpe);
  230. }
  231. if (EC_FLAGS_MSI)
  232. udelay(ACPI_EC_MSI_UDELAY);
  233. /* start transaction */
  234. spin_lock_irqsave(&ec->curr_lock, tmp);
  235. /* following two actions should be kept atomic */
  236. ec->curr = t;
  237. start_transaction(ec);
  238. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  239. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  240. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  241. ret = ec_poll(ec);
  242. pr_debug(PREFIX "transaction end\n");
  243. spin_lock_irqsave(&ec->curr_lock, tmp);
  244. ec->curr = NULL;
  245. spin_unlock_irqrestore(&ec->curr_lock, tmp);
  246. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  247. /* check if we received SCI during transaction */
  248. ec_check_sci(ec, acpi_ec_read_status(ec));
  249. /* it is safe to enable GPE outside of transaction */
  250. acpi_enable_gpe(NULL, ec->gpe);
  251. } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
  252. pr_info(PREFIX "GPE storm detected, "
  253. "transactions will use polling mode\n");
  254. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  255. }
  256. return ret;
  257. }
  258. static int ec_check_ibf0(struct acpi_ec *ec)
  259. {
  260. u8 status = acpi_ec_read_status(ec);
  261. return (status & ACPI_EC_FLAG_IBF) == 0;
  262. }
  263. static int ec_wait_ibf0(struct acpi_ec *ec)
  264. {
  265. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  266. /* interrupt wait manually if GPE mode is not active */
  267. while (time_before(jiffies, delay))
  268. if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
  269. msecs_to_jiffies(1)))
  270. return 0;
  271. return -ETIME;
  272. }
  273. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  274. {
  275. int status;
  276. u32 glk;
  277. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  278. return -EINVAL;
  279. if (t->rdata)
  280. memset(t->rdata, 0, t->rlen);
  281. mutex_lock(&ec->lock);
  282. if (ec->global_lock) {
  283. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  284. if (ACPI_FAILURE(status)) {
  285. status = -ENODEV;
  286. goto unlock;
  287. }
  288. }
  289. if (ec_wait_ibf0(ec)) {
  290. pr_err(PREFIX "input buffer is not empty, "
  291. "aborting transaction\n");
  292. status = -ETIME;
  293. goto end;
  294. }
  295. status = acpi_ec_transaction_unlocked(ec, t);
  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(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(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_gpe_query(void *ec_cxt)
  448. {
  449. struct acpi_ec *ec = ec_cxt;
  450. u8 value = 0;
  451. struct acpi_ec_query_handler *handler, copy;
  452. if (!ec || acpi_ec_query(ec, &value))
  453. return;
  454. mutex_lock(&ec->lock);
  455. list_for_each_entry(handler, &ec->list, node) {
  456. if (value == handler->query_bit) {
  457. /* have custom handler for this bit */
  458. memcpy(&copy, handler, sizeof(copy));
  459. mutex_unlock(&ec->lock);
  460. if (copy.func) {
  461. copy.func(copy.data);
  462. } else if (copy.handle) {
  463. acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
  464. }
  465. return;
  466. }
  467. }
  468. mutex_unlock(&ec->lock);
  469. }
  470. static u32 acpi_ec_gpe_handler(void *data)
  471. {
  472. struct acpi_ec *ec = data;
  473. u8 status;
  474. pr_debug(PREFIX "~~~> interrupt\n");
  475. status = acpi_ec_read_status(ec);
  476. advance_transaction(ec, status);
  477. if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
  478. wake_up(&ec->wait);
  479. ec_check_sci(ec, status);
  480. return ACPI_INTERRUPT_HANDLED;
  481. }
  482. /* --------------------------------------------------------------------------
  483. Address Space Management
  484. -------------------------------------------------------------------------- */
  485. static acpi_status
  486. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  487. u32 bits, acpi_integer *value,
  488. void *handler_context, void *region_context)
  489. {
  490. struct acpi_ec *ec = handler_context;
  491. int result = 0, i;
  492. u8 temp = 0;
  493. if ((address > 0xFF) || !value || !handler_context)
  494. return AE_BAD_PARAMETER;
  495. if (function != ACPI_READ && function != ACPI_WRITE)
  496. return AE_BAD_PARAMETER;
  497. if (bits != 8 && acpi_strict)
  498. return AE_BAD_PARAMETER;
  499. if (EC_FLAGS_MSI)
  500. acpi_ec_burst_enable(ec);
  501. if (function == ACPI_READ) {
  502. result = acpi_ec_read(ec, address, &temp);
  503. *value = temp;
  504. } else {
  505. temp = 0xff & (*value);
  506. result = acpi_ec_write(ec, address, temp);
  507. }
  508. for (i = 8; unlikely(bits - i > 0); i += 8) {
  509. ++address;
  510. if (function == ACPI_READ) {
  511. result = acpi_ec_read(ec, address, &temp);
  512. (*value) |= ((acpi_integer)temp) << i;
  513. } else {
  514. temp = 0xff & ((*value) >> i);
  515. result = acpi_ec_write(ec, address, temp);
  516. }
  517. }
  518. if (EC_FLAGS_MSI)
  519. acpi_ec_burst_disable(ec);
  520. switch (result) {
  521. case -EINVAL:
  522. return AE_BAD_PARAMETER;
  523. break;
  524. case -ENODEV:
  525. return AE_NOT_FOUND;
  526. break;
  527. case -ETIME:
  528. return AE_TIME;
  529. break;
  530. default:
  531. return AE_OK;
  532. }
  533. }
  534. /* --------------------------------------------------------------------------
  535. FS Interface (/proc)
  536. -------------------------------------------------------------------------- */
  537. static struct proc_dir_entry *acpi_ec_dir;
  538. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  539. {
  540. struct acpi_ec *ec = seq->private;
  541. if (!ec)
  542. goto end;
  543. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  544. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  545. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  546. seq_printf(seq, "use global lock:\t%s\n",
  547. ec->global_lock ? "yes" : "no");
  548. end:
  549. return 0;
  550. }
  551. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  552. {
  553. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  554. }
  555. static const struct file_operations acpi_ec_info_ops = {
  556. .open = acpi_ec_info_open_fs,
  557. .read = seq_read,
  558. .llseek = seq_lseek,
  559. .release = single_release,
  560. .owner = THIS_MODULE,
  561. };
  562. static int acpi_ec_add_fs(struct acpi_device *device)
  563. {
  564. struct proc_dir_entry *entry = NULL;
  565. if (!acpi_device_dir(device)) {
  566. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  567. acpi_ec_dir);
  568. if (!acpi_device_dir(device))
  569. return -ENODEV;
  570. }
  571. entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
  572. acpi_device_dir(device),
  573. &acpi_ec_info_ops, acpi_driver_data(device));
  574. if (!entry)
  575. return -ENODEV;
  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. spin_lock_init(&ec->curr_lock);
  602. return ec;
  603. }
  604. static acpi_status
  605. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  606. void *context, void **return_value)
  607. {
  608. char node_name[5];
  609. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  610. struct acpi_ec *ec = context;
  611. int value = 0;
  612. acpi_status status;
  613. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  614. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  615. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  616. }
  617. return AE_OK;
  618. }
  619. static acpi_status
  620. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  621. {
  622. acpi_status status;
  623. unsigned long long tmp = 0;
  624. struct acpi_ec *ec = context;
  625. /* clear addr values, ec_parse_io_ports depend on it */
  626. ec->command_addr = ec->data_addr = 0;
  627. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  628. ec_parse_io_ports, ec);
  629. if (ACPI_FAILURE(status))
  630. return status;
  631. /* Get GPE bit assignment (EC events). */
  632. /* TODO: Add support for _GPE returning a package */
  633. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  634. if (ACPI_FAILURE(status))
  635. return status;
  636. ec->gpe = tmp;
  637. /* Use the global lock for all EC transactions? */
  638. tmp = 0;
  639. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  640. ec->global_lock = tmp;
  641. ec->handle = handle;
  642. return AE_CTRL_TERMINATE;
  643. }
  644. static int ec_install_handlers(struct acpi_ec *ec)
  645. {
  646. acpi_status status;
  647. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  648. return 0;
  649. status = acpi_install_gpe_handler(NULL, ec->gpe,
  650. ACPI_GPE_EDGE_TRIGGERED,
  651. &acpi_ec_gpe_handler, ec);
  652. if (ACPI_FAILURE(status))
  653. return -ENODEV;
  654. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  655. acpi_enable_gpe(NULL, ec->gpe);
  656. status = acpi_install_address_space_handler(ec->handle,
  657. ACPI_ADR_SPACE_EC,
  658. &acpi_ec_space_handler,
  659. NULL, ec);
  660. if (ACPI_FAILURE(status)) {
  661. if (status == AE_NOT_FOUND) {
  662. /*
  663. * Maybe OS fails in evaluating the _REG object.
  664. * The AE_NOT_FOUND error will be ignored and OS
  665. * continue to initialize EC.
  666. */
  667. printk(KERN_ERR "Fail in evaluating the _REG object"
  668. " of EC device. Broken bios is suspected.\n");
  669. } else {
  670. acpi_remove_gpe_handler(NULL, ec->gpe,
  671. &acpi_ec_gpe_handler);
  672. return -ENODEV;
  673. }
  674. }
  675. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  676. return 0;
  677. }
  678. static void ec_remove_handlers(struct acpi_ec *ec)
  679. {
  680. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  681. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  682. pr_err(PREFIX "failed to remove space handler\n");
  683. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  684. &acpi_ec_gpe_handler)))
  685. pr_err(PREFIX "failed to remove gpe handler\n");
  686. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  687. }
  688. static int acpi_ec_add(struct acpi_device *device)
  689. {
  690. struct acpi_ec *ec = NULL;
  691. int ret;
  692. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  693. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  694. /* Check for boot EC */
  695. if (boot_ec &&
  696. (boot_ec->handle == device->handle ||
  697. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  698. ec = boot_ec;
  699. boot_ec = NULL;
  700. } else {
  701. ec = make_acpi_ec();
  702. if (!ec)
  703. return -ENOMEM;
  704. }
  705. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  706. AE_CTRL_TERMINATE) {
  707. kfree(ec);
  708. return -EINVAL;
  709. }
  710. ec->handle = device->handle;
  711. /* Find and register all query methods */
  712. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  713. acpi_ec_register_query_methods, NULL, ec, NULL);
  714. if (!first_ec)
  715. first_ec = ec;
  716. device->driver_data = ec;
  717. acpi_ec_add_fs(device);
  718. pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  719. ec->gpe, ec->command_addr, ec->data_addr);
  720. ret = ec_install_handlers(ec);
  721. /* EC is fully operational, allow queries */
  722. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  723. return ret;
  724. }
  725. static int acpi_ec_remove(struct acpi_device *device, int type)
  726. {
  727. struct acpi_ec *ec;
  728. struct acpi_ec_query_handler *handler, *tmp;
  729. if (!device)
  730. return -EINVAL;
  731. ec = acpi_driver_data(device);
  732. ec_remove_handlers(ec);
  733. mutex_lock(&ec->lock);
  734. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  735. list_del(&handler->node);
  736. kfree(handler);
  737. }
  738. mutex_unlock(&ec->lock);
  739. acpi_ec_remove_fs(device);
  740. device->driver_data = NULL;
  741. if (ec == first_ec)
  742. first_ec = NULL;
  743. kfree(ec);
  744. return 0;
  745. }
  746. static acpi_status
  747. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  748. {
  749. struct acpi_ec *ec = context;
  750. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  751. return AE_OK;
  752. /*
  753. * The first address region returned is the data port, and
  754. * the second address region returned is the status/command
  755. * port.
  756. */
  757. if (ec->data_addr == 0)
  758. ec->data_addr = resource->data.io.minimum;
  759. else if (ec->command_addr == 0)
  760. ec->command_addr = resource->data.io.minimum;
  761. else
  762. return AE_CTRL_TERMINATE;
  763. return AE_OK;
  764. }
  765. int __init acpi_boot_ec_enable(void)
  766. {
  767. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  768. return 0;
  769. if (!ec_install_handlers(boot_ec)) {
  770. first_ec = boot_ec;
  771. return 0;
  772. }
  773. return -EFAULT;
  774. }
  775. static const struct acpi_device_id ec_device_ids[] = {
  776. {"PNP0C09", 0},
  777. {"", 0},
  778. };
  779. /* Some BIOS do not survive early DSDT scan, skip it */
  780. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  781. {
  782. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  783. return 0;
  784. }
  785. /* ASUStek often supplies us with broken ECDT, validate it */
  786. static int ec_validate_ecdt(const struct dmi_system_id *id)
  787. {
  788. EC_FLAGS_VALIDATE_ECDT = 1;
  789. return 0;
  790. }
  791. /* MSI EC needs special treatment, enable it */
  792. static int ec_flag_msi(const struct dmi_system_id *id)
  793. {
  794. printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
  795. EC_FLAGS_MSI = 1;
  796. EC_FLAGS_VALIDATE_ECDT = 1;
  797. return 0;
  798. }
  799. static struct dmi_system_id __initdata ec_dmi_table[] = {
  800. {
  801. ec_skip_dsdt_scan, "Compal JFL92", {
  802. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  803. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  804. {
  805. ec_flag_msi, "MSI hardware", {
  806. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  807. {
  808. ec_flag_msi, "MSI hardware", {
  809. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  810. {
  811. ec_flag_msi, "MSI hardware", {
  812. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  813. {
  814. ec_validate_ecdt, "ASUS hardware", {
  815. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  816. {},
  817. };
  818. int __init acpi_ec_ecdt_probe(void)
  819. {
  820. acpi_status status;
  821. struct acpi_ec *saved_ec = NULL;
  822. struct acpi_table_ecdt *ecdt_ptr;
  823. boot_ec = make_acpi_ec();
  824. if (!boot_ec)
  825. return -ENOMEM;
  826. /*
  827. * Generate a boot ec context
  828. */
  829. dmi_check_system(ec_dmi_table);
  830. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  831. (struct acpi_table_header **)&ecdt_ptr);
  832. if (ACPI_SUCCESS(status)) {
  833. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  834. boot_ec->command_addr = ecdt_ptr->control.address;
  835. boot_ec->data_addr = ecdt_ptr->data.address;
  836. boot_ec->gpe = ecdt_ptr->gpe;
  837. boot_ec->handle = ACPI_ROOT_OBJECT;
  838. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  839. /* Don't trust ECDT, which comes from ASUSTek */
  840. if (!EC_FLAGS_VALIDATE_ECDT)
  841. goto install;
  842. saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  843. if (!saved_ec)
  844. return -ENOMEM;
  845. memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
  846. /* fall through */
  847. }
  848. if (EC_FLAGS_SKIP_DSDT_SCAN)
  849. return -ENODEV;
  850. /* This workaround is needed only on some broken machines,
  851. * which require early EC, but fail to provide ECDT */
  852. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  853. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  854. boot_ec, NULL);
  855. /* Check that acpi_get_devices actually find something */
  856. if (ACPI_FAILURE(status) || !boot_ec->handle)
  857. goto error;
  858. if (saved_ec) {
  859. /* try to find good ECDT from ASUSTek */
  860. if (saved_ec->command_addr != boot_ec->command_addr ||
  861. saved_ec->data_addr != boot_ec->data_addr ||
  862. saved_ec->gpe != boot_ec->gpe ||
  863. saved_ec->handle != boot_ec->handle)
  864. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  865. "ECDT tables, which are very hard to workaround. "
  866. "Trying to use DSDT EC info instead. Please send "
  867. "output of acpidump to linux-acpi@vger.kernel.org\n");
  868. kfree(saved_ec);
  869. saved_ec = NULL;
  870. } else {
  871. /* We really need to limit this workaround, the only ASUS,
  872. * which needs it, has fake EC._INI method, so use it as flag.
  873. * Keep boot_ec struct as it will be needed soon.
  874. */
  875. acpi_handle dummy;
  876. if (!dmi_name_in_vendors("ASUS") ||
  877. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  878. &dummy)))
  879. return -ENODEV;
  880. }
  881. install:
  882. if (!ec_install_handlers(boot_ec)) {
  883. first_ec = boot_ec;
  884. return 0;
  885. }
  886. error:
  887. kfree(boot_ec);
  888. boot_ec = NULL;
  889. return -ENODEV;
  890. }
  891. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  892. {
  893. struct acpi_ec *ec = acpi_driver_data(device);
  894. /* Stop using GPE */
  895. acpi_disable_gpe(NULL, ec->gpe);
  896. return 0;
  897. }
  898. static int acpi_ec_resume(struct acpi_device *device)
  899. {
  900. struct acpi_ec *ec = acpi_driver_data(device);
  901. /* Enable use of GPE back */
  902. acpi_enable_gpe(NULL, ec->gpe);
  903. return 0;
  904. }
  905. static struct acpi_driver acpi_ec_driver = {
  906. .name = "ec",
  907. .class = ACPI_EC_CLASS,
  908. .ids = ec_device_ids,
  909. .ops = {
  910. .add = acpi_ec_add,
  911. .remove = acpi_ec_remove,
  912. .suspend = acpi_ec_suspend,
  913. .resume = acpi_ec_resume,
  914. },
  915. };
  916. int __init acpi_ec_init(void)
  917. {
  918. int result = 0;
  919. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  920. if (!acpi_ec_dir)
  921. return -ENODEV;
  922. /* Now register the driver for the EC */
  923. result = acpi_bus_register_driver(&acpi_ec_driver);
  924. if (result < 0) {
  925. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  926. return -ENODEV;
  927. }
  928. return result;
  929. }
  930. /* EC driver currently not unloadable */
  931. #if 0
  932. static void __exit acpi_ec_exit(void)
  933. {
  934. acpi_bus_unregister_driver(&acpi_ec_driver);
  935. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  936. return;
  937. }
  938. #endif /* 0 */