1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/string.h>
5 #include <linux/miscdevice.h>
6 #include <linux/workqueue.h>
7 #include <linux/firmware.h>
8 #include "rockchip_hdmiv2.h"
9 #include "rockchip_hdmiv2_hw.h"
11 #define HDCP_KEY_SIZE 308
12 #define HDCP_PRIVATE_KEY_SIZE 280
13 #define HDCP_KEY_SHA_SIZE 20
14 #define HDCP_KEY_SEED_SIZE 2
18 u8 devicekey[HDCP_PRIVATE_KEY_SIZE];
19 u8 sha1[HDCP_KEY_SHA_SIZE];
26 struct hdcp_keys *keys;
32 static struct miscdevice mdev;
33 static struct hdcp *hdcp = NULL;
35 static void hdcp_load_key(struct hdmi *hdmi, struct hdcp_keys *key)
37 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
40 /* Disable decryption logic */
41 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
42 /* Poll untile DPK write is allowed */
44 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
45 } while ((value & m_DPK_WR_OK_STS) == 0);
47 /* write unencryped AKSV */
48 hdmi_writel(hdmi_dev, HDCPREG_DPK6, 0);
49 hdmi_writel(hdmi_dev, HDCPREG_DPK5, 0);
50 hdmi_writel(hdmi_dev, HDCPREG_DPK4, key->KSV[4]);
51 hdmi_writel(hdmi_dev, HDCPREG_DPK3, key->KSV[3]);
52 hdmi_writel(hdmi_dev, HDCPREG_DPK2, key->KSV[2]);
53 hdmi_writel(hdmi_dev, HDCPREG_DPK1, key->KSV[1]);
54 hdmi_writel(hdmi_dev, HDCPREG_DPK0, key->KSV[0]);
55 /* Poll untile DPK write is allowed */
57 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
58 } while ((value & m_DPK_WR_OK_STS) == 0);
60 if (hdcp->seeds != NULL) {
61 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 1);
62 hdmi_writel(hdmi_dev, HDCPREG_SEED1, hdcp->seeds[0]);
63 hdmi_writel(hdmi_dev, HDCPREG_SEED0, hdcp->seeds[1]);
65 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
68 /* write private key */
69 for (i = 0; i < HDCP_PRIVATE_KEY_SIZE; i += 7) {
70 hdmi_writel(hdmi_dev, HDCPREG_DPK6, key->devicekey[i + 6]);
71 hdmi_writel(hdmi_dev, HDCPREG_DPK5, key->devicekey[i + 5]);
72 hdmi_writel(hdmi_dev, HDCPREG_DPK4, key->devicekey[i + 4]);
73 hdmi_writel(hdmi_dev, HDCPREG_DPK3, key->devicekey[i + 3]);
74 hdmi_writel(hdmi_dev, HDCPREG_DPK2, key->devicekey[i + 2]);
75 hdmi_writel(hdmi_dev, HDCPREG_DPK1, key->devicekey[i + 1]);
76 hdmi_writel(hdmi_dev, HDCPREG_DPK0, key->devicekey[i]);
79 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
80 } while ((value & m_DPK_WR_OK_STS) == 0);
83 pr_info("%s success\n", __func__);
86 static void hdcp_load_keys_cb(const struct firmware *fw,
89 struct hdmi *hdmi = (struct hdmi *)context;
91 if (fw->size < HDCP_KEY_SIZE) {
92 pr_err("HDCP: firmware wrong size %d\n", (int)fw->size);
95 hdcp->keys = kmalloc(HDCP_KEY_SIZE, GFP_KERNEL);
96 memcpy(hdcp->keys, fw->data, HDCP_KEY_SIZE);
98 if (fw->size > HDCP_KEY_SIZE) {
99 if ((fw->size - HDCP_KEY_SIZE) < HDCP_KEY_SEED_SIZE) {
100 pr_err("HDCP: invalid seed key size\n");
103 hdcp->seeds = kmalloc(HDCP_KEY_SEED_SIZE, GFP_KERNEL);
104 if (hdcp->seeds == NULL) {
105 pr_err("HDCP: can't allocated space for seed keys\n");
108 memcpy(hdcp->seeds, fw->data + HDCP_KEY_SIZE,
111 hdcp_load_key(hdmi, hdcp->keys);
114 static void rockchip_hdmiv2_hdcp_start(struct hdmi *hdmi)
116 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
120 if (hdmi_dev->soctype == HDMI_SOC_RK3368) {
121 hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
122 m_HDCP2_OVR_EN | m_HDCP2_FORCE,
123 v_HDCP2_OVR_EN(1) | v_HDCP2_FORCE(0));
124 hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0x00);
125 hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0x00);
128 hdmi_msk_reg(hdmi_dev, FC_INVIDCONF,
129 m_FC_HDCP_KEEPOUT, v_FC_HDCP_KEEPOUT(1));
130 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
131 m_HDMI_DVI, v_HDMI_DVI(hdmi->edid.sink_hdmi));
132 hdmi_writel(hdmi_dev, A_OESSWCFG, 0x40);
133 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
134 m_ENCRYPT_BYPASS | m_FEATURE11_EN | m_SYNC_RI_CHECK,
135 v_ENCRYPT_BYPASS(0) | v_FEATURE11_EN(0) |
137 hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
138 m_ENCRYPT_DISBALE | m_PH2UPSHFTENC,
139 v_ENCRYPT_DISBALE(0) | v_PH2UPSHFTENC(1));
140 /* Reset HDCP Engine */
141 hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
142 m_HDCP_SW_RST, v_HDCP_SW_RST(0));
144 hdmi_writel(hdmi_dev, A_APIINTMSK, 0x00);
145 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(1));
147 hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
148 m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(0));
149 pr_info("%s success\n", __func__);
152 static void rockchip_hdmiv2_hdcp_stop(struct hdmi *hdmi)
154 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
159 hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
160 m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(1));
161 hdmi_writel(hdmi_dev, A_APIINTMSK, 0xff);
162 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(0));
165 static ssize_t hdcp_enable_read(struct device *device,
166 struct device_attribute *attr, char *buf)
171 enable = hdcp->enable;
173 return snprintf(buf, PAGE_SIZE, "%d\n", enable);
176 static ssize_t hdcp_enable_write(struct device *device,
177 struct device_attribute *attr,
178 const char *buf, size_t count)
185 if (kstrtoint(buf, 0, &enable))
188 if (hdcp->enable != enable) {
190 hdmi_submit_work(hdcp->hdmi, HDMI_ENABLE_HDCP, 0, NULL);
192 rockchip_hdmiv2_hdcp_stop(hdcp->hdmi);
193 hdcp->enable = enable;
198 static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR,
199 hdcp_enable_read, hdcp_enable_write);
201 static ssize_t hdcp_trytimes_read(struct device *device,
202 struct device_attribute *attr, char *buf)
207 trytimes = hdcp->retry_times;
209 return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
212 static ssize_t hdcp_trytimes_wrtie(struct device *device,
213 struct device_attribute *attr,
214 const char *buf, size_t count)
221 if (kstrtoint(buf, 0, &trytimes))
224 if (hdcp->retry_times != trytimes)
225 hdcp->retry_times = trytimes;
229 static DEVICE_ATTR(trytimes, S_IRUGO|S_IWUSR,
230 hdcp_trytimes_read, hdcp_trytimes_wrtie);
232 static int hdcp_init(struct hdmi *hdmi)
236 mdev.minor = MISC_DYNAMIC_MINOR;
239 hdcp = kmalloc(sizeof(*hdcp), GFP_KERNEL);
241 pr_err("HDCP: kmalloc fail!\n");
245 memset(hdcp, 0, sizeof(struct hdcp));
247 if (misc_register(&mdev)) {
248 pr_err("HDCP: Could not add character driver\n");
249 ret = HDMI_ERROR_FALSE;
252 ret = device_create_file(mdev.this_device, &dev_attr_enable);
254 pr_err("HDCP: Could not add sys file enable\n");
258 ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
260 pr_err("HDCP: Could not add sys file enable\n");
265 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
266 "hdcp", mdev.this_device, GFP_KERNEL,
267 hdmi, hdcp_load_keys_cb);
270 pr_err("HDCP: request_firmware_nowait failed: %d\n", ret);
274 hdmi->ops->hdcp_cb = rockchip_hdmiv2_hdcp_start;
278 device_remove_file(mdev.this_device, &dev_attr_trytimes);
280 device_remove_file(mdev.this_device, &dev_attr_enable);
282 misc_deregister(&mdev);
285 kfree(hdcp->invalidkeys);
291 void rockchip_hdmiv2_hdcp_init(struct hdmi *hdmi)
293 pr_info("%s", __func__);
297 hdcp_load_key(hdmi, hdcp->keys);