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