ec.c 41 KB

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