1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Intel Corporation.
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gfp.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/firmware.h>
16 #include <drm/drm_hdcp.h>
17 #include <drm/drm_sysfs.h>
18 #include <drm/drm_print.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_property.h>
21 #include <drm/drm_mode_object.h>
22 #include <drm/drm_connector.h>
24 #include "drm_internal.h"
26 static struct hdcp_srm {
30 /* Mutex to protect above struct member */
34 static inline void drm_hdcp_print_ksv(const u8 *ksv)
36 DRM_DEBUG("\t%#02x, %#02x, %#02x, %#02x, %#02x\n",
37 ksv[0], ksv[1], ksv[2], ksv[3], ksv[4]);
40 static u32 drm_hdcp_get_revoked_ksv_count(const u8 *buf, u32 vrls_length)
42 u32 parsed_bytes = 0, ksv_count = 0, vrl_ksv_cnt, vrl_sz;
44 while (parsed_bytes < vrls_length) {
46 ksv_count += vrl_ksv_cnt;
48 vrl_sz = (vrl_ksv_cnt * DRM_HDCP_KSV_LEN) + 1;
50 parsed_bytes += vrl_sz;
54 * When vrls are not valid, ksvs are not considered.
55 * Hence SRM will be discarded.
57 if (parsed_bytes != vrls_length)
63 static u32 drm_hdcp_get_revoked_ksvs(const u8 *buf, u8 *revoked_ksv_list,
66 u32 parsed_bytes = 0, ksv_count = 0;
67 u32 vrl_ksv_cnt, vrl_ksv_sz, vrl_idx = 0;
71 vrl_ksv_sz = vrl_ksv_cnt * DRM_HDCP_KSV_LEN;
75 DRM_DEBUG("vrl: %d, Revoked KSVs: %d\n", vrl_idx++,
77 memcpy(revoked_ksv_list, buf, vrl_ksv_sz);
79 ksv_count += vrl_ksv_cnt;
80 revoked_ksv_list += vrl_ksv_sz;
83 parsed_bytes += (vrl_ksv_sz + 1);
84 } while (parsed_bytes < vrls_length);
89 static inline u32 get_vrl_length(const u8 *buf)
91 return drm_hdcp_be24_to_cpu(buf);
94 static int drm_hdcp_parse_hdcp1_srm(const u8 *buf, size_t count)
96 struct hdcp_srm_header *header;
97 u32 vrl_length, ksv_count;
99 if (count < (sizeof(struct hdcp_srm_header) +
100 DRM_HDCP_1_4_VRL_LENGTH_SIZE + DRM_HDCP_1_4_DCP_SIG_SIZE)) {
101 DRM_ERROR("Invalid blob length\n");
105 header = (struct hdcp_srm_header *)buf;
106 DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
108 be16_to_cpu(header->srm_version), header->srm_gen_no);
110 WARN_ON(header->reserved);
112 buf = buf + sizeof(*header);
113 vrl_length = get_vrl_length(buf);
114 if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
115 vrl_length < (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
116 DRM_HDCP_1_4_DCP_SIG_SIZE)) {
117 DRM_ERROR("Invalid blob length or vrl length\n");
121 /* Length of the all vrls combined */
122 vrl_length -= (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
123 DRM_HDCP_1_4_DCP_SIG_SIZE);
126 DRM_ERROR("No vrl found\n");
130 buf += DRM_HDCP_1_4_VRL_LENGTH_SIZE;
131 ksv_count = drm_hdcp_get_revoked_ksv_count(buf, vrl_length);
133 DRM_DEBUG("Revoked KSV count is 0\n");
137 kfree(srm_data->revoked_ksv_list);
138 srm_data->revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN,
140 if (!srm_data->revoked_ksv_list) {
141 DRM_ERROR("Out of Memory\n");
145 if (drm_hdcp_get_revoked_ksvs(buf, srm_data->revoked_ksv_list,
146 vrl_length) != ksv_count) {
147 srm_data->revoked_ksv_cnt = 0;
148 kfree(srm_data->revoked_ksv_list);
152 srm_data->revoked_ksv_cnt = ksv_count;
156 static int drm_hdcp_parse_hdcp2_srm(const u8 *buf, size_t count)
158 struct hdcp_srm_header *header;
159 u32 vrl_length, ksv_count, ksv_sz;
161 if (count < (sizeof(struct hdcp_srm_header) +
162 DRM_HDCP_2_VRL_LENGTH_SIZE + DRM_HDCP_2_DCP_SIG_SIZE)) {
163 DRM_ERROR("Invalid blob length\n");
167 header = (struct hdcp_srm_header *)buf;
168 DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
169 header->srm_id & DRM_HDCP_SRM_ID_MASK,
170 be16_to_cpu(header->srm_version), header->srm_gen_no);
172 if (header->reserved)
175 buf = buf + sizeof(*header);
176 vrl_length = get_vrl_length(buf);
178 if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
179 vrl_length < (DRM_HDCP_2_VRL_LENGTH_SIZE +
180 DRM_HDCP_2_DCP_SIG_SIZE)) {
181 DRM_ERROR("Invalid blob length or vrl length\n");
185 /* Length of the all vrls combined */
186 vrl_length -= (DRM_HDCP_2_VRL_LENGTH_SIZE +
187 DRM_HDCP_2_DCP_SIG_SIZE);
190 DRM_ERROR("No vrl found\n");
194 buf += DRM_HDCP_2_VRL_LENGTH_SIZE;
195 ksv_count = (*buf << 2) | DRM_HDCP_2_KSV_COUNT_2_LSBITS(*(buf + 1));
197 DRM_DEBUG("Revoked KSV count is 0\n");
201 kfree(srm_data->revoked_ksv_list);
202 srm_data->revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN,
204 if (!srm_data->revoked_ksv_list) {
205 DRM_ERROR("Out of Memory\n");
209 ksv_sz = ksv_count * DRM_HDCP_KSV_LEN;
210 buf += DRM_HDCP_2_NO_OF_DEV_PLUS_RESERVED_SZ;
212 DRM_DEBUG("Revoked KSVs: %d\n", ksv_count);
213 memcpy(srm_data->revoked_ksv_list, buf, ksv_sz);
215 srm_data->revoked_ksv_cnt = ksv_count;
219 static inline bool is_srm_version_hdcp1(const u8 *buf)
221 return *buf == (u8)(DRM_HDCP_1_4_SRM_ID << 4);
224 static inline bool is_srm_version_hdcp2(const u8 *buf)
226 return *buf == (u8)(DRM_HDCP_2_SRM_ID << 4 | DRM_HDCP_2_INDICATOR);
229 static void drm_hdcp_srm_update(const u8 *buf, size_t count)
231 if (count < sizeof(struct hdcp_srm_header))
234 if (is_srm_version_hdcp1(buf))
235 drm_hdcp_parse_hdcp1_srm(buf, count);
236 else if (is_srm_version_hdcp2(buf))
237 drm_hdcp_parse_hdcp2_srm(buf, count);
240 static void drm_hdcp_request_srm(struct drm_device *drm_dev)
242 char fw_name[36] = "display_hdcp_srm.bin";
243 const struct firmware *fw;
247 ret = request_firmware_direct(&fw, (const char *)fw_name,
252 if (fw->size && fw->data)
253 drm_hdcp_srm_update(fw->data, fw->size);
256 release_firmware(fw);
260 * drm_hdcp_check_ksvs_revoked - Check the revoked status of the IDs
262 * @drm_dev: drm_device for which HDCP revocation check is requested
263 * @ksvs: List of KSVs (HDCP receiver IDs)
264 * @ksv_count: KSV count passed in through @ksvs
266 * This function reads the HDCP System renewability Message(SRM Table)
267 * from userspace as a firmware and parses it for the revoked HDCP
268 * KSVs(Receiver IDs) detected by DCP LLC. Once the revoked KSVs are known,
269 * revoked state of the KSVs in the list passed in by display drivers are
270 * decided and response is sent.
272 * SRM should be presented in the name of "display_hdcp_srm.bin".
275 * TRUE on any of the KSV is revoked, else FALSE.
277 bool drm_hdcp_check_ksvs_revoked(struct drm_device *drm_dev, u8 *ksvs,
280 u32 rev_ksv_cnt, cnt, i, j;
286 mutex_lock(&srm_data->mutex);
287 drm_hdcp_request_srm(drm_dev);
289 rev_ksv_cnt = srm_data->revoked_ksv_cnt;
290 rev_ksv_list = srm_data->revoked_ksv_list;
292 /* If the Revoked ksv list is empty */
293 if (!rev_ksv_cnt || !rev_ksv_list) {
294 mutex_unlock(&srm_data->mutex);
298 for (cnt = 0; cnt < ksv_count; cnt++) {
299 rev_ksv_list = srm_data->revoked_ksv_list;
300 for (i = 0; i < rev_ksv_cnt; i++) {
301 for (j = 0; j < DRM_HDCP_KSV_LEN; j++)
302 if (ksvs[j] != rev_ksv_list[j]) {
304 } else if (j == (DRM_HDCP_KSV_LEN - 1)) {
305 DRM_DEBUG("Revoked KSV is ");
306 drm_hdcp_print_ksv(ksvs);
307 mutex_unlock(&srm_data->mutex);
310 /* Move the offset to next KSV in the revoked list */
311 rev_ksv_list += DRM_HDCP_KSV_LEN;
314 /* Iterate to next ksv_offset */
315 ksvs += DRM_HDCP_KSV_LEN;
317 mutex_unlock(&srm_data->mutex);
320 EXPORT_SYMBOL_GPL(drm_hdcp_check_ksvs_revoked);
322 int drm_setup_hdcp_srm(struct class *drm_class)
324 srm_data = kzalloc(sizeof(*srm_data), GFP_KERNEL);
327 mutex_init(&srm_data->mutex);
332 void drm_teardown_hdcp_srm(struct class *drm_class)
335 kfree(srm_data->revoked_ksv_list);
340 static struct drm_prop_enum_list drm_cp_enum_list[] = {
341 { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
342 { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
343 { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
345 DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
348 * drm_connector_attach_content_protection_property - attach content protection
351 * @connector: connector to attach CP property on.
353 * This is used to add support for content protection on select connectors.
354 * Content Protection is intentionally vague to allow for different underlying
355 * technologies, however it is most implemented by HDCP.
357 * The content protection will be set to &drm_connector_state.content_protection
360 * Zero on success, negative errno on failure.
362 int drm_connector_attach_content_protection_property(
363 struct drm_connector *connector)
365 struct drm_device *dev = connector->dev;
366 struct drm_property *prop =
367 dev->mode_config.content_protection_property;
370 prop = drm_property_create_enum(dev, 0, "Content Protection",
372 ARRAY_SIZE(drm_cp_enum_list));
376 drm_object_attach_property(&connector->base, prop,
377 DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
378 dev->mode_config.content_protection_property = prop;
382 EXPORT_SYMBOL(drm_connector_attach_content_protection_property);