Revert "video: tegra: hdcp: Fix Locking Issue"
[linux-2.6.git] / drivers / video / tegra / dc / nvhdcp.c
1 /*
2  * drivers/video/tegra/dc/nvhdcp.c
3  *
4  * Copyright (c) 2010-2012, 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/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/kfuse.h>
30
31 #include <video/nvhdcp.h>
32
33 #include "dc_reg.h"
34 #include "dc_priv.h"
35 #include "hdmi_reg.h"
36 #include "hdmi.h"
37
38 DECLARE_WAIT_QUEUE_HEAD(wq_worker);
39
40 /* for 0x40 Bcaps */
41 #define BCAPS_REPEATER (1 << 6)
42 #define BCAPS_READY (1 << 5)
43 #define BCAPS_11 (1 << 1) /* used for both Bcaps and Ainfo */
44
45 /* for 0x41 Bstatus */
46 #define BSTATUS_MAX_DEVS_EXCEEDED       (1 << 7)
47 #define BSTATUS_MAX_CASCADE_EXCEEDED    (1 << 11)
48
49 #ifdef VERBOSE_DEBUG
50 #define nvhdcp_vdbg(...)        \
51                 printk("nvhdcp: " __VA_ARGS__)
52 #else
53 #define nvhdcp_vdbg(...)                \
54 ({                                              \
55         if(0)                                   \
56                 printk("nvhdcp: " __VA_ARGS__); \
57         0;                                      \
58 })
59 #endif
60 #define nvhdcp_debug(...)       \
61                 pr_debug("nvhdcp: " __VA_ARGS__)
62 #define nvhdcp_err(...) \
63                 pr_err("nvhdcp: Error: " __VA_ARGS__)
64 #define nvhdcp_info(...)        \
65                 pr_info("nvhdcp: " __VA_ARGS__)
66
67
68 /* for nvhdcp.state */
69 enum tegra_nvhdcp_state {
70         STATE_OFF,
71         STATE_UNAUTHENTICATED,
72         STATE_LINK_VERIFY,
73         STATE_RENEGOTIATE,
74 };
75
76 struct tegra_nvhdcp {
77         struct delayed_work             work;
78         struct tegra_dc_hdmi_data       *hdmi;
79         struct workqueue_struct         *downstream_wq;
80         struct mutex                    lock;
81         struct miscdevice               miscdev;
82         char                            name[12];
83         unsigned                        id;
84         bool                            plugged; /* true if hotplug detected */
85         atomic_t                        policy; /* set policy */
86         enum tegra_nvhdcp_state         state; /* STATE_xxx */
87         struct i2c_client               *client;
88         struct i2c_board_info           info;
89         int                             bus;
90         u32                             b_status;
91         u64                             a_n;
92         u64                             c_n;
93         u64                             a_ksv;
94         u64                             b_ksv;
95         u64                             c_ksv;
96         u64                             d_ksv;
97         u8                              v_prime[20];
98         u64                             m_prime;
99         u32                             num_bksv_list;
100         u64                             bksv_list[TEGRA_NVHDCP_MAX_DEVS];
101         int                             fail_count;
102 };
103
104 static inline bool nvhdcp_is_plugged(struct tegra_nvhdcp *nvhdcp)
105 {
106         rmb();
107         return nvhdcp->plugged;
108 }
109
110 static inline bool nvhdcp_set_plugged(struct tegra_nvhdcp *nvhdcp, bool plugged)
111 {
112         nvhdcp->plugged = plugged;
113         wmb();
114         return plugged;
115 }
116
117 static int nvhdcp_i2c_read(struct tegra_nvhdcp *nvhdcp, u8 reg,
118                                         size_t len, void *data)
119 {
120         int status;
121         int retries = 15;
122         struct i2c_msg msg[] = {
123                 {
124                         .addr = 0x74 >> 1, /* primary link */
125                         .flags = 0,
126                         .len = 1,
127                         .buf = &reg,
128                 },
129                 {
130                         .addr = 0x74 >> 1, /* primary link */
131                         .flags = I2C_M_RD,
132                         .len = len,
133                         .buf = data,
134                 },
135         };
136
137         do {
138                 if (!nvhdcp_is_plugged(nvhdcp)) {
139                         nvhdcp_err("disconnect during i2c xfer\n");
140                         return -EIO;
141                 }
142                 status = i2c_transfer(nvhdcp->client->adapter,
143                         msg, ARRAY_SIZE(msg));
144                 if ((status < 0) && (retries > 1))
145                         msleep(250);
146         } while ((status < 0) && retries--);
147
148         if (status < 0) {
149                 nvhdcp_err("i2c xfer error %d\n", status);
150                 return status;
151         }
152
153         return 0;
154 }
155
156 static int nvhdcp_i2c_write(struct tegra_nvhdcp *nvhdcp, u8 reg,
157                                         size_t len, const void *data)
158 {
159         int status;
160         u8 buf[len + 1];
161         struct i2c_msg msg[] = {
162                 {
163                         .addr = 0x74 >> 1, /* primary link */
164                         .flags = 0,
165                         .len = len + 1,
166                         .buf = buf,
167                 },
168         };
169         int retries = 15;
170
171         buf[0] = reg;
172         memcpy(buf + 1, data, len);
173
174         do {
175                 if (!nvhdcp_is_plugged(nvhdcp)) {
176                         nvhdcp_err("disconnect during i2c xfer\n");
177                         return -EIO;
178                 }
179                 status = i2c_transfer(nvhdcp->client->adapter,
180                         msg, ARRAY_SIZE(msg));
181                 if ((status < 0) && (retries > 1))
182                         msleep(250);
183         } while ((status < 0) && retries--);
184
185         if (status < 0) {
186                 nvhdcp_err("i2c xfer error %d\n", status);
187                 return status;
188         }
189
190         return 0;
191 }
192
193 static inline int nvhdcp_i2c_read8(struct tegra_nvhdcp *nvhdcp, u8 reg, u8 *val)
194 {
195         return nvhdcp_i2c_read(nvhdcp, reg, 1, val);
196 }
197
198 static inline int nvhdcp_i2c_write8(struct tegra_nvhdcp *nvhdcp, u8 reg, u8 val)
199 {
200         return nvhdcp_i2c_write(nvhdcp, reg, 1, &val);
201 }
202
203 static inline int nvhdcp_i2c_read16(struct tegra_nvhdcp *nvhdcp,
204                                         u8 reg, u16 *val)
205 {
206         u8 buf[2];
207         int e;
208
209         e = nvhdcp_i2c_read(nvhdcp, reg, sizeof buf, buf);
210         if (e)
211                 return e;
212
213         if (val)
214                 *val = buf[0] | (u16)buf[1] << 8;
215
216         return 0;
217 }
218
219 static int nvhdcp_i2c_read40(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 *val)
220 {
221         u8 buf[5];
222         int e, i;
223         u64 n;
224
225         e = nvhdcp_i2c_read(nvhdcp, reg, sizeof buf, buf);
226         if (e)
227                 return e;
228
229         for(i = 0, n = 0; i < 5; i++ ) {
230                 n <<= 8;
231                 n |= buf[4 - i];
232         }
233
234         if (val)
235                 *val = n;
236
237         return 0;
238 }
239
240 static int nvhdcp_i2c_write40(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 val)
241 {
242         char buf[5];
243         int i;
244         for(i = 0; i < 5; i++ ) {
245                 buf[i] = val;
246                 val >>= 8;
247         }
248         return nvhdcp_i2c_write(nvhdcp, reg, sizeof buf, buf);
249 }
250
251 static int nvhdcp_i2c_write64(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 val)
252 {
253         char buf[8];
254         int i;
255         for(i = 0; i < 8; i++ ) {
256                 buf[i] = val;
257                 val >>= 8;
258         }
259         return nvhdcp_i2c_write(nvhdcp, reg, sizeof buf, buf);
260 }
261
262
263 /* 64-bit link encryption session random number */
264 static inline u64 get_an(struct tegra_dc_hdmi_data *hdmi)
265 {
266         u64 r;
267         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AN_MSB) << 32;
268         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AN_LSB);
269         return r;
270 }
271
272 /* 64-bit upstream exchange random number */
273 static inline void set_cn(struct tegra_dc_hdmi_data *hdmi, u64 c_n)
274 {
275         tegra_hdmi_writel(hdmi, (u32)c_n, HDMI_NV_PDISP_RG_HDCP_CN_LSB);
276         tegra_hdmi_writel(hdmi, c_n >> 32, HDMI_NV_PDISP_RG_HDCP_CN_MSB);
277 }
278
279
280 /* 40-bit transmitter's key selection vector */
281 static inline u64 get_aksv(struct tegra_dc_hdmi_data *hdmi)
282 {
283         u64 r;
284         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AKSV_MSB) << 32;
285         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AKSV_LSB);
286         return r;
287 }
288
289 /* 40-bit receiver's key selection vector */
290 static inline void set_bksv(struct tegra_dc_hdmi_data *hdmi, u64 b_ksv, bool repeater)
291 {
292         if (repeater)
293                 b_ksv |= (u64)REPEATER << 32;
294         tegra_hdmi_writel(hdmi, (u32)b_ksv, HDMI_NV_PDISP_RG_HDCP_BKSV_LSB);
295         tegra_hdmi_writel(hdmi, b_ksv >> 32, HDMI_NV_PDISP_RG_HDCP_BKSV_MSB);
296 }
297
298
299 /* 40-bit software's key selection vector */
300 static inline void set_cksv(struct tegra_dc_hdmi_data *hdmi, u64 c_ksv)
301 {
302         tegra_hdmi_writel(hdmi, (u32)c_ksv, HDMI_NV_PDISP_RG_HDCP_CKSV_LSB);
303         tegra_hdmi_writel(hdmi, c_ksv >> 32, HDMI_NV_PDISP_RG_HDCP_CKSV_MSB);
304 }
305
306 /* 40-bit connection state */
307 static inline u64 get_cs(struct tegra_dc_hdmi_data *hdmi)
308 {
309         u64 r;
310         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CS_MSB) << 32;
311         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CS_LSB);
312         return r;
313 }
314
315 /* 40-bit upstream key selection vector */
316 static inline u64 get_dksv(struct tegra_dc_hdmi_data *hdmi)
317 {
318         u64 r;
319         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_DKSV_MSB) << 32;
320         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_DKSV_LSB);
321         return r;
322 }
323
324 /* 64-bit encrypted M0 value */
325 static inline u64 get_mprime(struct tegra_dc_hdmi_data *hdmi)
326 {
327         u64 r;
328         r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB) << 32;
329         r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB);
330         return r;
331 }
332
333 static inline u16 get_transmitter_ri(struct tegra_dc_hdmi_data *hdmi)
334 {
335         return tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_RI);
336 }
337
338 static inline int get_receiver_ri(struct tegra_nvhdcp *nvhdcp, u16 *r)
339 {
340         return nvhdcp_i2c_read16(nvhdcp, 0x8, r); /* long read */
341 }
342
343 static int get_bcaps(struct tegra_nvhdcp *nvhdcp, u8 *b_caps)
344 {
345         return nvhdcp_i2c_read8(nvhdcp, 0x40, b_caps);
346 }
347
348 static int get_ksvfifo(struct tegra_nvhdcp *nvhdcp,
349                                         unsigned num_bksv_list, u64 *ksv_list)
350 {
351         u8 *buf, *p;
352         int e;
353         unsigned i;
354         size_t buf_len = num_bksv_list * 5;
355
356         if (!ksv_list || num_bksv_list > TEGRA_NVHDCP_MAX_DEVS)
357                 return -EINVAL;
358
359         if (num_bksv_list == 0)
360                 return 0;
361
362         buf = kmalloc(buf_len, GFP_KERNEL);
363         if (IS_ERR_OR_NULL(buf))
364                 return -ENOMEM;
365
366         e = nvhdcp_i2c_read(nvhdcp, 0x43, buf_len, buf);
367         if (e) {
368                 kfree(buf);
369                 return e;
370         }
371
372         /* load 40-bit keys from repeater into array of u64 */
373         p = buf;
374         for (i = 0; i < num_bksv_list; i++) {
375                 ksv_list[i] = p[0] | ((u64)p[1] << 8) | ((u64)p[2] << 16)
376                                 | ((u64)p[3] << 24) | ((u64)p[4] << 32);
377                 p += 5;
378         }
379
380         kfree(buf);
381         return 0;
382 }
383
384 /* get V' 160-bit SHA-1 hash from repeater */
385 static int get_vprime(struct tegra_nvhdcp *nvhdcp, u8 *v_prime)
386 {
387         int e, i;
388
389         for (i = 0; i < 20; i += 4) {
390                 e = nvhdcp_i2c_read(nvhdcp, 0x20 + i, 4, v_prime + i);
391                 if (e)
392                         return e;
393         }
394         return 0;
395 }
396
397
398 /* set or clear RUN_YES */
399 static void hdcp_ctrl_run(struct tegra_dc_hdmi_data *hdmi, bool v)
400 {
401         u32 ctrl;
402
403         if (v) {
404                 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
405                 ctrl |= HDCP_RUN_YES;
406         } else {
407                 ctrl = 0;
408         }
409
410         tegra_hdmi_writel(hdmi, ctrl, HDMI_NV_PDISP_RG_HDCP_CTRL);
411 }
412
413 /* wait for any bits in mask to be set in HDMI_NV_PDISP_RG_HDCP_CTRL
414  * sleeps up to 120mS */
415 static int wait_hdcp_ctrl(struct tegra_dc_hdmi_data *hdmi, u32 mask, u32 *v)
416 {
417         int retries = 13;
418         u32 ctrl;
419
420         do {
421                 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
422                 if ((ctrl & mask)) {
423                         if (v)
424                                 *v = ctrl;
425                         break;
426                 }
427                 if (retries > 1)
428                         msleep(10);
429         } while (--retries);
430         if (!retries) {
431                 nvhdcp_err("ctrl read timeout (mask=0x%x)\n", mask);
432                 return -EIO;
433         }
434         return 0;
435 }
436
437 /* wait for bits in mask to be set to value in HDMI_NV_PDISP_KEY_CTRL
438  * waits up to 100mS */
439 static int wait_key_ctrl(struct tegra_dc_hdmi_data *hdmi, u32 mask, u32 value)
440 {
441         int retries = 101;
442         u32 ctrl;
443
444         do {
445                 msleep(1);
446                 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_KEY_CTRL);
447                 if (((ctrl ^ value) & mask) == 0)
448                         break;
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, 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, 0); /* 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(to_delayed_work(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 disable;
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         /* if repeater then get repeater info */
967         if (b_caps & BCAPS_REPEATER) {
968                 e = get_repeater_info(nvhdcp);
969                 if (e) {
970                         nvhdcp_err("get repeater info failed\n");
971                         goto failure;
972                 }
973         }
974
975         tmp = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
976         tmp |= CRYPT_ENABLED;
977         if (b_caps & BCAPS_11) /* HDCP 1.1 ? */
978                 tmp |= ONEONE_ENABLED;
979         tegra_hdmi_writel(hdmi, tmp, HDMI_NV_PDISP_RG_HDCP_CTRL);
980
981         nvhdcp_vdbg("CRYPT enabled\n");
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                 wait_event_interruptible_timeout(wq_worker,
1000                         !nvhdcp_is_plugged(nvhdcp), msecs_to_jiffies(1500));
1001                 mutex_lock(&nvhdcp->lock);
1002
1003         }
1004
1005 failure:
1006         nvhdcp->fail_count++;
1007         if(nvhdcp->fail_count > 5) {
1008                 nvhdcp_err("nvhdcp failure - too many failures, giving up!\n");
1009         } else {
1010                 nvhdcp_err("nvhdcp failure - renegotiating in 1 second\n");
1011                 if (!nvhdcp_is_plugged(nvhdcp))
1012                         goto lost_hdmi;
1013                 queue_delayed_work(nvhdcp->downstream_wq, &nvhdcp->work,
1014                                                 msecs_to_jiffies(1000));
1015         }
1016
1017 lost_hdmi:
1018         nvhdcp->state = STATE_UNAUTHENTICATED;
1019         hdcp_ctrl_run(hdmi, 0);
1020
1021 err:
1022         mutex_unlock(&nvhdcp->lock);
1023         return;
1024 disable:
1025         nvhdcp->state = STATE_OFF;
1026         nvhdcp_set_plugged(nvhdcp, false);
1027         mutex_unlock(&nvhdcp->lock);
1028         return;
1029 }
1030
1031 static int tegra_nvhdcp_on(struct tegra_nvhdcp *nvhdcp)
1032 {
1033         nvhdcp->state = STATE_UNAUTHENTICATED;
1034         if (nvhdcp_is_plugged(nvhdcp)) {
1035                 nvhdcp->fail_count = 0;
1036                 queue_delayed_work(nvhdcp->downstream_wq, &nvhdcp->work,
1037                                                 msecs_to_jiffies(100));
1038         }
1039         return 0;
1040 }
1041
1042 static int tegra_nvhdcp_off(struct tegra_nvhdcp *nvhdcp)
1043 {
1044         mutex_lock(&nvhdcp->lock);
1045         nvhdcp->state = STATE_OFF;
1046         nvhdcp_set_plugged(nvhdcp, false);
1047         mutex_unlock(&nvhdcp->lock);
1048         wake_up_interruptible(&wq_worker);
1049         flush_workqueue(nvhdcp->downstream_wq);
1050         return 0;
1051 }
1052
1053 void tegra_nvhdcp_set_plug(struct tegra_nvhdcp *nvhdcp, bool hpd)
1054 {
1055         nvhdcp_debug("hdmi hotplug detected (hpd = %d)\n", hpd);
1056
1057         if (hpd) {
1058                 nvhdcp_set_plugged(nvhdcp, true);
1059                 tegra_nvhdcp_on(nvhdcp);
1060         } else {
1061                 tegra_nvhdcp_off(nvhdcp);
1062         }
1063 }
1064
1065 int tegra_nvhdcp_set_policy(struct tegra_nvhdcp *nvhdcp, int pol)
1066 {
1067         if (pol == TEGRA_NVHDCP_POLICY_ALWAYS_ON) {
1068                 nvhdcp_info("using \"always on\" policy.\n");
1069                 if (atomic_xchg(&nvhdcp->policy, pol) != pol) {
1070                         /* policy changed, start working */
1071                         tegra_nvhdcp_on(nvhdcp);
1072                 }
1073         } else {
1074                 /* unsupported policy */
1075                 return -EINVAL;
1076         }
1077
1078         return 0;
1079 }
1080
1081 static int tegra_nvhdcp_renegotiate(struct tegra_nvhdcp *nvhdcp)
1082 {
1083         mutex_lock(&nvhdcp->lock);
1084         nvhdcp->state = STATE_RENEGOTIATE;
1085         mutex_unlock(&nvhdcp->lock);
1086         tegra_nvhdcp_on(nvhdcp);
1087         return 0;
1088 }
1089
1090 void tegra_nvhdcp_suspend(struct tegra_nvhdcp *nvhdcp)
1091 {
1092         if (!nvhdcp) return;
1093         tegra_nvhdcp_off(nvhdcp);
1094 }
1095
1096 void tegra_nvhdcp_resume(struct tegra_nvhdcp *nvhdcp)
1097 {
1098         if (!nvhdcp) return;
1099         tegra_nvhdcp_renegotiate(nvhdcp);
1100 }
1101
1102 static long nvhdcp_dev_ioctl(struct file *filp,
1103                 unsigned int cmd, unsigned long arg)
1104 {
1105         struct tegra_nvhdcp *nvhdcp = filp->private_data;
1106         struct tegra_nvhdcp_packet *pkt;
1107         int e = -ENOTTY;
1108
1109         switch (cmd) {
1110         case TEGRAIO_NVHDCP_ON:
1111                 return tegra_nvhdcp_on(nvhdcp);
1112
1113         case TEGRAIO_NVHDCP_OFF:
1114                 return tegra_nvhdcp_off(nvhdcp);
1115
1116         case TEGRAIO_NVHDCP_SET_POLICY:
1117                 return tegra_nvhdcp_set_policy(nvhdcp, arg);
1118
1119         case TEGRAIO_NVHDCP_READ_M:
1120                 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1121                 if (!pkt)
1122                         return -ENOMEM;
1123                 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1124                         e = -EFAULT;
1125                         goto kfree_pkt;
1126                 }
1127                 e = get_m_prime(nvhdcp, pkt);
1128                 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1129                         e = -EFAULT;
1130                         goto kfree_pkt;
1131                 }
1132                 kfree(pkt);
1133                 return e;
1134
1135         case TEGRAIO_NVHDCP_READ_S:
1136                 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1137                 if (!pkt)
1138                         return -ENOMEM;
1139                 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1140                         e = -EFAULT;
1141                         goto kfree_pkt;
1142                 }
1143                 e = get_s_prime(nvhdcp, pkt);
1144                 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1145                         e = -EFAULT;
1146                         goto kfree_pkt;
1147                 }
1148                 kfree(pkt);
1149                 return e;
1150
1151         case TEGRAIO_NVHDCP_RENEGOTIATE:
1152                 e = tegra_nvhdcp_renegotiate(nvhdcp);
1153                 break;
1154         }
1155
1156         return e;
1157 kfree_pkt:
1158         kfree(pkt);
1159         return e;
1160 }
1161
1162 static int nvhdcp_dev_open(struct inode *inode, struct file *filp)
1163 {
1164         struct miscdevice *miscdev = filp->private_data;
1165         struct tegra_nvhdcp *nvhdcp =
1166                 container_of(miscdev, struct tegra_nvhdcp, miscdev);
1167         filp->private_data = nvhdcp;
1168         return 0;
1169 }
1170
1171 static int nvhdcp_dev_release(struct inode *inode, struct file *filp)
1172 {
1173         filp->private_data = NULL;
1174         return 0;
1175 }
1176
1177 static const struct file_operations nvhdcp_fops = {
1178         .owner          = THIS_MODULE,
1179         .llseek         = no_llseek,
1180         .unlocked_ioctl = nvhdcp_dev_ioctl,
1181         .open           = nvhdcp_dev_open,
1182         .release        = nvhdcp_dev_release,
1183 };
1184
1185 /* we only support one AP right now, so should only call this once. */
1186 struct tegra_nvhdcp *tegra_nvhdcp_create(struct tegra_dc_hdmi_data *hdmi,
1187                         int id, int bus)
1188 {
1189         static struct tegra_nvhdcp *nvhdcp; /* prevent multiple calls */
1190         struct i2c_adapter *adapter;
1191         int e;
1192
1193         if (nvhdcp)
1194                 return ERR_PTR(-EMFILE);
1195
1196         nvhdcp = kzalloc(sizeof(*nvhdcp), GFP_KERNEL);
1197         if (!nvhdcp)
1198                 return ERR_PTR(-ENOMEM);
1199
1200         nvhdcp->id = id;
1201         snprintf(nvhdcp->name, sizeof(nvhdcp->name), "nvhdcp%u", id);
1202         nvhdcp->hdmi = hdmi;
1203         mutex_init(&nvhdcp->lock);
1204
1205         strlcpy(nvhdcp->info.type, nvhdcp->name, sizeof(nvhdcp->info.type));
1206         nvhdcp->bus = bus;
1207         nvhdcp->info.addr = 0x74 >> 1;
1208         nvhdcp->info.platform_data = nvhdcp;
1209         nvhdcp->fail_count = 0;
1210
1211         adapter = i2c_get_adapter(bus);
1212         if (!adapter) {
1213                 nvhdcp_err("can't get adapter for bus %d\n", bus);
1214                 e = -EBUSY;
1215                 goto free_nvhdcp;
1216         }
1217
1218         nvhdcp->client = i2c_new_device(adapter, &nvhdcp->info);
1219         i2c_put_adapter(adapter);
1220
1221         if (!nvhdcp->client) {
1222                 nvhdcp_err("can't create new device\n");
1223                 e = -EBUSY;
1224                 goto free_nvhdcp;
1225         }
1226
1227         nvhdcp->state = STATE_UNAUTHENTICATED;
1228
1229         nvhdcp->downstream_wq = create_singlethread_workqueue(nvhdcp->name);
1230         INIT_DELAYED_WORK(&nvhdcp->work, nvhdcp_downstream_worker);
1231
1232         nvhdcp->miscdev.minor = MISC_DYNAMIC_MINOR;
1233         nvhdcp->miscdev.name = nvhdcp->name;
1234         nvhdcp->miscdev.fops = &nvhdcp_fops;
1235
1236         e = misc_register(&nvhdcp->miscdev);
1237         if (e)
1238                 goto free_workqueue;
1239
1240         nvhdcp_vdbg("%s(): created misc device %s\n", __func__, nvhdcp->name);
1241
1242         return nvhdcp;
1243 free_workqueue:
1244         destroy_workqueue(nvhdcp->downstream_wq);
1245         i2c_release_client(nvhdcp->client);
1246 free_nvhdcp:
1247         kfree(nvhdcp);
1248         nvhdcp_err("unable to create device.\n");
1249         return ERR_PTR(e);
1250 }
1251
1252 void tegra_nvhdcp_destroy(struct tegra_nvhdcp *nvhdcp)
1253 {
1254         misc_deregister(&nvhdcp->miscdev);
1255         tegra_nvhdcp_off(nvhdcp);
1256         destroy_workqueue(nvhdcp->downstream_wq);
1257         i2c_release_client(nvhdcp->client);
1258         kfree(nvhdcp);
1259 }