tc1100-wmi.c 6.5 KB

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