// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: init_n_scan_device(WdtDevice*, ExecParam*, unsigned int)
int init_n_scan_device(WDT_DEV *pdev, EXEC_PARAM *pparam, unsigned int flag)
{
	WDT_DEVICE_INFO	wdtDevInfo;

	if (!pdev || !pparam)
		return 0;

	pdev->intf_index = pparam->interface_num;

	if (!pdev->func_wh_get_device_access_func(pdev->intf_index, &pdev->funcs_device)) {
		wh_printf("Get device funcs error");
		return 0;		
	}

	if (!strlen(pparam->dev_path)) {
		memset(&wdtDevInfo, 0, sizeof(WDT_DEVICE_INFO));	
		
		/* num of devices */
		int num = pdev->funcs_device.p_wh_scan_device(pdev);
		
		if (num == 0) {
			wh_printf("Open device error");
			return 0;
		}

		/* just use index 0 to get the device info */
		if (!pdev->funcs_device.p_wh_get_device(pdev, &wdtDevInfo, 0))
			return 0;
			
		strcpy(pdev->dev_path, wdtDevInfo.path);
	}else{
        	struct dirent *dir;
        	pdev->board_info.i2c_address = DEFAULT_I2C_ADDR;
        	wh_printf("%s\n", pparam->dev_path);
        	char i2c_sysfs_path[64] = "/sys/bus/i2c/devices";	
        	strcat(i2c_sysfs_path, strdup(&pparam->dev_path[4]));
        	DIR* d = opendir(i2c_sysfs_path);
        	wh_printf("%s\n", i2c_sysfs_path);
		if (d) {	
			while ((dir = readdir(d)) != NULL) {
				if (memcmp(dir->d_name, ACPI_NAME_HID, strlen(ACPI_NAME_HID)) == 0) {
					char* reg_gen_hid  = strdup(&dir->d_name[4]);
					wh_printf("current reg_gen_hid:%s\n", reg_gen_hid);
					pdev->board_info.i2c_address = get_i2c_address_map(reg_gen_hid);
					wh_printf("i2c_address %x\n", pdev->board_info.i2c_address);
					break;	
				}				
			}
            		closedir(d);
		}
        
		strcpy(pdev->dev_path, pparam->dev_path);
    	}
	if (pdev->funcs_device.p_wh_open_device(pdev)) {
		if (pdev->funcs_device.p_wh_prepare_data(pdev, &pdev->board_info)) {		
			if (pdev->is_legacy)
				return 1;
			
			if (!pdev->func_wh_get_device_private_access_func(pdev, &pdev->funcs_device_private)) {
				wh_printf("Get device private funcs error");
				return 0;		
			}
			return 1;
		}else
			wh_printf("Get system info error");
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x6b24
movq %rsi, %r15
movq %rdi, %rbx
movl 0x4(%rsi), %edi
movl %edi, (%rbx)
leaq 0x288(%rbx), %rsi
callq *0x320(%rbx)
testl %eax, %eax
je 0x6ac4
cmpb $0x0, 0x18(%r15)
je 0x6acd
leaq 0x18(%r15), %r14
movb $0x2c, 0x1d6(%rbx)
leaq 0x4f46(%rip), %r12      # 0xb8d6
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x24bf
movaps 0x44ac(%rip), %xmm0     # 0xae50
movq %rsp, %r13
movaps %xmm0, 0x30(%r13)
movaps 0x448d(%rip), %xmm0     # 0xae40
movaps %xmm0, 0x20(%r13)
movaps 0x4471(%rip), %xmm0     # 0xae30
movaps %xmm0, 0x10(%r13)
movaps 0x4455(%rip), %xmm0     # 0xae20
movaps %xmm0, (%r13)
addq $0x1c, %r15
movq %r15, %rdi
callq 0x22d0
movq %r13, %rdi
movq %rax, %rsi
callq 0x22c0
movq %r13, %rdi
callq 0x2250
movq %rax, %r15
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x24bf
testq %r15, %r15
je 0x6a68
movabsq $0x544844572d633269, %r12 # imm = 0x544844572D633269
movq %r15, %rdi
callq 0x2260
testq %rax, %rax
je 0x6a60
cmpq %r12, 0x13(%rax)
jne 0x6a0e
addq $0x17, %rax
movq %rax, %rdi
callq 0x22d0
movq %rax, %r12
leaq 0x4489(%rip), %rdi      # 0xaec0
movq %rax, %rsi
xorl %eax, %eax
callq 0x24bf
movq %r12, %rdi
callq 0x7e1f
movb %al, 0x1d6(%rbx)
movzbl %al, %esi
leaq 0x447f(%rip), %rdi      # 0xaed8
xorl %eax, %eax
callq 0x24bf
movq %r15, %rdi
callq 0x22a0
leaq 0xc(%rbx), %rdi
movq %r14, %rsi
callq 0x2180
movq %rbx, %rdi
callq *0x298(%rbx)
movl $0x0, %ebp
testl %eax, %eax
je 0x6b24
leaq 0x188(%rbx), %rsi
movq %rbx, %rdi
callq *0x2a8(%rbx)
testl %eax, %eax
je 0x6b0b
pushq $0x1
popq %rbp
cmpl $0x0, 0x4(%rbx)
jne 0x6b24
leaq 0x2b0(%rbx), %rsi
movq %rbx, %rdi
callq *0x328(%rbx)
testl %eax, %eax
jne 0x6b24
leaq 0x4426(%rip), %rdi      # 0xaee8
jmp 0x6b1b
leaq 0x43c3(%rip), %rdi      # 0xae8e
jmp 0x6b1b
movq %rsp, %rdi
xorl %ebp, %ebp
movl $0x108, %edx            # imm = 0x108
xorl %esi, %esi
callq 0x2110
movq %rbx, %rdi
callq *0x288(%rbx)
testl %eax, %eax
je 0x6b14
movq %rsp, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq *0x290(%rbx)
testl %eax, %eax
je 0x6b24
leaq 0xc(%rbx), %rdi
leaq 0x8(%rsp), %rsi
jmp 0x6a6f
leaq 0x43f5(%rip), %rdi      # 0xaf07
jmp 0x6b1b
leaq 0x438a(%rip), %rdi      # 0xaea5
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x24bf
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq