3 * Realtek Bluetooth USB driver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/types.h>
27 #include <linux/sched.h>
28 #include <linux/errno.h>
29 #include <linux/skbuff.h>
30 #include <linux/usb.h>
32 #include <linux/ioctl.h>
34 #include <linux/firmware.h>
35 #include <linux/vmalloc.h>
37 #include <linux/uaccess.h>
38 #include <linux/reboot.h>
40 #include "rtk_btusb.h"
42 #define RTKBT_RELEASE_NAME "20170109_TV_ANDROID_6.x"
43 #define VERSION "4.1.2"
45 #define SUSPNED_DW_FW 0
46 #define SET_WAKEUP_DEVICE 0
49 static spinlock_t queue_lock;
50 static spinlock_t dlfw_lock;
51 static volatile uint16_t dlfw_dis_state = 0;
54 static firmware_info *fw_info_4_suspend = NULL;
57 static patch_info fw_patch_table[] = {
58 /* { vid, pid, lmp_sub_default, lmp_sub, everion, mp_fw_name, fw_name, config_name, fw_cache, fw_len, mac_offset } */
59 { 0x0BDA, 0x1724, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723A */
60 { 0x0BDA, 0x8723, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AE */
61 { 0x0BDA, 0xA723, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AE for LI */
62 { 0x0BDA, 0x0723, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AE */
63 { 0x13D3, 0x3394, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AE for Azurewave*/
65 { 0x0BDA, 0x0724, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AU */
66 { 0x0BDA, 0x8725, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AU */
67 { 0x0BDA, 0x872A, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AU */
68 { 0x0BDA, 0x872B, 0x1200, 0, 0, "mp_rtl8723a_fw", "rtl8723a_fw", "rtl8723a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* 8723AU */
70 { 0x0BDA, 0xb720, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723bu_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BU */
71 { 0x0BDA, 0xb72A, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723bu_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BU */
72 { 0x0BDA, 0xb728, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for LC */
73 { 0x0BDA, 0xb723, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE */
74 { 0x0BDA, 0xb72B, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE */
75 { 0x0BDA, 0xb001, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for HP */
76 { 0x0BDA, 0xb002, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE */
77 { 0x0BDA, 0xb003, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE */
78 { 0x0BDA, 0xb004, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE */
79 { 0x0BDA, 0xb005, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE */
81 { 0x13D3, 0x3410, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for Azurewave */
82 { 0x13D3, 0x3416, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for Azurewave */
83 { 0x13D3, 0x3459, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for Azurewave */
84 { 0x0489, 0xE085, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for Foxconn */
85 { 0x0489, 0xE08B, 0x8723, 0, 0, "mp_rtl8723b_fw", "rtl8723b_fw", "rtl8723b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8723BE for Foxconn */
87 { 0x0BDA, 0x2850, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761au_fw", "rtl8761a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AU */
88 { 0x0BDA, 0xA761, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761au_fw", "rtl8761a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AU only */
89 { 0x0BDA, 0x818B, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761aw8192eu_fw", "rtl8761aw8192eu_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AW + 8192EU */
90 { 0x0BDA, 0x818C, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761aw8192eu_fw", "rtl8761aw8192eu_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AW + 8192EU */
91 { 0x0BDA, 0x8760, 0x8761, 0, 0, "mp_rtl8761a_fw", "rtl8761au8192ee_fw", "rtl8761a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8761AU + 8192EE */
92 { 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 */
93 { 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 */
94 { 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 */
96 { 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 */
97 { 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 */
98 { 0x0BDA, 0x0823, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AU */
99 { 0x13D3, 0x3414, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AE */
100 { 0x13D3, 0x3458, 0x8821, 0, 0, "mp_rtl8821a_fw", "rtl8821a_fw", "rtl8821a_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_1_2, MAX_PATCH_SIZE_24K}, /* RTL8821AE */
101 { 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 */
102 { 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 */
104 { 0x0BDA, 0xB822, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BU */
105 { 0x0BDA, 0xB82C, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BU */
106 { 0x0BDA, 0xb023, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BE */
107 { 0x0BDA, 0xB703, 0x8703, 0, 0, "mp_rtl8723c_fw", "rtl8723c_fw", "rtl8723c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8723CU */
108 /* todo: RTL8703BU */
110 { 0x0BDA, 0xD723, 0x8723, 0, 0, "mp_rtl8723d_fw", "rtl8723d_fw", "rtl8723d_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8723DU */
111 { 0x0BDA, 0xB820, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU */
112 { 0x0BDA, 0xC820, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU */
113 /* todo: RTL8703CU */
115 /* NOTE: must append patch entries above the null entry */
116 { 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0 }
120 struct hci_dev *hdev;
121 struct usb_device *udev;
122 struct usb_interface *intf;
123 struct usb_interface *isoc;
129 struct work_struct work;
130 struct work_struct waker;
132 struct usb_anchor tx_anchor;
133 struct usb_anchor intr_anchor;
134 struct usb_anchor bulk_anchor;
135 struct usb_anchor isoc_anchor;
136 struct usb_anchor deferred;
140 struct usb_endpoint_descriptor *intr_ep;
141 struct usb_endpoint_descriptor *bulk_tx_ep;
142 struct usb_endpoint_descriptor *bulk_rx_ep;
143 struct usb_endpoint_descriptor *isoc_tx_ep;
144 struct usb_endpoint_descriptor *isoc_rx_ep;
148 unsigned int sco_num;
151 //#ifdef CONFIG_HAS_EARLYSUSPEND
153 struct early_suspend early_suspend;
155 struct notifier_block pm_notifier;
156 struct notifier_block reboot_notifier;
158 firmware_info *fw_info;
160 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1)
161 static bool reset_on_close = 0;
164 int download_patch(firmware_info *fw_info, int cached);
166 static inline int check_set_dlfw_state_value(uint16_t change_value)
168 spin_lock(&dlfw_lock);
169 if(!dlfw_dis_state) {
170 dlfw_dis_state = change_value;
172 spin_unlock(&dlfw_lock);
173 return dlfw_dis_state;
176 static inline void set_dlfw_state_value(uint16_t change_value)
178 spin_lock(&dlfw_lock);
179 dlfw_dis_state = change_value;
180 spin_unlock(&dlfw_lock);
184 static int download_suspend_patch(firmware_info *fw_info, int cached);
186 #if SET_WAKEUP_DEVICE
187 static void set_wakeup_device_from_conf(firmware_info *fw_info);
188 int set_wakeup_device(firmware_info* fw_info, uint8_t* wakeup_bdaddr);
191 static void rtk_free( struct btusb_data *data)
193 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1)
199 static struct btusb_data *rtk_alloc(struct usb_interface *intf)
201 struct btusb_data *data;
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1)
203 data = kzalloc(sizeof(*data), GFP_KERNEL);
205 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
210 static void print_acl(struct sk_buff *skb, int direction)
213 uint wlength = skb->len;
214 u16 *handle = (u16 *)(skb->data);
215 u16 len = *(handle+1);
216 u8 *acl_data = (u8 *)(skb->data);
218 RTK_INFO("%s: direction %d, handle %04x, len %d",
219 __func__, direction, *handle, len);
223 static void print_sco(struct sk_buff *skb, int direction)
226 uint wlength = skb->len;
227 u16 *handle = (u16 *)(skb->data);
228 u8 len = *(u8 *)(handle+1);
229 u8 *sco_data =(u8 *)(skb->data);
231 RTKBT_INFO("%s: direction %d, handle %04x, len %d",
232 __func__, direction, *handle, len);
236 static void print_error_command(struct sk_buff *skb)
238 uint wlength = skb->len;
240 u16 *opcode = (u16*)(skb->data);
241 u8 *cmd_data = (u8*)(skb->data);
242 u8 len = *(cmd_data+2);
246 printk("HCI_OP_INQUIRY");
248 case HCI_OP_INQUIRY_CANCEL:
249 printk("HCI_OP_INQUIRY_CANCEL");
251 case HCI_OP_EXIT_PERIODIC_INQ:
252 printk("HCI_OP_EXIT_PERIODIC_INQ");
254 case HCI_OP_CREATE_CONN:
255 printk("HCI_OP_CREATE_CONN");
257 case HCI_OP_DISCONNECT:
258 printk("HCI_OP_DISCONNECT");
260 case HCI_OP_CREATE_CONN_CANCEL:
261 printk("HCI_OP_CREATE_CONN_CANCEL");
263 case HCI_OP_ACCEPT_CONN_REQ:
264 printk("HCI_OP_ACCEPT_CONN_REQ");
266 case HCI_OP_REJECT_CONN_REQ:
267 printk("HCI_OP_REJECT_CONN_REQ");
269 case HCI_OP_AUTH_REQUESTED:
270 printk("HCI_OP_AUTH_REQUESTED");
272 case HCI_OP_SET_CONN_ENCRYPT:
273 printk("HCI_OP_SET_CONN_ENCRYPT");
275 case HCI_OP_REMOTE_NAME_REQ:
276 printk("HCI_OP_REMOTE_NAME_REQ");
278 case HCI_OP_READ_REMOTE_FEATURES:
279 printk("HCI_OP_READ_REMOTE_FEATURES");
281 case HCI_OP_SNIFF_MODE:
282 printk("HCI_OP_SNIFF_MODE");
284 case HCI_OP_EXIT_SNIFF_MODE:
285 printk("HCI_OP_EXIT_SNIFF_MODE");
287 case HCI_OP_SWITCH_ROLE:
288 printk("HCI_OP_SWITCH_ROLE");
290 case HCI_OP_SNIFF_SUBRATE:
291 printk("HCI_OP_SNIFF_SUBRATE");
294 printk("HCI_OP_RESET");
296 case HCI_OP_Write_Extended_Inquiry_Response:
297 printk("HCI_Write_Extended_Inquiry_Response");
304 printk(":%04x,len:%d,", *opcode,len);
305 for (icount = 3; (icount < wlength) && (icount < 24); icount++)
306 printk("%02x ", *(cmd_data+icount));
310 static void print_command(struct sk_buff *skb)
313 print_error_command(skb);
318 /* Global parameters for bt usb char driver */
319 #define BT_CHAR_DEVICE_NAME "rtk_btusb"
320 struct mutex btchr_mutex;
321 static struct sk_buff_head btchr_readq;
322 static wait_queue_head_t btchr_read_wait;
323 static wait_queue_head_t bt_dlfw_wait;
324 static int bt_char_dev_registered;
325 static dev_t bt_devid; /* bt char device number */
326 static struct cdev bt_char_dev; /* bt character device structure */
327 static struct class *bt_char_class; /* device class for usb char driver */
328 static int bt_reset = 0;
329 /* HCI device & lock */
330 DEFINE_RWLOCK(hci_dev_lock);
331 struct hci_dev *ghdev = NULL;
333 static void print_event(struct sk_buff *skb)
336 uint wlength = skb->len;
338 u8 *opcode = (u8*)(skb->data);
339 u8 len = *(opcode+1);
342 case HCI_EV_INQUIRY_COMPLETE:
343 printk("HCI_EV_INQUIRY_COMPLETE");
345 case HCI_EV_INQUIRY_RESULT:
346 printk("HCI_EV_INQUIRY_RESULT");
348 case HCI_EV_CONN_COMPLETE:
349 printk("HCI_EV_CONN_COMPLETE");
351 case HCI_EV_CONN_REQUEST:
352 printk("HCI_EV_CONN_REQUEST");
354 case HCI_EV_DISCONN_COMPLETE:
355 printk("HCI_EV_DISCONN_COMPLETE");
357 case HCI_EV_AUTH_COMPLETE:
358 printk("HCI_EV_AUTH_COMPLETE");
360 case HCI_EV_REMOTE_NAME:
361 printk("HCI_EV_REMOTE_NAME");
363 case HCI_EV_ENCRYPT_CHANGE:
364 printk("HCI_EV_ENCRYPT_CHANGE");
366 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
367 printk("HCI_EV_CHANGE_LINK_KEY_COMPLETE");
369 case HCI_EV_REMOTE_FEATURES:
370 printk("HCI_EV_REMOTE_FEATURES");
372 case HCI_EV_REMOTE_VERSION:
373 printk("HCI_EV_REMOTE_VERSION");
375 case HCI_EV_QOS_SETUP_COMPLETE:
376 printk("HCI_EV_QOS_SETUP_COMPLETE");
378 case HCI_EV_CMD_COMPLETE:
379 printk("HCI_EV_CMD_COMPLETE");
381 case HCI_EV_CMD_STATUS:
382 printk("HCI_EV_CMD_STATUS");
384 case HCI_EV_ROLE_CHANGE:
385 printk("HCI_EV_ROLE_CHANGE");
387 case HCI_EV_NUM_COMP_PKTS:
388 printk("HCI_EV_NUM_COMP_PKTS");
390 case HCI_EV_MODE_CHANGE:
391 printk("HCI_EV_MODE_CHANGE");
393 case HCI_EV_PIN_CODE_REQ:
394 printk("HCI_EV_PIN_CODE_REQ");
396 case HCI_EV_LINK_KEY_REQ:
397 printk("HCI_EV_LINK_KEY_REQ");
399 case HCI_EV_LINK_KEY_NOTIFY:
400 printk("HCI_EV_LINK_KEY_NOTIFY");
402 case HCI_EV_CLOCK_OFFSET:
403 printk("HCI_EV_CLOCK_OFFSET");
405 case HCI_EV_PKT_TYPE_CHANGE:
406 printk("HCI_EV_PKT_TYPE_CHANGE");
408 case HCI_EV_PSCAN_REP_MODE:
409 printk("HCI_EV_PSCAN_REP_MODE");
411 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
412 printk("HCI_EV_INQUIRY_RESULT_WITH_RSSI");
414 case HCI_EV_REMOTE_EXT_FEATURES:
415 printk("HCI_EV_REMOTE_EXT_FEATURES");
417 case HCI_EV_SYNC_CONN_COMPLETE:
418 printk("HCI_EV_SYNC_CONN_COMPLETE");
420 case HCI_EV_SYNC_CONN_CHANGED:
421 printk("HCI_EV_SYNC_CONN_CHANGED");
423 case HCI_EV_SNIFF_SUBRATE:
424 printk("HCI_EV_SNIFF_SUBRATE");
426 case HCI_EV_EXTENDED_INQUIRY_RESULT:
427 printk("HCI_EV_EXTENDED_INQUIRY_RESULT");
429 case HCI_EV_IO_CAPA_REQUEST:
430 printk("HCI_EV_IO_CAPA_REQUEST");
432 case HCI_EV_SIMPLE_PAIR_COMPLETE:
433 printk("HCI_EV_SIMPLE_PAIR_COMPLETE");
435 case HCI_EV_REMOTE_HOST_FEATURES:
436 printk("HCI_EV_REMOTE_HOST_FEATURES");
442 printk(":%02x,len:%d,", *opcode,len);
443 for (icount = 2; (icount < wlength) && (icount < 24); icount++)
444 printk("%02x ", *(opcode+icount));
449 static inline ssize_t usb_put_user(struct sk_buff *skb,
450 char __user *buf, int count)
452 char __user *ptr = buf;
453 int len = min_t(unsigned int, skb->len, count);
455 if (copy_to_user(ptr, skb->data, len))
461 static struct sk_buff *rtk_skb_queue[QUEUE_SIZE];
462 static int rtk_skb_queue_front = 0;
463 static int rtk_skb_queue_rear = 0;
465 static void rtk_enqueue(struct sk_buff *skb)
467 spin_lock(&queue_lock);
468 if (rtk_skb_queue_front == (rtk_skb_queue_rear + 1) % QUEUE_SIZE) {
470 * If queue is full, current solution is to drop
471 * the following entries.
473 RTKBT_WARN("%s: Queue is full, entry will be dropped", __func__);
475 rtk_skb_queue[rtk_skb_queue_rear] = skb;
477 rtk_skb_queue_rear++;
478 rtk_skb_queue_rear %= QUEUE_SIZE;
481 spin_unlock(&queue_lock);
484 static struct sk_buff *rtk_dequeue_try(unsigned int deq_len)
487 struct sk_buff *skb_copy;
489 if (rtk_skb_queue_front == rtk_skb_queue_rear) {
490 RTKBT_WARN("%s: Queue is empty", __func__);
494 skb = rtk_skb_queue[rtk_skb_queue_front];
495 if (deq_len >= skb->len) {
497 rtk_skb_queue_front++;
498 rtk_skb_queue_front %= QUEUE_SIZE;
501 * Return skb addr to be dequeued, and the caller
502 * should free the skb eventually.
506 skb_copy = pskb_copy(skb, GFP_ATOMIC);
507 skb_pull(skb, deq_len);
508 /* Return its copy to be freed */
513 static inline int is_queue_empty(void)
515 return (rtk_skb_queue_front == rtk_skb_queue_rear) ? 1 : 0;
519 * Realtek - Integrate from hci_core.c
522 /* Get HCI device by index.
523 * Device is held on return. */
524 static struct hci_dev *hci_dev_get(int index)
532 /* ---- HCI ioctl helpers ---- */
533 static int hci_dev_open(__u16 dev)
535 struct hci_dev *hdev;
538 RTKBT_DBG("%s: dev %d", __func__, dev);
540 hdev = hci_dev_get(dev);
542 RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__);
546 if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) {
551 if (test_bit(HCI_UP, &hdev->flags)) {
560 static int hci_dev_do_close(struct hci_dev *hdev)
564 /* After this point our queues are empty
565 * and no tasks are scheduled. */
572 static int hci_dev_close(__u16 dev)
574 struct hci_dev *hdev;
576 hdev = hci_dev_get(dev);
578 RTKBT_ERR("%s: failed to get hci dev[Null]", __func__);
582 err = hci_dev_do_close(hdev);
587 static struct hci_dev *hci_alloc_dev(void)
589 struct hci_dev *hdev;
591 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
598 /* Free HCI device */
599 static void hci_free_dev(struct hci_dev *hdev)
604 /* Register HCI device */
605 static int hci_register_dev(struct hci_dev *hdev)
609 RTKBT_DBG("%s: %p name %s bus %d", __func__, hdev, hdev->name, hdev->bus);
610 /* Do not allow HCI_AMP devices to register at index 0,
611 * so the index can be used as the AMP controller ID.
613 id = (hdev->dev_type == HCI_BREDR) ? 0 : 1;
615 write_lock(&hci_dev_lock);
617 sprintf(hdev->name, "hci%d", id);
621 mutex_init(&hdev->lock);
623 RTKBT_DBG("%s: id %d, name %s", __func__, hdev->id, hdev->name);
626 for (i = 0; i < NUM_REASSEMBLY; i++)
627 hdev->reassembly[i] = NULL;
629 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
630 atomic_set(&hdev->promisc, 0);
633 write_unlock(&hci_dev_lock);
634 RTKBT_ERR("%s: Hci device has been registered already", __func__);
639 write_unlock(&hci_dev_lock);
644 /* Unregister HCI device */
645 static void hci_unregister_dev(struct hci_dev *hdev)
649 RTKBT_DBG("%s: hdev %p name %s bus %d", __func__, hdev, hdev->name, hdev->bus);
650 set_bit(HCI_UNREGISTER, &hdev->dev_flags);
652 write_lock(&hci_dev_lock);
654 write_unlock(&hci_dev_lock);
656 hci_dev_do_close(hdev);
657 for (i = 0; i < NUM_REASSEMBLY; i++)
658 kfree_skb(hdev->reassembly[i]);
661 static void hci_send_to_stack(struct hci_dev *hdev, struct sk_buff *skb)
663 struct sk_buff *rtk_skb_copy = NULL;
665 RTKBT_DBG("%s", __func__);
668 RTKBT_ERR("%s: Frame for unknown HCI device", __func__);
672 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
673 RTKBT_ERR("%s: HCI not running", __func__);
677 rtk_skb_copy = pskb_copy(skb, GFP_ATOMIC);
679 RTKBT_ERR("%s: Copy skb error", __func__);
683 memcpy(skb_push(rtk_skb_copy, 1), &bt_cb(skb)->pkt_type, 1);
684 rtk_enqueue(rtk_skb_copy);
686 /* Make sure bt char device existing before wakeup read queue */
687 hdev = hci_dev_get(0);
689 RTKBT_DBG("%s: Try to wakeup read queue", __func__);
690 wake_up_interruptible(&btchr_read_wait);
696 /* Receive frame from HCI drivers */
697 static int hci_recv_frame(struct sk_buff *skb)
699 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
702 (!test_bit(HCI_UP, &hdev->flags) && !test_bit(HCI_INIT, &hdev->flags))) {
708 bt_cb(skb)->incoming = 1;
711 __net_timestamp(skb);
713 if (atomic_read(&hdev->promisc)) {
714 /* Send copy to the sockets */
715 hci_send_to_stack(hdev, skb);
721 static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
722 int count, __u8 index)
728 struct bt_skb_cb *scb;
730 RTKBT_DBG("%s", __func__);
732 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
733 index >= NUM_REASSEMBLY)
736 skb = hdev->reassembly[index];
740 case HCI_ACLDATA_PKT:
741 len = HCI_MAX_FRAME_SIZE;
742 hlen = HCI_ACL_HDR_SIZE;
745 len = HCI_MAX_EVENT_SIZE;
746 hlen = HCI_EVENT_HDR_SIZE;
748 case HCI_SCODATA_PKT:
749 len = HCI_MAX_SCO_SIZE;
750 hlen = HCI_SCO_HDR_SIZE;
754 skb = bt_skb_alloc(len, GFP_ATOMIC);
758 scb = (void *) skb->cb;
760 scb->pkt_type = type;
762 skb->dev = (void *) hdev;
763 hdev->reassembly[index] = skb;
767 scb = (void *) skb->cb;
768 len = min_t(uint, scb->expect, count);
770 memcpy(skb_put(skb, len), data, len);
779 if (skb->len == HCI_EVENT_HDR_SIZE) {
780 struct hci_event_hdr *h = hci_event_hdr(skb);
781 scb->expect = h->plen;
783 if (skb_tailroom(skb) < scb->expect) {
785 hdev->reassembly[index] = NULL;
791 case HCI_ACLDATA_PKT:
792 if (skb->len == HCI_ACL_HDR_SIZE) {
793 struct hci_acl_hdr *h = hci_acl_hdr(skb);
794 scb->expect = __le16_to_cpu(h->dlen);
796 if (skb_tailroom(skb) < scb->expect) {
798 hdev->reassembly[index] = NULL;
804 case HCI_SCODATA_PKT:
805 if (skb->len == HCI_SCO_HDR_SIZE) {
806 struct hci_sco_hdr *h = hci_sco_hdr(skb);
807 scb->expect = h->dlen;
809 if (skb_tailroom(skb) < scb->expect) {
811 hdev->reassembly[index] = NULL;
818 if (scb->expect == 0) {
820 if(HCI_ACLDATA_PKT == type)
822 if(HCI_SCODATA_PKT == type)
824 if(HCI_EVENT_PKT == type)
827 bt_cb(skb)->pkt_type = type;
830 hdev->reassembly[index] = NULL;
838 static int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
842 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
846 rem = hci_reassembly(hdev, type, data, count, type - 1);
850 data += (count - rem);
857 void hci_hardware_error(void)
859 struct sk_buff *rtk_skb_copy = NULL;
861 uint8_t hardware_err_pkt[4] = {HCI_EVENT_PKT, 0x10, 0x01, HCI_VENDOR_USB_DISC_HARDWARE_ERROR};
863 rtk_skb_copy = alloc_skb(len, GFP_ATOMIC);
865 RTKBT_ERR("%s: Failed to allocate mem", __func__);
869 memcpy(skb_put(rtk_skb_copy, len), hardware_err_pkt, len);
870 rtk_enqueue(rtk_skb_copy);
872 wake_up_interruptible(&btchr_read_wait);
875 static int btchr_open(struct inode *inode_p, struct file *file_p)
877 struct btusb_data *data;
878 struct hci_dev *hdev;
880 RTKBT_INFO("%s: BT usb char device is opening", __func__);
881 /* Not open unless wanna tracing log */
882 /* trace_printk("%s: open....\n", __func__); */
884 hdev = hci_dev_get(0);
886 RTKBT_ERR("%s: Failed to get hci dev[NULL]", __func__);
889 data = GET_DRV_DATA(hdev);
891 atomic_inc(&hdev->promisc);
893 * As bt device is not re-opened when hotplugged out, we cannot
894 * trust on file's private data(may be null) when other file ops
897 file_p->private_data = data;
899 mutex_lock(&btchr_mutex);
901 mutex_unlock(&btchr_mutex);
903 return nonseekable_open(inode_p, file_p);
906 static int btchr_close(struct inode *inode_p, struct file *file_p)
908 struct btusb_data *data;
909 struct hci_dev *hdev;
911 RTKBT_INFO("%s: BT usb char device is closing", __func__);
912 /* Not open unless wanna tracing log */
913 /* trace_printk("%s: close....\n", __func__); */
915 data = file_p->private_data;
916 file_p->private_data = NULL;
920 * If the upper layer closes bt char interfaces, no reset
921 * action required even bt device hotplugged out.
926 hdev = hci_dev_get(0);
928 atomic_set(&hdev->promisc, 0);
929 mutex_lock(&btchr_mutex);
931 mutex_unlock(&btchr_mutex);
937 static ssize_t btchr_read(struct file *file_p,
942 struct hci_dev *hdev;
946 RTKBT_DBG("%s: BT usb char device is reading", __func__);
949 hdev = hci_dev_get(0);
952 * Note: Only when BT device hotplugged out, we wil get
953 * into such situation. In order to keep the upper layer
954 * stack alive (blocking the read), we should never return
955 * EFAULT or break the loop.
957 RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__);
960 ret = wait_event_interruptible(btchr_read_wait, !is_queue_empty());
962 RTKBT_ERR("%s: wait event is signaled %d", __func__, (int)ret);
966 skb = rtk_dequeue_try(count);
968 ret = usb_put_user(skb, buf_p, count);
970 RTKBT_ERR("%s: Failed to put data to user space", __func__);
979 static ssize_t btchr_write(struct file *file_p,
980 const char __user *buf_p,
984 struct btusb_data *data = file_p->private_data;
985 struct hci_dev *hdev;
988 RTKBT_DBG("%s: BT usb char device is writing", __func__);
990 hdev = hci_dev_get(0);
992 RTKBT_WARN("%s: Failed to get hci dev[Null]", __func__);
994 * Note: we bypass the data from the upper layer if bt device
995 * is hotplugged out. Fortunatelly, H4 or H5 HCI stack does
996 * NOT check btchr_write's return value. However, returning
997 * count instead of EFAULT is preferable.
999 /* return -EFAULT; */
1003 /* Never trust on btusb_data, as bt device may be hotplugged out */
1004 data = GET_DRV_DATA(hdev);
1006 RTKBT_WARN("%s: Failed to get bt usb driver data[Null]", __func__);
1010 if (count > HCI_MAX_FRAME_SIZE)
1013 skb = bt_skb_alloc(count, GFP_ATOMIC);
1016 skb_reserve(skb, -1); // Add this line
1018 if (copy_from_user(skb_put(skb, count), buf_p, count)) {
1019 RTKBT_ERR("%s: Failed to get data from user space", __func__);
1024 skb->dev = (void *)hdev;
1025 bt_cb(skb)->pkt_type = *((__u8 *)skb->data);
1027 data->hdev->send(skb);
1032 static unsigned int btchr_poll(struct file *file_p, poll_table *wait)
1034 struct btusb_data *data = file_p->private_data;
1035 struct hci_dev *hdev;
1037 RTKBT_DBG("%s: BT usb char device is polling", __func__);
1039 poll_wait(file_p, &btchr_read_wait, wait);
1041 hdev = hci_dev_get(0);
1043 RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__);
1044 mdelay(URB_CANCELING_DELAY_MS);
1045 return POLLOUT | POLLWRNORM;
1048 /* Never trust on btusb_data, as bt device may be hotplugged out */
1049 data = GET_DRV_DATA(hdev);
1052 * When bt device is hotplugged out, btusb_data will
1053 * be freed in disconnect.
1055 RTKBT_ERR("%s: Failed to get bt usb driver data[Null]", __func__);
1056 mdelay(URB_CANCELING_DELAY_MS);
1057 return POLLOUT | POLLWRNORM;
1060 if (!is_queue_empty())
1061 return POLLIN | POLLRDNORM;
1063 return POLLOUT | POLLWRNORM;
1065 static long btchr_ioctl(struct file *file_p,unsigned int cmd, unsigned long arg){
1067 struct hci_dev *hdev;
1068 struct btusb_data *data;
1069 firmware_info *fw_info;
1071 if(check_set_dlfw_state_value(1) != 1) {
1072 RTKBT_ERR("%s bt controller is disconnecting!", __func__);
1076 hdev = hci_dev_get(0);
1078 RTKBT_ERR("%s device is NULL!", __func__);
1079 set_dlfw_state_value(0);
1082 data = GET_DRV_DATA(hdev);
1083 fw_info = data->fw_info;
1085 RTKBT_INFO(" btchr_ioctl DOWN_FW_CFG with Cmd:%d",cmd);
1088 ret = usb_autopm_get_interface(data->intf);
1093 ret = download_patch(fw_info,1);
1094 usb_autopm_put_interface(data->intf);
1096 RTKBT_ERR("%s:Failed in download_patch with ret:%d",__func__,ret);
1100 ret = hdev->open(hdev);
1102 RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret);
1105 set_bit(HCI_UP, &hdev->flags);
1106 set_dlfw_state_value(0);
1107 wake_up_interruptible(&bt_dlfw_wait);
1110 RTKBT_ERR("%s:Failed with wrong Cmd:%d",__func__,cmd);
1114 set_dlfw_state_value(0);
1115 wake_up_interruptible(&bt_dlfw_wait);
1122 static struct file_operations bt_chrdev_ops = {
1124 release : btchr_close,
1126 write : btchr_write,
1128 unlocked_ioctl : btchr_ioctl,
1131 static int btchr_init(void)
1136 RTKBT_INFO("Register usb char device interface for BT driver");
1138 * btchr mutex is used to sync between
1139 * 1) downloading patch and opening bt char driver
1140 * 2) the file operations of bt char driver
1142 mutex_init(&btchr_mutex);
1144 skb_queue_head_init(&btchr_readq);
1145 init_waitqueue_head(&btchr_read_wait);
1146 init_waitqueue_head(&bt_dlfw_wait);
1148 bt_char_class = class_create(THIS_MODULE, BT_CHAR_DEVICE_NAME);
1149 if (IS_ERR(bt_char_class)) {
1150 RTKBT_ERR("Failed to create bt char class");
1151 return PTR_ERR(bt_char_class);
1154 res = alloc_chrdev_region(&bt_devid, 0, 1, BT_CHAR_DEVICE_NAME);
1156 RTKBT_ERR("Failed to allocate bt char device");
1160 dev = device_create(bt_char_class, NULL, bt_devid, NULL, BT_CHAR_DEVICE_NAME);
1162 RTKBT_ERR("Failed to create bt char device");
1167 cdev_init(&bt_char_dev, &bt_chrdev_ops);
1168 res = cdev_add(&bt_char_dev, bt_devid, 1);
1170 RTKBT_ERR("Failed to add bt char device");
1177 device_destroy(bt_char_class, bt_devid);
1179 unregister_chrdev_region(bt_devid, 1);
1181 class_destroy(bt_char_class);
1185 static void btchr_exit(void)
1187 RTKBT_INFO("Unregister usb char device interface for BT driver");
1189 device_destroy(bt_char_class, bt_devid);
1190 cdev_del(&bt_char_dev);
1191 unregister_chrdev_region(bt_devid, 1);
1192 class_destroy(bt_char_class);
1198 int send_hci_cmd(firmware_info *fw_info)
1202 while((ret_val<0)&&(i++<10))
1204 ret_val = usb_control_msg(
1205 fw_info->udev, fw_info->pipe_out,
1206 0, USB_TYPE_CLASS, 0, 0,
1207 (void *)(fw_info->send_pkt),
1208 fw_info->pkt_len, MSG_TO);
1213 int rcv_hci_evt(firmware_info *fw_info)
1215 int ret_len = 0, ret_val = 0;
1219 for(i = 0; i < 5; i++) {
1220 ret_val = usb_interrupt_msg(
1221 fw_info->udev, fw_info->pipe_in,
1222 (void *)(fw_info->rcv_pkt), PKT_LEN,
1231 if (CMD_CMP_EVT == fw_info->evt_hdr->evt) {
1232 if (fw_info->cmd_hdr->opcode == fw_info->cmd_cmp->opcode)
1238 int set_bt_onoff(firmware_info *fw_info, uint8_t onoff)
1240 patch_info *patch_entry;
1243 RTKBT_INFO("%s: %s", __func__, onoff != 0 ? "on" : "off");
1245 patch_entry = fw_info->patch_entry;
1249 fw_info->cmd_hdr->opcode = cpu_to_le16(BTOFF_OPCODE);
1250 fw_info->cmd_hdr->plen = 1;
1251 fw_info->pkt_len = CMD_HDR_LEN + 1;
1252 fw_info->send_pkt[CMD_HDR_LEN] = onoff;
1254 ret_val = send_hci_cmd(fw_info);
1256 RTKBT_ERR("%s: Failed to send bt %s cmd, errno %d",
1257 __func__, onoff != 0 ? "on" : "off", ret_val);
1261 ret_val = rcv_hci_evt(fw_info);
1263 RTKBT_ERR("%s: Failed to receive bt %s event, errno %d",
1264 __func__, onoff != 0 ? "on" : "off", ret_val);
1271 static patch_info *get_fw_table_entry(struct usb_device* udev)
1273 patch_info *patch_entry = fw_patch_table;
1274 uint16_t vid = le16_to_cpu(udev->descriptor.idVendor);
1275 uint16_t pid = le16_to_cpu(udev->descriptor.idProduct);
1276 uint32_t entry_size = sizeof(fw_patch_table) / sizeof(fw_patch_table[0]);
1279 RTKBT_INFO("%s: Product id = 0x%04x, fw table entry size %d", __func__, pid, entry_size);
1281 for (i = 0; i < entry_size; i++, patch_entry++) {
1282 if ((vid == patch_entry->vid)&&(pid == patch_entry->pid))
1286 if (i == entry_size) {
1287 RTKBT_ERR("%s: No fw table entry found", __func__);
1295 static patch_info *get_suspend_fw_table_entry(struct usb_device* udev)
1297 patch_info *patch_entry = fw_patch_table;
1298 uint16_t vid = le16_to_cpu(udev->descriptor.idVendor);
1299 uint16_t pid = le16_to_cpu(udev->descriptor.idProduct);
1300 uint32_t entry_size = sizeof(fw_patch_table) / sizeof(fw_patch_table[0]);
1303 RTKBT_INFO("%s: Product id = 0x%04x, fw table entry size %d", __func__, pid, entry_size);
1305 for (i = 0; i < entry_size; i++, patch_entry++) {
1306 if ((vid == patch_entry->vid)&&(pid == patch_entry->pid))
1310 if (i == entry_size) {
1311 RTKBT_ERR("%s: No fw table entry found", __func__);
1319 static struct rtk_epatch_entry *get_fw_patch_entry(struct rtk_epatch *epatch_info, uint16_t eco_ver)
1321 int patch_num = epatch_info->number_of_total_patch;
1322 uint8_t *epatch_buf = (uint8_t *)epatch_info;
1323 struct rtk_epatch_entry *p_entry = NULL;
1328 for (i = 0; i < patch_num; i++) {
1329 if (*(uint16_t *)(epatch_buf + 14 + 2*i) == eco_ver + 1) {
1330 p_entry = kzalloc(sizeof(*p_entry), GFP_KERNEL);
1332 RTKBT_ERR("%s: Failed to allocate mem for patch entry", __func__);
1335 p_entry->chip_id = eco_ver + 1;
1336 p_entry->patch_length = *(uint16_t*)(epatch_buf + 14 + 2*patch_num + 2*i);
1337 p_entry->start_offset = *(uint32_t*)(epatch_buf + 14 + 4*patch_num + 4*i);
1338 p_entry->coex_version = *(uint32_t*)(epatch_buf + p_entry->start_offset + p_entry->patch_length - 12);
1339 p_entry->svn_version = *(uint32_t*)(epatch_buf + p_entry->start_offset + p_entry->patch_length - 8);
1340 p_entry->fw_version = *(uint32_t*)(epatch_buf + p_entry->start_offset + p_entry->patch_length - 4);
1342 coex_date = ((p_entry->coex_version >> 16) & 0x7ff) + ((p_entry->coex_version >> 27) * 10000);
1343 coex_ver = p_entry->coex_version & 0xffff;
1345 RTKBT_INFO("BTCOEX:20%06d-0x%04x svn version:0x%08x fw version:0x%08x rtk_btusb version:%s Cut:%d, patch length:0x%04x, patch offset:0x%08x\n", \
1346 coex_date, coex_ver, p_entry->svn_version, p_entry->fw_version, VERSION, p_entry->chip_id, p_entry->patch_length, p_entry->start_offset);
1354 /*reset_controller is aimed to reset_bt_fw before updata Fw patch*/
1355 int reset_controller(firmware_info* fw_info)
1358 RTKBT_ERR("reset_controller");
1363 fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_FORCE_RESET_AND_PATCHABLE);
1364 fw_info->cmd_hdr->plen = 0;
1365 fw_info->pkt_len = CMD_HDR_LEN;
1366 ret_val = send_hci_cmd(fw_info);
1369 RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1370 __func__, fw_info->cmd_hdr->opcode, ret_val);
1374 //sleep 1s for firmware reset.
1376 RTKBT_INFO("%s: Wait fw reset for 1ms",__func__);
1380 /*reset_controller is aimed to reset_bt_fw before updata Fw patch*/
1383 * check the return value
1384 * 1: need to download fw patch
1385 * 0: no need to download fw patch
1386 * <0: failed to check lmp version
1388 int check_fw_version(firmware_info* fw_info)
1390 struct hci_rp_read_local_version *read_ver_rsp;
1391 patch_info *patch_entry = NULL;
1394 fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_OP_READ_LOCAL_VERSION);
1395 fw_info->cmd_hdr->plen = 0;
1396 fw_info->pkt_len = CMD_HDR_LEN;
1398 ret_val = send_hci_cmd(fw_info);
1400 RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1401 __func__, fw_info->cmd_hdr->opcode, ret_val);
1405 ret_val = rcv_hci_evt(fw_info);
1407 RTKBT_ERR("%s: Failed to receive hci event, errno %d",
1412 patch_entry = fw_info->patch_entry;
1413 read_ver_rsp = (struct hci_rp_read_local_version *)(fw_info->rsp_para);
1415 RTKBT_INFO("%s: Controller lmp = 0x%04x, patch lmp = 0x%04x, default patch lmp = 0x%04x",
1416 __func__, read_ver_rsp->lmp_subver, patch_entry->lmp_sub, patch_entry->lmp_sub_default);
1418 if (read_ver_rsp->lmp_subver == patch_entry->lmp_sub_default) {
1419 RTKBT_INFO("%s: Cold BT controller startup", __func__);
1423 } else if (read_ver_rsp->lmp_subver != patch_entry->lmp_sub) {
1424 RTKBT_INFO("%s: Warm BT controller startup with updated lmp", __func__);
1427 RTKBT_INFO("%s: Warm BT controller startup with same lmp", __func__);
1432 #if SET_WAKEUP_DEVICE
1433 int set_wakeup_device(firmware_info* fw_info, uint8_t* wakeup_bdaddr)
1435 struct rtk_eversion_evt *ever_evt;
1441 fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_ADD_WAKE_UP_DEVICE);
1442 fw_info->cmd_hdr->plen = 7;
1443 memcpy(fw_info->req_para, wakeup_bdaddr, 7);
1444 fw_info->pkt_len = CMD_HDR_LEN + 7;
1446 ret_val = send_hci_cmd(fw_info);
1448 RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d\n",
1449 __func__, fw_info->cmd_hdr->opcode, ret_val);
1453 ret_val = rcv_hci_evt(fw_info);
1455 RTKBT_ERR("%s: Failed to receive hci event, errno %d\n",__func__, ret_val);
1459 ever_evt = (struct rtk_eversion_evt *)(fw_info->rsp_para);
1461 RTKBT_DBG("%s: status %d, eversion %d", __func__, ever_evt->status, ever_evt->version);
1466 /*reset_channel to recover the communication between wifi 8192eu with 8761 bt controller in case of geteversion error*/
1468 int reset_channel(firmware_info* fw_info)
1470 struct rtk_reset_evt *ever_evt;
1476 fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_RESET);
1477 fw_info->cmd_hdr->plen = 0;
1478 fw_info->pkt_len = CMD_HDR_LEN;
1480 ret_val = send_hci_cmd(fw_info);
1482 RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1483 __func__, fw_info->cmd_hdr->opcode, ret_val);
1487 ret_val = rcv_hci_evt(fw_info);
1489 RTKBT_ERR("%s: Failed to receive hci event, errno %d",
1494 ever_evt = (struct rtk_reset_evt *)(fw_info->rsp_para);
1496 RTKBT_INFO("%s: status %d ", __func__, ever_evt->status);
1498 //sleep 300ms for channel reset.
1500 RTKBT_INFO("%s: Wait channel reset for 300ms",__func__);
1505 int read_localversion(firmware_info* fw_info)
1507 struct rtk_localversion_evt *ever_evt;
1513 fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_READ_LMP_VERISION);
1514 fw_info->cmd_hdr->plen = 0;
1515 fw_info->pkt_len = CMD_HDR_LEN;
1517 ret_val = send_hci_cmd(fw_info);
1519 RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1520 __func__, fw_info->cmd_hdr->opcode, ret_val);
1524 ret_val = rcv_hci_evt(fw_info);
1526 RTKBT_ERR("%s: Failed to receive hci event, errno %d",
1531 ever_evt = (struct rtk_localversion_evt *)(fw_info->rsp_para);
1533 RTKBT_INFO("%s: status %d ", __func__, ever_evt->status);
1534 RTKBT_INFO("%s: hci_version %d ", __func__, ever_evt->hci_version);
1535 RTKBT_INFO("%s: hci_revision %d ", __func__, ever_evt->hci_revision);
1536 RTKBT_INFO("%s: lmp_version %d ", __func__, ever_evt->lmp_version);
1537 RTKBT_INFO("%s: lmp_subversion %d ", __func__, ever_evt->lmp_subversion);
1538 RTKBT_INFO("%s: lmp_manufacture %d ", __func__, ever_evt->lmp_manufacture);
1539 //sleep 300ms for channel reset.
1541 RTKBT_INFO("%s: Wait channel reset for 300ms",__func__);
1546 int get_eversion(firmware_info* fw_info)
1548 struct rtk_eversion_evt *ever_evt;
1554 fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_READ_RTK_ROM_VERISION);
1555 fw_info->cmd_hdr->plen = 0;
1556 fw_info->pkt_len = CMD_HDR_LEN;
1558 ret_val = send_hci_cmd(fw_info);
1560 RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1561 __func__, fw_info->cmd_hdr->opcode, ret_val);
1565 ret_val = rcv_hci_evt(fw_info);
1567 RTKBT_ERR("%s: Failed to receive hci event, errno %d",
1572 ever_evt = (struct rtk_eversion_evt *)(fw_info->rsp_para);
1574 RTKBT_INFO("%s: status %d, eversion %d", __func__, ever_evt->status, ever_evt->version);
1576 if (ever_evt->status)
1577 fw_info->patch_entry->eversion = 0;
1579 fw_info->patch_entry->eversion = ever_evt->version;
1584 void rtk_update_altsettings(patch_info *patch_entry, const unsigned char* org_config_buf, int org_config_len, unsigned char ** new_config_buf_ptr, int *new_config_len_ptr)
1586 static unsigned char config_buf[1024];
1587 unsigned short offset[256];
1588 unsigned char val[256];
1590 struct rtk_bt_vendor_config* config = (struct rtk_bt_vendor_config*) config_buf;
1591 struct rtk_bt_vendor_config_entry* entry = config->entry;
1593 int count = 0,temp = 0, i = 0, j;
1595 memset(config_buf, 0, sizeof(config_buf));
1596 memset(offset, 0, sizeof(offset));
1597 memset(val, 0, sizeof(val));
1599 memcpy(config_buf, org_config_buf, org_config_len);
1600 *new_config_buf_ptr = config_buf;
1601 *new_config_len_ptr = org_config_len;
1603 count = getAltSettings(patch_entry, offset, sizeof(offset)/sizeof(unsigned short));
1605 RTKBT_INFO("rtk_update_altsettings: No AltSettings");
1608 RTKBT_INFO("rtk_update_altsettings: %d AltSettings", count);
1611 RTKBT_INFO("ORG Config len=%08x:\n", org_config_len);
1612 for(i=0;i<=org_config_len;i+=0x10)
1614 RTKBT_INFO("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, \
1615 config_buf[i], config_buf[i+1], config_buf[i+2], config_buf[i+3], config_buf[i+4], config_buf[i+5], config_buf[i+6], config_buf[i+7], \
1616 config_buf[i+8], config_buf[i+9], config_buf[i+10], config_buf[i+11], config_buf[i+12], config_buf[i+13], config_buf[i+14], config_buf[i+15]);
1619 if (config->data_len != org_config_len - sizeof(struct rtk_bt_vendor_config))
1621 RTKBT_ERR("rtk_update_altsettings: config len(%x) is not right(%x)", config->data_len, (unsigned int)(org_config_len-sizeof(struct rtk_bt_vendor_config)));
1625 for (i=0; i<config->data_len;)
1627 for(j = 0; j < count;j++)
1629 if(entry->offset == offset[j])
1632 if(getAltSettingVal(patch_entry, entry->offset, val) == entry->entry_len){
1633 RTKBT_INFO("rtk_update_altsettings: replace %04x[%02x]", entry->offset, entry->entry_len);
1634 memcpy(entry->entry_data, val, entry->entry_len);
1636 temp = entry->entry_len + sizeof(struct rtk_bt_vendor_config_entry);
1638 entry = (struct rtk_bt_vendor_config_entry*)((uint8_t*)entry + temp);
1640 for(j = 0; j < count;j++){
1643 entry->entry_len = getAltSettingVal(patch_entry, offset[j], val);
1644 if(entry->entry_len <= 0)
1646 entry->offset = offset[j];
1647 memcpy(entry->entry_data, val, entry->entry_len);
1648 RTKBT_INFO("rtk_update_altsettings: add %04x[%02x]", entry->offset, entry->entry_len);
1649 temp = entry->entry_len + sizeof(struct rtk_bt_vendor_config_entry);
1651 entry = (struct rtk_bt_vendor_config_entry*)((uint8_t*)entry + temp);
1653 config->data_len = i;
1654 *new_config_buf_ptr = config_buf;
1655 *new_config_len_ptr = config->data_len+sizeof(struct rtk_bt_vendor_config);
1660 int load_firmware(firmware_info *fw_info, uint8_t **buff)
1662 const struct firmware *fw, *cfg;
1663 struct usb_device *udev;
1664 patch_info *patch_entry;
1665 char *config_name, *fw_name;
1669 int config_len = 0, buf_len = -1;
1670 uint8_t *buf = *buff, *config_file_buf = NULL;
1671 uint8_t *epatch_buf = NULL;
1673 struct rtk_epatch *epatch_info = NULL;
1674 uint8_t need_download_fw = 1;
1675 struct rtk_extension_entry patch_lmp = {0};
1676 struct rtk_epatch_entry *p_epatch_entry = NULL;
1677 uint16_t lmp_version;
1678 //uint8_t use_mp_fw = 0;
1679 RTKBT_DBG("%s: start", __func__);
1681 udev = fw_info->udev;
1682 patch_entry = fw_info->patch_entry;
1683 lmp_version = patch_entry->lmp_sub_default;
1684 config_name = patch_entry->config_name;
1685 /* 1 Mptool Fw; 0 Normal Fw */
1686 if(DRV_MP_MODE == mp_drv_mode){
1687 fw_name = patch_entry->mp_patch_name;
1689 fw_name = patch_entry->patch_name;
1692 RTKBT_INFO("%s: Default lmp version = 0x%04x, config file name[%s], "
1693 "fw file name[%s]", __func__, lmp_version,config_name, fw_name);
1695 ret_val = request_firmware(&cfg, config_name, &udev->dev);
1700 rtk_update_altsettings(patch_entry, cfg->data, cfg->size, &config_file_buf, &config_len);
1702 RTKBT_INFO("Final Config len=%08x:\n", config_len);
1703 for(i=0;i<=config_len;i+=0x10)
1705 RTKBT_INFO("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, \
1706 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], \
1707 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]);
1710 release_firmware(cfg);
1713 ret_val = request_firmware(&fw, fw_name, &udev->dev);
1717 epatch_buf = vmalloc(fw->size);
1718 RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__);
1720 release_firmware(fw);
1723 memcpy(epatch_buf, fw->data, fw->size);
1725 buf_len = fw_len + config_len;
1726 release_firmware(fw);
1729 if (lmp_version == ROM_LMP_8723a) {
1730 RTKBT_DBG("%s: 8723a -> use old style patch", __func__);
1731 if (!memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) {
1732 RTKBT_ERR("%s: 8723a check signature error", __func__);
1733 need_download_fw = 0;
1735 if (!(buf = kzalloc(buf_len, GFP_KERNEL))) {
1736 RTKBT_ERR("%s: Failed to allocate mem for fw&config", __func__);
1739 RTKBT_DBG("%s: 8723a -> fw copy directly", __func__);
1740 memcpy(buf, epatch_buf, buf_len);
1741 patch_entry->lmp_sub = *(uint16_t *)(buf + buf_len - config_len - 4);
1742 RTKBT_DBG("%s: Config lmp version = 0x%04x", __func__,
1743 patch_entry->lmp_sub);
1745 RTKBT_INFO("%s:ROM_LMP_8723a vfree(epatch_buf)", __func__);
1748 memcpy(buf + buf_len - config_len, config_file_buf, config_len);
1752 RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__);
1754 RTKBT_DBG("%s: reset_channel before get_eversion from bt controller", __func__);
1755 ret_val = reset_channel(fw_info);
1757 RTKBT_ERR("%s: Failed to reset_channel, errno %d", __func__, ret_val);
1760 // read_localversion(fw_info);
1761 RTKBT_DBG("%s: get_eversion from bt controller", __func__);
1763 ret_val = get_eversion(fw_info);
1765 RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val);
1768 RTKBT_DBG("%s: Get eversion =%d", __func__, patch_entry->eversion);
1769 if (memcmp(epatch_buf + buf_len - config_len - 4 , EXTENSION_SECTION_SIGNATURE, 4)) {
1770 RTKBT_ERR("%s: Failed to check extension section signature", __func__);
1771 need_download_fw = 0;
1774 temp = epatch_buf+buf_len-config_len - 5;
1776 if (*temp == 0x00) {
1777 patch_lmp.opcode = *temp;
1778 patch_lmp.length = *(temp-1);
1779 if ((patch_lmp.data = kzalloc(patch_lmp.length, GFP_KERNEL))) {
1781 for (k = 0; k < patch_lmp.length; k++) {
1782 *(patch_lmp.data+k) = *(temp-2-k);
1783 RTKBT_DBG("data = 0x%x", *(patch_lmp.data+k));
1786 RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__,
1787 patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data));
1790 temp -= *(temp-1) + 2;
1791 } while (*temp != 0xFF);
1793 if (lmp_version != project_id[*(patch_lmp.data)]) {
1794 RTKBT_ERR("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x "
1795 "-> not match", __func__, lmp_version, *(patch_lmp.data),project_id[*(patch_lmp.data)]);
1797 kfree(patch_lmp.data);
1798 need_download_fw = 0;
1800 RTKBT_INFO("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x "
1801 "-> match", __func__, lmp_version, *(patch_lmp.data), project_id[*(patch_lmp.data)]);
1803 kfree(patch_lmp.data);
1804 if (memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) {
1805 RTKBT_ERR("%s: Check signature error", __func__);
1806 need_download_fw = 0;
1808 epatch_info = (struct rtk_epatch*)epatch_buf;
1809 patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version;
1811 RTKBT_DBG("%s: lmp version 0x%04x, fw_version 0x%x, "
1812 "number_of_total_patch %d", __func__,
1813 patch_entry->lmp_sub, epatch_info->fw_version,
1814 epatch_info->number_of_total_patch);
1816 /* Get right epatch entry */
1817 p_epatch_entry = get_fw_patch_entry(epatch_info, patch_entry->eversion);
1818 if (p_epatch_entry == NULL) {
1819 RTKBT_WARN("%s: Failed to get fw patch entry", __func__);
1824 buf_len = p_epatch_entry->patch_length + config_len;
1825 RTKBT_DBG("buf_len = 0x%x", buf_len);
1827 if (!(buf = kzalloc(buf_len, GFP_KERNEL))) {
1828 RTKBT_ERR("%s: Can't alloc memory for fw&config", __func__);
1831 memcpy(buf, &epatch_buf[p_epatch_entry->start_offset], p_epatch_entry->patch_length);
1832 memcpy(&buf[p_epatch_entry->patch_length-4], &epatch_info->fw_version, 4);
1833 kfree(p_epatch_entry);
1836 RTKBT_INFO("%s: vfree(epatch_buf)", __func__);
1840 memcpy(&buf[buf_len - config_len], config_file_buf, config_len);
1846 RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__,
1847 (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not");
1849 if (buf && buf_len > 0 && need_download_fw)
1852 RTKBT_DBG("%s: done", __func__);
1861 static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff)
1863 const struct firmware *fw, *cfg;
1864 struct usb_device *udev;
1865 patch_info *patch_entry;
1866 char config_name[100] = {0};
1867 char fw_name[100] = {0};
1871 int config_len = 0, buf_len = -1;
1872 uint8_t *buf = *buff, *config_file_buf = NULL;
1873 uint8_t *epatch_buf = NULL;
1875 struct rtk_epatch *epatch_info = NULL;
1876 uint8_t need_download_fw = 1;
1877 struct rtk_extension_entry patch_lmp = {0};
1878 struct rtk_epatch_entry *p_epatch_entry = NULL;
1879 uint16_t lmp_version;
1880 RTKBT_DBG("%s: start", __func__);
1882 udev = fw_info->udev;
1883 patch_entry = fw_info->patch_entry;
1884 lmp_version = patch_entry->lmp_sub_default;
1885 sprintf(config_name, "%s_suspend", patch_entry->config_name);
1886 sprintf(fw_name, "%s_suspend", patch_entry->patch_name);
1888 RTKBT_INFO("%s: Default lmp version = 0x%04x, config file name[%s], "
1889 "fw file name[%s]", __func__, lmp_version,config_name, fw_name);
1891 ret_val = request_firmware(&cfg, config_name, &udev->dev);
1895 config_file_buf = vmalloc(cfg->size);
1896 RTKBT_INFO("%s: epatch_buf = vmalloc(cfg->size)", __func__);
1897 if (!config_file_buf)
1899 memcpy(config_file_buf, cfg->data, cfg->size);
1900 config_len = cfg->size;
1901 release_firmware(cfg);
1904 ret_val = request_firmware(&fw, fw_name, &udev->dev);
1908 epatch_buf = vmalloc(fw->size);
1909 RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__);
1911 release_firmware(fw);
1914 memcpy(epatch_buf, fw->data, fw->size);
1916 buf_len = fw_len + config_len;
1917 release_firmware(fw);
1920 RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__);
1922 RTKBT_DBG("%s: get_eversion from bt controller", __func__);
1924 ret_val = get_eversion(fw_info);
1926 RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val);
1929 RTKBT_DBG("%s: Get eversion =%d", __func__, patch_entry->eversion);
1930 if (memcmp(epatch_buf + buf_len - config_len - 4 , EXTENSION_SECTION_SIGNATURE, 4)) {
1931 RTKBT_ERR("%s: Failed to check extension section signature", __func__);
1932 need_download_fw = 0;
1935 temp = epatch_buf+buf_len-config_len - 5;
1937 if (*temp == 0x00) {
1938 patch_lmp.opcode = *temp;
1939 patch_lmp.length = *(temp-1);
1940 if ((patch_lmp.data = kzalloc(patch_lmp.length, GFP_KERNEL))) {
1942 for (k = 0; k < patch_lmp.length; k++) {
1943 *(patch_lmp.data+k) = *(temp-2-k);
1944 RTKBT_DBG("data = 0x%x", *(patch_lmp.data+k));
1947 RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__,
1948 patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data));
1951 temp -= *(temp-1) + 2;
1952 } while (*temp != 0xFF);
1954 if (lmp_version != project_id[*(patch_lmp.data)]) {
1955 RTKBT_ERR("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x "
1956 "-> not match", __func__, lmp_version, *(patch_lmp.data),project_id[*(patch_lmp.data)]);
1958 kfree(patch_lmp.data);
1959 need_download_fw = 0;
1961 RTKBT_INFO("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x "
1962 "-> match", __func__, lmp_version, *(patch_lmp.data), project_id[*(patch_lmp.data)]);
1964 kfree(patch_lmp.data);
1965 if (memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) {
1966 RTKBT_ERR("%s: Check signature error", __func__);
1967 need_download_fw = 0;
1969 epatch_info = (struct rtk_epatch*)epatch_buf;
1970 patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version;
1972 RTKBT_DBG("%s: lmp version 0x%04x, fw_version 0x%x, "
1973 "number_of_total_patch %d", __func__,
1974 patch_entry->lmp_sub, epatch_info->fw_version,
1975 epatch_info->number_of_total_patch);
1977 /* Get right epatch entry */
1978 p_epatch_entry = get_fw_patch_entry(epatch_info, patch_entry->eversion);
1979 if (p_epatch_entry == NULL) {
1980 RTKBT_WARN("%s: Failed to get fw patch entry", __func__);
1985 buf_len = p_epatch_entry->patch_length + config_len;
1986 RTKBT_DBG("buf_len = 0x%x", buf_len);
1988 if (!(buf = kzalloc(buf_len, GFP_KERNEL))) {
1989 RTKBT_ERR("%s: Can't alloc memory for fw&config", __func__);
1992 memcpy(buf, &epatch_buf[p_epatch_entry->start_offset], p_epatch_entry->patch_length);
1993 memcpy(&buf[p_epatch_entry->patch_length-4], &epatch_info->fw_version, 4);
1994 kfree(p_epatch_entry);
1997 RTKBT_INFO("%s: vfree(epatch_buf)", __func__);
2001 memcpy(&buf[buf_len - config_len], config_file_buf, config_len);
2006 if (config_file_buf){
2007 vfree(config_file_buf);
2008 config_file_buf = NULL;
2009 RTKBT_INFO("%s: vfree(config_file_buf)", __func__);
2012 RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__,
2013 (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not");
2015 if (buf && buf_len > 0 && need_download_fw)
2018 RTKBT_DBG("%s: done", __func__);
2023 if (config_file_buf){
2024 vfree(config_file_buf);
2025 config_file_buf = NULL;
2027 RTKBT_INFO("%s: fw_fail vfree(config_file_buf)", __func__);
2032 int get_firmware(firmware_info *fw_info, int cached)
2034 patch_info *patch_entry = fw_info->patch_entry;
2036 RTKBT_INFO("%s: start, cached %d,patch_entry->fw_len= %d", __func__, cached,patch_entry->fw_len);
2039 if (patch_entry->fw_len > 0) {
2040 fw_info->fw_data = kzalloc(patch_entry->fw_len, GFP_KERNEL);
2041 if (!fw_info->fw_data)
2043 memcpy(fw_info->fw_data, patch_entry->fw_cache, patch_entry->fw_len);
2044 fw_info->fw_len = patch_entry->fw_len;
2046 fw_info->fw_len = load_firmware(fw_info, &fw_info->fw_data);
2047 if (fw_info->fw_len <= 0)
2051 fw_info->fw_len = load_firmware(fw_info, &fw_info->fw_data);
2052 if (fw_info->fw_len <= 0)
2060 static int get_suspend_firmware(firmware_info *fw_info, int cached)
2062 patch_info *patch_entry = fw_info->patch_entry;
2064 RTKBT_INFO("%s: start, cached %d,patch_entry->fw_len= %d", __func__, cached,patch_entry->fw_len);
2067 if (patch_entry->fw_len > 0) {
2068 fw_info->fw_data = kzalloc(patch_entry->fw_len, GFP_KERNEL);
2069 if (!fw_info->fw_data)
2071 memcpy(fw_info->fw_data, patch_entry->fw_cache, patch_entry->fw_len);
2072 fw_info->fw_len = patch_entry->fw_len;
2074 fw_info->fw_len = load_suspend_firmware(fw_info, &fw_info->fw_data);
2075 if (fw_info->fw_len <= 0)
2079 fw_info->fw_len = load_suspend_firmware(fw_info, &fw_info->fw_data);
2080 if (fw_info->fw_len <= 0)
2089 * Open the log message only if in debugging,
2090 * or it will decelerate download procedure.
2092 int download_data(firmware_info *fw_info)
2094 download_cp *cmd_para;
2095 download_rp *evt_para;
2097 int pkt_len, frag_num, frag_len;
2099 int ncmd = 1, step = 1;
2101 RTKBT_DBG("%s: start", __func__);
2103 cmd_para = (download_cp *)fw_info->req_para;
2104 evt_para = (download_rp *)fw_info->rsp_para;
2105 pcur = fw_info->fw_data;
2106 pkt_len = CMD_HDR_LEN + sizeof(download_cp);
2107 frag_num = fw_info->fw_len / PATCH_SEG_MAX + 1;
2108 frag_len = PATCH_SEG_MAX;
2110 for (i = 0; i < frag_num; i++) {
2111 cmd_para->index = i?((i-1)%0x7f+1):0;
2112 if (i == (frag_num - 1)) {
2113 cmd_para->index |= DATA_END;
2114 frag_len = fw_info->fw_len % PATCH_SEG_MAX;
2115 pkt_len -= (PATCH_SEG_MAX - frag_len);
2117 fw_info->cmd_hdr->opcode = cpu_to_le16(DOWNLOAD_OPCODE);
2118 fw_info->cmd_hdr->plen = sizeof(uint8_t) + frag_len;
2119 fw_info->pkt_len = pkt_len;
2120 memcpy(cmd_para->data, pcur, frag_len);
2123 ret_val = send_hci_cmd(fw_info);
2125 RTKBT_DBG("%s: Failed to send frag num %d", __func__, cmd_para->index);
2128 RTKBT_DBG("%s: Send frag num %d", __func__, cmd_para->index);
2130 if (--step > 0 && i < frag_num - 1) {
2131 RTKBT_DBG("%s: Continue to send frag num %d", __func__, cmd_para->index + 1);
2132 pcur += PATCH_SEG_MAX;
2138 ret_val = rcv_hci_evt(fw_info);
2140 RTKBT_ERR("%s: rcv_hci_evt err %d", __func__, ret_val);
2143 RTKBT_DBG("%s: Receive acked frag num %d", __func__, evt_para->index);
2147 if (0 != evt_para->status) {
2148 RTKBT_ERR("%s: Receive acked frag num %d, err status %d",
2149 __func__, ret_val, evt_para->status);
2153 if ((evt_para->index & DATA_END) || (evt_para->index == frag_num - 1)) {
2154 RTKBT_DBG("%s: Receive last acked index %d", __func__, evt_para->index);
2159 ncmd = step = fw_info->cmd_cmp->ncmd;
2160 pcur += PATCH_SEG_MAX;
2161 RTKBT_DBG("%s: HCI command packet num %d", __func__, ncmd);
2165 * It is tricky that Host cannot receive DATA_END index from BT
2166 * controller, at least for 8723au. We are doomed if failed.
2169 /* Continue to receive the responsed events until last index occurs */
2170 if (i == frag_num) {
2171 RTKBT_DBG("%s: total frag count %d", __func__, frag_num);
2172 while (!(evt_para->index & DATA_END)) {
2173 ret_val = rcv_hci_evt(fw_info);
2175 RTKBT_ERR("%s: rcv_hci_evt err %d", __func__, ret_val);
2178 if (0 != evt_para->status)
2180 RTKBT_DBG("%s: continue to receive acked frag num %d", __func__, evt_para->index);
2185 RTKBT_INFO("%s: done, sent %d frag pkts, received %d frag events",
2186 __func__, cmd_para->index, evt_para->index);
2187 return fw_info->fw_len;
2190 int download_patch(firmware_info *fw_info, int cached)
2194 RTKBT_DBG("%s: Download fw patch start, cached %d", __func__, cached);
2196 if (!fw_info || !fw_info->patch_entry) {
2197 RTKBT_ERR("%s: No patch entry exists(fw_info %p)", __func__, fw_info);
2203 * step1: get local firmware if existed
2204 * step2: check firmware version
2205 * step3: download firmware if updated
2207 ret_val = get_firmware(fw_info, cached);
2209 RTKBT_ERR("%s: Failed to get firmware", __func__);
2214 if(fw_info_4_suspend) {
2215 RTKBT_DBG("%s: get suspend fw first cached %d", __func__, cached);
2216 ret_val = get_suspend_firmware(fw_info_4_suspend, cached);
2218 RTKBT_ERR("%s: Failed to get suspend firmware", __func__);
2224 /*check the length of fw to be download*/
2225 RTKBT_DBG("%s: Check fw_info->fw_len:%d max_patch_size %d", __func__, fw_info->fw_len, fw_info->patch_entry->max_patch_size);
2226 if (fw_info->fw_len > fw_info->patch_entry->max_patch_size) {
2227 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);
2232 ret_val = check_fw_version(fw_info);
2235 RTKBT_ERR("%s: Cold reset bt chip only download", __func__);
2236 ret_val = download_data(fw_info);
2238 RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val);
2239 } else if(1 == ret_val){
2240 // reset bt chip to update Fw patch
2241 ret_val = reset_controller(fw_info);
2242 RTKBT_ERR("%s: reset bt chip to update Fw patch, fw len %d", __func__, ret_val);
2243 ret_val = download_data(fw_info);
2245 RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val);
2250 /* Free fw data after download finished */
2251 kfree(fw_info->fw_data);
2252 fw_info->fw_data = NULL;
2259 static int download_suspend_patch(firmware_info *fw_info, int cached)
2263 RTKBT_DBG("%s: Download fw patch start, cached %d", __func__, cached);
2265 if (!fw_info || !fw_info->patch_entry) {
2266 RTKBT_ERR("%s: No patch entry exists(fw_info %p)", __func__, fw_info);
2271 /*check the length of fw to be download*/
2272 RTKBT_DBG("%s:Check RTK_PATCH_LENGTH fw_info->fw_len:%d", __func__,fw_info->fw_len);
2273 if (fw_info->fw_len > RTK_PATCH_LENGTH_MAX || fw_info->fw_len == 0) {
2274 RTKBT_ERR("%s: Total length of fw&config larger than allowed 24K or no fw len:%d", __func__, fw_info->fw_len);
2279 ret_val = check_fw_version(fw_info);
2282 RTKBT_ERR("%s: Cold reset bt chip only download", __func__);
2283 ret_val = download_data(fw_info);
2285 RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val);
2286 } else if(1 == ret_val){
2287 // reset bt chip to update Fw patch
2288 ret_val = reset_controller(fw_info);
2289 RTKBT_ERR("%s: reset bt chip to update Fw patch, fw len %d", __func__, ret_val);
2290 ret_val = download_data(fw_info);
2292 RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val);
2297 /* Free fw data after download finished */
2298 kfree(fw_info->fw_data);
2299 fw_info->fw_data = NULL;
2305 static void suspend_firmware_info_init(firmware_info *fw_info)
2307 RTKBT_DBG("%s: start", __func__);
2311 fw_info_4_suspend= kzalloc(sizeof(*fw_info), GFP_KERNEL);
2312 if (!fw_info_4_suspend)
2315 fw_info_4_suspend->send_pkt = kzalloc(PKT_LEN, GFP_KERNEL);
2316 if (!fw_info_4_suspend->send_pkt) {
2317 kfree(fw_info_4_suspend);
2321 fw_info_4_suspend->rcv_pkt = kzalloc(PKT_LEN, GFP_KERNEL);
2322 if (!fw_info_4_suspend->rcv_pkt) {
2323 kfree(fw_info_4_suspend->send_pkt);
2324 kfree(fw_info_4_suspend);
2328 fw_info_4_suspend->patch_entry = get_suspend_fw_table_entry(fw_info->udev);
2329 if (!fw_info_4_suspend->patch_entry) {
2330 kfree(fw_info_4_suspend->rcv_pkt);
2331 kfree(fw_info_4_suspend->send_pkt);
2332 kfree(fw_info_4_suspend);
2336 fw_info_4_suspend->intf = fw_info->intf;
2337 fw_info_4_suspend->udev = fw_info->udev;
2338 fw_info_4_suspend->cmd_hdr = (struct hci_command_hdr *)(fw_info_4_suspend->send_pkt);
2339 fw_info_4_suspend->evt_hdr = (struct hci_event_hdr *)(fw_info_4_suspend->rcv_pkt);
2340 fw_info_4_suspend->cmd_cmp = (struct hci_ev_cmd_complete *)(fw_info_4_suspend->rcv_pkt + EVT_HDR_LEN);
2341 fw_info_4_suspend->req_para = fw_info_4_suspend->send_pkt + CMD_HDR_LEN;
2342 fw_info_4_suspend->rsp_para = fw_info_4_suspend->rcv_pkt + EVT_HDR_LEN + CMD_CMP_LEN;
2343 fw_info_4_suspend->pipe_in = fw_info->pipe_in;
2344 fw_info_4_suspend->pipe_out = fw_info->pipe_out;
2348 RTKBT_DBG("%s: fail !", __func__);
2349 fw_info_4_suspend = NULL;
2354 #if SET_WAKEUP_DEVICE
2355 static void set_wakeup_device_from_conf(firmware_info *fw_info)
2357 uint8_t paired_wakeup_bdaddr[7];
2364 memset(paired_wakeup_bdaddr, 0, 7);
2365 fp = filp_open(SET_WAKEUP_DEVICE_CONF, O_RDWR, 0);
2371 vfs_read(fp, &num, 1, &pos);
2372 RTKBT_DBG("read number = %d", num);
2374 for(i = 0; i < num; i++) {
2375 vfs_read(fp, paired_wakeup_bdaddr, 7, &pos);
2376 RTKBT_DBG("paired_wakeup_bdaddr: 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x",
2377 paired_wakeup_bdaddr[1],paired_wakeup_bdaddr[2],paired_wakeup_bdaddr[3],
2378 paired_wakeup_bdaddr[4],paired_wakeup_bdaddr[5],paired_wakeup_bdaddr[6]);
2379 set_wakeup_device(fw_info, paired_wakeup_bdaddr);
2382 filp_close(fp, NULL);
2386 RTKBT_ERR("open wakeup config file fail! errno = %ld", PTR_ERR(fp));
2391 firmware_info *firmware_info_init(struct usb_interface *intf)
2393 struct usb_device *udev = interface_to_usbdev(intf);
2394 firmware_info *fw_info;
2396 RTKBT_DBG("%s: start", __func__);
2398 fw_info = kzalloc(sizeof(*fw_info), GFP_KERNEL);
2402 fw_info->send_pkt = kzalloc(PKT_LEN, GFP_KERNEL);
2403 if (!fw_info->send_pkt) {
2408 fw_info->rcv_pkt = kzalloc(PKT_LEN, GFP_KERNEL);
2409 if (!fw_info->rcv_pkt) {
2410 kfree(fw_info->send_pkt);
2415 fw_info->patch_entry = get_fw_table_entry(udev);
2416 if (!fw_info->patch_entry) {
2417 kfree(fw_info->rcv_pkt);
2418 kfree(fw_info->send_pkt);
2423 fw_info->intf = intf;
2424 fw_info->udev = udev;
2425 fw_info->pipe_in = usb_rcvintpipe(fw_info->udev, INTR_EP);
2426 fw_info->pipe_out = usb_sndctrlpipe(fw_info->udev, CTRL_EP);
2427 fw_info->cmd_hdr = (struct hci_command_hdr *)(fw_info->send_pkt);
2428 fw_info->evt_hdr = (struct hci_event_hdr *)(fw_info->rcv_pkt);
2429 fw_info->cmd_cmp = (struct hci_ev_cmd_complete *)(fw_info->rcv_pkt + EVT_HDR_LEN);
2430 fw_info->req_para = fw_info->send_pkt + CMD_HDR_LEN;
2431 fw_info->rsp_para = fw_info->rcv_pkt + EVT_HDR_LEN + CMD_CMP_LEN;
2434 suspend_firmware_info_init(fw_info);
2438 RTKBT_INFO("%s: Auto suspend is enabled", __func__);
2439 usb_enable_autosuspend(udev);
2440 pm_runtime_set_autosuspend_delay(&(udev->dev), 2000);
2442 RTKBT_INFO("%s: Auto suspend is disabled", __func__);
2443 usb_disable_autosuspend(udev);
2446 #if BTUSB_WAKEUP_HOST
2447 device_wakeup_enable(&udev->dev);
2453 void firmware_info_destroy(struct usb_interface *intf)
2455 firmware_info *fw_info;
2456 struct usb_device *udev;
2457 struct btusb_data *data;
2459 udev = interface_to_usbdev(intf);
2460 data = usb_get_intfdata(intf);
2462 fw_info = data->fw_info;
2467 usb_disable_autosuspend(udev);
2471 * In order to reclaim fw data mem, we free fw_data immediately
2472 * after download patch finished instead of here.
2474 kfree(fw_info->rcv_pkt);
2475 kfree(fw_info->send_pkt);
2479 if (!fw_info_4_suspend)
2482 kfree(fw_info_4_suspend->rcv_pkt);
2483 kfree(fw_info_4_suspend->send_pkt);
2484 kfree(fw_info_4_suspend);
2485 fw_info_4_suspend = NULL;
2489 static struct usb_driver btusb_driver;
2491 static struct usb_device_id btusb_table[] = {
2492 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
2493 USB_DEVICE_ID_MATCH_INT_INFO,
2495 .bInterfaceClass = 0xe0,
2496 .bInterfaceSubClass = 0x01,
2497 .bInterfaceProtocol = 0x01 },
2499 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
2500 USB_DEVICE_ID_MATCH_INT_INFO,
2502 .bInterfaceClass = 0xe0,
2503 .bInterfaceSubClass = 0x01,
2504 .bInterfaceProtocol = 0x01 },
2509 MODULE_DEVICE_TABLE(usb, btusb_table);
2511 static int inc_tx(struct btusb_data *data)
2513 unsigned long flags;
2516 spin_lock_irqsave(&data->txlock, flags);
2517 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
2519 data->tx_in_flight++;
2520 spin_unlock_irqrestore(&data->txlock, flags);
2525 void check_sco_event(struct urb *urb)
2527 u8* opcode = (u8*)(urb->transfer_buffer);
2529 static uint16_t sco_handle = 0;
2531 struct hci_dev *hdev = urb->context;
2532 struct btusb_data *data = GET_DRV_DATA(hdev);
2535 case HCI_EV_SYNC_CONN_COMPLETE:
2536 RTKBT_INFO("%s: HCI_EV_SYNC_CONN_COMPLETE(0x%02x)", __func__, *opcode);
2537 status = *(opcode + 2);
2538 sco_handle = *(opcode + 3) | *(opcode + 4) << 8;
2540 hdev->conn_hash.sco_num++;
2541 schedule_work(&data->work);
2544 case HCI_EV_DISCONN_COMPLETE:
2545 RTKBT_INFO("%s: HCI_EV_DISCONN_COMPLETE(0x%02x)", __func__, *opcode);
2546 status = *(opcode + 2);
2547 handle = *(opcode + 3) | *(opcode + 4) << 8;
2548 if (status == 0 && sco_handle == handle) {
2549 hdev->conn_hash.sco_num--;
2550 schedule_work(&data->work);
2554 RTKBT_DBG("%s: event 0x%02x", __func__, *opcode);
2559 static void btusb_intr_complete(struct urb *urb)
2561 struct hci_dev *hdev = urb->context;
2562 struct btusb_data *data = GET_DRV_DATA(hdev);
2565 RTKBT_DBG("%s: urb %p status %d count %d ", __func__,
2566 urb, urb->status, urb->actual_length);
2568 // check_sco_event(urb);
2570 if (!test_bit(HCI_RUNNING, &hdev->flags))
2574 if (urb->status == 0) {
2575 hdev->stat.byte_rx += urb->actual_length;
2577 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
2578 urb->transfer_buffer,
2579 urb->actual_length) < 0) {
2580 RTKBT_ERR("%s: Corrupted event packet", __func__);
2581 hdev->stat.err_rx++;
2584 /* Avoid suspend failed when usb_kill_urb */
2585 else if(urb->status == -ENOENT) {
2590 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
2593 usb_mark_last_busy(data->udev);
2594 usb_anchor_urb(urb, &data->intr_anchor);
2596 err = usb_submit_urb(urb, GFP_ATOMIC);
2598 /* EPERM: urb is being killed;
2599 * ENODEV: device got disconnected */
2600 if (err != -EPERM && err != -ENODEV)
2601 RTKBT_ERR("%s: Failed to re-submit urb %p, err %d",
2602 __func__, urb, err);
2603 usb_unanchor_urb(urb);
2607 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
2609 struct btusb_data *data = GET_DRV_DATA(hdev);
2618 urb = usb_alloc_urb(0, mem_flags);
2622 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
2624 buf = kmalloc(size, mem_flags);
2630 RTKBT_DBG("%s: mMaxPacketSize %d, bEndpointAddress 0x%02x",
2631 __func__, size, data->intr_ep->bEndpointAddress);
2633 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
2635 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
2636 btusb_intr_complete, hdev,
2637 data->intr_ep->bInterval);
2639 urb->transfer_flags |= URB_FREE_BUFFER;
2641 usb_anchor_urb(urb, &data->intr_anchor);
2643 err = usb_submit_urb(urb, mem_flags);
2645 RTKBT_ERR("%s: Failed to submit urb %p, err %d",
2646 __func__, urb, err);
2647 usb_unanchor_urb(urb);
2655 static void btusb_bulk_complete(struct urb *urb)
2657 struct hci_dev *hdev = urb->context;
2658 struct btusb_data *data = GET_DRV_DATA(hdev);
2661 RTKBT_DBG("%s: urb %p status %d count %d",
2662 __func__, urb, urb->status, urb->actual_length);
2664 if (!test_bit(HCI_RUNNING, &hdev->flags))
2667 if (urb->status == 0) {
2668 hdev->stat.byte_rx += urb->actual_length;
2670 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
2671 urb->transfer_buffer,
2672 urb->actual_length) < 0) {
2673 RTKBT_ERR("%s: Corrupted ACL packet", __func__);
2674 hdev->stat.err_rx++;
2677 /* Avoid suspend failed when usb_kill_urb */
2678 else if(urb->status == -ENOENT) {
2683 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
2686 usb_anchor_urb(urb, &data->bulk_anchor);
2687 usb_mark_last_busy(data->udev);
2689 err = usb_submit_urb(urb, GFP_ATOMIC);
2691 /* -EPERM: urb is being killed;
2692 * -ENODEV: device got disconnected */
2693 if (err != -EPERM && err != -ENODEV)
2694 RTKBT_ERR("btusb_bulk_complete %s urb %p failed to resubmit (%d)",
2695 hdev->name, urb, -err);
2696 usb_unanchor_urb(urb);
2700 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
2702 struct btusb_data *data = GET_DRV_DATA(hdev);
2706 int err, size = HCI_MAX_FRAME_SIZE;
2708 RTKBT_DBG("%s: hdev name %s", __func__, hdev->name);
2710 if (!data->bulk_rx_ep)
2713 urb = usb_alloc_urb(0, mem_flags);
2717 buf = kmalloc(size, mem_flags);
2723 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
2725 usb_fill_bulk_urb(urb, data->udev, pipe,
2726 buf, size, btusb_bulk_complete, hdev);
2728 urb->transfer_flags |= URB_FREE_BUFFER;
2730 usb_mark_last_busy(data->udev);
2731 usb_anchor_urb(urb, &data->bulk_anchor);
2733 err = usb_submit_urb(urb, mem_flags);
2735 RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb, err);
2736 usb_unanchor_urb(urb);
2744 static void btusb_isoc_complete(struct urb *urb)
2746 struct hci_dev *hdev = urb->context;
2747 struct btusb_data *data = GET_DRV_DATA(hdev);
2751 RTKBT_DBG("%s: urb %p status %d count %d",
2752 __func__, urb, urb->status, urb->actual_length);
2754 if (!test_bit(HCI_RUNNING, &hdev->flags))
2757 if (urb->status == 0) {
2758 for (i = 0; i < urb->number_of_packets; i++) {
2759 unsigned int offset = urb->iso_frame_desc[i].offset;
2760 unsigned int length = urb->iso_frame_desc[i].actual_length;
2762 if (urb->iso_frame_desc[i].status)
2765 hdev->stat.byte_rx += length;
2767 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
2768 urb->transfer_buffer + offset,
2770 RTKBT_ERR("%s: Corrupted SCO packet", __func__);
2771 hdev->stat.err_rx++;
2775 /* Avoid suspend failed when usb_kill_urb */
2776 else if(urb->status == -ENOENT) {
2781 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
2784 usb_anchor_urb(urb, &data->isoc_anchor);
2787 err = usb_submit_urb(urb, GFP_ATOMIC);
2789 /* -EPERM: urb is being killed;
2790 * -ENODEV: device got disconnected */
2791 if (err != -EPERM && err != -ENODEV)
2792 RTKBT_ERR("%s: Failed to re-sumbit urb %p, retry %d, err %d",
2793 __func__, urb, i, err);
2800 usb_unanchor_urb(urb);
2804 static inline void fill_isoc_descriptor(struct urb *urb, int len, int mtu)
2808 RTKBT_DBG("%s: len %d mtu %d", __func__, len, mtu);
2810 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
2811 i++, offset += mtu, len -= mtu) {
2812 urb->iso_frame_desc[i].offset = offset;
2813 urb->iso_frame_desc[i].length = mtu;
2816 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
2817 urb->iso_frame_desc[i].offset = offset;
2818 urb->iso_frame_desc[i].length = len;
2822 urb->number_of_packets = i;
2825 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
2827 struct btusb_data *data = GET_DRV_DATA(hdev);
2833 if (!data->isoc_rx_ep)
2836 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
2840 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
2841 BTUSB_MAX_ISOC_FRAMES;
2843 buf = kmalloc(size, mem_flags);
2849 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
2851 urb->dev = data->udev;
2853 urb->context = hdev;
2854 urb->complete = btusb_isoc_complete;
2855 urb->interval = data->isoc_rx_ep->bInterval;
2857 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
2858 urb->transfer_buffer = buf;
2859 urb->transfer_buffer_length = size;
2861 fill_isoc_descriptor(urb, size,
2862 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
2864 usb_anchor_urb(urb, &data->isoc_anchor);
2866 err = usb_submit_urb(urb, mem_flags);
2868 RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb, err);
2869 usb_unanchor_urb(urb);
2877 static void btusb_tx_complete(struct urb *urb)
2879 struct sk_buff *skb = urb->context;
2880 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2881 struct btusb_data *data = GET_DRV_DATA(hdev);
2883 if (!test_bit(HCI_RUNNING, &hdev->flags))
2887 hdev->stat.byte_tx += urb->transfer_buffer_length;
2889 hdev->stat.err_tx++;
2892 spin_lock(&data->txlock);
2893 data->tx_in_flight--;
2894 spin_unlock(&data->txlock);
2896 kfree(urb->setup_packet);
2901 static void btusb_isoc_tx_complete(struct urb *urb)
2903 struct sk_buff *skb = urb->context;
2904 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2906 RTKBT_DBG("%s: urb %p status %d count %d",
2907 __func__, urb, urb->status, urb->actual_length);
2910 if (!test_bit(HCI_RUNNING, &hdev->flags))
2914 hdev->stat.byte_tx += urb->transfer_buffer_length;
2916 hdev->stat.err_tx++;
2918 RTKBT_ERR("%s: skb 0x%p hdev 0x%p", __func__, skb, hdev);
2921 kfree(urb->setup_packet);
2926 static int btusb_open(struct hci_dev *hdev)
2928 struct btusb_data *data = GET_DRV_DATA(hdev);
2931 RTKBT_INFO("%s: Start, PM usage count %d", __func__,
2932 atomic_read(&(data->intf->pm_usage_cnt)));
2934 err = usb_autopm_get_interface(data->intf);
2938 data->intf->needs_remote_wakeup = 1;
2940 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
2943 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
2946 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
2950 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
2952 mdelay(URB_CANCELING_DELAY_MS);
2953 usb_kill_anchored_urbs(&data->intr_anchor);
2957 set_bit(BTUSB_BULK_RUNNING, &data->flags);
2958 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
2961 usb_autopm_put_interface(data->intf);
2962 RTKBT_INFO("%s: End, PM usage count %d", __func__,
2963 atomic_read(&(data->intf->pm_usage_cnt)));
2967 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2968 clear_bit(HCI_RUNNING, &hdev->flags);
2969 usb_autopm_put_interface(data->intf);
2970 RTKBT_ERR("%s: Failed, PM usage count %d", __func__,
2971 atomic_read(&(data->intf->pm_usage_cnt)));
2975 static void btusb_stop_traffic(struct btusb_data *data)
2977 mdelay(URB_CANCELING_DELAY_MS);
2978 usb_kill_anchored_urbs(&data->intr_anchor);
2979 usb_kill_anchored_urbs(&data->bulk_anchor);
2980 usb_kill_anchored_urbs(&data->isoc_anchor);
2983 static int btusb_close(struct hci_dev *hdev)
2985 struct btusb_data *data = GET_DRV_DATA(hdev);
2988 RTKBT_INFO("%s: hci running %lu", __func__, hdev->flags & HCI_RUNNING);
2990 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
2993 for (i = 0; i < NUM_REASSEMBLY; i++) {
2994 if (hdev->reassembly[i]) {
2995 RTKBT_DBG("%s: free ressembly[%d]", __func__, i);
2996 kfree_skb(hdev->reassembly[i]);
2997 hdev->reassembly[i] = NULL;
3001 cancel_work_sync(&data->work);
3002 cancel_work_sync(&data->waker);
3004 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3005 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3006 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3008 btusb_stop_traffic(data);
3009 err = usb_autopm_get_interface(data->intf);
3013 data->intf->needs_remote_wakeup = 0;
3014 usb_autopm_put_interface(data->intf);
3017 mdelay(URB_CANCELING_DELAY_MS);
3018 usb_scuttle_anchored_urbs(&data->deferred);
3022 static int btusb_flush(struct hci_dev *hdev)
3024 struct btusb_data *data = GET_DRV_DATA(hdev);
3026 RTKBT_DBG("%s", __func__);
3028 mdelay(URB_CANCELING_DELAY_MS);
3029 usb_kill_anchored_urbs(&data->tx_anchor);
3035 static int btusb_send_frame(struct sk_buff *skb)
3037 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
3039 struct btusb_data *data = GET_DRV_DATA(hdev);
3040 struct usb_ctrlrequest *dr;
3046 RTKBT_DBG("%s: hdev %p, btusb data %p, pkt type %d",
3047 __func__, hdev, data, bt_cb(skb)->pkt_type);
3049 if (!test_bit(HCI_RUNNING, &hdev->flags))
3054 switch (bt_cb(skb)->pkt_type) {
3055 case HCI_COMMAND_PKT:
3057 urb = usb_alloc_urb(0, GFP_ATOMIC);
3061 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
3067 dr->bRequestType = data->cmdreq_type;
3071 dr->wLength = __cpu_to_le16(skb->len);
3073 pipe = usb_sndctrlpipe(data->udev, 0x00);
3075 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
3076 skb->data, skb->len, btusb_tx_complete, skb);
3078 hdev->stat.cmd_tx++;
3081 case HCI_ACLDATA_PKT:
3083 if (!data->bulk_tx_ep)
3086 urb = usb_alloc_urb(0, GFP_ATOMIC);
3090 pipe = usb_sndbulkpipe(data->udev,
3091 data->bulk_tx_ep->bEndpointAddress);
3093 usb_fill_bulk_urb(urb, data->udev, pipe,
3094 skb->data, skb->len, btusb_tx_complete, skb);
3096 hdev->stat.acl_tx++;
3099 case HCI_SCODATA_PKT:
3101 if (!data->isoc_tx_ep || SCO_NUM < 1) {
3106 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
3108 RTKBT_ERR("%s: Failed to allocate mem for sco pkts", __func__);
3113 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
3115 usb_fill_int_urb(urb, data->udev, pipe,
3116 skb->data, skb->len, btusb_isoc_tx_complete,
3117 skb, data->isoc_tx_ep->bInterval);
3119 urb->transfer_flags = URB_ISO_ASAP;
3121 fill_isoc_descriptor(urb, skb->len,
3122 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
3124 hdev->stat.sco_tx++;
3133 usb_anchor_urb(urb, &data->deferred);
3134 schedule_work(&data->waker);
3140 usb_anchor_urb(urb, &data->tx_anchor);
3142 err = usb_submit_urb(urb, GFP_ATOMIC);
3144 RTKBT_ERR("%s: Failed to submit urb %p, pkt type %d, err %d, retries %d",
3145 __func__, urb, bt_cb(skb)->pkt_type, err, retries);
3146 if ((bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) && (retries < 10)) {
3149 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)
3150 print_error_command(skb);
3154 kfree(urb->setup_packet);
3155 usb_unanchor_urb(urb);
3157 usb_mark_last_busy(data->udev);
3164 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
3165 static void btusb_destruct(struct hci_dev *hdev)
3167 struct btusb_data *data = GET_DRV_DATA(hdev);
3169 RTKBT_DBG("%s: name %s", __func__, hdev->name);
3175 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
3177 struct btusb_data *data = GET_DRV_DATA(hdev);
3179 RTKBT_DBG("%s: name %s, evt %d", __func__, hdev->name, evt);
3181 if (SCO_NUM != data->sco_num) {
3182 data->sco_num = SCO_NUM;
3183 schedule_work(&data->work);
3187 static inline int set_isoc_interface(struct hci_dev *hdev, int altsetting)
3189 struct btusb_data *data = GET_DRV_DATA(hdev);
3190 struct usb_interface *intf = data->isoc;
3191 struct usb_endpoint_descriptor *ep_desc;
3197 err = usb_set_interface(data->udev, 1, altsetting);
3199 RTKBT_ERR("%s: Failed to set interface, altsetting %d, err %d",
3200 __func__, altsetting, err);
3204 data->isoc_altsetting = altsetting;
3206 data->isoc_tx_ep = NULL;
3207 data->isoc_rx_ep = NULL;
3209 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3210 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3212 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
3213 data->isoc_tx_ep = ep_desc;
3217 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
3218 data->isoc_rx_ep = ep_desc;
3223 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
3224 RTKBT_ERR("%s: Invalid SCO descriptors", __func__);
3231 static void btusb_work(struct work_struct *work)
3233 struct btusb_data *data = container_of(work, struct btusb_data, work);
3234 struct hci_dev *hdev = data->hdev;
3237 if (data->sco_num > 0) {
3238 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
3239 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
3241 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3242 mdelay(URB_CANCELING_DELAY_MS);
3243 usb_kill_anchored_urbs(&data->isoc_anchor);
3247 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
3249 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
3250 if (hdev->voice_setting & 0x0020) {
3251 static const int alts[3] = { 2, 4, 5 };
3252 new_alts = alts[data->sco_num - 1];
3254 new_alts = data->sco_num;
3256 if (data->isoc_altsetting != new_alts) {
3258 if (data->isoc_altsetting != 2) {
3262 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3263 mdelay(URB_CANCELING_DELAY_MS);
3264 usb_kill_anchored_urbs(&data->isoc_anchor);
3266 if (set_isoc_interface(hdev, new_alts) < 0)
3270 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3271 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
3272 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3274 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
3277 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3278 mdelay(URB_CANCELING_DELAY_MS);
3279 usb_kill_anchored_urbs(&data->isoc_anchor);
3281 set_isoc_interface(hdev, 0);
3282 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
3283 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
3287 static void btusb_waker(struct work_struct *work)
3289 struct btusb_data *data = container_of(work, struct btusb_data, waker);
3292 RTKBT_DBG("%s: PM usage count %d", __func__,
3293 atomic_read(&data->intf->pm_usage_cnt));
3295 err = usb_autopm_get_interface(data->intf);
3299 usb_autopm_put_interface(data->intf);
3303 //#ifdef CONFIG_HAS_EARLYSUSPEND
3305 static void btusb_early_suspend(struct early_suspend *h)
3307 struct btusb_data *data;
3308 firmware_info *fw_info;
3309 patch_info *patch_entry;
3311 RTKBT_INFO("%s", __func__);
3313 data = container_of(h, struct btusb_data, early_suspend);
3314 fw_info = data->fw_info;
3315 patch_entry = fw_info->patch_entry;
3317 patch_entry->fw_len = load_firmware(fw_info, &patch_entry->fw_cache);
3318 if (patch_entry->fw_len <= 0) {
3319 /* We may encount failure in loading firmware, just give a warning */
3320 RTKBT_WARN("%s: Failed to load firmware", __func__);
3324 static void btusb_late_resume(struct early_suspend *h)
3326 struct btusb_data *data;
3327 firmware_info *fw_info;
3328 patch_info *patch_entry;
3330 RTKBT_INFO("%s", __func__);
3332 data = container_of(h, struct btusb_data, early_suspend);
3333 fw_info = data->fw_info;
3334 patch_entry = fw_info->patch_entry;
3336 /* Reclaim fw buffer when bt usb resumed */
3337 if (patch_entry->fw_len > 0) {
3338 kfree(patch_entry->fw_cache);
3339 patch_entry->fw_cache = NULL;
3340 patch_entry->fw_len = 0;
3344 int bt_pm_notify(struct notifier_block *notifier, ulong pm_event, void *unused)
3346 struct btusb_data *data;
3347 firmware_info *fw_info;
3348 patch_info *patch_entry;
3349 struct usb_device *udev;
3351 RTKBT_INFO("%s: pm event %ld", __func__, pm_event);
3353 data = container_of(notifier, struct btusb_data, pm_notifier);
3354 fw_info = data->fw_info;
3355 patch_entry = fw_info->patch_entry;
3356 udev = fw_info->udev;
3359 case PM_SUSPEND_PREPARE:
3360 case PM_HIBERNATION_PREPARE:
3362 patch_entry->fw_len = load_firmware(fw_info, &patch_entry->fw_cache);
3363 if (patch_entry->fw_len <= 0) {
3364 /* We may encount failure in loading firmware, just give a warning */
3365 RTKBT_WARN("%s: Failed to load firmware", __func__);
3368 if (!device_may_wakeup(&udev->dev)) {
3369 #if (CONFIG_RESET_RESUME || CONFIG_BLUEDROID)
3370 RTKBT_INFO("%s:remote wakeup not supported, reset resume supported", __func__);
3372 fw_info->intf->needs_binding = 1;
3373 RTKBT_INFO("%s:remote wakeup not supported, binding needed", __func__);
3378 case PM_POST_SUSPEND:
3379 case PM_POST_HIBERNATION:
3380 case PM_POST_RESTORE:
3382 /* Reclaim fw buffer when bt usb resumed */
3383 if (patch_entry->fw_len > 0) {
3384 kfree(patch_entry->fw_cache);
3385 patch_entry->fw_cache = NULL;
3386 patch_entry->fw_len = 0;
3391 usb_disable_autosuspend(udev);
3392 usb_enable_autosuspend(udev);
3393 pm_runtime_set_autosuspend_delay(&(udev->dev), 2000);
3404 int bt_reboot_notify(struct notifier_block *notifier, ulong pm_event, void *unused)
3406 struct btusb_data *data;
3407 firmware_info *fw_info;
3408 patch_info *patch_entry;
3409 struct usb_device *udev;
3411 RTKBT_INFO("%s: pm event %ld", __func__, pm_event);
3413 data = container_of(notifier, struct btusb_data, reboot_notifier);
3414 fw_info = data->fw_info;
3415 patch_entry = fw_info->patch_entry;
3416 udev = fw_info->udev;
3420 RTKBT_DBG("%s:system down or restart", __func__);
3426 cancel_work_sync(&data->work);
3428 btusb_stop_traffic(data);
3429 mdelay(URB_CANCELING_DELAY_MS);
3430 usb_kill_anchored_urbs(&data->tx_anchor);
3433 if(fw_info_4_suspend) {
3434 download_suspend_patch(fw_info_4_suspend,1);
3437 RTKBT_ERR("%s: Failed to download suspend fw", __func__);
3440 #if SET_WAKEUP_DEVICE
3441 set_wakeup_device_from_conf(fw_info_4_suspend);
3443 RTKBT_DBG("%s:system halt or power off", __func__);
3455 static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
3457 struct usb_device *udev = interface_to_usbdev(intf);
3458 struct usb_endpoint_descriptor *ep_desc;
3459 struct btusb_data *data;
3460 struct hci_dev *hdev;
3461 firmware_info *fw_info;
3464 RTKBT_INFO("%s: usb_interface %p, bInterfaceNumber %d, idVendor 0x%04x, "
3465 "idProduct 0x%04x", __func__, intf,
3466 intf->cur_altsetting->desc.bInterfaceNumber,
3467 id->idVendor, id->idProduct);
3469 /* interface numbers are hardcoded in the spec */
3470 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3473 RTKBT_DBG("%s: can wakeup = %x, may wakeup = %x", __func__,
3474 device_can_wakeup(&udev->dev), device_may_wakeup(&udev->dev));
3476 data = rtk_alloc(intf);
3480 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3481 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3483 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3484 data->intr_ep = ep_desc;
3488 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3489 data->bulk_tx_ep = ep_desc;
3493 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3494 data->bulk_rx_ep = ep_desc;
3499 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
3504 data->cmdreq_type = USB_TYPE_CLASS;
3510 spin_lock_init(&queue_lock);
3511 spin_lock_init(&dlfw_lock);
3512 spin_lock_init(&data->lock);
3514 INIT_WORK(&data->work, btusb_work);
3515 INIT_WORK(&data->waker, btusb_waker);
3516 spin_lock_init(&data->txlock);
3518 init_usb_anchor(&data->tx_anchor);
3519 init_usb_anchor(&data->intr_anchor);
3520 init_usb_anchor(&data->bulk_anchor);
3521 init_usb_anchor(&data->isoc_anchor);
3522 init_usb_anchor(&data->deferred);
3524 fw_info = firmware_info_init(intf);
3526 data->fw_info = fw_info;
3528 RTKBT_WARN("%s: Failed to initialize fw info", __func__);
3529 /* Skip download patch */
3533 RTKBT_INFO("%s: download begining...", __func__);
3535 #if CONFIG_BLUEDROID
3536 mutex_lock(&btchr_mutex);
3541 #if CONFIG_BLUEDROID
3542 mutex_unlock(&btchr_mutex);
3545 RTKBT_INFO("%s: download ending...", __func__);
3547 hdev = hci_alloc_dev();
3558 SET_HCIDEV_DEV(hdev, &intf->dev);
3560 hdev->open = btusb_open;
3561 hdev->close = btusb_close;
3562 hdev->flush = btusb_flush;
3563 hdev->send = btusb_send_frame;
3564 hdev->notify = btusb_notify;
3566 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
3567 hci_set_drvdata(hdev, data);
3569 hdev->driver_data = data;
3570 hdev->destruct = btusb_destruct;
3571 hdev->owner = THIS_MODULE;
3574 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1)
3575 if (!reset_on_close){
3576 /* set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); */
3577 RTKBT_DBG("%s: Set HCI_QUIRK_RESET_ON_CLOSE", __func__);
3581 /* Interface numbers are hardcoded in the specification */
3582 data->isoc = usb_ifnum_to_if(data->udev, 1);
3584 err = usb_driver_claim_interface(&btusb_driver,
3595 err = hci_register_dev(hdev);
3604 usb_set_intfdata(intf, data);
3606 //#ifdef CONFIG_HAS_EARLYSUSPEND
3608 data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
3609 data->early_suspend.suspend = btusb_early_suspend;
3610 data->early_suspend.resume = btusb_late_resume;
3611 register_early_suspend(&data->early_suspend);
3613 data->pm_notifier.notifier_call = bt_pm_notify;
3614 data->reboot_notifier.notifier_call = bt_reboot_notify;
3615 register_pm_notifier(&data->pm_notifier);
3616 register_reboot_notifier(&data->reboot_notifier);
3619 #if CONFIG_BLUEDROID
3620 RTKBT_INFO("%s: Check bt reset flag %d", __func__, bt_reset);
3621 /* Report hci hardware error after everthing is ready,
3622 * especially hci register is completed. Or, btchr_poll
3623 * will get null hci dev when hotplug in.
3625 if (bt_reset == 1) {
3626 hci_hardware_error();
3629 bt_reset = 0; /* Clear and reset it anyway */
3636 static void btusb_disconnect(struct usb_interface *intf)
3638 struct btusb_data *data;
3639 struct hci_dev *hdev = NULL;
3641 wait_event_interruptible(bt_dlfw_wait, (check_set_dlfw_state_value(2) == 2));
3643 RTKBT_INFO("%s: usb_interface %p, bInterfaceNumber %d",
3644 __func__, intf, intf->cur_altsetting->desc.bInterfaceNumber);
3646 data = usb_get_intfdata(intf);
3648 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3654 RTKBT_WARN("%s: Failed to get bt usb data[Null]", __func__);
3658 //#ifdef CONFIG_HAS_EARLYSUSPEND
3660 unregister_early_suspend(&data->early_suspend);
3662 unregister_pm_notifier(&data->pm_notifier);
3663 unregister_reboot_notifier(&data->reboot_notifier);
3666 firmware_info_destroy(intf);
3668 #if CONFIG_BLUEDROID
3669 if (test_bit(HCI_RUNNING, &hdev->flags)) {
3670 RTKBT_INFO("%s: Set BT reset flag", __func__);
3675 usb_set_intfdata(data->intf, NULL);
3678 usb_set_intfdata(data->isoc, NULL);
3680 hci_unregister_dev(hdev);
3682 if (intf == data->isoc)
3683 usb_driver_release_interface(&btusb_driver, data->intf);
3684 else if (data->isoc)
3685 usb_driver_release_interface(&btusb_driver, data->isoc);
3687 #if !CONFIG_BLUEDROID
3688 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
3689 __hci_dev_put(hdev);
3696 set_dlfw_state_value(0);
3700 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3702 struct btusb_data *data = usb_get_intfdata(intf);
3703 firmware_info *fw_info = data->fw_info;
3705 RTKBT_INFO("%s: event 0x%x, suspend count %d", __func__,
3706 message.event, data->suspend_count);
3708 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3711 if (!test_bit(HCI_RUNNING, &data->hdev->flags))
3712 set_bt_onoff(fw_info, 1);
3714 if (data->suspend_count++)
3717 spin_lock_irq(&data->txlock);
3718 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
3719 set_bit(BTUSB_SUSPENDING, &data->flags);
3720 spin_unlock_irq(&data->txlock);
3722 spin_unlock_irq(&data->txlock);
3723 data->suspend_count--;
3724 RTKBT_ERR("%s: Failed to enter suspend", __func__);
3728 cancel_work_sync(&data->work);
3730 btusb_stop_traffic(data);
3731 mdelay(URB_CANCELING_DELAY_MS);
3732 usb_kill_anchored_urbs(&data->tx_anchor);
3735 if(fw_info_4_suspend) {
3736 download_suspend_patch(fw_info_4_suspend,1);
3739 RTKBT_ERR("%s: Failed to download suspend fw", __func__);
3742 #if SET_WAKEUP_DEVICE
3743 set_wakeup_device_from_conf(fw_info_4_suspend);
3749 static void play_deferred(struct btusb_data *data)
3754 while ((urb = usb_get_from_anchor(&data->deferred))) {
3755 usb_anchor_urb(urb, &data->tx_anchor);
3756 err = usb_submit_urb(urb, GFP_ATOMIC);
3758 RTKBT_ERR("%s: Failed to submit urb %p, err %d",
3759 __func__, urb, err);
3760 kfree(urb->setup_packet);
3761 usb_unanchor_urb(urb);
3763 usb_mark_last_busy(data->udev);
3767 data->tx_in_flight++;
3769 mdelay(URB_CANCELING_DELAY_MS);
3770 usb_scuttle_anchored_urbs(&data->deferred);
3773 static int btusb_resume(struct usb_interface *intf)
3775 struct btusb_data *data = usb_get_intfdata(intf);
3776 struct hci_dev *hdev = data->hdev;
3777 firmware_info *fw_info = data->fw_info;
3780 RTKBT_INFO("%s: Suspend count %d", __func__, data->suspend_count);
3782 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3785 if (--data->suspend_count)
3788 /*check_fw_version to check the status of the BT Controller after USB Resume*/
3789 err = check_fw_version(fw_info);
3792 RTKBT_INFO("%s: BT Controller Power OFF And Return hci_hardware_error:%d", __func__, err);
3793 hci_hardware_error();
3797 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3798 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3800 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3805 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3806 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3808 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3812 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3815 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3816 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3817 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3819 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3822 spin_lock_irq(&data->txlock);
3823 play_deferred(data);
3824 clear_bit(BTUSB_SUSPENDING, &data->flags);
3825 spin_unlock_irq(&data->txlock);
3826 schedule_work(&data->work);
3831 mdelay(URB_CANCELING_DELAY_MS);
3832 usb_scuttle_anchored_urbs(&data->deferred);
3833 spin_lock_irq(&data->txlock);
3834 clear_bit(BTUSB_SUSPENDING, &data->flags);
3835 spin_unlock_irq(&data->txlock);
3841 static struct usb_driver btusb_driver = {
3842 .name = "rtk_btusb",
3843 .probe = btusb_probe,
3844 .disconnect = btusb_disconnect,
3846 .suspend = btusb_suspend,
3847 .resume = btusb_resume,
3849 #if CONFIG_RESET_RESUME
3850 .reset_resume = btusb_resume,
3852 .id_table = btusb_table,
3853 .supports_autosuspend = 1,
3854 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
3855 .disable_hub_initiated_lpm = 1,
3859 static int __init btusb_init(void)
3863 RTKBT_INFO("RTKBT_RELEASE_NAME: %s",RTKBT_RELEASE_NAME);
3864 RTKBT_INFO("Realtek Bluetooth USB driver module init, version %s", VERSION);
3865 #if CONFIG_BLUEDROID
3868 /* usb register will go on, even bt char register failed */
3869 RTKBT_ERR("Failed to register usb char device interfaces");
3871 bt_char_dev_registered = 1;
3873 err = usb_register(&btusb_driver);
3875 RTKBT_ERR("Failed to register RTK bluetooth USB driver");
3879 static void __exit btusb_exit(void)
3881 RTKBT_INFO("Realtek Bluetooth USB driver module exit");
3882 #if CONFIG_BLUEDROID
3883 if (bt_char_dev_registered > 0)
3886 usb_deregister(&btusb_driver);
3889 module_init(btusb_init);
3890 module_exit(btusb_exit);
3893 module_param(mp_drv_mode, int, 0644);
3894 MODULE_PARM_DESC(mp_drv_mode, "0: NORMAL; 1: MP MODE");
3897 MODULE_AUTHOR("Realtek Corporation");
3898 MODULE_DESCRIPTION("Realtek Bluetooth USB driver version");
3899 MODULE_VERSION(VERSION);
3900 MODULE_LICENSE("GPL");