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