Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Dell privacy notification driver
0004  *
0005  * Copyright (C) 2021 Dell Inc. All Rights Reserved.
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/acpi.h>
0011 #include <linux/bitops.h>
0012 #include <linux/input.h>
0013 #include <linux/input/sparse-keymap.h>
0014 #include <linux/list.h>
0015 #include <linux/leds.h>
0016 #include <linux/module.h>
0017 #include <linux/wmi.h>
0018 
0019 #include "dell-wmi-privacy.h"
0020 
0021 #define DELL_PRIVACY_GUID "6932965F-1671-4CEB-B988-D3AB0A901919"
0022 #define MICROPHONE_STATUS       BIT(0)
0023 #define CAMERA_STATUS               BIT(1)
0024 #define DELL_PRIVACY_AUDIO_EVENT  0x1
0025 #define DELL_PRIVACY_CAMERA_EVENT 0x2
0026 #define led_to_priv(c)       container_of(c, struct privacy_wmi_data, cdev)
0027 
0028 /*
0029  * The wmi_list is used to store the privacy_priv struct with mutex protecting
0030  */
0031 static LIST_HEAD(wmi_list);
0032 static DEFINE_MUTEX(list_mutex);
0033 
0034 struct privacy_wmi_data {
0035     struct input_dev *input_dev;
0036     struct wmi_device *wdev;
0037     struct list_head list;
0038     struct led_classdev cdev;
0039     u32 features_present;
0040     u32 last_status;
0041 };
0042 
0043 /* DELL Privacy Type */
0044 enum dell_hardware_privacy_type {
0045     DELL_PRIVACY_TYPE_AUDIO = 0,
0046     DELL_PRIVACY_TYPE_CAMERA,
0047     DELL_PRIVACY_TYPE_SCREEN,
0048     DELL_PRIVACY_TYPE_MAX,
0049 };
0050 
0051 static const char * const privacy_types[DELL_PRIVACY_TYPE_MAX] = {
0052     [DELL_PRIVACY_TYPE_AUDIO] = "Microphone",
0053     [DELL_PRIVACY_TYPE_CAMERA] = "Camera Shutter",
0054     [DELL_PRIVACY_TYPE_SCREEN] = "ePrivacy Screen",
0055 };
0056 
0057 /*
0058  * Keymap for WMI privacy events of type 0x0012
0059  */
0060 static const struct key_entry dell_wmi_keymap_type_0012[] = {
0061     /* privacy mic mute */
0062     { KE_KEY, 0x0001, { KEY_MICMUTE } },
0063     /* privacy camera mute */
0064     { KE_SW,  0x0002, { SW_CAMERA_LENS_COVER } },
0065     { KE_END, 0},
0066 };
0067 
0068 bool dell_privacy_has_mic_mute(void)
0069 {
0070     struct privacy_wmi_data *priv;
0071 
0072     mutex_lock(&list_mutex);
0073     priv = list_first_entry_or_null(&wmi_list,
0074             struct privacy_wmi_data,
0075             list);
0076     mutex_unlock(&list_mutex);
0077 
0078     return priv && (priv->features_present & BIT(DELL_PRIVACY_TYPE_AUDIO));
0079 }
0080 EXPORT_SYMBOL_GPL(dell_privacy_has_mic_mute);
0081 
0082 /*
0083  * The flow of privacy event:
0084  * 1) User presses key. HW does stuff with this key (timeout is started)
0085  * 2) WMI event is emitted from BIOS
0086  * 3) WMI event is received by dell-privacy
0087  * 4) KEY_MICMUTE emitted from dell-privacy
0088  * 5) Userland picks up key and modifies kcontrol for SW mute
0089  * 6) Codec kernel driver catches and calls ledtrig_audio_set which will call
0090  *    led_set_brightness() on the LED registered by dell_privacy_leds_setup()
0091  * 7) dell-privacy notifies EC, the timeout is cancelled and the HW mute activates.
0092  *    If the EC is not notified then the HW mic mute will activate when the timeout
0093  *    triggers, just a bit later than with the active ack.
0094  */
0095 bool dell_privacy_process_event(int type, int code, int status)
0096 {
0097     struct privacy_wmi_data *priv;
0098     const struct key_entry *key;
0099     bool ret = false;
0100 
0101     mutex_lock(&list_mutex);
0102     priv = list_first_entry_or_null(&wmi_list,
0103             struct privacy_wmi_data,
0104             list);
0105     if (!priv)
0106         goto error;
0107 
0108     key = sparse_keymap_entry_from_scancode(priv->input_dev, (type << 16) | code);
0109     if (!key) {
0110         dev_warn(&priv->wdev->dev, "Unknown key with type 0x%04x and code 0x%04x pressed\n",
0111             type, code);
0112         goto error;
0113     }
0114     dev_dbg(&priv->wdev->dev, "Key with type 0x%04x and code 0x%04x pressed\n", type, code);
0115 
0116     switch (code) {
0117     case DELL_PRIVACY_AUDIO_EVENT: /* Mic mute */
0118     case DELL_PRIVACY_CAMERA_EVENT: /* Camera mute */
0119         priv->last_status = status;
0120         sparse_keymap_report_entry(priv->input_dev, key, 1, true);
0121         ret = true;
0122         break;
0123     default:
0124         dev_dbg(&priv->wdev->dev, "unknown event type 0x%04x 0x%04x\n", type, code);
0125     }
0126 
0127 error:
0128     mutex_unlock(&list_mutex);
0129     return ret;
0130 }
0131 
0132 static ssize_t dell_privacy_supported_type_show(struct device *dev,
0133                     struct device_attribute *attr,
0134                     char *buf)
0135 {
0136     struct privacy_wmi_data *priv = dev_get_drvdata(dev);
0137     enum dell_hardware_privacy_type type;
0138     u32 privacy_list;
0139     int len = 0;
0140 
0141     privacy_list = priv->features_present;
0142     for (type = DELL_PRIVACY_TYPE_AUDIO; type < DELL_PRIVACY_TYPE_MAX; type++) {
0143         if (privacy_list & BIT(type))
0144             len += sysfs_emit_at(buf, len, "[%s] [supported]\n", privacy_types[type]);
0145         else
0146             len += sysfs_emit_at(buf, len, "[%s] [unsupported]\n", privacy_types[type]);
0147     }
0148 
0149     return len;
0150 }
0151 
0152 static ssize_t dell_privacy_current_state_show(struct device *dev,
0153                     struct device_attribute *attr,
0154                     char *buf)
0155 {
0156     struct privacy_wmi_data *priv = dev_get_drvdata(dev);
0157     u32 privacy_supported = priv->features_present;
0158     enum dell_hardware_privacy_type type;
0159     u32 privacy_state = priv->last_status;
0160     int len = 0;
0161 
0162     for (type = DELL_PRIVACY_TYPE_AUDIO; type < DELL_PRIVACY_TYPE_MAX; type++) {
0163         if (privacy_supported & BIT(type)) {
0164             if (privacy_state & BIT(type))
0165                 len += sysfs_emit_at(buf, len, "[%s] [unmuted]\n", privacy_types[type]);
0166             else
0167                 len += sysfs_emit_at(buf, len, "[%s] [muted]\n", privacy_types[type]);
0168         }
0169     }
0170 
0171     return len;
0172 }
0173 
0174 static DEVICE_ATTR_RO(dell_privacy_supported_type);
0175 static DEVICE_ATTR_RO(dell_privacy_current_state);
0176 
0177 static struct attribute *privacy_attributes[] = {
0178     &dev_attr_dell_privacy_supported_type.attr,
0179     &dev_attr_dell_privacy_current_state.attr,
0180     NULL,
0181 };
0182 
0183 static const struct attribute_group privacy_attribute_group = {
0184     .attrs = privacy_attributes
0185 };
0186 
0187 /*
0188  * Describes the Device State class exposed by BIOS which can be consumed by
0189  * various applications interested in knowing the Privacy feature capabilities.
0190  * class DeviceState
0191  * {
0192  *  [key, read] string InstanceName;
0193  *  [read] boolean ReadOnly;
0194  *
0195  *  [WmiDataId(1), read] uint32 DevicesSupported;
0196  *   0 - None; 0x1 - Microphone; 0x2 - Camera; 0x4 - ePrivacy  Screen
0197  *
0198  *  [WmiDataId(2), read] uint32 CurrentState;
0199  *   0 - Off; 1 - On; Bit0 - Microphone; Bit1 - Camera; Bit2 - ePrivacyScreen
0200  * };
0201  */
0202 static int get_current_status(struct wmi_device *wdev)
0203 {
0204     struct privacy_wmi_data *priv = dev_get_drvdata(&wdev->dev);
0205     union acpi_object *obj_present;
0206     u32 *buffer;
0207     int ret = 0;
0208 
0209     if (!priv) {
0210         dev_err(&wdev->dev, "dell privacy priv is NULL\n");
0211         return -EINVAL;
0212     }
0213     /* check privacy support features and device states */
0214     obj_present = wmidev_block_query(wdev, 0);
0215     if (!obj_present) {
0216         dev_err(&wdev->dev, "failed to read Binary MOF\n");
0217         return -EIO;
0218     }
0219 
0220     if (obj_present->type != ACPI_TYPE_BUFFER) {
0221         dev_err(&wdev->dev, "Binary MOF is not a buffer!\n");
0222         ret = -EIO;
0223         goto obj_free;
0224     }
0225     /*  Although it's not technically a failure, this would lead to
0226      *  unexpected behavior
0227      */
0228     if (obj_present->buffer.length != 8) {
0229         dev_err(&wdev->dev, "Dell privacy buffer has unexpected length (%d)!\n",
0230                 obj_present->buffer.length);
0231         ret = -EINVAL;
0232         goto obj_free;
0233     }
0234     buffer = (u32 *)obj_present->buffer.pointer;
0235     priv->features_present = buffer[0];
0236     priv->last_status = buffer[1];
0237 
0238 obj_free:
0239     kfree(obj_present);
0240     return ret;
0241 }
0242 
0243 static int dell_privacy_micmute_led_set(struct led_classdev *led_cdev,
0244                     enum led_brightness brightness)
0245 {
0246     struct privacy_wmi_data *priv = led_to_priv(led_cdev);
0247     static char *acpi_method = (char *)"ECAK";
0248     acpi_status status;
0249     acpi_handle handle;
0250 
0251     handle = ec_get_handle();
0252     if (!handle)
0253         return -EIO;
0254 
0255     if (!acpi_has_method(handle, acpi_method))
0256         return -EIO;
0257 
0258     status = acpi_evaluate_object(handle, acpi_method, NULL, NULL);
0259     if (ACPI_FAILURE(status)) {
0260         dev_err(&priv->wdev->dev, "Error setting privacy EC ack value: %s\n",
0261                 acpi_format_exception(status));
0262         return -EIO;
0263     }
0264 
0265     return 0;
0266 }
0267 
0268 /*
0269  * Pressing the mute key activates a time delayed circuit to physically cut
0270  * off the mute. The LED is in the same circuit, so it reflects the true
0271  * state of the HW mute.  The reason for the EC "ack" is so that software
0272  * can first invoke a SW mute before the HW circuit is cut off.  Without SW
0273  * cutting this off first does not affect the time delayed muting or status
0274  * of the LED but there is a possibility of a "popping" noise.
0275  *
0276  * If the EC receives the SW ack, the circuit will be activated before the
0277  * delay completed.
0278  *
0279  * Exposing as an LED device allows the codec drivers notification path to
0280  * EC ACK to work
0281  */
0282 static int dell_privacy_leds_setup(struct device *dev)
0283 {
0284     struct privacy_wmi_data *priv = dev_get_drvdata(dev);
0285 
0286     priv->cdev.name = "dell-privacy::micmute";
0287     priv->cdev.max_brightness = 1;
0288     priv->cdev.brightness_set_blocking = dell_privacy_micmute_led_set;
0289     priv->cdev.default_trigger = "audio-micmute";
0290     priv->cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
0291     return devm_led_classdev_register(dev, &priv->cdev);
0292 }
0293 
0294 static int dell_privacy_wmi_probe(struct wmi_device *wdev, const void *context)
0295 {
0296     struct privacy_wmi_data *priv;
0297     struct key_entry *keymap;
0298     int ret, i;
0299 
0300     ret = wmi_has_guid(DELL_PRIVACY_GUID);
0301     if (!ret)
0302         pr_debug("Unable to detect available Dell privacy devices!\n");
0303 
0304     priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
0305     if (!priv)
0306         return -ENOMEM;
0307 
0308     dev_set_drvdata(&wdev->dev, priv);
0309     priv->wdev = wdev;
0310     /* create evdev passing interface */
0311     priv->input_dev = devm_input_allocate_device(&wdev->dev);
0312     if (!priv->input_dev)
0313         return -ENOMEM;
0314 
0315     /* remap the wmi keymap event to new keymap */
0316     keymap = kcalloc(ARRAY_SIZE(dell_wmi_keymap_type_0012),
0317             sizeof(struct key_entry), GFP_KERNEL);
0318     if (!keymap)
0319         return -ENOMEM;
0320 
0321     /* remap the keymap code with Dell privacy key type 0x12 as prefix
0322      * KEY_MICMUTE scancode will be reported as 0x120001
0323      */
0324     for (i = 0; i < ARRAY_SIZE(dell_wmi_keymap_type_0012); i++) {
0325         keymap[i] = dell_wmi_keymap_type_0012[i];
0326         keymap[i].code |= (0x0012 << 16);
0327     }
0328     ret = sparse_keymap_setup(priv->input_dev, keymap, NULL);
0329     kfree(keymap);
0330     if (ret)
0331         return ret;
0332 
0333     priv->input_dev->dev.parent = &wdev->dev;
0334     priv->input_dev->name = "Dell Privacy Driver";
0335     priv->input_dev->id.bustype = BUS_HOST;
0336 
0337     ret = input_register_device(priv->input_dev);
0338     if (ret)
0339         return ret;
0340 
0341     ret = get_current_status(priv->wdev);
0342     if (ret)
0343         return ret;
0344 
0345     ret = devm_device_add_group(&wdev->dev, &privacy_attribute_group);
0346     if (ret)
0347         return ret;
0348 
0349     if (priv->features_present & BIT(DELL_PRIVACY_TYPE_AUDIO)) {
0350         ret = dell_privacy_leds_setup(&priv->wdev->dev);
0351         if (ret)
0352             return ret;
0353     }
0354     mutex_lock(&list_mutex);
0355     list_add_tail(&priv->list, &wmi_list);
0356     mutex_unlock(&list_mutex);
0357     return 0;
0358 }
0359 
0360 static void dell_privacy_wmi_remove(struct wmi_device *wdev)
0361 {
0362     struct privacy_wmi_data *priv = dev_get_drvdata(&wdev->dev);
0363 
0364     mutex_lock(&list_mutex);
0365     list_del(&priv->list);
0366     mutex_unlock(&list_mutex);
0367 }
0368 
0369 static const struct wmi_device_id dell_wmi_privacy_wmi_id_table[] = {
0370     { .guid_string = DELL_PRIVACY_GUID },
0371     { },
0372 };
0373 
0374 static struct wmi_driver dell_privacy_wmi_driver = {
0375     .driver = {
0376         .name = "dell-privacy",
0377     },
0378     .probe = dell_privacy_wmi_probe,
0379     .remove = dell_privacy_wmi_remove,
0380     .id_table = dell_wmi_privacy_wmi_id_table,
0381 };
0382 
0383 int dell_privacy_register_driver(void)
0384 {
0385     return wmi_driver_register(&dell_privacy_wmi_driver);
0386 }
0387 
0388 void dell_privacy_unregister_driver(void)
0389 {
0390     wmi_driver_unregister(&dell_privacy_wmi_driver);
0391 }