ec.c 21.9 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
41
ACPI_MODULE_NAME("ec");
L
Linus Torvalds 已提交
42 43 44 45 46
#define ACPI_EC_COMPONENT		0x00100000
#define ACPI_EC_CLASS			"embedded_controller"
#define ACPI_EC_HID			"PNP0C09"
#define ACPI_EC_DEVICE_NAME		"Embedded Controller"
#define ACPI_EC_FILE_INFO		"info"
47 48
#undef PREFIX
#define PREFIX				"ACPI: EC: "
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
/* EC commands */
55
enum ec_command {
A
Alexey Starikovskiy 已提交
56 57 58 59 60
	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,
61
};
62
/* EC events */
63
enum ec_event {
64
	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
A
Alexey Starikovskiy 已提交
65
	ACPI_EC_EVENT_IBF_0,	/* Input buffer empty */
66 67
};

68
#define ACPI_EC_DELAY		500	/* Wait 500ms max. during EC ops */
69 70
#define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */

71
static enum ec_mode {
A
Alexey Starikovskiy 已提交
72 73
	EC_INTR = 1,		/* Output buffer full */
	EC_POLL,		/* Input buffer empty */
74
} acpi_ec_mode = EC_INTR;
75

L
Len Brown 已提交
76 77 78
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);
79
static int acpi_ec_add(struct acpi_device *device);
L
Linus Torvalds 已提交
80 81

static struct acpi_driver acpi_ec_driver = {
L
Len Brown 已提交
82
	.name = "ec",
L
Len Brown 已提交
83 84 85
	.class = ACPI_EC_CLASS,
	.ids = ACPI_EC_HID,
	.ops = {
86
		.add = acpi_ec_add,
L
Len Brown 已提交
87 88 89 90
		.remove = acpi_ec_remove,
		.start = acpi_ec_start,
		.stop = acpi_ec_stop,
		},
L
Linus Torvalds 已提交
91
};
92 93

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

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

L
Linus Torvalds 已提交
111 112 113 114
/* --------------------------------------------------------------------------
                             Transaction Management
   -------------------------------------------------------------------------- */

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

120
static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
121
{
122
	return inb(ec->data_addr);
123 124
}

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

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

135 136
static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
				       unsigned old_count)
137
{
138
	u8 status = acpi_ec_read_status(ec);
139 140
	if (old_count == atomic_read(&ec->event_count))
		return 0;
A
Alexey Starikovskiy 已提交
141
	if (event == ACPI_EC_EVENT_OBF_1) {
142 143
		if (status & ACPI_EC_FLAG_OBF)
			return 1;
A
Alexey Starikovskiy 已提交
144
	} else if (event == ACPI_EC_EVENT_IBF_0) {
145 146
		if (!(status & ACPI_EC_FLAG_IBF))
			return 1;
L
Luming Yu 已提交
147 148
	}

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

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

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

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

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

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

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

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

226
static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
A
Alexey Starikovskiy 已提交
227 228
					const u8 * wdata, unsigned wdata_len,
					u8 * rdata, unsigned rdata_len)
L
Luming Yu 已提交
229
{
230
	int result = 0;
231
	unsigned count = atomic_read(&ec->event_count);
232
	acpi_ec_write_cmd(ec, command);
L
Luming Yu 已提交
233

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

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

A
Alexey Starikovskiy 已提交
256
	for (; rdata_len > 0; --rdata_len) {
257
		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
258 259
		if (result) {
			printk(KERN_ERR PREFIX "read timeout, command = %d\n",
A
Alexey Starikovskiy 已提交
260
			       command);
261 262
			goto end;
		}
263
		count = atomic_read(&ec->event_count);
264
		*(rdata++) = acpi_ec_read_data(ec);
265
	}
266 267
      end:
	return result;
L
Luming Yu 已提交
268 269
}

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

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

A
Alexey Starikovskiy 已提交
280 281
	if (rdata)
		memset(rdata, 0, rdata_len);
L
Linus Torvalds 已提交
282

283
	mutex_lock(&ec->lock);
284
	if (ec->global_lock) {
L
Linus Torvalds 已提交
285
		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
286 287
		if (ACPI_FAILURE(status)) {
			mutex_unlock(&ec->lock);
288
			return -ENODEV;
289
		}
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, 0);
296
	if (status) {
A
Alexey Starikovskiy 已提交
297 298
		printk(KERN_DEBUG PREFIX
		       "input buffer is not empty, aborting transaction\n");
D
Dmitry Torokhov 已提交
299
		goto end;
300
	}
L
Linus Torvalds 已提交
301

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

A
Alexey Starikovskiy 已提交
306
      end:
L
Linus Torvalds 已提交
307

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

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

A
Alexey Starikovskiy 已提交
315
static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
316 317 318 319 320 321 322 323 324
{
	int result;
	u8 d;

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

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

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

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

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

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

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

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

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

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

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

390 391
EXPORT_SYMBOL(ec_transaction);

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

A
Alexey Starikovskiy 已提交
397 398
	if (!ec || !data)
		return -EINVAL;
L
Linus Torvalds 已提交
399

A
Alexey Starikovskiy 已提交
400 401 402 403 404
	/*
	 * 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).
	 */
405

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

A
Alexey Starikovskiy 已提交
410 411
	if (!d)
		return -ENODATA;
L
Linus Torvalds 已提交
412

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

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

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

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

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

432
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
L
Luming Yu 已提交
433

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

L
Len Brown 已提交
437
static u32 acpi_ec_gpe_handler(void *data)
L
Linus Torvalds 已提交
438
{
L
Len Brown 已提交
439
	acpi_status status = AE_OK;
440
	u8 value;
441
	struct acpi_ec *ec = (struct acpi_ec *)data;
442
	atomic_inc(&ec->event_count);
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);
A
Alexey Starikovskiy 已提交
450 451 452
		status =
		    acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
				    ec);
L
Len Brown 已提交
453
	}
454

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

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

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

	return AE_OK;
}

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

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

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

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

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

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

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

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

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

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

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

	if (!ec)
		goto end;

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

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

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

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

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

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

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

604
	return 0;
L
Linus Torvalds 已提交
605 606
}

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

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

616
	return 0;
L
Linus Torvalds 已提交
617 618 619 620 621 622
}

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

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

	if (!device)
630
		return -EINVAL;
L
Luming Yu 已提交
631

632
	ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
633
	if (!ec)
634
		return -ENOMEM;
635 636 637

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

J
Jiri Slaby 已提交
652 653 654
	/* 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 已提交
655
		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
L
Len Brown 已提交
656 657
						  ACPI_ADR_SPACE_EC,
						  &acpi_ec_space_handler);
D
Dmitry Torokhov 已提交
658

659
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
660
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
661 662 663 664 665 666

		kfree(ec_ecdt);
	}

	/* Get GPE bit assignment (EC events). */
	/* TODO: Add support for _GPE returning a package */
A
Alexey Starikovskiy 已提交
667
	status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
L
Linus Torvalds 已提交
668
	if (ACPI_FAILURE(status)) {
A
Alexey Starikovskiy 已提交
669 670
		ACPI_EXCEPTION((AE_INFO, status,
				"Obtaining GPE bit assignment"));
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678
		result = -ENODEV;
		goto end;
	}

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

679
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
A
Alexey Starikovskiy 已提交
680 681
			  acpi_device_name(device), acpi_device_bid(device),
			  (u32) ec->gpe));
L
Linus Torvalds 已提交
682 683 684 685

	if (!first_ec)
		first_ec = device;

A
Alexey Starikovskiy 已提交
686
      end:
L
Linus Torvalds 已提交
687 688 689
	if (result)
		kfree(ec);

690
	return result;
L
Linus Torvalds 已提交
691 692
}

L
Len Brown 已提交
693
static int acpi_ec_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
694
{
695
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
696 697

	if (!device)
698
		return -EINVAL;
L
Linus Torvalds 已提交
699 700 701 702 703 704 705

	ec = acpi_driver_data(device);

	acpi_ec_remove_fs(device);

	kfree(ec);

706
	return 0;
L
Linus Torvalds 已提交
707 708 709
}

static acpi_status
L
Len Brown 已提交
710
acpi_ec_io_ports(struct acpi_resource *resource, void *context)
L
Linus Torvalds 已提交
711
{
712
	struct acpi_ec *ec = (struct acpi_ec *)context;
L
Linus Torvalds 已提交
713

B
Bob Moore 已提交
714
	if (resource->type != ACPI_RESOURCE_TYPE_IO) {
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722
		return AE_OK;
	}

	/*
	 * The first address region returned is the data port, and
	 * the second address region returned is the status/command
	 * port.
	 */
723 724 725 726
	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 已提交
727 728 729 730 731 732 733
	} else {
		return AE_CTRL_TERMINATE;
	}

	return AE_OK;
}

L
Len Brown 已提交
734
static int acpi_ec_start(struct acpi_device *device)
L
Linus Torvalds 已提交
735
{
L
Len Brown 已提交
736
	acpi_status status = AE_OK;
737
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
738 739

	if (!device)
740
		return -EINVAL;
L
Linus Torvalds 已提交
741 742 743 744

	ec = acpi_driver_data(device);

	if (!ec)
745
		return -EINVAL;
L
Linus Torvalds 已提交
746 747 748 749

	/*
	 * Get I/O port addresses. Convert to GAS format.
	 */
750
	status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
L
Len Brown 已提交
751
				     acpi_ec_io_ports, ec);
752
	if (ACPI_FAILURE(status) || ec->command_addr == 0) {
753 754
		ACPI_EXCEPTION((AE_INFO, status,
				"Error getting I/O port addresses"));
755
		return -ENODEV;
L
Linus Torvalds 已提交
756 757
	}

758
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
759
			  ec->gpe, ec->command_addr, ec->data_addr));
L
Linus Torvalds 已提交
760 761 762 763

	/*
	 * Install GPE handler
	 */
764
	status = acpi_install_gpe_handler(NULL, ec->gpe,
L
Len Brown 已提交
765 766
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec);
L
Linus Torvalds 已提交
767
	if (ACPI_FAILURE(status)) {
768
		return -ENODEV;
L
Linus Torvalds 已提交
769
	}
770 771
	acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
L
Linus Torvalds 已提交
772

773
	status = acpi_install_address_space_handler(ec->handle,
L
Len Brown 已提交
774 775 776
						    ACPI_ADR_SPACE_EC,
						    &acpi_ec_space_handler,
						    &acpi_ec_space_setup, ec);
L
Linus Torvalds 已提交
777
	if (ACPI_FAILURE(status)) {
A
Alexey Starikovskiy 已提交
778
		acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
779
		return -ENODEV;
L
Linus Torvalds 已提交
780 781
	}

782
	return AE_OK;
L
Linus Torvalds 已提交
783 784
}

L
Len Brown 已提交
785
static int acpi_ec_stop(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
786
{
L
Len Brown 已提交
787
	acpi_status status = AE_OK;
788
	struct acpi_ec *ec = NULL;
L
Linus Torvalds 已提交
789 790

	if (!device)
791
		return -EINVAL;
L
Linus Torvalds 已提交
792 793 794

	ec = acpi_driver_data(device);

795
	status = acpi_remove_address_space_handler(ec->handle,
L
Len Brown 已提交
796 797
						   ACPI_ADR_SPACE_EC,
						   &acpi_ec_space_handler);
L
Linus Torvalds 已提交
798
	if (ACPI_FAILURE(status))
799
		return -ENODEV;
L
Linus Torvalds 已提交
800

A
Alexey Starikovskiy 已提交
801
	status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
802
	if (ACPI_FAILURE(status))
803
		return -ENODEV;
L
Linus Torvalds 已提交
804

805
	return 0;
L
Linus Torvalds 已提交
806 807
}

L
Len Brown 已提交
808
static int __init acpi_ec_get_real_ecdt(void)
L
Luming Yu 已提交
809
{
L
Len Brown 已提交
810 811
	acpi_status status;
	struct acpi_table_ecdt *ecdt_ptr;
L
Luming Yu 已提交
812

813 814
	status = acpi_get_table(ACPI_SIG_ECDT, 1,
				(struct acpi_table_header **)&ecdt_ptr);
L
Luming Yu 已提交
815 816 817
	if (ACPI_FAILURE(status))
		return -ENODEV;

818
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
L
Luming Yu 已提交
819 820 821 822

	/*
	 * Generate a temporary ec context to use until the namespace is scanned
	 */
823
	ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
L
Luming Yu 已提交
824 825 826
	if (!ec_ecdt)
		return -ENOMEM;

827
	mutex_init(&ec_ecdt->lock);
828
	atomic_set(&ec_ecdt->event_count, 1);
829 830
	if (acpi_ec_mode == EC_INTR) {
		init_waitqueue_head(&ec_ecdt->wait);
L
Luming Yu 已提交
831
	}
832 833 834
	ec_ecdt->command_addr = ecdt_ptr->control.address;
	ec_ecdt->data_addr = ecdt_ptr->data.address;
	ec_ecdt->gpe = ecdt_ptr->gpe;
835
	ec_ecdt->uid = ecdt_ptr->uid;
L
Linus Torvalds 已提交
836

837
	status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
L
Linus Torvalds 已提交
838 839 840 841 842
	if (ACPI_FAILURE(status)) {
		goto error;
	}

	return 0;
A
Alexey Starikovskiy 已提交
843
      error:
844
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
845 846 847 848 849 850
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
851
int __init acpi_ec_ecdt_probe(void)
L
Linus Torvalds 已提交
852
{
L
Len Brown 已提交
853 854
	acpi_status status;
	int ret;
L
Linus Torvalds 已提交
855 856 857 858 859 860 861 862

	ret = acpi_ec_get_real_ecdt();
	if (ret)
		return 0;

	/*
	 * Install GPE handler
	 */
863
	status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
864 865
					  ACPI_GPE_EDGE_TRIGGERED,
					  &acpi_ec_gpe_handler, ec_ecdt);
L
Linus Torvalds 已提交
866 867 868
	if (ACPI_FAILURE(status)) {
		goto error;
	}
869 870
	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 已提交
871 872 873 874 875 876

	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 已提交
877
	if (ACPI_FAILURE(status)) {
878
		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
L
Len Brown 已提交
879
					&acpi_ec_gpe_handler);
L
Linus Torvalds 已提交
880 881 882 883 884
		goto error;
	}

	return 0;

L
Len Brown 已提交
885
      error:
886
	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
L
Linus Torvalds 已提交
887 888 889 890 891 892
	kfree(ec_ecdt);
	ec_ecdt = NULL;

	return -ENODEV;
}

L
Len Brown 已提交
893
static int __init acpi_ec_init(void)
L
Linus Torvalds 已提交
894
{
L
Len Brown 已提交
895
	int result = 0;
L
Linus Torvalds 已提交
896 897

	if (acpi_disabled)
898
		return 0;
L
Linus Torvalds 已提交
899 900 901

	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
	if (!acpi_ec_dir)
902
		return -ENODEV;
L
Linus Torvalds 已提交
903 904 905 906 907

	/* 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);
908
		return -ENODEV;
L
Linus Torvalds 已提交
909 910
	}

911
	return result;
L
Linus Torvalds 已提交
912 913 914 915 916 917
}

subsys_initcall(acpi_ec_init);

/* EC driver currently not unloadable */
#if 0
L
Len Brown 已提交
918
static void __exit acpi_ec_exit(void)
L
Linus Torvalds 已提交
919 920 921 922 923 924
{

	acpi_bus_unregister_driver(&acpi_ec_driver);

	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);

925
	return;
L
Linus Torvalds 已提交
926
}
L
Len Brown 已提交
927
#endif				/* 0 */
L
Linus Torvalds 已提交
928

929
static int __init acpi_ec_set_intr_mode(char *str)
L
Luming Yu 已提交
930
{
931
	int intr;
932

933
	if (!get_option(&str, &intr))
934 935
		return 0;

936
	if (intr) {
937
		acpi_ec_mode = EC_INTR;
938
	} else {
939
		acpi_ec_mode = EC_POLL;
940
	}
941
	acpi_ec_driver.ops.add = acpi_ec_add;
942
	printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
943

944
	return 1;
L
Luming Yu 已提交
945
}
L
Len Brown 已提交
946

947
__setup("ec_intr=", acpi_ec_set_intr_mode);