More Makefile cleanups, otherwise mainly noticeable are the netfilter fix
[davej-history.git] / drivers / acpi / ec.c
blob8f6f61e36dc10ea93b50e181dea0b0256faca8cc
1 /*
2 * ec.c - Embedded controller support
4 * Copyright (C) 2000 Andrew Henroid
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/acpi.h>
23 #include <linux/slab.h>
24 #include "acpi.h"
25 #include "driver.h"
26 #include "ec.h"
28 #define _COMPONENT OS_DEPENDENT
29 MODULE_NAME ("ec")
31 #define ACPI_EC_HID "PNP0C09"
33 enum
35 ACPI_EC_SMI = 0x40,
36 ACPI_EC_SCI = 0x20,
37 ACPI_EC_BURST = 0x10,
38 ACPI_EC_CMD = 0x08,
39 ACPI_EC_IBF = 0x02,
40 ACPI_EC_OBF = 0x01
43 enum
45 ACPI_EC_READ = 0x80,
46 ACPI_EC_WRITE = 0x81,
47 ACPI_EC_BURST_ENABLE = 0x82,
48 ACPI_EC_BURST_DISABLE = 0x83,
49 ACPI_EC_QUERY = 0x84,
52 typedef struct
54 ACPI_HANDLE acpi_handle;
55 u32 gpe_bit;
56 ACPI_IO_ADDRESS status_port;
57 ACPI_IO_ADDRESS data_port;
58 u32 need_global_lock;
59 } ec_context_t;
62 typedef struct
64 ec_context_t *ec;
65 u8 data;
67 } EC_QUERY_DATA;
69 static char object_name[] = {'_', 'Q', '0', '0', '\0'};
71 static char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
74 static ACPI_STATUS
75 ec_io_wait (
76 ec_context_t *ec,
77 EC_EVENT wait_event)
79 EC_STATUS ec_status = 0;
80 UINT32 i = 100;
82 if (!ec || ((wait_event != EC_EVENT_OUTPUT_BUFFER_FULL)
83 && (wait_event != EC_EVENT_INPUT_BUFFER_EMPTY)))
84 return(AE_BAD_PARAMETER);
86 /*
87 * Wait for Event:
88 * ---------------
89 * Poll the EC status register waiting for the event to occur.
90 * Note that we'll wait a maximum of 1ms in 10us chunks.
92 switch (wait_event) {
93 case EC_EVENT_OUTPUT_BUFFER_FULL:
94 do {
95 ec_status = acpi_os_in8(ec->status_port);
96 if (ec_status & EC_FLAG_OUTPUT_BUFFER)
97 return(AE_OK);
98 acpi_os_sleep_usec(10);
99 } while (--i>0);
100 break;
101 case EC_EVENT_INPUT_BUFFER_EMPTY:
102 do {
103 ec_status = acpi_os_in8(ec->status_port);
104 if (!(ec_status & EC_FLAG_INPUT_BUFFER))
105 return(AE_OK);
106 acpi_os_sleep_usec(10);
107 } while (--i>0);
108 break;
111 return(AE_TIME);
114 static ACPI_STATUS
115 ec_io_read (
116 ec_context_t *ec,
117 ACPI_IO_ADDRESS io_port,
118 UINT8 *data,
119 EC_EVENT wait_event)
121 ACPI_STATUS status = AE_OK;
123 if (!ec || !data)
124 return(AE_BAD_PARAMETER);
126 *data = acpi_os_in8(io_port);
128 if (wait_event)
129 status = ec_io_wait(ec, wait_event);
131 return(status);
134 static ACPI_STATUS
135 ec_io_write (
136 ec_context_t *ec,
137 ACPI_IO_ADDRESS io_port,
138 UINT8 data,
139 EC_EVENT wait_event)
141 ACPI_STATUS status = AE_OK;
143 if (!ec)
144 return(AE_BAD_PARAMETER);
146 acpi_os_out8(io_port, data);
148 if (wait_event)
149 status = ec_io_wait(ec, wait_event);
151 return(status);
154 static ACPI_STATUS
155 ec_read (
156 ec_context_t *ec,
157 UINT8 address,
158 UINT8 *data)
160 ACPI_STATUS status = AE_OK;
162 FUNCTION_TRACE("ec_read");
164 if (!ec || !data)
165 return_ACPI_STATUS(AE_BAD_PARAMETER);
167 status = ec_io_write(ec, ec->status_port, EC_COMMAND_READ, EC_EVENT_INPUT_BUFFER_EMPTY);
168 if (ACPI_FAILURE(status)) {
169 DEBUG_PRINT(ACPI_WARN, ("Unable to send 'read command' to EC.\n"));
170 return_ACPI_STATUS(status);
173 status = ec_io_write(ec, ec->data_port, address, EC_EVENT_OUTPUT_BUFFER_FULL);
174 if (ACPI_FAILURE(status)) {
175 DEBUG_PRINT(ACPI_WARN, ("Unable to send 'read address' to EC.\n"));
176 return_ACPI_STATUS(status);
179 status = ec_io_read(ec, ec->data_port, data, EC_EVENT_NONE);
181 DEBUG_PRINT(ACPI_INFO, ("Read data[0x%02x] from address[0x%02x] on ec.\n", (*data), address));
183 return_ACPI_STATUS(status);
186 static ACPI_STATUS
187 ec_write (
188 ec_context_t *ec,
189 UINT8 address,
190 UINT8 data)
192 ACPI_STATUS status = AE_OK;
194 FUNCTION_TRACE("ec_write");
196 if (!ec)
197 return_ACPI_STATUS(AE_BAD_PARAMETER);
199 status = ec_io_write(ec, ec->status_port, EC_COMMAND_WRITE, EC_EVENT_INPUT_BUFFER_EMPTY);
200 if (ACPI_FAILURE(status)) {
201 DEBUG_PRINT(ACPI_WARN, ("Unable to send 'write command' to EC.\n"));
202 return_ACPI_STATUS(status);
205 status = ec_io_write(ec, ec->data_port, address, EC_EVENT_INPUT_BUFFER_EMPTY);
206 if (ACPI_FAILURE(status)) {
207 DEBUG_PRINT(ACPI_WARN, ("Unable to send 'write address' to EC.\n"));
208 return_ACPI_STATUS(status);
211 status = ec_io_write(ec, ec->data_port, data, EC_EVENT_INPUT_BUFFER_EMPTY);
212 if (ACPI_FAILURE(status)) {
213 DEBUG_PRINT(ACPI_WARN, ("Unable to send 'write data' to EC.\n"));
214 return_ACPI_STATUS(status);
217 DEBUG_PRINT(ACPI_INFO, ("Wrote data[0x%02x] to address[0x%02x] on ec.\n", data, address));
219 return_ACPI_STATUS(status);
222 static ACPI_STATUS
223 ec_transaction (
224 ec_context_t *ec,
225 EC_REQUEST *request)
227 ACPI_STATUS status = AE_OK;
229 FUNCTION_TRACE("ec_transaction");
231 if (!ec || !request)
232 return_ACPI_STATUS(AE_BAD_PARAMETER);
235 * Obtaining semaphore (mutex) to serialize all EC transactions.
238 DEBUG_PRINT(ACPI_INFO, ("Calling acpi_os_wait_semaphore(%p, 1, %d)\n", ec->mutex, EC_DEFAULT_TIMEOUT));
239 status = acpi_os_wait_semaphore(ec->mutex, 1, EC_DEFAULT_TIMEOUT);
240 if (ACPI_FAILURE(status))
241 return_ACPI_STATUS(status);
245 * Perform the transaction.
247 switch (request->command) {
249 case EC_COMMAND_READ:
250 status = ec_read(ec, request->address, &(request->data));
251 break;
253 case EC_COMMAND_WRITE:
254 status = ec_write(ec, request->address, request->data);
255 break;
257 default:
258 status = AE_SUPPORT;
259 break;
263 * Signal the semaphore (mutex) to indicate transaction completion.
266 DEBUG_PRINT(ACPI_INFO, ("Calling acpi_os_signal_semaphore(%p, 1)\n", ec->mutex));
267 acpi_os_signal_semaphore(ec->mutex, 1);
270 return_ACPI_STATUS(status);
273 static ACPI_STATUS
274 ec_space_setup (
275 ACPI_HANDLE region_handle,
276 UINT32 function,
277 void *handler_context,
278 void **return_context)
280 // TODO: What is this function for?
282 * The ec object is in the handler context and is needed
283 * when calling the ec_space_handler.
285 *return_context = handler_context;
287 return AE_OK;
293 static void
294 ec_query_handler (
295 void *context)
297 ACPI_STATUS status = AE_OK;
298 EC_QUERY_DATA *ec_q = (EC_QUERY_DATA*)context;
300 FUNCTION_TRACE("ec_query_handler");
302 if (!ec_q || !ec_q->ec) {
303 DEBUG_PRINT(ACPI_ERROR, ("Invalid (NULL) context.\n"));
304 return_VOID;
308 * Evaluate _Qxx:
309 * --------------
310 * Evaluate corresponding _Qxx method. Note that a zero query
311 * value indicates a spurious EC_SCI (no such thing as _Q00).
313 object_name[2] = hex[((ec_q->data >> 4) & 0x0F)];
314 object_name[3] = hex[(ec_q->data & 0x0F)];
316 DEBUG_PRINT(ACPI_INFO, ("Read query data[0x%02x] from ec - evaluating [%s].\n", ec_q->data, object_name));
318 status = acpi_evaluate_object(ec_q->ec->acpi_handle, object_name, NULL, NULL);
320 kfree(ec_q);
322 return_VOID;
326 * handle GPE
328 static void
329 ec_gpe_handler(void *context)
331 ACPI_STATUS status = AE_OK;
332 ec_context_t *ec = (ec_context_t *) context;
333 EC_QUERY_DATA *ec_q = NULL;
334 EC_STATUS ec_status = 0;
336 FUNCTION_TRACE("ec_gpe_handler");
338 if (!ec) {
339 DEBUG_PRINT(ACPI_INFO, ("Invalid (NULL) context.\n"));
340 return_VOID;
343 // GET SPINLOCK!
346 * EC_SCI?
347 * -------
348 * Check the EC_SCI bit to see if this is an EC_SCI event. If not (e.g.
349 * OBF/IBE) just return, as we already poll to detect these events.
351 ec_status = acpi_os_in8(ec->status_port);
352 DEBUG_PRINT(ACPI_INFO, ("EC Status Register: [0x%02x]\n", ec_status));
353 if (!(ec_status & EC_FLAG_SCI))
354 return_VOID;
356 DEBUG_PRINT(ACPI_INFO, ("EC_SCI detected - running QUERY.\n"));
358 // TODO: Need GFP_ATOMIC 'switch' for OSL interface...
359 ec_q = kmalloc(sizeof(EC_QUERY_DATA), GFP_ATOMIC);
360 if (!ec_q) {
361 DEBUG_PRINT(ACPI_INFO, ("Memory allocation failure.\n"));
362 return_VOID;
365 ec_q->ec = ec;
366 ec_q->data = 0;
369 * Run Query:
370 * ----------
371 * Query the EC to find out which _Qxx method we need to evaluate.
372 * Note that successful completion of the query causes the EC_SCI
373 * bit to be cleared (and thus clearing the interrupt source).
375 status = ec_io_write(ec, ec->status_port, EC_COMMAND_QUERY, EC_EVENT_OUTPUT_BUFFER_FULL);
376 if (ACPI_FAILURE(status)) {
377 DEBUG_PRINT(ACPI_WARN, ("Unable to send 'query command' to EC.\n"));
378 goto End;
381 status = ec_io_read(ec, ec->data_port, &(ec_q->data), EC_EVENT_NONE);
382 if (ACPI_FAILURE(status)) {
383 DEBUG_PRINT(ACPI_WARN, ("Error reading query data.\n"));
384 goto End;
387 // RELEASE SPINLOCK!
389 if (!ec_q->data) {
390 DEBUG_PRINT(ACPI_WARN, ("Spurious EC SCI detected.\n"));
391 status = AE_ERROR;
392 goto End;
396 * Defer _Qxx Execution:
397 * ---------------------
398 * Can't evaluate this method now 'cause we're at interrupt-level.
400 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE, ec_query_handler, ec_q);
401 if (ACPI_FAILURE(status)) {
402 DEBUG_PRINT(ACPI_ERROR, ("Unable to defer _Qxx method evaluation.\n"));
403 goto End;
406 End:
407 if (ACPI_FAILURE(status))
408 kfree(ec_q);
410 return_VOID;
413 static ACPI_STATUS
414 ec_region_setup (
415 ACPI_HANDLE handle,
416 u32 function,
417 void *handler_context,
418 void **region_context)
420 FUNCTION_TRACE("acpi_ec_region_setup");
422 printk("acpi_ec_region_setup\n");
424 if (function == ACPI_REGION_DEACTIVATE)
426 if (*region_context)
428 acpi_cm_free (*region_context);
429 *region_context = NULL;
432 return_ACPI_STATUS (AE_OK);
435 *region_context = NULL;
437 return_ACPI_STATUS (AE_OK);
440 /*****************************************************************************
442 * FUNCTION: ec_region_handler
444 * PARAMETERS: function - Read or Write operation
445 * address - Where in the space to read or write
446 * bit_width - Field width in bits (8, 16, or 32)
447 * value - Pointer to in or out value
448 * context - context pointer
450 * RETURN: <TBD>
452 * DESCRIPTION: Handler for the Embedded Controller (EC) address space
453 * (Op Region)
455 ****************************************************************************/
457 static ACPI_STATUS
458 ec_region_handler (
459 UINT32 function,
460 ACPI_PHYSICAL_ADDRESS address,
461 UINT32 bit_width,
462 UINT32 *value,
463 void *handler_context,
464 void *region_context)
466 ACPI_STATUS status = AE_OK;
467 ec_context_t *ec = NULL;
468 EC_REQUEST ec_request;
470 FUNCTION_TRACE("ec_space_handler");
472 if (address > 0xFF || bit_width != 8 || !value || !handler_context)
473 return_ACPI_STATUS(AE_BAD_PARAMETER);
475 ec = (ec_context_t*)handler_context;
477 switch (function) {
479 case ADDRESS_SPACE_READ:
480 ec_request.command = EC_COMMAND_READ;
481 ec_request.address = address;
482 ec_request.data = 0;
483 break;
485 case ADDRESS_SPACE_WRITE:
486 ec_request.command = EC_COMMAND_WRITE;
487 ec_request.address = address;
488 ec_request.data = (UINT8)(*value);
489 break;
491 default:
492 DEBUG_PRINT(ACPI_WARN, ("Received request with invalid function [0x%08X].\n", function));
493 return_ACPI_STATUS(AE_BAD_PARAMETER);
494 break;
497 DEBUG_PRINT(ACPI_INFO, ("device[ec] command[0x%02X] address[0x%02X] data[0x%02X]\n", ec_request.command, ec_request.address, ec_request.data));
500 * Perform the Transaction.
502 status = ec_transaction(ec, &ec_request);
503 if (ACPI_SUCCESS(status))
504 (*value) = (UINT32)ec_request.data;
506 return_ACPI_STATUS(status);
510 * Get Embedded Controller information
512 static ACPI_STATUS
513 found_ec(
514 ACPI_HANDLE handle,
515 u32 level,
516 void *ctx,
517 void **value)
519 ACPI_STATUS status;
520 ACPI_OBJECT obj;
521 ACPI_BUFFER buf;
522 RESOURCE *res;
523 ec_context_t *ec_cxt;
525 buf.length = 0;
526 buf.pointer = NULL;
527 if (acpi_get_current_resources(handle, &buf) != AE_BUFFER_OVERFLOW)
528 return AE_OK;
530 buf.pointer = kmalloc(buf.length, GFP_KERNEL);
531 if (!buf.pointer)
532 return AE_NO_MEMORY;
534 if (!ACPI_SUCCESS(acpi_get_current_resources(handle, &buf))) {
535 kfree(buf.pointer);
536 return AE_OK;
539 ec_cxt = kmalloc(sizeof(ec_context_t), GFP_KERNEL);
540 if (!ec_cxt) {
541 kfree(buf.pointer);
542 return AE_NO_MEMORY;
545 ec_cxt->acpi_handle = handle;
547 res = (RESOURCE*) buf.pointer;
548 ec_cxt->data_port = res->data.io.min_base_address;
549 res = NEXT_RESOURCE(res);
550 ec_cxt->status_port = (int) res->data.io.min_base_address;
552 kfree(buf.pointer);
554 /* determine GPE bit */
555 /* BUG: in acpi 2.0 this could return a package */
556 buf.length = sizeof(obj);
557 buf.pointer = &obj;
558 if (!ACPI_SUCCESS(acpi_evaluate_object(handle, "_GPE", NULL, &buf))
559 || obj.type != ACPI_TYPE_NUMBER)
560 return AE_OK;
562 ec_cxt->gpe_bit = obj.number.value;
564 /* determine if we need the Global Lock when accessing */
565 buf.length = sizeof(obj);
566 buf.pointer = &obj;
568 status = acpi_evaluate_object(handle, "_GLK", NULL, &buf);
569 if (status == AE_NOT_FOUND)
570 ec_cxt->need_global_lock = 0;
571 else if (!ACPI_SUCCESS(status) || obj.type != ACPI_TYPE_NUMBER) {
572 DEBUG_PRINT(ACPI_ERROR, ("_GLK failed\n"));
573 return AE_OK;
576 ec_cxt->need_global_lock = obj.number.value;
578 printk(KERN_INFO "ACPI: found EC @ (0x%02x,0x%02x,gpe %d GL %d)\n",
579 ec_cxt->data_port, ec_cxt->status_port, ec_cxt->gpe_bit,
580 ec_cxt->need_global_lock);
582 if (!ACPI_SUCCESS(acpi_install_gpe_handler(
583 ec_cxt->gpe_bit,
584 ACPI_EVENT_EDGE_TRIGGERED,
585 ec_gpe_handler,
586 ec_cxt))) {
588 REPORT_ERROR(("Could not install GPE handler for EC.\n"));
589 return AE_OK;
592 status = acpi_install_address_space_handler (handle, ADDRESS_SPACE_EC,
593 ec_region_handler, ec_region_setup, ec_cxt);
595 if (!ACPI_SUCCESS(status)) {
596 REPORT_ERROR(("Could not install EC address "
597 "space handler, error %s\n", acpi_cm_format_exception (status)));
600 return AE_OK;
604 acpi_ec_init(void)
606 acpi_get_devices(ACPI_EC_HID,
607 found_ec,
608 NULL,
609 NULL);
611 return 0;
615 acpi_ec_terminate(void)
617 /* TODO */
618 /* walk list of EC's */
619 /* free their context and release resources */
620 return 0;