// File: /workspace/github_cpp_makefile_star2_O2/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 $0x40, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movb $0x8, (%rsp)
xorl %ebx, %ebx
decl %esi
cmpl $0x16, %esi
ja 0x2e6c
movq %rdi, %r14
leaq 0x778f(%rip), %rax      # 0xa400
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movb $-0x7b, 0x1(%rsp)
movw $0x1, 0x2(%rsp)
movb $0x2, 0x4(%rsp)
jmp 0x2da1
movb $-0x7b, 0x1(%rsp)
movw $0x2, 0x2(%rsp)
movb $-0x65, 0x5(%rsp)
jmp 0x2cf6
pushq $-0x14
popq %r15
pushq $0x1
popq %rbx
pushq $0x5
popq %rbp
movq %r14, %rdi
movl %ebx, %esi
callq 0x2e7b
movl %ebp, %edi
callq 0x2555
movq %r14, %rdi
callq 0x2e85
cmpb $0x1, %al
je 0x2e6c
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x2cad
jmp 0x2e6c
movq %r14, %rdi
xorl %esi, %esi
jmp 0x2db2
movb $-0x7b, 0x1(%rsp)
movl $0xda010002, 0x2(%rsp)  # imm = 0xDA010002
movl $0x100014, %r15d        # imm = 0x100014
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x2e0a
pushq $-0x14
popq %r15
pushq $0x5
popq %rbp
pushq $0x1
popq %rbx
movq %r14, %rdi
movl $0x90, %esi
callq 0x2e7b
movl %ebp, %edi
callq 0x2555
movq %r14, %rdi
callq 0x2e85
cmpb $-0x70, %al
je 0x2e6c
leal 0x1(%r15), %eax
testl %r15d, %r15d
movl %eax, %r15d
jne 0x2d10
jmp 0x2e6c
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x6, -0x1(%rsi)
movl %edx, %edi
callq 0x668e
movl $0x1d4c, %r15d          # imm = 0x1D4C
jmp 0x2e05
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x3, -0x1(%rsi)
movl %edx, %edi
callq 0x668e
movl $0x1f4, %r15d           # imm = 0x1F4
jmp 0x2e05
movb $-0x6f, 0x1(%rsp)
movw $0x2, 0x2(%rsp)
movb $-0x7e, 0x4(%rsp)
movb %dl, 0x5(%rsp)
movb $0x1, %bpl
xorl %r15d, %r15d
movb $0x1, %r12b
jmp 0x2e0a
pushq $0x1
popq %rsi
movq %r14, %rdi
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2eab
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x4, -0x1(%rsi)
movl %edx, %edi
callq 0x668e
movl $0x9c4, %r15d           # imm = 0x9C4
jmp 0x2e05
leaq 0x5(%rsp), %rsi
movb $-0x7b, -0x4(%rsi)
movw $0x5, -0x3(%rsi)
movb $0x5, -0x1(%rsi)
movl %edx, %edi
callq 0x668e
movl $0xbb8, %r15d           # imm = 0xBB8
movb $0x1, %r12b
xorl %ebp, %ebp
cmpq $0x0, 0xe576(%rip)      # 0x11388
je 0x2e4f
movq %rsp, %rsi
movzwl 0x2(%rsi), %edx
addl $0x4, %edx
movq %r14, %rdi
callq 0x3749
movl %eax, %ebx
testb %bpl, %bpl
jne 0x2e6c
testb %r12b, %r12b
je 0x2e5d
movq %r14, %rdi
movq %r15, %rsi
callq 0x2edd
leaq 0x7686(%rip), %rdi      # 0xa4ca
movl %eax, %esi
xorl %eax, %eax
callq 0x24bf
jmp 0x2e6c
leaq 0x78f8(%rip), %rdi      # 0xa74e
callq 0x2270
jmp 0x2e6c
andl $0x1ffc, %r15d          # imm = 0x1FFC
movl %r15d, %edi
callq 0x2555
movl %ebx, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq