ec.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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_UDELAY 100 /* Poll @ 100us increments */
  55. #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
  56. #define ACPI_EC_COMMAND_READ 0x80
  57. #define ACPI_EC_COMMAND_WRITE 0x81
  58. #define ACPI_EC_BURST_ENABLE 0x82
  59. #define ACPI_EC_BURST_DISABLE 0x83
  60. #define ACPI_EC_COMMAND_QUERY 0x84
  61. #define EC_POLL 0xFF
  62. #define EC_INTR 0x00
  63. static int acpi_ec_remove(struct acpi_device *device, int type);
  64. static int acpi_ec_start(struct acpi_device *device);
  65. static int acpi_ec_stop(struct acpi_device *device, int type);
  66. static int acpi_ec_intr_add(struct acpi_device *device);
  67. static int acpi_ec_poll_add(struct acpi_device *device);
  68. static struct acpi_driver acpi_ec_driver = {
  69. .name = ACPI_EC_DRIVER_NAME,
  70. .class = ACPI_EC_CLASS,
  71. .ids = ACPI_EC_HID,
  72. .ops = {
  73. .add = acpi_ec_intr_add,
  74. .remove = acpi_ec_remove,
  75. .start = acpi_ec_start,
  76. .stop = acpi_ec_stop,
  77. },
  78. };
  79. union acpi_ec {
  80. struct {
  81. u32 mode;
  82. acpi_handle handle;
  83. unsigned long uid;
  84. unsigned long gpe_bit;
  85. struct acpi_generic_address status_addr;
  86. struct acpi_generic_address command_addr;
  87. struct acpi_generic_address data_addr;
  88. unsigned long global_lock;
  89. } common;
  90. struct {
  91. u32 mode;
  92. acpi_handle handle;
  93. unsigned long uid;
  94. unsigned long gpe_bit;
  95. struct acpi_generic_address status_addr;
  96. struct acpi_generic_address command_addr;
  97. struct acpi_generic_address data_addr;
  98. unsigned long global_lock;
  99. unsigned int expect_event;
  100. atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
  101. atomic_t pending_gpe;
  102. struct semaphore sem;
  103. wait_queue_head_t wait;
  104. } intr;
  105. struct {
  106. u32 mode;
  107. acpi_handle handle;
  108. unsigned long uid;
  109. unsigned long gpe_bit;
  110. struct acpi_generic_address status_addr;
  111. struct acpi_generic_address command_addr;
  112. struct acpi_generic_address data_addr;
  113. unsigned long global_lock;
  114. struct semaphore sem;
  115. } poll;
  116. };
  117. static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
  118. static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
  119. static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
  120. static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
  121. static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
  122. static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
  123. static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
  124. static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
  125. static void acpi_ec_gpe_poll_query(void *ec_cxt);
  126. static void acpi_ec_gpe_intr_query(void *ec_cxt);
  127. static u32 acpi_ec_gpe_poll_handler(void *data);
  128. static u32 acpi_ec_gpe_intr_handler(void *data);
  129. static acpi_status __init
  130. acpi_fake_ecdt_poll_callback(acpi_handle handle,
  131. u32 Level, void *context, void **retval);
  132. static acpi_status __init
  133. acpi_fake_ecdt_intr_callback(acpi_handle handle,
  134. u32 Level, void *context, void **retval);
  135. static int __init acpi_ec_poll_get_real_ecdt(void);
  136. static int __init acpi_ec_intr_get_real_ecdt(void);
  137. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  138. static union acpi_ec *ec_ecdt;
  139. /* External interfaces use first EC only, so remember */
  140. static struct acpi_device *first_ec;
  141. static int acpi_ec_poll_mode = EC_INTR;
  142. /* --------------------------------------------------------------------------
  143. Transaction Management
  144. -------------------------------------------------------------------------- */
  145. static u32 acpi_ec_read_status(union acpi_ec *ec)
  146. {
  147. u32 status = 0;
  148. acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
  149. return status;
  150. }
  151. static int acpi_ec_wait(union acpi_ec *ec, u8 event)
  152. {
  153. if (acpi_ec_poll_mode)
  154. return acpi_ec_poll_wait(ec, event);
  155. else
  156. return acpi_ec_intr_wait(ec, event);
  157. }
  158. static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
  159. {
  160. u32 acpi_ec_status = 0;
  161. u32 i = ACPI_EC_UDELAY_COUNT;
  162. if (!ec)
  163. return -EINVAL;
  164. /* Poll the EC status register waiting for the event to occur. */
  165. switch (event) {
  166. case ACPI_EC_EVENT_OBF:
  167. do {
  168. acpi_hw_low_level_read(8, &acpi_ec_status,
  169. &ec->common.status_addr);
  170. if (acpi_ec_status & ACPI_EC_FLAG_OBF)
  171. return 0;
  172. udelay(ACPI_EC_UDELAY);
  173. } while (--i > 0);
  174. break;
  175. case ACPI_EC_EVENT_IBE:
  176. do {
  177. acpi_hw_low_level_read(8, &acpi_ec_status,
  178. &ec->common.status_addr);
  179. if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
  180. return 0;
  181. udelay(ACPI_EC_UDELAY);
  182. } while (--i > 0);
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. return -ETIME;
  188. }
  189. static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
  190. {
  191. int result = 0;
  192. ACPI_FUNCTION_TRACE("acpi_ec_wait");
  193. ec->intr.expect_event = event;
  194. smp_mb();
  195. switch (event) {
  196. case ACPI_EC_EVENT_IBE:
  197. if (~acpi_ec_read_status(ec) & event) {
  198. ec->intr.expect_event = 0;
  199. return_VALUE(0);
  200. }
  201. break;
  202. default:
  203. break;
  204. }
  205. result = wait_event_timeout(ec->intr.wait,
  206. !ec->intr.expect_event,
  207. msecs_to_jiffies(ACPI_EC_DELAY));
  208. ec->intr.expect_event = 0;
  209. smp_mb();
  210. /*
  211. * Verify that the event in question has actually happened by
  212. * querying EC status. Do the check even if operation timed-out
  213. * to make sure that we did not miss interrupt.
  214. */
  215. switch (event) {
  216. case ACPI_EC_EVENT_OBF:
  217. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
  218. return_VALUE(0);
  219. break;
  220. case ACPI_EC_EVENT_IBE:
  221. if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
  222. return_VALUE(0);
  223. break;
  224. }
  225. return_VALUE(-ETIME);
  226. }
  227. #ifdef ACPI_FUTURE_USAGE
  228. /*
  229. * Note: samsung nv5000 doesn't work with ec burst mode.
  230. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  231. */
  232. int acpi_ec_enter_burst_mode(union acpi_ec *ec)
  233. {
  234. u32 tmp = 0;
  235. int status = 0;
  236. ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
  237. status = acpi_ec_read_status(ec);
  238. if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
  239. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  240. if (status)
  241. goto end;
  242. acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
  243. &ec->common.command_addr);
  244. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  245. acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
  246. if (tmp != 0x90) { /* Burst ACK byte */
  247. return_VALUE(-EINVAL);
  248. }
  249. }
  250. atomic_set(&ec->intr.leaving_burst, 0);
  251. return_VALUE(0);
  252. end:
  253. ACPI_EXCEPTION ((AE_INFO, status, "EC wait, burst mode");
  254. return_VALUE(-1);
  255. }
  256. int acpi_ec_leave_burst_mode(union acpi_ec *ec)
  257. {
  258. int status = 0;
  259. ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
  260. status = acpi_ec_read_status(ec);
  261. if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
  262. status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
  263. if(status)
  264. goto end;
  265. acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
  266. acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
  267. }
  268. atomic_set(&ec->intr.leaving_burst, 1);
  269. return_VALUE(0);
  270. end:
  271. ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode");
  272. return_VALUE(-1);
  273. }
  274. #endif /* ACPI_FUTURE_USAGE */
  275. static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
  276. {
  277. if (acpi_ec_poll_mode)
  278. return acpi_ec_poll_read(ec, address, data);
  279. else
  280. return acpi_ec_intr_read(ec, address, data);
  281. }
  282. static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
  283. {
  284. if (acpi_ec_poll_mode)
  285. return acpi_ec_poll_write(ec, address, data);
  286. else
  287. return acpi_ec_intr_write(ec, address, data);
  288. }
  289. static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
  290. {
  291. acpi_status status = AE_OK;
  292. int result = 0;
  293. u32 glk = 0;
  294. ACPI_FUNCTION_TRACE("acpi_ec_read");
  295. if (!ec || !data)
  296. return_VALUE(-EINVAL);
  297. *data = 0;
  298. if (ec->common.global_lock) {
  299. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  300. if (ACPI_FAILURE(status))
  301. return_VALUE(-ENODEV);
  302. }
  303. if (down_interruptible(&ec->poll.sem)) {
  304. result = -ERESTARTSYS;
  305. goto end_nosem;
  306. }
  307. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
  308. &ec->common.command_addr);
  309. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  310. if (result)
  311. goto end;
  312. acpi_hw_low_level_write(8, address, &ec->common.data_addr);
  313. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  314. if (result)
  315. goto end;
  316. acpi_hw_low_level_read(8, data, &ec->common.data_addr);
  317. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
  318. *data, address));
  319. end:
  320. up(&ec->poll.sem);
  321. end_nosem:
  322. if (ec->common.global_lock)
  323. acpi_release_global_lock(glk);
  324. return_VALUE(result);
  325. }
  326. static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
  327. {
  328. int result = 0;
  329. acpi_status status = AE_OK;
  330. u32 glk = 0;
  331. ACPI_FUNCTION_TRACE("acpi_ec_write");
  332. if (!ec)
  333. return_VALUE(-EINVAL);
  334. if (ec->common.global_lock) {
  335. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  336. if (ACPI_FAILURE(status))
  337. return_VALUE(-ENODEV);
  338. }
  339. if (down_interruptible(&ec->poll.sem)) {
  340. result = -ERESTARTSYS;
  341. goto end_nosem;
  342. }
  343. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
  344. &ec->common.command_addr);
  345. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  346. if (result)
  347. goto end;
  348. acpi_hw_low_level_write(8, address, &ec->common.data_addr);
  349. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  350. if (result)
  351. goto end;
  352. acpi_hw_low_level_write(8, data, &ec->common.data_addr);
  353. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  354. if (result)
  355. goto end;
  356. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
  357. data, address));
  358. end:
  359. up(&ec->poll.sem);
  360. end_nosem:
  361. if (ec->common.global_lock)
  362. acpi_release_global_lock(glk);
  363. return_VALUE(result);
  364. }
  365. static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
  366. {
  367. int status = 0;
  368. u32 glk;
  369. ACPI_FUNCTION_TRACE("acpi_ec_read");
  370. if (!ec || !data)
  371. return_VALUE(-EINVAL);
  372. *data = 0;
  373. if (ec->common.global_lock) {
  374. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  375. if (ACPI_FAILURE(status))
  376. return_VALUE(-ENODEV);
  377. }
  378. WARN_ON(in_interrupt());
  379. down(&ec->intr.sem);
  380. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  381. if (status) {
  382. printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
  383. goto end;
  384. }
  385. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
  386. &ec->common.command_addr);
  387. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  388. if (status) {
  389. printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
  390. }
  391. acpi_hw_low_level_write(8, address, &ec->common.data_addr);
  392. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  393. if (status) {
  394. printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
  395. goto end;
  396. }
  397. acpi_hw_low_level_read(8, data, &ec->common.data_addr);
  398. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
  399. *data, address));
  400. end:
  401. up(&ec->intr.sem);
  402. if (ec->common.global_lock)
  403. acpi_release_global_lock(glk);
  404. return_VALUE(status);
  405. }
  406. static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
  407. {
  408. int status = 0;
  409. u32 glk;
  410. ACPI_FUNCTION_TRACE("acpi_ec_write");
  411. if (!ec)
  412. return_VALUE(-EINVAL);
  413. if (ec->common.global_lock) {
  414. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  415. if (ACPI_FAILURE(status))
  416. return_VALUE(-ENODEV);
  417. }
  418. WARN_ON(in_interrupt());
  419. down(&ec->intr.sem);
  420. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  421. if (status) {
  422. printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
  423. }
  424. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
  425. &ec->common.command_addr);
  426. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  427. if (status) {
  428. printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
  429. }
  430. acpi_hw_low_level_write(8, address, &ec->common.data_addr);
  431. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  432. if (status) {
  433. printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
  434. }
  435. acpi_hw_low_level_write(8, data, &ec->common.data_addr);
  436. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
  437. data, address));
  438. up(&ec->intr.sem);
  439. if (ec->common.global_lock)
  440. acpi_release_global_lock(glk);
  441. return_VALUE(status);
  442. }
  443. /*
  444. * Externally callable EC access functions. For now, assume 1 EC only
  445. */
  446. int ec_read(u8 addr, u8 * val)
  447. {
  448. union acpi_ec *ec;
  449. int err;
  450. u32 temp_data;
  451. if (!first_ec)
  452. return -ENODEV;
  453. ec = acpi_driver_data(first_ec);
  454. err = acpi_ec_read(ec, addr, &temp_data);
  455. if (!err) {
  456. *val = temp_data;
  457. return 0;
  458. } else
  459. return err;
  460. }
  461. EXPORT_SYMBOL(ec_read);
  462. int ec_write(u8 addr, u8 val)
  463. {
  464. union acpi_ec *ec;
  465. int err;
  466. if (!first_ec)
  467. return -ENODEV;
  468. ec = acpi_driver_data(first_ec);
  469. err = acpi_ec_write(ec, addr, val);
  470. return err;
  471. }
  472. EXPORT_SYMBOL(ec_write);
  473. static int acpi_ec_query(union acpi_ec *ec, u32 * data)
  474. {
  475. if (acpi_ec_poll_mode)
  476. return acpi_ec_poll_query(ec, data);
  477. else
  478. return acpi_ec_intr_query(ec, data);
  479. }
  480. static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
  481. {
  482. int result = 0;
  483. acpi_status status = AE_OK;
  484. u32 glk = 0;
  485. ACPI_FUNCTION_TRACE("acpi_ec_query");
  486. if (!ec || !data)
  487. return_VALUE(-EINVAL);
  488. *data = 0;
  489. if (ec->common.global_lock) {
  490. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  491. if (ACPI_FAILURE(status))
  492. return_VALUE(-ENODEV);
  493. }
  494. /*
  495. * Query the EC to find out which _Qxx method we need to evaluate.
  496. * Note that successful completion of the query causes the ACPI_EC_SCI
  497. * bit to be cleared (and thus clearing the interrupt source).
  498. */
  499. if (down_interruptible(&ec->poll.sem)) {
  500. result = -ERESTARTSYS;
  501. goto end_nosem;
  502. }
  503. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
  504. &ec->common.command_addr);
  505. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  506. if (result)
  507. goto end;
  508. acpi_hw_low_level_read(8, data, &ec->common.data_addr);
  509. if (!*data)
  510. result = -ENODATA;
  511. end:
  512. up(&ec->poll.sem);
  513. end_nosem:
  514. if (ec->common.global_lock)
  515. acpi_release_global_lock(glk);
  516. return_VALUE(result);
  517. }
  518. static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
  519. {
  520. int status = 0;
  521. u32 glk;
  522. ACPI_FUNCTION_TRACE("acpi_ec_query");
  523. if (!ec || !data)
  524. return_VALUE(-EINVAL);
  525. *data = 0;
  526. if (ec->common.global_lock) {
  527. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  528. if (ACPI_FAILURE(status))
  529. return_VALUE(-ENODEV);
  530. }
  531. down(&ec->intr.sem);
  532. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  533. if (status) {
  534. printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
  535. goto end;
  536. }
  537. /*
  538. * Query the EC to find out which _Qxx method we need to evaluate.
  539. * Note that successful completion of the query causes the ACPI_EC_SCI
  540. * bit to be cleared (and thus clearing the interrupt source).
  541. */
  542. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
  543. &ec->common.command_addr);
  544. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  545. if (status) {
  546. printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
  547. goto end;
  548. }
  549. acpi_hw_low_level_read(8, data, &ec->common.data_addr);
  550. if (!*data)
  551. status = -ENODATA;
  552. end:
  553. up(&ec->intr.sem);
  554. if (ec->common.global_lock)
  555. acpi_release_global_lock(glk);
  556. return_VALUE(status);
  557. }
  558. /* --------------------------------------------------------------------------
  559. Event Management
  560. -------------------------------------------------------------------------- */
  561. union acpi_ec_query_data {
  562. acpi_handle handle;
  563. u8 data;
  564. };
  565. static void acpi_ec_gpe_query(void *ec_cxt)
  566. {
  567. if (acpi_ec_poll_mode)
  568. acpi_ec_gpe_poll_query(ec_cxt);
  569. else
  570. acpi_ec_gpe_intr_query(ec_cxt);
  571. }
  572. static void acpi_ec_gpe_poll_query(void *ec_cxt)
  573. {
  574. union acpi_ec *ec = (union acpi_ec *)ec_cxt;
  575. u32 value = 0;
  576. static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
  577. const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
  578. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  579. };
  580. ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
  581. if (!ec_cxt)
  582. goto end;
  583. if (down_interruptible (&ec->poll.sem)) {
  584. return_VOID;
  585. }
  586. acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
  587. up(&ec->poll.sem);
  588. /* TBD: Implement asynch events!
  589. * NOTE: All we care about are EC-SCI's. Other EC events are
  590. * handled via polling (yuck!). This is because some systems
  591. * treat EC-SCIs as level (versus EDGE!) triggered, preventing
  592. * a purely interrupt-driven approach (grumble, grumble).
  593. */
  594. if (!(value & ACPI_EC_FLAG_SCI))
  595. goto end;
  596. if (acpi_ec_query(ec, &value))
  597. goto end;
  598. object_name[2] = hex[((value >> 4) & 0x0F)];
  599. object_name[3] = hex[(value & 0x0F)];
  600. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  601. acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
  602. end:
  603. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  604. }
  605. static void acpi_ec_gpe_intr_query(void *ec_cxt)
  606. {
  607. union acpi_ec *ec = (union acpi_ec *)ec_cxt;
  608. u32 value;
  609. int result = -ENODATA;
  610. static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
  611. const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
  612. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  613. };
  614. ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
  615. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
  616. result = acpi_ec_query(ec, &value);
  617. if (result)
  618. goto end;
  619. object_name[2] = hex[((value >> 4) & 0x0F)];
  620. object_name[3] = hex[(value & 0x0F)];
  621. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  622. acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
  623. end:
  624. atomic_dec(&ec->intr.pending_gpe);
  625. return;
  626. }
  627. static u32 acpi_ec_gpe_handler(void *data)
  628. {
  629. if (acpi_ec_poll_mode)
  630. return acpi_ec_gpe_poll_handler(data);
  631. else
  632. return acpi_ec_gpe_intr_handler(data);
  633. }
  634. static u32 acpi_ec_gpe_poll_handler(void *data)
  635. {
  636. acpi_status status = AE_OK;
  637. union acpi_ec *ec = (union acpi_ec *)data;
  638. if (!ec)
  639. return ACPI_INTERRUPT_NOT_HANDLED;
  640. acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  641. status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
  642. if (status == AE_OK)
  643. return ACPI_INTERRUPT_HANDLED;
  644. else
  645. return ACPI_INTERRUPT_NOT_HANDLED;
  646. }
  647. static u32 acpi_ec_gpe_intr_handler(void *data)
  648. {
  649. acpi_status status = AE_OK;
  650. u32 value;
  651. union acpi_ec *ec = (union acpi_ec *)data;
  652. if (!ec)
  653. return ACPI_INTERRUPT_NOT_HANDLED;
  654. acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  655. value = acpi_ec_read_status(ec);
  656. switch (ec->intr.expect_event) {
  657. case ACPI_EC_EVENT_OBF:
  658. if (!(value & ACPI_EC_FLAG_OBF))
  659. break;
  660. case ACPI_EC_EVENT_IBE:
  661. if ((value & ACPI_EC_FLAG_IBF))
  662. break;
  663. ec->intr.expect_event = 0;
  664. wake_up(&ec->intr.wait);
  665. return ACPI_INTERRUPT_HANDLED;
  666. default:
  667. break;
  668. }
  669. if (value & ACPI_EC_FLAG_SCI) {
  670. atomic_add(1, &ec->intr.pending_gpe);
  671. status = acpi_os_execute(OSL_EC_BURST_HANDLER,
  672. acpi_ec_gpe_query, ec);
  673. return status == AE_OK ?
  674. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  675. }
  676. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  677. return status == AE_OK ?
  678. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  679. }
  680. /* --------------------------------------------------------------------------
  681. Address Space Management
  682. -------------------------------------------------------------------------- */
  683. static acpi_status
  684. acpi_ec_space_setup(acpi_handle region_handle,
  685. u32 function, void *handler_context, void **return_context)
  686. {
  687. /*
  688. * The EC object is in the handler context and is needed
  689. * when calling the acpi_ec_space_handler.
  690. */
  691. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  692. handler_context : NULL;
  693. return AE_OK;
  694. }
  695. static acpi_status
  696. acpi_ec_space_handler(u32 function,
  697. acpi_physical_address address,
  698. u32 bit_width,
  699. acpi_integer * value,
  700. void *handler_context, void *region_context)
  701. {
  702. int result = 0;
  703. union acpi_ec *ec = NULL;
  704. u64 temp = *value;
  705. acpi_integer f_v = 0;
  706. int i = 0;
  707. ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
  708. if ((address > 0xFF) || !value || !handler_context)
  709. return_VALUE(AE_BAD_PARAMETER);
  710. if (bit_width != 8 && acpi_strict) {
  711. printk(KERN_WARNING PREFIX
  712. "acpi_ec_space_handler: bit_width should be 8\n");
  713. return_VALUE(AE_BAD_PARAMETER);
  714. }
  715. ec = (union acpi_ec *)handler_context;
  716. next_byte:
  717. switch (function) {
  718. case ACPI_READ:
  719. temp = 0;
  720. result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
  721. break;
  722. case ACPI_WRITE:
  723. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  724. break;
  725. default:
  726. result = -EINVAL;
  727. goto out;
  728. break;
  729. }
  730. bit_width -= 8;
  731. if (bit_width) {
  732. if (function == ACPI_READ)
  733. f_v |= temp << 8 * i;
  734. if (function == ACPI_WRITE)
  735. temp >>= 8;
  736. i++;
  737. address++;
  738. goto next_byte;
  739. }
  740. if (function == ACPI_READ) {
  741. f_v |= temp << 8 * i;
  742. *value = f_v;
  743. }
  744. out:
  745. switch (result) {
  746. case -EINVAL:
  747. return_VALUE(AE_BAD_PARAMETER);
  748. break;
  749. case -ENODEV:
  750. return_VALUE(AE_NOT_FOUND);
  751. break;
  752. case -ETIME:
  753. return_VALUE(AE_TIME);
  754. break;
  755. default:
  756. return_VALUE(AE_OK);
  757. }
  758. }
  759. /* --------------------------------------------------------------------------
  760. FS Interface (/proc)
  761. -------------------------------------------------------------------------- */
  762. static struct proc_dir_entry *acpi_ec_dir;
  763. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  764. {
  765. union acpi_ec *ec = (union acpi_ec *)seq->private;
  766. ACPI_FUNCTION_TRACE("acpi_ec_read_info");
  767. if (!ec)
  768. goto end;
  769. seq_printf(seq, "gpe bit: 0x%02x\n",
  770. (u32) ec->common.gpe_bit);
  771. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  772. (u32) ec->common.status_addr.address,
  773. (u32) ec->common.data_addr.address);
  774. seq_printf(seq, "use global lock: %s\n",
  775. ec->common.global_lock ? "yes" : "no");
  776. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  777. end:
  778. return_VALUE(0);
  779. }
  780. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  781. {
  782. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  783. }
  784. static struct file_operations acpi_ec_info_ops = {
  785. .open = acpi_ec_info_open_fs,
  786. .read = seq_read,
  787. .llseek = seq_lseek,
  788. .release = single_release,
  789. .owner = THIS_MODULE,
  790. };
  791. static int acpi_ec_add_fs(struct acpi_device *device)
  792. {
  793. struct proc_dir_entry *entry = NULL;
  794. ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
  795. if (!acpi_device_dir(device)) {
  796. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  797. acpi_ec_dir);
  798. if (!acpi_device_dir(device))
  799. return_VALUE(-ENODEV);
  800. }
  801. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  802. acpi_device_dir(device));
  803. if (!entry)
  804. return_VALUE(-ENODEV);
  805. else {
  806. entry->proc_fops = &acpi_ec_info_ops;
  807. entry->data = acpi_driver_data(device);
  808. entry->owner = THIS_MODULE;
  809. }
  810. return_VALUE(0);
  811. }
  812. static int acpi_ec_remove_fs(struct acpi_device *device)
  813. {
  814. ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
  815. if (acpi_device_dir(device)) {
  816. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  817. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  818. acpi_device_dir(device) = NULL;
  819. }
  820. return_VALUE(0);
  821. }
  822. /* --------------------------------------------------------------------------
  823. Driver Interface
  824. -------------------------------------------------------------------------- */
  825. static int acpi_ec_poll_add(struct acpi_device *device)
  826. {
  827. int result = 0;
  828. acpi_status status = AE_OK;
  829. union acpi_ec *ec = NULL;
  830. ACPI_FUNCTION_TRACE("acpi_ec_add");
  831. if (!device)
  832. return_VALUE(-EINVAL);
  833. ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  834. if (!ec)
  835. return_VALUE(-ENOMEM);
  836. memset(ec, 0, sizeof(union acpi_ec));
  837. ec->common.handle = device->handle;
  838. ec->common.uid = -1;
  839. init_MUTEX(&ec->poll.sem);
  840. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  841. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  842. acpi_driver_data(device) = ec;
  843. /* Use the global lock for all EC transactions? */
  844. acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
  845. &ec->common.global_lock);
  846. /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
  847. http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
  848. if (ec_ecdt) {
  849. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  850. ACPI_ADR_SPACE_EC,
  851. &acpi_ec_space_handler);
  852. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  853. &acpi_ec_gpe_handler);
  854. kfree(ec_ecdt);
  855. }
  856. /* Get GPE bit assignment (EC events). */
  857. /* TODO: Add support for _GPE returning a package */
  858. status =
  859. acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
  860. &ec->common.gpe_bit);
  861. if (ACPI_FAILURE(status)) {
  862. ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit"));
  863. result = -ENODEV;
  864. goto end;
  865. }
  866. result = acpi_ec_add_fs(device);
  867. if (result)
  868. goto end;
  869. printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
  870. acpi_device_name(device), acpi_device_bid(device),
  871. (u32) ec->common.gpe_bit);
  872. if (!first_ec)
  873. first_ec = device;
  874. end:
  875. if (result)
  876. kfree(ec);
  877. return_VALUE(result);
  878. }
  879. static int acpi_ec_intr_add(struct acpi_device *device)
  880. {
  881. int result = 0;
  882. acpi_status status = AE_OK;
  883. union acpi_ec *ec = NULL;
  884. ACPI_FUNCTION_TRACE("acpi_ec_add");
  885. if (!device)
  886. return_VALUE(-EINVAL);
  887. ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  888. if (!ec)
  889. return_VALUE(-ENOMEM);
  890. memset(ec, 0, sizeof(union acpi_ec));
  891. ec->common.handle = device->handle;
  892. ec->common.uid = -1;
  893. atomic_set(&ec->intr.pending_gpe, 0);
  894. atomic_set(&ec->intr.leaving_burst, 1);
  895. init_MUTEX(&ec->intr.sem);
  896. init_waitqueue_head(&ec->intr.wait);
  897. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  898. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  899. acpi_driver_data(device) = ec;
  900. /* Use the global lock for all EC transactions? */
  901. acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
  902. &ec->common.global_lock);
  903. /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
  904. http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
  905. if (ec_ecdt) {
  906. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  907. ACPI_ADR_SPACE_EC,
  908. &acpi_ec_space_handler);
  909. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  910. &acpi_ec_gpe_handler);
  911. kfree(ec_ecdt);
  912. }
  913. /* Get GPE bit assignment (EC events). */
  914. /* TODO: Add support for _GPE returning a package */
  915. status =
  916. acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
  917. &ec->common.gpe_bit);
  918. if (ACPI_FAILURE(status)) {
  919. printk(KERN_ERR PREFIX "Obtaining GPE bit assignment\n");
  920. result = -ENODEV;
  921. goto end;
  922. }
  923. result = acpi_ec_add_fs(device);
  924. if (result)
  925. goto end;
  926. printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
  927. acpi_device_name(device), acpi_device_bid(device),
  928. (u32) ec->common.gpe_bit);
  929. if (!first_ec)
  930. first_ec = device;
  931. end:
  932. if (result)
  933. kfree(ec);
  934. return_VALUE(result);
  935. }
  936. static int acpi_ec_remove(struct acpi_device *device, int type)
  937. {
  938. union acpi_ec *ec = NULL;
  939. ACPI_FUNCTION_TRACE("acpi_ec_remove");
  940. if (!device)
  941. return_VALUE(-EINVAL);
  942. ec = acpi_driver_data(device);
  943. acpi_ec_remove_fs(device);
  944. kfree(ec);
  945. return_VALUE(0);
  946. }
  947. static acpi_status
  948. acpi_ec_io_ports(struct acpi_resource *resource, void *context)
  949. {
  950. union acpi_ec *ec = (union acpi_ec *)context;
  951. struct acpi_generic_address *addr;
  952. if (resource->type != ACPI_RESOURCE_TYPE_IO) {
  953. return AE_OK;
  954. }
  955. /*
  956. * The first address region returned is the data port, and
  957. * the second address region returned is the status/command
  958. * port.
  959. */
  960. if (ec->common.data_addr.register_bit_width == 0) {
  961. addr = &ec->common.data_addr;
  962. } else if (ec->common.command_addr.register_bit_width == 0) {
  963. addr = &ec->common.command_addr;
  964. } else {
  965. return AE_CTRL_TERMINATE;
  966. }
  967. addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  968. addr->register_bit_width = 8;
  969. addr->register_bit_offset = 0;
  970. addr->address = resource->data.io.minimum;
  971. return AE_OK;
  972. }
  973. static int acpi_ec_start(struct acpi_device *device)
  974. {
  975. acpi_status status = AE_OK;
  976. union acpi_ec *ec = NULL;
  977. ACPI_FUNCTION_TRACE("acpi_ec_start");
  978. if (!device)
  979. return_VALUE(-EINVAL);
  980. ec = acpi_driver_data(device);
  981. if (!ec)
  982. return_VALUE(-EINVAL);
  983. /*
  984. * Get I/O port addresses. Convert to GAS format.
  985. */
  986. status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
  987. acpi_ec_io_ports, ec);
  988. if (ACPI_FAILURE(status)
  989. || ec->common.command_addr.register_bit_width == 0) {
  990. printk(KERN_ERR PREFIX "Error getting I/O port addresses\n");
  991. return_VALUE(-ENODEV);
  992. }
  993. ec->common.status_addr = ec->common.command_addr;
  994. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
  995. (u32) ec->common.gpe_bit,
  996. (u32) ec->common.command_addr.address,
  997. (u32) ec->common.data_addr.address));
  998. /*
  999. * Install GPE handler
  1000. */
  1001. status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
  1002. ACPI_GPE_EDGE_TRIGGERED,
  1003. &acpi_ec_gpe_handler, ec);
  1004. if (ACPI_FAILURE(status)) {
  1005. return_VALUE(-ENODEV);
  1006. }
  1007. acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  1008. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  1009. status = acpi_install_address_space_handler(ec->common.handle,
  1010. ACPI_ADR_SPACE_EC,
  1011. &acpi_ec_space_handler,
  1012. &acpi_ec_space_setup, ec);
  1013. if (ACPI_FAILURE(status)) {
  1014. acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
  1015. &acpi_ec_gpe_handler);
  1016. return_VALUE(-ENODEV);
  1017. }
  1018. return_VALUE(AE_OK);
  1019. }
  1020. static int acpi_ec_stop(struct acpi_device *device, int type)
  1021. {
  1022. acpi_status status = AE_OK;
  1023. union acpi_ec *ec = NULL;
  1024. ACPI_FUNCTION_TRACE("acpi_ec_stop");
  1025. if (!device)
  1026. return_VALUE(-EINVAL);
  1027. ec = acpi_driver_data(device);
  1028. status = acpi_remove_address_space_handler(ec->common.handle,
  1029. ACPI_ADR_SPACE_EC,
  1030. &acpi_ec_space_handler);
  1031. if (ACPI_FAILURE(status))
  1032. return_VALUE(-ENODEV);
  1033. status =
  1034. acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
  1035. &acpi_ec_gpe_handler);
  1036. if (ACPI_FAILURE(status))
  1037. return_VALUE(-ENODEV);
  1038. return_VALUE(0);
  1039. }
  1040. static acpi_status __init
  1041. acpi_fake_ecdt_callback(acpi_handle handle,
  1042. u32 Level, void *context, void **retval)
  1043. {
  1044. if (acpi_ec_poll_mode)
  1045. return acpi_fake_ecdt_poll_callback(handle,
  1046. Level, context, retval);
  1047. else
  1048. return acpi_fake_ecdt_intr_callback(handle,
  1049. Level, context, retval);
  1050. }
  1051. static acpi_status __init
  1052. acpi_fake_ecdt_poll_callback(acpi_handle handle,
  1053. u32 Level, void *context, void **retval)
  1054. {
  1055. acpi_status status;
  1056. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1057. acpi_ec_io_ports, ec_ecdt);
  1058. if (ACPI_FAILURE(status))
  1059. return status;
  1060. ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
  1061. ec_ecdt->common.uid = -1;
  1062. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
  1063. status =
  1064. acpi_evaluate_integer(handle, "_GPE", NULL,
  1065. &ec_ecdt->common.gpe_bit);
  1066. if (ACPI_FAILURE(status))
  1067. return status;
  1068. init_MUTEX(&ec_ecdt->poll.sem);
  1069. ec_ecdt->common.global_lock = TRUE;
  1070. ec_ecdt->common.handle = handle;
  1071. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  1072. (u32) ec_ecdt->common.gpe_bit,
  1073. (u32) ec_ecdt->common.command_addr.address,
  1074. (u32) ec_ecdt->common.data_addr.address);
  1075. return AE_CTRL_TERMINATE;
  1076. }
  1077. static acpi_status __init
  1078. acpi_fake_ecdt_intr_callback(acpi_handle handle,
  1079. u32 Level, void *context, void **retval)
  1080. {
  1081. acpi_status status;
  1082. init_MUTEX(&ec_ecdt->intr.sem);
  1083. init_waitqueue_head(&ec_ecdt->intr.wait);
  1084. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1085. acpi_ec_io_ports, ec_ecdt);
  1086. if (ACPI_FAILURE(status))
  1087. return status;
  1088. ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
  1089. ec_ecdt->common.uid = -1;
  1090. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
  1091. status =
  1092. acpi_evaluate_integer(handle, "_GPE", NULL,
  1093. &ec_ecdt->common.gpe_bit);
  1094. if (ACPI_FAILURE(status))
  1095. return status;
  1096. ec_ecdt->common.global_lock = TRUE;
  1097. ec_ecdt->common.handle = handle;
  1098. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  1099. (u32) ec_ecdt->common.gpe_bit,
  1100. (u32) ec_ecdt->common.command_addr.address,
  1101. (u32) ec_ecdt->common.data_addr.address);
  1102. return AE_CTRL_TERMINATE;
  1103. }
  1104. /*
  1105. * Some BIOS (such as some from Gateway laptops) access EC region very early
  1106. * such as in BAT0._INI or EC._INI before an EC device is found and
  1107. * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
  1108. * required, but if EC regison is accessed early, it is required.
  1109. * The routine tries to workaround the BIOS bug by pre-scan EC device
  1110. * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
  1111. * op region (since _REG isn't invoked yet). The assumption is true for
  1112. * all systems found.
  1113. */
  1114. static int __init acpi_ec_fake_ecdt(void)
  1115. {
  1116. acpi_status status;
  1117. int ret = 0;
  1118. printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
  1119. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1120. if (!ec_ecdt) {
  1121. ret = -ENOMEM;
  1122. goto error;
  1123. }
  1124. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1125. status = acpi_get_devices(ACPI_EC_HID,
  1126. acpi_fake_ecdt_callback, NULL, NULL);
  1127. if (ACPI_FAILURE(status)) {
  1128. kfree(ec_ecdt);
  1129. ec_ecdt = NULL;
  1130. ret = -ENODEV;
  1131. goto error;
  1132. }
  1133. return 0;
  1134. error:
  1135. printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
  1136. return ret;
  1137. }
  1138. static int __init acpi_ec_get_real_ecdt(void)
  1139. {
  1140. if (acpi_ec_poll_mode)
  1141. return acpi_ec_poll_get_real_ecdt();
  1142. else
  1143. return acpi_ec_intr_get_real_ecdt();
  1144. }
  1145. static int __init acpi_ec_poll_get_real_ecdt(void)
  1146. {
  1147. acpi_status status;
  1148. struct acpi_table_ecdt *ecdt_ptr;
  1149. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  1150. (struct acpi_table_header **)
  1151. &ecdt_ptr);
  1152. if (ACPI_FAILURE(status))
  1153. return -ENODEV;
  1154. printk(KERN_INFO PREFIX "Found ECDT\n");
  1155. /*
  1156. * Generate a temporary ec context to use until the namespace is scanned
  1157. */
  1158. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1159. if (!ec_ecdt)
  1160. return -ENOMEM;
  1161. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1162. ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
  1163. ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
  1164. ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
  1165. ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
  1166. init_MUTEX(&ec_ecdt->poll.sem);
  1167. /* use the GL just to be safe */
  1168. ec_ecdt->common.global_lock = TRUE;
  1169. ec_ecdt->common.uid = ecdt_ptr->uid;
  1170. status =
  1171. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
  1172. if (ACPI_FAILURE(status)) {
  1173. goto error;
  1174. }
  1175. return 0;
  1176. error:
  1177. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1178. kfree(ec_ecdt);
  1179. ec_ecdt = NULL;
  1180. return -ENODEV;
  1181. }
  1182. static int __init acpi_ec_intr_get_real_ecdt(void)
  1183. {
  1184. acpi_status status;
  1185. struct acpi_table_ecdt *ecdt_ptr;
  1186. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  1187. (struct acpi_table_header **)
  1188. &ecdt_ptr);
  1189. if (ACPI_FAILURE(status))
  1190. return -ENODEV;
  1191. printk(KERN_INFO PREFIX "Found ECDT\n");
  1192. /*
  1193. * Generate a temporary ec context to use until the namespace is scanned
  1194. */
  1195. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1196. if (!ec_ecdt)
  1197. return -ENOMEM;
  1198. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1199. init_MUTEX(&ec_ecdt->intr.sem);
  1200. init_waitqueue_head(&ec_ecdt->intr.wait);
  1201. ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
  1202. ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
  1203. ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
  1204. ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
  1205. /* use the GL just to be safe */
  1206. ec_ecdt->common.global_lock = TRUE;
  1207. ec_ecdt->common.uid = ecdt_ptr->uid;
  1208. status =
  1209. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
  1210. if (ACPI_FAILURE(status)) {
  1211. goto error;
  1212. }
  1213. return 0;
  1214. error:
  1215. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1216. kfree(ec_ecdt);
  1217. ec_ecdt = NULL;
  1218. return -ENODEV;
  1219. }
  1220. static int __initdata acpi_fake_ecdt_enabled;
  1221. int __init acpi_ec_ecdt_probe(void)
  1222. {
  1223. acpi_status status;
  1224. int ret;
  1225. ret = acpi_ec_get_real_ecdt();
  1226. /* Try to make a fake ECDT */
  1227. if (ret && acpi_fake_ecdt_enabled) {
  1228. ret = acpi_ec_fake_ecdt();
  1229. }
  1230. if (ret)
  1231. return 0;
  1232. /*
  1233. * Install GPE handler
  1234. */
  1235. status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  1236. ACPI_GPE_EDGE_TRIGGERED,
  1237. &acpi_ec_gpe_handler, ec_ecdt);
  1238. if (ACPI_FAILURE(status)) {
  1239. goto error;
  1240. }
  1241. acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  1242. acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
  1243. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  1244. ACPI_ADR_SPACE_EC,
  1245. &acpi_ec_space_handler,
  1246. &acpi_ec_space_setup,
  1247. ec_ecdt);
  1248. if (ACPI_FAILURE(status)) {
  1249. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  1250. &acpi_ec_gpe_handler);
  1251. goto error;
  1252. }
  1253. return 0;
  1254. error:
  1255. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1256. kfree(ec_ecdt);
  1257. ec_ecdt = NULL;
  1258. return -ENODEV;
  1259. }
  1260. static int __init acpi_ec_init(void)
  1261. {
  1262. int result = 0;
  1263. ACPI_FUNCTION_TRACE("acpi_ec_init");
  1264. if (acpi_disabled)
  1265. return_VALUE(0);
  1266. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  1267. if (!acpi_ec_dir)
  1268. return_VALUE(-ENODEV);
  1269. /* Now register the driver for the EC */
  1270. result = acpi_bus_register_driver(&acpi_ec_driver);
  1271. if (result < 0) {
  1272. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  1273. return_VALUE(-ENODEV);
  1274. }
  1275. return_VALUE(result);
  1276. }
  1277. subsys_initcall(acpi_ec_init);
  1278. /* EC driver currently not unloadable */
  1279. #if 0
  1280. static void __exit acpi_ec_exit(void)
  1281. {
  1282. ACPI_FUNCTION_TRACE("acpi_ec_exit");
  1283. acpi_bus_unregister_driver(&acpi_ec_driver);
  1284. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  1285. return_VOID;
  1286. }
  1287. #endif /* 0 */
  1288. static int __init acpi_fake_ecdt_setup(char *str)
  1289. {
  1290. acpi_fake_ecdt_enabled = 1;
  1291. return 1;
  1292. }
  1293. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
  1294. static int __init acpi_ec_set_intr_mode(char *str)
  1295. {
  1296. int intr;
  1297. if (!get_option(&str, &intr))
  1298. return 0;
  1299. if (intr) {
  1300. acpi_ec_poll_mode = EC_INTR;
  1301. acpi_ec_driver.ops.add = acpi_ec_intr_add;
  1302. } else {
  1303. acpi_ec_poll_mode = EC_POLL;
  1304. acpi_ec_driver.ops.add = acpi_ec_poll_add;
  1305. }
  1306. printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
  1307. return 1;
  1308. }
  1309. __setup("ec_intr=", acpi_ec_set_intr_mode);