// File: /workspace/github_cpp_makefile_star2_O1/chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_prepare_data(WdtDevice*, BoardInfo*)
int wh_i2c_prepare_data(WDT_DEV *pdev, BOARD_INFO* pboard_info)
{
	BYTE				buf[80] ={0};
	BOARD_INFO			board_info;	
	int	ret_size;
	int 	ret = 0;
	int 	fw_id = 0;
	int     retryF2 = 3;

	if (!pdev || !pdev->dev_handle || !pboard_info) {
		wh_printf("device ptr is null !\n");
		return 0;
	}
	wh_printf("prepare_i2c_addr %x\n", pdev->board_info.i2c_address);

	memset(&board_info, 0, sizeof(BOARD_INFO));
	board_info.i2c_address = pdev->board_info.i2c_address;

	pdev->dev_state = DS_GET_INFO;
	int get_param_hid_ret = wh_i2c_get_param_hid(pdev, &board_info);
	if(get_param_hid_ret == 0) {
		wh_printf("i2c_get_param_hid fail ! \n");
	}
		
	

	buf[0] = VND_REQ_DEV_INFO;
	while ((retryF2 && ret <= 0) || fw_id == 0)
	{
		ret = wh_i2c_get_feature(pdev, buf, 64);	
		fw_id = get_unaligned_le32(buf + 1);
		retryF2 --;

		if (ret <= 0 || fw_id == 0) {
			pboard_info->dev_type = FW_MAYBE_ISP;	
			wh_printf("Can't get fw id, should be in ISP mode !\n");

			if ((buf[0x26] == 0x49 && buf[0x27] == 0x53 && buf[0x28] == 0x50)) {
				BYTE romSig[8];
				memset(romSig, 0, sizeof(romSig));
				wh_w8760_get_rom_signature(2, romSig);
				if(memcmp(&buf[0 + 0x18], romSig, sizeof(romSig)) == 0)
				{	
					board_info.dev_type = FW_WDT8760_2_ISP;
					wh_w8762_isp_rerun_recovery(pdev);		
				}
				memset(romSig, 0, sizeof(romSig));
				wh_w8760_get_rom_signature(1, romSig);
				if(memcmp(&buf[0 + 0x18], romSig, sizeof(romSig)) == 0)
				{
					board_info.dev_type = FW_WDT8760_2_ISP;
					wh_w8762_isp_rerun_recovery(pdev);			
				}
				memset(romSig, 0, sizeof(romSig));
				wh_w8760_get_rom_signature(0, romSig);
				if(memcmp(&buf[0 + 0x18], romSig, sizeof(romSig)) == 0)
				{
					board_info.dev_type = FW_WDT8760_2_ISP;
					wh_w8760_isp_rerun_recovery(pdev);
				}	
			
			} 
			if(retryF2 == 0){
				printf("Can't get fw id, should be in ISP mode !\n");
				return 0;
			}
		}
	}
	


	if (buf[0] != VND_REQ_DEV_INFO) {
		pboard_info->dev_type = FW_MAYBE_ISP;	
		printf("Firmware id packet error !\n");
		return 0;
	}

	board_info.firmware_id = fw_id;
	board_info.hardware_id = get_unaligned_le32(buf + 5);
	board_info.serial_no = get_unaligned_le32(buf + 9);

	board_info.dev_type = check_firmware_id(pdev, board_info.firmware_id);

	if (board_info.dev_type & FW_WDT8790) {
		wh_w8790_parse_device_info(&board_info.dev_info.w8790_feature_devinfo, buf);

		memcpy(&pdev->board_info.dev_info.w8790_feature_devinfo, &board_info.dev_info.w8790_feature_devinfo,
			sizeof(W8790_DEV_INFO));
		if (wh_w8790_prepare_data(pdev, &board_info)){
			memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
                        return 1;

		}
	}



	if (board_info.dev_type & (FW_WDT8760_2 | FW_WDT8760_2_ISP)) {
		wh_w8760_get_feature_devinfo(&board_info.dev_info.w8760_feature_devinfo, buf);	
		
		memcpy(&pdev->board_info.dev_info.w8760_feature_devinfo, &board_info.dev_info.w8760_feature_devinfo, 
			sizeof(W8760_REPORT_FEATURE_DEVINFO));

		if (wh_w8760_prepare_data(pdev, &board_info)) {			
			memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
			return 1;
		}
	}

	buf[0] = 0xf4;
	if (!wh_i2c_get_feature(pdev, buf, 56)) 
		printf("failed to get i2c cfg\n");
	else {
		if (buf[0] != 0xf4)
			wh_printf("wrong id[0xf4] of fw response: 0x%x\n", buf[0]);
		else
			board_info.i2c_dummy = buf[1];
	}

	if (buf[0] == 0xf4 && (get_unaligned_le16(buf + 2) == 0x154f)) { 

		board_info.dev_type |= FW_WDT8755;
		board_info.platform_id[1] = buf[5];

		memcpy(&board_info.sys_param, &buf[10], get_unaligned_le16(buf + 12));
		if (wh_w8755_prepare_data(pdev, &board_info, 0)) {
			memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
			return 1;
		}
	}

	if (!wh_i2c_get_desc(pdev, GD_DEVICE, 0, (BYTE*) buf, 18)) {
		board_info.dev_type = FW_MAYBE_ISP;
		printf("Get device desc error !\n");
		return 0;	
	}



	ret_size = wh_i2c_get_desc(pdev, GD_STRING, STRIDX_PARAMETERS, (BYTE*) buf, 38);
	if (!ret_size)	{
		printf("Get parameters error !\n");
		return 0;
	}

	memset((void*) &board_info.sys_param, 0, sizeof(SYS_PARAM));
	memcpy((void*) &board_info.sys_param, buf, ret_size);	
	memcpy(pboard_info, &board_info, sizeof(BOARD_INFO));
	
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rsi, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
testq %rdi, %rdi
je 0x946c
cmpq $0x0, 0x8(%rsp)
je 0x946c
movq %rdi, %r14
cmpq $0x0, 0x50(%rdi)
je 0x946c
movzbl 0x1d6(%r14), %esi
leaq 0x3780(%rip), %rdi      # 0xc943
xorl %eax, %eax
callq 0x2506
leaq 0x68(%rsp), %rbx
movl $0xf0, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2110
movb 0x1d6(%r14), %al
movb %al, 0x4e(%rbx)
movl $0x2, 0x278(%r14)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8fef
testl %eax, %eax
jne 0x9210
leaq 0x374f(%rip), %rdi      # 0xc958
xorl %eax, %eax
callq 0x2506
leaq 0x11(%rsp), %r15
movb $-0xe, -0x1(%r15)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
leaq 0x10(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
movl $0x40, %edx
callq 0x95fc
movl %eax, %r13d
movq %r15, %rdi
callq 0x702b
movl %eax, %ebx
testl %r13d, %r13d
setg %al
testl %ebx, %ebx
setne %cl
testb %cl, %al
jne 0x9323
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
leaq 0x370d(%rip), %rdi      # 0xc973
xorl %eax, %eax
callq 0x2506
cmpb $0x49, 0x36(%rsp)
jne 0x931b
cmpb $0x53, 0x37(%rsp)
jne 0x931b
cmpb $0x50, 0x38(%rsp)
jne 0x931b
movq $0x0, (%rsp)
movl $0x2, %edi
movq %rsp, %rsi
callq 0x50c1
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x92be
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x50e1
movq $0x0, (%rsp)
movl $0x1, %edi
movq %rsp, %rsi
callq 0x50c1
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x92ee
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x50e1
movq $0x0, (%rsp)
xorl %edi, %edi
movq %rsp, %rsi
callq 0x50c1
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x931b
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x5151
testl %ebp, %ebp
je 0x9481
testl %ebp, %ebp
setne %al
testl %r13d, %r13d
setle %cl
incl %ebp
testl %ebx, %ebx
je 0x9224
andb %al, %cl
jne 0x9224
cmpb $-0xe, 0x10(%rsp)
jne 0x948a
movl %ebx, 0x78(%rsp)
leaq 0x15(%rsp), %rdi
callq 0x702b
movl %eax, 0x7c(%rsp)
leaq 0x19(%rsp), %rdi
callq 0x702b
movl %eax, 0x80(%rsp)
movl 0x78(%rsp), %esi
movq %r14, %rdi
callq 0x6f5e
movl %eax, 0x68(%rsp)
btl $0x9, %eax
jae 0x93db
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x5255
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x1f8(%r14)
movups %xmm1, 0x208(%r14)
movups %xmm2, 0x218(%r14)
movl 0x30(%rbx), %eax
movl %eax, 0x228(%r14)
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
callq 0x6ca8
testl %eax, %eax
jne 0x95ad
testb $0x78, 0x69(%rsp)
je 0x943b
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3d7c
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups 0x29(%rbx), %xmm3
movups %xmm0, 0x1f8(%r14)
movups %xmm1, 0x208(%r14)
movups %xmm2, 0x218(%r14)
movups %xmm3, 0x221(%r14)
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
callq 0x4c9a
testl %eax, %eax
jne 0x95ad
leaq 0x10(%rsp), %rsi
movb $-0xc, (%rsi)
movq %r14, %rdi
movl $0x38, %edx
callq 0x95fc
testl %eax, %eax
je 0x94a8
movzbl 0x10(%rsp), %esi
cmpl $0xf4, %esi
jne 0x94b6
movzbl 0x11(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x94c4
leaq 0x34ba(%rip), %rdi      # 0xc92d
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2506
jmp 0x95c6
leaq 0x36f0(%rip), %rdi      # 0xcb78
jmp 0x949c
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
leaq 0x36c1(%rip), %rdi      # 0xcb5d
callq 0x2270
xorl %ebx, %ebx
jmp 0x95c6
leaq 0x3669(%rip), %rdi      # 0xcb18
callq 0x2270
jmp 0x94c4
leaq 0x34e0(%rip), %rdi      # 0xc99d
xorl %eax, %eax
callq 0x2506
cmpb $-0xc, 0x10(%rsp)
jne 0x9526
leaq 0x12(%rsp), %rdi
callq 0x7027
movzwl %ax, %eax
cmpl $0x154f, %eax           # imm = 0x154F
jne 0x9526
leaq 0x84(%rsp), %rbx
orb $0x20, -0x1c(%rbx)
leaq 0x1a(%rsp), %r15
movb -0x5(%r15), %al
movb %al, 0x27(%rbx)
leaq 0x1c(%rsp), %rdi
callq 0x7027
movzwl %ax, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2150
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3cbc
testl %eax, %eax
jne 0x95ad
xorl %ebx, %ebx
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movl $0x1, %esi
xorl %edx, %edx
movl $0x12, %r8d
callq 0x9771
testl %eax, %eax
je 0x95da
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movl $0x3, %esi
movl $0x81, %edx
movl $0x26, %r8d
callq 0x9771
testl %eax, %eax
je 0x95f0
leaq 0x84(%rsp), %rbx
movslq %eax, %r14
movl $0x26, %ecx
subq %r14, %rcx
xorl %edx, %edx
cmpl $0x26, %eax
cmovbq %rcx, %rdx
leaq (%rsp,%r14), %rdi
addq $0x84, %rdi
xorl %esi, %esi
callq 0x2110
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2150
leaq 0x68(%rsp), %rsi
movl $0xf0, %edx
movq 0x8(%rsp), %rdi
callq 0x2150
movl $0x1, %ebx
movl %ebx, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0x68(%rsp)
leaq 0x3545(%rip), %rdi      # 0xcb2e
callq 0x2270
jmp 0x95c6
leaq 0x354f(%rip), %rdi      # 0xcb46
jmp 0x949c