ec.c 24 KB

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