wmi-bmof.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * WMI embedded Binary MOF driver
  3. *
  4. * Copyright (c) 2015 Andrew Lutomirski
  5. * Copyright (C) 2017 VMware, Inc. All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/acpi.h>
  18. #include <linux/device.h>
  19. #include <linux/fs.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/string.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/types.h>
  25. #include <linux/wmi.h>
  26. #define WMI_BMOF_GUID "05901221-D566-11D1-B2F0-00A0C9062910"
  27. struct bmof_priv {
  28. union acpi_object *bmofdata;
  29. struct bin_attribute bmof_bin_attr;
  30. };
  31. static ssize_t
  32. read_bmof(struct file *filp, struct kobject *kobj,
  33. struct bin_attribute *attr,
  34. char *buf, loff_t off, size_t count)
  35. {
  36. struct bmof_priv *priv =
  37. container_of(attr, struct bmof_priv, bmof_bin_attr);
  38. if (off < 0)
  39. return -EINVAL;
  40. if (off >= priv->bmofdata->buffer.length)
  41. return 0;
  42. if (count > priv->bmofdata->buffer.length - off)
  43. count = priv->bmofdata->buffer.length - off;
  44. memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
  45. return count;
  46. }
  47. static int wmi_bmof_probe(struct wmi_device *wdev)
  48. {
  49. struct bmof_priv *priv;
  50. int ret;
  51. priv = devm_kzalloc(&wdev->dev, sizeof(struct bmof_priv), GFP_KERNEL);
  52. if (!priv)
  53. return -ENOMEM;
  54. dev_set_drvdata(&wdev->dev, priv);
  55. priv->bmofdata = wmidev_block_query(wdev, 0);
  56. if (!priv->bmofdata) {
  57. dev_err(&wdev->dev, "failed to read Binary MOF\n");
  58. return -EIO;
  59. }
  60. if (priv->bmofdata->type != ACPI_TYPE_BUFFER) {
  61. dev_err(&wdev->dev, "Binary MOF is not a buffer\n");
  62. ret = -EIO;
  63. goto err_free;
  64. }
  65. sysfs_bin_attr_init(&priv->bmof_bin_attr);
  66. priv->bmof_bin_attr.attr.name = "bmof";
  67. priv->bmof_bin_attr.attr.mode = 0400;
  68. priv->bmof_bin_attr.read = read_bmof;
  69. priv->bmof_bin_attr.size = priv->bmofdata->buffer.length;
  70. ret = sysfs_create_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
  71. if (ret)
  72. goto err_free;
  73. return 0;
  74. err_free:
  75. kfree(priv->bmofdata);
  76. return ret;
  77. }
  78. static int wmi_bmof_remove(struct wmi_device *wdev)
  79. {
  80. struct bmof_priv *priv = dev_get_drvdata(&wdev->dev);
  81. sysfs_remove_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
  82. kfree(priv->bmofdata);
  83. return 0;
  84. }
  85. static const struct wmi_device_id wmi_bmof_id_table[] = {
  86. { .guid_string = WMI_BMOF_GUID },
  87. { },
  88. };
  89. static struct wmi_driver wmi_bmof_driver = {
  90. .driver = {
  91. .name = "wmi-bmof",
  92. },
  93. .probe = wmi_bmof_probe,
  94. .remove = wmi_bmof_remove,
  95. .id_table = wmi_bmof_id_table,
  96. };
  97. module_wmi_driver(wmi_bmof_driver);
  98. MODULE_ALIAS("wmi:" WMI_BMOF_GUID);
  99. MODULE_AUTHOR("Andrew Lutomirski <luto@kernel.org>");
  100. MODULE_DESCRIPTION("WMI embedded Binary MOF driver");
  101. MODULE_LICENSE("GPL");