]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/platform/x86/dell-wmi-descriptor.c
mmc: core: prepend 0x to OCR entry in sysfs
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / dell-wmi-descriptor.c
1 /*
2 * Dell WMI descriptor driver
3 *
4 * Copyright (C) 2017 Dell Inc. All Rights Reserved.
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/list.h>
20 #include <linux/module.h>
21 #include <linux/wmi.h>
22 #include "dell-wmi-descriptor.h"
23
24 #define DELL_WMI_DESCRIPTOR_GUID "8D9DDCBC-A997-11DA-B012-B622A1EF5492"
25
26 struct descriptor_priv {
27 struct list_head list;
28 u32 interface_version;
29 u32 size;
30 };
31 static int descriptor_valid = -EPROBE_DEFER;
32 static LIST_HEAD(wmi_list);
33 static DEFINE_MUTEX(list_mutex);
34
35 int dell_wmi_get_descriptor_valid(void)
36 {
37 if (!wmi_has_guid(DELL_WMI_DESCRIPTOR_GUID))
38 return -ENODEV;
39
40 return descriptor_valid;
41 }
42 EXPORT_SYMBOL_GPL(dell_wmi_get_descriptor_valid);
43
44 bool dell_wmi_get_interface_version(u32 *version)
45 {
46 struct descriptor_priv *priv;
47 bool ret = false;
48
49 mutex_lock(&list_mutex);
50 priv = list_first_entry_or_null(&wmi_list,
51 struct descriptor_priv,
52 list);
53 if (priv) {
54 *version = priv->interface_version;
55 ret = true;
56 }
57 mutex_unlock(&list_mutex);
58 return ret;
59 }
60 EXPORT_SYMBOL_GPL(dell_wmi_get_interface_version);
61
62 bool dell_wmi_get_size(u32 *size)
63 {
64 struct descriptor_priv *priv;
65 bool ret = false;
66
67 mutex_lock(&list_mutex);
68 priv = list_first_entry_or_null(&wmi_list,
69 struct descriptor_priv,
70 list);
71 if (priv) {
72 *size = priv->size;
73 ret = true;
74 }
75 mutex_unlock(&list_mutex);
76 return ret;
77 }
78 EXPORT_SYMBOL_GPL(dell_wmi_get_size);
79
80 /*
81 * Descriptor buffer is 128 byte long and contains:
82 *
83 * Name Offset Length Value
84 * Vendor Signature 0 4 "DELL"
85 * Object Signature 4 4 " WMI"
86 * WMI Interface Version 8 4 <version>
87 * WMI buffer length 12 4 <length>
88 */
89 static int dell_wmi_descriptor_probe(struct wmi_device *wdev)
90 {
91 union acpi_object *obj = NULL;
92 struct descriptor_priv *priv;
93 u32 *buffer;
94 int ret;
95
96 obj = wmidev_block_query(wdev, 0);
97 if (!obj) {
98 dev_err(&wdev->dev, "failed to read Dell WMI descriptor\n");
99 ret = -EIO;
100 goto out;
101 }
102
103 if (obj->type != ACPI_TYPE_BUFFER) {
104 dev_err(&wdev->dev, "Dell descriptor has wrong type\n");
105 ret = -EINVAL;
106 descriptor_valid = ret;
107 goto out;
108 }
109
110 /* Although it's not technically a failure, this would lead to
111 * unexpected behavior
112 */
113 if (obj->buffer.length != 128) {
114 dev_err(&wdev->dev,
115 "Dell descriptor buffer has unexpected length (%d)\n",
116 obj->buffer.length);
117 ret = -EINVAL;
118 descriptor_valid = ret;
119 goto out;
120 }
121
122 buffer = (u32 *)obj->buffer.pointer;
123
124 if (strncmp(obj->string.pointer, "DELL WMI", 8) != 0) {
125 dev_err(&wdev->dev, "Dell descriptor buffer has invalid signature (%8ph)\n",
126 buffer);
127 ret = -EINVAL;
128 descriptor_valid = ret;
129 goto out;
130 }
131 descriptor_valid = 0;
132
133 if (buffer[2] != 0 && buffer[2] != 1)
134 dev_warn(&wdev->dev, "Dell descriptor buffer has unknown version (%lu)\n",
135 (unsigned long) buffer[2]);
136
137 priv = devm_kzalloc(&wdev->dev, sizeof(struct descriptor_priv),
138 GFP_KERNEL);
139
140 if (!priv) {
141 ret = -ENOMEM;
142 goto out;
143 }
144
145 priv->interface_version = buffer[2];
146 priv->size = buffer[3];
147 ret = 0;
148 dev_set_drvdata(&wdev->dev, priv);
149 mutex_lock(&list_mutex);
150 list_add_tail(&priv->list, &wmi_list);
151 mutex_unlock(&list_mutex);
152
153 dev_dbg(&wdev->dev, "Detected Dell WMI interface version %lu and buffer size %lu\n",
154 (unsigned long) priv->interface_version,
155 (unsigned long) priv->size);
156
157 out:
158 kfree(obj);
159 return ret;
160 }
161
162 static int dell_wmi_descriptor_remove(struct wmi_device *wdev)
163 {
164 struct descriptor_priv *priv = dev_get_drvdata(&wdev->dev);
165
166 mutex_lock(&list_mutex);
167 list_del(&priv->list);
168 mutex_unlock(&list_mutex);
169 return 0;
170 }
171
172 static const struct wmi_device_id dell_wmi_descriptor_id_table[] = {
173 { .guid_string = DELL_WMI_DESCRIPTOR_GUID },
174 { },
175 };
176
177 static struct wmi_driver dell_wmi_descriptor_driver = {
178 .driver = {
179 .name = "dell-wmi-descriptor",
180 },
181 .probe = dell_wmi_descriptor_probe,
182 .remove = dell_wmi_descriptor_remove,
183 .id_table = dell_wmi_descriptor_id_table,
184 };
185
186 module_wmi_driver(dell_wmi_descriptor_driver);
187
188 MODULE_ALIAS("wmi:" DELL_WMI_DESCRIPTOR_GUID);
189 MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
190 MODULE_DESCRIPTION("Dell WMI descriptor driver");
191 MODULE_LICENSE("GPL");