dell-wmi-privacy.c 11 KB

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