ec.c 39 KB

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