ec.c 29.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 *  acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
 *
 *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
D
Dmitry Torokhov 已提交
34
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
35 36 37 38 39 40
#include <asm/io.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>
#include <acpi/actypes.h>

#define _COMPONENT		ACPI_EC_COMPONENT
L
Len Brown 已提交
41
ACPI_MODULE_NAME("acpi_ec")
L
Linus Torvalds 已提交
42 43 44 45 46 47
#define ACPI_EC_COMPONENT		0x00100000
#define ACPI_EC_CLASS			"embedded_controller"
#define ACPI_EC_HID			"PNP0C09"
#define ACPI_EC_DRIVER_NAME		"ACPI Embedded Controller Driver"
#define ACPI_EC_DEVICE_NAME		"Embedded Controller"
#define ACPI_EC_FILE_INFO		"info"
48 49

/* EC status register */
L
Linus Torvalds 已提交
50 51
#define ACPI_EC_FLAG_OBF	0x01	/* Output buffer full */
#define ACPI_EC_FLAG_IBF	0x02	/* Input buffer full */
D
Dmitry Torokhov 已提交
52
#define ACPI_EC_FLAG_BURST	0x10	/* burst mode */
L
Linus Torvalds 已提交
53
#define ACPI_EC_FLAG_SCI	0x20	/* EC-SCI occurred */
54 55

/* EC commands */
L
Linus Torvalds 已提交
56 57
#define ACPI_EC_COMMAND_READ	0x80
#define ACPI_EC_COMMAND_WRITE	0x81
D
Dmitry Torokhov 已提交
58 59
#define ACPI_EC_BURST_ENABLE	0x82
#define ACPI_EC_BURST_DISABLE	0x83
L
Linus Torvalds 已提交
60
#define ACPI_EC_COMMAND_QUERY	0x84
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/* EC events */
enum {
	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
	ACPI_EC_EVENT_IBF_0,		/* Input buffer empty */
};

#define ACPI_EC_DELAY		50	/* Wait 50ms max. during EC ops */
#define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */
#define ACPI_EC_UDELAY         100	/* Poll @ 100us increments */
#define ACPI_EC_UDELAY_COUNT   1000	/* Wait 10ms max. during EC ops */

enum {
	EC_INTR = 1,	/* Output buffer full */
	EC_POLL,		/* Input buffer empty */
};

L
Len Brown 已提交
78 79 80
static int acpi_ec_remove(struct acpi_device *device, int type);
static int acpi_ec_start(struct acpi_device *device);
static int acpi_ec_stop(struct acpi_device *device, int type);
81
static int acpi_ec_add(struct acpi_device *device);
L
Linus Torvalds 已提交
82 83

static struct acpi_driver acpi_ec_driver = {
L
Len Brown 已提交
84 85 86 87
	.name = ACPI_EC_DRIVER_NAME,
	.class = ACPI_EC_CLASS,
	.ids = ACPI_EC_HID,
	.ops = {
88
		.add = acpi_ec_add,
L
Len Brown 已提交
89 90 91 92
		.remove = acpi_ec_remove,
		.start = acpi_ec_start,
		.stop = acpi_ec_stop,
		},
L
Linus Torvalds 已提交
93
};
94 95 96 97 98 99 100 101 102 103 104 105
struct acpi_ec {
	acpi_handle handle;
	unsigned long uid;
	unsigned long gpe_bit;
	struct acpi_generic_address status_addr;
	struct acpi_generic_address command_addr;
	struct acpi_generic_address data_addr;
	unsigned long global_lock;
	struct semaphore sem;
	unsigned int expect_event;
	atomic_t leaving_burst;	/* 0 : No, 1 : Yes, 2: abort */
	wait_queue_head_t wait;
L
Linus Torvalds 已提交
106 107
};

108 109 110 111 112 113 114 115
/* If we find an EC via the ECDT, we need to keep a ptr to its context */
static struct acpi_ec *ec_ecdt;

/* External interfaces use first EC only, so remember */
static struct acpi_device *first_ec;
static int acpi_ec_mode = EC_INTR;

static int acpi_ec_poll_transaction(struct acpi_ec *ec, u8 command,
116 117
                                    const u8 *wdata, unsigned wdata_len,
                                    u8 *rdata, unsigned rdata_len);
118
static int acpi_ec_intr_transaction(struct acpi_ec *ec, u8 command,
119 120
                                    const u8 *wdata, unsigned wdata_len,
                                    u8 *rdata, unsigned rdata_len);
121 122 123 124
static void acpi_ec_gpe_poll_query(void *ec_cxt);
static void acpi_ec_gpe_intr_query(void *ec_cxt);
static u32 acpi_ec_gpe_poll_handler(void *data);
static u32 acpi_ec_gpe_intr_handler(void *data);
L
Linus Torvalds 已提交
125 126 127 128 129

/* --------------------------------------------------------------------------
                             Transaction Management
   -------------------------------------------------------------------------- */

130
static u32 acpi_ec_read_status(struct acpi_ec *ec)
L
Linus Torvalds 已提交
131
{
L
Len Brown 已提交
132
	u32 status = 0;
L
Linus Torvalds 已提交
133

134
	acpi_hw_low_level_read(8, &status, &ec->status_addr);
D
Dmitry Torokhov 已提交
135 136 137
	return status;
}

138 139 140
static u32 acpi_ec_read_data(struct acpi_ec *ec)
{
	u32 data = 0;
141

142 143
	acpi_hw_low_level_read(8, &data, &ec->data_addr);
	return data;
144 145
}

146
static void acpi_ec_write_cmd(struct acpi_ec *ec, u32 command)
L
Luming Yu 已提交
147
{
148
	acpi_hw_low_level_write(8, command, &ec->command_addr);
L
Luming Yu 已提交
149 150
}

151
static void acpi_ec_write_data(struct acpi_ec *ec, u32 data)
L
Luming Yu 已提交
152
{
153 154
	acpi_hw_low_level_write(8, data, &ec->data_addr);
}
L
Luming Yu 已提交
155

156
static int acpi_ec_check_status(u32 status, u8 event) {
L
Luming Yu 已提交
157 158

	switch (event) {
159 160 161
	case ACPI_EC_EVENT_OBF_1:
		if (status & ACPI_EC_FLAG_OBF)
			return 1;
L
Luming Yu 已提交
162
		break;
163 164 165
	case ACPI_EC_EVENT_IBF_0:
		if (!(status & ACPI_EC_FLAG_IBF))
			return 1;
L
Luming Yu 已提交
166 167
		break;
	default:
168
		break;
L
Luming Yu 已提交
169 170
	}

171
	return 0;
L
Luming Yu 已提交
172
}
D
Dmitry Torokhov 已提交
173

174
static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
175
{
176 177
	int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
	long time_left;
D
Dmitry Torokhov 已提交
178

179
	ec->expect_event = event;
180
	if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
181 182
		ec->expect_event = 0;
		return 0;
183 184
	}

185 186 187 188 189 190 191 192 193
	do {
		if (acpi_ec_mode == EC_POLL) {
			udelay(ACPI_EC_UDELAY);
		} else {
			time_left = wait_event_timeout(ec->wait,
				    !ec->expect_event,
				    msecs_to_jiffies(ACPI_EC_DELAY));
			if (time_left > 0) {
				ec->expect_event = 0;
194
				return 0;
195
			}
196
		}
197 198 199 200 201 202 203
		if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
			ec->expect_event = 0;
			return 0;
		}
	} while (--i > 0);

	ec->expect_event = 0;
L
Linus Torvalds 已提交
204

205
	return -ETIME;
L
Linus Torvalds 已提交
206 207
}

208
#ifdef ACPI_FUTURE_USAGE
209 210 211 212
/*
 * Note: samsung nv5000 doesn't work with ec burst mode.
 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
 */
213
int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
214
{
L
Len Brown 已提交
215
	u32 tmp = 0;
216
	u32 status = 0;
D
Dmitry Torokhov 已提交
217 218 219


	status = acpi_ec_read_status(ec);
L
Len Brown 已提交
220
	if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
221
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
L
Len Brown 已提交
222
		if (status)
223
			goto end;
224 225 226
		acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
		tmp = acpi_ec_read_data(ec);
L
Len Brown 已提交
227
		if (tmp != 0x90) {	/* Burst ACK byte */
228
			return -EINVAL;
D
Dmitry Torokhov 已提交
229
		}
230 231
	}

232
	atomic_set(&ec->leaving_burst, 0);
233
	return 0;
234 235
  end:
	ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
236
	return -1;
D
Dmitry Torokhov 已提交
237 238
}

239
int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
240
{
241
	u32 status = 0;
D
Dmitry Torokhov 已提交
242 243


244 245
	status = acpi_ec_read_status(ec);
	if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
246
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
247 248
		if(status)
			goto end;
249 250
		acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
		acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
251
	}
252
	atomic_set(&ec->leaving_burst, 1);
253
	return 0;
254 255
  end:
	ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
256
	return -1;
D
Dmitry Torokhov 已提交
257
}
258
#endif /* ACPI_FUTURE_USAGE */
D
Dmitry Torokhov 已提交
259

260
static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
261 262
                               const u8 *wdata, unsigned wdata_len,
                               u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
263
{
264
	if (acpi_ec_mode == EC_POLL)
265
		return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
L
Luming Yu 已提交
266
	else
267 268
		return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
}
269
static int acpi_ec_read(struct acpi_ec *ec, u8 address, u32 * data)
270 271 272 273 274 275
{
        int result;
        u8 d;
        result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
        *data = d;
        return result;
L
Luming Yu 已提交
276
}
277
static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
L
Luming Yu 已提交
278
{
279 280
        u8 wdata[2] = { address, data };
        return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
L
Luming Yu 已提交
281
}
282

283 284 285
static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
                                        const u8 *wdata, unsigned wdata_len,
                                        u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
286
{
287
	int result;
L
Luming Yu 已提交
288

289
	acpi_ec_write_cmd(ec, command);
L
Luming Yu 已提交
290

291
	for (; wdata_len > 0; wdata_len --) {
292
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
293 294
		if (result)
			return result;
L
Luming Yu 已提交
295

296
		acpi_ec_write_data(ec, *(wdata++));
297
        }
L
Luming Yu 已提交
298

299
	if (command == ACPI_EC_COMMAND_WRITE) {
300
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
301 302 303
		if (result)
			return result;
	}
L
Luming Yu 已提交
304

305 306
	for (; rdata_len > 0; rdata_len --) {
		u32 d;
L
Luming Yu 已提交
307

308
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
309 310
		if (result)
			return result;
L
Len Brown 已提交
311

312
		d = acpi_ec_read_data(ec);
313 314
		*(rdata++) = (u8) d;
	}
L
Luming Yu 已提交
315

316
	return 0;
L
Luming Yu 已提交
317 318
}

319
static int acpi_ec_poll_transaction(struct acpi_ec *ec, u8 command,
320 321
                                    const u8 *wdata, unsigned wdata_len,
                                    u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
322
{
L
Len Brown 已提交
323
	acpi_status status = AE_OK;
324
	int result;
L
Len Brown 已提交
325
	u32 glk = 0;
L
Luming Yu 已提交
326

327
	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
328
		return -EINVAL;
L
Luming Yu 已提交
329

330 331 332
        if (rdata)
                memset(rdata, 0, rdata_len);

333
	if (ec->global_lock) {
L
Luming Yu 已提交
334 335
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
336
			return -ENODEV;
337
        }
L
Luming Yu 已提交
338

339
	if (down_interruptible(&ec->sem)) {
340 341 342
		result = -ERESTARTSYS;
		goto end_nosem;
	}
L
Luming Yu 已提交
343

344 345 346
        result = acpi_ec_transaction_unlocked(ec, command,
                                              wdata, wdata_len,
                                              rdata, rdata_len);
347
	up(&ec->sem);
348

349
end_nosem:
350
	if (ec->global_lock)
L
Luming Yu 已提交
351 352
		acpi_release_global_lock(glk);

353
	return result;
L
Luming Yu 已提交
354 355
}

356
static int acpi_ec_intr_transaction(struct acpi_ec *ec, u8 command,
357 358
                                    const u8 *wdata, unsigned wdata_len,
                                    u8 *rdata, unsigned rdata_len)
L
Linus Torvalds 已提交
359
{
360
	int status;
L
Len Brown 已提交
361
	u32 glk;
L
Linus Torvalds 已提交
362

363
	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
364
		return -EINVAL;
L
Linus Torvalds 已提交
365

366 367
        if (rdata)
                memset(rdata, 0, rdata_len);
L
Linus Torvalds 已提交
368

369
	if (ec->global_lock) {
L
Linus Torvalds 已提交
370 371
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
372
			return -ENODEV;
L
Linus Torvalds 已提交
373
	}
D
Dmitry Torokhov 已提交
374 375

	WARN_ON(in_interrupt());
376
	down(&ec->sem);
D
Dmitry Torokhov 已提交
377

378
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
379
	if (status) {
380
		ACPI_EXCEPTION((AE_INFO, status, "read EC, IB not empty"));
D
Dmitry Torokhov 已提交
381
		goto end;
382
	}
L
Linus Torvalds 已提交
383

384 385 386
        status = acpi_ec_transaction_unlocked(ec, command,
                                              wdata, wdata_len,
                                              rdata, rdata_len);
L
Linus Torvalds 已提交
387

388
end:
389
	up(&ec->sem);
L
Linus Torvalds 已提交
390

391
	if (ec->global_lock)
L
Linus Torvalds 已提交
392 393
		acpi_release_global_lock(glk);

394
	return status;
L
Linus Torvalds 已提交
395 396 397 398 399
}

/*
 * Externally callable EC access functions. For now, assume 1 EC only
 */
L
Len Brown 已提交
400
int ec_read(u8 addr, u8 * val)
L
Linus Torvalds 已提交
401
{
402
	struct acpi_ec *ec;
L
Linus Torvalds 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415
	int err;
	u32 temp_data;

	if (!first_ec)
		return -ENODEV;

	ec = acpi_driver_data(first_ec);

	err = acpi_ec_read(ec, addr, &temp_data);

	if (!err) {
		*val = temp_data;
		return 0;
L
Len Brown 已提交
416
	} else
L
Linus Torvalds 已提交
417 418
		return err;
}
L
Len Brown 已提交
419

L
Linus Torvalds 已提交
420 421
EXPORT_SYMBOL(ec_read);

L
Len Brown 已提交
422
int ec_write(u8 addr, u8 val)
L
Linus Torvalds 已提交
423
{
424
	struct acpi_ec *ec;
L
Linus Torvalds 已提交
425 426 427 428 429 430 431 432 433 434 435
	int err;

	if (!first_ec)
		return -ENODEV;

	ec = acpi_driver_data(first_ec);

	err = acpi_ec_write(ec, addr, val);

	return err;
}
L
Len Brown 已提交
436

L
Linus Torvalds 已提交
437 438
EXPORT_SYMBOL(ec_write);

439 440 441
extern int ec_transaction(u8 command,
                          const u8 *wdata, unsigned wdata_len,
                          u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
442
{
443
	struct acpi_ec *ec;
L
Luming Yu 已提交
444

445 446
	if (!first_ec)
		return -ENODEV;
L
Luming Yu 已提交
447

448
	ec = acpi_driver_data(first_ec);
L
Luming Yu 已提交
449

450
	return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
L
Luming Yu 已提交
451
}
L
Linus Torvalds 已提交
452

453
EXPORT_SYMBOL(ec_transaction);
L
Linus Torvalds 已提交
454

455
static int acpi_ec_query(struct acpi_ec *ec, u32 * data) {
456 457
        int result;
        u8 d;
L
Linus Torvalds 已提交
458

459 460
        if (!ec || !data)
                return -EINVAL;
L
Linus Torvalds 已提交
461

462 463 464 465 466
        /*
         * Query the EC to find out which _Qxx method we need to evaluate.
         * Note that successful completion of the query causes the ACPI_EC_SCI
         * bit to be cleared (and thus clearing the interrupt source).
         */
467

468 469 470
        result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
        if (result)
                return result;
L
Linus Torvalds 已提交
471

472 473
        if (!d)
                return -ENODATA;
L
Linus Torvalds 已提交
474

475 476
        *data = d;
        return 0;
L
Linus Torvalds 已提交
477 478 479 480 481 482
}

/* --------------------------------------------------------------------------
                                Event Management
   -------------------------------------------------------------------------- */

L
Luming Yu 已提交
483
union acpi_ec_query_data {
L
Len Brown 已提交
484 485
	acpi_handle handle;
	u8 data;
L
Linus Torvalds 已提交
486 487
};

L
Len Brown 已提交
488
static void acpi_ec_gpe_query(void *ec_cxt)
L
Linus Torvalds 已提交
489
{
490
	if (acpi_ec_mode == EC_POLL)
491
		acpi_ec_gpe_poll_query(ec_cxt);
L
Luming Yu 已提交
492
	else
493
		acpi_ec_gpe_intr_query(ec_cxt);
L
Luming Yu 已提交
494 495
}

496
static void acpi_ec_gpe_poll_query(void *ec_cxt)
L
Luming Yu 已提交
497
{
498
	struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
L
Len Brown 已提交
499 500 501 502 503
	u32 value = 0;
	static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
	const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
L
Luming Yu 已提交
504 505 506 507 508


	if (!ec_cxt)
		goto end;

509
	if (down_interruptible (&ec->sem)) {
510
		return;
511
	}
512 513
	value = acpi_ec_read_status(ec);
	up(&ec->sem);
L
Luming Yu 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

	/* TBD: Implement asynch events!
	 * NOTE: All we care about are EC-SCI's.  Other EC events are
	 * handled via polling (yuck!).  This is because some systems
	 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
	 *  a purely interrupt-driven approach (grumble, grumble).
	 */
	if (!(value & ACPI_EC_FLAG_SCI))
		goto end;

	if (acpi_ec_query(ec, &value))
		goto end;

	object_name[2] = hex[((value >> 4) & 0x0F)];
	object_name[3] = hex[(value & 0x0F)];

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));

532
	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
L
Luming Yu 已提交
533

L
Len Brown 已提交
534
      end:
535
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
L
Luming Yu 已提交
536
}
537
static void acpi_ec_gpe_intr_query(void *ec_cxt)
L
Luming Yu 已提交
538
{
539
	struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
L
Len Brown 已提交
540 541 542 543 544 545
	u32 value;
	int result = -ENODATA;
	static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
	const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
L
Linus Torvalds 已提交
546 547


D
Dmitry Torokhov 已提交
548 549
	if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
		result = acpi_ec_query(ec, &value);
L
Linus Torvalds 已提交
550

D
Dmitry Torokhov 已提交
551
	if (result)
L
Linus Torvalds 已提交
552 553 554 555 556 557 558
		goto end;

	object_name[2] = hex[((value >> 4) & 0x0F)];
	object_name[3] = hex[(value & 0x0F)];

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));

559
	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
L
Len Brown 已提交
560
      end:
D
Dmitry Torokhov 已提交
561
	return;
L
Linus Torvalds 已提交
562 563
}

L
Len Brown 已提交
564
static u32 acpi_ec_gpe_handler(void *data)
L
Luming Yu 已提交
565
{
566
	if (acpi_ec_mode == EC_POLL)
567
		return acpi_ec_gpe_poll_handler(data);
L
Luming Yu 已提交
568
	else
569
		return acpi_ec_gpe_intr_handler(data);
L
Luming Yu 已提交
570
}
571
static u32 acpi_ec_gpe_poll_handler(void *data)
L
Luming Yu 已提交
572
{
L
Len Brown 已提交
573
	acpi_status status = AE_OK;
574
	struct acpi_ec *ec = (struct acpi_ec *)data;
L
Luming Yu 已提交
575 576 577 578

	if (!ec)
		return ACPI_INTERRUPT_NOT_HANDLED;

579
	acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
L
Luming Yu 已提交
580

581
	status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
L
Luming Yu 已提交
582 583 584 585 586 587

	if (status == AE_OK)
		return ACPI_INTERRUPT_HANDLED;
	else
		return ACPI_INTERRUPT_NOT_HANDLED;
}
588
static u32 acpi_ec_gpe_intr_handler(void *data)
L
Linus Torvalds 已提交
589
{
L
Len Brown 已提交
590 591
	acpi_status status = AE_OK;
	u32 value;
592
	struct acpi_ec *ec = (struct acpi_ec *)data;
L
Linus Torvalds 已提交
593 594 595 596

	if (!ec)
		return ACPI_INTERRUPT_NOT_HANDLED;

597
	acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
D
Dmitry Torokhov 已提交
598
	value = acpi_ec_read_status(ec);
L
Linus Torvalds 已提交
599

600 601
	switch (ec->expect_event) {
	case ACPI_EC_EVENT_OBF_1:
602 603
		if (!(value & ACPI_EC_FLAG_OBF))
			break;
604 605
		ec->expect_event = 0;
		wake_up(&ec->wait);
606
		break;
607
	case ACPI_EC_EVENT_IBF_0:
608 609
		if ((value & ACPI_EC_FLAG_IBF))
			break;
610 611
		ec->expect_event = 0;
		wake_up(&ec->wait);
612
		break;
613 614
	default:
		break;
D
Dmitry Torokhov 已提交
615 616
	}

L
Len Brown 已提交
617
	if (value & ACPI_EC_FLAG_SCI) {
618
		status = acpi_os_execute(OSL_EC_BURST_HANDLER,
L
Len Brown 已提交
619
						     acpi_ec_gpe_query, ec);
L
Luming Yu 已提交
620
		return status == AE_OK ?
L
Len Brown 已提交
621 622
		    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
	}
623
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
D
Dmitry Torokhov 已提交
624
	return status == AE_OK ?
L
Len Brown 已提交
625
	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632
}

/* --------------------------------------------------------------------------
                             Address Space Management
   -------------------------------------------------------------------------- */

static acpi_status
L
Len Brown 已提交
633 634
acpi_ec_space_setup(acpi_handle region_handle,
		    u32 function, void *handler_context, void **return_context)
L
Linus Torvalds 已提交
635 636 637 638 639
{
	/*
	 * The EC object is in the handler context and is needed
	 * when calling the acpi_ec_space_handler.
	 */
L
Len Brown 已提交
640 641
	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
	    handler_context : NULL;
L
Linus Torvalds 已提交
642 643 644 645 646

	return AE_OK;
}

static acpi_status
L
Len Brown 已提交
647 648 649 650 651
acpi_ec_space_handler(u32 function,
		      acpi_physical_address address,
		      u32 bit_width,
		      acpi_integer * value,
		      void *handler_context, void *region_context)
L
Linus Torvalds 已提交
652
{
L
Len Brown 已提交
653
	int result = 0;
654
	struct acpi_ec *ec = NULL;
L
Len Brown 已提交
655 656 657
	u64 temp = *value;
	acpi_integer f_v = 0;
	int i = 0;
L
Linus Torvalds 已提交
658 659 660


	if ((address > 0xFF) || !value || !handler_context)
661
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
662

L
Luming Yu 已提交
663
	if (bit_width != 8 && acpi_strict) {
664
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
665 666
	}

667
	ec = (struct acpi_ec *)handler_context;
L
Linus Torvalds 已提交
668

L
Len Brown 已提交
669
      next_byte:
L
Linus Torvalds 已提交
670 671
	switch (function) {
	case ACPI_READ:
L
Luming Yu 已提交
672
		temp = 0;
L
Len Brown 已提交
673
		result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
L
Linus Torvalds 已提交
674 675
		break;
	case ACPI_WRITE:
L
Luming Yu 已提交
676
		result = acpi_ec_write(ec, (u8) address, (u8) temp);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684
		break;
	default:
		result = -EINVAL;
		goto out;
		break;
	}

	bit_width -= 8;
L
Luming Yu 已提交
685 686 687 688 689
	if (bit_width) {
		if (function == ACPI_READ)
			f_v |= temp << 8 * i;
		if (function == ACPI_WRITE)
			temp >>= 8;
L
Linus Torvalds 已提交
690
		i++;
A
Andrew Morton 已提交
691
		address++;
L
Linus Torvalds 已提交
692 693 694
		goto next_byte;
	}

L
Luming Yu 已提交
695 696
	if (function == ACPI_READ) {
		f_v |= temp << 8 * i;
L
Linus Torvalds 已提交
697 698 699
		*value = f_v;
	}

L
Len Brown 已提交
700
      out:
L
Linus Torvalds 已提交
701 702
	switch (result) {
	case -EINVAL:
703
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
704 705
		break;
	case -ENODEV:
706
		return AE_NOT_FOUND;
L
Linus Torvalds 已提交
707 708
		break;
	case -ETIME:
709
		return AE_TIME;
L
Linus Torvalds 已提交
710 711
		break;
	default:
712
		return AE_OK;
L
Linus Torvalds 已提交
713 714 715 716 717 718 719
	}
}

/* --------------------------------------------------------------------------
                              FS Interface (/proc)
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
720
static struct proc_dir_entry *acpi_ec_dir;
L
Linus Torvalds 已提交
721

L
Len Brown 已提交
722
static int acpi_ec_read_info(struct seq_file *seq, void *offset)
L
Linus Torvalds 已提交
723
{
724
	struct acpi_ec *ec = (struct acpi_ec *)seq->private;
L
Linus Torvalds 已提交
725 726 727 728 729 730


	if (!ec)
		goto end;

	seq_printf(seq, "gpe bit:                 0x%02x\n",
731
		   (u32) ec->gpe_bit);
L
Linus Torvalds 已提交
732
	seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
733 734
		   (u32) ec->status_addr.address,
		   (u32) ec->data_addr.address);
L
Linus Torvalds 已提交
735
	seq_printf(seq, "use global lock:         %s\n",
736 737
		   ec->global_lock ? "yes" : "no");
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
738

L
Len Brown 已提交
739
      end:
740
	return 0;
L
Linus Torvalds 已提交
741 742 743 744 745 746 747
}

static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_ec_read_info, PDE(inode)->data);
}

748
static struct file_operations acpi_ec_info_ops = {
L
Len Brown 已提交
749 750 751 752
	.open = acpi_ec_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
753 754 755
	.owner = THIS_MODULE,
};

L
Len Brown 已提交
756
static int acpi_ec_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
757
{
L
Len Brown 已提交
758
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
759 760 761 762


	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
763
						     acpi_ec_dir);
L
Linus Torvalds 已提交
764
		if (!acpi_device_dir(device))
765
			return -ENODEV;
L
Linus Torvalds 已提交
766 767 768
	}

	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
L
Len Brown 已提交
769
				  acpi_device_dir(device));
L
Linus Torvalds 已提交
770
	if (!entry)
771
		return -ENODEV;
L
Linus Torvalds 已提交
772 773 774 775 776 777
	else {
		entry->proc_fops = &acpi_ec_info_ops;
		entry->data = acpi_driver_data(device);
		entry->owner = THIS_MODULE;
	}

778
	return 0;
L
Linus Torvalds 已提交
779 780
}

L
Len Brown 已提交
781
static int acpi_ec_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789
{

	if (acpi_device_dir(device)) {
		remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
		remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
		acpi_device_dir(device) = NULL;
	}

790
	return 0;
L
Linus Torvalds 已提交
791 792 793 794 795 796
}

/* --------------------------------------------------------------------------
                               Driver Interface
   -------------------------------------------------------------------------- */

797
static int acpi_ec_add(struct acpi_device *device)
L
Linus Torvalds 已提交
798
{
L
Len Brown 已提交
799 800
	int result = 0;
	acpi_status status = AE_OK;
801
	struct acpi_ec *ec = NULL;
L
Luming Yu 已提交
802 803 804


	if (!device)
805
		return -EINVAL;
L
Luming Yu 已提交
806

807
	ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
808
	if (!ec)
809
		return -ENOMEM;
810 811 812 813 814 815 816 817
	memset(ec, 0, sizeof(struct acpi_ec));

	ec->handle = device->handle;
	ec->uid = -1;
	init_MUTEX(&ec->sem);
	if (acpi_ec_mode == EC_INTR) {
		atomic_set(&ec->leaving_burst, 1);
		init_waitqueue_head(&ec->wait);
L
Luming Yu 已提交
818
	}
L
Linus Torvalds 已提交
819 820 821 822 823
	strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_EC_CLASS);
	acpi_driver_data(device) = ec;

	/* Use the global lock for all EC transactions? */
824 825
	acpi_evaluate_integer(ec->handle, "_GLK", NULL,
			      &ec->global_lock);
L
Linus Torvalds 已提交
826

J
Jiri Slaby 已提交
827 828 829
	/* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
	   http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
	if (ec_ecdt) {
L
Linus Torvalds 已提交
830
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
831 832
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);
D
Dmitry Torokhov 已提交
833

834
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
L
Len Brown 已提交
835
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
836 837 838 839 840 841

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
L
Len Brown 已提交
842
	status =
843 844
	    acpi_evaluate_integer(ec->handle, "_GPE", NULL,
				  &ec->gpe_bit);
L
Linus Torvalds 已提交
845
	if (ACPI_FAILURE(status)) {
846
		ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854
		result = -ENODEV;
		goto end;
	}

	result = acpi_ec_add_fs(device);
	if (result)
		goto end;

855
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
L
Len Brown 已提交
856
	       acpi_device_name(device), acpi_device_bid(device),
857
	       (u32) ec->gpe_bit));
L
Linus Torvalds 已提交
858 859 860 861

	if (!first_ec)
		first_ec = device;

862
  end:
L
Linus Torvalds 已提交
863 864 865
	if (result)
		kfree(ec);

866
	return result;
L
Linus Torvalds 已提交
867 868
}

L
Len Brown 已提交
869
static int acpi_ec_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
870
{
871
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
872 873 874


	if (!device)
875
		return -EINVAL;
L
Linus Torvalds 已提交
876 877 878 879 880 881 882

	ec = acpi_driver_data(device);

	acpi_ec_remove_fs(device);

	kfree(ec);

883
	return 0;
L
Linus Torvalds 已提交
884 885 886
}

static acpi_status
L
Len Brown 已提交
887
acpi_ec_io_ports(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
888
{
889
	struct acpi_ec *ec = (struct acpi_ec *)context;
L
Linus Torvalds 已提交
890 891
	struct acpi_generic_address *addr;

B
Bob Moore 已提交
892
	if (resource->type != ACPI_RESOURCE_TYPE_IO) {
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900
		return AE_OK;
	}

	/*
	 * The first address region returned is the data port, and
	 * the second address region returned is the status/command
	 * port.
	 */
901 902 903 904
	if (ec->data_addr.register_bit_width == 0) {
		addr = &ec->data_addr;
	} else if (ec->command_addr.register_bit_width == 0) {
		addr = &ec->command_addr;
L
Linus Torvalds 已提交
905 906 907 908 909 910 911
	} else {
		return AE_CTRL_TERMINATE;
	}

	addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
	addr->register_bit_width = 8;
	addr->register_bit_offset = 0;
B
Bob Moore 已提交
912
	addr->address = resource->data.io.minimum;
L
Linus Torvalds 已提交
913 914 915 916

	return AE_OK;
}

L
Len Brown 已提交
917
static int acpi_ec_start(struct acpi_device *device)
L
Linus Torvalds 已提交
918
{
L
Len Brown 已提交
919
	acpi_status status = AE_OK;
920
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
921 922 923


	if (!device)
924
		return -EINVAL;
L
Linus Torvalds 已提交
925 926 927 928

	ec = acpi_driver_data(device);

	if (!ec)
929
		return -EINVAL;
L
Linus Torvalds 已提交
930 931 932 933

	/*
	 * Get I/O port addresses. Convert to GAS format.
	 */
934
	status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
935 936
				     acpi_ec_io_ports, ec);
	if (ACPI_FAILURE(status)
937 938 939
	    || ec->command_addr.register_bit_width == 0) {
		ACPI_EXCEPTION((AE_INFO, status,
				"Error getting I/O port addresses"));
940
		return -ENODEV;
L
Linus Torvalds 已提交
941 942
	}

943
	ec->status_addr = ec->command_addr;
L
Linus Torvalds 已提交
944

945 946 947 948
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x",
			  (u32) ec->gpe_bit,
			  (u32) ec->command_addr.address,
			  (u32) ec->data_addr.address));
L
Linus Torvalds 已提交
949 950 951 952

	/*
	 * Install GPE handler
	 */
953
	status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
L
Len Brown 已提交
954 955
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec);
L
Linus Torvalds 已提交
956
	if (ACPI_FAILURE(status)) {
957
		return -ENODEV;
L
Linus Torvalds 已提交
958
	}
959 960
	acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
961

962
	status = acpi_install_address_space_handler(ec->handle,
L
Len Brown 已提交
963 964 965
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup, ec);
L
Linus Torvalds 已提交
966
	if (ACPI_FAILURE(status)) {
967
		acpi_remove_gpe_handler(NULL, ec->gpe_bit,
L
Len Brown 已提交
968
					&acpi_ec_gpe_handler);
969
		return -ENODEV;
L
Linus Torvalds 已提交
970 971
	}

972
	return AE_OK;
L
Linus Torvalds 已提交
973 974
}

L
Len Brown 已提交
975
static int acpi_ec_stop(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
976
{
L
Len Brown 已提交
977
	acpi_status status = AE_OK;
978
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
979 980 981


	if (!device)
982
		return -EINVAL;
L
Linus Torvalds 已提交
983 984 985

	ec = acpi_driver_data(device);

986
	status = acpi_remove_address_space_handler(ec->handle,
L
Len Brown 已提交
987 988
						   ACPI_ADR_SPACE_EC,
						   &acpi_ec_space_handler);
L
Linus Torvalds 已提交
989
	if (ACPI_FAILURE(status))
990
		return -ENODEV;
L
Linus Torvalds 已提交
991

L
Len Brown 已提交
992
	status =
993
	    acpi_remove_gpe_handler(NULL, ec->gpe_bit,
L
Len Brown 已提交
994
				    &acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
995
	if (ACPI_FAILURE(status))
996
		return -ENODEV;
L
Linus Torvalds 已提交
997

998
	return 0;
L
Linus Torvalds 已提交
999 1000 1001
}

static acpi_status __init
L
Len Brown 已提交
1002 1003
acpi_fake_ecdt_callback(acpi_handle handle,
			u32 Level, void *context, void **retval)
L
Linus Torvalds 已提交
1004
{
L
Len Brown 已提交
1005
	acpi_status status;
L
Linus Torvalds 已提交
1006

1007 1008 1009 1010
	init_MUTEX(&ec_ecdt->sem);
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
	}
L
Linus Torvalds 已提交
1011
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Len Brown 已提交
1012
				     acpi_ec_io_ports, ec_ecdt);
L
Linus Torvalds 已提交
1013 1014
	if (ACPI_FAILURE(status))
		return status;
1015
	ec_ecdt->status_addr = ec_ecdt->command_addr;
L
Linus Torvalds 已提交
1016

1017 1018
	ec_ecdt->uid = -1;
	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
L
Linus Torvalds 已提交
1019

L
Len Brown 已提交
1020 1021
	status =
	    acpi_evaluate_integer(handle, "_GPE", NULL,
1022
				  &ec_ecdt->gpe_bit);
L
Linus Torvalds 已提交
1023 1024
	if (ACPI_FAILURE(status))
		return status;
1025 1026
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->handle = handle;
L
Linus Torvalds 已提交
1027

1028 1029 1030 1031
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02x, ports=0x%2x, 0x%2x",
	       (u32) ec_ecdt->gpe_bit,
	       (u32) ec_ecdt->command_addr.address,
	       (u32) ec_ecdt->data_addr.address));
L
Linus Torvalds 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

	return AE_CTRL_TERMINATE;
}

/*
 * Some BIOS (such as some from Gateway laptops) access EC region very early
 * such as in BAT0._INI or EC._INI before an EC device is found and
 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
 * required, but if EC regison is accessed early, it is required.
 * The routine tries to workaround the BIOS bug by pre-scan EC device
 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
 * op region (since _REG isn't invoked yet). The assumption is true for
 * all systems found.
 */
L
Len Brown 已提交
1046
static int __init acpi_ec_fake_ecdt(void)
L
Linus Torvalds 已提交
1047
{
L
Len Brown 已提交
1048 1049
	acpi_status status;
	int ret = 0;
L
Linus Torvalds 已提交
1050

1051
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
L
Linus Torvalds 已提交
1052

1053
	ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
1054 1055 1056 1057
	if (!ec_ecdt) {
		ret = -ENOMEM;
		goto error;
	}
1058
	memset(ec_ecdt, 0, sizeof(struct acpi_ec));
L
Linus Torvalds 已提交
1059

L
Len Brown 已提交
1060 1061
	status = acpi_get_devices(ACPI_EC_HID,
				  acpi_fake_ecdt_callback, NULL, NULL);
L
Linus Torvalds 已提交
1062 1063 1064 1065
	if (ACPI_FAILURE(status)) {
		kfree(ec_ecdt);
		ec_ecdt = NULL;
		ret = -ENODEV;
1066
		ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
L
Linus Torvalds 已提交
1067 1068 1069
		goto error;
	}
	return 0;
1070
  error:
L
Linus Torvalds 已提交
1071 1072 1073
	return ret;
}

L
Len Brown 已提交
1074
static int __init acpi_ec_get_real_ecdt(void)
L
Luming Yu 已提交
1075
{
L
Len Brown 已提交
1076 1077
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Luming Yu 已提交
1078

L
Len Brown 已提交
1079 1080 1081
	status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
					 (struct acpi_table_header **)
					 &ecdt_ptr);
L
Luming Yu 已提交
1082 1083 1084
	if (ACPI_FAILURE(status))
		return -ENODEV;

1085
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
L
Luming Yu 已提交
1086 1087 1088 1089

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
1090
	ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
1091 1092
	if (!ec_ecdt)
		return -ENOMEM;
1093
	memset(ec_ecdt, 0, sizeof(struct acpi_ec));
L
Luming Yu 已提交
1094

1095 1096 1097
	init_MUTEX(&ec_ecdt->sem);
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
L
Luming Yu 已提交
1098
	}
1099 1100 1101 1102
	ec_ecdt->command_addr = ecdt_ptr->ec_control;
	ec_ecdt->status_addr = ecdt_ptr->ec_control;
	ec_ecdt->data_addr = ecdt_ptr->ec_data;
	ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
L
Linus Torvalds 已提交
1103
	/* use the GL just to be safe */
1104 1105
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->uid = ecdt_ptr->uid;
L
Linus Torvalds 已提交
1106

L
Len Brown 已提交
1107
	status =
1108
	    acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
L
Linus Torvalds 已提交
1109 1110 1111 1112 1113
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
1114 1115
  error:
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121 1122
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

static int __initdata acpi_fake_ecdt_enabled;
L
Len Brown 已提交
1123
int __init acpi_ec_ecdt_probe(void)
L
Linus Torvalds 已提交
1124
{
L
Len Brown 已提交
1125 1126
	acpi_status status;
	int ret;
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

	ret = acpi_ec_get_real_ecdt();
	/* Try to make a fake ECDT */
	if (ret && acpi_fake_ecdt_enabled) {
		ret = acpi_ec_fake_ecdt();
	}

	if (ret)
		return 0;

	/*
	 * Install GPE handler
	 */
1140
	status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
L
Len Brown 已提交
1141 1142
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec_ecdt);
L
Linus Torvalds 已提交
1143 1144 1145
	if (ACPI_FAILURE(status)) {
		goto error;
	}
1146 1147
	acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
L
Len Brown 已提交
1148 1149 1150 1151 1152 1153

	status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup,
						    ec_ecdt);
L
Linus Torvalds 已提交
1154
	if (ACPI_FAILURE(status)) {
1155
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
L
Len Brown 已提交
1156
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161
		goto error;
	}

	return 0;

L
Len Brown 已提交
1162
      error:
1163
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
1164 1165 1166 1167 1168 1169
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
1170
static int __init acpi_ec_init(void)
L
Linus Torvalds 已提交
1171
{
L
Len Brown 已提交
1172
	int result = 0;
L
Linus Torvalds 已提交
1173 1174 1175


	if (acpi_disabled)
1176
		return 0;
L
Linus Torvalds 已提交
1177 1178 1179

	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
	if (!acpi_ec_dir)
1180
		return -ENODEV;
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185

	/* Now register the driver for the EC */
	result = acpi_bus_register_driver(&acpi_ec_driver);
	if (result < 0) {
		remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1186
		return -ENODEV;
L
Linus Torvalds 已提交
1187 1188
	}

1189
	return result;
L
Linus Torvalds 已提交
1190 1191 1192 1193 1194 1195
}

subsys_initcall(acpi_ec_init);

/* EC driver currently not unloadable */
#if 0
L
Len Brown 已提交
1196
static void __exit acpi_ec_exit(void)
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202
{

	acpi_bus_unregister_driver(&acpi_ec_driver);

	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);

1203
	return;
L
Linus Torvalds 已提交
1204
}
L
Len Brown 已提交
1205
#endif				/* 0 */
L
Linus Torvalds 已提交
1206 1207 1208 1209

static int __init acpi_fake_ecdt_setup(char *str)
{
	acpi_fake_ecdt_enabled = 1;
1210
	return 1;
L
Linus Torvalds 已提交
1211
}
1212

L
Linus Torvalds 已提交
1213
__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1214
static int __init acpi_ec_set_intr_mode(char *str)
L
Luming Yu 已提交
1215
{
1216
	int intr;
1217

1218
	if (!get_option(&str, &intr))
1219 1220
		return 0;

1221
	if (intr) {
1222
		acpi_ec_mode = EC_INTR;
1223
	} else {
1224
		acpi_ec_mode = EC_POLL;
1225
	}
1226 1227 1228
	acpi_ec_driver.ops.add = acpi_ec_add;
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));

1229
	return 1;
L
Luming Yu 已提交
1230
}
L
Len Brown 已提交
1231

1232
__setup("ec_intr=", acpi_ec_set_intr_mode);