1 // SPDX-License-Identifier: GPL-2.0-only
3 * Dell privacy notification driver
5 * Copyright (C) 2021 Dell Inc. All Rights Reserved.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
19 #include "dell-wmi-privacy.h"
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)
29 * The wmi_list is used to store the privacy_priv struct with mutex protecting
31 static LIST_HEAD(wmi_list);
32 static DEFINE_MUTEX(list_mutex);
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;
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,
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",
58 * Keymap for WMI privacy events of type 0x0012
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_SW, 0x0002, { SW_CAMERA_LENS_COVER } },
68 bool dell_privacy_has_mic_mute(void)
70 struct privacy_wmi_data *priv;
72 mutex_lock(&list_mutex);
73 priv = list_first_entry_or_null(&wmi_list,
74 struct privacy_wmi_data,
76 mutex_unlock(&list_mutex);
78 return priv && (priv->features_present & BIT(DELL_PRIVACY_TYPE_AUDIO));
80 EXPORT_SYMBOL_GPL(dell_privacy_has_mic_mute);
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.
95 bool dell_privacy_process_event(int type, int code, int status)
97 struct privacy_wmi_data *priv;
98 const struct key_entry *key;
101 mutex_lock(&list_mutex);
102 priv = list_first_entry_or_null(&wmi_list,
103 struct privacy_wmi_data,
108 key = sparse_keymap_entry_from_scancode(priv->input_dev, (type << 16) | code);
110 dev_warn(&priv->wdev->dev, "Unknown key with type 0x%04x and code 0x%04x pressed\n",
114 dev_dbg(&priv->wdev->dev, "Key with type 0x%04x and code 0x%04x pressed\n", type, code);
117 case DELL_PRIVACY_AUDIO_EVENT: /* Mic mute */
118 case DELL_PRIVACY_CAMERA_EVENT: /* Camera mute */
119 priv->last_status = status;
120 sparse_keymap_report_entry(priv->input_dev, key, 1, true);
124 dev_dbg(&priv->wdev->dev, "unknown event type 0x%04x 0x%04x\n", type, code);
128 mutex_unlock(&list_mutex);
132 static ssize_t dell_privacy_supported_type_show(struct device *dev,
133 struct device_attribute *attr,
136 struct privacy_wmi_data *priv = dev_get_drvdata(dev);
137 enum dell_hardware_privacy_type type;
141 privacy_list = priv->features_present;
142 for (type = DELL_PRIVACY_TYPE_AUDIO; type < DELL_PRIVACY_TYPE_MAX; type++) {
143 if (privacy_list & BIT(type))
144 len += sysfs_emit_at(buf, len, "[%s] [supported]\n", privacy_types[type]);
146 len += sysfs_emit_at(buf, len, "[%s] [unsupported]\n", privacy_types[type]);
152 static ssize_t dell_privacy_current_state_show(struct device *dev,
153 struct device_attribute *attr,
156 struct privacy_wmi_data *priv = dev_get_drvdata(dev);
157 u32 privacy_supported = priv->features_present;
158 enum dell_hardware_privacy_type type;
159 u32 privacy_state = priv->last_status;
162 for (type = DELL_PRIVACY_TYPE_AUDIO; type < DELL_PRIVACY_TYPE_MAX; type++) {
163 if (privacy_supported & BIT(type)) {
164 if (privacy_state & BIT(type))
165 len += sysfs_emit_at(buf, len, "[%s] [unmuted]\n", privacy_types[type]);
167 len += sysfs_emit_at(buf, len, "[%s] [muted]\n", privacy_types[type]);
174 static DEVICE_ATTR_RO(dell_privacy_supported_type);
175 static DEVICE_ATTR_RO(dell_privacy_current_state);
177 static struct attribute *privacy_attributes[] = {
178 &dev_attr_dell_privacy_supported_type.attr,
179 &dev_attr_dell_privacy_current_state.attr,
183 static const struct attribute_group privacy_attribute_group = {
184 .attrs = privacy_attributes
188 * Describes the Device State class exposed by BIOS which can be consumed by
189 * various applications interested in knowing the Privacy feature capabilities.
192 * [key, read] string InstanceName;
193 * [read] boolean ReadOnly;
195 * [WmiDataId(1), read] uint32 DevicesSupported;
196 * 0 - None; 0x1 - Microphone; 0x2 - Camera; 0x4 - ePrivacy Screen
198 * [WmiDataId(2), read] uint32 CurrentState;
199 * 0 - Off; 1 - On; Bit0 - Microphone; Bit1 - Camera; Bit2 - ePrivacyScreen
202 static int get_current_status(struct wmi_device *wdev)
204 struct privacy_wmi_data *priv = dev_get_drvdata(&wdev->dev);
205 union acpi_object *obj_present;
210 dev_err(&wdev->dev, "dell privacy priv is NULL\n");
213 /* check privacy support features and device states */
214 obj_present = wmidev_block_query(wdev, 0);
216 dev_err(&wdev->dev, "failed to read Binary MOF\n");
220 if (obj_present->type != ACPI_TYPE_BUFFER) {
221 dev_err(&wdev->dev, "Binary MOF is not a buffer!\n");
225 /* Although it's not technically a failure, this would lead to
226 * unexpected behavior
228 if (obj_present->buffer.length != 8) {
229 dev_err(&wdev->dev, "Dell privacy buffer has unexpected length (%d)!\n",
230 obj_present->buffer.length);
234 buffer = (u32 *)obj_present->buffer.pointer;
235 priv->features_present = buffer[0];
236 priv->last_status = buffer[1];
243 static int dell_privacy_micmute_led_set(struct led_classdev *led_cdev,
244 enum led_brightness brightness)
246 struct privacy_wmi_data *priv = led_to_priv(led_cdev);
247 static char *acpi_method = (char *)"ECAK";
251 handle = ec_get_handle();
255 if (!acpi_has_method(handle, acpi_method))
258 status = acpi_evaluate_object(handle, acpi_method, NULL, NULL);
259 if (ACPI_FAILURE(status)) {
260 dev_err(&priv->wdev->dev, "Error setting privacy EC ack value: %s\n",
261 acpi_format_exception(status));
269 * Pressing the mute key activates a time delayed circuit to physically cut
270 * off the mute. The LED is in the same circuit, so it reflects the true
271 * state of the HW mute. The reason for the EC "ack" is so that software
272 * can first invoke a SW mute before the HW circuit is cut off. Without SW
273 * cutting this off first does not affect the time delayed muting or status
274 * of the LED but there is a possibility of a "popping" noise.
276 * If the EC receives the SW ack, the circuit will be activated before the
279 * Exposing as an LED device allows the codec drivers notification path to
282 static int dell_privacy_leds_setup(struct device *dev)
284 struct privacy_wmi_data *priv = dev_get_drvdata(dev);
286 priv->cdev.name = "dell-privacy::micmute";
287 priv->cdev.max_brightness = 1;
288 priv->cdev.brightness_set_blocking = dell_privacy_micmute_led_set;
289 priv->cdev.default_trigger = "audio-micmute";
290 priv->cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
291 return devm_led_classdev_register(dev, &priv->cdev);
294 static int dell_privacy_wmi_probe(struct wmi_device *wdev, const void *context)
296 struct privacy_wmi_data *priv;
297 struct key_entry *keymap;
300 ret = wmi_has_guid(DELL_PRIVACY_GUID);
302 pr_debug("Unable to detect available Dell privacy devices!\n");
304 priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
308 dev_set_drvdata(&wdev->dev, priv);
310 /* create evdev passing interface */
311 priv->input_dev = devm_input_allocate_device(&wdev->dev);
312 if (!priv->input_dev)
315 /* remap the wmi keymap event to new keymap */
316 keymap = kcalloc(ARRAY_SIZE(dell_wmi_keymap_type_0012),
317 sizeof(struct key_entry), GFP_KERNEL);
321 /* remap the keymap code with Dell privacy key type 0x12 as prefix
322 * KEY_MICMUTE scancode will be reported as 0x120001
324 for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0012); i++) {
325 keymap[i] = dell_wmi_keymap_type_0012[i];
326 keymap[i].code |= (0x0012 << 16);
328 ret = sparse_keymap_setup(priv->input_dev, keymap, NULL);
333 priv->input_dev->dev.parent = &wdev->dev;
334 priv->input_dev->name = "Dell Privacy Driver";
335 priv->input_dev->id.bustype = BUS_HOST;
337 ret = input_register_device(priv->input_dev);
341 ret = get_current_status(priv->wdev);
345 ret = devm_device_add_group(&wdev->dev, &privacy_attribute_group);
349 if (priv->features_present & BIT(DELL_PRIVACY_TYPE_AUDIO)) {
350 ret = dell_privacy_leds_setup(&priv->wdev->dev);
354 mutex_lock(&list_mutex);
355 list_add_tail(&priv->list, &wmi_list);
356 mutex_unlock(&list_mutex);
360 static void dell_privacy_wmi_remove(struct wmi_device *wdev)
362 struct privacy_wmi_data *priv = dev_get_drvdata(&wdev->dev);
364 mutex_lock(&list_mutex);
365 list_del(&priv->list);
366 mutex_unlock(&list_mutex);
369 static const struct wmi_device_id dell_wmi_privacy_wmi_id_table[] = {
370 { .guid_string = DELL_PRIVACY_GUID },
374 static struct wmi_driver dell_privacy_wmi_driver = {
376 .name = "dell-privacy",
378 .probe = dell_privacy_wmi_probe,
379 .remove = dell_privacy_wmi_remove,
380 .id_table = dell_wmi_privacy_wmi_id_table,
383 int dell_privacy_register_driver(void)
385 return wmi_driver_register(&dell_privacy_wmi_driver);
388 void dell_privacy_unregister_driver(void)
390 wmi_driver_unregister(&dell_privacy_wmi_driver);