ec.c 22 KB

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