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