ec.c 24 KB

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