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