ec.c 39 KB

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