blob: 94922b9342cec8f76fdf5e0ff5e8c20bd4187d45 [file] [log] [blame]
Darren Hart (VMware)f9dd82c2017-06-06 10:07:32 -07001/*
2 * WMI embedded Binary MOF driver
3 *
4 * Copyright (c) 2015 Andrew Lutomirski
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
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include <linux/acpi.h>
19#include <linux/device.h>
20#include <linux/fs.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/string.h>
24#include <linux/sysfs.h>
25#include <linux/types.h>
26#include <linux/wmi.h>
27
28#define WMI_BMOF_GUID "05901221-D566-11D1-B2F0-00A0C9062910"
29
30struct bmof_priv {
31 union acpi_object *bmofdata;
32 struct bin_attribute bmof_bin_attr;
33};
34
35static ssize_t
36read_bmof(struct file *filp, struct kobject *kobj,
37 struct bin_attribute *attr,
38 char *buf, loff_t off, size_t count)
39{
40 struct bmof_priv *priv =
41 container_of(attr, struct bmof_priv, bmof_bin_attr);
42
43 if (off < 0)
44 return -EINVAL;
45
46 if (off >= priv->bmofdata->buffer.length)
47 return 0;
48
49 if (count > priv->bmofdata->buffer.length - off)
50 count = priv->bmofdata->buffer.length - off;
51
52 memcpy(buf, priv->bmofdata->buffer.pointer + off, count);
53 return count;
54}
55
56static int wmi_bmof_probe(struct wmi_device *wdev)
57{
58 struct bmof_priv *priv;
59 int ret;
60
61 priv = devm_kzalloc(&wdev->dev, sizeof(struct bmof_priv), GFP_KERNEL);
62 if (!priv)
63 return -ENOMEM;
64
65 dev_set_drvdata(&wdev->dev, priv);
66
67 priv->bmofdata = wmidev_block_query(wdev, 0);
68 if (!priv->bmofdata) {
69 dev_err(&wdev->dev, "failed to read Binary MOF\n");
70 return -EIO;
71 }
72
73 if (priv->bmofdata->type != ACPI_TYPE_BUFFER) {
74 dev_err(&wdev->dev, "Binary MOF is not a buffer\n");
75 ret = -EIO;
76 goto err_free;
77 }
78
79 sysfs_bin_attr_init(&priv->bmof_bin_attr);
80 priv->bmof_bin_attr.attr.name = "bmof";
81 priv->bmof_bin_attr.attr.mode = 0400;
82 priv->bmof_bin_attr.read = read_bmof;
83 priv->bmof_bin_attr.size = priv->bmofdata->buffer.length;
84
85 ret = sysfs_create_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
86 if (ret)
87 goto err_free;
88
89 return 0;
90
91 err_free:
92 kfree(priv->bmofdata);
93 return ret;
94}
95
96static int wmi_bmof_remove(struct wmi_device *wdev)
97{
98 struct bmof_priv *priv = dev_get_drvdata(&wdev->dev);
99
100 sysfs_remove_bin_file(&wdev->dev.kobj, &priv->bmof_bin_attr);
101 kfree(priv->bmofdata);
102 return 0;
103}
104
105static const struct wmi_device_id wmi_bmof_id_table[] = {
106 { .guid_string = WMI_BMOF_GUID },
107 { },
108};
109
110static struct wmi_driver wmi_bmof_driver = {
111 .driver = {
112 .name = "wmi-bmof",
113 },
114 .probe = wmi_bmof_probe,
115 .remove = wmi_bmof_remove,
116 .id_table = wmi_bmof_id_table,
117};
118
119module_wmi_driver(wmi_bmof_driver);
120
121MODULE_ALIAS("wmi:" WMI_BMOF_GUID);
122MODULE_AUTHOR("Andrew Lutomirski <luto@kernel.org>");
123MODULE_DESCRIPTION("WMI embedded Binary MOF driver");
124MODULE_LICENSE("GPL");