// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
testl %esi, %esi
je 0x6761
movl %esi, %ebx
movq %rdi, %r14
leaq 0x30f6(%rip), %r13      # 0x93d0
movabsq $0x200000004, %r12      # imm = 0x200000004
movabsq $0x400000002, %r15      # imm = 0x400000002
cmpl $0x0, 0x93f3(%rip)      # 0xf6e8
je 0x6330
movzbl (%r14), %eax
cmpl $0xc0, %eax
je 0x66d9
movl 0x93e4(%rip), %ecx      # 0xf6f0
cmpl $0x1, %ecx
je 0x6363
testl %ecx, %ecx
jne 0x66c7
cmpb $-0x25, %al
jne 0x63ea
movl $0x1, 0x93c5(%rip)      # 0xf6f0
jmp 0x66c7
movl 0x93b6(%rip), %eax      # 0xf6ec
cmpq $0x4, %rax
ja 0x66cc
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
cmpb $-0x40, (%r14)
jne 0x66c7
movl $0x1, 0x938e(%rip)      # 0xf6ec
jmp 0x66c7
movl %eax, %ecx
addl $0xffffff24, %ecx       # imm = 0xFFFFFF24
cmpl $0x3, %ecx
ja 0x671b
leaq 0x3045(%rip), %rdx      # 0x93c0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq 0x936d(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x6784
movl %edx, 0x4(%rax)
movb $-0x40, 0x8(%rax,%rcx)
movq 0x9350(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x66b7
cmpl $0x4, 0x9333(%rip)      # 0xf6ec
je 0x66b7
movzwl 0x9320(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
leaq 0x4dcb(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rax,2), %cx
movzwl %cx, %eax
shrl $0x4, %eax
andl $0xf, %ecx
xorl $0xc, %ecx
jmp 0x66ac
movq 0x9307(%rip), %rcx      # 0xf6f8
movl 0x4(%rcx), %edx
leal 0x1(%rdx), %esi
cmpl (%rcx), %esi
ja 0x6770
movl %esi, 0x4(%rcx)
movb %al, 0x8(%rcx,%rdx)
movq 0x92eb(%rip), %rcx      # 0xf6f8
testb $0x40, 0x8(%rcx)
je 0x66c1
cmpl $0x4, 0x92ce(%rip)      # 0xf6ec
je 0x66c1
movzwl 0x92bb(%rip), %ecx      # 0xf6e6
movl %ecx, %edx
shrl $0x4, %edx
xorl %eax, %ecx
andl $0xf, %ecx
leaq 0x4d64(%rip), %rsi      # 0xb1a0
xorw (%rsi,%rcx,2), %dx
movzwl %dx, %ecx
shrl $0x4, %ecx
shrl $0x4, %eax
andl $0xf, %edx
xorl %eax, %edx
xorw (%rsi,%rdx,2), %cx
movw %cx, 0x928d(%rip)       # 0xf6e6
jmp 0x66c1
movzwl 0x9281(%rip), %eax      # 0xf6e6
movzbl %al, %ecx
leaq 0x2f91(%rip), %rdx      # 0x9400
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
shrl $0x8, %eax
movzbl (%rax,%rdx), %edi
orl %ecx, %edi
movq 0x9272(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
movzwl 0x6(%rax,%rcx), %esi
rolw $0x8, %si
cmpw %si, %di
jne 0x66e9
leal -0x2(%rcx), %r8d
cmpl $0x2, %ecx
jb 0x66f7
movl %r8d, 0x4(%rax)
jmp 0x6517
movq 0x9243(%rip), %rax      # 0xf6f8
movb 0x8(%rax), %cl
movb 0x9(%rax), %dl
addb %cl, %dl
addb 0xa(%rax), %dl
xorb 0xb(%rax), %dl
cmpb $-0x1, %dl
jne 0x66f0
testb %cl, %cl
jns 0x64e1
andb $0x7, %cl
movb 0x9207(%rip), %dl       # 0xf6e0
cmpb %dl, %cl
jne 0x6732
movl $0x3, 0x9201(%rip)      # 0xf6ec
movzbl 0x9(%rax), %ecx
shrl $0x4, %ecx
movzbl 0xa(%rax), %eax
shll $0x4, %eax
orl %ecx, %eax
movl %eax, 0x91e7(%rip)      # 0xf6e8
jmp 0x66cc
movq 0x91eb(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
jne 0x66e0
callq 0x81dd
jmp 0x66cc
cmpb $-0x40, (%r14)
je 0x66c7
movq %r15, %r13
movq %r12, %r15
movq %r12, 0x91b0(%rip)      # 0xf6e8
movl $0x0, 0x91ae(%rip)      # 0xf6f0
movw $0xffff, 0x919b(%rip)   # imm = 0xFFFF
movl $0x100d, %edi           # imm = 0x100D
callq 0x2190
testq %rax, %rax
je 0x6725
movq %rax, %r12
movq $0x1005, (%rax)         # imm = 0x1005
leaq 0x8(%r12), %rdi
movl $0x1005, %edx           # imm = 0x1005
xorl %esi, %esi
callq 0x20d0
movq %r12, 0x9178(%rip)      # 0xf6f8
testq %r12, %r12
movq %r15, %r12
movq %r13, %r15
leaq 0x2e40(%rip), %r13      # 0x93d0
jne 0x66cc
jmp 0x6756
movq 0x9156(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x677a
movl %edx, 0x4(%rax)
movb $0x11, 0x8(%rax,%rcx)
movq 0x9139(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x66b7
cmpl $0x4, 0x911c(%rip)      # 0xf6ec
je 0x66b7
movzwl 0x9109(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x1, %eax
jmp 0x6637
movq 0x9107(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x6775
movl %edx, 0x4(%rax)
movb $0x13, 0x8(%rax,%rcx)
movq 0x90ea(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x66b7
cmpl $0x4, 0x90cd(%rip)      # 0xf6ec
je 0x66b7
movzwl 0x90ba(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0x3, %eax
leaq 0x4b62(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rax,2), %cx
movzwl %cx, %eax
shrl $0x4, %eax
andl $0xf, %ecx
xorl $0x1, %ecx
jmp 0x66ac
movq 0x90a1(%rip), %rax      # 0xf6f8
movl 0x4(%rax), %ecx
leal 0x1(%rcx), %edx
cmpl (%rax), %edx
ja 0x677f
movl %edx, 0x4(%rax)
movb $-0x25, 0x8(%rax,%rcx)
movq 0x9084(%rip), %rax      # 0xf6f8
testb $0x40, 0x8(%rax)
je 0x66b7
cmpl $0x4, 0x906b(%rip)      # 0xf6ec
je 0x66b7
movzwl 0x905c(%rip), %eax      # 0xf6e6
movl %eax, %ecx
shrl $0x4, %ecx
andl $0xf, %eax
xorl $0xb, %eax
leaq 0x4b04(%rip), %rdx      # 0xb1a0
xorw (%rdx,%rax,2), %cx
movzwl %cx, %eax
shrl $0x4, %eax
andl $0xf, %ecx
xorl $0xd, %ecx
xorw (%rdx,%rcx,2), %ax
movw %ax, 0x902f(%rip)       # 0xf6e6
movl $0x0, 0x902f(%rip)      # 0xf6f0
decl 0x9021(%rip)            # 0xf6e8
incq %r14
decl %ebx
testl %ebx, %ebx
jne 0x62ee
jmp 0x6761
callq 0x2eaa
jmp 0x66c7
movq %r15, 0x9001(%rip)      # 0xf6e8
jmp 0x66cc
callq 0x2f10
jmp 0x66cc
callq 0x2f53
jmp 0x66cc
movq 0x88fa(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x4186(%rip), %rsi      # 0xa88e
leaq 0x2e2a(%rip), %rdx      # 0x9539
xorl %eax, %eax
callq 0x2150
jmp 0x6517
movzbl %al, %edi
callq 0x2e42
jmp 0x66c7
callq 0x2eec
xorl %r12d, %r12d
jmp 0x6568
movzbl %dl, %edi
movzbl %cl, %esi
movl %ebx, %edx
movq %r14, %rcx
leaq -0x2c(%rbp), %r8
leaq -0x38(%rbp), %r9
callq 0x2f8c
movl -0x2c(%rbp), %ebx
movq -0x38(%rbp), %r14
jmp 0x66cc
movq $0x0, 0x8f87(%rip) # 0xf6e8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2e87
callq 0x2db6
callq 0x2dd9
callq 0x2dfc
callq 0x2e1f