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/display/drm_hdcp_helper.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 static inline void drm_hdcp_print_ksv(const u8 *ksv)
26 DRM_DEBUG("\t%#02x, %#02x, %#02x, %#02x, %#02x\n",
27 ksv[0], ksv[1], ksv[2], ksv[3], ksv[4]);
30 static u32 drm_hdcp_get_revoked_ksv_count(const u8 *buf, u32 vrls_length)
32 u32 parsed_bytes = 0, ksv_count = 0, vrl_ksv_cnt, vrl_sz;
34 while (parsed_bytes < vrls_length) {
36 ksv_count += vrl_ksv_cnt;
38 vrl_sz = (vrl_ksv_cnt * DRM_HDCP_KSV_LEN) + 1;
40 parsed_bytes += vrl_sz;
44 * When vrls are not valid, ksvs are not considered.
45 * Hence SRM will be discarded.
47 if (parsed_bytes != vrls_length)
53 static u32 drm_hdcp_get_revoked_ksvs(const u8 *buf, u8 **revoked_ksv_list,
56 u32 vrl_ksv_cnt, vrl_ksv_sz, vrl_idx = 0;
57 u32 parsed_bytes = 0, ksv_count = 0;
61 vrl_ksv_sz = vrl_ksv_cnt * DRM_HDCP_KSV_LEN;
65 DRM_DEBUG("vrl: %d, Revoked KSVs: %d\n", vrl_idx++,
67 memcpy((*revoked_ksv_list) + (ksv_count * DRM_HDCP_KSV_LEN),
70 ksv_count += vrl_ksv_cnt;
73 parsed_bytes += (vrl_ksv_sz + 1);
74 } while (parsed_bytes < vrls_length);
79 static inline u32 get_vrl_length(const u8 *buf)
81 return drm_hdcp_be24_to_cpu(buf);
84 static int drm_hdcp_parse_hdcp1_srm(const u8 *buf, size_t count,
85 u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
87 struct hdcp_srm_header *header;
88 u32 vrl_length, ksv_count;
90 if (count < (sizeof(struct hdcp_srm_header) +
91 DRM_HDCP_1_4_VRL_LENGTH_SIZE + DRM_HDCP_1_4_DCP_SIG_SIZE)) {
92 DRM_ERROR("Invalid blob length\n");
96 header = (struct hdcp_srm_header *)buf;
97 DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
99 be16_to_cpu(header->srm_version), header->srm_gen_no);
101 WARN_ON(header->reserved);
103 buf = buf + sizeof(*header);
104 vrl_length = get_vrl_length(buf);
105 if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
106 vrl_length < (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
107 DRM_HDCP_1_4_DCP_SIG_SIZE)) {
108 DRM_ERROR("Invalid blob length or vrl length\n");
112 /* Length of the all vrls combined */
113 vrl_length -= (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
114 DRM_HDCP_1_4_DCP_SIG_SIZE);
117 DRM_ERROR("No vrl found\n");
121 buf += DRM_HDCP_1_4_VRL_LENGTH_SIZE;
122 ksv_count = drm_hdcp_get_revoked_ksv_count(buf, vrl_length);
124 DRM_DEBUG("Revoked KSV count is 0\n");
128 *revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN, GFP_KERNEL);
129 if (!*revoked_ksv_list) {
130 DRM_ERROR("Out of Memory\n");
134 if (drm_hdcp_get_revoked_ksvs(buf, revoked_ksv_list,
135 vrl_length) != ksv_count) {
136 *revoked_ksv_cnt = 0;
137 kfree(*revoked_ksv_list);
141 *revoked_ksv_cnt = ksv_count;
145 static int drm_hdcp_parse_hdcp2_srm(const u8 *buf, size_t count,
146 u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
148 struct hdcp_srm_header *header;
149 u32 vrl_length, ksv_count, ksv_sz;
151 if (count < (sizeof(struct hdcp_srm_header) +
152 DRM_HDCP_2_VRL_LENGTH_SIZE + DRM_HDCP_2_DCP_SIG_SIZE)) {
153 DRM_ERROR("Invalid blob length\n");
157 header = (struct hdcp_srm_header *)buf;
158 DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
159 header->srm_id & DRM_HDCP_SRM_ID_MASK,
160 be16_to_cpu(header->srm_version), header->srm_gen_no);
162 if (header->reserved)
165 buf = buf + sizeof(*header);
166 vrl_length = get_vrl_length(buf);
168 if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
169 vrl_length < (DRM_HDCP_2_VRL_LENGTH_SIZE +
170 DRM_HDCP_2_DCP_SIG_SIZE)) {
171 DRM_ERROR("Invalid blob length or vrl length\n");
175 /* Length of the all vrls combined */
176 vrl_length -= (DRM_HDCP_2_VRL_LENGTH_SIZE +
177 DRM_HDCP_2_DCP_SIG_SIZE);
180 DRM_ERROR("No vrl found\n");
184 buf += DRM_HDCP_2_VRL_LENGTH_SIZE;
185 ksv_count = (*buf << 2) | DRM_HDCP_2_KSV_COUNT_2_LSBITS(*(buf + 1));
187 DRM_DEBUG("Revoked KSV count is 0\n");
191 *revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN, GFP_KERNEL);
192 if (!*revoked_ksv_list) {
193 DRM_ERROR("Out of Memory\n");
197 ksv_sz = ksv_count * DRM_HDCP_KSV_LEN;
198 buf += DRM_HDCP_2_NO_OF_DEV_PLUS_RESERVED_SZ;
200 DRM_DEBUG("Revoked KSVs: %d\n", ksv_count);
201 memcpy(*revoked_ksv_list, buf, ksv_sz);
203 *revoked_ksv_cnt = ksv_count;
207 static inline bool is_srm_version_hdcp1(const u8 *buf)
209 return *buf == (u8)(DRM_HDCP_1_4_SRM_ID << 4);
212 static inline bool is_srm_version_hdcp2(const u8 *buf)
214 return *buf == (u8)(DRM_HDCP_2_SRM_ID << 4 | DRM_HDCP_2_INDICATOR);
217 static int drm_hdcp_srm_update(const u8 *buf, size_t count,
218 u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
220 if (count < sizeof(struct hdcp_srm_header))
223 if (is_srm_version_hdcp1(buf))
224 return drm_hdcp_parse_hdcp1_srm(buf, count, revoked_ksv_list,
226 else if (is_srm_version_hdcp2(buf))
227 return drm_hdcp_parse_hdcp2_srm(buf, count, revoked_ksv_list,
233 static int drm_hdcp_request_srm(struct drm_device *drm_dev,
234 u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)
236 char fw_name[36] = "display_hdcp_srm.bin";
237 const struct firmware *fw;
240 ret = request_firmware_direct(&fw, (const char *)fw_name,
243 *revoked_ksv_cnt = 0;
244 *revoked_ksv_list = NULL;
249 if (fw->size && fw->data)
250 ret = drm_hdcp_srm_update(fw->data, fw->size, revoked_ksv_list,
254 release_firmware(fw);
259 * drm_hdcp_check_ksvs_revoked - Check the revoked status of the IDs
261 * @drm_dev: drm_device for which HDCP revocation check is requested
262 * @ksvs: List of KSVs (HDCP receiver IDs)
263 * @ksv_count: KSV count passed in through @ksvs
265 * This function reads the HDCP System renewability Message(SRM Table)
266 * from userspace as a firmware and parses it for the revoked HDCP
267 * KSVs(Receiver IDs) detected by DCP LLC. Once the revoked KSVs are known,
268 * revoked state of the KSVs in the list passed in by display drivers are
269 * decided and response is sent.
271 * SRM should be presented in the name of "display_hdcp_srm.bin".
273 * Format of the SRM table, that userspace needs to write into the binary file,
275 * 1. Renewability chapter on 55th page of HDCP 1.4 specification
276 * https://www.digital-cp.com/sites/default/files/specifications/HDCP%20Specification%20Rev1_4_Secure.pdf
277 * 2. Renewability chapter on 63rd page of HDCP 2.2 specification
278 * https://www.digital-cp.com/sites/default/files/specifications/HDCP%20on%20HDMI%20Specification%20Rev2_2_Final1.pdf
281 * Count of the revoked KSVs or -ve error number in case of the failure.
283 int drm_hdcp_check_ksvs_revoked(struct drm_device *drm_dev, u8 *ksvs,
286 u32 revoked_ksv_cnt = 0, i, j;
287 u8 *revoked_ksv_list = NULL;
290 ret = drm_hdcp_request_srm(drm_dev, &revoked_ksv_list,
295 /* revoked_ksv_cnt will be zero when above function failed */
296 for (i = 0; i < revoked_ksv_cnt; i++)
297 for (j = 0; j < ksv_count; j++)
298 if (!memcmp(&ksvs[j * DRM_HDCP_KSV_LEN],
299 &revoked_ksv_list[i * DRM_HDCP_KSV_LEN],
301 DRM_DEBUG("Revoked KSV is ");
302 drm_hdcp_print_ksv(&ksvs[j * DRM_HDCP_KSV_LEN]);
306 kfree(revoked_ksv_list);
309 EXPORT_SYMBOL_GPL(drm_hdcp_check_ksvs_revoked);
311 static struct drm_prop_enum_list drm_cp_enum_list[] = {
312 { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
313 { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
314 { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
316 DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
318 static struct drm_prop_enum_list drm_hdcp_content_type_enum_list[] = {
319 { DRM_MODE_HDCP_CONTENT_TYPE0, "HDCP Type0" },
320 { DRM_MODE_HDCP_CONTENT_TYPE1, "HDCP Type1" },
322 DRM_ENUM_NAME_FN(drm_get_hdcp_content_type_name,
323 drm_hdcp_content_type_enum_list)
326 * drm_connector_attach_content_protection_property - attach content protection
329 * @connector: connector to attach CP property on.
330 * @hdcp_content_type: is HDCP Content Type property needed for connector
332 * This is used to add support for content protection on select connectors.
333 * Content Protection is intentionally vague to allow for different underlying
334 * technologies, however it is most implemented by HDCP.
336 * When hdcp_content_type is true enum property called HDCP Content Type is
337 * created (if it is not already) and attached to the connector.
339 * This property is used for sending the protected content's stream type
340 * from userspace to kernel on selected connectors. Protected content provider
341 * will decide their type of their content and declare the same to kernel.
343 * Content type will be used during the HDCP 2.2 authentication.
344 * Content type will be set to &drm_connector_state.hdcp_content_type.
346 * The content protection will be set to &drm_connector_state.content_protection
348 * When kernel triggered content protection state change like DESIRED->ENABLED
349 * and ENABLED->DESIRED, will use drm_hdcp_update_content_protection() to update
350 * the content protection state of a connector.
353 * Zero on success, negative errno on failure.
355 int drm_connector_attach_content_protection_property(
356 struct drm_connector *connector, bool hdcp_content_type)
358 struct drm_device *dev = connector->dev;
359 struct drm_property *prop =
360 dev->mode_config.content_protection_property;
363 prop = drm_property_create_enum(dev, 0, "Content Protection",
365 ARRAY_SIZE(drm_cp_enum_list));
369 drm_object_attach_property(&connector->base, prop,
370 DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
371 dev->mode_config.content_protection_property = prop;
373 if (!hdcp_content_type)
376 prop = dev->mode_config.hdcp_content_type_property;
378 prop = drm_property_create_enum(dev, 0, "HDCP Content Type",
379 drm_hdcp_content_type_enum_list,
381 drm_hdcp_content_type_enum_list));
385 drm_object_attach_property(&connector->base, prop,
386 DRM_MODE_HDCP_CONTENT_TYPE0);
387 dev->mode_config.hdcp_content_type_property = prop;
391 EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
394 * drm_hdcp_update_content_protection - Updates the content protection state
397 * @connector: drm_connector on which content protection state needs an update
398 * @val: New state of the content protection property
400 * This function can be used by display drivers, to update the kernel triggered
401 * content protection state changes of a drm_connector such as DESIRED->ENABLED
402 * and ENABLED->DESIRED. No uevent for DESIRED->UNDESIRED or ENABLED->UNDESIRED,
403 * as userspace is triggering such state change and kernel performs it without
404 * fail.This function update the new state of the property into the connector's
405 * state and generate an uevent to notify the userspace.
407 void drm_hdcp_update_content_protection(struct drm_connector *connector,
410 struct drm_device *dev = connector->dev;
411 struct drm_connector_state *state = connector->state;
413 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
414 if (state->content_protection == val)
417 state->content_protection = val;
418 drm_sysfs_connector_property_event(connector,
419 dev->mode_config.content_protection_property);
421 EXPORT_SYMBOL(drm_hdcp_update_content_protection);