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