Linux Audio

Check our new training course

Loading...
v4.6
 
  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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 29
 30#include <linux/kernel.h>
 31#include <linux/module.h>
 32#include <linux/slab.h>
 33#include <linux/init.h>
 34#include <linux/types.h>
 35#include <linux/acpi.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
 43MODULE_AUTHOR("Jamey Hicks, Carlos Corbacho");
 44MODULE_DESCRIPTION("HP Compaq TC1100 Tablet WMI Extras");
 45MODULE_LICENSE("GPL");
 46MODULE_ALIAS("wmi:C364AC71-36DB-495A-8494-B439D472A505");
 47
 48static struct platform_device *tc1100_device;
 49
 50struct tc1100_data {
 51	u32 wireless;
 52	u32 jogdial;
 53};
 54
 55#ifdef CONFIG_PM
 56static struct tc1100_data suspend_data;
 57#endif
 58
 59/* --------------------------------------------------------------------------
 60				Device Management
 61   -------------------------------------------------------------------------- */
 62
 63static int get_state(u32 *out, u8 instance)
 64{
 65	u32 tmp;
 66	acpi_status status;
 67	struct acpi_buffer result = { ACPI_ALLOCATE_BUFFER, NULL };
 68	union acpi_object *obj;
 69
 70	if (!out)
 71		return -EINVAL;
 72
 73	if (instance > 2)
 74		return -ENODEV;
 75
 76	status = wmi_query_block(GUID, instance, &result);
 77	if (ACPI_FAILURE(status))
 78		return -ENODEV;
 79
 80	obj = (union acpi_object *) result.pointer;
 81	if (obj && obj->type == ACPI_TYPE_INTEGER) {
 82		tmp = obj->integer.value;
 83	} else {
 84		tmp = 0;
 85	}
 86
 87	if (result.length > 0)
 88		kfree(result.pointer);
 89
 90	switch (instance) {
 91	case TC1100_INSTANCE_WIRELESS:
 92		*out = (tmp == 3) ? 1 : 0;
 93		return 0;
 94	case TC1100_INSTANCE_JOGDIAL:
 95		*out = (tmp == 1) ? 0 : 1;
 96		return 0;
 97	default:
 98		return -ENODEV;
 99	}
100}
101
102static int set_state(u32 *in, u8 instance)
103{
104	u32 value;
105	acpi_status status;
106	struct acpi_buffer input;
107
108	if (!in)
109		return -EINVAL;
110
111	if (instance > 2)
112		return -ENODEV;
113
114	switch (instance) {
115	case TC1100_INSTANCE_WIRELESS:
116		value = (*in) ? 1 : 2;
117		break;
118	case TC1100_INSTANCE_JOGDIAL:
119		value = (*in) ? 0 : 1;
120		break;
121	default:
122		return -ENODEV;
123	}
124
125	input.length = sizeof(u32);
126	input.pointer = &value;
127
128	status = wmi_set_block(GUID, instance, &input);
129	if (ACPI_FAILURE(status))
130		return -ENODEV;
131
132	return 0;
133}
134
135/* --------------------------------------------------------------------------
136				FS Interface (/sys)
137   -------------------------------------------------------------------------- */
138
139/*
140 * Read/ write bool sysfs macro
141 */
142#define show_set_bool(value, instance) \
143static ssize_t \
144show_bool_##value(struct device *dev, struct device_attribute *attr, \
145	char *buf) \
146{ \
147	u32 result; \
148	acpi_status status = get_state(&result, instance); \
149	if (ACPI_SUCCESS(status)) \
150		return sprintf(buf, "%d\n", result); \
151	return sprintf(buf, "Read error\n"); \
152} \
153\
154static ssize_t \
155set_bool_##value(struct device *dev, struct device_attribute *attr, \
156	const char *buf, size_t count) \
157{ \
158	u32 tmp = simple_strtoul(buf, NULL, 10); \
159	acpi_status status = set_state(&tmp, instance); \
160		if (ACPI_FAILURE(status)) \
161			return -EINVAL; \
162	return count; \
163} \
164static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, \
165	show_bool_##value, set_bool_##value);
166
167show_set_bool(wireless, TC1100_INSTANCE_WIRELESS);
168show_set_bool(jogdial, TC1100_INSTANCE_JOGDIAL);
169
170static struct attribute *tc1100_attributes[] = {
171	&dev_attr_wireless.attr,
172	&dev_attr_jogdial.attr,
173	NULL
174};
175
176static struct attribute_group tc1100_attribute_group = {
177	.attrs	= tc1100_attributes,
178};
179
180/* --------------------------------------------------------------------------
181				Driver Model
182   -------------------------------------------------------------------------- */
183
184static int __init tc1100_probe(struct platform_device *device)
185{
186	return sysfs_create_group(&device->dev.kobj, &tc1100_attribute_group);
187}
188
189
190static int tc1100_remove(struct platform_device *device)
191{
192	sysfs_remove_group(&device->dev.kobj, &tc1100_attribute_group);
193
194	return 0;
195}
196
197#ifdef CONFIG_PM
198static int tc1100_suspend(struct device *dev)
199{
200	int ret;
201
202	ret = get_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
203	if (ret)
204		return ret;
205
206	ret = get_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
207	if (ret)
208		return ret;
209
210	return 0;
211}
212
213static int tc1100_resume(struct device *dev)
214{
215	int ret;
216
217	ret = set_state(&suspend_data.wireless, TC1100_INSTANCE_WIRELESS);
218	if (ret)
219		return ret;
220
221	ret = set_state(&suspend_data.jogdial, TC1100_INSTANCE_JOGDIAL);
222	if (ret)
223		return ret;
224
225	return 0;
226}
227
228static const struct dev_pm_ops tc1100_pm_ops = {
229	.suspend	= tc1100_suspend,
230	.resume		= tc1100_resume,
231	.freeze		= tc1100_suspend,
232	.restore	= tc1100_resume,
233};
234#endif
235
236static struct platform_driver tc1100_driver = {
237	.driver = {
238		.name = "tc1100-wmi",
239#ifdef CONFIG_PM
240		.pm = &tc1100_pm_ops,
241#endif
242	},
243	.remove = tc1100_remove,
244};
245
246static int __init tc1100_init(void)
247{
248	int error;
249
250	if (!wmi_has_guid(GUID))
251		return -ENODEV;
252
253	tc1100_device = platform_device_alloc("tc1100-wmi", -1);
254	if (!tc1100_device)
255		return -ENOMEM;
256
257	error = platform_device_add(tc1100_device);
258	if (error)
259		goto err_device_put;
260
261	error = platform_driver_probe(&tc1100_driver, tc1100_probe);
262	if (error)
263		goto err_device_del;
264
265	pr_info("HP Compaq TC1100 Tablet WMI Extras loaded\n");
266	return 0;
267
268 err_device_del:
269	platform_device_del(tc1100_device);
270 err_device_put:
271	platform_device_put(tc1100_device);
272	return error;
273}
274
275static void __exit tc1100_exit(void)
276{
277	platform_device_unregister(tc1100_device);
278	platform_driver_unregister(&tc1100_driver);
279}
280
281module_init(tc1100_init);
282module_exit(tc1100_exit);
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  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>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 */
 10
 11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 12
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/slab.h>
 16#include <linux/init.h>
 17#include <linux/types.h>
 18#include <linux/acpi.h>
 19#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
 26MODULE_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
 31static struct platform_device *tc1100_device;
 32
 33struct tc1100_data {
 34	u32 wireless;
 35	u32 jogdial;
 36};
 37
 38#ifdef CONFIG_PM
 39static struct tc1100_data suspend_data;
 40#endif
 41
 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;
 64	if (obj && obj->type == ACPI_TYPE_INTEGER) {
 65		tmp = obj->integer.value;
 66	} else {
 67		tmp = 0;
 68	}
 69
 70	if (result.length > 0)
 71		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:
 78		*out = (tmp == 1) ? 0 : 1;
 79		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} \
147static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, \
148	show_bool_##value, set_bool_##value);
149
150show_set_bool(wireless, TC1100_INSTANCE_WIRELESS);
151show_set_bool(jogdial, TC1100_INSTANCE_JOGDIAL);
152
153static struct attribute *tc1100_attributes[] = {
154	&dev_attr_wireless.attr,
155	&dev_attr_jogdial.attr,
156	NULL
157};
158
159static struct attribute_group tc1100_attribute_group = {
160	.attrs	= tc1100_attributes,
161};
162
163/* --------------------------------------------------------------------------
164				Driver Model
165   -------------------------------------------------------------------------- */
166
167static int __init tc1100_probe(struct platform_device *device)
168{
169	return sysfs_create_group(&device->dev.kobj, &tc1100_attribute_group);
170}
171
172
173static int tc1100_remove(struct platform_device *device)
174{
175	sysfs_remove_group(&device->dev.kobj, &tc1100_attribute_group);
176
177	return 0;
178}
179
180#ifdef CONFIG_PM
181static int tc1100_suspend(struct device *dev)
182{
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
193	return 0;
194}
195
196static int tc1100_resume(struct device *dev)
197{
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
208	return 0;
209}
210
211static 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
219static struct platform_driver tc1100_driver = {
220	.driver = {
221		.name = "tc1100-wmi",
222#ifdef CONFIG_PM
223		.pm = &tc1100_pm_ops,
224#endif
225	},
226	.remove = tc1100_remove,
227};
228
229static int __init tc1100_init(void)
230{
231	int error;
232
233	if (!wmi_has_guid(GUID))
234		return -ENODEV;
235
236	tc1100_device = platform_device_alloc("tc1100-wmi", -1);
237	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;
247
248	pr_info("HP Compaq TC1100 Tablet WMI Extras loaded\n");
249	return 0;
250
251 err_device_del:
252	platform_device_del(tc1100_device);
253 err_device_put:
254	platform_device_put(tc1100_device);
255	return error;
256}
257
258static void __exit tc1100_exit(void)
259{
260	platform_device_unregister(tc1100_device);
261	platform_driver_unregister(&tc1100_driver);
262}
263
264module_init(tc1100_init);
265module_exit(tc1100_exit);