/*****************************************************************************
 *  bt_hwctl_ioctl
*****************************************************************************/
static long bt_hwctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret = 0;
     
    BT_HWCTL_DEBUG("bt_hwctl_ioctl\n");
    
    if(!bh) {
        BT_HWCTL_ALERT("bt_hwctl struct not initialized\n");
        return -EFAULT;
    }
    
    switch(cmd)
    {
        case BTHWCTL_IOCTL_SET_POWER:
        {
            unsigned long pwr = 0;
            if (copy_from_user(&pwr, (void*)arg, sizeof(unsigned long)))
                return -EFAULT;
                
            BT_HWCTL_DEBUG("BTHWCTL_IOCTL_SET_POWER: %d\n", (int)pwr);
            
            mutex_lock(&bh->sem);
            if (pwr){
                ret = mt_bt_power_on();
            }
            else{
                mt_bt_power_off();
            }
            mutex_unlock(&bh->sem);
            
            break;
        }
        case BTHWCTL_IOCTL_SET_EINT:
        {
            unsigned long eint = 0;
            if (copy_from_user(&eint, (void*)arg, sizeof(unsigned long)))
                return -EFAULT;
                
            BT_HWCTL_DEBUG("BTHWCTL_IOCTL_SET_EINT: %d\n", (int)eint);
            
            mutex_lock(&bh->sem);
            if (eint){
                /* Enable irq from user space */ 
                BT_HWCTL_DEBUG("Set BT EINT enable\n");
                mt_bt_enable_irq();
            }
            else{
                /* Disable irq from user space, maybe time to close driver */
                BT_HWCTL_DEBUG("Set BT EINT disable\n");
                mt_bt_disable_irq();
                eint_mask = 1;
                wake_up_interruptible(&eint_wait);
            }
            mutex_unlock(&bh->sem);
            
            break;
        }    
        default:
            BT_HWCTL_ALERT("BTHWCTL_IOCTL not support\n");
            return -EPERM;
    }
    
    return ret;
}
/*****************************************************************************
* btwaln_em_ioctl
*****************************************************************************/
static int btwlan_em_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    BTWLAN_EM_DEBUG("btwlan_em_ioctl ++\n");
    
    if(!pbtwlan_em)
    {
        BTWLAN_EM_ALERT("btwlan_em_ioctl failed get valid struct\n");
        return -EFAULT;
    }
    
    switch(cmd)
    {
        case BTWLAN_EM_IOCTL_SET_BTPWR:
        {
            unsigned long btpwr = 0;
            if (copy_from_user(&btpwr, (void*)arg, sizeof(unsigned long)))
                return -EFAULT;
                
            BTWLAN_EM_DEBUG("BTWLAN_EM_IOCTL_SET_BTPWR:%d\n", (int)btpwr);
            
            mutex_lock(&pbtwlan_em->sem);
            if (btpwr){
                mt_bt_power_on();
            }
            else{
                mt_bt_power_off();
            }
            mutex_unlock(&pbtwlan_em->sem);
            
            break;
        }
        case BTWLAN_EM_IOCTL_SET_WIFIPWR:
        {
            unsigned long wifipwr = 0;
            if (copy_from_user(&wifipwr, (void*)arg, sizeof(unsigned long)))
                return -EFAULT;
                
            BTWLAN_EM_DEBUG("BTWLAN_EM_IOCTL_SET_WIFIPWR:%d\n", (int)wifipwr);
            
            mutex_lock(&pbtwlan_em->sem);
            if (wifipwr){
                mt_wifi_power_on();
            }
            else{
                mt_wifi_power_off();
            }
            mutex_unlock(&pbtwlan_em->sem);
            
            break;
        }
        case BT_IOCTL_SET_EINT:
        {
            unsigned long bt_eint = 0;
            if (copy_from_user(&bt_eint, (void*)arg, sizeof(unsigned long)))
                return -EFAULT;
                
            BTWLAN_EM_DEBUG("BT_IOCTL_SET_EINT:%d\n", bt_eint);
            
            mutex_lock(&pbtwlan_em->sem);
            if (bt_eint){
                mt65xx_eint_unmask(CUST_EINT_BT_NUM);
                BTWLAN_EM_DEBUG("Set enable BT EINT\n");
            }
            else{
                mt65xx_eint_mask(CUST_EINT_BT_NUM);
                BTWLAN_EM_DEBUG("Set disable BT EINT\n");
                eint_mask = 1;
                wake_up_interruptible(&eint_wait);
            }
            mutex_unlock(&pbtwlan_em->sem);
            break;
        }
        
        default:
            BTWLAN_EM_ALERT("btwlan_em_ioctl not support\n");
            return -EPERM;
    }
    
    BTWLAN_EM_DEBUG("btwlan_em_ioctl --\n");
    return 0;
}