// File: /workspace/github_c_makefile_star2_O2/seuscq[P]rtk_hciattach/hciattach_rtk.c
// Function: h5_recv
static int h5_recv(rtk_hw_cfg_t * h5, void *data, int count)
{
	unsigned char *ptr;
	//RS_DBG("count %d rx_state %d rx_count %ld", count, h5->rx_state, h5->rx_count);
	ptr = (unsigned char *)data;

	while (count) {
		if (h5->rx_count) {
			if (*ptr == 0xc0) {
				RS_ERR("short h5 packet");
				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_START;
				h5->rx_count = 0;
			} else
				h5_unslip_one_byte(h5, *ptr);

			ptr++;
			count--;
			continue;
		}

		switch (h5->rx_state) {
		case H5_W4_HDR:
			/* check header checksum. see Core Spec V4 "3-wire uart" page 67 */
			if ((0xff & (RT_U8) ~
			     (h5->rx_skb->data[0] + h5->rx_skb->data[1] +
			      h5->rx_skb->data[2])) != h5->rx_skb->data[3]) {
				RS_ERR("h5 hdr checksum error!!!");
				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_DELIMITER;
				h5->rx_count = 0;
				continue;
			}

			/* reliable pkt  & h5->hdr->SeqNumber != h5->Rxseq_txack */
			if (h5->rx_skb->data[0] & 0x80
			    && (h5->rx_skb->data[0] & 0x07) !=
			    h5->rxseq_txack) {
				RS_ERR
				    ("Out-of-order packet arrived, got(%u)expected(%u)",
				     h5->rx_skb->data[0] & 0x07,
				     h5->rxseq_txack);
				h5->is_txack_req = 1;

				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_DELIMITER;
				h5->rx_count = 0;

				/* depend on weather remote will reset ack numb or not!!!!!!special */
				if (rtk_hw_cfg.tx_index == rtk_hw_cfg.total_num) {
					rtk_hw_cfg.rxseq_txack =
					    h5->rx_skb->data[0] & 0x07;
				}
				continue;
			}
			h5->rx_state = H5_W4_DATA;
			h5->rx_count =
			    (h5->rx_skb->data[1] >> 4) +
			    (h5->rx_skb->data[2] << 4);
			continue;

		case H5_W4_DATA:
			/* pkt with crc */
			if (h5->rx_skb->data[0] & 0x40) {
				h5->rx_state = H5_W4_CRC;
				h5->rx_count = 2;
			} else {
				h5_complete_rx_pkt(h5);
				//RS_DBG(DF_SLIP,("--------> H5_W4_DATA ACK\n"));
			}
			continue;

		case H5_W4_CRC:
			if (bit_rev16(h5->message_crc) != h5_get_crc(h5)) {
				RS_ERR
				    ("Checksum failed, computed(%04x)received(%04x)",
				     bit_rev16(h5->message_crc),
				     h5_get_crc(h5));
				skb_free(h5->rx_skb);
				h5->rx_state = H5_W4_PKT_DELIMITER;
				h5->rx_count = 0;
				continue;
			}
			skb_trim(h5->rx_skb, h5->rx_skb->data_len - 2);
			h5_complete_rx_pkt(h5);
			continue;

		case H5_W4_PKT_DELIMITER:
			switch (*ptr) {
			case 0xc0:
				h5->rx_state = H5_W4_PKT_START;
				break;

			default:
				break;
			}
			ptr++;
			count--;
			break;

		case H5_W4_PKT_START:
			switch (*ptr) {
			case 0xc0:
				ptr++;
				count--;
				break;

			default:
				h5->rx_state = H5_W4_HDR;
				h5->rx_count = 4;
				h5->rx_esc_state = H5_ESCSTATE_NOESC;
				H5_CRC_INIT(h5->message_crc);

				// Do not increment ptr or decrement count
				// Allocate packet. Max len of a H5 pkt=
				// 0xFFF (payload) +4 (header) +2 (crc)
				h5->rx_skb = skb_alloc(0x1005);
				if (!h5->rx_skb) {
					h5->rx_state = H5_W4_PKT_DELIMITER;
					h5->rx_count = 0;
					return 0;
				}
				break;
			}
			break;

		default:
			break;
		}
	}
	return count;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r14
leaq 0x3a29(%rip), %r13      # 0x83d0
leaq 0x3a12(%rip), %r15      # 0x83c0
testl %ebx, %ebx
je 0x4e07
cmpl $0x0, 0x7d2b(%rip)      # 0xc6e8
je 0x49fc
movzbl (%r14), %r12d
cmpl $0xc0, %r12d
je 0x4c81
movl 0x7d1a(%rip), %eax      # 0xc6f0
cmpl $0x1, %eax
je 0x4a2b
testl %eax, %eax
jne 0x4c77
cmpb $-0x25, %r12b
jne 0x4a86
movl $0x1, 0x7cf9(%rip)      # 0xc6f0
jmp 0x4c77
movl 0x7cea(%rip), %eax      # 0xc6ec
cmpq $0x4, %rax
ja 0x49ae
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
cmpb $-0x40, (%r14)
jne 0x4c77
movl $0x1, 0x7cc6(%rip)      # 0xc6ec
jmp 0x4c77
leal -0xdc(%r12), %eax
cmpl $0x3, %eax
ja 0x4d59
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movq 0x7cac(%rip), %rdi      # 0xc6f8
pushq $0x1
popq %rsi
callq 0x723f
movb $-0x40, (%rax)
movq 0x7c9a(%rip), %rax      # 0xc6f8
testb $0x40, 0x8(%rax)
je 0x4c6a
cmpl $0x4, 0x7c7d(%rip)      # 0xc6ec
je 0x4c6a
leaq 0x7c6a(%rip), %rdi      # 0xc6e6
movl $0xc0, %esi
jmp 0x4c65
movq 0x7c6b(%rip), %rdi      # 0xc6f8
pushq $0x1
popq %rsi
callq 0x723f
movb %r12b, (%rax)
movq 0x7c59(%rip), %rax      # 0xc6f8
testb $0x40, 0x8(%rax)
je 0x4c71
cmpl $0x4, 0x7c3c(%rip)      # 0xc6ec
je 0x4c71
movzbl %r12b, %esi
leaq 0x7c25(%rip), %rdi      # 0xc6e6
callq 0x71eb
jmp 0x4c71
movzwl 0x7c14(%rip), %edi      # 0xc6e6
callq 0x721e
movq 0x7c1a(%rip), %rdx      # 0xc6f8
movl 0x4(%rdx), %ecx
movzwl 0x6(%rdx,%rcx), %esi
rolw $0x8, %si
cmpw %si, %ax
jne 0x4cd5
leal -0x2(%rcx), %r8d
cmpl $0x2, %ecx
jb 0x4d35
movl %r8d, 0x4(%rdx)
jmp 0x4b73
movq 0x7beb(%rip), %rax      # 0xc6f8
movzbl 0x8(%rax), %ecx
movb 0x9(%rax), %dl
addb %cl, %dl
addb 0xa(%rax), %dl
xorb 0xb(%rax), %dl
cmpb $-0x1, %dl
jne 0x4cfd
testb %cl, %cl
jns 0x4b3d
andl $0x7, %ecx
movzbl 0x7bac(%rip), %r8d      # 0xc6e0
cmpb %r8b, %cl
jne 0x4d9c
movl $0x3, 0x7ba5(%rip)      # 0xc6ec
movzbl 0x9(%rax), %ecx
shrl $0x4, %ecx
movzbl 0xa(%rax), %eax
shll $0x4, %eax
orl %ecx, %eax
movl %eax, 0x7b8b(%rip)      # 0xc6e8
jmp 0x49ae
movq 0x7b8f(%rip), %rax      # 0xc6f8
testb $0x40, 0x8(%rax)
jne 0x4cbf
callq 0x7286
jmp 0x49ae
cmpb $-0x40, (%r14)
je 0x4c77
movabsq $0x200000004, %rax      # imm = 0x200000004
movq %rax, 0x7b50(%rip)      # 0xc6e8
andl $0x0, 0x7b51(%rip)      # 0xc6f0
orw $-0x1, 0x7b3f(%rip)     # 0xc6e6
movl $0x1005, %edi           # imm = 0x1005
callq 0x710f
movq %rax, 0x7b40(%rip)      # 0xc6f8
testq %rax, %rax
jne 0x49ae
jmp 0x4dff
movq 0x7b2b(%rip), %rdi      # 0xc6f8
pushq $0x1
popq %rsi
callq 0x723f
movb $0x11, (%rax)
movq 0x7b19(%rip), %rax      # 0xc6f8
testb $0x40, 0x8(%rax)
je 0x4c6a
cmpl $0x4, 0x7afc(%rip)      # 0xc6ec
je 0x4c6a
leaq 0x7aed(%rip), %rdi      # 0xc6e6
pushq $0x11
jmp 0x4c2e
movq 0x7af4(%rip), %rdi      # 0xc6f8
pushq $0x1
popq %rsi
callq 0x723f
movb $0x13, (%rax)
movq 0x7ae2(%rip), %rax      # 0xc6f8
testb $0x40, 0x8(%rax)
je 0x4c6a
cmpl $0x4, 0x7ac9(%rip)      # 0xc6ec
je 0x4c6a
leaq 0x7aba(%rip), %rdi      # 0xc6e6
pushq $0x13
popq %rsi
jmp 0x4c65
movq 0x7ac0(%rip), %rdi      # 0xc6f8
pushq $0x1
popq %rsi
callq 0x723f
movb $-0x25, (%rax)
movq 0x7aae(%rip), %rax      # 0xc6f8
testb $0x40, 0x8(%rax)
je 0x4c6a
cmpl $0x4, 0x7a95(%rip)      # 0xc6ec
je 0x4c6a
leaq 0x7a86(%rip), %rdi      # 0xc6e6
movl $0xdb, %esi
callq 0x71eb
andl $0x0, 0x7a7f(%rip)      # 0xc6f0
decl 0x7a71(%rip)            # 0xc6e8
incq %r14
decl %ebx
jmp 0x49ae
movq 0x7370(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x47bc(%rip), %rsi      # 0x944e
leaq 0x38a0(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movq 0x7a51(%rip), %rdi      # 0xc6f8
callq 0x2030
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x7a2b(%rip)      # 0xc6e8
jmp 0x4c77
movabsq $0x400000002, %rax      # imm = 0x400000002
movq %rax, 0x7a18(%rip)      # 0xc6e8
jmp 0x49ae
movzwl %si, %r8d
movq 0x7318(%rip), %rcx      # 0xbff8
movq (%rcx), %rdi
movzwl %ax, %ecx
leaq 0x47dc(%rip), %rsi      # 0x94c9
leaq 0x3845(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x4d1c
movq 0x72f4(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x475b(%rip), %rsi      # 0x9469
leaq 0x3824(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movq 0x79d5(%rip), %rdi      # 0xc6f8
callq 0x2030
andq $0x0, 0x79b8(%rip)      # 0xc6e8
jmp 0x49ae
movq 0x72bc(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4b48(%rip), %rsi      # 0x988e
leaq 0x37ec(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
jmp 0x4b73
movq 0x7298(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4798(%rip), %rsi      # 0x9502
leaq 0x37c8(%rip), %rdx      # 0x8539
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2150
movq 0x7976(%rip), %rdi      # 0xc6f8
callq 0x2030
andq $0x0, 0x7969(%rip)      # 0xc6f8
andq $0x0, 0x7951(%rip)      # 0xc6e8
jmp 0x4c77
movq 0x7255(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x46e0(%rip), %rsi      # 0x948d
leaq 0x3785(%rip), %rdx      # 0x8539
xorl %eax, %eax
callq 0x2150
movb $0x1, 0x7921(%rip)      # 0xc6e3
movq 0x792f(%rip), %rdi      # 0xc6f8
callq 0x2030
andq $0x0, 0x7912(%rip)      # 0xc6e8
movl 0x7958(%rip), %eax      # 0xc734
cmpl 0x794e(%rip), %eax      # 0xc730
jne 0x49ae
movq 0x7909(%rip), %rax      # 0xc6f8
movb 0x8(%rax), %al
andb $0x7, %al
movb %al, 0x78e6(%rip)       # 0xc6e0
jmp 0x49ae
andq $0x0, 0x78e1(%rip)      # 0xc6e8
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq