ec.c 39 KB

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