ec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. } *boot_ec, *first_ec;
  111. /* --------------------------------------------------------------------------
  112. Transaction Management
  113. -------------------------------------------------------------------------- */
  114. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  115. {
  116. return inb(ec->command_addr);
  117. }
  118. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  119. {
  120. return inb(ec->data_addr);
  121. }
  122. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  123. {
  124. outb(command, ec->command_addr);
  125. }
  126. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  127. {
  128. outb(data, ec->data_addr);
  129. }
  130. static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
  131. unsigned old_count)
  132. {
  133. u8 status = acpi_ec_read_status(ec);
  134. if (old_count == atomic_read(&ec->event_count))
  135. return 0;
  136. if (event == ACPI_EC_EVENT_OBF_1) {
  137. if (status & ACPI_EC_FLAG_OBF)
  138. return 1;
  139. } else if (event == ACPI_EC_EVENT_IBF_0) {
  140. if (!(status & ACPI_EC_FLAG_IBF))
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
  146. unsigned count, int force_poll)
  147. {
  148. if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
  149. unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
  150. while (time_before(jiffies, delay)) {
  151. if (acpi_ec_check_status(ec, event, 0))
  152. return 0;
  153. }
  154. } else {
  155. if (wait_event_timeout(ec->wait,
  156. acpi_ec_check_status(ec, event, count),
  157. msecs_to_jiffies(ACPI_EC_DELAY)) ||
  158. acpi_ec_check_status(ec, event, 0)) {
  159. return 0;
  160. } else {
  161. printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
  162. " status = %d, expect_event = %d\n",
  163. acpi_ec_read_status(ec), event);
  164. }
  165. }
  166. return -ETIME;
  167. }
  168. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
  169. const u8 * wdata, unsigned wdata_len,
  170. u8 * rdata, unsigned rdata_len,
  171. int force_poll)
  172. {
  173. int result = 0;
  174. unsigned count = atomic_read(&ec->event_count);
  175. acpi_ec_write_cmd(ec, command);
  176. for (; wdata_len > 0; --wdata_len) {
  177. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
  178. if (result) {
  179. printk(KERN_ERR PREFIX
  180. "write_cmd timeout, command = %d\n", command);
  181. goto end;
  182. }
  183. count = atomic_read(&ec->event_count);
  184. acpi_ec_write_data(ec, *(wdata++));
  185. }
  186. if (!rdata_len) {
  187. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
  188. if (result) {
  189. printk(KERN_ERR PREFIX
  190. "finish-write timeout, command = %d\n", command);
  191. goto end;
  192. }
  193. } else if (command == ACPI_EC_COMMAND_QUERY) {
  194. atomic_set(&ec->query_pending, 0);
  195. }
  196. for (; rdata_len > 0; --rdata_len) {
  197. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
  198. if (result) {
  199. printk(KERN_ERR PREFIX "read timeout, command = %d\n",
  200. command);
  201. goto end;
  202. }
  203. count = atomic_read(&ec->event_count);
  204. *(rdata++) = acpi_ec_read_data(ec);
  205. }
  206. end:
  207. return result;
  208. }
  209. static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
  210. const u8 * wdata, unsigned wdata_len,
  211. u8 * rdata, unsigned rdata_len,
  212. int force_poll)
  213. {
  214. int status;
  215. u32 glk;
  216. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  217. return -EINVAL;
  218. if (rdata)
  219. memset(rdata, 0, rdata_len);
  220. mutex_lock(&ec->lock);
  221. if (ec->global_lock) {
  222. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  223. if (ACPI_FAILURE(status)) {
  224. mutex_unlock(&ec->lock);
  225. return -ENODEV;
  226. }
  227. }
  228. /* Make sure GPE is enabled before doing transaction */
  229. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  230. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 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_tail(&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. break;
  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. printk(KERN_ERR PREFIX "Handler for query 0x%x is not found!\n", value);
  403. }
  404. static u32 acpi_ec_gpe_handler(void *data)
  405. {
  406. acpi_status status = AE_OK;
  407. u8 value;
  408. struct acpi_ec *ec = data;
  409. atomic_inc(&ec->event_count);
  410. if (acpi_ec_mode == EC_INTR) {
  411. wake_up(&ec->wait);
  412. }
  413. value = acpi_ec_read_status(ec);
  414. if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
  415. atomic_set(&ec->query_pending, 1);
  416. status =
  417. acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
  418. }
  419. return status == AE_OK ?
  420. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  421. }
  422. /* --------------------------------------------------------------------------
  423. Address Space Management
  424. -------------------------------------------------------------------------- */
  425. static acpi_status
  426. acpi_ec_space_setup(acpi_handle region_handle,
  427. u32 function, void *handler_context, void **return_context)
  428. {
  429. /*
  430. * The EC object is in the handler context and is needed
  431. * when calling the acpi_ec_space_handler.
  432. */
  433. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  434. handler_context : NULL;
  435. return AE_OK;
  436. }
  437. static acpi_status
  438. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  439. u32 bits, acpi_integer *value,
  440. void *handler_context, void *region_context)
  441. {
  442. struct acpi_ec *ec = handler_context;
  443. int result = 0, i = 0;
  444. u8 temp = 0;
  445. if ((address > 0xFF) || !value || !handler_context)
  446. return AE_BAD_PARAMETER;
  447. if (function != ACPI_READ && function != ACPI_WRITE)
  448. return AE_BAD_PARAMETER;
  449. if (bits != 8 && acpi_strict)
  450. return AE_BAD_PARAMETER;
  451. while (bits - i > 0) {
  452. if (function == ACPI_READ) {
  453. result = acpi_ec_read(ec, address, &temp);
  454. (*value) |= ((acpi_integer)temp) << i;
  455. } else {
  456. temp = 0xff & ((*value) >> i);
  457. result = acpi_ec_write(ec, address, temp);
  458. }
  459. i += 8;
  460. ++address;
  461. }
  462. switch (result) {
  463. case -EINVAL:
  464. return AE_BAD_PARAMETER;
  465. break;
  466. case -ENODEV:
  467. return AE_NOT_FOUND;
  468. break;
  469. case -ETIME:
  470. return AE_TIME;
  471. break;
  472. default:
  473. return AE_OK;
  474. }
  475. }
  476. /* --------------------------------------------------------------------------
  477. FS Interface (/proc)
  478. -------------------------------------------------------------------------- */
  479. static struct proc_dir_entry *acpi_ec_dir;
  480. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  481. {
  482. struct acpi_ec *ec = seq->private;
  483. if (!ec)
  484. goto end;
  485. seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
  486. seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
  487. (unsigned)ec->command_addr, (unsigned)ec->data_addr);
  488. seq_printf(seq, "use global lock:\t%s\n",
  489. ec->global_lock ? "yes" : "no");
  490. end:
  491. return 0;
  492. }
  493. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  494. {
  495. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  496. }
  497. static struct file_operations acpi_ec_info_ops = {
  498. .open = acpi_ec_info_open_fs,
  499. .read = seq_read,
  500. .llseek = seq_lseek,
  501. .release = single_release,
  502. .owner = THIS_MODULE,
  503. };
  504. static int acpi_ec_add_fs(struct acpi_device *device)
  505. {
  506. struct proc_dir_entry *entry = NULL;
  507. if (!acpi_device_dir(device)) {
  508. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  509. acpi_ec_dir);
  510. if (!acpi_device_dir(device))
  511. return -ENODEV;
  512. }
  513. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  514. acpi_device_dir(device));
  515. if (!entry)
  516. return -ENODEV;
  517. else {
  518. entry->proc_fops = &acpi_ec_info_ops;
  519. entry->data = acpi_driver_data(device);
  520. entry->owner = THIS_MODULE;
  521. }
  522. return 0;
  523. }
  524. static int acpi_ec_remove_fs(struct acpi_device *device)
  525. {
  526. if (acpi_device_dir(device)) {
  527. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  528. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  529. acpi_device_dir(device) = NULL;
  530. }
  531. return 0;
  532. }
  533. /* --------------------------------------------------------------------------
  534. Driver Interface
  535. -------------------------------------------------------------------------- */
  536. static acpi_status
  537. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  538. static struct acpi_ec *make_acpi_ec(void)
  539. {
  540. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  541. if (!ec)
  542. return NULL;
  543. atomic_set(&ec->query_pending, 1);
  544. atomic_set(&ec->event_count, 1);
  545. mutex_init(&ec->lock);
  546. init_waitqueue_head(&ec->wait);
  547. INIT_LIST_HEAD(&ec->list);
  548. return ec;
  549. }
  550. static acpi_status
  551. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  552. void *context, void **return_value)
  553. {
  554. struct acpi_namespace_node *node = handle;
  555. struct acpi_ec *ec = context;
  556. int value = 0;
  557. if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
  558. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  559. }
  560. return AE_OK;
  561. }
  562. static int ec_parse_device(struct acpi_ec *ec, acpi_handle handle)
  563. {
  564. if (ACPI_FAILURE(acpi_walk_resources(handle, METHOD_NAME__CRS,
  565. ec_parse_io_ports, ec)))
  566. return -EINVAL;
  567. /* Get GPE bit assignment (EC events). */
  568. /* TODO: Add support for _GPE returning a package */
  569. if (ACPI_FAILURE(acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe)))
  570. return -EINVAL;
  571. /* Use the global lock for all EC transactions? */
  572. acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
  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. ec->handle = handle;
  577. printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx",
  578. ec->gpe, ec->command_addr, ec->data_addr);
  579. return 0;
  580. }
  581. static int acpi_ec_add(struct acpi_device *device)
  582. {
  583. struct acpi_ec *ec = NULL;
  584. if (!device)
  585. return -EINVAL;
  586. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  587. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  588. ec = make_acpi_ec();
  589. if (!ec)
  590. return -ENOMEM;
  591. if (ec_parse_device(ec, device->handle)) {
  592. kfree(ec);
  593. return -EINVAL;
  594. }
  595. /* Check if we found the boot EC */
  596. if (boot_ec) {
  597. if (boot_ec->gpe == ec->gpe) {
  598. /* We might have incorrect info for GL at boot time */
  599. mutex_lock(&boot_ec->lock);
  600. boot_ec->global_lock = ec->global_lock;
  601. /* Copy handlers from new ec into boot ec */
  602. list_splice(&ec->list, &boot_ec->list);
  603. mutex_unlock(&boot_ec->lock);
  604. kfree(ec);
  605. ec = boot_ec;
  606. }
  607. } else
  608. first_ec = ec;
  609. ec->handle = device->handle;
  610. acpi_driver_data(device) = ec;
  611. acpi_ec_add_fs(device);
  612. return 0;
  613. }
  614. static int acpi_ec_remove(struct acpi_device *device, int type)
  615. {
  616. struct acpi_ec *ec;
  617. struct acpi_ec_query_handler *handler;
  618. if (!device)
  619. return -EINVAL;
  620. ec = acpi_driver_data(device);
  621. mutex_lock(&ec->lock);
  622. list_for_each_entry(handler, &ec->list, node) {
  623. list_del(&handler->node);
  624. kfree(handler);
  625. }
  626. mutex_unlock(&ec->lock);
  627. acpi_ec_remove_fs(device);
  628. acpi_driver_data(device) = NULL;
  629. if (ec == first_ec)
  630. first_ec = NULL;
  631. /* Don't touch boot EC */
  632. if (boot_ec != ec)
  633. kfree(ec);
  634. return 0;
  635. }
  636. static acpi_status
  637. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  638. {
  639. struct acpi_ec *ec = context;
  640. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  641. return AE_OK;
  642. /*
  643. * The first address region returned is the data port, and
  644. * the second address region returned is the status/command
  645. * port.
  646. */
  647. if (ec->data_addr == 0)
  648. ec->data_addr = resource->data.io.minimum;
  649. else if (ec->command_addr == 0)
  650. ec->command_addr = resource->data.io.minimum;
  651. else
  652. return AE_CTRL_TERMINATE;
  653. return AE_OK;
  654. }
  655. static int ec_install_handlers(struct acpi_ec *ec)
  656. {
  657. acpi_status status;
  658. status = acpi_install_gpe_handler(NULL, ec->gpe,
  659. ACPI_GPE_EDGE_TRIGGERED,
  660. &acpi_ec_gpe_handler, ec);
  661. if (ACPI_FAILURE(status))
  662. return -ENODEV;
  663. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  664. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  665. status = acpi_install_address_space_handler(ec->handle,
  666. ACPI_ADR_SPACE_EC,
  667. &acpi_ec_space_handler,
  668. &acpi_ec_space_setup, ec);
  669. if (ACPI_FAILURE(status)) {
  670. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  671. return -ENODEV;
  672. }
  673. return 0;
  674. }
  675. static int acpi_ec_start(struct acpi_device *device)
  676. {
  677. struct acpi_ec *ec;
  678. int ret = 0;
  679. if (!device)
  680. return -EINVAL;
  681. ec = acpi_driver_data(device);
  682. if (!ec)
  683. return -EINVAL;
  684. /* Boot EC is already working */
  685. if (ec != boot_ec)
  686. ret = ec_install_handlers(ec);
  687. /* EC is fully operational, allow queries */
  688. atomic_set(&ec->query_pending, 0);
  689. return ret;
  690. }
  691. static int acpi_ec_stop(struct acpi_device *device, int type)
  692. {
  693. acpi_status status;
  694. struct acpi_ec *ec;
  695. if (!device)
  696. return -EINVAL;
  697. ec = acpi_driver_data(device);
  698. if (!ec)
  699. return -EINVAL;
  700. /* Don't touch boot EC */
  701. if (ec == boot_ec)
  702. return 0;
  703. status = acpi_remove_address_space_handler(ec->handle,
  704. ACPI_ADR_SPACE_EC,
  705. &acpi_ec_space_handler);
  706. if (ACPI_FAILURE(status))
  707. return -ENODEV;
  708. status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  709. if (ACPI_FAILURE(status))
  710. return -ENODEV;
  711. return 0;
  712. }
  713. int __init acpi_ec_ecdt_probe(void)
  714. {
  715. int ret;
  716. acpi_status status;
  717. struct acpi_table_ecdt *ecdt_ptr;
  718. boot_ec = make_acpi_ec();
  719. if (!boot_ec)
  720. return -ENOMEM;
  721. /*
  722. * Generate a boot ec context
  723. */
  724. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  725. (struct acpi_table_header **)&ecdt_ptr);
  726. if (ACPI_FAILURE(status))
  727. goto error;
  728. printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
  729. boot_ec->command_addr = ecdt_ptr->control.address;
  730. boot_ec->data_addr = ecdt_ptr->data.address;
  731. boot_ec->gpe = ecdt_ptr->gpe;
  732. boot_ec->handle = ACPI_ROOT_OBJECT;
  733. ret = ec_install_handlers(boot_ec);
  734. if (!ret) {
  735. first_ec = boot_ec;
  736. return 0;
  737. }
  738. error:
  739. kfree(boot_ec);
  740. boot_ec = NULL;
  741. return -ENODEV;
  742. }
  743. static int __init acpi_ec_init(void)
  744. {
  745. int result = 0;
  746. if (acpi_disabled)
  747. return 0;
  748. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  749. if (!acpi_ec_dir)
  750. return -ENODEV;
  751. /* Now register the driver for the EC */
  752. result = acpi_bus_register_driver(&acpi_ec_driver);
  753. if (result < 0) {
  754. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  755. return -ENODEV;
  756. }
  757. return result;
  758. }
  759. subsys_initcall(acpi_ec_init);
  760. /* EC driver currently not unloadable */
  761. #if 0
  762. static void __exit acpi_ec_exit(void)
  763. {
  764. acpi_bus_unregister_driver(&acpi_ec_driver);
  765. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  766. return;
  767. }
  768. #endif /* 0 */
  769. static int __init acpi_ec_set_intr_mode(char *str)
  770. {
  771. int intr;
  772. if (!get_option(&str, &intr))
  773. return 0;
  774. acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
  775. printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
  776. return 1;
  777. }
  778. __setup("ec_intr=", acpi_ec_set_intr_mode);