// File: /workspace/github_cpp_makefile_star2_O2/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 0x875b
cmpq $0x0, 0x8(%rsp)
je 0x875b
movq %rdi, %r14
cmpq $0x0, 0x50(%rdi)
je 0x875b
movzbl 0x1d6(%r14), %esi
leaq 0x335b(%rip), %rdi      # 0xb93f
xorl %eax, %eax
callq 0x24bf
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 0x842c
testl %eax, %eax
jne 0x8631
leaq 0x332a(%rip), %rdi      # 0xb954
xorl %eax, %eax
callq 0x24bf
leaq 0x11(%rsp), %r15
movb $-0xe, -0x1(%r15)
pushq $0x3
popq %r12
xorl %ebx, %ebx
xorl %ebp, %ebp
movl %r12d, %r13d
addl $-0x1, %r12d
setae %al
testl %ebx, %ebx
setg %cl
orb %al, %cl
testl %ebp, %ebp
setne %al
testb %cl, %al
jne 0x8770
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
pushq $0x40
popq %rdx
callq 0x8a10
movl %eax, %ebx
movq %r15, %rdi
callq 0x6687
movl %eax, %ebp
testl %ebx, %ebx
setg %al
testl %ebp, %ebp
setne %cl
testb %cl, %al
jne 0x8643
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
leaq 0x32d2(%rip), %rdi      # 0xb96f
xorl %eax, %eax
callq 0x24bf
cmpb $0x49, 0x36(%rsp)
jne 0x8745
cmpb $0x53, 0x37(%rsp)
jne 0x8745
cmpb $0x50, 0x38(%rsp)
jne 0x8745
andq $0x0, (%rsp)
pushq $0x2
popq %rdi
movq %rsp, %rsi
callq 0x4bd2
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x86f0
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x4bf0
andq $0x0, (%rsp)
pushq $0x1
popq %rdi
movq %rsp, %rsi
callq 0x4bd2
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x871b
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x4bf0
andq $0x0, (%rsp)
xorl %edi, %edi
movq %rsp, %rsi
callq 0x4bd2
movq 0x28(%rsp), %rax
cmpq (%rsp), %rax
jne 0x8745
movl $0x5000, 0x68(%rsp)     # imm = 0x5000
movq %r14, %rdi
callq 0x4c39
cmpl $0x1, %r13d
jne 0x8643
leaq 0x341e(%rip), %rdi      # 0xbb74
jmp 0x8895
leaq 0x31c7(%rip), %rdi      # 0xb929
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
jmp 0x889c
cmpb $-0xe, 0x10(%rsp)
jne 0x8883
movl %ebp, 0x78(%rsp)
leaq 0x15(%rsp), %rdi
callq 0x6687
movl %eax, 0x7c(%rsp)
leaq 0x19(%rsp), %rdi
callq 0x6687
movl %eax, 0x80(%rsp)
movl 0x78(%rsp), %esi
movq %r14, %rdi
callq 0x65c0
movl %eax, 0x68(%rsp)
btl $0x9, %eax
pushq $0x1
popq %rbp
jae 0x880e
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4d2f
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 0x632d
testl %eax, %eax
jne 0x886d
movl 0x68(%rsp), %eax
testw $0x7800, %ax            # imm = 0x7800
je 0x88b0
leaq 0xd8(%rsp), %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3af8
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 0x47d4
testl %eax, %eax
je 0x88b0
leaq 0x68(%rsp), %rsi
movl $0xf0, %edx
movq 0x8(%rsp), %rdi
callq 0x2150
jmp 0x889c
movq 0x8(%rsp), %rax
movl $0x1, (%rax)
leaq 0x32c4(%rip), %rdi      # 0xbb59
callq 0x2270
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rsi
movb $-0xc, (%rsi)
pushq $0x38
popq %rdx
movq %r14, %rdi
callq 0x8a10
testl %eax, %eax
je 0x88df
movzbl 0x10(%rsp), %esi
cmpl $0xf4, %esi
jne 0x88ed
movzbl 0x11(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x88fb
leaq 0x322e(%rip), %rdi      # 0xbb14
callq 0x2270
jmp 0x88fb
leaq 0x30a5(%rip), %rdi      # 0xb999
xorl %eax, %eax
callq 0x24bf
cmpb $-0xc, 0x10(%rsp)
jne 0x8959
leaq 0x12(%rsp), %rdi
callq 0x6683
cmpw $0x154f, %ax            # imm = 0x154F
jne 0x8959
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 0x6683
movzwl %ax, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2150
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3a3e
testl %eax, %eax
jne 0x886d
pushq $0x1
popq %rbx
xorl %ebp, %ebp
leaq 0x10(%rsp), %rcx
pushq $0x12
popq %r8
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x8b7a
testl %eax, %eax
je 0x89eb
pushq $0x3
popq %rsi
leaq 0x10(%rsp), %rcx
pushq $0x26
popq %r8
movq %r14, %rdi
movl $0x81, %edx
callq 0x8b7a
testl %eax, %eax
je 0x8a04
leaq 0x84(%rsp), %r14
movslq %eax, %r15
pushq $0x26
popq %rcx
subq %r15, %rcx
xorl %edx, %edx
cmpl $0x26, %eax
cmovbq %rcx, %rdx
leaq (%rsp,%r15), %rdi
addq $0x84, %rdi
xorl %esi, %esi
callq 0x2110
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x2150
leaq 0x68(%rsp), %rsi
movl $0xf0, %edx
movq 0x8(%rsp), %rdi
callq 0x2150
movl %ebx, %ebp
jmp 0x889c
movl $0x1, 0x68(%rsp)
leaq 0x3130(%rip), %rdi      # 0xbb2a
callq 0x2270
jmp 0x889c
leaq 0x3137(%rip), %rdi      # 0xbb42
jmp 0x8895