dell-wmi-descriptor.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. struct descriptor_priv {
  22. struct list_head list;
  23. u32 interface_version;
  24. u32 size;
  25. };
  26. static LIST_HEAD(wmi_list);
  27. static DEFINE_MUTEX(list_mutex);
  28. bool dell_wmi_get_interface_version(u32 *version)
  29. {
  30. struct descriptor_priv *priv;
  31. bool ret = false;
  32. mutex_lock(&list_mutex);
  33. priv = list_first_entry_or_null(&wmi_list,
  34. struct descriptor_priv,
  35. list);
  36. if (priv) {
  37. *version = priv->interface_version;
  38. ret = true;
  39. }
  40. mutex_unlock(&list_mutex);
  41. return ret;
  42. }
  43. EXPORT_SYMBOL_GPL(dell_wmi_get_interface_version);
  44. bool dell_wmi_get_size(u32 *size)
  45. {
  46. struct descriptor_priv *priv;
  47. bool ret = false;
  48. mutex_lock(&list_mutex);
  49. priv = list_first_entry_or_null(&wmi_list,
  50. struct descriptor_priv,
  51. list);
  52. if (priv) {
  53. *size = priv->size;
  54. ret = true;
  55. }
  56. mutex_unlock(&list_mutex);
  57. return ret;
  58. }
  59. EXPORT_SYMBOL_GPL(dell_wmi_get_size);
  60. /*
  61. * Descriptor buffer is 128 byte long and contains:
  62. *
  63. * Name Offset Length Value
  64. * Vendor Signature 0 4 "DELL"
  65. * Object Signature 4 4 " WMI"
  66. * WMI Interface Version 8 4 <version>
  67. * WMI buffer length 12 4 <length>
  68. */
  69. static int dell_wmi_descriptor_probe(struct wmi_device *wdev)
  70. {
  71. union acpi_object *obj = NULL;
  72. struct descriptor_priv *priv;
  73. u32 *buffer;
  74. int ret;
  75. obj = wmidev_block_query(wdev, 0);
  76. if (!obj) {
  77. dev_err(&wdev->dev, "failed to read Dell WMI descriptor\n");
  78. ret = -EIO;
  79. goto out;
  80. }
  81. if (obj->type != ACPI_TYPE_BUFFER) {
  82. dev_err(&wdev->dev, "Dell descriptor has wrong type\n");
  83. ret = -EINVAL;
  84. goto out;
  85. }
  86. /* Although it's not technically a failure, this would lead to
  87. * unexpected behavior
  88. */
  89. if (obj->buffer.length != 128) {
  90. dev_err(&wdev->dev,
  91. "Dell descriptor buffer has unexpected length (%d)\n",
  92. obj->buffer.length);
  93. ret = -EINVAL;
  94. goto out;
  95. }
  96. buffer = (u32 *)obj->buffer.pointer;
  97. if (strncmp(obj->string.pointer, "DELL WMI", 8) != 0) {
  98. dev_err(&wdev->dev, "Dell descriptor buffer has invalid signature (%8ph)\n",
  99. buffer);
  100. ret = -EINVAL;
  101. goto out;
  102. }
  103. if (buffer[2] != 0 && buffer[2] != 1)
  104. dev_warn(&wdev->dev, "Dell descriptor buffer has unknown version (%lu)\n",
  105. (unsigned long) buffer[2]);
  106. priv = devm_kzalloc(&wdev->dev, sizeof(struct descriptor_priv),
  107. GFP_KERNEL);
  108. if (!priv) {
  109. ret = -ENOMEM;
  110. goto out;
  111. }
  112. priv->interface_version = buffer[2];
  113. priv->size = buffer[3];
  114. ret = 0;
  115. dev_set_drvdata(&wdev->dev, priv);
  116. mutex_lock(&list_mutex);
  117. list_add_tail(&priv->list, &wmi_list);
  118. mutex_unlock(&list_mutex);
  119. dev_dbg(&wdev->dev, "Detected Dell WMI interface version %lu and buffer size %lu\n",
  120. (unsigned long) priv->interface_version,
  121. (unsigned long) priv->size);
  122. out:
  123. kfree(obj);
  124. return ret;
  125. }
  126. static int dell_wmi_descriptor_remove(struct wmi_device *wdev)
  127. {
  128. struct descriptor_priv *priv = dev_get_drvdata(&wdev->dev);
  129. mutex_lock(&list_mutex);
  130. list_del(&priv->list);
  131. mutex_unlock(&list_mutex);
  132. return 0;
  133. }
  134. static const struct wmi_device_id dell_wmi_descriptor_id_table[] = {
  135. { .guid_string = DELL_WMI_DESCRIPTOR_GUID },
  136. { },
  137. };
  138. static struct wmi_driver dell_wmi_descriptor_driver = {
  139. .driver = {
  140. .name = "dell-wmi-descriptor",
  141. },
  142. .probe = dell_wmi_descriptor_probe,
  143. .remove = dell_wmi_descriptor_remove,
  144. .id_table = dell_wmi_descriptor_id_table,
  145. };
  146. module_wmi_driver(dell_wmi_descriptor_driver);
  147. MODULE_ALIAS("wmi:" DELL_WMI_DESCRIPTOR_GUID);
  148. MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
  149. MODULE_DESCRIPTION("Dell WMI descriptor driver");
  150. MODULE_LICENSE("GPL");