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 <linux/delay.h>
9 #include "rockchip_hdmiv2.h"
10 #include "rockchip_hdmiv2_hw.h"
12 #define HDCP_KEY_SIZE 308
13 #define HDCP_PRIVATE_KEY_SIZE 280
14 #define HDCP_KEY_SHA_SIZE 20
15 #define HDCP_KEY_SEED_SIZE 2
21 #define MAX_DOWNSTREAM_DEVICE_NUM 5
25 u8 devicekey[HDCP_PRIVATE_KEY_SIZE];
26 u8 sha1[HDCP_KEY_SHA_SIZE];
33 struct hdcp_keys *keys;
45 unsigned int mdigest[5];
48 static struct miscdevice mdev;
49 static struct hdcp *hdcp;
51 static void sha_reset(struct sha_t *sha)
56 sha->mcomputed = false;
57 sha->mcorrupted = false;
58 for (i = 0; i < sizeof(sha->mlength); i++)
61 sha->mdigest[0] = 0x67452301;
62 sha->mdigest[1] = 0xEFCDAB89;
63 sha->mdigest[2] = 0x98BADCFE;
64 sha->mdigest[3] = 0x10325476;
65 sha->mdigest[4] = 0xC3D2E1F0;
68 #define shacircularshift(bits, word) ((((word) << (bits)) & 0xFFFFFFFF) | \
69 ((word) >> (32 - (bits))))
70 void sha_processblock(struct sha_t *sha)
72 const unsigned K[] = {
73 /* constants defined in SHA-1 */
74 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 };
75 unsigned W[80]; /* word sequence */
76 unsigned A, B, C, D, E; /* word buffers */
80 /* Initialize the first 16 words in the array W */
81 for (t = 0; t < 80; t++) {
83 W[t] = ((unsigned)sha->mblock[t * 4 + 0]) << 24;
84 W[t] |= ((unsigned)sha->mblock[t * 4 + 1]) << 16;
85 W[t] |= ((unsigned)sha->mblock[t * 4 + 2]) << 8;
86 W[t] |= ((unsigned)sha->mblock[t * 4 + 3]) << 0;
88 A = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
89 W[t] = shacircularshift(1, A);
99 for (t = 0; t < 80; t++) {
100 temp = shacircularshift(5, A);
102 temp += ((B & C) | ((~B) & D)) + E + W[t] + K[0];
104 temp += (B ^ C ^ D) + E + W[t] + K[1];
106 temp += ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
108 temp += (B ^ C ^ D) + E + W[t] + K[3];
112 C = shacircularshift(30, B);
114 A = (temp & 0xFFFFFFFF);
117 sha->mdigest[0] = (sha->mdigest[0] + A) & 0xFFFFFFFF;
118 sha->mdigest[1] = (sha->mdigest[1] + B) & 0xFFFFFFFF;
119 sha->mdigest[2] = (sha->mdigest[2] + C) & 0xFFFFFFFF;
120 sha->mdigest[3] = (sha->mdigest[3] + D) & 0xFFFFFFFF;
121 sha->mdigest[4] = (sha->mdigest[4] + E) & 0xFFFFFFFF;
126 static void sha_padmessage(struct sha_t *sha)
129 * Check to see if the current message block is too small to hold
130 * the initial padding bits and length. If so, we will pad the
131 * block, process it, and then continue padding into a second
134 if (sha->mindex > 55) {
135 sha->mblock[sha->mindex++] = 0x80;
136 while (sha->mindex < 64)
137 sha->mblock[sha->mindex++] = 0;
139 sha_processblock(sha);
140 while (sha->mindex < 56)
141 sha->mblock[sha->mindex++] = 0;
143 sha->mblock[sha->mindex++] = 0x80;
144 while (sha->mindex < 56)
145 sha->mblock[sha->mindex++] = 0;
148 /* Store the message length as the last 8 octets */
149 sha->mblock[56] = sha->mlength[7];
150 sha->mblock[57] = sha->mlength[6];
151 sha->mblock[58] = sha->mlength[5];
152 sha->mblock[59] = sha->mlength[4];
153 sha->mblock[60] = sha->mlength[3];
154 sha->mblock[61] = sha->mlength[2];
155 sha->mblock[62] = sha->mlength[1];
156 sha->mblock[63] = sha->mlength[0];
158 sha_processblock(sha);
161 static int sha_result(struct sha_t *sha)
166 if (sha->mcomputed == 0) {
168 sha->mcomputed = true;
173 static void sha_input(struct sha_t *sha, const u8 *data, u32 size)
179 if (data == 0 || size == 0) {
180 pr_err("invalid input data");
183 if (sha->mcomputed || sha->mcorrupted) {
184 sha->mcorrupted = true;
187 while (size-- && !sha->mcorrupted) {
188 sha->mblock[sha->mindex++] = *data;
190 for (i = 0; i < 8; i++) {
192 for (j = 0; j < sizeof(sha->mlength); j++) {
194 if (sha->mlength[j] != 0) {
199 sha->mcorrupted = (sha->mcorrupted ||
202 /* if corrupted then message is too long */
203 if (sha->mindex == 64)
204 sha_processblock(sha);
209 static int hdcpverify_ksv(const u8 *data, u32 size)
214 if ((!data) || (size < (HEADER + SHAMAX))) {
215 pr_err("invalid input data");
220 sha_input(&sha, data, size - SHAMAX);
221 if (sha_result(&sha) == false) {
222 pr_err("cannot process SHA digest");
226 for (i = 0; i < SHAMAX; i++) {
227 if (data[size - SHAMAX + i] != (u8)(sha.mdigest[i / 4]
229 pr_err("SHA digest does not match");
236 static int rockchip_hdmiv2_hdcp_ksvsha1(struct hdmi_dev *hdmi_dev)
238 int rc = 0, value, list, i;
239 char bstaus0, bstaus1;
242 hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL, m_KSV_MEM_REQ, v_KSV_MEM_REQ(1));
245 value = hdmi_readl(hdmi_dev, A_KSVMEMCTRL);
246 usleep_range(500, 1000);
247 } while ((value & m_KSV_MEM_ACCESS) == 0 && --list);
249 if ((value & m_KSV_MEM_ACCESS) == 0) {
250 pr_err("KSV memory can not access\n");
255 hdmi_readl(hdmi_dev, HDCP_BSTATUS_0);
256 bstaus0 = hdmi_readl(hdmi_dev, HDCP_BSTATUS_0 + 1);
257 bstaus1 = hdmi_readl(hdmi_dev, HDCP_BSTATUS_1 + 1);
259 if (bstaus0 & m_MAX_DEVS_EXCEEDED) {
260 pr_err("m_MAX_DEVS_EXCEEDED\n");
264 list = bstaus0 & m_DEVICE_COUNT;
265 if (list > MAX_DOWNSTREAM_DEVICE_NUM) {
266 pr_err("MAX_DOWNSTREAM_DEVICE_NUM\n");
270 if (bstaus1 & (1 << 3)) {
271 pr_err("MAX_CASCADE_EXCEEDED\n");
275 value = (list * KSV_LEN) + HEADER + SHAMAX;
276 ksvlistbuf = kmalloc(value, GFP_KERNEL);
278 pr_err("HDCP: kmalloc ksvlistbuf fail!\n");
282 ksvlistbuf[(list * KSV_LEN)] = bstaus0;
283 ksvlistbuf[(list * KSV_LEN) + 1] = bstaus1;
284 for (i = 2; i < value; i++) {
285 if (i < HEADER) /* BSTATUS & M0 */
286 ksvlistbuf[(list * KSV_LEN) + i] =
287 hdmi_readl(hdmi_dev, HDCP_BSTATUS_0 + i + 1);
288 else if (i < (HEADER + (list * KSV_LEN))) /* KSV list */
289 ksvlistbuf[i - HEADER] =
290 hdmi_readl(hdmi_dev, HDCP_BSTATUS_0 + i + 1);
293 hdmi_readl(hdmi_dev, HDCP_BSTATUS_0 + i + 1);
295 if (hdcpverify_ksv(ksvlistbuf, value) == true) {
297 pr_info("ksv check valid\n");
299 pr_info("ksv check invalid\n");
304 hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL, m_KSV_MEM_REQ, v_KSV_MEM_REQ(0));
308 static void rockchip_hdmiv2_hdcp_2nd_auth(struct hdmi *hdmi)
310 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
312 if (rockchip_hdmiv2_hdcp_ksvsha1(hdmi_dev))
313 hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL,
314 m_SHA1_FAIL | m_KSV_UPDATE,
315 v_SHA1_FAIL(1) | v_KSV_UPDATE(1));
317 hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL,
318 m_SHA1_FAIL | m_KSV_UPDATE,
319 v_SHA1_FAIL(0) | v_KSV_UPDATE(1));
322 static void hdcp_load_key(struct hdmi *hdmi, struct hdcp_keys *key)
324 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
327 /* Disable decryption logic */
328 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
329 /* Poll untile DPK write is allowed */
331 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
332 } while ((value & m_DPK_WR_OK_STS) == 0);
334 /* write unencryped AKSV */
335 hdmi_writel(hdmi_dev, HDCPREG_DPK6, 0);
336 hdmi_writel(hdmi_dev, HDCPREG_DPK5, 0);
337 hdmi_writel(hdmi_dev, HDCPREG_DPK4, key->KSV[4]);
338 hdmi_writel(hdmi_dev, HDCPREG_DPK3, key->KSV[3]);
339 hdmi_writel(hdmi_dev, HDCPREG_DPK2, key->KSV[2]);
340 hdmi_writel(hdmi_dev, HDCPREG_DPK1, key->KSV[1]);
341 hdmi_writel(hdmi_dev, HDCPREG_DPK0, key->KSV[0]);
342 /* Poll untile DPK write is allowed */
344 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
345 } while ((value & m_DPK_WR_OK_STS) == 0);
348 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 1);
349 hdmi_writel(hdmi_dev, HDCPREG_SEED1, hdcp->seeds[0]);
350 hdmi_writel(hdmi_dev, HDCPREG_SEED0, hdcp->seeds[1]);
352 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
355 /* write private key */
356 for (i = 0; i < HDCP_PRIVATE_KEY_SIZE; i += 7) {
357 hdmi_writel(hdmi_dev, HDCPREG_DPK6, key->devicekey[i + 6]);
358 hdmi_writel(hdmi_dev, HDCPREG_DPK5, key->devicekey[i + 5]);
359 hdmi_writel(hdmi_dev, HDCPREG_DPK4, key->devicekey[i + 4]);
360 hdmi_writel(hdmi_dev, HDCPREG_DPK3, key->devicekey[i + 3]);
361 hdmi_writel(hdmi_dev, HDCPREG_DPK2, key->devicekey[i + 2]);
362 hdmi_writel(hdmi_dev, HDCPREG_DPK1, key->devicekey[i + 1]);
363 hdmi_writel(hdmi_dev, HDCPREG_DPK0, key->devicekey[i]);
366 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
367 } while ((value & m_DPK_WR_OK_STS) == 0);
370 pr_info("%s success\n", __func__);
373 static void hdcp_load_keys_cb(const struct firmware *fw,
376 struct hdmi *hdmi = (struct hdmi *)context;
379 pr_info("HDCP: firmware is not loaded\n");
382 if (fw->size < HDCP_KEY_SIZE) {
383 pr_err("HDCP: firmware wrong size %d\n", (int)fw->size);
386 hdcp->keys = kmalloc(HDCP_KEY_SIZE, GFP_KERNEL);
387 memcpy(hdcp->keys, fw->data, HDCP_KEY_SIZE);
389 if (fw->size > HDCP_KEY_SIZE) {
390 if ((fw->size - HDCP_KEY_SIZE) < HDCP_KEY_SEED_SIZE) {
391 pr_err("HDCP: invalid seed key size\n");
394 hdcp->seeds = kmalloc(HDCP_KEY_SEED_SIZE, GFP_KERNEL);
398 memcpy(hdcp->seeds, fw->data + HDCP_KEY_SIZE,
401 hdcp_load_key(hdmi, hdcp->keys);
404 void rockchip_hdmiv2_hdcp2_enable(int enable)
406 struct hdmi_dev *hdmi_dev;
409 pr_err("rockchip hdmiv2 hdcp is not exist\n");
412 hdmi_dev = hdcp->hdmi->property->priv;
413 if ((hdmi_readl(hdmi_dev, CONFIG1_ID) & m_HDCP22) == 0) {
414 pr_err("Don't support hdcp22\n");
417 if (hdmi_dev->hdcp2_enable != enable) {
418 hdmi_dev->hdcp2_enable = enable;
419 if (hdmi_dev->hdcp2_enable == 0) {
420 hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
421 m_HDCP2_OVR_EN | m_HDCP2_FORCE,
422 v_HDCP2_OVR_EN(1) | v_HDCP2_FORCE(0));
423 hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0xff);
424 hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0xff);
426 hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
427 m_HDCP2_OVR_EN | m_HDCP2_FORCE,
428 v_HDCP2_OVR_EN(0) | v_HDCP2_FORCE(0));
429 hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0x00);
430 hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0x00);
434 EXPORT_SYMBOL(rockchip_hdmiv2_hdcp2_enable);
436 void rockchip_hdmiv2_hdcp2_init(void (*hdcp2_enble)(int),
437 void (*hdcp2_reset)(void),
438 void (*hdcp2_start)(void))
440 struct hdmi_dev *hdmi_dev;
443 pr_err("rockchip hdmiv2 hdcp is not exist\n");
446 hdmi_dev = hdcp->hdmi->property->priv;
447 hdmi_dev->hdcp2_en = hdcp2_enble;
448 hdmi_dev->hdcp2_reset = hdcp2_reset;
449 hdmi_dev->hdcp2_start = hdcp2_start;
451 EXPORT_SYMBOL(rockchip_hdmiv2_hdcp2_init);
453 static void rockchip_hdmiv2_hdcp_start(struct hdmi *hdmi)
455 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
459 if (hdmi_readl(hdmi_dev, CONFIG1_ID) & m_HDCP22) {
460 if (hdmi_dev->hdcp2_enable == 0) {
461 hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
462 m_HDCP2_OVR_EN | m_HDCP2_FORCE,
463 v_HDCP2_OVR_EN(1) | v_HDCP2_FORCE(0));
464 hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0xff);
465 hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0xff);
467 hdmi_msk_reg(hdmi_dev, HDCP2REG_CTRL,
468 m_HDCP2_OVR_EN | m_HDCP2_FORCE,
469 v_HDCP2_OVR_EN(0) | v_HDCP2_FORCE(0));
470 hdmi_writel(hdmi_dev, HDCP2REG_MASK, 0x00);
471 hdmi_writel(hdmi_dev, HDCP2REG_MUTE, 0x00);
475 hdmi_msk_reg(hdmi_dev, FC_INVIDCONF,
476 m_FC_HDCP_KEEPOUT, v_FC_HDCP_KEEPOUT(1));
477 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
478 m_HDMI_DVI, v_HDMI_DVI(hdmi->edid.sink_hdmi));
479 hdmi_writel(hdmi_dev, A_OESSWCFG, 0x40);
480 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0,
481 m_ENCRYPT_BYPASS | m_FEATURE11_EN | m_SYNC_RI_CHECK,
482 v_ENCRYPT_BYPASS(0) | v_FEATURE11_EN(0) |
484 hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
485 m_ENCRYPT_DISBALE | m_PH2UPSHFTENC,
486 v_ENCRYPT_DISBALE(0) | v_PH2UPSHFTENC(1));
487 /* Reset HDCP Engine */
488 if (hdmi_readl(hdmi_dev, MC_CLKDIS) & m_HDCPCLK_DISABLE)
489 hdmi_msk_reg(hdmi_dev, A_HDCPCFG1,
490 m_HDCP_SW_RST, v_HDCP_SW_RST(0));
492 hdmi_writel(hdmi_dev, A_APIINTMSK, 0x00);
493 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(1));
495 hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
496 m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(0));
497 if (hdmi_dev->hdcp2_start)
498 hdmi_dev->hdcp2_start();
499 pr_info("%s success\n", __func__);
502 static void rockchip_hdmiv2_hdcp_stop(struct hdmi *hdmi)
504 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
509 hdmi_msk_reg(hdmi_dev, MC_CLKDIS,
510 m_HDCPCLK_DISABLE, v_HDCPCLK_DISABLE(1));
511 hdmi_writel(hdmi_dev, A_APIINTMSK, 0xff);
512 hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(0));
513 hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL,
514 m_SHA1_FAIL | m_KSV_UPDATE,
515 v_SHA1_FAIL(0) | v_KSV_UPDATE(0));
516 rockchip_hdmiv2_hdcp2_enable(0);
519 void rockchip_hdmiv2_hdcp_isr(struct hdmi_dev *hdmi_dev, int hdcp_int)
521 pr_info("hdcp_int is 0x%02x\n", hdcp_int);
523 if (hdcp_int & m_KSVSHA1_CALC_INT) {
524 pr_info("hdcp sink is a repeater\n");
525 hdmi_submit_work(hdcp->hdmi, HDMI_HDCP_AUTH_2ND, 0, 0);
527 if (hdcp_int & 0x40) {
528 pr_info("hdcp check failed\n");
529 rockchip_hdmiv2_hdcp_stop(hdmi_dev->hdmi);
530 hdmi_submit_work(hdcp->hdmi, HDMI_ENABLE_HDCP, 0, 0);
534 static ssize_t hdcp_enable_read(struct device *device,
535 struct device_attribute *attr, char *buf)
540 enable = hdcp->enable;
542 return snprintf(buf, PAGE_SIZE, "%d\n", enable);
545 static ssize_t hdcp_enable_write(struct device *device,
546 struct device_attribute *attr,
547 const char *buf, size_t count)
554 pr_err("HDCP: key is not loaded\n");
557 if (kstrtoint(buf, 0, &enable))
560 if (hdcp->enable != enable) {
562 hdmi_submit_work(hdcp->hdmi, HDMI_ENABLE_HDCP, 0, 0);
564 rockchip_hdmiv2_hdcp_stop(hdcp->hdmi);
565 hdcp->enable = enable;
570 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
571 hdcp_enable_read, hdcp_enable_write);
573 static ssize_t hdcp_trytimes_read(struct device *device,
574 struct device_attribute *attr, char *buf)
579 trytimes = hdcp->retry_times;
581 return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
584 static ssize_t hdcp_trytimes_wrtie(struct device *device,
585 struct device_attribute *attr,
586 const char *buf, size_t count)
593 if (kstrtoint(buf, 0, &trytimes))
596 if (hdcp->retry_times != trytimes)
597 hdcp->retry_times = trytimes;
601 static DEVICE_ATTR(trytimes, S_IRUGO | S_IWUSR,
602 hdcp_trytimes_read, hdcp_trytimes_wrtie);
604 static int hdcp_init(struct hdmi *hdmi)
607 struct hdmi_dev *hdmi_dev = hdmi->property->priv;
609 mdev.minor = MISC_DYNAMIC_MINOR;
612 hdcp = kmalloc(sizeof(*hdcp), GFP_KERNEL);
614 pr_err("HDCP: kmalloc fail!\n");
618 memset(hdcp, 0, sizeof(struct hdcp));
620 if (misc_register(&mdev)) {
621 pr_err("HDCP: Could not add character driver\n");
622 ret = HDMI_ERROR_FALSE;
625 ret = device_create_file(mdev.this_device, &dev_attr_enable);
627 pr_err("HDCP: Could not add sys file enable\n");
631 ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
633 pr_err("HDCP: Could not add sys file enable\n");
638 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
639 "hdcp", mdev.this_device, GFP_KERNEL,
640 hdmi, hdcp_load_keys_cb);
643 pr_err("HDCP: request_firmware_nowait failed: %d\n", ret);
646 if ((hdmi_readl(hdmi_dev, MC_CLKDIS) & m_HDCPCLK_DISABLE) == 0)
648 hdmi->ops->hdcp_cb = rockchip_hdmiv2_hdcp_start;
649 hdmi->ops->hdcp_auth2nd = rockchip_hdmiv2_hdcp_2nd_auth;
650 hdmi->ops->hdcp_power_off_cb = rockchip_hdmiv2_hdcp_stop;
654 device_remove_file(mdev.this_device, &dev_attr_trytimes);
656 device_remove_file(mdev.this_device, &dev_attr_enable);
658 misc_deregister(&mdev);
661 kfree(hdcp->invalidkeys);
667 void rockchip_hdmiv2_hdcp_init(struct hdmi *hdmi)
669 pr_info("%s", __func__);
674 hdcp_load_key(hdmi, hdcp->keys);
676 pr_info("hdcpkeys is no load\n");