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