Bluetooth: update rtk_btusb driver to v 4.1.2
[firefly-linux-kernel-4.4.55.git] / drivers / bluetooth / rtk_btusb.c
1 /*
2  *
3  *  Realtek Bluetooth USB driver
4  *
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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>
31
32 #include <linux/ioctl.h>
33 #include <linux/io.h>
34 #include <linux/firmware.h>
35 #include <linux/vmalloc.h>
36 #include <linux/fs.h>
37 #include <linux/uaccess.h>
38 #include <linux/reboot.h>
39
40 #include "rtk_btusb.h"
41
42 #define RTKBT_RELEASE_NAME "20170109_TV_ANDROID_6.x"
43 #define VERSION "4.1.2"
44
45 #define SUSPNED_DW_FW 0
46 #define SET_WAKEUP_DEVICE 0
47
48
49 static spinlock_t queue_lock;
50 static spinlock_t dlfw_lock;
51 static volatile uint16_t    dlfw_dis_state = 0;
52
53 #if SUSPNED_DW_FW
54 static firmware_info *fw_info_4_suspend = NULL;
55 #endif
56
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*/
64
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 */
69
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 */
80
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 */
86
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 */
95
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 */
103
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 */
109
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 */
114
115 /* NOTE: must append patch entries above the null entry */
116 { 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0 }
117 };
118
119 struct btusb_data {
120     struct hci_dev       *hdev;
121     struct usb_device    *udev;
122     struct usb_interface *intf;
123     struct usb_interface *isoc;
124
125     spinlock_t lock;
126
127     unsigned long flags;
128
129     struct work_struct work;
130     struct work_struct waker;
131
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;
137     int tx_in_flight;
138     spinlock_t txlock;
139
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;
145
146     __u8 cmdreq_type;
147
148     unsigned int sco_num;
149     int isoc_altsetting;
150     int suspend_count;
151 //#ifdef CONFIG_HAS_EARLYSUSPEND
152 #if 0
153     struct early_suspend early_suspend;
154 #else
155     struct notifier_block pm_notifier;
156     struct notifier_block reboot_notifier;
157 #endif
158     firmware_info *fw_info;
159 };
160 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1)
161 static bool reset_on_close = 0;
162 #endif
163
164 int download_patch(firmware_info *fw_info, int cached);
165
166 static inline int check_set_dlfw_state_value(uint16_t change_value)
167 {
168     spin_lock(&dlfw_lock);
169     if(!dlfw_dis_state) {
170         dlfw_dis_state = change_value;
171     }
172     spin_unlock(&dlfw_lock);
173     return dlfw_dis_state;
174 }
175
176 static inline void set_dlfw_state_value(uint16_t change_value)
177 {
178     spin_lock(&dlfw_lock);
179     dlfw_dis_state = change_value;
180     spin_unlock(&dlfw_lock);
181 }
182
183 #if SUSPNED_DW_FW
184 static int download_suspend_patch(firmware_info *fw_info, int cached);
185 #endif
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);
189 #endif
190
191 static void rtk_free( struct btusb_data *data)
192 {
193 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1)
194     kfree(data);
195 #endif
196     return;
197 }
198
199 static struct btusb_data *rtk_alloc(struct usb_interface *intf)
200 {
201     struct btusb_data *data;
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1)
203     data = kzalloc(sizeof(*data), GFP_KERNEL);
204 #else
205     data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
206 #endif
207     return data;
208 }
209
210 static void print_acl(struct sk_buff *skb, int direction)
211 {
212 #if PRINT_ACL_DATA
213     uint wlength = skb->len;
214     u16 *handle = (u16 *)(skb->data);
215     u16 len = *(handle+1);
216     u8 *acl_data = (u8 *)(skb->data);
217
218     RTK_INFO("%s: direction %d, handle %04x, len %d",
219             __func__, direction, *handle, len);
220 #endif
221 }
222
223 static void print_sco(struct sk_buff *skb, int direction)
224 {
225 #if PRINT_SCO_DATA
226     uint wlength = skb->len;
227     u16 *handle = (u16 *)(skb->data);
228     u8 len = *(u8 *)(handle+1);
229     u8 *sco_data =(u8 *)(skb->data);
230
231     RTKBT_INFO("%s: direction %d, handle %04x, len %d",
232             __func__, direction, *handle, len);
233 #endif
234 }
235
236 static void print_error_command(struct sk_buff *skb)
237 {
238     uint wlength = skb->len;
239     uint icount = 0;
240     u16 *opcode = (u16*)(skb->data);
241     u8 *cmd_data = (u8*)(skb->data);
242     u8 len = *(cmd_data+2);
243
244     switch (*opcode) {
245     case HCI_OP_INQUIRY:
246         printk("HCI_OP_INQUIRY");
247         break;
248     case HCI_OP_INQUIRY_CANCEL:
249         printk("HCI_OP_INQUIRY_CANCEL");
250         break;
251     case HCI_OP_EXIT_PERIODIC_INQ:
252         printk("HCI_OP_EXIT_PERIODIC_INQ");
253         break;
254     case HCI_OP_CREATE_CONN:
255         printk("HCI_OP_CREATE_CONN");
256         break;
257     case HCI_OP_DISCONNECT:
258         printk("HCI_OP_DISCONNECT");
259         break;
260     case HCI_OP_CREATE_CONN_CANCEL:
261         printk("HCI_OP_CREATE_CONN_CANCEL");
262         break;
263     case HCI_OP_ACCEPT_CONN_REQ:
264         printk("HCI_OP_ACCEPT_CONN_REQ");
265         break;
266     case HCI_OP_REJECT_CONN_REQ:
267         printk("HCI_OP_REJECT_CONN_REQ");
268         break;
269     case HCI_OP_AUTH_REQUESTED:
270         printk("HCI_OP_AUTH_REQUESTED");
271         break;
272     case HCI_OP_SET_CONN_ENCRYPT:
273         printk("HCI_OP_SET_CONN_ENCRYPT");
274         break;
275     case HCI_OP_REMOTE_NAME_REQ:
276         printk("HCI_OP_REMOTE_NAME_REQ");
277         break;
278     case HCI_OP_READ_REMOTE_FEATURES:
279         printk("HCI_OP_READ_REMOTE_FEATURES");
280         break;
281     case HCI_OP_SNIFF_MODE:
282         printk("HCI_OP_SNIFF_MODE");
283         break;
284     case HCI_OP_EXIT_SNIFF_MODE:
285         printk("HCI_OP_EXIT_SNIFF_MODE");
286         break;
287     case HCI_OP_SWITCH_ROLE:
288         printk("HCI_OP_SWITCH_ROLE");
289         break;
290     case HCI_OP_SNIFF_SUBRATE:
291         printk("HCI_OP_SNIFF_SUBRATE");
292         break;
293     case HCI_OP_RESET:
294         printk("HCI_OP_RESET");
295         break;
296     case HCI_OP_Write_Extended_Inquiry_Response:
297         printk("HCI_Write_Extended_Inquiry_Response");
298         break;
299
300     default:
301         printk("CMD");
302         break;
303     }
304     printk(":%04x,len:%d,", *opcode,len);
305     for (icount = 3; (icount < wlength) && (icount < 24); icount++)
306         printk("%02x ", *(cmd_data+icount));
307     printk("\n");
308 }
309
310 static void print_command(struct sk_buff *skb)
311 {
312 #if PRINT_CMD_EVENT
313     print_error_command(skb);
314 #endif
315 }
316
317 #if CONFIG_BLUEDROID
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;
332
333 static void print_event(struct sk_buff *skb)
334 {
335 #if PRINT_CMD_EVENT
336     uint wlength = skb->len;
337     uint icount = 0;
338     u8 *opcode = (u8*)(skb->data);
339     u8 len = *(opcode+1);
340
341     switch (*opcode) {
342     case HCI_EV_INQUIRY_COMPLETE:
343         printk("HCI_EV_INQUIRY_COMPLETE");
344         break;
345     case HCI_EV_INQUIRY_RESULT:
346         printk("HCI_EV_INQUIRY_RESULT");
347         break;
348     case HCI_EV_CONN_COMPLETE:
349         printk("HCI_EV_CONN_COMPLETE");
350         break;
351     case HCI_EV_CONN_REQUEST:
352         printk("HCI_EV_CONN_REQUEST");
353         break;
354     case HCI_EV_DISCONN_COMPLETE:
355         printk("HCI_EV_DISCONN_COMPLETE");
356         break;
357     case HCI_EV_AUTH_COMPLETE:
358         printk("HCI_EV_AUTH_COMPLETE");
359         break;
360     case HCI_EV_REMOTE_NAME:
361         printk("HCI_EV_REMOTE_NAME");
362         break;
363     case HCI_EV_ENCRYPT_CHANGE:
364         printk("HCI_EV_ENCRYPT_CHANGE");
365         break;
366     case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
367         printk("HCI_EV_CHANGE_LINK_KEY_COMPLETE");
368         break;
369     case HCI_EV_REMOTE_FEATURES:
370         printk("HCI_EV_REMOTE_FEATURES");
371         break;
372     case HCI_EV_REMOTE_VERSION:
373         printk("HCI_EV_REMOTE_VERSION");
374         break;
375     case HCI_EV_QOS_SETUP_COMPLETE:
376         printk("HCI_EV_QOS_SETUP_COMPLETE");
377         break;
378     case HCI_EV_CMD_COMPLETE:
379         printk("HCI_EV_CMD_COMPLETE");
380         break;
381     case HCI_EV_CMD_STATUS:
382         printk("HCI_EV_CMD_STATUS");
383         break;
384     case HCI_EV_ROLE_CHANGE:
385         printk("HCI_EV_ROLE_CHANGE");
386         break;
387     case HCI_EV_NUM_COMP_PKTS:
388         printk("HCI_EV_NUM_COMP_PKTS");
389         break;
390     case HCI_EV_MODE_CHANGE:
391         printk("HCI_EV_MODE_CHANGE");
392         break;
393     case HCI_EV_PIN_CODE_REQ:
394         printk("HCI_EV_PIN_CODE_REQ");
395         break;
396     case HCI_EV_LINK_KEY_REQ:
397         printk("HCI_EV_LINK_KEY_REQ");
398         break;
399     case HCI_EV_LINK_KEY_NOTIFY:
400         printk("HCI_EV_LINK_KEY_NOTIFY");
401         break;
402     case HCI_EV_CLOCK_OFFSET:
403         printk("HCI_EV_CLOCK_OFFSET");
404         break;
405     case HCI_EV_PKT_TYPE_CHANGE:
406         printk("HCI_EV_PKT_TYPE_CHANGE");
407         break;
408     case HCI_EV_PSCAN_REP_MODE:
409         printk("HCI_EV_PSCAN_REP_MODE");
410         break;
411     case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
412         printk("HCI_EV_INQUIRY_RESULT_WITH_RSSI");
413         break;
414     case HCI_EV_REMOTE_EXT_FEATURES:
415         printk("HCI_EV_REMOTE_EXT_FEATURES");
416         break;
417     case HCI_EV_SYNC_CONN_COMPLETE:
418         printk("HCI_EV_SYNC_CONN_COMPLETE");
419         break;
420     case HCI_EV_SYNC_CONN_CHANGED:
421         printk("HCI_EV_SYNC_CONN_CHANGED");
422         break;
423     case HCI_EV_SNIFF_SUBRATE:
424         printk("HCI_EV_SNIFF_SUBRATE");
425         break;
426     case HCI_EV_EXTENDED_INQUIRY_RESULT:
427         printk("HCI_EV_EXTENDED_INQUIRY_RESULT");
428         break;
429     case HCI_EV_IO_CAPA_REQUEST:
430         printk("HCI_EV_IO_CAPA_REQUEST");
431         break;
432     case HCI_EV_SIMPLE_PAIR_COMPLETE:
433         printk("HCI_EV_SIMPLE_PAIR_COMPLETE");
434         break;
435     case HCI_EV_REMOTE_HOST_FEATURES:
436         printk("HCI_EV_REMOTE_HOST_FEATURES");
437         break;
438     default:
439         printk("event");
440         break;
441     }
442     printk(":%02x,len:%d,", *opcode,len);
443     for (icount = 2; (icount < wlength) && (icount < 24); icount++)
444         printk("%02x ", *(opcode+icount));
445     printk("\n");
446 #endif
447 }
448
449 static inline ssize_t usb_put_user(struct sk_buff *skb,
450         char __user *buf, int count)
451 {
452     char __user *ptr = buf;
453     int len = min_t(unsigned int, skb->len, count);
454
455     if (copy_to_user(ptr, skb->data, len))
456         return -EFAULT;
457
458     return len;
459 }
460
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;
464
465 static void rtk_enqueue(struct sk_buff *skb)
466 {
467     spin_lock(&queue_lock);
468     if (rtk_skb_queue_front == (rtk_skb_queue_rear + 1) % QUEUE_SIZE) {
469         /*
470          * If queue is full, current solution is to drop
471          * the following entries.
472          */
473         RTKBT_WARN("%s: Queue is full, entry will be dropped", __func__);
474     } else {
475         rtk_skb_queue[rtk_skb_queue_rear] = skb;
476
477         rtk_skb_queue_rear++;
478         rtk_skb_queue_rear %= QUEUE_SIZE;
479
480     }
481     spin_unlock(&queue_lock);
482 }
483
484 static struct sk_buff *rtk_dequeue_try(unsigned int deq_len)
485 {
486     struct sk_buff *skb;
487     struct sk_buff *skb_copy;
488
489     if (rtk_skb_queue_front == rtk_skb_queue_rear) {
490         RTKBT_WARN("%s: Queue is empty", __func__);
491         return NULL;
492     }
493
494     skb = rtk_skb_queue[rtk_skb_queue_front];
495     if (deq_len >= skb->len) {
496
497         rtk_skb_queue_front++;
498         rtk_skb_queue_front %= QUEUE_SIZE;
499
500         /*
501          * Return skb addr to be dequeued, and the caller
502          * should free the skb eventually.
503          */
504         return skb;
505     } else {
506         skb_copy = pskb_copy(skb, GFP_ATOMIC);
507         skb_pull(skb, deq_len);
508         /* Return its copy to be freed */
509         return skb_copy;
510     }
511 }
512
513 static inline int is_queue_empty(void)
514 {
515     return (rtk_skb_queue_front == rtk_skb_queue_rear) ? 1 : 0;
516 }
517
518 /*
519  * Realtek - Integrate from hci_core.c
520  */
521
522 /* Get HCI device by index.
523  * Device is held on return. */
524 static struct hci_dev *hci_dev_get(int index)
525 {
526     if (index != 0)
527         return NULL;
528
529     return ghdev;
530 }
531
532 /* ---- HCI ioctl helpers ---- */
533 static int hci_dev_open(__u16 dev)
534 {
535     struct hci_dev *hdev;
536     int ret = 0;
537
538     RTKBT_DBG("%s: dev %d", __func__, dev);
539
540     hdev = hci_dev_get(dev);
541     if (!hdev) {
542         RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__);
543         return -ENODEV;
544     }
545
546     if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) {
547         ret = -ENODEV;
548         goto done;
549     }
550
551     if (test_bit(HCI_UP, &hdev->flags)) {
552         ret = -EALREADY;
553         goto done;
554     }
555
556 done:
557     return ret;
558 }
559
560 static int hci_dev_do_close(struct hci_dev *hdev)
561 {
562     if (hdev->flush)
563         hdev->flush(hdev);
564     /* After this point our queues are empty
565      * and no tasks are scheduled. */
566     hdev->close(hdev);
567     /* Clear flags */
568     hdev->flags = 0;
569     return 0;
570 }
571
572 static int hci_dev_close(__u16 dev)
573 {
574     struct hci_dev *hdev;
575     int err;
576     hdev = hci_dev_get(dev);
577     if (!hdev) {
578         RTKBT_ERR("%s: failed to get hci dev[Null]", __func__);
579         return -ENODEV;
580     }
581
582     err = hci_dev_do_close(hdev);
583
584     return err;
585 }
586
587 static struct hci_dev *hci_alloc_dev(void)
588 {
589     struct hci_dev *hdev;
590
591     hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
592     if (!hdev)
593         return NULL;
594
595     return hdev;
596 }
597
598 /* Free HCI device */
599 static void hci_free_dev(struct hci_dev *hdev)
600 {
601     kfree(hdev);
602 }
603
604 /* Register HCI device */
605 static int hci_register_dev(struct hci_dev *hdev)
606 {
607     int i, id;
608
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.
612      */
613     id = (hdev->dev_type == HCI_BREDR) ? 0 : 1;
614
615     write_lock(&hci_dev_lock);
616
617     sprintf(hdev->name, "hci%d", id);
618     hdev->id = id;
619     hdev->flags = 0;
620     hdev->dev_flags = 0;
621     mutex_init(&hdev->lock);
622
623     RTKBT_DBG("%s: id %d, name %s", __func__, hdev->id, hdev->name);
624
625
626     for (i = 0; i < NUM_REASSEMBLY; i++)
627         hdev->reassembly[i] = NULL;
628
629     memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
630     atomic_set(&hdev->promisc, 0);
631
632     if (ghdev) {
633         write_unlock(&hci_dev_lock);
634         RTKBT_ERR("%s: Hci device has been registered already", __func__);
635         return -1;
636     } else
637         ghdev = hdev;
638
639     write_unlock(&hci_dev_lock);
640
641     return id;
642 }
643
644 /* Unregister HCI device */
645 static void hci_unregister_dev(struct hci_dev *hdev)
646 {
647     int i;
648
649     RTKBT_DBG("%s: hdev %p name %s bus %d", __func__, hdev, hdev->name, hdev->bus);
650     set_bit(HCI_UNREGISTER, &hdev->dev_flags);
651
652     write_lock(&hci_dev_lock);
653     ghdev = NULL;
654     write_unlock(&hci_dev_lock);
655
656     hci_dev_do_close(hdev);
657     for (i = 0; i < NUM_REASSEMBLY; i++)
658         kfree_skb(hdev->reassembly[i]);
659 }
660
661 static void hci_send_to_stack(struct hci_dev *hdev, struct sk_buff *skb)
662 {
663     struct sk_buff *rtk_skb_copy = NULL;
664
665     RTKBT_DBG("%s", __func__);
666
667     if (!hdev) {
668         RTKBT_ERR("%s: Frame for unknown HCI device", __func__);
669         return;
670     }
671
672     if (!test_bit(HCI_RUNNING, &hdev->flags)) {
673         RTKBT_ERR("%s: HCI not running", __func__);
674         return;
675     }
676
677     rtk_skb_copy = pskb_copy(skb, GFP_ATOMIC);
678     if (!rtk_skb_copy) {
679         RTKBT_ERR("%s: Copy skb error", __func__);
680         return;
681     }
682
683     memcpy(skb_push(rtk_skb_copy, 1), &bt_cb(skb)->pkt_type, 1);
684     rtk_enqueue(rtk_skb_copy);
685
686     /* Make sure bt char device existing before wakeup read queue */
687     hdev = hci_dev_get(0);
688     if (hdev) {
689         RTKBT_DBG("%s: Try to wakeup read queue", __func__);
690         wake_up_interruptible(&btchr_read_wait);
691     }
692
693     return;
694 }
695
696 /* Receive frame from HCI drivers */
697 static int hci_recv_frame(struct sk_buff *skb)
698 {
699     struct hci_dev *hdev = (struct hci_dev *) skb->dev;
700
701     if (!hdev ||
702         (!test_bit(HCI_UP, &hdev->flags) && !test_bit(HCI_INIT, &hdev->flags))) {
703         kfree_skb(skb);
704         return -ENXIO;
705     }
706
707     /* Incomming skb */
708     bt_cb(skb)->incoming = 1;
709
710     /* Time stamp */
711     __net_timestamp(skb);
712
713     if (atomic_read(&hdev->promisc)) {
714         /* Send copy to the sockets */
715         hci_send_to_stack(hdev, skb);
716     }
717     kfree_skb(skb);
718     return 0;
719 }
720
721 static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
722                           int count, __u8 index)
723 {
724     int len = 0;
725     int hlen = 0;
726     int remain = count;
727     struct sk_buff *skb;
728     struct bt_skb_cb *scb;
729
730     RTKBT_DBG("%s", __func__);
731
732     if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
733             index >= NUM_REASSEMBLY)
734         return -EILSEQ;
735
736     skb = hdev->reassembly[index];
737
738     if (!skb) {
739         switch (type) {
740         case HCI_ACLDATA_PKT:
741             len = HCI_MAX_FRAME_SIZE;
742             hlen = HCI_ACL_HDR_SIZE;
743             break;
744         case HCI_EVENT_PKT:
745             len = HCI_MAX_EVENT_SIZE;
746             hlen = HCI_EVENT_HDR_SIZE;
747             break;
748         case HCI_SCODATA_PKT:
749             len = HCI_MAX_SCO_SIZE;
750             hlen = HCI_SCO_HDR_SIZE;
751             break;
752         }
753
754         skb = bt_skb_alloc(len, GFP_ATOMIC);
755         if (!skb)
756             return -ENOMEM;
757
758         scb = (void *) skb->cb;
759         scb->expect = hlen;
760         scb->pkt_type = type;
761
762         skb->dev = (void *) hdev;
763         hdev->reassembly[index] = skb;
764     }
765
766     while (count) {
767         scb = (void *) skb->cb;
768         len = min_t(uint, scb->expect, count);
769
770         memcpy(skb_put(skb, len), data, len);
771
772         count -= len;
773         data += len;
774         scb->expect -= len;
775         remain = count;
776
777         switch (type) {
778         case HCI_EVENT_PKT:
779             if (skb->len == HCI_EVENT_HDR_SIZE) {
780                 struct hci_event_hdr *h = hci_event_hdr(skb);
781                 scb->expect = h->plen;
782
783                 if (skb_tailroom(skb) < scb->expect) {
784                     kfree_skb(skb);
785                     hdev->reassembly[index] = NULL;
786                     return -ENOMEM;
787                 }
788             }
789             break;
790
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);
795
796                 if (skb_tailroom(skb) < scb->expect) {
797                     kfree_skb(skb);
798                     hdev->reassembly[index] = NULL;
799                     return -ENOMEM;
800                 }
801             }
802             break;
803
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;
808
809                 if (skb_tailroom(skb) < scb->expect) {
810                     kfree_skb(skb);
811                     hdev->reassembly[index] = NULL;
812                     return -ENOMEM;
813                 }
814             }
815             break;
816         }
817
818         if (scb->expect == 0) {
819             /* Complete frame */
820             if(HCI_ACLDATA_PKT == type)
821                 print_acl(skb,0);
822             if(HCI_SCODATA_PKT == type)
823                 print_sco(skb,0);
824             if(HCI_EVENT_PKT == type)
825                 print_event(skb);
826
827             bt_cb(skb)->pkt_type = type;
828             hci_recv_frame(skb);
829
830             hdev->reassembly[index] = NULL;
831             return remain;
832         }
833     }
834
835     return remain;
836 }
837
838 static int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
839 {
840     int rem = 0;
841
842     if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
843         return -EILSEQ;
844
845     while (count) {
846         rem = hci_reassembly(hdev, type, data, count, type - 1);
847         if (rem < 0)
848             return rem;
849
850         data += (count - rem);
851         count = rem;
852     }
853
854     return rem;
855 }
856
857 void hci_hardware_error(void)
858 {
859     struct sk_buff *rtk_skb_copy = NULL;
860     int len = 3;
861     uint8_t hardware_err_pkt[4] = {HCI_EVENT_PKT, 0x10, 0x01, HCI_VENDOR_USB_DISC_HARDWARE_ERROR};
862
863     rtk_skb_copy = alloc_skb(len, GFP_ATOMIC);
864     if (!rtk_skb_copy) {
865         RTKBT_ERR("%s: Failed to allocate mem", __func__);
866         return;
867     }
868
869     memcpy(skb_put(rtk_skb_copy, len), hardware_err_pkt, len);
870     rtk_enqueue(rtk_skb_copy);
871
872     wake_up_interruptible(&btchr_read_wait);
873 }
874
875 static int btchr_open(struct inode *inode_p, struct file  *file_p)
876 {
877     struct btusb_data *data;
878     struct hci_dev *hdev;
879
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__); */
883
884     hdev = hci_dev_get(0);
885     if (!hdev) {
886         RTKBT_ERR("%s: Failed to get hci dev[NULL]", __func__);
887         return -ENODEV;
888     }
889     data = GET_DRV_DATA(hdev);
890
891     atomic_inc(&hdev->promisc);
892     /*
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
895      * are invoked.
896      */
897     file_p->private_data = data;
898
899     mutex_lock(&btchr_mutex);
900     hci_dev_open(0);
901     mutex_unlock(&btchr_mutex);
902
903     return nonseekable_open(inode_p, file_p);
904 }
905
906 static int btchr_close(struct inode  *inode_p, struct file   *file_p)
907 {
908     struct btusb_data *data;
909     struct hci_dev *hdev;
910
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__); */
914
915     data = file_p->private_data;
916     file_p->private_data = NULL;
917
918 #if CONFIG_BLUEDROID
919     /*
920      * If the upper layer closes bt char interfaces, no reset
921      * action required even bt device hotplugged out.
922      */
923     bt_reset = 0;
924 #endif
925
926     hdev = hci_dev_get(0);
927     if (hdev) {
928         atomic_set(&hdev->promisc, 0);
929         mutex_lock(&btchr_mutex);
930         hci_dev_close(0);
931         mutex_unlock(&btchr_mutex);
932     }
933
934     return 0;
935 }
936
937 static ssize_t btchr_read(struct file *file_p,
938         char __user *buf_p,
939         size_t count,
940         loff_t *pos_p)
941 {
942     struct hci_dev *hdev;
943     struct sk_buff *skb;
944     ssize_t ret = 0;
945
946     RTKBT_DBG("%s: BT usb char device is reading", __func__);
947
948     while (count) {
949         hdev = hci_dev_get(0);
950         if (!hdev) {
951             /*
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.
956              */
957             RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__);
958         }
959
960         ret = wait_event_interruptible(btchr_read_wait, !is_queue_empty());
961         if (ret < 0) {
962             RTKBT_ERR("%s: wait event is signaled %d", __func__, (int)ret);
963             break;
964         }
965
966         skb = rtk_dequeue_try(count);
967         if (skb) {
968             ret = usb_put_user(skb, buf_p, count);
969             if (ret < 0)
970                 RTKBT_ERR("%s: Failed to put data to user space", __func__);
971             kfree_skb(skb);
972             break;
973         }
974     }
975
976     return ret;
977 }
978
979 static ssize_t btchr_write(struct file *file_p,
980         const char __user *buf_p,
981         size_t count,
982         loff_t *pos_p)
983 {
984     struct btusb_data *data = file_p->private_data;
985     struct hci_dev *hdev;
986     struct sk_buff *skb;
987
988     RTKBT_DBG("%s: BT usb char device is writing", __func__);
989
990     hdev = hci_dev_get(0);
991     if (!hdev) {
992         RTKBT_WARN("%s: Failed to get hci dev[Null]", __func__);
993         /*
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.
998          */
999         /* return -EFAULT; */
1000         return count;
1001     }
1002
1003     /* Never trust on btusb_data, as bt device may be hotplugged out */
1004     data = GET_DRV_DATA(hdev);
1005     if (!data) {
1006         RTKBT_WARN("%s: Failed to get bt usb driver data[Null]", __func__);
1007         return count;
1008     }
1009
1010     if (count > HCI_MAX_FRAME_SIZE)
1011         return -EINVAL;
1012
1013     skb = bt_skb_alloc(count, GFP_ATOMIC);
1014     if (!skb)
1015         return -ENOMEM;
1016     skb_reserve(skb, -1); // Add this line
1017
1018     if (copy_from_user(skb_put(skb, count), buf_p, count)) {
1019         RTKBT_ERR("%s: Failed to get data from user space", __func__);
1020         kfree_skb(skb);
1021         return -EFAULT;
1022     }
1023
1024     skb->dev = (void *)hdev;
1025     bt_cb(skb)->pkt_type = *((__u8 *)skb->data);
1026     skb_pull(skb, 1);
1027     data->hdev->send(skb);
1028
1029     return count;
1030 }
1031
1032 static unsigned int btchr_poll(struct file *file_p, poll_table *wait)
1033 {
1034     struct btusb_data *data = file_p->private_data;
1035     struct hci_dev *hdev;
1036
1037     RTKBT_DBG("%s: BT usb char device is polling", __func__);
1038
1039     poll_wait(file_p, &btchr_read_wait, wait);
1040
1041     hdev = hci_dev_get(0);
1042     if (!hdev) {
1043         RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__);
1044         mdelay(URB_CANCELING_DELAY_MS);
1045         return POLLOUT | POLLWRNORM;
1046     }
1047
1048     /* Never trust on btusb_data, as bt device may be hotplugged out */
1049     data = GET_DRV_DATA(hdev);
1050     if (!data) {
1051         /*
1052          * When bt device is hotplugged out, btusb_data will
1053          * be freed in disconnect.
1054          */
1055         RTKBT_ERR("%s: Failed to get bt usb driver data[Null]", __func__);
1056         mdelay(URB_CANCELING_DELAY_MS);
1057         return POLLOUT | POLLWRNORM;
1058     }
1059
1060     if (!is_queue_empty())
1061         return POLLIN | POLLRDNORM;
1062
1063     return POLLOUT | POLLWRNORM;
1064 }
1065 static long btchr_ioctl(struct file *file_p,unsigned int cmd, unsigned long arg){
1066     int ret = 0;
1067     struct hci_dev *hdev;
1068     struct btusb_data *data;
1069     firmware_info *fw_info;
1070
1071     if(check_set_dlfw_state_value(1) != 1) {
1072         RTKBT_ERR("%s bt controller is disconnecting!", __func__);
1073         return 0;
1074     }
1075
1076     hdev = hci_dev_get(0);
1077     if(!hdev) {
1078         RTKBT_ERR("%s device is NULL!", __func__);
1079         set_dlfw_state_value(0);
1080         return 0;
1081     }
1082     data = GET_DRV_DATA(hdev);
1083     fw_info = data->fw_info;
1084
1085     RTKBT_INFO(" btchr_ioctl DOWN_FW_CFG with Cmd:%d",cmd);
1086     switch (cmd) {
1087         case DOWN_FW_CFG:
1088             ret = usb_autopm_get_interface(data->intf);
1089             if (ret < 0){
1090                 goto failed;
1091             }
1092
1093             ret = download_patch(fw_info,1);
1094             usb_autopm_put_interface(data->intf);
1095             if(ret < 0){
1096                 RTKBT_ERR("%s:Failed in download_patch with ret:%d",__func__,ret);
1097                 goto failed;
1098             }
1099
1100             ret = hdev->open(hdev);
1101             if(ret < 0){
1102                 RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret);
1103                 goto failed;
1104             }
1105             set_bit(HCI_UP, &hdev->flags);
1106             set_dlfw_state_value(0);
1107             wake_up_interruptible(&bt_dlfw_wait);
1108             return 1;
1109         default:
1110             RTKBT_ERR("%s:Failed with wrong Cmd:%d",__func__,cmd);
1111             goto failed;
1112         }
1113     failed:
1114         set_dlfw_state_value(0);
1115         wake_up_interruptible(&bt_dlfw_wait);
1116         return ret;
1117
1118 }
1119
1120
1121
1122 static struct file_operations bt_chrdev_ops  = {
1123     open    :    btchr_open,
1124     release    :    btchr_close,
1125     read    :    btchr_read,
1126     write    :    btchr_write,
1127     poll    :    btchr_poll,
1128     unlocked_ioctl   :   btchr_ioctl,
1129 };
1130
1131 static int btchr_init(void)
1132 {
1133     int res = 0;
1134     struct device *dev;
1135
1136     RTKBT_INFO("Register usb char device interface for BT driver");
1137     /*
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
1141      */
1142     mutex_init(&btchr_mutex);
1143
1144     skb_queue_head_init(&btchr_readq);
1145     init_waitqueue_head(&btchr_read_wait);
1146     init_waitqueue_head(&bt_dlfw_wait);
1147
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);
1152     }
1153
1154     res = alloc_chrdev_region(&bt_devid, 0, 1, BT_CHAR_DEVICE_NAME);
1155     if (res < 0) {
1156         RTKBT_ERR("Failed to allocate bt char device");
1157         goto err_alloc;
1158     }
1159
1160     dev = device_create(bt_char_class, NULL, bt_devid, NULL, BT_CHAR_DEVICE_NAME);
1161     if (IS_ERR(dev)) {
1162         RTKBT_ERR("Failed to create bt char device");
1163         res = PTR_ERR(dev);
1164         goto err_create;
1165     }
1166
1167     cdev_init(&bt_char_dev, &bt_chrdev_ops);
1168     res = cdev_add(&bt_char_dev, bt_devid, 1);
1169     if (res < 0) {
1170         RTKBT_ERR("Failed to add bt char device");
1171         goto err_add;
1172     }
1173
1174     return 0;
1175
1176 err_add:
1177     device_destroy(bt_char_class, bt_devid);
1178 err_create:
1179     unregister_chrdev_region(bt_devid, 1);
1180 err_alloc:
1181     class_destroy(bt_char_class);
1182     return res;
1183 }
1184
1185 static void btchr_exit(void)
1186 {
1187     RTKBT_INFO("Unregister usb char device interface for BT driver");
1188
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);
1193
1194     return;
1195 }
1196 #endif
1197
1198 int send_hci_cmd(firmware_info *fw_info)
1199 {
1200    int i = 0;
1201    int ret_val = -1;
1202    while((ret_val<0)&&(i++<10))
1203    {
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);
1209    }
1210    return ret_val;
1211 }
1212
1213 int rcv_hci_evt(firmware_info *fw_info)
1214 {
1215     int ret_len = 0, ret_val = 0;
1216     int i;
1217
1218     while (1) {
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,
1223             &ret_len, MSG_TO);
1224             if (ret_val >= 0)
1225                 break;
1226         }
1227
1228         if (ret_val < 0)
1229             return ret_val;
1230
1231         if (CMD_CMP_EVT == fw_info->evt_hdr->evt) {
1232             if (fw_info->cmd_hdr->opcode == fw_info->cmd_cmp->opcode)
1233                 return ret_len;
1234         }
1235     }
1236 }
1237
1238 int set_bt_onoff(firmware_info *fw_info, uint8_t onoff)
1239 {
1240     patch_info *patch_entry;
1241     int ret_val;
1242
1243     RTKBT_INFO("%s: %s", __func__, onoff != 0 ? "on" : "off");
1244
1245     patch_entry = fw_info->patch_entry;
1246     if (!patch_entry)
1247         return -1;
1248
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;
1253
1254     ret_val = send_hci_cmd(fw_info);
1255     if (ret_val < 0) {
1256         RTKBT_ERR("%s: Failed to send bt %s cmd, errno %d",
1257                 __func__, onoff != 0 ? "on" : "off", ret_val);
1258         return ret_val;
1259     }
1260
1261     ret_val = rcv_hci_evt(fw_info);
1262     if (ret_val < 0) {
1263         RTKBT_ERR("%s: Failed to receive bt %s event, errno %d",
1264                 __func__, onoff != 0 ? "on" : "off", ret_val);
1265         return ret_val;
1266     }
1267
1268     return ret_val;
1269 }
1270
1271 static patch_info *get_fw_table_entry(struct usb_device* udev)
1272 {
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]);
1277     uint32_t i;
1278
1279     RTKBT_INFO("%s: Product id = 0x%04x, fw table entry size %d", __func__, pid, entry_size);
1280
1281     for (i = 0; i < entry_size; i++, patch_entry++) {
1282         if ((vid == patch_entry->vid)&&(pid == patch_entry->pid))
1283             break;
1284     }
1285
1286     if (i == entry_size) {
1287         RTKBT_ERR("%s: No fw table entry found", __func__);
1288         return NULL;
1289     }
1290
1291     return patch_entry;
1292 }
1293
1294 #if SUSPNED_DW_FW
1295 static patch_info *get_suspend_fw_table_entry(struct usb_device* udev)
1296 {
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]);
1301     uint32_t i;
1302
1303     RTKBT_INFO("%s: Product id = 0x%04x, fw table entry size %d", __func__, pid, entry_size);
1304
1305     for (i = 0; i < entry_size; i++, patch_entry++) {
1306         if ((vid == patch_entry->vid)&&(pid == patch_entry->pid))
1307             break;
1308     }
1309
1310     if (i == entry_size) {
1311         RTKBT_ERR("%s: No fw table entry found", __func__);
1312         return NULL;
1313     }
1314
1315     return patch_entry;
1316 }
1317 #endif
1318
1319 static struct rtk_epatch_entry *get_fw_patch_entry(struct rtk_epatch *epatch_info, uint16_t eco_ver)
1320 {
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;
1324     int coex_date;
1325     int coex_ver;
1326     int i;
1327
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);
1331             if (!p_entry) {
1332                 RTKBT_ERR("%s: Failed to allocate mem for patch entry", __func__);
1333                 return NULL;
1334             }
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);
1341
1342             coex_date = ((p_entry->coex_version >> 16) & 0x7ff) + ((p_entry->coex_version >> 27) * 10000);
1343             coex_ver = p_entry->coex_version & 0xffff;
1344
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);
1347             break;
1348         }
1349     }
1350
1351     return p_entry;
1352 }
1353
1354 /*reset_controller is aimed to reset_bt_fw before updata Fw patch*/
1355 int reset_controller(firmware_info* fw_info)
1356 {
1357     int ret_val;
1358     RTKBT_ERR("reset_controller");
1359
1360     if (!fw_info)
1361         return -ENODEV;
1362
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);
1367
1368     if (ret_val < 0) {
1369         RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1370                 __func__, fw_info->cmd_hdr->opcode, ret_val);
1371         return ret_val;
1372     }
1373
1374     //sleep 1s for firmware reset.
1375     msleep(1000);
1376     RTKBT_INFO("%s: Wait fw reset for 1ms",__func__);
1377
1378     return ret_val;
1379 }
1380 /*reset_controller is aimed to reset_bt_fw before updata Fw patch*/
1381
1382 /*
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
1387  */
1388 int check_fw_version(firmware_info* fw_info)
1389 {
1390     struct hci_rp_read_local_version *read_ver_rsp;
1391     patch_info *patch_entry = NULL;
1392     int ret_val = -1;
1393
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;
1397
1398     ret_val = send_hci_cmd(fw_info);
1399     if (ret_val < 0) {
1400         RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1401                 __func__, fw_info->cmd_hdr->opcode, ret_val);
1402         return ret_val;
1403     }
1404
1405     ret_val = rcv_hci_evt(fw_info);
1406     if (ret_val < 0) {
1407         RTKBT_ERR("%s: Failed to receive hci event, errno %d",
1408                 __func__, ret_val);
1409         return ret_val;
1410     }
1411
1412     patch_entry = fw_info->patch_entry;
1413     read_ver_rsp = (struct hci_rp_read_local_version *)(fw_info->rsp_para);
1414
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);
1417
1418     if (read_ver_rsp->lmp_subver == patch_entry->lmp_sub_default) {
1419         RTKBT_INFO("%s: Cold BT controller startup", __func__);
1420
1421         return 2;
1422
1423     } else if (read_ver_rsp->lmp_subver != patch_entry->lmp_sub) {
1424         RTKBT_INFO("%s: Warm BT controller startup with updated lmp", __func__);
1425         return 1;
1426     } else {
1427         RTKBT_INFO("%s: Warm BT controller startup with same lmp", __func__);
1428         return 0;
1429     }
1430 }
1431
1432 #if SET_WAKEUP_DEVICE
1433 int set_wakeup_device(firmware_info* fw_info, uint8_t* wakeup_bdaddr)
1434 {
1435     struct rtk_eversion_evt *ever_evt;
1436     int ret_val;
1437
1438     if (!fw_info)
1439         return -ENODEV;
1440
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;
1445
1446     ret_val = send_hci_cmd(fw_info);
1447     if (ret_val < 0) {
1448         RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d\n",
1449             __func__, fw_info->cmd_hdr->opcode, ret_val);
1450         return ret_val;
1451     }
1452
1453     ret_val = rcv_hci_evt(fw_info);
1454     if (ret_val < 0) {
1455         RTKBT_ERR("%s: Failed to receive hci event, errno %d\n",__func__, ret_val);
1456         return ret_val;
1457     }
1458
1459     ever_evt = (struct rtk_eversion_evt *)(fw_info->rsp_para);
1460
1461     RTKBT_DBG("%s: status %d, eversion %d", __func__, ever_evt->status, ever_evt->version);
1462     return ret_val;
1463 }
1464 #endif
1465
1466 /*reset_channel to recover the communication between wifi 8192eu with 8761 bt controller in case of geteversion error*/
1467
1468 int reset_channel(firmware_info* fw_info)
1469 {
1470     struct rtk_reset_evt *ever_evt;
1471     int ret_val;
1472
1473     if (!fw_info)
1474         return -ENODEV;
1475
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;
1479
1480     ret_val = send_hci_cmd(fw_info);
1481     if (ret_val < 0) {
1482         RTKBT_ERR("%s: Failed to send  hci cmd 0x%04x, errno %d",
1483                 __func__, fw_info->cmd_hdr->opcode, ret_val);
1484         return ret_val;
1485     }
1486
1487     ret_val = rcv_hci_evt(fw_info);
1488     if (ret_val < 0) {
1489         RTKBT_ERR("%s: Failed to receive  hci event, errno %d",
1490                 __func__, ret_val);
1491         return ret_val;
1492     }
1493
1494     ever_evt = (struct rtk_reset_evt *)(fw_info->rsp_para);
1495
1496     RTKBT_INFO("%s: status %d ", __func__, ever_evt->status);
1497
1498     //sleep 300ms for channel reset.
1499     msleep(300);
1500     RTKBT_INFO("%s: Wait channel reset for 300ms",__func__);
1501
1502     return ret_val;
1503 }
1504
1505 int read_localversion(firmware_info* fw_info)
1506 {
1507     struct rtk_localversion_evt *ever_evt;
1508     int ret_val;
1509
1510     if (!fw_info)
1511         return -ENODEV;
1512
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;
1516
1517     ret_val = send_hci_cmd(fw_info);
1518     if (ret_val < 0) {
1519         RTKBT_ERR("%s: Failed to send  hci cmd 0x%04x, errno %d",
1520                 __func__, fw_info->cmd_hdr->opcode, ret_val);
1521         return ret_val;
1522     }
1523
1524     ret_val = rcv_hci_evt(fw_info);
1525     if (ret_val < 0) {
1526         RTKBT_ERR("%s: Failed to receive  hci event, errno %d",
1527                 __func__, ret_val);
1528         return ret_val;
1529     }
1530
1531     ever_evt = (struct rtk_localversion_evt *)(fw_info->rsp_para);
1532
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.
1540     msleep(300);
1541     RTKBT_INFO("%s: Wait channel reset for 300ms",__func__);
1542
1543     return ret_val;
1544 }
1545
1546 int get_eversion(firmware_info* fw_info)
1547 {
1548     struct rtk_eversion_evt *ever_evt;
1549     int ret_val;
1550
1551     if (!fw_info)
1552         return -ENODEV;
1553
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;
1557
1558     ret_val = send_hci_cmd(fw_info);
1559     if (ret_val < 0) {
1560         RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d",
1561                 __func__, fw_info->cmd_hdr->opcode, ret_val);
1562         return ret_val;
1563     }
1564
1565     ret_val = rcv_hci_evt(fw_info);
1566     if (ret_val < 0) {
1567         RTKBT_ERR("%s: Failed to receive hci event, errno %d",
1568                 __func__, ret_val);
1569         return ret_val;
1570     }
1571
1572     ever_evt = (struct rtk_eversion_evt *)(fw_info->rsp_para);
1573
1574     RTKBT_INFO("%s: status %d, eversion %d", __func__, ever_evt->status, ever_evt->version);
1575
1576     if (ever_evt->status)
1577         fw_info->patch_entry->eversion = 0;
1578     else
1579         fw_info->patch_entry->eversion = ever_evt->version;
1580
1581     return ret_val;
1582 }
1583
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)
1585 {
1586     static unsigned char config_buf[1024];
1587     unsigned short offset[256];
1588     unsigned char val[256];
1589
1590     struct rtk_bt_vendor_config* config = (struct rtk_bt_vendor_config*) config_buf;
1591     struct rtk_bt_vendor_config_entry* entry = config->entry;
1592
1593     int count = 0,temp = 0, i = 0, j;
1594
1595     memset(config_buf, 0, sizeof(config_buf));
1596     memset(offset, 0, sizeof(offset));
1597     memset(val, 0, sizeof(val));
1598
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;
1602
1603     count = getAltSettings(patch_entry, offset, sizeof(offset)/sizeof(unsigned short));
1604     if(count <= 0){
1605         RTKBT_INFO("rtk_update_altsettings: No AltSettings");
1606         return;
1607     }else{
1608         RTKBT_INFO("rtk_update_altsettings: %d AltSettings", count);
1609     }
1610
1611     RTKBT_INFO("ORG Config len=%08x:\n", org_config_len);
1612     for(i=0;i<=org_config_len;i+=0x10)
1613     {
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]);
1617     }
1618
1619     if (config->data_len != org_config_len - sizeof(struct rtk_bt_vendor_config))
1620     {
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)));
1622         return;
1623     }
1624
1625     for (i=0; i<config->data_len;)
1626     {
1627         for(j = 0; j < count;j++)
1628         {
1629             if(entry->offset == offset[j])
1630                 offset[j] = 0;
1631         }
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);
1635         }
1636         temp = entry->entry_len + sizeof(struct rtk_bt_vendor_config_entry);
1637         i += temp;
1638         entry = (struct rtk_bt_vendor_config_entry*)((uint8_t*)entry + temp);
1639     }
1640     for(j = 0; j < count;j++){
1641         if(offset[j] == 0)
1642             continue;
1643         entry->entry_len = getAltSettingVal(patch_entry, offset[j], val);
1644         if(entry->entry_len <= 0)
1645             continue;
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);
1650         i += temp;
1651         entry = (struct rtk_bt_vendor_config_entry*)((uint8_t*)entry + temp);
1652     }
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);
1656
1657     return;
1658 }
1659
1660 int load_firmware(firmware_info *fw_info, uint8_t **buff)
1661 {
1662     const struct firmware *fw, *cfg;
1663     struct usb_device *udev;
1664     patch_info *patch_entry;
1665     char *config_name, *fw_name;
1666     int fw_len = 0;
1667     int ret_val;
1668
1669     int config_len = 0, buf_len = -1;
1670     uint8_t *buf = *buff, *config_file_buf = NULL;
1671     uint8_t *epatch_buf = NULL;
1672
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__);
1680
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;
1688     }else{
1689         fw_name = patch_entry->patch_name;
1690     }
1691
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);
1694
1695     ret_val = request_firmware(&cfg, config_name, &udev->dev);
1696     if (ret_val < 0)
1697         config_len = 0;
1698     else {
1699         int i;
1700         rtk_update_altsettings(patch_entry, cfg->data, cfg->size, &config_file_buf, &config_len);
1701
1702         RTKBT_INFO("Final Config len=%08x:\n", config_len);
1703         for(i=0;i<=config_len;i+=0x10)
1704         {
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]);
1708         }
1709
1710         release_firmware(cfg);
1711     }
1712
1713     ret_val = request_firmware(&fw, fw_name, &udev->dev);
1714     if (ret_val < 0)
1715         goto fw_fail;
1716     else {
1717         epatch_buf = vmalloc(fw->size);
1718         RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__);
1719         if (!epatch_buf) {
1720             release_firmware(fw);
1721             goto fw_fail;
1722         }
1723         memcpy(epatch_buf, fw->data, fw->size);
1724         fw_len = fw->size;
1725         buf_len = fw_len + config_len;
1726         release_firmware(fw);
1727     }
1728
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;
1734         } else {
1735             if (!(buf = kzalloc(buf_len, GFP_KERNEL))) {
1736                 RTKBT_ERR("%s: Failed to allocate mem for fw&config", __func__);
1737                 buf_len = -1;
1738             } else {
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);
1744                 vfree(epatch_buf);
1745                 RTKBT_INFO("%s:ROM_LMP_8723a vfree(epatch_buf)", __func__);
1746                 epatch_buf = NULL;
1747                 if (config_len)
1748                     memcpy(buf + buf_len - config_len, config_file_buf, config_len);
1749             }
1750         }
1751     } else {
1752         RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__);
1753
1754         RTKBT_DBG("%s: reset_channel before get_eversion from bt controller", __func__);
1755         ret_val = reset_channel(fw_info);
1756         if (ret_val < 0) {
1757             RTKBT_ERR("%s: Failed to reset_channel, errno %d", __func__, ret_val);
1758             goto fw_fail;
1759         }
1760 //        read_localversion(fw_info);
1761         RTKBT_DBG("%s: get_eversion from bt controller", __func__);
1762
1763         ret_val = get_eversion(fw_info);
1764         if (ret_val < 0) {
1765             RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val);
1766             goto fw_fail;
1767         }
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;
1772         } else {
1773             uint8_t *temp;
1774             temp = epatch_buf+buf_len-config_len - 5;
1775             do {
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))) {
1780                         int k;
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));
1784                         }
1785                     }
1786                     RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__,
1787                             patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data));
1788                     break;
1789                 }
1790                 temp -= *(temp-1) + 2;
1791             } while (*temp != 0xFF);
1792
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)]);
1796                 if (patch_lmp.data)
1797                     kfree(patch_lmp.data);
1798                 need_download_fw = 0;
1799             } else {
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)]);
1802                 if (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;
1807                 } else {
1808                     epatch_info = (struct rtk_epatch*)epatch_buf;
1809                     patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version;
1810
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);
1815
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__);
1820                         ret_val = -1;
1821                         goto fw_fail ;
1822                     }
1823
1824                     buf_len = p_epatch_entry->patch_length + config_len;
1825                     RTKBT_DBG("buf_len = 0x%x", buf_len);
1826
1827                     if (!(buf = kzalloc(buf_len, GFP_KERNEL))) {
1828                         RTKBT_ERR("%s: Can't alloc memory for  fw&config", __func__);
1829                         buf_len = -1;
1830                     } else {
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);
1834                     }
1835                     vfree(epatch_buf);
1836                     RTKBT_INFO("%s: vfree(epatch_buf)", __func__);
1837                     epatch_buf = NULL;
1838
1839                     if (config_len)
1840                         memcpy(&buf[buf_len - config_len], config_file_buf, config_len);
1841                 }
1842             }
1843         }
1844     }
1845
1846     RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__,
1847             (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not");
1848
1849     if (buf && buf_len > 0 && need_download_fw)
1850         *buff = buf;
1851
1852     RTKBT_DBG("%s: done", __func__);
1853
1854     return buf_len;
1855
1856 fw_fail:
1857     return ret_val;
1858 }
1859
1860 #if SUSPNED_DW_FW
1861 static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff)
1862 {
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};
1868     int fw_len = 0;
1869     int ret_val;
1870
1871     int config_len = 0, buf_len = -1;
1872     uint8_t *buf = *buff, *config_file_buf = NULL;
1873     uint8_t *epatch_buf = NULL;
1874
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__);
1881
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);
1887
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);
1890
1891     ret_val = request_firmware(&cfg, config_name, &udev->dev);
1892     if (ret_val < 0)
1893         config_len = 0;
1894     else {
1895         config_file_buf = vmalloc(cfg->size);
1896         RTKBT_INFO("%s: epatch_buf = vmalloc(cfg->size)", __func__);
1897         if (!config_file_buf)
1898             return -ENOMEM;
1899         memcpy(config_file_buf, cfg->data, cfg->size);
1900         config_len = cfg->size;
1901         release_firmware(cfg);
1902     }
1903
1904     ret_val = request_firmware(&fw, fw_name, &udev->dev);
1905     if (ret_val < 0)
1906         goto fw_fail;
1907     else {
1908         epatch_buf = vmalloc(fw->size);
1909         RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__);
1910         if (!epatch_buf) {
1911             release_firmware(fw);
1912             goto fw_fail;
1913         }
1914         memcpy(epatch_buf, fw->data, fw->size);
1915         fw_len = fw->size;
1916         buf_len = fw_len + config_len;
1917         release_firmware(fw);
1918     }
1919
1920     RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__);
1921
1922     RTKBT_DBG("%s: get_eversion from bt controller", __func__);
1923
1924     ret_val = get_eversion(fw_info);
1925     if (ret_val < 0) {
1926         RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val);
1927         goto fw_fail;
1928     }
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;
1933     } else {
1934         uint8_t *temp;
1935         temp = epatch_buf+buf_len-config_len - 5;
1936         do {
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))) {
1941                     int k;
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));
1945                     }
1946                 }
1947                 RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__,
1948                     patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data));
1949                 break;
1950             }
1951             temp -= *(temp-1) + 2;
1952         } while (*temp != 0xFF);
1953
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)]);
1957             if (patch_lmp.data)
1958                 kfree(patch_lmp.data);
1959             need_download_fw = 0;
1960         } else {
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)]);
1963             if (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;
1968             } else {
1969                 epatch_info = (struct rtk_epatch*)epatch_buf;
1970                 patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version;
1971
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);
1976
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__);
1981                     ret_val = -1;
1982                     goto fw_fail ;
1983                 }
1984
1985                 buf_len = p_epatch_entry->patch_length + config_len;
1986                 RTKBT_DBG("buf_len = 0x%x", buf_len);
1987
1988                 if (!(buf = kzalloc(buf_len, GFP_KERNEL))) {
1989                     RTKBT_ERR("%s: Can't alloc memory for  fw&config", __func__);
1990                     buf_len = -1;
1991                 } else {
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);
1995                 }
1996                 vfree(epatch_buf);
1997                 RTKBT_INFO("%s: vfree(epatch_buf)", __func__);
1998                 epatch_buf = NULL;
1999
2000                 if (config_len)
2001                     memcpy(&buf[buf_len - config_len], config_file_buf, config_len);
2002             }
2003         }
2004     }
2005
2006     if (config_file_buf){
2007         vfree(config_file_buf);
2008         config_file_buf = NULL;
2009         RTKBT_INFO("%s: vfree(config_file_buf)", __func__);
2010         }
2011
2012     RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__,
2013             (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not");
2014
2015     if (buf && buf_len > 0 && need_download_fw)
2016         *buff = buf;
2017
2018     RTKBT_DBG("%s: done", __func__);
2019
2020     return buf_len;
2021
2022 fw_fail:
2023     if (config_file_buf){
2024         vfree(config_file_buf);
2025         config_file_buf = NULL;
2026         }
2027     RTKBT_INFO("%s: fw_fail vfree(config_file_buf)", __func__);
2028     return ret_val;
2029 }
2030 #endif
2031
2032 int get_firmware(firmware_info *fw_info, int cached)
2033 {
2034     patch_info *patch_entry = fw_info->patch_entry;
2035
2036     RTKBT_INFO("%s: start, cached %d,patch_entry->fw_len= %d", __func__, cached,patch_entry->fw_len);
2037
2038     if (cached > 0) {
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)
2042                 return -ENOMEM;
2043             memcpy(fw_info->fw_data, patch_entry->fw_cache, patch_entry->fw_len);
2044             fw_info->fw_len = patch_entry->fw_len;
2045         } else {
2046             fw_info->fw_len = load_firmware(fw_info, &fw_info->fw_data);
2047             if (fw_info->fw_len <= 0)
2048                 return -1;
2049         }
2050     } else {
2051         fw_info->fw_len = load_firmware(fw_info, &fw_info->fw_data);
2052         if (fw_info->fw_len <= 0)
2053             return -1;
2054     }
2055
2056     return 0;
2057 }
2058
2059 #if SUSPNED_DW_FW
2060 static int get_suspend_firmware(firmware_info *fw_info, int cached)
2061 {
2062     patch_info *patch_entry = fw_info->patch_entry;
2063
2064     RTKBT_INFO("%s: start, cached %d,patch_entry->fw_len= %d", __func__, cached,patch_entry->fw_len);
2065
2066     if (cached > 0) {
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)
2070                 return -ENOMEM;
2071             memcpy(fw_info->fw_data, patch_entry->fw_cache, patch_entry->fw_len);
2072             fw_info->fw_len = patch_entry->fw_len;
2073         } else {
2074             fw_info->fw_len = load_suspend_firmware(fw_info, &fw_info->fw_data);
2075             if (fw_info->fw_len <= 0)
2076                 return -1;
2077         }
2078     } else {
2079         fw_info->fw_len = load_suspend_firmware(fw_info, &fw_info->fw_data);
2080         if (fw_info->fw_len <= 0)
2081             return -1;
2082     }
2083
2084     return 0;
2085 }
2086 #endif
2087
2088 /*
2089  * Open the log message only if in debugging,
2090  * or it will decelerate download procedure.
2091  */
2092 int download_data(firmware_info *fw_info)
2093 {
2094     download_cp *cmd_para;
2095     download_rp *evt_para;
2096     uint8_t *pcur;
2097     int pkt_len, frag_num, frag_len;
2098     int i, ret_val;
2099     int ncmd = 1, step = 1;
2100
2101     RTKBT_DBG("%s: start", __func__);
2102
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;
2109
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);
2116         }
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);
2121
2122         if (step > 0) {
2123             ret_val = send_hci_cmd(fw_info);
2124             if (ret_val < 0) {
2125                 RTKBT_DBG("%s: Failed to send frag num %d", __func__, cmd_para->index);
2126                 return ret_val;
2127             } else
2128                 RTKBT_DBG("%s: Send frag num %d", __func__, cmd_para->index);
2129
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;
2133                 continue;
2134             }
2135         }
2136
2137         while (ncmd > 0) {
2138             ret_val = rcv_hci_evt(fw_info);
2139             if (ret_val < 0) {
2140                 RTKBT_ERR("%s: rcv_hci_evt err %d", __func__, ret_val);
2141                 return ret_val;
2142             } else {
2143                 RTKBT_DBG("%s: Receive acked frag num %d", __func__, evt_para->index);
2144                 ncmd--;
2145             }
2146
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);
2150                 return -1;
2151             }
2152
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);
2155                 goto end;
2156             }
2157         }
2158
2159         ncmd = step = fw_info->cmd_cmp->ncmd;
2160         pcur += PATCH_SEG_MAX;
2161         RTKBT_DBG("%s: HCI command packet num %d", __func__, ncmd);
2162     }
2163
2164     /*
2165      * It is tricky that Host cannot receive DATA_END index from BT
2166      * controller, at least for 8723au. We are doomed if failed.
2167      */
2168 #if 0
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);
2174             if (ret_val < 0) {
2175                 RTKBT_ERR("%s: rcv_hci_evt err %d", __func__, ret_val);
2176                 return ret_val;
2177             }
2178             if (0 != evt_para->status)
2179                 return -1;
2180             RTKBT_DBG("%s: continue to receive acked frag num %d", __func__, evt_para->index);
2181         }
2182     }
2183 #endif
2184 end:
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;
2188 }
2189
2190 int download_patch(firmware_info *fw_info, int cached)
2191 {
2192     int ret_val = 0;
2193
2194     RTKBT_DBG("%s: Download fw patch start, cached %d", __func__, cached);
2195
2196     if (!fw_info || !fw_info->patch_entry) {
2197         RTKBT_ERR("%s: No patch entry exists(fw_info %p)", __func__, fw_info);
2198         ret_val = -1;
2199         goto end;
2200     }
2201
2202     /*
2203      * step1: get local firmware if existed
2204      * step2: check firmware version
2205      * step3: download firmware if updated
2206      */
2207     ret_val = get_firmware(fw_info, cached);
2208     if (ret_val < 0) {
2209         RTKBT_ERR("%s: Failed to get firmware", __func__);
2210         goto end;
2211     }
2212
2213 #if SUSPNED_DW_FW
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);
2217         if (ret_val < 0) {
2218             RTKBT_ERR("%s: Failed to get suspend firmware", __func__);
2219             goto end;
2220         }
2221     }
2222 #endif
2223
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);
2228         ret_val = -1;
2229         goto free;
2230     }
2231
2232     ret_val = check_fw_version(fw_info);
2233
2234     if (2 == ret_val) {
2235         RTKBT_ERR("%s: Cold reset bt chip only download", __func__);
2236         ret_val = download_data(fw_info);
2237         if (ret_val > 0)
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);
2244         if (ret_val > 0)
2245                 RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val);
2246     }
2247
2248
2249 free:
2250     /* Free fw data after download finished */
2251     kfree(fw_info->fw_data);
2252     fw_info->fw_data = NULL;
2253
2254 end:
2255     return ret_val;
2256 }
2257
2258 #if SUSPNED_DW_FW
2259 static int download_suspend_patch(firmware_info *fw_info, int cached)
2260 {
2261     int ret_val = 0;
2262
2263     RTKBT_DBG("%s: Download fw patch start, cached %d", __func__, cached);
2264
2265     if (!fw_info || !fw_info->patch_entry) {
2266         RTKBT_ERR("%s: No patch entry exists(fw_info %p)", __func__, fw_info);
2267         ret_val = -1;
2268         goto end;
2269     }
2270
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);
2275         ret_val = -1;
2276         goto free;
2277     }
2278
2279     ret_val = check_fw_version(fw_info);
2280
2281     if (2 == ret_val) {
2282         RTKBT_ERR("%s: Cold reset bt chip only download", __func__);
2283         ret_val = download_data(fw_info);
2284         if (ret_val > 0)
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);
2291         if (ret_val > 0)
2292                 RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val);
2293     }
2294
2295
2296 free:
2297     /* Free fw data after download finished */
2298     kfree(fw_info->fw_data);
2299     fw_info->fw_data = NULL;
2300
2301 end:
2302     return ret_val;
2303 }
2304
2305 static void suspend_firmware_info_init(firmware_info *fw_info)
2306 {
2307     RTKBT_DBG("%s: start", __func__);
2308     if(!fw_info)
2309         return;
2310
2311     fw_info_4_suspend= kzalloc(sizeof(*fw_info), GFP_KERNEL);
2312     if (!fw_info_4_suspend)
2313         goto error;
2314
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);
2318         goto error;
2319     }
2320
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);
2325         goto error;
2326     }
2327
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);
2333         goto error;
2334     }
2335
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;
2345
2346     return;
2347 error:
2348     RTKBT_DBG("%s: fail !", __func__);
2349     fw_info_4_suspend = NULL;
2350     return;
2351 }
2352 #endif
2353
2354 #if SET_WAKEUP_DEVICE
2355 static void set_wakeup_device_from_conf(firmware_info *fw_info)
2356 {
2357     uint8_t paired_wakeup_bdaddr[7];
2358     uint8_t num = 0;
2359     int i;
2360     struct file *fp;
2361     mm_segment_t fs;
2362     loff_t pos;
2363
2364     memset(paired_wakeup_bdaddr, 0, 7);
2365     fp = filp_open(SET_WAKEUP_DEVICE_CONF, O_RDWR, 0);
2366     if (!IS_ERR(fp)) {
2367         fs = get_fs();
2368         set_fs(KERNEL_DS);
2369         pos = 0;
2370         //read number
2371         vfs_read(fp, &num, 1, &pos);
2372         RTKBT_DBG("read number = %d", num);
2373         if(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);
2380             }
2381         }
2382         filp_close(fp, NULL);
2383         set_fs(fs);
2384     }
2385     else {
2386         RTKBT_ERR("open wakeup config file fail! errno = %ld", PTR_ERR(fp));
2387     }
2388 }
2389 #endif
2390
2391 firmware_info *firmware_info_init(struct usb_interface *intf)
2392 {
2393     struct usb_device *udev = interface_to_usbdev(intf);
2394     firmware_info *fw_info;
2395
2396     RTKBT_DBG("%s: start", __func__);
2397
2398     fw_info = kzalloc(sizeof(*fw_info), GFP_KERNEL);
2399     if (!fw_info)
2400         return NULL;
2401
2402     fw_info->send_pkt = kzalloc(PKT_LEN, GFP_KERNEL);
2403     if (!fw_info->send_pkt) {
2404         kfree(fw_info);
2405         return NULL;
2406     }
2407
2408     fw_info->rcv_pkt = kzalloc(PKT_LEN, GFP_KERNEL);
2409     if (!fw_info->rcv_pkt) {
2410         kfree(fw_info->send_pkt);
2411         kfree(fw_info);
2412         return NULL;
2413     }
2414
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);
2419         kfree(fw_info);
2420         return NULL;
2421     }
2422
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;
2432
2433 #if SUSPNED_DW_FW
2434     suspend_firmware_info_init(fw_info);
2435 #endif
2436
2437 #if BTUSB_RPM
2438     RTKBT_INFO("%s: Auto suspend is enabled", __func__);
2439     usb_enable_autosuspend(udev);
2440     pm_runtime_set_autosuspend_delay(&(udev->dev), 2000);
2441 #else
2442     RTKBT_INFO("%s: Auto suspend is disabled", __func__);
2443     usb_disable_autosuspend(udev);
2444 #endif
2445
2446 #if BTUSB_WAKEUP_HOST
2447     device_wakeup_enable(&udev->dev);
2448 #endif
2449
2450     return fw_info;
2451 }
2452
2453 void firmware_info_destroy(struct usb_interface *intf)
2454 {
2455     firmware_info *fw_info;
2456     struct usb_device *udev;
2457     struct btusb_data *data;
2458
2459     udev = interface_to_usbdev(intf);
2460     data = usb_get_intfdata(intf);
2461
2462     fw_info = data->fw_info;
2463     if (!fw_info)
2464         return;
2465
2466 #if BTUSB_RPM
2467     usb_disable_autosuspend(udev);
2468 #endif
2469
2470     /*
2471      * In order to reclaim fw data mem, we free fw_data immediately
2472      * after download patch finished instead of here.
2473      */
2474     kfree(fw_info->rcv_pkt);
2475     kfree(fw_info->send_pkt);
2476     kfree(fw_info);
2477
2478 #if SUSPNED_DW_FW
2479     if (!fw_info_4_suspend)
2480         return;
2481
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;
2486 #endif
2487 }
2488
2489 static struct usb_driver btusb_driver;
2490
2491 static struct usb_device_id btusb_table[] = {
2492     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
2493                      USB_DEVICE_ID_MATCH_INT_INFO,
2494       .idVendor = 0x0bda,
2495       .bInterfaceClass = 0xe0,
2496       .bInterfaceSubClass = 0x01,
2497       .bInterfaceProtocol = 0x01 },
2498
2499     { .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
2500                      USB_DEVICE_ID_MATCH_INT_INFO,
2501       .idVendor = 0x13d3,
2502       .bInterfaceClass = 0xe0,
2503       .bInterfaceSubClass = 0x01,
2504       .bInterfaceProtocol = 0x01 },
2505
2506     { }
2507 };
2508
2509 MODULE_DEVICE_TABLE(usb, btusb_table);
2510
2511 static int inc_tx(struct btusb_data *data)
2512 {
2513     unsigned long flags;
2514     int rv;
2515
2516     spin_lock_irqsave(&data->txlock, flags);
2517     rv = test_bit(BTUSB_SUSPENDING, &data->flags);
2518     if (!rv)
2519         data->tx_in_flight++;
2520     spin_unlock_irqrestore(&data->txlock, flags);
2521
2522     return rv;
2523 }
2524
2525 void check_sco_event(struct urb *urb)
2526 {
2527     u8* opcode = (u8*)(urb->transfer_buffer);
2528     u8 status;
2529     static uint16_t sco_handle = 0;
2530     uint16_t handle;
2531     struct hci_dev *hdev = urb->context;
2532     struct btusb_data *data = GET_DRV_DATA(hdev);
2533
2534     switch (*opcode) {
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;
2539         if (status == 0) {
2540             hdev->conn_hash.sco_num++;
2541             schedule_work(&data->work);
2542         }
2543         break;
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);
2551         }
2552         break;
2553     default:
2554         RTKBT_DBG("%s: event 0x%02x", __func__, *opcode);
2555         break;
2556     }
2557 }
2558
2559 static void btusb_intr_complete(struct urb *urb)
2560 {
2561     struct hci_dev *hdev = urb->context;
2562     struct btusb_data *data = GET_DRV_DATA(hdev);
2563     int err;
2564
2565     RTKBT_DBG("%s: urb %p status %d count %d ", __func__,
2566             urb, urb->status, urb->actual_length);
2567
2568 //    check_sco_event(urb);
2569
2570     if (!test_bit(HCI_RUNNING, &hdev->flags))
2571         return;
2572
2573
2574     if (urb->status == 0) {
2575         hdev->stat.byte_rx += urb->actual_length;
2576
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++;
2582         }
2583     }
2584     /* Avoid suspend failed when usb_kill_urb */
2585     else if(urb->status == -ENOENT)    {
2586         return;
2587     }
2588
2589
2590     if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
2591         return;
2592
2593     usb_mark_last_busy(data->udev);
2594     usb_anchor_urb(urb, &data->intr_anchor);
2595
2596     err = usb_submit_urb(urb, GFP_ATOMIC);
2597     if (err < 0) {
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);
2604     }
2605 }
2606
2607 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
2608 {
2609     struct btusb_data *data = GET_DRV_DATA(hdev);
2610     struct urb *urb;
2611     unsigned char *buf;
2612     unsigned int pipe;
2613     int err, size;
2614
2615     if (!data->intr_ep)
2616         return -ENODEV;
2617
2618     urb = usb_alloc_urb(0, mem_flags);
2619     if (!urb)
2620         return -ENOMEM;
2621
2622     size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
2623
2624     buf = kmalloc(size, mem_flags);
2625     if (!buf) {
2626         usb_free_urb(urb);
2627         return -ENOMEM;
2628     }
2629
2630     RTKBT_DBG("%s: mMaxPacketSize %d, bEndpointAddress 0x%02x",
2631             __func__, size, data->intr_ep->bEndpointAddress);
2632
2633     pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
2634
2635     usb_fill_int_urb(urb, data->udev, pipe, buf, size,
2636                         btusb_intr_complete, hdev,
2637                         data->intr_ep->bInterval);
2638
2639     urb->transfer_flags |= URB_FREE_BUFFER;
2640
2641     usb_anchor_urb(urb, &data->intr_anchor);
2642
2643     err = usb_submit_urb(urb, mem_flags);
2644     if (err < 0) {
2645         RTKBT_ERR("%s: Failed to submit urb %p, err %d",
2646                 __func__, urb, err);
2647         usb_unanchor_urb(urb);
2648     }
2649
2650     usb_free_urb(urb);
2651
2652     return err;
2653 }
2654
2655 static void btusb_bulk_complete(struct urb *urb)
2656 {
2657     struct hci_dev *hdev = urb->context;
2658     struct btusb_data *data = GET_DRV_DATA(hdev);
2659     int err;
2660
2661     RTKBT_DBG("%s: urb %p status %d count %d",
2662             __func__, urb, urb->status, urb->actual_length);
2663
2664     if (!test_bit(HCI_RUNNING, &hdev->flags))
2665         return;
2666
2667     if (urb->status == 0) {
2668         hdev->stat.byte_rx += urb->actual_length;
2669
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++;
2675         }
2676     }
2677     /* Avoid suspend failed when usb_kill_urb */
2678     else if(urb->status == -ENOENT)    {
2679         return;
2680     }
2681
2682
2683     if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
2684         return;
2685
2686     usb_anchor_urb(urb, &data->bulk_anchor);
2687     usb_mark_last_busy(data->udev);
2688
2689     err = usb_submit_urb(urb, GFP_ATOMIC);
2690     if (err < 0) {
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);
2697     }
2698 }
2699
2700 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
2701 {
2702     struct btusb_data *data = GET_DRV_DATA(hdev);
2703     struct urb *urb;
2704     unsigned char *buf;
2705     unsigned int pipe;
2706     int err, size = HCI_MAX_FRAME_SIZE;
2707
2708     RTKBT_DBG("%s: hdev name %s", __func__, hdev->name);
2709
2710     if (!data->bulk_rx_ep)
2711         return -ENODEV;
2712
2713     urb = usb_alloc_urb(0, mem_flags);
2714     if (!urb)
2715         return -ENOMEM;
2716
2717     buf = kmalloc(size, mem_flags);
2718     if (!buf) {
2719         usb_free_urb(urb);
2720         return -ENOMEM;
2721     }
2722
2723     pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
2724
2725     usb_fill_bulk_urb(urb, data->udev, pipe,
2726                     buf, size, btusb_bulk_complete, hdev);
2727
2728     urb->transfer_flags |= URB_FREE_BUFFER;
2729
2730     usb_mark_last_busy(data->udev);
2731     usb_anchor_urb(urb, &data->bulk_anchor);
2732
2733     err = usb_submit_urb(urb, mem_flags);
2734     if (err < 0) {
2735         RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb, err);
2736         usb_unanchor_urb(urb);
2737     }
2738
2739     usb_free_urb(urb);
2740
2741     return err;
2742 }
2743
2744 static void btusb_isoc_complete(struct urb *urb)
2745 {
2746     struct hci_dev *hdev = urb->context;
2747     struct btusb_data *data = GET_DRV_DATA(hdev);
2748     int i, err;
2749
2750
2751     RTKBT_DBG("%s: urb %p status %d count %d",
2752             __func__, urb, urb->status, urb->actual_length);
2753
2754     if (!test_bit(HCI_RUNNING, &hdev->flags))
2755         return;
2756
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;
2761
2762             if (urb->iso_frame_desc[i].status)
2763                 continue;
2764
2765             hdev->stat.byte_rx += length;
2766
2767             if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
2768                         urb->transfer_buffer + offset,
2769                                 length) < 0) {
2770                 RTKBT_ERR("%s: Corrupted SCO packet", __func__);
2771                 hdev->stat.err_rx++;
2772             }
2773         }
2774     }
2775     /* Avoid suspend failed when usb_kill_urb */
2776     else if(urb->status == -ENOENT)    {
2777         return;
2778     }
2779
2780
2781     if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
2782         return;
2783
2784     usb_anchor_urb(urb, &data->isoc_anchor);
2785     i = 0;
2786 retry:
2787     err = usb_submit_urb(urb, GFP_ATOMIC);
2788     if (err < 0) {
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);
2794         if (i < 10) {
2795             i++;
2796             mdelay(1);
2797             goto retry;
2798         }
2799
2800         usb_unanchor_urb(urb);
2801     }
2802 }
2803
2804 static inline void fill_isoc_descriptor(struct urb *urb, int len, int mtu)
2805 {
2806     int i, offset = 0;
2807
2808     RTKBT_DBG("%s: len %d mtu %d", __func__, len, mtu);
2809
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;
2814     }
2815
2816     if (len && i < BTUSB_MAX_ISOC_FRAMES) {
2817         urb->iso_frame_desc[i].offset = offset;
2818         urb->iso_frame_desc[i].length = len;
2819         i++;
2820     }
2821
2822     urb->number_of_packets = i;
2823 }
2824
2825 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
2826 {
2827     struct btusb_data *data = GET_DRV_DATA(hdev);
2828     struct urb *urb;
2829     unsigned char *buf;
2830     unsigned int pipe;
2831     int err, size;
2832
2833     if (!data->isoc_rx_ep)
2834         return -ENODEV;
2835
2836     urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
2837     if (!urb)
2838         return -ENOMEM;
2839
2840     size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
2841                         BTUSB_MAX_ISOC_FRAMES;
2842
2843     buf = kmalloc(size, mem_flags);
2844     if (!buf) {
2845         usb_free_urb(urb);
2846         return -ENOMEM;
2847     }
2848
2849     pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
2850
2851     urb->dev      = data->udev;
2852     urb->pipe     = pipe;
2853     urb->context  = hdev;
2854     urb->complete = btusb_isoc_complete;
2855     urb->interval = data->isoc_rx_ep->bInterval;
2856
2857     urb->transfer_flags  = URB_FREE_BUFFER | URB_ISO_ASAP;
2858     urb->transfer_buffer = buf;
2859     urb->transfer_buffer_length = size;
2860
2861     fill_isoc_descriptor(urb, size,
2862             le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
2863
2864     usb_anchor_urb(urb, &data->isoc_anchor);
2865
2866     err = usb_submit_urb(urb, mem_flags);
2867     if (err < 0) {
2868         RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb, err);
2869         usb_unanchor_urb(urb);
2870     }
2871
2872     usb_free_urb(urb);
2873
2874     return err;
2875 }
2876
2877 static void btusb_tx_complete(struct urb *urb)
2878 {
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);
2882
2883     if (!test_bit(HCI_RUNNING, &hdev->flags))
2884         goto done;
2885
2886     if (!urb->status)
2887         hdev->stat.byte_tx += urb->transfer_buffer_length;
2888     else
2889         hdev->stat.err_tx++;
2890
2891 done:
2892     spin_lock(&data->txlock);
2893     data->tx_in_flight--;
2894     spin_unlock(&data->txlock);
2895
2896     kfree(urb->setup_packet);
2897
2898     kfree_skb(skb);
2899 }
2900
2901 static void btusb_isoc_tx_complete(struct urb *urb)
2902 {
2903     struct sk_buff *skb = urb->context;
2904     struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2905
2906     RTKBT_DBG("%s: urb %p status %d count %d",
2907             __func__, urb, urb->status, urb->actual_length);
2908
2909     if (skb && hdev) {
2910         if (!test_bit(HCI_RUNNING, &hdev->flags))
2911             goto done;
2912
2913         if (!urb->status)
2914             hdev->stat.byte_tx += urb->transfer_buffer_length;
2915         else
2916             hdev->stat.err_tx++;
2917     } else
2918         RTKBT_ERR("%s: skb 0x%p hdev 0x%p", __func__, skb, hdev);
2919
2920 done:
2921     kfree(urb->setup_packet);
2922
2923     kfree_skb(skb);
2924 }
2925
2926 static int btusb_open(struct hci_dev *hdev)
2927 {
2928     struct btusb_data *data = GET_DRV_DATA(hdev);
2929     int err = 0;
2930
2931     RTKBT_INFO("%s: Start, PM usage count %d", __func__,
2932             atomic_read(&(data->intf->pm_usage_cnt)));
2933
2934     err = usb_autopm_get_interface(data->intf);
2935     if (err < 0)
2936         return err;
2937
2938     data->intf->needs_remote_wakeup = 1;
2939
2940     if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
2941         goto done;
2942
2943     if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
2944         goto done;
2945
2946     err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
2947     if (err < 0)
2948         goto failed;
2949
2950     err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
2951     if (err < 0) {
2952         mdelay(URB_CANCELING_DELAY_MS);
2953         usb_kill_anchored_urbs(&data->intr_anchor);
2954         goto failed;
2955     }
2956
2957     set_bit(BTUSB_BULK_RUNNING, &data->flags);
2958     btusb_submit_bulk_urb(hdev, GFP_KERNEL);
2959
2960 done:
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)));
2964     return 0;
2965
2966 failed:
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)));
2972     return err;
2973 }
2974
2975 static void btusb_stop_traffic(struct btusb_data *data)
2976 {
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);
2981 }
2982
2983 static int btusb_close(struct hci_dev *hdev)
2984 {
2985     struct btusb_data *data = GET_DRV_DATA(hdev);
2986     int i, err;
2987
2988     RTKBT_INFO("%s: hci running %lu", __func__, hdev->flags & HCI_RUNNING);
2989
2990     if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
2991         return 0;
2992
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;
2998         }
2999     }
3000
3001     cancel_work_sync(&data->work);
3002     cancel_work_sync(&data->waker);
3003
3004     clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3005     clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3006     clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3007
3008     btusb_stop_traffic(data);
3009     err = usb_autopm_get_interface(data->intf);
3010     if (err < 0)
3011         goto failed;
3012
3013     data->intf->needs_remote_wakeup = 0;
3014     usb_autopm_put_interface(data->intf);
3015
3016 failed:
3017     mdelay(URB_CANCELING_DELAY_MS);
3018     usb_scuttle_anchored_urbs(&data->deferred);
3019     return 0;
3020 }
3021
3022 static int btusb_flush(struct hci_dev *hdev)
3023 {
3024     struct btusb_data *data = GET_DRV_DATA(hdev);
3025
3026     RTKBT_DBG("%s", __func__);
3027
3028     mdelay(URB_CANCELING_DELAY_MS);
3029     usb_kill_anchored_urbs(&data->tx_anchor);
3030
3031     return 0;
3032 }
3033
3034
3035 static int btusb_send_frame(struct sk_buff *skb)
3036 {
3037     struct hci_dev *hdev = (struct hci_dev *) skb->dev;
3038
3039     struct btusb_data *data = GET_DRV_DATA(hdev);
3040     struct usb_ctrlrequest *dr;
3041     struct urb *urb;
3042     unsigned int pipe;
3043     int err;
3044     int retries = 0;
3045
3046     RTKBT_DBG("%s: hdev %p, btusb data %p, pkt type %d",
3047             __func__, hdev, data, bt_cb(skb)->pkt_type);
3048
3049     if (!test_bit(HCI_RUNNING, &hdev->flags))
3050         return -EBUSY;
3051
3052
3053
3054     switch (bt_cb(skb)->pkt_type) {
3055     case HCI_COMMAND_PKT:
3056         print_command(skb);
3057         urb = usb_alloc_urb(0, GFP_ATOMIC);
3058         if (!urb)
3059             return -ENOMEM;
3060
3061         dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
3062         if (!dr) {
3063             usb_free_urb(urb);
3064             return -ENOMEM;
3065         }
3066
3067         dr->bRequestType = data->cmdreq_type;
3068         dr->bRequest     = 0;
3069         dr->wIndex       = 0;
3070         dr->wValue       = 0;
3071         dr->wLength      = __cpu_to_le16(skb->len);
3072
3073         pipe = usb_sndctrlpipe(data->udev, 0x00);
3074
3075         usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
3076                 skb->data, skb->len, btusb_tx_complete, skb);
3077
3078         hdev->stat.cmd_tx++;
3079         break;
3080
3081     case HCI_ACLDATA_PKT:
3082         print_acl(skb, 1);
3083         if (!data->bulk_tx_ep)
3084             return -ENODEV;
3085
3086         urb = usb_alloc_urb(0, GFP_ATOMIC);
3087         if (!urb)
3088             return -ENOMEM;
3089
3090         pipe = usb_sndbulkpipe(data->udev,
3091                     data->bulk_tx_ep->bEndpointAddress);
3092
3093         usb_fill_bulk_urb(urb, data->udev, pipe,
3094                 skb->data, skb->len, btusb_tx_complete, skb);
3095
3096         hdev->stat.acl_tx++;
3097         break;
3098
3099     case HCI_SCODATA_PKT:
3100         print_sco(skb, 1);
3101         if (!data->isoc_tx_ep || SCO_NUM < 1) {
3102             kfree(skb);
3103             return -ENODEV;
3104         }
3105
3106         urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
3107         if (!urb) {
3108             RTKBT_ERR("%s: Failed to allocate mem for sco pkts", __func__);
3109             kfree(skb);
3110             return -ENOMEM;
3111         }
3112
3113         pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
3114
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);
3118
3119         urb->transfer_flags  = URB_ISO_ASAP;
3120
3121         fill_isoc_descriptor(urb, skb->len,
3122                 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
3123
3124         hdev->stat.sco_tx++;
3125         goto skip_waking;
3126
3127     default:
3128         return -EILSEQ;
3129     }
3130
3131     err = inc_tx(data);
3132     if (err) {
3133         usb_anchor_urb(urb, &data->deferred);
3134         schedule_work(&data->waker);
3135         err = 0;
3136         goto done;
3137     }
3138
3139 skip_waking:
3140     usb_anchor_urb(urb, &data->tx_anchor);
3141 retry:
3142     err = usb_submit_urb(urb, GFP_ATOMIC);
3143     if (err < 0) {
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)) {
3147             mdelay(1);
3148
3149             if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)
3150                 print_error_command(skb);
3151             retries++;
3152             goto retry;
3153         }
3154         kfree(urb->setup_packet);
3155         usb_unanchor_urb(urb);
3156     } else
3157         usb_mark_last_busy(data->udev);
3158     usb_free_urb(urb);
3159
3160 done:
3161     return err;
3162 }
3163
3164 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
3165 static void btusb_destruct(struct hci_dev *hdev)
3166 {
3167     struct btusb_data *data = GET_DRV_DATA(hdev);
3168
3169     RTKBT_DBG("%s: name %s", __func__, hdev->name);
3170
3171     kfree(data);
3172 }
3173 #endif
3174
3175 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
3176 {
3177     struct btusb_data *data = GET_DRV_DATA(hdev);
3178
3179     RTKBT_DBG("%s: name %s, evt %d", __func__, hdev->name, evt);
3180
3181     if (SCO_NUM != data->sco_num) {
3182         data->sco_num = SCO_NUM;
3183         schedule_work(&data->work);
3184     }
3185 }
3186
3187 static inline int set_isoc_interface(struct hci_dev *hdev, int altsetting)
3188 {
3189     struct btusb_data *data = GET_DRV_DATA(hdev);
3190     struct usb_interface *intf = data->isoc;
3191     struct usb_endpoint_descriptor *ep_desc;
3192     int i, err;
3193
3194     if (!data->isoc)
3195         return -ENODEV;
3196
3197     err = usb_set_interface(data->udev, 1, altsetting);
3198     if (err < 0) {
3199         RTKBT_ERR("%s: Failed to set interface, altsetting %d, err %d",
3200                 __func__, altsetting, err);
3201         return err;
3202     }
3203
3204     data->isoc_altsetting = altsetting;
3205
3206     data->isoc_tx_ep = NULL;
3207     data->isoc_rx_ep = NULL;
3208
3209     for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3210         ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3211
3212         if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
3213             data->isoc_tx_ep = ep_desc;
3214             continue;
3215         }
3216
3217         if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
3218             data->isoc_rx_ep = ep_desc;
3219             continue;
3220         }
3221     }
3222
3223     if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
3224         RTKBT_ERR("%s: Invalid SCO descriptors", __func__);
3225         return -ENODEV;
3226     }
3227
3228     return 0;
3229 }
3230
3231 static void btusb_work(struct work_struct *work)
3232 {
3233     struct btusb_data *data = container_of(work, struct btusb_data, work);
3234     struct hci_dev *hdev = data->hdev;
3235     int err;
3236     int new_alts;
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);
3240             if (err < 0) {
3241                 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3242                 mdelay(URB_CANCELING_DELAY_MS);
3243                 usb_kill_anchored_urbs(&data->isoc_anchor);
3244                 return;
3245             }
3246
3247             set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
3248         }
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];
3253         } else{
3254             new_alts = data->sco_num;
3255         }
3256         if (data->isoc_altsetting != new_alts) {
3257 #else
3258         if (data->isoc_altsetting != 2) {
3259             new_alts = 2;
3260 #endif
3261
3262             clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3263             mdelay(URB_CANCELING_DELAY_MS);
3264             usb_kill_anchored_urbs(&data->isoc_anchor);
3265
3266             if (set_isoc_interface(hdev, new_alts) < 0)
3267                 return;
3268         }
3269
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);
3273             else
3274                 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
3275         }
3276     } else {
3277         clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3278         mdelay(URB_CANCELING_DELAY_MS);
3279         usb_kill_anchored_urbs(&data->isoc_anchor);
3280
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);
3284     }
3285 }
3286
3287 static void btusb_waker(struct work_struct *work)
3288 {
3289     struct btusb_data *data = container_of(work, struct btusb_data, waker);
3290     int err;
3291
3292     RTKBT_DBG("%s: PM usage count %d", __func__,
3293             atomic_read(&data->intf->pm_usage_cnt));
3294
3295     err = usb_autopm_get_interface(data->intf);
3296     if (err < 0)
3297         return;
3298
3299     usb_autopm_put_interface(data->intf);
3300 }
3301
3302
3303 //#ifdef CONFIG_HAS_EARLYSUSPEND
3304 #if 0
3305 static void btusb_early_suspend(struct early_suspend *h)
3306 {
3307     struct btusb_data *data;
3308     firmware_info *fw_info;
3309     patch_info *patch_entry;
3310
3311     RTKBT_INFO("%s", __func__);
3312
3313     data = container_of(h, struct btusb_data, early_suspend);
3314     fw_info = data->fw_info;
3315     patch_entry = fw_info->patch_entry;
3316
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__);
3321     }
3322 }
3323
3324 static void btusb_late_resume(struct early_suspend *h)
3325 {
3326     struct btusb_data *data;
3327     firmware_info *fw_info;
3328     patch_info *patch_entry;
3329
3330     RTKBT_INFO("%s", __func__);
3331
3332     data = container_of(h, struct btusb_data, early_suspend);
3333     fw_info = data->fw_info;
3334     patch_entry = fw_info->patch_entry;
3335
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;
3341     }
3342 }
3343 #else
3344 int bt_pm_notify(struct notifier_block *notifier, ulong pm_event, void *unused)
3345 {
3346     struct btusb_data *data;
3347     firmware_info *fw_info;
3348     patch_info *patch_entry;
3349     struct usb_device *udev;
3350
3351     RTKBT_INFO("%s: pm event %ld", __func__, pm_event);
3352
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;
3357
3358     switch (pm_event) {
3359     case PM_SUSPEND_PREPARE:
3360     case PM_HIBERNATION_PREPARE:
3361 #if 0
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__);
3366         }
3367 #endif
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__);
3371 #else
3372             fw_info->intf->needs_binding = 1;
3373             RTKBT_INFO("%s:remote wakeup not supported, binding needed", __func__);
3374 #endif
3375         }
3376         break;
3377
3378     case PM_POST_SUSPEND:
3379     case PM_POST_HIBERNATION:
3380     case PM_POST_RESTORE:
3381 #if 0
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;
3387         }
3388 #endif
3389
3390 #if BTUSB_RPM
3391         usb_disable_autosuspend(udev);
3392         usb_enable_autosuspend(udev);
3393         pm_runtime_set_autosuspend_delay(&(udev->dev), 2000);
3394 #endif
3395         break;
3396
3397     default:
3398         break;
3399     }
3400
3401     return NOTIFY_DONE;
3402 }
3403
3404 int bt_reboot_notify(struct notifier_block *notifier, ulong pm_event, void *unused)
3405 {
3406     struct btusb_data *data;
3407     firmware_info *fw_info;
3408     patch_info *patch_entry;
3409     struct usb_device *udev;
3410
3411     RTKBT_INFO("%s: pm event %ld", __func__, pm_event);
3412
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;
3417
3418     switch (pm_event) {
3419     case SYS_DOWN:
3420         RTKBT_DBG("%s:system down or restart", __func__);
3421     break;
3422
3423     case SYS_HALT:
3424     case SYS_POWER_OFF:
3425 #if SUSPNED_DW_FW
3426         cancel_work_sync(&data->work);
3427
3428         btusb_stop_traffic(data);
3429         mdelay(URB_CANCELING_DELAY_MS);
3430         usb_kill_anchored_urbs(&data->tx_anchor);
3431
3432
3433         if(fw_info_4_suspend) {
3434             download_suspend_patch(fw_info_4_suspend,1);
3435         }
3436             else
3437                     RTKBT_ERR("%s: Failed to download suspend fw", __func__);
3438 #endif
3439
3440 #if SET_WAKEUP_DEVICE
3441         set_wakeup_device_from_conf(fw_info_4_suspend);
3442 #endif
3443         RTKBT_DBG("%s:system halt or power off", __func__);
3444     break;
3445
3446     default:
3447         break;
3448     }
3449
3450     return NOTIFY_DONE;
3451 }
3452
3453 #endif
3454
3455 static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
3456 {
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;
3462     int i, err=0;
3463
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);
3468
3469     /* interface numbers are hardcoded in the spec */
3470     if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3471         return -ENODEV;
3472
3473     RTKBT_DBG("%s: can wakeup = %x, may wakeup = %x", __func__,
3474             device_can_wakeup(&udev->dev), device_may_wakeup(&udev->dev));
3475
3476     data = rtk_alloc(intf);
3477     if (!data)
3478         return -ENOMEM;
3479
3480     for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3481         ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3482
3483         if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3484             data->intr_ep = ep_desc;
3485             continue;
3486         }
3487
3488         if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3489             data->bulk_tx_ep = ep_desc;
3490             continue;
3491         }
3492
3493         if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3494             data->bulk_rx_ep = ep_desc;
3495             continue;
3496         }
3497     }
3498
3499     if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
3500         rtk_free(data);
3501         return -ENODEV;
3502     }
3503
3504     data->cmdreq_type = USB_TYPE_CLASS;
3505
3506     data->udev = udev;
3507     data->intf = intf;
3508
3509     dlfw_dis_state = 0;
3510     spin_lock_init(&queue_lock);
3511     spin_lock_init(&dlfw_lock);
3512     spin_lock_init(&data->lock);
3513
3514     INIT_WORK(&data->work, btusb_work);
3515     INIT_WORK(&data->waker, btusb_waker);
3516     spin_lock_init(&data->txlock);
3517
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);
3523
3524     fw_info = firmware_info_init(intf);
3525     if (fw_info)
3526         data->fw_info = fw_info;
3527     else {
3528         RTKBT_WARN("%s: Failed to initialize fw info", __func__);
3529         /* Skip download patch */
3530         goto end;
3531     }
3532
3533     RTKBT_INFO("%s: download begining...", __func__);
3534
3535 #if CONFIG_BLUEDROID
3536     mutex_lock(&btchr_mutex);
3537 #endif
3538
3539
3540
3541 #if CONFIG_BLUEDROID
3542     mutex_unlock(&btchr_mutex);
3543 #endif
3544
3545     RTKBT_INFO("%s: download ending...", __func__);
3546
3547     hdev = hci_alloc_dev();
3548     if (!hdev) {
3549         rtk_free(data);
3550         data = NULL;
3551         return -ENOMEM;
3552     }
3553
3554     HDEV_BUS = HCI_USB;
3555
3556     data->hdev = hdev;
3557
3558     SET_HCIDEV_DEV(hdev, &intf->dev);
3559
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;
3565
3566 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
3567     hci_set_drvdata(hdev, data);
3568 #else
3569     hdev->driver_data = data;
3570     hdev->destruct = btusb_destruct;
3571     hdev->owner = THIS_MODULE;
3572 #endif
3573
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__);
3578     }
3579 #endif
3580
3581     /* Interface numbers are hardcoded in the specification */
3582     data->isoc = usb_ifnum_to_if(data->udev, 1);
3583     if (data->isoc) {
3584         err = usb_driver_claim_interface(&btusb_driver,
3585                             data->isoc, data);
3586         if (err < 0) {
3587             hci_free_dev(hdev);
3588             hdev = NULL;
3589             rtk_free(data);
3590             data = NULL;
3591             return err;
3592         }
3593     }
3594
3595     err = hci_register_dev(hdev);
3596     if (err < 0) {
3597         hci_free_dev(hdev);
3598         hdev = NULL;
3599         rtk_free(data);
3600         data = NULL;
3601         return err;
3602     }
3603
3604     usb_set_intfdata(intf, data);
3605
3606 //#ifdef CONFIG_HAS_EARLYSUSPEND
3607 #if 0
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);
3612 #else
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);
3617 #endif
3618
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.
3624      */
3625     if (bt_reset == 1) {
3626         hci_hardware_error();
3627         bt_reset = 0;
3628     } else
3629         bt_reset = 0; /* Clear and reset it anyway */
3630 #endif
3631
3632 end:
3633     return 0;
3634 }
3635
3636 static void btusb_disconnect(struct usb_interface *intf)
3637 {
3638     struct btusb_data *data;
3639     struct hci_dev *hdev = NULL;
3640
3641     wait_event_interruptible(bt_dlfw_wait, (check_set_dlfw_state_value(2) == 2));
3642
3643     RTKBT_INFO("%s: usb_interface %p, bInterfaceNumber %d",
3644             __func__, intf, intf->cur_altsetting->desc.bInterfaceNumber);
3645
3646     data = usb_get_intfdata(intf);
3647
3648     if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3649         return;
3650
3651     if (data)
3652         hdev = data->hdev;
3653     else {
3654         RTKBT_WARN("%s: Failed to get bt usb data[Null]", __func__);
3655         return;
3656     }
3657
3658 //#ifdef CONFIG_HAS_EARLYSUSPEND
3659 #if 0
3660     unregister_early_suspend(&data->early_suspend);
3661 #else
3662     unregister_pm_notifier(&data->pm_notifier);
3663     unregister_reboot_notifier(&data->reboot_notifier);
3664 #endif
3665
3666     firmware_info_destroy(intf);
3667
3668 #if CONFIG_BLUEDROID
3669     if (test_bit(HCI_RUNNING, &hdev->flags)) {
3670         RTKBT_INFO("%s: Set BT reset flag", __func__);
3671         bt_reset = 1;
3672     }
3673 #endif
3674
3675     usb_set_intfdata(data->intf, NULL);
3676
3677     if (data->isoc)
3678         usb_set_intfdata(data->isoc, NULL);
3679
3680     hci_unregister_dev(hdev);
3681
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);
3686
3687 #if !CONFIG_BLUEDROID
3688 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
3689     __hci_dev_put(hdev);
3690 #endif
3691 #endif
3692
3693     hci_free_dev(hdev);
3694     rtk_free(data);
3695     data = NULL;
3696     set_dlfw_state_value(0);
3697 }
3698
3699 #ifdef CONFIG_PM
3700 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3701 {
3702     struct btusb_data *data = usb_get_intfdata(intf);
3703     firmware_info *fw_info = data->fw_info;
3704
3705     RTKBT_INFO("%s: event 0x%x, suspend count %d", __func__,
3706             message.event, data->suspend_count);
3707
3708     if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3709         return 0;
3710
3711     if (!test_bit(HCI_RUNNING, &data->hdev->flags))
3712         set_bt_onoff(fw_info, 1);
3713
3714     if (data->suspend_count++)
3715         return 0;
3716
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);
3721     } else {
3722         spin_unlock_irq(&data->txlock);
3723         data->suspend_count--;
3724         RTKBT_ERR("%s: Failed to enter suspend", __func__);
3725         return -EBUSY;
3726     }
3727
3728     cancel_work_sync(&data->work);
3729
3730     btusb_stop_traffic(data);
3731     mdelay(URB_CANCELING_DELAY_MS);
3732     usb_kill_anchored_urbs(&data->tx_anchor);
3733
3734 #if SUSPNED_DW_FW
3735     if(fw_info_4_suspend) {
3736         download_suspend_patch(fw_info_4_suspend,1);
3737     }
3738     else
3739         RTKBT_ERR("%s: Failed to download suspend fw", __func__);
3740 #endif
3741
3742 #if SET_WAKEUP_DEVICE
3743     set_wakeup_device_from_conf(fw_info_4_suspend);
3744 #endif
3745
3746     return 0;
3747 }
3748
3749 static void play_deferred(struct btusb_data *data)
3750 {
3751     struct urb *urb;
3752     int err;
3753
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);
3757         if (err < 0) {
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);
3762         } else {
3763             usb_mark_last_busy(data->udev);
3764         }
3765         usb_free_urb(urb);
3766
3767         data->tx_in_flight++;
3768     }
3769     mdelay(URB_CANCELING_DELAY_MS);
3770     usb_scuttle_anchored_urbs(&data->deferred);
3771 }
3772
3773 static int btusb_resume(struct usb_interface *intf)
3774 {
3775     struct btusb_data *data = usb_get_intfdata(intf);
3776     struct hci_dev *hdev = data->hdev;
3777     firmware_info *fw_info = data->fw_info;
3778     int err = 0;
3779
3780     RTKBT_INFO("%s: Suspend count %d", __func__, data->suspend_count);
3781
3782     if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3783         return 0;
3784
3785     if (--data->suspend_count)
3786         return 0;
3787
3788     /*check_fw_version to check the status of the BT Controller after USB Resume*/
3789     err = check_fw_version(fw_info);
3790     if (err !=0)
3791     {
3792         RTKBT_INFO("%s: BT Controller Power OFF And Return hci_hardware_error:%d", __func__, err);
3793         hci_hardware_error();
3794     }
3795
3796
3797     if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3798         err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3799         if (err < 0) {
3800             clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3801             goto failed;
3802         }
3803     }
3804
3805     if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3806         err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3807         if (err < 0) {
3808             clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3809             goto failed;
3810         }
3811
3812         btusb_submit_bulk_urb(hdev, GFP_NOIO);
3813     }
3814
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);
3818         else
3819             btusb_submit_isoc_urb(hdev, GFP_NOIO);
3820     }
3821
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);
3827
3828     return 0;
3829
3830 failed:
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);
3836
3837     return err;
3838 }
3839 #endif
3840
3841 static struct usb_driver btusb_driver = {
3842     .name        = "rtk_btusb",
3843     .probe        = btusb_probe,
3844     .disconnect    = btusb_disconnect,
3845 #ifdef CONFIG_PM
3846     .suspend    = btusb_suspend,
3847     .resume        = btusb_resume,
3848 #endif
3849 #if CONFIG_RESET_RESUME
3850     .reset_resume    = btusb_resume,
3851 #endif
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,
3856 #endif
3857 };
3858
3859 static int __init btusb_init(void)
3860 {
3861     int err;
3862
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
3866     err = btchr_init();
3867     if (err < 0) {
3868         /* usb register will go on, even bt char register failed */
3869         RTKBT_ERR("Failed to register usb char device interfaces");
3870     } else
3871         bt_char_dev_registered = 1;
3872 #endif
3873     err = usb_register(&btusb_driver);
3874     if (err < 0)
3875         RTKBT_ERR("Failed to register RTK bluetooth USB driver");
3876     return err;
3877 }
3878
3879 static void __exit btusb_exit(void)
3880 {
3881     RTKBT_INFO("Realtek Bluetooth USB driver module exit");
3882 #if CONFIG_BLUEDROID
3883     if (bt_char_dev_registered > 0)
3884         btchr_exit();
3885 #endif
3886     usb_deregister(&btusb_driver);
3887 }
3888
3889 module_init(btusb_init);
3890 module_exit(btusb_exit);
3891
3892
3893 module_param(mp_drv_mode, int, 0644);
3894 MODULE_PARM_DESC(mp_drv_mode, "0: NORMAL; 1: MP MODE");
3895
3896
3897 MODULE_AUTHOR("Realtek Corporation");
3898 MODULE_DESCRIPTION("Realtek Bluetooth USB driver version");
3899 MODULE_VERSION(VERSION);
3900 MODULE_LICENSE("GPL");