/**
 * Stops the Sleep-Mode Protocol on the Host.
 */
void brcm_btsleep_stop(enum sleep_type type)
{
#ifdef LPM_BLUESLEEP
//BT_S : fix wrong lpm_param issue, [START]
    printk("%s - %d\n", __func__, type);
    if(type == SLEEP_BLUESLEEP)
#if defined(CONFIG_BT_MSM_SLEEP)
        bluesleep_stop();
#elif defined(CONFIG_LGE_BLUETOOTH_PM)
        bluetooth_pm_sleep_stop();
#endif
//BT_E : fix wrong lpm_param issue, [END]
#endif
}
/**
 * Modify the low-power protocol used by the Host via the proc interface.
 * @param file Not used.
 * @param buffer The buffer to read from.
 * @param count The number of bytes to be written.
 * @param data Not used.
 * @return On success, the number of bytes written. On error, -1, and
 * <code>errno</code> is set appropriately.
 */
static int bluetooth_pm_write_proc_proto(struct file *file, const char *buffer,
					unsigned long count, void *data)
{
	char proto;

	if (count < 1)
		return -EINVAL;

	if (copy_from_user(&proto, buffer, 1))
		return -EFAULT;

	if (proto == '1')
                bluetooth_pm_sleep_start();
	else
		bluetooth_pm_sleep_stop();

	/* claim that we wrote everything */
	return count;
}
static ssize_t proc_bt_proto_write(struct file *file, const char __user *buf,
		       size_t length, loff_t *ppos)
{
	char proto;

	BT_INFO("");

	if (length < 1)
		return -EINVAL;

	if (copy_from_user(&proto, buf, 1))
		return -EFAULT;

	if (proto == '0')
		bluetooth_pm_sleep_stop();
	else
		bluetooth_pm_sleep_start();

	/* claim that we wrote everything */
	return length;

}
//BT_S : [CONBT-1572] LGC_BT_COMMON_IMP_KERNEL_UART_SHUTDOWN_EXCEPTION_HANDLING
void bluetooth_pm_sleep_stop_by_uart(void)
{
	printk("%s\n", __func__);
	bluetooth_pm_sleep_stop();
}
static int bluetooth_pm_rfkill_set_power(void *data, bool blocked)
{
	struct bluetooth_pm_device_info *bdev = data;
//BT_S : [CONBT-1256] Fix to kernel crash happened when CONFIG_DEBUG_SPINLOCK is removed.
	//struct pinctrl_state *pins_state;
//BT_E : [CONBT-1256] Fix to kernel crash happened when CONFIG_DEBUG_SPINLOCK is removed.
	unsigned long irq_flags;

	printk("%s: blocked (%d)\n", __func__, blocked);
	
	if (gpio_get_value(bsi->bt_reset) == !blocked)
	{
		printk("%s: Receive same status(%d)\n", __func__, blocked);
		return 0;
	}

	// Check Proto...
	spin_lock_irqsave(&rw_lock, irq_flags);
	if (test_bit(BT_PROTO, &flags)) {
		spin_unlock_irqrestore(&rw_lock, irq_flags);

		printk("%s: proto is enabled. kill proto first..\n", __func__);
		bluetooth_pm_sleep_stop();

		mdelay(100);	
	}
	else
	{
		spin_unlock_irqrestore(&rw_lock, irq_flags);
	}	

//BT_S : [CONBT-1256] Fix to kernel crash happened when CONFIG_DEBUG_SPINLOCK is removed.
    /*
	if (!IS_ERR_OR_NULL(bdev->bt_pinctrl)) {
		pins_state = blocked ? bdev->gpio_state_active : bdev->gpio_state_suspend;
		if (pinctrl_select_state(bdev->bt_pinctrl, pins_state)) {
			pr_err("%s: error on pinctrl_select_state for bt enable - %s\n", __func__,
					blocked ? "bt_enable_active" : "bt_enable_suspend");
		} else {
			printk("%s: success to set pinctrl for bt enable - %s\n", __func__,
					blocked ? "bt_enable_active" : "bt_enable_suspend");
		}
	}
	*/
//BT_E : [CONBT-1256]] Fix to kernel crash happened when CONFIG_DEBUG_SPINLOCK is removed.

    printk("%s:  bdev->gpio_bt_reset = %d\n",__func__, bdev->gpio_bt_reset);
	printk("%s:  bdev->gpio_bt_host_wake = %d \n",__func__,bdev->gpio_bt_host_wake);
	printk("%s:  bdev->gpio_bt_ext_wake = %d \n",__func__,bdev->gpio_bt_ext_wake);
	
	if (!blocked) { // BT ON
		gpio_direction_output(bdev->gpio_bt_reset, 0);
		msleep(30);
		gpio_direction_output(bdev->gpio_bt_reset, 1);
		printk("%s: Bluetooth RESET HIGH!!\n", __func__);
	} else {  // BT OFF
		gpio_direction_output(bdev->gpio_bt_reset, 0);
		printk("%s: Bluetooth RESET LOW!!\n", __func__);
	}

	return 0;
}