ec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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_COMMAND_READ 0x80
  55. #define ACPI_EC_COMMAND_WRITE 0x81
  56. #define ACPI_EC_BURST_ENABLE 0x82
  57. #define ACPI_EC_BURST_DISABLE 0x83
  58. #define ACPI_EC_COMMAND_QUERY 0x84
  59. static int acpi_ec_add (struct acpi_device *device);
  60. static int acpi_ec_remove (struct acpi_device *device, int type);
  61. static int acpi_ec_start (struct acpi_device *device);
  62. static int acpi_ec_stop (struct acpi_device *device, int type);
  63. static struct acpi_driver acpi_ec_driver = {
  64. .name = ACPI_EC_DRIVER_NAME,
  65. .class = ACPI_EC_CLASS,
  66. .ids = ACPI_EC_HID,
  67. .ops = {
  68. .add = acpi_ec_add,
  69. .remove = acpi_ec_remove,
  70. .start = acpi_ec_start,
  71. .stop = acpi_ec_stop,
  72. },
  73. };
  74. struct acpi_ec {
  75. acpi_handle handle;
  76. unsigned long uid;
  77. unsigned long gpe_bit;
  78. struct acpi_generic_address status_addr;
  79. struct acpi_generic_address command_addr;
  80. struct acpi_generic_address data_addr;
  81. unsigned long global_lock;
  82. unsigned int expect_event;
  83. atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
  84. atomic_t pending_gpe;
  85. struct semaphore sem;
  86. wait_queue_head_t wait;
  87. };
  88. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  89. static struct acpi_ec *ec_ecdt;
  90. /* External interfaces use first EC only, so remember */
  91. static struct acpi_device *first_ec;
  92. /* --------------------------------------------------------------------------
  93. Transaction Management
  94. -------------------------------------------------------------------------- */
  95. static inline u32 acpi_ec_read_status(struct acpi_ec *ec)
  96. {
  97. u32 status = 0;
  98. acpi_hw_low_level_read(8, &status, &ec->status_addr);
  99. return status;
  100. }
  101. static int acpi_ec_wait(struct acpi_ec *ec, unsigned int event)
  102. {
  103. int result = 0;
  104. ACPI_FUNCTION_TRACE("acpi_ec_wait");
  105. ec->expect_event = event;
  106. smp_mb();
  107. result = wait_event_interruptible_timeout(ec->wait,
  108. !ec->expect_event,
  109. msecs_to_jiffies(ACPI_EC_DELAY));
  110. ec->expect_event = 0;
  111. smp_mb();
  112. if (result < 0){
  113. ACPI_DEBUG_PRINT((ACPI_DB_ERROR," result = %d ", result));
  114. return_VALUE(result);
  115. }
  116. /*
  117. * Verify that the event in question has actually happened by
  118. * querying EC status. Do the check even if operation timed-out
  119. * to make sure that we did not miss interrupt.
  120. */
  121. switch (event) {
  122. case ACPI_EC_EVENT_OBF:
  123. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
  124. return_VALUE(0);
  125. break;
  126. case ACPI_EC_EVENT_IBE:
  127. if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
  128. return_VALUE(0);
  129. break;
  130. }
  131. return_VALUE(-ETIME);
  132. }
  133. static int
  134. acpi_ec_enter_burst_mode (
  135. struct acpi_ec *ec)
  136. {
  137. u32 tmp = 0;
  138. int status = 0;
  139. ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
  140. status = acpi_ec_read_status(ec);
  141. if (status != -EINVAL &&
  142. !(status & ACPI_EC_FLAG_BURST)){
  143. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"entering burst mode \n"));
  144. acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
  145. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  146. if (status){
  147. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  148. ACPI_DEBUG_PRINT((ACPI_DB_ERROR," status = %d\n", status));
  149. return_VALUE(-EINVAL);
  150. }
  151. acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
  152. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  153. if(tmp != 0x90 ) {/* Burst ACK byte*/
  154. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"Ack failed \n"));
  155. return_VALUE(-EINVAL);
  156. }
  157. } else
  158. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"already be in burst mode \n"));
  159. atomic_set(&ec->leaving_burst , 0);
  160. return_VALUE(0);
  161. }
  162. static int
  163. acpi_ec_leave_burst_mode (
  164. struct acpi_ec *ec)
  165. {
  166. int status =0;
  167. ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
  168. atomic_set(&ec->leaving_burst , 1);
  169. status = acpi_ec_read_status(ec);
  170. if (status != -EINVAL &&
  171. (status & ACPI_EC_FLAG_BURST)){
  172. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"leaving burst mode\n"));
  173. acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->command_addr);
  174. status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
  175. if (status){
  176. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  177. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->wait fail\n"));
  178. return_VALUE(-EINVAL);
  179. }
  180. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  181. status = acpi_ec_read_status(ec);
  182. if (status != -EINVAL &&
  183. (status & ACPI_EC_FLAG_BURST)) {
  184. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->status fail\n"));
  185. return_VALUE(-EINVAL);
  186. }
  187. }else
  188. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"already be in Non-burst mode \n"));
  189. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"leaving burst mode\n"));
  190. return_VALUE(0);
  191. }
  192. static int
  193. acpi_ec_read (
  194. struct acpi_ec *ec,
  195. u8 address,
  196. u32 *data)
  197. {
  198. int status = 0;
  199. u32 glk;
  200. ACPI_FUNCTION_TRACE("acpi_ec_read");
  201. if (!ec || !data)
  202. return_VALUE(-EINVAL);
  203. retry:
  204. *data = 0;
  205. if (ec->global_lock) {
  206. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  207. if (ACPI_FAILURE(status))
  208. return_VALUE(-ENODEV);
  209. }
  210. WARN_ON(in_interrupt());
  211. down(&ec->sem);
  212. if(acpi_ec_enter_burst_mode(ec))
  213. goto end;
  214. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
  215. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  216. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  217. if (status) {
  218. goto end;
  219. }
  220. acpi_hw_low_level_write(8, address, &ec->data_addr);
  221. status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  222. if (status){
  223. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  224. goto end;
  225. }
  226. acpi_hw_low_level_read(8, data, &ec->data_addr);
  227. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
  229. *data, address));
  230. end:
  231. acpi_ec_leave_burst_mode(ec);
  232. up(&ec->sem);
  233. if (ec->global_lock)
  234. acpi_release_global_lock(glk);
  235. if(atomic_read(&ec->leaving_burst) == 2){
  236. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
  237. while(!atomic_read(&ec->pending_gpe)){
  238. msleep(1);
  239. }
  240. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  241. goto retry;
  242. }
  243. return_VALUE(status);
  244. }
  245. static int
  246. acpi_ec_write (
  247. struct acpi_ec *ec,
  248. u8 address,
  249. u8 data)
  250. {
  251. int status = 0;
  252. u32 glk;
  253. u32 tmp;
  254. ACPI_FUNCTION_TRACE("acpi_ec_write");
  255. if (!ec)
  256. return_VALUE(-EINVAL);
  257. retry:
  258. if (ec->global_lock) {
  259. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  260. if (ACPI_FAILURE(status))
  261. return_VALUE(-ENODEV);
  262. }
  263. WARN_ON(in_interrupt());
  264. down(&ec->sem);
  265. if(acpi_ec_enter_burst_mode(ec))
  266. goto end;
  267. status = acpi_ec_read_status(ec);
  268. if (status != -EINVAL &&
  269. !(status & ACPI_EC_FLAG_BURST)){
  270. acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
  271. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  272. if (status)
  273. goto end;
  274. acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
  275. if(tmp != 0x90 ) /* Burst ACK byte*/
  276. goto end;
  277. }
  278. /*Now we are in burst mode*/
  279. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
  280. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  281. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  282. if (status){
  283. goto end;
  284. }
  285. acpi_hw_low_level_write(8, address, &ec->data_addr);
  286. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  287. if (status){
  288. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  289. goto end;
  290. }
  291. acpi_hw_low_level_write(8, data, &ec->data_addr);
  292. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  293. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  294. if (status)
  295. goto end;
  296. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
  297. data, address));
  298. end:
  299. acpi_ec_leave_burst_mode(ec);
  300. up(&ec->sem);
  301. if (ec->global_lock)
  302. acpi_release_global_lock(glk);
  303. if(atomic_read(&ec->leaving_burst) == 2){
  304. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
  305. while(!atomic_read(&ec->pending_gpe)){
  306. msleep(1);
  307. }
  308. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  309. goto retry;
  310. }
  311. return_VALUE(status);
  312. }
  313. /*
  314. * Externally callable EC access functions. For now, assume 1 EC only
  315. */
  316. int
  317. ec_read(u8 addr, u8 *val)
  318. {
  319. struct acpi_ec *ec;
  320. int err;
  321. u32 temp_data;
  322. if (!first_ec)
  323. return -ENODEV;
  324. ec = acpi_driver_data(first_ec);
  325. err = acpi_ec_read(ec, addr, &temp_data);
  326. if (!err) {
  327. *val = temp_data;
  328. return 0;
  329. }
  330. else
  331. return err;
  332. }
  333. EXPORT_SYMBOL(ec_read);
  334. int
  335. ec_write(u8 addr, u8 val)
  336. {
  337. struct acpi_ec *ec;
  338. int err;
  339. if (!first_ec)
  340. return -ENODEV;
  341. ec = acpi_driver_data(first_ec);
  342. err = acpi_ec_write(ec, addr, val);
  343. return err;
  344. }
  345. EXPORT_SYMBOL(ec_write);
  346. static int
  347. acpi_ec_query (
  348. struct acpi_ec *ec,
  349. u32 *data)
  350. {
  351. int status = 0;
  352. u32 glk;
  353. ACPI_FUNCTION_TRACE("acpi_ec_query");
  354. if (!ec || !data)
  355. return_VALUE(-EINVAL);
  356. retry:
  357. *data = 0;
  358. if (ec->global_lock) {
  359. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  360. if (ACPI_FAILURE(status))
  361. return_VALUE(-ENODEV);
  362. }
  363. down(&ec->sem);
  364. if(acpi_ec_enter_burst_mode(ec))
  365. goto end;
  366. /*
  367. * Query the EC to find out which _Qxx method we need to evaluate.
  368. * Note that successful completion of the query causes the ACPI_EC_SCI
  369. * bit to be cleared (and thus clearing the interrupt source).
  370. */
  371. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
  372. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  373. if (status){
  374. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  375. goto end;
  376. }
  377. acpi_hw_low_level_read(8, data, &ec->data_addr);
  378. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  379. if (!*data)
  380. status = -ENODATA;
  381. end:
  382. acpi_ec_leave_burst_mode(ec);
  383. up(&ec->sem);
  384. if (ec->global_lock)
  385. acpi_release_global_lock(glk);
  386. if(atomic_read(&ec->leaving_burst) == 2){
  387. ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
  388. while(!atomic_read(&ec->pending_gpe)){
  389. msleep(1);
  390. }
  391. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  392. goto retry;
  393. }
  394. return_VALUE(status);
  395. }
  396. /* --------------------------------------------------------------------------
  397. Event Management
  398. -------------------------------------------------------------------------- */
  399. struct acpi_ec_query_data {
  400. acpi_handle handle;
  401. u8 data;
  402. };
  403. static void
  404. acpi_ec_gpe_query (
  405. void *ec_cxt)
  406. {
  407. struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
  408. u32 value;
  409. int result = -ENODATA;
  410. static char object_name[5] = {'_','Q','0','0','\0'};
  411. const char hex[] = {'0','1','2','3','4','5','6','7',
  412. '8','9','A','B','C','D','E','F'};
  413. ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
  414. if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
  415. result = acpi_ec_query(ec, &value);
  416. if (result)
  417. goto end;
  418. object_name[2] = hex[((value >> 4) & 0x0F)];
  419. object_name[3] = hex[(value & 0x0F)];
  420. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  421. acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
  422. atomic_dec(&ec->pending_gpe);
  423. end:
  424. return;
  425. }
  426. static u32
  427. acpi_ec_gpe_handler (
  428. void *data)
  429. {
  430. acpi_status status = AE_OK;
  431. u32 value;
  432. struct acpi_ec *ec = (struct acpi_ec *) data;
  433. if (!ec)
  434. return ACPI_INTERRUPT_NOT_HANDLED;
  435. acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
  436. value = acpi_ec_read_status(ec);
  437. if((value & ACPI_EC_FLAG_IBF) &&
  438. !(value & ACPI_EC_FLAG_BURST) &&
  439. (atomic_read(&ec->leaving_burst) == 0)) {
  440. /*
  441. * the embedded controller disables
  442. * burst mode for any reason other
  443. * than the burst disable command
  444. * to process critical event.
  445. */
  446. atomic_set(&ec->leaving_burst , 2); /* block current pending transaction
  447. and retry */
  448. wake_up(&ec->wait);
  449. }else {
  450. if ((ec->expect_event == ACPI_EC_EVENT_OBF &&
  451. (value & ACPI_EC_FLAG_OBF)) ||
  452. (ec->expect_event == ACPI_EC_EVENT_IBE &&
  453. !(value & ACPI_EC_FLAG_IBF))) {
  454. ec->expect_event = 0;
  455. wake_up(&ec->wait);
  456. }
  457. }
  458. if (value & ACPI_EC_FLAG_SCI){
  459. atomic_add(1, &ec->pending_gpe) ;
  460. status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
  461. acpi_ec_gpe_query, ec);
  462. }
  463. return status == AE_OK ?
  464. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  465. }
  466. /* --------------------------------------------------------------------------
  467. Address Space Management
  468. -------------------------------------------------------------------------- */
  469. static acpi_status
  470. acpi_ec_space_setup (
  471. acpi_handle region_handle,
  472. u32 function,
  473. void *handler_context,
  474. void **return_context)
  475. {
  476. /*
  477. * The EC object is in the handler context and is needed
  478. * when calling the acpi_ec_space_handler.
  479. */
  480. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  481. handler_context : NULL;
  482. return AE_OK;
  483. }
  484. static acpi_status
  485. acpi_ec_space_handler (
  486. u32 function,
  487. acpi_physical_address address,
  488. u32 bit_width,
  489. acpi_integer *value,
  490. void *handler_context,
  491. void *region_context)
  492. {
  493. int result = 0;
  494. struct acpi_ec *ec = NULL;
  495. u64 temp = *value;
  496. acpi_integer f_v = 0;
  497. int i = 0;
  498. ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
  499. if ((address > 0xFF) || !value || !handler_context)
  500. return_VALUE(AE_BAD_PARAMETER);
  501. if (bit_width != 8 && acpi_strict) {
  502. printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
  503. return_VALUE(AE_BAD_PARAMETER);
  504. }
  505. ec = (struct acpi_ec *) handler_context;
  506. next_byte:
  507. switch (function) {
  508. case ACPI_READ:
  509. temp = 0;
  510. result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
  511. break;
  512. case ACPI_WRITE:
  513. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  514. break;
  515. default:
  516. result = -EINVAL;
  517. goto out;
  518. break;
  519. }
  520. bit_width -= 8;
  521. if (bit_width) {
  522. if (function == ACPI_READ)
  523. f_v |= temp << 8 * i;
  524. if (function == ACPI_WRITE)
  525. temp >>= 8;
  526. i++;
  527. (u8)address ++;
  528. goto next_byte;
  529. }
  530. if (function == ACPI_READ) {
  531. f_v |= temp << 8 * i;
  532. *value = f_v;
  533. }
  534. out:
  535. switch (result) {
  536. case -EINVAL:
  537. return_VALUE(AE_BAD_PARAMETER);
  538. break;
  539. case -ENODEV:
  540. return_VALUE(AE_NOT_FOUND);
  541. break;
  542. case -ETIME:
  543. return_VALUE(AE_TIME);
  544. break;
  545. default:
  546. return_VALUE(AE_OK);
  547. }
  548. }
  549. /* --------------------------------------------------------------------------
  550. FS Interface (/proc)
  551. -------------------------------------------------------------------------- */
  552. static struct proc_dir_entry *acpi_ec_dir;
  553. static int
  554. acpi_ec_read_info (struct seq_file *seq, void *offset)
  555. {
  556. struct acpi_ec *ec = (struct acpi_ec *) seq->private;
  557. ACPI_FUNCTION_TRACE("acpi_ec_read_info");
  558. if (!ec)
  559. goto end;
  560. seq_printf(seq, "gpe bit: 0x%02x\n",
  561. (u32) ec->gpe_bit);
  562. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  563. (u32) ec->status_addr.address, (u32) ec->data_addr.address);
  564. seq_printf(seq, "use global lock: %s\n",
  565. ec->global_lock?"yes":"no");
  566. end:
  567. return_VALUE(0);
  568. }
  569. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  570. {
  571. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  572. }
  573. static struct file_operations acpi_ec_info_ops = {
  574. .open = acpi_ec_info_open_fs,
  575. .read = seq_read,
  576. .llseek = seq_lseek,
  577. .release = single_release,
  578. .owner = THIS_MODULE,
  579. };
  580. static int
  581. acpi_ec_add_fs (
  582. struct acpi_device *device)
  583. {
  584. struct proc_dir_entry *entry;
  585. ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
  586. if (!acpi_device_dir(device)) {
  587. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  588. acpi_ec_dir);
  589. if (!acpi_device_dir(device))
  590. return_VALUE(-ENODEV);
  591. }
  592. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  593. acpi_device_dir(device));
  594. if (!entry)
  595. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  596. "Unable to create '%s' fs entry\n",
  597. ACPI_EC_FILE_INFO));
  598. else {
  599. entry->proc_fops = &acpi_ec_info_ops;
  600. entry->data = acpi_driver_data(device);
  601. entry->owner = THIS_MODULE;
  602. }
  603. return_VALUE(0);
  604. }
  605. static int
  606. acpi_ec_remove_fs (
  607. struct acpi_device *device)
  608. {
  609. ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
  610. if (acpi_device_dir(device)) {
  611. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  612. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  613. acpi_device_dir(device) = NULL;
  614. }
  615. return_VALUE(0);
  616. }
  617. /* --------------------------------------------------------------------------
  618. Driver Interface
  619. -------------------------------------------------------------------------- */
  620. static int
  621. acpi_ec_add (
  622. struct acpi_device *device)
  623. {
  624. int result;
  625. acpi_status status;
  626. struct acpi_ec *ec;
  627. unsigned long uid;
  628. ACPI_FUNCTION_TRACE("acpi_ec_add");
  629. if (!device)
  630. return_VALUE(-EINVAL);
  631. ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  632. if (!ec)
  633. return_VALUE(-ENOMEM);
  634. memset(ec, 0, sizeof(struct acpi_ec));
  635. ec->handle = device->handle;
  636. ec->uid = -1;
  637. atomic_set(&ec->pending_gpe, 0);
  638. atomic_set(&ec->leaving_burst , 1);
  639. init_MUTEX(&ec->sem);
  640. init_waitqueue_head(&ec->wait);
  641. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  642. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  643. acpi_driver_data(device) = ec;
  644. /* Use the global lock for all EC transactions? */
  645. acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
  646. /* If our UID matches the UID for the ECDT-enumerated EC,
  647. we now have the *real* EC info, so kill the makeshift one.*/
  648. acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
  649. if (ec_ecdt && ec_ecdt->uid == uid) {
  650. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  651. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
  652. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
  653. kfree(ec_ecdt);
  654. }
  655. /* Get GPE bit assignment (EC events). */
  656. /* TODO: Add support for _GPE returning a package */
  657. status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
  658. if (ACPI_FAILURE(status)) {
  659. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  660. "Error obtaining GPE bit assignment\n"));
  661. result = -ENODEV;
  662. goto end;
  663. }
  664. result = acpi_ec_add_fs(device);
  665. if (result)
  666. goto end;
  667. printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
  668. acpi_device_name(device), acpi_device_bid(device),
  669. (u32) ec->gpe_bit);
  670. if (!first_ec)
  671. first_ec = device;
  672. end:
  673. if (result)
  674. kfree(ec);
  675. return_VALUE(result);
  676. }
  677. static int
  678. acpi_ec_remove (
  679. struct acpi_device *device,
  680. int type)
  681. {
  682. struct acpi_ec *ec;
  683. ACPI_FUNCTION_TRACE("acpi_ec_remove");
  684. if (!device)
  685. return_VALUE(-EINVAL);
  686. ec = acpi_driver_data(device);
  687. acpi_ec_remove_fs(device);
  688. kfree(ec);
  689. return_VALUE(0);
  690. }
  691. static acpi_status
  692. acpi_ec_io_ports (
  693. struct acpi_resource *resource,
  694. void *context)
  695. {
  696. struct acpi_ec *ec = (struct acpi_ec *) context;
  697. struct acpi_generic_address *addr;
  698. if (resource->id != ACPI_RSTYPE_IO) {
  699. return AE_OK;
  700. }
  701. /*
  702. * The first address region returned is the data port, and
  703. * the second address region returned is the status/command
  704. * port.
  705. */
  706. if (ec->data_addr.register_bit_width == 0) {
  707. addr = &ec->data_addr;
  708. } else if (ec->command_addr.register_bit_width == 0) {
  709. addr = &ec->command_addr;
  710. } else {
  711. return AE_CTRL_TERMINATE;
  712. }
  713. addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  714. addr->register_bit_width = 8;
  715. addr->register_bit_offset = 0;
  716. addr->address = resource->data.io.min_base_address;
  717. return AE_OK;
  718. }
  719. static int
  720. acpi_ec_start (
  721. struct acpi_device *device)
  722. {
  723. acpi_status status;
  724. struct acpi_ec *ec;
  725. ACPI_FUNCTION_TRACE("acpi_ec_start");
  726. if (!device)
  727. return_VALUE(-EINVAL);
  728. ec = acpi_driver_data(device);
  729. if (!ec)
  730. return_VALUE(-EINVAL);
  731. /*
  732. * Get I/O port addresses. Convert to GAS format.
  733. */
  734. status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
  735. acpi_ec_io_ports, ec);
  736. if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
  737. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
  738. return_VALUE(-ENODEV);
  739. }
  740. ec->status_addr = ec->command_addr;
  741. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
  742. (u32) ec->gpe_bit, (u32) ec->command_addr.address,
  743. (u32) ec->data_addr.address));
  744. /*
  745. * Install GPE handler
  746. */
  747. status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
  748. ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
  749. if (ACPI_FAILURE(status)) {
  750. return_VALUE(-ENODEV);
  751. }
  752. acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  753. acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
  754. status = acpi_install_address_space_handler (ec->handle,
  755. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
  756. &acpi_ec_space_setup, ec);
  757. if (ACPI_FAILURE(status)) {
  758. acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
  759. return_VALUE(-ENODEV);
  760. }
  761. return_VALUE(AE_OK);
  762. }
  763. static int
  764. acpi_ec_stop (
  765. struct acpi_device *device,
  766. int type)
  767. {
  768. acpi_status status;
  769. struct acpi_ec *ec;
  770. ACPI_FUNCTION_TRACE("acpi_ec_stop");
  771. if (!device)
  772. return_VALUE(-EINVAL);
  773. ec = acpi_driver_data(device);
  774. status = acpi_remove_address_space_handler(ec->handle,
  775. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
  776. if (ACPI_FAILURE(status))
  777. return_VALUE(-ENODEV);
  778. status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
  779. if (ACPI_FAILURE(status))
  780. return_VALUE(-ENODEV);
  781. return_VALUE(0);
  782. }
  783. static acpi_status __init
  784. acpi_fake_ecdt_callback (
  785. acpi_handle handle,
  786. u32 Level,
  787. void *context,
  788. void **retval)
  789. {
  790. acpi_status status;
  791. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  792. acpi_ec_io_ports, ec_ecdt);
  793. if (ACPI_FAILURE(status))
  794. return status;
  795. ec_ecdt->status_addr = ec_ecdt->command_addr;
  796. ec_ecdt->uid = -1;
  797. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
  798. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
  799. if (ACPI_FAILURE(status))
  800. return status;
  801. ec_ecdt->global_lock = TRUE;
  802. ec_ecdt->handle = handle;
  803. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  804. (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
  805. (u32) ec_ecdt->data_addr.address);
  806. return AE_CTRL_TERMINATE;
  807. }
  808. /*
  809. * Some BIOS (such as some from Gateway laptops) access EC region very early
  810. * such as in BAT0._INI or EC._INI before an EC device is found and
  811. * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
  812. * required, but if EC regison is accessed early, it is required.
  813. * The routine tries to workaround the BIOS bug by pre-scan EC device
  814. * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
  815. * op region (since _REG isn't invoked yet). The assumption is true for
  816. * all systems found.
  817. */
  818. static int __init
  819. acpi_ec_fake_ecdt(void)
  820. {
  821. acpi_status status;
  822. int ret = 0;
  823. printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
  824. ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  825. if (!ec_ecdt) {
  826. ret = -ENOMEM;
  827. goto error;
  828. }
  829. memset(ec_ecdt, 0, sizeof(struct acpi_ec));
  830. status = acpi_get_devices (ACPI_EC_HID,
  831. acpi_fake_ecdt_callback,
  832. NULL,
  833. NULL);
  834. if (ACPI_FAILURE(status)) {
  835. kfree(ec_ecdt);
  836. ec_ecdt = NULL;
  837. ret = -ENODEV;
  838. goto error;
  839. }
  840. return 0;
  841. error:
  842. printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
  843. return ret;
  844. }
  845. static int __init
  846. acpi_ec_get_real_ecdt(void)
  847. {
  848. acpi_status status;
  849. struct acpi_table_ecdt *ecdt_ptr;
  850. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  851. (struct acpi_table_header **) &ecdt_ptr);
  852. if (ACPI_FAILURE(status))
  853. return -ENODEV;
  854. printk(KERN_INFO PREFIX "Found ECDT\n");
  855. /*
  856. * Generate a temporary ec context to use until the namespace is scanned
  857. */
  858. ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  859. if (!ec_ecdt)
  860. return -ENOMEM;
  861. memset(ec_ecdt, 0, sizeof(struct acpi_ec));
  862. init_MUTEX(&ec_ecdt->sem);
  863. init_waitqueue_head(&ec_ecdt->wait);
  864. ec_ecdt->command_addr = ecdt_ptr->ec_control;
  865. ec_ecdt->status_addr = ecdt_ptr->ec_control;
  866. ec_ecdt->data_addr = ecdt_ptr->ec_data;
  867. ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
  868. /* use the GL just to be safe */
  869. ec_ecdt->global_lock = TRUE;
  870. ec_ecdt->uid = ecdt_ptr->uid;
  871. status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
  872. if (ACPI_FAILURE(status)) {
  873. goto error;
  874. }
  875. return 0;
  876. error:
  877. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  878. kfree(ec_ecdt);
  879. ec_ecdt = NULL;
  880. return -ENODEV;
  881. }
  882. static int __initdata acpi_fake_ecdt_enabled;
  883. int __init
  884. acpi_ec_ecdt_probe (void)
  885. {
  886. acpi_status status;
  887. int ret;
  888. ret = acpi_ec_get_real_ecdt();
  889. /* Try to make a fake ECDT */
  890. if (ret && acpi_fake_ecdt_enabled) {
  891. ret = acpi_ec_fake_ecdt();
  892. }
  893. if (ret)
  894. return 0;
  895. /*
  896. * Install GPE handler
  897. */
  898. status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
  899. ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
  900. ec_ecdt);
  901. if (ACPI_FAILURE(status)) {
  902. goto error;
  903. }
  904. acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  905. acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
  906. status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
  907. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
  908. &acpi_ec_space_setup, ec_ecdt);
  909. if (ACPI_FAILURE(status)) {
  910. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
  911. &acpi_ec_gpe_handler);
  912. goto error;
  913. }
  914. return 0;
  915. error:
  916. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  917. kfree(ec_ecdt);
  918. ec_ecdt = NULL;
  919. return -ENODEV;
  920. }
  921. static int __init acpi_ec_init (void)
  922. {
  923. int result;
  924. ACPI_FUNCTION_TRACE("acpi_ec_init");
  925. if (acpi_disabled)
  926. return_VALUE(0);
  927. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  928. if (!acpi_ec_dir)
  929. return_VALUE(-ENODEV);
  930. /* Now register the driver for the EC */
  931. result = acpi_bus_register_driver(&acpi_ec_driver);
  932. if (result < 0) {
  933. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  934. return_VALUE(-ENODEV);
  935. }
  936. return_VALUE(result);
  937. }
  938. subsys_initcall(acpi_ec_init);
  939. /* EC driver currently not unloadable */
  940. #if 0
  941. static void __exit
  942. acpi_ec_exit (void)
  943. {
  944. ACPI_FUNCTION_TRACE("acpi_ec_exit");
  945. acpi_bus_unregister_driver(&acpi_ec_driver);
  946. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  947. return_VOID;
  948. }
  949. #endif /* 0 */
  950. static int __init acpi_fake_ecdt_setup(char *str)
  951. {
  952. acpi_fake_ecdt_enabled = 1;
  953. return 0;
  954. }
  955. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);