cec-notifier.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cec-notifier.c - notify CEC drivers of physical address changes
  4. *
  5. * Copyright 2016 Russell King <rmk+kernel@arm.linux.org.uk>
  6. * Copyright 2016-2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/string.h>
  10. #include <linux/slab.h>
  11. #include <linux/list.h>
  12. #include <linux/kref.h>
  13. #include <media/cec.h>
  14. #include <media/cec-notifier.h>
  15. #include <drm/drm_edid.h>
  16. struct cec_notifier {
  17. struct mutex lock;
  18. struct list_head head;
  19. struct kref kref;
  20. struct device *dev;
  21. struct cec_adapter *cec_adap;
  22. void (*callback)(struct cec_adapter *adap, u16 pa);
  23. u16 phys_addr;
  24. };
  25. static LIST_HEAD(cec_notifiers);
  26. static DEFINE_MUTEX(cec_notifiers_lock);
  27. struct cec_notifier *cec_notifier_get(struct device *dev)
  28. {
  29. struct cec_notifier *n;
  30. mutex_lock(&cec_notifiers_lock);
  31. list_for_each_entry(n, &cec_notifiers, head) {
  32. if (n->dev == dev) {
  33. kref_get(&n->kref);
  34. mutex_unlock(&cec_notifiers_lock);
  35. return n;
  36. }
  37. }
  38. n = kzalloc(sizeof(*n), GFP_KERNEL);
  39. if (!n)
  40. goto unlock;
  41. n->dev = dev;
  42. n->phys_addr = CEC_PHYS_ADDR_INVALID;
  43. mutex_init(&n->lock);
  44. kref_init(&n->kref);
  45. list_add_tail(&n->head, &cec_notifiers);
  46. unlock:
  47. mutex_unlock(&cec_notifiers_lock);
  48. return n;
  49. }
  50. EXPORT_SYMBOL_GPL(cec_notifier_get);
  51. static void cec_notifier_release(struct kref *kref)
  52. {
  53. struct cec_notifier *n =
  54. container_of(kref, struct cec_notifier, kref);
  55. list_del(&n->head);
  56. kfree(n);
  57. }
  58. void cec_notifier_put(struct cec_notifier *n)
  59. {
  60. mutex_lock(&cec_notifiers_lock);
  61. kref_put(&n->kref, cec_notifier_release);
  62. mutex_unlock(&cec_notifiers_lock);
  63. }
  64. EXPORT_SYMBOL_GPL(cec_notifier_put);
  65. void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
  66. {
  67. if (n == NULL)
  68. return;
  69. mutex_lock(&n->lock);
  70. n->phys_addr = pa;
  71. if (n->callback)
  72. n->callback(n->cec_adap, n->phys_addr);
  73. mutex_unlock(&n->lock);
  74. }
  75. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
  76. void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
  77. const struct edid *edid)
  78. {
  79. u16 pa = CEC_PHYS_ADDR_INVALID;
  80. if (n == NULL)
  81. return;
  82. if (edid && edid->extensions)
  83. pa = cec_get_edid_phys_addr((const u8 *)edid,
  84. EDID_LENGTH * (edid->extensions + 1), NULL);
  85. cec_notifier_set_phys_addr(n, pa);
  86. }
  87. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
  88. void cec_notifier_register(struct cec_notifier *n,
  89. struct cec_adapter *adap,
  90. void (*callback)(struct cec_adapter *adap, u16 pa))
  91. {
  92. kref_get(&n->kref);
  93. mutex_lock(&n->lock);
  94. n->cec_adap = adap;
  95. n->callback = callback;
  96. n->callback(adap, n->phys_addr);
  97. mutex_unlock(&n->lock);
  98. }
  99. EXPORT_SYMBOL_GPL(cec_notifier_register);
  100. void cec_notifier_unregister(struct cec_notifier *n)
  101. {
  102. mutex_lock(&n->lock);
  103. n->callback = NULL;
  104. mutex_unlock(&n->lock);
  105. cec_notifier_put(n);
  106. }
  107. EXPORT_SYMBOL_GPL(cec_notifier_unregister);