ec.c 22 KB

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