drm_privacy_screen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright (C) 2020 - 2021 Red Hat, Inc.
  4. *
  5. * Authors:
  6. * Hans de Goede <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/list.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. #include <drm/drm_privacy_screen_machine.h>
  15. #include <drm/drm_privacy_screen_consumer.h>
  16. #include <drm/drm_privacy_screen_driver.h>
  17. #include "drm_internal.h"
  18. /**
  19. * DOC: overview
  20. *
  21. * This class allows non KMS drivers, from e.g. drivers/platform/x86 to
  22. * register a privacy-screen device, which the KMS drivers can then use
  23. * to implement the standard privacy-screen properties, see
  24. * :ref:`Standard Connector Properties<standard_connector_properties>`.
  25. *
  26. * KMS drivers using a privacy-screen class device are advised to use the
  27. * drm_connector_attach_privacy_screen_provider() and
  28. * drm_connector_update_privacy_screen() helpers for dealing with this.
  29. */
  30. #define to_drm_privacy_screen(dev) \
  31. container_of(dev, struct drm_privacy_screen, dev)
  32. static DEFINE_MUTEX(drm_privacy_screen_lookup_lock);
  33. static LIST_HEAD(drm_privacy_screen_lookup_list);
  34. static DEFINE_MUTEX(drm_privacy_screen_devs_lock);
  35. static LIST_HEAD(drm_privacy_screen_devs);
  36. /*** drm_privacy_screen_machine.h functions ***/
  37. /**
  38. * drm_privacy_screen_lookup_add - add an entry to the static privacy-screen
  39. * lookup list
  40. * @lookup: lookup list entry to add
  41. *
  42. * Add an entry to the static privacy-screen lookup list. Note the
  43. * &struct list_head which is part of the &struct drm_privacy_screen_lookup
  44. * gets added to a list owned by the privacy-screen core. So the passed in
  45. * &struct drm_privacy_screen_lookup must not be free-ed until it is removed
  46. * from the lookup list by calling drm_privacy_screen_lookup_remove().
  47. */
  48. void drm_privacy_screen_lookup_add(struct drm_privacy_screen_lookup *lookup)
  49. {
  50. mutex_lock(&drm_privacy_screen_lookup_lock);
  51. list_add(&lookup->list, &drm_privacy_screen_lookup_list);
  52. mutex_unlock(&drm_privacy_screen_lookup_lock);
  53. }
  54. EXPORT_SYMBOL(drm_privacy_screen_lookup_add);
  55. /**
  56. * drm_privacy_screen_lookup_remove - remove an entry to the static
  57. * privacy-screen lookup list
  58. * @lookup: lookup list entry to remove
  59. *
  60. * Remove an entry previously added with drm_privacy_screen_lookup_add()
  61. * from the static privacy-screen lookup list.
  62. */
  63. void drm_privacy_screen_lookup_remove(struct drm_privacy_screen_lookup *lookup)
  64. {
  65. mutex_lock(&drm_privacy_screen_lookup_lock);
  66. list_del(&lookup->list);
  67. mutex_unlock(&drm_privacy_screen_lookup_lock);
  68. }
  69. EXPORT_SYMBOL(drm_privacy_screen_lookup_remove);
  70. /*** drm_privacy_screen_consumer.h functions ***/
  71. static struct drm_privacy_screen *drm_privacy_screen_get_by_name(
  72. const char *name)
  73. {
  74. struct drm_privacy_screen *priv;
  75. struct device *dev = NULL;
  76. mutex_lock(&drm_privacy_screen_devs_lock);
  77. list_for_each_entry(priv, &drm_privacy_screen_devs, list) {
  78. if (strcmp(dev_name(&priv->dev), name) == 0) {
  79. dev = get_device(&priv->dev);
  80. break;
  81. }
  82. }
  83. mutex_unlock(&drm_privacy_screen_devs_lock);
  84. return dev ? to_drm_privacy_screen(dev) : NULL;
  85. }
  86. /**
  87. * drm_privacy_screen_get - get a privacy-screen provider
  88. * @dev: consumer-device for which to get a privacy-screen provider
  89. * @con_id: (video)connector name for which to get a privacy-screen provider
  90. *
  91. * Get a privacy-screen provider for a privacy-screen attached to the
  92. * display described by the @dev and @con_id parameters.
  93. *
  94. * Return:
  95. * * A pointer to a &struct drm_privacy_screen on success.
  96. * * ERR_PTR(-ENODEV) if no matching privacy-screen is found
  97. * * ERR_PTR(-EPROBE_DEFER) if there is a matching privacy-screen,
  98. * but it has not been registered yet.
  99. */
  100. struct drm_privacy_screen *drm_privacy_screen_get(struct device *dev,
  101. const char *con_id)
  102. {
  103. const char *dev_id = dev ? dev_name(dev) : NULL;
  104. struct drm_privacy_screen_lookup *l;
  105. struct drm_privacy_screen *priv;
  106. const char *provider = NULL;
  107. int match, best = -1;
  108. /*
  109. * For now we only support using a static lookup table, which is
  110. * populated by the drm_privacy_screen_arch_init() call. This should
  111. * be extended with device-tree / fw_node lookup when support is added
  112. * for device-tree using hardware with a privacy-screen.
  113. *
  114. * The lookup algorithm was shamelessly taken from the clock
  115. * framework:
  116. *
  117. * We do slightly fuzzy matching here:
  118. * An entry with a NULL ID is assumed to be a wildcard.
  119. * If an entry has a device ID, it must match
  120. * If an entry has a connection ID, it must match
  121. * Then we take the most specific entry - with the following order
  122. * of precedence: dev+con > dev only > con only.
  123. */
  124. mutex_lock(&drm_privacy_screen_lookup_lock);
  125. list_for_each_entry(l, &drm_privacy_screen_lookup_list, list) {
  126. match = 0;
  127. if (l->dev_id) {
  128. if (!dev_id || strcmp(l->dev_id, dev_id))
  129. continue;
  130. match += 2;
  131. }
  132. if (l->con_id) {
  133. if (!con_id || strcmp(l->con_id, con_id))
  134. continue;
  135. match += 1;
  136. }
  137. if (match > best) {
  138. provider = l->provider;
  139. best = match;
  140. }
  141. }
  142. mutex_unlock(&drm_privacy_screen_lookup_lock);
  143. if (!provider)
  144. return ERR_PTR(-ENODEV);
  145. priv = drm_privacy_screen_get_by_name(provider);
  146. if (!priv)
  147. return ERR_PTR(-EPROBE_DEFER);
  148. return priv;
  149. }
  150. EXPORT_SYMBOL(drm_privacy_screen_get);
  151. /**
  152. * drm_privacy_screen_put - release a privacy-screen reference
  153. * @priv: privacy screen reference to release
  154. *
  155. * Release a privacy-screen provider reference gotten through
  156. * drm_privacy_screen_get(). May be called with a NULL or ERR_PTR,
  157. * in which case it is a no-op.
  158. */
  159. void drm_privacy_screen_put(struct drm_privacy_screen *priv)
  160. {
  161. if (IS_ERR_OR_NULL(priv))
  162. return;
  163. put_device(&priv->dev);
  164. }
  165. EXPORT_SYMBOL(drm_privacy_screen_put);
  166. /**
  167. * drm_privacy_screen_set_sw_state - set a privacy-screen's sw-state
  168. * @priv: privacy screen to set the sw-state for
  169. * @sw_state: new sw-state value to set
  170. *
  171. * Set the sw-state of a privacy screen. If the privacy-screen is not
  172. * in a locked hw-state, then the actual and hw-state of the privacy-screen
  173. * will be immediately updated to the new value. If the privacy-screen is
  174. * in a locked hw-state, then the new sw-state will be remembered as the
  175. * requested state to put the privacy-screen in when it becomes unlocked.
  176. *
  177. * Return: 0 on success, negative error code on failure.
  178. */
  179. int drm_privacy_screen_set_sw_state(struct drm_privacy_screen *priv,
  180. enum drm_privacy_screen_status sw_state)
  181. {
  182. int ret = 0;
  183. mutex_lock(&priv->lock);
  184. if (!priv->ops) {
  185. ret = -ENODEV;
  186. goto out;
  187. }
  188. /*
  189. * As per the DRM connector properties documentation, setting the
  190. * sw_state while the hw_state is locked is allowed. In this case
  191. * it is a no-op other then storing the new sw_state so that it
  192. * can be honored when the state gets unlocked.
  193. * Also skip the set if the hw already is in the desired state.
  194. */
  195. if (priv->hw_state >= PRIVACY_SCREEN_DISABLED_LOCKED ||
  196. priv->hw_state == sw_state) {
  197. priv->sw_state = sw_state;
  198. goto out;
  199. }
  200. ret = priv->ops->set_sw_state(priv, sw_state);
  201. out:
  202. mutex_unlock(&priv->lock);
  203. return ret;
  204. }
  205. EXPORT_SYMBOL(drm_privacy_screen_set_sw_state);
  206. /**
  207. * drm_privacy_screen_get_state - get privacy-screen's current state
  208. * @priv: privacy screen to get the state for
  209. * @sw_state_ret: address where to store the privacy-screens current sw-state
  210. * @hw_state_ret: address where to store the privacy-screens current hw-state
  211. *
  212. * Get the current state of a privacy-screen, both the sw-state and the
  213. * hw-state.
  214. */
  215. void drm_privacy_screen_get_state(struct drm_privacy_screen *priv,
  216. enum drm_privacy_screen_status *sw_state_ret,
  217. enum drm_privacy_screen_status *hw_state_ret)
  218. {
  219. mutex_lock(&priv->lock);
  220. *sw_state_ret = priv->sw_state;
  221. *hw_state_ret = priv->hw_state;
  222. mutex_unlock(&priv->lock);
  223. }
  224. EXPORT_SYMBOL(drm_privacy_screen_get_state);
  225. /**
  226. * drm_privacy_screen_register_notifier - register a notifier
  227. * @priv: Privacy screen to register the notifier with
  228. * @nb: Notifier-block for the notifier to register
  229. *
  230. * Register a notifier with the privacy-screen to be notified of changes made
  231. * to the privacy-screen state from outside of the privacy-screen class.
  232. * E.g. the state may be changed by the hardware itself in response to a
  233. * hotkey press.
  234. *
  235. * The notifier is called with no locks held. The new hw_state and sw_state
  236. * can be retrieved using the drm_privacy_screen_get_state() function.
  237. * A pointer to the drm_privacy_screen's struct is passed as the ``void *data``
  238. * argument of the notifier_block's notifier_call.
  239. *
  240. * The notifier will NOT be called when changes are made through
  241. * drm_privacy_screen_set_sw_state(). It is only called for external changes.
  242. *
  243. * Return: 0 on success, negative error code on failure.
  244. */
  245. int drm_privacy_screen_register_notifier(struct drm_privacy_screen *priv,
  246. struct notifier_block *nb)
  247. {
  248. return blocking_notifier_chain_register(&priv->notifier_head, nb);
  249. }
  250. EXPORT_SYMBOL(drm_privacy_screen_register_notifier);
  251. /**
  252. * drm_privacy_screen_unregister_notifier - unregister a notifier
  253. * @priv: Privacy screen to register the notifier with
  254. * @nb: Notifier-block for the notifier to register
  255. *
  256. * Unregister a notifier registered with drm_privacy_screen_register_notifier().
  257. *
  258. * Return: 0 on success, negative error code on failure.
  259. */
  260. int drm_privacy_screen_unregister_notifier(struct drm_privacy_screen *priv,
  261. struct notifier_block *nb)
  262. {
  263. return blocking_notifier_chain_unregister(&priv->notifier_head, nb);
  264. }
  265. EXPORT_SYMBOL(drm_privacy_screen_unregister_notifier);
  266. /*** drm_privacy_screen_driver.h functions ***/
  267. static ssize_t sw_state_show(struct device *dev,
  268. struct device_attribute *attr, char *buf)
  269. {
  270. struct drm_privacy_screen *priv = to_drm_privacy_screen(dev);
  271. const char * const sw_state_names[] = {
  272. "Disabled",
  273. "Enabled",
  274. };
  275. ssize_t ret;
  276. mutex_lock(&priv->lock);
  277. if (!priv->ops)
  278. ret = -ENODEV;
  279. else if (WARN_ON(priv->sw_state >= ARRAY_SIZE(sw_state_names)))
  280. ret = -ENXIO;
  281. else
  282. ret = sprintf(buf, "%s\n", sw_state_names[priv->sw_state]);
  283. mutex_unlock(&priv->lock);
  284. return ret;
  285. }
  286. /*
  287. * RO: Do not allow setting the sw_state through sysfs, this MUST be done
  288. * through the drm_properties on the drm_connector.
  289. */
  290. static DEVICE_ATTR_RO(sw_state);
  291. static ssize_t hw_state_show(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. struct drm_privacy_screen *priv = to_drm_privacy_screen(dev);
  295. const char * const hw_state_names[] = {
  296. "Disabled",
  297. "Enabled",
  298. "Disabled, locked",
  299. "Enabled, locked",
  300. };
  301. ssize_t ret;
  302. mutex_lock(&priv->lock);
  303. if (!priv->ops)
  304. ret = -ENODEV;
  305. else if (WARN_ON(priv->hw_state >= ARRAY_SIZE(hw_state_names)))
  306. ret = -ENXIO;
  307. else
  308. ret = sprintf(buf, "%s\n", hw_state_names[priv->hw_state]);
  309. mutex_unlock(&priv->lock);
  310. return ret;
  311. }
  312. static DEVICE_ATTR_RO(hw_state);
  313. static struct attribute *drm_privacy_screen_attrs[] = {
  314. &dev_attr_sw_state.attr,
  315. &dev_attr_hw_state.attr,
  316. NULL
  317. };
  318. ATTRIBUTE_GROUPS(drm_privacy_screen);
  319. static struct device_type drm_privacy_screen_type = {
  320. .name = "privacy_screen",
  321. .groups = drm_privacy_screen_groups,
  322. };
  323. static void drm_privacy_screen_device_release(struct device *dev)
  324. {
  325. struct drm_privacy_screen *priv = to_drm_privacy_screen(dev);
  326. kfree(priv);
  327. }
  328. /**
  329. * drm_privacy_screen_register - register a privacy-screen
  330. * @parent: parent-device for the privacy-screen
  331. * @ops: &struct drm_privacy_screen_ops pointer with ops for the privacy-screen
  332. * @data: Private data owned by the privacy screen provider
  333. *
  334. * Create and register a privacy-screen.
  335. *
  336. * Return:
  337. * * A pointer to the created privacy-screen on success.
  338. * * An ERR_PTR(errno) on failure.
  339. */
  340. struct drm_privacy_screen *drm_privacy_screen_register(
  341. struct device *parent, const struct drm_privacy_screen_ops *ops,
  342. void *data)
  343. {
  344. struct drm_privacy_screen *priv;
  345. int ret;
  346. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  347. if (!priv)
  348. return ERR_PTR(-ENOMEM);
  349. mutex_init(&priv->lock);
  350. BLOCKING_INIT_NOTIFIER_HEAD(&priv->notifier_head);
  351. priv->dev.class = drm_class;
  352. priv->dev.type = &drm_privacy_screen_type;
  353. priv->dev.parent = parent;
  354. priv->dev.release = drm_privacy_screen_device_release;
  355. dev_set_name(&priv->dev, "privacy_screen-%s", dev_name(parent));
  356. priv->drvdata = data;
  357. priv->ops = ops;
  358. priv->ops->get_hw_state(priv);
  359. ret = device_register(&priv->dev);
  360. if (ret) {
  361. put_device(&priv->dev);
  362. return ERR_PTR(ret);
  363. }
  364. mutex_lock(&drm_privacy_screen_devs_lock);
  365. list_add(&priv->list, &drm_privacy_screen_devs);
  366. mutex_unlock(&drm_privacy_screen_devs_lock);
  367. return priv;
  368. }
  369. EXPORT_SYMBOL(drm_privacy_screen_register);
  370. /**
  371. * drm_privacy_screen_unregister - unregister privacy-screen
  372. * @priv: privacy-screen to unregister
  373. *
  374. * Unregister a privacy-screen registered with drm_privacy_screen_register().
  375. * May be called with a NULL or ERR_PTR, in which case it is a no-op.
  376. */
  377. void drm_privacy_screen_unregister(struct drm_privacy_screen *priv)
  378. {
  379. if (IS_ERR_OR_NULL(priv))
  380. return;
  381. mutex_lock(&drm_privacy_screen_devs_lock);
  382. list_del(&priv->list);
  383. mutex_unlock(&drm_privacy_screen_devs_lock);
  384. mutex_lock(&priv->lock);
  385. priv->drvdata = NULL;
  386. priv->ops = NULL;
  387. mutex_unlock(&priv->lock);
  388. device_unregister(&priv->dev);
  389. }
  390. EXPORT_SYMBOL(drm_privacy_screen_unregister);
  391. /**
  392. * drm_privacy_screen_call_notifier_chain - notify consumers of state change
  393. * @priv: Privacy screen to register the notifier with
  394. *
  395. * A privacy-screen provider driver can call this functions upon external
  396. * changes to the privacy-screen state. E.g. the state may be changed by the
  397. * hardware itself in response to a hotkey press.
  398. * This function must be called without holding the privacy-screen lock.
  399. * the driver must update sw_state and hw_state to reflect the new state before
  400. * calling this function.
  401. * The expected behavior from the driver upon receiving an external state
  402. * change event is: 1. Take the lock; 2. Update sw_state and hw_state;
  403. * 3. Release the lock. 4. Call drm_privacy_screen_call_notifier_chain().
  404. */
  405. void drm_privacy_screen_call_notifier_chain(struct drm_privacy_screen *priv)
  406. {
  407. blocking_notifier_call_chain(&priv->notifier_head, 0, priv);
  408. }
  409. EXPORT_SYMBOL(drm_privacy_screen_call_notifier_chain);