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