// File: /workspace/github_cpp_makefile_star2_O3/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 0x93fc
cmpq $0x0, 0x8(%rsp)
je 0x93fc
movq %rdi, %r14
cmpq $0x0, 0x50(%rdi)
je 0x93fc
movzbl 0x1d6(%r14), %esi
leaq 0x37f0(%rip), %rdi      # 0xc93f
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 0x8f7d
testl %eax, %eax
jne 0x919c
leaq 0x37bf(%rip), %rdi      # 0xc954
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 0x958c
movl %eax, %r13d
movq %r15, %rdi
callq 0x6f6b
movl %eax, %ebx
testl %r13d, %r13d
setg %al
testl %ebx, %ebx
setne %cl
testb %cl, %al
jne 0x92af
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
leaq 0x377d(%rip), %rdi      # 0xc96f
xorl %eax, %eax
callq 0x2506
cmpb $0x49, 0x36(%rsp)
jne 0x92a7
cmpb $0x53, 0x37(%rsp)
jne 0x92a7
cmpb $0x50, 0x38(%rsp)
jne 0x92a7
movq $0x0, (%rsp)
movl $0x2, %edi
movq %rsp, %rsi
callq 0x50a9
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x924a
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x50c9
movq $0x0, (%rsp)
movl $0x1, %edi
movq %rsp, %rsi
callq 0x50a9
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x927a
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x50c9
movq $0x0, (%rsp)
xorl %edi, %edi
movq %rsp, %rsi
callq 0x50a9
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x92a7
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x5133
testl %ebp, %ebp
je 0x9411
testl %r13d, %r13d
setle %al
testl %ebp, %ebp
setne %cl
incl %ebp
testl %ebx, %ebx
je 0x91b0
andb %cl, %al
jne 0x91b0
cmpb $-0xe, 0x10(%rsp)
jne 0x941a
movl %ebx, 0x78(%rsp)
leaq 0x15(%rsp), %rdi
callq 0x6f6b
movl %eax, 0x7c(%rsp)
leaq 0x19(%rsp), %rdi
callq 0x6f6b
movl %eax, 0x80(%rsp)
movl 0x78(%rsp), %esi
movq %r14, %rdi
callq 0x6e9e
movl %eax, 0x68(%rsp)
btl $0x9, %eax
jae 0x936b
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x5235
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 0x6bff
testl %eax, %eax
jne 0x953d
movl 0x68(%rsp), %eax
testl $0x7800, %eax           # imm = 0x7800
je 0x93cb
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3da0
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 0x4c89
testl %eax, %eax
jne 0x953d
leaq 0x10(%rsp), %rsi
movb $-0xc, (%rsi)
movq %r14, %rdi
movl $0x38, %edx
callq 0x958c
testl %eax, %eax
je 0x9438
movzbl 0x10(%rsp), %esi
cmpl $0xf4, %esi
jne 0x9446
movzbl 0x11(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x9454
leaq 0x3526(%rip), %rdi      # 0xc929
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2506
jmp 0x9556
leaq 0x375c(%rip), %rdi      # 0xcb74
jmp 0x942c
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
leaq 0x372d(%rip), %rdi      # 0xcb59
callq 0x2270
xorl %ebx, %ebx
jmp 0x9556
leaq 0x36d5(%rip), %rdi      # 0xcb14
callq 0x2270
jmp 0x9454
leaq 0x354c(%rip), %rdi      # 0xc999
xorl %eax, %eax
callq 0x2506
cmpb $-0xc, 0x10(%rsp)
jne 0x94b6
leaq 0x12(%rsp), %rdi
callq 0x6f67
movzwl %ax, %eax
cmpl $0x154f, %eax           # imm = 0x154F
jne 0x94b6
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 0x6f67
movzwl %ax, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2150
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3cdf
testl %eax, %eax
jne 0x953d
xorl %ebx, %ebx
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movl $0x1, %esi
xorl %edx, %edx
movl $0x12, %r8d
callq 0x970f
testl %eax, %eax
je 0x956a
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movl $0x3, %esi
movl $0x81, %edx
movl $0x26, %r8d
callq 0x970f
testl %eax, %eax
je 0x9580
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 0x35b1(%rip), %rdi      # 0xcb2a
callq 0x2270
jmp 0x9556
leaq 0x35bb(%rip), %rdi      # 0xcb42
jmp 0x942c