cec-notifier.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <linux/of_platform.h>
  14. #include <media/cec.h>
  15. #include <media/cec-notifier.h>
  16. #include <drm/drm_edid.h>
  17. struct cec_notifier {
  18. struct mutex lock;
  19. struct list_head head;
  20. struct kref kref;
  21. struct device *hdmi_dev;
  22. struct cec_connector_info conn_info;
  23. const char *conn_name;
  24. struct cec_adapter *cec_adap;
  25. void (*callback)(struct cec_adapter *adap, u16 pa);
  26. u16 phys_addr;
  27. };
  28. static LIST_HEAD(cec_notifiers);
  29. static DEFINE_MUTEX(cec_notifiers_lock);
  30. struct cec_notifier *
  31. cec_notifier_get_conn(struct device *hdmi_dev, const char *conn_name)
  32. {
  33. struct cec_notifier *n;
  34. mutex_lock(&cec_notifiers_lock);
  35. list_for_each_entry(n, &cec_notifiers, head) {
  36. if (n->hdmi_dev == hdmi_dev &&
  37. (!conn_name ||
  38. (n->conn_name && !strcmp(n->conn_name, conn_name)))) {
  39. kref_get(&n->kref);
  40. mutex_unlock(&cec_notifiers_lock);
  41. return n;
  42. }
  43. }
  44. n = kzalloc(sizeof(*n), GFP_KERNEL);
  45. if (!n)
  46. goto unlock;
  47. n->hdmi_dev = hdmi_dev;
  48. if (conn_name) {
  49. n->conn_name = kstrdup(conn_name, GFP_KERNEL);
  50. if (!n->conn_name) {
  51. kfree(n);
  52. n = NULL;
  53. goto unlock;
  54. }
  55. }
  56. n->phys_addr = CEC_PHYS_ADDR_INVALID;
  57. mutex_init(&n->lock);
  58. kref_init(&n->kref);
  59. list_add_tail(&n->head, &cec_notifiers);
  60. unlock:
  61. mutex_unlock(&cec_notifiers_lock);
  62. return n;
  63. }
  64. EXPORT_SYMBOL_GPL(cec_notifier_get_conn);
  65. static void cec_notifier_release(struct kref *kref)
  66. {
  67. struct cec_notifier *n =
  68. container_of(kref, struct cec_notifier, kref);
  69. list_del(&n->head);
  70. kfree(n->conn_name);
  71. kfree(n);
  72. }
  73. void cec_notifier_put(struct cec_notifier *n)
  74. {
  75. mutex_lock(&cec_notifiers_lock);
  76. kref_put(&n->kref, cec_notifier_release);
  77. mutex_unlock(&cec_notifiers_lock);
  78. }
  79. EXPORT_SYMBOL_GPL(cec_notifier_put);
  80. struct cec_notifier *
  81. cec_notifier_conn_register(struct device *hdmi_dev, const char *conn_name,
  82. const struct cec_connector_info *conn_info)
  83. {
  84. struct cec_notifier *n = cec_notifier_get_conn(hdmi_dev, conn_name);
  85. if (!n)
  86. return n;
  87. mutex_lock(&n->lock);
  88. n->phys_addr = CEC_PHYS_ADDR_INVALID;
  89. if (conn_info)
  90. n->conn_info = *conn_info;
  91. else
  92. memset(&n->conn_info, 0, sizeof(n->conn_info));
  93. if (n->cec_adap) {
  94. cec_phys_addr_invalidate(n->cec_adap);
  95. cec_s_conn_info(n->cec_adap, conn_info);
  96. }
  97. mutex_unlock(&n->lock);
  98. return n;
  99. }
  100. EXPORT_SYMBOL_GPL(cec_notifier_conn_register);
  101. void cec_notifier_conn_unregister(struct cec_notifier *n)
  102. {
  103. if (!n)
  104. return;
  105. mutex_lock(&n->lock);
  106. memset(&n->conn_info, 0, sizeof(n->conn_info));
  107. n->phys_addr = CEC_PHYS_ADDR_INVALID;
  108. if (n->cec_adap) {
  109. cec_phys_addr_invalidate(n->cec_adap);
  110. cec_s_conn_info(n->cec_adap, NULL);
  111. }
  112. mutex_unlock(&n->lock);
  113. cec_notifier_put(n);
  114. }
  115. EXPORT_SYMBOL_GPL(cec_notifier_conn_unregister);
  116. struct cec_notifier *
  117. cec_notifier_cec_adap_register(struct device *hdmi_dev, const char *conn_name,
  118. struct cec_adapter *adap)
  119. {
  120. struct cec_notifier *n;
  121. if (WARN_ON(!adap))
  122. return NULL;
  123. n = cec_notifier_get_conn(hdmi_dev, conn_name);
  124. if (!n)
  125. return n;
  126. mutex_lock(&n->lock);
  127. n->cec_adap = adap;
  128. adap->conn_info = n->conn_info;
  129. adap->notifier = n;
  130. cec_s_phys_addr(adap, n->phys_addr, false);
  131. mutex_unlock(&n->lock);
  132. return n;
  133. }
  134. EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_register);
  135. void cec_notifier_cec_adap_unregister(struct cec_notifier *n)
  136. {
  137. if (!n)
  138. return;
  139. mutex_lock(&n->lock);
  140. n->cec_adap->notifier = NULL;
  141. n->cec_adap = NULL;
  142. n->callback = NULL;
  143. mutex_unlock(&n->lock);
  144. cec_notifier_put(n);
  145. }
  146. EXPORT_SYMBOL_GPL(cec_notifier_cec_adap_unregister);
  147. void cec_notifier_set_phys_addr(struct cec_notifier *n, u16 pa)
  148. {
  149. if (n == NULL)
  150. return;
  151. mutex_lock(&n->lock);
  152. n->phys_addr = pa;
  153. if (n->callback)
  154. n->callback(n->cec_adap, n->phys_addr);
  155. else if (n->cec_adap)
  156. cec_s_phys_addr(n->cec_adap, n->phys_addr, false);
  157. mutex_unlock(&n->lock);
  158. }
  159. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr);
  160. void cec_notifier_set_phys_addr_from_edid(struct cec_notifier *n,
  161. const struct edid *edid)
  162. {
  163. u16 pa = CEC_PHYS_ADDR_INVALID;
  164. if (n == NULL)
  165. return;
  166. if (edid && edid->extensions)
  167. pa = cec_get_edid_phys_addr((const u8 *)edid,
  168. EDID_LENGTH * (edid->extensions + 1), NULL);
  169. cec_notifier_set_phys_addr(n, pa);
  170. }
  171. EXPORT_SYMBOL_GPL(cec_notifier_set_phys_addr_from_edid);
  172. void cec_notifier_register(struct cec_notifier *n,
  173. struct cec_adapter *adap,
  174. void (*callback)(struct cec_adapter *adap, u16 pa))
  175. {
  176. kref_get(&n->kref);
  177. mutex_lock(&n->lock);
  178. n->cec_adap = adap;
  179. n->callback = callback;
  180. n->callback(adap, n->phys_addr);
  181. mutex_unlock(&n->lock);
  182. }
  183. EXPORT_SYMBOL_GPL(cec_notifier_register);
  184. void cec_notifier_unregister(struct cec_notifier *n)
  185. {
  186. /* Do nothing unless cec_notifier_register was called first */
  187. if (!n->callback)
  188. return;
  189. mutex_lock(&n->lock);
  190. n->callback = NULL;
  191. n->cec_adap->notifier = NULL;
  192. n->cec_adap = NULL;
  193. mutex_unlock(&n->lock);
  194. cec_notifier_put(n);
  195. }
  196. EXPORT_SYMBOL_GPL(cec_notifier_unregister);
  197. struct device *cec_notifier_parse_hdmi_phandle(struct device *dev)
  198. {
  199. struct platform_device *hdmi_pdev;
  200. struct device *hdmi_dev = NULL;
  201. struct device_node *np;
  202. np = of_parse_phandle(dev->of_node, "hdmi-phandle", 0);
  203. if (!np) {
  204. dev_err(dev, "Failed to find HDMI node in device tree\n");
  205. return ERR_PTR(-ENODEV);
  206. }
  207. hdmi_pdev = of_find_device_by_node(np);
  208. of_node_put(np);
  209. if (hdmi_pdev) {
  210. hdmi_dev = &hdmi_pdev->dev;
  211. /*
  212. * Note that the device struct is only used as a key into the
  213. * cec_notifiers list, it is never actually accessed.
  214. * So we decrement the reference here so we don't leak
  215. * memory.
  216. */
  217. put_device(hdmi_dev);
  218. return hdmi_dev;
  219. }
  220. return ERR_PTR(-EPROBE_DEFER);
  221. }
  222. EXPORT_SYMBOL_GPL(cec_notifier_parse_hdmi_phandle);