// File: /workspace/github_c_makefile_star2_O0/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: hci_download_patch
static int hci_download_patch(int dd, int index, uint8_t * data, int len,
			      struct termios *ti)
{
	unsigned char hcipatch[256] = { 0x20, 0xfc, 00 };
	unsigned char bytes[READ_DATA_SIZE];
	int retlen;
	struct sigaction sa;

	sa.sa_handler = h5_tpatch_sig_alarm;
	sigaction(SIGALRM, &sa, NULL);
	alarm(2);

	download_vendor_patch_cp cp;
	memset(&cp, 0, sizeof(cp));
	cp.index = index;
	if (data != NULL) {
		memcpy(cp.data, data, len);
	}

	if (index & 0x80)
		rtk_hw_cfg.tx_index = index & 0x7f;
	else
		rtk_hw_cfg.tx_index = index;

	hcipatch[2] = len + 1;
	memcpy(hcipatch + 3, &cp, len + 1);

	struct sk_buff *nskb = h5_prepare_pkt(&rtk_hw_cfg, hcipatch, len + 4, HCI_COMMAND_PKT);	//data:len+head:4

	if (rtk_hw_cfg.host_last_cmd) {
		skb_free(rtk_hw_cfg.host_last_cmd);
		rtk_hw_cfg.host_last_cmd = NULL;
	}

	rtk_hw_cfg.host_last_cmd = nskb;

	len = write(dd, nskb->data, nskb->data_len);
	RS_DBG("hci_download_patch tx_index:%d rx_index: %d\n",
	       rtk_hw_cfg.tx_index, rtk_hw_cfg.rx_index);

	while (rtk_hw_cfg.rx_index != rtk_hw_cfg.tx_index) {	//receive data and wait last pkt
		if ((retlen = read_check_rtk(dd, &bytes, READ_DATA_SIZE)) == -1) {
			RS_ERR("read fail\n");
			return -1;
		}
		h5_recv(&rtk_hw_cfg, &bytes, retlen);
	}

	alarm(0);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x130(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x20d0
movb $0x20, -0x130(%rbp)
movb $-0x4, -0x12f(%rbp)
leaq 0x1ec(%rip), %rax       # 0xa6b0
movq %rax, -0x1e0(%rbp)
movl $0xe, %edi
leaq -0x1e0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x2060
movl $0x2, %edi
callq 0x20f0
leaq -0x2dd(%rbp), %rdi
xorl %esi, %esi
movl $0xfd, %edx
callq 0x20d0
movl -0xc(%rbp), %eax
movb %al, -0x2dd(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xa525
leaq -0x2dd(%rbp), %rdi
addq $0x1, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x2180
movl -0xc(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xa540
movl -0xc(%rbp), %eax
andl $0x7f, %eax
movl %eax, 0x61e6(%rip)      # 0x10724
jmp 0xa549
movl -0xc(%rbp), %eax
movl %eax, 0x61db(%rip)      # 0x10724
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movb %al, -0x12e(%rbp)
leaq -0x130(%rbp), %rdi
addq $0x3, %rdi
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
leaq -0x2dd(%rbp), %rsi
callq 0x2180
leaq -0x130(%rbp), %rsi
movl -0x1c(%rbp), %edx
addl $0x4, %edx
leaq 0x6147(%rip), %rdi      # 0x106d0
movl $0x1, %ecx
callq 0x5600
movq %rax, -0x2e8(%rbp)
cmpq $0x0, 0x614e(%rip)      # 0x106f0
je 0xa5bb
movq 0x6145(%rip), %rdi      # 0x106f0
callq 0x66c0
movq $0x0, 0x6135(%rip) # 0x106f0
movq -0x2e8(%rbp), %rax
movq %rax, 0x6127(%rip)      # 0x106f0
movl -0x8(%rbp), %edi
movq -0x2e8(%rbp), %rsi
addq $0x8, %rsi
movq -0x2e8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
callq 0x2070
movl %eax, -0x1c(%rbp)
cmpb $0x0, 0x5cde(%rip)      # 0x102d0
je 0xa620
movq 0x59fd(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movl 0x6120(%rip), %ecx      # 0x10724
movl 0x611d(%rip), %r8d      # 0x10728
leaq 0x2bae(%rip), %rsi      # 0xd1c0
leaq 0xf70(%rip), %rdx       # 0xb589
movb $0x0, %al
callq 0x2150
jmp 0xa622
jmp 0xa624
movl 0x60fe(%rip), %eax      # 0x10728
cmpl 0x60f4(%rip), %eax      # 0x10724
je 0xa696
movl -0x8(%rbp), %edi
leaq -0x140(%rbp), %rsi
movl $0x10, %edx
callq 0x5850
movl %eax, -0x144(%rbp)
cmpl $-0x1, %eax
jne 0xa67b
jmp 0xa653
movq 0x599e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x2b8e(%rip), %rsi      # 0xd1f2
leaq 0xf1e(%rip), %rdx       # 0xb589
movb $0x0, %al
callq 0x2150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa6a4
movl -0x144(%rbp), %edx
leaq 0x6048(%rip), %rdi      # 0x106d0
leaq -0x140(%rbp), %rsi
callq 0x58f0
jmp 0xa624
xorl %edi, %edi
callq 0x20f0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq