blob: 803920b6f01da07ea753aeea13c7b3e65a3cddbc [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Carlos Corbachodd8cd772008-02-05 02:17:15 +00002/*
3 * HP Compaq TC1100 Tablet WMI Extras Driver
4 *
5 * Copyright (C) 2007 Carlos Corbacho <carlos@strangeworlds.co.uk>
6 * Copyright (C) 2004 Jamey Hicks <jamey.hicks@hp.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Carlos Corbachodd8cd772008-02-05 02:17:15 +00009 */
10
Joe Perches33cab1b2011-03-29 15:21:49 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Carlos Corbachodd8cd772008-02-05 02:17:15 +000013#include <linux/kernel.h>
14#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Carlos Corbachodd8cd772008-02-05 02:17:15 +000016#include <linux/init.h>
17#include <linux/types.h>
Lv Zheng8b484632013-12-03 08:49:16 +080018#include <linux/acpi.h>
Carlos Corbachodd8cd772008-02-05 02:17:15 +000019#include <linux/platform_device.h>
20
21#define GUID "C364AC71-36DB-495A-8494-B439D472A505"
22
23#define TC1100_INSTANCE_WIRELESS 1
24#define TC1100_INSTANCE_JOGDIAL 2
25
Carlos Corbachodd8cd772008-02-05 02:17:15 +000026MODULE_AUTHOR("Jamey Hicks, Carlos Corbacho");
27MODULE_DESCRIPTION("HP Compaq TC1100 Tablet WMI Extras");
28MODULE_LICENSE("GPL");
29MODULE_ALIAS("wmi:C364AC71-36DB-495A-8494-B439D472A505");
30
Carlos Corbachodd8cd772008-02-05 02:17:15 +000031static struct platform_device *tc1100_device;
32
33struct tc1100_data {
34 u32 wireless;
35 u32 jogdial;
36};
37
Colin Ian King75d7e7d2016-01-06 18:02:59 +000038#ifdef CONFIG_PM
Carlos Corbachodd8cd772008-02-05 02:17:15 +000039static struct tc1100_data suspend_data;
Colin Ian King75d7e7d2016-01-06 18:02:59 +000040#endif
Carlos Corbachodd8cd772008-02-05 02:17:15 +000041
42/* --------------------------------------------------------------------------
43 Device Management
44 -------------------------------------------------------------------------- */
45
46static int get_state(u32 *out, u8 instance)
47{
48 u32 tmp;
49 acpi_status status;
50 struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
51 union acpi_object *obj;
52
53 if (!out)
54 return -EINVAL;
55
56 if (instance > 2)
57 return -ENODEV;
58
59 status = wmi_query_block(GUID, instance, &result);
60 if (ACPI_FAILURE(status))
61 return -ENODEV;
62
63 obj = (union acpi_object *) result.pointer;
Krzysztof Kosiński07de5bd2009-03-19 23:22:31 +010064 if (obj && obj->type == ACPI_TYPE_INTEGER) {
65 tmp = obj->integer.value;
Carlos Corbachodd8cd772008-02-05 02:17:15 +000066 } else {
67 tmp = 0;
68 }
69
Markus Elfring0d44b412015-06-26 21:21:16 +020070 if (result.length > 0)
Carlos Corbachodd8cd772008-02-05 02:17:15 +000071 kfree(result.pointer);
72
73 switch (instance) {
74 case TC1100_INSTANCE_WIRELESS:
75 *out = (tmp == 3) ? 1 : 0;
76 return 0;
77 case TC1100_INSTANCE_JOGDIAL:
Krzysztof Kosiński07de5bd2009-03-19 23:22:31 +010078 *out = (tmp == 1) ? 0 : 1;
Carlos Corbachodd8cd772008-02-05 02:17:15 +000079 return 0;
80 default:
81 return -ENODEV;
82 }
83}
84
85static int set_state(u32 *in, u8 instance)
86{
87 u32 value;
88 acpi_status status;
89 struct acpi_buffer input;
90
91 if (!in)
92 return -EINVAL;
93
94 if (instance > 2)
95 return -ENODEV;
96
97 switch (instance) {
98 case TC1100_INSTANCE_WIRELESS:
99 value = (*in) ? 1 : 2;
100 break;
101 case TC1100_INSTANCE_JOGDIAL:
102 value = (*in) ? 0 : 1;
103 break;
104 default:
105 return -ENODEV;
106 }
107
108 input.length = sizeof(u32);
109 input.pointer = &value;
110
111 status = wmi_set_block(GUID, instance, &input);
112 if (ACPI_FAILURE(status))
113 return -ENODEV;
114
115 return 0;
116}
117
118/* --------------------------------------------------------------------------
119 FS Interface (/sys)
120 -------------------------------------------------------------------------- */
121
122/*
123 * Read/ write bool sysfs macro
124 */
125#define show_set_bool(value, instance) \
126static ssize_t \
127show_bool_##value(struct device *dev, struct device_attribute *attr, \
128 char *buf) \
129{ \
130 u32 result; \
131 acpi_status status = get_state(&result, instance); \
132 if (ACPI_SUCCESS(status)) \
133 return sprintf(buf, "%d\n", result); \
134 return sprintf(buf, "Read error\n"); \
135} \
136\
137static ssize_t \
138set_bool_##value(struct device *dev, struct device_attribute *attr, \
139 const char *buf, size_t count) \
140{ \
141 u32 tmp = simple_strtoul(buf, NULL, 10); \
142 acpi_status status = set_state(&tmp, instance); \
143 if (ACPI_FAILURE(status)) \
144 return -EINVAL; \
145 return count; \
146} \
Vasiliy Kulikov8a6a1422011-02-04 15:24:03 +0300147static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, \
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000148 show_bool_##value, set_bool_##value);
149
150show_set_bool(wireless, TC1100_INSTANCE_WIRELESS);
151show_set_bool(jogdial, TC1100_INSTANCE_JOGDIAL);
152
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800153static struct attribute *tc1100_attributes[] = {
154 &dev_attr_wireless.attr,
155 &dev_attr_jogdial.attr,
156 NULL
157};
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000158
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800159static struct attribute_group tc1100_attribute_group = {
160 .attrs = tc1100_attributes,
161};
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000162
163/* --------------------------------------------------------------------------
164 Driver Model
165 -------------------------------------------------------------------------- */
166
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800167static int __init tc1100_probe(struct platform_device *device)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000168{
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800169 return sysfs_create_group(&device->dev.kobj, &tc1100_attribute_group);
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000170}
171
172
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800173static int tc1100_remove(struct platform_device *device)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000174{
Dmitry Torokhov0ad3dc32009-12-04 00:36:09 -0800175 sysfs_remove_group(&device->dev.kobj, &tc1100_attribute_group);
176
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000177 return 0;
178}
179
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800180#ifdef CONFIG_PM
181static int tc1100_suspend(struct device *dev)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000182{
183 int ret;
184
185 ret = get_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
186 if (ret)
187 return ret;
188
189 ret = get_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
190 if (ret)
191 return ret;
192
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800193 return 0;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000194}
195
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800196static int tc1100_resume(struct device *dev)
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000197{
198 int ret;
199
200 ret = set_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
201 if (ret)
202 return ret;
203
204 ret = set_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
205 if (ret)
206 return ret;
207
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800208 return 0;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000209}
210
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800211static const struct dev_pm_ops tc1100_pm_ops = {
212 .suspend = tc1100_suspend,
213 .resume = tc1100_resume,
214 .freeze = tc1100_suspend,
215 .restore = tc1100_resume,
216};
217#endif
218
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800219static struct platform_driver tc1100_driver = {
220 .driver = {
221 .name = "tc1100-wmi",
Dmitry Torokhov8e698a32009-12-04 00:36:20 -0800222#ifdef CONFIG_PM
223 .pm = &tc1100_pm_ops,
224#endif
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800225 },
Greg Kroah-Hartmanb859f152012-12-21 13:18:33 -0800226 .remove = tc1100_remove,
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800227};
228
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000229static int __init tc1100_init(void)
230{
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800231 int error;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000232
233 if (!wmi_has_guid(GUID))
234 return -ENODEV;
235
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000236 tc1100_device = platform_device_alloc("tc1100-wmi", -1);
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800237 if (!tc1100_device)
238 return -ENOMEM;
239
240 error = platform_device_add(tc1100_device);
241 if (error)
242 goto err_device_put;
243
244 error = platform_driver_probe(&tc1100_driver, tc1100_probe);
245 if (error)
246 goto err_device_del;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000247
Joe Perches33cab1b2011-03-29 15:21:49 -0700248 pr_info("HP Compaq TC1100 Tablet WMI Extras loaded\n");
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800249 return 0;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000250
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800251 err_device_del:
252 platform_device_del(tc1100_device);
253 err_device_put:
254 platform_device_put(tc1100_device);
255 return error;
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000256}
257
258static void __exit tc1100_exit(void)
259{
Dmitry Torokhov9634a622009-12-04 00:36:15 -0800260 platform_device_unregister(tc1100_device);
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000261 platform_driver_unregister(&tc1100_driver);
Carlos Corbachodd8cd772008-02-05 02:17:15 +0000262}
263
264module_init(tc1100_init);
265module_exit(tc1100_exit);