tc1100-wmi.c 6.9 KB

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