ec.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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;
  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. static int acpi_ec_mode = EC_INTR;
  103. /* --------------------------------------------------------------------------
  104. Transaction Management
  105. -------------------------------------------------------------------------- */
  106. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  107. {
  108. return inb(ec->command_addr);
  109. }
  110. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  111. {
  112. return inb(ec->data_addr);
  113. }
  114. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  115. {
  116. outb(command, ec->command_addr);
  117. }
  118. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  119. {
  120. outb(data, ec->data_addr);
  121. }
  122. static int acpi_ec_check_status(struct acpi_ec *ec, u8 event)
  123. {
  124. u8 status = acpi_ec_read_status(ec);
  125. switch (event) {
  126. case ACPI_EC_EVENT_OBF_1:
  127. if (status & ACPI_EC_FLAG_OBF)
  128. return 1;
  129. break;
  130. case ACPI_EC_EVENT_IBF_0:
  131. if (!(status & ACPI_EC_FLAG_IBF))
  132. return 1;
  133. break;
  134. default:
  135. break;
  136. }
  137. return 0;
  138. }
  139. static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
  140. {
  141. if (acpi_ec_mode == EC_POLL) {
  142. int i;
  143. for (i = 0; i < ACPI_EC_UDELAY_COUNT; ++i) {
  144. if (acpi_ec_check_status(ec, event))
  145. return 0;
  146. udelay(ACPI_EC_UDELAY);
  147. }
  148. } else {
  149. if (wait_event_timeout(ec->wait,
  150. acpi_ec_check_status(ec, event),
  151. msecs_to_jiffies(ACPI_EC_DELAY)) ||
  152. acpi_ec_check_status(ec, event)) {
  153. return 0;
  154. } else {
  155. printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
  156. " status = %d, expect_event = %d\n",
  157. acpi_ec_read_status(ec), event);
  158. }
  159. }
  160. return -ETIME;
  161. }
  162. #ifdef ACPI_FUTURE_USAGE
  163. /*
  164. * Note: samsung nv5000 doesn't work with ec burst mode.
  165. * http://bugzilla.kernel.org/show_bug.cgi?id=4980
  166. */
  167. int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
  168. {
  169. u8 tmp = 0;
  170. u8 status = 0;
  171. status = acpi_ec_read_status(ec);
  172. if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
  173. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  174. if (status)
  175. goto end;
  176. acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
  177. status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
  178. tmp = acpi_ec_read_data(ec);
  179. if (tmp != 0x90) { /* Burst ACK byte */
  180. return -EINVAL;
  181. }
  182. }
  183. atomic_set(&ec->leaving_burst, 0);
  184. return 0;
  185. end:
  186. ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
  187. return -1;
  188. }
  189. int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
  190. {
  191. u8 status = 0;
  192. status = acpi_ec_read_status(ec);
  193. if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
  194. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  195. if(status)
  196. goto end;
  197. acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
  198. acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  199. }
  200. atomic_set(&ec->leaving_burst, 1);
  201. return 0;
  202. end:
  203. ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
  204. return -1;
  205. }
  206. #endif /* ACPI_FUTURE_USAGE */
  207. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
  208. const u8 *wdata, unsigned wdata_len,
  209. u8 *rdata, unsigned rdata_len)
  210. {
  211. int result = 0;
  212. acpi_ec_write_cmd(ec, command);
  213. for (; wdata_len > 0; wdata_len --) {
  214. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  215. if (result) {
  216. printk(KERN_ERR PREFIX "write_cmd timeout, command = %d\n",
  217. command);
  218. goto end;
  219. }
  220. acpi_ec_write_data(ec, *(wdata++));
  221. }
  222. if (!rdata_len) {
  223. result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  224. if (result) {
  225. printk(KERN_ERR PREFIX "finish-write timeout, command = %d\n",
  226. command);
  227. goto end;
  228. }
  229. } else if (command == ACPI_EC_COMMAND_QUERY) {
  230. atomic_set(&ec->query_pending, 0);
  231. }
  232. for (; rdata_len > 0; rdata_len --) {
  233. result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
  234. if (result) {
  235. printk(KERN_ERR PREFIX "read timeout, command = %d\n",
  236. command);
  237. goto end;
  238. }
  239. *(rdata++) = acpi_ec_read_data(ec);
  240. }
  241. end:
  242. return result;
  243. }
  244. static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
  245. const u8 *wdata, unsigned wdata_len,
  246. u8 *rdata, unsigned rdata_len)
  247. {
  248. int status;
  249. u32 glk;
  250. if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
  251. return -EINVAL;
  252. if (rdata)
  253. memset(rdata, 0, rdata_len);
  254. if (ec->global_lock) {
  255. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  256. if (ACPI_FAILURE(status))
  257. return -ENODEV;
  258. }
  259. mutex_lock(&ec->lock);
  260. /* Make sure GPE is enabled before doing transaction */
  261. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  262. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  263. if (status) {
  264. printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
  265. goto end;
  266. }
  267. status = acpi_ec_transaction_unlocked(ec, command,
  268. wdata, wdata_len,
  269. rdata, rdata_len);
  270. end:
  271. mutex_unlock(&ec->lock);
  272. if (ec->global_lock)
  273. acpi_release_global_lock(glk);
  274. return status;
  275. }
  276. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
  277. {
  278. int result;
  279. u8 d;
  280. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
  281. &address, 1, &d, 1);
  282. *data = d;
  283. return result;
  284. }
  285. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  286. {
  287. u8 wdata[2] = { address, data };
  288. return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
  289. wdata, 2, NULL, 0);
  290. }
  291. /*
  292. * Externally callable EC access functions. For now, assume 1 EC only
  293. */
  294. int ec_read(u8 addr, u8 *val)
  295. {
  296. struct acpi_ec *ec;
  297. int err;
  298. u8 temp_data;
  299. if (!first_ec)
  300. return -ENODEV;
  301. ec = acpi_driver_data(first_ec);
  302. err = acpi_ec_read(ec, addr, &temp_data);
  303. if (!err) {
  304. *val = temp_data;
  305. return 0;
  306. } else
  307. return err;
  308. }
  309. EXPORT_SYMBOL(ec_read);
  310. int ec_write(u8 addr, u8 val)
  311. {
  312. struct acpi_ec *ec;
  313. int err;
  314. if (!first_ec)
  315. return -ENODEV;
  316. ec = acpi_driver_data(first_ec);
  317. err = acpi_ec_write(ec, addr, val);
  318. return err;
  319. }
  320. EXPORT_SYMBOL(ec_write);
  321. extern int ec_transaction(u8 command,
  322. const u8 *wdata, unsigned wdata_len,
  323. u8 *rdata, unsigned rdata_len)
  324. {
  325. struct acpi_ec *ec;
  326. if (!first_ec)
  327. return -ENODEV;
  328. ec = acpi_driver_data(first_ec);
  329. return acpi_ec_transaction(ec, command, wdata,
  330. wdata_len, rdata, rdata_len);
  331. }
  332. EXPORT_SYMBOL(ec_transaction);
  333. static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
  334. {
  335. int result;
  336. u8 d;
  337. if (!ec || !data)
  338. return -EINVAL;
  339. /*
  340. * Query the EC to find out which _Qxx method we need to evaluate.
  341. * Note that successful completion of the query causes the ACPI_EC_SCI
  342. * bit to be cleared (and thus clearing the interrupt source).
  343. */
  344. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
  345. if (result)
  346. return result;
  347. if (!d)
  348. return -ENODATA;
  349. *data = d;
  350. return 0;
  351. }
  352. /* --------------------------------------------------------------------------
  353. Event Management
  354. -------------------------------------------------------------------------- */
  355. static void acpi_ec_gpe_query(void *ec_cxt)
  356. {
  357. struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
  358. u8 value = 0;
  359. char object_name[8];
  360. if (!ec || acpi_ec_query(ec, &value))
  361. return;
  362. snprintf(object_name, 8, "_Q%2.2X", value);
  363. printk(KERN_INFO PREFIX "evaluating %s\n", object_name);
  364. acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
  365. }
  366. static u32 acpi_ec_gpe_handler(void *data)
  367. {
  368. acpi_status status = AE_OK;
  369. u8 value;
  370. struct acpi_ec *ec = (struct acpi_ec *)data;
  371. if (acpi_ec_mode == EC_INTR) {
  372. wake_up(&ec->wait);
  373. }
  374. value = acpi_ec_read_status(ec);
  375. if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
  376. atomic_set(&ec->query_pending, 1);
  377. status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
  378. }
  379. return status == AE_OK ?
  380. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  381. }
  382. /* --------------------------------------------------------------------------
  383. Address Space Management
  384. -------------------------------------------------------------------------- */
  385. static acpi_status
  386. acpi_ec_space_setup(acpi_handle region_handle,
  387. u32 function, void *handler_context, void **return_context)
  388. {
  389. /*
  390. * The EC object is in the handler context and is needed
  391. * when calling the acpi_ec_space_handler.
  392. */
  393. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  394. handler_context : NULL;
  395. return AE_OK;
  396. }
  397. static acpi_status
  398. acpi_ec_space_handler(u32 function,
  399. acpi_physical_address address,
  400. u32 bit_width,
  401. acpi_integer * value,
  402. void *handler_context, void *region_context)
  403. {
  404. int result = 0;
  405. struct acpi_ec *ec = NULL;
  406. u64 temp = *value;
  407. acpi_integer f_v = 0;
  408. int i = 0;
  409. if ((address > 0xFF) || !value || !handler_context)
  410. return AE_BAD_PARAMETER;
  411. if (bit_width != 8 && acpi_strict) {
  412. return AE_BAD_PARAMETER;
  413. }
  414. ec = (struct acpi_ec *)handler_context;
  415. next_byte:
  416. switch (function) {
  417. case ACPI_READ:
  418. temp = 0;
  419. result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
  420. break;
  421. case ACPI_WRITE:
  422. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  423. break;
  424. default:
  425. result = -EINVAL;
  426. goto out;
  427. break;
  428. }
  429. bit_width -= 8;
  430. if (bit_width) {
  431. if (function == ACPI_READ)
  432. f_v |= temp << 8 * i;
  433. if (function == ACPI_WRITE)
  434. temp >>= 8;
  435. i++;
  436. address++;
  437. goto next_byte;
  438. }
  439. if (function == ACPI_READ) {
  440. f_v |= temp << 8 * i;
  441. *value = f_v;
  442. }
  443. out:
  444. switch (result) {
  445. case -EINVAL:
  446. return AE_BAD_PARAMETER;
  447. break;
  448. case -ENODEV:
  449. return AE_NOT_FOUND;
  450. break;
  451. case -ETIME:
  452. return AE_TIME;
  453. break;
  454. default:
  455. return AE_OK;
  456. }
  457. }
  458. /* --------------------------------------------------------------------------
  459. FS Interface (/proc)
  460. -------------------------------------------------------------------------- */
  461. static struct proc_dir_entry *acpi_ec_dir;
  462. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  463. {
  464. struct acpi_ec *ec = (struct acpi_ec *)seq->private;
  465. if (!ec)
  466. goto end;
  467. seq_printf(seq, "gpe: 0x%02x\n",
  468. (u32) ec->gpe);
  469. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  470. (u32) ec->command_addr,
  471. (u32) ec->data_addr);
  472. seq_printf(seq, "use global lock: %s\n",
  473. ec->global_lock ? "yes" : "no");
  474. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  475. end:
  476. return 0;
  477. }
  478. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  479. {
  480. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  481. }
  482. static struct file_operations acpi_ec_info_ops = {
  483. .open = acpi_ec_info_open_fs,
  484. .read = seq_read,
  485. .llseek = seq_lseek,
  486. .release = single_release,
  487. .owner = THIS_MODULE,
  488. };
  489. static int acpi_ec_add_fs(struct acpi_device *device)
  490. {
  491. struct proc_dir_entry *entry = NULL;
  492. if (!acpi_device_dir(device)) {
  493. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  494. acpi_ec_dir);
  495. if (!acpi_device_dir(device))
  496. return -ENODEV;
  497. }
  498. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  499. acpi_device_dir(device));
  500. if (!entry)
  501. return -ENODEV;
  502. else {
  503. entry->proc_fops = &acpi_ec_info_ops;
  504. entry->data = acpi_driver_data(device);
  505. entry->owner = THIS_MODULE;
  506. }
  507. return 0;
  508. }
  509. static int acpi_ec_remove_fs(struct acpi_device *device)
  510. {
  511. if (acpi_device_dir(device)) {
  512. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  513. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  514. acpi_device_dir(device) = NULL;
  515. }
  516. return 0;
  517. }
  518. /* --------------------------------------------------------------------------
  519. Driver Interface
  520. -------------------------------------------------------------------------- */
  521. static int acpi_ec_add(struct acpi_device *device)
  522. {
  523. int result = 0;
  524. acpi_status status = AE_OK;
  525. struct acpi_ec *ec = NULL;
  526. if (!device)
  527. return -EINVAL;
  528. ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  529. if (!ec)
  530. return -ENOMEM;
  531. memset(ec, 0, sizeof(struct acpi_ec));
  532. ec->handle = device->handle;
  533. ec->uid = -1;
  534. mutex_init(&ec->lock);
  535. atomic_set(&ec->query_pending, 0);
  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,
  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);
  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));
  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, ec->command_addr, ec->data_addr));
  631. /*
  632. * Install GPE handler
  633. */
  634. status = acpi_install_gpe_handler(NULL, ec->gpe,
  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, ACPI_GPE_TYPE_RUNTIME);
  641. acpi_enable_gpe(NULL, ec->gpe, 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,
  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,
  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. mutex_init(&ec_ecdt->lock);
  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);
  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, 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. mutex_init(&ec_ecdt->lock);
  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 = 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,
  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, ACPI_GPE_TYPE_RUNTIME);
  793. acpi_enable_gpe(NULL, ec_ecdt->gpe, 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,
  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);