ec.c 22 KB

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