drm_hdcp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Intel Corporation.
  4. *
  5. * Authors:
  6. * Ramalingam C <ramalingam.c@intel.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/gfp.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/firmware.h>
  14. #include <drm/drm_hdcp.h>
  15. #include <drm/drm_sysfs.h>
  16. #include <drm/drm_print.h>
  17. #include <drm/drm_device.h>
  18. #include <drm/drm_property.h>
  19. #include <drm/drm_mode_object.h>
  20. #include <drm/drm_connector.h>
  21. #include "drm_internal.h"
  22. static inline void drm_hdcp_print_ksv(const u8 *ksv)
  23. {
  24. DRM_DEBUG("\t%#02x, %#02x, %#02x, %#02x, %#02x\n",
  25. ksv[0], ksv[1], ksv[2], ksv[3], ksv[4]);
  26. }
  27. static u32 drm_hdcp_get_revoked_ksv_count(const u8 *buf, u32 vrls_length)
  28. {
  29. u32 parsed_bytes = 0, ksv_count = 0, vrl_ksv_cnt, vrl_sz;
  30. while (parsed_bytes < vrls_length) {
  31. vrl_ksv_cnt = *buf;
  32. ksv_count += vrl_ksv_cnt;
  33. vrl_sz = (vrl_ksv_cnt * DRM_HDCP_KSV_LEN) + 1;
  34. buf += vrl_sz;
  35. parsed_bytes += vrl_sz;
  36. }
  37. /*
  38. * When vrls are not valid, ksvs are not considered.
  39. * Hence SRM will be discarded.
  40. */
  41. if (parsed_bytes != vrls_length)
  42. ksv_count = 0;
  43. return ksv_count;
  44. }
  45. static u32 drm_hdcp_get_revoked_ksvs(const u8 *buf, u8 **revoked_ksv_list,
  46. u32 vrls_length)
  47. {
  48. u32 vrl_ksv_cnt, vrl_ksv_sz, vrl_idx = 0;
  49. u32 parsed_bytes = 0, ksv_count = 0;
  50. do {
  51. vrl_ksv_cnt = *buf;
  52. vrl_ksv_sz = vrl_ksv_cnt * DRM_HDCP_KSV_LEN;
  53. buf++;
  54. DRM_DEBUG("vrl: %d, Revoked KSVs: %d\n", vrl_idx++,
  55. vrl_ksv_cnt);
  56. memcpy((*revoked_ksv_list) + (ksv_count * DRM_HDCP_KSV_LEN),
  57. buf, vrl_ksv_sz);
  58. ksv_count += vrl_ksv_cnt;
  59. buf += vrl_ksv_sz;
  60. parsed_bytes += (vrl_ksv_sz + 1);
  61. } while (parsed_bytes < vrls_length);
  62. return ksv_count;
  63. }
  64. static inline u32 get_vrl_length(const u8 *buf)
  65. {
  66. return drm_hdcp_be24_to_cpu(buf);
  67. }
  68. static int drm_hdcp_parse_hdcp1_srm(const u8 *buf, size_t count,
  69. u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
  70. {
  71. struct hdcp_srm_header *header;
  72. u32 vrl_length, ksv_count;
  73. if (count < (sizeof(struct hdcp_srm_header) +
  74. DRM_HDCP_1_4_VRL_LENGTH_SIZE + DRM_HDCP_1_4_DCP_SIG_SIZE)) {
  75. DRM_ERROR("Invalid blob length\n");
  76. return -EINVAL;
  77. }
  78. header = (struct hdcp_srm_header *)buf;
  79. DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
  80. header->srm_id,
  81. be16_to_cpu(header->srm_version), header->srm_gen_no);
  82. WARN_ON(header->reserved);
  83. buf = buf + sizeof(*header);
  84. vrl_length = get_vrl_length(buf);
  85. if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
  86. vrl_length < (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
  87. DRM_HDCP_1_4_DCP_SIG_SIZE)) {
  88. DRM_ERROR("Invalid blob length or vrl length\n");
  89. return -EINVAL;
  90. }
  91. /* Length of the all vrls combined */
  92. vrl_length -= (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
  93. DRM_HDCP_1_4_DCP_SIG_SIZE);
  94. if (!vrl_length) {
  95. DRM_ERROR("No vrl found\n");
  96. return -EINVAL;
  97. }
  98. buf += DRM_HDCP_1_4_VRL_LENGTH_SIZE;
  99. ksv_count = drm_hdcp_get_revoked_ksv_count(buf, vrl_length);
  100. if (!ksv_count) {
  101. DRM_DEBUG("Revoked KSV count is 0\n");
  102. return 0;
  103. }
  104. *revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN, GFP_KERNEL);
  105. if (!*revoked_ksv_list) {
  106. DRM_ERROR("Out of Memory\n");
  107. return -ENOMEM;
  108. }
  109. if (drm_hdcp_get_revoked_ksvs(buf, revoked_ksv_list,
  110. vrl_length) != ksv_count) {
  111. *revoked_ksv_cnt = 0;
  112. kfree(*revoked_ksv_list);
  113. return -EINVAL;
  114. }
  115. *revoked_ksv_cnt = ksv_count;
  116. return 0;
  117. }
  118. static int drm_hdcp_parse_hdcp2_srm(const u8 *buf, size_t count,
  119. u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
  120. {
  121. struct hdcp_srm_header *header;
  122. u32 vrl_length, ksv_count, ksv_sz;
  123. if (count < (sizeof(struct hdcp_srm_header) +
  124. DRM_HDCP_2_VRL_LENGTH_SIZE + DRM_HDCP_2_DCP_SIG_SIZE)) {
  125. DRM_ERROR("Invalid blob length\n");
  126. return -EINVAL;
  127. }
  128. header = (struct hdcp_srm_header *)buf;
  129. DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
  130. header->srm_id & DRM_HDCP_SRM_ID_MASK,
  131. be16_to_cpu(header->srm_version), header->srm_gen_no);
  132. if (header->reserved)
  133. return -EINVAL;
  134. buf = buf + sizeof(*header);
  135. vrl_length = get_vrl_length(buf);
  136. if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
  137. vrl_length < (DRM_HDCP_2_VRL_LENGTH_SIZE +
  138. DRM_HDCP_2_DCP_SIG_SIZE)) {
  139. DRM_ERROR("Invalid blob length or vrl length\n");
  140. return -EINVAL;
  141. }
  142. /* Length of the all vrls combined */
  143. vrl_length -= (DRM_HDCP_2_VRL_LENGTH_SIZE +
  144. DRM_HDCP_2_DCP_SIG_SIZE);
  145. if (!vrl_length) {
  146. DRM_ERROR("No vrl found\n");
  147. return -EINVAL;
  148. }
  149. buf += DRM_HDCP_2_VRL_LENGTH_SIZE;
  150. ksv_count = (*buf << 2) | DRM_HDCP_2_KSV_COUNT_2_LSBITS(*(buf + 1));
  151. if (!ksv_count) {
  152. DRM_DEBUG("Revoked KSV count is 0\n");
  153. return 0;
  154. }
  155. *revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN, GFP_KERNEL);
  156. if (!*revoked_ksv_list) {
  157. DRM_ERROR("Out of Memory\n");
  158. return -ENOMEM;
  159. }
  160. ksv_sz = ksv_count * DRM_HDCP_KSV_LEN;
  161. buf += DRM_HDCP_2_NO_OF_DEV_PLUS_RESERVED_SZ;
  162. DRM_DEBUG("Revoked KSVs: %d\n", ksv_count);
  163. memcpy(*revoked_ksv_list, buf, ksv_sz);
  164. *revoked_ksv_cnt = ksv_count;
  165. return 0;
  166. }
  167. static inline bool is_srm_version_hdcp1(const u8 *buf)
  168. {
  169. return *buf == (u8)(DRM_HDCP_1_4_SRM_ID << 4);
  170. }
  171. static inline bool is_srm_version_hdcp2(const u8 *buf)
  172. {
  173. return *buf == (u8)(DRM_HDCP_2_SRM_ID << 4 | DRM_HDCP_2_INDICATOR);
  174. }
  175. static int drm_hdcp_srm_update(const u8 *buf, size_t count,
  176. u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
  177. {
  178. if (count < sizeof(struct hdcp_srm_header))
  179. return -EINVAL;
  180. if (is_srm_version_hdcp1(buf))
  181. return drm_hdcp_parse_hdcp1_srm(buf, count, revoked_ksv_list,
  182. revoked_ksv_cnt);
  183. else if (is_srm_version_hdcp2(buf))
  184. return drm_hdcp_parse_hdcp2_srm(buf, count, revoked_ksv_list,
  185. revoked_ksv_cnt);
  186. else
  187. return -EINVAL;
  188. }
  189. static int drm_hdcp_request_srm(struct drm_device *drm_dev,
  190. u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
  191. {
  192. char fw_name[36] = "display_hdcp_srm.bin";
  193. const struct firmware *fw;
  194. int ret;
  195. ret = request_firmware_direct(&fw, (const char *)fw_name,
  196. drm_dev->dev);
  197. if (ret < 0) {
  198. *revoked_ksv_cnt = 0;
  199. *revoked_ksv_list = NULL;
  200. ret = 0;
  201. goto exit;
  202. }
  203. if (fw->size && fw->data)
  204. ret = drm_hdcp_srm_update(fw->data, fw->size, revoked_ksv_list,
  205. revoked_ksv_cnt);
  206. exit:
  207. release_firmware(fw);
  208. return ret;
  209. }
  210. /**
  211. * drm_hdcp_check_ksvs_revoked - Check the revoked status of the IDs
  212. *
  213. * @drm_dev: drm_device for which HDCP revocation check is requested
  214. * @ksvs: List of KSVs (HDCP receiver IDs)
  215. * @ksv_count: KSV count passed in through @ksvs
  216. *
  217. * This function reads the HDCP System renewability Message(SRM Table)
  218. * from userspace as a firmware and parses it for the revoked HDCP
  219. * KSVs(Receiver IDs) detected by DCP LLC. Once the revoked KSVs are known,
  220. * revoked state of the KSVs in the list passed in by display drivers are
  221. * decided and response is sent.
  222. *
  223. * SRM should be presented in the name of "display_hdcp_srm.bin".
  224. *
  225. * Format of the SRM table, that userspace needs to write into the binary file,
  226. * is defined at:
  227. * 1. Renewability chapter on 55th page of HDCP 1.4 specification
  228. * https://www.digital-cp.com/sites/default/files/specifications/HDCP%20Specification%20Rev1_4_Secure.pdf
  229. * 2. Renewability chapter on 63rd page of HDCP 2.2 specification
  230. * https://www.digital-cp.com/sites/default/files/specifications/HDCP%20on%20HDMI%20Specification%20Rev2_2_Final1.pdf
  231. *
  232. * Returns:
  233. * Count of the revoked KSVs or -ve error number incase of the failure.
  234. */
  235. int drm_hdcp_check_ksvs_revoked(struct drm_device *drm_dev, u8 *ksvs,
  236. u32 ksv_count)
  237. {
  238. u32 revoked_ksv_cnt = 0, i, j;
  239. u8 *revoked_ksv_list = NULL;
  240. int ret = 0;
  241. ret = drm_hdcp_request_srm(drm_dev, &revoked_ksv_list,
  242. &revoked_ksv_cnt);
  243. if (ret)
  244. return ret;
  245. /* revoked_ksv_cnt will be zero when above function failed */
  246. for (i = 0; i < revoked_ksv_cnt; i++)
  247. for (j = 0; j < ksv_count; j++)
  248. if (!memcmp(&ksvs[j * DRM_HDCP_KSV_LEN],
  249. &revoked_ksv_list[i * DRM_HDCP_KSV_LEN],
  250. DRM_HDCP_KSV_LEN)) {
  251. DRM_DEBUG("Revoked KSV is ");
  252. drm_hdcp_print_ksv(&ksvs[j * DRM_HDCP_KSV_LEN]);
  253. ret++;
  254. }
  255. kfree(revoked_ksv_list);
  256. return ret;
  257. }
  258. EXPORT_SYMBOL_GPL(drm_hdcp_check_ksvs_revoked);
  259. static struct drm_prop_enum_list drm_cp_enum_list[] = {
  260. { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
  261. { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
  262. { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
  263. };
  264. DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
  265. static struct drm_prop_enum_list drm_hdcp_content_type_enum_list[] = {
  266. { DRM_MODE_HDCP_CONTENT_TYPE0, "HDCP Type0" },
  267. { DRM_MODE_HDCP_CONTENT_TYPE1, "HDCP Type1" },
  268. };
  269. DRM_ENUM_NAME_FN(drm_get_hdcp_content_type_name,
  270. drm_hdcp_content_type_enum_list)
  271. /**
  272. * drm_connector_attach_content_protection_property - attach content protection
  273. * property
  274. *
  275. * @connector: connector to attach CP property on.
  276. * @hdcp_content_type: is HDCP Content Type property needed for connector
  277. *
  278. * This is used to add support for content protection on select connectors.
  279. * Content Protection is intentionally vague to allow for different underlying
  280. * technologies, however it is most implemented by HDCP.
  281. *
  282. * When hdcp_content_type is true enum property called HDCP Content Type is
  283. * created (if it is not already) and attached to the connector.
  284. *
  285. * This property is used for sending the protected content's stream type
  286. * from userspace to kernel on selected connectors. Protected content provider
  287. * will decide their type of their content and declare the same to kernel.
  288. *
  289. * Content type will be used during the HDCP 2.2 authentication.
  290. * Content type will be set to &drm_connector_state.hdcp_content_type.
  291. *
  292. * The content protection will be set to &drm_connector_state.content_protection
  293. *
  294. * When kernel triggered content protection state change like DESIRED->ENABLED
  295. * and ENABLED->DESIRED, will use drm_hdcp_update_content_protection() to update
  296. * the content protection state of a connector.
  297. *
  298. * Returns:
  299. * Zero on success, negative errno on failure.
  300. */
  301. int drm_connector_attach_content_protection_property(
  302. struct drm_connector *connector, bool hdcp_content_type)
  303. {
  304. struct drm_device *dev = connector->dev;
  305. struct drm_property *prop =
  306. dev->mode_config.content_protection_property;
  307. if (!prop)
  308. prop = drm_property_create_enum(dev, 0, "Content Protection",
  309. drm_cp_enum_list,
  310. ARRAY_SIZE(drm_cp_enum_list));
  311. if (!prop)
  312. return -ENOMEM;
  313. drm_object_attach_property(&connector->base, prop,
  314. DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
  315. dev->mode_config.content_protection_property = prop;
  316. if (!hdcp_content_type)
  317. return 0;
  318. prop = dev->mode_config.hdcp_content_type_property;
  319. if (!prop)
  320. prop = drm_property_create_enum(dev, 0, "HDCP Content Type",
  321. drm_hdcp_content_type_enum_list,
  322. ARRAY_SIZE(
  323. drm_hdcp_content_type_enum_list));
  324. if (!prop)
  325. return -ENOMEM;
  326. drm_object_attach_property(&connector->base, prop,
  327. DRM_MODE_HDCP_CONTENT_TYPE0);
  328. dev->mode_config.hdcp_content_type_property = prop;
  329. return 0;
  330. }
  331. EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
  332. /**
  333. * drm_hdcp_update_content_protection - Updates the content protection state
  334. * of a connector
  335. *
  336. * @connector: drm_connector on which content protection state needs an update
  337. * @val: New state of the content protection property
  338. *
  339. * This function can be used by display drivers, to update the kernel triggered
  340. * content protection state changes of a drm_connector such as DESIRED->ENABLED
  341. * and ENABLED->DESIRED. No uevent for DESIRED->UNDESIRED or ENABLED->UNDESIRED,
  342. * as userspace is triggering such state change and kernel performs it without
  343. * fail.This function update the new state of the property into the connector's
  344. * state and generate an uevent to notify the userspace.
  345. */
  346. void drm_hdcp_update_content_protection(struct drm_connector *connector,
  347. u64 val)
  348. {
  349. struct drm_device *dev = connector->dev;
  350. struct drm_connector_state *state = connector->state;
  351. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  352. if (state->content_protection == val)
  353. return;
  354. state->content_protection = val;
  355. drm_sysfs_connector_status_event(connector,
  356. dev->mode_config.content_protection_property);
  357. }
  358. EXPORT_SYMBOL(drm_hdcp_update_content_protection);