// File: /workspace/github_cpp_makefile_star2_O3/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)
xorl %ebp, %ebp
decl %esi
cmpl $0x16, %esi
ja 0x3066
movq %rdi, %rbx
leaq 0x8628(%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 0x2f83
movb $-0x7b, 0x1(%rsp)
movw $0x2, 0x2(%rsp)
movb $-0x65, 0x5(%rsp)
jmp 0x2ea3
movl $0xffffffec, %r15d      # imm = 0xFFFFFFEC
leaq 0x40(%rsp), %r14
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x2d98
movl $0x5, %edi
callq 0x259c
movq %rbx, %rdi
movl $0x73, %esi
movq %r14, %rdx
movl $0x20, %ecx
movl $0x1000c, %r8d          # imm = 0x1000C
callq 0x2b61
testl %eax, %eax
je 0x2e62
cmpb $0x1, 0x40(%rsp)
setne %al
jmp 0x2e64
movb $0x1, %al
testb %al, %al
je 0x3066
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x2e1d
jmp 0x3066
movq %rbx, %rdi
xorl %esi, %esi
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x30b0
movb $-0x7b, 0x1(%rsp)
movl $0xda010002, 0x2(%rsp)  # imm = 0xDA010002
movl $0x100014, %r14d        # imm = 0x100014
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x2ff8
movl $0xffffffec, %r15d      # imm = 0xFFFFFFEC
leaq 0x40(%rsp), %r14
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x5, %esi
movl $0x90, %edx
callq 0x2d98
movl $0x5, %edi
callq 0x259c
movq %rbx, %rdi
movl $0x73, %esi
movq %r14, %rdx
movl $0x20, %ecx
movl $0x1000c, %r8d          # imm = 0x1000C
callq 0x2b61
testl %eax, %eax
je 0x2f09
cmpb $-0x70, 0x40(%rsp)
setne %al
jmp 0x2f0b
movb $0x1, %al
testb %al, %al
je 0x3066
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x2ec4
jmp 0x3066
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x6, -0x1(%rsi)
movl %edx, %edi
callq 0x6f72
movl $0x1d4c, %r14d          # imm = 0x1D4C
jmp 0x2ff2
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x3, -0x1(%rsi)
movl %edx, %edi
callq 0x6f72
movl $0x1f4, %r14d           # imm = 0x1F4
jmp 0x2ff2
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 0x2ff8
movq %rbx, %rdi
movl $0x16, %esi
xorl %edx, %edx
callq 0x2d98
movl %eax, %ebx
movl $0x14, %edi
callq 0x259c
movl %ebx, %eax
jmp 0x3068
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x4, -0x1(%rsi)
movl %edx, %edi
callq 0x6f72
movl $0x9c4, %r14d           # imm = 0x9C4
jmp 0x2ff2
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x5, -0x1(%rsi)
movl %edx, %edi
callq 0x6f72
movl $0xbb8, %r14d           # imm = 0xBB8
movb $0x1, %r12b
xorl %r15d, %r15d
cmpq $0x0, 0xe388(%rip)      # 0x11388
je 0x3049
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x3022
cmpl $0x2, (%rbx)
jne 0x3022
movq %rsp, %rsi
movzwl 0x2(%rsi), %edx
addl $0x4, %edx
movq %rbx, %rdi
callq 0x9a12
movl %eax, %ebp
testb %r15b, %r15b
jne 0x3066
testb %r12b, %r12b
je 0x3057
movq %rbx, %rdi
movq %r14, %rsi
callq 0x30f3
leaq 0x848c(%rip), %rdi      # 0xb4ca
movl %eax, %esi
xorl %eax, %eax
callq 0x2506
jmp 0x3066
leaq 0x86fe(%rip), %rdi      # 0xb74e
callq 0x2270
jmp 0x3066
andl $0x1ffc, %r14d          # imm = 0x1FFC
movl %r14d, %edi
callq 0x259c
movl %ebp, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq