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 
bmof_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t off,size_t count)23 static ssize_t bmof_read(struct file *filp, struct kobject *kobj, const struct bin_attribute *attr,
24 			 char *buf, loff_t off, size_t count)
25 {
26 	struct device *dev = kobj_to_dev(kobj);
27 	union acpi_object *obj = dev_get_drvdata(dev);
28 
29 	return memory_read_from_buffer(buf, count, &off, obj->buffer.pointer, obj->buffer.length);
30 }
31 
32 static const BIN_ATTR_ADMIN_RO(bmof, 0);
33 
34 static const struct bin_attribute * const bmof_attrs[] = {
35 	&bin_attr_bmof,
36 	NULL
37 };
38 
bmof_bin_size(struct kobject * kobj,const struct bin_attribute * attr,int n)39 static size_t bmof_bin_size(struct kobject *kobj, const struct bin_attribute *attr, int n)
40 {
41 	struct device *dev = kobj_to_dev(kobj);
42 	union acpi_object *obj = dev_get_drvdata(dev);
43 
44 	return obj->buffer.length;
45 }
46 
47 static const struct attribute_group bmof_group = {
48 	.bin_size = bmof_bin_size,
49 	.bin_attrs_new = bmof_attrs,
50 };
51 
52 static const struct attribute_group *bmof_groups[] = {
53 	&bmof_group,
54 	NULL
55 };
56 
wmi_bmof_probe(struct wmi_device * wdev,const void * context)57 static int wmi_bmof_probe(struct wmi_device *wdev, const void *context)
58 {
59 	union acpi_object *obj;
60 
61 	obj = wmidev_block_query(wdev, 0);
62 	if (!obj) {
63 		dev_err(&wdev->dev, "failed to read Binary MOF\n");
64 		return -EIO;
65 	}
66 
67 	if (obj->type != ACPI_TYPE_BUFFER) {
68 		dev_err(&wdev->dev, "Binary MOF is not a buffer\n");
69 		kfree(obj);
70 		return -EIO;
71 	}
72 
73 	dev_set_drvdata(&wdev->dev, obj);
74 
75 	return 0;
76 }
77 
wmi_bmof_remove(struct wmi_device * wdev)78 static void wmi_bmof_remove(struct wmi_device *wdev)
79 {
80 	union acpi_object *obj = dev_get_drvdata(&wdev->dev);
81 
82 	kfree(obj);
83 }
84 
85 static const struct wmi_device_id wmi_bmof_id_table[] = {
86 	{ .guid_string = WMI_BMOF_GUID },
87 	{ },
88 };
89 
90 static struct wmi_driver wmi_bmof_driver = {
91 	.driver = {
92 		.name = "wmi-bmof",
93 		.dev_groups = bmof_groups,
94 	},
95 	.probe = wmi_bmof_probe,
96 	.remove = wmi_bmof_remove,
97 	.id_table = wmi_bmof_id_table,
98 	.no_singleton = true,
99 };
100 
101 module_wmi_driver(wmi_bmof_driver);
102 
103 MODULE_DEVICE_TABLE(wmi, wmi_bmof_id_table);
104 MODULE_AUTHOR("Andrew Lutomirski <[email protected]>");
105 MODULE_DESCRIPTION("WMI embedded Binary MOF driver");
106 MODULE_LICENSE("GPL");
107