ec.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. /* --------------------------------------------------------------------------
  109. Transaction Management
  110. -------------------------------------------------------------------------- */
  111. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  112. {
  113. u8 x = inb(ec->command_addr);
  114. pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
  115. return x;
  116. }
  117. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  118. {
  119. u8 x = inb(ec->data_addr);
  120. pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
  121. return x;
  122. }
  123. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  124. {
  125. pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
  126. outb(command, ec->command_addr);
  127. }
  128. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  129. {
  130. pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
  131. outb(data, ec->data_addr);
  132. }
  133. static int ec_transaction_done(struct acpi_ec *ec)
  134. {
  135. unsigned long flags;
  136. int ret = 0;
  137. spin_lock_irqsave(&ec->curr_lock, flags);
  138. if (!ec->curr || ec->curr->done)
  139. ret = 1;
  140. spin_unlock_irqrestore(&ec->curr_lock, flags);
  141. return ret;
  142. }
  143. static void start_transaction(struct acpi_ec *ec)
  144. {
  145. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  146. ec->curr->done = false;
  147. acpi_ec_write_cmd(ec, ec->curr->command);
  148. }
  149. static void advance_transaction(struct acpi_ec *ec, u8 status)
  150. {
  151. unsigned long flags;
  152. spin_lock_irqsave(&ec->curr_lock, flags);
  153. if (!ec->curr)
  154. goto unlock;
  155. if (ec->curr->wlen > ec->curr->wi) {
  156. if ((status & ACPI_EC_FLAG_IBF) == 0)
  157. acpi_ec_write_data(ec,
  158. ec->curr->wdata[ec->curr->wi++]);
  159. else
  160. goto err;
  161. } else if (ec->curr->rlen > ec->curr->ri) {
  162. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  163. ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
  164. if (ec->curr->rlen == ec->curr->ri)
  165. ec->curr->done = true;
  166. } else
  167. goto err;
  168. } else if (ec->curr->wlen == ec->curr->wi &&
  169. (status & ACPI_EC_FLAG_IBF) == 0)
  170. ec->curr->done = true;
  171. goto unlock;
  172. err:
  173. /* false interrupt, state didn't change */
  174. if (in_interrupt())
  175. ++ec->curr->irq_count;
  176. unlock:
  177. spin_unlock_irqrestore(&ec->curr_lock, flags);
  178. }
  179. static void acpi_ec_gpe_query(void *ec_cxt);
  180. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  181. {
  182. if (state & ACPI_EC_FLAG_SCI) {
  183. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  184. return acpi_os_execute(OSL_EC_BURST_HANDLER,
  185. acpi_ec_gpe_query, ec);
  186. }
  187. return 0;
  188. }
  189. static int ec_poll(struct acpi_ec *ec)
  190. {
  191. unsigned long flags;
  192. int repeat = 2; /* number of command restarts */
  193. while (repeat--) {
  194. unsigned long delay = jiffies +
  195. msecs_to_jiffies(ACPI_EC_DELAY);
  196. do {
  197. /* don't sleep with disabled interrupts */
  198. if (EC_FLAGS_MSI || irqs_disabled()) {
  199. udelay(ACPI_EC_MSI_UDELAY);
  200. if (ec_transaction_done(ec))
  201. return 0;
  202. } else {
  203. if (wait_event_timeout(ec->wait,
  204. ec_transaction_done(ec),
  205. msecs_to_jiffies(1)))
  206. return 0;
  207. }
  208. advance_transaction(ec, acpi_ec_read_status(ec));
  209. } while (time_before(jiffies, delay));
  210. if (!ec->curr->irq_count ||
  211. (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
  212. break;
  213. /* try restart command if we get any false interrupts */
  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, 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. int __init acpi_ec_ecdt_probe(void)
  780. {
  781. acpi_status status;
  782. struct acpi_ec *saved_ec = NULL;
  783. struct acpi_table_ecdt *ecdt_ptr;
  784. boot_ec = make_acpi_ec();
  785. if (!boot_ec)
  786. return -ENOMEM;
  787. /*
  788. * Generate a boot ec context
  789. */
  790. if (dmi_name_in_vendors("Micro-Star") ||
  791. dmi_name_in_vendors("Notebook")) {
  792. pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
  793. EC_FLAGS_MSI = 1;
  794. }
  795. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  796. (struct acpi_table_header **)&ecdt_ptr);
  797. if (ACPI_SUCCESS(status)) {
  798. pr_info(PREFIX "EC description table is found, configuring boot EC\n");
  799. boot_ec->command_addr = ecdt_ptr->control.address;
  800. boot_ec->data_addr = ecdt_ptr->data.address;
  801. boot_ec->gpe = ecdt_ptr->gpe;
  802. boot_ec->handle = ACPI_ROOT_OBJECT;
  803. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  804. /* Don't trust ECDT, which comes from ASUSTek */
  805. if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
  806. goto install;
  807. saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  808. if (!saved_ec)
  809. return -ENOMEM;
  810. memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
  811. /* fall through */
  812. }
  813. /* This workaround is needed only on some broken machines,
  814. * which require early EC, but fail to provide ECDT */
  815. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  816. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  817. boot_ec, NULL);
  818. /* Check that acpi_get_devices actually find something */
  819. if (ACPI_FAILURE(status) || !boot_ec->handle)
  820. goto error;
  821. if (saved_ec) {
  822. /* try to find good ECDT from ASUSTek */
  823. if (saved_ec->command_addr != boot_ec->command_addr ||
  824. saved_ec->data_addr != boot_ec->data_addr ||
  825. saved_ec->gpe != boot_ec->gpe ||
  826. saved_ec->handle != boot_ec->handle)
  827. pr_info(PREFIX "ASUSTek keeps feeding us with broken "
  828. "ECDT tables, which are very hard to workaround. "
  829. "Trying to use DSDT EC info instead. Please send "
  830. "output of acpidump to linux-acpi@vger.kernel.org\n");
  831. kfree(saved_ec);
  832. saved_ec = NULL;
  833. } else {
  834. /* We really need to limit this workaround, the only ASUS,
  835. * which needs it, has fake EC._INI method, so use it as flag.
  836. * Keep boot_ec struct as it will be needed soon.
  837. */
  838. acpi_handle dummy;
  839. if (!dmi_name_in_vendors("ASUS") ||
  840. ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
  841. &dummy)))
  842. return -ENODEV;
  843. }
  844. install:
  845. if (!ec_install_handlers(boot_ec)) {
  846. first_ec = boot_ec;
  847. return 0;
  848. }
  849. error:
  850. kfree(boot_ec);
  851. boot_ec = NULL;
  852. return -ENODEV;
  853. }
  854. static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
  855. {
  856. struct acpi_ec *ec = acpi_driver_data(device);
  857. /* Stop using GPE */
  858. acpi_disable_gpe(NULL, ec->gpe);
  859. return 0;
  860. }
  861. static int acpi_ec_resume(struct acpi_device *device)
  862. {
  863. struct acpi_ec *ec = acpi_driver_data(device);
  864. /* Enable use of GPE back */
  865. acpi_enable_gpe(NULL, ec->gpe);
  866. return 0;
  867. }
  868. static struct acpi_driver acpi_ec_driver = {
  869. .name = "ec",
  870. .class = ACPI_EC_CLASS,
  871. .ids = ec_device_ids,
  872. .ops = {
  873. .add = acpi_ec_add,
  874. .remove = acpi_ec_remove,
  875. .suspend = acpi_ec_suspend,
  876. .resume = acpi_ec_resume,
  877. },
  878. };
  879. int __init acpi_ec_init(void)
  880. {
  881. int result = 0;
  882. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  883. if (!acpi_ec_dir)
  884. return -ENODEV;
  885. /* Now register the driver for the EC */
  886. result = acpi_bus_register_driver(&acpi_ec_driver);
  887. if (result < 0) {
  888. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  889. return -ENODEV;
  890. }
  891. return result;
  892. }
  893. /* EC driver currently not unloadable */
  894. #if 0
  895. static void __exit acpi_ec_exit(void)
  896. {
  897. acpi_bus_unregister_driver(&acpi_ec_driver);
  898. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  899. return;
  900. }
  901. #endif /* 0 */