ec.c 24 KB

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