ec.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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("ec");
  40. #define ACPI_EC_COMPONENT 0x00100000
  41. #define ACPI_EC_CLASS "embedded_controller"
  42. #define ACPI_EC_HID "PNP0C09"
  43. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  44. #define ACPI_EC_FILE_INFO "info"
  45. #undef PREFIX
  46. #define PREFIX "ACPI: EC: "
  47. /* EC status register */
  48. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  49. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  50. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  51. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  52. /* EC commands */
  53. enum ec_command {
  54. ACPI_EC_COMMAND_READ = 0x80,
  55. ACPI_EC_COMMAND_WRITE = 0x81,
  56. ACPI_EC_BURST_ENABLE = 0x82,
  57. ACPI_EC_BURST_DISABLE = 0x83,
  58. ACPI_EC_COMMAND_QUERY = 0x84,
  59. };
  60. /* EC events */
  61. enum ec_event {
  62. ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
  63. ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
  64. };
  65. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  66. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  67. static enum ec_mode {
  68. EC_INTR = 1, /* Output buffer full */
  69. EC_POLL, /* Input buffer empty */
  70. } acpi_ec_mode = EC_INTR;
  71. static int acpi_ec_remove(struct acpi_device *device, int type);
  72. static int acpi_ec_start(struct acpi_device *device);
  73. static int acpi_ec_stop(struct acpi_device *device, int type);
  74. static int acpi_ec_add(struct acpi_device *device);
  75. static struct acpi_driver acpi_ec_driver = {
  76. .name = "ec",
  77. .class = ACPI_EC_CLASS,
  78. .ids = ACPI_EC_HID,
  79. .ops = {
  80. .add = acpi_ec_add,
  81. .remove = acpi_ec_remove,
  82. .start = acpi_ec_start,
  83. .stop = acpi_ec_stop,
  84. },
  85. };
  86. /* If we find an EC via the ECDT, we need to keep a ptr to its context */
  87. static struct acpi_ec {
  88. acpi_handle handle;
  89. unsigned long uid;
  90. unsigned long gpe;
  91. unsigned long command_addr;
  92. unsigned long data_addr;
  93. unsigned long global_lock;
  94. struct mutex lock;
  95. atomic_t query_pending;
  96. atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
  97. wait_queue_head_t wait;
  98. } *ec_ecdt;
  99. /* External interfaces use first EC only, so remember */
  100. static struct acpi_device *first_ec;
  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, enum ec_event 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, enum ec_event 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
  209. "write_cmd timeout, command = %d\n", 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
  218. "finish-write timeout, command = %d\n", 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. mutex_unlock(&ec->lock);
  251. return -ENODEV;
  252. }
  253. }
  254. /* Make sure GPE is enabled before doing transaction */
  255. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  256. status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
  257. if (status) {
  258. printk(KERN_DEBUG PREFIX
  259. "input buffer is not empty, aborting transaction\n");
  260. goto end;
  261. }
  262. status = acpi_ec_transaction_unlocked(ec, command,
  263. wdata, wdata_len,
  264. rdata, rdata_len);
  265. end:
  266. if (ec->global_lock)
  267. acpi_release_global_lock(glk);
  268. mutex_unlock(&ec->lock);
  269. return status;
  270. }
  271. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  272. {
  273. int result;
  274. u8 d;
  275. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
  276. &address, 1, &d, 1);
  277. *data = d;
  278. return result;
  279. }
  280. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  281. {
  282. u8 wdata[2] = { address, data };
  283. return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
  284. wdata, 2, NULL, 0);
  285. }
  286. /*
  287. * Externally callable EC access functions. For now, assume 1 EC only
  288. */
  289. int ec_read(u8 addr, u8 * val)
  290. {
  291. struct acpi_ec *ec;
  292. int err;
  293. u8 temp_data;
  294. if (!first_ec)
  295. return -ENODEV;
  296. ec = acpi_driver_data(first_ec);
  297. err = acpi_ec_read(ec, addr, &temp_data);
  298. if (!err) {
  299. *val = temp_data;
  300. return 0;
  301. } else
  302. return err;
  303. }
  304. EXPORT_SYMBOL(ec_read);
  305. int ec_write(u8 addr, u8 val)
  306. {
  307. struct acpi_ec *ec;
  308. int err;
  309. if (!first_ec)
  310. return -ENODEV;
  311. ec = acpi_driver_data(first_ec);
  312. err = acpi_ec_write(ec, addr, val);
  313. return err;
  314. }
  315. EXPORT_SYMBOL(ec_write);
  316. int ec_transaction(u8 command,
  317. const u8 * wdata, unsigned wdata_len,
  318. u8 * rdata, unsigned rdata_len)
  319. {
  320. struct acpi_ec *ec;
  321. if (!first_ec)
  322. return -ENODEV;
  323. ec = acpi_driver_data(first_ec);
  324. return acpi_ec_transaction(ec, command, wdata,
  325. wdata_len, rdata, rdata_len);
  326. }
  327. EXPORT_SYMBOL(ec_transaction);
  328. static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
  329. {
  330. int result;
  331. u8 d;
  332. if (!ec || !data)
  333. return -EINVAL;
  334. /*
  335. * Query the EC to find out which _Qxx method we need to evaluate.
  336. * Note that successful completion of the query causes the ACPI_EC_SCI
  337. * bit to be cleared (and thus clearing the interrupt source).
  338. */
  339. result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
  340. if (result)
  341. return result;
  342. if (!d)
  343. return -ENODATA;
  344. *data = d;
  345. return 0;
  346. }
  347. /* --------------------------------------------------------------------------
  348. Event Management
  349. -------------------------------------------------------------------------- */
  350. static void acpi_ec_gpe_query(void *ec_cxt)
  351. {
  352. struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
  353. u8 value = 0;
  354. char object_name[8];
  355. if (!ec || acpi_ec_query(ec, &value))
  356. return;
  357. snprintf(object_name, 8, "_Q%2.2X", value);
  358. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
  359. acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
  360. }
  361. static u32 acpi_ec_gpe_handler(void *data)
  362. {
  363. acpi_status status = AE_OK;
  364. u8 value;
  365. struct acpi_ec *ec = (struct acpi_ec *)data;
  366. if (acpi_ec_mode == EC_INTR) {
  367. wake_up(&ec->wait);
  368. }
  369. value = acpi_ec_read_status(ec);
  370. if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
  371. atomic_set(&ec->query_pending, 1);
  372. status =
  373. acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
  374. ec);
  375. }
  376. return status == AE_OK ?
  377. ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
  378. }
  379. /* --------------------------------------------------------------------------
  380. Address Space Management
  381. -------------------------------------------------------------------------- */
  382. static acpi_status
  383. acpi_ec_space_setup(acpi_handle region_handle,
  384. u32 function, void *handler_context, void **return_context)
  385. {
  386. /*
  387. * The EC object is in the handler context and is needed
  388. * when calling the acpi_ec_space_handler.
  389. */
  390. *return_context = (function != ACPI_REGION_DEACTIVATE) ?
  391. handler_context : NULL;
  392. return AE_OK;
  393. }
  394. static acpi_status
  395. acpi_ec_space_handler(u32 function,
  396. acpi_physical_address address,
  397. u32 bit_width,
  398. acpi_integer * value,
  399. void *handler_context, void *region_context)
  400. {
  401. int result = 0;
  402. struct acpi_ec *ec = NULL;
  403. u64 temp = *value;
  404. acpi_integer f_v = 0;
  405. int i = 0;
  406. if ((address > 0xFF) || !value || !handler_context)
  407. return AE_BAD_PARAMETER;
  408. if (bit_width != 8 && acpi_strict) {
  409. return AE_BAD_PARAMETER;
  410. }
  411. ec = (struct acpi_ec *)handler_context;
  412. next_byte:
  413. switch (function) {
  414. case ACPI_READ:
  415. temp = 0;
  416. result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
  417. break;
  418. case ACPI_WRITE:
  419. result = acpi_ec_write(ec, (u8) address, (u8) temp);
  420. break;
  421. default:
  422. result = -EINVAL;
  423. goto out;
  424. break;
  425. }
  426. bit_width -= 8;
  427. if (bit_width) {
  428. if (function == ACPI_READ)
  429. f_v |= temp << 8 * i;
  430. if (function == ACPI_WRITE)
  431. temp >>= 8;
  432. i++;
  433. address++;
  434. goto next_byte;
  435. }
  436. if (function == ACPI_READ) {
  437. f_v |= temp << 8 * i;
  438. *value = f_v;
  439. }
  440. out:
  441. switch (result) {
  442. case -EINVAL:
  443. return AE_BAD_PARAMETER;
  444. break;
  445. case -ENODEV:
  446. return AE_NOT_FOUND;
  447. break;
  448. case -ETIME:
  449. return AE_TIME;
  450. break;
  451. default:
  452. return AE_OK;
  453. }
  454. }
  455. /* --------------------------------------------------------------------------
  456. FS Interface (/proc)
  457. -------------------------------------------------------------------------- */
  458. static struct proc_dir_entry *acpi_ec_dir;
  459. static int acpi_ec_read_info(struct seq_file *seq, void *offset)
  460. {
  461. struct acpi_ec *ec = (struct acpi_ec *)seq->private;
  462. if (!ec)
  463. goto end;
  464. seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
  465. seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
  466. (u32) ec->command_addr, (u32) ec->data_addr);
  467. seq_printf(seq, "use global lock: %s\n",
  468. ec->global_lock ? "yes" : "no");
  469. acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
  470. end:
  471. return 0;
  472. }
  473. static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
  474. {
  475. return single_open(file, acpi_ec_read_info, PDE(inode)->data);
  476. }
  477. static struct file_operations acpi_ec_info_ops = {
  478. .open = acpi_ec_info_open_fs,
  479. .read = seq_read,
  480. .llseek = seq_lseek,
  481. .release = single_release,
  482. .owner = THIS_MODULE,
  483. };
  484. static int acpi_ec_add_fs(struct acpi_device *device)
  485. {
  486. struct proc_dir_entry *entry = NULL;
  487. if (!acpi_device_dir(device)) {
  488. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  489. acpi_ec_dir);
  490. if (!acpi_device_dir(device))
  491. return -ENODEV;
  492. }
  493. entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
  494. acpi_device_dir(device));
  495. if (!entry)
  496. return -ENODEV;
  497. else {
  498. entry->proc_fops = &acpi_ec_info_ops;
  499. entry->data = acpi_driver_data(device);
  500. entry->owner = THIS_MODULE;
  501. }
  502. return 0;
  503. }
  504. static int acpi_ec_remove_fs(struct acpi_device *device)
  505. {
  506. if (acpi_device_dir(device)) {
  507. remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
  508. remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
  509. acpi_device_dir(device) = NULL;
  510. }
  511. return 0;
  512. }
  513. /* --------------------------------------------------------------------------
  514. Driver Interface
  515. -------------------------------------------------------------------------- */
  516. static int acpi_ec_add(struct acpi_device *device)
  517. {
  518. int result = 0;
  519. acpi_status status = AE_OK;
  520. struct acpi_ec *ec = NULL;
  521. if (!device)
  522. return -EINVAL;
  523. ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  524. if (!ec)
  525. return -ENOMEM;
  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 = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  702. if (!ec_ecdt) {
  703. ret = -ENOMEM;
  704. goto error;
  705. }
  706. status = acpi_get_devices(ACPI_EC_HID,
  707. acpi_fake_ecdt_callback, NULL, NULL);
  708. if (ACPI_FAILURE(status)) {
  709. kfree(ec_ecdt);
  710. ec_ecdt = NULL;
  711. ret = -ENODEV;
  712. ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
  713. goto error;
  714. }
  715. return 0;
  716. error:
  717. return ret;
  718. }
  719. static int __init acpi_ec_get_real_ecdt(void)
  720. {
  721. acpi_status status;
  722. struct acpi_table_ecdt *ecdt_ptr;
  723. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  724. (struct acpi_table_header **)&ecdt_ptr);
  725. if (ACPI_FAILURE(status))
  726. return -ENODEV;
  727. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
  728. /*
  729. * Generate a temporary ec context to use until the namespace is scanned
  730. */
  731. ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  732. if (!ec_ecdt)
  733. return -ENOMEM;
  734. mutex_init(&ec_ecdt->lock);
  735. if (acpi_ec_mode == EC_INTR) {
  736. init_waitqueue_head(&ec_ecdt->wait);
  737. }
  738. ec_ecdt->command_addr = ecdt_ptr->control.address;
  739. ec_ecdt->data_addr = ecdt_ptr->data.address;
  740. ec_ecdt->gpe = ecdt_ptr->gpe;
  741. /* use the GL just to be safe */
  742. ec_ecdt->global_lock = TRUE;
  743. ec_ecdt->uid = ecdt_ptr->uid;
  744. status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
  745. if (ACPI_FAILURE(status)) {
  746. goto error;
  747. }
  748. return 0;
  749. error:
  750. ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
  751. kfree(ec_ecdt);
  752. ec_ecdt = NULL;
  753. return -ENODEV;
  754. }
  755. static int __initdata acpi_fake_ecdt_enabled;
  756. int __init acpi_ec_ecdt_probe(void)
  757. {
  758. acpi_status status;
  759. int ret;
  760. ret = acpi_ec_get_real_ecdt();
  761. /* Try to make a fake ECDT */
  762. if (ret && acpi_fake_ecdt_enabled) {
  763. ret = acpi_ec_fake_ecdt();
  764. }
  765. if (ret)
  766. return 0;
  767. /*
  768. * Install GPE handler
  769. */
  770. status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
  771. ACPI_GPE_EDGE_TRIGGERED,
  772. &acpi_ec_gpe_handler, ec_ecdt);
  773. if (ACPI_FAILURE(status)) {
  774. goto error;
  775. }
  776. acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
  777. acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
  778. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  779. ACPI_ADR_SPACE_EC,
  780. &acpi_ec_space_handler,
  781. &acpi_ec_space_setup,
  782. ec_ecdt);
  783. if (ACPI_FAILURE(status)) {
  784. acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
  785. &acpi_ec_gpe_handler);
  786. goto error;
  787. }
  788. return 0;
  789. error:
  790. ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
  791. kfree(ec_ecdt);
  792. ec_ecdt = NULL;
  793. return -ENODEV;
  794. }
  795. static int __init acpi_ec_init(void)
  796. {
  797. int result = 0;
  798. if (acpi_disabled)
  799. return 0;
  800. acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
  801. if (!acpi_ec_dir)
  802. return -ENODEV;
  803. /* Now register the driver for the EC */
  804. result = acpi_bus_register_driver(&acpi_ec_driver);
  805. if (result < 0) {
  806. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  807. return -ENODEV;
  808. }
  809. return result;
  810. }
  811. subsys_initcall(acpi_ec_init);
  812. /* EC driver currently not unloadable */
  813. #if 0
  814. static void __exit acpi_ec_exit(void)
  815. {
  816. acpi_bus_unregister_driver(&acpi_ec_driver);
  817. remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
  818. return;
  819. }
  820. #endif /* 0 */
  821. static int __init acpi_fake_ecdt_setup(char *str)
  822. {
  823. acpi_fake_ecdt_enabled = 1;
  824. return 1;
  825. }
  826. __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
  827. static int __init acpi_ec_set_intr_mode(char *str)
  828. {
  829. int intr;
  830. if (!get_option(&str, &intr))
  831. return 0;
  832. if (intr) {
  833. acpi_ec_mode = EC_INTR;
  834. } else {
  835. acpi_ec_mode = EC_POLL;
  836. }
  837. acpi_ec_driver.ops.add = acpi_ec_add;
  838. printk(KERN_NOTICE PREFIX "%s mode.\n",
  839. intr ? "interrupt" : "polling");
  840. return 1;
  841. }
  842. __setup("ec_intr=", acpi_ec_set_intr_mode);