blob: 1fe0f1feff71607e9397dac7aed322af871b9424 [file] [log] [blame]
Carlos Corbachodd8cd772008-02-05 02:17:15 +00001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Carlos Corbachodd8cd772008-02-05 02:17:15 +000031#include <linux/init.h>
32#include <linux/types.h>
33#include <acpi/acpi.h>
Carlos Corbachodd8cd772008-02-05 02:17:15 +000034#include <acpi/acpi_bus.h>
35#include <acpi/acpi_drivers.h>
36#include <linux/platform_device.h>
37
38#define GUID "C364AC71-36DB-495A-8494-B439D472A505"
39
40#define TC1100_INSTANCE_WIRELESS 1
41#define TC1100_INSTANCE_JOGDIAL 2
42
43#define TC1100_LOGPREFIX "tc1100-wmi: "
44#define TC1100_INFO KERN_INFO TC1100_LOGPREFIX
45
46MODULE_AUTHOR("Jamey Hicks, Carlos Corbacho");
47MODULE_DESCRIPTION("HP Compaq TC1100 Tablet WMI Extras");
48MODULE_LICENSE("GPL");
49MODULE_ALIAS("wmi:C364AC71-36DB-495A-8494-B439D472A505");
50
Carlos Corbachodd8cd772008-02-05 02:17:15 +000051static struct platform_device *tc1100_device;
52
53struct tc1100_data {
54 u32 wireless;
55 u32 jogdial;
56};
57
58static struct tc1100_data suspend_data;
59
60/* --------------------------------------------------------------------------
61 Device Management
62 -------------------------------------------------------------------------- */
63
64static int get_state(u32 *out, u8 instance)
65{
66 u32 tmp;
67 acpi_status status;
68 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
69 union acpi_object *obj;
70
71 if (!out)
72 return -EINVAL;
73
74 if (instance > 2)
75 return -ENODEV;
76
77 status = wmi_query_block(GUID, instance, &result);
78 if (ACPI_FAILURE(status))
79 return -ENODEV;
80
81 obj = (union acpi_object *) result.pointer;
Krzysztof Kosiński07de5bd2009-03-19 23:22:31 +010082 if (obj && obj->type == ACPI_TYPE_INTEGER) {
83 tmp = obj->integer.value;
Carlos Corbachodd8cd772008-02-05 02:17:15 +000084 } else {
85 tmp = 0;
86 }
87
88 if (result.length > 0 && result.pointer)
89 kfree(result.pointer);
90
91 switch (instance) {
92 case TC1100_INSTANCE_WIRELESS:
93 *out = (tmp == 3) ? 1 : 0;
94 return 0;
95 case TC1100_INSTANCE_JOGDIAL:
Krzysztof Kosiński07de5bd2009-03-19 23:22:31 +010096 *out = (tmp == 1) ? 0 : 1;
Carlos Corbachodd8cd772008-02-05 02:17:15 +000097 return 0;
98 default:
99 return -ENODEV;
100 }
101}
102
103static int set_state(u32 *in, u8 instance)
104{
105 u32 value;
106 acpi_status status;
107 struct acpi_buffer input;
108
109 if (!in)
110 return -EINVAL;
111
112 if (instance > 2)
113 return -ENODEV;
114
115 switch (instance) {
116 case TC1100_INSTANCE_WIRELESS:
117 value = (*in) ? 1 : 2;
118 break;
119 case TC1100_INSTANCE_JOGDIAL:
120 value = (*in) ? 0 : 1;
121 break;
122 default:
123 return -ENODEV;
124 }
125
126 input.length = sizeof(u32);
127 input.pointer = &value;
128
129 status = wmi_set_block(GUID, instance, &input);
130 if (ACPI_FAILURE(status))
131 return -ENODEV;
132
133 return 0;
134}
135
136/* --------------------------------------------------------------------------
137 FS Interface (/sys)
138 -------------------------------------------------------------------------- */
139
140/*
141 * Read/ write bool sysfs macro
142 */
143#define show_set_bool(value, instance) \
144static ssize_t \
145show_bool_##value(struct device *dev, struct device_attribute *attr, \
146 char *buf) \
147{ \
148 u32 result; \
149 acpi_status status = get_state(&result, instance); \
150 if (ACPI_SUCCESS(status)) \
151 return sprintf(buf, "%d\n", result); \
152 return sprintf(buf, "Read error\n"); \
153} \
154\
155static ssize_t \
156set_bool_##value(struct device *dev, struct device_attribute *attr, \
157 const char *buf, size_t count) \
158{ \
159 u32 tmp = simple_strtoul(buf, NULL, 10); \
160 acpi_status status = set_state(&tmp, instance); \
161 if (ACPI_FAILURE(status)) \
162 return -EINVAL; \
163 return count; \
164} \
165static DEVICE_ATTR(value, S_IWUGO | S_IRUGO | S_IWUSR, \
166 show_bool_##value, set_bool_##value);
167
168show_set_bool(wireless, TC1100_INSTANCE_WIRELESS);
169show_set_bool(jogdial, TC1100_INSTANCE_JOGDIAL);
170
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800171static struct attribute *tc1100_attributes[] = {
172 &dev_attr_wireless.attr,
173 &dev_attr_jogdial.attr,
174 NULL
175};
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000176
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800177static struct attribute_group tc1100_attribute_group = {
178 .attrs = tc1100_attributes,
179};
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000180
181/* --------------------------------------------------------------------------
182 Driver Model
183 -------------------------------------------------------------------------- */
184
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800185static int __init tc1100_probe(struct platform_device *device)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000186{
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800187 return sysfs_create_group(&device->dev.kobj, &tc1100_attribute_group);
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000188}
189
190
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800191static int __devexit tc1100_remove(struct platform_device *device)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000192{
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800193 sysfs_remove_group(&device->dev.kobj, &tc1100_attribute_group);
194
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000195 return 0;
196}
197
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800198#ifdef CONFIG_PM
199static int tc1100_suspend(struct device *dev)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000200{
201 int ret;
202
203 ret = get_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
204 if (ret)
205 return ret;
206
207 ret = get_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
208 if (ret)
209 return ret;
210
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800211 return 0;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000212}
213
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800214static int tc1100_resume(struct device *dev)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000215{
216 int ret;
217
218 ret = set_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
219 if (ret)
220 return ret;
221
222 ret = set_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
223 if (ret)
224 return ret;
225
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800226 return 0;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000227}
228
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800229static const struct dev_pm_ops tc1100_pm_ops = {
230 .suspend = tc1100_suspend,
231 .resume = tc1100_resume,
232 .freeze = tc1100_suspend,
233 .restore = tc1100_resume,
234};
235#endif
236
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800237static struct platform_driver tc1100_driver = {
238 .driver = {
239 .name = "tc1100-wmi",
240 .owner = THIS_MODULE,
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800241#ifdef CONFIG_PM
242 .pm = &tc1100_pm_ops,
243#endif
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800244 },
245 .remove = __devexit_p(tc1100_remove),
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800246};
247
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000248static int __init tc1100_init(void)
249{
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800250 int error;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000251
252 if (!wmi_has_guid(GUID))
253 return -ENODEV;
254
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000255 tc1100_device = platform_device_alloc("tc1100-wmi", -1);
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800256 if (!tc1100_device)
257 return -ENOMEM;
258
259 error = platform_device_add(tc1100_device);
260 if (error)
261 goto err_device_put;
262
263 error = platform_driver_probe(&tc1100_driver, tc1100_probe);
264 if (error)
265 goto err_device_del;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000266
267 printk(TC1100_INFO "HP Compaq TC1100 Tablet WMI Extras loaded\n");
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800268 return 0;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000269
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800270 err_device_del:
271 platform_device_del(tc1100_device);
272 err_device_put:
273 platform_device_put(tc1100_device);
274 return error;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000275}
276
277static void __exit tc1100_exit(void)
278{
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800279 platform_device_unregister(tc1100_device);
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000280 platform_driver_unregister(&tc1100_driver);
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000281}
282
283module_init(tc1100_init);
284module_exit(tc1100_exit);