video: rockchip: hdmi: fix panic if enable hdcp buy load key fail
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / hdmi / rockchip-hdmiv2 / rockchip_hdmiv2_hdcp.c
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"
11
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
16
17 #define KSV_LEN                 5
18 #define HEADER                  10
19 #define SHAMAX                  20
20
21 #define MAX_DOWNSTREAM_DEVICE_NUM       5
22
23 struct hdcp_keys {
24         u8 KSV[8];
25         u8 devicekey[HDCP_PRIVATE_KEY_SIZE];
26         u8 sha1[HDCP_KEY_SHA_SIZE];
27 };
28
29 struct hdcp {
30         struct hdmi             *hdmi;
31         int                     enable;
32         int                     retry_times;
33         struct hdcp_keys        *keys;
34         char                    *seeds;
35         int                     invalidkey;
36         char                    *invalidkeys;
37 };
38
39 struct sha_t {
40         u8 mlength[8];
41         u8 mblock[64];
42         int mindex;
43         int mcomputed;
44         int mcorrupted;
45         unsigned int mdigest[5];
46 };
47
48 static struct miscdevice mdev;
49 static struct hdcp *hdcp;
50
51 static void sha_reset(struct sha_t *sha)
52 {
53         u32 i = 0;
54
55         sha->mindex = 0;
56         sha->mcomputed = false;
57         sha->mcorrupted = false;
58         for (i = 0; i < sizeof(sha->mlength); i++)
59                 sha->mlength[i] = 0;
60
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;
66 }
67
68 #define shacircularshift(bits, word) ((((word) << (bits)) & 0xFFFFFFFF) | \
69                                      ((word) >> (32 - (bits))))
70 void sha_processblock(struct sha_t *sha)
71 {
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 */
77         unsigned temp = 0;
78         int t = 0;
79
80         /* Initialize the first 16 words in the array W */
81         for (t = 0; t < 80; t++) {
82                 if (t < 16) {
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;
87                 } else {
88                         A = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
89                         W[t] = shacircularshift(1, A);
90                 }
91         }
92
93         A = sha->mdigest[0];
94         B = sha->mdigest[1];
95         C = sha->mdigest[2];
96         D = sha->mdigest[3];
97         E = sha->mdigest[4];
98
99         for (t = 0; t < 80; t++) {
100                 temp = shacircularshift(5, A);
101                 if (t < 20)
102                         temp += ((B & C) | ((~B) & D)) + E + W[t] + K[0];
103                 else if (t < 40)
104                         temp += (B ^ C ^ D) + E + W[t] + K[1];
105                 else if (t < 60)
106                         temp += ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
107                 else
108                         temp += (B ^ C ^ D) + E + W[t] + K[3];
109
110                 E = D;
111                 D = C;
112                 C = shacircularshift(30, B);
113                 B = A;
114                 A = (temp & 0xFFFFFFFF);
115         }
116
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;
122
123         sha->mindex = 0;
124 }
125
126 static void sha_padmessage(struct sha_t *sha)
127 {
128         /*
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
132          *  block.
133          */
134         if (sha->mindex > 55) {
135                 sha->mblock[sha->mindex++] = 0x80;
136                 while (sha->mindex < 64)
137                         sha->mblock[sha->mindex++] = 0;
138
139                 sha_processblock(sha);
140                 while (sha->mindex < 56)
141                         sha->mblock[sha->mindex++] = 0;
142         } else {
143                 sha->mblock[sha->mindex++] = 0x80;
144                 while (sha->mindex < 56)
145                         sha->mblock[sha->mindex++] = 0;
146         }
147
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];
157
158         sha_processblock(sha);
159 }
160
161 static int sha_result(struct sha_t *sha)
162 {
163         if (sha->mcorrupted)
164                 return false;
165
166         if (sha->mcomputed == 0) {
167                 sha_padmessage(sha);
168                 sha->mcomputed = true;
169         }
170         return true;
171 }
172
173 static void sha_input(struct sha_t *sha, const u8 *data, u32 size)
174 {
175         int i = 0;
176         unsigned j = 0;
177         int rc = true;
178
179         if (data == 0 || size == 0) {
180                 pr_err("invalid input data");
181                 return;
182         }
183         if (sha->mcomputed || sha->mcorrupted) {
184                 sha->mcorrupted = true;
185                 return;
186         }
187         while (size-- && !sha->mcorrupted) {
188                 sha->mblock[sha->mindex++] = *data;
189
190                 for (i = 0; i < 8; i++) {
191                         rc = true;
192                         for (j = 0; j < sizeof(sha->mlength); j++) {
193                                 sha->mlength[j]++;
194                                 if (sha->mlength[j] != 0) {
195                                         rc = false;
196                                         break;
197                                 }
198                         }
199                         sha->mcorrupted = (sha->mcorrupted  ||
200                                            rc) ? true : false;
201                 }
202                 /* if corrupted then message is too long */
203                 if (sha->mindex == 64)
204                         sha_processblock(sha);
205                 data++;
206         }
207 }
208
209 static int hdcpverify_ksv(const u8 *data, u32 size)
210 {
211         u32 i = 0;
212         struct sha_t sha;
213
214         if ((!data) || (size < (HEADER + SHAMAX))) {
215                 pr_err("invalid input data");
216                 return false;
217         }
218
219         sha_reset(&sha);
220         sha_input(&sha, data, size - SHAMAX);
221         if (sha_result(&sha) == false) {
222                 pr_err("cannot process SHA digest");
223                 return false;
224         }
225
226         for (i = 0; i < SHAMAX; i++) {
227                 if (data[size - SHAMAX + i] != (u8)(sha.mdigest[i / 4]
228                                 >> ((i % 4) * 8))) {
229                         pr_err("SHA digest does not match");
230                         return false;
231                 }
232         }
233         return true;
234 }
235
236 static int rockchip_hdmiv2_hdcp_ksvsha1(struct hdmi_dev *hdmi_dev)
237 {
238         int rc = 0, value, list, i;
239         char bstaus0, bstaus1;
240         char *ksvlistbuf;
241
242         hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL, m_KSV_MEM_REQ, v_KSV_MEM_REQ(1));
243         list = 20;
244         do {
245                 value = hdmi_readl(hdmi_dev, A_KSVMEMCTRL);
246                 usleep_range(500, 1000);
247         } while ((value & m_KSV_MEM_ACCESS) == 0 && --list);
248
249         if ((value & m_KSV_MEM_ACCESS) == 0) {
250                 pr_err("KSV memory can not access\n");
251                 rc = -1;
252                 goto out;
253         }
254
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);
258
259         if (bstaus0 & m_MAX_DEVS_EXCEEDED) {
260                 pr_err("m_MAX_DEVS_EXCEEDED\n");
261                 rc = -1;
262                 goto out;
263         }
264         list = bstaus0 & m_DEVICE_COUNT;
265         if (list > MAX_DOWNSTREAM_DEVICE_NUM) {
266                 pr_err("MAX_DOWNSTREAM_DEVICE_NUM\n");
267                 rc = -1;
268                 goto out;
269         }
270         if (bstaus1 & (1 << 3)) {
271                 pr_err("MAX_CASCADE_EXCEEDED\n");
272                 rc = -1;
273                 goto out;
274         }
275         value = (list * KSV_LEN) + HEADER + SHAMAX;
276         ksvlistbuf = kmalloc(value, GFP_KERNEL);
277         if (!ksvlistbuf) {
278                 pr_err("HDCP: kmalloc ksvlistbuf fail!\n");
279                 rc = -ENOMEM;
280                 goto out;
281         }
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);
291                 else /* SHA */
292                         ksvlistbuf[i] =
293                                 hdmi_readl(hdmi_dev, HDCP_BSTATUS_0 + i + 1);
294         }
295         if (hdcpverify_ksv(ksvlistbuf, value) == true) {
296                 rc = 0;
297                 pr_info("ksv check valid\n");
298         } else {
299                 pr_info("ksv check invalid\n");
300                 rc = -1;
301         }
302         kfree(ksvlistbuf);
303 out:
304         hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL, m_KSV_MEM_REQ, v_KSV_MEM_REQ(0));
305         return rc;
306 }
307
308 static void rockchip_hdmiv2_hdcp_2nd_auth(struct hdmi *hdmi)
309 {
310         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
311
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));
316         else
317                 hdmi_msk_reg(hdmi_dev, A_KSVMEMCTRL,
318                              m_SHA1_FAIL | m_KSV_UPDATE,
319                              v_SHA1_FAIL(0) | v_KSV_UPDATE(1));
320 }
321
322 static void hdcp_load_key(struct hdmi *hdmi, struct hdcp_keys *key)
323 {
324         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
325         int i, value;
326
327         /* Disable decryption logic */
328         hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
329         /* Poll untile DPK write is allowed */
330         do {
331                 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
332         } while ((value & m_DPK_WR_OK_STS) == 0);
333
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 */
343         do {
344                 value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
345         } while ((value & m_DPK_WR_OK_STS) == 0);
346
347         if (hdcp->seeds) {
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]);
351         } else {
352                 hdmi_writel(hdmi_dev, HDCPREG_RMCTL, 0);
353         }
354
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]);
364
365                 do {
366                         value = hdmi_readl(hdmi_dev, HDCPREG_RMSTS);
367                 } while ((value & m_DPK_WR_OK_STS) == 0);
368         }
369
370         pr_info("%s success\n", __func__);
371 }
372
373 static void hdcp_load_keys_cb(const struct firmware *fw,
374                               void *context)
375 {
376         struct hdmi *hdmi = (struct hdmi *)context;
377
378         if (!fw) {
379                 pr_info("HDCP: firmware is not loaded\n");
380                 return;
381         }
382         if (fw->size < HDCP_KEY_SIZE) {
383                 pr_err("HDCP: firmware wrong size %d\n", (int)fw->size);
384                 return;
385         }
386         hdcp->keys = kmalloc(HDCP_KEY_SIZE, GFP_KERNEL);
387         memcpy(hdcp->keys, fw->data, HDCP_KEY_SIZE);
388
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");
392                         return;
393                 }
394                 hdcp->seeds = kmalloc(HDCP_KEY_SEED_SIZE, GFP_KERNEL);
395                 if (!hdcp->seeds)
396                         return;
397
398                 memcpy(hdcp->seeds, fw->data + HDCP_KEY_SIZE,
399                        HDCP_KEY_SEED_SIZE);
400         }
401         hdcp_load_key(hdmi, hdcp->keys);
402 }
403
404 void rockchip_hdmiv2_hdcp2_enable(int enable)
405 {
406         struct hdmi_dev *hdmi_dev;
407
408         if (!hdcp) {
409                 pr_err("rockchip hdmiv2 hdcp is not exist\n");
410                 return;
411         }
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");
415                 return;
416         }
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);
425                 } else {
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);
431                 }
432         }
433 }
434 EXPORT_SYMBOL(rockchip_hdmiv2_hdcp2_enable);
435
436 void rockchip_hdmiv2_hdcp2_init(void (*hdcp2_enble)(int),
437                                 void (*hdcp2_reset)(void),
438                                 void (*hdcp2_start)(void))
439 {
440         struct hdmi_dev *hdmi_dev;
441
442         if (!hdcp) {
443                 pr_err("rockchip hdmiv2 hdcp is not exist\n");
444                 return;
445         }
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;
450 }
451 EXPORT_SYMBOL(rockchip_hdmiv2_hdcp2_init);
452
453 static void rockchip_hdmiv2_hdcp_start(struct hdmi *hdmi)
454 {
455         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
456
457         if (!hdcp->enable)
458                 return;
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);
466                 } else {
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);
472                 }
473         }
474
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) |
483                      v_SYNC_RI_CHECK(1));
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));
491
492         hdmi_writel(hdmi_dev, A_APIINTMSK, 0x00);
493         hdmi_msk_reg(hdmi_dev, A_HDCPCFG0, m_RX_DETECT, v_RX_DETECT(1));
494
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__);
500 }
501
502 static void rockchip_hdmiv2_hdcp_stop(struct hdmi *hdmi)
503 {
504         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
505
506         if (!hdcp->enable)
507                 return;
508
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);
517 }
518
519 void rockchip_hdmiv2_hdcp_isr(struct hdmi_dev *hdmi_dev, int hdcp_int)
520 {
521         pr_info("hdcp_int is 0x%02x\n", hdcp_int);
522
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);
526         }
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);
531         }
532 }
533
534 static ssize_t hdcp_enable_read(struct device *device,
535                                 struct device_attribute *attr, char *buf)
536 {
537         int enable = 0;
538
539         if (hdcp)
540                 enable = hdcp->enable;
541
542         return snprintf(buf, PAGE_SIZE, "%d\n", enable);
543 }
544
545 static ssize_t hdcp_enable_write(struct device *device,
546                                  struct device_attribute *attr,
547                                  const char *buf, size_t count)
548 {
549         int enable;
550
551         if (!hdcp)
552                 return -EINVAL;
553         if (!hdcp->keys) {
554                 pr_err("HDCP: key is not loaded\n");
555                 return -EINVAL;
556         }
557         if (kstrtoint(buf, 0, &enable))
558                 return -EINVAL;
559
560         if (hdcp->enable != enable) {
561                 if (!hdcp->enable)
562                         hdmi_submit_work(hdcp->hdmi, HDMI_ENABLE_HDCP, 0, 0);
563                 else
564                         rockchip_hdmiv2_hdcp_stop(hdcp->hdmi);
565                 hdcp->enable =  enable;
566         }
567
568         return count;
569 }
570 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
571                    hdcp_enable_read, hdcp_enable_write);
572
573 static ssize_t hdcp_trytimes_read(struct device *device,
574                                   struct device_attribute *attr, char *buf)
575 {
576         int trytimes = 0;
577
578         if (hdcp)
579                 trytimes = hdcp->retry_times;
580
581         return snprintf(buf, PAGE_SIZE, "%d\n", trytimes);
582 }
583
584 static ssize_t hdcp_trytimes_wrtie(struct device *device,
585                                    struct device_attribute *attr,
586                                    const char *buf, size_t count)
587 {
588         int trytimes;
589
590         if (!hdcp)
591                 return -EINVAL;
592
593         if (kstrtoint(buf, 0, &trytimes))
594                 return -EINVAL;
595
596         if (hdcp->retry_times != trytimes)
597                 hdcp->retry_times = trytimes;
598
599         return count;
600 }
601 static DEVICE_ATTR(trytimes, S_IRUGO | S_IWUSR,
602                    hdcp_trytimes_read, hdcp_trytimes_wrtie);
603
604 static int hdcp_init(struct hdmi *hdmi)
605 {
606         int ret;
607         struct hdmi_dev *hdmi_dev = hdmi->property->priv;
608
609         mdev.minor = MISC_DYNAMIC_MINOR;
610         mdev.name = "hdcp";
611         mdev.mode = 0666;
612         hdcp = kmalloc(sizeof(*hdcp), GFP_KERNEL);
613         if (!hdcp) {
614                 pr_err("HDCP: kmalloc fail!\n");
615                 ret = -ENOMEM;
616                 goto error0;
617         }
618         memset(hdcp, 0, sizeof(struct hdcp));
619         hdcp->hdmi = hdmi;
620         if (misc_register(&mdev)) {
621                 pr_err("HDCP: Could not add character driver\n");
622                 ret = HDMI_ERROR_FALSE;
623                 goto error1;
624         }
625         ret = device_create_file(mdev.this_device, &dev_attr_enable);
626         if (ret) {
627                 pr_err("HDCP: Could not add sys file enable\n");
628                 ret = -EINVAL;
629                 goto error2;
630         }
631         ret = device_create_file(mdev.this_device, &dev_attr_trytimes);
632         if (ret) {
633                 pr_err("HDCP: Could not add sys file enable\n");
634                 ret = -EINVAL;
635                 goto error3;
636         }
637
638         ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
639                                       "hdcp", mdev.this_device, GFP_KERNEL,
640                                       hdmi, hdcp_load_keys_cb);
641
642         if (ret < 0) {
643                 pr_err("HDCP: request_firmware_nowait failed: %d\n", ret);
644                 goto error4;
645         }
646         if ((hdmi_readl(hdmi_dev, MC_CLKDIS) & m_HDCPCLK_DISABLE) == 0)
647                 hdcp->enable = 1;
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;
651         return 0;
652
653 error4:
654         device_remove_file(mdev.this_device, &dev_attr_trytimes);
655 error3:
656         device_remove_file(mdev.this_device, &dev_attr_enable);
657 error2:
658         misc_deregister(&mdev);
659 error1:
660         kfree(hdcp->keys);
661         kfree(hdcp->invalidkeys);
662         kfree(hdcp);
663 error0:
664         return ret;
665 }
666
667 void rockchip_hdmiv2_hdcp_init(struct hdmi *hdmi)
668 {
669         pr_info("%s", __func__);
670         if (!hdcp)
671                 hdcp_init(hdmi);
672         else {
673                 if (hdcp->keys)
674                         hdcp_load_key(hdmi, hdcp->keys);
675                 else
676                         pr_info("hdcpkeys is no load\n");
677         }
678 }