Merge branch 'cpus4096-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / drivers / platform / x86 / tc1100-wmi.c
1 /*
2  *  HP Compaq TC1100 Tablet WMI Extras Driver
3  *
4  *  Copyright (C) 2007 Carlos Corbacho <carlos@strangeworlds.co.uk>
5  *  Copyright (C) 2004 Jamey Hicks <jamey.hicks@hp.com>
6  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 (at
14  *  your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24  *
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <acpi/acpi.h>
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
35 #include <linux/platform_device.h>
36
37 #define GUID "C364AC71-36DB-495A-8494-B439D472A505"
38
39 #define TC1100_INSTANCE_WIRELESS                1
40 #define TC1100_INSTANCE_JOGDIAL         2
41
42 #define TC1100_LOGPREFIX "tc1100-wmi: "
43 #define TC1100_INFO KERN_INFO TC1100_LOGPREFIX
44
45 MODULE_AUTHOR("Jamey Hicks, Carlos Corbacho");
46 MODULE_DESCRIPTION("HP Compaq TC1100 Tablet WMI Extras");
47 MODULE_LICENSE("GPL");
48 MODULE_ALIAS("wmi:C364AC71-36DB-495A-8494-B439D472A505");
49
50 static int tc1100_probe(struct platform_device *device);
51 static int tc1100_remove(struct platform_device *device);
52 static int tc1100_suspend(struct platform_device *device, pm_message_t state);
53 static int tc1100_resume(struct platform_device *device);
54
55 static struct platform_driver tc1100_driver = {
56         .driver = {
57                 .name = "tc1100-wmi",
58                 .owner = THIS_MODULE,
59         },
60         .probe = tc1100_probe,
61         .remove = tc1100_remove,
62         .suspend = tc1100_suspend,
63         .resume = tc1100_resume,
64 };
65
66 static struct platform_device *tc1100_device;
67
68 struct tc1100_data {
69         u32 wireless;
70         u32 jogdial;
71 };
72
73 static struct tc1100_data suspend_data;
74
75 /* --------------------------------------------------------------------------
76                                 Device Management
77    -------------------------------------------------------------------------- */
78
79 static int get_state(u32 *out, u8 instance)
80 {
81         u32 tmp;
82         acpi_status status;
83         struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
84         union acpi_object *obj;
85
86         if (!out)
87                 return -EINVAL;
88
89         if (instance > 2)
90                 return -ENODEV;
91
92         status = wmi_query_block(GUID, instance, &result);
93         if (ACPI_FAILURE(status))
94                 return -ENODEV;
95
96         obj = (union acpi_object *) result.pointer;
97         if (obj && obj->type == ACPI_TYPE_BUFFER &&
98                 obj->buffer.length == sizeof(u32)) {
99                 tmp = *((u32 *) obj->buffer.pointer);
100         } else {
101                 tmp = 0;
102         }
103
104         if (result.length > 0 && result.pointer)
105                 kfree(result.pointer);
106
107         switch (instance) {
108         case TC1100_INSTANCE_WIRELESS:
109                 *out = (tmp == 3) ? 1 : 0;
110                 return 0;
111         case TC1100_INSTANCE_JOGDIAL:
112                 *out = (tmp == 1) ? 1 : 0;
113                 return 0;
114         default:
115                 return -ENODEV;
116         }
117 }
118
119 static int set_state(u32 *in, u8 instance)
120 {
121         u32 value;
122         acpi_status status;
123         struct acpi_buffer input;
124
125         if (!in)
126                 return -EINVAL;
127
128         if (instance > 2)
129                 return -ENODEV;
130
131         switch (instance) {
132         case TC1100_INSTANCE_WIRELESS:
133                 value = (*in) ? 1 : 2;
134                 break;
135         case TC1100_INSTANCE_JOGDIAL:
136                 value = (*in) ? 0 : 1;
137                 break;
138         default:
139                 return -ENODEV;
140         }
141
142         input.length = sizeof(u32);
143         input.pointer = &value;
144
145         status = wmi_set_block(GUID, instance, &input);
146         if (ACPI_FAILURE(status))
147                 return -ENODEV;
148
149         return 0;
150 }
151
152 /* --------------------------------------------------------------------------
153                                 FS Interface (/sys)
154    -------------------------------------------------------------------------- */
155
156 /*
157  * Read/ write bool sysfs macro
158  */
159 #define show_set_bool(value, instance) \
160 static ssize_t \
161 show_bool_##value(struct device *dev, struct device_attribute *attr, \
162         char *buf) \
163 { \
164         u32 result; \
165         acpi_status status = get_state(&result, instance); \
166         if (ACPI_SUCCESS(status)) \
167                 return sprintf(buf, "%d\n", result); \
168         return sprintf(buf, "Read error\n"); \
169 } \
170 \
171 static ssize_t \
172 set_bool_##value(struct device *dev, struct device_attribute *attr, \
173         const char *buf, size_t count) \
174 { \
175         u32 tmp = simple_strtoul(buf, NULL, 10); \
176         acpi_status status = set_state(&tmp, instance); \
177                 if (ACPI_FAILURE(status)) \
178                         return -EINVAL; \
179         return count; \
180 } \
181 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
182         show_bool_##value, set_bool_##value);
183
184 show_set_bool(wireless, TC1100_INSTANCE_WIRELESS);
185 show_set_bool(jogdial, TC1100_INSTANCE_JOGDIAL);
186
187 static void remove_fs(void)
188 {
189         device_remove_file(&tc1100_device->dev, &dev_attr_wireless);
190         device_remove_file(&tc1100_device->dev, &dev_attr_jogdial);
191 }
192
193 static int add_fs(void)
194 {
195         int ret;
196
197         ret = device_create_file(&tc1100_device->dev, &dev_attr_wireless);
198         if (ret)
199                 goto add_sysfs_error;
200
201         ret = device_create_file(&tc1100_device->dev, &dev_attr_jogdial);
202         if (ret)
203                 goto add_sysfs_error;
204
205         return ret;
206
207 add_sysfs_error:
208         remove_fs();
209         return ret;
210 }
211
212 /* --------------------------------------------------------------------------
213                                 Driver Model
214    -------------------------------------------------------------------------- */
215
216 static int tc1100_probe(struct platform_device *device)
217 {
218         int result = 0;
219
220         result = add_fs();
221         return result;
222 }
223
224
225 static int tc1100_remove(struct platform_device *device)
226 {
227         remove_fs();
228         return 0;
229 }
230
231 static int tc1100_suspend(struct platform_device *dev, pm_message_t state)
232 {
233         int ret;
234
235         ret = get_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
236         if (ret)
237                 return ret;
238
239         ret = get_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
240         if (ret)
241                 return ret;
242
243         return ret;
244 }
245
246 static int tc1100_resume(struct platform_device *dev)
247 {
248         int ret;
249
250         ret = set_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
251         if (ret)
252                 return ret;
253
254         ret = set_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
255         if (ret)
256                 return ret;
257
258         return ret;
259 }
260
261 static int __init tc1100_init(void)
262 {
263         int result = 0;
264
265         if (!wmi_has_guid(GUID))
266                 return -ENODEV;
267
268         result = platform_driver_register(&tc1100_driver);
269         if (result)
270                 return result;
271
272         tc1100_device = platform_device_alloc("tc1100-wmi", -1);
273         platform_device_add(tc1100_device);
274
275         printk(TC1100_INFO "HP Compaq TC1100 Tablet WMI Extras loaded\n");
276
277         return result;
278 }
279
280 static void __exit tc1100_exit(void)
281 {
282         platform_device_del(tc1100_device);
283         platform_driver_unregister(&tc1100_driver);
284
285         printk(TC1100_INFO "HP Compaq TC1100 Tablet WMI Extras unloaded\n");
286 }
287
288 module_init(tc1100_init);
289 module_exit(tc1100_exit);