ec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. } *ec_ecdt;
  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 = (struct acpi_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 = (struct acpi_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 = NULL;
  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. ec = (struct acpi_ec *)handler_context;
  405. next_byte:
  406. switch (function) {
  407. case ACPI_READ:
  408. temp = 0;
  409. result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
  410. break;
  411. case ACPI_WRITE:
  412. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  413. break;
  414. default:
  415. result = -EINVAL;
  416. goto out;
  417. break;
  418. }
  419. bit_width -= 8;
  420. if (bit_width) {
  421. if (function == ACPI_READ)
  422. f_v |= temp << 8 * i;
  423. if (function == ACPI_WRITE)
  424. temp >>= 8;
  425. i++;
  426. address++;
  427. goto next_byte;
  428. }
  429. if (function == ACPI_READ) {
  430. f_v |= temp << 8 * i;
  431. *value = f_v;
  432. }
  433. out:
  434. switch (result) {
  435. case -EINVAL:
  436. return AE_BAD_PARAMETER;
  437. break;
  438. case -ENODEV:
  439. return AE_NOT_FOUND;
  440. break;
  441. case -ETIME:
  442. return AE_TIME;
  443. break;
  444. default:
  445. return AE_OK;
  446. }
  447. }
  448. /* --------------------------------------------------------------------------
  449. FS Interface (/proc)
  450. -------------------------------------------------------------------------- */
  451. static struct proc_dir_entry *acpi_ec_dir;
  452. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  453. {
  454. struct acpi_ec *ec = (struct acpi_ec *)seq->private;
  455. if (!ec)
  456. goto end;
  457. seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
  458. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  459. (u32) ec->command_addr, (u32) ec->data_addr);
  460. seq_printf(seq, "use global lock: %s\n",
  461. ec->global_lock ? "yes" : "no");
  462. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  463. end:
  464. return 0;
  465. }
  466. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  467. {
  468. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  469. }
  470. static struct file_operations acpi_ec_info_ops = {
  471. .open = acpi_ec_info_open_fs,
  472. .read = seq_read,
  473. .llseek = seq_lseek,
  474. .release = single_release,
  475. .owner = THIS_MODULE,
  476. };
  477. static int acpi_ec_add_fs(struct acpi_device *device)
  478. {
  479. struct proc_dir_entry *entry = NULL;
  480. if (!acpi_device_dir(device)) {
  481. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  482. acpi_ec_dir);
  483. if (!acpi_device_dir(device))
  484. return -ENODEV;
  485. }
  486. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  487. acpi_device_dir(device));
  488. if (!entry)
  489. return -ENODEV;
  490. else {
  491. entry->proc_fops = &acpi_ec_info_ops;
  492. entry->data = acpi_driver_data(device);
  493. entry->owner = THIS_MODULE;
  494. }
  495. return 0;
  496. }
  497. static int acpi_ec_remove_fs(struct acpi_device *device)
  498. {
  499. if (acpi_device_dir(device)) {
  500. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  501. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  502. acpi_device_dir(device) = NULL;
  503. }
  504. return 0;
  505. }
  506. /* --------------------------------------------------------------------------
  507. Driver Interface
  508. -------------------------------------------------------------------------- */
  509. static int acpi_ec_add(struct acpi_device *device)
  510. {
  511. int result = 0;
  512. acpi_status status = AE_OK;
  513. struct acpi_ec *ec = NULL;
  514. if (!device)
  515. return -EINVAL;
  516. ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  517. if (!ec)
  518. return -ENOMEM;
  519. ec->handle = device->handle;
  520. ec->uid = -1;
  521. mutex_init(&ec->lock);
  522. atomic_set(&ec->query_pending, 0);
  523. atomic_set(&ec->event_count, 1);
  524. if (acpi_ec_mode == EC_INTR) {
  525. init_waitqueue_head(&ec->wait);
  526. }
  527. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  528. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  529. acpi_driver_data(device) = ec;
  530. /* Use the global lock for all EC transactions? */
  531. acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
  532. /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
  533. http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
  534. if (ec_ecdt) {
  535. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  536. ACPI_ADR_SPACE_EC,
  537. &acpi_ec_space_handler);
  538. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
  539. &acpi_ec_gpe_handler);
  540. kfree(ec_ecdt);
  541. }
  542. /* Get GPE bit assignment (EC events). */
  543. /* TODO: Add support for _GPE returning a package */
  544. status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
  545. if (ACPI_FAILURE(status)) {
  546. ACPI_EXCEPTION((AE_INFO, status,
  547. "Obtaining GPE bit assignment"));
  548. result = -ENODEV;
  549. goto end;
  550. }
  551. result = acpi_ec_add_fs(device);
  552. if (result)
  553. goto end;
  554. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
  555. acpi_device_name(device), acpi_device_bid(device),
  556. (u32) ec->gpe));
  557. if (!first_ec)
  558. first_ec = device;
  559. end:
  560. if (result)
  561. kfree(ec);
  562. return result;
  563. }
  564. static int acpi_ec_remove(struct acpi_device *device, int type)
  565. {
  566. struct acpi_ec *ec = NULL;
  567. if (!device)
  568. return -EINVAL;
  569. ec = acpi_driver_data(device);
  570. acpi_ec_remove_fs(device);
  571. kfree(ec);
  572. return 0;
  573. }
  574. static acpi_status
  575. acpi_ec_io_ports(struct acpi_resource *resource, void *context)
  576. {
  577. struct acpi_ec *ec = (struct acpi_ec *)context;
  578. if (resource->type != ACPI_RESOURCE_TYPE_IO) {
  579. return AE_OK;
  580. }
  581. /*
  582. * The first address region returned is the data port, and
  583. * the second address region returned is the status/command
  584. * port.
  585. */
  586. if (ec->data_addr == 0) {
  587. ec->data_addr = resource->data.io.minimum;
  588. } else if (ec->command_addr == 0) {
  589. ec->command_addr = resource->data.io.minimum;
  590. } else {
  591. return AE_CTRL_TERMINATE;
  592. }
  593. return AE_OK;
  594. }
  595. static int acpi_ec_start(struct acpi_device *device)
  596. {
  597. acpi_status status = AE_OK;
  598. struct acpi_ec *ec = NULL;
  599. if (!device)
  600. return -EINVAL;
  601. ec = acpi_driver_data(device);
  602. if (!ec)
  603. return -EINVAL;
  604. /*
  605. * Get I/O port addresses. Convert to GAS format.
  606. */
  607. status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
  608. acpi_ec_io_ports, ec);
  609. if (ACPI_FAILURE(status) || ec->command_addr == 0) {
  610. ACPI_EXCEPTION((AE_INFO, status,
  611. "Error getting I/O port addresses"));
  612. return -ENODEV;
  613. }
  614. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
  615. ec->gpe, ec->command_addr, ec->data_addr));
  616. /*
  617. * Install GPE handler
  618. */
  619. status = acpi_install_gpe_handler(NULL, ec->gpe,
  620. ACPI_GPE_EDGE_TRIGGERED,
  621. &acpi_ec_gpe_handler, ec);
  622. if (ACPI_FAILURE(status)) {
  623. return -ENODEV;
  624. }
  625. acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
  626. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  627. status = acpi_install_address_space_handler(ec->handle,
  628. ACPI_ADR_SPACE_EC,
  629. &acpi_ec_space_handler,
  630. &acpi_ec_space_setup, ec);
  631. if (ACPI_FAILURE(status)) {
  632. acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  633. return -ENODEV;
  634. }
  635. return AE_OK;
  636. }
  637. static int acpi_ec_stop(struct acpi_device *device, int type)
  638. {
  639. acpi_status status = AE_OK;
  640. struct acpi_ec *ec = NULL;
  641. if (!device)
  642. return -EINVAL;
  643. ec = acpi_driver_data(device);
  644. status = acpi_remove_address_space_handler(ec->handle,
  645. ACPI_ADR_SPACE_EC,
  646. &acpi_ec_space_handler);
  647. if (ACPI_FAILURE(status))
  648. return -ENODEV;
  649. status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
  650. if (ACPI_FAILURE(status))
  651. return -ENODEV;
  652. return 0;
  653. }
  654. static int __init acpi_ec_get_real_ecdt(void)
  655. {
  656. acpi_status status;
  657. struct acpi_table_ecdt *ecdt_ptr;
  658. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  659. (struct acpi_table_header **)&ecdt_ptr);
  660. if (ACPI_FAILURE(status))
  661. return -ENODEV;
  662. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
  663. /*
  664. * Generate a temporary ec context to use until the namespace is scanned
  665. */
  666. ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  667. if (!ec_ecdt)
  668. return -ENOMEM;
  669. mutex_init(&ec_ecdt->lock);
  670. atomic_set(&ec_ecdt->event_count, 1);
  671. if (acpi_ec_mode == EC_INTR) {
  672. init_waitqueue_head(&ec_ecdt->wait);
  673. }
  674. ec_ecdt->command_addr = ecdt_ptr->control.address;
  675. ec_ecdt->data_addr = ecdt_ptr->data.address;
  676. ec_ecdt->gpe = ecdt_ptr->gpe;
  677. ec_ecdt->uid = ecdt_ptr->uid;
  678. status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
  679. if (ACPI_FAILURE(status)) {
  680. goto error;
  681. }
  682. return 0;
  683. error:
  684. ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
  685. kfree(ec_ecdt);
  686. ec_ecdt = NULL;
  687. return -ENODEV;
  688. }
  689. int __init acpi_ec_ecdt_probe(void)
  690. {
  691. acpi_status status;
  692. int ret;
  693. ret = acpi_ec_get_real_ecdt();
  694. if (ret)
  695. return 0;
  696. /*
  697. * Install GPE handler
  698. */
  699. status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
  700. ACPI_GPE_EDGE_TRIGGERED,
  701. &acpi_ec_gpe_handler, ec_ecdt);
  702. if (ACPI_FAILURE(status)) {
  703. goto error;
  704. }
  705. acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
  706. acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
  707. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  708. ACPI_ADR_SPACE_EC,
  709. &acpi_ec_space_handler,
  710. &acpi_ec_space_setup,
  711. ec_ecdt);
  712. if (ACPI_FAILURE(status)) {
  713. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
  714. &acpi_ec_gpe_handler);
  715. goto error;
  716. }
  717. return 0;
  718. error:
  719. ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
  720. kfree(ec_ecdt);
  721. ec_ecdt = NULL;
  722. return -ENODEV;
  723. }
  724. static int __init acpi_ec_init(void)
  725. {
  726. int result = 0;
  727. if (acpi_disabled)
  728. return 0;
  729. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  730. if (!acpi_ec_dir)
  731. return -ENODEV;
  732. /* Now register the driver for the EC */
  733. result = acpi_bus_register_driver(&acpi_ec_driver);
  734. if (result < 0) {
  735. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  736. return -ENODEV;
  737. }
  738. return result;
  739. }
  740. subsys_initcall(acpi_ec_init);
  741. /* EC driver currently not unloadable */
  742. #if 0
  743. static void __exit acpi_ec_exit(void)
  744. {
  745. acpi_bus_unregister_driver(&acpi_ec_driver);
  746. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  747. return;
  748. }
  749. #endif /* 0 */
  750. static int __init acpi_ec_set_intr_mode(char *str)
  751. {
  752. int intr;
  753. if (!get_option(&str, &intr))
  754. return 0;
  755. if (intr) {
  756. acpi_ec_mode = EC_INTR;
  757. } else {
  758. acpi_ec_mode = EC_POLL;
  759. }
  760. acpi_ec_driver.ops.add = acpi_ec_add;
  761. printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
  762. return 1;
  763. }
  764. __setup("ec_intr=", acpi_ec_set_intr_mode);