b509a6c784994ffef9485e1a48deac5e9de13a7f
[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         nvhdcp->state = STATE_UNAUTHENTICATED;
845
846         /* check plug state to terminate early in case flush_workqueue() */
847         if (!nvhdcp_is_plugged(nvhdcp)) {
848                 nvhdcp_err("worker started while unplugged!\n");
849                 goto lost_hdmi;
850         }
851         nvhdcp_vdbg("%s():hpd=%d\n", __func__, nvhdcp->plugged);
852
853         nvhdcp->a_ksv = 0;
854         nvhdcp->b_ksv = 0;
855         nvhdcp->a_n = 0;
856
857         e = get_bcaps(nvhdcp, &b_caps);
858         if (e) {
859                 nvhdcp_err("Bcaps read failure\n");
860                 goto failure;
861         }
862
863         nvhdcp_vdbg("read Bcaps = 0x%02x\n", b_caps);
864
865         nvhdcp_vdbg("kfuse loading ...\n");
866
867         /* repeater flag in Bskv must be configured before loading fuses */
868         set_bksv(hdmi, 0, (b_caps & BCAPS_REPEATER));
869
870         e = load_kfuse(hdmi);
871         if (e) {
872                 nvhdcp_err("kfuse could not be loaded\n");
873                 goto failure;
874         }
875
876         hdcp_ctrl_run(hdmi, 1);
877
878         nvhdcp_vdbg("wait AN_VALID ...\n");
879
880         /* wait for hardware to generate HDCP values */
881         e = wait_hdcp_ctrl(hdmi, AN_VALID | SROM_ERR, &res);
882         if (e) {
883                 nvhdcp_err("An key generation timeout\n");
884                 goto failure;
885         }
886         if (res & SROM_ERR) {
887                 nvhdcp_err("SROM error\n");
888                 goto failure;
889         }
890
891         msleep(25);
892
893         nvhdcp->a_ksv = get_aksv(hdmi);
894         nvhdcp->a_n = get_an(hdmi);
895         nvhdcp_vdbg("Aksv is 0x%016llx\n", nvhdcp->a_ksv);
896         nvhdcp_vdbg("An is 0x%016llx\n", nvhdcp->a_n);
897         if (verify_ksv(nvhdcp->a_ksv)) {
898                 nvhdcp_err("Aksv verify failure! (0x%016llx)\n", nvhdcp->a_ksv);
899                 goto disable;
900         }
901
902         /* write Ainfo to receiver - set 1.1 only if b_caps supports it */
903         e = nvhdcp_i2c_write8(nvhdcp, 0x15, b_caps & BCAPS_11);
904         if (e) {
905                 nvhdcp_err("Ainfo write failure\n");
906                 goto failure;
907         }
908
909         /* write An to receiver */
910         e = nvhdcp_i2c_write64(nvhdcp, 0x18, nvhdcp->a_n);
911         if (e) {
912                 nvhdcp_err("An write failure\n");
913                 goto failure;
914         }
915
916         nvhdcp_vdbg("wrote An = 0x%016llx\n", nvhdcp->a_n);
917
918         /* write Aksv to receiver - triggers auth sequence */
919         e = nvhdcp_i2c_write40(nvhdcp, 0x10, nvhdcp->a_ksv);
920         if (e) {
921                 nvhdcp_err("Aksv write failure\n");
922                 goto failure;
923         }
924
925         nvhdcp_vdbg("wrote Aksv = 0x%010llx\n", nvhdcp->a_ksv);
926
927         /* bail out if unplugged in the middle of negotiation */
928         if (!nvhdcp_is_plugged(nvhdcp))
929                 goto lost_hdmi;
930
931         /* get Bksv from receiver */
932         e = nvhdcp_i2c_read40(nvhdcp, 0x00, &nvhdcp->b_ksv);
933         if (e) {
934                 nvhdcp_err("Bksv read failure\n");
935                 goto failure;
936         }
937         nvhdcp_vdbg("Bksv is 0x%016llx\n", nvhdcp->b_ksv);
938         if (verify_ksv(nvhdcp->b_ksv)) {
939                 nvhdcp_err("Bksv verify failure!\n");
940                 goto failure;
941         }
942
943         nvhdcp_vdbg("read Bksv = 0x%010llx from device\n", nvhdcp->b_ksv);
944
945         set_bksv(hdmi, nvhdcp->b_ksv, (b_caps & BCAPS_REPEATER));
946
947         nvhdcp_vdbg("loaded Bksv into controller\n");
948
949         e = wait_hdcp_ctrl(hdmi, R0_VALID, NULL);
950         if (e) {
951                 nvhdcp_err("R0 read failure!\n");
952                 goto failure;
953         }
954
955         nvhdcp_vdbg("R0 valid\n");
956
957         msleep(100); /* can't read R0' within 100ms of writing Aksv */
958
959         nvhdcp_vdbg("verifying links ...\n");
960
961         e = verify_link(nvhdcp, false);
962         if (e) {
963                 nvhdcp_err("link verification failed err %d\n", e);
964                 goto failure;
965         }
966
967         /* if repeater then get repeater info */
968         if (b_caps & BCAPS_REPEATER) {
969                 e = get_repeater_info(nvhdcp);
970                 if (e) {
971                         nvhdcp_err("get repeater info failed\n");
972                         goto failure;
973                 }
974         }
975
976         tmp = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
977         tmp |= CRYPT_ENABLED;
978         if (b_caps & BCAPS_11) /* HDCP 1.1 ? */
979                 tmp |= ONEONE_ENABLED;
980         tegra_hdmi_writel(hdmi, tmp, HDMI_NV_PDISP_RG_HDCP_CTRL);
981
982         nvhdcp_vdbg("CRYPT enabled\n");
983
984         mutex_lock(&nvhdcp->state_lock);
985         nvhdcp->state = STATE_LINK_VERIFY;
986         mutex_unlock(&nvhdcp->state_lock);
987         nvhdcp_info("link verified!\n");
988
989         while (1) {
990                 if (!nvhdcp_is_plugged(nvhdcp))
991                         goto lost_hdmi;
992
993                 if (nvhdcp->state != STATE_LINK_VERIFY)
994                         goto failure;
995
996                 e = verify_link(nvhdcp, true);
997                 if (e) {
998                         nvhdcp_err("link verification failed err %d\n", e);
999                         goto failure;
1000                 }
1001                 mutex_unlock(&nvhdcp->lock);
1002                 wait_event_interruptible_timeout(wq_worker,
1003                         !nvhdcp_is_plugged(nvhdcp), msecs_to_jiffies(1500));
1004                 mutex_lock(&nvhdcp->lock);
1005
1006         }
1007
1008 failure:
1009         nvhdcp->fail_count++;
1010         if(nvhdcp->fail_count > 5) {
1011                 nvhdcp_err("nvhdcp failure - too many failures, giving up!\n");
1012         } else {
1013                 if (!nvhdcp_is_plugged(nvhdcp)) {
1014                         nvhdcp_err("nvhdcp failure\n");
1015                         goto lost_hdmi;
1016                 }
1017                 nvhdcp_err("nvhdcp failure - renegotiating in 1 second\n");
1018                 queue_delayed_work(nvhdcp->downstream_wq, &nvhdcp->work,
1019                                                 msecs_to_jiffies(1000));
1020         }
1021
1022 lost_hdmi:
1023         mutex_lock(&nvhdcp->state_lock);
1024         nvhdcp->state = STATE_UNAUTHENTICATED;
1025         mutex_unlock(&nvhdcp->state_lock);
1026         hdcp_ctrl_run(hdmi, 0);
1027
1028 err:
1029         mutex_unlock(&nvhdcp->lock);
1030         return;
1031 disable:
1032         mutex_lock(&nvhdcp->state_lock);
1033         nvhdcp->state = STATE_OFF;
1034         nvhdcp_set_plugged(nvhdcp, false);
1035         mutex_unlock(&nvhdcp->state_lock);
1036         mutex_unlock(&nvhdcp->lock);
1037         return;
1038 }
1039
1040 static int tegra_nvhdcp_on(struct tegra_nvhdcp *nvhdcp)
1041 {
1042         mutex_lock(&nvhdcp->state_lock);
1043         nvhdcp_set_plugged(nvhdcp, true);
1044         nvhdcp->state = STATE_UNAUTHENTICATED;
1045         mutex_unlock(&nvhdcp->state_lock);
1046         if (nvhdcp_is_plugged(nvhdcp)) {
1047                 nvhdcp->fail_count = 0;
1048                 queue_delayed_work(nvhdcp->downstream_wq, &nvhdcp->work,
1049                                                 msecs_to_jiffies(100));
1050         }
1051         return 0;
1052 }
1053
1054 static int tegra_nvhdcp_off(struct tegra_nvhdcp *nvhdcp)
1055 {
1056         mutex_lock(&nvhdcp->state_lock);
1057         nvhdcp->state = STATE_OFF;
1058         nvhdcp_set_plugged(nvhdcp, false);
1059         mutex_unlock(&nvhdcp->state_lock);
1060         wake_up_interruptible(&wq_worker);
1061         flush_workqueue(nvhdcp->downstream_wq);
1062         return 0;
1063 }
1064
1065 void tegra_nvhdcp_set_plug(struct tegra_nvhdcp *nvhdcp, bool hpd)
1066 {
1067         nvhdcp_debug("hdmi hotplug detected (hpd = %d)\n", hpd);
1068
1069         if (hpd) {
1070                 tegra_nvhdcp_on(nvhdcp);
1071         } else {
1072                 tegra_nvhdcp_off(nvhdcp);
1073         }
1074 }
1075
1076 int tegra_nvhdcp_set_policy(struct tegra_nvhdcp *nvhdcp, int pol)
1077 {
1078         if (pol == TEGRA_NVHDCP_POLICY_ALWAYS_ON) {
1079                 nvhdcp_info("using \"always on\" policy.\n");
1080                 if (atomic_xchg(&nvhdcp->policy, pol) != pol) {
1081                         /* policy changed, start working */
1082                         tegra_nvhdcp_on(nvhdcp);
1083                 }
1084         } else {
1085                 /* unsupported policy */
1086                 return -EINVAL;
1087         }
1088
1089         return 0;
1090 }
1091
1092 static int tegra_nvhdcp_renegotiate(struct tegra_nvhdcp *nvhdcp)
1093 {
1094         mutex_lock(&nvhdcp->state_lock);
1095         nvhdcp->state = STATE_RENEGOTIATE;
1096         mutex_unlock(&nvhdcp->state_lock);
1097         tegra_nvhdcp_on(nvhdcp);
1098         return 0;
1099 }
1100
1101 void tegra_nvhdcp_suspend(struct tegra_nvhdcp *nvhdcp)
1102 {
1103         if (!nvhdcp) return;
1104         tegra_nvhdcp_off(nvhdcp);
1105 }
1106
1107 void tegra_nvhdcp_resume(struct tegra_nvhdcp *nvhdcp)
1108 {
1109         if (!nvhdcp) return;
1110         tegra_nvhdcp_renegotiate(nvhdcp);
1111 }
1112
1113 static long nvhdcp_dev_ioctl(struct file *filp,
1114                 unsigned int cmd, unsigned long arg)
1115 {
1116         struct tegra_nvhdcp *nvhdcp = filp->private_data;
1117         struct tegra_nvhdcp_packet *pkt;
1118         int e = -ENOTTY;
1119
1120         switch (cmd) {
1121         case TEGRAIO_NVHDCP_ON:
1122                 return tegra_nvhdcp_on(nvhdcp);
1123
1124         case TEGRAIO_NVHDCP_OFF:
1125                 return tegra_nvhdcp_off(nvhdcp);
1126
1127         case TEGRAIO_NVHDCP_SET_POLICY:
1128                 return tegra_nvhdcp_set_policy(nvhdcp, arg);
1129
1130         case TEGRAIO_NVHDCP_READ_M:
1131                 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1132                 if (!pkt)
1133                         return -ENOMEM;
1134                 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1135                         e = -EFAULT;
1136                         goto kfree_pkt;
1137                 }
1138                 e = get_m_prime(nvhdcp, pkt);
1139                 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1140                         e = -EFAULT;
1141                         goto kfree_pkt;
1142                 }
1143                 kfree(pkt);
1144                 return e;
1145
1146         case TEGRAIO_NVHDCP_READ_S:
1147                 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1148                 if (!pkt)
1149                         return -ENOMEM;
1150                 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1151                         e = -EFAULT;
1152                         goto kfree_pkt;
1153                 }
1154                 e = get_s_prime(nvhdcp, pkt);
1155                 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1156                         e = -EFAULT;
1157                         goto kfree_pkt;
1158                 }
1159                 kfree(pkt);
1160                 return e;
1161
1162         case TEGRAIO_NVHDCP_RENEGOTIATE:
1163                 e = tegra_nvhdcp_renegotiate(nvhdcp);
1164                 break;
1165         }
1166
1167         return e;
1168 kfree_pkt:
1169         kfree(pkt);
1170         return e;
1171 }
1172
1173 static int nvhdcp_dev_open(struct inode *inode, struct file *filp)
1174 {
1175         struct miscdevice *miscdev = filp->private_data;
1176         struct tegra_nvhdcp *nvhdcp =
1177                 container_of(miscdev, struct tegra_nvhdcp, miscdev);
1178         filp->private_data = nvhdcp;
1179         return 0;
1180 }
1181
1182 static int nvhdcp_dev_release(struct inode *inode, struct file *filp)
1183 {
1184         filp->private_data = NULL;
1185         return 0;
1186 }
1187
1188 static const struct file_operations nvhdcp_fops = {
1189         .owner          = THIS_MODULE,
1190         .llseek         = no_llseek,
1191         .unlocked_ioctl = nvhdcp_dev_ioctl,
1192         .open           = nvhdcp_dev_open,
1193         .release        = nvhdcp_dev_release,
1194 };
1195
1196 /* we only support one AP right now, so should only call this once. */
1197 struct tegra_nvhdcp *tegra_nvhdcp_create(struct tegra_dc_hdmi_data *hdmi,
1198                         int id, int bus)
1199 {
1200         static struct tegra_nvhdcp *nvhdcp; /* prevent multiple calls */
1201         struct i2c_adapter *adapter;
1202         int e;
1203
1204         if (nvhdcp)
1205                 return ERR_PTR(-EMFILE);
1206
1207         nvhdcp = kzalloc(sizeof(*nvhdcp), GFP_KERNEL);
1208         if (!nvhdcp)
1209                 return ERR_PTR(-ENOMEM);
1210
1211         nvhdcp->id = id;
1212         snprintf(nvhdcp->name, sizeof(nvhdcp->name), "nvhdcp%u", id);
1213         nvhdcp->hdmi = hdmi;
1214         mutex_init(&nvhdcp->lock);
1215         mutex_init(&nvhdcp->state_lock);
1216
1217         strlcpy(nvhdcp->info.type, nvhdcp->name, sizeof(nvhdcp->info.type));
1218         nvhdcp->bus = bus;
1219         nvhdcp->info.addr = 0x74 >> 1;
1220         nvhdcp->info.platform_data = nvhdcp;
1221         nvhdcp->fail_count = 0;
1222
1223         adapter = i2c_get_adapter(bus);
1224         if (!adapter) {
1225                 nvhdcp_err("can't get adapter for bus %d\n", bus);
1226                 e = -EBUSY;
1227                 goto free_nvhdcp;
1228         }
1229
1230         nvhdcp->client = i2c_new_device(adapter, &nvhdcp->info);
1231         i2c_put_adapter(adapter);
1232
1233         if (!nvhdcp->client) {
1234                 nvhdcp_err("can't create new device\n");
1235                 e = -EBUSY;
1236                 goto free_nvhdcp;
1237         }
1238
1239         mutex_lock(&nvhdcp->state_lock);
1240         nvhdcp->state = STATE_UNAUTHENTICATED;
1241         mutex_unlock(&nvhdcp->state_lock);
1242
1243         nvhdcp->downstream_wq = create_singlethread_workqueue(nvhdcp->name);
1244         INIT_DELAYED_WORK(&nvhdcp->work, nvhdcp_downstream_worker);
1245
1246         nvhdcp->miscdev.minor = MISC_DYNAMIC_MINOR;
1247         nvhdcp->miscdev.name = nvhdcp->name;
1248         nvhdcp->miscdev.fops = &nvhdcp_fops;
1249
1250         e = misc_register(&nvhdcp->miscdev);
1251         if (e)
1252                 goto free_workqueue;
1253
1254         nvhdcp_vdbg("%s(): created misc device %s\n", __func__, nvhdcp->name);
1255
1256         return nvhdcp;
1257 free_workqueue:
1258         destroy_workqueue(nvhdcp->downstream_wq);
1259         i2c_release_client(nvhdcp->client);
1260 free_nvhdcp:
1261         kfree(nvhdcp);
1262         nvhdcp_err("unable to create device.\n");
1263         return ERR_PTR(e);
1264 }
1265
1266 void tegra_nvhdcp_destroy(struct tegra_nvhdcp *nvhdcp)
1267 {
1268         misc_deregister(&nvhdcp->miscdev);
1269         tegra_nvhdcp_off(nvhdcp);
1270         destroy_workqueue(nvhdcp->downstream_wq);
1271         i2c_release_client(nvhdcp->client);
1272         kfree(nvhdcp);
1273 }