ec.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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("acpi_ec")
  40. #define ACPI_EC_COMPONENT 0x00100000
  41. #define ACPI_EC_CLASS "embedded_controller"
  42. #define ACPI_EC_HID "PNP0C09"
  43. #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
  44. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  45. #define ACPI_EC_FILE_INFO "info"
  46. /* EC status register */
  47. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  48. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  49. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  50. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  51. /* EC commands */
  52. #define ACPI_EC_COMMAND_READ 0x80
  53. #define ACPI_EC_COMMAND_WRITE 0x81
  54. #define ACPI_EC_BURST_ENABLE 0x82
  55. #define ACPI_EC_BURST_DISABLE 0x83
  56. #define ACPI_EC_COMMAND_QUERY 0x84
  57. /* EC events */
  58. enum {
  59. ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
  60. ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
  61. };
  62. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  63. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  64. #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
  65. #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 100ms max. during EC ops */
  66. enum {
  67. EC_INTR = 1, /* Output buffer full */
  68. EC_POLL, /* Input buffer empty */
  69. };
  70. static int acpi_ec_remove(struct acpi_device *device, int type);
  71. static int acpi_ec_start(struct acpi_device *device);
  72. static int acpi_ec_stop(struct acpi_device *device, int type);
  73. static int acpi_ec_add(struct acpi_device *device);
  74. static struct acpi_driver acpi_ec_driver = {
  75. .name = ACPI_EC_DRIVER_NAME,
  76. .class = ACPI_EC_CLASS,
  77. .ids = ACPI_EC_HID,
  78. .ops = {
  79. .add = acpi_ec_add,
  80. .remove = acpi_ec_remove,
  81. .start = acpi_ec_start,
  82. .stop = acpi_ec_stop,
  83. },
  84. };
  85. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  86. struct acpi_ec {
  87. acpi_handle handle;
  88. unsigned long uid;
  89. unsigned long gpe_bit;
  90. unsigned long command_addr;
  91. unsigned long data_addr;
  92. unsigned long global_lock;
  93. struct semaphore sem;
  94. unsigned int expect_event;
  95. atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
  96. wait_queue_head_t wait;
  97. } *ec_ecdt;
  98. /* External interfaces use first EC only, so remember */
  99. static struct acpi_device *first_ec;
  100. static int acpi_ec_mode = EC_INTR;
  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 int acpi_ec_check_status(struct acpi_ec *ec, u8 event)
  121. {
  122. u8 status = acpi_ec_read_status(ec);
  123. switch (event) {
  124. case ACPI_EC_EVENT_OBF_1:
  125. if (status & ACPI_EC_FLAG_OBF)
  126. return 1;
  127. break;
  128. case ACPI_EC_EVENT_IBF_0:
  129. if (!(status & ACPI_EC_FLAG_IBF))
  130. return 1;
  131. break;
  132. default:
  133. break;
  134. }
  135. return 0;
  136. }
  137. static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
  138. {
  139. int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
  140. long time_left;
  141. ec->expect_event = event;
  142. if (acpi_ec_check_status(ec, event)) {
  143. ec->expect_event = 0;
  144. return 0;
  145. }
  146. do {
  147. if (acpi_ec_mode == EC_POLL) {
  148. udelay(ACPI_EC_UDELAY);
  149. } else {
  150. time_left = wait_event_timeout(ec->wait,
  151. !ec->expect_event,
  152. msecs_to_jiffies(ACPI_EC_DELAY));
  153. if (time_left > 0) {
  154. ec->expect_event = 0;
  155. return 0;
  156. }
  157. }
  158. if (acpi_ec_check_status(ec, event)) {
  159. ec->expect_event = 0;
  160. return 0;
  161. }
  162. } while (--i > 0);
  163. ec->expect_event = 0;
  164. return -ETIME;
  165. }
  166. #ifdef ACPI_FUTURE_USAGE
  167. /*
  168. * Note: samsung nv5000 doesn't work with ec burst mode.
  169. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  170. */
  171. int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
  172. {
  173. u8 tmp = 0;
  174. u8 status = 0;
  175. status = acpi_ec_read_status(ec);
  176. if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
  177. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  178. if (status)
  179. goto end;
  180. acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
  181. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
  182. tmp = acpi_ec_read_data(ec);
  183. if (tmp != 0x90) { /* Burst ACK byte */
  184. return -EINVAL;
  185. }
  186. }
  187. atomic_set(&ec->leaving_burst, 0);
  188. return 0;
  189. end:
  190. ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
  191. return -1;
  192. }
  193. int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
  194. {
  195. u8 status = 0;
  196. status = acpi_ec_read_status(ec);
  197. if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
  198. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  199. if(status)
  200. goto end;
  201. acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
  202. acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  203. }
  204. atomic_set(&ec->leaving_burst, 1);
  205. return 0;
  206. end:
  207. ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
  208. return -1;
  209. }
  210. #endif /* ACPI_FUTURE_USAGE */
  211. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
  212. const u8 *wdata, unsigned wdata_len,
  213. u8 *rdata, unsigned rdata_len)
  214. {
  215. int result;
  216. acpi_ec_write_cmd(ec, command);
  217. for (; wdata_len > 0; wdata_len --) {
  218. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  219. if (result)
  220. return result;
  221. acpi_ec_write_data(ec, *(wdata++));
  222. }
  223. if (!rdata_len) {
  224. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  225. if (result)
  226. return result;
  227. }
  228. for (; rdata_len > 0; rdata_len --) {
  229. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
  230. if (result)
  231. return result;
  232. *(rdata++) = acpi_ec_read_data(ec);
  233. }
  234. return 0;
  235. }
  236. static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
  237. const u8 *wdata, unsigned wdata_len,
  238. u8 *rdata, unsigned rdata_len)
  239. {
  240. int status;
  241. u32 glk;
  242. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  243. return -EINVAL;
  244. if (rdata)
  245. memset(rdata, 0, rdata_len);
  246. if (ec->global_lock) {
  247. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  248. if (ACPI_FAILURE(status))
  249. return -ENODEV;
  250. }
  251. down(&ec->sem);
  252. /* Make sure GPE is enabled before doing transaction */
  253. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  254. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  255. if (status) {
  256. printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
  257. goto end;
  258. }
  259. status = acpi_ec_transaction_unlocked(ec, command,
  260. wdata, wdata_len,
  261. rdata, rdata_len);
  262. end:
  263. up(&ec->sem);
  264. if (ec->global_lock)
  265. acpi_release_global_lock(glk);
  266. return status;
  267. }
  268. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
  269. {
  270. int result;
  271. u8 d;
  272. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
  273. &address, 1, &d, 1);
  274. *data = d;
  275. return result;
  276. }
  277. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  278. {
  279. u8 wdata[2] = { address, data };
  280. return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
  281. wdata, 2, NULL, 0);
  282. }
  283. /*
  284. * Externally callable EC access functions. For now, assume 1 EC only
  285. */
  286. int ec_read(u8 addr, u8 *val)
  287. {
  288. struct acpi_ec *ec;
  289. int err;
  290. u8 temp_data;
  291. if (!first_ec)
  292. return -ENODEV;
  293. ec = acpi_driver_data(first_ec);
  294. err = acpi_ec_read(ec, addr, &temp_data);
  295. if (!err) {
  296. *val = temp_data;
  297. return 0;
  298. } else
  299. return err;
  300. }
  301. EXPORT_SYMBOL(ec_read);
  302. int ec_write(u8 addr, u8 val)
  303. {
  304. struct acpi_ec *ec;
  305. int err;
  306. if (!first_ec)
  307. return -ENODEV;
  308. ec = acpi_driver_data(first_ec);
  309. err = acpi_ec_write(ec, addr, val);
  310. return err;
  311. }
  312. EXPORT_SYMBOL(ec_write);
  313. extern int ec_transaction(u8 command,
  314. const u8 *wdata, unsigned wdata_len,
  315. u8 *rdata, unsigned rdata_len)
  316. {
  317. struct acpi_ec *ec;
  318. if (!first_ec)
  319. return -ENODEV;
  320. ec = acpi_driver_data(first_ec);
  321. return acpi_ec_transaction(ec, command, wdata,
  322. wdata_len, rdata, rdata_len);
  323. }
  324. EXPORT_SYMBOL(ec_transaction);
  325. static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
  326. {
  327. int result;
  328. u8 d;
  329. if (!ec || !data)
  330. return -EINVAL;
  331. /*
  332. * Query the EC to find out which _Qxx method we need to evaluate.
  333. * Note that successful completion of the query causes the ACPI_EC_SCI
  334. * bit to be cleared (and thus clearing the interrupt source).
  335. */
  336. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
  337. if (result)
  338. return result;
  339. if (!d)
  340. return -ENODATA;
  341. *data = d;
  342. return 0;
  343. }
  344. /* --------------------------------------------------------------------------
  345. Event Management
  346. -------------------------------------------------------------------------- */
  347. struct acpi_ec_query_data {
  348. acpi_handle handle;
  349. u8 data;
  350. };
  351. static void acpi_ec_gpe_query(void *ec_cxt)
  352. {
  353. struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
  354. u8 value = 0;
  355. static char object_name[8];
  356. if (!ec)
  357. goto end;
  358. value = acpi_ec_read_status(ec);
  359. if (!(value & ACPI_EC_FLAG_SCI))
  360. goto end;
  361. if (acpi_ec_query(ec, &value))
  362. goto end;
  363. snprintf(object_name, 8, "_Q%2.2X", value);
  364. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
  365. acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
  366. end:
  367. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  368. }
  369. static u32 acpi_ec_gpe_handler(void *data)
  370. {
  371. acpi_status status = AE_OK;
  372. u8 value;
  373. struct acpi_ec *ec = (struct acpi_ec *)data;
  374. acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
  375. if (acpi_ec_mode == EC_INTR) {
  376. if (acpi_ec_check_status(ec, ec->expect_event)) {
  377. ec->expect_event = 0;
  378. wake_up(&ec->wait);
  379. }
  380. }
  381. value = acpi_ec_read_status(ec);
  382. if (value & ACPI_EC_FLAG_SCI) {
  383. status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
  384. return status == AE_OK ?
  385. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  386. }
  387. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
  388. return status == AE_OK ?
  389. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  390. }
  391. /* --------------------------------------------------------------------------
  392. Address Space Management
  393. -------------------------------------------------------------------------- */
  394. static acpi_status
  395. acpi_ec_space_setup(acpi_handle region_handle,
  396. u32 function, void *handler_context, void **return_context)
  397. {
  398. /*
  399. * The EC object is in the handler context and is needed
  400. * when calling the acpi_ec_space_handler.
  401. */
  402. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  403. handler_context : NULL;
  404. return AE_OK;
  405. }
  406. static acpi_status
  407. acpi_ec_space_handler(u32 function,
  408. acpi_physical_address address,
  409. u32 bit_width,
  410. acpi_integer * value,
  411. void *handler_context, void *region_context)
  412. {
  413. int result = 0;
  414. struct acpi_ec *ec = NULL;
  415. u64 temp = *value;
  416. acpi_integer f_v = 0;
  417. int i = 0;
  418. if ((address > 0xFF) || !value || !handler_context)
  419. return AE_BAD_PARAMETER;
  420. if (bit_width != 8 && acpi_strict) {
  421. return AE_BAD_PARAMETER;
  422. }
  423. ec = (struct acpi_ec *)handler_context;
  424. next_byte:
  425. switch (function) {
  426. case ACPI_READ:
  427. temp = 0;
  428. result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
  429. break;
  430. case ACPI_WRITE:
  431. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  432. break;
  433. default:
  434. result = -EINVAL;
  435. goto out;
  436. break;
  437. }
  438. bit_width -= 8;
  439. if (bit_width) {
  440. if (function == ACPI_READ)
  441. f_v |= temp << 8 * i;
  442. if (function == ACPI_WRITE)
  443. temp >>= 8;
  444. i++;
  445. address++;
  446. goto next_byte;
  447. }
  448. if (function == ACPI_READ) {
  449. f_v |= temp << 8 * i;
  450. *value = f_v;
  451. }
  452. out:
  453. switch (result) {
  454. case -EINVAL:
  455. return AE_BAD_PARAMETER;
  456. break;
  457. case -ENODEV:
  458. return AE_NOT_FOUND;
  459. break;
  460. case -ETIME:
  461. return AE_TIME;
  462. break;
  463. default:
  464. return AE_OK;
  465. }
  466. }
  467. /* --------------------------------------------------------------------------
  468. FS Interface (/proc)
  469. -------------------------------------------------------------------------- */
  470. static struct proc_dir_entry *acpi_ec_dir;
  471. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  472. {
  473. struct acpi_ec *ec = (struct acpi_ec *)seq->private;
  474. if (!ec)
  475. goto end;
  476. seq_printf(seq, "gpe bit: 0x%02x\n",
  477. (u32) ec->gpe_bit);
  478. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  479. (u32) ec->command_addr,
  480. (u32) ec->data_addr);
  481. seq_printf(seq, "use global lock: %s\n",
  482. ec->global_lock ? "yes" : "no");
  483. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  484. end:
  485. return 0;
  486. }
  487. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  488. {
  489. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  490. }
  491. static struct file_operations acpi_ec_info_ops = {
  492. .open = acpi_ec_info_open_fs,
  493. .read = seq_read,
  494. .llseek = seq_lseek,
  495. .release = single_release,
  496. .owner = THIS_MODULE,
  497. };
  498. static int acpi_ec_add_fs(struct acpi_device *device)
  499. {
  500. struct proc_dir_entry *entry = NULL;
  501. if (!acpi_device_dir(device)) {
  502. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  503. acpi_ec_dir);
  504. if (!acpi_device_dir(device))
  505. return -ENODEV;
  506. }
  507. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  508. acpi_device_dir(device));
  509. if (!entry)
  510. return -ENODEV;
  511. else {
  512. entry->proc_fops = &acpi_ec_info_ops;
  513. entry->data = acpi_driver_data(device);
  514. entry->owner = THIS_MODULE;
  515. }
  516. return 0;
  517. }
  518. static int acpi_ec_remove_fs(struct acpi_device *device)
  519. {
  520. if (acpi_device_dir(device)) {
  521. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  522. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  523. acpi_device_dir(device) = NULL;
  524. }
  525. return 0;
  526. }
  527. /* --------------------------------------------------------------------------
  528. Driver Interface
  529. -------------------------------------------------------------------------- */
  530. static int acpi_ec_add(struct acpi_device *device)
  531. {
  532. int result = 0;
  533. acpi_status status = AE_OK;
  534. struct acpi_ec *ec = NULL;
  535. if (!device)
  536. return -EINVAL;
  537. ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  538. if (!ec)
  539. return -ENOMEM;
  540. memset(ec, 0, sizeof(struct acpi_ec));
  541. ec->handle = device->handle;
  542. ec->uid = -1;
  543. init_MUTEX(&ec->sem);
  544. if (acpi_ec_mode == EC_INTR) {
  545. atomic_set(&ec->leaving_burst, 1);
  546. init_waitqueue_head(&ec->wait);
  547. }
  548. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  549. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  550. acpi_driver_data(device) = ec;
  551. /* Use the global lock for all EC transactions? */
  552. acpi_evaluate_integer(ec->handle, "_GLK", NULL,
  553. &ec->global_lock);
  554. /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
  555. http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
  556. if (ec_ecdt) {
  557. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  558. ACPI_ADR_SPACE_EC,
  559. &acpi_ec_space_handler);
  560. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
  561. &acpi_ec_gpe_handler);
  562. kfree(ec_ecdt);
  563. }
  564. /* Get GPE bit assignment (EC events). */
  565. /* TODO: Add support for _GPE returning a package */
  566. status =
  567. acpi_evaluate_integer(ec->handle, "_GPE", NULL,
  568. &ec->gpe_bit);
  569. if (ACPI_FAILURE(status)) {
  570. ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
  571. result = -ENODEV;
  572. goto end;
  573. }
  574. result = acpi_ec_add_fs(device);
  575. if (result)
  576. goto end;
  577. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
  578. acpi_device_name(device), acpi_device_bid(device),
  579. (u32) ec->gpe_bit));
  580. if (!first_ec)
  581. first_ec = device;
  582. end:
  583. if (result)
  584. kfree(ec);
  585. return result;
  586. }
  587. static int acpi_ec_remove(struct acpi_device *device, int type)
  588. {
  589. struct acpi_ec *ec = NULL;
  590. if (!device)
  591. return -EINVAL;
  592. ec = acpi_driver_data(device);
  593. acpi_ec_remove_fs(device);
  594. kfree(ec);
  595. return 0;
  596. }
  597. static acpi_status
  598. acpi_ec_io_ports(struct acpi_resource *resource, void *context)
  599. {
  600. struct acpi_ec *ec = (struct acpi_ec *)context;
  601. if (resource->type != ACPI_RESOURCE_TYPE_IO) {
  602. return AE_OK;
  603. }
  604. /*
  605. * The first address region returned is the data port, and
  606. * the second address region returned is the status/command
  607. * port.
  608. */
  609. if (ec->data_addr == 0) {
  610. ec->data_addr = resource->data.io.minimum;
  611. } else if (ec->command_addr == 0) {
  612. ec->command_addr = resource->data.io.minimum;
  613. } else {
  614. return AE_CTRL_TERMINATE;
  615. }
  616. return AE_OK;
  617. }
  618. static int acpi_ec_start(struct acpi_device *device)
  619. {
  620. acpi_status status = AE_OK;
  621. struct acpi_ec *ec = NULL;
  622. if (!device)
  623. return -EINVAL;
  624. ec = acpi_driver_data(device);
  625. if (!ec)
  626. return -EINVAL;
  627. /*
  628. * Get I/O port addresses. Convert to GAS format.
  629. */
  630. status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
  631. acpi_ec_io_ports, ec);
  632. if (ACPI_FAILURE(status) || ec->command_addr == 0) {
  633. ACPI_EXCEPTION((AE_INFO, status,
  634. "Error getting I/O port addresses"));
  635. return -ENODEV;
  636. }
  637. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
  638. ec->gpe_bit, ec->command_addr, ec->data_addr));
  639. /*
  640. * Install GPE handler
  641. */
  642. status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
  643. ACPI_GPE_EDGE_TRIGGERED,
  644. &acpi_ec_gpe_handler, ec);
  645. if (ACPI_FAILURE(status)) {
  646. return -ENODEV;
  647. }
  648. acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  649. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  650. status = acpi_install_address_space_handler(ec->handle,
  651. ACPI_ADR_SPACE_EC,
  652. &acpi_ec_space_handler,
  653. &acpi_ec_space_setup, ec);
  654. if (ACPI_FAILURE(status)) {
  655. acpi_remove_gpe_handler(NULL, ec->gpe_bit,
  656. &acpi_ec_gpe_handler);
  657. return -ENODEV;
  658. }
  659. return AE_OK;
  660. }
  661. static int acpi_ec_stop(struct acpi_device *device, int type)
  662. {
  663. acpi_status status = AE_OK;
  664. struct acpi_ec *ec = NULL;
  665. if (!device)
  666. return -EINVAL;
  667. ec = acpi_driver_data(device);
  668. status = acpi_remove_address_space_handler(ec->handle,
  669. ACPI_ADR_SPACE_EC,
  670. &acpi_ec_space_handler);
  671. if (ACPI_FAILURE(status))
  672. return -ENODEV;
  673. status =
  674. acpi_remove_gpe_handler(NULL, ec->gpe_bit,
  675. &acpi_ec_gpe_handler);
  676. if (ACPI_FAILURE(status))
  677. return -ENODEV;
  678. return 0;
  679. }
  680. static acpi_status __init
  681. acpi_fake_ecdt_callback(acpi_handle handle,
  682. u32 Level, void *context, void **retval)
  683. {
  684. acpi_status status;
  685. init_MUTEX(&ec_ecdt->sem);
  686. if (acpi_ec_mode == EC_INTR) {
  687. init_waitqueue_head(&ec_ecdt->wait);
  688. }
  689. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  690. acpi_ec_io_ports, ec_ecdt);
  691. if (ACPI_FAILURE(status))
  692. return status;
  693. ec_ecdt->uid = -1;
  694. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
  695. status =
  696. acpi_evaluate_integer(handle, "_GPE", NULL,
  697. &ec_ecdt->gpe_bit);
  698. if (ACPI_FAILURE(status))
  699. return status;
  700. ec_ecdt->global_lock = TRUE;
  701. ec_ecdt->handle = handle;
  702. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
  703. ec_ecdt->gpe_bit, ec_ecdt->command_addr, ec_ecdt->data_addr));
  704. return AE_CTRL_TERMINATE;
  705. }
  706. /*
  707. * Some BIOS (such as some from Gateway laptops) access EC region very early
  708. * such as in BAT0._INI or EC._INI before an EC device is found and
  709. * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
  710. * required, but if EC regison is accessed early, it is required.
  711. * The routine tries to workaround the BIOS bug by pre-scan EC device
  712. * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
  713. * op region (since _REG isn't invoked yet). The assumption is true for
  714. * all systems found.
  715. */
  716. static int __init acpi_ec_fake_ecdt(void)
  717. {
  718. acpi_status status;
  719. int ret = 0;
  720. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
  721. ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  722. if (!ec_ecdt) {
  723. ret = -ENOMEM;
  724. goto error;
  725. }
  726. memset(ec_ecdt, 0, sizeof(struct acpi_ec));
  727. status = acpi_get_devices(ACPI_EC_HID,
  728. acpi_fake_ecdt_callback, NULL, NULL);
  729. if (ACPI_FAILURE(status)) {
  730. kfree(ec_ecdt);
  731. ec_ecdt = NULL;
  732. ret = -ENODEV;
  733. ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
  734. goto error;
  735. }
  736. return 0;
  737. error:
  738. return ret;
  739. }
  740. static int __init acpi_ec_get_real_ecdt(void)
  741. {
  742. acpi_status status;
  743. struct acpi_table_ecdt *ecdt_ptr;
  744. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  745. (struct acpi_table_header **)
  746. &ecdt_ptr);
  747. if (ACPI_FAILURE(status))
  748. return -ENODEV;
  749. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
  750. /*
  751. * Generate a temporary ec context to use until the namespace is scanned
  752. */
  753. ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  754. if (!ec_ecdt)
  755. return -ENOMEM;
  756. memset(ec_ecdt, 0, sizeof(struct acpi_ec));
  757. init_MUTEX(&ec_ecdt->sem);
  758. if (acpi_ec_mode == EC_INTR) {
  759. init_waitqueue_head(&ec_ecdt->wait);
  760. }
  761. ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
  762. ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
  763. ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
  764. /* use the GL just to be safe */
  765. ec_ecdt->global_lock = TRUE;
  766. ec_ecdt->uid = ecdt_ptr->uid;
  767. status =
  768. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
  769. if (ACPI_FAILURE(status)) {
  770. goto error;
  771. }
  772. return 0;
  773. error:
  774. ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
  775. kfree(ec_ecdt);
  776. ec_ecdt = NULL;
  777. return -ENODEV;
  778. }
  779. static int __initdata acpi_fake_ecdt_enabled;
  780. int __init acpi_ec_ecdt_probe(void)
  781. {
  782. acpi_status status;
  783. int ret;
  784. ret = acpi_ec_get_real_ecdt();
  785. /* Try to make a fake ECDT */
  786. if (ret && acpi_fake_ecdt_enabled) {
  787. ret = acpi_ec_fake_ecdt();
  788. }
  789. if (ret)
  790. return 0;
  791. /*
  792. * Install GPE handler
  793. */
  794. status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
  795. ACPI_GPE_EDGE_TRIGGERED,
  796. &acpi_ec_gpe_handler, ec_ecdt);
  797. if (ACPI_FAILURE(status)) {
  798. goto error;
  799. }
  800. acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  801. acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
  802. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  803. ACPI_ADR_SPACE_EC,
  804. &acpi_ec_space_handler,
  805. &acpi_ec_space_setup,
  806. ec_ecdt);
  807. if (ACPI_FAILURE(status)) {
  808. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
  809. &acpi_ec_gpe_handler);
  810. goto error;
  811. }
  812. return 0;
  813. error:
  814. ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
  815. kfree(ec_ecdt);
  816. ec_ecdt = NULL;
  817. return -ENODEV;
  818. }
  819. static int __init acpi_ec_init(void)
  820. {
  821. int result = 0;
  822. if (acpi_disabled)
  823. return 0;
  824. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  825. if (!acpi_ec_dir)
  826. return -ENODEV;
  827. /* Now register the driver for the EC */
  828. result = acpi_bus_register_driver(&acpi_ec_driver);
  829. if (result < 0) {
  830. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  831. return -ENODEV;
  832. }
  833. return result;
  834. }
  835. subsys_initcall(acpi_ec_init);
  836. /* EC driver currently not unloadable */
  837. #if 0
  838. static void __exit acpi_ec_exit(void)
  839. {
  840. acpi_bus_unregister_driver(&acpi_ec_driver);
  841. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  842. return;
  843. }
  844. #endif /* 0 */
  845. static int __init acpi_fake_ecdt_setup(char *str)
  846. {
  847. acpi_fake_ecdt_enabled = 1;
  848. return 1;
  849. }
  850. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
  851. static int __init acpi_ec_set_intr_mode(char *str)
  852. {
  853. int intr;
  854. if (!get_option(&str, &intr))
  855. return 0;
  856. if (intr) {
  857. acpi_ec_mode = EC_INTR;
  858. } else {
  859. acpi_ec_mode = EC_POLL;
  860. }
  861. acpi_ec_driver.ops.add = acpi_ec_add;
  862. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
  863. return 1;
  864. }
  865. __setup("ec_intr=", acpi_ec_set_intr_mode);