ec.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. u8 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, u8 event);
  119. static int acpi_ec_poll_transaction(union acpi_ec *ec, u8 command,
  120. const u8 *wdata, unsigned wdata_len,
  121. u8 *rdata, unsigned rdata_len);
  122. static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
  123. const u8 *wdata, unsigned wdata_len,
  124. u8 *rdata, unsigned rdata_len);
  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_check_status(u32 status, u8 event) {
  152. switch (event) {
  153. case ACPI_EC_EVENT_OBF:
  154. if (status & ACPI_EC_FLAG_OBF)
  155. return 1;
  156. case ACPI_EC_EVENT_IBE:
  157. if (!(status & ACPI_EC_FLAG_IBF))
  158. return 1;
  159. default:
  160. break;
  161. }
  162. return 0;
  163. }
  164. static int acpi_ec_wait(union acpi_ec *ec, u8 event)
  165. {
  166. if (acpi_ec_poll_mode)
  167. return acpi_ec_poll_wait(ec, event);
  168. else
  169. return acpi_ec_intr_wait(ec, event);
  170. }
  171. static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
  172. {
  173. u32 acpi_ec_status = 0;
  174. u32 i = ACPI_EC_UDELAY_COUNT;
  175. if (!ec)
  176. return -EINVAL;
  177. /* Poll the EC status register waiting for the event to occur. */
  178. switch (event) {
  179. case ACPI_EC_EVENT_OBF:
  180. do {
  181. acpi_hw_low_level_read(8, &acpi_ec_status,
  182. &ec->common.status_addr);
  183. if (acpi_ec_status & ACPI_EC_FLAG_OBF)
  184. return 0;
  185. udelay(ACPI_EC_UDELAY);
  186. } while (--i > 0);
  187. break;
  188. case ACPI_EC_EVENT_IBE:
  189. do {
  190. acpi_hw_low_level_read(8, &acpi_ec_status,
  191. &ec->common.status_addr);
  192. if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
  193. return 0;
  194. udelay(ACPI_EC_UDELAY);
  195. } while (--i > 0);
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. return -ETIME;
  201. }
  202. static int acpi_ec_intr_wait(union acpi_ec *ec, u8 event)
  203. {
  204. long time_left;
  205. ec->intr.expect_event = event;
  206. if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
  207. ec->intr.expect_event = 0;
  208. return 0;
  209. }
  210. time_left = wait_event_timeout(ec->intr.wait, !ec->intr.expect_event,
  211. msecs_to_jiffies(ACPI_EC_DELAY));
  212. ec->intr.expect_event = 0;
  213. if (time_left <= 0) {
  214. if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
  215. return 0;
  216. }
  217. } else {
  218. return 0;
  219. }
  220. return -ETIME;
  221. }
  222. #ifdef ACPI_FUTURE_USAGE
  223. /*
  224. * Note: samsung nv5000 doesn't work with ec burst mode.
  225. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  226. */
  227. int acpi_ec_enter_burst_mode(union acpi_ec *ec)
  228. {
  229. u32 tmp = 0;
  230. int status = 0;
  231. status = acpi_ec_read_status(ec);
  232. if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
  233. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  234. if (status)
  235. goto end;
  236. acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
  237. &ec->common.command_addr);
  238. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  239. acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
  240. if (tmp != 0x90) { /* Burst ACK byte */
  241. return -EINVAL;
  242. }
  243. }
  244. atomic_set(&ec->intr.leaving_burst, 0);
  245. return 0;
  246. end:
  247. ACPI_EXCEPTION ((AE_INFO, status, "EC wait, burst mode");
  248. return -1;
  249. }
  250. int acpi_ec_leave_burst_mode(union acpi_ec *ec)
  251. {
  252. int status = 0;
  253. status = acpi_ec_read_status(ec);
  254. if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
  255. status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
  256. if(status)
  257. goto end;
  258. acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
  259. acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
  260. }
  261. atomic_set(&ec->intr.leaving_burst, 1);
  262. return 0;
  263. end:
  264. ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode");
  265. return -1;
  266. }
  267. #endif /* ACPI_FUTURE_USAGE */
  268. static int acpi_ec_transaction(union acpi_ec *ec, u8 command,
  269. const u8 *wdata, unsigned wdata_len,
  270. u8 *rdata, unsigned rdata_len)
  271. {
  272. if (acpi_ec_poll_mode)
  273. return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
  274. else
  275. return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
  276. }
  277. static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
  278. {
  279. int result;
  280. u8 d;
  281. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
  282. *data = d;
  283. return result;
  284. }
  285. static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
  286. {
  287. u8 wdata[2] = { address, data };
  288. return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
  289. }
  290. static int acpi_ec_transaction_unlocked(union acpi_ec *ec, u8 command,
  291. const u8 *wdata, unsigned wdata_len,
  292. u8 *rdata, unsigned rdata_len)
  293. {
  294. int result;
  295. acpi_hw_low_level_write(8, command, &ec->common.command_addr);
  296. for (; wdata_len > 0; wdata_len --) {
  297. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  298. if (result)
  299. return result;
  300. acpi_hw_low_level_write(8, *(wdata++), &ec->common.data_addr);
  301. }
  302. if (command == ACPI_EC_COMMAND_WRITE) {
  303. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  304. if (result)
  305. return result;
  306. }
  307. for (; rdata_len > 0; rdata_len --) {
  308. u32 d;
  309. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  310. if (result)
  311. return result;
  312. acpi_hw_low_level_read(8, &d, &ec->common.data_addr);
  313. *(rdata++) = (u8) d;
  314. }
  315. return 0;
  316. }
  317. static int acpi_ec_poll_transaction(union acpi_ec *ec, u8 command,
  318. const u8 *wdata, unsigned wdata_len,
  319. u8 *rdata, unsigned rdata_len)
  320. {
  321. acpi_status status = AE_OK;
  322. int result;
  323. u32 glk = 0;
  324. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  325. return -EINVAL;
  326. if (rdata)
  327. memset(rdata, 0, rdata_len);
  328. if (ec->common.global_lock) {
  329. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  330. if (ACPI_FAILURE(status))
  331. return -ENODEV;
  332. }
  333. if (down_interruptible(&ec->poll.sem)) {
  334. result = -ERESTARTSYS;
  335. goto end_nosem;
  336. }
  337. result = acpi_ec_transaction_unlocked(ec, command,
  338. wdata, wdata_len,
  339. rdata, rdata_len);
  340. up(&ec->poll.sem);
  341. end_nosem:
  342. if (ec->common.global_lock)
  343. acpi_release_global_lock(glk);
  344. return result;
  345. }
  346. static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
  347. const u8 *wdata, unsigned wdata_len,
  348. u8 *rdata, unsigned rdata_len)
  349. {
  350. int status;
  351. u32 glk;
  352. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  353. return -EINVAL;
  354. if (rdata)
  355. memset(rdata, 0, rdata_len);
  356. if (ec->common.global_lock) {
  357. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  358. if (ACPI_FAILURE(status))
  359. return -ENODEV;
  360. }
  361. WARN_ON(in_interrupt());
  362. down(&ec->intr.sem);
  363. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  364. if (status) {
  365. printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
  366. goto end;
  367. }
  368. status = acpi_ec_transaction_unlocked(ec, command,
  369. wdata, wdata_len,
  370. rdata, rdata_len);
  371. end:
  372. up(&ec->intr.sem);
  373. if (ec->common.global_lock)
  374. acpi_release_global_lock(glk);
  375. return status;
  376. }
  377. /*
  378. * Externally callable EC access functions. For now, assume 1 EC only
  379. */
  380. int ec_read(u8 addr, u8 * val)
  381. {
  382. union acpi_ec *ec;
  383. int err;
  384. u32 temp_data;
  385. if (!first_ec)
  386. return -ENODEV;
  387. ec = acpi_driver_data(first_ec);
  388. err = acpi_ec_read(ec, addr, &temp_data);
  389. if (!err) {
  390. *val = temp_data;
  391. return 0;
  392. } else
  393. return err;
  394. }
  395. EXPORT_SYMBOL(ec_read);
  396. int ec_write(u8 addr, u8 val)
  397. {
  398. union acpi_ec *ec;
  399. int err;
  400. if (!first_ec)
  401. return -ENODEV;
  402. ec = acpi_driver_data(first_ec);
  403. err = acpi_ec_write(ec, addr, val);
  404. return err;
  405. }
  406. EXPORT_SYMBOL(ec_write);
  407. extern int ec_transaction(u8 command,
  408. const u8 *wdata, unsigned wdata_len,
  409. u8 *rdata, unsigned rdata_len)
  410. {
  411. union acpi_ec *ec;
  412. if (!first_ec)
  413. return -ENODEV;
  414. ec = acpi_driver_data(first_ec);
  415. return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
  416. }
  417. EXPORT_SYMBOL(ec_transaction);
  418. static int acpi_ec_query(union acpi_ec *ec, u32 * data) {
  419. int result;
  420. u8 d;
  421. if (!ec || !data)
  422. return -EINVAL;
  423. /*
  424. * Query the EC to find out which _Qxx method we need to evaluate.
  425. * Note that successful completion of the query causes the ACPI_EC_SCI
  426. * bit to be cleared (and thus clearing the interrupt source).
  427. */
  428. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
  429. if (result)
  430. return result;
  431. if (!d)
  432. return -ENODATA;
  433. *data = d;
  434. return 0;
  435. }
  436. /* --------------------------------------------------------------------------
  437. Event Management
  438. -------------------------------------------------------------------------- */
  439. union acpi_ec_query_data {
  440. acpi_handle handle;
  441. u8 data;
  442. };
  443. static void acpi_ec_gpe_query(void *ec_cxt)
  444. {
  445. if (acpi_ec_poll_mode)
  446. acpi_ec_gpe_poll_query(ec_cxt);
  447. else
  448. acpi_ec_gpe_intr_query(ec_cxt);
  449. }
  450. static void acpi_ec_gpe_poll_query(void *ec_cxt)
  451. {
  452. union acpi_ec *ec = (union acpi_ec *)ec_cxt;
  453. u32 value = 0;
  454. static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
  455. const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
  456. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  457. };
  458. if (!ec_cxt)
  459. goto end;
  460. if (down_interruptible (&ec->poll.sem)) {
  461. return;
  462. }
  463. acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
  464. up(&ec->poll.sem);
  465. /* TBD: Implement asynch events!
  466. * NOTE: All we care about are EC-SCI's. Other EC events are
  467. * handled via polling (yuck!). This is because some systems
  468. * treat EC-SCIs as level (versus EDGE!) triggered, preventing
  469. * a purely interrupt-driven approach (grumble, grumble).
  470. */
  471. if (!(value & ACPI_EC_FLAG_SCI))
  472. goto end;
  473. if (acpi_ec_query(ec, &value))
  474. goto end;
  475. object_name[2] = hex[((value >> 4) & 0x0F)];
  476. object_name[3] = hex[(value & 0x0F)];
  477. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  478. acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
  479. end:
  480. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  481. }
  482. static void acpi_ec_gpe_intr_query(void *ec_cxt)
  483. {
  484. union acpi_ec *ec = (union acpi_ec *)ec_cxt;
  485. u32 value;
  486. int result = -ENODATA;
  487. static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
  488. const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
  489. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  490. };
  491. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
  492. result = acpi_ec_query(ec, &value);
  493. if (result)
  494. goto end;
  495. object_name[2] = hex[((value >> 4) & 0x0F)];
  496. object_name[3] = hex[(value & 0x0F)];
  497. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  498. acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
  499. end:
  500. atomic_dec(&ec->intr.pending_gpe);
  501. return;
  502. }
  503. static u32 acpi_ec_gpe_handler(void *data)
  504. {
  505. if (acpi_ec_poll_mode)
  506. return acpi_ec_gpe_poll_handler(data);
  507. else
  508. return acpi_ec_gpe_intr_handler(data);
  509. }
  510. static u32 acpi_ec_gpe_poll_handler(void *data)
  511. {
  512. acpi_status status = AE_OK;
  513. union acpi_ec *ec = (union acpi_ec *)data;
  514. if (!ec)
  515. return ACPI_INTERRUPT_NOT_HANDLED;
  516. acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  517. status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
  518. if (status == AE_OK)
  519. return ACPI_INTERRUPT_HANDLED;
  520. else
  521. return ACPI_INTERRUPT_NOT_HANDLED;
  522. }
  523. static u32 acpi_ec_gpe_intr_handler(void *data)
  524. {
  525. acpi_status status = AE_OK;
  526. u32 value;
  527. union acpi_ec *ec = (union acpi_ec *)data;
  528. if (!ec)
  529. return ACPI_INTERRUPT_NOT_HANDLED;
  530. acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  531. value = acpi_ec_read_status(ec);
  532. switch (ec->intr.expect_event) {
  533. case ACPI_EC_EVENT_OBF:
  534. if (!(value & ACPI_EC_FLAG_OBF))
  535. break;
  536. ec->intr.expect_event = 0;
  537. wake_up(&ec->intr.wait);
  538. break;
  539. case ACPI_EC_EVENT_IBE:
  540. if ((value & ACPI_EC_FLAG_IBF))
  541. break;
  542. ec->intr.expect_event = 0;
  543. wake_up(&ec->intr.wait);
  544. break;
  545. default:
  546. break;
  547. }
  548. if (value & ACPI_EC_FLAG_SCI) {
  549. atomic_add(1, &ec->intr.pending_gpe);
  550. status = acpi_os_execute(OSL_EC_BURST_HANDLER,
  551. acpi_ec_gpe_query, ec);
  552. return status == AE_OK ?
  553. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  554. }
  555. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
  556. return status == AE_OK ?
  557. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  558. }
  559. /* --------------------------------------------------------------------------
  560. Address Space Management
  561. -------------------------------------------------------------------------- */
  562. static acpi_status
  563. acpi_ec_space_setup(acpi_handle region_handle,
  564. u32 function, void *handler_context, void **return_context)
  565. {
  566. /*
  567. * The EC object is in the handler context and is needed
  568. * when calling the acpi_ec_space_handler.
  569. */
  570. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  571. handler_context : NULL;
  572. return AE_OK;
  573. }
  574. static acpi_status
  575. acpi_ec_space_handler(u32 function,
  576. acpi_physical_address address,
  577. u32 bit_width,
  578. acpi_integer * value,
  579. void *handler_context, void *region_context)
  580. {
  581. int result = 0;
  582. union acpi_ec *ec = NULL;
  583. u64 temp = *value;
  584. acpi_integer f_v = 0;
  585. int i = 0;
  586. if ((address > 0xFF) || !value || !handler_context)
  587. return AE_BAD_PARAMETER;
  588. if (bit_width != 8 && acpi_strict) {
  589. printk(KERN_WARNING PREFIX
  590. "acpi_ec_space_handler: bit_width should be 8\n");
  591. return AE_BAD_PARAMETER;
  592. }
  593. ec = (union acpi_ec *)handler_context;
  594. next_byte:
  595. switch (function) {
  596. case ACPI_READ:
  597. temp = 0;
  598. result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
  599. break;
  600. case ACPI_WRITE:
  601. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  602. break;
  603. default:
  604. result = -EINVAL;
  605. goto out;
  606. break;
  607. }
  608. bit_width -= 8;
  609. if (bit_width) {
  610. if (function == ACPI_READ)
  611. f_v |= temp << 8 * i;
  612. if (function == ACPI_WRITE)
  613. temp >>= 8;
  614. i++;
  615. address++;
  616. goto next_byte;
  617. }
  618. if (function == ACPI_READ) {
  619. f_v |= temp << 8 * i;
  620. *value = f_v;
  621. }
  622. out:
  623. switch (result) {
  624. case -EINVAL:
  625. return AE_BAD_PARAMETER;
  626. break;
  627. case -ENODEV:
  628. return AE_NOT_FOUND;
  629. break;
  630. case -ETIME:
  631. return AE_TIME;
  632. break;
  633. default:
  634. return AE_OK;
  635. }
  636. }
  637. /* --------------------------------------------------------------------------
  638. FS Interface (/proc)
  639. -------------------------------------------------------------------------- */
  640. static struct proc_dir_entry *acpi_ec_dir;
  641. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  642. {
  643. union acpi_ec *ec = (union acpi_ec *)seq->private;
  644. if (!ec)
  645. goto end;
  646. seq_printf(seq, "gpe bit: 0x%02x\n",
  647. (u32) ec->common.gpe_bit);
  648. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  649. (u32) ec->common.status_addr.address,
  650. (u32) ec->common.data_addr.address);
  651. seq_printf(seq, "use global lock: %s\n",
  652. ec->common.global_lock ? "yes" : "no");
  653. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  654. end:
  655. return 0;
  656. }
  657. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  658. {
  659. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  660. }
  661. static const struct file_operations acpi_ec_info_ops = {
  662. .open = acpi_ec_info_open_fs,
  663. .read = seq_read,
  664. .llseek = seq_lseek,
  665. .release = single_release,
  666. .owner = THIS_MODULE,
  667. };
  668. static int acpi_ec_add_fs(struct acpi_device *device)
  669. {
  670. struct proc_dir_entry *entry = NULL;
  671. if (!acpi_device_dir(device)) {
  672. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  673. acpi_ec_dir);
  674. if (!acpi_device_dir(device))
  675. return -ENODEV;
  676. }
  677. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  678. acpi_device_dir(device));
  679. if (!entry)
  680. return -ENODEV;
  681. else {
  682. entry->proc_fops = &acpi_ec_info_ops;
  683. entry->data = acpi_driver_data(device);
  684. entry->owner = THIS_MODULE;
  685. }
  686. return 0;
  687. }
  688. static int acpi_ec_remove_fs(struct acpi_device *device)
  689. {
  690. if (acpi_device_dir(device)) {
  691. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  692. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  693. acpi_device_dir(device) = NULL;
  694. }
  695. return 0;
  696. }
  697. /* --------------------------------------------------------------------------
  698. Driver Interface
  699. -------------------------------------------------------------------------- */
  700. static int acpi_ec_poll_add(struct acpi_device *device)
  701. {
  702. int result = 0;
  703. acpi_status status = AE_OK;
  704. union acpi_ec *ec = NULL;
  705. if (!device)
  706. return -EINVAL;
  707. ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  708. if (!ec)
  709. return -ENOMEM;
  710. memset(ec, 0, sizeof(union acpi_ec));
  711. ec->common.handle = device->handle;
  712. ec->common.uid = -1;
  713. init_MUTEX(&ec->poll.sem);
  714. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  715. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  716. acpi_driver_data(device) = ec;
  717. /* Use the global lock for all EC transactions? */
  718. acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
  719. &ec->common.global_lock);
  720. /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
  721. http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
  722. if (ec_ecdt) {
  723. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  724. ACPI_ADR_SPACE_EC,
  725. &acpi_ec_space_handler);
  726. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  727. &acpi_ec_gpe_handler);
  728. kfree(ec_ecdt);
  729. }
  730. /* Get GPE bit assignment (EC events). */
  731. /* TODO: Add support for _GPE returning a package */
  732. status =
  733. acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
  734. &ec->common.gpe_bit);
  735. if (ACPI_FAILURE(status)) {
  736. ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit"));
  737. result = -ENODEV;
  738. goto end;
  739. }
  740. result = acpi_ec_add_fs(device);
  741. if (result)
  742. goto end;
  743. printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
  744. acpi_device_name(device), acpi_device_bid(device),
  745. (u32) ec->common.gpe_bit);
  746. if (!first_ec)
  747. first_ec = device;
  748. end:
  749. if (result)
  750. kfree(ec);
  751. return result;
  752. }
  753. static int acpi_ec_intr_add(struct acpi_device *device)
  754. {
  755. int result = 0;
  756. acpi_status status = AE_OK;
  757. union acpi_ec *ec = NULL;
  758. if (!device)
  759. return -EINVAL;
  760. ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  761. if (!ec)
  762. return -ENOMEM;
  763. memset(ec, 0, sizeof(union acpi_ec));
  764. ec->common.handle = device->handle;
  765. ec->common.uid = -1;
  766. atomic_set(&ec->intr.pending_gpe, 0);
  767. atomic_set(&ec->intr.leaving_burst, 1);
  768. init_MUTEX(&ec->intr.sem);
  769. init_waitqueue_head(&ec->intr.wait);
  770. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  771. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  772. acpi_driver_data(device) = ec;
  773. /* Use the global lock for all EC transactions? */
  774. acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
  775. &ec->common.global_lock);
  776. /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
  777. http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
  778. if (ec_ecdt) {
  779. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  780. ACPI_ADR_SPACE_EC,
  781. &acpi_ec_space_handler);
  782. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  783. &acpi_ec_gpe_handler);
  784. kfree(ec_ecdt);
  785. }
  786. /* Get GPE bit assignment (EC events). */
  787. /* TODO: Add support for _GPE returning a package */
  788. status =
  789. acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
  790. &ec->common.gpe_bit);
  791. if (ACPI_FAILURE(status)) {
  792. printk(KERN_ERR PREFIX "Obtaining GPE bit assignment\n");
  793. result = -ENODEV;
  794. goto end;
  795. }
  796. result = acpi_ec_add_fs(device);
  797. if (result)
  798. goto end;
  799. printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
  800. acpi_device_name(device), acpi_device_bid(device),
  801. (u32) ec->common.gpe_bit);
  802. if (!first_ec)
  803. first_ec = device;
  804. end:
  805. if (result)
  806. kfree(ec);
  807. return result;
  808. }
  809. static int acpi_ec_remove(struct acpi_device *device, int type)
  810. {
  811. union acpi_ec *ec = NULL;
  812. if (!device)
  813. return -EINVAL;
  814. ec = acpi_driver_data(device);
  815. acpi_ec_remove_fs(device);
  816. kfree(ec);
  817. return 0;
  818. }
  819. static acpi_status
  820. acpi_ec_io_ports(struct acpi_resource *resource, void *context)
  821. {
  822. union acpi_ec *ec = (union acpi_ec *)context;
  823. struct acpi_generic_address *addr;
  824. if (resource->type != ACPI_RESOURCE_TYPE_IO) {
  825. return AE_OK;
  826. }
  827. /*
  828. * The first address region returned is the data port, and
  829. * the second address region returned is the status/command
  830. * port.
  831. */
  832. if (ec->common.data_addr.register_bit_width == 0) {
  833. addr = &ec->common.data_addr;
  834. } else if (ec->common.command_addr.register_bit_width == 0) {
  835. addr = &ec->common.command_addr;
  836. } else {
  837. return AE_CTRL_TERMINATE;
  838. }
  839. addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  840. addr->register_bit_width = 8;
  841. addr->register_bit_offset = 0;
  842. addr->address = resource->data.io.minimum;
  843. return AE_OK;
  844. }
  845. static int acpi_ec_start(struct acpi_device *device)
  846. {
  847. acpi_status status = AE_OK;
  848. union acpi_ec *ec = NULL;
  849. if (!device)
  850. return -EINVAL;
  851. ec = acpi_driver_data(device);
  852. if (!ec)
  853. return -EINVAL;
  854. /*
  855. * Get I/O port addresses. Convert to GAS format.
  856. */
  857. status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
  858. acpi_ec_io_ports, ec);
  859. if (ACPI_FAILURE(status)
  860. || ec->common.command_addr.register_bit_width == 0) {
  861. printk(KERN_ERR PREFIX "Error getting I/O port addresses\n");
  862. return -ENODEV;
  863. }
  864. ec->common.status_addr = ec->common.command_addr;
  865. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
  866. (u32) ec->common.gpe_bit,
  867. (u32) ec->common.command_addr.address,
  868. (u32) ec->common.data_addr.address));
  869. /*
  870. * Install GPE handler
  871. */
  872. status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
  873. ACPI_GPE_EDGE_TRIGGERED,
  874. &acpi_ec_gpe_handler, ec);
  875. if (ACPI_FAILURE(status)) {
  876. return -ENODEV;
  877. }
  878. acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  879. acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
  880. status = acpi_install_address_space_handler(ec->common.handle,
  881. ACPI_ADR_SPACE_EC,
  882. &acpi_ec_space_handler,
  883. &acpi_ec_space_setup, ec);
  884. if (ACPI_FAILURE(status)) {
  885. acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
  886. &acpi_ec_gpe_handler);
  887. return -ENODEV;
  888. }
  889. return AE_OK;
  890. }
  891. static int acpi_ec_stop(struct acpi_device *device, int type)
  892. {
  893. acpi_status status = AE_OK;
  894. union acpi_ec *ec = NULL;
  895. if (!device)
  896. return -EINVAL;
  897. ec = acpi_driver_data(device);
  898. status = acpi_remove_address_space_handler(ec->common.handle,
  899. ACPI_ADR_SPACE_EC,
  900. &acpi_ec_space_handler);
  901. if (ACPI_FAILURE(status))
  902. return -ENODEV;
  903. status =
  904. acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
  905. &acpi_ec_gpe_handler);
  906. if (ACPI_FAILURE(status))
  907. return -ENODEV;
  908. return 0;
  909. }
  910. static acpi_status __init
  911. acpi_fake_ecdt_callback(acpi_handle handle,
  912. u32 Level, void *context, void **retval)
  913. {
  914. if (acpi_ec_poll_mode)
  915. return acpi_fake_ecdt_poll_callback(handle,
  916. Level, context, retval);
  917. else
  918. return acpi_fake_ecdt_intr_callback(handle,
  919. Level, context, retval);
  920. }
  921. static acpi_status __init
  922. acpi_fake_ecdt_poll_callback(acpi_handle handle,
  923. u32 Level, void *context, void **retval)
  924. {
  925. acpi_status status;
  926. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  927. acpi_ec_io_ports, ec_ecdt);
  928. if (ACPI_FAILURE(status))
  929. return status;
  930. ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
  931. ec_ecdt->common.uid = -1;
  932. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
  933. status =
  934. acpi_evaluate_integer(handle, "_GPE", NULL,
  935. &ec_ecdt->common.gpe_bit);
  936. if (ACPI_FAILURE(status))
  937. return status;
  938. init_MUTEX(&ec_ecdt->poll.sem);
  939. ec_ecdt->common.global_lock = TRUE;
  940. ec_ecdt->common.handle = handle;
  941. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  942. (u32) ec_ecdt->common.gpe_bit,
  943. (u32) ec_ecdt->common.command_addr.address,
  944. (u32) ec_ecdt->common.data_addr.address);
  945. return AE_CTRL_TERMINATE;
  946. }
  947. static acpi_status __init
  948. acpi_fake_ecdt_intr_callback(acpi_handle handle,
  949. u32 Level, void *context, void **retval)
  950. {
  951. acpi_status status;
  952. init_MUTEX(&ec_ecdt->intr.sem);
  953. init_waitqueue_head(&ec_ecdt->intr.wait);
  954. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  955. acpi_ec_io_ports, ec_ecdt);
  956. if (ACPI_FAILURE(status))
  957. return status;
  958. ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
  959. ec_ecdt->common.uid = -1;
  960. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
  961. status =
  962. acpi_evaluate_integer(handle, "_GPE", NULL,
  963. &ec_ecdt->common.gpe_bit);
  964. if (ACPI_FAILURE(status))
  965. return status;
  966. ec_ecdt->common.global_lock = TRUE;
  967. ec_ecdt->common.handle = handle;
  968. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  969. (u32) ec_ecdt->common.gpe_bit,
  970. (u32) ec_ecdt->common.command_addr.address,
  971. (u32) ec_ecdt->common.data_addr.address);
  972. return AE_CTRL_TERMINATE;
  973. }
  974. /*
  975. * Some BIOS (such as some from Gateway laptops) access EC region very early
  976. * such as in BAT0._INI or EC._INI before an EC device is found and
  977. * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
  978. * required, but if EC regison is accessed early, it is required.
  979. * The routine tries to workaround the BIOS bug by pre-scan EC device
  980. * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
  981. * op region (since _REG isn't invoked yet). The assumption is true for
  982. * all systems found.
  983. */
  984. static int __init acpi_ec_fake_ecdt(void)
  985. {
  986. acpi_status status;
  987. int ret = 0;
  988. printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
  989. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  990. if (!ec_ecdt) {
  991. ret = -ENOMEM;
  992. goto error;
  993. }
  994. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  995. status = acpi_get_devices(ACPI_EC_HID,
  996. acpi_fake_ecdt_callback, NULL, NULL);
  997. if (ACPI_FAILURE(status)) {
  998. kfree(ec_ecdt);
  999. ec_ecdt = NULL;
  1000. ret = -ENODEV;
  1001. goto error;
  1002. }
  1003. return 0;
  1004. error:
  1005. printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
  1006. return ret;
  1007. }
  1008. static int __init acpi_ec_get_real_ecdt(void)
  1009. {
  1010. if (acpi_ec_poll_mode)
  1011. return acpi_ec_poll_get_real_ecdt();
  1012. else
  1013. return acpi_ec_intr_get_real_ecdt();
  1014. }
  1015. static int __init acpi_ec_poll_get_real_ecdt(void)
  1016. {
  1017. acpi_status status;
  1018. struct acpi_table_ecdt *ecdt_ptr;
  1019. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  1020. (struct acpi_table_header **)
  1021. &ecdt_ptr);
  1022. if (ACPI_FAILURE(status))
  1023. return -ENODEV;
  1024. printk(KERN_INFO PREFIX "Found ECDT\n");
  1025. /*
  1026. * Generate a temporary ec context to use until the namespace is scanned
  1027. */
  1028. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1029. if (!ec_ecdt)
  1030. return -ENOMEM;
  1031. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1032. ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
  1033. ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
  1034. ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
  1035. ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
  1036. init_MUTEX(&ec_ecdt->poll.sem);
  1037. /* use the GL just to be safe */
  1038. ec_ecdt->common.global_lock = TRUE;
  1039. ec_ecdt->common.uid = ecdt_ptr->uid;
  1040. status =
  1041. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
  1042. if (ACPI_FAILURE(status)) {
  1043. goto error;
  1044. }
  1045. return 0;
  1046. error:
  1047. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1048. kfree(ec_ecdt);
  1049. ec_ecdt = NULL;
  1050. return -ENODEV;
  1051. }
  1052. static int __init acpi_ec_intr_get_real_ecdt(void)
  1053. {
  1054. acpi_status status;
  1055. struct acpi_table_ecdt *ecdt_ptr;
  1056. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  1057. (struct acpi_table_header **)
  1058. &ecdt_ptr);
  1059. if (ACPI_FAILURE(status))
  1060. return -ENODEV;
  1061. printk(KERN_INFO PREFIX "Found ECDT\n");
  1062. /*
  1063. * Generate a temporary ec context to use until the namespace is scanned
  1064. */
  1065. ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
  1066. if (!ec_ecdt)
  1067. return -ENOMEM;
  1068. memset(ec_ecdt, 0, sizeof(union acpi_ec));
  1069. init_MUTEX(&ec_ecdt->intr.sem);
  1070. init_waitqueue_head(&ec_ecdt->intr.wait);
  1071. ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
  1072. ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
  1073. ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
  1074. ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
  1075. /* use the GL just to be safe */
  1076. ec_ecdt->common.global_lock = TRUE;
  1077. ec_ecdt->common.uid = ecdt_ptr->uid;
  1078. status =
  1079. acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
  1080. if (ACPI_FAILURE(status)) {
  1081. goto error;
  1082. }
  1083. return 0;
  1084. error:
  1085. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1086. kfree(ec_ecdt);
  1087. ec_ecdt = NULL;
  1088. return -ENODEV;
  1089. }
  1090. static int __initdata acpi_fake_ecdt_enabled;
  1091. int __init acpi_ec_ecdt_probe(void)
  1092. {
  1093. acpi_status status;
  1094. int ret;
  1095. ret = acpi_ec_get_real_ecdt();
  1096. /* Try to make a fake ECDT */
  1097. if (ret && acpi_fake_ecdt_enabled) {
  1098. ret = acpi_ec_fake_ecdt();
  1099. }
  1100. if (ret)
  1101. return 0;
  1102. /*
  1103. * Install GPE handler
  1104. */
  1105. status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  1106. ACPI_GPE_EDGE_TRIGGERED,
  1107. &acpi_ec_gpe_handler, ec_ecdt);
  1108. if (ACPI_FAILURE(status)) {
  1109. goto error;
  1110. }
  1111. acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  1112. acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
  1113. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  1114. ACPI_ADR_SPACE_EC,
  1115. &acpi_ec_space_handler,
  1116. &acpi_ec_space_setup,
  1117. ec_ecdt);
  1118. if (ACPI_FAILURE(status)) {
  1119. acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
  1120. &acpi_ec_gpe_handler);
  1121. goto error;
  1122. }
  1123. return 0;
  1124. error:
  1125. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  1126. kfree(ec_ecdt);
  1127. ec_ecdt = NULL;
  1128. return -ENODEV;
  1129. }
  1130. static int __init acpi_ec_init(void)
  1131. {
  1132. int result = 0;
  1133. if (acpi_disabled)
  1134. return 0;
  1135. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  1136. if (!acpi_ec_dir)
  1137. return -ENODEV;
  1138. /* Now register the driver for the EC */
  1139. result = acpi_bus_register_driver(&acpi_ec_driver);
  1140. if (result < 0) {
  1141. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  1142. return -ENODEV;
  1143. }
  1144. return result;
  1145. }
  1146. subsys_initcall(acpi_ec_init);
  1147. /* EC driver currently not unloadable */
  1148. #if 0
  1149. static void __exit acpi_ec_exit(void)
  1150. {
  1151. acpi_bus_unregister_driver(&acpi_ec_driver);
  1152. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  1153. return;
  1154. }
  1155. #endif /* 0 */
  1156. static int __init acpi_fake_ecdt_setup(char *str)
  1157. {
  1158. acpi_fake_ecdt_enabled = 1;
  1159. return 1;
  1160. }
  1161. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
  1162. static int __init acpi_ec_set_intr_mode(char *str)
  1163. {
  1164. int intr;
  1165. if (!get_option(&str, &intr))
  1166. return 0;
  1167. if (intr) {
  1168. acpi_ec_poll_mode = EC_INTR;
  1169. acpi_ec_driver.ops.add = acpi_ec_intr_add;
  1170. } else {
  1171. acpi_ec_poll_mode = EC_POLL;
  1172. acpi_ec_driver.ops.add = acpi_ec_poll_add;
  1173. }
  1174. printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
  1175. return 1;
  1176. }
  1177. __setup("ec_intr=", acpi_ec_set_intr_mode);