// File: 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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x9874
cmpq $0x0, -0x18(%rbp)
jne 0x9880
movl $0x0, -0x4(%rbp)
jmp 0x9b87
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq 0x320(%rax), %rax
movq -0x10(%rbp), %rcx
movl (%rcx), %edi
movq -0x10(%rbp), %rsi
addq $0x288, %rsi            # imm = 0x288
callq *%rax
cmpl $0x0, %eax
jne 0x98ca
leaq 0x669f(%rip), %rdi      # 0xff56
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0x9b87
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
callq 0x20d0
cmpq $0x0, %rax
jne 0x9976
leaq -0x124(%rbp), %rdi
xorl %esi, %esi
movl $0x108, %edx            # imm = 0x108
callq 0x2110
movq -0x10(%rbp), %rax
movq 0x288(%rax), %rax
movq -0x10(%rbp), %rdi
callq *%rax
movl %eax, -0x128(%rbp)
cmpl $0x0, -0x128(%rbp)
jne 0x992e
leaq 0x6652(%rip), %rdi      # 0xff6d
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0x9b87
movq -0x10(%rbp), %rax
movq 0x290(%rax), %rax
movq -0x10(%rbp), %rdi
leaq -0x124(%rbp), %rsi
xorl %edx, %edx
callq *%rax
cmpl $0x0, %eax
jne 0x9959
movl $0x0, -0x4(%rbp)
jmp 0x9b87
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
leaq -0x124(%rbp), %rsi
addq $0x8, %rsi
callq 0x2180
jmp 0x9ae1
movq -0x10(%rbp), %rax
movb $0x2c, 0x1d6(%rax)
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq 0x7094(%rip), %rdi      # 0x10a24
movb $0x0, %al
callq 0x24e0
leaq -0x170(%rbp), %rdi
leaq 0x6e1b(%rip), %rsi      # 0x107c0
movl $0x40, %edx
callq 0x2150
leaq -0x170(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
addq $0x4, %rdi
callq 0x22b0
movq -0x188(%rbp), %rdi
movq %rax, %rsi
callq 0x22a0
leaq -0x170(%rbp), %rdi
callq 0x2240
movq %rax, -0x178(%rbp)
leaq -0x170(%rbp), %rsi
leaq 0x7026(%rip), %rdi      # 0x10a24
movb $0x0, %al
callq 0x24e0
cmpq $0x0, -0x178(%rbp)
je 0x9acc
jmp 0x9a15
movq -0x178(%rbp), %rdi
callq 0x2250
movq %rax, -0x130(%rbp)
cmpq $0x0, %rax
je 0x9ac0
movq -0x130(%rbp), %rax
addq $0x13, %rax
movq (%rax), %rax
movabsq $0x544844572d633269, %rcx # imm = 0x544844572D633269
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x9abb
movq -0x130(%rbp), %rdi
addq $0x13, %rdi
addq $0x4, %rdi
callq 0x22b0
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rsi
leaq 0x6507(%rip), %rdi      # 0xff88
movb $0x0, %al
callq 0x24e0
movq -0x180(%rbp), %rdi
callq 0xb830
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x1d6(%rax)
movq -0x10(%rbp), %rax
movzbl 0x1d6(%rax), %esi
leaq 0x64ee(%rip), %rdi      # 0xffa0
movb $0x0, %al
callq 0x24e0
jmp 0x9ac0
jmp 0x9a15
movq -0x178(%rbp), %rdi
callq 0x2280
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
callq 0x2180
movq -0x10(%rbp), %rax
movq 0x298(%rax), %rax
movq -0x10(%rbp), %rdi
callq *%rax
cmpl $0x0, %eax
je 0x9b80
movq -0x10(%rbp), %rax
movq 0x2a8(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x188, %rsi            # imm = 0x188
callq *%rax
cmpl $0x0, %eax
je 0x9b70
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x9b2f
movl $0x1, -0x4(%rbp)
jmp 0x9b87
movq -0x10(%rbp), %rax
movq 0x328(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x2b0, %rsi            # imm = 0x2B0
callq *%rax
cmpl $0x0, %eax
jne 0x9b67
leaq 0x6459(%rip), %rdi      # 0xffb0
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0x9b87
movl $0x1, -0x4(%rbp)
jmp 0x9b87
leaq 0x6458(%rip), %rdi      # 0xffcf
movb $0x0, %al
callq 0x24e0
jmp 0x9b80
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopw %cs:(%rax,%rax)