Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/privacy-screen: Add notifier support (v2)

Add support for privacy-screen consumers to register a notifier to
be notified of external (e.g. done by the hw itself on a hotkey press)
state changes.

Changes in v2:
- Drop WARN_ON(mutex_is_locked(&priv->lock)) check in
drm_privacy_screen_call_notifier_chain() it may be locked by
another thread, which would lead to a false-positive triggering
of the check

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lyude Paul <lyude@redhat.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20211005202322.700909-5-hdegoede@redhat.com

+83
+64
drivers/gpu/drm/drm_privacy_screen.c
··· 257 257 } 258 258 EXPORT_SYMBOL(drm_privacy_screen_get_state); 259 259 260 + /** 261 + * drm_privacy_screen_register_notifier - register a notifier 262 + * @priv: Privacy screen to register the notifier with 263 + * @nb: Notifier-block for the notifier to register 264 + * 265 + * Register a notifier with the privacy-screen to be notified of changes made 266 + * to the privacy-screen state from outside of the privacy-screen class. 267 + * E.g. the state may be changed by the hardware itself in response to a 268 + * hotkey press. 269 + * 270 + * The notifier is called with no locks held. The new hw_state and sw_state 271 + * can be retrieved using the drm_privacy_screen_get_state() function. 272 + * A pointer to the drm_privacy_screen's struct is passed as the void *data 273 + * argument of the notifier_block's notifier_call. 274 + * 275 + * The notifier will NOT be called when changes are made through 276 + * drm_privacy_screen_set_sw_state(). It is only called for external changes. 277 + * 278 + * Return: 0 on success, negative error code on failure. 279 + */ 280 + int drm_privacy_screen_register_notifier(struct drm_privacy_screen *priv, 281 + struct notifier_block *nb) 282 + { 283 + return blocking_notifier_chain_register(&priv->notifier_head, nb); 284 + } 285 + EXPORT_SYMBOL(drm_privacy_screen_register_notifier); 286 + 287 + /** 288 + * drm_privacy_screen_unregister_notifier - unregister a notifier 289 + * @priv: Privacy screen to register the notifier with 290 + * @nb: Notifier-block for the notifier to register 291 + * 292 + * Unregister a notifier registered with drm_privacy_screen_register_notifier(). 293 + * 294 + * Return: 0 on success, negative error code on failure. 295 + */ 296 + int drm_privacy_screen_unregister_notifier(struct drm_privacy_screen *priv, 297 + struct notifier_block *nb) 298 + { 299 + return blocking_notifier_chain_unregister(&priv->notifier_head, nb); 300 + } 301 + EXPORT_SYMBOL(drm_privacy_screen_unregister_notifier); 302 + 260 303 /*** drm_privacy_screen_driver.h functions ***/ 261 304 262 305 static ssize_t sw_state_show(struct device *dev, ··· 397 354 return ERR_PTR(-ENOMEM); 398 355 399 356 mutex_init(&priv->lock); 357 + BLOCKING_INIT_NOTIFIER_HEAD(&priv->notifier_head); 400 358 401 359 priv->dev.class = drm_class; 402 360 priv->dev.type = &drm_privacy_screen_type; ··· 445 401 device_unregister(&priv->dev); 446 402 } 447 403 EXPORT_SYMBOL(drm_privacy_screen_unregister); 404 + 405 + /** 406 + * drm_privacy_screen_call_notifier_chain - notify consumers of state change 407 + * @priv: Privacy screen to register the notifier with 408 + * 409 + * A privacy-screen provider driver can call this functions upon external 410 + * changes to the privacy-screen state. E.g. the state may be changed by the 411 + * hardware itself in response to a hotkey press. 412 + * This function must be called without holding the privacy-screen lock. 413 + * the driver must update sw_state and hw_state to reflect the new state before 414 + * calling this function. 415 + * The expected behavior from the driver upon receiving an external state 416 + * change event is: 1. Take the lock; 2. Update sw_state and hw_state; 417 + * 3. Release the lock. 4. Call drm_privacy_screen_call_notifier_chain(). 418 + */ 419 + void drm_privacy_screen_call_notifier_chain(struct drm_privacy_screen *priv) 420 + { 421 + blocking_notifier_call_chain(&priv->notifier_head, 0, priv); 422 + } 423 + EXPORT_SYMBOL(drm_privacy_screen_call_notifier_chain);
+15
include/drm/drm_privacy_screen_consumer.h
··· 24 24 void drm_privacy_screen_get_state(struct drm_privacy_screen *priv, 25 25 enum drm_privacy_screen_status *sw_state_ret, 26 26 enum drm_privacy_screen_status *hw_state_ret); 27 + 28 + int drm_privacy_screen_register_notifier(struct drm_privacy_screen *priv, 29 + struct notifier_block *nb); 30 + int drm_privacy_screen_unregister_notifier(struct drm_privacy_screen *priv, 31 + struct notifier_block *nb); 27 32 #else 28 33 static inline struct drm_privacy_screen *drm_privacy_screen_get(struct device *dev, 29 34 const char *con_id) ··· 49 44 { 50 45 *sw_state_ret = PRIVACY_SCREEN_DISABLED; 51 46 *hw_state_ret = PRIVACY_SCREEN_DISABLED; 47 + } 48 + static inline int drm_privacy_screen_register_notifier(struct drm_privacy_screen *priv, 49 + struct notifier_block *nb) 50 + { 51 + return -ENODEV; 52 + } 53 + static inline int drm_privacy_screen_unregister_notifier(struct drm_privacy_screen *priv, 54 + struct notifier_block *nb) 55 + { 56 + return -ENODEV; 52 57 } 53 58 #endif 54 59
+4
include/drm/drm_privacy_screen_driver.h
··· 54 54 struct mutex lock; 55 55 /** @list: privacy-screen devices list list-entry. */ 56 56 struct list_head list; 57 + /** @notifier_head: privacy-screen notifier head. */ 58 + struct blocking_notifier_head notifier_head; 57 59 /** 58 60 * @ops: &struct drm_privacy_screen_ops for this privacy-screen. 59 61 * This is NULL if the driver has unregistered the privacy-screen. ··· 78 76 struct drm_privacy_screen *drm_privacy_screen_register( 79 77 struct device *parent, const struct drm_privacy_screen_ops *ops); 80 78 void drm_privacy_screen_unregister(struct drm_privacy_screen *priv); 79 + 80 + void drm_privacy_screen_call_notifier_chain(struct drm_privacy_screen *priv); 81 81 82 82 #endif