ec.c 24.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 50 51
#undef PREFIX
#define PREFIX				"ACPI: EC: "

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

/* EC commands */
59 60 61 62 63 64 65
enum ec_command {
	ACPI_EC_COMMAND_READ	= 0x80,
	ACPI_EC_COMMAND_WRITE	= 0x81,
	ACPI_EC_BURST_ENABLE	= 0x82,
	ACPI_EC_BURST_DISABLE	= 0x83,
	ACPI_EC_COMMAND_QUERY	= 0x84,
};
66
/* EC events */
67
enum ec_event {
68 69 70 71
	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
	ACPI_EC_EVENT_IBF_0,		/* Input buffer empty */
};

72
#define ACPI_EC_DELAY		500	/* Wait 500ms max. during EC ops */
73 74
#define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */

75
static enum ec_mode {
76
	EC_INTR = 1,	/* Output buffer full */
77
	EC_POLL,	/* Input buffer empty */
78
} acpi_ec_mode = EC_INTR;
79

L
Len Brown 已提交
80 81 82
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);
83
static int acpi_ec_add(struct acpi_device *device);
L
Linus Torvalds 已提交
84 85

static struct acpi_driver acpi_ec_driver = {
L
Len Brown 已提交
86 87 88 89
	.name = ACPI_EC_DRIVER_NAME,
	.class = ACPI_EC_CLASS,
	.ids = ACPI_EC_HID,
	.ops = {
90
		.add = acpi_ec_add,
L
Len Brown 已提交
91 92 93 94
		.remove = acpi_ec_remove,
		.start = acpi_ec_start,
		.stop = acpi_ec_stop,
		},
L
Linus Torvalds 已提交
95
};
96 97

/* If we find an EC via the ECDT, we need to keep a ptr to its context */
98 99 100
struct acpi_ec {
	acpi_handle handle;
	unsigned long uid;
101
	unsigned long gpe;
102 103
	unsigned long command_addr;
	unsigned long data_addr;
104
	unsigned long global_lock;
105
	struct mutex lock;
106
	atomic_t query_pending;
107 108
	atomic_t leaving_burst;	/* 0 : No, 1 : Yes, 2: abort */
	wait_queue_head_t wait;
109
} *ec_ecdt;
110 111 112 113

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

L
Linus Torvalds 已提交
114 115 116 117
/* --------------------------------------------------------------------------
                             Transaction Management
   -------------------------------------------------------------------------- */

118
static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
L
Linus Torvalds 已提交
119
{
120
	return inb(ec->command_addr);
D
Dmitry Torokhov 已提交
121 122
}

123
static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
124
{
125
	return inb(ec->data_addr);
126 127
}

128
static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
L
Luming Yu 已提交
129
{
130
	outb(command, ec->command_addr);
L
Luming Yu 已提交
131 132
}

133
static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
L
Luming Yu 已提交
134
{
135
	outb(data, ec->data_addr);
136
}
L
Luming Yu 已提交
137

138
static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
139
{
140
	u8 status = acpi_ec_read_status(ec);
A
Alexey Starikovskiy 已提交
141 142

	if (event == ACPI_EC_EVENT_OBF_1) {
143 144
		if (status & ACPI_EC_FLAG_OBF)
			return 1;
A
Alexey Starikovskiy 已提交
145
	} else if (event == ACPI_EC_EVENT_IBF_0) {
146 147
		if (!(status & ACPI_EC_FLAG_IBF))
			return 1;
L
Luming Yu 已提交
148 149
	}

150
	return 0;
L
Luming Yu 已提交
151
}
D
Dmitry Torokhov 已提交
152

153
static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event)
154
{
155
	if (acpi_ec_mode == EC_POLL) {
156 157
		unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
		while (time_before(jiffies, delay)) {
158
			if (acpi_ec_check_status(ec, event))
159 160
				return 0;
		}
161 162 163 164 165
	} else {
		if (wait_event_timeout(ec->wait,
				       acpi_ec_check_status(ec, event),
				       msecs_to_jiffies(ACPI_EC_DELAY)) ||
		    acpi_ec_check_status(ec, event)) {
166
			return 0;
167 168 169 170
		} else {
			printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
			       " status = %d, expect_event = %d\n",
			     acpi_ec_read_status(ec), event);
171
		}
172
	}
L
Linus Torvalds 已提交
173

174
	return -ETIME;
L
Linus Torvalds 已提交
175 176
}

177
#ifdef ACPI_FUTURE_USAGE
178 179 180 181
/*
 * Note: samsung nv5000 doesn't work with ec burst mode.
 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
 */
182
int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
183
{
184 185
	u8 tmp = 0;
	u8 status = 0;
D
Dmitry Torokhov 已提交
186 187 188


	status = acpi_ec_read_status(ec);
L
Len Brown 已提交
189
	if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
190
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
L
Len Brown 已提交
191
		if (status)
192
			goto end;
193 194 195
		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 已提交
196
		if (tmp != 0x90) {	/* Burst ACK byte */
197
			return -EINVAL;
D
Dmitry Torokhov 已提交
198
		}
199 200
	}

201
	atomic_set(&ec->leaving_burst, 0);
202
	return 0;
203 204
  end:
	ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
205
	return -1;
D
Dmitry Torokhov 已提交
206 207
}

208
int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
D
Dmitry Torokhov 已提交
209
{
210
	u8 status = 0;
D
Dmitry Torokhov 已提交
211 212


213 214
	status = acpi_ec_read_status(ec);
	if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
215
		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
216 217
		if(status)
			goto end;
218 219
		acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
		acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
220
	}
221
	atomic_set(&ec->leaving_burst, 1);
222
	return 0;
223 224
  end:
	ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
225
	return -1;
D
Dmitry Torokhov 已提交
226
}
227
#endif /* ACPI_FUTURE_USAGE */
D
Dmitry Torokhov 已提交
228

229
static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
230 231
					const u8 *wdata, unsigned wdata_len,
					u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
232
{
233
	int result = 0;
L
Luming Yu 已提交
234

235
	acpi_ec_write_cmd(ec, command);
L
Luming Yu 已提交
236

A
Alexey Starikovskiy 已提交
237
	for (; wdata_len > 0; --wdata_len) {
238
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
239 240 241 242 243
		if (result) {
			printk(KERN_ERR PREFIX "write_cmd timeout, command = %d\n",
			     command);
			goto end;
		}
244
		acpi_ec_write_data(ec, *(wdata++));
245
	}
L
Luming Yu 已提交
246

247
	if (!rdata_len) {
248
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
249 250 251 252 253
		if (result) {
			printk(KERN_ERR PREFIX "finish-write timeout, command = %d\n",
			     command);
			goto end;
		}
254 255
	} else if (command == ACPI_EC_COMMAND_QUERY) {
		atomic_set(&ec->query_pending, 0);
256
	}
L
Luming Yu 已提交
257

A
Alexey Starikovskiy 已提交
258
	for (; rdata_len > 0; --rdata_len) {
259
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
260 261 262 263 264
		if (result) {
			printk(KERN_ERR PREFIX "read timeout, command = %d\n",
			     command);
			goto end;
		}
L
Len Brown 已提交
265

266
		*(rdata++) = acpi_ec_read_data(ec);
267
	}
268 269
      end:
	return result;
L
Luming Yu 已提交
270 271
}

272 273 274
static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
				const u8 *wdata, unsigned wdata_len,
				u8 *rdata, unsigned rdata_len)
L
Linus Torvalds 已提交
275
{
276
	int status;
L
Len Brown 已提交
277
	u32 glk;
L
Linus Torvalds 已提交
278

279
	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
280
		return -EINVAL;
L
Linus Torvalds 已提交
281

282 283
        if (rdata)
                memset(rdata, 0, rdata_len);
L
Linus Torvalds 已提交
284

285
	mutex_lock(&ec->lock);
286
	if (ec->global_lock) {
L
Linus Torvalds 已提交
287 288
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
		if (ACPI_FAILURE(status))
289
			return -ENODEV;
L
Linus Torvalds 已提交
290
	}
D
Dmitry Torokhov 已提交
291

292
	/* Make sure GPE is enabled before doing transaction */
293
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
294

295
	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
296
	if (status) {
297
		printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
D
Dmitry Torokhov 已提交
298
		goto end;
299
	}
L
Linus Torvalds 已提交
300

301 302 303
        status = acpi_ec_transaction_unlocked(ec, command,
                                              wdata, wdata_len,
                                              rdata, rdata_len);
L
Linus Torvalds 已提交
304

305
end:
L
Linus Torvalds 已提交
306

307
	if (ec->global_lock)
L
Linus Torvalds 已提交
308
		acpi_release_global_lock(glk);
309
	mutex_unlock(&ec->lock);
L
Linus Torvalds 已提交
310

311
	return status;
L
Linus Torvalds 已提交
312 313
}

314
static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
315 316 317 318 319 320 321 322 323
{
	int result;
	u8 d;

	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
				     &address, 1, &d, 1);
	*data = d;
	return result;
}
324

325 326 327 328 329 330 331
static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
{
        u8 wdata[2] = { address, data };
        return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
				   wdata, 2, NULL, 0);
}

L
Linus Torvalds 已提交
332 333 334
/*
 * Externally callable EC access functions. For now, assume 1 EC only
 */
335
int ec_read(u8 addr, u8 *val)
L
Linus Torvalds 已提交
336
{
337
	struct acpi_ec *ec;
L
Linus Torvalds 已提交
338
	int err;
339
	u8 temp_data;
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348 349 350

	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 已提交
351
	} else
L
Linus Torvalds 已提交
352 353
		return err;
}
L
Len Brown 已提交
354

L
Linus Torvalds 已提交
355 356
EXPORT_SYMBOL(ec_read);

L
Len Brown 已提交
357
int ec_write(u8 addr, u8 val)
L
Linus Torvalds 已提交
358
{
359
	struct acpi_ec *ec;
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368 369 370
	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 已提交
371

L
Linus Torvalds 已提交
372 373
EXPORT_SYMBOL(ec_write);

374 375 376
extern int ec_transaction(u8 command,
                          const u8 *wdata, unsigned wdata_len,
                          u8 *rdata, unsigned rdata_len)
L
Luming Yu 已提交
377
{
378
	struct acpi_ec *ec;
L
Luming Yu 已提交
379

380 381
	if (!first_ec)
		return -ENODEV;
L
Luming Yu 已提交
382

383
	ec = acpi_driver_data(first_ec);
L
Luming Yu 已提交
384

385 386
	return acpi_ec_transaction(ec, command, wdata,
				   wdata_len, rdata, rdata_len);
L
Luming Yu 已提交
387
}
L
Linus Torvalds 已提交
388

389 390
EXPORT_SYMBOL(ec_transaction);

391
static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
392 393
{
	int result;
394
        u8 d;
L
Linus Torvalds 已提交
395

396 397
        if (!ec || !data)
                return -EINVAL;
L
Linus Torvalds 已提交
398

399 400 401 402 403
        /*
         * 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).
         */
404

405 406 407
        result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
        if (result)
                return result;
L
Linus Torvalds 已提交
408

409 410
        if (!d)
                return -ENODATA;
L
Linus Torvalds 已提交
411

412 413
        *data = d;
        return 0;
L
Linus Torvalds 已提交
414 415 416 417 418 419
}

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

L
Len Brown 已提交
420
static void acpi_ec_gpe_query(void *ec_cxt)
L
Luming Yu 已提交
421
{
422
	struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
423
	u8 value = 0;
424
	char object_name[8];
L
Luming Yu 已提交
425

426
	if (!ec || acpi_ec_query(ec, &value))
427
		return;
L
Luming Yu 已提交
428

429
	snprintf(object_name, 8, "_Q%2.2X", value);
L
Luming Yu 已提交
430

431
	printk(KERN_INFO PREFIX "evaluating %s\n", object_name);
L
Luming Yu 已提交
432

433
	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
L
Luming Yu 已提交
434
}
L
Linus Torvalds 已提交
435

L
Len Brown 已提交
436
static u32 acpi_ec_gpe_handler(void *data)
L
Linus Torvalds 已提交
437
{
L
Len Brown 已提交
438
	acpi_status status = AE_OK;
439
	u8 value;
440
	struct acpi_ec *ec = (struct acpi_ec *)data;
L
Linus Torvalds 已提交
441 442


443
	if (acpi_ec_mode == EC_INTR) {
444
		wake_up(&ec->wait);
D
Dmitry Torokhov 已提交
445 446
	}

447
	value = acpi_ec_read_status(ec);
448 449
	if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
		atomic_set(&ec->query_pending, 1);
450
		status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
L
Len Brown 已提交
451
	}
452

D
Dmitry Torokhov 已提交
453
	return status == AE_OK ?
L
Len Brown 已提交
454
	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
L
Linus Torvalds 已提交
455 456 457 458 459 460 461
}

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

static acpi_status
L
Len Brown 已提交
462 463
acpi_ec_space_setup(acpi_handle region_handle,
		    u32 function, void *handler_context, void **return_context)
L
Linus Torvalds 已提交
464 465 466 467 468
{
	/*
	 * The EC object is in the handler context and is needed
	 * when calling the acpi_ec_space_handler.
	 */
L
Len Brown 已提交
469 470
	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
	    handler_context : NULL;
L
Linus Torvalds 已提交
471 472 473 474 475

	return AE_OK;
}

static acpi_status
L
Len Brown 已提交
476 477 478 479 480
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 已提交
481
{
L
Len Brown 已提交
482
	int result = 0;
483
	struct acpi_ec *ec = NULL;
L
Len Brown 已提交
484 485 486
	u64 temp = *value;
	acpi_integer f_v = 0;
	int i = 0;
L
Linus Torvalds 已提交
487 488 489


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

L
Luming Yu 已提交
492
	if (bit_width != 8 && acpi_strict) {
493
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
494 495
	}

496
	ec = (struct acpi_ec *)handler_context;
L
Linus Torvalds 已提交
497

L
Len Brown 已提交
498
      next_byte:
L
Linus Torvalds 已提交
499 500
	switch (function) {
	case ACPI_READ:
L
Luming Yu 已提交
501
		temp = 0;
502
		result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
L
Linus Torvalds 已提交
503 504
		break;
	case ACPI_WRITE:
L
Luming Yu 已提交
505
		result = acpi_ec_write(ec, (u8) address, (u8) temp);
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513
		break;
	default:
		result = -EINVAL;
		goto out;
		break;
	}

	bit_width -= 8;
L
Luming Yu 已提交
514 515 516 517 518
	if (bit_width) {
		if (function == ACPI_READ)
			f_v |= temp << 8 * i;
		if (function == ACPI_WRITE)
			temp >>= 8;
L
Linus Torvalds 已提交
519
		i++;
A
Andrew Morton 已提交
520
		address++;
L
Linus Torvalds 已提交
521 522 523
		goto next_byte;
	}

L
Luming Yu 已提交
524 525
	if (function == ACPI_READ) {
		f_v |= temp << 8 * i;
L
Linus Torvalds 已提交
526 527 528
		*value = f_v;
	}

L
Len Brown 已提交
529
      out:
L
Linus Torvalds 已提交
530 531
	switch (result) {
	case -EINVAL:
532
		return AE_BAD_PARAMETER;
L
Linus Torvalds 已提交
533 534
		break;
	case -ENODEV:
535
		return AE_NOT_FOUND;
L
Linus Torvalds 已提交
536 537
		break;
	case -ETIME:
538
		return AE_TIME;
L
Linus Torvalds 已提交
539 540
		break;
	default:
541
		return AE_OK;
L
Linus Torvalds 已提交
542 543 544 545 546 547 548
	}
}

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

L
Len Brown 已提交
549
static struct proc_dir_entry *acpi_ec_dir;
L
Linus Torvalds 已提交
550

L
Len Brown 已提交
551
static int acpi_ec_read_info(struct seq_file *seq, void *offset)
L
Linus Torvalds 已提交
552
{
553
	struct acpi_ec *ec = (struct acpi_ec *)seq->private;
L
Linus Torvalds 已提交
554 555 556 557 558


	if (!ec)
		goto end;

559 560
	seq_printf(seq, "gpe:                 0x%02x\n",
		   (u32) ec->gpe);
L
Linus Torvalds 已提交
561
	seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
562 563
		   (u32) ec->command_addr,
		   (u32) ec->data_addr);
L
Linus Torvalds 已提交
564
	seq_printf(seq, "use global lock:         %s\n",
565
		   ec->global_lock ? "yes" : "no");
566
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
567

L
Len Brown 已提交
568
      end:
569
	return 0;
L
Linus Torvalds 已提交
570 571 572 573 574 575 576
}

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

577
static struct file_operations acpi_ec_info_ops = {
L
Len Brown 已提交
578 579 580 581
	.open = acpi_ec_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
582 583 584
	.owner = THIS_MODULE,
};

L
Len Brown 已提交
585
static int acpi_ec_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
586
{
L
Len Brown 已提交
587
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
588 589 590 591


	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
592
						     acpi_ec_dir);
L
Linus Torvalds 已提交
593
		if (!acpi_device_dir(device))
594
			return -ENODEV;
L
Linus Torvalds 已提交
595 596 597
	}

	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
L
Len Brown 已提交
598
				  acpi_device_dir(device));
L
Linus Torvalds 已提交
599
	if (!entry)
600
		return -ENODEV;
L
Linus Torvalds 已提交
601 602 603 604 605 606
	else {
		entry->proc_fops = &acpi_ec_info_ops;
		entry->data = acpi_driver_data(device);
		entry->owner = THIS_MODULE;
	}

607
	return 0;
L
Linus Torvalds 已提交
608 609
}

L
Len Brown 已提交
610
static int acpi_ec_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618
{

	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;
	}

619
	return 0;
L
Linus Torvalds 已提交
620 621 622 623 624 625
}

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

626
static int acpi_ec_add(struct acpi_device *device)
L
Linus Torvalds 已提交
627
{
L
Len Brown 已提交
628 629
	int result = 0;
	acpi_status status = AE_OK;
630
	struct acpi_ec *ec = NULL;
L
Luming Yu 已提交
631 632 633


	if (!device)
634
		return -EINVAL;
L
Luming Yu 已提交
635

636
	ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
637
	if (!ec)
638
		return -ENOMEM;
639 640 641 642
	memset(ec, 0, sizeof(struct acpi_ec));

	ec->handle = device->handle;
	ec->uid = -1;
643
	mutex_init(&ec->lock);
644
	atomic_set(&ec->query_pending, 0);
645 646 647
	if (acpi_ec_mode == EC_INTR) {
		atomic_set(&ec->leaving_burst, 1);
		init_waitqueue_head(&ec->wait);
L
Luming Yu 已提交
648
	}
L
Linus Torvalds 已提交
649 650 651 652 653
	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? */
654 655
	acpi_evaluate_integer(ec->handle, "_GLK", NULL,
			      &ec->global_lock);
L
Linus Torvalds 已提交
656

J
Jiri Slaby 已提交
657 658 659
	/* 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 已提交
660
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
661 662
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);
D
Dmitry Torokhov 已提交
663

664
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
665
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
666 667 668 669 670 671

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
L
Len Brown 已提交
672
	status =
673
	    acpi_evaluate_integer(ec->handle, "_GPE", NULL,
674
				  &ec->gpe);
L
Linus Torvalds 已提交
675
	if (ACPI_FAILURE(status)) {
676
		ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684
		result = -ENODEV;
		goto end;
	}

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

685
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
L
Len Brown 已提交
686
	       acpi_device_name(device), acpi_device_bid(device),
687
	       (u32) ec->gpe));
L
Linus Torvalds 已提交
688 689 690 691

	if (!first_ec)
		first_ec = device;

692
  end:
L
Linus Torvalds 已提交
693 694 695
	if (result)
		kfree(ec);

696
	return result;
L
Linus Torvalds 已提交
697 698
}

L
Len Brown 已提交
699
static int acpi_ec_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
700
{
701
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
702 703 704


	if (!device)
705
		return -EINVAL;
L
Linus Torvalds 已提交
706 707 708 709 710 711 712

	ec = acpi_driver_data(device);

	acpi_ec_remove_fs(device);

	kfree(ec);

713
	return 0;
L
Linus Torvalds 已提交
714 715 716
}

static acpi_status
L
Len Brown 已提交
717
acpi_ec_io_ports(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
718
{
719
	struct acpi_ec *ec = (struct acpi_ec *)context;
L
Linus Torvalds 已提交
720

B
Bob Moore 已提交
721
	if (resource->type != ACPI_RESOURCE_TYPE_IO) {
L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729
		return AE_OK;
	}

	/*
	 * The first address region returned is the data port, and
	 * the second address region returned is the status/command
	 * port.
	 */
730 731 732 733
	if (ec->data_addr == 0) {
		ec->data_addr = resource->data.io.minimum;
	} else if (ec->command_addr == 0) {
		ec->command_addr = resource->data.io.minimum;
L
Linus Torvalds 已提交
734 735 736 737 738 739 740
	} else {
		return AE_CTRL_TERMINATE;
	}

	return AE_OK;
}

L
Len Brown 已提交
741
static int acpi_ec_start(struct acpi_device *device)
L
Linus Torvalds 已提交
742
{
L
Len Brown 已提交
743
	acpi_status status = AE_OK;
744
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
745 746 747


	if (!device)
748
		return -EINVAL;
L
Linus Torvalds 已提交
749 750 751 752

	ec = acpi_driver_data(device);

	if (!ec)
753
		return -EINVAL;
L
Linus Torvalds 已提交
754 755 756 757

	/*
	 * Get I/O port addresses. Convert to GAS format.
	 */
758
	status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
759
				     acpi_ec_io_ports, ec);
760
	if (ACPI_FAILURE(status) || ec->command_addr == 0) {
761 762
		ACPI_EXCEPTION((AE_INFO, status,
				"Error getting I/O port addresses"));
763
		return -ENODEV;
L
Linus Torvalds 已提交
764 765
	}

766
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
767
			  ec->gpe, ec->command_addr, ec->data_addr));
L
Linus Torvalds 已提交
768 769 770 771

	/*
	 * Install GPE handler
	 */
772
	status = acpi_install_gpe_handler(NULL, ec->gpe,
L
Len Brown 已提交
773 774
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec);
L
Linus Torvalds 已提交
775
	if (ACPI_FAILURE(status)) {
776
		return -ENODEV;
L
Linus Torvalds 已提交
777
	}
778 779
	acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
780

781
	status = acpi_install_address_space_handler(ec->handle,
L
Len Brown 已提交
782 783 784
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup, ec);
L
Linus Torvalds 已提交
785
	if (ACPI_FAILURE(status)) {
786
		acpi_remove_gpe_handler(NULL, ec->gpe,
L
Len Brown 已提交
787
					&acpi_ec_gpe_handler);
788
		return -ENODEV;
L
Linus Torvalds 已提交
789 790
	}

791
	return AE_OK;
L
Linus Torvalds 已提交
792 793
}

L
Len Brown 已提交
794
static int acpi_ec_stop(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
795
{
L
Len Brown 已提交
796
	acpi_status status = AE_OK;
797
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
798 799 800


	if (!device)
801
		return -EINVAL;
L
Linus Torvalds 已提交
802 803 804

	ec = acpi_driver_data(device);

805
	status = acpi_remove_address_space_handler(ec->handle,
L
Len Brown 已提交
806 807
						   ACPI_ADR_SPACE_EC,
						   &acpi_ec_space_handler);
L
Linus Torvalds 已提交
808
	if (ACPI_FAILURE(status))
809
		return -ENODEV;
L
Linus Torvalds 已提交
810

L
Len Brown 已提交
811
	status =
812
	    acpi_remove_gpe_handler(NULL, ec->gpe,
L
Len Brown 已提交
813
				    &acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
814
	if (ACPI_FAILURE(status))
815
		return -ENODEV;
L
Linus Torvalds 已提交
816

817
	return 0;
L
Linus Torvalds 已提交
818 819 820
}

static acpi_status __init
L
Len Brown 已提交
821 822
acpi_fake_ecdt_callback(acpi_handle handle,
			u32 Level, void *context, void **retval)
L
Linus Torvalds 已提交
823
{
L
Len Brown 已提交
824
	acpi_status status;
L
Linus Torvalds 已提交
825

826
	mutex_init(&ec_ecdt->lock);
827 828 829
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
	}
L
Linus Torvalds 已提交
830
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Len Brown 已提交
831
				     acpi_ec_io_ports, ec_ecdt);
L
Linus Torvalds 已提交
832 833 834
	if (ACPI_FAILURE(status))
		return status;

835 836
	ec_ecdt->uid = -1;
	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
L
Linus Torvalds 已提交
837

L
Len Brown 已提交
838 839
	status =
	    acpi_evaluate_integer(handle, "_GPE", NULL,
840
				  &ec_ecdt->gpe);
L
Linus Torvalds 已提交
841 842
	if (ACPI_FAILURE(status))
		return status;
843 844
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->handle = handle;
L
Linus Torvalds 已提交
845

846
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
847
	       ec_ecdt->gpe, ec_ecdt->command_addr, ec_ecdt->data_addr));
L
Linus Torvalds 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861

	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 已提交
862
static int __init acpi_ec_fake_ecdt(void)
L
Linus Torvalds 已提交
863
{
L
Len Brown 已提交
864 865
	acpi_status status;
	int ret = 0;
L
Linus Torvalds 已提交
866

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

869
	ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Linus Torvalds 已提交
870 871 872 873
	if (!ec_ecdt) {
		ret = -ENOMEM;
		goto error;
	}
874
	memset(ec_ecdt, 0, sizeof(struct acpi_ec));
L
Linus Torvalds 已提交
875

L
Len Brown 已提交
876 877
	status = acpi_get_devices(ACPI_EC_HID,
				  acpi_fake_ecdt_callback, NULL, NULL);
L
Linus Torvalds 已提交
878 879 880 881
	if (ACPI_FAILURE(status)) {
		kfree(ec_ecdt);
		ec_ecdt = NULL;
		ret = -ENODEV;
882
		ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
L
Linus Torvalds 已提交
883 884 885
		goto error;
	}
	return 0;
886
  error:
L
Linus Torvalds 已提交
887 888 889
	return ret;
}

L
Len Brown 已提交
890
static int __init acpi_ec_get_real_ecdt(void)
L
Luming Yu 已提交
891
{
L
Len Brown 已提交
892 893
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Luming Yu 已提交
894

L
Len Brown 已提交
895 896 897
	status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
					 (struct acpi_table_header **)
					 &ecdt_ptr);
L
Luming Yu 已提交
898 899 900
	if (ACPI_FAILURE(status))
		return -ENODEV;

901
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
L
Luming Yu 已提交
902 903 904 905

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
906
	ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
907 908
	if (!ec_ecdt)
		return -ENOMEM;
909
	memset(ec_ecdt, 0, sizeof(struct acpi_ec));
L
Luming Yu 已提交
910

911
	mutex_init(&ec_ecdt->lock);
912 913
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
L
Luming Yu 已提交
914
	}
915 916
	ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
	ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
917
	ec_ecdt->gpe = ecdt_ptr->gpe_bit;
L
Linus Torvalds 已提交
918
	/* use the GL just to be safe */
919 920
	ec_ecdt->global_lock = TRUE;
	ec_ecdt->uid = ecdt_ptr->uid;
L
Linus Torvalds 已提交
921

L
Len Brown 已提交
922
	status =
923
	    acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
L
Linus Torvalds 已提交
924 925 926 927 928
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
929 930
  error:
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
931 932 933 934 935 936 937
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

static int __initdata acpi_fake_ecdt_enabled;
L
Len Brown 已提交
938
int __init acpi_ec_ecdt_probe(void)
L
Linus Torvalds 已提交
939
{
L
Len Brown 已提交
940 941
	acpi_status status;
	int ret;
L
Linus Torvalds 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954

	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
	 */
955
	status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
956 957
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec_ecdt);
L
Linus Torvalds 已提交
958 959 960
	if (ACPI_FAILURE(status)) {
		goto error;
	}
961 962
	acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
L
Len Brown 已提交
963 964 965 966 967 968

	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 已提交
969
	if (ACPI_FAILURE(status)) {
970
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
971
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
972 973 974 975 976
		goto error;
	}

	return 0;

L
Len Brown 已提交
977
      error:
978
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
979 980 981 982 983 984
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
985
static int __init acpi_ec_init(void)
L
Linus Torvalds 已提交
986
{
L
Len Brown 已提交
987
	int result = 0;
L
Linus Torvalds 已提交
988 989 990


	if (acpi_disabled)
991
		return 0;
L
Linus Torvalds 已提交
992 993 994

	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
	if (!acpi_ec_dir)
995
		return -ENODEV;
L
Linus Torvalds 已提交
996 997 998 999 1000

	/* 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);
1001
		return -ENODEV;
L
Linus Torvalds 已提交
1002 1003
	}

1004
	return result;
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010
}

subsys_initcall(acpi_ec_init);

/* EC driver currently not unloadable */
#if 0
L
Len Brown 已提交
1011
static void __exit acpi_ec_exit(void)
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017
{

	acpi_bus_unregister_driver(&acpi_ec_driver);

	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);

1018
	return;
L
Linus Torvalds 已提交
1019
}
L
Len Brown 已提交
1020
#endif				/* 0 */
L
Linus Torvalds 已提交
1021 1022 1023 1024

static int __init acpi_fake_ecdt_setup(char *str)
{
	acpi_fake_ecdt_enabled = 1;
1025
	return 1;
L
Linus Torvalds 已提交
1026
}
1027

L
Linus Torvalds 已提交
1028
__setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1029
static int __init acpi_ec_set_intr_mode(char *str)
L
Luming Yu 已提交
1030
{
1031
	int intr;
1032

1033
	if (!get_option(&str, &intr))
1034 1035
		return 0;

1036
	if (intr) {
1037
		acpi_ec_mode = EC_INTR;
1038
	} else {
1039
		acpi_ec_mode = EC_POLL;
1040
	}
1041 1042 1043
	acpi_ec_driver.ops.add = acpi_ec_add;
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));

1044
	return 1;
L
Luming Yu 已提交
1045
}
L
Len Brown 已提交
1046

1047
__setup("ec_intr=", acpi_ec_set_intr_mode);