1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16
17#include <linux/acpi.h>
18#include <linux/dmi.h>
19#include <linux/input.h>
20#include <linux/mutex.h>
21#include <linux/platform_device.h>
22#include <linux/spi/spi.h>
23
24MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@redhat.com>");
25MODULE_DESCRIPTION("Surface 3 platform driver");
26MODULE_LICENSE("GPL");
27
28#define ACPI_BUTTON_HID_LID "PNP0C0D"
29#define SPI_CTL_OBJ_NAME "SPI"
30#define SPI_TS_OBJ_NAME "NTRG"
31
32#define SURFACE3_LID_GUID "F7CC25EC-D20B-404C-8903-0ED4359C18AE"
33
34MODULE_ALIAS("wmi:" SURFACE3_LID_GUID);
35
36static const struct dmi_system_id surface3_dmi_table[] = {
37#if defined(CONFIG_X86)
38 {
39 .matches = {
40 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
41 DMI_MATCH(DMI_PRODUCT_NAME, "Surface 3"),
42 },
43 },
44#endif
45 { }
46};
47
48struct surface3_wmi {
49 struct acpi_device *touchscreen_adev;
50 struct acpi_device *pnp0c0d_adev;
51 struct acpi_hotplug_context hp;
52 struct input_dev *input;
53};
54
55static struct platform_device *s3_wmi_pdev;
56
57static struct surface3_wmi s3_wmi;
58
59static DEFINE_MUTEX(s3_wmi_lock);
60
61static int s3_wmi_query_block(const char *guid, int instance, int *ret)
62{
63 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
64 acpi_status status;
65 union acpi_object *obj;
66 int error = 0;
67
68 mutex_lock(&s3_wmi_lock);
69 status = wmi_query_block(guid, instance, &output);
70
71 obj = output.pointer;
72
73 if (!obj || obj->type != ACPI_TYPE_INTEGER) {
74 if (obj) {
75 pr_err("query block returned object type: %d - buffer length:%d\n",
76 obj->type,
77 obj->type == ACPI_TYPE_BUFFER ?
78 obj->buffer.length : 0);
79 }
80 error = -EINVAL;
81 goto out_free_unlock;
82 }
83 *ret = obj->integer.value;
84 out_free_unlock:
85 kfree(obj);
86 mutex_unlock(&s3_wmi_lock);
87 return error;
88}
89
90static inline int s3_wmi_query_lid(int *ret)
91{
92 return s3_wmi_query_block(SURFACE3_LID_GUID, 0, ret);
93}
94
95static int s3_wmi_send_lid_state(void)
96{
97 int ret, lid_sw;
98
99 ret = s3_wmi_query_lid(&lid_sw);
100 if (ret)
101 return ret;
102
103 input_report_switch(s3_wmi.input, SW_LID, lid_sw);
104 input_sync(s3_wmi.input);
105
106 return 0;
107}
108
109static int s3_wmi_hp_notify(struct acpi_device *adev, u32 value)
110{
111 return s3_wmi_send_lid_state();
112}
113
114static acpi_status s3_wmi_attach_spi_device(acpi_handle handle,
115 u32 level,
116 void *data,
117 void **return_value)
118{
119 struct acpi_device *adev, **ts_adev;
120
121 if (acpi_bus_get_device(handle, &adev))
122 return AE_OK;
123
124 ts_adev = data;
125
126 if (strncmp(acpi_device_bid(adev), SPI_TS_OBJ_NAME,
127 strlen(SPI_TS_OBJ_NAME)))
128 return AE_OK;
129
130 if (*ts_adev) {
131 pr_err("duplicate entry %s\n", SPI_TS_OBJ_NAME);
132 return AE_OK;
133 }
134
135 *ts_adev = adev;
136
137 return AE_OK;
138}
139
140static int s3_wmi_check_platform_device(struct device *dev, void *data)
141{
142 struct acpi_device *adev, *ts_adev = NULL;
143 acpi_handle handle;
144 acpi_status status;
145
146
147 handle = ACPI_HANDLE(dev);
148 if (!handle || acpi_bus_get_device(handle, &adev))
149 return 0;
150
151
152 if (!strcmp(ACPI_BUTTON_HID_LID, acpi_device_hid(adev))) {
153 s3_wmi.pnp0c0d_adev = adev;
154 return 0;
155 }
156
157
158 if (strncmp(acpi_device_bid(adev), SPI_CTL_OBJ_NAME,
159 strlen(SPI_CTL_OBJ_NAME)))
160 return 0;
161
162 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
163 s3_wmi_attach_spi_device, NULL,
164 &ts_adev, NULL);
165 if (ACPI_FAILURE(status))
166 dev_warn(dev, "failed to enumerate SPI slaves\n");
167
168 if (!ts_adev)
169 return 0;
170
171 s3_wmi.touchscreen_adev = ts_adev;
172
173 return 0;
174}
175
176static int s3_wmi_create_and_register_input(struct platform_device *pdev)
177{
178 struct input_dev *input;
179 int error;
180
181 input = devm_input_allocate_device(&pdev->dev);
182 if (!input)
183 return -ENOMEM;
184
185 input->name = "Lid Switch";
186 input->phys = "button/input0";
187 input->id.bustype = BUS_HOST;
188 input->id.product = 0x0005;
189
190 input_set_capability(input, EV_SW, SW_LID);
191
192 error = input_register_device(input);
193 if (error)
194 goto out_err;
195
196 s3_wmi.input = input;
197
198 return 0;
199 out_err:
200 input_free_device(s3_wmi.input);
201 return error;
202}
203
204static int __init s3_wmi_probe(struct platform_device *pdev)
205{
206 int error;
207
208 if (!dmi_check_system(surface3_dmi_table))
209 return -ENODEV;
210
211 memset(&s3_wmi, 0, sizeof(s3_wmi));
212
213 bus_for_each_dev(&platform_bus_type, NULL, NULL,
214 s3_wmi_check_platform_device);
215
216 if (!s3_wmi.touchscreen_adev)
217 return -ENODEV;
218
219 acpi_bus_trim(s3_wmi.pnp0c0d_adev);
220
221 error = s3_wmi_create_and_register_input(pdev);
222 if (error)
223 goto restore_acpi_lid;
224
225 acpi_initialize_hp_context(s3_wmi.touchscreen_adev, &s3_wmi.hp,
226 s3_wmi_hp_notify, NULL);
227
228 s3_wmi_send_lid_state();
229
230 return 0;
231
232 restore_acpi_lid:
233 acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle);
234 return error;
235}
236
237static int s3_wmi_remove(struct platform_device *device)
238{
239
240 s3_wmi.touchscreen_adev->hp = NULL;
241
242
243 acpi_bus_scan(s3_wmi.pnp0c0d_adev->handle);
244 return 0;
245}
246
247static int __maybe_unused s3_wmi_resume(struct device *dev)
248{
249 s3_wmi_send_lid_state();
250 return 0;
251}
252static SIMPLE_DEV_PM_OPS(s3_wmi_pm, NULL, s3_wmi_resume);
253
254static struct platform_driver s3_wmi_driver = {
255 .driver = {
256 .name = "surface3-wmi",
257 .pm = &s3_wmi_pm,
258 },
259 .remove = s3_wmi_remove,
260};
261
262static int __init s3_wmi_init(void)
263{
264 int error;
265
266 s3_wmi_pdev = platform_device_alloc("surface3-wmi", -1);
267 if (!s3_wmi_pdev)
268 return -ENOMEM;
269
270 error = platform_device_add(s3_wmi_pdev);
271 if (error)
272 goto err_device_put;
273
274 error = platform_driver_probe(&s3_wmi_driver, s3_wmi_probe);
275 if (error)
276 goto err_device_del;
277
278 pr_info("Surface 3 WMI Extras loaded\n");
279 return 0;
280
281 err_device_del:
282 platform_device_del(s3_wmi_pdev);
283 err_device_put:
284 platform_device_put(s3_wmi_pdev);
285 return error;
286}
287
288static void __exit s3_wmi_exit(void)
289{
290 platform_device_unregister(s3_wmi_pdev);
291 platform_driver_unregister(&s3_wmi_driver);
292}
293
294module_init(s3_wmi_init);
295module_exit(s3_wmi_exit);
296