ec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags);
  157. else
  158. /* missing GPEs, switch back to poll mode */
  159. clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  160. return 0;
  161. }
  162. } else {
  163. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  164. clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  165. while (time_before(jiffies, delay)) {
  166. if (acpi_ec_check_status(ec, event))
  167. return 0;
  168. }
  169. }
  170. printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
  171. " status = %d, expect_event = %d\n",
  172. acpi_ec_read_status(ec), event);
  173. return -ETIME;
  174. }
  175. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
  176. const u8 * wdata, unsigned wdata_len,
  177. u8 * rdata, unsigned rdata_len,
  178. int force_poll)
  179. {
  180. int result = 0;
  181. set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  182. acpi_ec_write_cmd(ec, command);
  183. for (; wdata_len > 0; --wdata_len) {
  184. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
  185. if (result) {
  186. printk(KERN_ERR PREFIX
  187. "write_cmd timeout, command = %d\n", command);
  188. goto end;
  189. }
  190. set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  191. acpi_ec_write_data(ec, *(wdata++));
  192. }
  193. if (!rdata_len) {
  194. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
  195. if (result) {
  196. printk(KERN_ERR PREFIX
  197. "finish-write timeout, command = %d\n", command);
  198. goto end;
  199. }
  200. } else if (command == ACPI_EC_COMMAND_QUERY)
  201. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  202. for (; rdata_len > 0; --rdata_len) {
  203. if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags))
  204. force_poll = 1;
  205. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
  206. if (result) {
  207. printk(KERN_ERR PREFIX "read timeout, command = %d\n",
  208. command);
  209. goto end;
  210. }
  211. /* Don't expect GPE after last read */
  212. if (rdata_len > 1)
  213. set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  214. *(rdata++) = acpi_ec_read_data(ec);
  215. }
  216. end:
  217. return result;
  218. }
  219. static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
  220. const u8 * wdata, unsigned wdata_len,
  221. u8 * rdata, unsigned rdata_len,
  222. int force_poll)
  223. {
  224. int status;
  225. u32 glk;
  226. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  227. return -EINVAL;
  228. if (rdata)
  229. memset(rdata, 0, rdata_len);
  230. mutex_lock(&ec->lock);
  231. if (ec->global_lock) {
  232. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  233. if (ACPI_FAILURE(status)) {
  234. mutex_unlock(&ec->lock);
  235. return -ENODEV;
  236. }
  237. }
  238. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
  239. if (status) {
  240. printk(KERN_ERR PREFIX
  241. "input buffer is not empty, aborting transaction\n");
  242. goto end;
  243. }
  244. status = acpi_ec_transaction_unlocked(ec, command,
  245. wdata, wdata_len,
  246. rdata, rdata_len,
  247. force_poll);
  248. end:
  249. if (ec->global_lock)
  250. acpi_release_global_lock(glk);
  251. mutex_unlock(&ec->lock);
  252. return status;
  253. }
  254. /*
  255. * Note: samsung nv5000 doesn't work with ec burst mode.
  256. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  257. */
  258. int acpi_ec_burst_enable(struct acpi_ec *ec)
  259. {
  260. u8 d;
  261. return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
  262. }
  263. int acpi_ec_burst_disable(struct acpi_ec *ec)
  264. {
  265. return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
  266. }
  267. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  268. {
  269. int result;
  270. u8 d;
  271. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
  272. &address, 1, &d, 1, 0);
  273. *data = d;
  274. return result;
  275. }
  276. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  277. {
  278. u8 wdata[2] = { address, data };
  279. return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
  280. wdata, 2, NULL, 0, 0);
  281. }
  282. /*
  283. * Externally callable EC access functions. For now, assume 1 EC only
  284. */
  285. int ec_burst_enable(void)
  286. {
  287. if (!first_ec)
  288. return -ENODEV;
  289. return acpi_ec_burst_enable(first_ec);
  290. }
  291. EXPORT_SYMBOL(ec_burst_enable);
  292. int ec_burst_disable(void)
  293. {
  294. if (!first_ec)
  295. return -ENODEV;
  296. return acpi_ec_burst_disable(first_ec);
  297. }
  298. EXPORT_SYMBOL(ec_burst_disable);
  299. int ec_read(u8 addr, u8 * val)
  300. {
  301. int err;
  302. u8 temp_data;
  303. if (!first_ec)
  304. return -ENODEV;
  305. err = acpi_ec_read(first_ec, addr, &temp_data);
  306. if (!err) {
  307. *val = temp_data;
  308. return 0;
  309. } else
  310. return err;
  311. }
  312. EXPORT_SYMBOL(ec_read);
  313. int ec_write(u8 addr, u8 val)
  314. {
  315. int err;
  316. if (!first_ec)
  317. return -ENODEV;
  318. err = acpi_ec_write(first_ec, addr, val);
  319. return err;
  320. }
  321. EXPORT_SYMBOL(ec_write);
  322. int ec_transaction(u8 command,
  323. const u8 * wdata, unsigned wdata_len,
  324. u8 * rdata, unsigned rdata_len,
  325. int force_poll)
  326. {
  327. if (!first_ec)
  328. return -ENODEV;
  329. return acpi_ec_transaction(first_ec, command, wdata,
  330. wdata_len, rdata, rdata_len,
  331. force_poll);
  332. }
  333. EXPORT_SYMBOL(ec_transaction);
  334. static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
  335. {
  336. int result;
  337. u8 d;
  338. if (!ec || !data)
  339. return -EINVAL;
  340. /*
  341. * Query the EC to find out which _Qxx method we need to evaluate.
  342. * Note that successful completion of the query causes the ACPI_EC_SCI
  343. * bit to be cleared (and thus clearing the interrupt source).
  344. */
  345. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
  346. if (result)
  347. return result;
  348. if (!d)
  349. return -ENODATA;
  350. *data = d;
  351. return 0;
  352. }
  353. /* --------------------------------------------------------------------------
  354. Event Management
  355. -------------------------------------------------------------------------- */
  356. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  357. acpi_handle handle, acpi_ec_query_func func,
  358. void *data)
  359. {
  360. struct acpi_ec_query_handler *handler =
  361. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  362. if (!handler)
  363. return -ENOMEM;
  364. handler->query_bit = query_bit;
  365. handler->handle = handle;
  366. handler->func = func;
  367. handler->data = data;
  368. mutex_lock(&ec->lock);
  369. list_add(&handler->node, &ec->list);
  370. mutex_unlock(&ec->lock);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  374. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  375. {
  376. struct acpi_ec_query_handler *handler;
  377. mutex_lock(&ec->lock);
  378. list_for_each_entry(handler, &ec->list, node) {
  379. if (query_bit == handler->query_bit) {
  380. list_del(&handler->node);
  381. kfree(handler);
  382. }
  383. }
  384. mutex_unlock(&ec->lock);
  385. }
  386. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  387. static void acpi_ec_gpe_query(void *ec_cxt)
  388. {
  389. struct acpi_ec *ec = ec_cxt;
  390. u8 value = 0;
  391. struct acpi_ec_query_handler *handler, copy;
  392. if (!ec || acpi_ec_query(ec, &value))
  393. return;
  394. mutex_lock(&ec->lock);
  395. list_for_each_entry(handler, &ec->list, node) {
  396. if (value == handler->query_bit) {
  397. /* have custom handler for this bit */
  398. memcpy(&copy, handler, sizeof(copy));
  399. mutex_unlock(&ec->lock);
  400. if (copy.func) {
  401. copy.func(copy.data);
  402. } else if (copy.handle) {
  403. acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
  404. }
  405. return;
  406. }
  407. }
  408. mutex_unlock(&ec->lock);
  409. }
  410. static u32 acpi_ec_gpe_handler(void *data)
  411. {
  412. acpi_status status = AE_OK;
  413. struct acpi_ec *ec = data;
  414. clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
  415. if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
  416. wake_up(&ec->wait);
  417. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
  418. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  419. status = acpi_os_execute(OSL_EC_BURST_HANDLER,
  420. acpi_ec_gpe_query, ec);
  421. } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags)))
  422. set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
  423. return ACPI_SUCCESS(status) ?
  424. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  425. }
  426. /* --------------------------------------------------------------------------
  427. Address Space Management
  428. -------------------------------------------------------------------------- */
  429. static acpi_status
  430. acpi_ec_space_setup(acpi_handle region_handle,
  431. u32 function, void *handler_context, void **return_context)
  432. {
  433. /*
  434. * The EC object is in the handler context and is needed
  435. * when calling the acpi_ec_space_handler.
  436. */
  437. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  438. handler_context : NULL;
  439. return AE_OK;
  440. }
  441. static acpi_status
  442. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  443. u32 bits, acpi_integer *value,
  444. void *handler_context, void *region_context)
  445. {
  446. struct acpi_ec *ec = handler_context;
  447. int result = 0, i = 0;
  448. u8 temp = 0;
  449. if ((address > 0xFF) || !value || !handler_context)
  450. return AE_BAD_PARAMETER;
  451. if (function != ACPI_READ && function != ACPI_WRITE)
  452. return AE_BAD_PARAMETER;
  453. if (bits != 8 && acpi_strict)
  454. return AE_BAD_PARAMETER;
  455. while (bits - i > 0) {
  456. if (function == ACPI_READ) {
  457. result = acpi_ec_read(ec, address, &temp);
  458. (*value) |= ((acpi_integer)temp) << i;
  459. } else {
  460. temp = 0xff & ((*value) >> i);
  461. result = acpi_ec_write(ec, address, temp);
  462. }
  463. i += 8;
  464. ++address;
  465. }
  466. switch (result) {
  467. case -EINVAL:
  468. return AE_BAD_PARAMETER;
  469. break;
  470. case -ENODEV:
  471. return AE_NOT_FOUND;
  472. break;
  473. case -ETIME:
  474. return AE_TIME;
  475. break;
  476. default:
  477. return AE_OK;
  478. }
  479. }
  480. /* --------------------------------------------------------------------------
  481. FS Interface (/proc)
  482. -------------------------------------------------------------------------- */
  483. static struct proc_dir_entry *acpi_ec_dir;
  484. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  485. {
  486. struct acpi_ec *ec = seq->private;
  487. if (!ec)
  488. goto end;
  489. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  490. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  491. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  492. seq_printf(seq, "use global lock:\t%s\n",
  493. ec->global_lock ? "yes" : "no");
  494. end:
  495. return 0;
  496. }
  497. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  498. {
  499. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  500. }
  501. static struct file_operations acpi_ec_info_ops = {
  502. .open = acpi_ec_info_open_fs,
  503. .read = seq_read,
  504. .llseek = seq_lseek,
  505. .release = single_release,
  506. .owner = THIS_MODULE,
  507. };
  508. static int acpi_ec_add_fs(struct acpi_device *device)
  509. {
  510. struct proc_dir_entry *entry = NULL;
  511. if (!acpi_device_dir(device)) {
  512. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  513. acpi_ec_dir);
  514. if (!acpi_device_dir(device))
  515. return -ENODEV;
  516. }
  517. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  518. acpi_device_dir(device));
  519. if (!entry)
  520. return -ENODEV;
  521. else {
  522. entry->proc_fops = &acpi_ec_info_ops;
  523. entry->data = acpi_driver_data(device);
  524. entry->owner = THIS_MODULE;
  525. }
  526. return 0;
  527. }
  528. static int acpi_ec_remove_fs(struct acpi_device *device)
  529. {
  530. if (acpi_device_dir(device)) {
  531. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  532. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  533. acpi_device_dir(device) = NULL;
  534. }
  535. return 0;
  536. }
  537. /* --------------------------------------------------------------------------
  538. Driver Interface
  539. -------------------------------------------------------------------------- */
  540. static acpi_status
  541. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  542. static struct acpi_ec *make_acpi_ec(void)
  543. {
  544. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  545. if (!ec)
  546. return NULL;
  547. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  548. mutex_init(&ec->lock);
  549. init_waitqueue_head(&ec->wait);
  550. INIT_LIST_HEAD(&ec->list);
  551. return ec;
  552. }
  553. static acpi_status
  554. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  555. void *context, void **return_value)
  556. {
  557. struct acpi_namespace_node *node = handle;
  558. struct acpi_ec *ec = context;
  559. int value = 0;
  560. if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
  561. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  562. }
  563. return AE_OK;
  564. }
  565. static acpi_status
  566. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  567. {
  568. acpi_status status;
  569. struct acpi_ec *ec = context;
  570. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  571. ec_parse_io_ports, ec);
  572. if (ACPI_FAILURE(status))
  573. return status;
  574. /* Get GPE bit assignment (EC events). */
  575. /* TODO: Add support for _GPE returning a package */
  576. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
  577. if (ACPI_FAILURE(status))
  578. return status;
  579. /* Find and register all query methods */
  580. acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
  581. acpi_ec_register_query_methods, ec, NULL);
  582. /* Use the global lock for all EC transactions? */
  583. acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
  584. ec->handle = handle;
  585. return AE_CTRL_TERMINATE;
  586. }
  587. static void ec_remove_handlers(struct acpi_ec *ec)
  588. {
  589. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  590. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  591. printk(KERN_ERR PREFIX "failed to remove space handler\n");
  592. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  593. &acpi_ec_gpe_handler)))
  594. printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
  595. ec->handlers_installed = 0;
  596. }
  597. static int acpi_ec_add(struct acpi_device *device)
  598. {
  599. struct acpi_ec *ec = NULL;
  600. if (!device)
  601. return -EINVAL;
  602. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  603. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  604. /* Check for boot EC */
  605. if (boot_ec) {
  606. if (boot_ec->handle == device->handle) {
  607. /* Pre-loaded EC from DSDT, just move pointer */
  608. ec = boot_ec;
  609. boot_ec = NULL;
  610. goto end;
  611. } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
  612. /* ECDT-based EC, time to shut it down */
  613. ec_remove_handlers(boot_ec);
  614. kfree(boot_ec);
  615. first_ec = boot_ec = NULL;
  616. }
  617. }
  618. ec = make_acpi_ec();
  619. if (!ec)
  620. return -ENOMEM;
  621. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  622. AE_CTRL_TERMINATE) {
  623. kfree(ec);
  624. return -EINVAL;
  625. }
  626. ec->handle = device->handle;
  627. end:
  628. if (!first_ec)
  629. first_ec = ec;
  630. acpi_driver_data(device) = ec;
  631. acpi_ec_add_fs(device);
  632. printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  633. ec->gpe, ec->command_addr, ec->data_addr);
  634. return 0;
  635. }
  636. static int acpi_ec_remove(struct acpi_device *device, int type)
  637. {
  638. struct acpi_ec *ec;
  639. struct acpi_ec_query_handler *handler, *tmp;
  640. if (!device)
  641. return -EINVAL;
  642. ec = acpi_driver_data(device);
  643. mutex_lock(&ec->lock);
  644. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  645. list_del(&handler->node);
  646. kfree(handler);
  647. }
  648. mutex_unlock(&ec->lock);
  649. acpi_ec_remove_fs(device);
  650. acpi_driver_data(device) = NULL;
  651. if (ec == first_ec)
  652. first_ec = NULL;
  653. kfree(ec);
  654. return 0;
  655. }
  656. static acpi_status
  657. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  658. {
  659. struct acpi_ec *ec = context;
  660. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  661. return AE_OK;
  662. /*
  663. * The first address region returned is the data port, and
  664. * the second address region returned is the status/command
  665. * port.
  666. */
  667. if (ec->data_addr == 0)
  668. ec->data_addr = resource->data.io.minimum;
  669. else if (ec->command_addr == 0)
  670. ec->command_addr = resource->data.io.minimum;
  671. else
  672. return AE_CTRL_TERMINATE;
  673. return AE_OK;
  674. }
  675. static int ec_install_handlers(struct acpi_ec *ec)
  676. {
  677. acpi_status status;
  678. if (ec->handlers_installed)
  679. return 0;
  680. status = acpi_install_gpe_handler(NULL, ec->gpe,
  681. ACPI_GPE_EDGE_TRIGGERED,
  682. &acpi_ec_gpe_handler, ec);
  683. if (ACPI_FAILURE(status))
  684. return -ENODEV;
  685. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  686. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  687. status = acpi_install_address_space_handler(ec->handle,
  688. ACPI_ADR_SPACE_EC,
  689. &acpi_ec_space_handler,
  690. &acpi_ec_space_setup, ec);
  691. if (ACPI_FAILURE(status)) {
  692. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  693. return -ENODEV;
  694. }
  695. ec->handlers_installed = 1;
  696. return 0;
  697. }
  698. static int acpi_ec_start(struct acpi_device *device)
  699. {
  700. struct acpi_ec *ec;
  701. int ret = 0;
  702. if (!device)
  703. return -EINVAL;
  704. ec = acpi_driver_data(device);
  705. if (!ec)
  706. return -EINVAL;
  707. ret = ec_install_handlers(ec);
  708. /* EC is fully operational, allow queries */
  709. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  710. return ret;
  711. }
  712. static int acpi_ec_stop(struct acpi_device *device, int type)
  713. {
  714. struct acpi_ec *ec;
  715. if (!device)
  716. return -EINVAL;
  717. ec = acpi_driver_data(device);
  718. if (!ec)
  719. return -EINVAL;
  720. ec_remove_handlers(ec);
  721. return 0;
  722. }
  723. int __init acpi_ec_ecdt_probe(void)
  724. {
  725. int ret;
  726. acpi_status status;
  727. struct acpi_table_ecdt *ecdt_ptr;
  728. boot_ec = make_acpi_ec();
  729. if (!boot_ec)
  730. return -ENOMEM;
  731. /*
  732. * Generate a boot ec context
  733. */
  734. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  735. (struct acpi_table_header **)&ecdt_ptr);
  736. if (ACPI_SUCCESS(status)) {
  737. printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
  738. boot_ec->command_addr = ecdt_ptr->control.address;
  739. boot_ec->data_addr = ecdt_ptr->data.address;
  740. boot_ec->gpe = ecdt_ptr->gpe;
  741. boot_ec->handle = ACPI_ROOT_OBJECT;
  742. } else {
  743. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  744. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  745. boot_ec, NULL);
  746. /* Check that acpi_get_devices actually find something */
  747. if (ACPI_FAILURE(status) || !boot_ec->handle)
  748. goto error;
  749. }
  750. ret = ec_install_handlers(boot_ec);
  751. if (!ret) {
  752. first_ec = boot_ec;
  753. return 0;
  754. }
  755. error:
  756. kfree(boot_ec);
  757. boot_ec = NULL;
  758. return -ENODEV;
  759. }
  760. static int __init acpi_ec_init(void)
  761. {
  762. int result = 0;
  763. if (acpi_disabled)
  764. return 0;
  765. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  766. if (!acpi_ec_dir)
  767. return -ENODEV;
  768. /* Now register the driver for the EC */
  769. result = acpi_bus_register_driver(&acpi_ec_driver);
  770. if (result < 0) {
  771. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  772. return -ENODEV;
  773. }
  774. return result;
  775. }
  776. subsys_initcall(acpi_ec_init);
  777. /* EC driver currently not unloadable */
  778. #if 0
  779. static void __exit acpi_ec_exit(void)
  780. {
  781. acpi_bus_unregister_driver(&acpi_ec_driver);
  782. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  783. return;
  784. }
  785. #endif /* 0 */