wmi-bmof.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * WMI embedded Binary MOF driver
  4. *
  5. * Copyright (c) 2015 Andrew Lutomirski
  6. * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/acpi.h>
  10. #include <linux/device.h>
  11. #include <linux/fs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/types.h>
  17. #include <linux/wmi.h>
  18. #define WMI_BMOF_GUID "05901221-D566-11D1-B2F0-00A0C9062910"
  19. struct bmof_priv {
  20. union acpi_object *bmofdata;
  21. struct bin_attribute bmof_bin_attr;
  22. };
  23. static ssize_t
  24. read_bmof(struct file *filp, struct kobject *kobj,
  25. struct bin_attribute *attr,
  26. char *buf, loff_t off, size_t count)
  27. {
  28. struct bmof_priv *priv =
  29. container_of(attr, struct bmof_priv, bmof_bin_attr);
  30. if (off < 0)
  31. return -EINVAL;
  32. if (off >= priv->bmofdata->buffer.length)
  33. return 0;
  34. if (count > priv->bmofdata->buffer.length - off)
  35. count = priv->bmofdata->buffer.length - off;
  36. memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
  37. return count;
  38. }
  39. static int wmi_bmof_probe(struct wmi_device *wdev, const void *context)
  40. {
  41. struct bmof_priv *priv;
  42. int ret;
  43. priv = devm_kzalloc(&wdev->dev, sizeof(struct bmof_priv), GFP_KERNEL);
  44. if (!priv)
  45. return -ENOMEM;
  46. dev_set_drvdata(&wdev->dev, priv);
  47. priv->bmofdata = wmidev_block_query(wdev, 0);
  48. if (!priv->bmofdata) {
  49. dev_err(&wdev->dev, "failed to read Binary MOF\n");
  50. return -EIO;
  51. }
  52. if (priv->bmofdata->type != ACPI_TYPE_BUFFER) {
  53. dev_err(&wdev->dev, "Binary MOF is not a buffer\n");
  54. ret = -EIO;
  55. goto err_free;
  56. }
  57. sysfs_bin_attr_init(&priv->bmof_bin_attr);
  58. priv->bmof_bin_attr.attr.name = "bmof";
  59. priv->bmof_bin_attr.attr.mode = 0400;
  60. priv->bmof_bin_attr.read = read_bmof;
  61. priv->bmof_bin_attr.size = priv->bmofdata->buffer.length;
  62. ret = sysfs_create_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
  63. if (ret)
  64. goto err_free;
  65. return 0;
  66. err_free:
  67. kfree(priv->bmofdata);
  68. return ret;
  69. }
  70. static void wmi_bmof_remove(struct wmi_device *wdev)
  71. {
  72. struct bmof_priv *priv = dev_get_drvdata(&wdev->dev);
  73. sysfs_remove_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
  74. kfree(priv->bmofdata);
  75. }
  76. static const struct wmi_device_id wmi_bmof_id_table[] = {
  77. { .guid_string = WMI_BMOF_GUID },
  78. { },
  79. };
  80. static struct wmi_driver wmi_bmof_driver = {
  81. .driver = {
  82. .name = "wmi-bmof",
  83. },
  84. .probe = wmi_bmof_probe,
  85. .remove = wmi_bmof_remove,
  86. .id_table = wmi_bmof_id_table,
  87. };
  88. module_wmi_driver(wmi_bmof_driver);
  89. MODULE_DEVICE_TABLE(wmi, wmi_bmof_id_table);
  90. MODULE_AUTHOR("Andrew Lutomirski <[email protected]>");
  91. MODULE_DESCRIPTION("WMI embedded Binary MOF driver");
  92. MODULE_LICENSE("GPL");