2 * drivers/video/tegra/dc/nvhdcp.c
4 * Copyright (c) 2010-2011, NVIDIA Corporation.
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.
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.
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>
29 #include <mach/nvhost.h>
30 #include <mach/kfuse.h>
32 #include <video/nvhdcp.h>
40 #define BCAPS_REPEATER (1 << 6)
41 #define BCAPS_READY (1 << 5)
42 #define BCAPS_11 (1 << 1) /* used for both Bcaps and Ainfo */
44 /* for 0x41 Bstatus */
45 #define BSTATUS_MAX_DEVS_EXCEEDED (1 << 7)
46 #define BSTATUS_MAX_CASCADE_EXCEEDED (1 << 11)
49 #define nvhdcp_vdbg(...) \
50 printk("nvhdcp: " __VA_ARGS__)
52 #define nvhdcp_vdbg(...) \
55 printk("nvhdcp: " __VA_ARGS__); \
59 #define nvhdcp_debug(...) \
60 pr_debug("nvhdcp: " __VA_ARGS__)
61 #define nvhdcp_err(...) \
62 pr_err("nvhdcp: Error: " __VA_ARGS__)
63 #define nvhdcp_info(...) \
64 pr_info("nvhdcp: " __VA_ARGS__)
67 /* for nvhdcp.state */
68 enum tegra_nvhdcp_state {
70 STATE_UNAUTHENTICATED,
76 struct work_struct work;
77 struct tegra_dc_hdmi_data *hdmi;
78 struct workqueue_struct *downstream_wq;
80 struct miscdevice miscdev;
83 bool plugged; /* true if hotplug detected */
84 atomic_t policy; /* set policy */
85 enum tegra_nvhdcp_state state; /* STATE_xxx */
86 struct i2c_client *client;
87 struct i2c_board_info info;
99 u64 bksv_list[TEGRA_NVHDCP_MAX_DEVS];
103 static inline bool nvhdcp_is_plugged(struct tegra_nvhdcp *nvhdcp)
106 return nvhdcp->plugged;
109 static inline bool nvhdcp_set_plugged(struct tegra_nvhdcp *nvhdcp, bool plugged)
111 nvhdcp->plugged = plugged;
116 static int nvhdcp_i2c_read(struct tegra_nvhdcp *nvhdcp, u8 reg,
117 size_t len, void *data)
121 struct i2c_msg msg[] = {
123 .addr = 0x74 >> 1, /* primary link */
129 .addr = 0x74 >> 1, /* primary link */
137 if (!nvhdcp_is_plugged(nvhdcp)) {
138 nvhdcp_err("disconnect during i2c xfer\n");
141 status = i2c_transfer(nvhdcp->client->adapter,
142 msg, ARRAY_SIZE(msg));
145 } while ((status < 0) && retries--);
148 nvhdcp_err("i2c xfer error %d\n", status);
155 static int nvhdcp_i2c_write(struct tegra_nvhdcp *nvhdcp, u8 reg,
156 size_t len, const void *data)
160 struct i2c_msg msg[] = {
162 .addr = 0x74 >> 1, /* primary link */
171 memcpy(buf + 1, data, len);
174 if (!nvhdcp_is_plugged(nvhdcp)) {
175 nvhdcp_err("disconnect during i2c xfer\n");
178 status = i2c_transfer(nvhdcp->client->adapter,
179 msg, ARRAY_SIZE(msg));
182 } while ((status < 0) && retries--);
185 nvhdcp_err("i2c xfer error %d\n", status);
192 static inline int nvhdcp_i2c_read8(struct tegra_nvhdcp *nvhdcp, u8 reg, u8 *val)
194 return nvhdcp_i2c_read(nvhdcp, reg, 1, val);
197 static inline int nvhdcp_i2c_write8(struct tegra_nvhdcp *nvhdcp, u8 reg, u8 val)
199 return nvhdcp_i2c_write(nvhdcp, reg, 1, &val);
202 static inline int nvhdcp_i2c_read16(struct tegra_nvhdcp *nvhdcp,
208 e = nvhdcp_i2c_read(nvhdcp, reg, sizeof buf, buf);
213 *val = buf[0] | (u16)buf[1] << 8;
218 static int nvhdcp_i2c_read40(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 *val)
224 e = nvhdcp_i2c_read(nvhdcp, reg, sizeof buf, buf);
228 for(i = 0, n = 0; i < 5; i++ ) {
239 static int nvhdcp_i2c_write40(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 val)
243 for(i = 0; i < 5; i++ ) {
247 return nvhdcp_i2c_write(nvhdcp, reg, sizeof buf, buf);
250 static int nvhdcp_i2c_write64(struct tegra_nvhdcp *nvhdcp, u8 reg, u64 val)
254 for(i = 0; i < 8; i++ ) {
258 return nvhdcp_i2c_write(nvhdcp, reg, sizeof buf, buf);
262 /* 64-bit link encryption session random number */
263 static inline u64 get_an(struct tegra_dc_hdmi_data *hdmi)
266 r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AN_MSB) << 32;
267 r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AN_LSB);
271 /* 64-bit upstream exchange random number */
272 static inline void set_cn(struct tegra_dc_hdmi_data *hdmi, u64 c_n)
274 tegra_hdmi_writel(hdmi, (u32)c_n, HDMI_NV_PDISP_RG_HDCP_CN_LSB);
275 tegra_hdmi_writel(hdmi, c_n >> 32, HDMI_NV_PDISP_RG_HDCP_CN_MSB);
279 /* 40-bit transmitter's key selection vector */
280 static inline u64 get_aksv(struct tegra_dc_hdmi_data *hdmi)
283 r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AKSV_MSB) << 32;
284 r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_AKSV_LSB);
288 /* 40-bit receiver's key selection vector */
289 static inline void set_bksv(struct tegra_dc_hdmi_data *hdmi, u64 b_ksv, bool repeater)
292 b_ksv |= (u64)REPEATER << 32;
293 tegra_hdmi_writel(hdmi, (u32)b_ksv, HDMI_NV_PDISP_RG_HDCP_BKSV_LSB);
294 tegra_hdmi_writel(hdmi, b_ksv >> 32, HDMI_NV_PDISP_RG_HDCP_BKSV_MSB);
298 /* 40-bit software's key selection vector */
299 static inline void set_cksv(struct tegra_dc_hdmi_data *hdmi, u64 c_ksv)
301 tegra_hdmi_writel(hdmi, (u32)c_ksv, HDMI_NV_PDISP_RG_HDCP_CKSV_LSB);
302 tegra_hdmi_writel(hdmi, c_ksv >> 32, HDMI_NV_PDISP_RG_HDCP_CKSV_MSB);
305 /* 40-bit connection state */
306 static inline u64 get_cs(struct tegra_dc_hdmi_data *hdmi)
309 r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CS_MSB) << 32;
310 r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CS_LSB);
314 /* 40-bit upstream key selection vector */
315 static inline u64 get_dksv(struct tegra_dc_hdmi_data *hdmi)
318 r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_DKSV_MSB) << 32;
319 r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_DKSV_LSB);
323 /* 64-bit encrypted M0 value */
324 static inline u64 get_mprime(struct tegra_dc_hdmi_data *hdmi)
327 r = (u64)tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_MPRIME_MSB) << 32;
328 r |= tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_MPRIME_LSB);
332 static inline u16 get_transmitter_ri(struct tegra_dc_hdmi_data *hdmi)
334 return tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_RI);
337 static inline int get_receiver_ri(struct tegra_nvhdcp *nvhdcp, u16 *r)
339 return nvhdcp_i2c_read16(nvhdcp, 0x8, r); /* long read */
342 static int get_bcaps(struct tegra_nvhdcp *nvhdcp, u8 *b_caps)
344 return nvhdcp_i2c_read8(nvhdcp, 0x40, b_caps);
347 static int get_ksvfifo(struct tegra_nvhdcp *nvhdcp,
348 unsigned num_bksv_list, u64 *ksv_list)
353 size_t buf_len = num_bksv_list * 5;
355 if (!ksv_list || num_bksv_list > TEGRA_NVHDCP_MAX_DEVS)
358 if (num_bksv_list == 0)
361 buf = kmalloc(buf_len, GFP_KERNEL);
362 if (IS_ERR_OR_NULL(buf))
365 e = nvhdcp_i2c_read(nvhdcp, 0x43, buf_len, buf);
371 /* load 40-bit keys from repeater into array of u64 */
373 for (i = 0; i < num_bksv_list; i++) {
374 ksv_list[i] = p[0] | ((u64)p[1] << 8) | ((u64)p[2] << 16)
375 | ((u64)p[3] << 24) | ((u64)p[4] << 32);
383 /* get V' 160-bit SHA-1 hash from repeater */
384 static int get_vprime(struct tegra_nvhdcp *nvhdcp, u8 *v_prime)
388 for (i = 0; i < 20; i += 4) {
389 e = nvhdcp_i2c_read(nvhdcp, 0x20 + i, 4, v_prime + i);
397 /* set or clear RUN_YES */
398 static void hdcp_ctrl_run(struct tegra_dc_hdmi_data *hdmi, bool v)
403 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
404 ctrl |= HDCP_RUN_YES;
409 tegra_hdmi_writel(hdmi, ctrl, HDMI_NV_PDISP_RG_HDCP_CTRL);
412 /* wait for any bits in mask to be set in HDMI_NV_PDISP_RG_HDCP_CTRL
413 * sleeps up to 120mS */
414 static int wait_hdcp_ctrl(struct tegra_dc_hdmi_data *hdmi, u32 mask, u32 *v)
420 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
421 if ((ctrl | (mask))) {
430 nvhdcp_err("ctrl read timeout (mask=0x%x)\n", mask);
436 /* wait for any bits in mask to be set in HDMI_NV_PDISP_KEY_CTRL
437 * waits up to 100mS */
438 static int wait_key_ctrl(struct tegra_dc_hdmi_data *hdmi, u32 mask)
444 ctrl = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_KEY_CTRL);
451 nvhdcp_err("key ctrl read timeout (mask=0x%x)\n", mask);
457 /* check that key selection vector is well formed.
458 * NOTE: this function assumes KSV has already been checked against
461 static int verify_ksv(u64 k)
465 /* count set bits, must be exactly 20 set to be valid */
469 return (i != 20) ? -EINVAL : 0;
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)
475 struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
476 u32 sp_msb, sp_lsb1, sp_lsb2;
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;
488 pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
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);
498 pkt->value_flags = 0;
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;
504 nvhdcp_vdbg("%s():cn %llx cksv %llx\n", __func__, pkt->c_n, pkt->c_ksv);
506 set_cn(hdmi, pkt->c_n);
508 tegra_hdmi_writel(hdmi, TMDS0_LINK0 | READ_S,
509 HDMI_NV_PDISP_RG_HDCP_CMODE);
511 set_cksv(hdmi, pkt->c_ksv);
513 e = wait_hdcp_ctrl(hdmi, SPRIME_VALID, NULL);
515 nvhdcp_err("Sprime read timeout\n");
516 pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
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);
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;
533 pkt->k_prime = ((u64)(sp_lsb2 & 0xffffff) << 32) | sp_lsb1;
534 pkt->value_flags |= TEGRA_NVHDCP_FLAG_KP;
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;
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 */
549 pkt->value_flags |= TEGRA_NVHDCP_FLAG_DKSV;
551 /* copy current Bksv */
552 pkt->b_ksv = nvhdcp->b_ksv;
553 pkt->value_flags |= TEGRA_NVHDCP_FLAG_BKSV;
555 pkt->packet_results = TEGRA_NVHDCP_RESULT_SUCCESS;
556 mutex_unlock(&nvhdcp->lock);
560 mutex_unlock(&nvhdcp->lock);
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)
567 struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
570 pkt->packet_results = TEGRA_NVHDCP_RESULT_UNSUCCESSFUL;
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;
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;
585 set_cn(hdmi, pkt->c_n);
587 tegra_hdmi_writel(hdmi, TMDS0_LINK0 | READ_M,
588 HDMI_NV_PDISP_RG_HDCP_CMODE);
590 /* Cksv write triggers Mprime update */
591 set_cksv(hdmi, pkt->c_ksv);
593 e = wait_hdcp_ctrl(hdmi, MPRIME_VALID, NULL);
595 nvhdcp_err("Mprime read timeout\n");
602 pkt->m_prime = get_mprime(hdmi);
603 pkt->value_flags |= TEGRA_NVHDCP_FLAG_MP;
605 pkt->b_status = nvhdcp->b_status;
606 pkt->value_flags |= TEGRA_NVHDCP_FLAG_BSTATUS;
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;
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;
623 pkt->d_ksv = get_dksv(hdmi);
624 if (verify_ksv(pkt->d_ksv)) {
625 nvhdcp_err("Dksv invalid!\n");
629 pkt->value_flags |= TEGRA_NVHDCP_FLAG_DKSV;
631 /* copy current Bksv */
632 pkt->b_ksv = nvhdcp->b_ksv;
633 pkt->value_flags |= TEGRA_NVHDCP_FLAG_BKSV;
635 pkt->packet_results = TEGRA_NVHDCP_RESULT_SUCCESS;
636 mutex_unlock(&nvhdcp->lock);
640 mutex_unlock(&nvhdcp->lock);
644 static int load_kfuse(struct tegra_dc_hdmi_data *hdmi)
646 unsigned buf[KFUSE_DATA_SZ / 4];
652 /* copy load kfuse into buffer - only needed for early Tegra parts */
653 e = tegra_kfuse_read(buf, sizeof buf);
655 nvhdcp_err("Kfuse read failure\n");
659 /* write the kfuse to HDMI SRAM */
661 tegra_hdmi_writel(hdmi, 1, HDMI_NV_PDISP_KEY_CTRL); /* LOAD_KEYS */
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);
668 e = wait_key_ctrl(hdmi, PKEY_LOADED);
670 nvhdcp_err("key reload timeout\n");
674 tegra_hdmi_writel(hdmi, 0, HDMI_NV_PDISP_KEY_SKEY_INDEX);
676 /* wait for SRAM to be cleared */
679 tmp = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_KEY_DEBUG0);
680 if ((tmp & 1) == 0) break;
685 nvhdcp_err("key SRAM clear timeout\n");
689 for (i = 0; i < KFUSE_DATA_SZ / 4; i += 4) {
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);
697 /* trigger LOAD_HDCP_KEY */
698 tegra_hdmi_writel(hdmi, 0x100, HDMI_NV_PDISP_KEY_HDCP_KEY_TRIG);
700 tmp = LOCAL_KEYS | WRITE16;
703 tegra_hdmi_writel(hdmi, tmp, HDMI_NV_PDISP_KEY_CTRL);
705 /* wait for WRITE16 to complete */
706 e = wait_key_ctrl(hdmi, 0x10); /* WRITE16 */
708 nvhdcp_err("key write timeout\n");
716 static int verify_link(struct tegra_nvhdcp *nvhdcp, bool wait_ri)
718 struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
726 /* retry 3 times to deal with I2C link issues */
729 old = get_transmitter_ri(hdmi);
731 e = get_receiver_ri(nvhdcp, &rx);
734 nvhdcp_err("Ri is 0!\n");
738 tx = get_transmitter_ri(hdmi);
744 } while (wait_ri && --retries && old != tx);
746 nvhdcp_debug("R0 Ri poll:rx=0x%04x tx=0x%04x\n", rx, tx);
748 if (!nvhdcp_is_plugged(nvhdcp)) {
749 nvhdcp_err("aborting verify links - lost hdmi connection\n");
759 static int get_repeater_info(struct tegra_nvhdcp *nvhdcp)
765 nvhdcp_vdbg("repeater found:fetching repeater info\n");
767 /* wait up to 5 seconds for READY on repeater */
770 if (!nvhdcp_is_plugged(nvhdcp)) {
771 nvhdcp_err("disconnect while waiting for repeater\n");
775 e = get_bcaps(nvhdcp, &b_caps);
776 if (!e && (b_caps & BCAPS_READY)) {
777 nvhdcp_debug("Bcaps READY from repeater\n");
784 nvhdcp_err("repeater Bcaps read timeout\n");
788 memset(nvhdcp->v_prime, 0, sizeof nvhdcp->v_prime);
789 e = get_vprime(nvhdcp, nvhdcp->v_prime);
791 nvhdcp_err("repeater Vprime read failure!\n");
795 e = nvhdcp_i2c_read16(nvhdcp, 0x41, &b_status);
797 nvhdcp_err("Bstatus read failure!\n");
801 if (b_status & BSTATUS_MAX_DEVS_EXCEEDED) {
802 nvhdcp_err("repeater:max devices (0x%04x)\n", b_status);
806 if (b_status & BSTATUS_MAX_CASCADE_EXCEEDED) {
807 nvhdcp_err("repeater:max cascade (0x%04x)\n", b_status);
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);
816 memset(nvhdcp->bksv_list, 0, sizeof nvhdcp->bksv_list);
817 e = get_ksvfifo(nvhdcp, nvhdcp->num_bksv_list, nvhdcp->bksv_list);
819 nvhdcp_err("repeater:could not read KSVFIFO (err %d)\n", e);
826 static void nvhdcp_downstream_worker(struct work_struct *work)
828 struct tegra_nvhdcp *nvhdcp =
829 container_of(work, struct tegra_nvhdcp, work);
830 struct tegra_dc_hdmi_data *hdmi = nvhdcp->hdmi;
836 nvhdcp_vdbg("%s():started thread %s\n", __func__, nvhdcp->name);
838 mutex_lock(&nvhdcp->lock);
839 if (nvhdcp->state == STATE_OFF) {
840 nvhdcp_err("nvhdcp failure - giving up\n");
843 nvhdcp->state = STATE_UNAUTHENTICATED;
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");
850 nvhdcp_vdbg("%s():hpd=%d\n", __func__, nvhdcp->plugged);
856 e = get_bcaps(nvhdcp, &b_caps);
858 nvhdcp_err("Bcaps read failure\n");
862 nvhdcp_vdbg("read Bcaps = 0x%02x\n", b_caps);
864 nvhdcp_vdbg("kfuse loading ...\n");
866 /* repeater flag in Bskv must be configured before loading fuses */
867 set_bksv(hdmi, 0, (b_caps & BCAPS_REPEATER));
869 e = load_kfuse(hdmi);
871 nvhdcp_err("kfuse could not be loaded\n");
875 hdcp_ctrl_run(hdmi, 1);
877 nvhdcp_vdbg("wait AN_VALID ...\n");
879 /* wait for hardware to generate HDCP values */
880 e = wait_hdcp_ctrl(hdmi, AN_VALID | SROM_ERR, &res);
882 nvhdcp_err("An key generation timeout\n");
885 if (res & SROM_ERR) {
886 nvhdcp_err("SROM error\n");
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);
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);
904 nvhdcp_err("Ainfo write failure\n");
908 /* write An to receiver */
909 e = nvhdcp_i2c_write64(nvhdcp, 0x18, nvhdcp->a_n);
911 nvhdcp_err("An write failure\n");
915 nvhdcp_vdbg("wrote An = 0x%016llx\n", nvhdcp->a_n);
917 /* write Aksv to receiver - triggers auth sequence */
918 e = nvhdcp_i2c_write40(nvhdcp, 0x10, nvhdcp->a_ksv);
920 nvhdcp_err("Aksv write failure\n");
924 nvhdcp_vdbg("wrote Aksv = 0x%010llx\n", nvhdcp->a_ksv);
926 /* bail out if unplugged in the middle of negotiation */
927 if (!nvhdcp_is_plugged(nvhdcp))
930 /* get Bksv from receiver */
931 e = nvhdcp_i2c_read40(nvhdcp, 0x00, &nvhdcp->b_ksv);
933 nvhdcp_err("Bksv read failure\n");
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");
942 nvhdcp_vdbg("read Bksv = 0x%010llx from device\n", nvhdcp->b_ksv);
944 set_bksv(hdmi, nvhdcp->b_ksv, (b_caps & BCAPS_REPEATER));
946 nvhdcp_vdbg("loaded Bksv into controller\n");
948 e = wait_hdcp_ctrl(hdmi, R0_VALID, NULL);
950 nvhdcp_err("R0 read failure!\n");
954 nvhdcp_vdbg("R0 valid\n");
956 msleep(100); /* can't read R0' within 100ms of writing Aksv */
958 nvhdcp_vdbg("verifying links ...\n");
960 e = verify_link(nvhdcp, false);
962 nvhdcp_err("link verification failed err %d\n", e);
966 tmp = tegra_hdmi_readl(hdmi, HDMI_NV_PDISP_RG_HDCP_CTRL);
967 tmp |= CRYPT_ENABLED;
968 if (b_caps & BCAPS_11) /* HDCP 1.1 ? */
969 tmp |= ONEONE_ENABLED;
970 tegra_hdmi_writel(hdmi, tmp, HDMI_NV_PDISP_RG_HDCP_CTRL);
972 nvhdcp_vdbg("CRYPT enabled\n");
974 /* if repeater then get repeater info */
975 if (b_caps & BCAPS_REPEATER) {
976 e = get_repeater_info(nvhdcp);
978 nvhdcp_err("get repeater info failed\n");
983 nvhdcp->state = STATE_LINK_VERIFY;
984 nvhdcp_info("link verified!\n");
987 if (!nvhdcp_is_plugged(nvhdcp))
990 if (nvhdcp->state != STATE_LINK_VERIFY)
993 e = verify_link(nvhdcp, true);
995 nvhdcp_err("link verification failed err %d\n", e);
998 mutex_unlock(&nvhdcp->lock);
1000 mutex_lock(&nvhdcp->lock);
1005 nvhdcp->fail_count++;
1006 if(nvhdcp->fail_count > 5) {
1007 nvhdcp_err("nvhdcp failure - too many failures, giving up!\n");
1009 nvhdcp_err("nvhdcp failure - renegotiating in 1.75 seconds\n");
1010 mutex_unlock(&nvhdcp->lock);
1012 mutex_lock(&nvhdcp->lock);
1013 queue_work(nvhdcp->downstream_wq, &nvhdcp->work);
1017 nvhdcp->state = STATE_UNAUTHENTICATED;
1018 hdcp_ctrl_run(hdmi, 0);
1021 mutex_unlock(&nvhdcp->lock);
1025 static int tegra_nvhdcp_on(struct tegra_nvhdcp *nvhdcp)
1027 nvhdcp->state = STATE_UNAUTHENTICATED;
1028 if (nvhdcp_is_plugged(nvhdcp)) {
1029 nvhdcp->fail_count = 0;
1030 queue_work(nvhdcp->downstream_wq, &nvhdcp->work);
1035 static int tegra_nvhdcp_off(struct tegra_nvhdcp *nvhdcp)
1037 mutex_lock(&nvhdcp->lock);
1038 nvhdcp->state = STATE_OFF;
1039 nvhdcp_set_plugged(nvhdcp, false);
1040 mutex_unlock(&nvhdcp->lock);
1041 flush_workqueue(nvhdcp->downstream_wq);
1045 void tegra_nvhdcp_set_plug(struct tegra_nvhdcp *nvhdcp, bool hpd)
1047 nvhdcp_debug("hdmi hotplug detected (hpd = %d)\n", hpd);
1050 nvhdcp_set_plugged(nvhdcp, true);
1051 tegra_nvhdcp_on(nvhdcp);
1053 tegra_nvhdcp_off(nvhdcp);
1057 int tegra_nvhdcp_set_policy(struct tegra_nvhdcp *nvhdcp, int pol)
1059 if (pol == TEGRA_NVHDCP_POLICY_ALWAYS_ON) {
1060 nvhdcp_info("using \"always on\" policy.\n");
1061 if (atomic_xchg(&nvhdcp->policy, pol) != pol) {
1062 /* policy changed, start working */
1063 tegra_nvhdcp_on(nvhdcp);
1066 /* unsupported policy */
1073 static int tegra_nvhdcp_renegotiate(struct tegra_nvhdcp *nvhdcp)
1075 mutex_lock(&nvhdcp->lock);
1076 nvhdcp->state = STATE_RENEGOTIATE;
1077 mutex_unlock(&nvhdcp->lock);
1078 tegra_nvhdcp_on(nvhdcp);
1082 void tegra_nvhdcp_suspend(struct tegra_nvhdcp *nvhdcp)
1084 if (!nvhdcp) return;
1085 tegra_nvhdcp_off(nvhdcp);
1089 static long nvhdcp_dev_ioctl(struct file *filp,
1090 unsigned int cmd, unsigned long arg)
1092 struct tegra_nvhdcp *nvhdcp = filp->private_data;
1093 struct tegra_nvhdcp_packet *pkt;
1097 case TEGRAIO_NVHDCP_ON:
1098 return tegra_nvhdcp_on(nvhdcp);
1100 case TEGRAIO_NVHDCP_OFF:
1101 return tegra_nvhdcp_off(nvhdcp);
1103 case TEGRAIO_NVHDCP_SET_POLICY:
1104 return tegra_nvhdcp_set_policy(nvhdcp, arg);
1106 case TEGRAIO_NVHDCP_READ_M:
1107 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1110 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1114 e = get_m_prime(nvhdcp, pkt);
1115 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1122 case TEGRAIO_NVHDCP_READ_S:
1123 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1126 if (copy_from_user(pkt, (void __user *)arg, sizeof(*pkt))) {
1130 e = get_s_prime(nvhdcp, pkt);
1131 if (copy_to_user((void __user *)arg, pkt, sizeof(*pkt))) {
1138 case TEGRAIO_NVHDCP_RENEGOTIATE:
1139 e = tegra_nvhdcp_renegotiate(nvhdcp);
1149 static int nvhdcp_dev_open(struct inode *inode, struct file *filp)
1151 struct miscdevice *miscdev = filp->private_data;
1152 struct tegra_nvhdcp *nvhdcp =
1153 container_of(miscdev, struct tegra_nvhdcp, miscdev);
1154 filp->private_data = nvhdcp;
1158 static int nvhdcp_dev_release(struct inode *inode, struct file *filp)
1160 filp->private_data = NULL;
1164 static const struct file_operations nvhdcp_fops = {
1165 .owner = THIS_MODULE,
1166 .llseek = no_llseek,
1167 .unlocked_ioctl = nvhdcp_dev_ioctl,
1168 .open = nvhdcp_dev_open,
1169 .release = nvhdcp_dev_release,
1172 /* we only support one AP right now, so should only call this once. */
1173 struct tegra_nvhdcp *tegra_nvhdcp_create(struct tegra_dc_hdmi_data *hdmi,
1176 static struct tegra_nvhdcp *nvhdcp; /* prevent multiple calls */
1177 struct i2c_adapter *adapter;
1181 return ERR_PTR(-EMFILE);
1183 nvhdcp = kzalloc(sizeof(*nvhdcp), GFP_KERNEL);
1185 return ERR_PTR(-ENOMEM);
1188 snprintf(nvhdcp->name, sizeof(nvhdcp->name), "nvhdcp%u", id);
1189 nvhdcp->hdmi = hdmi;
1190 mutex_init(&nvhdcp->lock);
1192 strlcpy(nvhdcp->info.type, nvhdcp->name, sizeof(nvhdcp->info.type));
1194 nvhdcp->info.addr = 0x74 >> 1;
1195 nvhdcp->info.platform_data = nvhdcp;
1196 nvhdcp->fail_count = 0;
1198 adapter = i2c_get_adapter(bus);
1200 nvhdcp_err("can't get adapter for bus %d\n", bus);
1205 nvhdcp->client = i2c_new_device(adapter, &nvhdcp->info);
1206 i2c_put_adapter(adapter);
1208 if (!nvhdcp->client) {
1209 nvhdcp_err("can't create new device\n");
1214 nvhdcp->state = STATE_UNAUTHENTICATED;
1216 nvhdcp->downstream_wq = create_singlethread_workqueue(nvhdcp->name);
1217 INIT_WORK(&nvhdcp->work, nvhdcp_downstream_worker);
1219 nvhdcp->miscdev.minor = MISC_DYNAMIC_MINOR;
1220 nvhdcp->miscdev.name = nvhdcp->name;
1221 nvhdcp->miscdev.fops = &nvhdcp_fops;
1223 e = misc_register(&nvhdcp->miscdev);
1225 goto free_workqueue;
1227 nvhdcp_vdbg("%s(): created misc device %s\n", __func__, nvhdcp->name);
1231 destroy_workqueue(nvhdcp->downstream_wq);
1232 i2c_release_client(nvhdcp->client);
1235 nvhdcp_err("unable to create device.\n");
1239 void tegra_nvhdcp_destroy(struct tegra_nvhdcp *nvhdcp)
1241 misc_deregister(&nvhdcp->miscdev);
1242 tegra_nvhdcp_off(nvhdcp);
1243 destroy_workqueue(nvhdcp->downstream_wq);
1244 i2c_release_client(nvhdcp->client);