ec.c 27 KB

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