ec.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 <asm/io.h>
  34. #include <acpi/acpi_bus.h>
  35. #include <acpi/acpi_drivers.h>
  36. #include <acpi/actypes.h>
  37. #define _COMPONENT ACPI_EC_COMPONENT
  38. ACPI_MODULE_NAME ("acpi_ec")
  39. #define ACPI_EC_COMPONENT 0x00100000
  40. #define ACPI_EC_CLASS "embedded_controller"
  41. #define ACPI_EC_HID "PNP0C09"
  42. #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
  43. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  44. #define ACPI_EC_FILE_INFO "info"
  45. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  46. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  47. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  48. #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
  49. #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
  50. #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
  51. #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
  52. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  53. #define ACPI_EC_COMMAND_READ 0x80
  54. #define ACPI_EC_COMMAND_WRITE 0x81
  55. #define ACPI_EC_COMMAND_QUERY 0x84
  56. static int acpi_ec_add (struct acpi_device *device);
  57. static int acpi_ec_remove (struct acpi_device *device, int type);
  58. static int acpi_ec_start (struct acpi_device *device);
  59. static int acpi_ec_stop (struct acpi_device *device, int type);
  60. static struct acpi_driver acpi_ec_driver = {
  61. .name = ACPI_EC_DRIVER_NAME,
  62. .class = ACPI_EC_CLASS,
  63. .ids = ACPI_EC_HID,
  64. .ops = {
  65. .add = acpi_ec_add,
  66. .remove = acpi_ec_remove,
  67. .start = acpi_ec_start,
  68. .stop = acpi_ec_stop,
  69. },
  70. };
  71. struct acpi_ec {
  72. acpi_handle handle;
  73. unsigned long uid;
  74. unsigned long gpe_bit;
  75. struct acpi_generic_address status_addr;
  76. struct acpi_generic_address command_addr;
  77. struct acpi_generic_address data_addr;
  78. unsigned long global_lock;
  79. spinlock_t lock;
  80. };
  81. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  82. static struct acpi_ec *ec_ecdt;
  83. /* External interfaces use first EC only, so remember */
  84. static struct acpi_device *first_ec;
  85. /* --------------------------------------------------------------------------
  86. Transaction Management
  87. -------------------------------------------------------------------------- */
  88. static int
  89. acpi_ec_wait (
  90. struct acpi_ec *ec,
  91. u8 event)
  92. {
  93. u32 acpi_ec_status = 0;
  94. u32 i = ACPI_EC_UDELAY_COUNT;
  95. if (!ec)
  96. return -EINVAL;
  97. /* Poll the EC status register waiting for the event to occur. */
  98. switch (event) {
  99. case ACPI_EC_EVENT_OBF:
  100. do {
  101. acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
  102. if (acpi_ec_status & ACPI_EC_FLAG_OBF)
  103. return 0;
  104. udelay(ACPI_EC_UDELAY);
  105. } while (--i>0);
  106. break;
  107. case ACPI_EC_EVENT_IBE:
  108. do {
  109. acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
  110. if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
  111. return 0;
  112. udelay(ACPI_EC_UDELAY);
  113. } while (--i>0);
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. return -ETIME;
  119. }
  120. static int
  121. acpi_ec_read (
  122. struct acpi_ec *ec,
  123. u8 address,
  124. u32 *data)
  125. {
  126. acpi_status status = AE_OK;
  127. int result = 0;
  128. unsigned long flags = 0;
  129. u32 glk = 0;
  130. ACPI_FUNCTION_TRACE("acpi_ec_read");
  131. if (!ec || !data)
  132. return_VALUE(-EINVAL);
  133. *data = 0;
  134. if (ec->global_lock) {
  135. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  136. if (ACPI_FAILURE(status))
  137. return_VALUE(-ENODEV);
  138. }
  139. spin_lock_irqsave(&ec->lock, flags);
  140. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
  141. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  142. if (result)
  143. goto end;
  144. acpi_hw_low_level_write(8, address, &ec->data_addr);
  145. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  146. if (result)
  147. goto end;
  148. acpi_hw_low_level_read(8, data, &ec->data_addr);
  149. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
  150. *data, address));
  151. end:
  152. spin_unlock_irqrestore(&ec->lock, flags);
  153. if (ec->global_lock)
  154. acpi_release_global_lock(glk);
  155. return_VALUE(result);
  156. }
  157. static int
  158. acpi_ec_write (
  159. struct acpi_ec *ec,
  160. u8 address,
  161. u8 data)
  162. {
  163. int result = 0;
  164. acpi_status status = AE_OK;
  165. unsigned long flags = 0;
  166. u32 glk = 0;
  167. ACPI_FUNCTION_TRACE("acpi_ec_write");
  168. if (!ec)
  169. return_VALUE(-EINVAL);
  170. if (ec->global_lock) {
  171. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  172. if (ACPI_FAILURE(status))
  173. return_VALUE(-ENODEV);
  174. }
  175. spin_lock_irqsave(&ec->lock, flags);
  176. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
  177. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  178. if (result)
  179. goto end;
  180. acpi_hw_low_level_write(8, address, &ec->data_addr);
  181. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  182. if (result)
  183. goto end;
  184. acpi_hw_low_level_write(8, data, &ec->data_addr);
  185. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
  186. if (result)
  187. goto end;
  188. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
  189. data, address));
  190. end:
  191. spin_unlock_irqrestore(&ec->lock, flags);
  192. if (ec->global_lock)
  193. acpi_release_global_lock(glk);
  194. return_VALUE(result);
  195. }
  196. /*
  197. * Externally callable EC access functions. For now, assume 1 EC only
  198. */
  199. int
  200. ec_read(u8 addr, u8 *val)
  201. {
  202. struct acpi_ec *ec;
  203. int err;
  204. u32 temp_data;
  205. if (!first_ec)
  206. return -ENODEV;
  207. ec = acpi_driver_data(first_ec);
  208. err = acpi_ec_read(ec, addr, &temp_data);
  209. if (!err) {
  210. *val = temp_data;
  211. return 0;
  212. }
  213. else
  214. return err;
  215. }
  216. EXPORT_SYMBOL(ec_read);
  217. int
  218. ec_write(u8 addr, u8 val)
  219. {
  220. struct acpi_ec *ec;
  221. int err;
  222. if (!first_ec)
  223. return -ENODEV;
  224. ec = acpi_driver_data(first_ec);
  225. err = acpi_ec_write(ec, addr, val);
  226. return err;
  227. }
  228. EXPORT_SYMBOL(ec_write);
  229. static int
  230. acpi_ec_query (
  231. struct acpi_ec *ec,
  232. u32 *data)
  233. {
  234. int result = 0;
  235. acpi_status status = AE_OK;
  236. unsigned long flags = 0;
  237. u32 glk = 0;
  238. ACPI_FUNCTION_TRACE("acpi_ec_query");
  239. if (!ec || !data)
  240. return_VALUE(-EINVAL);
  241. *data = 0;
  242. if (ec->global_lock) {
  243. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  244. if (ACPI_FAILURE(status))
  245. return_VALUE(-ENODEV);
  246. }
  247. /*
  248. * Query the EC to find out which _Qxx method we need to evaluate.
  249. * Note that successful completion of the query causes the ACPI_EC_SCI
  250. * bit to be cleared (and thus clearing the interrupt source).
  251. */
  252. spin_lock_irqsave(&ec->lock, flags);
  253. acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
  254. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
  255. if (result)
  256. goto end;
  257. acpi_hw_low_level_read(8, data, &ec->data_addr);
  258. if (!*data)
  259. result = -ENODATA;
  260. end:
  261. spin_unlock_irqrestore(&ec->lock, flags);
  262. if (ec->global_lock)
  263. acpi_release_global_lock(glk);
  264. return_VALUE(result);
  265. }
  266. /* --------------------------------------------------------------------------
  267. Event Management
  268. -------------------------------------------------------------------------- */
  269. struct acpi_ec_query_data {
  270. acpi_handle handle;
  271. u8 data;
  272. };
  273. static void
  274. acpi_ec_gpe_query (
  275. void *ec_cxt)
  276. {
  277. struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
  278. u32 value = 0;
  279. unsigned long flags = 0;
  280. static char object_name[5] = {'_','Q','0','0','\0'};
  281. const char hex[] = {'0','1','2','3','4','5','6','7',
  282. '8','9','A','B','C','D','E','F'};
  283. ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
  284. if (!ec_cxt)
  285. goto end;
  286. spin_lock_irqsave(&ec->lock, flags);
  287. acpi_hw_low_level_read(8, &value, &ec->command_addr);
  288. spin_unlock_irqrestore(&ec->lock, flags);
  289. /* TBD: Implement asynch events!
  290. * NOTE: All we care about are EC-SCI's. Other EC events are
  291. * handled via polling (yuck!). This is because some systems
  292. * treat EC-SCIs as level (versus EDGE!) triggered, preventing
  293. * a purely interrupt-driven approach (grumble, grumble).
  294. */
  295. if (!(value & ACPI_EC_FLAG_SCI))
  296. goto end;
  297. if (acpi_ec_query(ec, &value))
  298. goto end;
  299. object_name[2] = hex[((value >> 4) & 0x0F)];
  300. object_name[3] = hex[(value & 0x0F)];
  301. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
  302. acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
  303. end:
  304. acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
  305. }
  306. static u32
  307. acpi_ec_gpe_handler (
  308. void *data)
  309. {
  310. acpi_status status = AE_OK;
  311. struct acpi_ec *ec = (struct acpi_ec *) data;
  312. if (!ec)
  313. return ACPI_INTERRUPT_NOT_HANDLED;
  314. acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
  315. status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
  316. acpi_ec_gpe_query, ec);
  317. if (status == AE_OK)
  318. return ACPI_INTERRUPT_HANDLED;
  319. else
  320. return ACPI_INTERRUPT_NOT_HANDLED;
  321. }
  322. /* --------------------------------------------------------------------------
  323. Address Space Management
  324. -------------------------------------------------------------------------- */
  325. static acpi_status
  326. acpi_ec_space_setup (
  327. acpi_handle region_handle,
  328. u32 function,
  329. void *handler_context,
  330. void **return_context)
  331. {
  332. /*
  333. * The EC object is in the handler context and is needed
  334. * when calling the acpi_ec_space_handler.
  335. */
  336. if(function == ACPI_REGION_DEACTIVATE)
  337. *return_context = NULL;
  338. else
  339. *return_context = handler_context;
  340. return AE_OK;
  341. }
  342. static acpi_status
  343. acpi_ec_space_handler (
  344. u32 function,
  345. acpi_physical_address address,
  346. u32 bit_width,
  347. acpi_integer *value,
  348. void *handler_context,
  349. void *region_context)
  350. {
  351. int result = 0;
  352. struct acpi_ec *ec = NULL;
  353. u32 temp = 0;
  354. acpi_integer f_v = 0;
  355. int i = 0;
  356. ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
  357. if ((address > 0xFF) || !value || !handler_context)
  358. return_VALUE(AE_BAD_PARAMETER);
  359. if(bit_width != 8) {
  360. printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
  361. if (acpi_strict)
  362. return_VALUE(AE_BAD_PARAMETER);
  363. }
  364. ec = (struct acpi_ec *) handler_context;
  365. next_byte:
  366. switch (function) {
  367. case ACPI_READ:
  368. result = acpi_ec_read(ec, (u8) address, &temp);
  369. *value = (acpi_integer) temp;
  370. break;
  371. case ACPI_WRITE:
  372. result = acpi_ec_write(ec, (u8) address, (u8) *value);
  373. break;
  374. default:
  375. result = -EINVAL;
  376. goto out;
  377. break;
  378. }
  379. bit_width -= 8;
  380. if(bit_width){
  381. if(function == ACPI_READ)
  382. f_v |= (acpi_integer) (*value) << 8*i;
  383. if(function == ACPI_WRITE)
  384. (*value) >>=8;
  385. i++;
  386. goto next_byte;
  387. }
  388. if(function == ACPI_READ){
  389. f_v |= (acpi_integer) (*value) << 8*i;
  390. *value = f_v;
  391. }
  392. out:
  393. switch (result) {
  394. case -EINVAL:
  395. return_VALUE(AE_BAD_PARAMETER);
  396. break;
  397. case -ENODEV:
  398. return_VALUE(AE_NOT_FOUND);
  399. break;
  400. case -ETIME:
  401. return_VALUE(AE_TIME);
  402. break;
  403. default:
  404. return_VALUE(AE_OK);
  405. }
  406. }
  407. /* --------------------------------------------------------------------------
  408. FS Interface (/proc)
  409. -------------------------------------------------------------------------- */
  410. static struct proc_dir_entry *acpi_ec_dir;
  411. static int
  412. acpi_ec_read_info (struct seq_file *seq, void *offset)
  413. {
  414. struct acpi_ec *ec = (struct acpi_ec *) seq->private;
  415. ACPI_FUNCTION_TRACE("acpi_ec_read_info");
  416. if (!ec)
  417. goto end;
  418. seq_printf(seq, "gpe bit: 0x%02x\n",
  419. (u32) ec->gpe_bit);
  420. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  421. (u32) ec->status_addr.address, (u32) ec->data_addr.address);
  422. seq_printf(seq, "use global lock: %s\n",
  423. ec->global_lock?"yes":"no");
  424. end:
  425. return_VALUE(0);
  426. }
  427. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  428. {
  429. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  430. }
  431. static struct file_operations acpi_ec_info_ops = {
  432. .open = acpi_ec_info_open_fs,
  433. .read = seq_read,
  434. .llseek = seq_lseek,
  435. .release = single_release,
  436. .owner = THIS_MODULE,
  437. };
  438. static int
  439. acpi_ec_add_fs (
  440. struct acpi_device *device)
  441. {
  442. struct proc_dir_entry *entry = NULL;
  443. ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
  444. if (!acpi_device_dir(device)) {
  445. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  446. acpi_ec_dir);
  447. if (!acpi_device_dir(device))
  448. return_VALUE(-ENODEV);
  449. }
  450. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  451. acpi_device_dir(device));
  452. if (!entry)
  453. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  454. "Unable to create '%s' fs entry\n",
  455. ACPI_EC_FILE_INFO));
  456. else {
  457. entry->proc_fops = &acpi_ec_info_ops;
  458. entry->data = acpi_driver_data(device);
  459. entry->owner = THIS_MODULE;
  460. }
  461. return_VALUE(0);
  462. }
  463. static int
  464. acpi_ec_remove_fs (
  465. struct acpi_device *device)
  466. {
  467. ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
  468. if (acpi_device_dir(device)) {
  469. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  470. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  471. acpi_device_dir(device) = NULL;
  472. }
  473. return_VALUE(0);
  474. }
  475. /* --------------------------------------------------------------------------
  476. Driver Interface
  477. -------------------------------------------------------------------------- */
  478. static int
  479. acpi_ec_add (
  480. struct acpi_device *device)
  481. {
  482. int result = 0;
  483. acpi_status status = AE_OK;
  484. struct acpi_ec *ec = NULL;
  485. unsigned long uid;
  486. ACPI_FUNCTION_TRACE("acpi_ec_add");
  487. if (!device)
  488. return_VALUE(-EINVAL);
  489. ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  490. if (!ec)
  491. return_VALUE(-ENOMEM);
  492. memset(ec, 0, sizeof(struct acpi_ec));
  493. ec->handle = device->handle;
  494. ec->uid = -1;
  495. spin_lock_init(&ec->lock);
  496. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  497. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  498. acpi_driver_data(device) = ec;
  499. /* Use the global lock for all EC transactions? */
  500. acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
  501. /* If our UID matches the UID for the ECDT-enumerated EC,
  502. we now have the *real* EC info, so kill the makeshift one.*/
  503. acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
  504. if (ec_ecdt && ec_ecdt->uid == uid) {
  505. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  506. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
  507. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
  508. kfree(ec_ecdt);
  509. }
  510. /* Get GPE bit assignment (EC events). */
  511. /* TODO: Add support for _GPE returning a package */
  512. status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
  513. if (ACPI_FAILURE(status)) {
  514. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  515. "Error obtaining GPE bit assignment\n"));
  516. result = -ENODEV;
  517. goto end;
  518. }
  519. result = acpi_ec_add_fs(device);
  520. if (result)
  521. goto end;
  522. printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
  523. acpi_device_name(device), acpi_device_bid(device),
  524. (u32) ec->gpe_bit);
  525. if (!first_ec)
  526. first_ec = device;
  527. end:
  528. if (result)
  529. kfree(ec);
  530. return_VALUE(result);
  531. }
  532. static int
  533. acpi_ec_remove (
  534. struct acpi_device *device,
  535. int type)
  536. {
  537. struct acpi_ec *ec = NULL;
  538. ACPI_FUNCTION_TRACE("acpi_ec_remove");
  539. if (!device)
  540. return_VALUE(-EINVAL);
  541. ec = acpi_driver_data(device);
  542. acpi_ec_remove_fs(device);
  543. kfree(ec);
  544. return_VALUE(0);
  545. }
  546. static acpi_status
  547. acpi_ec_io_ports (
  548. struct acpi_resource *resource,
  549. void *context)
  550. {
  551. struct acpi_ec *ec = (struct acpi_ec *) context;
  552. struct acpi_generic_address *addr;
  553. if (resource->id != ACPI_RSTYPE_IO) {
  554. return AE_OK;
  555. }
  556. /*
  557. * The first address region returned is the data port, and
  558. * the second address region returned is the status/command
  559. * port.
  560. */
  561. if (ec->data_addr.register_bit_width == 0) {
  562. addr = &ec->data_addr;
  563. } else if (ec->command_addr.register_bit_width == 0) {
  564. addr = &ec->command_addr;
  565. } else {
  566. return AE_CTRL_TERMINATE;
  567. }
  568. addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  569. addr->register_bit_width = 8;
  570. addr->register_bit_offset = 0;
  571. addr->address = resource->data.io.min_base_address;
  572. return AE_OK;
  573. }
  574. static int
  575. acpi_ec_start (
  576. struct acpi_device *device)
  577. {
  578. acpi_status status = AE_OK;
  579. struct acpi_ec *ec = NULL;
  580. ACPI_FUNCTION_TRACE("acpi_ec_start");
  581. if (!device)
  582. return_VALUE(-EINVAL);
  583. ec = acpi_driver_data(device);
  584. if (!ec)
  585. return_VALUE(-EINVAL);
  586. /*
  587. * Get I/O port addresses. Convert to GAS format.
  588. */
  589. status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
  590. acpi_ec_io_ports, ec);
  591. if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
  592. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
  593. return_VALUE(-ENODEV);
  594. }
  595. ec->status_addr = ec->command_addr;
  596. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
  597. (u32) ec->gpe_bit, (u32) ec->command_addr.address,
  598. (u32) ec->data_addr.address));
  599. /*
  600. * Install GPE handler
  601. */
  602. status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
  603. ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
  604. if (ACPI_FAILURE(status)) {
  605. return_VALUE(-ENODEV);
  606. }
  607. acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  608. acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
  609. status = acpi_install_address_space_handler (ec->handle,
  610. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
  611. &acpi_ec_space_setup, ec);
  612. if (ACPI_FAILURE(status)) {
  613. acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
  614. return_VALUE(-ENODEV);
  615. }
  616. return_VALUE(AE_OK);
  617. }
  618. static int
  619. acpi_ec_stop (
  620. struct acpi_device *device,
  621. int type)
  622. {
  623. acpi_status status = AE_OK;
  624. struct acpi_ec *ec = NULL;
  625. ACPI_FUNCTION_TRACE("acpi_ec_stop");
  626. if (!device)
  627. return_VALUE(-EINVAL);
  628. ec = acpi_driver_data(device);
  629. status = acpi_remove_address_space_handler(ec->handle,
  630. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
  631. if (ACPI_FAILURE(status))
  632. return_VALUE(-ENODEV);
  633. status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
  634. if (ACPI_FAILURE(status))
  635. return_VALUE(-ENODEV);
  636. return_VALUE(0);
  637. }
  638. static acpi_status __init
  639. acpi_fake_ecdt_callback (
  640. acpi_handle handle,
  641. u32 Level,
  642. void *context,
  643. void **retval)
  644. {
  645. acpi_status status;
  646. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  647. acpi_ec_io_ports, ec_ecdt);
  648. if (ACPI_FAILURE(status))
  649. return status;
  650. ec_ecdt->status_addr = ec_ecdt->command_addr;
  651. ec_ecdt->uid = -1;
  652. acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
  653. status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
  654. if (ACPI_FAILURE(status))
  655. return status;
  656. spin_lock_init(&ec_ecdt->lock);
  657. ec_ecdt->global_lock = TRUE;
  658. ec_ecdt->handle = handle;
  659. printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
  660. (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
  661. (u32) ec_ecdt->data_addr.address);
  662. return AE_CTRL_TERMINATE;
  663. }
  664. /*
  665. * Some BIOS (such as some from Gateway laptops) access EC region very early
  666. * such as in BAT0._INI or EC._INI before an EC device is found and
  667. * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
  668. * required, but if EC regison is accessed early, it is required.
  669. * The routine tries to workaround the BIOS bug by pre-scan EC device
  670. * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
  671. * op region (since _REG isn't invoked yet). The assumption is true for
  672. * all systems found.
  673. */
  674. static int __init
  675. acpi_ec_fake_ecdt(void)
  676. {
  677. acpi_status status;
  678. int ret = 0;
  679. printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
  680. ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  681. if (!ec_ecdt) {
  682. ret = -ENOMEM;
  683. goto error;
  684. }
  685. memset(ec_ecdt, 0, sizeof(struct acpi_ec));
  686. status = acpi_get_devices (ACPI_EC_HID,
  687. acpi_fake_ecdt_callback,
  688. NULL,
  689. NULL);
  690. if (ACPI_FAILURE(status)) {
  691. kfree(ec_ecdt);
  692. ec_ecdt = NULL;
  693. ret = -ENODEV;
  694. goto error;
  695. }
  696. return 0;
  697. error:
  698. printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
  699. return ret;
  700. }
  701. static int __init
  702. acpi_ec_get_real_ecdt(void)
  703. {
  704. acpi_status status;
  705. struct acpi_table_ecdt *ecdt_ptr;
  706. status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
  707. (struct acpi_table_header **) &ecdt_ptr);
  708. if (ACPI_FAILURE(status))
  709. return -ENODEV;
  710. printk(KERN_INFO PREFIX "Found ECDT\n");
  711. /*
  712. * Generate a temporary ec context to use until the namespace is scanned
  713. */
  714. ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  715. if (!ec_ecdt)
  716. return -ENOMEM;
  717. memset(ec_ecdt, 0, sizeof(struct acpi_ec));
  718. ec_ecdt->command_addr = ecdt_ptr->ec_control;
  719. ec_ecdt->status_addr = ecdt_ptr->ec_control;
  720. ec_ecdt->data_addr = ecdt_ptr->ec_data;
  721. ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
  722. spin_lock_init(&ec_ecdt->lock);
  723. /* use the GL just to be safe */
  724. ec_ecdt->global_lock = TRUE;
  725. ec_ecdt->uid = ecdt_ptr->uid;
  726. status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
  727. if (ACPI_FAILURE(status)) {
  728. goto error;
  729. }
  730. return 0;
  731. error:
  732. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  733. kfree(ec_ecdt);
  734. ec_ecdt = NULL;
  735. return -ENODEV;
  736. }
  737. static int __initdata acpi_fake_ecdt_enabled;
  738. int __init
  739. acpi_ec_ecdt_probe (void)
  740. {
  741. acpi_status status;
  742. int ret;
  743. ret = acpi_ec_get_real_ecdt();
  744. /* Try to make a fake ECDT */
  745. if (ret && acpi_fake_ecdt_enabled) {
  746. ret = acpi_ec_fake_ecdt();
  747. }
  748. if (ret)
  749. return 0;
  750. /*
  751. * Install GPE handler
  752. */
  753. status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
  754. ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
  755. ec_ecdt);
  756. if (ACPI_FAILURE(status)) {
  757. goto error;
  758. }
  759. acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
  760. acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
  761. status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
  762. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
  763. &acpi_ec_space_setup, ec_ecdt);
  764. if (ACPI_FAILURE(status)) {
  765. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
  766. &acpi_ec_gpe_handler);
  767. goto error;
  768. }
  769. return 0;
  770. error:
  771. printk(KERN_ERR PREFIX "Could not use ECDT\n");
  772. kfree(ec_ecdt);
  773. ec_ecdt = NULL;
  774. return -ENODEV;
  775. }
  776. static int __init acpi_ec_init (void)
  777. {
  778. int result = 0;
  779. ACPI_FUNCTION_TRACE("acpi_ec_init");
  780. if (acpi_disabled)
  781. return_VALUE(0);
  782. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  783. if (!acpi_ec_dir)
  784. return_VALUE(-ENODEV);
  785. /* Now register the driver for the EC */
  786. result = acpi_bus_register_driver(&acpi_ec_driver);
  787. if (result < 0) {
  788. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  789. return_VALUE(-ENODEV);
  790. }
  791. return_VALUE(result);
  792. }
  793. subsys_initcall(acpi_ec_init);
  794. /* EC driver currently not unloadable */
  795. #if 0
  796. static void __exit
  797. acpi_ec_exit (void)
  798. {
  799. ACPI_FUNCTION_TRACE("acpi_ec_exit");
  800. acpi_bus_unregister_driver(&acpi_ec_driver);
  801. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  802. return_VOID;
  803. }
  804. #endif /* 0 */
  805. static int __init acpi_fake_ecdt_setup(char *str)
  806. {
  807. acpi_fake_ecdt_enabled = 1;
  808. return 0;
  809. }
  810. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);