From 4c267a45b30832c71e57f25bae599bfeb0b90236 Mon Sep 17 00:00:00 2001 From: Dengming Zou Date: Fri, 21 Aug 2020 11:21:38 +0800 Subject: [PATCH] Bluetooth: rtk_btusb: update rtk_btusb to version 5.2.1 Change-Id: I1ff4a930233773d864d0a2e124865c58a97c5268 Signed-off-by: Dengming Zou --- drivers/bluetooth/rtk_btusb.c | 651 +++++++++++++++++++++++++--------- drivers/bluetooth/rtk_btusb.h | 65 ++-- 2 files changed, 501 insertions(+), 215 deletions(-) diff --git a/drivers/bluetooth/rtk_btusb.c b/drivers/bluetooth/rtk_btusb.c index f99a7885ae3f5..2df817818047f 100644 --- a/drivers/bluetooth/rtk_btusb.c +++ b/drivers/bluetooth/rtk_btusb.c @@ -39,16 +39,16 @@ #include "rtk_btusb.h" -#define RTKBT_RELEASE_NAME "20180702_BT_ANDROID_8.1" -#define VERSION "4.1.5" +#define RTKBT_RELEASE_NAME "20200318_BT_ANDROID_9.0" +#define VERSION "5.2.1" #define SUSPNED_DW_FW 0 #define SET_WAKEUP_DEVICE 0 static spinlock_t queue_lock; -static spinlock_t dlfw_lock; -static volatile uint16_t dlfw_dis_state = 0; +static spinlock_t running_flag_lock; +static volatile uint16_t driver_state = 0; #if SUSPNED_DW_FW static firmware_info *fw_info_4_suspend = NULL; @@ -94,6 +94,9 @@ static patch_info fw_patch_table[] = { { 0x0BDA, 0xB761, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761au_fw", "rtl8761a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AUV only */ { 0x0BDA, 0x8761, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761au8192ee_fw", "rtl8761a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AU + 8192EE for LI */ { 0x0BDA, 0x8A60, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761au8812ae_fw", "rtl8761a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AU + 8812AE */ +{ 0x0BDA, 0x8771, 0x8761, 0, 0, "mp_rtl8761b_fw", "rtl8761b_fw", "rtl8761b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8761BU */ +{ 0x0BDA, 0xa725, 0x8761, 0, 0, "mp_rtl8725a_fw", "rtl8725a_fw", "rtl8725a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8725AU */ +{ 0x0BDA, 0xa72A, 0x8761, 0, 0, "mp_rtl8725a_fw", "rtl8725a_fw", "rtl8725a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8725AU BT only */ { 0x0BDA, 0x8821, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AE */ { 0x0BDA, 0x0821, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AE */ @@ -103,21 +106,30 @@ static patch_info fw_patch_table[] = { { 0x13D3, 0x3461, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AE */ { 0x13D3, 0x3462, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AE */ -{ 0x0BDA, 0xB822, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BE */ -{ 0x0BDA, 0xB82C, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BU */ -{ 0x0BDA, 0xB023, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BE */ +{ 0x0BDA, 0xB822, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_25K}, /* RTL8822BE */ +{ 0x0BDA, 0xB82C, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_25K}, /* RTL8822BU */ +{ 0x0BDA, 0xB81D, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_25K}, /* RTL8822BU BT only */ +{ 0x0BDA, 0xB82E, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_25K}, /* RTL8822BU-VN */ +{ 0x0BDA, 0xB023, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_25K}, /* RTL8822BE */ { 0x0BDA, 0xB703, 0x8703, 0, 0, "mp_rtl8723c_fw", "rtl8723c_fw", "rtl8723c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8723CU */ /* todo: RTL8703BU */ { 0x0BDA, 0xD723, 0x8723, 0, 0, "mp_rtl8723d_fw", "rtl8723d_fw", "rtl8723d_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8723DU */ +{ 0x0BDA, 0xD72A, 0x8723, 0, 0, "mp_rtl8723d_fw", "rtl8723d_fw", "rtl8723d_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8723DU BT only */ { 0x0BDA, 0xD720, 0x8723, 0, 0, "mp_rtl8723d_fw", "rtl8723d_fw", "rtl8723d_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8723DE */ { 0x0BDA, 0xB820, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU */ { 0x0BDA, 0xC820, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU */ +{ 0x0BDA, 0xC82A, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU BT only */ { 0x0BDA, 0xC821, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CE */ /* todo: RTL8703CU */ - +{ 0x0BDA, 0xC82C, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CU */ +{ 0x0BDA, 0xC82E, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CU-VN */ +{ 0x0BDA, 0xC81D, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CU BT only */ +{ 0x0BDA, 0xC82F, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CE-VS */ +{ 0x0BDA, 0xC822, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CE */ +{ 0x0BDA, 0xB00C, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CE */ /* NOTE: must append patch entries above the null entry */ -{ 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0 } +{ 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0, 0 } }; struct btusb_data { @@ -166,30 +178,51 @@ struct btusb_data { RTK_sco_card_t *pSCOSnd; #endif }; -#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1) -static bool reset_on_close = 0; -#endif int download_patch(firmware_info *fw_info, int cached); int reset_controller(firmware_info* fw_info); -static inline int check_set_dlfw_state_value(uint16_t change_value) + +/******************************************************** +** this function first check the value, if true then set value +** +*********************************************************/ +static inline bool check_set_driver_state_value(uint16_t check_value, uint16_t change_value) { - int state; - spin_lock(&dlfw_lock); - if(!dlfw_dis_state) { - dlfw_dis_state = change_value; + bool res; + spin_lock(&running_flag_lock); + if((driver_state & check_value) != check_value) { + res = false; } - state = dlfw_dis_state; - spin_unlock(&dlfw_lock); + else { + driver_state |= change_value; + res = true; + } + spin_unlock(&running_flag_lock); + return res; +} + +static inline uint16_t get_driver_state_value(void) +{ + uint16_t state; + spin_lock(&running_flag_lock); + state = driver_state; + spin_unlock(&running_flag_lock); return state; } -static inline void set_dlfw_state_value(uint16_t change_value) +static inline void clear_driver_state(uint16_t clear_value) { - spin_lock(&dlfw_lock); - dlfw_dis_state = change_value; - spin_unlock(&dlfw_lock); + spin_lock(&running_flag_lock); + driver_state &= (~clear_value); + spin_unlock(&running_flag_lock); +} + +static inline void set_driver_state_value(uint16_t change_value) +{ + spin_lock(&running_flag_lock); + driver_state |= change_value; + spin_unlock(&running_flag_lock); } #if SUSPNED_DW_FW @@ -329,11 +362,9 @@ static void print_command(struct sk_buff *skb) #if CONFIG_BLUEDROID /* Global parameters for bt usb char driver */ #define BT_CHAR_DEVICE_NAME "rtkbt_dev" -struct mutex btchr_mutex; static struct sk_buff_head btchr_readq; static wait_queue_head_t btchr_read_wait; -static wait_queue_head_t bt_dlfw_wait; -static bool bt_char_dev_registered; +static wait_queue_head_t bt_drv_state_wait; static dev_t bt_devid; /* bt char device number */ static struct cdev bt_char_dev; /* bt character device structure */ static struct class *bt_char_class; /* device class for usb char driver */ @@ -476,7 +507,8 @@ static int rtk_skb_queue_rear = 0; static void rtk_enqueue(struct sk_buff *skb) { - spin_lock(&queue_lock); + unsigned long flags; + spin_lock_irqsave(&queue_lock, flags); if (rtk_skb_queue_front == (rtk_skb_queue_rear + 1) % QUEUE_SIZE) { /* * If queue is full, current solution is to drop @@ -490,7 +522,7 @@ static void rtk_enqueue(struct sk_buff *skb) rtk_skb_queue_rear %= QUEUE_SIZE; } - spin_unlock(&queue_lock); + spin_unlock_irqrestore(&queue_lock, flags); } static struct sk_buff *rtk_dequeue_try(unsigned int deq_len) @@ -530,7 +562,8 @@ static inline int is_queue_empty(void) static void rtk_clear_queue(void) { struct sk_buff *skb; - spin_lock(&queue_lock); + unsigned long flags; + spin_lock_irqsave(&queue_lock, flags); while(!is_queue_empty()) { skb = rtk_skb_queue[rtk_skb_queue_front]; rtk_skb_queue[rtk_skb_queue_front] = NULL; @@ -540,7 +573,7 @@ static void rtk_clear_queue(void) kfree_skb(skb); } } - spin_unlock(&queue_lock); + spin_unlock_irqrestore(&queue_lock, flags); } /* @@ -571,7 +604,7 @@ static int hci_dev_open(__u16 dev) return -ENODEV; } - if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { + if (test_bit(HCI_UNREGISTER, &hdev->flags)) { ret = -ENODEV; goto done; } @@ -580,14 +613,6 @@ static int hci_dev_open(__u16 dev) ret = -EALREADY; goto done; } -/* - ret = hdev->open(hdev); - if(ret < 0){ - RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret); - goto done; - } - set_bit(HCI_UP, &hdev->flags); -*/ done: return ret; @@ -602,6 +627,7 @@ static int hci_dev_do_close(struct hci_dev *hdev) hdev->close(hdev); /* Clear flags */ hdev->flags = 0; + memset(&hdev->conn_hash, 0, sizeof(struct hci_conn_hash)); return 0; } @@ -653,7 +679,6 @@ static int hci_register_dev(struct hci_dev *hdev) sprintf(hdev->name, "hci%d", id); hdev->id = id; hdev->flags = 0; - hdev->dev_flags = 0; mutex_init(&hdev->lock); RTKBT_DBG("%s: id %d, name %s", __func__, hdev->id, hdev->name); @@ -683,7 +708,7 @@ static void hci_unregister_dev(struct hci_dev *hdev) int i; RTKBT_DBG("%s: hdev %p name %s bus %d", __func__, hdev, hdev->name, hdev->bus); - set_bit(HCI_UNREGISTER, &hdev->dev_flags); + set_bit(HCI_UNREGISTER, &hdev->flags); write_lock(&hci_dev_lock); ghdev = NULL; @@ -797,8 +822,36 @@ static int hci_recv_frame(struct sk_buff *skb) { struct hci_dev *hdev = (struct hci_dev *) skb->dev; - if (!hdev || - (!test_bit(HCI_UP, &hdev->flags) && !test_bit(HCI_INIT, &hdev->flags))) { + if (!hdev || !test_bit(HCI_UP, &hdev->flags)) { + kfree_skb(skb); + return -ENXIO; + } + + /* Incomming skb */ + bt_cb(skb)->incoming = 1; + + /* Time stamp */ + __net_timestamp(skb); + + if (atomic_read(&hdev->promisc)) { +#ifdef CONFIG_SCO_OVER_HCI + if(bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) + hci_send_to_alsa_ringbuffer(hdev, skb); +#endif + /* Send copy to the sockets */ + hci_send_to_stack(hdev, skb); + } + + kfree_skb(skb); + return 0; +} + +/* Receive frame from HCI drivers */ +static int hci_recv_sco_frame(struct sk_buff *skb) +{ + struct hci_dev *hdev = (struct hci_dev *) skb->dev; + + if (!hdev || !test_bit(HCI_UP, &hdev->flags)) { kfree_skb(skb); return -ENXIO; } @@ -865,6 +918,7 @@ static int hci_reassembly(struct hci_dev *hdev, int type, void *data, skb->dev = (void *) hdev; hdev->reassembly[index] = skb; + } while (count) { @@ -929,7 +983,11 @@ static int hci_reassembly(struct hci_dev *hdev, int type, void *data, print_event(skb); bt_cb(skb)->pkt_type = type; - hci_recv_frame(skb); + if(type == HCI_SCODATA_PKT) { + hci_recv_sco_frame(skb); + } + else + hci_recv_frame(skb); hdev->reassembly[index] = NULL; return remain; @@ -961,7 +1019,7 @@ static int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int cou void hci_hardware_error(void) { struct sk_buff *rtk_skb_copy = NULL; - int len = 3; + int len = 4; uint8_t hardware_err_pkt[4] = {HCI_EVENT_PKT, 0x10, 0x01, HCI_VENDOR_USB_DISC_HARDWARE_ERROR}; rtk_skb_copy = alloc_skb(len, GFP_ATOMIC); @@ -980,16 +1038,24 @@ static int btchr_open(struct inode *inode_p, struct file *file_p) { struct btusb_data *data; struct hci_dev *hdev; + struct sk_buff *skb; + int i; RTKBT_INFO("%s: BT usb char device is opening", __func__); - /* Not open unless wanna tracing log */ - /* trace_printk("%s: open....\n", __func__); */ + + if(!check_set_driver_state_value(DEVICE_PROBED, CHAR_OPENED)) { + RTKBT_ERR("%s: Device not probed", __func__); + return -ENODEV; + } + hdev = hci_dev_get(0); if (!hdev) { RTKBT_ERR("%s: Failed to get hci dev[NULL]", __func__); return -ENODEV; } + + set_bit(HCI_UP, &hdev->flags); data = GET_DRV_DATA(hdev); atomic_inc(&hdev->promisc); @@ -1000,11 +1066,15 @@ static int btchr_open(struct inode *inode_p, struct file *file_p) */ file_p->private_data = data; - mutex_lock(&btchr_mutex); hci_dev_open(0); - mutex_unlock(&btchr_mutex); - rtk_clear_queue(); + for(i = 0; i < NUM_REASSEMBLY; i++) { + skb = hdev->reassembly[i]; + if(skb) { + hdev->reassembly[i] = NULL; + kfree_skb(skb); + } + } return nonseekable_open(inode_p, file_p); } @@ -1014,8 +1084,6 @@ static int btchr_close(struct inode *inode_p, struct file *file_p) struct hci_dev *hdev; RTKBT_INFO("%s: BT usb char device is closing", __func__); - /* Not open unless wanna tracing log */ - /* trace_printk("%s: close....\n", __func__); */ data = file_p->private_data; file_p->private_data = NULL; @@ -1028,14 +1096,19 @@ static int btchr_close(struct inode *inode_p, struct file *file_p) bt_reset = 0; #endif + hdev = hci_dev_get(0); if (hdev) { atomic_set(&hdev->promisc, 0); - mutex_lock(&btchr_mutex); hci_dev_close(0); - mutex_unlock(&btchr_mutex); + clear_bit(HCI_UP, &hdev->flags); } + clear_driver_state(CHAR_OPENED); + //if the state is not probed, the driver may be in the disconnecting state + //and waitting for signal to wake up + if((get_driver_state_value() & DEVICE_PROBED) == 0) + wake_up_interruptible(&bt_drv_state_wait); return 0; } @@ -1064,7 +1137,7 @@ static ssize_t btchr_read(struct file *file_p, ret = wait_event_interruptible(btchr_read_wait, !is_queue_empty()); if (ret < 0) { - RTKBT_ERR("%s: wait event is signaled %d", __func__, (int)ret); + RTKBT_ERR("%s: wait event is signaled %zu", __func__, ret); break; } @@ -1092,6 +1165,11 @@ static ssize_t btchr_write(struct file *file_p, RTKBT_DBG("%s: BT usb char device is writing", __func__); + if((get_driver_state_value() & DEVICE_PROBED) == 0) { + RTKBT_ERR("%s: Device not probed", __func__); + return POLLERR | POLLHUP; + } + hdev = hci_dev_get(0); if (!hdev) { RTKBT_WARN("%s: Failed to get hci dev[Null]", __func__); @@ -1141,8 +1219,9 @@ static unsigned int btchr_poll(struct file *file_p, poll_table *wait) RTKBT_DBG("%s: BT usb char device is polling", __func__); - if(!bt_char_dev_registered) { - RTKBT_ERR("%s: char device has not registered!", __func__); + if((get_driver_state_value() & DRIVER_ON) == 0 || + (get_driver_state_value() & DEVICE_PROBED) == 0) { + RTKBT_ERR("%s: Device not probed", __func__); return POLLERR | POLLHUP; } @@ -1172,31 +1251,28 @@ static unsigned int btchr_poll(struct file *file_p, poll_table *wait) return POLLOUT | POLLWRNORM; } + static long btchr_ioctl(struct file *file_p, unsigned int cmd, unsigned long arg){ int ret = 0; struct hci_dev *hdev; struct btusb_data *data; firmware_info *fw_info; - if(!bt_char_dev_registered) { + if((get_driver_state_value() & DRIVER_ON) == 0 || + (get_driver_state_value() & DEVICE_PROBED) == 0) { + RTKBT_ERR("%s bt controller is disconnect!", __func__); return -ENODEV; } - if(check_set_dlfw_state_value(1) != 1) { - RTKBT_ERR("%s bt controller is disconnecting!", __func__); - return 0; - } - hdev = hci_dev_get(0); if(!hdev) { RTKBT_ERR("%s device is NULL!", __func__); - set_dlfw_state_value(0); return 0; } data = GET_DRV_DATA(hdev); fw_info = data->fw_info; - RTKBT_INFO(" btchr_ioctl DOWN_FW_CFG with Cmd:%d",cmd); + RTKBT_INFO(" btchr_ioctl with Cmd:%d",cmd); switch (cmd) { case DOWN_FW_CFG: ret = usb_autopm_get_interface(data->intf); @@ -1216,40 +1292,61 @@ static long btchr_ioctl(struct file *file_p, unsigned int cmd, unsigned long arg RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret); goto failed; } - set_bit(HCI_UP, &hdev->flags); - set_dlfw_state_value(0); - wake_up_interruptible(&bt_dlfw_wait); - return 1; + ret = 1; + break; + +#ifdef CONFIG_SCO_OVER_HCI + case SET_ISO_CFG: + if(get_user(hdev->voice_setting, (__u16 __user*)arg)) { + ret = -EFAULT; + } + RTKBT_INFO(" voice settings = 0x%04x", hdev->voice_setting); + break; +#endif + case GET_USB_INFO: ret = hdev->open(hdev); if(ret < 0){ RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret); //goto done; } - set_bit(HCI_UP, &hdev->flags); - return usb_info; + put_user(usb_info, (__u32 __user*)arg); + ret = 1; + break; case RESET_CONTROLLER: reset_controller(fw_info); - return 1; + ret = 1; + break; + + case DWFW_CMPLT: + { + uint16_t lmp_sub = 0; + if(get_user(lmp_sub, (__u16 __user*)arg)) { + ret = -EFAULT; + } + else if(lmp_sub != 0) { + fw_info->patch_entry->lmp_sub = lmp_sub; + } + RTKBT_INFO("%s lmp_sub = 0x%x, patch_entry->lmp_sub = 0x%x", __func__, + lmp_sub, fw_info->patch_entry->lmp_sub); + } + break; -#ifdef CONFIG_SCO_OVER_HCI - case SET_ISO_CFG: - hdev->voice_setting = *(__u16 *)arg; - RTKBT_INFO(" voice settings = 0x%04x", hdev->voice_setting); - return 1; -#endif default: RTKBT_ERR("%s:Failed with wrong Cmd:%d",__func__,cmd); goto failed; } - failed: - set_dlfw_state_value(0); - wake_up_interruptible(&bt_dlfw_wait); +failed: return ret; } - +#ifdef CONFIG_COMPAT +static long compat_btchr_ioctl (struct file *filp, unsigned int cmd, unsigned long arg) +{ + return btchr_ioctl(filp, cmd, (unsigned long) compat_ptr(arg)); +} +#endif static struct file_operations bt_chrdev_ops = { open : btchr_open, @@ -1258,6 +1355,9 @@ static struct file_operations bt_chrdev_ops = { write : btchr_write, poll : btchr_poll, unlocked_ioctl : btchr_ioctl, +#ifdef CONFIG_COMPAT + compat_ioctl : compat_btchr_ioctl, +#endif }; static int btchr_init(void) @@ -1266,16 +1366,9 @@ static int btchr_init(void) struct device *dev; RTKBT_INFO("Register usb char device interface for BT driver"); - /* - * btchr mutex is used to sync between - * 1) downloading patch and opening bt char driver - * 2) the file operations of bt char driver - */ - mutex_init(&btchr_mutex); - skb_queue_head_init(&btchr_readq); init_waitqueue_head(&btchr_read_wait); - init_waitqueue_head(&bt_dlfw_wait); + init_waitqueue_head(&bt_drv_state_wait); bt_char_class = class_create(THIS_MODULE, BT_CHAR_DEVICE_NAME); if (IS_ERR(bt_char_class)) { @@ -1428,6 +1521,7 @@ static patch_info *get_fw_table_entry(struct usb_device* udev) static patch_info *get_suspend_fw_table_entry(struct usb_device* udev) { patch_info *patch_entry = fw_patch_table; + patch_info *patch_entry_real = NULL; uint16_t vid = le16_to_cpu(udev->descriptor.idVendor); uint16_t pid = le16_to_cpu(udev->descriptor.idProduct); uint32_t entry_size = sizeof(fw_patch_table) / sizeof(fw_patch_table[0]); @@ -1444,8 +1538,11 @@ static patch_info *get_suspend_fw_table_entry(struct usb_device* udev) RTKBT_ERR("%s: No fw table entry found", __func__); return NULL; } - - return patch_entry; + patch_entry_real = kmalloc(sizeof(fw_patch_table[0]), GFP_KERNEL); + if(!patch_entry_real) + return NULL; + memcpy(patch_entry_real, patch_entry, sizeof(fw_patch_table[0])); + return patch_entry_real; } #endif @@ -1518,11 +1615,13 @@ int reset_controller(firmware_info* fw_info) * 0: no need to download fw patch * <0: failed to check lmp version */ -int check_fw_version(firmware_info* fw_info) +int check_fw_version(firmware_info* fw_info, bool resume_check) { struct hci_rp_read_local_version *read_ver_rsp; patch_info *patch_entry = NULL; - int ret_val = -1; + int ret_val = -1, i, ret_len = 0; + struct sk_buff *rtk_skb_copy = NULL; + unsigned char pkt_type = HCI_EVENT_PKT; fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_OP_READ_LOCAL_VERSION); fw_info->cmd_hdr->plen = 0; @@ -1535,11 +1634,40 @@ int check_fw_version(firmware_info* fw_info) return ret_val; } - ret_val = rcv_hci_evt(fw_info); - if (ret_val < 0) { - RTKBT_ERR("%s: Failed to receive hci event, errno %d", + while (1) { + for(i = 0; i < 5; i++) { + ret_val = usb_interrupt_msg( + fw_info->udev, fw_info->pipe_in, + (void *)(fw_info->rcv_pkt), PKT_LEN, + &ret_len, MSG_TO); + if (ret_val >= 0) + break; + } + + if (ret_val < 0) { + RTKBT_ERR("%s: Failed to receive hci event, errno %d", __func__, ret_val); - return ret_val; + return ret_val; + } + + if ((CMD_CMP_EVT == fw_info->evt_hdr->evt) && + (fw_info->cmd_hdr->opcode == fw_info->cmd_cmp->opcode)) { + break; + } + else if(resume_check) { + rtk_skb_copy = bt_skb_alloc((ret_len), GFP_ATOMIC); + if (!rtk_skb_copy) { + RTKBT_ERR("%s: Failed to allocate mem", __func__); + return 2; + } + + memcpy(skb_put(rtk_skb_copy, ret_len), fw_info->rcv_pkt, ret_len); + memcpy(skb_push(rtk_skb_copy, 1), &pkt_type, 1); + rtk_enqueue(rtk_skb_copy); + + rtk_skb_copy = NULL; + wake_up_interruptible(&btchr_read_wait); + } } patch_entry = fw_info->patch_entry; @@ -1635,6 +1763,55 @@ int reset_channel(firmware_info* fw_info) return ret_val; } + +//sometimes the controller is in warm resume,and still send message to host +//we should reset controller and clean the hardware buffer +static bool reset_and_clean_hw_buffer(firmware_info* fw_info) +{ + int ret_val/*, i*/; + int ret_len = 0; + bool event_recv = false; + + if (!fw_info) + return -ENODEV; + + fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_RESET); + fw_info->cmd_hdr->plen = 0; + fw_info->pkt_len = CMD_HDR_LEN; + + ret_val = send_hci_cmd(fw_info); + if (ret_val < 0) { + RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d", + __func__, fw_info->cmd_hdr->opcode, ret_val); + return ret_val; + } + + //we need to clean the hardware buffer + while (1) { + ret_val = usb_interrupt_msg( + fw_info->udev, fw_info->pipe_in, + (void *)(fw_info->rcv_pkt), PKT_LEN, + &ret_len, (MSG_TO/2)); + + if(ret_val >= 0) { + if(event_recv) { + RTKBT_INFO("%s: clear hardware event", __func__); + continue; + } + } + else { + return event_recv; + } + + if (CMD_CMP_EVT == fw_info->evt_hdr->evt) { + if (fw_info->cmd_hdr->opcode == fw_info->cmd_cmp->opcode) { + event_recv = true; + } + } + } + return ret_val; +} + int read_localversion(firmware_info* fw_info) { struct rtk_localversion_evt *ever_evt; @@ -1751,7 +1928,7 @@ void rtk_update_altsettings(patch_info *patch_entry, const unsigned char* org_co if (config->data_len != org_config_len - sizeof(struct rtk_bt_vendor_config)) { - RTKBT_ERR("rtk_update_altsettings: config len(%x) is not right(%x)", config->data_len, org_config_len-(int)sizeof(struct rtk_bt_vendor_config)); + RTKBT_ERR("rtk_update_altsettings: config len(%x) is not right(%x)", (int)(config->data_len), (int)(org_config_len-sizeof(struct rtk_bt_vendor_config))); return; } @@ -1960,6 +2137,7 @@ int load_firmware(firmware_info *fw_info, uint8_t **buff) if (!(buf = kzalloc(buf_len, GFP_KERNEL))) { RTKBT_ERR("%s: Can't alloc memory for fw&config", __func__); buf_len = -1; + kfree(p_epatch_entry); } else { memcpy(buf, &epatch_buf[p_epatch_entry->start_offset], p_epatch_entry->patch_length); memcpy(&buf[p_epatch_entry->patch_length-4], &epatch_info->fw_version, 4); @@ -1990,6 +2168,113 @@ int load_firmware(firmware_info *fw_info, uint8_t **buff) return ret_val; } +static void load_firmware_info(firmware_info *fw_info) +{ + const struct firmware *fw/*, *cfg*/; + struct usb_device *udev; + patch_info *patch_entry; + char *fw_name; + int ret_val; + + int buf_len = -1; + uint8_t *epatch_buf = NULL; + + struct rtk_epatch *epatch_info = NULL; + struct rtk_extension_entry patch_lmp = {0}; + uint16_t lmp_version; + RTKBT_DBG("%s: start", __func__); + + udev = fw_info->udev; + patch_entry = fw_info->patch_entry; + lmp_version = patch_entry->lmp_sub_default; + + if(DRV_MP_MODE == mp_drv_mode){ + fw_name = patch_entry->mp_patch_name; + } else { + fw_name = patch_entry->patch_name; + } + + RTKBT_INFO("%s: Default lmp version = 0x%04x, fw file name[%s]", __func__, lmp_version, fw_name); + + ret_val = request_firmware(&fw, fw_name, &udev->dev); + if (ret_val < 0) + goto fw_fail; + else { + epatch_buf = vmalloc(fw->size); + RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__); + if (!epatch_buf) { + release_firmware(fw); + goto fw_fail; + } + memcpy(epatch_buf, fw->data, fw->size); + buf_len = fw->size; + release_firmware(fw); + } + + ret_val = reset_and_clean_hw_buffer(fw_info); + + if (lmp_version != ROM_LMP_8723a) { + RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__); + ret_val = get_eversion(fw_info); + if (ret_val < 0) { + RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val); + goto fw_fail; + } + RTKBT_DBG("%s: Get eversion =%d", __func__, patch_entry->eversion); + if (memcmp(epatch_buf + buf_len - 4 , EXTENSION_SECTION_SIGNATURE, 4)) { + RTKBT_ERR("%s: Failed to check extension section signature", __func__); + } else { + uint8_t *temp; + temp = epatch_buf + buf_len - 5; + do { + if (*temp == 0x00) { + patch_lmp.opcode = *temp; + patch_lmp.length = *(temp-1); + if ((patch_lmp.data = vmalloc(patch_lmp.length))) { + int k; + for (k = 0; k < patch_lmp.length; k++) { + *(patch_lmp.data+k) = *(temp-2-k); + RTKBT_DBG("data = 0x%x", *(patch_lmp.data+k)); + } + } + else + goto fw_fail; + RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__, + patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data)); + break; + } + temp -= *(temp-1) + 2; + } while (*temp != 0xFF); + + if (lmp_version != project_id[*(patch_lmp.data)]) { + RTKBT_ERR("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x " + "-> not match", __func__, lmp_version, *(patch_lmp.data),project_id[*(patch_lmp.data)]); + } else { + RTKBT_INFO("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x " + "-> match", __func__, lmp_version, *(patch_lmp.data), project_id[*(patch_lmp.data)]); + if (memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) { + RTKBT_ERR("%s: Check signature error", __func__); + } else { + epatch_info = (struct rtk_epatch*)epatch_buf; + patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version; + + RTKBT_DBG("%s: lmp version 0x%04x, fw_version 0x%x, " + "number_of_total_patch %d", __func__, + patch_entry->lmp_sub, epatch_info->fw_version, + epatch_info->number_of_total_patch); + } + } + } + } + + RTKBT_DBG("%s: done", __func__); +fw_fail: + if(epatch_buf) + vfree(epatch_buf); + if (patch_lmp.data) + vfree(patch_lmp.data); +} + #if SUSPNED_DW_FW static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff) { @@ -2025,12 +2310,17 @@ static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff) if (ret_val < 0) config_len = 0; else { - config_file_buf = vmalloc(cfg->size); - RTKBT_INFO("%s: epatch_buf = vmalloc(cfg->size)", __func__); - if (!config_file_buf) - return -ENOMEM; - memcpy(config_file_buf, cfg->data, cfg->size); - config_len = cfg->size; + int i; + rtk_update_altsettings(patch_entry, cfg->data, cfg->size, &config_file_buf, &config_len); + + RTKBT_INFO("Final Config len=%08x:\n", config_len); + for(i=0;i<=config_len;i+=0x10) + { + RTKBT_INFO("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, \ + config_file_buf[i], config_file_buf[i+1], config_file_buf[i+2], config_file_buf[i+3], config_file_buf[i+4], config_file_buf[i+5], config_file_buf[i+6], config_file_buf[i+7], \ + config_file_buf[i+8], config_file_buf[i+9], config_file_buf[i+10], config_file_buf[i+11], config_file_buf[i+12], config_file_buf[i+13], config_file_buf[i+14], config_file_buf[i+15]); + } + release_firmware(cfg); } @@ -2121,6 +2411,7 @@ static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff) if (!(buf = kzalloc(buf_len, GFP_KERNEL))) { RTKBT_ERR("%s: Can't alloc memory for fw&config", __func__); buf_len = -1; + kfree(p_epatch_entry); } else { memcpy(buf, &epatch_buf[p_epatch_entry->start_offset], p_epatch_entry->patch_length); memcpy(&buf[p_epatch_entry->patch_length-4], &epatch_info->fw_version, 4); @@ -2136,12 +2427,6 @@ static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff) } } - if (config_file_buf){ - vfree(config_file_buf); - config_file_buf = NULL; - RTKBT_INFO("%s: vfree(config_file_buf)", __func__); - } - RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__, (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not"); @@ -2153,11 +2438,6 @@ static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff) return buf_len; fw_fail: - if (config_file_buf){ - vfree(config_file_buf); - config_file_buf = NULL; - } - RTKBT_INFO("%s: fw_fail vfree(config_file_buf)", __func__); return ret_val; } #endif @@ -2362,7 +2642,7 @@ int download_patch(firmware_info *fw_info, int cached) goto free; } - ret_val = check_fw_version(fw_info); + ret_val = check_fw_version(fw_info, false); if (2 == ret_val) { RTKBT_ERR("%s: Cold reset bt chip only download", __func__); @@ -2403,13 +2683,13 @@ static int download_suspend_patch(firmware_info *fw_info, int cached) /*check the length of fw to be download*/ RTKBT_DBG("%s:Check RTK_PATCH_LENGTH fw_info->fw_len:%d", __func__,fw_info->fw_len); - if (fw_info->fw_len > RTK_PATCH_LENGTH_MAX || fw_info->fw_len == 0) { - RTKBT_ERR("%s: Total length of fw&config larger than allowed 24K or no fw len:%d", __func__, fw_info->fw_len); + if (fw_info->fw_len > fw_info->patch_entry->max_patch_size || fw_info->fw_len == 0) { + RTKBT_ERR("%s: Total length of fw&config(%08x) larger than max_patch_size 0x%08x", __func__, fw_info->fw_len, fw_info->patch_entry->max_patch_size); ret_val = -1; goto free; } - ret_val = check_fw_version(fw_info); + ret_val = check_fw_version(fw_info, false); if (2 == ret_val) { RTKBT_ERR("%s: Cold reset bt chip only download", __func__); @@ -2614,8 +2894,9 @@ void firmware_info_destroy(struct usb_interface *intf) kfree(fw_info_4_suspend->rcv_pkt); kfree(fw_info_4_suspend->send_pkt); + kfree(fw_info_4_suspend->patch_entry); kfree(fw_info_4_suspend); - fw_info_4_suspend = NULL; + fw_info_4_suspend = NULL; #endif } @@ -2887,7 +3168,7 @@ static void btusb_isoc_complete(struct urb *urb) RTKBT_DBG("%s: urb %p status %d count %d", __func__, urb, urb->status, urb->actual_length); - if (!test_bit(HCI_RUNNING, &hdev->flags)) + if (!test_bit(HCI_RUNNING, &hdev->flags) || !test_bit(BTUSB_ISOC_RUNNING, &data->flags)) return; if (urb->status == 0) { @@ -2913,10 +3194,6 @@ static void btusb_isoc_complete(struct urb *urb) return; } - - if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags)) - return; - usb_anchor_urb(urb, &data->isoc_anchor); i = 0; retry: @@ -3062,16 +3339,28 @@ static void btusb_isoc_tx_complete(struct urb *urb) static int btusb_open(struct hci_dev *hdev) { struct btusb_data *data = GET_DRV_DATA(hdev); - int err = 0; + int i, err = 0; RTKBT_INFO("%s: Start, PM usage count %d", __func__, - atomic_read(&(data->intf->dev.power.usage_count))); +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) + atomic_read(&data->intf->dev.power.usage_count) +#else + 0 +#endif + ); err = usb_autopm_get_interface(data->intf); if (err < 0) return err; data->intf->needs_remote_wakeup = 1; + for (i = 0; i < NUM_REASSEMBLY; i++) { + if (hdev->reassembly[i]) { + RTKBT_DBG("%s: free ressembly[%d]", __func__, i); + kfree_skb(hdev->reassembly[i]); + hdev->reassembly[i] = NULL; + } + } if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) goto done; @@ -3095,16 +3384,28 @@ static int btusb_open(struct hci_dev *hdev) done: usb_autopm_put_interface(data->intf); + RTKBT_INFO("%s: End, PM usage count %d", __func__, - atomic_read(&(data->intf->dev.power.usage_count))); +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) + atomic_read(&data->intf->dev.power.usage_count) +#else + 0 +#endif + ); return 0; failed: clear_bit(BTUSB_INTR_RUNNING, &data->flags); clear_bit(HCI_RUNNING, &hdev->flags); usb_autopm_put_interface(data->intf); + RTKBT_ERR("%s: Failed, PM usage count %d", __func__, - atomic_read(&(data->intf->dev.power.usage_count))); +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) + atomic_read(&data->intf->dev.power.usage_count) +#else + 0 +#endif + ); return err; } @@ -3119,21 +3420,13 @@ static void btusb_stop_traffic(struct btusb_data *data) static int btusb_close(struct hci_dev *hdev) { struct btusb_data *data = GET_DRV_DATA(hdev); - int i, err; + int err; RTKBT_INFO("%s: hci running %lu", __func__, hdev->flags & HCI_RUNNING); if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) return 0; - for (i = 0; i < NUM_REASSEMBLY; i++) { - if (hdev->reassembly[i]) { - RTKBT_DBG("%s: free ressembly[%d]", __func__, i); - kfree_skb(hdev->reassembly[i]); - hdev->reassembly[i] = NULL; - } - } - cancel_work_sync(&data->work); cancel_work_sync(&data->waker); @@ -3335,8 +3628,6 @@ static int btusb_send_frame(struct sk_buff *skb) if (!test_bit(HCI_RUNNING, &hdev->flags)) return -EBUSY; - - switch (bt_cb(skb)->pkt_type) { case HCI_COMMAND_PKT: print_command(skb); @@ -3532,6 +3823,7 @@ static void btusb_work(struct work_struct *work) { struct btusb_data *data = container_of(work, struct btusb_data, work); struct hci_dev *hdev = data->hdev; + struct sk_buff *skb; int err; int new_alts; @@ -3574,6 +3866,11 @@ static void btusb_work(struct work_struct *work) mdelay(URB_CANCELING_DELAY_MS); usb_kill_anchored_urbs(&data->isoc_anchor); + if(hdev->reassembly[HCI_SCODATA_PKT - 1]) { + skb = hdev->reassembly[HCI_SCODATA_PKT - 1]; + hdev->reassembly[HCI_SCODATA_PKT - 1] = NULL; + kfree_skb(skb); + } if (set_isoc_interface(hdev, new_alts) < 0) return; } @@ -3600,6 +3897,7 @@ static void btusb_work(struct work_struct *work) usb_kill_anchored_urbs(&data->isoc_anchor); set_isoc_interface(hdev, 0); + if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags)) usb_autopm_put_interface(data->isoc ? data->isoc : data->intf); } @@ -3611,7 +3909,12 @@ static void btusb_waker(struct work_struct *work) int err; RTKBT_DBG("%s: PM usage count %d", __func__, - atomic_read(&data->intf->dev.power.usage_count)); +#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) + atomic_read(&data->intf->pm_usage_cnt) +#else + 0 +#endif + ); err = usb_autopm_get_interface(data->intf); if (err < 0) @@ -4082,6 +4385,7 @@ static RTK_sco_card_t* btusb_snd_init(struct usb_interface *intf, const struct u err = snd_pcm_new(card, RTK_SCO_ID, 0, 1, 1, &pSCOSnd->pcm); if (err < 0) { RTKBT_ERR("%s: sco snd card new pcm fail", __func__); + snd_card_free(card); return NULL; } pSCOSnd->pcm->private_data = pSCOSnd; @@ -4094,6 +4398,7 @@ static RTK_sco_card_t* btusb_snd_init(struct usb_interface *intf, const struct u err = snd_card_register(card); if (err < 0) { RTKBT_ERR("%s: sco snd card register card fail", __func__); + snd_card_free(card); return NULL; } @@ -4102,6 +4407,17 @@ static RTK_sco_card_t* btusb_snd_init(struct usb_interface *intf, const struct u INIT_WORK(&pSCOSnd->send_sco_work, playback_work); return pSCOSnd; } + +static void btusb_snd_remove(RTK_sco_card_t *pSCOSnd) +{ + if(!pSCOSnd) { + RTKBT_ERR("%s: sco private data is null", __func__); + return; + } + set_bit(DISCONNECTED, &pSCOSnd->states); + snd_card_disconnect(pSCOSnd->card); + snd_card_free_when_closed(pSCOSnd->card); +} #endif static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *id) @@ -4158,9 +4474,8 @@ static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *i data->udev = udev; data->intf = intf; - dlfw_dis_state = 0; spin_lock_init(&queue_lock); - spin_lock_init(&dlfw_lock); + spin_lock_init(&running_flag_lock); spin_lock_init(&data->lock); INIT_WORK(&data->work, btusb_work); @@ -4209,13 +4524,6 @@ static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *i hdev->owner = THIS_MODULE; #endif -#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1) - if (!reset_on_close){ - /* set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); */ - RTKBT_DBG("%s: Set HCI_QUIRK_RESET_ON_CLOSE", __func__); - } -#endif - /* Interface numbers are hardcoded in the specification */ data->isoc = usb_ifnum_to_if(data->udev, 1); if (data->isoc) { @@ -4270,7 +4578,10 @@ static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *i bt_reset = 0; /* Clear and reset it anyway */ #endif + load_firmware_info(fw_info); + end: + set_driver_state_value(DEVICE_PROBED); return 0; } @@ -4282,7 +4593,11 @@ static void btusb_disconnect(struct usb_interface *intf) if (intf->cur_altsetting->desc.bInterfaceNumber != 0) return; - wait_event_interruptible(bt_dlfw_wait, (check_set_dlfw_state_value(2) == 2)); + clear_driver_state(DEVICE_PROBED); + if((get_driver_state_value() & CHAR_OPENED) == CHAR_OPENED) + wake_up_interruptible(&btchr_read_wait); + + wait_event_interruptible(bt_drv_state_wait, ((get_driver_state_value() & CHAR_OPENED) == 0)); RTKBT_INFO("%s: usb_interface %p, bInterfaceNumber %d", __func__, intf, intf->cur_altsetting->desc.bInterfaceNumber); @@ -4299,13 +4614,7 @@ static void btusb_disconnect(struct usb_interface *intf) #ifdef CONFIG_SCO_OVER_HCI if (intf->cur_altsetting->desc.bInterfaceNumber == 0) { RTK_sco_card_t *pSCOSnd = data->pSCOSnd; - if(!pSCOSnd) { - RTKBT_ERR("%s: sco private data is null", __func__); - return; - } - set_bit(DISCONNECTED, &pSCOSnd->states); - snd_card_disconnect(pSCOSnd->card); - snd_card_free_when_closed(pSCOSnd->card); + btusb_snd_remove(pSCOSnd); } #endif @@ -4347,7 +4656,7 @@ static void btusb_disconnect(struct usb_interface *intf) hci_free_dev(hdev); rtk_free(data); data = NULL; - set_dlfw_state_value(0); + set_driver_state_value(0); } #ifdef CONFIG_PM @@ -4440,7 +4749,7 @@ static int btusb_resume(struct usb_interface *intf) return 0; /*check_fw_version to check the status of the BT Controller after USB Resume*/ - err = check_fw_version(fw_info); + err = check_fw_version(fw_info, true); if (err !=0) { RTKBT_INFO("%s: BT Controller Power OFF And Return hci_hardware_error:%d", __func__, err); @@ -4516,13 +4825,15 @@ static int __init btusb_init(void) RTKBT_INFO("RTKBT_RELEASE_NAME: %s",RTKBT_RELEASE_NAME); RTKBT_INFO("Realtek Bluetooth USB driver module init, version %s", VERSION); + driver_state = 0; #if CONFIG_BLUEDROID err = btchr_init(); if (err < 0) { /* usb register will go on, even bt char register failed */ RTKBT_ERR("Failed to register usb char device interfaces"); - } else - bt_char_dev_registered = true; + } + else + set_driver_state_value(DRIVER_ON); #endif err = usb_register(&btusb_driver); if (err < 0) @@ -4535,16 +4846,14 @@ static void __exit btusb_exit(void) struct hci_dev *hdev; RTKBT_INFO("Realtek Bluetooth USB driver module exit"); #if CONFIG_BLUEDROID + clear_driver_state(DRIVER_ON); hdev = hci_dev_get(0); - if (bt_char_dev_registered) { - bt_char_dev_registered = false; - while(hdev && atomic_read(&hdev->promisc)) { - RTKBT_ERR("%s: rtkbt driver is being removed, but application is still running!", __func__); - RTKBT_ERR("%s: wait bt application to stop, or the driver can't be removed", __func__); - mdelay(100); - } - btchr_exit(); + while(hdev && atomic_read(&hdev->promisc)) { + RTKBT_ERR("%s: rtkbt driver is being removed, but application is still running!", __func__); + RTKBT_ERR("%s: wait bt application to stop, or the driver can't be removed", __func__); + mdelay(100); } + btchr_exit(); #endif usb_deregister(&btusb_driver); } diff --git a/drivers/bluetooth/rtk_btusb.h b/drivers/bluetooth/rtk_btusb.h index 7f6222ebba594..e1f78bcf6e106 100644 --- a/drivers/bluetooth/rtk_btusb.h +++ b/drivers/bluetooth/rtk_btusb.h @@ -165,6 +165,7 @@ int mp_drv_mode = 0; /* 1 Mptool Fw; 0 Normal Fw */ #define ROM_LMP_8723b 0x8723 #define ROM_LMP_8821a 0X8821 #define ROM_LMP_8761a 0X8761 +#define ROM_LMP_8761b 0X8761 #define ROM_LMP_8703a 0x8723 #define ROM_LMP_8763a 0x8763 #define ROM_LMP_8703b 0x8703 @@ -172,6 +173,7 @@ int mp_drv_mode = 0; /* 1 Mptool Fw; 0 Normal Fw */ #define ROM_LMP_8822b 0x8822 #define ROM_LMP_8723d 0x8723 #define ROM_LMP_8821c 0x8821 +#define ROM_LMP_8822c 0x8822 /* signature: Realtek */ const uint8_t RTK_EPATCH_SIGNATURE[8] = {0x52,0x65,0x61,0x6C,0x74,0x65,0x63,0x68}; @@ -190,6 +192,10 @@ uint16_t project_id[] = { ROM_LMP_8822b, ROM_LMP_8723d, ROM_LMP_8821c, + ROM_LMP_NONE, + ROM_LMP_NONE, + ROM_LMP_8822c, //0x0d + ROM_LMP_8761b, ROM_LMP_NONE }; struct rtk_eversion_evt { @@ -308,45 +314,16 @@ static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how) #define HCI_BREDR 0x00 #define HCI_AMP 0x01 -/* HCI device flags */ -enum { - HCI_UP, - HCI_INIT, - HCI_RUNNING, - - HCI_PSCAN, - HCI_ISCAN, - HCI_AUTH, - HCI_ENCRYPT, - HCI_INQUIRY, - - HCI_RAW, +#define DRIVER_ON 1 +#define DEVICE_PROBED 2 +#define CHAR_OPENED 4 +#define CHAR_DLFW 8 - HCI_RESET, -}; - -/* - * BR/EDR and/or LE controller flags: the flags defined here should represent - * states from the controller. - */ +/* HCI device flags */ enum { - HCI_SETUP, - HCI_AUTO_OFF, - HCI_MGMT, - HCI_PAIRABLE, - HCI_SERVICE_CACHE, - HCI_LINK_KEYS, - HCI_DEBUG_KEYS, + HCI_UP, //if char device is opened set this flag, clear flag when close + HCI_RUNNING, //if usb transport has opened set this flag, clear flag when close HCI_UNREGISTER, - - HCI_LE_SCAN, - HCI_SSP_ENABLED, - HCI_HS_ENABLED, - HCI_LE_ENABLED, - HCI_CONNECTABLE, - HCI_DISCOVERABLE, - HCI_LINK_SECURITY, - HCI_PENDING_CLASS, }; /* HCI data types */ @@ -470,8 +447,6 @@ struct hci_dev { struct device *parent; struct device dev; - unsigned long dev_flags; - int (*open)(struct hci_dev *hdev); int (*close)(struct hci_dev *hdev); int (*flush)(struct hci_dev *hdev); @@ -591,6 +566,7 @@ static inline void hci_set_drvdata(struct hci_dev *hdev, void *data) #define CONFIG_MAC_OFFSET_GEN_1_2 (0x3C) //MAC's OFFSET in config/efuse for realtek generation 1~2 bluetooth chip #define CONFIG_MAC_OFFSET_GEN_3PLUS (0x44) //MAC's OFFSET in config/efuse for rtk generation 3+ bluetooth chip +#define CONFIG_MAC_OFFSET_GEN_4PLUS (0x30) //MAC's OFFSET in config/efuse for rtk generation 4+ bluetooth chip /******************************* ** Reasil patch code @@ -608,6 +584,7 @@ static inline void hci_set_drvdata(struct hci_dev *hdev, void *data) #define EVT_HDR_LEN sizeof(struct hci_event_hdr) #define CMD_CMP_LEN sizeof(struct hci_ev_cmd_complete) #define MAX_PATCH_SIZE_24K (1024*24) +#define MAX_PATCH_SIZE_25K (1024*25) #define MAX_PATCH_SIZE_40K (1024*40) enum rtk_endpoit { @@ -659,14 +636,14 @@ typedef struct { } __attribute__((packed)) download_rp; - -//Define ioctl cmd the same as HCIDEVUP in the kernel -#define DOWN_FW_CFG _IOW('H', 201, int) +#define DOWN_FW_CFG _IOW('E', 176, int) #ifdef CONFIG_SCO_OVER_HCI -#define SET_ISO_CFG _IOW('H', 202, int) +#define SET_ISO_CFG _IOW('E', 177, int) #endif -#define GET_USB_INFO _IOW('H', 203, int) -#define RESET_CONTROLLER _IOW('H', 204, int) +#define RESET_CONTROLLER _IOW('E', 178, int) +#define DWFW_CMPLT _IOW('E', 179, int) + +#define GET_USB_INFO _IOR('E', 180, int) /* for altsettings*/ #include