ec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
  3. *
  4. * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/delay.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/interrupt.h>
  34. #include <asm/io.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #include <acpi/actypes.h>
  38. #define _COMPONENT ACPI_EC_COMPONENT
  39. ACPI_MODULE_NAME("ec");
  40. #define ACPI_EC_COMPONENT 0x00100000
  41. #define ACPI_EC_CLASS "embedded_controller"
  42. #define ACPI_EC_HID "PNP0C09"
  43. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  44. #define ACPI_EC_FILE_INFO "info"
  45. #undef PREFIX
  46. #define PREFIX "ACPI: EC: "
  47. /* EC status register */
  48. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  49. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  50. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  51. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  52. /* EC commands */
  53. enum ec_command {
  54. ACPI_EC_COMMAND_READ = 0x80,
  55. ACPI_EC_COMMAND_WRITE = 0x81,
  56. ACPI_EC_BURST_ENABLE = 0x82,
  57. ACPI_EC_BURST_DISABLE = 0x83,
  58. ACPI_EC_COMMAND_QUERY = 0x84,
  59. };
  60. /* EC events */
  61. enum ec_event {
  62. ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
  63. ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
  64. };
  65. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  66. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  67. static enum ec_mode {
  68. EC_INTR = 1, /* Output buffer full */
  69. EC_POLL, /* Input buffer empty */
  70. } acpi_ec_mode = EC_INTR;
  71. static int acpi_ec_remove(struct acpi_device *device, int type);
  72. static int acpi_ec_start(struct acpi_device *device);
  73. static int acpi_ec_stop(struct acpi_device *device, int type);
  74. static int acpi_ec_add(struct acpi_device *device);
  75. static struct acpi_driver acpi_ec_driver = {
  76. .name = "ec",
  77. .class = ACPI_EC_CLASS,
  78. .ids = ACPI_EC_HID,
  79. .ops = {
  80. .add = acpi_ec_add,
  81. .remove = acpi_ec_remove,
  82. .start = acpi_ec_start,
  83. .stop = acpi_ec_stop,
  84. },
  85. };
  86. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  87. static struct acpi_ec {
  88. acpi_handle handle;
  89. unsigned long uid;
  90. unsigned long gpe;
  91. unsigned long command_addr;
  92. unsigned long data_addr;
  93. unsigned long global_lock;
  94. struct mutex lock;
  95. atomic_t query_pending;
  96. atomic_t event_count;
  97. wait_queue_head_t wait;
  98. } *boot_ec;
  99. /* External interfaces use first EC only, so remember */
  100. static struct acpi_device *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. struct acpi_ec *ec;
  266. if (!first_ec)
  267. return -ENODEV;
  268. ec = acpi_driver_data(first_ec);
  269. return acpi_ec_burst_enable(ec);
  270. }
  271. EXPORT_SYMBOL(ec_burst_enable);
  272. int ec_burst_disable(void)
  273. {
  274. struct acpi_ec *ec;
  275. if (!first_ec)
  276. return -ENODEV;
  277. ec = acpi_driver_data(first_ec);
  278. return acpi_ec_burst_disable(ec);
  279. }
  280. EXPORT_SYMBOL(ec_burst_disable);
  281. int ec_read(u8 addr, u8 * val)
  282. {
  283. struct acpi_ec *ec;
  284. int err;
  285. u8 temp_data;
  286. if (!first_ec)
  287. return -ENODEV;
  288. ec = acpi_driver_data(first_ec);
  289. err = acpi_ec_read(ec, addr, &temp_data);
  290. if (!err) {
  291. *val = temp_data;
  292. return 0;
  293. } else
  294. return err;
  295. }
  296. EXPORT_SYMBOL(ec_read);
  297. int ec_write(u8 addr, u8 val)
  298. {
  299. struct acpi_ec *ec;
  300. int err;
  301. if (!first_ec)
  302. return -ENODEV;
  303. ec = acpi_driver_data(first_ec);
  304. err = acpi_ec_write(ec, addr, val);
  305. return err;
  306. }
  307. EXPORT_SYMBOL(ec_write);
  308. int ec_transaction(u8 command,
  309. const u8 * wdata, unsigned wdata_len,
  310. u8 * rdata, unsigned rdata_len)
  311. {
  312. struct acpi_ec *ec;
  313. if (!first_ec)
  314. return -ENODEV;
  315. ec = acpi_driver_data(first_ec);
  316. return acpi_ec_transaction(ec, command, wdata,
  317. wdata_len, rdata, rdata_len);
  318. }
  319. EXPORT_SYMBOL(ec_transaction);
  320. static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
  321. {
  322. int result;
  323. u8 d;
  324. if (!ec || !data)
  325. return -EINVAL;
  326. /*
  327. * Query the EC to find out which _Qxx method we need to evaluate.
  328. * Note that successful completion of the query causes the ACPI_EC_SCI
  329. * bit to be cleared (and thus clearing the interrupt source).
  330. */
  331. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
  332. if (result)
  333. return result;
  334. if (!d)
  335. return -ENODATA;
  336. *data = d;
  337. return 0;
  338. }
  339. /* --------------------------------------------------------------------------
  340. Event Management
  341. -------------------------------------------------------------------------- */
  342. static void acpi_ec_gpe_query(void *ec_cxt)
  343. {
  344. struct acpi_ec *ec = ec_cxt;
  345. u8 value = 0;
  346. char object_name[8];
  347. if (!ec || acpi_ec_query(ec, &value))
  348. return;
  349. snprintf(object_name, 8, "_Q%2.2X", value);
  350. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
  351. acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
  352. }
  353. static u32 acpi_ec_gpe_handler(void *data)
  354. {
  355. acpi_status status = AE_OK;
  356. u8 value;
  357. struct acpi_ec *ec = data;
  358. atomic_inc(&ec->event_count);
  359. if (acpi_ec_mode == EC_INTR) {
  360. wake_up(&ec->wait);
  361. }
  362. value = acpi_ec_read_status(ec);
  363. if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
  364. atomic_set(&ec->query_pending, 1);
  365. status =
  366. acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
  367. ec);
  368. }
  369. return status == AE_OK ?
  370. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  371. }
  372. /* --------------------------------------------------------------------------
  373. Address Space Management
  374. -------------------------------------------------------------------------- */
  375. static acpi_status
  376. acpi_ec_space_setup(acpi_handle region_handle,
  377. u32 function, void *handler_context, void **return_context)
  378. {
  379. /*
  380. * The EC object is in the handler context and is needed
  381. * when calling the acpi_ec_space_handler.
  382. */
  383. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  384. handler_context : NULL;
  385. return AE_OK;
  386. }
  387. static acpi_status
  388. acpi_ec_space_handler(u32 function,
  389. acpi_physical_address address,
  390. u32 bit_width,
  391. acpi_integer * value,
  392. void *handler_context, void *region_context)
  393. {
  394. int result = 0;
  395. struct acpi_ec *ec = handler_context;
  396. u64 temp = *value;
  397. acpi_integer f_v = 0;
  398. int i = 0;
  399. if ((address > 0xFF) || !value || !handler_context)
  400. return AE_BAD_PARAMETER;
  401. if (bit_width != 8 && acpi_strict) {
  402. return AE_BAD_PARAMETER;
  403. }
  404. next_byte:
  405. switch (function) {
  406. case ACPI_READ:
  407. temp = 0;
  408. result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
  409. break;
  410. case ACPI_WRITE:
  411. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  412. break;
  413. default:
  414. result = -EINVAL;
  415. goto out;
  416. break;
  417. }
  418. bit_width -= 8;
  419. if (bit_width) {
  420. if (function == ACPI_READ)
  421. f_v |= temp << 8 * i;
  422. if (function == ACPI_WRITE)
  423. temp >>= 8;
  424. i++;
  425. address++;
  426. goto next_byte;
  427. }
  428. if (function == ACPI_READ) {
  429. f_v |= temp << 8 * i;
  430. *value = f_v;
  431. }
  432. out:
  433. switch (result) {
  434. case -EINVAL:
  435. return AE_BAD_PARAMETER;
  436. break;
  437. case -ENODEV:
  438. return AE_NOT_FOUND;
  439. break;
  440. case -ETIME:
  441. return AE_TIME;
  442. break;
  443. default:
  444. return AE_OK;
  445. }
  446. }
  447. /* --------------------------------------------------------------------------
  448. FS Interface (/proc)
  449. -------------------------------------------------------------------------- */
  450. static struct proc_dir_entry *acpi_ec_dir;
  451. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  452. {
  453. struct acpi_ec *ec = seq->private;
  454. if (!ec)
  455. goto end;
  456. seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
  457. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  458. (u32) ec->command_addr, (u32) ec->data_addr);
  459. seq_printf(seq, "use global lock: %s\n",
  460. ec->global_lock ? "yes" : "no");
  461. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  462. end:
  463. return 0;
  464. }
  465. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  466. {
  467. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  468. }
  469. static struct file_operations acpi_ec_info_ops = {
  470. .open = acpi_ec_info_open_fs,
  471. .read = seq_read,
  472. .llseek = seq_lseek,
  473. .release = single_release,
  474. .owner = THIS_MODULE,
  475. };
  476. static int acpi_ec_add_fs(struct acpi_device *device)
  477. {
  478. struct proc_dir_entry *entry = NULL;
  479. if (!acpi_device_dir(device)) {
  480. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  481. acpi_ec_dir);
  482. if (!acpi_device_dir(device))
  483. return -ENODEV;
  484. }
  485. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  486. acpi_device_dir(device));
  487. if (!entry)
  488. return -ENODEV;
  489. else {
  490. entry->proc_fops = &acpi_ec_info_ops;
  491. entry->data = acpi_driver_data(device);
  492. entry->owner = THIS_MODULE;
  493. }
  494. return 0;
  495. }
  496. static int acpi_ec_remove_fs(struct acpi_device *device)
  497. {
  498. if (acpi_device_dir(device)) {
  499. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  500. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  501. acpi_device_dir(device) = NULL;
  502. }
  503. return 0;
  504. }
  505. /* --------------------------------------------------------------------------
  506. Driver Interface
  507. -------------------------------------------------------------------------- */
  508. static acpi_status
  509. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  510. static acpi_status
  511. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
  512. static struct acpi_ec *make_acpi_ec(void)
  513. {
  514. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  515. if (!ec)
  516. return NULL;
  517. atomic_set(&ec->query_pending, 0);
  518. atomic_set(&ec->event_count, 1);
  519. mutex_init(&ec->lock);
  520. init_waitqueue_head(&ec->wait);
  521. return ec;
  522. }
  523. static int acpi_ec_add(struct acpi_device *device)
  524. {
  525. acpi_status status = AE_OK;
  526. struct acpi_ec *ec = NULL;
  527. if (!device)
  528. return -EINVAL;
  529. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  530. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  531. ec = make_acpi_ec();
  532. if (!ec)
  533. return -ENOMEM;
  534. status = ec_parse_device(device->handle, 0, ec, NULL);
  535. if (status != AE_CTRL_TERMINATE) {
  536. kfree(ec);
  537. return -EINVAL;
  538. }
  539. /* Check if we found the boot EC */
  540. if (boot_ec) {
  541. if (boot_ec->gpe == ec->gpe) {
  542. /* We might have incorrect info for GL at boot time */
  543. mutex_lock(&boot_ec->lock);
  544. boot_ec->global_lock = ec->global_lock;
  545. mutex_unlock(&boot_ec->lock);
  546. kfree(ec);
  547. ec = boot_ec;
  548. }
  549. }
  550. ec->handle = device->handle;
  551. acpi_driver_data(device) = ec;
  552. if (!first_ec)
  553. first_ec = device;
  554. acpi_ec_add_fs(device);
  555. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
  556. acpi_device_name(device), acpi_device_bid(device),
  557. (u32) ec->gpe));
  558. return 0;
  559. }
  560. static int acpi_ec_remove(struct acpi_device *device, int type)
  561. {
  562. struct acpi_ec *ec = NULL;
  563. if (!device)
  564. return -EINVAL;
  565. ec = acpi_driver_data(device);
  566. acpi_ec_remove_fs(device);
  567. acpi_driver_data(device) = NULL;
  568. if (device == first_ec)
  569. first_ec = NULL;
  570. /* Don't touch boot EC */
  571. if (boot_ec != ec)
  572. kfree(ec);
  573. return 0;
  574. }
  575. static acpi_status
  576. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  577. {
  578. struct acpi_ec *ec = context;
  579. if (resource->type != ACPI_RESOURCE_TYPE_IO) {
  580. return AE_OK;
  581. }
  582. /*
  583. * The first address region returned is the data port, and
  584. * the second address region returned is the status/command
  585. * port.
  586. */
  587. if (ec->data_addr == 0) {
  588. ec->data_addr = resource->data.io.minimum;
  589. } else if (ec->command_addr == 0) {
  590. ec->command_addr = resource->data.io.minimum;
  591. } else {
  592. return AE_CTRL_TERMINATE;
  593. }
  594. return AE_OK;
  595. }
  596. static int ec_install_handlers(struct acpi_ec *ec)
  597. {
  598. acpi_status status;
  599. status = acpi_install_gpe_handler(NULL, ec->gpe,
  600. ACPI_GPE_EDGE_TRIGGERED,
  601. &acpi_ec_gpe_handler, ec);
  602. if (ACPI_FAILURE(status))
  603. return -ENODEV;
  604. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  605. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  606. status = acpi_install_address_space_handler(ec->handle,
  607. ACPI_ADR_SPACE_EC,
  608. &acpi_ec_space_handler,
  609. &acpi_ec_space_setup, ec);
  610. if (ACPI_FAILURE(status)) {
  611. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  612. return -ENODEV;
  613. }
  614. return 0;
  615. }
  616. static int acpi_ec_start(struct acpi_device *device)
  617. {
  618. struct acpi_ec *ec;
  619. if (!device)
  620. return -EINVAL;
  621. ec = acpi_driver_data(device);
  622. if (!ec)
  623. return -EINVAL;
  624. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
  625. ec->gpe, ec->command_addr, ec->data_addr));
  626. /* Boot EC is already working */
  627. if (ec == boot_ec)
  628. return 0;
  629. return ec_install_handlers(ec);
  630. }
  631. static int acpi_ec_stop(struct acpi_device *device, int type)
  632. {
  633. acpi_status status;
  634. struct acpi_ec *ec;
  635. if (!device)
  636. return -EINVAL;
  637. ec = acpi_driver_data(device);
  638. if (!ec)
  639. return -EINVAL;
  640. /* Don't touch boot EC */
  641. if (ec == boot_ec)
  642. return 0;
  643. status = acpi_remove_address_space_handler(ec->handle,
  644. ACPI_ADR_SPACE_EC,
  645. &acpi_ec_space_handler);
  646. if (ACPI_FAILURE(status))
  647. return -ENODEV;
  648. status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  649. if (ACPI_FAILURE(status))
  650. return -ENODEV;
  651. return 0;
  652. }
  653. static acpi_status
  654. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  655. {
  656. acpi_status status;
  657. struct acpi_ec *ec = context;
  658. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  659. ec_parse_io_ports, ec);
  660. if (ACPI_FAILURE(status))
  661. return status;
  662. /* Get GPE bit assignment (EC events). */
  663. /* TODO: Add support for _GPE returning a package */
  664. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
  665. if (ACPI_FAILURE(status))
  666. return status;
  667. /* Use the global lock for all EC transactions? */
  668. acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
  669. ec->handle = handle;
  670. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
  671. ec->gpe, ec->command_addr, ec->data_addr));
  672. return AE_CTRL_TERMINATE;
  673. }
  674. int __init acpi_ec_ecdt_probe(void)
  675. {
  676. int ret;
  677. acpi_status status;
  678. struct acpi_table_ecdt *ecdt_ptr;
  679. boot_ec = make_acpi_ec();
  680. if (!boot_ec)
  681. return -ENOMEM;
  682. /*
  683. * Generate a boot ec context
  684. */
  685. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  686. (struct acpi_table_header **)&ecdt_ptr);
  687. if (ACPI_FAILURE(status))
  688. goto error;
  689. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
  690. boot_ec->command_addr = ecdt_ptr->control.address;
  691. boot_ec->data_addr = ecdt_ptr->data.address;
  692. boot_ec->gpe = ecdt_ptr->gpe;
  693. boot_ec->uid = ecdt_ptr->uid;
  694. boot_ec->handle = ACPI_ROOT_OBJECT;
  695. ret = ec_install_handlers(boot_ec);
  696. if (!ret)
  697. return 0;
  698. error:
  699. kfree(boot_ec);
  700. boot_ec = NULL;
  701. return -ENODEV;
  702. }
  703. static int __init acpi_ec_init(void)
  704. {
  705. int result = 0;
  706. if (acpi_disabled)
  707. return 0;
  708. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  709. if (!acpi_ec_dir)
  710. return -ENODEV;
  711. /* Now register the driver for the EC */
  712. result = acpi_bus_register_driver(&acpi_ec_driver);
  713. if (result < 0) {
  714. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  715. return -ENODEV;
  716. }
  717. return result;
  718. }
  719. subsys_initcall(acpi_ec_init);
  720. /* EC driver currently not unloadable */
  721. #if 0
  722. static void __exit acpi_ec_exit(void)
  723. {
  724. acpi_bus_unregister_driver(&acpi_ec_driver);
  725. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  726. return;
  727. }
  728. #endif /* 0 */
  729. static int __init acpi_ec_set_intr_mode(char *str)
  730. {
  731. int intr;
  732. if (!get_option(&str, &intr))
  733. return 0;
  734. acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
  735. printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
  736. return 1;
  737. }
  738. __setup("ec_intr=", acpi_ec_set_intr_mode);