ec.c 41 KB

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