// File: goodix[P]gdix_hid_firmware_update/gtx5/gtx5.cpp
// Function: GTx5Device::ReadPkg(unsigned int, unsigned char*, unsigned int)
int GTx5Device::ReadPkg(unsigned int addr, unsigned char *buf, unsigned int len)
{
	int ret;
	int retry = 0;
	unsigned char HidBuf[65] = {0x0e, _I2C_DIRECT_RW, 0, 0, 5, 1};
	unsigned int pkg_index = 0;
	unsigned int read_data_len = 0;
re_start:
	pkg_index = 0;
	read_data_len = 0;
	HidBuf[0] = 0x0e;
	HidBuf[1] = _I2C_DIRECT_RW;
	HidBuf[2] = 0;
	HidBuf[3] = 0;
	HidBuf[4] = 5;
	HidBuf[5] = 1; /* read operation flag */
	HidBuf[6] = (addr >> 8) & 0xff;
	HidBuf[7] = addr & 0xff;
	HidBuf[8] = (len >> 8) & 0xff;
	HidBuf[9] = len & 0xff;
	ret = Write(HidBuf, 10);

	if (ret < 0) {
		gdix_dbg("Failed send read start package, ret = %d\n", ret);
		return -1;
	}
	do {
		ret = GetReport(0x0e, HidBuf);
		if (ret) {
			gdix_dbg("Failed read addr=0x%x, len=%d\n", addr, len);
			break;
		} else {
			if (pkg_index != HidBuf[3]) {
				if (retry++ < GDIX_RETRY_TIMES) {
					gdix_dbg("Read retry %d, pkg_index %d != HidBuf[3](%d)\n",
							 retry, pkg_index, HidBuf[3]);
					usleep(1000);
					goto re_start;
				}
				ret = -E_HID_PKG_INDEX;
				break;
			} else {
				if (HidBuf[4] == len - read_data_len) {
					memcpy(buf + read_data_len, &HidBuf[5], HidBuf[4]);
					read_data_len += HidBuf[4];
					pkg_index++;
				} else {
					gdix_dbg("Data length err: %d != %d\n", HidBuf[4],
							 len - read_data_len);
					gdix_dbg_array(HidBuf, 6);
					if (retry++ < GDIX_RETRY_TIMES) {
						gdix_dbg("Read retry: %d\n", retry);
						usleep(1000);
						goto re_start;
					}
					ret = -E_HID_PKG_LEN;
					break;
				}
			}
		}
	} while (read_data_len != len && (retry < GDIX_RETRY_TIMES));
	if (ret < 0)
		return ret;
	else
		return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x2c(%rbp)
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x41, %edx
callq 0x50a0
movb $0xe, -0x70(%rbp)
movb $0x20, -0x6f(%rbp)
movb $0x5, -0x6c(%rbp)
movb $0x1, -0x6b(%rbp)
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x80(%rbp), %rdi
movl $0x0, -0x74(%rbp)
movl $0x0, -0x78(%rbp)
movb $0xe, -0x70(%rbp)
movb $0x20, -0x6f(%rbp)
movb $0x0, -0x6e(%rbp)
movb $0x0, -0x6d(%rbp)
movb $0x5, -0x6c(%rbp)
movb $0x1, -0x6b(%rbp)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x6a(%rbp)
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, -0x69(%rbp)
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x68(%rbp)
movl -0x24(%rbp), %eax
andl $0xff, %eax
movb %al, -0x67(%rbp)
leaq -0x70(%rbp), %rsi
movq (%rdi), %rax
movl $0xa, %edx
callq *0x38(%rax)
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jge 0xee8e
jmp 0xee4c
leaq 0x17446(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xee80
movq 0x17159(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x28(%rbp), %r8d
leaq 0xf5ba(%rip), %rsi      # 0x1e427
leaq 0xf5ee(%rip), %rdx      # 0x1e462
movl $0x9b, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xee82
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xf093
jmp 0xee90
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rdx
movq (%rdi), %rax
movl $0xe, %esi
callq *0x28(%rax)
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0xeeed
jmp 0xeeae
leaq 0x173e4(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xeee6
movq 0x170f7(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x14(%rbp), %r8d
movl -0x24(%rbp), %r9d
leaq 0xf597(%rip), %rsi      # 0x1e46a
leaq 0xf588(%rip), %rdx      # 0x1e462
movl $0xa1, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xeee8
jmp 0xf07f
movl -0x74(%rbp), %eax
movzbl -0x6d(%rbp), %ecx
cmpl %ecx, %eax
je 0xef66
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cmpl $0x6, %eax
jge 0xef5a
jmp 0xef0a
leaq 0x17388(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xef49
movq 0x1709b(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
movl -0x74(%rbp), %r9d
movzbl -0x6d(%rbp), %eax
leaq 0xf567(%rip), %rsi      # 0x1e49a
leaq 0xf528(%rip), %rdx      # 0x1e462
movl $0xa7, %ecx
movl %eax, (%rsp)
movb $0x0, %al
callq 0x5190
jmp 0xef4b
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5180
jmp 0xedd6
movl $0xfffffe0c, -0x28(%rbp) # imm = 0xFFFFFE0C
jmp 0xf07f
movzbl -0x6c(%rbp), %eax
movl -0x24(%rbp), %ecx
subl -0x78(%rbp), %ecx
cmpl %ecx, %eax
jne 0xefa9
movq -0x20(%rbp), %rdi
movl -0x78(%rbp), %eax
addq %rax, %rdi
leaq -0x70(%rbp), %rsi
addq $0x5, %rsi
movzbl -0x6c(%rbp), %eax
movl %eax, %edx
callq 0x50b0
movzbl -0x6c(%rbp), %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xf04e
jmp 0xefab
leaq 0x172e7(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xefe8
movq 0x16ffa(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movzbl -0x6c(%rbp), %r8d
movl -0x24(%rbp), %r9d
subl -0x78(%rbp), %r9d
leaq 0xf504(%rip), %rsi      # 0x1e4d9
leaq 0xf486(%rip), %rdx      # 0x1e462
movl $0xb4, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xefea
jmp 0xefec
jmp 0xefee
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cmpl $0x6, %eax
jge 0xf045
jmp 0xf000
leaq 0x17292(%rip), %rax     # 0x26299
testb $0x1, (%rax)
je 0xf034
movq 0x16fa5(%rip), %rax     # 0x25fb8
movq (%rax), %rdi
movl -0x2c(%rbp), %r8d
leaq 0xf4e4(%rip), %rsi      # 0x1e505
leaq 0xf43a(%rip), %rdx      # 0x1e462
movl $0xb7, %ecx
movb $0x0, %al
callq 0x5190
jmp 0xf036
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x5180
jmp 0xedd6
movl $0xfffffe0b, -0x28(%rbp) # imm = 0xFFFFFE0B
jmp 0xf07f
jmp 0xf050
jmp 0xf052
jmp 0xf054
movl -0x78(%rbp), %ecx
xorl %eax, %eax
cmpl -0x24(%rbp), %ecx
movb %al, -0x81(%rbp)
je 0xf071
cmpl $0x6, -0x2c(%rbp)
setl %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0xee90
cmpl $0x0, -0x28(%rbp)
jge 0xf08d
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf093
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop