ec.c 38 KB

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