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