msm_hdcp.c 6.7 KB

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