ec.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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. spinlock_t lock;
  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. unsigned long flags = 0;
  294. u32 glk = 0;
  295. ACPI_FUNCTION_TRACE("acpi_ec_read");
  296. if (!ec || !data)
  297. return_VALUE(-EINVAL);
  298. *data = 0;
  299. if (ec->common.global_lock) {
  300. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  301. if (ACPI_FAILURE(status))
  302. return_VALUE(-ENODEV);
  303. }
  304. spin_lock_irqsave(&ec->poll.lock, flags);
  305. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
  306. &ec->common.command_addr);
  307. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  308. if (result)
  309. goto end;
  310. acpi_hw_low_level_write(8, address, &ec->common.data_addr);
  311. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  312. if (result)
  313. goto end;
  314. acpi_hw_low_level_read(8, data, &ec->common.data_addr);
  315. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
  316. *data, address));
  317. end:
  318. spin_unlock_irqrestore(&ec->poll.lock, flags);
  319. if (ec->common.global_lock)
  320. acpi_release_global_lock(glk);
  321. return_VALUE(result);
  322. }
  323. static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
  324. {
  325. int result = 0;
  326. acpi_status status = AE_OK;
  327. unsigned long flags = 0;
  328. u32 glk = 0;
  329. ACPI_FUNCTION_TRACE("acpi_ec_write");
  330. if (!ec)
  331. return_VALUE(-EINVAL);
  332. if (ec->common.global_lock) {
  333. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  334. if (ACPI_FAILURE(status))
  335. return_VALUE(-ENODEV);
  336. }
  337. spin_lock_irqsave(&ec->poll.lock, flags);
  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. spin_unlock_irqrestore(&ec->poll.lock, flags);
  355. if (ec->common.global_lock)
  356. acpi_release_global_lock(glk);
  357. return_VALUE(result);
  358. }
  359. static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
  360. {
  361. int status = 0;
  362. u32 glk;
  363. ACPI_FUNCTION_TRACE("acpi_ec_read");
  364. if (!ec || !data)
  365. return_VALUE(-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_VALUE(-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_VALUE(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. ACPI_FUNCTION_TRACE("acpi_ec_write");
  405. if (!ec)
  406. return_VALUE(-EINVAL);
  407. if (ec->common.global_lock) {
  408. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  409. if (ACPI_FAILURE(status))
  410. return_VALUE(-ENODEV);
  411. }
  412. WARN_ON(in_interrupt());
  413. down(&ec->intr.sem);
  414. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  415. if (status) {
  416. printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
  417. }
  418. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
  419. &ec->common.command_addr);
  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, address, &ec->common.data_addr);
  425. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  426. if (status) {
  427. printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
  428. }
  429. acpi_hw_low_level_write(8, data, &ec->common.data_addr);
  430. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
  431. data, address));
  432. up(&ec->intr.sem);
  433. if (ec->common.global_lock)
  434. acpi_release_global_lock(glk);
  435. return_VALUE(status);
  436. }
  437. /*
  438. * Externally callable EC access functions. For now, assume 1 EC only
  439. */
  440. int ec_read(u8 addr, u8 * val)
  441. {
  442. union acpi_ec *ec;
  443. int err;
  444. u32 temp_data;
  445. if (!first_ec)
  446. return -ENODEV;
  447. ec = acpi_driver_data(first_ec);
  448. err = acpi_ec_read(ec, addr, &temp_data);
  449. if (!err) {
  450. *val = temp_data;
  451. return 0;
  452. } else
  453. return err;
  454. }
  455. EXPORT_SYMBOL(ec_read);
  456. int ec_write(u8 addr, u8 val)
  457. {
  458. union acpi_ec *ec;
  459. int err;
  460. if (!first_ec)
  461. return -ENODEV;
  462. ec = acpi_driver_data(first_ec);
  463. err = acpi_ec_write(ec, addr, val);
  464. return err;
  465. }
  466. EXPORT_SYMBOL(ec_write);
  467. static int acpi_ec_query(union acpi_ec *ec, u32 * data)
  468. {
  469. if (acpi_ec_poll_mode)
  470. return acpi_ec_poll_query(ec, data);
  471. else
  472. return acpi_ec_intr_query(ec, data);
  473. }
  474. static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
  475. {
  476. int result = 0;
  477. acpi_status status = AE_OK;
  478. unsigned long flags = 0;
  479. u32 glk = 0;
  480. ACPI_FUNCTION_TRACE("acpi_ec_query");
  481. if (!ec || !data)
  482. return_VALUE(-EINVAL);
  483. *data = 0;
  484. if (ec->common.global_lock) {
  485. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  486. if (ACPI_FAILURE(status))
  487. return_VALUE(-ENODEV);
  488. }
  489. /*
  490. * Query the EC to find out which _Qxx method we need to evaluate.
  491. * Note that successful completion of the query causes the ACPI_EC_SCI
  492. * bit to be cleared (and thus clearing the interrupt source).
  493. */
  494. spin_lock_irqsave(&ec->poll.lock, flags);
  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. spin_unlock_irqrestore(&ec->poll.lock, flags);
  505. if (ec->common.global_lock)
  506. acpi_release_global_lock(glk);
  507. return_VALUE(result);
  508. }
  509. static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
  510. {
  511. int status = 0;
  512. u32 glk;
  513. ACPI_FUNCTION_TRACE("acpi_ec_query");
  514. if (!ec || !data)
  515. return_VALUE(-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_VALUE(-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_VALUE(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. unsigned long flags = 0;
  568. static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
  569. const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
  570. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  571. };
  572. ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
  573. if (!ec_cxt)
  574. goto end;
  575. spin_lock_irqsave(&ec->poll.lock, flags);
  576. acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
  577. spin_unlock_irqrestore(&ec->poll.lock, flags);
  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. ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
  605. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
  606. result = acpi_ec_query(ec, &value);
  607. if (result)
  608. goto end;
  609. object_name[2] = hex[((value >> 4) & 0x0F)];
  610. object_name[3] = hex[(value & 0x0F)];
  611. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  612. acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
  613. end:
  614. atomic_dec(&ec->intr.pending_gpe);
  615. return;
  616. }
  617. static u32 acpi_ec_gpe_handler(void *data)
  618. {
  619. if (acpi_ec_poll_mode)
  620. return acpi_ec_gpe_poll_handler(data);
  621. else
  622. return acpi_ec_gpe_intr_handler(data);
  623. }
  624. static u32 acpi_ec_gpe_poll_handler(void *data)
  625. {
  626. acpi_status status = AE_OK;
  627. union acpi_ec *ec = (union acpi_ec *)data;
  628. if (!ec)
  629. return ACPI_INTERRUPT_NOT_HANDLED;
  630. acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  631. status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
  632. if (status == AE_OK)
  633. return ACPI_INTERRUPT_HANDLED;
  634. else
  635. return ACPI_INTERRUPT_NOT_HANDLED;
  636. }
  637. static u32 acpi_ec_gpe_intr_handler(void *data)
  638. {
  639. acpi_status status = AE_OK;
  640. u32 value;
  641. union acpi_ec *ec = (union acpi_ec *)data;
  642. if (!ec)
  643. return ACPI_INTERRUPT_NOT_HANDLED;
  644. acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  645. value = acpi_ec_read_status(ec);
  646. switch (ec->intr.expect_event) {
  647. case ACPI_EC_EVENT_OBF:
  648. if (!(value & ACPI_EC_FLAG_OBF))
  649. break;
  650. case ACPI_EC_EVENT_IBE:
  651. if ((value & ACPI_EC_FLAG_IBF))
  652. break;
  653. ec->intr.expect_event = 0;
  654. wake_up(&ec->intr.wait);
  655. return ACPI_INTERRUPT_HANDLED;
  656. default:
  657. break;
  658. }
  659. if (value & ACPI_EC_FLAG_SCI) {
  660. atomic_add(1, &ec->intr.pending_gpe);
  661. status = acpi_os_execute(OSL_EC_BURST_HANDLER,
  662. acpi_ec_gpe_query, ec);
  663. return status == AE_OK ?
  664. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  665. }
  666. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  667. return status == AE_OK ?
  668. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  669. }
  670. /* --------------------------------------------------------------------------
  671. Address Space Management
  672. -------------------------------------------------------------------------- */
  673. static acpi_status
  674. acpi_ec_space_setup(acpi_handle region_handle,
  675. u32 function, void *handler_context, void **return_context)
  676. {
  677. /*
  678. * The EC object is in the handler context and is needed
  679. * when calling the acpi_ec_space_handler.
  680. */
  681. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  682. handler_context : NULL;
  683. return AE_OK;
  684. }
  685. static acpi_status
  686. acpi_ec_space_handler(u32 function,
  687. acpi_physical_address address,
  688. u32 bit_width,
  689. acpi_integer * value,
  690. void *handler_context, void *region_context)
  691. {
  692. int result = 0;
  693. union acpi_ec *ec = NULL;
  694. u64 temp = *value;
  695. acpi_integer f_v = 0;
  696. int i = 0;
  697. ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
  698. if ((address > 0xFF) || !value || !handler_context)
  699. return_VALUE(AE_BAD_PARAMETER);
  700. if (bit_width != 8 && acpi_strict) {
  701. printk(KERN_WARNING PREFIX
  702. "acpi_ec_space_handler: bit_width should be 8\n");
  703. return_VALUE(AE_BAD_PARAMETER);
  704. }
  705. ec = (union acpi_ec *)handler_context;
  706. next_byte:
  707. switch (function) {
  708. case ACPI_READ:
  709. temp = 0;
  710. result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
  711. break;
  712. case ACPI_WRITE:
  713. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  714. break;
  715. default:
  716. result = -EINVAL;
  717. goto out;
  718. break;
  719. }
  720. bit_width -= 8;
  721. if (bit_width) {
  722. if (function == ACPI_READ)
  723. f_v |= temp << 8 * i;
  724. if (function == ACPI_WRITE)
  725. temp >>= 8;
  726. i++;
  727. address++;
  728. goto next_byte;
  729. }
  730. if (function == ACPI_READ) {
  731. f_v |= temp << 8 * i;
  732. *value = f_v;
  733. }
  734. out:
  735. switch (result) {
  736. case -EINVAL:
  737. return_VALUE(AE_BAD_PARAMETER);
  738. break;
  739. case -ENODEV:
  740. return_VALUE(AE_NOT_FOUND);
  741. break;
  742. case -ETIME:
  743. return_VALUE(AE_TIME);
  744. break;
  745. default:
  746. return_VALUE(AE_OK);
  747. }
  748. }
  749. /* --------------------------------------------------------------------------
  750. FS Interface (/proc)
  751. -------------------------------------------------------------------------- */
  752. static struct proc_dir_entry *acpi_ec_dir;
  753. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  754. {
  755. union acpi_ec *ec = (union acpi_ec *)seq->private;
  756. ACPI_FUNCTION_TRACE("acpi_ec_read_info");
  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_VALUE(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 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. ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
  785. if (!acpi_device_dir(device)) {
  786. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  787. acpi_ec_dir);
  788. if (!acpi_device_dir(device))
  789. return_VALUE(-ENODEV);
  790. }
  791. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  792. acpi_device_dir(device));
  793. if (!entry)
  794. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  795. "Unable to create '%s' fs entry\n",
  796. ACPI_EC_FILE_INFO));
  797. else {
  798. entry->proc_fops = &acpi_ec_info_ops;
  799. entry->data = acpi_driver_data(device);
  800. entry->owner = THIS_MODULE;
  801. }
  802. return_VALUE(0);
  803. }
  804. static int acpi_ec_remove_fs(struct acpi_device *device)
  805. {
  806. ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
  807. if (acpi_device_dir(device)) {
  808. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  809. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  810. acpi_device_dir(device) = NULL;
  811. }
  812. return_VALUE(0);
  813. }
  814. /* --------------------------------------------------------------------------
  815. Driver Interface
  816. -------------------------------------------------------------------------- */
  817. static int acpi_ec_poll_add(struct acpi_device *device)
  818. {
  819. int result = 0;
  820. acpi_status status = AE_OK;
  821. union acpi_ec *ec = NULL;
  822. unsigned long uid;
  823. ACPI_FUNCTION_TRACE("acpi_ec_add");
  824. if (!device)
  825. return_VALUE(-EINVAL);
  826. ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  827. if (!ec)
  828. return_VALUE(-ENOMEM);
  829. memset(ec, 0, sizeof(union acpi_ec));
  830. ec->common.handle = device->handle;
  831. ec->common.uid = -1;
  832. spin_lock_init(&ec->poll.lock);
  833. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  834. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  835. acpi_driver_data(device) = ec;
  836. /* Use the global lock for all EC transactions? */
  837. acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
  838. &ec->common.global_lock);
  839. /* If our UID matches the UID for the ECDT-enumerated EC,
  840. we now have the *real* EC info, so kill the makeshift one. */
  841. acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
  842. if (ec_ecdt && ec_ecdt->common.uid == uid) {
  843. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  844. ACPI_ADR_SPACE_EC,
  845. &acpi_ec_space_handler);
  846. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  847. &acpi_ec_gpe_handler);
  848. kfree(ec_ecdt);
  849. }
  850. /* Get GPE bit assignment (EC events). */
  851. /* TODO: Add support for _GPE returning a package */
  852. status =
  853. acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
  854. &ec->common.gpe_bit);
  855. if (ACPI_FAILURE(status)) {
  856. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  857. "Error obtaining GPE bit assignment\n"));
  858. result = -ENODEV;
  859. goto end;
  860. }
  861. result = acpi_ec_add_fs(device);
  862. if (result)
  863. goto end;
  864. printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
  865. acpi_device_name(device), acpi_device_bid(device),
  866. (u32) ec->common.gpe_bit);
  867. if (!first_ec)
  868. first_ec = device;
  869. end:
  870. if (result)
  871. kfree(ec);
  872. return_VALUE(result);
  873. }
  874. static int acpi_ec_intr_add(struct acpi_device *device)
  875. {
  876. int result = 0;
  877. acpi_status status = AE_OK;
  878. union acpi_ec *ec = NULL;
  879. unsigned long uid;
  880. ACPI_FUNCTION_TRACE("acpi_ec_add");
  881. if (!device)
  882. return_VALUE(-EINVAL);
  883. ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  884. if (!ec)
  885. return_VALUE(-ENOMEM);
  886. memset(ec, 0, sizeof(union acpi_ec));
  887. ec->common.handle = device->handle;
  888. ec->common.uid = -1;
  889. atomic_set(&ec->intr.pending_gpe, 0);
  890. atomic_set(&ec->intr.leaving_burst, 1);
  891. init_MUTEX(&ec->intr.sem);
  892. init_waitqueue_head(&ec->intr.wait);
  893. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  894. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  895. acpi_driver_data(device) = ec;
  896. /* Use the global lock for all EC transactions? */
  897. acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
  898. &ec->common.global_lock);
  899. /* If our UID matches the UID for the ECDT-enumerated EC,
  900. we now have the *real* EC info, so kill the makeshift one. */
  901. acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
  902. if (ec_ecdt && ec_ecdt->common.uid == uid) {
  903. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  904. ACPI_ADR_SPACE_EC,
  905. &acpi_ec_space_handler);
  906. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  907. &acpi_ec_gpe_handler);
  908. kfree(ec_ecdt);
  909. }
  910. /* Get GPE bit assignment (EC events). */
  911. /* TODO: Add support for _GPE returning a package */
  912. status =
  913. acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
  914. &ec->common.gpe_bit);
  915. if (ACPI_FAILURE(status)) {
  916. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  917. "Error obtaining GPE bit assignment\n"));
  918. result = -ENODEV;
  919. goto end;
  920. }
  921. result = acpi_ec_add_fs(device);
  922. if (result)
  923. goto end;
  924. printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
  925. acpi_device_name(device), acpi_device_bid(device),
  926. (u32) ec->common.gpe_bit);
  927. if (!first_ec)
  928. first_ec = device;
  929. end:
  930. if (result)
  931. kfree(ec);
  932. return_VALUE(result);
  933. }
  934. static int acpi_ec_remove(struct acpi_device *device, int type)
  935. {
  936. union acpi_ec *ec = NULL;
  937. ACPI_FUNCTION_TRACE("acpi_ec_remove");
  938. if (!device)
  939. return_VALUE(-EINVAL);
  940. ec = acpi_driver_data(device);
  941. acpi_ec_remove_fs(device);
  942. kfree(ec);
  943. return_VALUE(0);
  944. }
  945. static acpi_status
  946. acpi_ec_io_ports(struct acpi_resource *resource, void *context)
  947. {
  948. union acpi_ec *ec = (union acpi_ec *)context;
  949. struct acpi_generic_address *addr;
  950. if (resource->type != ACPI_RESOURCE_TYPE_IO) {
  951. return AE_OK;
  952. }
  953. /*
  954. * The first address region returned is the data port, and
  955. * the second address region returned is the status/command
  956. * port.
  957. */
  958. if (ec->common.data_addr.register_bit_width == 0) {
  959. addr = &ec->common.data_addr;
  960. } else if (ec->common.command_addr.register_bit_width == 0) {
  961. addr = &ec->common.command_addr;
  962. } else {
  963. return AE_CTRL_TERMINATE;
  964. }
  965. addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  966. addr->register_bit_width = 8;
  967. addr->register_bit_offset = 0;
  968. addr->address = resource->data.io.minimum;
  969. return AE_OK;
  970. }
  971. static int acpi_ec_start(struct acpi_device *device)
  972. {
  973. acpi_status status = AE_OK;
  974. union acpi_ec *ec = NULL;
  975. ACPI_FUNCTION_TRACE("acpi_ec_start");
  976. if (!device)
  977. return_VALUE(-EINVAL);
  978. ec = acpi_driver_data(device);
  979. if (!ec)
  980. return_VALUE(-EINVAL);
  981. /*
  982. * Get I/O port addresses. Convert to GAS format.
  983. */
  984. status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
  985. acpi_ec_io_ports, ec);
  986. if (ACPI_FAILURE(status)
  987. || ec->common.command_addr.register_bit_width == 0) {
  988. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  989. "Error getting I/O port addresses"));
  990. return_VALUE(-ENODEV);
  991. }
  992. ec->common.status_addr = ec->common.command_addr;
  993. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
  994. (u32) ec->common.gpe_bit,
  995. (u32) ec->common.command_addr.address,
  996. (u32) ec->common.data_addr.address));
  997. /*
  998. * Install GPE handler
  999. */
  1000. status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
  1001. ACPI_GPE_EDGE_TRIGGERED,
  1002. &acpi_ec_gpe_handler, ec);
  1003. if (ACPI_FAILURE(status)) {
  1004. return_VALUE(-ENODEV);
  1005. }
  1006. acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  1007. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  1008. status = acpi_install_address_space_handler(ec->common.handle,
  1009. ACPI_ADR_SPACE_EC,
  1010. &acpi_ec_space_handler,
  1011. &acpi_ec_space_setup, ec);
  1012. if (ACPI_FAILURE(status)) {
  1013. acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
  1014. &acpi_ec_gpe_handler);
  1015. return_VALUE(-ENODEV);
  1016. }
  1017. return_VALUE(AE_OK);
  1018. }
  1019. static int acpi_ec_stop(struct acpi_device *device, int type)
  1020. {
  1021. acpi_status status = AE_OK;
  1022. union acpi_ec *ec = NULL;
  1023. ACPI_FUNCTION_TRACE("acpi_ec_stop");
  1024. if (!device)
  1025. return_VALUE(-EINVAL);
  1026. ec = acpi_driver_data(device);
  1027. status = acpi_remove_address_space_handler(ec->common.handle,
  1028. ACPI_ADR_SPACE_EC,
  1029. &acpi_ec_space_handler);
  1030. if (ACPI_FAILURE(status))
  1031. return_VALUE(-ENODEV);
  1032. status =
  1033. acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
  1034. &acpi_ec_gpe_handler);
  1035. if (ACPI_FAILURE(status))
  1036. return_VALUE(-ENODEV);
  1037. return_VALUE(0);
  1038. }
  1039. static acpi_status __init
  1040. acpi_fake_ecdt_callback(acpi_handle handle,
  1041. u32 Level, void *context, void **retval)
  1042. {
  1043. if (acpi_ec_poll_mode)
  1044. return acpi_fake_ecdt_poll_callback(handle,
  1045. Level, context, retval);
  1046. else
  1047. return acpi_fake_ecdt_intr_callback(handle,
  1048. Level, context, retval);
  1049. }
  1050. static acpi_status __init
  1051. acpi_fake_ecdt_poll_callback(acpi_handle handle,
  1052. u32 Level, void *context, void **retval)
  1053. {
  1054. acpi_status status;
  1055. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1056. acpi_ec_io_ports, ec_ecdt);
  1057. if (ACPI_FAILURE(status))
  1058. return status;
  1059. ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
  1060. ec_ecdt->common.uid = -1;
  1061. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
  1062. status =
  1063. acpi_evaluate_integer(handle, "_GPE", NULL,
  1064. &ec_ecdt->common.gpe_bit);
  1065. if (ACPI_FAILURE(status))
  1066. return status;
  1067. spin_lock_init(&ec_ecdt->poll.lock);
  1068. ec_ecdt->common.global_lock = TRUE;
  1069. ec_ecdt->common.handle = handle;
  1070. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  1071. (u32) ec_ecdt->common.gpe_bit,
  1072. (u32) ec_ecdt->common.command_addr.address,
  1073. (u32) ec_ecdt->common.data_addr.address);
  1074. return AE_CTRL_TERMINATE;
  1075. }
  1076. static acpi_status __init
  1077. acpi_fake_ecdt_intr_callback(acpi_handle handle,
  1078. u32 Level, void *context, void **retval)
  1079. {
  1080. acpi_status status;
  1081. init_MUTEX(&ec_ecdt->intr.sem);
  1082. init_waitqueue_head(&ec_ecdt->intr.wait);
  1083. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1084. acpi_ec_io_ports, ec_ecdt);
  1085. if (ACPI_FAILURE(status))
  1086. return status;
  1087. ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
  1088. ec_ecdt->common.uid = -1;
  1089. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
  1090. status =
  1091. acpi_evaluate_integer(handle, "_GPE", NULL,
  1092. &ec_ecdt->common.gpe_bit);
  1093. if (ACPI_FAILURE(status))
  1094. return status;
  1095. ec_ecdt->common.global_lock = TRUE;
  1096. ec_ecdt->common.handle = handle;
  1097. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  1098. (u32) ec_ecdt->common.gpe_bit,
  1099. (u32) ec_ecdt->common.command_addr.address,
  1100. (u32) ec_ecdt->common.data_addr.address);
  1101. return AE_CTRL_TERMINATE;
  1102. }
  1103. /*
  1104. * Some BIOS (such as some from Gateway laptops) access EC region very early
  1105. * such as in BAT0._INI or EC._INI before an EC device is found and
  1106. * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
  1107. * required, but if EC regison is accessed early, it is required.
  1108. * The routine tries to workaround the BIOS bug by pre-scan EC device
  1109. * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
  1110. * op region (since _REG isn't invoked yet). The assumption is true for
  1111. * all systems found.
  1112. */
  1113. static int __init acpi_ec_fake_ecdt(void)
  1114. {
  1115. acpi_status status;
  1116. int ret = 0;
  1117. printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
  1118. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1119. if (!ec_ecdt) {
  1120. ret = -ENOMEM;
  1121. goto error;
  1122. }
  1123. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1124. status = acpi_get_devices(ACPI_EC_HID,
  1125. acpi_fake_ecdt_callback, NULL, NULL);
  1126. if (ACPI_FAILURE(status)) {
  1127. kfree(ec_ecdt);
  1128. ec_ecdt = NULL;
  1129. ret = -ENODEV;
  1130. goto error;
  1131. }
  1132. return 0;
  1133. error:
  1134. printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
  1135. return ret;
  1136. }
  1137. static int __init acpi_ec_get_real_ecdt(void)
  1138. {
  1139. if (acpi_ec_poll_mode)
  1140. return acpi_ec_poll_get_real_ecdt();
  1141. else
  1142. return acpi_ec_intr_get_real_ecdt();
  1143. }
  1144. static int __init acpi_ec_poll_get_real_ecdt(void)
  1145. {
  1146. acpi_status status;
  1147. struct acpi_table_ecdt *ecdt_ptr;
  1148. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  1149. (struct acpi_table_header **)
  1150. &ecdt_ptr);
  1151. if (ACPI_FAILURE(status))
  1152. return -ENODEV;
  1153. printk(KERN_INFO PREFIX "Found ECDT\n");
  1154. /*
  1155. * Generate a temporary ec context to use until the namespace is scanned
  1156. */
  1157. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1158. if (!ec_ecdt)
  1159. return -ENOMEM;
  1160. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1161. ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
  1162. ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
  1163. ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
  1164. ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
  1165. spin_lock_init(&ec_ecdt->poll.lock);
  1166. /* use the GL just to be safe */
  1167. ec_ecdt->common.global_lock = TRUE;
  1168. ec_ecdt->common.uid = ecdt_ptr->uid;
  1169. status =
  1170. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
  1171. if (ACPI_FAILURE(status)) {
  1172. goto error;
  1173. }
  1174. return 0;
  1175. error:
  1176. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1177. kfree(ec_ecdt);
  1178. ec_ecdt = NULL;
  1179. return -ENODEV;
  1180. }
  1181. static int __init acpi_ec_intr_get_real_ecdt(void)
  1182. {
  1183. acpi_status status;
  1184. struct acpi_table_ecdt *ecdt_ptr;
  1185. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  1186. (struct acpi_table_header **)
  1187. &ecdt_ptr);
  1188. if (ACPI_FAILURE(status))
  1189. return -ENODEV;
  1190. printk(KERN_INFO PREFIX "Found ECDT\n");
  1191. /*
  1192. * Generate a temporary ec context to use until the namespace is scanned
  1193. */
  1194. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1195. if (!ec_ecdt)
  1196. return -ENOMEM;
  1197. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1198. init_MUTEX(&ec_ecdt->intr.sem);
  1199. init_waitqueue_head(&ec_ecdt->intr.wait);
  1200. ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
  1201. ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
  1202. ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
  1203. ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
  1204. /* use the GL just to be safe */
  1205. ec_ecdt->common.global_lock = TRUE;
  1206. ec_ecdt->common.uid = ecdt_ptr->uid;
  1207. status =
  1208. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
  1209. if (ACPI_FAILURE(status)) {
  1210. goto error;
  1211. }
  1212. return 0;
  1213. error:
  1214. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1215. kfree(ec_ecdt);
  1216. ec_ecdt = NULL;
  1217. return -ENODEV;
  1218. }
  1219. static int __initdata acpi_fake_ecdt_enabled;
  1220. int __init acpi_ec_ecdt_probe(void)
  1221. {
  1222. acpi_status status;
  1223. int ret;
  1224. ret = acpi_ec_get_real_ecdt();
  1225. /* Try to make a fake ECDT */
  1226. if (ret && acpi_fake_ecdt_enabled) {
  1227. ret = acpi_ec_fake_ecdt();
  1228. }
  1229. if (ret)
  1230. return 0;
  1231. /*
  1232. * Install GPE handler
  1233. */
  1234. status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  1235. ACPI_GPE_EDGE_TRIGGERED,
  1236. &acpi_ec_gpe_handler, ec_ecdt);
  1237. if (ACPI_FAILURE(status)) {
  1238. goto error;
  1239. }
  1240. acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  1241. acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
  1242. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  1243. ACPI_ADR_SPACE_EC,
  1244. &acpi_ec_space_handler,
  1245. &acpi_ec_space_setup,
  1246. ec_ecdt);
  1247. if (ACPI_FAILURE(status)) {
  1248. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  1249. &acpi_ec_gpe_handler);
  1250. goto error;
  1251. }
  1252. return 0;
  1253. error:
  1254. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1255. kfree(ec_ecdt);
  1256. ec_ecdt = NULL;
  1257. return -ENODEV;
  1258. }
  1259. static int __init acpi_ec_init(void)
  1260. {
  1261. int result = 0;
  1262. ACPI_FUNCTION_TRACE("acpi_ec_init");
  1263. if (acpi_disabled)
  1264. return_VALUE(0);
  1265. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  1266. if (!acpi_ec_dir)
  1267. return_VALUE(-ENODEV);
  1268. /* Now register the driver for the EC */
  1269. result = acpi_bus_register_driver(&acpi_ec_driver);
  1270. if (result < 0) {
  1271. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  1272. return_VALUE(-ENODEV);
  1273. }
  1274. return_VALUE(result);
  1275. }
  1276. subsys_initcall(acpi_ec_init);
  1277. /* EC driver currently not unloadable */
  1278. #if 0
  1279. static void __exit acpi_ec_exit(void)
  1280. {
  1281. ACPI_FUNCTION_TRACE("acpi_ec_exit");
  1282. acpi_bus_unregister_driver(&acpi_ec_driver);
  1283. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  1284. return_VOID;
  1285. }
  1286. #endif /* 0 */
  1287. static int __init acpi_fake_ecdt_setup(char *str)
  1288. {
  1289. acpi_fake_ecdt_enabled = 1;
  1290. return 1;
  1291. }
  1292. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
  1293. static int __init acpi_ec_set_intr_mode(char *str)
  1294. {
  1295. int intr;
  1296. if (!get_option(&str, &intr))
  1297. return 0;
  1298. if (intr) {
  1299. acpi_ec_poll_mode = EC_INTR;
  1300. acpi_ec_driver.ops.add = acpi_ec_intr_add;
  1301. } else {
  1302. acpi_ec_poll_mode = EC_POLL;
  1303. acpi_ec_driver.ops.add = acpi_ec_poll_add;
  1304. }
  1305. printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
  1306. return 1;
  1307. }
  1308. __setup("ec_intr=", acpi_ec_set_intr_mode);