ec.c 22 KB

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