ec.c 23 KB

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