ec.c 24 KB

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