ec.c 24 KB

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