// File: chenhn123[P]wdt_util_src/wdt_ct/func_i2c.cpp
// Function: wh_i2c_set_feature(WdtDevice*, unsigned char*, unsigned int)
int wh_i2c_set_feature(WDT_DEV *pdev, BYTE* buf, UINT32 buf_size)
{

	REQ_DATA* p_req_data = (REQ_DATA*) buf;
	int data_len = 0;
	BYTE cmd;
	BYTE tx_buffer[80] = {0};
	bool retryflag = true;	
	int retval = 0;

	if (buf_size > 64)
		buf_size = 64;
	
	if (!buf || !pdev) {
		wh_printf("%s : pointer is null\n", __func__);
		return 0;
	}

retry:
	data_len = 0;
	cmd = buf[0];
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wCommandRegister; 
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wCommandRegister >> 8;



	if (p_req_data->DD.report_id > 0xF) {
		if(retryflag == true)
			tx_buffer[data_len++] = 0x3F;
		else 
			tx_buffer[data_len++] = 0x30;
		tx_buffer[data_len++] = I2C_HID_OPCODE_SET_REPORT;
		tx_buffer[data_len++] = cmd;
		
	} else {
		tx_buffer[data_len++] = 0x30 | cmd;
		tx_buffer[data_len++] = I2C_HID_OPCODE_SET_REPORT;
	}
	

	
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wDataRegister; 
	tx_buffer[data_len++] = pdev->board_info.dev_hid_desc.wDataRegister >> 8;

	if ((pdev->board_info.dev_type & FW_WDT8755 &&
		 pdev->board_info.dev_info.w8755_dev_info.protocol_version >= 0x01000007) ||
		 (pdev->dev_state != DS_PROGRAM) )
	{
		int xfer_size = buf_size + 2;
		tx_buffer[data_len++] = (xfer_size & 0xFF);
		tx_buffer[data_len++] = ((xfer_size & 0xFF00) >> 8);
	} else {
		tx_buffer[data_len++] = (buf_size & 0xFF);
		tx_buffer[data_len++] = ((buf_size & 0xFF00) >> 8);
	}

	memcpy(&tx_buffer[data_len], buf, buf_size);

	retval = wh_i2c_tx(pdev, pdev->board_info.i2c_address, tx_buffer,
			 data_len + buf_size);	

	
	if( (buf[2] != cmd) && (retryflag == true) && (p_req_data->DD.report_id > 0xF) ) {
		retryflag = false;
		goto  retry;
	}

	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movb $0x1, -0x81(%rbp)
movl $0x0, -0x88(%rbp)
cmpl $0x40, -0x1c(%rbp)
jbe 0xd1b3
movl $0x40, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xd1c1
cmpq $0x0, -0x10(%rbp)
jne 0xd1e2
leaq 0x39a8(%rip), %rdi      # 0x10b70
leaq 0x39f9(%rip), %rsi      # 0x10bc8
movb $0x0, %al
callq 0x24e0
movl $0x0, -0x4(%rbp)
jmp 0xd43d
jmp 0xd1e4
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movb (%rax), %al
movb %al, -0x2d(%rbp)
movq -0x10(%rbp), %rax
movw 0x1e7(%rax), %ax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movq -0x10(%rbp), %rax
movzwl 0x1e7(%rax), %eax
sarl $0x8, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xf, %eax
jle 0xd29d
movb -0x81(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xd263
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x3f, -0x80(%rbp,%rax)
jmp 0xd275
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x30, -0x80(%rbp,%rax)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x3, -0x80(%rbp,%rax)
movb -0x2d(%rbp), %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
jmp 0xd2c9
movzbl -0x2d(%rbp), %eax
orl $0x30, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movl -0x2c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
cltq
movb $0x3, -0x80(%rbp,%rax)
movq -0x10(%rbp), %rax
movw 0x1e9(%rax), %ax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movq -0x10(%rbp), %rax
movzwl 0x1e9(%rax), %eax
sarl $0x8, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movq -0x10(%rbp), %rax
movl 0x188(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xd32a
movq -0x10(%rbp), %rax
cmpl $0x1000007, 0x1f8(%rax) # imm = 0x1000007
jae 0xd337
movq -0x10(%rbp), %rax
cmpl $0x3, 0x278(%rax)
je 0xd384
movl -0x1c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movl -0x8c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
jmp 0xd3bd
movl -0x1c(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movl -0x1c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
shrl $0x8, %eax
movb %al, %cl
movl -0x2c(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
cltq
movb %cl, -0x80(%rbp,%rax)
movslq -0x2c(%rbp), %rax
leaq -0x80(%rbp), %rdi
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %eax
movl %eax, %edx
callq 0x2150
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movb 0x1d6(%rax), %al
leaq -0x80(%rbp), %rdx
movl -0x2c(%rbp), %ecx
addl -0x1c(%rbp), %ecx
movzbl %al, %esi
callq 0xcf80
movl %eax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %eax
movzbl -0x2d(%rbp), %ecx
cmpl %ecx, %eax
je 0xd434
movb -0x81(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xd434
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xf, %eax
jle 0xd434
movb $0x0, -0x81(%rbp)
jmp 0xd1e4
movl -0x88(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)