0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010
0011 #include <linux/acpi.h>
0012 #include <linux/device.h>
0013 #include <linux/fs.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/string.h>
0017 #include <linux/sysfs.h>
0018 #include <linux/types.h>
0019 #include <linux/wmi.h>
0020
0021 #define WMI_BMOF_GUID "05901221-D566-11D1-B2F0-00A0C9062910"
0022
0023 struct bmof_priv {
0024 union acpi_object *bmofdata;
0025 struct bin_attribute bmof_bin_attr;
0026 };
0027
0028 static ssize_t
0029 read_bmof(struct file *filp, struct kobject *kobj,
0030 struct bin_attribute *attr,
0031 char *buf, loff_t off, size_t count)
0032 {
0033 struct bmof_priv *priv =
0034 container_of(attr, struct bmof_priv, bmof_bin_attr);
0035
0036 if (off < 0)
0037 return -EINVAL;
0038
0039 if (off >= priv->bmofdata->buffer.length)
0040 return 0;
0041
0042 if (count > priv->bmofdata->buffer.length - off)
0043 count = priv->bmofdata->buffer.length - off;
0044
0045 memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
0046 return count;
0047 }
0048
0049 static int wmi_bmof_probe(struct wmi_device *wdev, const void *context)
0050 {
0051 struct bmof_priv *priv;
0052 int ret;
0053
0054 priv = devm_kzalloc(&wdev->dev, sizeof(struct bmof_priv), GFP_KERNEL);
0055 if (!priv)
0056 return -ENOMEM;
0057
0058 dev_set_drvdata(&wdev->dev, priv);
0059
0060 priv->bmofdata = wmidev_block_query(wdev, 0);
0061 if (!priv->bmofdata) {
0062 dev_err(&wdev->dev, "failed to read Binary MOF\n");
0063 return -EIO;
0064 }
0065
0066 if (priv->bmofdata->type != ACPI_TYPE_BUFFER) {
0067 dev_err(&wdev->dev, "Binary MOF is not a buffer\n");
0068 ret = -EIO;
0069 goto err_free;
0070 }
0071
0072 sysfs_bin_attr_init(&priv->bmof_bin_attr);
0073 priv->bmof_bin_attr.attr.name = "bmof";
0074 priv->bmof_bin_attr.attr.mode = 0400;
0075 priv->bmof_bin_attr.read = read_bmof;
0076 priv->bmof_bin_attr.size = priv->bmofdata->buffer.length;
0077
0078 ret = sysfs_create_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
0079 if (ret)
0080 goto err_free;
0081
0082 return 0;
0083
0084 err_free:
0085 kfree(priv->bmofdata);
0086 return ret;
0087 }
0088
0089 static void wmi_bmof_remove(struct wmi_device *wdev)
0090 {
0091 struct bmof_priv *priv = dev_get_drvdata(&wdev->dev);
0092
0093 sysfs_remove_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
0094 kfree(priv->bmofdata);
0095 }
0096
0097 static const struct wmi_device_id wmi_bmof_id_table[] = {
0098 { .guid_string = WMI_BMOF_GUID },
0099 { },
0100 };
0101
0102 static struct wmi_driver wmi_bmof_driver = {
0103 .driver = {
0104 .name = "wmi-bmof",
0105 },
0106 .probe = wmi_bmof_probe,
0107 .remove = wmi_bmof_remove,
0108 .id_table = wmi_bmof_id_table,
0109 };
0110
0111 module_wmi_driver(wmi_bmof_driver);
0112
0113 MODULE_DEVICE_TABLE(wmi, wmi_bmof_id_table);
0114 MODULE_AUTHOR("Andrew Lutomirski <luto@kernel.org>");
0115 MODULE_DESCRIPTION("WMI embedded Binary MOF driver");
0116 MODULE_LICENSE("GPL");