// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_get_desc(WdtDevice*, unsigned char, unsigned char, unsigned char*, unsigned int)
int wh_i2c_get_desc(WDT_DEV *pdev, BYTE desc_type, BYTE string_idx, BYTE* target_buf, UINT32 buf_size)
{
	int	retval;
	int	ret_size = 0;
	UINT16  cmd_reg = pdev->board_info.dev_hid_desc.wCommandRegister;
        UINT16  data_reg = pdev->board_info.dev_hid_desc.wDataRegister;
        char    str_txdata[10] = { (char)cmd_reg, (char)(cmd_reg>>8), 0x13, 0x0E, 0x00, (char)data_reg, (char)(data_reg>>8), 0x00, 0x00, 0x00 };
        char    desc_txdata[10] = { (char)cmd_reg, (char)(cmd_reg>>8), 0x10, 0x0E, (char)data_reg, (char)(data_reg>>8), 0x00, 0x00, 0x00, 0x00 };
	
	BYTE*	txbuf;
	int 	txlen, rxlen;
	BYTE	xfer_buffer[80] ={0}; 

	if (buf_size > 64)
		buf_size = 64;

	if (!target_buf || !pdev)	{
		wh_printf("%s : pointer is null\n", __FUNCTION__);
		return 0;
	}

	if (desc_type == GD_STRING)	{
		str_txdata[4] = string_idx;
		txlen = 7;

		txbuf = (BYTE*) str_txdata;
		rxlen = buf_size + 2;
	} else {
		desc_txdata[2] = 0x10 | (desc_type & 0xF);
		txlen = 6;
		txbuf = (BYTE*) desc_txdata;
		rxlen = buf_size;
	}

	if (pdev->pparam->options & EXEC_I2C_REDUNDANT) 
		txlen += 2;

	retval = wh_i2c_xfer(pdev, pdev->board_info.i2c_address, (BYTE*) txbuf, txlen, (BYTE*) xfer_buffer, rxlen);

	if (!retval)
		return 0;
	
	if (desc_type == GD_STRING)	{
		if (xfer_buffer[1] != 0x03) {
			printf("packet error on string id: %d\n", xfer_buffer[1]);
			return 0;
		}
	
		UINT32 	xfer_length = xfer_buffer[0] - 2;

		if (buf_size < xfer_length)
			xfer_length = buf_size;

		memcpy(target_buf, &xfer_buffer[2], xfer_length);
		ret_size = xfer_length;
	} else {
		if (xfer_buffer[0] == buf_size) {
			memcpy(target_buf, &xfer_buffer[0], buf_size);
			ret_size = buf_size;
		} else	{
			printf("packet error on desc: %x %x %x %x\n", xfer_buffer[0],
				xfer_buffer[1], xfer_buffer[2], xfer_buffer[3]);
		}
	}
	
	return ret_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movzwl 0x1e7(%rdi), %ecx
movzwl 0x1e9(%rdi), %eax
movb %cl, 0xc(%rsp)
movb %cl, 0x16(%rsp)
shrl $0x8, %ecx
movb %cl, 0xd(%rsp)
movw $0xe13, 0xe(%rsp)       # imm = 0xE13
xorl %r14d, %r14d
movb %r14b, 0x10(%rsp)
movb %al, 0x11(%rsp)
movb %cl, 0x17(%rsp)
movw $0xe10, 0x18(%rsp)      # imm = 0xE10
movb %al, 0x1a(%rsp)
shrl $0x8, %eax
movb %al, 0x12(%rsp)
andw $0x0, 0x13(%rsp)
movb %al, 0x1b(%rsp)
andl $0x0, 0x1c(%rsp)
movb %r14b, 0x15(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpl $0x40, %r8d
pushq $0x40
popq %r15
cmovbl %r8d, %r15d
testq %rbx, %rbx
setne %al
testq %rdi, %rdi
setne %cl
testb %al, %cl
je 0x8c32
movl %esi, %ebp
cmpb $0x3, %bpl
jne 0x8c4c
leaq 0xc(%rsp), %rax
movb %dl, 0x4(%rax)
leal 0x2(%r15), %r9d
pushq $0x7
popq %rdx
jmp 0x8c62
leaq 0x2d85(%rip), %rdi      # 0xb9be
leaq 0x2dfc(%rip), %rsi      # 0xba3c
xorl %eax, %eax
callq 0x24bf
jmp 0x8d15
movl %ebp, %ecx
andb $0xf, %cl
orb $0x10, %cl
leaq 0x16(%rsp), %rax
movb %cl, 0x2(%rax)
pushq $0x6
popq %rdx
movl %r15d, %r9d
movq 0x280(%rdi), %rcx
movl 0x8(%rcx), %ecx
shrl $0x8, %ecx
andl $0x2, %ecx
addl %edx, %ecx
movzbl 0x1d6(%rdi), %esi
leaq 0x20(%rsp), %r8
movq %rax, %rdx
callq 0x84cb
testl %eax, %eax
je 0x8d15
cmpb $0x3, %bpl
jne 0x8cc2
movzbl 0x21(%rsp), %esi
cmpl $0x3, %esi
jne 0x8ce1
leaq 0x22(%rsp), %rsi
movzbl -0x2(%rsi), %r14d
addl $-0x2, %r14d
cmpl %r14d, %r15d
cmovbl %r15d, %r14d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2150
jmp 0x8d15
movzbl 0x20(%rsp), %esi
cmpl %esi, %r15d
jne 0x8cf4
movl %r15d, %edx
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x2150
movl %r15d, %r14d
jmp 0x8d15
leaq 0x2d64(%rip), %rdi      # 0xba4c
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2050
jmp 0x8d15
movzbl 0x21(%rsp), %edx
movzbl 0x22(%rsp), %ecx
movzbl 0x23(%rsp), %r8d
leaq 0x2d60(%rip), %rdi      # 0xba6b
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2050
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq