cec-notifier.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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-notifier.h>
  26. #include <drm/drm_edid.h>
  27. struct cec_notifier {
  28. struct mutex lock;
  29. struct list_head head;
  30. struct kref kref;
  31. struct device *dev;
  32. struct cec_adapter *cec_adap;
  33. void (*callback)(struct cec_adapter *adap, u16 pa);
  34. u16 phys_addr;
  35. };
  36. static LIST_HEAD(cec_notifiers);
  37. static DEFINE_MUTEX(cec_notifiers_lock);
  38. struct cec_notifier *cec_notifier_get(struct device *dev)
  39. {
  40. struct cec_notifier *n;
  41. mutex_lock(&cec_notifiers_lock);
  42. list_for_each_entry(n, &cec_notifiers, head) {
  43. if (n->dev == dev) {
  44. kref_get(&n->kref);
  45. mutex_unlock(&cec_notifiers_lock);
  46. return n;
  47. }
  48. }
  49. n = kzalloc(sizeof(*n), GFP_KERNEL);
  50. if (!n)
  51. goto unlock;
  52. n->dev = dev;
  53. n->phys_addr = CEC_PHYS_ADDR_INVALID;
  54. mutex_init(&n->lock);
  55. kref_init(&n->kref);
  56. list_add_tail(&n->head, &cec_notifiers);
  57. unlock:
  58. mutex_unlock(&cec_notifiers_lock);
  59. return n;
  60. }
  61. EXPORT_SYMBOL_GPL(cec_notifier_get);
  62. static void cec_notifier_release(struct kref *kref)
  63. {
  64. struct cec_notifier *n =
  65. container_of(kref, struct cec_notifier, kref);
  66. list_del(&n->head);
  67. kfree(n);
  68. }
  69. void cec_notifier_put(struct cec_notifier *n)
  70. {
  71. mutex_lock(&cec_notifiers_lock);
  72. kref_put(&n->kref, cec_notifier_release);
  73. mutex_unlock(&cec_notifiers_lock);
  74. }
  75. EXPORT_SYMBOL_GPL(cec_notifier_put);
  76. void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
  77. {
  78. mutex_lock(&n->lock);
  79. n->phys_addr = pa;
  80. if (n->callback)
  81. n->callback(n->cec_adap, n->phys_addr);
  82. mutex_unlock(&n->lock);
  83. }
  84. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
  85. void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
  86. const struct edid *edid)
  87. {
  88. u16 pa = CEC_PHYS_ADDR_INVALID;
  89. if (edid && edid->extensions)
  90. pa = cec_get_edid_phys_addr((const u8 *)edid,
  91. EDID_LENGTH * (edid->extensions + 1), NULL);
  92. cec_notifier_set_phys_addr(n, pa);
  93. }
  94. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
  95. void cec_notifier_register(struct cec_notifier *n,
  96. struct cec_adapter *adap,
  97. void (*callback)(struct cec_adapter *adap, u16 pa))
  98. {
  99. kref_get(&n->kref);
  100. mutex_lock(&n->lock);
  101. n->cec_adap = adap;
  102. n->callback = callback;
  103. n->callback(adap, n->phys_addr);
  104. mutex_unlock(&n->lock);
  105. }
  106. EXPORT_SYMBOL_GPL(cec_notifier_register);
  107. void cec_notifier_unregister(struct cec_notifier *n)
  108. {
  109. mutex_lock(&n->lock);
  110. n->callback = NULL;
  111. mutex_unlock(&n->lock);
  112. cec_notifier_put(n);
  113. }
  114. EXPORT_SYMBOL_GPL(cec_notifier_unregister);