dell-wmi-descriptor.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Dell WMI descriptor driver
  3. *
  4. * Copyright (C) 2017 Dell Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/acpi.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/wmi.h>
  20. #include "dell-wmi-descriptor.h"
  21. #define DELL_WMI_DESCRIPTOR_GUID "8D9DDCBC-A997-11DA-B012-B622A1EF5492"
  22. struct descriptor_priv {
  23. struct list_head list;
  24. u32 interface_version;
  25. u32 size;
  26. u32 hotfix;
  27. };
  28. static int descriptor_valid = -EPROBE_DEFER;
  29. static LIST_HEAD(wmi_list);
  30. static DEFINE_MUTEX(list_mutex);
  31. int dell_wmi_get_descriptor_valid(void)
  32. {
  33. if (!wmi_has_guid(DELL_WMI_DESCRIPTOR_GUID))
  34. return -ENODEV;
  35. return descriptor_valid;
  36. }
  37. EXPORT_SYMBOL_GPL(dell_wmi_get_descriptor_valid);
  38. bool dell_wmi_get_interface_version(u32 *version)
  39. {
  40. struct descriptor_priv *priv;
  41. bool ret = false;
  42. mutex_lock(&list_mutex);
  43. priv = list_first_entry_or_null(&wmi_list,
  44. struct descriptor_priv,
  45. list);
  46. if (priv) {
  47. *version = priv->interface_version;
  48. ret = true;
  49. }
  50. mutex_unlock(&list_mutex);
  51. return ret;
  52. }
  53. EXPORT_SYMBOL_GPL(dell_wmi_get_interface_version);
  54. bool dell_wmi_get_size(u32 *size)
  55. {
  56. struct descriptor_priv *priv;
  57. bool ret = false;
  58. mutex_lock(&list_mutex);
  59. priv = list_first_entry_or_null(&wmi_list,
  60. struct descriptor_priv,
  61. list);
  62. if (priv) {
  63. *size = priv->size;
  64. ret = true;
  65. }
  66. mutex_unlock(&list_mutex);
  67. return ret;
  68. }
  69. EXPORT_SYMBOL_GPL(dell_wmi_get_size);
  70. bool dell_wmi_get_hotfix(u32 *hotfix)
  71. {
  72. struct descriptor_priv *priv;
  73. bool ret = false;
  74. mutex_lock(&list_mutex);
  75. priv = list_first_entry_or_null(&wmi_list,
  76. struct descriptor_priv,
  77. list);
  78. if (priv) {
  79. *hotfix = priv->hotfix;
  80. ret = true;
  81. }
  82. mutex_unlock(&list_mutex);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(dell_wmi_get_hotfix);
  86. /*
  87. * Descriptor buffer is 128 byte long and contains:
  88. *
  89. * Name Offset Length Value
  90. * Vendor Signature 0 4 "DELL"
  91. * Object Signature 4 4 " WMI"
  92. * WMI Interface Version 8 4 <version>
  93. * WMI buffer length 12 4 <length>
  94. * WMI hotfix number 16 4 <hotfix>
  95. */
  96. static int dell_wmi_descriptor_probe(struct wmi_device *wdev)
  97. {
  98. union acpi_object *obj = NULL;
  99. struct descriptor_priv *priv;
  100. u32 *buffer;
  101. int ret;
  102. obj = wmidev_block_query(wdev, 0);
  103. if (!obj) {
  104. dev_err(&wdev->dev, "failed to read Dell WMI descriptor\n");
  105. ret = -EIO;
  106. goto out;
  107. }
  108. if (obj->type != ACPI_TYPE_BUFFER) {
  109. dev_err(&wdev->dev, "Dell descriptor has wrong type\n");
  110. ret = -EINVAL;
  111. descriptor_valid = ret;
  112. goto out;
  113. }
  114. /* Although it's not technically a failure, this would lead to
  115. * unexpected behavior
  116. */
  117. if (obj->buffer.length != 128) {
  118. dev_err(&wdev->dev,
  119. "Dell descriptor buffer has unexpected length (%d)\n",
  120. obj->buffer.length);
  121. ret = -EINVAL;
  122. descriptor_valid = ret;
  123. goto out;
  124. }
  125. buffer = (u32 *)obj->buffer.pointer;
  126. if (strncmp(obj->string.pointer, "DELL WMI", 8) != 0) {
  127. dev_err(&wdev->dev, "Dell descriptor buffer has invalid signature (%8ph)\n",
  128. buffer);
  129. ret = -EINVAL;
  130. descriptor_valid = ret;
  131. goto out;
  132. }
  133. descriptor_valid = 0;
  134. if (buffer[2] != 0 && buffer[2] != 1)
  135. dev_warn(&wdev->dev, "Dell descriptor buffer has unknown version (%lu)\n",
  136. (unsigned long) buffer[2]);
  137. priv = devm_kzalloc(&wdev->dev, sizeof(struct descriptor_priv),
  138. GFP_KERNEL);
  139. if (!priv) {
  140. ret = -ENOMEM;
  141. goto out;
  142. }
  143. priv->interface_version = buffer[2];
  144. priv->size = buffer[3];
  145. priv->hotfix = buffer[4];
  146. ret = 0;
  147. dev_set_drvdata(&wdev->dev, priv);
  148. mutex_lock(&list_mutex);
  149. list_add_tail(&priv->list, &wmi_list);
  150. mutex_unlock(&list_mutex);
  151. dev_dbg(&wdev->dev, "Detected Dell WMI interface version %lu, buffer size %lu, hotfix %lu\n",
  152. (unsigned long) priv->interface_version,
  153. (unsigned long) priv->size,
  154. (unsigned long) priv->hotfix);
  155. out:
  156. kfree(obj);
  157. return ret;
  158. }
  159. static int dell_wmi_descriptor_remove(struct wmi_device *wdev)
  160. {
  161. struct descriptor_priv *priv = dev_get_drvdata(&wdev->dev);
  162. mutex_lock(&list_mutex);
  163. list_del(&priv->list);
  164. mutex_unlock(&list_mutex);
  165. return 0;
  166. }
  167. static const struct wmi_device_id dell_wmi_descriptor_id_table[] = {
  168. { .guid_string = DELL_WMI_DESCRIPTOR_GUID },
  169. { },
  170. };
  171. static struct wmi_driver dell_wmi_descriptor_driver = {
  172. .driver = {
  173. .name = "dell-wmi-descriptor",
  174. },
  175. .probe = dell_wmi_descriptor_probe,
  176. .remove = dell_wmi_descriptor_remove,
  177. .id_table = dell_wmi_descriptor_id_table,
  178. };
  179. module_wmi_driver(dell_wmi_descriptor_driver);
  180. MODULE_ALIAS("wmi:" DELL_WMI_DESCRIPTOR_GUID);
  181. MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
  182. MODULE_DESCRIPTION("Dell WMI descriptor driver");
  183. MODULE_LICENSE("GPL");