ec.c 24 KB

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