msm_hdcp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[msm-hdcp] %s: " fmt, __func__
  6. #include <linux/platform_device.h>
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/file.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/cdev.h>
  14. #include <linux/list.h>
  15. #include <linux/device.h>
  16. #include <linux/errno.h>
  17. #include <linux/msm_hdcp.h>
  18. #include <linux/of.h>
  19. #define CLASS_NAME "hdcp"
  20. #define DRIVER_NAME "msm_hdcp"
  21. struct msm_hdcp {
  22. struct platform_device *pdev;
  23. dev_t dev_num;
  24. struct cdev cdev;
  25. struct class *class;
  26. struct device *device;
  27. struct HDCP_V2V1_MSG_TOPOLOGY cached_tp;
  28. u32 tp_msgid;
  29. void *client_ctx;
  30. void (*cb)(void *ctx, u8 data);
  31. };
  32. void msm_hdcp_register_cb(struct device *dev, void *ctx,
  33. void (*cb)(void *ctx, u8 data))
  34. {
  35. struct msm_hdcp *hdcp = NULL;
  36. if (!dev) {
  37. pr_err("invalid device pointer\n");
  38. return;
  39. }
  40. hdcp = dev_get_drvdata(dev);
  41. if (!hdcp) {
  42. pr_err("invalid driver pointer\n");
  43. return;
  44. }
  45. hdcp->cb = cb;
  46. hdcp->client_ctx = ctx;
  47. }
  48. EXPORT_SYMBOL(msm_hdcp_register_cb);
  49. void msm_hdcp_notify_topology(struct device *dev)
  50. {
  51. char *envp[4];
  52. char tp[SZ_16];
  53. char ver[SZ_16];
  54. struct msm_hdcp *hdcp = NULL;
  55. if (!dev) {
  56. pr_err("invalid device pointer\n");
  57. return;
  58. }
  59. hdcp = dev_get_drvdata(dev);
  60. if (!hdcp) {
  61. pr_err("invalid driver pointer\n");
  62. return;
  63. }
  64. snprintf(tp, SZ_16, "%d", DOWN_CHECK_TOPOLOGY);
  65. snprintf(ver, SZ_16, "%d", HDCP_V1_TX);
  66. envp[0] = "HDCP_MGR_EVENT=MSG_READY";
  67. envp[1] = tp;
  68. envp[2] = ver;
  69. envp[3] = NULL;
  70. kobject_uevent_env(&hdcp->device->kobj, KOBJ_CHANGE, envp);
  71. }
  72. EXPORT_SYMBOL(msm_hdcp_notify_topology);
  73. void msm_hdcp_cache_repeater_topology(struct device *dev,
  74. struct HDCP_V2V1_MSG_TOPOLOGY *tp)
  75. {
  76. struct msm_hdcp *hdcp = NULL;
  77. if (!dev || !tp) {
  78. pr_err("invalid input\n");
  79. return;
  80. }
  81. hdcp = dev_get_drvdata(dev);
  82. if (!hdcp) {
  83. pr_err("invalid driver pointer\n");
  84. return;
  85. }
  86. memcpy(&hdcp->cached_tp, tp,
  87. sizeof(struct HDCP_V2V1_MSG_TOPOLOGY));
  88. }
  89. EXPORT_SYMBOL(msm_hdcp_cache_repeater_topology);
  90. static ssize_t tp_show(struct device *dev, struct device_attribute *attr,
  91. char *buf)
  92. {
  93. ssize_t ret = 0;
  94. struct msm_hdcp *hdcp = NULL;
  95. if (!dev) {
  96. pr_err("invalid device pointer\n");
  97. return -ENODEV;
  98. }
  99. hdcp = dev_get_drvdata(dev);
  100. if (!hdcp) {
  101. pr_err("invalid driver pointer\n");
  102. return -ENODEV;
  103. }
  104. switch (hdcp->tp_msgid) {
  105. case DOWN_CHECK_TOPOLOGY:
  106. case DOWN_REQUEST_TOPOLOGY:
  107. buf[MSG_ID_IDX] = hdcp->tp_msgid;
  108. buf[RET_CODE_IDX] = HDCP_AUTHED;
  109. ret = HEADER_LEN;
  110. memcpy(buf + HEADER_LEN, &hdcp->cached_tp,
  111. sizeof(struct HDCP_V2V1_MSG_TOPOLOGY));
  112. ret += sizeof(struct HDCP_V2V1_MSG_TOPOLOGY);
  113. /* clear the flag once data is read back to user space*/
  114. hdcp->tp_msgid = -1;
  115. break;
  116. default:
  117. ret = -EINVAL;
  118. }
  119. return ret;
  120. }
  121. static ssize_t tp_store(struct device *dev, struct device_attribute *attr,
  122. const char *buf, size_t count)
  123. {
  124. int msgid = 0;
  125. ssize_t ret = count;
  126. struct msm_hdcp *hdcp = NULL;
  127. if (!dev) {
  128. pr_err("invalid device pointer\n");
  129. return -ENODEV;
  130. }
  131. hdcp = dev_get_drvdata(dev);
  132. if (!hdcp) {
  133. pr_err("invalid driver pointer\n");
  134. return -ENODEV;
  135. }
  136. msgid = buf[0];
  137. switch (msgid) {
  138. case DOWN_CHECK_TOPOLOGY:
  139. case DOWN_REQUEST_TOPOLOGY:
  140. hdcp->tp_msgid = msgid;
  141. break;
  142. default:
  143. ret = -EINVAL;
  144. }
  145. return ret;
  146. }
  147. static ssize_t min_level_change_store(struct device *dev,
  148. struct device_attribute *attr, const char *buf, size_t count)
  149. {
  150. int rc;
  151. int min_enc_lvl;
  152. ssize_t ret = count;
  153. struct msm_hdcp *hdcp = NULL;
  154. if (!dev) {
  155. pr_err("invalid device pointer\n");
  156. return -ENODEV;
  157. }
  158. hdcp = dev_get_drvdata(dev);
  159. if (!hdcp) {
  160. pr_err("invalid driver pointer\n");
  161. return -ENODEV;
  162. }
  163. rc = kstrtoint(buf, 10, &min_enc_lvl);
  164. if (rc) {
  165. pr_err("kstrtoint failed. rc=%d\n", rc);
  166. return -EINVAL;
  167. }
  168. if (hdcp->cb && hdcp->client_ctx)
  169. hdcp->cb(hdcp->client_ctx, min_enc_lvl);
  170. return ret;
  171. }
  172. static DEVICE_ATTR_RW(tp);
  173. static DEVICE_ATTR_WO(min_level_change);
  174. static struct attribute *msm_hdcp_fs_attrs[] = {
  175. &dev_attr_tp.attr,
  176. &dev_attr_min_level_change.attr,
  177. NULL
  178. };
  179. static struct attribute_group msm_hdcp_fs_attr_group = {
  180. .attrs = msm_hdcp_fs_attrs
  181. };
  182. static int msm_hdcp_open(struct inode *inode, struct file *file)
  183. {
  184. return 0;
  185. }
  186. static int msm_hdcp_close(struct inode *inode, struct file *file)
  187. {
  188. return 0;
  189. }
  190. static const struct file_operations msm_hdcp_fops = {
  191. .owner = THIS_MODULE,
  192. .open = msm_hdcp_open,
  193. .release = msm_hdcp_close,
  194. };
  195. static const struct of_device_id msm_hdcp_dt_match[] = {
  196. { .compatible = "qcom,msm-hdcp",},
  197. {}
  198. };
  199. MODULE_DEVICE_TABLE(of, msm_hdcp_dt_match);
  200. static int msm_hdcp_probe(struct platform_device *pdev)
  201. {
  202. int ret;
  203. struct msm_hdcp *hdcp;
  204. hdcp = devm_kzalloc(&pdev->dev, sizeof(struct msm_hdcp), GFP_KERNEL);
  205. if (!hdcp)
  206. return -ENOMEM;
  207. hdcp->pdev = pdev;
  208. platform_set_drvdata(pdev, hdcp);
  209. ret = alloc_chrdev_region(&hdcp->dev_num, 0, 1, DRIVER_NAME);
  210. if (ret < 0) {
  211. pr_err("alloc_chrdev_region failed ret = %d\n", ret);
  212. return ret;
  213. }
  214. hdcp->class = class_create(THIS_MODULE, CLASS_NAME);
  215. if (IS_ERR(hdcp->class)) {
  216. ret = PTR_ERR(hdcp->class);
  217. pr_err("couldn't create class rc = %d\n", ret);
  218. goto error_class_create;
  219. }
  220. hdcp->device = device_create(hdcp->class, NULL,
  221. hdcp->dev_num, hdcp, DRIVER_NAME);
  222. if (IS_ERR(hdcp->device)) {
  223. ret = PTR_ERR(hdcp->device);
  224. pr_err("device_create failed %d\n", ret);
  225. goto error_class_device_create;
  226. }
  227. cdev_init(&hdcp->cdev, &msm_hdcp_fops);
  228. ret = cdev_add(&hdcp->cdev, MKDEV(MAJOR(hdcp->dev_num), 0), 1);
  229. if (ret < 0) {
  230. pr_err("cdev_add failed %d\n", ret);
  231. goto error_cdev_add;
  232. }
  233. ret = sysfs_create_group(&hdcp->device->kobj, &msm_hdcp_fs_attr_group);
  234. if (ret)
  235. pr_err("unable to register msm_hdcp sysfs nodes\n");
  236. return 0;
  237. error_cdev_add:
  238. device_destroy(hdcp->class, hdcp->dev_num);
  239. error_class_device_create:
  240. class_destroy(hdcp->class);
  241. error_class_create:
  242. unregister_chrdev_region(hdcp->dev_num, 1);
  243. return ret;
  244. }
  245. static int msm_hdcp_remove(struct platform_device *pdev)
  246. {
  247. struct msm_hdcp *hdcp;
  248. hdcp = platform_get_drvdata(pdev);
  249. if (!hdcp)
  250. return -ENODEV;
  251. sysfs_remove_group(&hdcp->device->kobj,
  252. &msm_hdcp_fs_attr_group);
  253. cdev_del(&hdcp->cdev);
  254. device_destroy(hdcp->class, hdcp->dev_num);
  255. class_destroy(hdcp->class);
  256. unregister_chrdev_region(hdcp->dev_num, 1);
  257. return 0;
  258. }
  259. static struct platform_driver msm_hdcp_driver = {
  260. .probe = msm_hdcp_probe,
  261. .remove = msm_hdcp_remove,
  262. .driver = {
  263. .name = "msm_hdcp",
  264. .of_match_table = msm_hdcp_dt_match,
  265. .pm = NULL,
  266. }
  267. };
  268. void __init msm_hdcp_register(void)
  269. {
  270. platform_driver_register(&msm_hdcp_driver);
  271. }
  272. void __exit msm_hdcp_unregister(void)
  273. {
  274. platform_driver_unregister(&msm_hdcp_driver);
  275. }