blob: 94b983b9cfbae03fa93799d78b389d46a4efef11 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
Dmitry Torokhov451566f2005-03-19 01:10:05 -050034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/io.h>
36#include <acpi/acpi_bus.h>
37#include <acpi/acpi_drivers.h>
38#include <acpi/actypes.h>
39
40#define _COMPONENT ACPI_EC_COMPONENT
Len Brown50526df2005-08-11 17:32:05 -040041ACPI_MODULE_NAME("acpi_ec")
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ACPI_EC_COMPONENT 0x00100000
43#define ACPI_EC_CLASS "embedded_controller"
44#define ACPI_EC_HID "PNP0C09"
45#define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
46#define ACPI_EC_DEVICE_NAME "Embedded Controller"
47#define ACPI_EC_FILE_INFO "info"
Denis M. Sadykov703959d2006-09-26 19:50:33 +040048
49/* EC status register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
Dmitry Torokhov451566f2005-03-19 01:10:05 -050052#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040054
55/* EC commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define ACPI_EC_COMMAND_READ 0x80
57#define ACPI_EC_COMMAND_WRITE 0x81
Dmitry Torokhov451566f2005-03-19 01:10:05 -050058#define ACPI_EC_BURST_ENABLE 0x82
59#define ACPI_EC_BURST_DISABLE 0x83
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define ACPI_EC_COMMAND_QUERY 0x84
Denis M. Sadykov703959d2006-09-26 19:50:33 +040061
62/* EC events */
63enum {
64 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
65 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
66};
67
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030068#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040069#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
70#define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
Alexey Starikovskiy5c406412006-12-07 18:42:16 +030071#define ACPI_EC_UDELAY_COUNT 1000 /* Wait 100ms max. during EC ops */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040072
73enum {
74 EC_INTR = 1, /* Output buffer full */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040075 EC_POLL, /* Input buffer empty */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040076};
77
Len Brown50526df2005-08-11 17:32:05 -040078static int acpi_ec_remove(struct acpi_device *device, int type);
79static int acpi_ec_start(struct acpi_device *device);
80static int acpi_ec_stop(struct acpi_device *device, int type);
Denis M. Sadykov703959d2006-09-26 19:50:33 +040081static int acpi_ec_add(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct acpi_driver acpi_ec_driver = {
Len Brown50526df2005-08-11 17:32:05 -040084 .name = ACPI_EC_DRIVER_NAME,
85 .class = ACPI_EC_CLASS,
86 .ids = ACPI_EC_HID,
87 .ops = {
Denis M. Sadykov703959d2006-09-26 19:50:33 +040088 .add = acpi_ec_add,
Len Brown50526df2005-08-11 17:32:05 -040089 .remove = acpi_ec_remove,
90 .start = acpi_ec_start,
91 .stop = acpi_ec_stop,
92 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +040094
95/* If we find an EC via the ECDT, we need to keep a ptr to its context */
Denis M. Sadykov703959d2006-09-26 19:50:33 +040096struct acpi_ec {
97 acpi_handle handle;
98 unsigned long uid;
99 unsigned long gpe_bit;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400100 unsigned long command_addr;
101 unsigned long data_addr;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400102 unsigned long global_lock;
103 struct semaphore sem;
104 unsigned int expect_event;
105 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
106 wait_queue_head_t wait;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400107} *ec_ecdt;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400108
109/* External interfaces use first EC only, so remember */
110static struct acpi_device *first_ec;
111static int acpi_ec_mode = EC_INTR;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* --------------------------------------------------------------------------
114 Transaction Management
115 -------------------------------------------------------------------------- */
116
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400117static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400119 return inb(ec->command_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400122static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400123{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400124 return inb(ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400125}
126
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400127static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400128{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400129 outb(command, ec->command_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400130}
131
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400132static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400133{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400134 outb(data, ec->data_addr);
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400135}
136
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300137static int acpi_ec_check_status(struct acpi_ec *ec, u8 event)
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400138{
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300139 u8 status = acpi_ec_read_status(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400140 switch (event) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400141 case ACPI_EC_EVENT_OBF_1:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400142 if (status & ACPI_EC_FLAG_OBF)
143 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400144 break;
145 case ACPI_EC_EVENT_IBF_0:
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400146 if (!(status & ACPI_EC_FLAG_IBF))
147 return 1;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400148 break;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400149 default:
150 break;
151 }
152
153 return 0;
154}
155
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400156static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
Luming Yu45bea152005-07-23 04:08:00 -0400157{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400158 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
159 long time_left;
Luming Yu45bea152005-07-23 04:08:00 -0400160
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400161 ec->expect_event = event;
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300162 if (acpi_ec_check_status(ec, event)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400163 ec->expect_event = 0;
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400164 return 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500165 }
166
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400167 do {
168 if (acpi_ec_mode == EC_POLL) {
169 udelay(ACPI_EC_UDELAY);
170 } else {
171 time_left = wait_event_timeout(ec->wait,
172 !ec->expect_event,
173 msecs_to_jiffies(ACPI_EC_DELAY));
174 if (time_left > 0) {
175 ec->expect_event = 0;
176 return 0;
177 }
178 }
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300179 if (acpi_ec_check_status(ec, event)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400180 ec->expect_event = 0;
181 return 0;
182 }
183 } while (--i > 0);
184
185 ec->expect_event = 0;
186
Patrick Mocheld550d982006-06-27 00:41:40 -0400187 return -ETIME;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500188}
189
Len Brown02b28a32005-12-05 16:33:04 -0500190#ifdef ACPI_FUTURE_USAGE
Luming Yu06a2a382005-09-27 00:43:00 -0400191/*
192 * Note: samsung nv5000 doesn't work with ec burst mode.
193 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
194 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400195int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500196{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400197 u8 tmp = 0;
198 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500199
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500200
201 status = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400202 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400203 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Len Brown50526df2005-08-11 17:32:05 -0400204 if (status)
Luming Yu716e0842005-08-10 01:40:00 -0400205 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400206 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
207 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
208 tmp = acpi_ec_read_data(ec);
Len Brown50526df2005-08-11 17:32:05 -0400209 if (tmp != 0x90) { /* Burst ACK byte */
Patrick Mocheld550d982006-06-27 00:41:40 -0400210 return -EINVAL;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500211 }
Luming Yu668d74c2005-07-23 00:26:33 -0400212 }
213
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400214 atomic_set(&ec->leaving_burst, 0);
Patrick Mocheld550d982006-06-27 00:41:40 -0400215 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400216 end:
217 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400218 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500219}
220
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400221int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500222{
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400223 u8 status = 0;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500224
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500225
Luming Yu06a2a382005-09-27 00:43:00 -0400226 status = acpi_ec_read_status(ec);
227 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400228 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu06a2a382005-09-27 00:43:00 -0400229 if(status)
230 goto end;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400231 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
232 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400233 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400234 atomic_set(&ec->leaving_burst, 1);
Patrick Mocheld550d982006-06-27 00:41:40 -0400235 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400236 end:
237 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400238 return -1;
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500239}
Len Brown02b28a32005-12-05 16:33:04 -0500240#endif /* ACPI_FUTURE_USAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400242static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400243 const u8 *wdata, unsigned wdata_len,
244 u8 *rdata, unsigned rdata_len)
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400245{
246 int result;
247
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400248 acpi_ec_write_cmd(ec, command);
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400249
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400250 for (; wdata_len > 0; wdata_len --) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400251 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400252 if (result)
253 return result;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400254 acpi_ec_write_data(ec, *(wdata++));
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400255 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400256
Alexey Starikovskiyd91df1a2006-12-07 18:42:16 +0300257 if (!rdata_len) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400258 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400259 if (result)
260 return result;
261 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400262
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400263 for (; rdata_len > 0; rdata_len --) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400264 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400265 if (result)
266 return result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400267
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400268 *(rdata++) = acpi_ec_read_data(ec);
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400269 }
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400270
Denis M. Sadykov7c6db5e2006-09-26 19:50:33 +0400271 return 0;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400272}
273
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400274static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
275 const u8 *wdata, unsigned wdata_len,
276 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400277{
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400278 int status;
Len Brown50526df2005-08-11 17:32:05 -0400279 u32 glk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400281 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400284 if (rdata)
285 memset(rdata, 0, rdata_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400287 if (ec->global_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
289 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400292 down(&ec->sem);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500293
Alexey Starikovskiy5d57a6a2006-12-07 18:42:16 +0300294 /* Make sure GPE is enabled before doing transaction */
295 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
296
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400297 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
Luming Yu716e0842005-08-10 01:40:00 -0400298 if (status) {
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400299 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500300 goto end;
Luming Yu716e0842005-08-10 01:40:00 -0400301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400303 status = acpi_ec_transaction_unlocked(ec, command,
304 wdata, wdata_len,
305 rdata, rdata_len);
Len Brown50526df2005-08-11 17:32:05 -0400306
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400307end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400308 up(&ec->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400310 if (ec->global_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 acpi_release_global_lock(glk);
312
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400316static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400317{
318 int result;
319 u8 d;
320
321 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
322 &address, 1, &d, 1);
323 *data = d;
324 return result;
325}
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400326
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400327static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
328{
329 u8 wdata[2] = { address, data };
330 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
331 wdata, 2, NULL, 0);
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
335 * Externally callable EC access functions. For now, assume 1 EC only
336 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400337int ec_read(u8 addr, u8 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400339 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 int err;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400341 u8 temp_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (!first_ec)
344 return -ENODEV;
345
346 ec = acpi_driver_data(first_ec);
347
348 err = acpi_ec_read(ec, addr, &temp_data);
349
350 if (!err) {
351 *val = temp_data;
352 return 0;
Len Brown50526df2005-08-11 17:32:05 -0400353 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return err;
355}
Len Brown50526df2005-08-11 17:32:05 -0400356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357EXPORT_SYMBOL(ec_read);
358
Len Brown50526df2005-08-11 17:32:05 -0400359int ec_write(u8 addr, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400361 struct acpi_ec *ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int err;
363
364 if (!first_ec)
365 return -ENODEV;
366
367 ec = acpi_driver_data(first_ec);
368
369 err = acpi_ec_write(ec, addr, val);
370
371 return err;
372}
Len Brown50526df2005-08-11 17:32:05 -0400373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374EXPORT_SYMBOL(ec_write);
375
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400376extern int ec_transaction(u8 command,
377 const u8 *wdata, unsigned wdata_len,
378 u8 *rdata, unsigned rdata_len)
Luming Yu45bea152005-07-23 04:08:00 -0400379{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400380 struct acpi_ec *ec;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400381
382 if (!first_ec)
383 return -ENODEV;
384
385 ec = acpi_driver_data(first_ec);
386
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400387 return acpi_ec_transaction(ec, command, wdata,
388 wdata_len, rdata, rdata_len);
Luming Yu45bea152005-07-23 04:08:00 -0400389}
Luming Yu45bea152005-07-23 04:08:00 -0400390
Lennart Poetteringab9e43c2006-10-03 22:49:00 -0400391EXPORT_SYMBOL(ec_transaction);
392
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400393static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
Denis M. Sadykov3576cf62006-09-26 19:50:33 +0400394{
395 int result;
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400396 u8 d;
Luming Yu45bea152005-07-23 04:08:00 -0400397
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400398 if (!ec || !data)
399 return -EINVAL;
Luming Yu45bea152005-07-23 04:08:00 -0400400
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400401 /*
402 * Query the EC to find out which _Qxx method we need to evaluate.
403 * Note that successful completion of the query causes the ACPI_EC_SCI
404 * bit to be cleared (and thus clearing the interrupt source).
405 */
Luming Yu45bea152005-07-23 04:08:00 -0400406
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400407 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
408 if (result)
409 return result;
Luming Yu45bea152005-07-23 04:08:00 -0400410
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400411 if (!d)
412 return -ENODATA;
Luming Yu45bea152005-07-23 04:08:00 -0400413
Lennart Poetteringd7a76e42006-09-05 12:12:24 -0400414 *data = d;
415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* --------------------------------------------------------------------------
419 Event Management
420 -------------------------------------------------------------------------- */
421
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400422struct acpi_ec_query_data {
Len Brown50526df2005-08-11 17:32:05 -0400423 acpi_handle handle;
424 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425};
426
Len Brown50526df2005-08-11 17:32:05 -0400427static void acpi_ec_gpe_query(void *ec_cxt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400429 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400430 u8 value = 0;
431 static char object_name[8];
Luming Yu45bea152005-07-23 04:08:00 -0400432
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400433 if (!ec)
Luming Yu45bea152005-07-23 04:08:00 -0400434 goto end;
435
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400436 value = acpi_ec_read_status(ec);
Luming Yu45bea152005-07-23 04:08:00 -0400437
Luming Yu45bea152005-07-23 04:08:00 -0400438 if (!(value & ACPI_EC_FLAG_SCI))
439 goto end;
440
441 if (acpi_ec_query(ec, &value))
442 goto end;
443
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400444 snprintf(object_name, 8, "_Q%2.2X", value);
Luming Yu45bea152005-07-23 04:08:00 -0400445
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400446 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
Luming Yu45bea152005-07-23 04:08:00 -0400447
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400448 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
Luming Yu45bea152005-07-23 04:08:00 -0400449
Len Brown50526df2005-08-11 17:32:05 -0400450 end:
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400451 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Luming Yu45bea152005-07-23 04:08:00 -0400452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Len Brown50526df2005-08-11 17:32:05 -0400454static u32 acpi_ec_gpe_handler(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Len Brown50526df2005-08-11 17:32:05 -0400456 acpi_status status = AE_OK;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400457 u8 value;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400458 struct acpi_ec *ec = (struct acpi_ec *)data;
Luming Yu45bea152005-07-23 04:08:00 -0400459
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400460 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400462 if (acpi_ec_mode == EC_INTR) {
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300463 if (acpi_ec_check_status(ec, ec->expect_event)) {
Denis M. Sadykov8e0341b2006-09-26 19:50:33 +0400464 ec->expect_event = 0;
465 wake_up(&ec->wait);
466 }
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500467 }
468
Alexey Starikovskiybec5a1e2006-12-07 18:42:16 +0300469 value = acpi_ec_read_status(ec);
Len Brown50526df2005-08-11 17:32:05 -0400470 if (value & ACPI_EC_FLAG_SCI) {
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400471 status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
Luming Yu17e9c782005-04-22 23:07:10 -0400472 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400473 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
474 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400475 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
Dmitry Torokhov451566f2005-03-19 01:10:05 -0500476 return status == AE_OK ?
Len Brown50526df2005-08-11 17:32:05 -0400477 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480/* --------------------------------------------------------------------------
481 Address Space Management
482 -------------------------------------------------------------------------- */
483
484static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400485acpi_ec_space_setup(acpi_handle region_handle,
486 u32 function, void *handler_context, void **return_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 /*
489 * The EC object is in the handler context and is needed
490 * when calling the acpi_ec_space_handler.
491 */
Len Brown50526df2005-08-11 17:32:05 -0400492 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
493 handler_context : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 return AE_OK;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400499acpi_ec_space_handler(u32 function,
500 acpi_physical_address address,
501 u32 bit_width,
502 acpi_integer * value,
503 void *handler_context, void *region_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Len Brown50526df2005-08-11 17:32:05 -0400505 int result = 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400506 struct acpi_ec *ec = NULL;
Len Brown50526df2005-08-11 17:32:05 -0400507 u64 temp = *value;
508 acpi_integer f_v = 0;
509 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if ((address > 0xFF) || !value || !handler_context)
Patrick Mocheld550d982006-06-27 00:41:40 -0400513 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Luming Yufa9cd542005-03-19 01:54:47 -0500515 if (bit_width != 8 && acpi_strict) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400516 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400519 ec = (struct acpi_ec *)handler_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Len Brown50526df2005-08-11 17:32:05 -0400521 next_byte:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 switch (function) {
523 case ACPI_READ:
Luming Yufa9cd542005-03-19 01:54:47 -0500524 temp = 0;
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400525 result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527 case ACPI_WRITE:
Luming Yufa9cd542005-03-19 01:54:47 -0500528 result = acpi_ec_write(ec, (u8) address, (u8) temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 default:
531 result = -EINVAL;
532 goto out;
533 break;
534 }
535
536 bit_width -= 8;
Luming Yufa9cd542005-03-19 01:54:47 -0500537 if (bit_width) {
538 if (function == ACPI_READ)
539 f_v |= temp << 8 * i;
540 if (function == ACPI_WRITE)
541 temp >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 i++;
Andrew Morton83ea7442005-03-30 22:12:13 -0500543 address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto next_byte;
545 }
546
Luming Yufa9cd542005-03-19 01:54:47 -0500547 if (function == ACPI_READ) {
548 f_v |= temp << 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *value = f_v;
550 }
551
Len Brown50526df2005-08-11 17:32:05 -0400552 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 switch (result) {
554 case -EINVAL:
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557 case -ENODEV:
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return AE_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 case -ETIME:
Patrick Mocheld550d982006-06-27 00:41:40 -0400561 return AE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563 default:
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/* --------------------------------------------------------------------------
569 FS Interface (/proc)
570 -------------------------------------------------------------------------- */
571
Len Brown50526df2005-08-11 17:32:05 -0400572static struct proc_dir_entry *acpi_ec_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Len Brown50526df2005-08-11 17:32:05 -0400574static int acpi_ec_read_info(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400576 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (!ec)
580 goto end;
581
582 seq_printf(seq, "gpe bit: 0x%02x\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400583 (u32) ec->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400585 (u32) ec->command_addr,
586 (u32) ec->data_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 seq_printf(seq, "use global lock: %s\n",
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400588 ec->global_lock ? "yes" : "no");
589 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Len Brown50526df2005-08-11 17:32:05 -0400591 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
596{
597 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
598}
599
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400600static struct file_operations acpi_ec_info_ops = {
Len Brown50526df2005-08-11 17:32:05 -0400601 .open = acpi_ec_info_open_fs,
602 .read = seq_read,
603 .llseek = seq_lseek,
604 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 .owner = THIS_MODULE,
606};
607
Len Brown50526df2005-08-11 17:32:05 -0400608static int acpi_ec_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Len Brown50526df2005-08-11 17:32:05 -0400610 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (!acpi_device_dir(device)) {
614 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown50526df2005-08-11 17:32:05 -0400615 acpi_ec_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
Len Brown50526df2005-08-11 17:32:05 -0400621 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 else {
625 entry->proc_fops = &acpi_ec_info_ops;
626 entry->data = acpi_driver_data(device);
627 entry->owner = THIS_MODULE;
628 }
629
Patrick Mocheld550d982006-06-27 00:41:40 -0400630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Len Brown50526df2005-08-11 17:32:05 -0400633static int acpi_ec_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (acpi_device_dir(device)) {
637 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
638 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
639 acpi_device_dir(device) = NULL;
640 }
641
Patrick Mocheld550d982006-06-27 00:41:40 -0400642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/* --------------------------------------------------------------------------
646 Driver Interface
647 -------------------------------------------------------------------------- */
648
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400649static int acpi_ec_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Len Brown50526df2005-08-11 17:32:05 -0400651 int result = 0;
652 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400653 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400657 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400659 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400662 memset(ec, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400664 ec->handle = device->handle;
665 ec->uid = -1;
666 init_MUTEX(&ec->sem);
667 if (acpi_ec_mode == EC_INTR) {
668 atomic_set(&ec->leaving_burst, 1);
669 init_waitqueue_head(&ec->wait);
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
672 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
673 acpi_driver_data(device) = ec;
674
675 /* Use the global lock for all EC transactions? */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400676 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
677 &ec->global_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jiri Slabyff2fc3e2006-03-28 17:04:00 -0500679 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
680 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
681 if (ec_ecdt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Len Brown50526df2005-08-11 17:32:05 -0400683 ACPI_ADR_SPACE_EC,
684 &acpi_ec_space_handler);
685
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400686 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400687 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 kfree(ec_ecdt);
690 }
691
692 /* Get GPE bit assignment (EC events). */
693 /* TODO: Add support for _GPE returning a package */
Len Brown50526df2005-08-11 17:32:05 -0400694 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400695 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
696 &ec->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400698 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 result = -ENODEV;
700 goto end;
701 }
702
703 result = acpi_ec_add_fs(device);
704 if (result)
705 goto end;
706
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400707 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
Len Brown50526df2005-08-11 17:32:05 -0400708 acpi_device_name(device), acpi_device_bid(device),
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400709 (u32) ec->gpe_bit));
Luming Yu45bea152005-07-23 04:08:00 -0400710
711 if (!first_ec)
712 first_ec = device;
713
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400714 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (result)
716 kfree(ec);
717
Patrick Mocheld550d982006-06-27 00:41:40 -0400718 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Len Brown50526df2005-08-11 17:32:05 -0400721static int acpi_ec_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400723 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400727 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 ec = acpi_driver_data(device);
730
731 acpi_ec_remove_fs(device);
732
733 kfree(ec);
734
Patrick Mocheld550d982006-06-27 00:41:40 -0400735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static acpi_status
Len Brown50526df2005-08-11 17:32:05 -0400739acpi_ec_io_ports(struct acpi_resource *resource, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400741 struct acpi_ec *ec = (struct acpi_ec *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Bob Moore50eca3e2005-09-30 19:03:00 -0400743 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return AE_OK;
745 }
746
747 /*
748 * The first address region returned is the data port, and
749 * the second address region returned is the status/command
750 * port.
751 */
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400752 if (ec->data_addr == 0) {
753 ec->data_addr = resource->data.io.minimum;
754 } else if (ec->command_addr == 0) {
755 ec->command_addr = resource->data.io.minimum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 } else {
757 return AE_CTRL_TERMINATE;
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return AE_OK;
761}
762
Len Brown50526df2005-08-11 17:32:05 -0400763static int acpi_ec_start(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Len Brown50526df2005-08-11 17:32:05 -0400765 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400766 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400770 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 ec = acpi_driver_data(device);
773
774 if (!ec)
Patrick Mocheld550d982006-06-27 00:41:40 -0400775 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * Get I/O port addresses. Convert to GAS format.
779 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400780 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400781 acpi_ec_io_ports, ec);
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400782 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400783 ACPI_EXCEPTION((AE_INFO, status,
784 "Error getting I/O port addresses"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400785 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400788 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
789 ec->gpe_bit, ec->command_addr, ec->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /*
792 * Install GPE handler
793 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400794 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400795 ACPI_GPE_EDGE_TRIGGERED,
796 &acpi_ec_gpe_handler, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (ACPI_FAILURE(status)) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400798 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400800 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
801 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400803 status = acpi_install_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400804 ACPI_ADR_SPACE_EC,
805 &acpi_ec_space_handler,
806 &acpi_ec_space_setup, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400808 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400809 &acpi_ec_gpe_handler);
Patrick Mocheld550d982006-06-27 00:41:40 -0400810 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Patrick Mocheld550d982006-06-27 00:41:40 -0400813 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Len Brown50526df2005-08-11 17:32:05 -0400816static int acpi_ec_stop(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Len Brown50526df2005-08-11 17:32:05 -0400818 acpi_status status = AE_OK;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400819 struct acpi_ec *ec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400823 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 ec = acpi_driver_data(device);
826
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400827 status = acpi_remove_address_space_handler(ec->handle,
Len Brown50526df2005-08-11 17:32:05 -0400828 ACPI_ADR_SPACE_EC,
829 &acpi_ec_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400831 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Len Brown50526df2005-08-11 17:32:05 -0400833 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400834 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400835 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400837 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Patrick Mocheld550d982006-06-27 00:41:40 -0400839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842static acpi_status __init
Len Brown50526df2005-08-11 17:32:05 -0400843acpi_fake_ecdt_callback(acpi_handle handle,
844 u32 Level, void *context, void **retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Len Brown50526df2005-08-11 17:32:05 -0400846 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400848 init_MUTEX(&ec_ecdt->sem);
849 if (acpi_ec_mode == EC_INTR) {
850 init_waitqueue_head(&ec_ecdt->wait);
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Len Brown50526df2005-08-11 17:32:05 -0400853 acpi_ec_io_ports, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (ACPI_FAILURE(status))
855 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400857 ec_ecdt->uid = -1;
858 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Len Brown50526df2005-08-11 17:32:05 -0400860 status =
861 acpi_evaluate_integer(handle, "_GPE", NULL,
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400862 &ec_ecdt->gpe_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (ACPI_FAILURE(status))
864 return status;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400865 ec_ecdt->global_lock = TRUE;
866 ec_ecdt->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400868 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
869 ec_ecdt->gpe_bit, ec_ecdt->command_addr, ec_ecdt->data_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 return AE_CTRL_TERMINATE;
872}
873
874/*
875 * Some BIOS (such as some from Gateway laptops) access EC region very early
876 * such as in BAT0._INI or EC._INI before an EC device is found and
877 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
878 * required, but if EC regison is accessed early, it is required.
879 * The routine tries to workaround the BIOS bug by pre-scan EC device
880 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
881 * op region (since _REG isn't invoked yet). The assumption is true for
882 * all systems found.
883 */
Len Brown50526df2005-08-11 17:32:05 -0400884static int __init acpi_ec_fake_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Len Brown50526df2005-08-11 17:32:05 -0400886 acpi_status status;
887 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400889 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400891 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!ec_ecdt) {
893 ret = -ENOMEM;
894 goto error;
895 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400896 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Len Brown50526df2005-08-11 17:32:05 -0400898 status = acpi_get_devices(ACPI_EC_HID,
899 acpi_fake_ecdt_callback, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (ACPI_FAILURE(status)) {
901 kfree(ec_ecdt);
902 ec_ecdt = NULL;
903 ret = -ENODEV;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400904 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto error;
906 }
907 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400908 error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return ret;
910}
911
Len Brown50526df2005-08-11 17:32:05 -0400912static int __init acpi_ec_get_real_ecdt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Len Brown50526df2005-08-11 17:32:05 -0400914 acpi_status status;
915 struct acpi_table_ecdt *ecdt_ptr;
Luming Yu45bea152005-07-23 04:08:00 -0400916
Len Brown50526df2005-08-11 17:32:05 -0400917 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
918 (struct acpi_table_header **)
919 &ecdt_ptr);
Luming Yu45bea152005-07-23 04:08:00 -0400920 if (ACPI_FAILURE(status))
921 return -ENODEV;
922
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400923 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
Luming Yu45bea152005-07-23 04:08:00 -0400924
925 /*
926 * Generate a temporary ec context to use until the namespace is scanned
927 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400928 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
Luming Yu45bea152005-07-23 04:08:00 -0400929 if (!ec_ecdt)
930 return -ENOMEM;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400931 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
Luming Yu45bea152005-07-23 04:08:00 -0400932
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400933 init_MUTEX(&ec_ecdt->sem);
934 if (acpi_ec_mode == EC_INTR) {
935 init_waitqueue_head(&ec_ecdt->wait);
936 }
Denis M. Sadykov6ffb2212006-09-26 19:50:33 +0400937 ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
938 ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400939 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
Luming Yu45bea152005-07-23 04:08:00 -0400940 /* use the GL just to be safe */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400941 ec_ecdt->global_lock = TRUE;
942 ec_ecdt->uid = ecdt_ptr->uid;
Luming Yu45bea152005-07-23 04:08:00 -0400943
Len Brown50526df2005-08-11 17:32:05 -0400944 status =
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400945 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
Luming Yu45bea152005-07-23 04:08:00 -0400946 if (ACPI_FAILURE(status)) {
947 goto error;
948 }
949
950 return 0;
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400951 error:
952 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree(ec_ecdt);
954 ec_ecdt = NULL;
955
956 return -ENODEV;
957}
958
959static int __initdata acpi_fake_ecdt_enabled;
Len Brown50526df2005-08-11 17:32:05 -0400960int __init acpi_ec_ecdt_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Len Brown50526df2005-08-11 17:32:05 -0400962 acpi_status status;
963 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 ret = acpi_ec_get_real_ecdt();
966 /* Try to make a fake ECDT */
967 if (ret && acpi_fake_ecdt_enabled) {
968 ret = acpi_ec_fake_ecdt();
969 }
970
971 if (ret)
972 return 0;
973
974 /*
975 * Install GPE handler
976 */
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400977 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400978 ACPI_GPE_EDGE_TRIGGERED,
979 &acpi_ec_gpe_handler, ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (ACPI_FAILURE(status)) {
981 goto error;
982 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400983 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
984 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Len Brown50526df2005-08-11 17:32:05 -0400986 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
987 ACPI_ADR_SPACE_EC,
988 &acpi_ec_space_handler,
989 &acpi_ec_space_setup,
990 ec_ecdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (ACPI_FAILURE(status)) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +0400992 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
Len Brown50526df2005-08-11 17:32:05 -0400993 &acpi_ec_gpe_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto error;
995 }
996
997 return 0;
998
Len Brown50526df2005-08-11 17:32:05 -0400999 error:
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001000 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 kfree(ec_ecdt);
1002 ec_ecdt = NULL;
1003
1004 return -ENODEV;
1005}
1006
Len Brown50526df2005-08-11 17:32:05 -04001007static int __init acpi_ec_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Len Brown50526df2005-08-11 17:32:05 -04001009 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1016 if (!acpi_ec_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* Now register the driver for the EC */
1020 result = acpi_bus_register_driver(&acpi_ec_driver);
1021 if (result < 0) {
1022 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001023 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029subsys_initcall(acpi_ec_init);
1030
1031/* EC driver currently not unloadable */
1032#if 0
Len Brown50526df2005-08-11 17:32:05 -04001033static void __exit acpi_ec_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 acpi_bus_unregister_driver(&acpi_ec_driver);
1037
1038 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1039
Patrick Mocheld550d982006-06-27 00:41:40 -04001040 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
Len Brown50526df2005-08-11 17:32:05 -04001042#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044static int __init acpi_fake_ecdt_setup(char *str)
1045{
1046 acpi_fake_ecdt_enabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001047 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
Luming Yu7b15f5e2005-08-03 17:38:04 -04001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
Len Brown02b28a32005-12-05 16:33:04 -05001051static int __init acpi_ec_set_intr_mode(char *str)
Luming Yu45bea152005-07-23 04:08:00 -04001052{
Len Brown02b28a32005-12-05 16:33:04 -05001053 int intr;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001054
Len Brown02b28a32005-12-05 16:33:04 -05001055 if (!get_option(&str, &intr))
Luming Yu7b15f5e2005-08-03 17:38:04 -04001056 return 0;
1057
Len Brown02b28a32005-12-05 16:33:04 -05001058 if (intr) {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001059 acpi_ec_mode = EC_INTR;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001060 } else {
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001061 acpi_ec_mode = EC_POLL;
Luming Yu7b15f5e2005-08-03 17:38:04 -04001062 }
Denis M. Sadykov703959d2006-09-26 19:50:33 +04001063 acpi_ec_driver.ops.add = acpi_ec_add;
1064 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1065
OGAWA Hirofumi9b410462006-03-31 02:30:33 -08001066 return 1;
Luming Yu45bea152005-07-23 04:08:00 -04001067}
Len Brown50526df2005-08-11 17:32:05 -04001068
Len Brown53f11d42005-12-05 16:46:36 -05001069__setup("ec_intr=", acpi_ec_set_intr_mode);