Path: blob/master/drivers/gpu/drm/display/drm_hdcp_helper.c
26494 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2019 Intel Corporation.3*4* Authors:5* Ramalingam C <[email protected]>6*/78#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>1415#include <drm/display/drm_hdcp_helper.h>16#include <drm/drm_sysfs.h>17#include <drm/drm_print.h>18#include <drm/drm_device.h>19#include <drm/drm_property.h>20#include <drm/drm_mode_object.h>21#include <drm/drm_connector.h>2223static inline void drm_hdcp_print_ksv(const u8 *ksv)24{25DRM_DEBUG("\t%#02x, %#02x, %#02x, %#02x, %#02x\n",26ksv[0], ksv[1], ksv[2], ksv[3], ksv[4]);27}2829static u32 drm_hdcp_get_revoked_ksv_count(const u8 *buf, u32 vrls_length)30{31u32 parsed_bytes = 0, ksv_count = 0, vrl_ksv_cnt, vrl_sz;3233while (parsed_bytes < vrls_length) {34vrl_ksv_cnt = *buf;35ksv_count += vrl_ksv_cnt;3637vrl_sz = (vrl_ksv_cnt * DRM_HDCP_KSV_LEN) + 1;38buf += vrl_sz;39parsed_bytes += vrl_sz;40}4142/*43* When vrls are not valid, ksvs are not considered.44* Hence SRM will be discarded.45*/46if (parsed_bytes != vrls_length)47ksv_count = 0;4849return ksv_count;50}5152static u32 drm_hdcp_get_revoked_ksvs(const u8 *buf, u8 **revoked_ksv_list,53u32 vrls_length)54{55u32 vrl_ksv_cnt, vrl_ksv_sz, vrl_idx = 0;56u32 parsed_bytes = 0, ksv_count = 0;5758do {59vrl_ksv_cnt = *buf;60vrl_ksv_sz = vrl_ksv_cnt * DRM_HDCP_KSV_LEN;6162buf++;6364DRM_DEBUG("vrl: %d, Revoked KSVs: %d\n", vrl_idx++,65vrl_ksv_cnt);66memcpy((*revoked_ksv_list) + (ksv_count * DRM_HDCP_KSV_LEN),67buf, vrl_ksv_sz);6869ksv_count += vrl_ksv_cnt;70buf += vrl_ksv_sz;7172parsed_bytes += (vrl_ksv_sz + 1);73} while (parsed_bytes < vrls_length);7475return ksv_count;76}7778static inline u32 get_vrl_length(const u8 *buf)79{80return drm_hdcp_be24_to_cpu(buf);81}8283static int drm_hdcp_parse_hdcp1_srm(const u8 *buf, size_t count,84u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)85{86struct hdcp_srm_header *header;87u32 vrl_length, ksv_count;8889if (count < (sizeof(struct hdcp_srm_header) +90DRM_HDCP_1_4_VRL_LENGTH_SIZE + DRM_HDCP_1_4_DCP_SIG_SIZE)) {91DRM_ERROR("Invalid blob length\n");92return -EINVAL;93}9495header = (struct hdcp_srm_header *)buf;96DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",97header->srm_id,98be16_to_cpu(header->srm_version), header->srm_gen_no);99100WARN_ON(header->reserved);101102buf = buf + sizeof(*header);103vrl_length = get_vrl_length(buf);104if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||105vrl_length < (DRM_HDCP_1_4_VRL_LENGTH_SIZE +106DRM_HDCP_1_4_DCP_SIG_SIZE)) {107DRM_ERROR("Invalid blob length or vrl length\n");108return -EINVAL;109}110111/* Length of the all vrls combined */112vrl_length -= (DRM_HDCP_1_4_VRL_LENGTH_SIZE +113DRM_HDCP_1_4_DCP_SIG_SIZE);114115if (!vrl_length) {116DRM_ERROR("No vrl found\n");117return -EINVAL;118}119120buf += DRM_HDCP_1_4_VRL_LENGTH_SIZE;121ksv_count = drm_hdcp_get_revoked_ksv_count(buf, vrl_length);122if (!ksv_count) {123DRM_DEBUG("Revoked KSV count is 0\n");124return 0;125}126127*revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN, GFP_KERNEL);128if (!*revoked_ksv_list) {129DRM_ERROR("Out of Memory\n");130return -ENOMEM;131}132133if (drm_hdcp_get_revoked_ksvs(buf, revoked_ksv_list,134vrl_length) != ksv_count) {135*revoked_ksv_cnt = 0;136kfree(*revoked_ksv_list);137return -EINVAL;138}139140*revoked_ksv_cnt = ksv_count;141return 0;142}143144static int drm_hdcp_parse_hdcp2_srm(const u8 *buf, size_t count,145u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)146{147struct hdcp_srm_header *header;148u32 vrl_length, ksv_count, ksv_sz;149150if (count < (sizeof(struct hdcp_srm_header) +151DRM_HDCP_2_VRL_LENGTH_SIZE + DRM_HDCP_2_DCP_SIG_SIZE)) {152DRM_ERROR("Invalid blob length\n");153return -EINVAL;154}155156header = (struct hdcp_srm_header *)buf;157DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",158header->srm_id & DRM_HDCP_SRM_ID_MASK,159be16_to_cpu(header->srm_version), header->srm_gen_no);160161if (header->reserved)162return -EINVAL;163164buf = buf + sizeof(*header);165vrl_length = get_vrl_length(buf);166167if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||168vrl_length < (DRM_HDCP_2_VRL_LENGTH_SIZE +169DRM_HDCP_2_DCP_SIG_SIZE)) {170DRM_ERROR("Invalid blob length or vrl length\n");171return -EINVAL;172}173174/* Length of the all vrls combined */175vrl_length -= (DRM_HDCP_2_VRL_LENGTH_SIZE +176DRM_HDCP_2_DCP_SIG_SIZE);177178if (!vrl_length) {179DRM_ERROR("No vrl found\n");180return -EINVAL;181}182183buf += DRM_HDCP_2_VRL_LENGTH_SIZE;184ksv_count = (*buf << 2) | DRM_HDCP_2_KSV_COUNT_2_LSBITS(*(buf + 1));185if (!ksv_count) {186DRM_DEBUG("Revoked KSV count is 0\n");187return 0;188}189190*revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN, GFP_KERNEL);191if (!*revoked_ksv_list) {192DRM_ERROR("Out of Memory\n");193return -ENOMEM;194}195196ksv_sz = ksv_count * DRM_HDCP_KSV_LEN;197buf += DRM_HDCP_2_NO_OF_DEV_PLUS_RESERVED_SZ;198199DRM_DEBUG("Revoked KSVs: %d\n", ksv_count);200memcpy(*revoked_ksv_list, buf, ksv_sz);201202*revoked_ksv_cnt = ksv_count;203return 0;204}205206static inline bool is_srm_version_hdcp1(const u8 *buf)207{208return *buf == (u8)(DRM_HDCP_1_4_SRM_ID << 4);209}210211static inline bool is_srm_version_hdcp2(const u8 *buf)212{213return *buf == (u8)(DRM_HDCP_2_SRM_ID << 4 | DRM_HDCP_2_INDICATOR);214}215216static int drm_hdcp_srm_update(const u8 *buf, size_t count,217u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)218{219if (count < sizeof(struct hdcp_srm_header))220return -EINVAL;221222if (is_srm_version_hdcp1(buf))223return drm_hdcp_parse_hdcp1_srm(buf, count, revoked_ksv_list,224revoked_ksv_cnt);225else if (is_srm_version_hdcp2(buf))226return drm_hdcp_parse_hdcp2_srm(buf, count, revoked_ksv_list,227revoked_ksv_cnt);228else229return -EINVAL;230}231232static int drm_hdcp_request_srm(struct drm_device *drm_dev,233u8 **revoked_ksv_list, u32 *revoked_ksv_cnt)234{235char fw_name[36] = "display_hdcp_srm.bin";236const struct firmware *fw;237int ret;238239ret = request_firmware_direct(&fw, (const char *)fw_name,240drm_dev->dev);241if (ret < 0) {242*revoked_ksv_cnt = 0;243*revoked_ksv_list = NULL;244ret = 0;245goto exit;246}247248if (fw->size && fw->data)249ret = drm_hdcp_srm_update(fw->data, fw->size, revoked_ksv_list,250revoked_ksv_cnt);251252exit:253release_firmware(fw);254return ret;255}256257/**258* drm_hdcp_check_ksvs_revoked - Check the revoked status of the IDs259*260* @drm_dev: drm_device for which HDCP revocation check is requested261* @ksvs: List of KSVs (HDCP receiver IDs)262* @ksv_count: KSV count passed in through @ksvs263*264* This function reads the HDCP System renewability Message(SRM Table)265* from userspace as a firmware and parses it for the revoked HDCP266* KSVs(Receiver IDs) detected by DCP LLC. Once the revoked KSVs are known,267* revoked state of the KSVs in the list passed in by display drivers are268* decided and response is sent.269*270* SRM should be presented in the name of "display_hdcp_srm.bin".271*272* Format of the SRM table, that userspace needs to write into the binary file,273* is defined at:274* 1. Renewability chapter on 55th page of HDCP 1.4 specification275* https://www.digital-cp.com/sites/default/files/specifications/HDCP%20Specification%20Rev1_4_Secure.pdf276* 2. Renewability chapter on 63rd page of HDCP 2.2 specification277* https://www.digital-cp.com/sites/default/files/specifications/HDCP%20on%20HDMI%20Specification%20Rev2_2_Final1.pdf278*279* Returns:280* Count of the revoked KSVs or -ve error number in case of the failure.281*/282int drm_hdcp_check_ksvs_revoked(struct drm_device *drm_dev, u8 *ksvs,283u32 ksv_count)284{285u32 revoked_ksv_cnt = 0, i, j;286u8 *revoked_ksv_list = NULL;287int ret = 0;288289ret = drm_hdcp_request_srm(drm_dev, &revoked_ksv_list,290&revoked_ksv_cnt);291if (ret)292return ret;293294/* revoked_ksv_cnt will be zero when above function failed */295for (i = 0; i < revoked_ksv_cnt; i++)296for (j = 0; j < ksv_count; j++)297if (!memcmp(&ksvs[j * DRM_HDCP_KSV_LEN],298&revoked_ksv_list[i * DRM_HDCP_KSV_LEN],299DRM_HDCP_KSV_LEN)) {300DRM_DEBUG("Revoked KSV is ");301drm_hdcp_print_ksv(&ksvs[j * DRM_HDCP_KSV_LEN]);302ret++;303}304305kfree(revoked_ksv_list);306return ret;307}308EXPORT_SYMBOL_GPL(drm_hdcp_check_ksvs_revoked);309310static struct drm_prop_enum_list drm_cp_enum_list[] = {311{ DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },312{ DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },313{ DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },314};315DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)316317static struct drm_prop_enum_list drm_hdcp_content_type_enum_list[] = {318{ DRM_MODE_HDCP_CONTENT_TYPE0, "HDCP Type0" },319{ DRM_MODE_HDCP_CONTENT_TYPE1, "HDCP Type1" },320};321DRM_ENUM_NAME_FN(drm_get_hdcp_content_type_name,322drm_hdcp_content_type_enum_list)323324/**325* drm_connector_attach_content_protection_property - attach content protection326* property327*328* @connector: connector to attach CP property on.329* @hdcp_content_type: is HDCP Content Type property needed for connector330*331* This is used to add support for content protection on select connectors.332* Content Protection is intentionally vague to allow for different underlying333* technologies, however it is most implemented by HDCP.334*335* When hdcp_content_type is true enum property called HDCP Content Type is336* created (if it is not already) and attached to the connector.337*338* This property is used for sending the protected content's stream type339* from userspace to kernel on selected connectors. Protected content provider340* will decide their type of their content and declare the same to kernel.341*342* Content type will be used during the HDCP 2.2 authentication.343* Content type will be set to &drm_connector_state.hdcp_content_type.344*345* The content protection will be set to &drm_connector_state.content_protection346*347* When kernel triggered content protection state change like DESIRED->ENABLED348* and ENABLED->DESIRED, will use drm_hdcp_update_content_protection() to update349* the content protection state of a connector.350*351* Returns:352* Zero on success, negative errno on failure.353*/354int drm_connector_attach_content_protection_property(355struct drm_connector *connector, bool hdcp_content_type)356{357struct drm_device *dev = connector->dev;358struct drm_property *prop =359dev->mode_config.content_protection_property;360361if (!prop)362prop = drm_property_create_enum(dev, 0, "Content Protection",363drm_cp_enum_list,364ARRAY_SIZE(drm_cp_enum_list));365if (!prop)366return -ENOMEM;367368drm_object_attach_property(&connector->base, prop,369DRM_MODE_CONTENT_PROTECTION_UNDESIRED);370dev->mode_config.content_protection_property = prop;371372if (!hdcp_content_type)373return 0;374375prop = dev->mode_config.hdcp_content_type_property;376if (!prop)377prop = drm_property_create_enum(dev, 0, "HDCP Content Type",378drm_hdcp_content_type_enum_list,379ARRAY_SIZE(380drm_hdcp_content_type_enum_list));381if (!prop)382return -ENOMEM;383384drm_object_attach_property(&connector->base, prop,385DRM_MODE_HDCP_CONTENT_TYPE0);386dev->mode_config.hdcp_content_type_property = prop;387388return 0;389}390EXPORT_SYMBOL(drm_connector_attach_content_protection_property);391392/**393* drm_hdcp_update_content_protection - Updates the content protection state394* of a connector395*396* @connector: drm_connector on which content protection state needs an update397* @val: New state of the content protection property398*399* This function can be used by display drivers, to update the kernel triggered400* content protection state changes of a drm_connector such as DESIRED->ENABLED401* and ENABLED->DESIRED. No uevent for DESIRED->UNDESIRED or ENABLED->UNDESIRED,402* as userspace is triggering such state change and kernel performs it without403* fail.This function update the new state of the property into the connector's404* state and generate an uevent to notify the userspace.405*/406void drm_hdcp_update_content_protection(struct drm_connector *connector,407u64 val)408{409struct drm_device *dev = connector->dev;410struct drm_connector_state *state = connector->state;411412WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));413if (state->content_protection == val)414return;415416state->content_protection = val;417drm_sysfs_connector_property_event(connector,418dev->mode_config.content_protection_property);419}420EXPORT_SYMBOL(drm_hdcp_update_content_protection);421422423