// File: /workspace/github_cpp_makefile_star2_O1/chenhn123[P]wdt_util_src/wdt_ct/w8755_funcs.cpp
// Function: wh_w8755_dev_send_commands(WdtDevice*, int, unsigned int)
int wh_w8755_dev_send_commands(WDT_DEV* pdev, int cmd, UINT32 value)
{
	W8755_CMD_DATA	cmd_data;
	unsigned long 	delay = 0;
	int				result = 0;

	memset(&cmd_data, 0, sizeof(cmd_data));

	cmd_data.DD.rpt_id = W8755_RPTID_WRITE_DATA;

	switch(cmd)
	{
		case 	WH_CMD_ALGO_STOP: {
			int count = 20;
			do {
				wh_w8755_dev_set_device_mode(pdev, W8755_DM_COMMAND);
				wh_sleep(5);
			} while (wh_w8755_dev_get_device_mode(pdev) != W8755_DM_COMMAND && count-- > 0);
			
			return 1;
		}
		break;
		case 	WH_CMD_ALGO_START: {
			int count = 20;
			do {
				wh_w8755_dev_set_device_mode(pdev, W8755_DM_SENSING);
				wh_sleep(5);
			} while (wh_w8755_dev_get_device_mode(pdev) != W8755_DM_SENSING && count--> 0);

			return 1;
		}
		break;
		case 	WH_CMD_SET_DEV_MODE: {
			cmd_data.DD.type = W8755_FW_SET_COMMAND;
			cmd_data.DD.size = 2;
			cmd_data.DD.cmd = W8755_SET_CMD_DEVICE_MODE; 
			cmd_data.DD.param1 = (BYTE) value;			
		}
		break;
		case 	WH_CMD_RESET: {							
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;			
			cmd_data.DD.size = 1;
			cmd_data.DD.cmd = W8755_SET_CMD_RESET;		
		}
		break;
		case	WH_CMD_FLASH_ERASEALL:	{
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;			
			cmd_data.DD.size = 5;
			cmd_data.DD.cmd = W8755_SET_CMD_ERASEALL;
			put_unaligned_le32(value, &cmd_data.buffer[5]);
			
			delay = W8755_FLASH_CHIP_DELAY;			
		}
		break;
		case	WH_CMD_FLASH_ERASE4K:	{
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;			
			cmd_data.DD.size = 5;
			cmd_data.DD.cmd = W8755_SET_CMD_ERASE4K;
			put_unaligned_le32(value, &cmd_data.buffer[5]);

			delay = W8755_FLASH_4K_DELAY;			
		}
		break;
		case	WH_CMD_FLASH_ERASE32K:	{
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;						
			cmd_data.DD.size = 5;
			cmd_data.DD.cmd = W8755_SET_CMD_ERASE32K;
			put_unaligned_le32(value, &cmd_data.buffer[5]);

			delay = W8755_FLASH_32K_DELAY;				
		}
		break;
		case	WH_CMD_FLASH_ERASE64K:	{
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;						
			cmd_data.DD.size = 5;
			cmd_data.DD.cmd = W8755_SET_CMD_ERASE64K;
			put_unaligned_le32(value, &cmd_data.buffer[5]);

			delay = W8755_FLASH_64K_DELAY;				
		}
		break;
		case WH_CMD_FLASH_PROTECTION_ON: {
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;						
			cmd_data.DD.size = 2;
			cmd_data.DD.cmd = W8755_SET_CMD_SFLOCK; 
			cmd_data.DD.param1 = 0x9B;

			/* to wait read flash status */
			delay = 20 | DELAY_REAL_SLEEP;
		}
		break;
		case WH_CMD_FLASH_PROTECTION_OFF: {
			cmd_data.DD.type = W8755_ISP_SET_COMMAND;						
			cmd_data.DD.size = 2;
			cmd_data.DD.cmd = W8755_SET_CMD_SFUNLOCK; 
			cmd_data.DD.param1 = 0xDA;		
			
			/* to wait read flash status */
			delay = 20 | DELAY_REAL_SLEEP;
		}
		break;		
		case	WH_CMD_FLASH_LOCK:	{		
			return wh_w8755_dev_enable_spi_flash_protection(pdev, 1);
		}
		case	WH_CMD_FLASH_UNLOCK: {		
			return wh_w8755_dev_enable_spi_flash_protection(pdev, 0);
		}
		break;

		default: {
			cmd_data.DD.rpt_id = 0;
			return 0;
		}
	}

	if (g_func_dev_basic.p_wh_set_feature) {
		result = g_func_dev_basic.p_wh_set_feature(pdev, &cmd_data.buffer[0], 4 + cmd_data.DD.size);

		if (delay) {
			if (delay & DELAY_REAL_SLEEP) 
				wh_sleep(delay & DELAY_REAL_SLEEP_MASK);
			else {
				delay = wh_w8755_i2c_delay(pdev, delay);
				wh_printf("cmd delay: %d ms\n", (int) delay);
			}
		}

		return result;
	} else
		printf("set feature ptr is null\n");

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movb $0x8, (%rsp)
decl %esi
cmpl $0x16, %esi
ja 0x3035
movq %rdi, %rbx
leaq 0x8657(%rip), %rax      # 0xb400
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movb $-0x7b, 0x1(%rsp)
movw $0x1, 0x2(%rsp)
movb $0x2, 0x4(%rsp)
jmp 0x2f50
movb $-0x7b, 0x1(%rsp)
movl $0x9b000002, 0x2(%rsp)  # imm = 0x9B000002
jmp 0x2e70
movl $0xffffffec, %r15d      # imm = 0xFFFFFFEC
leaq 0x40(%rsp), %r14
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x2d6b
movl $0x5, %edi
callq 0x259c
movq %rbx, %rdi
movl $0x73, %esi
movq %r14, %rdx
movl $0x20, %ecx
movl $0x1000c, %r8d          # imm = 0x1000C
callq 0x2b31
testl %eax, %eax
sete %al
cmpb $0x1, 0x40(%rsp)
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x303b
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x2dea
jmp 0x303b
movq %rbx, %rdi
xorl %esi, %esi
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3081
movb $-0x7b, 0x1(%rsp)
movl $0xda010002, 0x2(%rsp)  # imm = 0xDA010002
movl $0x100014, %r14d        # imm = 0x100014
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x2fc5
movl $0xffffffec, %r15d      # imm = 0xFFFFFFEC
leaq 0x40(%rsp), %r14
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x5, %esi
movl $0x90, %edx
callq 0x2d6b
movl $0x5, %edi
callq 0x259c
movq %rbx, %rdi
movl $0x73, %esi
movq %r14, %rdx
movl $0x20, %ecx
movl $0x1000c, %r8d          # imm = 0x1000C
callq 0x2b31
testl %eax, %eax
sete %al
cmpb $-0x70, 0x40(%rsp)
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x303b
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x2e91
jmp 0x303b
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x6, -0x1(%rsi)
movl %edx, %edi
callq 0x7032
movl $0x1d4c, %r14d          # imm = 0x1D4C
jmp 0x2fbf
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x3, -0x1(%rsi)
movl %edx, %edi
callq 0x7032
movl $0x1f4, %r14d           # imm = 0x1F4
jmp 0x2fbf
movb $-0x6f, 0x1(%rsp)
movw $0x2, 0x2(%rsp)
movb $-0x7e, 0x4(%rsp)
movb %dl, 0x5(%rsp)
movb $0x1, %r15b
xorl %r14d, %r14d
movb $0x1, %r12b
jmp 0x2fc5
movq %rbx, %rdi
movl $0x16, %esi
xorl %edx, %edx
callq 0x2d6b
movl %eax, %ebx
movl $0x14, %edi
callq 0x259c
movl %ebx, %eax
jmp 0x303d
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x4, -0x1(%rsi)
movl %edx, %edi
callq 0x7032
movl $0x9c4, %r14d           # imm = 0x9C4
jmp 0x2fbf
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x5, -0x1(%rsi)
movl %edx, %edi
callq 0x7032
movl $0xbb8, %r14d           # imm = 0xBB8
movb $0x1, %r12b
xorl %r15d, %r15d
cmpq $0x0, 0xe3bb(%rip)      # 0x11388
je 0x3016
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x2fef
cmpl $0x2, (%rbx)
jne 0x2fef
movq %rsp, %rsi
movzwl 0x2(%rsi), %edx
addl $0x4, %edx
movq %rbx, %rdi
callq 0x9a74
movl %eax, %ebp
testb %r15b, %r15b
jne 0x303b
testb %r12b, %r12b
je 0x3024
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30c4
leaq 0x84bf(%rip), %rdi      # 0xb4ca
movl %eax, %esi
xorl %eax, %eax
callq 0x2506
jmp 0x303b
leaq 0x8731(%rip), %rdi      # 0xb74e
callq 0x2270
jmp 0x3039
andl $0x1ffc, %r14d          # imm = 0x1FFC
movl %r14d, %edi
callq 0x259c
jmp 0x303b
movb $0x0, (%rsp)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq