ec.c 21 KB

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