1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Dell privacy notification driver
4 *
5 * Copyright (C) 2021 Dell Inc. All Rights Reserved.
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/acpi.h>
11 #include <linux/bitops.h>
12 #include <linux/cleanup.h>
13 #include <linux/compiler_attributes.h>
14 #include <linux/input.h>
15 #include <linux/input/sparse-keymap.h>
16 #include <linux/list.h>
17 #include <linux/leds.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/wmi.h>
21
22 #include "dell-wmi-privacy.h"
23
24 #define DELL_PRIVACY_GUID "6932965F-1671-4CEB-B988-D3AB0A901919"
25 #define MICROPHONE_STATUS BIT(0)
26 #define CAMERA_STATUS BIT(1)
27 #define DELL_PRIVACY_AUDIO_EVENT 0x1
28 #define DELL_PRIVACY_CAMERA_EVENT 0x2
29 #define led_to_priv(c) container_of(c, struct privacy_wmi_data, cdev)
30
31 /*
32 * Describes the Device State class exposed by BIOS which can be consumed by
33 * various applications interested in knowing the Privacy feature capabilities.
34 * class DeviceState
35 * {
36 * [key, read] string InstanceName;
37 * [read] boolean ReadOnly;
38 *
39 * [WmiDataId(1), read] uint32 DevicesSupported;
40 * 0 - None; 0x1 - Microphone; 0x2 - Camera; 0x4 - ePrivacy Screen
41 *
42 * [WmiDataId(2), read] uint32 CurrentState;
43 * 0 - Off; 1 - On; Bit0 - Microphone; Bit1 - Camera; Bit2 - ePrivacyScreen
44 * };
45 */
46 struct device_state {
47 __le32 devices_supported;
48 __le32 current_state;
49 } __packed;
50
51 /*
52 * The wmi_list is used to store the privacy_priv struct with mutex protecting
53 */
54 static LIST_HEAD(wmi_list);
55 static DEFINE_MUTEX(list_mutex);
56
57 struct privacy_wmi_data {
58 struct input_dev *input_dev;
59 struct wmi_device *wdev;
60 struct list_head list;
61 struct led_classdev cdev;
62 u32 features_present;
63 u32 last_status;
64 };
65
66 /* DELL Privacy Type */
67 enum dell_hardware_privacy_type {
68 DELL_PRIVACY_TYPE_AUDIO = 0,
69 DELL_PRIVACY_TYPE_CAMERA,
70 DELL_PRIVACY_TYPE_SCREEN,
71 DELL_PRIVACY_TYPE_MAX,
72 };
73
74 static const char * const privacy_types[DELL_PRIVACY_TYPE_MAX] = {
75 [DELL_PRIVACY_TYPE_AUDIO] = "Microphone",
76 [DELL_PRIVACY_TYPE_CAMERA] = "Camera Shutter",
77 [DELL_PRIVACY_TYPE_SCREEN] = "ePrivacy Screen",
78 };
79
80 /*
81 * Keymap for WMI privacy events of type 0x0012
82 */
83 static const struct key_entry dell_wmi_keymap_type_0012[] = {
84 /* privacy mic mute */
85 { KE_KEY, 0x0001, { KEY_MICMUTE } },
86 /* privacy camera mute */
87 { KE_VSW, 0x0002, { SW_CAMERA_LENS_COVER } },
88 { KE_END, 0},
89 };
90
dell_privacy_has_mic_mute(void)91 bool dell_privacy_has_mic_mute(void)
92 {
93 struct privacy_wmi_data *priv;
94
95 guard(mutex)(&list_mutex);
96
97 priv = list_first_entry_or_null(&wmi_list,
98 struct privacy_wmi_data,
99 list);
100
101 return priv && (priv->features_present & BIT(DELL_PRIVACY_TYPE_AUDIO));
102 }
103 EXPORT_SYMBOL_GPL(dell_privacy_has_mic_mute);
104
105 /*
106 * The flow of privacy event:
107 * 1) User presses key. HW does stuff with this key (timeout is started)
108 * 2) WMI event is emitted from BIOS
109 * 3) WMI event is received by dell-privacy
110 * 4) KEY_MICMUTE emitted from dell-privacy
111 * 5) Userland picks up key and modifies kcontrol for SW mute
112 * 6) Codec kernel driver catches and calls ledtrig_audio_set which will call
113 * led_set_brightness() on the LED registered by dell_privacy_leds_setup()
114 * 7) dell-privacy notifies EC, the timeout is cancelled and the HW mute activates.
115 * If the EC is not notified then the HW mic mute will activate when the timeout
116 * triggers, just a bit later than with the active ack.
117 */
dell_privacy_process_event(int type,int code,int status)118 bool dell_privacy_process_event(int type, int code, int status)
119 {
120 struct privacy_wmi_data *priv;
121 const struct key_entry *key;
122 bool ret = false;
123
124 mutex_lock(&list_mutex);
125 priv = list_first_entry_or_null(&wmi_list,
126 struct privacy_wmi_data,
127 list);
128 if (!priv)
129 goto error;
130
131 key = sparse_keymap_entry_from_scancode(priv->input_dev, (type << 16) | code);
132 if (!key) {
133 dev_warn(&priv->wdev->dev, "Unknown key with type 0x%04x and code 0x%04x pressed\n",
134 type, code);
135 goto error;
136 }
137 dev_dbg(&priv->wdev->dev, "Key with type 0x%04x and code 0x%04x pressed\n", type, code);
138
139 switch (code) {
140 case DELL_PRIVACY_AUDIO_EVENT: /* Mic mute */
141 priv->last_status = status;
142 sparse_keymap_report_entry(priv->input_dev, key, 1, true);
143 ret = true;
144 break;
145 case DELL_PRIVACY_CAMERA_EVENT: /* Camera mute */
146 priv->last_status = status;
147 sparse_keymap_report_entry(priv->input_dev, key, !(status & CAMERA_STATUS), false);
148 ret = true;
149 break;
150 default:
151 dev_dbg(&priv->wdev->dev, "unknown event type 0x%04x 0x%04x\n", type, code);
152 }
153
154 error:
155 mutex_unlock(&list_mutex);
156 return ret;
157 }
158
dell_privacy_supported_type_show(struct device * dev,struct device_attribute * attr,char * buf)159 static ssize_t dell_privacy_supported_type_show(struct device *dev,
160 struct device_attribute *attr,
161 char *buf)
162 {
163 struct privacy_wmi_data *priv = dev_get_drvdata(dev);
164 enum dell_hardware_privacy_type type;
165 u32 privacy_list;
166 int len = 0;
167
168 privacy_list = priv->features_present;
169 for (type = DELL_PRIVACY_TYPE_AUDIO; type < DELL_PRIVACY_TYPE_MAX; type++) {
170 if (privacy_list & BIT(type))
171 len += sysfs_emit_at(buf, len, "[%s] [supported]\n", privacy_types[type]);
172 else
173 len += sysfs_emit_at(buf, len, "[%s] [unsupported]\n", privacy_types[type]);
174 }
175
176 return len;
177 }
178
dell_privacy_current_state_show(struct device * dev,struct device_attribute * attr,char * buf)179 static ssize_t dell_privacy_current_state_show(struct device *dev,
180 struct device_attribute *attr,
181 char *buf)
182 {
183 struct privacy_wmi_data *priv = dev_get_drvdata(dev);
184 u32 privacy_supported = priv->features_present;
185 enum dell_hardware_privacy_type type;
186 u32 privacy_state = priv->last_status;
187 int len = 0;
188
189 for (type = DELL_PRIVACY_TYPE_AUDIO; type < DELL_PRIVACY_TYPE_MAX; type++) {
190 if (privacy_supported & BIT(type)) {
191 if (privacy_state & BIT(type))
192 len += sysfs_emit_at(buf, len, "[%s] [unmuted]\n", privacy_types[type]);
193 else
194 len += sysfs_emit_at(buf, len, "[%s] [muted]\n", privacy_types[type]);
195 }
196 }
197
198 return len;
199 }
200
201 static DEVICE_ATTR_RO(dell_privacy_supported_type);
202 static DEVICE_ATTR_RO(dell_privacy_current_state);
203
204 static struct attribute *privacy_attrs[] = {
205 &dev_attr_dell_privacy_supported_type.attr,
206 &dev_attr_dell_privacy_current_state.attr,
207 NULL,
208 };
209 ATTRIBUTE_GROUPS(privacy);
210
get_current_status(struct wmi_device * wdev)211 static int get_current_status(struct wmi_device *wdev)
212 {
213 struct privacy_wmi_data *priv = dev_get_drvdata(&wdev->dev);
214 struct wmi_buffer buffer;
215 int ret;
216
217 if (!priv) {
218 dev_err(&wdev->dev, "dell privacy priv is NULL\n");
219 return -EINVAL;
220 }
221
222 /* check privacy support features and device states */
223 ret = wmidev_query_block(wdev, 0, &buffer, sizeof(struct device_state));
224 if (ret < 0)
225 return ret;
226
227 struct device_state *state __free(kfree) = buffer.data;
228
229 priv->features_present = le32_to_cpu(state->devices_supported);
230 priv->last_status = le32_to_cpu(state->current_state);
231
232 return 0;
233 }
234
dell_privacy_micmute_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)235 static int dell_privacy_micmute_led_set(struct led_classdev *led_cdev,
236 enum led_brightness brightness)
237 {
238 struct privacy_wmi_data *priv = led_to_priv(led_cdev);
239 static char *acpi_method = (char *)"ECAK";
240 acpi_status status;
241 acpi_handle handle;
242
243 handle = ec_get_handle();
244 if (!handle)
245 return -EIO;
246
247 if (!acpi_has_method(handle, acpi_method))
248 return -EIO;
249
250 status = acpi_evaluate_object(handle, acpi_method, NULL, NULL);
251 if (ACPI_FAILURE(status)) {
252 dev_err(&priv->wdev->dev, "Error setting privacy EC ack value: %s\n",
253 acpi_format_exception(status));
254 return -EIO;
255 }
256
257 return 0;
258 }
259
260 /*
261 * Pressing the mute key activates a time delayed circuit to physically cut
262 * off the mute. The LED is in the same circuit, so it reflects the true
263 * state of the HW mute. The reason for the EC "ack" is so that software
264 * can first invoke a SW mute before the HW circuit is cut off. Without SW
265 * cutting this off first does not affect the time delayed muting or status
266 * of the LED but there is a possibility of a "popping" noise.
267 *
268 * If the EC receives the SW ack, the circuit will be activated before the
269 * delay completed.
270 *
271 * Exposing as an LED device allows the codec drivers notification path to
272 * EC ACK to work
273 */
dell_privacy_leds_setup(struct device * dev)274 static int dell_privacy_leds_setup(struct device *dev)
275 {
276 struct privacy_wmi_data *priv = dev_get_drvdata(dev);
277
278 priv->cdev.name = "dell-privacy::micmute";
279 priv->cdev.max_brightness = 1;
280 priv->cdev.brightness_set_blocking = dell_privacy_micmute_led_set;
281 priv->cdev.default_trigger = "audio-micmute";
282 return devm_led_classdev_register(dev, &priv->cdev);
283 }
284
dell_privacy_wmi_probe(struct wmi_device * wdev,const void * context)285 static int dell_privacy_wmi_probe(struct wmi_device *wdev, const void *context)
286 {
287 struct privacy_wmi_data *priv;
288 struct key_entry *keymap;
289 int ret, i, j;
290
291 priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
292 if (!priv)
293 return -ENOMEM;
294
295 dev_set_drvdata(&wdev->dev, priv);
296 priv->wdev = wdev;
297
298 ret = get_current_status(priv->wdev);
299 if (ret)
300 return ret;
301
302 /* create evdev passing interface */
303 priv->input_dev = devm_input_allocate_device(&wdev->dev);
304 if (!priv->input_dev)
305 return -ENOMEM;
306
307 /* remap the wmi keymap event to new keymap */
308 keymap = kzalloc_objs(struct key_entry,
309 ARRAY_SIZE(dell_wmi_keymap_type_0012));
310 if (!keymap)
311 return -ENOMEM;
312
313 /* remap the keymap code with Dell privacy key type 0x12 as prefix
314 * KEY_MICMUTE scancode will be reported as 0x120001
315 */
316 for (i = 0, j = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0012); i++) {
317 /*
318 * Unlike keys where only presses matter, userspace may act
319 * on switches in both of their positions. Only register
320 * SW_CAMERA_LENS_COVER if it is actually there.
321 */
322 if (dell_wmi_keymap_type_0012[i].type == KE_VSW &&
323 dell_wmi_keymap_type_0012[i].sw.code == SW_CAMERA_LENS_COVER &&
324 !(priv->features_present & BIT(DELL_PRIVACY_TYPE_CAMERA)))
325 continue;
326
327 keymap[j] = dell_wmi_keymap_type_0012[i];
328 keymap[j].code |= (0x0012 << 16);
329 j++;
330 }
331 ret = sparse_keymap_setup(priv->input_dev, keymap, NULL);
332 kfree(keymap);
333 if (ret)
334 return ret;
335
336 priv->input_dev->dev.parent = &wdev->dev;
337 priv->input_dev->name = "Dell Privacy Driver";
338 priv->input_dev->id.bustype = BUS_HOST;
339
340 /* Report initial camera-cover status */
341 if (priv->features_present & BIT(DELL_PRIVACY_TYPE_CAMERA))
342 input_report_switch(priv->input_dev, SW_CAMERA_LENS_COVER,
343 !(priv->last_status & CAMERA_STATUS));
344
345 ret = input_register_device(priv->input_dev);
346 if (ret)
347 return ret;
348
349 if (priv->features_present & BIT(DELL_PRIVACY_TYPE_AUDIO)) {
350 ret = dell_privacy_leds_setup(&priv->wdev->dev);
351 if (ret)
352 return ret;
353 }
354 mutex_lock(&list_mutex);
355 list_add_tail(&priv->list, &wmi_list);
356 mutex_unlock(&list_mutex);
357 return 0;
358 }
359
dell_privacy_wmi_remove(struct wmi_device * wdev)360 static void dell_privacy_wmi_remove(struct wmi_device *wdev)
361 {
362 struct privacy_wmi_data *priv = dev_get_drvdata(&wdev->dev);
363
364 mutex_lock(&list_mutex);
365 list_del(&priv->list);
366 mutex_unlock(&list_mutex);
367 }
368
369 static const struct wmi_device_id dell_wmi_privacy_wmi_id_table[] = {
370 { .guid_string = DELL_PRIVACY_GUID },
371 { },
372 };
373
374 static struct wmi_driver dell_privacy_wmi_driver = {
375 .driver = {
376 .name = "dell-privacy",
377 .dev_groups = privacy_groups,
378 },
379 .probe = dell_privacy_wmi_probe,
380 .remove = dell_privacy_wmi_remove,
381 .id_table = dell_wmi_privacy_wmi_id_table,
382 };
383
dell_privacy_register_driver(void)384 int dell_privacy_register_driver(void)
385 {
386 return wmi_driver_register(&dell_privacy_wmi_driver);
387 }
388
dell_privacy_unregister_driver(void)389 void dell_privacy_unregister_driver(void)
390 {
391 wmi_driver_unregister(&dell_privacy_wmi_driver);
392 }
393