// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx2/gtx2.cpp
// Function: GTx2Device::SetBasicProperties()
int GTx2Device::SetBasicProperties()
{
	int ret;
	unsigned char fw_info[12] = {0};
	m_firmwareVersionMajor = 20;
	m_firmwareVersionMinor = 20;
	m_sensorID = 2;
	unsigned char cfg_ver = 0;
	int retry = 10;

	if (!m_deviceOpen) {
		gdix_err("Please open device first\n");
		return -1;
	}

	do {
		ret = Read(0x8050, &cfg_ver, 1);
		if (ret < 0) {
			gdix_dbg("Failed read cfg VERSION, retry=%d\n", retry);
			continue;
		}

		ret = Read(0x8140, fw_info, sizeof(fw_info));
		if (ret < 0)
			gdix_dbg("Failed read VERSION, retry=%d\n", retry);
		else
			break;
	} while (--retry);

	if (!retry)
		return -1;

	memcpy(m_pid, fw_info, 4);
	m_pid[4] = '\0';
	gdix_dbg("pid:%s\n", m_pid);
	m_sensorID = fw_info[10] & 0x0f;
	gdix_dbg("sensorID:%d\n", m_sensorID);

	if (!memcmp(m_pid, "7288", 4)) {
		m_firmwareVersionMajor = fw_info[5];
		m_firmwareVersionMinor = ((fw_info[6] << 16) | (0x00) << 8) | cfg_ver;
	} else {
		m_firmwareVersionMajor = fw_info[4];
		m_firmwareVersionMinor =
			((fw_info[5] << 16) | (fw_info[6]) << 8) | cfg_ver;
	}

	gdix_dbg("cfg version:%d\n", cfg_ver);
	gdix_dbg("version:0x%x,0x%x\n", m_firmwareVersionMajor,
			 m_firmwareVersionMinor);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
andl $0x0, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
movabsq $0x1400000014, %rax     # imm = 0x1400000014
movq %rax, 0x10(%rdi)
movl $0x2, 0x18(%rdi)
movb $0x0, 0x7(%rsp)
cmpb $0x1, 0x8(%rdi)
jne 0x8db9
movq %rdi, %rbx
pushq $0xa
popq %r14
leaq 0x7(%rsp), %r15
pushq $0x1
popq %r12
leaq 0x17569(%rip), %rbp     # 0x20291
pushq $0xc
popq %r13
movq (%rbx), %rax
movq %rbx, %rdi
movl $0x8050, %esi           # imm = 0x8050
movq %r15, %rdx
movl %r12d, %ecx
callq *0x20(%rax)
testl %eax, %eax
js 0x8d80
movq (%rbx), %rax
movq %rbx, %rdi
movl $0x8140, %esi           # imm = 0x8140
leaq 0x8(%rsp), %rdx
movl %r13d, %ecx
callq *0x20(%rax)
testl %eax, %eax
jns 0x8ddd
cmpb $0x1, (%rbp)
jne 0x8dab
movq 0x1724d(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xcfab(%rip), %rsi      # 0x15d20
leaq 0xcedd(%rip), %rdx      # 0x15c59
pushq $0x3e
jmp 0x8da0
cmpb $0x1, (%rbp)
jne 0x8dab
movq 0x1722b(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xcf38(%rip), %rsi      # 0x15ccf
leaq 0xcebb(%rip), %rdx      # 0x15c59
pushq $0x38
popq %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x5180
decl %r14d
jne 0x8d2c
pushq $-0x1
popq %rbp
jmp 0x8dcc
leaq 0x174d1(%rip), %rax     # 0x20291
pushq $-0x1
popq %rbp
cmpb $0x1, (%rax)
je 0x8ef5
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c(%rbx), %r14
movl 0x8(%rsp), %eax
movl %eax, 0x1c(%rbx)
movb $0x0, 0x20(%rbx)
movb (%rbp), %al
cmpb $0x1, %al
jne 0x8e1b
movq 0x171be(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xd015(%rip), %rsi      # 0x15e19
leaq 0xce4e(%rip), %rdx      # 0x15c59
pushq $0x48
popq %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x5180
movb (%rbp), %al
movzbl 0x12(%rsp), %r8d
andl $0xf, %r8d
movl %r8d, 0x18(%rbx)
testb $0x1, %al
je 0x8e4f
movq 0x17184(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xcff4(%rip), %rsi      # 0x15e32
leaq 0xce14(%rip), %rdx      # 0x15c59
pushq $0x4a
popq %rcx
xorl %eax, %eax
callq 0x5180
cmpl $0x38383237, (%r14)     # imm = 0x38383237
jne 0x8e66
movb 0xd(%rsp), %al
movzbl 0xe(%rsp), %ecx
shll $0x10, %ecx
jmp 0x8e7c
movb 0xc(%rsp), %al
movzbl 0xd(%rsp), %edx
shll $0x10, %edx
movzbl 0xe(%rsp), %ecx
shll $0x8, %ecx
orl %edx, %ecx
movzbl 0x7(%rsp), %r8d
orl %r8d, %ecx
movzbl %al, %eax
movl %eax, 0x10(%rbx)
movl %ecx, 0x14(%rbx)
movb (%rbp), %al
cmpb $0x1, %al
jne 0x8eba
movq 0x1711c(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
leaq 0xdc76(%rip), %rsi      # 0x16b1c
leaq 0xcdac(%rip), %rdx      # 0x15c59
pushq $0x55
popq %rcx
xorl %eax, %eax
callq 0x5180
movb (%rbp), %al
xorl %ebp, %ebp
testb $0x1, %al
je 0x8dcc
movq 0x170ed(%rip), %rax     # 0x1ffb8
movq (%rax), %rdi
movl 0x10(%rbx), %r8d
movl 0x14(%rbx), %r9d
leaq 0xcf73(%rip), %rsi      # 0x15e50
leaq 0xcd75(%rip), %rdx      # 0x15c59
xorl %ebp, %ebp
pushq $0x57
popq %rcx
xorl %eax, %eax
callq 0x5180
jmp 0x8dcc
movq 0x170dc(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0xcd28(%rip), %rsi      # 0x15c2e
leaq 0xcd4c(%rip), %rdx      # 0x15c59
pushq $0x31
popq %rcx
xorl %eax, %eax
callq 0x5180
jmp 0x8dcc