cec-notifier.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * cec-notifier.c - notify CEC drivers of physical address changes
  3. *
  4. * Copyright 2016 Russell King <rmk+kernel@arm.linux.org.uk>
  5. * Copyright 2016-2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. *
  7. * This program is free software; you may redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. * SOFTWARE.
  19. */
  20. #include <linux/export.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. #include <linux/list.h>
  24. #include <linux/kref.h>
  25. #include <media/cec.h>
  26. #include <media/cec-notifier.h>
  27. #include <drm/drm_edid.h>
  28. struct cec_notifier {
  29. struct mutex lock;
  30. struct list_head head;
  31. struct kref kref;
  32. struct device *dev;
  33. struct cec_adapter *cec_adap;
  34. void (*callback)(struct cec_adapter *adap, u16 pa);
  35. u16 phys_addr;
  36. };
  37. static LIST_HEAD(cec_notifiers);
  38. static DEFINE_MUTEX(cec_notifiers_lock);
  39. struct cec_notifier *cec_notifier_get(struct device *dev)
  40. {
  41. struct cec_notifier *n;
  42. mutex_lock(&cec_notifiers_lock);
  43. list_for_each_entry(n, &cec_notifiers, head) {
  44. if (n->dev == dev) {
  45. kref_get(&n->kref);
  46. mutex_unlock(&cec_notifiers_lock);
  47. return n;
  48. }
  49. }
  50. n = kzalloc(sizeof(*n), GFP_KERNEL);
  51. if (!n)
  52. goto unlock;
  53. n->dev = dev;
  54. n->phys_addr = CEC_PHYS_ADDR_INVALID;
  55. mutex_init(&n->lock);
  56. kref_init(&n->kref);
  57. list_add_tail(&n->head, &cec_notifiers);
  58. unlock:
  59. mutex_unlock(&cec_notifiers_lock);
  60. return n;
  61. }
  62. EXPORT_SYMBOL_GPL(cec_notifier_get);
  63. static void cec_notifier_release(struct kref *kref)
  64. {
  65. struct cec_notifier *n =
  66. container_of(kref, struct cec_notifier, kref);
  67. list_del(&n->head);
  68. kfree(n);
  69. }
  70. void cec_notifier_put(struct cec_notifier *n)
  71. {
  72. mutex_lock(&cec_notifiers_lock);
  73. kref_put(&n->kref, cec_notifier_release);
  74. mutex_unlock(&cec_notifiers_lock);
  75. }
  76. EXPORT_SYMBOL_GPL(cec_notifier_put);
  77. void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
  78. {
  79. mutex_lock(&n->lock);
  80. n->phys_addr = pa;
  81. if (n->callback)
  82. n->callback(n->cec_adap, n->phys_addr);
  83. mutex_unlock(&n->lock);
  84. }
  85. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
  86. void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
  87. const struct edid *edid)
  88. {
  89. u16 pa = CEC_PHYS_ADDR_INVALID;
  90. if (edid && edid->extensions)
  91. pa = cec_get_edid_phys_addr((const u8 *)edid,
  92. EDID_LENGTH * (edid->extensions + 1), NULL);
  93. cec_notifier_set_phys_addr(n, pa);
  94. }
  95. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
  96. void cec_notifier_register(struct cec_notifier *n,
  97. struct cec_adapter *adap,
  98. void (*callback)(struct cec_adapter *adap, u16 pa))
  99. {
  100. kref_get(&n->kref);
  101. mutex_lock(&n->lock);
  102. n->cec_adap = adap;
  103. n->callback = callback;
  104. n->callback(adap, n->phys_addr);
  105. mutex_unlock(&n->lock);
  106. }
  107. EXPORT_SYMBOL_GPL(cec_notifier_register);
  108. void cec_notifier_unregister(struct cec_notifier *n)
  109. {
  110. mutex_lock(&n->lock);
  111. n->callback = NULL;
  112. mutex_unlock(&n->lock);
  113. cec_notifier_put(n);
  114. }
  115. EXPORT_SYMBOL_GPL(cec_notifier_unregister);