ec.c 20 KB

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