// File: goodix[P]gdix_hid_firmware_update/gtx9/gtx9_i2c_update.cpp
// Function: gdix_send_flash_cmd(goodix_flash_cmd*)
static int gdix_send_flash_cmd(struct goodix_flash_cmd *flash_cmd)
{
	int i, ret, retry;
	struct goodix_flash_cmd tmp_cmd;

	gdix_dbg("try send flash cmd:%*ph\n", (int)sizeof(flash_cmd->buf),
			 flash_cmd->buf);
	memset(tmp_cmd.buf, 0, sizeof(tmp_cmd));
	ret = i2c_write(0x13400, flash_cmd->buf, sizeof(flash_cmd->buf));
	if (ret) {
		gdix_err("failed send flash cmd %d\n", ret);
		return ret;
	}

	retry = 5;
	for (i = 0; i < retry; i++) {
		ret = i2c_read(0x13400, tmp_cmd.buf, sizeof(tmp_cmd.buf));
		if (!ret && tmp_cmd.ack == FLASH_CMD_ACK_CHK_PASS)
			break;
		usleep(5000);
		gdix_dbg("flash cmd ack error retry %d, ack 0x%x, ret %d\n", i,
				 tmp_cmd.ack, ret);
	}
	if (tmp_cmd.ack != FLASH_CMD_ACK_CHK_PASS) {
		gdix_err("flash cmd ack error, ack 0x%x, ret %d\n", tmp_cmd.ack, ret);
		gdix_err("data:%*ph\n", (int)sizeof(tmp_cmd.buf), tmp_cmd.buf);
		return -1;
	}
	gdix_dbg("flash cmd ack check pass\n");

	usleep(80000);
	retry = 20;
	for (i = 0; i < retry; i++) {
		ret = i2c_read(0x13400, tmp_cmd.buf, sizeof(tmp_cmd.buf));
		if (!ret && tmp_cmd.ack == FLASH_CMD_ACK_CHK_PASS &&
			tmp_cmd.status == FLASH_CMD_W_STATUS_WRITE_OK) {
			gdix_dbg("flash status check pass\n");
			return 0;
		}

		gdix_dbg(
			"flash cmd status not ready, retry %d, ack 0x%x, status 0x%x, ret "
			"%d\n",
			i, tmp_cmd.ack, tmp_cmd.status, ret);
		usleep(20000);
	}

	gdix_err("flash cmd status error %d, ack 0x%x, status 0x%x, ret %d\n", i,
			 tmp_cmd.ack, tmp_cmd.status, ret);
	if (ret) {
		gdix_err("reason: bus or paltform error\n");
		return -1;
	}

	switch (tmp_cmd.status) {
	case FLASH_CMD_W_STATUS_CHK_PASS:
		gdix_err("data check pass, but failed get follow-up results\n");
		return -1;
	case FLASH_CMD_W_STATUS_CHK_FAIL:
		gdix_err("data check failed, please retry\n");
		return -1;
	case FLASH_CMD_W_STATUS_ADDR_ERR:
		gdix_err("flash target addr error, please check\n");
		return -1;
	case FLASH_CMD_W_STATUS_WRITE_ERR:
		gdix_err("flash data write err, please retry\n");
		return -1;
	default:
		gdix_err("unknown status\n");
		return -1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
leaq 0xf0a6(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17226
movq 0xedb9(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movq -0x10(%rbp), %r9
leaq 0x8adc(%rip), %rsi      # 0x1fce9
leaq 0x8aff(%rip), %rdx      # 0x1fd13
movl $0x24c, %ecx            # imm = 0x24C
movl $0x10, %r8d
movb $0x0, %al
callq 0x5190
jmp 0x17228
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x50a0
movq -0x10(%rbp), %rsi
movl $0x13400, %edi          # imm = 0x13400
movl $0x10, %edx
callq 0x15870
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x17297
jmp 0x17256
leaq 0xf03c(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1728a
movq 0xed6f(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x18(%rbp), %r8d
leaq 0x8ab0(%rip), %rsi      # 0x1fd27
leaq 0x8a95(%rip), %rdx      # 0x1fd13
movl $0x250, %ecx            # imm = 0x250
movb $0x0, %al
callq 0x5190
jmp 0x1728c
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x176f9
movl $0x5, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x17335
leaq -0x2c(%rbp), %rsi
movl $0x13400, %edi          # imm = 0x13400
movl $0x10, %edx
callq 0x168d0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x172da
movzbl -0x2b(%rbp), %eax
cmpl $0xee, %eax
jne 0x172da
jmp 0x17335
movl $0x1388, %edi           # imm = 0x1388
callq 0x5180
leaq 0xefae(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17323
movq 0xecc1(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movl -0x14(%rbp), %r8d
movzbl -0x2b(%rbp), %r9d
movl -0x18(%rbp), %eax
leaq 0x8a45(%rip), %rsi      # 0x1fd52
leaq 0x89ff(%rip), %rdx      # 0x1fd13
movl $0x25b, %ecx            # imm = 0x25B
movl %eax, (%rsp)
movb $0x0, %al
callq 0x5190
jmp 0x17325
jmp 0x17327
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x172a5
movzbl -0x2b(%rbp), %eax
cmpl $0xee, %eax
je 0x173cb
jmp 0x17346
leaq 0xef4c(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1737f
movq 0xec7f(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movzbl -0x2b(%rbp), %r8d
movl -0x18(%rbp), %r9d
leaq 0x8a27(%rip), %rsi      # 0x1fd93
leaq 0x89a0(%rip), %rdx      # 0x1fd13
movl $0x25e, %ecx            # imm = 0x25E
movb $0x0, %al
callq 0x5190
jmp 0x17381
jmp 0x17383
leaq 0xef0f(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x173bd
movq 0xec42(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq -0x2c(%rbp), %r9
leaq 0x8a27(%rip), %rsi      # 0x1fdcb
leaq 0x8968(%rip), %rdx      # 0x1fd13
movl $0x25f, %ecx            # imm = 0x25F
movl $0x10, %r8d
movb $0x0, %al
callq 0x5190
jmp 0x173bf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176f9
jmp 0x173cd
leaq 0xeec5(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x173fd
movq 0xebd8(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x89fd(%rip), %rsi      # 0x1fde7
leaq 0x8922(%rip), %rdx      # 0x1fd13
movl $0x262, %ecx            # imm = 0x262
movb $0x0, %al
callq 0x5190
jmp 0x173ff
movl $0x13880, %edi          # imm = 0x13880
callq 0x5180
movl $0x14, -0x1c(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x174fa
leaq -0x2c(%rbp), %rsi
movl $0x13400, %edi          # imm = 0x13400
movl $0x10, %edx
callq 0x168d0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x17495
movzbl -0x2b(%rbp), %eax
cmpl $0xee, %eax
jne 0x17495
movzbl -0x2c(%rbp), %eax
cmpl $0xee, %eax
jne 0x17495
jmp 0x17457
leaq 0xee3b(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17487
movq 0xeb4e(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
leaq 0x899e(%rip), %rsi      # 0x1fe12
leaq 0x8898(%rip), %rdx      # 0x1fd13
movl $0x26a, %ecx            # imm = 0x26A
movb $0x0, %al
callq 0x5190
jmp 0x17489
movl $0x0, -0x4(%rbp)
jmp 0x176f9
jmp 0x17497
leaq 0xedfb(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x174e0
movq 0xeb0e(%rip), %rax      # 0x25fb8
movq (%rax), %rdi
movl -0x14(%rbp), %r8d
movzbl -0x2b(%rbp), %r9d
movzbl -0x2c(%rbp), %r10d
movl -0x18(%rbp), %eax
leaq 0x8977(%rip), %rsi      # 0x1fe3c
leaq 0x8847(%rip), %rdx      # 0x1fd13
movl $0x271, %ecx            # imm = 0x271
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x5190
jmp 0x174e2
movl $0x4e20, %edi           # imm = 0x4E20
callq 0x5180
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x17417
jmp 0x174fc
leaq 0xed96(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17545
movq 0xeac9(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
movl -0x14(%rbp), %r8d
movzbl -0x2b(%rbp), %r9d
movzbl -0x2c(%rbp), %r10d
movl -0x18(%rbp), %eax
leaq 0x8968(%rip), %rsi      # 0x1fe92
leaq 0x87e2(%rip), %rdx      # 0x1fd13
movl $0x276, %ecx            # imm = 0x276
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x5190
jmp 0x17547
cmpl $0x0, -0x18(%rbp)
je 0x1758d
jmp 0x1754f
leaq 0xed43(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x1757f
movq 0xea76(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x8971(%rip), %rsi      # 0x1fedd
leaq 0x87a0(%rip), %rdx      # 0x1fd13
movl $0x278, %ecx            # imm = 0x278
movb $0x0, %al
callq 0x5190
jmp 0x17581
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176f9
movzbl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
subl $0x22, %eax
je 0x175c4
jmp 0x1759b
movl -0x30(%rbp), %eax
subl $0x33, %eax
je 0x17604
jmp 0x175a5
movl -0x30(%rbp), %eax
subl $0x44, %eax
je 0x17644
jmp 0x175b3
movl -0x30(%rbp), %eax
subl $0x55, %eax
je 0x17681
jmp 0x176be
jmp 0x175c6
leaq 0xeccc(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x175f6
movq 0xe9ff(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x892a(%rip), %rsi      # 0x1ff0d
leaq 0x8729(%rip), %rdx      # 0x1fd13
movl $0x27e, %ecx            # imm = 0x27E
movb $0x0, %al
callq 0x5190
jmp 0x175f8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176f9
jmp 0x17606
leaq 0xec8c(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17636
movq 0xe9bf(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x892e(%rip), %rsi      # 0x1ff51
leaq 0x86e9(%rip), %rdx      # 0x1fd13
movl $0x281, %ecx            # imm = 0x281
movb $0x0, %al
callq 0x5190
jmp 0x17638
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176f9
jmp 0x17646
leaq 0xec4c(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x17676
movq 0xe97f(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x8920(%rip), %rsi      # 0x1ff83
leaq 0x86a9(%rip), %rdx      # 0x1fd13
movl $0x284, %ecx            # imm = 0x284
movb $0x0, %al
callq 0x5190
jmp 0x17678
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176f9
jmp 0x17683
leaq 0xec0f(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x176b3
movq 0xe942(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x891b(%rip), %rsi      # 0x1ffbb
leaq 0x866c(%rip), %rdx      # 0x1fd13
movl $0x287, %ecx            # imm = 0x287
movb $0x0, %al
callq 0x5190
jmp 0x176b5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x176f9
jmp 0x176c0
leaq 0xebd2(%rip), %rax      # 0x26299
testb $0x1, (%rax)
je 0x176f0
movq 0xe905(%rip), %rax      # 0x25fd8
movq (%rax), %rdi
leaq 0x8913(%rip), %rsi      # 0x1fff0
leaq 0x862f(%rip), %rdx      # 0x1fd13
movl $0x28a, %ecx            # imm = 0x28A
movb $0x0, %al
callq 0x5190
jmp 0x176f2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)