ec.c 24 KB

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