// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x0, 0x10(%rsp)
movq $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 0xa6cc
movq %rdi, %rbx
movl $0xa, %ebp
leaq 0x7(%rsp), %r14
leaq 0x16c5e(%rip), %r15     # 0x21291
leaq 0xc680(%rip), %r12      # 0x16cba
leaq 0x8(%rsp), %r13
movq (%rbx), %rax
movq %rbx, %rdi
movl $0x8050, %esi           # imm = 0x8050
movq %r14, %rdx
movl $0x1, %ecx
callq *0x20(%rax)
testl %eax, %eax
js 0xa694
movq (%rbx), %rax
movq %rbx, %rdi
movl $0x8140, %esi           # imm = 0x8140
movq %r13, %rdx
movl $0xc, %ecx
callq *0x20(%rax)
testl %eax, %eax
jns 0xa6f2
cmpb $0x1, (%r15)
jne 0xa6bd
movq 0x16938(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc6f7(%rip), %rsi      # 0x16d81
movq %r12, %rdx
movl $0x3e, %ecx
jmp 0xa6b3
cmpb $0x1, (%r15)
jne 0xa6bd
movq 0x16917(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc685(%rip), %rsi      # 0x16d30
movq %r12, %rdx
movl $0x38, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
decl %ebp
jne 0xa63f
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xa6e1
leaq 0x16bbe(%rip), %rax     # 0x21291
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x1, (%rax)
je 0xa814
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)
cmpb $0x1, (%r15)
jne 0xa72e
movq 0x168aa(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc762(%rip), %rsi      # 0x16e7a
leaq 0xc59b(%rip), %rdx      # 0x16cba
movl $0x48, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x5180
movzbl 0x12(%rsp), %r8d
andl $0xf, %r8d
movl %r8d, 0x18(%rbx)
cmpb $0x1, (%r15)
jne 0xa766
movq 0x1686f(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
leaq 0xc740(%rip), %rsi      # 0x16e93
leaq 0xc560(%rip), %rdx      # 0x16cba
movl $0x4a, %ecx
xorl %eax, %eax
callq 0x5180
cmpl $0x38383237, (%r14)     # imm = 0x38383237
jne 0xa781
movzbl 0xd(%rsp), %eax
movl %eax, 0x10(%rbx)
movzbl 0xe(%rsp), %eax
shll $0x10, %eax
jmp 0xa79b
movzbl 0xc(%rsp), %eax
movl %eax, 0x10(%rbx)
movzbl 0xd(%rsp), %ecx
shll $0x10, %ecx
movzbl 0xe(%rsp), %eax
shll $0x8, %eax
orl %ecx, %eax
movzbl 0x7(%rsp), %ecx
orl %eax, %ecx
movl %ecx, 0x14(%rbx)
cmpb $0x1, (%r15)
jne 0xa7d5
movq 0x16806(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movzbl 0x7(%rsp), %r8d
leaq 0xd3bb(%rip), %rsi      # 0x17b7d
leaq 0xc4f1(%rip), %rdx      # 0x16cba
movl $0x55, %ecx
xorl %eax, %eax
callq 0x5180
xorl %ebp, %ebp
cmpb $0x1, (%r15)
jne 0xa6e1
movq 0x167d0(%rip), %rax     # 0x20fb8
movq (%rax), %rdi
movl 0x10(%rbx), %r8d
movl 0x14(%rbx), %r9d
leaq 0xc6b7(%rip), %rsi      # 0x16eb1
leaq 0xc4b9(%rip), %rdx      # 0x16cba
xorl %ebp, %ebp
movl $0x57, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xa6e1
callq 0x56dd
jmp 0xa6e1