// File: /workspace/github_cpp_makefile_star2_O2/goodix[P]gdix_hid_firmware_update/gtx9/gtx9.cpp
// Function: GTx9Device::WriteSpeCmd(unsigned char const*, unsigned int)
int GTx9Device::WriteSpeCmd(const unsigned char *buf, unsigned int len)
{
	unsigned char cmdBuf[16] = {0};
	uint16_t checksum = 0;
	uint32_t i;
	int ret;

	if (len > 11) {
		gdix_err("Cmd data len is invalid len=%d\n", len);
		return -EINVAL;
	}

	cmdBuf[0] = 0;
	cmdBuf[1] = 0;
	cmdBuf[2] = len + 3;
	memcpy(&cmdBuf[3], buf, len);
	for (i = 2; i < len + 3; i++)
		checksum += cmdBuf[i];

	cmdBuf[len + 3] = checksum & 0xFF;
	cmdBuf[len + 4] = (checksum >> 8) & 0XFF;
	ret = Write(0x10174, cmdBuf, len + 5);
	if (ret < 0) {
		gdix_err("Failed write cmd\n");
		return ret;
	}
	for (i = 0; i < 20; i++) {
		ret = Read(0x10174, cmdBuf, 2);
		if (ret < 0) {
			gdix_err("Failed read cmd status\n");
			return ret;
		}
		if (cmdBuf[1] == 0x80 && cmdBuf[0] == 0x80) {
			usleep(5000);
			return 0;
		}
		usleep(15000);
	}

	gdix_err("Failed get valid cmd ack:0x%02x sta:0x%02x\n", cmdBuf[1],
			 cmdBuf[0]);
	return -EINVAL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
cmpl $0xc, %edx
jb 0xf2d3
leaq 0x10ff6(%rip), %rax     # 0x20291
pushq $-0x16
popq %rbx
cmpb $0x1, (%rax)
jne 0xf421
movq 0x10d2a(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x883a(%rip), %rsi      # 0x17af2
leaq 0x82e3(%rip), %rdx      # 0x175a2
movl $0xcb, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x5180
jmp 0xf421
movq %rdi, %r14
movl %ebp, %ebx
leaq 0x3(%rbx), %r15
leaq 0x3(%rsp), %rdi
movb %r15b, -0x1(%rdi)
movq %rbx, %rdx
callq 0x5090
pushq $0x2
popq %rcx
xorl %eax, %eax
cmpq %rcx, %r15
je 0xf302
movzbl (%rsp,%rcx), %edx
addl %edx, %eax
incq %rcx
jmp 0xf2f2
movb %al, (%rsp,%r15)
movq %rsp, %rdx
movb %ah, 0x4(%rbx,%rdx)
addl $0x5, %ebp
movq (%r14), %rax
movq %r14, %rdi
movl $0x10174, %esi          # imm = 0x10174
movl %ebp, %ecx
callq *0x30(%rax)
testl %eax, %eax
js 0xf370
pushq $0x14
popq %r12
movq %rsp, %r15
pushq $0x2
popq %rbp
xorl %r13d, %r13d
subl $0x1, %r12d
jb 0xf3a1
movq (%r14), %rax
movq %r14, %rdi
movl $0x10174, %esi          # imm = 0x10174
movq %r15, %rdx
movl %ebp, %ecx
callq *0x20(%rax)
testl %eax, %eax
js 0xf3e1
cmpb 0x1(%rsp), %r13b
jno 0xf364
movb (%rsp), %al
negb %al
jo 0xf415
movl $0x3a98, %edi           # imm = 0x3A98
callq 0x5170
jmp 0xf331
movl %eax, %ebx
leaq 0x10f18(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xf421
movq 0x10c4f(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x8790(%rip), %rsi      # 0x17b23
leaq 0x8208(%rip), %rdx      # 0x175a2
movl $0xda, %ecx
jmp 0xf40c
leaq 0x10ee9(%rip), %rax     # 0x20291
pushq $-0x16
popq %rbx
cmpb $0x1, (%rax)
jne 0xf421
movq 0x10c21(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
movzbl 0x1(%rsp), %r8d
movzbl (%rsp), %r9d
leaq 0x87a3(%rip), %rsi      # 0x17b6f
leaq 0x81cf(%rip), %rdx      # 0x175a2
movl $0xeb, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xf421
movl %eax, %ebx
leaq 0x10ea7(%rip), %rax     # 0x20291
cmpb $0x1, (%rax)
jne 0xf421
movq 0x10be2(%rip), %rax     # 0x1ffd8
movq (%rax), %rdi
leaq 0x8746(%rip), %rsi      # 0x17b46
leaq 0x819b(%rip), %rdx      # 0x175a2
movl $0xe0, %ecx
xorl %eax, %eax
callq 0x5180
jmp 0xf421
movl $0x1388, %edi           # imm = 0x1388
callq 0x5170
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq