import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / acpi / ospm / ec / ecspace.c
1 /*****************************************************************************
2  *
3  * Module Name: ecspace.c
4  *   $Revision: 1.1.1.1 $
5  *
6  *****************************************************************************/
7
8 /*
9  *  Copyright (C) 2000, 2001 Andrew Grover
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26
27 #include <acpi.h>
28 #include "ec.h"
29
30 #define _COMPONENT              ACPI_EC
31         MODULE_NAME             ("ecspace")
32
33
34 /****************************************************************************
35  *
36  * FUNCTION:    ec_space_setup
37  *
38  * PARAMETERS:
39  *
40  * RETURN:
41  *
42  * DESCRIPTION:
43  *
44  ****************************************************************************/
45
46 acpi_status
47 ec_space_setup (
48         acpi_handle             region_handle,
49         u32                     function,
50         void                    *handler_context,
51         void                    **return_context)
52 {
53         /*
54          * The EC object is in the handler context and is needed
55          * when calling the ec_space_handler.
56          */
57         *return_context = handler_context;
58
59         return AE_OK;
60 }
61
62
63 /****************************************************************************
64  *
65  * FUNCTION:    ec_space_handler
66  *
67  * PARAMETERS:  function            - Read or Write operation
68  *              address             - Where in the space to read or write
69  *              bit_width           - Field width in bits (should be 8)
70  *              value               - Pointer to in or out value
71  *              context             - context pointer
72  *
73  * RETURN:
74  *
75  * DESCRIPTION: Handler for the Embedded Controller (EC) address space
76  *              (Op Region)
77  *
78  ****************************************************************************/
79
80 acpi_status
81 ec_space_handler (
82         u32                     function,
83         ACPI_PHYSICAL_ADDRESS   address,
84         u32                     bit_width,
85         u32                     *value,
86         void                    *handler_context,
87         void                    *region_context)
88 {
89         acpi_status             status = AE_OK;
90         EC_CONTEXT              *ec = NULL;
91         EC_REQUEST              ec_request;
92
93         FUNCTION_TRACE("ec_space_handler");
94
95         if (address > 0xFF || bit_width != 8 || !value || !handler_context) {
96                 return_ACPI_STATUS(AE_BAD_PARAMETER);
97         }
98
99         ec = (EC_CONTEXT*)handler_context;
100
101         switch (function) {
102
103         case ACPI_READ_ADR_SPACE:
104                 ec_request.command = EC_COMMAND_READ;
105                 ec_request.address = address;
106                 ec_request.data = 0;
107                 break;
108
109         case ACPI_WRITE_ADR_SPACE:
110                 ec_request.command = EC_COMMAND_WRITE;
111                 ec_request.address = address;
112                 ec_request.data = (u8)(*value);
113                 break;
114
115         default:
116                 ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Received request with invalid function [%X].\n", function));
117                 return_ACPI_STATUS(AE_BAD_PARAMETER);
118                 break;
119         }
120
121         /*
122          * Perform the Transaction.
123          */
124         status = ec_transaction(ec, &ec_request);
125         if (ACPI_SUCCESS(status)) {
126                 (*value) = (u32)ec_request.data;
127         }
128
129         return_ACPI_STATUS(status);
130 }
131
132
133 /****************************************************************************
134  *
135  * FUNCTION:    ec_install_space_handler
136  *
137  * PARAMETERS:
138  *
139  * RETURN:
140  *
141  * DESCRIPTION:
142  *
143  ****************************************************************************/
144
145 acpi_status
146 ec_install_space_handler (
147         EC_CONTEXT              *ec)
148 {
149         acpi_status             status = AE_OK;
150
151         FUNCTION_TRACE("ec_install_space_handler");
152
153         if (!ec) {
154                 return_ACPI_STATUS(AE_BAD_PARAMETER);
155         }
156
157         status = acpi_install_address_space_handler (ec->acpi_handle,
158                 ACPI_ADR_SPACE_EC, &ec_space_handler, &ec_space_setup, ec);
159         
160         return_ACPI_STATUS(status);
161 }
162
163
164 /****************************************************************************
165  *
166  * FUNCTION:    ec_remove_space_handler
167  *
168  * PARAMETERS:
169  *
170  * RETURN:
171  *
172  * DESCRIPTION:
173  *
174  ****************************************************************************/
175
176 acpi_status
177 ec_remove_space_handler (
178         EC_CONTEXT              *ec)
179 {
180         acpi_status             status = AE_OK;
181
182         FUNCTION_TRACE("ec_remove_space_handler");
183
184         if (!ec) {
185                 return_ACPI_STATUS(AE_BAD_PARAMETER);
186         }
187
188         status = acpi_remove_address_space_handler(ec->acpi_handle,
189                 ACPI_ADR_SPACE_EC, &ec_space_handler);
190
191         return_ACPI_STATUS(status);
192 }