ec.c 27 KB

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