ec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. }
  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. printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  582. ec->gpe, ec->command_addr, ec->data_addr);
  583. return AE_CTRL_TERMINATE;
  584. }
  585. static int acpi_ec_add(struct acpi_device *device)
  586. {
  587. struct acpi_ec *ec = NULL;
  588. if (!device)
  589. return -EINVAL;
  590. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  591. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  592. ec = make_acpi_ec();
  593. if (!ec)
  594. return -ENOMEM;
  595. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  596. AE_CTRL_TERMINATE) {
  597. kfree(ec);
  598. return -EINVAL;
  599. }
  600. /* Check if we found the boot EC */
  601. if (boot_ec) {
  602. if (boot_ec->gpe == ec->gpe) {
  603. /* We might have incorrect info for GL at boot time */
  604. mutex_lock(&boot_ec->lock);
  605. boot_ec->global_lock = ec->global_lock;
  606. /* Copy handlers from new ec into boot ec */
  607. list_splice(&ec->list, &boot_ec->list);
  608. mutex_unlock(&boot_ec->lock);
  609. kfree(ec);
  610. ec = boot_ec;
  611. }
  612. } else
  613. first_ec = ec;
  614. ec->handle = device->handle;
  615. acpi_driver_data(device) = ec;
  616. acpi_ec_add_fs(device);
  617. return 0;
  618. }
  619. static int acpi_ec_remove(struct acpi_device *device, int type)
  620. {
  621. struct acpi_ec *ec;
  622. struct acpi_ec_query_handler *handler, *tmp;
  623. if (!device)
  624. return -EINVAL;
  625. ec = acpi_driver_data(device);
  626. mutex_lock(&ec->lock);
  627. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  628. list_del(&handler->node);
  629. kfree(handler);
  630. }
  631. mutex_unlock(&ec->lock);
  632. acpi_ec_remove_fs(device);
  633. acpi_driver_data(device) = NULL;
  634. if (ec == first_ec)
  635. first_ec = NULL;
  636. /* Don't touch boot EC */
  637. if (boot_ec != ec)
  638. kfree(ec);
  639. return 0;
  640. }
  641. static acpi_status
  642. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  643. {
  644. struct acpi_ec *ec = context;
  645. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  646. return AE_OK;
  647. /*
  648. * The first address region returned is the data port, and
  649. * the second address region returned is the status/command
  650. * port.
  651. */
  652. if (ec->data_addr == 0)
  653. ec->data_addr = resource->data.io.minimum;
  654. else if (ec->command_addr == 0)
  655. ec->command_addr = resource->data.io.minimum;
  656. else
  657. return AE_CTRL_TERMINATE;
  658. return AE_OK;
  659. }
  660. static int ec_install_handlers(struct acpi_ec *ec)
  661. {
  662. acpi_status status;
  663. status = acpi_install_gpe_handler(NULL, ec->gpe,
  664. ACPI_GPE_EDGE_TRIGGERED,
  665. &acpi_ec_gpe_handler, ec);
  666. if (ACPI_FAILURE(status))
  667. return -ENODEV;
  668. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  669. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  670. status = acpi_install_address_space_handler(ec->handle,
  671. ACPI_ADR_SPACE_EC,
  672. &acpi_ec_space_handler,
  673. &acpi_ec_space_setup, ec);
  674. if (ACPI_FAILURE(status)) {
  675. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  676. return -ENODEV;
  677. }
  678. return 0;
  679. }
  680. static int acpi_ec_start(struct acpi_device *device)
  681. {
  682. struct acpi_ec *ec;
  683. int ret = 0;
  684. if (!device)
  685. return -EINVAL;
  686. ec = acpi_driver_data(device);
  687. if (!ec)
  688. return -EINVAL;
  689. /* Boot EC is already working */
  690. if (ec != boot_ec)
  691. ret = ec_install_handlers(ec);
  692. /* EC is fully operational, allow queries */
  693. atomic_set(&ec->query_pending, 0);
  694. return ret;
  695. }
  696. static int acpi_ec_stop(struct acpi_device *device, int type)
  697. {
  698. acpi_status status;
  699. struct acpi_ec *ec;
  700. if (!device)
  701. return -EINVAL;
  702. ec = acpi_driver_data(device);
  703. if (!ec)
  704. return -EINVAL;
  705. /* Don't touch boot EC */
  706. if (ec == boot_ec)
  707. return 0;
  708. status = acpi_remove_address_space_handler(ec->handle,
  709. ACPI_ADR_SPACE_EC,
  710. &acpi_ec_space_handler);
  711. if (ACPI_FAILURE(status))
  712. return -ENODEV;
  713. status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  714. if (ACPI_FAILURE(status))
  715. return -ENODEV;
  716. return 0;
  717. }
  718. int __init acpi_ec_ecdt_probe(void)
  719. {
  720. int ret;
  721. acpi_status status;
  722. struct acpi_table_ecdt *ecdt_ptr;
  723. boot_ec = make_acpi_ec();
  724. if (!boot_ec)
  725. return -ENOMEM;
  726. /*
  727. * Generate a boot ec context
  728. */
  729. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  730. (struct acpi_table_header **)&ecdt_ptr);
  731. if (ACPI_SUCCESS(status)) {
  732. printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n\n");
  733. boot_ec->command_addr = ecdt_ptr->control.address;
  734. boot_ec->data_addr = ecdt_ptr->data.address;
  735. boot_ec->gpe = ecdt_ptr->gpe;
  736. boot_ec->handle = ACPI_ROOT_OBJECT;
  737. } else {
  738. printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
  739. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  740. boot_ec, NULL);
  741. /* Check that acpi_get_devices actually find something */
  742. if (ACPI_FAILURE(status) || !boot_ec->handle)
  743. goto error;
  744. }
  745. ret = ec_install_handlers(boot_ec);
  746. if (!ret) {
  747. first_ec = boot_ec;
  748. return 0;
  749. }
  750. error:
  751. kfree(boot_ec);
  752. boot_ec = NULL;
  753. return -ENODEV;
  754. }
  755. static int __init acpi_ec_init(void)
  756. {
  757. int result = 0;
  758. if (acpi_disabled)
  759. return 0;
  760. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  761. if (!acpi_ec_dir)
  762. return -ENODEV;
  763. /* Now register the driver for the EC */
  764. result = acpi_bus_register_driver(&acpi_ec_driver);
  765. if (result < 0) {
  766. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  767. return -ENODEV;
  768. }
  769. return result;
  770. }
  771. subsys_initcall(acpi_ec_init);
  772. /* EC driver currently not unloadable */
  773. #if 0
  774. static void __exit acpi_ec_exit(void)
  775. {
  776. acpi_bus_unregister_driver(&acpi_ec_driver);
  777. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  778. return;
  779. }
  780. #endif /* 0 */
  781. static int __init acpi_ec_set_intr_mode(char *str)
  782. {
  783. int intr;
  784. if (!get_option(&str, &intr))
  785. return 0;
  786. acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
  787. printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
  788. return 1;
  789. }
  790. __setup("ec_intr=", acpi_ec_set_intr_mode);