temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / video / tegra / dc / nvhdcp.c
1 /*
2  * drivers/video/tegra/dc/nvhdcp.c
3  *
4  * Copyright (c) 2010-2011, NVIDIA Corporation.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/miscdevice.h>
21 #include <linux/poll.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/wait.h>
25 #include <linux/workqueue.h>
26 #include <asm/atomic.h>
27
28 #include <mach/dc.h>
29 #include <mach/nvhost.h>
30 #include <mach/kfuse.h>
31
32 #include <video/nvhdcp.h>
33
34 #include "dc_reg.h"
35 #include "dc_priv.h"
36 #include "hdmi_reg.h"
37 #include "hdmi.h"
38
39 /* for 0x40 Bcaps */
40 #define BCAPS_REPEATER (1 << 6)
41 #define BCAPS_READY (1 << 5)
42 #define BCAPS_11 (1 << 1) /* used for both Bcaps and Ainfo */
43
44 /* for 0x41 Bstatus */
45 #define BSTATUS_MAX_DEVS_EXCEEDED       (1 << 7)
46 #define BSTATUS_MAX_CASCADE_EXCEEDED    (1 << 11)
47
48 #ifdef VERBOSE_DEBUG
49 #define nvhdcp_vdbg(...)        \
50                 printk("nvhdcp: " __VA_ARGS__)
51 #else
52 #define nvhdcp_vdbg(...)                \
53 ({                                              \
54         if(0)                                   \
55                 printk("nvhdcp: " __VA_ARGS__); \
56         0;                                      \
57 })
58 #endif
59 #define nvhdcp_debug(...)       \
60                 pr_debug("nvhdcp: " __VA_ARGS__)
61 #define nvhdcp_err(...) \
62                 pr_err("nvhdcp: Error: " __VA_ARGS__)
63 #define nvhdcp_info(...)        \
64                 pr_info("nvhdcp: " __VA_ARGS__)
65
66
67 /* for nvhdcp.state */
68 enum tegra_nvhdcp_state {
69         STATE_OFF,
70         STATE_UNAUTHENTICATED,
71         STATE_LINK_VERIFY,
72         STATE_RENEGOTIATE,
73 };
74
75 struct tegra_nvhdcp {
76         struct work_struct              work;
77         struct tegra_dc_hdmi_data       *hdmi;
78         struct workqueue_struct         *downstream_wq;
79         struct mutex                    lock;
80         struct miscdevice               miscdev;
81         char                            name[12];
82         unsigned                        id;
83         bool                            plugged; /* true if hotplug detected */
84         atomic_t                        policy; /* set policy */
85         enum tegra_nvhdcp_state         state; /* STATE_xxx */
86         struct i2c_client               *client;
87         struct i2c_board_info           info;
88         int                             bus;
89         u32                             b_status;
90         u64                             a_n;
91         u64                             c_n;
92         u64                             a_ksv;
93         u64                             b_ksv;
94         u64                             c_ksv;
95         u64                             d_ksv;
96         u8                              v_prime[20];
97         u64                             m_prime;
98         u32                             num_bksv_list;
99         u64                             bksv_list[TEGRA_NVHDCP_MAX_DEVS];
100         int                             fail_count;
101 };
102
103 static inline bool nvhdcp_is_plugged(struct tegra_nvhdcp *nvhdcp)
104 {
105         rmb();
106         return nvhdcp->plugged;
107 }
108
109 static inline bool nvhdcp_set_plugged(struct tegra_nvhdcp *nvhdcp, bool plugged)
110 {
111         nvhdcp->plugged = plugged;
112         wmb();
113         return plugged;
114 }
115
116 static int nvhdcp_i2c_read(struct tegra_nvhdcp *nvhdcp, u8 reg,
117                                         size_t len, void *data)
118 {
119         int status;
120         int retries = 15;
121         struct i2c_msg msg[] = {
122                 {
123                         .addr = 0x74 >> 1, /* primary link */
124                         .flags = 0,
125                         .len = 1,
126                         .buf = &reg,
127                 },
128                 {
129                         .addr = 0x74 >> 1, /* primary link */
130                         .flags = I2C_M_RD,
131                         .len = len,
132                         .buf = data,
133                 },
134         };
135
136         do {
137                 if (!nvhdcp_is_plugged(nvhdcp)) {
138                         nvhdcp_err("disconnect during i2c xfer\n");
139                         return -EIO;
140                 }
141                 status = i2c_transfer(nvhdcp->client->adapter,
142                         msg, ARRAY_SIZE(msg));
143                 if (retries > 1)
144                         msleep(250);
145         } while ((status < 0) && retries--);
146
147         if (status < 0) {
148                 nvhdcp_err("i2c xfer error %d\n", status);
149                 return status;
150         }
151
152         return 0;
153 }
154
155 static int nvhdcp_i2c_write(struct tegra_nvhdcp *nvhdcp, u8 reg,
156                                         size_t len, const void *data)
157 {
158         int status;
159         u8 buf[len + 1];
160         struct i2c_msg msg[] = {
161                 {
162                         .addr = 0x74 >> 1, /* primary link */
163                         .flags = 0,
164                         .len = len + 1,
165                         .buf = buf,
166                 },
167         };
168         int retries = 15;
169
170         buf[0] = reg;
171         memcpy(buf + 1, data, len);
172
173         do {
174                 if (!nvhdcp_is_plugged(nvhdcp)) {
175                         nvhdcp_err("disconnect during i2c xfer\n");
176                         return -EIO;
177                 }
178                 status = i2c_transfer(nvhdcp->client->adapter,
179                         msg, ARRAY_SIZE(msg));
180                 if (retries > 1)
181                         msleep(250);
182         } while ((status < 0) && retries--);
183
184         if (status < 0) {
185                 nvhdcp_err("i2c xfer error %d\n", status);
186                 return status;
187         }
188
189         return 0;
190 }
191
192 static inline int nvhdcp_i2c_read8(struct tegra_nvhdcp *nvhdcp, u8 reg, u8 *val)
193 {
194         return nvhdcp_i2c_read(nvhdcp, reg, 1, val);
195 }
196
197 static inline int nvhdcp_i2c_write8(struct tegra_nvhdcp *nvhdcp, u8 reg, u8 val)
198 {
199         return nvhdcp_i2c_write(nvhdcp, reg, 1, &val);
200 }
201
202 static inline int nvhdcp_i2c_read16(struct tegra_nvhdcp *nvhdcp,
203                                         u8 reg, u16 *val)
204 {
205         u8 buf[2];
206         int e;
207
208         e = nvhdcp_i2c_read(nvhdcp, reg, sizeof buf, buf);
209         if (e)
210                 return e;
211
212         if (val)
213                 *val = buf[0] | (u16)buf[1] << 8;
214
215         return 0;
216 }
217
218 static int nvhdcp_i2c_read40(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 *val)
219 {
220         u8 buf[5];
221         int e, i;
222         u64 n;
223
224         e = nvhdcp_i2c_read(nvhdcp, reg, sizeof buf, buf);
225         if (e)
226                 return e;
227
228         for(i = 0, n = 0; i < 5; i++ ) {
229                 n <<= 8;
230                 n |= buf[4 - i];
231         }
232
233         if (val)
234                 *val = n;
235
236         return 0;
237 }
238
239 static int nvhdcp_i2c_write40(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 val)
240 {
241         char buf[5];
242         int i;
243         for(i = 0; i < 5; i++ ) {
244                 buf[i] = val;
245                 val >>= 8;
246         }
247         return nvhdcp_i2c_write(nvhdcp, reg, sizeof buf, buf);
248 }
249
250 static int nvhdcp_i2c_write64(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 val)
251 {
252         char buf[8];
253         int i;
254         for(i = 0; i < 8; i++ ) {
255                 buf[i] = val;
256                 val >>= 8;
257         }
258         return nvhdcp_i2c_write(nvhdcp, reg, sizeof buf, buf);
259 }
260
261
262 /* 64-bit link encryption session random number */
263 static inline u64 get_an(struct tegra_dc_hdmi_data *hdmi)
264 {
265         u64 r;
266         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AN_MSB) << 32;
267         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AN_LSB);
268         return r;
269 }
270
271 /* 64-bit upstream exchange random number */
272 static inline void set_cn(struct tegra_dc_hdmi_data *hdmi, u64 c_n)
273 {
274         tegra_hdmi_writel(hdmi, (u32)c_n, HDMI_NV_PDISP_RG_HDCP_CN_LSB);
275         tegra_hdmi_writel(hdmi, c_n >> 32, HDMI_NV_PDISP_RG_HDCP_CN_MSB);
276 }
277
278
279 /* 40-bit transmitter's key selection vector */
280 static inline u64 get_aksv(struct tegra_dc_hdmi_data *hdmi)
281 {
282         u64 r;
283         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AKSV_MSB) << 32;
284         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AKSV_LSB);
285         return r;
286 }
287
288 /* 40-bit receiver's key selection vector */
289 static inline void set_bksv(struct tegra_dc_hdmi_data *hdmi, u64 b_ksv, bool repeater)
290 {
291         if (repeater)
292                 b_ksv |= (u64)REPEATER << 32;
293         tegra_hdmi_writel(hdmi, (u32)b_ksv, HDMI_NV_PDISP_RG_HDCP_BKSV_LSB);
294         tegra_hdmi_writel(hdmi, b_ksv >> 32, HDMI_NV_PDISP_RG_HDCP_BKSV_MSB);
295 }
296
297
298 /* 40-bit software's key selection vector */
299 static inline void set_cksv(struct tegra_dc_hdmi_data *hdmi, u64 c_ksv)
300 {
301         tegra_hdmi_writel(hdmi, (u32)c_ksv, HDMI_NV_PDISP_RG_HDCP_CKSV_LSB);
302         tegra_hdmi_writel(hdmi, c_ksv >> 32, HDMI_NV_PDISP_RG_HDCP_CKSV_MSB);
303 }
304
305 /* 40-bit connection state */
306 static inline u64 get_cs(struct tegra_dc_hdmi_data *hdmi)
307 {
308         u64 r;
309         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CS_MSB) << 32;
310         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CS_LSB);
311         return r;
312 }
313
314 /* 40-bit upstream key selection vector */
315 static inline u64 get_dksv(struct tegra_dc_hdmi_data *hdmi)
316 {
317         u64 r;
318         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_DKSV_MSB) << 32;
319         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_DKSV_LSB);
320         return r;
321 }
322
323 /* 64-bit encrypted M0 value */
324 static inline u64 get_mprime(struct tegra_dc_hdmi_data *hdmi)
325 {
326         u64 r;
327         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB) << 32;
328         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB);
329         return r;
330 }
331
332 static inline u16 get_transmitter_ri(struct tegra_dc_hdmi_data *hdmi)
333 {
334         return tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_RI);
335 }
336
337 static inline int get_receiver_ri(struct tegra_nvhdcp *nvhdcp, u16 *r)
338 {
339         return nvhdcp_i2c_read16(nvhdcp, 0x8, r); /* long read */
340 }
341
342 static int get_bcaps(struct tegra_nvhdcp *nvhdcp, u8 *b_caps)
343 {
344         return nvhdcp_i2c_read8(nvhdcp, 0x40, b_caps);
345 }
346
347 static int get_ksvfifo(struct tegra_nvhdcp *nvhdcp,
348                                         unsigned num_bksv_list, u64 *ksv_list)
349 {
350         u8 *buf, *p;
351         int e;
352         unsigned i;
353         size_t buf_len = num_bksv_list * 5;
354
355         if (!ksv_list || num_bksv_list > TEGRA_NVHDCP_MAX_DEVS)
356                 return -EINVAL;
357
358         if (num_bksv_list == 0)
359                 return 0;
360
361         buf = kmalloc(buf_len, GFP_KERNEL);
362         if (IS_ERR_OR_NULL(buf))
363                 return -ENOMEM;
364
365         e = nvhdcp_i2c_read(nvhdcp, 0x43, buf_len, buf);
366         if (e) {
367                 kfree(buf);
368                 return e;
369         }
370
371         /* load 40-bit keys from repeater into array of u64 */
372         p = buf;
373         for (i = 0; i < num_bksv_list; i++) {
374                 ksv_list[i] = p[0] | ((u64)p[1] << 8) | ((u64)p[2] << 16)
375                                 | ((u64)p[3] << 24) | ((u64)p[4] << 32);
376                 p += 5;
377         }
378
379         kfree(buf);
380         return 0;
381 }
382
383 /* get V' 160-bit SHA-1 hash from repeater */
384 static int get_vprime(struct tegra_nvhdcp *nvhdcp, u8 *v_prime)
385 {
386         int e, i;
387
388         for (i = 0; i < 20; i += 4) {
389                 e = nvhdcp_i2c_read(nvhdcp, 0x20 + i, 4, v_prime + i);
390                 if (e)
391                         return e;
392         }
393         return 0;
394 }
395
396
397 /* set or clear RUN_YES */
398 static void hdcp_ctrl_run(struct tegra_dc_hdmi_data *hdmi, bool v)
399 {
400         u32 ctrl;
401
402         if (v) {
403                 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
404                 ctrl |= HDCP_RUN_YES;
405         } else {
406                 ctrl = 0;
407         }
408
409         tegra_hdmi_writel(hdmi, ctrl, HDMI_NV_PDISP_RG_HDCP_CTRL);
410 }
411
412 /* wait for any bits in mask to be set in HDMI_NV_PDISP_RG_HDCP_CTRL
413  * sleeps up to 120mS */
414 static int wait_hdcp_ctrl(struct tegra_dc_hdmi_data *hdmi, u32 mask, u32 *v)
415 {
416         int retries = 13;
417         u32 ctrl;
418
419         do {
420                 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
421                 if ((ctrl | (mask))) {
422                         if (v)
423                                 *v = ctrl;
424                         break;
425                 }
426                 if (retries > 1)
427                         msleep(10);
428         } while (--retries);
429         if (!retries) {
430                 nvhdcp_err("ctrl read timeout (mask=0x%x)\n", mask);
431                 return -EIO;
432         }
433         return 0;
434 }
435
436 /* wait for any bits in mask to be set in HDMI_NV_PDISP_KEY_CTRL
437  * waits up to 100mS */
438 static int wait_key_ctrl(struct tegra_dc_hdmi_data *hdmi, u32 mask)
439 {
440         int retries = 101;
441         u32 ctrl;
442
443         do {
444                 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_KEY_CTRL);
445                 if ((ctrl | (mask)))
446                         break;
447                 if (retries > 1)
448                         msleep(1);
449         } while (--retries);
450         if (!retries) {
451                 nvhdcp_err("key ctrl read timeout (mask=0x%x)\n", mask);
452                 return -EIO;
453         }
454         return 0;
455 }
456
457 /* check that key selection vector is well formed.
458  * NOTE: this function assumes KSV has already been checked against
459  * revocation list.
460  */
461 static int verify_ksv(u64 k)
462 {
463         unsigned i;
464
465         /* count set bits, must be exactly 20 set to be valid */
466         for(i = 0; k; i++)
467                 k ^= k & -k;
468
469         return  (i != 20) ? -EINVAL : 0;
470 }
471
472 /* get Status and Kprime signature - READ_S on TMDS0_LINK0 only */
473 static int get_s_prime(struct tegra_nvhdcp *nvhdcp, struct tegra_nvhdcp_packet *pkt)
474 {
475         struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
476         u32 sp_msb, sp_lsb1, sp_lsb2;
477         int e;
478
479         /* if connection isn't authenticated ... */
480         mutex_lock(&nvhdcp->lock);
481         if (nvhdcp->state != STATE_LINK_VERIFY) {
482                 memset(pkt, 0, sizeof *pkt);
483                 pkt->packet_results = TEGRA_NVHDCP_RESULT_LINK_FAILED;
484                 e = 0;
485                 goto err;
486         }
487
488         pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
489
490         /* we will be taking c_n, c_ksv as input */
491         if (!(pkt->value_flags & TEGRA_NVHDCP_FLAG_CN)
492                         || !(pkt->value_flags & TEGRA_NVHDCP_FLAG_CKSV)) {
493                 nvhdcp_err("missing value_flags (0x%x)\n", pkt->value_flags);
494                 e = -EINVAL;
495                 goto err;
496         }
497
498         pkt->value_flags = 0;
499
500         pkt->a_ksv = nvhdcp->a_ksv;
501         pkt->a_n = nvhdcp->a_n;
502         pkt->value_flags = TEGRA_NVHDCP_FLAG_AKSV | TEGRA_NVHDCP_FLAG_AN;
503
504         nvhdcp_vdbg("%s():cn %llx cksv %llx\n", __func__, pkt->c_n, pkt->c_ksv);
505
506         set_cn(hdmi, pkt->c_n);
507
508         tegra_hdmi_writel(hdmi, TMDS0_LINK0 | READ_S,
509                                         HDMI_NV_PDISP_RG_HDCP_CMODE);
510
511         set_cksv(hdmi, pkt->c_ksv);
512
513         e = wait_hdcp_ctrl(hdmi, SPRIME_VALID, NULL);
514         if (e) {
515                 nvhdcp_err("Sprime read timeout\n");
516                 pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
517                 e = -EIO;
518                 goto err;
519         }
520
521         msleep(50);
522
523         /* read 56-bit Sprime plus 16 status bits */
524         sp_msb = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_SPRIME_MSB);
525         sp_lsb1 = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB1);
526         sp_lsb2 = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_SPRIME_LSB2);
527
528         /* top 8 bits of LSB2 and bottom 8 bits of MSB hold status bits. */
529         pkt->hdcp_status = ( sp_msb << 8 ) | ( sp_lsb2 >> 24);
530         pkt->value_flags |= TEGRA_NVHDCP_FLAG_S;
531
532         /* 56-bit Kprime */
533         pkt->k_prime = ((u64)(sp_lsb2 & 0xffffff) << 32) | sp_lsb1;
534         pkt->value_flags |= TEGRA_NVHDCP_FLAG_KP;
535
536         /* is connection state supported? */
537         if (sp_msb & STATUS_CS) {
538                 pkt->cs = get_cs(hdmi);
539                 pkt->value_flags |= TEGRA_NVHDCP_FLAG_CS;
540         }
541
542         /* load Dksv */
543         pkt->d_ksv = get_dksv(hdmi);
544         if (verify_ksv(pkt->d_ksv)) {
545                 nvhdcp_err("Dksv invalid!\n");
546                 pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
547                 e = -EIO; /* treat bad Dksv as I/O error */
548         }
549         pkt->value_flags |= TEGRA_NVHDCP_FLAG_DKSV;
550
551         /* copy current Bksv */
552         pkt->b_ksv = nvhdcp->b_ksv;
553         pkt->value_flags |= TEGRA_NVHDCP_FLAG_BKSV;
554
555         pkt->packet_results = TEGRA_NVHDCP_RESULT_SUCCESS;
556         mutex_unlock(&nvhdcp->lock);
557         return 0;
558
559 err:
560         mutex_unlock(&nvhdcp->lock);
561         return e;
562 }
563
564 /* get M prime - READ_M on TMDS0_LINK0 only */
565 static inline int get_m_prime(struct tegra_nvhdcp *nvhdcp, struct tegra_nvhdcp_packet *pkt)
566 {
567         struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
568         int e;
569
570         pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
571
572         /* if connection isn't authenticated ... */
573         mutex_lock(&nvhdcp->lock);
574         if (nvhdcp->state != STATE_LINK_VERIFY) {
575                 memset(pkt, 0, sizeof *pkt);
576                 pkt->packet_results = TEGRA_NVHDCP_RESULT_LINK_FAILED;
577                 e = 0;
578                 goto err;
579         }
580
581         pkt->a_ksv = nvhdcp->a_ksv;
582         pkt->a_n = nvhdcp->a_n;
583         pkt->value_flags = TEGRA_NVHDCP_FLAG_AKSV | TEGRA_NVHDCP_FLAG_AN;
584
585         set_cn(hdmi, pkt->c_n);
586
587         tegra_hdmi_writel(hdmi, TMDS0_LINK0 | READ_M,
588                                         HDMI_NV_PDISP_RG_HDCP_CMODE);
589
590         /* Cksv write triggers Mprime update */
591         set_cksv(hdmi, pkt->c_ksv);
592
593         e = wait_hdcp_ctrl(hdmi, MPRIME_VALID, NULL);
594         if (e) {
595                 nvhdcp_err("Mprime read timeout\n");
596                 e = -EIO;
597                 goto err;
598         }
599         msleep(50);
600
601         /* load Mprime */
602         pkt->m_prime = get_mprime(hdmi);
603         pkt->value_flags |= TEGRA_NVHDCP_FLAG_MP;
604
605         pkt->b_status = nvhdcp->b_status;
606         pkt->value_flags |= TEGRA_NVHDCP_FLAG_BSTATUS;
607
608         /* copy most recent KSVFIFO, if it is non-zero */
609         pkt->num_bksv_list = nvhdcp->num_bksv_list;
610         if( nvhdcp->num_bksv_list ) {
611                 BUILD_BUG_ON(sizeof(pkt->bksv_list) != sizeof(nvhdcp->bksv_list));
612                 memcpy(pkt->bksv_list, nvhdcp->bksv_list,
613                         nvhdcp->num_bksv_list * sizeof(*pkt->bksv_list));
614                 pkt->value_flags |= TEGRA_NVHDCP_FLAG_BKSVLIST;
615         }
616
617         /* copy v_prime */
618         BUILD_BUG_ON(sizeof(pkt->v_prime) != sizeof(nvhdcp->v_prime));
619         memcpy(pkt->v_prime, nvhdcp->v_prime, sizeof(nvhdcp->v_prime));
620         pkt->value_flags |= TEGRA_NVHDCP_FLAG_V;
621
622         /* load Dksv */
623         pkt->d_ksv = get_dksv(hdmi);
624         if (verify_ksv(pkt->d_ksv)) {
625                 nvhdcp_err("Dksv invalid!\n");
626                 e = -EIO;
627                 goto err;
628         }
629         pkt->value_flags |= TEGRA_NVHDCP_FLAG_DKSV;
630
631         /* copy current Bksv */
632         pkt->b_ksv = nvhdcp->b_ksv;
633         pkt->value_flags |= TEGRA_NVHDCP_FLAG_BKSV;
634
635         pkt->packet_results = TEGRA_NVHDCP_RESULT_SUCCESS;
636         mutex_unlock(&nvhdcp->lock);
637         return 0;
638
639 err:
640         mutex_unlock(&nvhdcp->lock);
641         return e;
642 }
643
644 static int load_kfuse(struct tegra_dc_hdmi_data *hdmi)
645 {
646         unsigned buf[KFUSE_DATA_SZ / 4];
647         int e, i;
648         u32 ctrl;
649         u32 tmp;
650         int retries;
651
652         /* copy load kfuse into buffer - only needed for early Tegra parts */
653         e = tegra_kfuse_read(buf, sizeof buf);
654         if (e) {
655                 nvhdcp_err("Kfuse read failure\n");
656                 return e;
657         }
658
659         /* write the kfuse to HDMI SRAM */
660
661         tegra_hdmi_writel(hdmi, 1, HDMI_NV_PDISP_KEY_CTRL); /* LOAD_KEYS */
662
663         /* issue a reload */
664         ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_KEY_CTRL);
665         tegra_hdmi_writel(hdmi, ctrl | PKEY_REQUEST_RELOAD_TRIGGER
666                                         | LOCAL_KEYS , HDMI_NV_PDISP_KEY_CTRL);
667
668         e = wait_key_ctrl(hdmi, PKEY_LOADED);
669         if (e) {
670                 nvhdcp_err("key reload timeout\n");
671                 return -EIO;
672         }
673
674         tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_KEY_SKEY_INDEX);
675
676         /* wait for SRAM to be cleared */
677         retries = 6;
678         do {
679                 tmp = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_KEY_DEBUG0);
680                 if ((tmp & 1) == 0) break;
681                 if (retries > 1)
682                         mdelay(1);
683         } while (--retries);
684         if (!retries) {
685                 nvhdcp_err("key SRAM clear timeout\n");
686                 return -EIO;
687         }
688
689         for (i = 0; i < KFUSE_DATA_SZ / 4; i += 4) {
690
691                 /* load 128-bits*/
692                 tegra_hdmi_writel(hdmi, buf[i], HDMI_NV_PDISP_KEY_HDCP_KEY_0);
693                 tegra_hdmi_writel(hdmi, buf[i+1], HDMI_NV_PDISP_KEY_HDCP_KEY_1);
694                 tegra_hdmi_writel(hdmi, buf[i+2], HDMI_NV_PDISP_KEY_HDCP_KEY_2);
695                 tegra_hdmi_writel(hdmi, buf[i+3], HDMI_NV_PDISP_KEY_HDCP_KEY_3);
696
697                 /* trigger LOAD_HDCP_KEY */
698                 tegra_hdmi_writel(hdmi, 0x100, HDMI_NV_PDISP_KEY_HDCP_KEY_TRIG);
699
700                 tmp = LOCAL_KEYS | WRITE16;
701                 if (i)
702                         tmp |= AUTOINC;
703                 tegra_hdmi_writel(hdmi, tmp, HDMI_NV_PDISP_KEY_CTRL);
704
705                 /* wait for WRITE16 to complete */
706                 e = wait_key_ctrl(hdmi, 0x10); /* WRITE16 */
707                 if (e) {
708                         nvhdcp_err("key write timeout\n");
709                         return -EIO;
710                 }
711         }
712
713         return 0;
714 }
715
716 static int verify_link(struct tegra_nvhdcp *nvhdcp, bool wait_ri)
717 {
718         struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
719         int retries = 3;
720         u16 old, rx, tx;
721         int e;
722
723         old = 0;
724         rx = 0;
725         tx = 0;
726         /* retry 3 times to deal with I2C link issues */
727         do {
728                 if (wait_ri)
729                         old = get_transmitter_ri(hdmi);
730
731                 e = get_receiver_ri(nvhdcp, &rx);
732                 if (!e) {
733                         if (!rx) {
734                                 nvhdcp_err("Ri is 0!\n");
735                                 return -EINVAL;
736                         }
737
738                         tx = get_transmitter_ri(hdmi);
739                 } else {
740                         rx = ~tx;
741                         msleep(50);
742                 }
743
744         } while (wait_ri && --retries && old != tx);
745
746         nvhdcp_debug("R0 Ri poll:rx=0x%04x tx=0x%04x\n", rx, tx);
747
748         if (!nvhdcp_is_plugged(nvhdcp)) {
749                 nvhdcp_err("aborting verify links - lost hdmi connection\n");
750                 return -EIO;
751         }
752
753         if (rx != tx)
754                 return -EINVAL;
755
756         return 0;
757 }
758
759 static int get_repeater_info(struct tegra_nvhdcp *nvhdcp)
760 {
761         int e, retries;
762         u8 b_caps;
763         u16 b_status;
764
765         nvhdcp_vdbg("repeater found:fetching repeater info\n");
766
767         /* wait up to 5 seconds for READY on repeater */
768         retries = 51;
769         do {
770                 if (!nvhdcp_is_plugged(nvhdcp)) {
771                         nvhdcp_err("disconnect while waiting for repeater\n");
772                         return -EIO;
773                 }
774
775                 e = get_bcaps(nvhdcp, &b_caps);
776                 if (!e && (b_caps & BCAPS_READY)) {
777                         nvhdcp_debug("Bcaps READY from repeater\n");
778                         break;
779                 }
780                 if (retries > 1)
781                         msleep(100);
782         } while (--retries);
783         if (!retries) {
784                 nvhdcp_err("repeater Bcaps read timeout\n");
785                 return -ETIMEDOUT;
786         }
787
788         memset(nvhdcp->v_prime, 0, sizeof nvhdcp->v_prime);
789         e = get_vprime(nvhdcp, nvhdcp->v_prime);
790         if (e) {
791                 nvhdcp_err("repeater Vprime read failure!\n");
792                 return e;
793         }
794
795         e = nvhdcp_i2c_read16(nvhdcp, 0x41, &b_status);
796         if (e) {
797                 nvhdcp_err("Bstatus read failure!\n");
798                 return e;
799         }
800
801         if (b_status & BSTATUS_MAX_DEVS_EXCEEDED) {
802                 nvhdcp_err("repeater:max devices (0x%04x)\n", b_status);
803                 return -EINVAL;
804         }
805
806         if (b_status & BSTATUS_MAX_CASCADE_EXCEEDED) {
807                 nvhdcp_err("repeater:max cascade (0x%04x)\n", b_status);
808                 return -EINVAL;
809         }
810
811         nvhdcp->b_status = b_status;
812         nvhdcp->num_bksv_list = b_status & 0x7f;
813         nvhdcp_vdbg("Bstatus 0x%x (devices: %d)\n",
814                                 b_status, nvhdcp->num_bksv_list);
815
816         memset(nvhdcp->bksv_list, 0, sizeof nvhdcp->bksv_list);
817         e = get_ksvfifo(nvhdcp, nvhdcp->num_bksv_list, nvhdcp->bksv_list);
818         if (e) {
819                 nvhdcp_err("repeater:could not read KSVFIFO (err %d)\n", e);
820                 return e;
821         }
822
823         return 0;
824 }
825
826 static void nvhdcp_downstream_worker(struct work_struct *work)
827 {
828         struct tegra_nvhdcp *nvhdcp =
829                 container_of(work, struct tegra_nvhdcp, work);
830         struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
831         int e;
832         u8 b_caps;
833         u32 tmp;
834         u32 res;
835
836         nvhdcp_vdbg("%s():started thread %s\n", __func__, nvhdcp->name);
837
838         mutex_lock(&nvhdcp->lock);
839         if (nvhdcp->state == STATE_OFF) {
840                 nvhdcp_err("nvhdcp failure - giving up\n");
841                 goto err;
842         }
843         nvhdcp->state = STATE_UNAUTHENTICATED;
844
845         /* check plug state to terminate early in case flush_workqueue() */
846         if (!nvhdcp_is_plugged(nvhdcp)) {
847                 nvhdcp_err("worker started while unplugged!\n");
848                 goto lost_hdmi;
849         }
850         nvhdcp_vdbg("%s():hpd=%d\n", __func__, nvhdcp->plugged);
851
852         nvhdcp->a_ksv = 0;
853         nvhdcp->b_ksv = 0;
854         nvhdcp->a_n = 0;
855
856         e = get_bcaps(nvhdcp, &b_caps);
857         if (e) {
858                 nvhdcp_err("Bcaps read failure\n");
859                 goto failure;
860         }
861
862         nvhdcp_vdbg("read Bcaps = 0x%02x\n", b_caps);
863
864         nvhdcp_vdbg("kfuse loading ...\n");
865
866         /* repeater flag in Bskv must be configured before loading fuses */
867         set_bksv(hdmi, 0, (b_caps & BCAPS_REPEATER));
868
869         e = load_kfuse(hdmi);
870         if (e) {
871                 nvhdcp_err("kfuse could not be loaded\n");
872                 goto failure;
873         }
874
875         hdcp_ctrl_run(hdmi, 1);
876
877         nvhdcp_vdbg("wait AN_VALID ...\n");
878
879         /* wait for hardware to generate HDCP values */
880         e = wait_hdcp_ctrl(hdmi, AN_VALID | SROM_ERR, &res);
881         if (e) {
882                 nvhdcp_err("An key generation timeout\n");
883                 goto failure;
884         }
885         if (res & SROM_ERR) {
886                 nvhdcp_err("SROM error\n");
887                 goto failure;
888         }
889
890         msleep(25);
891
892         nvhdcp->a_ksv = get_aksv(hdmi);
893         nvhdcp->a_n = get_an(hdmi);
894         nvhdcp_vdbg("Aksv is 0x%016llx\n", nvhdcp->a_ksv);
895         nvhdcp_vdbg("An is 0x%016llx\n", nvhdcp->a_n);
896         if (verify_ksv(nvhdcp->a_ksv)) {
897                 nvhdcp_err("Aksv verify failure! (0x%016llx)\n", nvhdcp->a_ksv);
898                 goto failure;
899         }
900
901         /* write Ainfo to receiver - set 1.1 only if b_caps supports it */
902         e = nvhdcp_i2c_write8(nvhdcp, 0x15, b_caps & BCAPS_11);
903         if (e) {
904                 nvhdcp_err("Ainfo write failure\n");
905                 goto failure;
906         }
907
908         /* write An to receiver */
909         e = nvhdcp_i2c_write64(nvhdcp, 0x18, nvhdcp->a_n);
910         if (e) {
911                 nvhdcp_err("An write failure\n");
912                 goto failure;
913         }
914
915         nvhdcp_vdbg("wrote An = 0x%016llx\n", nvhdcp->a_n);
916
917         /* write Aksv to receiver - triggers auth sequence */
918         e = nvhdcp_i2c_write40(nvhdcp, 0x10, nvhdcp->a_ksv);
919         if (e) {
920                 nvhdcp_err("Aksv write failure\n");
921                 goto failure;
922         }
923
924         nvhdcp_vdbg("wrote Aksv = 0x%010llx\n", nvhdcp->a_ksv);
925
926         /* bail out if unplugged in the middle of negotiation */
927         if (!nvhdcp_is_plugged(nvhdcp))
928                 goto lost_hdmi;
929
930         /* get Bksv from receiver */
931         e = nvhdcp_i2c_read40(nvhdcp, 0x00, &nvhdcp->b_ksv);
932         if (e) {
933                 nvhdcp_err("Bksv read failure\n");
934                 goto failure;
935         }
936         nvhdcp_vdbg("Bksv is 0x%016llx\n", nvhdcp->b_ksv);
937         if (verify_ksv(nvhdcp->b_ksv)) {
938                 nvhdcp_err("Bksv verify failure!\n");
939                 goto failure;
940         }
941
942         nvhdcp_vdbg("read Bksv = 0x%010llx from device\n", nvhdcp->b_ksv);
943
944         set_bksv(hdmi, nvhdcp->b_ksv, (b_caps & BCAPS_REPEATER));
945
946         nvhdcp_vdbg("loaded Bksv into controller\n");
947
948         e = wait_hdcp_ctrl(hdmi, R0_VALID, NULL);
949         if (e) {
950                 nvhdcp_err("R0 read failure!\n");
951                 goto failure;
952         }
953
954         nvhdcp_vdbg("R0 valid\n");
955
956         msleep(100); /* can't read R0' within 100ms of writing Aksv */
957
958         nvhdcp_vdbg("verifying links ...\n");
959
960         e = verify_link(nvhdcp, false);
961         if (e) {
962                 nvhdcp_err("link verification failed err %d\n", e);
963                 goto failure;
964         }
965
966         tmp = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
967         tmp |= CRYPT_ENABLED;
968         if (b_caps & BCAPS_11) /* HDCP 1.1 ? */
969                 tmp |= ONEONE_ENABLED;
970         tegra_hdmi_writel(hdmi, tmp, HDMI_NV_PDISP_RG_HDCP_CTRL);
971
972         nvhdcp_vdbg("CRYPT enabled\n");
973
974         /* if repeater then get repeater info */
975         if (b_caps & BCAPS_REPEATER) {
976                 e = get_repeater_info(nvhdcp);
977                 if (e) {
978                         nvhdcp_err("get repeater info failed\n");
979                         goto failure;
980                 }
981         }
982
983         nvhdcp->state = STATE_LINK_VERIFY;
984         nvhdcp_info("link verified!\n");
985
986         while (1) {
987                 if (!nvhdcp_is_plugged(nvhdcp))
988                         goto lost_hdmi;
989
990                 if (nvhdcp->state != STATE_LINK_VERIFY)
991                         goto failure;
992
993                 e = verify_link(nvhdcp, true);
994                 if (e) {
995                         nvhdcp_err("link verification failed err %d\n", e);
996                         goto failure;
997                 }
998                 mutex_unlock(&nvhdcp->lock);
999                 msleep(1500);
1000                 mutex_lock(&nvhdcp->lock);
1001
1002         }
1003
1004 failure:
1005         nvhdcp->fail_count++;
1006         if(nvhdcp->fail_count > 5) {
1007                 nvhdcp_err("nvhdcp failure - too many failures, giving up!\n");
1008         } else {
1009                 nvhdcp_err("nvhdcp failure - renegotiating in 1.75 seconds\n");
1010                 mutex_unlock(&nvhdcp->lock);
1011                 msleep(1750);
1012                 mutex_lock(&nvhdcp->lock);
1013                 queue_work(nvhdcp->downstream_wq, &nvhdcp->work);
1014         }
1015
1016 lost_hdmi:
1017         nvhdcp->state = STATE_UNAUTHENTICATED;
1018         hdcp_ctrl_run(hdmi, 0);
1019
1020 err:
1021         mutex_unlock(&nvhdcp->lock);
1022         return;
1023 }
1024
1025 static int tegra_nvhdcp_on(struct tegra_nvhdcp *nvhdcp)
1026 {
1027         nvhdcp->state = STATE_UNAUTHENTICATED;
1028         if (nvhdcp_is_plugged(nvhdcp)) {
1029                 nvhdcp->fail_count = 0;
1030                 queue_work(nvhdcp->downstream_wq, &nvhdcp->work);
1031         }
1032         return 0;
1033 }
1034
1035 static int tegra_nvhdcp_off(struct tegra_nvhdcp *nvhdcp)
1036 {
1037         mutex_lock(&nvhdcp->lock);
1038         nvhdcp->state = STATE_OFF;
1039         nvhdcp_set_plugged(nvhdcp, false);
1040         mutex_unlock(&nvhdcp->lock);
1041         flush_workqueue(nvhdcp->downstream_wq);
1042         return 0;
1043 }
1044
1045 void tegra_nvhdcp_set_plug(struct tegra_nvhdcp *nvhdcp, bool hpd)
1046 {
1047         nvhdcp_debug("hdmi hotplug detected (hpd = %d)\n", hpd);
1048
1049         if (hpd) {
1050                 nvhdcp_set_plugged(nvhdcp, true);
1051                 tegra_nvhdcp_on(nvhdcp);
1052         } else {
1053                 tegra_nvhdcp_off(nvhdcp);
1054         }
1055 }
1056
1057 int tegra_nvhdcp_set_policy(struct tegra_nvhdcp *nvhdcp, int pol)
1058 {
1059         if (pol == TEGRA_NVHDCP_POLICY_ALWAYS_ON) {
1060                 nvhdcp_info("using \"always on\" policy.\n");
1061                 if (atomic_xchg(&nvhdcp->policy, pol) != pol) {
1062                         /* policy changed, start working */
1063                         tegra_nvhdcp_on(nvhdcp);
1064                 }
1065         } else {
1066                 /* unsupported policy */
1067                 return -EINVAL;
1068         }
1069
1070         return 0;
1071 }
1072
1073 static int tegra_nvhdcp_renegotiate(struct tegra_nvhdcp *nvhdcp)
1074 {
1075         mutex_lock(&nvhdcp->lock);
1076         nvhdcp->state = STATE_RENEGOTIATE;
1077         mutex_unlock(&nvhdcp->lock);
1078         tegra_nvhdcp_on(nvhdcp);
1079         return 0;
1080 }
1081
1082 void tegra_nvhdcp_suspend(struct tegra_nvhdcp *nvhdcp)
1083 {
1084         if (!nvhdcp) return;
1085         tegra_nvhdcp_off(nvhdcp);
1086 }
1087
1088
1089 static long nvhdcp_dev_ioctl(struct file *filp,
1090                 unsigned int cmd, unsigned long arg)
1091 {
1092         struct tegra_nvhdcp *nvhdcp = filp->private_data;
1093         struct tegra_nvhdcp_packet *pkt;
1094         int e = -ENOTTY;
1095
1096         switch (cmd) {
1097         case TEGRAIO_NVHDCP_ON:
1098                 return tegra_nvhdcp_on(nvhdcp);
1099
1100         case TEGRAIO_NVHDCP_OFF:
1101                 return tegra_nvhdcp_off(nvhdcp);
1102
1103         case TEGRAIO_NVHDCP_SET_POLICY:
1104                 return tegra_nvhdcp_set_policy(nvhdcp, arg);
1105
1106         case TEGRAIO_NVHDCP_READ_M:
1107                 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1108                 if (!pkt)
1109                         return -ENOMEM;
1110                 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1111                         e = -EFAULT;
1112                         goto kfree_pkt;
1113                 }
1114                 e = get_m_prime(nvhdcp, pkt);
1115                 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1116                         e = -EFAULT;
1117                         goto kfree_pkt;
1118                 }
1119                 kfree(pkt);
1120                 return e;
1121
1122         case TEGRAIO_NVHDCP_READ_S:
1123                 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1124                 if (!pkt)
1125                         return -ENOMEM;
1126                 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1127                         e = -EFAULT;
1128                         goto kfree_pkt;
1129                 }
1130                 e = get_s_prime(nvhdcp, pkt);
1131                 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1132                         e = -EFAULT;
1133                         goto kfree_pkt;
1134                 }
1135                 kfree(pkt);
1136                 return e;
1137
1138         case TEGRAIO_NVHDCP_RENEGOTIATE:
1139                 e = tegra_nvhdcp_renegotiate(nvhdcp);
1140                 break;
1141         }
1142
1143         return e;
1144 kfree_pkt:
1145         kfree(pkt);
1146         return e;
1147 }
1148
1149 static int nvhdcp_dev_open(struct inode *inode, struct file *filp)
1150 {
1151         struct miscdevice *miscdev = filp->private_data;
1152         struct tegra_nvhdcp *nvhdcp =
1153                 container_of(miscdev, struct tegra_nvhdcp, miscdev);
1154         filp->private_data = nvhdcp;
1155         return 0;
1156 }
1157
1158 static int nvhdcp_dev_release(struct inode *inode, struct file *filp)
1159 {
1160         filp->private_data = NULL;
1161         return 0;
1162 }
1163
1164 static const struct file_operations nvhdcp_fops = {
1165         .owner          = THIS_MODULE,
1166         .llseek         = no_llseek,
1167         .unlocked_ioctl = nvhdcp_dev_ioctl,
1168         .open           = nvhdcp_dev_open,
1169         .release        = nvhdcp_dev_release,
1170 };
1171
1172 /* we only support one AP right now, so should only call this once. */
1173 struct tegra_nvhdcp *tegra_nvhdcp_create(struct tegra_dc_hdmi_data *hdmi,
1174                         int id, int bus)
1175 {
1176         static struct tegra_nvhdcp *nvhdcp; /* prevent multiple calls */
1177         struct i2c_adapter *adapter;
1178         int e;
1179
1180         if (nvhdcp)
1181                 return ERR_PTR(-EMFILE);
1182
1183         nvhdcp = kzalloc(sizeof(*nvhdcp), GFP_KERNEL);
1184         if (!nvhdcp)
1185                 return ERR_PTR(-ENOMEM);
1186
1187         nvhdcp->id = id;
1188         snprintf(nvhdcp->name, sizeof(nvhdcp->name), "nvhdcp%u", id);
1189         nvhdcp->hdmi = hdmi;
1190         mutex_init(&nvhdcp->lock);
1191
1192         strlcpy(nvhdcp->info.type, nvhdcp->name, sizeof(nvhdcp->info.type));
1193         nvhdcp->bus = bus;
1194         nvhdcp->info.addr = 0x74 >> 1;
1195         nvhdcp->info.platform_data = nvhdcp;
1196         nvhdcp->fail_count = 0;
1197
1198         adapter = i2c_get_adapter(bus);
1199         if (!adapter) {
1200                 nvhdcp_err("can't get adapter for bus %d\n", bus);
1201                 e = -EBUSY;
1202                 goto free_nvhdcp;
1203         }
1204
1205         nvhdcp->client = i2c_new_device(adapter, &nvhdcp->info);
1206         i2c_put_adapter(adapter);
1207
1208         if (!nvhdcp->client) {
1209                 nvhdcp_err("can't create new device\n");
1210                 e = -EBUSY;
1211                 goto free_nvhdcp;
1212         }
1213
1214         nvhdcp->state = STATE_UNAUTHENTICATED;
1215
1216         nvhdcp->downstream_wq = create_singlethread_workqueue(nvhdcp->name);
1217         INIT_WORK(&nvhdcp->work, nvhdcp_downstream_worker);
1218
1219         nvhdcp->miscdev.minor = MISC_DYNAMIC_MINOR;
1220         nvhdcp->miscdev.name = nvhdcp->name;
1221         nvhdcp->miscdev.fops = &nvhdcp_fops;
1222
1223         e = misc_register(&nvhdcp->miscdev);
1224         if (e)
1225                 goto free_workqueue;
1226
1227         nvhdcp_vdbg("%s(): created misc device %s\n", __func__, nvhdcp->name);
1228
1229         return nvhdcp;
1230 free_workqueue:
1231         destroy_workqueue(nvhdcp->downstream_wq);
1232         i2c_release_client(nvhdcp->client);
1233 free_nvhdcp:
1234         kfree(nvhdcp);
1235         nvhdcp_err("unable to create device.\n");
1236         return ERR_PTR(e);
1237 }
1238
1239 void tegra_nvhdcp_destroy(struct tegra_nvhdcp *nvhdcp)
1240 {
1241         misc_deregister(&nvhdcp->miscdev);
1242         tegra_nvhdcp_off(nvhdcp);
1243         destroy_workqueue(nvhdcp->downstream_wq);
1244         i2c_release_client(nvhdcp->client);
1245         kfree(nvhdcp);
1246 }