// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movb %dl, %al
movb %sil, %dl
movq %rdi, -0x10(%rbp)
movb %dl, -0x11(%rbp)
movb %al, -0x12(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movw 0x1e7(%rax), %ax
movw %ax, -0x2e(%rbp)
movq -0x10(%rbp), %rax
movw 0x1e9(%rax), %ax
movw %ax, -0x30(%rbp)
movw -0x2e(%rbp), %ax
movb %al, -0x3a(%rbp)
movzwl -0x2e(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x39(%rbp)
movb $0x13, -0x38(%rbp)
movb $0xe, -0x37(%rbp)
movb $0x0, -0x36(%rbp)
movw -0x30(%rbp), %ax
movb %al, -0x35(%rbp)
movzwl -0x30(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x34(%rbp)
movb $0x0, -0x33(%rbp)
movb $0x0, -0x32(%rbp)
movb $0x0, -0x31(%rbp)
movw -0x2e(%rbp), %ax
movb %al, -0x44(%rbp)
movzwl -0x2e(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x43(%rbp)
movb $0x10, -0x42(%rbp)
movb $0xe, -0x41(%rbp)
movw -0x30(%rbp), %ax
movb %al, -0x40(%rbp)
movzwl -0x30(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x3f(%rbp)
movb $0x0, -0x3e(%rbp)
movb $0x0, -0x3d(%rbp)
movb $0x0, -0x3c(%rbp)
movb $0x0, -0x3b(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
cmpl $0x40, -0x24(%rbp)
jbe 0xcdbb
movl $0x40, -0x24(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xcdc9
cmpq $0x0, -0x10(%rbp)
jne 0xcdea
leaq 0x3da0(%rip), %rdi      # 0x10b70
leaq 0x3e17(%rip), %rsi      # 0x10bee
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0xcf70
movzbl -0x11(%rbp), %eax
cmpl $0x3, %eax
jne 0xce13
movb -0x12(%rbp), %al
movb %al, -0x36(%rbp)
movl $0x7, -0x54(%rbp)
leaq -0x3a(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x24(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x58(%rbp)
jmp 0xce35
movzbl -0x11(%rbp), %eax
andl $0xf, %eax
orl $0x10, %eax
movb %al, -0x42(%rbp)
movl $0x6, -0x54(%rbp)
leaq -0x44(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x280(%rax), %rax
movl 0x8(%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xce56
movl -0x54(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movb 0x1d6(%rax), %al
movq -0x50(%rbp), %rdx
movl -0x54(%rbp), %ecx
leaq -0xb0(%rbp), %r8
movl -0x58(%rbp), %r9d
movzbl %al, %esi
callq 0xc290
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0xce93
movl $0x0, -0x4(%rbp)
jmp 0xcf70
movzbl -0x11(%rbp), %eax
cmpl $0x3, %eax
jne 0xcf14
movzbl -0xaf(%rbp), %eax
cmpl $0x3, %eax
je 0xcec9
movzbl -0xaf(%rbp), %esi
leaq 0x3d48(%rip), %rdi      # 0x10bfe
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0xcf70
movzbl -0xb0(%rbp), %eax
subl $0x2, %eax
movl %eax, -0xb4(%rbp)
movl -0x24(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jae 0xceed
movl -0x24(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0x20(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
addq $0x2, %rsi
movl -0xb4(%rbp), %eax
movl %eax, %edx
callq 0x2150
movl -0xb4(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xcf6a
movzbl -0xb0(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0xcf3d
movq -0x20(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
movl -0x24(%rbp), %eax
movl %eax, %edx
callq 0x2150
movl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0xcf68
movzbl -0xb0(%rbp), %esi
movzbl -0xaf(%rbp), %edx
movzbl -0xae(%rbp), %ecx
movzbl -0xad(%rbp), %r8d
leaq 0x3cbc(%rip), %rdi      # 0x10c1d
movb $0x0, %al
callq 0x2050
jmp 0xcf6a
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)