Commit | Line | Data |
---|---|---|
b2ef244e JA |
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 "20190311_BT_ANDROID_9.0" | |
43 | #define VERSION "5.0.1" | |
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 uint32_t usb_info; | |
58 | ||
59 | static patch_info fw_patch_table[] = { | |
60 | /* { vid, pid, lmp_sub_default, lmp_sub, everion, mp_fw_name, fw_name, config_name, fw_cache, fw_len, mac_offset } */ | |
61 | { 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 */ | |
62 | { 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 */ | |
63 | { 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 */ | |
64 | { 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 */ | |
65 | { 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*/ | |
66 | ||
67 | { 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 */ | |
68 | { 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 */ | |
69 | { 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 */ | |
70 | { 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 */ | |
71 | ||
72 | { 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 */ | |
73 | { 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 */ | |
74 | { 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 */ | |
75 | { 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 */ | |
76 | { 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 */ | |
77 | { 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 */ | |
78 | { 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 */ | |
79 | { 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 */ | |
80 | { 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 */ | |
81 | { 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 */ | |
82 | ||
83 | { 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 */ | |
84 | { 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 */ | |
85 | { 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 */ | |
86 | { 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 */ | |
87 | { 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 */ | |
88 | ||
89 | { 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 */ | |
90 | { 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 */ | |
91 | { 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 */ | |
92 | { 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 */ | |
93 | { 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 */ | |
94 | { 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 */ | |
95 | { 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 */ | |
96 | { 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 */ | |
97 | { 0x0BDA, 0x8771, 0x8761, 0, 0, "mp_rtl8761b_fw", "rtl8761b_fw", "rtl8761b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8761BU */ | |
98 | ||
99 | { 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 */ | |
100 | { 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 */ | |
101 | { 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 */ | |
102 | { 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 */ | |
103 | { 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 */ | |
104 | { 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 */ | |
105 | { 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 */ | |
106 | ||
107 | { 0x0BDA, 0xB822, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BE */ | |
108 | { 0x0BDA, 0xB82C, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BU */ | |
109 | { 0x0BDA, 0xB023, 0x8822, 0, 0, "mp_rtl8822b_fw", "rtl8822b_fw", "rtl8822b_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8822BE */ | |
110 | { 0x0BDA, 0xB703, 0x8703, 0, 0, "mp_rtl8723c_fw", "rtl8723c_fw", "rtl8723c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_24K}, /* RTL8723CU */ | |
111 | /* todo: RTL8703BU */ | |
112 | ||
113 | { 0x0BDA, 0xD723, 0x8723, 0, 0, "mp_rtl8723d_fw", "rtl8723d_fw", "rtl8723d_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8723DU */ | |
114 | { 0x0BDA, 0xD720, 0x8723, 0, 0, "mp_rtl8723d_fw", "rtl8723d_fw", "rtl8723d_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8723DE */ | |
115 | { 0x0BDA, 0xB820, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU */ | |
116 | { 0x0BDA, 0xC820, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CU */ | |
117 | { 0x0BDA, 0xC821, 0x8821, 0, 0, "mp_rtl8821c_fw", "rtl8821c_fw", "rtl8821c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_3PLUS, MAX_PATCH_SIZE_40K}, /* RTL8821CE */ | |
118 | /* todo: RTL8703CU */ | |
119 | { 0x0BDA, 0xC82C, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CU */ | |
120 | { 0x0BDA, 0xC822, 0x8822, 0, 0, "mp_rtl8822c_fw", "rtl8822c_fw", "rtl8822c_config", NULL, 0 ,CONFIG_MAC_OFFSET_GEN_4PLUS, MAX_PATCH_SIZE_40K}, /* RTL8822CE */ | |
121 | /* NOTE: must append patch entries above the null entry */ | |
122 | { 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0 } | |
123 | }; | |
124 | ||
125 | struct btusb_data { | |
126 | struct hci_dev *hdev; | |
127 | struct usb_device *udev; | |
128 | struct usb_interface *intf; | |
129 | struct usb_interface *isoc; | |
130 | ||
131 | spinlock_t lock; | |
132 | ||
133 | unsigned long flags; | |
134 | ||
135 | struct work_struct work; | |
136 | struct work_struct waker; | |
137 | ||
138 | struct usb_anchor tx_anchor; | |
139 | struct usb_anchor intr_anchor; | |
140 | struct usb_anchor bulk_anchor; | |
141 | struct usb_anchor isoc_anchor; | |
142 | struct usb_anchor deferred; | |
143 | int tx_in_flight; | |
144 | spinlock_t txlock; | |
145 | ||
146 | struct usb_endpoint_descriptor *intr_ep; | |
147 | struct usb_endpoint_descriptor *bulk_tx_ep; | |
148 | struct usb_endpoint_descriptor *bulk_rx_ep; | |
149 | struct usb_endpoint_descriptor *isoc_tx_ep; | |
150 | struct usb_endpoint_descriptor *isoc_rx_ep; | |
151 | ||
152 | __u8 cmdreq_type; | |
153 | ||
154 | unsigned int sco_num; | |
155 | int isoc_altsetting; | |
156 | int suspend_count; | |
157 | uint16_t sco_handle; | |
158 | //#ifdef CONFIG_HAS_EARLYSUSPEND | |
159 | #if 0 | |
160 | struct early_suspend early_suspend; | |
161 | #else | |
162 | struct notifier_block pm_notifier; | |
163 | struct notifier_block reboot_notifier; | |
164 | #endif | |
165 | firmware_info *fw_info; | |
166 | ||
167 | #ifdef CONFIG_SCO_OVER_HCI | |
168 | RTK_sco_card_t *pSCOSnd; | |
169 | #endif | |
170 | }; | |
171 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1) | |
172 | static bool reset_on_close = 0; | |
173 | #endif | |
174 | ||
175 | int download_patch(firmware_info *fw_info, int cached); | |
176 | int reset_controller(firmware_info* fw_info); | |
177 | ||
178 | static inline int check_set_dlfw_state_value(uint16_t change_value) | |
179 | { | |
180 | int state; | |
181 | spin_lock(&dlfw_lock); | |
182 | if(!dlfw_dis_state) { | |
183 | dlfw_dis_state = change_value; | |
184 | } | |
185 | state = dlfw_dis_state; | |
186 | spin_unlock(&dlfw_lock); | |
187 | return state; | |
188 | } | |
189 | ||
190 | static inline void set_dlfw_state_value(uint16_t change_value) | |
191 | { | |
192 | spin_lock(&dlfw_lock); | |
193 | dlfw_dis_state = change_value; | |
194 | spin_unlock(&dlfw_lock); | |
195 | } | |
196 | ||
197 | #if SUSPNED_DW_FW | |
198 | static int download_suspend_patch(firmware_info *fw_info, int cached); | |
199 | #endif | |
200 | #if SET_WAKEUP_DEVICE | |
201 | static void set_wakeup_device_from_conf(firmware_info *fw_info); | |
202 | int set_wakeup_device(firmware_info* fw_info, uint8_t* wakeup_bdaddr); | |
203 | #endif | |
204 | ||
205 | static void rtk_free( struct btusb_data *data) | |
206 | { | |
207 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1) | |
208 | kfree(data); | |
209 | #endif | |
210 | return; | |
211 | } | |
212 | ||
213 | static struct btusb_data *rtk_alloc(struct usb_interface *intf) | |
214 | { | |
215 | struct btusb_data *data; | |
216 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1) | |
217 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
218 | #else | |
219 | data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL); | |
220 | #endif | |
221 | return data; | |
222 | } | |
223 | ||
224 | static void print_acl(struct sk_buff *skb, int direction) | |
225 | { | |
226 | #if PRINT_ACL_DATA | |
227 | uint wlength = skb->len; | |
228 | u16 *handle = (u16 *)(skb->data); | |
229 | u16 len = *(handle+1); | |
230 | u8 *acl_data = (u8 *)(skb->data); | |
231 | ||
232 | RTK_INFO("%s: direction %d, handle %04x, len %d", | |
233 | __func__, direction, *handle, len); | |
234 | #endif | |
235 | } | |
236 | ||
237 | static void print_sco(struct sk_buff *skb, int direction) | |
238 | { | |
239 | #if PRINT_SCO_DATA | |
240 | uint wlength = skb->len; | |
241 | u16 *handle = (u16 *)(skb->data); | |
242 | u8 len = *(u8 *)(handle+1); | |
243 | u8 *sco_data =(u8 *)(skb->data); | |
244 | ||
245 | RTKBT_INFO("%s: direction %d, handle %04x, len %d", | |
246 | __func__, direction, *handle, len); | |
247 | #endif | |
248 | } | |
249 | ||
250 | static void print_error_command(struct sk_buff *skb) | |
251 | { | |
252 | uint wlength = skb->len; | |
253 | uint icount = 0; | |
254 | u16 *opcode = (u16*)(skb->data); | |
255 | u8 *cmd_data = (u8*)(skb->data); | |
256 | u8 len = *(cmd_data+2); | |
257 | ||
258 | switch (*opcode) { | |
259 | case HCI_OP_INQUIRY: | |
260 | printk("HCI_OP_INQUIRY"); | |
261 | break; | |
262 | case HCI_OP_INQUIRY_CANCEL: | |
263 | printk("HCI_OP_INQUIRY_CANCEL"); | |
264 | break; | |
265 | case HCI_OP_EXIT_PERIODIC_INQ: | |
266 | printk("HCI_OP_EXIT_PERIODIC_INQ"); | |
267 | break; | |
268 | case HCI_OP_CREATE_CONN: | |
269 | printk("HCI_OP_CREATE_CONN"); | |
270 | break; | |
271 | case HCI_OP_DISCONNECT: | |
272 | printk("HCI_OP_DISCONNECT"); | |
273 | break; | |
274 | case HCI_OP_CREATE_CONN_CANCEL: | |
275 | printk("HCI_OP_CREATE_CONN_CANCEL"); | |
276 | break; | |
277 | case HCI_OP_ACCEPT_CONN_REQ: | |
278 | printk("HCI_OP_ACCEPT_CONN_REQ"); | |
279 | break; | |
280 | case HCI_OP_REJECT_CONN_REQ: | |
281 | printk("HCI_OP_REJECT_CONN_REQ"); | |
282 | break; | |
283 | case HCI_OP_AUTH_REQUESTED: | |
284 | printk("HCI_OP_AUTH_REQUESTED"); | |
285 | break; | |
286 | case HCI_OP_SET_CONN_ENCRYPT: | |
287 | printk("HCI_OP_SET_CONN_ENCRYPT"); | |
288 | break; | |
289 | case HCI_OP_REMOTE_NAME_REQ: | |
290 | printk("HCI_OP_REMOTE_NAME_REQ"); | |
291 | break; | |
292 | case HCI_OP_READ_REMOTE_FEATURES: | |
293 | printk("HCI_OP_READ_REMOTE_FEATURES"); | |
294 | break; | |
295 | case HCI_OP_SNIFF_MODE: | |
296 | printk("HCI_OP_SNIFF_MODE"); | |
297 | break; | |
298 | case HCI_OP_EXIT_SNIFF_MODE: | |
299 | printk("HCI_OP_EXIT_SNIFF_MODE"); | |
300 | break; | |
301 | case HCI_OP_SWITCH_ROLE: | |
302 | printk("HCI_OP_SWITCH_ROLE"); | |
303 | break; | |
304 | case HCI_OP_SNIFF_SUBRATE: | |
305 | printk("HCI_OP_SNIFF_SUBRATE"); | |
306 | break; | |
307 | case HCI_OP_RESET: | |
308 | printk("HCI_OP_RESET"); | |
309 | break; | |
310 | case HCI_OP_Write_Extended_Inquiry_Response: | |
311 | printk("HCI_Write_Extended_Inquiry_Response"); | |
312 | break; | |
313 | ||
314 | default: | |
315 | printk("CMD"); | |
316 | break; | |
317 | } | |
318 | printk(":%04x,len:%d,", *opcode,len); | |
319 | for (icount = 3; (icount < wlength) && (icount < 24); icount++) | |
320 | printk("%02x ", *(cmd_data+icount)); | |
321 | printk("\n"); | |
322 | } | |
323 | ||
324 | static void print_command(struct sk_buff *skb) | |
325 | { | |
326 | #if PRINT_CMD_EVENT | |
327 | print_error_command(skb); | |
328 | #endif | |
329 | } | |
330 | ||
331 | #if CONFIG_BLUEDROID | |
332 | /* Global parameters for bt usb char driver */ | |
333 | #define BT_CHAR_DEVICE_NAME "rtkbt_dev" | |
334 | struct mutex btchr_mutex; | |
335 | static struct sk_buff_head btchr_readq; | |
336 | static wait_queue_head_t btchr_read_wait; | |
337 | static wait_queue_head_t bt_dlfw_wait; | |
338 | static bool bt_char_dev_registered; | |
339 | static dev_t bt_devid; /* bt char device number */ | |
340 | static struct cdev bt_char_dev; /* bt character device structure */ | |
341 | static struct class *bt_char_class; /* device class for usb char driver */ | |
342 | static int bt_reset = 0; | |
343 | /* HCI device & lock */ | |
344 | DEFINE_RWLOCK(hci_dev_lock); | |
345 | struct hci_dev *ghdev = NULL; | |
346 | ||
347 | static void print_event(struct sk_buff *skb) | |
348 | { | |
349 | #if PRINT_CMD_EVENT | |
350 | uint wlength = skb->len; | |
351 | uint icount = 0; | |
352 | u8 *opcode = (u8*)(skb->data); | |
353 | u8 len = *(opcode+1); | |
354 | ||
355 | switch (*opcode) { | |
356 | case HCI_EV_INQUIRY_COMPLETE: | |
357 | printk("HCI_EV_INQUIRY_COMPLETE"); | |
358 | break; | |
359 | case HCI_EV_INQUIRY_RESULT: | |
360 | printk("HCI_EV_INQUIRY_RESULT"); | |
361 | break; | |
362 | case HCI_EV_CONN_COMPLETE: | |
363 | printk("HCI_EV_CONN_COMPLETE"); | |
364 | break; | |
365 | case HCI_EV_CONN_REQUEST: | |
366 | printk("HCI_EV_CONN_REQUEST"); | |
367 | break; | |
368 | case HCI_EV_DISCONN_COMPLETE: | |
369 | printk("HCI_EV_DISCONN_COMPLETE"); | |
370 | break; | |
371 | case HCI_EV_AUTH_COMPLETE: | |
372 | printk("HCI_EV_AUTH_COMPLETE"); | |
373 | break; | |
374 | case HCI_EV_REMOTE_NAME: | |
375 | printk("HCI_EV_REMOTE_NAME"); | |
376 | break; | |
377 | case HCI_EV_ENCRYPT_CHANGE: | |
378 | printk("HCI_EV_ENCRYPT_CHANGE"); | |
379 | break; | |
380 | case HCI_EV_CHANGE_LINK_KEY_COMPLETE: | |
381 | printk("HCI_EV_CHANGE_LINK_KEY_COMPLETE"); | |
382 | break; | |
383 | case HCI_EV_REMOTE_FEATURES: | |
384 | printk("HCI_EV_REMOTE_FEATURES"); | |
385 | break; | |
386 | case HCI_EV_REMOTE_VERSION: | |
387 | printk("HCI_EV_REMOTE_VERSION"); | |
388 | break; | |
389 | case HCI_EV_QOS_SETUP_COMPLETE: | |
390 | printk("HCI_EV_QOS_SETUP_COMPLETE"); | |
391 | break; | |
392 | case HCI_EV_CMD_COMPLETE: | |
393 | printk("HCI_EV_CMD_COMPLETE"); | |
394 | break; | |
395 | case HCI_EV_CMD_STATUS: | |
396 | printk("HCI_EV_CMD_STATUS"); | |
397 | break; | |
398 | case HCI_EV_ROLE_CHANGE: | |
399 | printk("HCI_EV_ROLE_CHANGE"); | |
400 | break; | |
401 | case HCI_EV_NUM_COMP_PKTS: | |
402 | printk("HCI_EV_NUM_COMP_PKTS"); | |
403 | break; | |
404 | case HCI_EV_MODE_CHANGE: | |
405 | printk("HCI_EV_MODE_CHANGE"); | |
406 | break; | |
407 | case HCI_EV_PIN_CODE_REQ: | |
408 | printk("HCI_EV_PIN_CODE_REQ"); | |
409 | break; | |
410 | case HCI_EV_LINK_KEY_REQ: | |
411 | printk("HCI_EV_LINK_KEY_REQ"); | |
412 | break; | |
413 | case HCI_EV_LINK_KEY_NOTIFY: | |
414 | printk("HCI_EV_LINK_KEY_NOTIFY"); | |
415 | break; | |
416 | case HCI_EV_CLOCK_OFFSET: | |
417 | printk("HCI_EV_CLOCK_OFFSET"); | |
418 | break; | |
419 | case HCI_EV_PKT_TYPE_CHANGE: | |
420 | printk("HCI_EV_PKT_TYPE_CHANGE"); | |
421 | break; | |
422 | case HCI_EV_PSCAN_REP_MODE: | |
423 | printk("HCI_EV_PSCAN_REP_MODE"); | |
424 | break; | |
425 | case HCI_EV_INQUIRY_RESULT_WITH_RSSI: | |
426 | printk("HCI_EV_INQUIRY_RESULT_WITH_RSSI"); | |
427 | break; | |
428 | case HCI_EV_REMOTE_EXT_FEATURES: | |
429 | printk("HCI_EV_REMOTE_EXT_FEATURES"); | |
430 | break; | |
431 | case HCI_EV_SYNC_CONN_COMPLETE: | |
432 | printk("HCI_EV_SYNC_CONN_COMPLETE"); | |
433 | break; | |
434 | case HCI_EV_SYNC_CONN_CHANGED: | |
435 | printk("HCI_EV_SYNC_CONN_CHANGED"); | |
436 | break; | |
437 | case HCI_EV_SNIFF_SUBRATE: | |
438 | printk("HCI_EV_SNIFF_SUBRATE"); | |
439 | break; | |
440 | case HCI_EV_EXTENDED_INQUIRY_RESULT: | |
441 | printk("HCI_EV_EXTENDED_INQUIRY_RESULT"); | |
442 | break; | |
443 | case HCI_EV_IO_CAPA_REQUEST: | |
444 | printk("HCI_EV_IO_CAPA_REQUEST"); | |
445 | break; | |
446 | case HCI_EV_SIMPLE_PAIR_COMPLETE: | |
447 | printk("HCI_EV_SIMPLE_PAIR_COMPLETE"); | |
448 | break; | |
449 | case HCI_EV_REMOTE_HOST_FEATURES: | |
450 | printk("HCI_EV_REMOTE_HOST_FEATURES"); | |
451 | break; | |
452 | default: | |
453 | printk("event"); | |
454 | break; | |
455 | } | |
456 | printk(":%02x,len:%d,", *opcode,len); | |
457 | for (icount = 2; (icount < wlength) && (icount < 24); icount++) | |
458 | printk("%02x ", *(opcode+icount)); | |
459 | printk("\n"); | |
460 | #endif | |
461 | } | |
462 | ||
463 | static inline ssize_t usb_put_user(struct sk_buff *skb, | |
464 | char __user *buf, int count) | |
465 | { | |
466 | char __user *ptr = buf; | |
467 | int len = min_t(unsigned int, skb->len, count); | |
468 | ||
469 | if (copy_to_user(ptr, skb->data, len)) | |
470 | return -EFAULT; | |
471 | ||
472 | return len; | |
473 | } | |
474 | ||
475 | static struct sk_buff *rtk_skb_queue[QUEUE_SIZE]; | |
476 | static int rtk_skb_queue_front = 0; | |
477 | static int rtk_skb_queue_rear = 0; | |
478 | ||
479 | static void rtk_enqueue(struct sk_buff *skb) | |
480 | { | |
481 | unsigned long flags; | |
482 | spin_lock_irqsave(&queue_lock, flags); | |
483 | if (rtk_skb_queue_front == (rtk_skb_queue_rear + 1) % QUEUE_SIZE) { | |
484 | /* | |
485 | * If queue is full, current solution is to drop | |
486 | * the following entries. | |
487 | */ | |
488 | RTKBT_WARN("%s: Queue is full, entry will be dropped", __func__); | |
489 | } else { | |
490 | rtk_skb_queue[rtk_skb_queue_rear] = skb; | |
491 | ||
492 | rtk_skb_queue_rear++; | |
493 | rtk_skb_queue_rear %= QUEUE_SIZE; | |
494 | ||
495 | } | |
496 | spin_unlock_irqrestore(&queue_lock, flags); | |
497 | } | |
498 | ||
499 | static struct sk_buff *rtk_dequeue_try(unsigned int deq_len) | |
500 | { | |
501 | struct sk_buff *skb; | |
502 | struct sk_buff *skb_copy; | |
503 | ||
504 | if (rtk_skb_queue_front == rtk_skb_queue_rear) { | |
505 | RTKBT_WARN("%s: Queue is empty", __func__); | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | skb = rtk_skb_queue[rtk_skb_queue_front]; | |
510 | if (deq_len >= skb->len) { | |
511 | rtk_skb_queue[rtk_skb_queue_front] = NULL; | |
512 | rtk_skb_queue_front++; | |
513 | rtk_skb_queue_front %= QUEUE_SIZE; | |
514 | ||
515 | /* | |
516 | * Return skb addr to be dequeued, and the caller | |
517 | * should free the skb eventually. | |
518 | */ | |
519 | return skb; | |
520 | } else { | |
521 | skb_copy = pskb_copy(skb, GFP_ATOMIC); | |
522 | skb_pull(skb, deq_len); | |
523 | /* Return its copy to be freed */ | |
524 | return skb_copy; | |
525 | } | |
526 | } | |
527 | ||
528 | static inline int is_queue_empty(void) | |
529 | { | |
530 | return (rtk_skb_queue_front == rtk_skb_queue_rear) ? 1 : 0; | |
531 | } | |
532 | ||
533 | static void rtk_clear_queue(void) | |
534 | { | |
535 | struct sk_buff *skb; | |
536 | unsigned long flags; | |
537 | spin_lock_irqsave(&queue_lock, flags); | |
538 | while(!is_queue_empty()) { | |
539 | skb = rtk_skb_queue[rtk_skb_queue_front]; | |
540 | rtk_skb_queue[rtk_skb_queue_front] = NULL; | |
541 | rtk_skb_queue_front++; | |
542 | rtk_skb_queue_front %= QUEUE_SIZE; | |
543 | if (skb) { | |
544 | kfree_skb(skb); | |
545 | } | |
546 | } | |
547 | spin_unlock_irqrestore(&queue_lock, flags); | |
548 | } | |
549 | ||
550 | /* | |
551 | * Realtek - Integrate from hci_core.c | |
552 | */ | |
553 | ||
554 | /* Get HCI device by index. | |
555 | * Device is held on return. */ | |
556 | static struct hci_dev *hci_dev_get(int index) | |
557 | { | |
558 | if (index != 0) | |
559 | return NULL; | |
560 | ||
561 | return ghdev; | |
562 | } | |
563 | ||
564 | /* ---- HCI ioctl helpers ---- */ | |
565 | static int hci_dev_open(__u16 dev) | |
566 | { | |
567 | struct hci_dev *hdev; | |
568 | int ret = 0; | |
569 | ||
570 | RTKBT_DBG("%s: dev %d", __func__, dev); | |
571 | ||
572 | hdev = hci_dev_get(dev); | |
573 | if (!hdev) { | |
574 | RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__); | |
575 | return -ENODEV; | |
576 | } | |
577 | ||
578 | if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { | |
579 | ret = -ENODEV; | |
580 | goto done; | |
581 | } | |
582 | ||
583 | if (test_bit(HCI_UP, &hdev->flags)) { | |
584 | ret = -EALREADY; | |
585 | goto done; | |
586 | } | |
587 | ||
588 | done: | |
589 | return ret; | |
590 | } | |
591 | ||
592 | static int hci_dev_do_close(struct hci_dev *hdev) | |
593 | { | |
594 | if (hdev->flush) | |
595 | hdev->flush(hdev); | |
596 | /* After this point our queues are empty | |
597 | * and no tasks are scheduled. */ | |
598 | hdev->close(hdev); | |
599 | /* Clear flags */ | |
600 | hdev->flags = 0; | |
601 | memset(&hdev->conn_hash, 0, sizeof(struct hci_conn_hash)); | |
602 | return 0; | |
603 | } | |
604 | ||
605 | static int hci_dev_close(__u16 dev) | |
606 | { | |
607 | struct hci_dev *hdev; | |
608 | int err; | |
609 | hdev = hci_dev_get(dev); | |
610 | if (!hdev) { | |
611 | RTKBT_ERR("%s: failed to get hci dev[Null]", __func__); | |
612 | return -ENODEV; | |
613 | } | |
614 | ||
615 | err = hci_dev_do_close(hdev); | |
616 | ||
617 | return err; | |
618 | } | |
619 | ||
620 | static struct hci_dev *hci_alloc_dev(void) | |
621 | { | |
622 | struct hci_dev *hdev; | |
623 | ||
624 | hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL); | |
625 | if (!hdev) | |
626 | return NULL; | |
627 | ||
628 | return hdev; | |
629 | } | |
630 | ||
631 | /* Free HCI device */ | |
632 | static void hci_free_dev(struct hci_dev *hdev) | |
633 | { | |
634 | kfree(hdev); | |
635 | } | |
636 | ||
637 | /* Register HCI device */ | |
638 | static int hci_register_dev(struct hci_dev *hdev) | |
639 | { | |
640 | int i, id; | |
641 | ||
642 | RTKBT_DBG("%s: %p name %s bus %d", __func__, hdev, hdev->name, hdev->bus); | |
643 | /* Do not allow HCI_AMP devices to register at index 0, | |
644 | * so the index can be used as the AMP controller ID. | |
645 | */ | |
646 | id = (hdev->dev_type == HCI_BREDR) ? 0 : 1; | |
647 | ||
648 | write_lock(&hci_dev_lock); | |
649 | ||
650 | sprintf(hdev->name, "hci%d", id); | |
651 | hdev->id = id; | |
652 | hdev->flags = 0; | |
653 | hdev->dev_flags = 0; | |
654 | mutex_init(&hdev->lock); | |
655 | ||
656 | RTKBT_DBG("%s: id %d, name %s", __func__, hdev->id, hdev->name); | |
657 | ||
658 | ||
659 | for (i = 0; i < NUM_REASSEMBLY; i++) | |
660 | hdev->reassembly[i] = NULL; | |
661 | ||
662 | memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); | |
663 | atomic_set(&hdev->promisc, 0); | |
664 | ||
665 | if (ghdev) { | |
666 | write_unlock(&hci_dev_lock); | |
667 | RTKBT_ERR("%s: Hci device has been registered already", __func__); | |
668 | return -1; | |
669 | } else | |
670 | ghdev = hdev; | |
671 | ||
672 | write_unlock(&hci_dev_lock); | |
673 | ||
674 | return id; | |
675 | } | |
676 | ||
677 | /* Unregister HCI device */ | |
678 | static void hci_unregister_dev(struct hci_dev *hdev) | |
679 | { | |
680 | int i; | |
681 | ||
682 | RTKBT_DBG("%s: hdev %p name %s bus %d", __func__, hdev, hdev->name, hdev->bus); | |
683 | set_bit(HCI_UNREGISTER, &hdev->dev_flags); | |
684 | ||
685 | write_lock(&hci_dev_lock); | |
686 | ghdev = NULL; | |
687 | write_unlock(&hci_dev_lock); | |
688 | ||
689 | hci_dev_do_close(hdev); | |
690 | for (i = 0; i < NUM_REASSEMBLY; i++) | |
691 | kfree_skb(hdev->reassembly[i]); | |
692 | } | |
693 | ||
694 | ||
695 | #ifdef CONFIG_SCO_OVER_HCI | |
696 | /* copy data from the URB buffer into the ALSA ring buffer */ | |
697 | static bool rtk_copy_capture_data_to_alsa(struct btusb_data *data, uint8_t* p_data, unsigned int frames) | |
698 | { | |
699 | struct snd_pcm_runtime *runtime; | |
700 | unsigned int frame_bytes, frames1; | |
701 | u8 *dest; | |
702 | RTK_sco_card_t *pSCOSnd = data->pSCOSnd; | |
703 | ||
704 | runtime = pSCOSnd->capture.substream->runtime; | |
705 | frame_bytes = 2; | |
706 | ||
707 | dest = runtime->dma_area + pSCOSnd->capture.buffer_pos * frame_bytes; | |
708 | if (pSCOSnd->capture.buffer_pos + frames <= runtime->buffer_size) { | |
709 | memcpy(dest, p_data, frames * frame_bytes); | |
710 | } else { | |
711 | /* wrap around at end of ring buffer */ | |
712 | frames1 = runtime->buffer_size - pSCOSnd->capture.buffer_pos; | |
713 | memcpy(dest, p_data, frames1 * frame_bytes); | |
714 | memcpy(runtime->dma_area, | |
715 | p_data + frames1 * frame_bytes, | |
716 | (frames - frames1) * frame_bytes); | |
717 | } | |
718 | ||
719 | pSCOSnd->capture.buffer_pos += frames; | |
720 | if (pSCOSnd->capture.buffer_pos >= runtime->buffer_size) { | |
721 | pSCOSnd->capture.buffer_pos -= runtime->buffer_size; | |
722 | } | |
723 | ||
724 | if((pSCOSnd->capture.buffer_pos%runtime->period_size) == 0) { | |
725 | snd_pcm_period_elapsed(pSCOSnd->capture.substream); | |
726 | } | |
727 | ||
728 | return false; | |
729 | } | |
730 | ||
731 | ||
732 | static void hci_send_to_alsa_ringbuffer(struct hci_dev *hdev, struct sk_buff *skb) | |
733 | { | |
734 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
735 | RTK_sco_card_t *pSCOSnd = data->pSCOSnd; | |
736 | uint8_t* p_data; | |
737 | int sco_length = skb->len - HCI_SCO_HDR_SIZE; | |
738 | ||
739 | RTKBT_DBG("%s", __func__); | |
740 | ||
741 | if (!hdev) { | |
742 | RTKBT_ERR("%s: Frame for unknown HCI device", __func__); | |
743 | return; | |
744 | } | |
745 | ||
746 | if (!test_bit(ALSA_CAPTURE_RUNNING, &pSCOSnd->states)) { | |
747 | //RTKBT_WARN("%s: ALSA is not running", __func__); | |
748 | return; | |
749 | } | |
750 | ||
751 | p_data = (uint8_t *)skb->data + HCI_SCO_HDR_SIZE; | |
752 | rtk_copy_capture_data_to_alsa(data, p_data, sco_length/2); | |
753 | } | |
754 | ||
755 | #endif | |
756 | ||
757 | static void hci_send_to_stack(struct hci_dev *hdev, struct sk_buff *skb) | |
758 | { | |
759 | struct sk_buff *rtk_skb_copy = NULL; | |
760 | ||
761 | RTKBT_DBG("%s", __func__); | |
762 | ||
763 | if (!hdev) { | |
764 | RTKBT_ERR("%s: Frame for unknown HCI device", __func__); | |
765 | return; | |
766 | } | |
767 | ||
768 | if (!test_bit(HCI_RUNNING, &hdev->flags)) { | |
769 | RTKBT_ERR("%s: HCI not running", __func__); | |
770 | return; | |
771 | } | |
772 | ||
773 | rtk_skb_copy = pskb_copy(skb, GFP_ATOMIC); | |
774 | if (!rtk_skb_copy) { | |
775 | RTKBT_ERR("%s: Copy skb error", __func__); | |
776 | return; | |
777 | } | |
778 | ||
779 | memcpy(skb_push(rtk_skb_copy, 1), &bt_cb(skb)->pkt_type, 1); | |
780 | rtk_enqueue(rtk_skb_copy); | |
781 | ||
782 | /* Make sure bt char device existing before wakeup read queue */ | |
783 | hdev = hci_dev_get(0); | |
784 | if (hdev) { | |
785 | RTKBT_DBG("%s: Try to wakeup read queue", __func__); | |
786 | wake_up_interruptible(&btchr_read_wait); | |
787 | } | |
788 | ||
789 | return; | |
790 | } | |
791 | ||
792 | /* Receive frame from HCI drivers */ | |
793 | static int hci_recv_frame(struct sk_buff *skb) | |
794 | { | |
795 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
796 | ||
797 | if (!hdev || | |
798 | (!test_bit(HCI_UP, &hdev->flags) && !test_bit(HCI_INIT, &hdev->flags))) { | |
799 | kfree_skb(skb); | |
800 | return -ENXIO; | |
801 | } | |
802 | ||
803 | /* Incomming skb */ | |
804 | bt_cb(skb)->incoming = 1; | |
805 | ||
806 | /* Time stamp */ | |
807 | __net_timestamp(skb); | |
808 | ||
809 | if (atomic_read(&hdev->promisc)) { | |
810 | #ifdef CONFIG_SCO_OVER_HCI | |
811 | if(bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) | |
812 | hci_send_to_alsa_ringbuffer(hdev, skb); | |
813 | #endif | |
814 | /* Send copy to the sockets */ | |
815 | hci_send_to_stack(hdev, skb); | |
816 | } | |
817 | ||
818 | kfree_skb(skb); | |
819 | return 0; | |
820 | } | |
821 | ||
822 | /* Receive frame from HCI drivers */ | |
823 | static int hci_recv_sco_frame(struct sk_buff *skb) | |
824 | { | |
825 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
826 | ||
827 | if (!hdev || | |
828 | (!test_bit(HCI_UP, &hdev->flags) && !test_bit(HCI_INIT, &hdev->flags))) { | |
829 | kfree_skb(skb); | |
830 | return -ENXIO; | |
831 | } | |
832 | ||
833 | /* Incomming skb */ | |
834 | bt_cb(skb)->incoming = 1; | |
835 | ||
836 | /* Time stamp */ | |
837 | __net_timestamp(skb); | |
838 | ||
839 | if (atomic_read(&hdev->promisc)) { | |
840 | #ifdef CONFIG_SCO_OVER_HCI | |
841 | if(bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) | |
842 | hci_send_to_alsa_ringbuffer(hdev, skb); | |
843 | #endif | |
844 | /* Send copy to the sockets */ | |
845 | hci_send_to_stack(hdev, skb); | |
846 | } | |
847 | ||
848 | kfree_skb(skb); | |
849 | return 0; | |
850 | } | |
851 | ||
852 | static int hci_reassembly(struct hci_dev *hdev, int type, void *data, | |
853 | int count, __u8 index) | |
854 | { | |
855 | int len = 0; | |
856 | int hlen = 0; | |
857 | int remain = count; | |
858 | struct sk_buff *skb; | |
859 | struct bt_skb_cb *scb; | |
860 | ||
861 | RTKBT_DBG("%s", __func__); | |
862 | ||
863 | if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) || | |
864 | index >= NUM_REASSEMBLY) | |
865 | return -EILSEQ; | |
866 | ||
867 | skb = hdev->reassembly[index]; | |
868 | ||
869 | if (!skb) { | |
870 | switch (type) { | |
871 | case HCI_ACLDATA_PKT: | |
872 | len = HCI_MAX_FRAME_SIZE; | |
873 | hlen = HCI_ACL_HDR_SIZE; | |
874 | break; | |
875 | case HCI_EVENT_PKT: | |
876 | len = HCI_MAX_EVENT_SIZE; | |
877 | hlen = HCI_EVENT_HDR_SIZE; | |
878 | break; | |
879 | case HCI_SCODATA_PKT: | |
880 | len = HCI_MAX_SCO_SIZE; | |
881 | hlen = HCI_SCO_HDR_SIZE; | |
882 | break; | |
883 | } | |
884 | ||
885 | skb = bt_skb_alloc(len, GFP_ATOMIC); | |
886 | if (!skb) | |
887 | return -ENOMEM; | |
888 | ||
889 | scb = (void *) skb->cb; | |
890 | scb->expect = hlen; | |
891 | scb->pkt_type = type; | |
892 | ||
893 | skb->dev = (void *) hdev; | |
894 | hdev->reassembly[index] = skb; | |
895 | ||
896 | } | |
897 | ||
898 | while (count) { | |
899 | scb = (void *) skb->cb; | |
900 | len = min_t(uint, scb->expect, count); | |
901 | ||
902 | memcpy(skb_put(skb, len), data, len); | |
903 | ||
904 | count -= len; | |
905 | data += len; | |
906 | scb->expect -= len; | |
907 | remain = count; | |
908 | ||
909 | switch (type) { | |
910 | case HCI_EVENT_PKT: | |
911 | if (skb->len == HCI_EVENT_HDR_SIZE) { | |
912 | struct hci_event_hdr *h = hci_event_hdr(skb); | |
913 | scb->expect = h->plen; | |
914 | ||
915 | if (skb_tailroom(skb) < scb->expect) { | |
916 | kfree_skb(skb); | |
917 | hdev->reassembly[index] = NULL; | |
918 | return -ENOMEM; | |
919 | } | |
920 | } | |
921 | break; | |
922 | ||
923 | case HCI_ACLDATA_PKT: | |
924 | if (skb->len == HCI_ACL_HDR_SIZE) { | |
925 | struct hci_acl_hdr *h = hci_acl_hdr(skb); | |
926 | scb->expect = __le16_to_cpu(h->dlen); | |
927 | ||
928 | if (skb_tailroom(skb) < scb->expect) { | |
929 | kfree_skb(skb); | |
930 | hdev->reassembly[index] = NULL; | |
931 | return -ENOMEM; | |
932 | } | |
933 | } | |
934 | break; | |
935 | ||
936 | case HCI_SCODATA_PKT: | |
937 | if (skb->len == HCI_SCO_HDR_SIZE) { | |
938 | struct hci_sco_hdr *h = hci_sco_hdr(skb); | |
939 | scb->expect = h->dlen; | |
940 | ||
941 | if (skb_tailroom(skb) < scb->expect) { | |
942 | kfree_skb(skb); | |
943 | hdev->reassembly[index] = NULL; | |
944 | return -ENOMEM; | |
945 | } | |
946 | } | |
947 | break; | |
948 | } | |
949 | ||
950 | if (scb->expect == 0) { | |
951 | /* Complete frame */ | |
952 | if(HCI_ACLDATA_PKT == type) | |
953 | print_acl(skb,0); | |
954 | if(HCI_SCODATA_PKT == type) | |
955 | print_sco(skb,0); | |
956 | if(HCI_EVENT_PKT == type) | |
957 | print_event(skb); | |
958 | ||
959 | bt_cb(skb)->pkt_type = type; | |
960 | if(type == HCI_SCODATA_PKT) { | |
961 | hci_recv_sco_frame(skb); | |
962 | } | |
963 | else | |
964 | hci_recv_frame(skb); | |
965 | ||
966 | hdev->reassembly[index] = NULL; | |
967 | return remain; | |
968 | } | |
969 | } | |
970 | ||
971 | return remain; | |
972 | } | |
973 | ||
974 | static int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count) | |
975 | { | |
976 | int rem = 0; | |
977 | ||
978 | if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) | |
979 | return -EILSEQ; | |
980 | ||
981 | while (count) { | |
982 | rem = hci_reassembly(hdev, type, data, count, type - 1); | |
983 | if (rem < 0) | |
984 | return rem; | |
985 | ||
986 | data += (count - rem); | |
987 | count = rem; | |
988 | } | |
989 | ||
990 | return rem; | |
991 | } | |
992 | ||
993 | void hci_hardware_error(void) | |
994 | { | |
995 | struct sk_buff *rtk_skb_copy = NULL; | |
996 | int len = 4; | |
997 | uint8_t hardware_err_pkt[4] = {HCI_EVENT_PKT, 0x10, 0x01, HCI_VENDOR_USB_DISC_HARDWARE_ERROR}; | |
998 | ||
999 | rtk_skb_copy = alloc_skb(len, GFP_ATOMIC); | |
1000 | if (!rtk_skb_copy) { | |
1001 | RTKBT_ERR("%s: Failed to allocate mem", __func__); | |
1002 | return; | |
1003 | } | |
1004 | ||
1005 | memcpy(skb_put(rtk_skb_copy, len), hardware_err_pkt, len); | |
1006 | rtk_enqueue(rtk_skb_copy); | |
1007 | ||
1008 | wake_up_interruptible(&btchr_read_wait); | |
1009 | } | |
1010 | ||
1011 | static int btchr_open(struct inode *inode_p, struct file *file_p) | |
1012 | { | |
1013 | struct btusb_data *data; | |
1014 | struct hci_dev *hdev; | |
1015 | struct sk_buff *skb; | |
1016 | int i; | |
1017 | ||
1018 | RTKBT_INFO("%s: BT usb char device is opening", __func__); | |
1019 | /* Not open unless wanna tracing log */ | |
1020 | /* trace_printk("%s: open....\n", __func__); */ | |
1021 | ||
1022 | hdev = hci_dev_get(0); | |
1023 | if (!hdev) { | |
1024 | RTKBT_ERR("%s: Failed to get hci dev[NULL]", __func__); | |
1025 | return -ENODEV; | |
1026 | } | |
1027 | data = GET_DRV_DATA(hdev); | |
1028 | ||
1029 | atomic_inc(&hdev->promisc); | |
1030 | /* | |
1031 | * As bt device is not re-opened when hotplugged out, we cannot | |
1032 | * trust on file's private data(may be null) when other file ops | |
1033 | * are invoked. | |
1034 | */ | |
1035 | file_p->private_data = data; | |
1036 | ||
1037 | mutex_lock(&btchr_mutex); | |
1038 | hci_dev_open(0); | |
1039 | mutex_unlock(&btchr_mutex); | |
1040 | ||
1041 | rtk_clear_queue(); | |
1042 | for(i = 0; i < NUM_REASSEMBLY; i++) { | |
1043 | skb = hdev->reassembly[i]; | |
1044 | if(skb) { | |
1045 | hdev->reassembly[i] = NULL; | |
1046 | kfree_skb(skb); | |
1047 | } | |
1048 | } | |
1049 | return nonseekable_open(inode_p, file_p); | |
1050 | } | |
1051 | ||
1052 | static int btchr_close(struct inode *inode_p, struct file *file_p) | |
1053 | { | |
1054 | struct btusb_data *data; | |
1055 | struct hci_dev *hdev; | |
1056 | ||
1057 | RTKBT_INFO("%s: BT usb char device is closing", __func__); | |
1058 | /* Not open unless wanna tracing log */ | |
1059 | /* trace_printk("%s: close....\n", __func__); */ | |
1060 | ||
1061 | data = file_p->private_data; | |
1062 | file_p->private_data = NULL; | |
1063 | ||
1064 | #if CONFIG_BLUEDROID | |
1065 | /* | |
1066 | * If the upper layer closes bt char interfaces, no reset | |
1067 | * action required even bt device hotplugged out. | |
1068 | */ | |
1069 | bt_reset = 0; | |
1070 | #endif | |
1071 | ||
1072 | hdev = hci_dev_get(0); | |
1073 | if (hdev) { | |
1074 | atomic_set(&hdev->promisc, 0); | |
1075 | mutex_lock(&btchr_mutex); | |
1076 | hci_dev_close(0); | |
1077 | mutex_unlock(&btchr_mutex); | |
1078 | } | |
1079 | ||
1080 | return 0; | |
1081 | } | |
1082 | ||
1083 | static ssize_t btchr_read(struct file *file_p, | |
1084 | char __user *buf_p, | |
1085 | size_t count, | |
1086 | loff_t *pos_p) | |
1087 | { | |
1088 | struct hci_dev *hdev; | |
1089 | struct sk_buff *skb; | |
1090 | ssize_t ret = 0; | |
1091 | ||
1092 | RTKBT_DBG("%s: BT usb char device is reading", __func__); | |
1093 | ||
1094 | while (count) { | |
1095 | hdev = hci_dev_get(0); | |
1096 | if (!hdev) { | |
1097 | /* | |
1098 | * Note: Only when BT device hotplugged out, we wil get | |
1099 | * into such situation. In order to keep the upper layer | |
1100 | * stack alive (blocking the read), we should never return | |
1101 | * EFAULT or break the loop. | |
1102 | */ | |
1103 | RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__); | |
1104 | } | |
1105 | ||
1106 | ret = wait_event_interruptible(btchr_read_wait, !is_queue_empty()); | |
1107 | if (ret < 0) { | |
1108 | //RTKBT_ERR("%s: wait event is signaled %d", __func__, ret); | |
1109 | break; | |
1110 | } | |
1111 | ||
1112 | skb = rtk_dequeue_try(count); | |
1113 | if (skb) { | |
1114 | ret = usb_put_user(skb, buf_p, count); | |
1115 | if (ret < 0) | |
1116 | RTKBT_ERR("%s: Failed to put data to user space", __func__); | |
1117 | kfree_skb(skb); | |
1118 | break; | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | return ret; | |
1123 | } | |
1124 | ||
1125 | static ssize_t btchr_write(struct file *file_p, | |
1126 | const char __user *buf_p, | |
1127 | size_t count, | |
1128 | loff_t *pos_p) | |
1129 | { | |
1130 | struct btusb_data *data = file_p->private_data; | |
1131 | struct hci_dev *hdev; | |
1132 | struct sk_buff *skb; | |
1133 | ||
1134 | RTKBT_DBG("%s: BT usb char device is writing", __func__); | |
1135 | ||
1136 | hdev = hci_dev_get(0); | |
1137 | if (!hdev) { | |
1138 | RTKBT_WARN("%s: Failed to get hci dev[Null]", __func__); | |
1139 | /* | |
1140 | * Note: we bypass the data from the upper layer if bt device | |
1141 | * is hotplugged out. Fortunatelly, H4 or H5 HCI stack does | |
1142 | * NOT check btchr_write's return value. However, returning | |
1143 | * count instead of EFAULT is preferable. | |
1144 | */ | |
1145 | /* return -EFAULT; */ | |
1146 | return count; | |
1147 | } | |
1148 | ||
1149 | /* Never trust on btusb_data, as bt device may be hotplugged out */ | |
1150 | data = GET_DRV_DATA(hdev); | |
1151 | if (!data) { | |
1152 | RTKBT_WARN("%s: Failed to get bt usb driver data[Null]", __func__); | |
1153 | return count; | |
1154 | } | |
1155 | ||
1156 | if (count > HCI_MAX_FRAME_SIZE) | |
1157 | return -EINVAL; | |
1158 | ||
1159 | skb = bt_skb_alloc(count, GFP_ATOMIC); | |
1160 | if (!skb) | |
1161 | return -ENOMEM; | |
1162 | skb_reserve(skb, -1); // Add this line | |
1163 | ||
1164 | if (copy_from_user(skb_put(skb, count), buf_p, count)) { | |
1165 | RTKBT_ERR("%s: Failed to get data from user space", __func__); | |
1166 | kfree_skb(skb); | |
1167 | return -EFAULT; | |
1168 | } | |
1169 | ||
1170 | skb->dev = (void *)hdev; | |
1171 | bt_cb(skb)->pkt_type = *((__u8 *)skb->data); | |
1172 | skb_pull(skb, 1); | |
1173 | data->hdev->send(skb); | |
1174 | ||
1175 | return count; | |
1176 | } | |
1177 | ||
1178 | static unsigned int btchr_poll(struct file *file_p, poll_table *wait) | |
1179 | { | |
1180 | struct btusb_data *data = file_p->private_data; | |
1181 | struct hci_dev *hdev; | |
1182 | ||
1183 | RTKBT_DBG("%s: BT usb char device is polling", __func__); | |
1184 | ||
1185 | if(!bt_char_dev_registered) { | |
1186 | RTKBT_ERR("%s: char device has not registered!", __func__); | |
1187 | return POLLERR | POLLHUP; | |
1188 | } | |
1189 | ||
1190 | poll_wait(file_p, &btchr_read_wait, wait); | |
1191 | ||
1192 | hdev = hci_dev_get(0); | |
1193 | if (!hdev) { | |
1194 | RTKBT_ERR("%s: Failed to get hci dev[Null]", __func__); | |
1195 | mdelay(URB_CANCELING_DELAY_MS); | |
1196 | return POLLOUT | POLLWRNORM; | |
1197 | } | |
1198 | ||
1199 | /* Never trust on btusb_data, as bt device may be hotplugged out */ | |
1200 | data = GET_DRV_DATA(hdev); | |
1201 | if (!data) { | |
1202 | /* | |
1203 | * When bt device is hotplugged out, btusb_data will | |
1204 | * be freed in disconnect. | |
1205 | */ | |
1206 | RTKBT_ERR("%s: Failed to get bt usb driver data[Null]", __func__); | |
1207 | mdelay(URB_CANCELING_DELAY_MS); | |
1208 | return POLLOUT | POLLWRNORM; | |
1209 | } | |
1210 | ||
1211 | if (!is_queue_empty()) | |
1212 | return POLLIN | POLLRDNORM; | |
1213 | ||
1214 | return POLLOUT | POLLWRNORM; | |
1215 | } | |
1216 | static long btchr_ioctl(struct file *file_p, unsigned int cmd, unsigned long arg){ | |
1217 | int ret = 0; | |
1218 | struct hci_dev *hdev; | |
1219 | struct btusb_data *data; | |
1220 | firmware_info *fw_info; | |
1221 | ||
1222 | if(!bt_char_dev_registered) { | |
1223 | return -ENODEV; | |
1224 | } | |
1225 | ||
1226 | if(check_set_dlfw_state_value(1) != 1) { | |
1227 | RTKBT_ERR("%s bt controller is disconnecting!", __func__); | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | hdev = hci_dev_get(0); | |
1232 | if(!hdev) { | |
1233 | RTKBT_ERR("%s device is NULL!", __func__); | |
1234 | set_dlfw_state_value(0); | |
1235 | return 0; | |
1236 | } | |
1237 | data = GET_DRV_DATA(hdev); | |
1238 | fw_info = data->fw_info; | |
1239 | ||
1240 | RTKBT_INFO(" btchr_ioctl DOWN_FW_CFG with Cmd:%d",cmd); | |
1241 | switch (cmd) { | |
1242 | case DOWN_FW_CFG: | |
1243 | ret = usb_autopm_get_interface(data->intf); | |
1244 | if (ret < 0){ | |
1245 | goto failed; | |
1246 | } | |
1247 | ||
1248 | ret = download_patch(fw_info,1); | |
1249 | usb_autopm_put_interface(data->intf); | |
1250 | if(ret < 0){ | |
1251 | RTKBT_ERR("%s:Failed in download_patch with ret:%d",__func__,ret); | |
1252 | goto failed; | |
1253 | } | |
1254 | ||
1255 | ret = hdev->open(hdev); | |
1256 | if(ret < 0){ | |
1257 | RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret); | |
1258 | goto failed; | |
1259 | } | |
1260 | set_bit(HCI_UP, &hdev->flags); | |
1261 | set_dlfw_state_value(0); | |
1262 | wake_up_interruptible(&bt_dlfw_wait); | |
1263 | return 1; | |
1264 | ||
1265 | #ifdef CONFIG_SCO_OVER_HCI | |
1266 | case SET_ISO_CFG: | |
1267 | if(get_user(hdev->voice_setting, (__u16 __user*)arg)) { | |
1268 | ret = -EFAULT; | |
1269 | } | |
1270 | RTKBT_INFO(" voice settings = 0x%04x", hdev->voice_setting); | |
1271 | return ret; | |
1272 | #endif | |
1273 | ||
1274 | case GET_USB_INFO: | |
1275 | ret = hdev->open(hdev); | |
1276 | if(ret < 0){ | |
1277 | RTKBT_ERR("%s:Failed in hdev->open(hdev):%d",__func__,ret); | |
1278 | //goto done; | |
1279 | } | |
1280 | set_bit(HCI_UP, &hdev->flags); | |
1281 | return usb_info; | |
1282 | case RESET_CONTROLLER: | |
1283 | reset_controller(fw_info); | |
1284 | return 1; | |
1285 | ||
1286 | case DWFW_CMPLT: | |
1287 | set_dlfw_state_value(0); | |
1288 | wake_up_interruptible(&bt_dlfw_wait); | |
1289 | return 1; | |
1290 | ||
1291 | #ifdef CONFIG_SCO_OVER_HCI | |
1292 | case SET_ISO_CFG: | |
1293 | hdev->voice_setting = *(__u16 *)arg; | |
1294 | RTKBT_INFO(" voice settings = 0x%04x", hdev->voice_setting); | |
1295 | return 1; | |
1296 | #endif | |
1297 | default: | |
1298 | RTKBT_ERR("%s:Failed with wrong Cmd:%d",__func__,cmd); | |
1299 | goto failed; | |
1300 | } | |
1301 | failed: | |
1302 | set_dlfw_state_value(0); | |
1303 | wake_up_interruptible(&bt_dlfw_wait); | |
1304 | return ret; | |
1305 | ||
1306 | } | |
1307 | ||
1308 | ||
1309 | ||
1310 | static struct file_operations bt_chrdev_ops = { | |
1311 | open : btchr_open, | |
1312 | release : btchr_close, | |
1313 | read : btchr_read, | |
1314 | write : btchr_write, | |
1315 | poll : btchr_poll, | |
1316 | unlocked_ioctl : btchr_ioctl, | |
1317 | }; | |
1318 | ||
1319 | static int btchr_init(void) | |
1320 | { | |
1321 | int res = 0; | |
1322 | struct device *dev; | |
1323 | ||
1324 | RTKBT_INFO("Register usb char device interface for BT driver"); | |
1325 | /* | |
1326 | * btchr mutex is used to sync between | |
1327 | * 1) downloading patch and opening bt char driver | |
1328 | * 2) the file operations of bt char driver | |
1329 | */ | |
1330 | mutex_init(&btchr_mutex); | |
1331 | ||
1332 | skb_queue_head_init(&btchr_readq); | |
1333 | init_waitqueue_head(&btchr_read_wait); | |
1334 | init_waitqueue_head(&bt_dlfw_wait); | |
1335 | ||
1336 | bt_char_class = class_create(THIS_MODULE, BT_CHAR_DEVICE_NAME); | |
1337 | if (IS_ERR(bt_char_class)) { | |
1338 | RTKBT_ERR("Failed to create bt char class"); | |
1339 | return PTR_ERR(bt_char_class); | |
1340 | } | |
1341 | ||
1342 | res = alloc_chrdev_region(&bt_devid, 0, 1, BT_CHAR_DEVICE_NAME); | |
1343 | if (res < 0) { | |
1344 | RTKBT_ERR("Failed to allocate bt char device"); | |
1345 | goto err_alloc; | |
1346 | } | |
1347 | ||
1348 | dev = device_create(bt_char_class, NULL, bt_devid, NULL, BT_CHAR_DEVICE_NAME); | |
1349 | if (IS_ERR(dev)) { | |
1350 | RTKBT_ERR("Failed to create bt char device"); | |
1351 | res = PTR_ERR(dev); | |
1352 | goto err_create; | |
1353 | } | |
1354 | ||
1355 | cdev_init(&bt_char_dev, &bt_chrdev_ops); | |
1356 | res = cdev_add(&bt_char_dev, bt_devid, 1); | |
1357 | if (res < 0) { | |
1358 | RTKBT_ERR("Failed to add bt char device"); | |
1359 | goto err_add; | |
1360 | } | |
1361 | ||
1362 | return 0; | |
1363 | ||
1364 | err_add: | |
1365 | device_destroy(bt_char_class, bt_devid); | |
1366 | err_create: | |
1367 | unregister_chrdev_region(bt_devid, 1); | |
1368 | err_alloc: | |
1369 | class_destroy(bt_char_class); | |
1370 | return res; | |
1371 | } | |
1372 | ||
1373 | static void btchr_exit(void) | |
1374 | { | |
1375 | RTKBT_INFO("Unregister usb char device interface for BT driver"); | |
1376 | ||
1377 | device_destroy(bt_char_class, bt_devid); | |
1378 | cdev_del(&bt_char_dev); | |
1379 | unregister_chrdev_region(bt_devid, 1); | |
1380 | class_destroy(bt_char_class); | |
1381 | ||
1382 | return; | |
1383 | } | |
1384 | #endif | |
1385 | ||
1386 | int send_hci_cmd(firmware_info *fw_info) | |
1387 | { | |
1388 | int i = 0; | |
1389 | int ret_val = -1; | |
1390 | while((ret_val<0)&&(i++<10)) | |
1391 | { | |
1392 | ret_val = usb_control_msg( | |
1393 | fw_info->udev, fw_info->pipe_out, | |
1394 | 0, USB_TYPE_CLASS, 0, 0, | |
1395 | (void *)(fw_info->send_pkt), | |
1396 | fw_info->pkt_len, MSG_TO); | |
1397 | } | |
1398 | return ret_val; | |
1399 | } | |
1400 | ||
1401 | int rcv_hci_evt(firmware_info *fw_info) | |
1402 | { | |
1403 | int ret_len = 0, ret_val = 0; | |
1404 | int i; | |
1405 | ||
1406 | while (1) { | |
1407 | for(i = 0; i < 5; i++) { | |
1408 | ret_val = usb_interrupt_msg( | |
1409 | fw_info->udev, fw_info->pipe_in, | |
1410 | (void *)(fw_info->rcv_pkt), PKT_LEN, | |
1411 | &ret_len, MSG_TO); | |
1412 | if (ret_val >= 0) | |
1413 | break; | |
1414 | } | |
1415 | ||
1416 | if (ret_val < 0) | |
1417 | return ret_val; | |
1418 | ||
1419 | if (CMD_CMP_EVT == fw_info->evt_hdr->evt) { | |
1420 | if (fw_info->cmd_hdr->opcode == fw_info->cmd_cmp->opcode) | |
1421 | return ret_len; | |
1422 | } | |
1423 | } | |
1424 | } | |
1425 | ||
1426 | int set_bt_onoff(firmware_info *fw_info, uint8_t onoff) | |
1427 | { | |
1428 | patch_info *patch_entry; | |
1429 | int ret_val; | |
1430 | ||
1431 | RTKBT_INFO("%s: %s", __func__, onoff != 0 ? "on" : "off"); | |
1432 | ||
1433 | patch_entry = fw_info->patch_entry; | |
1434 | if (!patch_entry) | |
1435 | return -1; | |
1436 | ||
1437 | fw_info->cmd_hdr->opcode = cpu_to_le16(BTOFF_OPCODE); | |
1438 | fw_info->cmd_hdr->plen = 1; | |
1439 | fw_info->pkt_len = CMD_HDR_LEN + 1; | |
1440 | fw_info->send_pkt[CMD_HDR_LEN] = onoff; | |
1441 | ||
1442 | ret_val = send_hci_cmd(fw_info); | |
1443 | if (ret_val < 0) { | |
1444 | RTKBT_ERR("%s: Failed to send bt %s cmd, errno %d", | |
1445 | __func__, onoff != 0 ? "on" : "off", ret_val); | |
1446 | return ret_val; | |
1447 | } | |
1448 | ||
1449 | ret_val = rcv_hci_evt(fw_info); | |
1450 | if (ret_val < 0) { | |
1451 | RTKBT_ERR("%s: Failed to receive bt %s event, errno %d", | |
1452 | __func__, onoff != 0 ? "on" : "off", ret_val); | |
1453 | return ret_val; | |
1454 | } | |
1455 | ||
1456 | return ret_val; | |
1457 | } | |
1458 | ||
1459 | static patch_info *get_fw_table_entry(struct usb_device* udev) | |
1460 | { | |
1461 | patch_info *patch_entry = fw_patch_table; | |
1462 | uint16_t vid = le16_to_cpu(udev->descriptor.idVendor); | |
1463 | uint16_t pid = le16_to_cpu(udev->descriptor.idProduct); | |
1464 | uint32_t entry_size = sizeof(fw_patch_table) / sizeof(fw_patch_table[0]); | |
1465 | uint32_t i; | |
1466 | ||
1467 | RTKBT_INFO("%s: Product id = 0x%04x, fw table entry size %d", __func__, pid, entry_size); | |
1468 | usb_info = (uint32_t)(vid<<16) | pid; | |
1469 | ||
1470 | for (i = 0; i < entry_size; i++, patch_entry++) { | |
1471 | if ((vid == patch_entry->vid)&&(pid == patch_entry->pid)) | |
1472 | break; | |
1473 | } | |
1474 | ||
1475 | if (i == entry_size) { | |
1476 | RTKBT_ERR("%s: No fw table entry found", __func__); | |
1477 | return NULL; | |
1478 | } | |
1479 | ||
1480 | return patch_entry; | |
1481 | } | |
1482 | ||
1483 | #if SUSPNED_DW_FW | |
1484 | static patch_info *get_suspend_fw_table_entry(struct usb_device* udev) | |
1485 | { | |
1486 | patch_info *patch_entry = fw_patch_table; | |
1487 | patch_info *patch_entry_real = NULL; | |
1488 | uint16_t vid = le16_to_cpu(udev->descriptor.idVendor); | |
1489 | uint16_t pid = le16_to_cpu(udev->descriptor.idProduct); | |
1490 | uint32_t entry_size = sizeof(fw_patch_table) / sizeof(fw_patch_table[0]); | |
1491 | uint32_t i; | |
1492 | ||
1493 | RTKBT_INFO("%s: Product id = 0x%04x, fw table entry size %d", __func__, pid, entry_size); | |
1494 | ||
1495 | for (i = 0; i < entry_size; i++, patch_entry++) { | |
1496 | if ((vid == patch_entry->vid)&&(pid == patch_entry->pid)) | |
1497 | break; | |
1498 | } | |
1499 | ||
1500 | if (i == entry_size) { | |
1501 | RTKBT_ERR("%s: No fw table entry found", __func__); | |
1502 | return NULL; | |
1503 | } | |
1504 | patch_entry_real = kmalloc(sizeof(fw_patch_table[0]), GFP_KERNEL); | |
1505 | if(!patch_entry_real) | |
1506 | return NULL; | |
1507 | memcpy(patch_entry_real, patch_entry, sizeof(fw_patch_table[0])); | |
1508 | return patch_entry_real; | |
1509 | } | |
1510 | #endif | |
1511 | ||
1512 | static struct rtk_epatch_entry *get_fw_patch_entry(struct rtk_epatch *epatch_info, uint16_t eco_ver) | |
1513 | { | |
1514 | int patch_num = epatch_info->number_of_total_patch; | |
1515 | uint8_t *epatch_buf = (uint8_t *)epatch_info; | |
1516 | struct rtk_epatch_entry *p_entry = NULL; | |
1517 | int coex_date; | |
1518 | int coex_ver; | |
1519 | int i; | |
1520 | ||
1521 | for (i = 0; i < patch_num; i++) { | |
1522 | if (*(uint16_t *)(epatch_buf + 14 + 2*i) == eco_ver + 1) { | |
1523 | p_entry = kzalloc(sizeof(*p_entry), GFP_KERNEL); | |
1524 | if (!p_entry) { | |
1525 | RTKBT_ERR("%s: Failed to allocate mem for patch entry", __func__); | |
1526 | return NULL; | |
1527 | } | |
1528 | p_entry->chip_id = eco_ver + 1; | |
1529 | p_entry->patch_length = *(uint16_t*)(epatch_buf + 14 + 2*patch_num + 2*i); | |
1530 | p_entry->start_offset = *(uint32_t*)(epatch_buf + 14 + 4*patch_num + 4*i); | |
1531 | p_entry->coex_version = *(uint32_t*)(epatch_buf + p_entry->start_offset + p_entry->patch_length - 12); | |
1532 | p_entry->svn_version = *(uint32_t*)(epatch_buf + p_entry->start_offset + p_entry->patch_length - 8); | |
1533 | p_entry->fw_version = *(uint32_t*)(epatch_buf + p_entry->start_offset + p_entry->patch_length - 4); | |
1534 | ||
1535 | coex_date = ((p_entry->coex_version >> 16) & 0x7ff) + ((p_entry->coex_version >> 27) * 10000); | |
1536 | coex_ver = p_entry->coex_version & 0xffff; | |
1537 | ||
1538 | 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", \ | |
1539 | 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); | |
1540 | break; | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | return p_entry; | |
1545 | } | |
1546 | ||
1547 | /*reset_controller is aimed to reset_bt_fw before updata Fw patch*/ | |
1548 | int reset_controller(firmware_info* fw_info) | |
1549 | { | |
1550 | int ret_val; | |
1551 | RTKBT_ERR("reset_controller"); | |
1552 | ||
1553 | if (!fw_info) | |
1554 | return -ENODEV; | |
1555 | ||
1556 | fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_FORCE_RESET_AND_PATCHABLE); | |
1557 | fw_info->cmd_hdr->plen = 0; | |
1558 | fw_info->pkt_len = CMD_HDR_LEN; | |
1559 | ret_val = send_hci_cmd(fw_info); | |
1560 | ||
1561 | if (ret_val < 0) { | |
1562 | RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d", | |
1563 | __func__, fw_info->cmd_hdr->opcode, ret_val); | |
1564 | return ret_val; | |
1565 | } | |
1566 | ||
1567 | //sleep 1s for firmware reset. | |
1568 | msleep(1000); | |
1569 | RTKBT_INFO("%s: Wait fw reset for 1000ms",__func__); | |
1570 | ||
1571 | return ret_val; | |
1572 | } | |
1573 | /*reset_controller is aimed to reset_bt_fw before updata Fw patch*/ | |
1574 | ||
1575 | /* | |
1576 | * check the return value | |
1577 | * 1: need to download fw patch | |
1578 | * 0: no need to download fw patch | |
1579 | * <0: failed to check lmp version | |
1580 | */ | |
1581 | int check_fw_version(firmware_info* fw_info, bool resume_check) | |
1582 | { | |
1583 | struct hci_rp_read_local_version *read_ver_rsp; | |
1584 | patch_info *patch_entry = NULL; | |
1585 | int ret_val = -1, i, ret_len = 0; | |
1586 | struct sk_buff *rtk_skb_copy = NULL; | |
1587 | unsigned char pkt_type = HCI_EVENT_PKT; | |
1588 | ||
1589 | fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_OP_READ_LOCAL_VERSION); | |
1590 | fw_info->cmd_hdr->plen = 0; | |
1591 | fw_info->pkt_len = CMD_HDR_LEN; | |
1592 | ||
1593 | ret_val = send_hci_cmd(fw_info); | |
1594 | if (ret_val < 0) { | |
1595 | RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d", | |
1596 | __func__, fw_info->cmd_hdr->opcode, ret_val); | |
1597 | return ret_val; | |
1598 | } | |
1599 | ||
1600 | while (1) { | |
1601 | for(i = 0; i < 5; i++) { | |
1602 | ret_val = usb_interrupt_msg( | |
1603 | fw_info->udev, fw_info->pipe_in, | |
1604 | (void *)(fw_info->rcv_pkt), PKT_LEN, | |
1605 | &ret_len, MSG_TO); | |
1606 | if (ret_val >= 0) | |
1607 | break; | |
1608 | } | |
1609 | ||
1610 | if (ret_val < 0) { | |
1611 | RTKBT_ERR("%s: Failed to receive hci event, errno %d", | |
1612 | __func__, ret_val); | |
1613 | return ret_val; | |
1614 | } | |
1615 | ||
1616 | if ((CMD_CMP_EVT == fw_info->evt_hdr->evt) && | |
1617 | (fw_info->cmd_hdr->opcode == fw_info->cmd_cmp->opcode)) { | |
1618 | break; | |
1619 | } | |
1620 | else if(resume_check) { | |
1621 | rtk_skb_copy = bt_skb_alloc((ret_len), GFP_ATOMIC); | |
1622 | if (!rtk_skb_copy) { | |
1623 | RTKBT_ERR("%s: Failed to allocate mem", __func__); | |
1624 | return 2; | |
1625 | } | |
1626 | ||
1627 | memcpy(skb_put(rtk_skb_copy, ret_len), fw_info->rcv_pkt, ret_len); | |
1628 | memcpy(skb_push(rtk_skb_copy, 1), &pkt_type, 1); | |
1629 | rtk_enqueue(rtk_skb_copy); | |
1630 | ||
1631 | rtk_skb_copy = NULL; | |
1632 | wake_up_interruptible(&btchr_read_wait); | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | patch_entry = fw_info->patch_entry; | |
1637 | read_ver_rsp = (struct hci_rp_read_local_version *)(fw_info->rsp_para); | |
1638 | ||
1639 | RTKBT_INFO("%s: Controller lmp = 0x%04x, patch lmp = 0x%04x, default patch lmp = 0x%04x", | |
1640 | __func__, read_ver_rsp->lmp_subver, patch_entry->lmp_sub, patch_entry->lmp_sub_default); | |
1641 | ||
1642 | if (read_ver_rsp->lmp_subver == patch_entry->lmp_sub_default) { | |
1643 | RTKBT_INFO("%s: Cold BT controller startup", __func__); | |
1644 | ||
1645 | return 2; | |
1646 | ||
1647 | } else if (read_ver_rsp->lmp_subver != patch_entry->lmp_sub) { | |
1648 | RTKBT_INFO("%s: Warm BT controller startup with updated lmp", __func__); | |
1649 | return 1; | |
1650 | } else { | |
1651 | RTKBT_INFO("%s: Warm BT controller startup with same lmp", __func__); | |
1652 | return 0; | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | #if SET_WAKEUP_DEVICE | |
1657 | int set_wakeup_device(firmware_info* fw_info, uint8_t* wakeup_bdaddr) | |
1658 | { | |
1659 | struct rtk_eversion_evt *ever_evt; | |
1660 | int ret_val; | |
1661 | ||
1662 | if (!fw_info) | |
1663 | return -ENODEV; | |
1664 | ||
1665 | fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_ADD_WAKE_UP_DEVICE); | |
1666 | fw_info->cmd_hdr->plen = 7; | |
1667 | memcpy(fw_info->req_para, wakeup_bdaddr, 7); | |
1668 | fw_info->pkt_len = CMD_HDR_LEN + 7; | |
1669 | ||
1670 | ret_val = send_hci_cmd(fw_info); | |
1671 | if (ret_val < 0) { | |
1672 | RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d\n", | |
1673 | __func__, fw_info->cmd_hdr->opcode, ret_val); | |
1674 | return ret_val; | |
1675 | } | |
1676 | ||
1677 | ret_val = rcv_hci_evt(fw_info); | |
1678 | if (ret_val < 0) { | |
1679 | RTKBT_ERR("%s: Failed to receive hci event, errno %d\n",__func__, ret_val); | |
1680 | return ret_val; | |
1681 | } | |
1682 | ||
1683 | ever_evt = (struct rtk_eversion_evt *)(fw_info->rsp_para); | |
1684 | ||
1685 | RTKBT_DBG("%s: status %d, eversion %d", __func__, ever_evt->status, ever_evt->version); | |
1686 | return ret_val; | |
1687 | } | |
1688 | #endif | |
1689 | ||
1690 | /*reset_channel to recover the communication between wifi 8192eu with 8761 bt controller in case of geteversion error*/ | |
1691 | ||
1692 | int reset_channel(firmware_info* fw_info) | |
1693 | { | |
1694 | struct rtk_reset_evt *ever_evt; | |
1695 | int ret_val; | |
1696 | ||
1697 | if (!fw_info) | |
1698 | return -ENODEV; | |
1699 | ||
1700 | fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_RESET); | |
1701 | fw_info->cmd_hdr->plen = 0; | |
1702 | fw_info->pkt_len = CMD_HDR_LEN; | |
1703 | ||
1704 | ret_val = send_hci_cmd(fw_info); | |
1705 | if (ret_val < 0) { | |
1706 | RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d", | |
1707 | __func__, fw_info->cmd_hdr->opcode, ret_val); | |
1708 | return ret_val; | |
1709 | } | |
1710 | ||
1711 | ret_val = rcv_hci_evt(fw_info); | |
1712 | if (ret_val < 0) { | |
1713 | RTKBT_ERR("%s: Failed to receive hci event, errno %d", | |
1714 | __func__, ret_val); | |
1715 | return ret_val; | |
1716 | } | |
1717 | ||
1718 | ever_evt = (struct rtk_reset_evt *)(fw_info->rsp_para); | |
1719 | ||
1720 | RTKBT_INFO("%s: status %d ", __func__, ever_evt->status); | |
1721 | ||
1722 | //sleep 300ms for channel reset. | |
1723 | msleep(300); | |
1724 | RTKBT_INFO("%s: Wait channel reset for 300ms",__func__); | |
1725 | ||
1726 | return ret_val; | |
1727 | } | |
1728 | ||
1729 | int read_localversion(firmware_info* fw_info) | |
1730 | { | |
1731 | struct rtk_localversion_evt *ever_evt; | |
1732 | int ret_val; | |
1733 | ||
1734 | if (!fw_info) | |
1735 | return -ENODEV; | |
1736 | ||
1737 | fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_READ_LMP_VERISION); | |
1738 | fw_info->cmd_hdr->plen = 0; | |
1739 | fw_info->pkt_len = CMD_HDR_LEN; | |
1740 | ||
1741 | ret_val = send_hci_cmd(fw_info); | |
1742 | if (ret_val < 0) { | |
1743 | RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d", | |
1744 | __func__, fw_info->cmd_hdr->opcode, ret_val); | |
1745 | return ret_val; | |
1746 | } | |
1747 | ||
1748 | ret_val = rcv_hci_evt(fw_info); | |
1749 | if (ret_val < 0) { | |
1750 | RTKBT_ERR("%s: Failed to receive hci event, errno %d", | |
1751 | __func__, ret_val); | |
1752 | return ret_val; | |
1753 | } | |
1754 | ||
1755 | ever_evt = (struct rtk_localversion_evt *)(fw_info->rsp_para); | |
1756 | ||
1757 | RTKBT_INFO("%s: status %d ", __func__, ever_evt->status); | |
1758 | RTKBT_INFO("%s: hci_version %d ", __func__, ever_evt->hci_version); | |
1759 | RTKBT_INFO("%s: hci_revision %d ", __func__, ever_evt->hci_revision); | |
1760 | RTKBT_INFO("%s: lmp_version %d ", __func__, ever_evt->lmp_version); | |
1761 | RTKBT_INFO("%s: lmp_subversion %d ", __func__, ever_evt->lmp_subversion); | |
1762 | RTKBT_INFO("%s: lmp_manufacture %d ", __func__, ever_evt->lmp_manufacture); | |
1763 | //sleep 300ms for channel reset. | |
1764 | msleep(300); | |
1765 | RTKBT_INFO("%s: Wait channel reset for 300ms",__func__); | |
1766 | ||
1767 | return ret_val; | |
1768 | } | |
1769 | ||
1770 | int get_eversion(firmware_info* fw_info) | |
1771 | { | |
1772 | struct rtk_eversion_evt *ever_evt; | |
1773 | int ret_val; | |
1774 | ||
1775 | if (!fw_info) | |
1776 | return -ENODEV; | |
1777 | ||
1778 | fw_info->cmd_hdr->opcode = cpu_to_le16(HCI_VENDOR_READ_RTK_ROM_VERISION); | |
1779 | fw_info->cmd_hdr->plen = 0; | |
1780 | fw_info->pkt_len = CMD_HDR_LEN; | |
1781 | ||
1782 | ret_val = send_hci_cmd(fw_info); | |
1783 | if (ret_val < 0) { | |
1784 | RTKBT_ERR("%s: Failed to send hci cmd 0x%04x, errno %d", | |
1785 | __func__, fw_info->cmd_hdr->opcode, ret_val); | |
1786 | return ret_val; | |
1787 | } | |
1788 | ||
1789 | ret_val = rcv_hci_evt(fw_info); | |
1790 | if (ret_val < 0) { | |
1791 | RTKBT_ERR("%s: Failed to receive hci event, errno %d", | |
1792 | __func__, ret_val); | |
1793 | return ret_val; | |
1794 | } | |
1795 | ||
1796 | ever_evt = (struct rtk_eversion_evt *)(fw_info->rsp_para); | |
1797 | ||
1798 | RTKBT_INFO("%s: status %d, eversion %d", __func__, ever_evt->status, ever_evt->version); | |
1799 | ||
1800 | if (ever_evt->status) | |
1801 | fw_info->patch_entry->eversion = 0; | |
1802 | else | |
1803 | fw_info->patch_entry->eversion = ever_evt->version; | |
1804 | ||
1805 | return ret_val; | |
1806 | } | |
1807 | ||
1808 | 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) | |
1809 | { | |
1810 | static unsigned char config_buf[1024]; | |
1811 | unsigned short offset[256]; | |
1812 | unsigned char val[256]; | |
1813 | ||
1814 | struct rtk_bt_vendor_config* config = (struct rtk_bt_vendor_config*) config_buf; | |
1815 | struct rtk_bt_vendor_config_entry* entry = config->entry; | |
1816 | ||
1817 | int count = 0,temp = 0, i = 0, j; | |
1818 | ||
1819 | memset(config_buf, 0, sizeof(config_buf)); | |
1820 | memset(offset, 0, sizeof(offset)); | |
1821 | memset(val, 0, sizeof(val)); | |
1822 | ||
1823 | memcpy(config_buf, org_config_buf, org_config_len); | |
1824 | *new_config_buf_ptr = config_buf; | |
1825 | *new_config_len_ptr = org_config_len; | |
1826 | ||
1827 | count = getAltSettings(patch_entry, offset, sizeof(offset)/sizeof(unsigned short)); | |
1828 | if(count <= 0){ | |
1829 | RTKBT_INFO("rtk_update_altsettings: No AltSettings"); | |
1830 | return; | |
1831 | }else{ | |
1832 | RTKBT_INFO("rtk_update_altsettings: %d AltSettings", count); | |
1833 | } | |
1834 | ||
1835 | RTKBT_INFO("ORG Config len=%08x:\n", org_config_len); | |
1836 | for(i=0;i<=org_config_len;i+=0x10) | |
1837 | { | |
1838 | RTKBT_INFO("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, \ | |
1839 | 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], \ | |
1840 | 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]); | |
1841 | } | |
1842 | ||
1843 | if (config->data_len != org_config_len - sizeof(struct rtk_bt_vendor_config)) | |
1844 | { | |
1845 | //RTKBT_ERR("rtk_update_altsettings: config len(%x) is not right(%x)", config->data_len, org_config_len-sizeof(struct rtk_bt_vendor_config)); | |
1846 | return; | |
1847 | } | |
1848 | ||
1849 | for (i=0; i<config->data_len;) | |
1850 | { | |
1851 | for(j = 0; j < count;j++) | |
1852 | { | |
1853 | if(entry->offset == offset[j]) | |
1854 | offset[j] = 0; | |
1855 | } | |
1856 | if(getAltSettingVal(patch_entry, entry->offset, val) == entry->entry_len){ | |
1857 | RTKBT_INFO("rtk_update_altsettings: replace %04x[%02x]", entry->offset, entry->entry_len); | |
1858 | memcpy(entry->entry_data, val, entry->entry_len); | |
1859 | } | |
1860 | temp = entry->entry_len + sizeof(struct rtk_bt_vendor_config_entry); | |
1861 | i += temp; | |
1862 | entry = (struct rtk_bt_vendor_config_entry*)((uint8_t*)entry + temp); | |
1863 | } | |
1864 | for(j = 0; j < count;j++){ | |
1865 | if(offset[j] == 0) | |
1866 | continue; | |
1867 | entry->entry_len = getAltSettingVal(patch_entry, offset[j], val); | |
1868 | if(entry->entry_len <= 0) | |
1869 | continue; | |
1870 | entry->offset = offset[j]; | |
1871 | memcpy(entry->entry_data, val, entry->entry_len); | |
1872 | RTKBT_INFO("rtk_update_altsettings: add %04x[%02x]", entry->offset, entry->entry_len); | |
1873 | temp = entry->entry_len + sizeof(struct rtk_bt_vendor_config_entry); | |
1874 | i += temp; | |
1875 | entry = (struct rtk_bt_vendor_config_entry*)((uint8_t*)entry + temp); | |
1876 | } | |
1877 | config->data_len = i; | |
1878 | *new_config_buf_ptr = config_buf; | |
1879 | *new_config_len_ptr = config->data_len+sizeof(struct rtk_bt_vendor_config); | |
1880 | ||
1881 | return; | |
1882 | } | |
1883 | ||
1884 | int load_firmware(firmware_info *fw_info, uint8_t **buff) | |
1885 | { | |
1886 | const struct firmware *fw, *cfg; | |
1887 | struct usb_device *udev; | |
1888 | patch_info *patch_entry; | |
1889 | char *config_name, *fw_name; | |
1890 | int fw_len = 0; | |
1891 | int ret_val; | |
1892 | ||
1893 | int config_len = 0, buf_len = -1; | |
1894 | uint8_t *buf = *buff, *config_file_buf = NULL; | |
1895 | uint8_t *epatch_buf = NULL; | |
1896 | ||
1897 | struct rtk_epatch *epatch_info = NULL; | |
1898 | uint8_t need_download_fw = 1; | |
1899 | struct rtk_extension_entry patch_lmp = {0}; | |
1900 | struct rtk_epatch_entry *p_epatch_entry = NULL; | |
1901 | uint16_t lmp_version; | |
1902 | //uint8_t use_mp_fw = 0; | |
1903 | RTKBT_DBG("%s: start", __func__); | |
1904 | ||
1905 | udev = fw_info->udev; | |
1906 | patch_entry = fw_info->patch_entry; | |
1907 | lmp_version = patch_entry->lmp_sub_default; | |
1908 | config_name = patch_entry->config_name; | |
1909 | /* 1 Mptool Fw; 0 Normal Fw */ | |
1910 | if(DRV_MP_MODE == mp_drv_mode){ | |
1911 | fw_name = patch_entry->mp_patch_name; | |
1912 | }else{ | |
1913 | fw_name = patch_entry->patch_name; | |
1914 | } | |
1915 | ||
1916 | RTKBT_INFO("%s: Default lmp version = 0x%04x, config file name[%s], " | |
1917 | "fw file name[%s]", __func__, lmp_version,config_name, fw_name); | |
1918 | ||
1919 | ret_val = request_firmware(&cfg, config_name, &udev->dev); | |
1920 | if (ret_val < 0) | |
1921 | config_len = 0; | |
1922 | else { | |
1923 | int i; | |
1924 | rtk_update_altsettings(patch_entry, cfg->data, cfg->size, &config_file_buf, &config_len); | |
1925 | ||
1926 | RTKBT_INFO("Final Config len=%08x:\n", config_len); | |
1927 | for(i=0;i<=config_len;i+=0x10) | |
1928 | { | |
1929 | RTKBT_INFO("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, \ | |
1930 | 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], \ | |
1931 | 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]); | |
1932 | } | |
1933 | ||
1934 | release_firmware(cfg); | |
1935 | } | |
1936 | ||
1937 | ret_val = request_firmware(&fw, fw_name, &udev->dev); | |
1938 | if (ret_val < 0) | |
1939 | goto fw_fail; | |
1940 | else { | |
1941 | epatch_buf = vmalloc(fw->size); | |
1942 | RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__); | |
1943 | if (!epatch_buf) { | |
1944 | release_firmware(fw); | |
1945 | goto fw_fail; | |
1946 | } | |
1947 | memcpy(epatch_buf, fw->data, fw->size); | |
1948 | fw_len = fw->size; | |
1949 | buf_len = fw_len + config_len; | |
1950 | release_firmware(fw); | |
1951 | } | |
1952 | ||
1953 | if (lmp_version == ROM_LMP_8723a) { | |
1954 | RTKBT_DBG("%s: 8723a -> use old style patch", __func__); | |
1955 | if (!memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) { | |
1956 | RTKBT_ERR("%s: 8723a check signature error", __func__); | |
1957 | need_download_fw = 0; | |
1958 | } else { | |
1959 | if (!(buf = kzalloc(buf_len, GFP_KERNEL))) { | |
1960 | RTKBT_ERR("%s: Failed to allocate mem for fw&config", __func__); | |
1961 | buf_len = -1; | |
1962 | } else { | |
1963 | RTKBT_DBG("%s: 8723a -> fw copy directly", __func__); | |
1964 | memcpy(buf, epatch_buf, buf_len); | |
1965 | patch_entry->lmp_sub = *(uint16_t *)(buf + buf_len - config_len - 4); | |
1966 | RTKBT_DBG("%s: Config lmp version = 0x%04x", __func__, | |
1967 | patch_entry->lmp_sub); | |
1968 | vfree(epatch_buf); | |
1969 | RTKBT_INFO("%s:ROM_LMP_8723a vfree(epatch_buf)", __func__); | |
1970 | epatch_buf = NULL; | |
1971 | if (config_len) | |
1972 | memcpy(buf + buf_len - config_len, config_file_buf, config_len); | |
1973 | } | |
1974 | } | |
1975 | } else { | |
1976 | RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__); | |
1977 | ||
1978 | RTKBT_DBG("%s: reset_channel before get_eversion from bt controller", __func__); | |
1979 | ret_val = reset_channel(fw_info); | |
1980 | if (ret_val < 0) { | |
1981 | RTKBT_ERR("%s: Failed to reset_channel, errno %d", __func__, ret_val); | |
1982 | goto fw_fail; | |
1983 | } | |
1984 | // read_localversion(fw_info); | |
1985 | RTKBT_DBG("%s: get_eversion from bt controller", __func__); | |
1986 | ||
1987 | ret_val = get_eversion(fw_info); | |
1988 | if (ret_val < 0) { | |
1989 | RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val); | |
1990 | goto fw_fail; | |
1991 | } | |
1992 | RTKBT_DBG("%s: Get eversion =%d", __func__, patch_entry->eversion); | |
1993 | if (memcmp(epatch_buf + buf_len - config_len - 4 , EXTENSION_SECTION_SIGNATURE, 4)) { | |
1994 | RTKBT_ERR("%s: Failed to check extension section signature", __func__); | |
1995 | need_download_fw = 0; | |
1996 | } else { | |
1997 | uint8_t *temp; | |
1998 | temp = epatch_buf+buf_len-config_len - 5; | |
1999 | do { | |
2000 | if (*temp == 0x00) { | |
2001 | patch_lmp.opcode = *temp; | |
2002 | patch_lmp.length = *(temp-1); | |
2003 | if ((patch_lmp.data = kzalloc(patch_lmp.length, GFP_KERNEL))) { | |
2004 | int k; | |
2005 | for (k = 0; k < patch_lmp.length; k++) { | |
2006 | *(patch_lmp.data+k) = *(temp-2-k); | |
2007 | RTKBT_DBG("data = 0x%x", *(patch_lmp.data+k)); | |
2008 | } | |
2009 | } | |
2010 | RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__, | |
2011 | patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data)); | |
2012 | break; | |
2013 | } | |
2014 | temp -= *(temp-1) + 2; | |
2015 | } while (*temp != 0xFF); | |
2016 | ||
2017 | if (lmp_version != project_id[*(patch_lmp.data)]) { | |
2018 | RTKBT_ERR("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x " | |
2019 | "-> not match", __func__, lmp_version, *(patch_lmp.data),project_id[*(patch_lmp.data)]); | |
2020 | if (patch_lmp.data) | |
2021 | kfree(patch_lmp.data); | |
2022 | need_download_fw = 0; | |
2023 | } else { | |
2024 | RTKBT_INFO("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x " | |
2025 | "-> match", __func__, lmp_version, *(patch_lmp.data), project_id[*(patch_lmp.data)]); | |
2026 | if (patch_lmp.data) | |
2027 | kfree(patch_lmp.data); | |
2028 | if (memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) { | |
2029 | RTKBT_ERR("%s: Check signature error", __func__); | |
2030 | need_download_fw = 0; | |
2031 | } else { | |
2032 | epatch_info = (struct rtk_epatch*)epatch_buf; | |
2033 | patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version; | |
2034 | ||
2035 | RTKBT_DBG("%s: lmp version 0x%04x, fw_version 0x%x, " | |
2036 | "number_of_total_patch %d", __func__, | |
2037 | patch_entry->lmp_sub, epatch_info->fw_version, | |
2038 | epatch_info->number_of_total_patch); | |
2039 | ||
2040 | /* Get right epatch entry */ | |
2041 | p_epatch_entry = get_fw_patch_entry(epatch_info, patch_entry->eversion); | |
2042 | if (p_epatch_entry == NULL) { | |
2043 | RTKBT_WARN("%s: Failed to get fw patch entry", __func__); | |
2044 | ret_val = -1; | |
2045 | goto fw_fail ; | |
2046 | } | |
2047 | ||
2048 | buf_len = p_epatch_entry->patch_length + config_len; | |
2049 | RTKBT_DBG("buf_len = 0x%x", buf_len); | |
2050 | ||
2051 | if (!(buf = kzalloc(buf_len, GFP_KERNEL))) { | |
2052 | RTKBT_ERR("%s: Can't alloc memory for fw&config", __func__); | |
2053 | buf_len = -1; | |
2054 | } else { | |
2055 | memcpy(buf, &epatch_buf[p_epatch_entry->start_offset], p_epatch_entry->patch_length); | |
2056 | memcpy(&buf[p_epatch_entry->patch_length-4], &epatch_info->fw_version, 4); | |
2057 | kfree(p_epatch_entry); | |
2058 | } | |
2059 | vfree(epatch_buf); | |
2060 | RTKBT_INFO("%s: vfree(epatch_buf)", __func__); | |
2061 | epatch_buf = NULL; | |
2062 | ||
2063 | if (config_len) | |
2064 | memcpy(&buf[buf_len - config_len], config_file_buf, config_len); | |
2065 | } | |
2066 | } | |
2067 | } | |
2068 | } | |
2069 | ||
2070 | RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__, | |
2071 | (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not"); | |
2072 | ||
2073 | if (buf && buf_len > 0 && need_download_fw) | |
2074 | *buff = buf; | |
2075 | ||
2076 | RTKBT_DBG("%s: done", __func__); | |
2077 | ||
2078 | return buf_len; | |
2079 | ||
2080 | fw_fail: | |
2081 | return ret_val; | |
2082 | } | |
2083 | ||
2084 | #if SUSPNED_DW_FW | |
2085 | static int load_suspend_firmware(firmware_info *fw_info, uint8_t **buff) | |
2086 | { | |
2087 | const struct firmware *fw, *cfg; | |
2088 | struct usb_device *udev; | |
2089 | patch_info *patch_entry; | |
2090 | char config_name[100] = {0}; | |
2091 | char fw_name[100] = {0}; | |
2092 | int fw_len = 0; | |
2093 | int ret_val; | |
2094 | ||
2095 | int config_len = 0, buf_len = -1; | |
2096 | uint8_t *buf = *buff, *config_file_buf = NULL; | |
2097 | uint8_t *epatch_buf = NULL; | |
2098 | ||
2099 | struct rtk_epatch *epatch_info = NULL; | |
2100 | uint8_t need_download_fw = 1; | |
2101 | struct rtk_extension_entry patch_lmp = {0}; | |
2102 | struct rtk_epatch_entry *p_epatch_entry = NULL; | |
2103 | uint16_t lmp_version; | |
2104 | RTKBT_DBG("%s: start", __func__); | |
2105 | ||
2106 | udev = fw_info->udev; | |
2107 | patch_entry = fw_info->patch_entry; | |
2108 | lmp_version = patch_entry->lmp_sub_default; | |
2109 | sprintf(config_name, "%s_suspend", patch_entry->config_name); | |
2110 | sprintf(fw_name, "%s_suspend", patch_entry->patch_name); | |
2111 | ||
2112 | RTKBT_INFO("%s: Default lmp version = 0x%04x, config file name[%s], " | |
2113 | "fw file name[%s]", __func__, lmp_version,config_name, fw_name); | |
2114 | ||
2115 | ret_val = request_firmware(&cfg, config_name, &udev->dev); | |
2116 | if (ret_val < 0) | |
2117 | config_len = 0; | |
2118 | else { | |
2119 | int i; | |
2120 | rtk_update_altsettings(patch_entry, cfg->data, cfg->size, &config_file_buf, &config_len); | |
2121 | ||
2122 | RTKBT_INFO("Final Config len=%08x:\n", config_len); | |
2123 | for(i=0;i<=config_len;i+=0x10) | |
2124 | { | |
2125 | RTKBT_INFO("%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, \ | |
2126 | 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], \ | |
2127 | 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]); | |
2128 | } | |
2129 | ||
2130 | release_firmware(cfg); | |
2131 | } | |
2132 | ||
2133 | ret_val = request_firmware(&fw, fw_name, &udev->dev); | |
2134 | if (ret_val < 0) | |
2135 | goto fw_fail; | |
2136 | else { | |
2137 | epatch_buf = vmalloc(fw->size); | |
2138 | RTKBT_INFO("%s: epatch_buf = vmalloc(fw->size, GFP_KERNEL)", __func__); | |
2139 | if (!epatch_buf) { | |
2140 | release_firmware(fw); | |
2141 | goto fw_fail; | |
2142 | } | |
2143 | memcpy(epatch_buf, fw->data, fw->size); | |
2144 | fw_len = fw->size; | |
2145 | buf_len = fw_len + config_len; | |
2146 | release_firmware(fw); | |
2147 | } | |
2148 | ||
2149 | RTKBT_DBG("%s: Not 8723a -> use new style patch", __func__); | |
2150 | ||
2151 | RTKBT_DBG("%s: get_eversion from bt controller", __func__); | |
2152 | ||
2153 | ret_val = get_eversion(fw_info); | |
2154 | if (ret_val < 0) { | |
2155 | RTKBT_ERR("%s: Failed to get eversion, errno %d", __func__, ret_val); | |
2156 | goto fw_fail; | |
2157 | } | |
2158 | RTKBT_DBG("%s: Get eversion =%d", __func__, patch_entry->eversion); | |
2159 | if (memcmp(epatch_buf + buf_len - config_len - 4 , EXTENSION_SECTION_SIGNATURE, 4)) { | |
2160 | RTKBT_ERR("%s: Failed to check extension section signature", __func__); | |
2161 | need_download_fw = 0; | |
2162 | } else { | |
2163 | uint8_t *temp; | |
2164 | temp = epatch_buf+buf_len-config_len - 5; | |
2165 | do { | |
2166 | if (*temp == 0x00) { | |
2167 | patch_lmp.opcode = *temp; | |
2168 | patch_lmp.length = *(temp-1); | |
2169 | if ((patch_lmp.data = kzalloc(patch_lmp.length, GFP_KERNEL))) { | |
2170 | int k; | |
2171 | for (k = 0; k < patch_lmp.length; k++) { | |
2172 | *(patch_lmp.data+k) = *(temp-2-k); | |
2173 | RTKBT_DBG("data = 0x%x", *(patch_lmp.data+k)); | |
2174 | } | |
2175 | } | |
2176 | RTKBT_DBG("%s: opcode = 0x%x, length = 0x%x, data = 0x%x", __func__, | |
2177 | patch_lmp.opcode, patch_lmp.length, *(patch_lmp.data)); | |
2178 | break; | |
2179 | } | |
2180 | temp -= *(temp-1) + 2; | |
2181 | } while (*temp != 0xFF); | |
2182 | ||
2183 | if (lmp_version != project_id[*(patch_lmp.data)]) { | |
2184 | RTKBT_ERR("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x " | |
2185 | "-> not match", __func__, lmp_version, *(patch_lmp.data),project_id[*(patch_lmp.data)]); | |
2186 | if (patch_lmp.data) | |
2187 | kfree(patch_lmp.data); | |
2188 | need_download_fw = 0; | |
2189 | } else { | |
2190 | RTKBT_INFO("%s: Default lmp_version 0x%04x, project_id[%d] 0x%04x " | |
2191 | "-> match", __func__, lmp_version, *(patch_lmp.data), project_id[*(patch_lmp.data)]); | |
2192 | if (patch_lmp.data) | |
2193 | kfree(patch_lmp.data); | |
2194 | if (memcmp(epatch_buf, RTK_EPATCH_SIGNATURE, 8)) { | |
2195 | RTKBT_ERR("%s: Check signature error", __func__); | |
2196 | need_download_fw = 0; | |
2197 | } else { | |
2198 | epatch_info = (struct rtk_epatch*)epatch_buf; | |
2199 | patch_entry->lmp_sub = (uint16_t)epatch_info->fw_version; | |
2200 | ||
2201 | RTKBT_DBG("%s: lmp version 0x%04x, fw_version 0x%x, " | |
2202 | "number_of_total_patch %d", __func__, | |
2203 | patch_entry->lmp_sub, epatch_info->fw_version, | |
2204 | epatch_info->number_of_total_patch); | |
2205 | ||
2206 | /* Get right epatch entry */ | |
2207 | p_epatch_entry = get_fw_patch_entry(epatch_info, patch_entry->eversion); | |
2208 | if (p_epatch_entry == NULL) { | |
2209 | RTKBT_WARN("%s: Failed to get fw patch entry", __func__); | |
2210 | ret_val = -1; | |
2211 | goto fw_fail ; | |
2212 | } | |
2213 | ||
2214 | buf_len = p_epatch_entry->patch_length + config_len; | |
2215 | RTKBT_DBG("buf_len = 0x%x", buf_len); | |
2216 | ||
2217 | if (!(buf = kzalloc(buf_len, GFP_KERNEL))) { | |
2218 | RTKBT_ERR("%s: Can't alloc memory for fw&config", __func__); | |
2219 | buf_len = -1; | |
2220 | } else { | |
2221 | memcpy(buf, &epatch_buf[p_epatch_entry->start_offset], p_epatch_entry->patch_length); | |
2222 | memcpy(&buf[p_epatch_entry->patch_length-4], &epatch_info->fw_version, 4); | |
2223 | kfree(p_epatch_entry); | |
2224 | } | |
2225 | vfree(epatch_buf); | |
2226 | RTKBT_INFO("%s: vfree(epatch_buf)", __func__); | |
2227 | epatch_buf = NULL; | |
2228 | ||
2229 | if (config_len) | |
2230 | memcpy(&buf[buf_len - config_len], config_file_buf, config_len); | |
2231 | } | |
2232 | } | |
2233 | } | |
2234 | ||
2235 | RTKBT_INFO("%s: fw%s exists, config file%s exists", __func__, | |
2236 | (buf_len > 0) ? "" : " not", (config_len > 0) ? "":" not"); | |
2237 | ||
2238 | if (buf && buf_len > 0 && need_download_fw) | |
2239 | *buff = buf; | |
2240 | ||
2241 | RTKBT_DBG("%s: done", __func__); | |
2242 | ||
2243 | return buf_len; | |
2244 | ||
2245 | fw_fail: | |
2246 | return ret_val; | |
2247 | } | |
2248 | #endif | |
2249 | ||
2250 | int get_firmware(firmware_info *fw_info, int cached) | |
2251 | { | |
2252 | patch_info *patch_entry = fw_info->patch_entry; | |
2253 | ||
2254 | RTKBT_INFO("%s: start, cached %d,patch_entry->fw_len= %d", __func__, cached,patch_entry->fw_len); | |
2255 | ||
2256 | if (cached > 0) { | |
2257 | if (patch_entry->fw_len > 0) { | |
2258 | fw_info->fw_data = kzalloc(patch_entry->fw_len, GFP_KERNEL); | |
2259 | if (!fw_info->fw_data) | |
2260 | return -ENOMEM; | |
2261 | memcpy(fw_info->fw_data, patch_entry->fw_cache, patch_entry->fw_len); | |
2262 | fw_info->fw_len = patch_entry->fw_len; | |
2263 | } else { | |
2264 | fw_info->fw_len = load_firmware(fw_info, &fw_info->fw_data); | |
2265 | if (fw_info->fw_len <= 0) | |
2266 | return -1; | |
2267 | } | |
2268 | } else { | |
2269 | fw_info->fw_len = load_firmware(fw_info, &fw_info->fw_data); | |
2270 | if (fw_info->fw_len <= 0) | |
2271 | return -1; | |
2272 | } | |
2273 | ||
2274 | return 0; | |
2275 | } | |
2276 | ||
2277 | #if SUSPNED_DW_FW | |
2278 | static int get_suspend_firmware(firmware_info *fw_info, int cached) | |
2279 | { | |
2280 | patch_info *patch_entry = fw_info->patch_entry; | |
2281 | ||
2282 | RTKBT_INFO("%s: start, cached %d,patch_entry->fw_len= %d", __func__, cached,patch_entry->fw_len); | |
2283 | ||
2284 | if (cached > 0) { | |
2285 | if (patch_entry->fw_len > 0) { | |
2286 | fw_info->fw_data = kzalloc(patch_entry->fw_len, GFP_KERNEL); | |
2287 | if (!fw_info->fw_data) | |
2288 | return -ENOMEM; | |
2289 | memcpy(fw_info->fw_data, patch_entry->fw_cache, patch_entry->fw_len); | |
2290 | fw_info->fw_len = patch_entry->fw_len; | |
2291 | } else { | |
2292 | fw_info->fw_len = load_suspend_firmware(fw_info, &fw_info->fw_data); | |
2293 | if (fw_info->fw_len <= 0) | |
2294 | return -1; | |
2295 | } | |
2296 | } else { | |
2297 | fw_info->fw_len = load_suspend_firmware(fw_info, &fw_info->fw_data); | |
2298 | if (fw_info->fw_len <= 0) | |
2299 | return -1; | |
2300 | } | |
2301 | ||
2302 | return 0; | |
2303 | } | |
2304 | #endif | |
2305 | ||
2306 | /* | |
2307 | * Open the log message only if in debugging, | |
2308 | * or it will decelerate download procedure. | |
2309 | */ | |
2310 | int download_data(firmware_info *fw_info) | |
2311 | { | |
2312 | download_cp *cmd_para; | |
2313 | download_rp *evt_para; | |
2314 | uint8_t *pcur; | |
2315 | int pkt_len, frag_num, frag_len; | |
2316 | int i, ret_val; | |
2317 | int ncmd = 1, step = 1; | |
2318 | ||
2319 | RTKBT_DBG("%s: start", __func__); | |
2320 | ||
2321 | cmd_para = (download_cp *)fw_info->req_para; | |
2322 | evt_para = (download_rp *)fw_info->rsp_para; | |
2323 | pcur = fw_info->fw_data; | |
2324 | pkt_len = CMD_HDR_LEN + sizeof(download_cp); | |
2325 | frag_num = fw_info->fw_len / PATCH_SEG_MAX + 1; | |
2326 | frag_len = PATCH_SEG_MAX; | |
2327 | ||
2328 | for (i = 0; i < frag_num; i++) { | |
2329 | cmd_para->index = i?((i-1)%0x7f+1):0; | |
2330 | if (i == (frag_num - 1)) { | |
2331 | cmd_para->index |= DATA_END; | |
2332 | frag_len = fw_info->fw_len % PATCH_SEG_MAX; | |
2333 | pkt_len -= (PATCH_SEG_MAX - frag_len); | |
2334 | } | |
2335 | fw_info->cmd_hdr->opcode = cpu_to_le16(DOWNLOAD_OPCODE); | |
2336 | fw_info->cmd_hdr->plen = sizeof(uint8_t) + frag_len; | |
2337 | fw_info->pkt_len = pkt_len; | |
2338 | memcpy(cmd_para->data, pcur, frag_len); | |
2339 | ||
2340 | if (step > 0) { | |
2341 | ret_val = send_hci_cmd(fw_info); | |
2342 | if (ret_val < 0) { | |
2343 | RTKBT_DBG("%s: Failed to send frag num %d", __func__, cmd_para->index); | |
2344 | return ret_val; | |
2345 | } else | |
2346 | RTKBT_DBG("%s: Send frag num %d", __func__, cmd_para->index); | |
2347 | ||
2348 | if (--step > 0 && i < frag_num - 1) { | |
2349 | RTKBT_DBG("%s: Continue to send frag num %d", __func__, cmd_para->index + 1); | |
2350 | pcur += PATCH_SEG_MAX; | |
2351 | continue; | |
2352 | } | |
2353 | } | |
2354 | ||
2355 | while (ncmd > 0) { | |
2356 | ret_val = rcv_hci_evt(fw_info); | |
2357 | if (ret_val < 0) { | |
2358 | RTKBT_ERR("%s: rcv_hci_evt err %d", __func__, ret_val); | |
2359 | return ret_val; | |
2360 | } else { | |
2361 | RTKBT_DBG("%s: Receive acked frag num %d", __func__, evt_para->index); | |
2362 | ncmd--; | |
2363 | } | |
2364 | ||
2365 | if (0 != evt_para->status) { | |
2366 | RTKBT_ERR("%s: Receive acked frag num %d, err status %d", | |
2367 | __func__, ret_val, evt_para->status); | |
2368 | return -1; | |
2369 | } | |
2370 | ||
2371 | if ((evt_para->index & DATA_END) || (evt_para->index == frag_num - 1)) { | |
2372 | RTKBT_DBG("%s: Receive last acked index %d", __func__, evt_para->index); | |
2373 | goto end; | |
2374 | } | |
2375 | } | |
2376 | ||
2377 | ncmd = step = fw_info->cmd_cmp->ncmd; | |
2378 | pcur += PATCH_SEG_MAX; | |
2379 | RTKBT_DBG("%s: HCI command packet num %d", __func__, ncmd); | |
2380 | } | |
2381 | ||
2382 | /* | |
2383 | * It is tricky that Host cannot receive DATA_END index from BT | |
2384 | * controller, at least for 8723au. We are doomed if failed. | |
2385 | */ | |
2386 | #if 0 | |
2387 | /* Continue to receive the responsed events until last index occurs */ | |
2388 | if (i == frag_num) { | |
2389 | RTKBT_DBG("%s: total frag count %d", __func__, frag_num); | |
2390 | while (!(evt_para->index & DATA_END)) { | |
2391 | ret_val = rcv_hci_evt(fw_info); | |
2392 | if (ret_val < 0) { | |
2393 | RTKBT_ERR("%s: rcv_hci_evt err %d", __func__, ret_val); | |
2394 | return ret_val; | |
2395 | } | |
2396 | if (0 != evt_para->status) | |
2397 | return -1; | |
2398 | RTKBT_DBG("%s: continue to receive acked frag num %d", __func__, evt_para->index); | |
2399 | } | |
2400 | } | |
2401 | #endif | |
2402 | end: | |
2403 | RTKBT_INFO("%s: done, sent %d frag pkts, received %d frag events", | |
2404 | __func__, cmd_para->index, evt_para->index); | |
2405 | return fw_info->fw_len; | |
2406 | } | |
2407 | ||
2408 | int download_patch(firmware_info *fw_info, int cached) | |
2409 | { | |
2410 | int ret_val = 0; | |
2411 | ||
2412 | RTKBT_DBG("%s: Download fw patch start, cached %d", __func__, cached); | |
2413 | ||
2414 | if (!fw_info || !fw_info->patch_entry) { | |
2415 | RTKBT_ERR("%s: No patch entry exists(fw_info %p)", __func__, fw_info); | |
2416 | ret_val = -1; | |
2417 | goto end; | |
2418 | } | |
2419 | ||
2420 | /* | |
2421 | * step1: get local firmware if existed | |
2422 | * step2: check firmware version | |
2423 | * step3: download firmware if updated | |
2424 | */ | |
2425 | ret_val = get_firmware(fw_info, cached); | |
2426 | if (ret_val < 0) { | |
2427 | RTKBT_ERR("%s: Failed to get firmware", __func__); | |
2428 | goto end; | |
2429 | } | |
2430 | ||
2431 | #if SUSPNED_DW_FW | |
2432 | if(fw_info_4_suspend) { | |
2433 | RTKBT_DBG("%s: get suspend fw first cached %d", __func__, cached); | |
2434 | ret_val = get_suspend_firmware(fw_info_4_suspend, cached); | |
2435 | if (ret_val < 0) { | |
2436 | RTKBT_ERR("%s: Failed to get suspend firmware", __func__); | |
2437 | goto end; | |
2438 | } | |
2439 | } | |
2440 | #endif | |
2441 | ||
2442 | /*check the length of fw to be download*/ | |
2443 | 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); | |
2444 | if (fw_info->fw_len > fw_info->patch_entry->max_patch_size) { | |
2445 | 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); | |
2446 | ret_val = -1; | |
2447 | goto free; | |
2448 | } | |
2449 | ||
2450 | ret_val = check_fw_version(fw_info, false); | |
2451 | ||
2452 | if (2 == ret_val) { | |
2453 | RTKBT_ERR("%s: Cold reset bt chip only download", __func__); | |
2454 | ret_val = download_data(fw_info); | |
2455 | if (ret_val > 0) | |
2456 | RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val); | |
2457 | } else if(1 == ret_val){ | |
2458 | // reset bt chip to update Fw patch | |
2459 | ret_val = reset_controller(fw_info); | |
2460 | RTKBT_ERR("%s: reset bt chip to update Fw patch, fw len %d", __func__, ret_val); | |
2461 | ret_val = download_data(fw_info); | |
2462 | if (ret_val > 0) | |
2463 | RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val); | |
2464 | } | |
2465 | ||
2466 | ||
2467 | free: | |
2468 | /* Free fw data after download finished */ | |
2469 | kfree(fw_info->fw_data); | |
2470 | fw_info->fw_data = NULL; | |
2471 | ||
2472 | end: | |
2473 | return ret_val; | |
2474 | } | |
2475 | ||
2476 | #if SUSPNED_DW_FW | |
2477 | static int download_suspend_patch(firmware_info *fw_info, int cached) | |
2478 | { | |
2479 | int ret_val = 0; | |
2480 | ||
2481 | RTKBT_DBG("%s: Download fw patch start, cached %d", __func__, cached); | |
2482 | ||
2483 | if (!fw_info || !fw_info->patch_entry) { | |
2484 | RTKBT_ERR("%s: No patch entry exists(fw_info %p)", __func__, fw_info); | |
2485 | ret_val = -1; | |
2486 | goto end; | |
2487 | } | |
2488 | ||
2489 | /*check the length of fw to be download*/ | |
2490 | RTKBT_DBG("%s:Check RTK_PATCH_LENGTH fw_info->fw_len:%d", __func__,fw_info->fw_len); | |
2491 | if (fw_info->fw_len > fw_info->patch_entry->max_patch_size || fw_info->fw_len == 0) { | |
2492 | 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); | |
2493 | ret_val = -1; | |
2494 | goto free; | |
2495 | } | |
2496 | ||
2497 | ret_val = check_fw_version(fw_info, false); | |
2498 | ||
2499 | if (2 == ret_val) { | |
2500 | RTKBT_ERR("%s: Cold reset bt chip only download", __func__); | |
2501 | ret_val = download_data(fw_info); | |
2502 | if (ret_val > 0) | |
2503 | RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val); | |
2504 | } else if(1 == ret_val){ | |
2505 | // reset bt chip to update Fw patch | |
2506 | ret_val = reset_controller(fw_info); | |
2507 | RTKBT_ERR("%s: reset bt chip to update Fw patch, fw len %d", __func__, ret_val); | |
2508 | ret_val = download_data(fw_info); | |
2509 | if (ret_val > 0) | |
2510 | RTKBT_ERR("%s: Download fw patch done, fw len %d", __func__, ret_val); | |
2511 | } | |
2512 | ||
2513 | ||
2514 | free: | |
2515 | /* Free fw data after download finished */ | |
2516 | kfree(fw_info->fw_data); | |
2517 | fw_info->fw_data = NULL; | |
2518 | ||
2519 | end: | |
2520 | return ret_val; | |
2521 | } | |
2522 | ||
2523 | static void suspend_firmware_info_init(firmware_info *fw_info) | |
2524 | { | |
2525 | RTKBT_DBG("%s: start", __func__); | |
2526 | if(!fw_info) | |
2527 | return; | |
2528 | ||
2529 | fw_info_4_suspend= kzalloc(sizeof(*fw_info), GFP_KERNEL); | |
2530 | if (!fw_info_4_suspend) | |
2531 | goto error; | |
2532 | ||
2533 | fw_info_4_suspend->send_pkt = kzalloc(PKT_LEN, GFP_KERNEL); | |
2534 | if (!fw_info_4_suspend->send_pkt) { | |
2535 | kfree(fw_info_4_suspend); | |
2536 | goto error; | |
2537 | } | |
2538 | ||
2539 | fw_info_4_suspend->rcv_pkt = kzalloc(PKT_LEN, GFP_KERNEL); | |
2540 | if (!fw_info_4_suspend->rcv_pkt) { | |
2541 | kfree(fw_info_4_suspend->send_pkt); | |
2542 | kfree(fw_info_4_suspend); | |
2543 | goto error; | |
2544 | } | |
2545 | ||
2546 | fw_info_4_suspend->patch_entry = get_suspend_fw_table_entry(fw_info->udev); | |
2547 | if (!fw_info_4_suspend->patch_entry) { | |
2548 | kfree(fw_info_4_suspend->rcv_pkt); | |
2549 | kfree(fw_info_4_suspend->send_pkt); | |
2550 | kfree(fw_info_4_suspend); | |
2551 | goto error; | |
2552 | } | |
2553 | ||
2554 | fw_info_4_suspend->intf = fw_info->intf; | |
2555 | fw_info_4_suspend->udev = fw_info->udev; | |
2556 | fw_info_4_suspend->cmd_hdr = (struct hci_command_hdr *)(fw_info_4_suspend->send_pkt); | |
2557 | fw_info_4_suspend->evt_hdr = (struct hci_event_hdr *)(fw_info_4_suspend->rcv_pkt); | |
2558 | fw_info_4_suspend->cmd_cmp = (struct hci_ev_cmd_complete *)(fw_info_4_suspend->rcv_pkt + EVT_HDR_LEN); | |
2559 | fw_info_4_suspend->req_para = fw_info_4_suspend->send_pkt + CMD_HDR_LEN; | |
2560 | fw_info_4_suspend->rsp_para = fw_info_4_suspend->rcv_pkt + EVT_HDR_LEN + CMD_CMP_LEN; | |
2561 | fw_info_4_suspend->pipe_in = fw_info->pipe_in; | |
2562 | fw_info_4_suspend->pipe_out = fw_info->pipe_out; | |
2563 | ||
2564 | return; | |
2565 | error: | |
2566 | RTKBT_DBG("%s: fail !", __func__); | |
2567 | fw_info_4_suspend = NULL; | |
2568 | return; | |
2569 | } | |
2570 | #endif | |
2571 | ||
2572 | #if SET_WAKEUP_DEVICE | |
2573 | static void set_wakeup_device_from_conf(firmware_info *fw_info) | |
2574 | { | |
2575 | uint8_t paired_wakeup_bdaddr[7]; | |
2576 | uint8_t num = 0; | |
2577 | int i; | |
2578 | struct file *fp; | |
2579 | mm_segment_t fs; | |
2580 | loff_t pos; | |
2581 | ||
2582 | memset(paired_wakeup_bdaddr, 0, 7); | |
2583 | fp = filp_open(SET_WAKEUP_DEVICE_CONF, O_RDWR, 0); | |
2584 | if (!IS_ERR(fp)) { | |
2585 | fs = get_fs(); | |
2586 | set_fs(KERNEL_DS); | |
2587 | pos = 0; | |
2588 | //read number | |
2589 | vfs_read(fp, &num, 1, &pos); | |
2590 | RTKBT_DBG("read number = %d", num); | |
2591 | if(num) { | |
2592 | for(i = 0; i < num; i++) { | |
2593 | vfs_read(fp, paired_wakeup_bdaddr, 7, &pos); | |
2594 | RTKBT_DBG("paired_wakeup_bdaddr: 0x%02x:0x%02x:0x%02x:0x%02x:0x%02x:0x%02x", | |
2595 | paired_wakeup_bdaddr[1],paired_wakeup_bdaddr[2],paired_wakeup_bdaddr[3], | |
2596 | paired_wakeup_bdaddr[4],paired_wakeup_bdaddr[5],paired_wakeup_bdaddr[6]); | |
2597 | set_wakeup_device(fw_info, paired_wakeup_bdaddr); | |
2598 | } | |
2599 | } | |
2600 | filp_close(fp, NULL); | |
2601 | set_fs(fs); | |
2602 | } | |
2603 | else { | |
2604 | RTKBT_ERR("open wakeup config file fail! errno = %ld", PTR_ERR(fp)); | |
2605 | } | |
2606 | } | |
2607 | #endif | |
2608 | ||
2609 | firmware_info *firmware_info_init(struct usb_interface *intf) | |
2610 | { | |
2611 | struct usb_device *udev = interface_to_usbdev(intf); | |
2612 | firmware_info *fw_info; | |
2613 | ||
2614 | RTKBT_DBG("%s: start", __func__); | |
2615 | ||
2616 | fw_info = kzalloc(sizeof(*fw_info), GFP_KERNEL); | |
2617 | if (!fw_info) | |
2618 | return NULL; | |
2619 | ||
2620 | fw_info->send_pkt = kzalloc(PKT_LEN, GFP_KERNEL); | |
2621 | if (!fw_info->send_pkt) { | |
2622 | kfree(fw_info); | |
2623 | return NULL; | |
2624 | } | |
2625 | ||
2626 | fw_info->rcv_pkt = kzalloc(PKT_LEN, GFP_KERNEL); | |
2627 | if (!fw_info->rcv_pkt) { | |
2628 | kfree(fw_info->send_pkt); | |
2629 | kfree(fw_info); | |
2630 | return NULL; | |
2631 | } | |
2632 | ||
2633 | fw_info->patch_entry = get_fw_table_entry(udev); | |
2634 | if (!fw_info->patch_entry) { | |
2635 | kfree(fw_info->rcv_pkt); | |
2636 | kfree(fw_info->send_pkt); | |
2637 | kfree(fw_info); | |
2638 | return NULL; | |
2639 | } | |
2640 | ||
2641 | fw_info->intf = intf; | |
2642 | fw_info->udev = udev; | |
2643 | fw_info->pipe_in = usb_rcvintpipe(fw_info->udev, INTR_EP); | |
2644 | fw_info->pipe_out = usb_sndctrlpipe(fw_info->udev, CTRL_EP); | |
2645 | fw_info->cmd_hdr = (struct hci_command_hdr *)(fw_info->send_pkt); | |
2646 | fw_info->evt_hdr = (struct hci_event_hdr *)(fw_info->rcv_pkt); | |
2647 | fw_info->cmd_cmp = (struct hci_ev_cmd_complete *)(fw_info->rcv_pkt + EVT_HDR_LEN); | |
2648 | fw_info->req_para = fw_info->send_pkt + CMD_HDR_LEN; | |
2649 | fw_info->rsp_para = fw_info->rcv_pkt + EVT_HDR_LEN + CMD_CMP_LEN; | |
2650 | ||
2651 | #if SUSPNED_DW_FW | |
2652 | suspend_firmware_info_init(fw_info); | |
2653 | #endif | |
2654 | ||
2655 | #if BTUSB_RPM | |
2656 | RTKBT_INFO("%s: Auto suspend is enabled", __func__); | |
2657 | usb_enable_autosuspend(udev); | |
2658 | pm_runtime_set_autosuspend_delay(&(udev->dev), 2000); | |
2659 | #else | |
2660 | RTKBT_INFO("%s: Auto suspend is disabled", __func__); | |
2661 | usb_disable_autosuspend(udev); | |
2662 | #endif | |
2663 | ||
2664 | #if BTUSB_WAKEUP_HOST | |
2665 | device_wakeup_enable(&udev->dev); | |
2666 | #endif | |
2667 | ||
2668 | return fw_info; | |
2669 | } | |
2670 | ||
2671 | void firmware_info_destroy(struct usb_interface *intf) | |
2672 | { | |
2673 | firmware_info *fw_info; | |
2674 | struct usb_device *udev; | |
2675 | struct btusb_data *data; | |
2676 | ||
2677 | udev = interface_to_usbdev(intf); | |
2678 | data = usb_get_intfdata(intf); | |
2679 | ||
2680 | fw_info = data->fw_info; | |
2681 | if (!fw_info) | |
2682 | return; | |
2683 | ||
2684 | #if BTUSB_RPM | |
2685 | usb_disable_autosuspend(udev); | |
2686 | #endif | |
2687 | ||
2688 | /* | |
2689 | * In order to reclaim fw data mem, we free fw_data immediately | |
2690 | * after download patch finished instead of here. | |
2691 | */ | |
2692 | kfree(fw_info->rcv_pkt); | |
2693 | kfree(fw_info->send_pkt); | |
2694 | kfree(fw_info); | |
2695 | ||
2696 | #if SUSPNED_DW_FW | |
2697 | if (!fw_info_4_suspend) | |
2698 | return; | |
2699 | ||
2700 | kfree(fw_info_4_suspend->rcv_pkt); | |
2701 | kfree(fw_info_4_suspend->send_pkt); | |
2702 | kfree(fw_info_4_suspend->patch_entry); | |
2703 | kfree(fw_info_4_suspend); | |
2704 | fw_info_4_suspend = NULL; | |
2705 | #endif | |
2706 | } | |
2707 | ||
2708 | static struct usb_driver btusb_driver; | |
2709 | ||
2710 | static struct usb_device_id btusb_table[] = { | |
2711 | { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | | |
2712 | USB_DEVICE_ID_MATCH_INT_INFO, | |
2713 | .idVendor = 0x0bda, | |
2714 | .bInterfaceClass = 0xe0, | |
2715 | .bInterfaceSubClass = 0x01, | |
2716 | .bInterfaceProtocol = 0x01 }, | |
2717 | ||
2718 | { .match_flags = USB_DEVICE_ID_MATCH_VENDOR | | |
2719 | USB_DEVICE_ID_MATCH_INT_INFO, | |
2720 | .idVendor = 0x13d3, | |
2721 | .bInterfaceClass = 0xe0, | |
2722 | .bInterfaceSubClass = 0x01, | |
2723 | .bInterfaceProtocol = 0x01 }, | |
2724 | ||
2725 | { } | |
2726 | }; | |
2727 | ||
2728 | MODULE_DEVICE_TABLE(usb, btusb_table); | |
2729 | ||
2730 | static int inc_tx(struct btusb_data *data) | |
2731 | { | |
2732 | unsigned long flags; | |
2733 | int rv; | |
2734 | ||
2735 | spin_lock_irqsave(&data->txlock, flags); | |
2736 | rv = test_bit(BTUSB_SUSPENDING, &data->flags); | |
2737 | if (!rv) | |
2738 | data->tx_in_flight++; | |
2739 | spin_unlock_irqrestore(&data->txlock, flags); | |
2740 | ||
2741 | return rv; | |
2742 | } | |
2743 | ||
2744 | void check_sco_event(struct urb *urb) | |
2745 | { | |
2746 | u8* opcode = (u8*)(urb->transfer_buffer); | |
2747 | u8 status; | |
2748 | uint16_t handle; | |
2749 | struct hci_dev *hdev = urb->context; | |
2750 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
2751 | ||
2752 | switch (*opcode) { | |
2753 | case HCI_EV_SYNC_CONN_COMPLETE: | |
2754 | RTKBT_INFO("%s: HCI_EV_SYNC_CONN_COMPLETE(0x%02x)", __func__, *opcode); | |
2755 | status = *(opcode + 2); | |
2756 | data->sco_handle = *(opcode + 3) | *(opcode + 4) << 8; | |
2757 | //hdev->voice_setting = *(uint16_t*)&opcode[15]; | |
2758 | if (status == 0) { | |
2759 | hdev->conn_hash.sco_num++; | |
2760 | hdev->notify(hdev, 0); | |
2761 | } | |
2762 | break; | |
2763 | case HCI_EV_DISCONN_COMPLETE: | |
2764 | status = *(opcode + 2); | |
2765 | handle = *(opcode + 3) | *(opcode + 4) << 8; | |
2766 | if (status == 0 && data->sco_handle == handle) { | |
2767 | RTKBT_INFO("%s: SCO HCI_EV_DISCONN_COMPLETE(0x%02x)", __func__, *opcode); | |
2768 | hdev->conn_hash.sco_num--; | |
2769 | hdev->notify(hdev, 0); | |
2770 | data->sco_handle = 0; | |
2771 | } | |
2772 | break; | |
2773 | default: | |
2774 | RTKBT_DBG("%s: event 0x%02x", __func__, *opcode); | |
2775 | break; | |
2776 | } | |
2777 | } | |
2778 | ||
2779 | static void btusb_intr_complete(struct urb *urb) | |
2780 | { | |
2781 | struct hci_dev *hdev = urb->context; | |
2782 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
2783 | int err; | |
2784 | ||
2785 | RTKBT_DBG("%s: urb %p status %d count %d ", __func__, | |
2786 | urb, urb->status, urb->actual_length); | |
2787 | ||
2788 | #ifdef CONFIG_SCO_OVER_HCI | |
2789 | check_sco_event(urb); | |
2790 | #endif | |
2791 | ||
2792 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
2793 | return; | |
2794 | ||
2795 | ||
2796 | if (urb->status == 0) { | |
2797 | hdev->stat.byte_rx += urb->actual_length; | |
2798 | ||
2799 | if (hci_recv_fragment(hdev, HCI_EVENT_PKT, | |
2800 | urb->transfer_buffer, | |
2801 | urb->actual_length) < 0) { | |
2802 | RTKBT_ERR("%s: Corrupted event packet", __func__); | |
2803 | hdev->stat.err_rx++; | |
2804 | } | |
2805 | } | |
2806 | /* Avoid suspend failed when usb_kill_urb */ | |
2807 | else if(urb->status == -ENOENT) { | |
2808 | return; | |
2809 | } | |
2810 | ||
2811 | ||
2812 | if (!test_bit(BTUSB_INTR_RUNNING, &data->flags)) | |
2813 | return; | |
2814 | ||
2815 | usb_mark_last_busy(data->udev); | |
2816 | usb_anchor_urb(urb, &data->intr_anchor); | |
2817 | ||
2818 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
2819 | if (err < 0) { | |
2820 | /* EPERM: urb is being killed; | |
2821 | * ENODEV: device got disconnected */ | |
2822 | if (err != -EPERM && err != -ENODEV) | |
2823 | RTKBT_ERR("%s: Failed to re-submit urb %p, err %d", | |
2824 | __func__, urb, err); | |
2825 | usb_unanchor_urb(urb); | |
2826 | } | |
2827 | } | |
2828 | ||
2829 | static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags) | |
2830 | { | |
2831 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
2832 | struct urb *urb; | |
2833 | unsigned char *buf; | |
2834 | unsigned int pipe; | |
2835 | int err, size; | |
2836 | ||
2837 | if (!data->intr_ep) | |
2838 | return -ENODEV; | |
2839 | ||
2840 | urb = usb_alloc_urb(0, mem_flags); | |
2841 | if (!urb) | |
2842 | return -ENOMEM; | |
2843 | ||
2844 | size = le16_to_cpu(data->intr_ep->wMaxPacketSize); | |
2845 | ||
2846 | buf = kmalloc(size, mem_flags); | |
2847 | if (!buf) { | |
2848 | usb_free_urb(urb); | |
2849 | return -ENOMEM; | |
2850 | } | |
2851 | ||
2852 | RTKBT_DBG("%s: mMaxPacketSize %d, bEndpointAddress 0x%02x", | |
2853 | __func__, size, data->intr_ep->bEndpointAddress); | |
2854 | ||
2855 | pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress); | |
2856 | ||
2857 | usb_fill_int_urb(urb, data->udev, pipe, buf, size, | |
2858 | btusb_intr_complete, hdev, | |
2859 | data->intr_ep->bInterval); | |
2860 | ||
2861 | urb->transfer_flags |= URB_FREE_BUFFER; | |
2862 | ||
2863 | usb_anchor_urb(urb, &data->intr_anchor); | |
2864 | ||
2865 | err = usb_submit_urb(urb, mem_flags); | |
2866 | if (err < 0) { | |
2867 | RTKBT_ERR("%s: Failed to submit urb %p, err %d", | |
2868 | __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_bulk_complete(struct urb *urb) | |
2878 | { | |
2879 | struct hci_dev *hdev = urb->context; | |
2880 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
2881 | int err; | |
2882 | ||
2883 | RTKBT_DBG("%s: urb %p status %d count %d", | |
2884 | __func__, urb, urb->status, urb->actual_length); | |
2885 | ||
2886 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
2887 | return; | |
2888 | ||
2889 | if (urb->status == 0) { | |
2890 | hdev->stat.byte_rx += urb->actual_length; | |
2891 | ||
2892 | if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT, | |
2893 | urb->transfer_buffer, | |
2894 | urb->actual_length) < 0) { | |
2895 | RTKBT_ERR("%s: Corrupted ACL packet", __func__); | |
2896 | hdev->stat.err_rx++; | |
2897 | } | |
2898 | } | |
2899 | /* Avoid suspend failed when usb_kill_urb */ | |
2900 | else if(urb->status == -ENOENT) { | |
2901 | return; | |
2902 | } | |
2903 | ||
2904 | ||
2905 | if (!test_bit(BTUSB_BULK_RUNNING, &data->flags)) | |
2906 | return; | |
2907 | ||
2908 | usb_anchor_urb(urb, &data->bulk_anchor); | |
2909 | usb_mark_last_busy(data->udev); | |
2910 | ||
2911 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
2912 | if (err < 0) { | |
2913 | /* -EPERM: urb is being killed; | |
2914 | * -ENODEV: device got disconnected */ | |
2915 | if (err != -EPERM && err != -ENODEV) | |
2916 | RTKBT_ERR("btusb_bulk_complete %s urb %p failed to resubmit (%d)", | |
2917 | hdev->name, urb, -err); | |
2918 | usb_unanchor_urb(urb); | |
2919 | } | |
2920 | } | |
2921 | ||
2922 | static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags) | |
2923 | { | |
2924 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
2925 | struct urb *urb; | |
2926 | unsigned char *buf; | |
2927 | unsigned int pipe; | |
2928 | int err, size = HCI_MAX_FRAME_SIZE; | |
2929 | ||
2930 | RTKBT_DBG("%s: hdev name %s", __func__, hdev->name); | |
2931 | ||
2932 | if (!data->bulk_rx_ep) | |
2933 | return -ENODEV; | |
2934 | ||
2935 | urb = usb_alloc_urb(0, mem_flags); | |
2936 | if (!urb) | |
2937 | return -ENOMEM; | |
2938 | ||
2939 | buf = kmalloc(size, mem_flags); | |
2940 | if (!buf) { | |
2941 | usb_free_urb(urb); | |
2942 | return -ENOMEM; | |
2943 | } | |
2944 | ||
2945 | pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress); | |
2946 | ||
2947 | usb_fill_bulk_urb(urb, data->udev, pipe, | |
2948 | buf, size, btusb_bulk_complete, hdev); | |
2949 | ||
2950 | urb->transfer_flags |= URB_FREE_BUFFER; | |
2951 | ||
2952 | usb_mark_last_busy(data->udev); | |
2953 | usb_anchor_urb(urb, &data->bulk_anchor); | |
2954 | ||
2955 | err = usb_submit_urb(urb, mem_flags); | |
2956 | if (err < 0) { | |
2957 | RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb, err); | |
2958 | usb_unanchor_urb(urb); | |
2959 | } | |
2960 | ||
2961 | usb_free_urb(urb); | |
2962 | ||
2963 | return err; | |
2964 | } | |
2965 | ||
2966 | static void btusb_isoc_complete(struct urb *urb) | |
2967 | { | |
2968 | struct hci_dev *hdev = urb->context; | |
2969 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
2970 | int i, err; | |
2971 | ||
2972 | ||
2973 | RTKBT_DBG("%s: urb %p status %d count %d", | |
2974 | __func__, urb, urb->status, urb->actual_length); | |
2975 | ||
2976 | if (!test_bit(HCI_RUNNING, &hdev->flags) || !test_bit(BTUSB_ISOC_RUNNING, &data->flags)) | |
2977 | return; | |
2978 | ||
2979 | if (urb->status == 0) { | |
2980 | for (i = 0; i < urb->number_of_packets; i++) { | |
2981 | unsigned int offset = urb->iso_frame_desc[i].offset; | |
2982 | unsigned int length = urb->iso_frame_desc[i].actual_length; | |
2983 | ||
2984 | if (urb->iso_frame_desc[i].status) | |
2985 | continue; | |
2986 | ||
2987 | hdev->stat.byte_rx += length; | |
2988 | ||
2989 | if (hci_recv_fragment(hdev, HCI_SCODATA_PKT, | |
2990 | urb->transfer_buffer + offset, | |
2991 | length) < 0) { | |
2992 | RTKBT_ERR("%s: Corrupted SCO packet", __func__); | |
2993 | hdev->stat.err_rx++; | |
2994 | } | |
2995 | } | |
2996 | } | |
2997 | /* Avoid suspend failed when usb_kill_urb */ | |
2998 | else if(urb->status == -ENOENT) { | |
2999 | return; | |
3000 | } | |
3001 | ||
3002 | usb_anchor_urb(urb, &data->isoc_anchor); | |
3003 | i = 0; | |
3004 | retry: | |
3005 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
3006 | if (err < 0) { | |
3007 | /* -EPERM: urb is being killed; | |
3008 | * -ENODEV: device got disconnected */ | |
3009 | if (err != -EPERM && err != -ENODEV) | |
3010 | RTKBT_ERR("%s: Failed to re-sumbit urb %p, retry %d, err %d", | |
3011 | __func__, urb, i, err); | |
3012 | if (i < 10) { | |
3013 | i++; | |
3014 | mdelay(1); | |
3015 | goto retry; | |
3016 | } | |
3017 | ||
3018 | usb_unanchor_urb(urb); | |
3019 | } | |
3020 | } | |
3021 | ||
3022 | static inline void fill_isoc_descriptor(struct urb *urb, int len, int mtu) | |
3023 | { | |
3024 | int i, offset = 0; | |
3025 | ||
3026 | RTKBT_DBG("%s: len %d mtu %d", __func__, len, mtu); | |
3027 | ||
3028 | for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu; | |
3029 | i++, offset += mtu, len -= mtu) { | |
3030 | urb->iso_frame_desc[i].offset = offset; | |
3031 | urb->iso_frame_desc[i].length = mtu; | |
3032 | } | |
3033 | ||
3034 | if (len && i < BTUSB_MAX_ISOC_FRAMES) { | |
3035 | urb->iso_frame_desc[i].offset = offset; | |
3036 | urb->iso_frame_desc[i].length = len; | |
3037 | i++; | |
3038 | } | |
3039 | ||
3040 | urb->number_of_packets = i; | |
3041 | } | |
3042 | ||
3043 | static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags) | |
3044 | { | |
3045 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3046 | struct urb *urb; | |
3047 | unsigned char *buf; | |
3048 | unsigned int pipe; | |
3049 | int err, size; | |
3050 | ||
3051 | if (!data->isoc_rx_ep) | |
3052 | return -ENODEV; | |
3053 | ||
3054 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags); | |
3055 | if (!urb) | |
3056 | return -ENOMEM; | |
3057 | ||
3058 | size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) * | |
3059 | BTUSB_MAX_ISOC_FRAMES; | |
3060 | ||
3061 | buf = kmalloc(size, mem_flags); | |
3062 | if (!buf) { | |
3063 | usb_free_urb(urb); | |
3064 | return -ENOMEM; | |
3065 | } | |
3066 | ||
3067 | pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress); | |
3068 | ||
3069 | urb->dev = data->udev; | |
3070 | urb->pipe = pipe; | |
3071 | urb->context = hdev; | |
3072 | urb->complete = btusb_isoc_complete; | |
3073 | urb->interval = data->isoc_rx_ep->bInterval; | |
3074 | ||
3075 | urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP; | |
3076 | urb->transfer_buffer = buf; | |
3077 | urb->transfer_buffer_length = size; | |
3078 | ||
3079 | fill_isoc_descriptor(urb, size, | |
3080 | le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize)); | |
3081 | ||
3082 | usb_anchor_urb(urb, &data->isoc_anchor); | |
3083 | ||
3084 | err = usb_submit_urb(urb, mem_flags); | |
3085 | if (err < 0) { | |
3086 | RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb, err); | |
3087 | usb_unanchor_urb(urb); | |
3088 | } | |
3089 | ||
3090 | usb_free_urb(urb); | |
3091 | ||
3092 | return err; | |
3093 | } | |
3094 | ||
3095 | static void btusb_tx_complete(struct urb *urb) | |
3096 | { | |
3097 | struct sk_buff *skb = urb->context; | |
3098 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
3099 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3100 | ||
3101 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
3102 | goto done; | |
3103 | ||
3104 | if (!urb->status) | |
3105 | hdev->stat.byte_tx += urb->transfer_buffer_length; | |
3106 | else | |
3107 | hdev->stat.err_tx++; | |
3108 | ||
3109 | done: | |
3110 | spin_lock(&data->txlock); | |
3111 | data->tx_in_flight--; | |
3112 | spin_unlock(&data->txlock); | |
3113 | ||
3114 | kfree(urb->setup_packet); | |
3115 | ||
3116 | kfree_skb(skb); | |
3117 | } | |
3118 | ||
3119 | static void btusb_isoc_tx_complete(struct urb *urb) | |
3120 | { | |
3121 | struct sk_buff *skb = urb->context; | |
3122 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
3123 | ||
3124 | RTKBT_DBG("%s: urb %p status %d count %d", | |
3125 | __func__, urb, urb->status, urb->actual_length); | |
3126 | ||
3127 | if (skb && hdev) { | |
3128 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
3129 | goto done; | |
3130 | ||
3131 | if (!urb->status) | |
3132 | hdev->stat.byte_tx += urb->transfer_buffer_length; | |
3133 | else | |
3134 | hdev->stat.err_tx++; | |
3135 | } else | |
3136 | RTKBT_ERR("%s: skb 0x%p hdev 0x%p", __func__, skb, hdev); | |
3137 | ||
3138 | done: | |
3139 | kfree(urb->setup_packet); | |
3140 | ||
3141 | kfree_skb(skb); | |
3142 | } | |
3143 | ||
3144 | static int btusb_open(struct hci_dev *hdev) | |
3145 | { | |
3146 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3147 | int err = 0; | |
3148 | ||
f7839fb1 JA |
3149 | // RTKBT_INFO("%s: Start, PM usage count %d", __func__, |
3150 | // atomic_read(&(data->intf->pm_usage_cnt))); | |
b2ef244e JA |
3151 | |
3152 | err = usb_autopm_get_interface(data->intf); | |
3153 | if (err < 0) | |
3154 | return err; | |
3155 | ||
3156 | data->intf->needs_remote_wakeup = 1; | |
3157 | ||
3158 | if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) | |
3159 | goto done; | |
3160 | ||
3161 | if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags)) | |
3162 | goto done; | |
3163 | ||
3164 | err = btusb_submit_intr_urb(hdev, GFP_KERNEL); | |
3165 | if (err < 0) | |
3166 | goto failed; | |
3167 | ||
3168 | err = btusb_submit_bulk_urb(hdev, GFP_KERNEL); | |
3169 | if (err < 0) { | |
3170 | mdelay(URB_CANCELING_DELAY_MS); | |
3171 | usb_kill_anchored_urbs(&data->intr_anchor); | |
3172 | goto failed; | |
3173 | } | |
3174 | ||
3175 | set_bit(BTUSB_BULK_RUNNING, &data->flags); | |
3176 | btusb_submit_bulk_urb(hdev, GFP_KERNEL); | |
3177 | ||
3178 | done: | |
3179 | usb_autopm_put_interface(data->intf); | |
f7839fb1 JA |
3180 | // RTKBT_INFO("%s: End, PM usage count %d", __func__, |
3181 | // atomic_read(&(data->intf->pm_usage_cnt))); | |
b2ef244e JA |
3182 | return 0; |
3183 | ||
3184 | failed: | |
3185 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); | |
3186 | clear_bit(HCI_RUNNING, &hdev->flags); | |
3187 | usb_autopm_put_interface(data->intf); | |
f7839fb1 JA |
3188 | // RTKBT_ERR("%s: Failed, PM usage count %d", __func__, |
3189 | // atomic_read(&(data->intf->pm_usage_cnt))); | |
b2ef244e JA |
3190 | return err; |
3191 | } | |
3192 | ||
3193 | static void btusb_stop_traffic(struct btusb_data *data) | |
3194 | { | |
3195 | mdelay(URB_CANCELING_DELAY_MS); | |
3196 | usb_kill_anchored_urbs(&data->intr_anchor); | |
3197 | usb_kill_anchored_urbs(&data->bulk_anchor); | |
3198 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
3199 | } | |
3200 | ||
3201 | static int btusb_close(struct hci_dev *hdev) | |
3202 | { | |
3203 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3204 | int i, err; | |
3205 | ||
3206 | RTKBT_INFO("%s: hci running %lu", __func__, hdev->flags & HCI_RUNNING); | |
3207 | ||
3208 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) | |
3209 | return 0; | |
3210 | ||
3211 | for (i = 0; i < NUM_REASSEMBLY; i++) { | |
3212 | if (hdev->reassembly[i]) { | |
3213 | RTKBT_DBG("%s: free ressembly[%d]", __func__, i); | |
3214 | kfree_skb(hdev->reassembly[i]); | |
3215 | hdev->reassembly[i] = NULL; | |
3216 | } | |
3217 | } | |
3218 | ||
3219 | cancel_work_sync(&data->work); | |
3220 | cancel_work_sync(&data->waker); | |
3221 | ||
3222 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
3223 | clear_bit(BTUSB_BULK_RUNNING, &data->flags); | |
3224 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); | |
3225 | ||
3226 | btusb_stop_traffic(data); | |
3227 | err = usb_autopm_get_interface(data->intf); | |
3228 | if (err < 0) | |
3229 | goto failed; | |
3230 | ||
3231 | data->intf->needs_remote_wakeup = 0; | |
3232 | usb_autopm_put_interface(data->intf); | |
3233 | ||
3234 | failed: | |
3235 | mdelay(URB_CANCELING_DELAY_MS); | |
3236 | usb_scuttle_anchored_urbs(&data->deferred); | |
3237 | return 0; | |
3238 | } | |
3239 | ||
3240 | static int btusb_flush(struct hci_dev *hdev) | |
3241 | { | |
3242 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3243 | ||
3244 | RTKBT_DBG("%s", __func__); | |
3245 | ||
3246 | mdelay(URB_CANCELING_DELAY_MS); | |
3247 | usb_kill_anchored_urbs(&data->tx_anchor); | |
3248 | ||
3249 | return 0; | |
3250 | } | |
3251 | ||
3252 | #ifdef CONFIG_SCO_OVER_HCI | |
3253 | static void btusb_isoc_snd_tx_complete(struct urb *urb); | |
3254 | ||
3255 | static int snd_send_sco_frame(struct sk_buff *skb) | |
3256 | { | |
3257 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
3258 | ||
3259 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3260 | //struct usb_ctrlrequest *dr; | |
3261 | struct urb *urb; | |
3262 | unsigned int pipe; | |
3263 | int err; | |
3264 | ||
3265 | RTKBT_DBG("%s:pkt type %d, packet_len : %d", | |
3266 | __func__,bt_cb(skb)->pkt_type, skb->len); | |
3267 | ||
3268 | if (!hdev && !test_bit(HCI_RUNNING, &hdev->flags)) | |
3269 | return -EBUSY; | |
3270 | ||
3271 | if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1) { | |
3272 | kfree(skb); | |
3273 | return -ENODEV; | |
3274 | } | |
3275 | ||
3276 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC); | |
3277 | if (!urb) { | |
3278 | RTKBT_ERR("%s: Failed to allocate mem for sco pkts", __func__); | |
3279 | kfree(skb); | |
3280 | return -ENOMEM; | |
3281 | } | |
3282 | ||
3283 | pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress); | |
3284 | ||
3285 | usb_fill_int_urb(urb, data->udev, pipe, | |
3286 | skb->data, skb->len, btusb_isoc_snd_tx_complete, | |
3287 | skb, data->isoc_tx_ep->bInterval); | |
3288 | ||
3289 | urb->transfer_flags = URB_ISO_ASAP; | |
3290 | ||
3291 | fill_isoc_descriptor(urb, skb->len, | |
3292 | le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); | |
3293 | ||
3294 | hdev->stat.sco_tx++; | |
3295 | ||
3296 | usb_anchor_urb(urb, &data->tx_anchor); | |
3297 | ||
3298 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
3299 | if (err < 0) { | |
3300 | RTKBT_ERR("%s: Failed to submit urb %p, pkt type %d, err %d", | |
3301 | __func__, urb, bt_cb(skb)->pkt_type, err); | |
3302 | kfree(urb->setup_packet); | |
3303 | usb_unanchor_urb(urb); | |
3304 | } else | |
3305 | usb_mark_last_busy(data->udev); | |
3306 | usb_free_urb(urb); | |
3307 | ||
3308 | return err; | |
3309 | ||
3310 | } | |
3311 | ||
3312 | static bool snd_copy_send_sco_data( RTK_sco_card_t *pSCOSnd) | |
3313 | { | |
3314 | struct snd_pcm_runtime *runtime = pSCOSnd->playback.substream->runtime; | |
3315 | unsigned int frame_bytes = 2, frames1; | |
3316 | const u8 *source; | |
3317 | ||
3318 | snd_pcm_uframes_t period_size = runtime->period_size; | |
3319 | int i, count; | |
3320 | u8 buffer[period_size * 3]; | |
3321 | int sco_packet_bytes = pSCOSnd->playback.sco_packet_bytes; | |
3322 | struct sk_buff *skb; | |
3323 | ||
3324 | count = frames_to_bytes(runtime, period_size)/sco_packet_bytes; | |
3325 | skb = bt_skb_alloc(((sco_packet_bytes + HCI_SCO_HDR_SIZE) * count), GFP_ATOMIC); | |
3326 | skb->dev = (void *)hci_dev_get(0); | |
3327 | bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; | |
3328 | skb_put(skb, ((sco_packet_bytes + HCI_SCO_HDR_SIZE) * count)); | |
3329 | if(!skb) | |
3330 | return false; | |
3331 | ||
3332 | RTKBT_DBG("%s, buffer_pos: %d", __FUNCTION__, pSCOSnd->playback.buffer_pos); | |
3333 | ||
3334 | source = runtime->dma_area + pSCOSnd->playback.buffer_pos * frame_bytes; | |
3335 | ||
3336 | if (pSCOSnd->playback.buffer_pos + period_size <= runtime->buffer_size) { | |
3337 | memcpy(buffer, source, period_size * frame_bytes); | |
3338 | } else { | |
3339 | /* wrap around at end of ring buffer */ | |
3340 | frames1 = runtime->buffer_size - pSCOSnd->playback.buffer_pos; | |
3341 | memcpy(buffer, source, frames1 * frame_bytes); | |
3342 | memcpy(&buffer[frames1 * frame_bytes], | |
3343 | runtime->dma_area, (period_size - frames1) * frame_bytes); | |
3344 | } | |
3345 | ||
3346 | pSCOSnd->playback.buffer_pos += period_size; | |
3347 | if ( pSCOSnd->playback.buffer_pos >= runtime->buffer_size) | |
3348 | pSCOSnd->playback.buffer_pos -= runtime->buffer_size; | |
3349 | ||
3350 | for(i = 0; i < count; i++) { | |
3351 | *((__u16 *)(skb->data + i * (sco_packet_bytes + HCI_SCO_HDR_SIZE))) = pSCOSnd->usb_data->sco_handle; | |
3352 | *((__u8 *)(skb->data + i*(sco_packet_bytes + HCI_SCO_HDR_SIZE) + 2)) = sco_packet_bytes; | |
3353 | memcpy((skb->data + i * (sco_packet_bytes + HCI_SCO_HDR_SIZE) + HCI_SCO_HDR_SIZE), | |
3354 | &buffer[sco_packet_bytes * i], sco_packet_bytes); | |
3355 | } | |
3356 | ||
3357 | if(test_bit(ALSA_PLAYBACK_RUNNING, &pSCOSnd->states)) { | |
3358 | snd_pcm_period_elapsed(pSCOSnd->playback.substream); | |
3359 | } | |
3360 | snd_send_sco_frame(skb); | |
3361 | return true; | |
3362 | } | |
3363 | ||
3364 | static void btusb_isoc_snd_tx_complete(struct urb *urb) | |
3365 | { | |
3366 | struct sk_buff *skb = urb->context; | |
3367 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
3368 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3369 | RTK_sco_card_t *pSCOSnd = data->pSCOSnd; | |
3370 | ||
3371 | RTKBT_DBG("%s: status %d count %d", | |
3372 | __func__,urb->status, urb->actual_length); | |
3373 | ||
3374 | if (skb && hdev) { | |
3375 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
3376 | goto done; | |
3377 | ||
3378 | if (!urb->status) | |
3379 | hdev->stat.byte_tx += urb->transfer_buffer_length; | |
3380 | else | |
3381 | hdev->stat.err_tx++; | |
3382 | } else | |
3383 | RTKBT_ERR("%s: skb 0x%p hdev 0x%p", __func__, skb, hdev); | |
3384 | ||
3385 | done: | |
3386 | kfree(urb->setup_packet); | |
3387 | kfree_skb(skb); | |
3388 | if(test_bit(ALSA_PLAYBACK_RUNNING, &pSCOSnd->states)){ | |
3389 | snd_copy_send_sco_data(pSCOSnd); | |
3390 | //schedule_work(&pSCOSnd->send_sco_work); | |
3391 | } | |
3392 | } | |
3393 | ||
3394 | static void playback_work(struct work_struct *work) | |
3395 | { | |
3396 | RTK_sco_card_t *pSCOSnd = container_of(work, RTK_sco_card_t, send_sco_work); | |
3397 | ||
3398 | snd_copy_send_sco_data(pSCOSnd); | |
3399 | } | |
3400 | ||
3401 | #endif | |
3402 | ||
3403 | static int btusb_send_frame(struct sk_buff *skb) | |
3404 | { | |
3405 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; | |
3406 | ||
3407 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3408 | struct usb_ctrlrequest *dr; | |
3409 | struct urb *urb; | |
3410 | unsigned int pipe; | |
3411 | int err; | |
3412 | int retries = 0; | |
3413 | ||
3414 | RTKBT_DBG("%s: hdev %p, btusb data %p, pkt type %d", | |
3415 | __func__, hdev, data, bt_cb(skb)->pkt_type); | |
3416 | ||
3417 | if (!test_bit(HCI_RUNNING, &hdev->flags)) | |
3418 | return -EBUSY; | |
3419 | ||
3420 | ||
3421 | ||
3422 | switch (bt_cb(skb)->pkt_type) { | |
3423 | case HCI_COMMAND_PKT: | |
3424 | print_command(skb); | |
3425 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
3426 | if (!urb) | |
3427 | return -ENOMEM; | |
3428 | ||
3429 | dr = kmalloc(sizeof(*dr), GFP_ATOMIC); | |
3430 | if (!dr) { | |
3431 | usb_free_urb(urb); | |
3432 | return -ENOMEM; | |
3433 | } | |
3434 | ||
3435 | dr->bRequestType = data->cmdreq_type; | |
3436 | dr->bRequest = 0; | |
3437 | dr->wIndex = 0; | |
3438 | dr->wValue = 0; | |
3439 | dr->wLength = __cpu_to_le16(skb->len); | |
3440 | ||
3441 | pipe = usb_sndctrlpipe(data->udev, 0x00); | |
3442 | ||
3443 | usb_fill_control_urb(urb, data->udev, pipe, (void *) dr, | |
3444 | skb->data, skb->len, btusb_tx_complete, skb); | |
3445 | ||
3446 | hdev->stat.cmd_tx++; | |
3447 | break; | |
3448 | ||
3449 | case HCI_ACLDATA_PKT: | |
3450 | print_acl(skb, 1); | |
3451 | if (!data->bulk_tx_ep) | |
3452 | return -ENODEV; | |
3453 | ||
3454 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
3455 | if (!urb) | |
3456 | return -ENOMEM; | |
3457 | ||
3458 | pipe = usb_sndbulkpipe(data->udev, | |
3459 | data->bulk_tx_ep->bEndpointAddress); | |
3460 | ||
3461 | usb_fill_bulk_urb(urb, data->udev, pipe, | |
3462 | skb->data, skb->len, btusb_tx_complete, skb); | |
3463 | ||
3464 | hdev->stat.acl_tx++; | |
3465 | break; | |
3466 | ||
3467 | case HCI_SCODATA_PKT: | |
3468 | print_sco(skb, 1); | |
3469 | if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1) { | |
3470 | kfree(skb); | |
3471 | return -ENODEV; | |
3472 | } | |
3473 | ||
3474 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC); | |
3475 | if (!urb) { | |
3476 | RTKBT_ERR("%s: Failed to allocate mem for sco pkts", __func__); | |
3477 | kfree(skb); | |
3478 | return -ENOMEM; | |
3479 | } | |
3480 | ||
3481 | pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress); | |
3482 | ||
3483 | usb_fill_int_urb(urb, data->udev, pipe, | |
3484 | skb->data, skb->len, btusb_isoc_tx_complete, | |
3485 | skb, data->isoc_tx_ep->bInterval); | |
3486 | ||
3487 | urb->transfer_flags = URB_ISO_ASAP; | |
3488 | ||
3489 | fill_isoc_descriptor(urb, skb->len, | |
3490 | le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); | |
3491 | ||
3492 | hdev->stat.sco_tx++; | |
3493 | goto skip_waking; | |
3494 | ||
3495 | default: | |
3496 | return -EILSEQ; | |
3497 | } | |
3498 | ||
3499 | err = inc_tx(data); | |
3500 | if (err) { | |
3501 | usb_anchor_urb(urb, &data->deferred); | |
3502 | schedule_work(&data->waker); | |
3503 | err = 0; | |
3504 | goto done; | |
3505 | } | |
3506 | ||
3507 | skip_waking: | |
3508 | usb_anchor_urb(urb, &data->tx_anchor); | |
3509 | retry: | |
3510 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
3511 | if (err < 0) { | |
3512 | RTKBT_ERR("%s: Failed to submit urb %p, pkt type %d, err %d, retries %d", | |
3513 | __func__, urb, bt_cb(skb)->pkt_type, err, retries); | |
3514 | if ((bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) && (retries < 10)) { | |
3515 | mdelay(1); | |
3516 | ||
3517 | if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) | |
3518 | print_error_command(skb); | |
3519 | retries++; | |
3520 | goto retry; | |
3521 | } | |
3522 | kfree(urb->setup_packet); | |
3523 | usb_unanchor_urb(urb); | |
3524 | } else | |
3525 | usb_mark_last_busy(data->udev); | |
3526 | usb_free_urb(urb); | |
3527 | ||
3528 | done: | |
3529 | return err; | |
3530 | } | |
3531 | ||
3532 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0) | |
3533 | static void btusb_destruct(struct hci_dev *hdev) | |
3534 | { | |
3535 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3536 | ||
3537 | RTKBT_DBG("%s: name %s", __func__, hdev->name); | |
3538 | ||
3539 | kfree(data); | |
3540 | } | |
3541 | #endif | |
3542 | ||
3543 | static void btusb_notify(struct hci_dev *hdev, unsigned int evt) | |
3544 | { | |
3545 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3546 | ||
3547 | RTKBT_DBG("%s: name %s, evt %d", __func__, hdev->name, evt); | |
3548 | ||
3549 | RTKBT_INFO("%s: hdev->conn_hash.sco_num= %d, data->sco_num = %d", __func__, hdev->conn_hash.sco_num, | |
3550 | data->sco_num); | |
3551 | if (hdev->conn_hash.sco_num != data->sco_num) { | |
3552 | data->sco_num = hdev->conn_hash.sco_num; | |
3553 | schedule_work(&data->work); | |
3554 | } | |
3555 | } | |
3556 | ||
3557 | static inline int set_isoc_interface(struct hci_dev *hdev, int altsetting) | |
3558 | { | |
3559 | struct btusb_data *data = GET_DRV_DATA(hdev); | |
3560 | struct usb_interface *intf = data->isoc; | |
3561 | struct usb_endpoint_descriptor *ep_desc; | |
3562 | int i, err; | |
3563 | ||
3564 | if (!data->isoc) | |
3565 | return -ENODEV; | |
3566 | ||
3567 | err = usb_set_interface(data->udev, 1, altsetting); | |
3568 | if (err < 0) { | |
3569 | RTKBT_ERR("%s: Failed to set interface, altsetting %d, err %d", | |
3570 | __func__, altsetting, err); | |
3571 | return err; | |
3572 | } | |
3573 | ||
3574 | data->isoc_altsetting = altsetting; | |
3575 | ||
3576 | data->isoc_tx_ep = NULL; | |
3577 | data->isoc_rx_ep = NULL; | |
3578 | ||
3579 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { | |
3580 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; | |
3581 | ||
3582 | if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) { | |
3583 | data->isoc_tx_ep = ep_desc; | |
3584 | continue; | |
3585 | } | |
3586 | ||
3587 | if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) { | |
3588 | data->isoc_rx_ep = ep_desc; | |
3589 | continue; | |
3590 | } | |
3591 | } | |
3592 | ||
3593 | if (!data->isoc_tx_ep || !data->isoc_rx_ep) { | |
3594 | RTKBT_ERR("%s: Invalid SCO descriptors", __func__); | |
3595 | return -ENODEV; | |
3596 | } | |
3597 | ||
3598 | return 0; | |
3599 | } | |
3600 | ||
3601 | static int check_controller_support_msbc( struct usb_device *udev) | |
3602 | { | |
3603 | //fix this in the future,when new card support msbc decode and encode | |
3604 | RTKBT_INFO("%s:pid = 0x%02x, vid = 0x%02x",__func__,udev->descriptor.idProduct, udev->descriptor.idVendor); | |
3605 | switch (udev->descriptor.idProduct) { | |
3606 | ||
3607 | default: | |
3608 | return 0; | |
3609 | } | |
3610 | return 0; | |
3611 | } | |
3612 | ||
3613 | static void btusb_work(struct work_struct *work) | |
3614 | { | |
3615 | struct btusb_data *data = container_of(work, struct btusb_data, work); | |
3616 | struct hci_dev *hdev = data->hdev; | |
3617 | struct sk_buff *skb; | |
3618 | ||
3619 | int err; | |
3620 | int new_alts; | |
3621 | if (data->sco_num > 0) { | |
3622 | if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) { | |
3623 | err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf); | |
3624 | if (err < 0) { | |
3625 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
3626 | mdelay(URB_CANCELING_DELAY_MS); | |
3627 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
3628 | return; | |
3629 | } | |
3630 | ||
3631 | set_bit(BTUSB_DID_ISO_RESUME, &data->flags); | |
3632 | } | |
3633 | ||
3634 | RTKBT_INFO("%s voice settings = 0x%04x", __func__, hdev->voice_setting); | |
3635 | if (!(hdev->voice_setting & 0x0003)) { | |
3636 | if(data->sco_num == 1) | |
3637 | new_alts = 2; | |
3638 | else { | |
3639 | RTKBT_ERR("%s: we don't support mutiple sco link for cvsd", __func__); | |
3640 | return; | |
3641 | } | |
3642 | } else{ | |
3643 | if(check_controller_support_msbc(data->udev)) { | |
3644 | if(data->sco_num == 1) | |
3645 | new_alts = 4; | |
3646 | else { | |
3647 | RTKBT_ERR("%s: we don't support mutiple sco link for msbc", __func__); | |
3648 | return; | |
3649 | } | |
3650 | } else { | |
3651 | new_alts = 2; | |
3652 | } | |
3653 | } | |
3654 | if (data->isoc_altsetting != new_alts) { | |
3655 | ||
3656 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
3657 | mdelay(URB_CANCELING_DELAY_MS); | |
3658 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
3659 | ||
3660 | if(hdev->reassembly[HCI_SCODATA_PKT - 1]) { | |
3661 | skb = hdev->reassembly[HCI_SCODATA_PKT - 1]; | |
3662 | hdev->reassembly[HCI_SCODATA_PKT - 1] = NULL; | |
3663 | kfree_skb(skb); | |
3664 | } | |
3665 | if (set_isoc_interface(hdev, new_alts) < 0) | |
3666 | return; | |
3667 | } | |
3668 | ||
3669 | if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) { | |
3670 | if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0) | |
3671 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
3672 | else | |
3673 | btusb_submit_isoc_urb(hdev, GFP_KERNEL); | |
3674 | } | |
3675 | #ifdef CONFIG_SCO_OVER_HCI | |
3676 | if(test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { | |
3677 | set_bit(USB_CAPTURE_RUNNING, &data->pSCOSnd->states); | |
3678 | set_bit(USB_PLAYBACK_RUNNING, &data->pSCOSnd->states); | |
3679 | } | |
3680 | #endif | |
3681 | } else { | |
3682 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
3683 | #ifdef CONFIG_SCO_OVER_HCI | |
3684 | clear_bit(USB_CAPTURE_RUNNING, &data->pSCOSnd->states); | |
3685 | clear_bit(USB_PLAYBACK_RUNNING, &data->pSCOSnd->states); | |
3686 | #endif | |
3687 | mdelay(URB_CANCELING_DELAY_MS); | |
3688 | usb_kill_anchored_urbs(&data->isoc_anchor); | |
3689 | ||
3690 | set_isoc_interface(hdev, 0); | |
3691 | ||
3692 | if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags)) | |
3693 | usb_autopm_put_interface(data->isoc ? data->isoc : data->intf); | |
3694 | } | |
3695 | } | |
3696 | ||
3697 | static void btusb_waker(struct work_struct *work) | |
3698 | { | |
3699 | struct btusb_data *data = container_of(work, struct btusb_data, waker); | |
3700 | int err; | |
3701 | ||
f7839fb1 JA |
3702 | // RTKBT_DBG("%s: PM usage count %d", __func__, |
3703 | // atomic_read(&data->intf->pm_usage_cnt)); | |
b2ef244e JA |
3704 | |
3705 | err = usb_autopm_get_interface(data->intf); | |
3706 | if (err < 0) | |
3707 | return; | |
3708 | ||
3709 | usb_autopm_put_interface(data->intf); | |
3710 | } | |
3711 | ||
3712 | ||
3713 | //#ifdef CONFIG_HAS_EARLYSUSPEND | |
3714 | #if 0 | |
3715 | static void btusb_early_suspend(struct early_suspend *h) | |
3716 | { | |
3717 | struct btusb_data *data; | |
3718 | firmware_info *fw_info; | |
3719 | patch_info *patch_entry; | |
3720 | ||
3721 | RTKBT_INFO("%s", __func__); | |
3722 | ||
3723 | data = container_of(h, struct btusb_data, early_suspend); | |
3724 | fw_info = data->fw_info; | |
3725 | patch_entry = fw_info->patch_entry; | |
3726 | ||
3727 | patch_entry->fw_len = load_firmware(fw_info, &patch_entry->fw_cache); | |
3728 | if (patch_entry->fw_len <= 0) { | |
3729 | /* We may encount failure in loading firmware, just give a warning */ | |
3730 | RTKBT_WARN("%s: Failed to load firmware", __func__); | |
3731 | } | |
3732 | } | |
3733 | ||
3734 | static void btusb_late_resume(struct early_suspend *h) | |
3735 | { | |
3736 | struct btusb_data *data; | |
3737 | firmware_info *fw_info; | |
3738 | patch_info *patch_entry; | |
3739 | ||
3740 | RTKBT_INFO("%s", __func__); | |
3741 | ||
3742 | data = container_of(h, struct btusb_data, early_suspend); | |
3743 | fw_info = data->fw_info; | |
3744 | patch_entry = fw_info->patch_entry; | |
3745 | ||
3746 | /* Reclaim fw buffer when bt usb resumed */ | |
3747 | if (patch_entry->fw_len > 0) { | |
3748 | kfree(patch_entry->fw_cache); | |
3749 | patch_entry->fw_cache = NULL; | |
3750 | patch_entry->fw_len = 0; | |
3751 | } | |
3752 | } | |
3753 | #else | |
3754 | int bt_pm_notify(struct notifier_block *notifier, ulong pm_event, void *unused) | |
3755 | { | |
3756 | struct btusb_data *data; | |
3757 | firmware_info *fw_info; | |
3758 | patch_info *patch_entry; | |
3759 | struct usb_device *udev; | |
3760 | ||
3761 | RTKBT_INFO("%s: pm event %ld", __func__, pm_event); | |
3762 | ||
3763 | data = container_of(notifier, struct btusb_data, pm_notifier); | |
3764 | fw_info = data->fw_info; | |
3765 | patch_entry = fw_info->patch_entry; | |
3766 | udev = fw_info->udev; | |
3767 | ||
3768 | switch (pm_event) { | |
3769 | case PM_SUSPEND_PREPARE: | |
3770 | case PM_HIBERNATION_PREPARE: | |
3771 | #if 0 | |
3772 | patch_entry->fw_len = load_firmware(fw_info, &patch_entry->fw_cache); | |
3773 | if (patch_entry->fw_len <= 0) { | |
3774 | /* We may encount failure in loading firmware, just give a warning */ | |
3775 | RTKBT_WARN("%s: Failed to load firmware", __func__); | |
3776 | } | |
3777 | #endif | |
3778 | if (!device_may_wakeup(&udev->dev)) { | |
3779 | #if (CONFIG_RESET_RESUME || CONFIG_BLUEDROID) | |
3780 | RTKBT_INFO("%s:remote wakeup not supported, reset resume supported", __func__); | |
3781 | #else | |
3782 | fw_info->intf->needs_binding = 1; | |
3783 | RTKBT_INFO("%s:remote wakeup not supported, binding needed", __func__); | |
3784 | #endif | |
3785 | } | |
3786 | break; | |
3787 | ||
3788 | case PM_POST_SUSPEND: | |
3789 | case PM_POST_HIBERNATION: | |
3790 | case PM_POST_RESTORE: | |
3791 | #if 0 | |
3792 | /* Reclaim fw buffer when bt usb resumed */ | |
3793 | if (patch_entry->fw_len > 0) { | |
3794 | kfree(patch_entry->fw_cache); | |
3795 | patch_entry->fw_cache = NULL; | |
3796 | patch_entry->fw_len = 0; | |
3797 | } | |
3798 | #endif | |
3799 | ||
3800 | #if BTUSB_RPM | |
3801 | usb_disable_autosuspend(udev); | |
3802 | usb_enable_autosuspend(udev); | |
3803 | pm_runtime_set_autosuspend_delay(&(udev->dev), 2000); | |
3804 | #endif | |
3805 | break; | |
3806 | ||
3807 | default: | |
3808 | break; | |
3809 | } | |
3810 | ||
3811 | return NOTIFY_DONE; | |
3812 | } | |
3813 | ||
3814 | int bt_reboot_notify(struct notifier_block *notifier, ulong pm_event, void *unused) | |
3815 | { | |
3816 | struct btusb_data *data; | |
3817 | firmware_info *fw_info; | |
3818 | patch_info *patch_entry; | |
3819 | struct usb_device *udev; | |
3820 | ||
3821 | RTKBT_INFO("%s: pm event %ld", __func__, pm_event); | |
3822 | ||
3823 | data = container_of(notifier, struct btusb_data, reboot_notifier); | |
3824 | fw_info = data->fw_info; | |
3825 | patch_entry = fw_info->patch_entry; | |
3826 | udev = fw_info->udev; | |
3827 | ||
3828 | switch (pm_event) { | |
3829 | case SYS_DOWN: | |
3830 | RTKBT_DBG("%s:system down or restart", __func__); | |
3831 | break; | |
3832 | ||
3833 | case SYS_HALT: | |
3834 | case SYS_POWER_OFF: | |
3835 | #if SUSPNED_DW_FW | |
3836 | cancel_work_sync(&data->work); | |
3837 | ||
3838 | btusb_stop_traffic(data); | |
3839 | mdelay(URB_CANCELING_DELAY_MS); | |
3840 | usb_kill_anchored_urbs(&data->tx_anchor); | |
3841 | ||
3842 | ||
3843 | if(fw_info_4_suspend) { | |
3844 | download_suspend_patch(fw_info_4_suspend,1); | |
3845 | } | |
3846 | else | |
3847 | RTKBT_ERR("%s: Failed to download suspend fw", __func__); | |
3848 | #endif | |
3849 | ||
3850 | #if SET_WAKEUP_DEVICE | |
3851 | set_wakeup_device_from_conf(fw_info_4_suspend); | |
3852 | #endif | |
3853 | RTKBT_DBG("%s:system halt or power off", __func__); | |
3854 | break; | |
3855 | ||
3856 | default: | |
3857 | break; | |
3858 | } | |
3859 | ||
3860 | return NOTIFY_DONE; | |
3861 | } | |
3862 | ||
3863 | #endif | |
3864 | ||
3865 | #ifdef CONFIG_SCO_OVER_HCI | |
3866 | static const struct snd_pcm_hardware snd_card_sco_capture_default = | |
3867 | { | |
3868 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_NONINTERLEAVED | | |
3869 | SNDRV_PCM_ACCESS_RW_INTERLEAVED | SNDRV_PCM_INFO_FIFO_IN_FRAMES), | |
3870 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8, | |
3871 | .rates = (SNDRV_PCM_RATE_8000), | |
3872 | .rate_min = 8000, | |
3873 | .rate_max = 8000, | |
3874 | .channels_min = 1, | |
3875 | .channels_max = 1, | |
3876 | .buffer_bytes_max = 8 * 768, | |
3877 | .period_bytes_min = 48, | |
3878 | .period_bytes_max = 768, | |
3879 | .periods_min = 1, | |
3880 | .periods_max = 8, | |
3881 | .fifo_size = 8, | |
3882 | ||
3883 | }; | |
3884 | ||
3885 | static int snd_sco_capture_pcm_open(struct snd_pcm_substream * substream) | |
3886 | { | |
3887 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
3888 | ||
3889 | RTKBT_INFO("%s", __FUNCTION__); | |
3890 | pSCOSnd->capture.substream = substream; | |
3891 | ||
3892 | memcpy(&substream->runtime->hw, &snd_card_sco_capture_default, sizeof(struct snd_pcm_hardware)); | |
3893 | ||
3894 | if(check_controller_support_msbc(pSCOSnd->dev)) { | |
3895 | substream->runtime->hw.rates |= SNDRV_PCM_RATE_16000; | |
3896 | substream->runtime->hw.rate_max = 16000; | |
3897 | substream->runtime->hw.period_bytes_min = 96; | |
3898 | substream->runtime->hw.period_bytes_max = 16 * 96; | |
3899 | substream->runtime->hw.buffer_bytes_max = 8 * 16 * 96; | |
3900 | } | |
3901 | set_bit(ALSA_CAPTURE_OPEN, &pSCOSnd->states); | |
3902 | return 0; | |
3903 | } | |
3904 | ||
3905 | static int snd_sco_capture_pcm_close(struct snd_pcm_substream *substream) | |
3906 | { | |
3907 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
3908 | ||
3909 | clear_bit(ALSA_CAPTURE_OPEN, &pSCOSnd->states); | |
3910 | return 0; | |
3911 | } | |
3912 | ||
3913 | static int snd_sco_capture_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg) | |
3914 | { | |
3915 | RTKBT_DBG("%s, cmd = %d", __FUNCTION__, cmd); | |
3916 | switch (cmd) | |
3917 | { | |
3918 | default: | |
3919 | return snd_pcm_lib_ioctl(substream, cmd, arg); | |
3920 | } | |
3921 | return 0; | |
3922 | } | |
3923 | ||
3924 | static int snd_sco_capture_pcm_hw_params(struct snd_pcm_substream * substream, struct snd_pcm_hw_params * hw_params) | |
3925 | { | |
3926 | ||
3927 | int err; | |
3928 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3929 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, params_buffer_bytes(hw_params)); | |
3930 | RTKBT_INFO("%s,err : %d, runtime state : %d", __FUNCTION__, err, runtime->status->state); | |
3931 | return err; | |
3932 | } | |
3933 | ||
3934 | static int snd_sco_capture_pcm_hw_free(struct snd_pcm_substream * substream) | |
3935 | { | |
3936 | RTKBT_DBG("%s", __FUNCTION__); | |
3937 | return snd_pcm_lib_free_vmalloc_buffer(substream);; | |
3938 | } | |
3939 | ||
3940 | static int snd_sco_capture_pcm_prepare(struct snd_pcm_substream *substream) | |
3941 | { | |
3942 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
3943 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3944 | ||
3945 | RTKBT_INFO("%s", __FUNCTION__); | |
3946 | if (test_bit(DISCONNECTED, &pSCOSnd->states)) | |
3947 | return -ENODEV; | |
3948 | if (!test_bit(USB_CAPTURE_RUNNING, &pSCOSnd->states)) | |
3949 | return -EIO; | |
3950 | ||
3951 | if(runtime->rate == 8000) { | |
3952 | if(pSCOSnd->usb_data->isoc_altsetting != 2) | |
3953 | return -ENOEXEC; | |
3954 | pSCOSnd->capture.sco_packet_bytes = 48; | |
3955 | } | |
3956 | else if(runtime->rate == 16000 && check_controller_support_msbc(pSCOSnd->dev)) { | |
3957 | if(pSCOSnd->usb_data->isoc_altsetting != 4) | |
3958 | return -ENOEXEC; | |
3959 | pSCOSnd->capture.sco_packet_bytes = 96; | |
3960 | } | |
3961 | else if(pSCOSnd->usb_data->isoc_altsetting == 2) { | |
3962 | pSCOSnd->capture.sco_packet_bytes = 48; | |
3963 | } | |
3964 | else if(pSCOSnd->usb_data->isoc_altsetting == 1) { | |
3965 | pSCOSnd->capture.sco_packet_bytes = 24; | |
3966 | } | |
3967 | return 0; | |
3968 | } | |
3969 | ||
3970 | static int snd_sco_capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
3971 | { | |
3972 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
3973 | RTKBT_INFO("%s, cmd : %d", __FUNCTION__, cmd); | |
3974 | ||
3975 | switch (cmd) { | |
3976 | case SNDRV_PCM_TRIGGER_START: | |
3977 | if (!test_bit(USB_CAPTURE_RUNNING, &pSCOSnd->states)) | |
3978 | return -EIO; | |
3979 | set_bit(ALSA_CAPTURE_RUNNING, &pSCOSnd->states); | |
3980 | return 0; | |
3981 | case SNDRV_PCM_TRIGGER_STOP: | |
3982 | clear_bit(ALSA_CAPTURE_RUNNING, &pSCOSnd->states); | |
3983 | return 0; | |
3984 | default: | |
3985 | return -EINVAL; | |
3986 | } | |
3987 | } | |
3988 | ||
3989 | static snd_pcm_uframes_t snd_sco_capture_pcm_pointer(struct snd_pcm_substream *substream) | |
3990 | { | |
3991 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
3992 | ||
3993 | return pSCOSnd->capture.buffer_pos; | |
3994 | } | |
3995 | ||
3996 | ||
3997 | static struct snd_pcm_ops snd_sco_capture_pcm_ops = { | |
3998 | .open = snd_sco_capture_pcm_open, | |
3999 | .close = snd_sco_capture_pcm_close, | |
4000 | .ioctl = snd_sco_capture_ioctl, | |
4001 | .hw_params = snd_sco_capture_pcm_hw_params, | |
4002 | .hw_free = snd_sco_capture_pcm_hw_free, | |
4003 | .prepare = snd_sco_capture_pcm_prepare, | |
4004 | .trigger = snd_sco_capture_pcm_trigger, | |
4005 | .pointer = snd_sco_capture_pcm_pointer, | |
4006 | }; | |
4007 | ||
4008 | ||
4009 | static const struct snd_pcm_hardware snd_card_sco_playback_default = | |
4010 | { | |
4011 | .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_NONINTERLEAVED | | |
4012 | SNDRV_PCM_ACCESS_RW_INTERLEAVED | SNDRV_PCM_INFO_FIFO_IN_FRAMES), | |
4013 | .formats = SNDRV_PCM_FMTBIT_S16_LE, | |
4014 | .rates = (SNDRV_PCM_RATE_8000), | |
4015 | .rate_min = 8000, | |
4016 | .rate_max = 8000, | |
4017 | .channels_min = 1, | |
4018 | .channels_max = 1, | |
4019 | .buffer_bytes_max = 8 * 768, | |
4020 | .period_bytes_min = 48, | |
4021 | .period_bytes_max = 768, | |
4022 | .periods_min = 1, | |
4023 | .periods_max = 8, | |
4024 | .fifo_size = 8, | |
4025 | }; | |
4026 | ||
4027 | static int snd_sco_playback_pcm_open(struct snd_pcm_substream * substream) | |
4028 | { | |
4029 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
4030 | int err = 0; | |
4031 | ||
4032 | RTKBT_INFO("%s, rate : %d", __FUNCTION__, substream->runtime->rate); | |
4033 | memcpy(&substream->runtime->hw, &snd_card_sco_playback_default, sizeof(struct snd_pcm_hardware)); | |
4034 | if(check_controller_support_msbc(pSCOSnd->dev)) { | |
4035 | substream->runtime->hw.rates |= SNDRV_PCM_RATE_16000; | |
4036 | substream->runtime->hw.rate_max = 16000; | |
4037 | substream->runtime->hw.period_bytes_min = 96; | |
4038 | substream->runtime->hw.period_bytes_max = 16 * 96; | |
4039 | substream->runtime->hw.buffer_bytes_max = 8 * 16 * 96; | |
4040 | } | |
4041 | pSCOSnd->playback.substream = substream; | |
4042 | set_bit(ALSA_PLAYBACK_OPEN, &pSCOSnd->states); | |
4043 | ||
4044 | return err; | |
4045 | } | |
4046 | ||
4047 | static int snd_sco_playback_pcm_close(struct snd_pcm_substream *substream) | |
4048 | { | |
4049 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
4050 | ||
4051 | clear_bit(ALSA_PLAYBACK_OPEN, &pSCOSnd->states); | |
4052 | cancel_work_sync(&pSCOSnd->send_sco_work); | |
4053 | return 0; | |
4054 | } | |
4055 | ||
4056 | static int snd_sco_playback_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg) | |
4057 | { | |
4058 | RTKBT_DBG("%s, cmd : %d", __FUNCTION__, cmd); | |
4059 | switch (cmd) | |
4060 | { | |
4061 | default: | |
4062 | return snd_pcm_lib_ioctl(substream, cmd, arg); | |
4063 | break; | |
4064 | } | |
4065 | return 0; | |
4066 | } | |
4067 | ||
4068 | static int snd_sco_playback_pcm_hw_params(struct snd_pcm_substream * substream, struct snd_pcm_hw_params * hw_params) | |
4069 | { | |
4070 | int err; | |
4071 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, params_buffer_bytes(hw_params)); | |
4072 | return err; | |
4073 | } | |
4074 | ||
4075 | static int snd_sco_palyback_pcm_hw_free(struct snd_pcm_substream * substream) | |
4076 | { | |
4077 | RTKBT_DBG("%s", __FUNCTION__); | |
4078 | return snd_pcm_lib_free_vmalloc_buffer(substream); | |
4079 | } | |
4080 | ||
4081 | static int snd_sco_playback_pcm_prepare(struct snd_pcm_substream *substream) | |
4082 | { | |
4083 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
4084 | struct snd_pcm_runtime *runtime = substream->runtime; | |
4085 | ||
4086 | RTKBT_INFO("%s, bound_rate = %d", __FUNCTION__, runtime->rate); | |
4087 | ||
4088 | if (test_bit(DISCONNECTED, &pSCOSnd->states)) | |
4089 | return -ENODEV; | |
4090 | if (!test_bit(USB_PLAYBACK_RUNNING, &pSCOSnd->states)) | |
4091 | return -EIO; | |
4092 | ||
4093 | if(runtime->rate == 8000) { | |
4094 | if(pSCOSnd->usb_data->isoc_altsetting != 2) | |
4095 | return -ENOEXEC; | |
4096 | pSCOSnd->playback.sco_packet_bytes = 48; | |
4097 | } | |
4098 | else if(runtime->rate == 16000) { | |
4099 | if(pSCOSnd->usb_data->isoc_altsetting != 4) | |
4100 | return -ENOEXEC; | |
4101 | pSCOSnd->playback.sco_packet_bytes = 96; | |
4102 | } | |
4103 | ||
4104 | return 0; | |
4105 | } | |
4106 | ||
4107 | static int snd_sco_playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
4108 | { | |
4109 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
4110 | ||
4111 | RTKBT_INFO("%s, cmd = %d", __FUNCTION__, cmd); | |
4112 | switch (cmd) { | |
4113 | case SNDRV_PCM_TRIGGER_START: | |
4114 | if (!test_bit(USB_PLAYBACK_RUNNING, &pSCOSnd->states)) | |
4115 | return -EIO; | |
4116 | set_bit(ALSA_PLAYBACK_RUNNING, &pSCOSnd->states); | |
4117 | schedule_work(&pSCOSnd->send_sco_work); | |
4118 | return 0; | |
4119 | case SNDRV_PCM_TRIGGER_STOP: | |
4120 | clear_bit(ALSA_PLAYBACK_RUNNING, &pSCOSnd->states); | |
4121 | return 0; | |
4122 | default: | |
4123 | return -EINVAL; | |
4124 | } | |
4125 | } | |
4126 | ||
4127 | static snd_pcm_uframes_t snd_sco_playback_pcm_pointer(struct snd_pcm_substream *substream) | |
4128 | { | |
4129 | RTK_sco_card_t *pSCOSnd = substream->private_data; | |
4130 | ||
4131 | return pSCOSnd->playback.buffer_pos; | |
4132 | } | |
4133 | ||
4134 | ||
4135 | static struct snd_pcm_ops snd_sco_playback_pcm_ops = { | |
4136 | .open = snd_sco_playback_pcm_open, | |
4137 | .close = snd_sco_playback_pcm_close, | |
4138 | .ioctl = snd_sco_playback_ioctl, | |
4139 | .hw_params = snd_sco_playback_pcm_hw_params, | |
4140 | .hw_free = snd_sco_palyback_pcm_hw_free, | |
4141 | .prepare = snd_sco_playback_pcm_prepare, | |
4142 | .trigger = snd_sco_playback_pcm_trigger, | |
4143 | .pointer = snd_sco_playback_pcm_pointer, | |
4144 | }; | |
4145 | ||
4146 | ||
4147 | static RTK_sco_card_t* btusb_snd_init(struct usb_interface *intf, const struct usb_device_id *id, struct btusb_data *data) | |
4148 | { | |
4149 | struct snd_card *card; | |
4150 | RTK_sco_card_t *pSCOSnd; | |
4151 | int err=0; | |
4152 | RTKBT_INFO("%s", __func__); | |
4153 | err = snd_card_new(&intf->dev, | |
4154 | -1, RTK_SCO_ID, THIS_MODULE, | |
4155 | sizeof(RTK_sco_card_t), &card); | |
4156 | if (err < 0) { | |
4157 | RTKBT_ERR("%s: sco snd card create fail", __func__); | |
4158 | return NULL; | |
4159 | } | |
4160 | // private data | |
4161 | pSCOSnd = (RTK_sco_card_t *)card->private_data; | |
4162 | pSCOSnd->card = card; | |
4163 | pSCOSnd->dev = interface_to_usbdev(intf); | |
4164 | pSCOSnd->usb_data = data; | |
4165 | ||
4166 | strcpy(card->driver, RTK_SCO_ID); | |
4167 | strcpy(card->shortname, "Realtek sco snd"); | |
4168 | sprintf(card->longname, "Realtek sco over hci: VID:0x%04x, PID:0x%04x", | |
4169 | id->idVendor, pSCOSnd->dev->descriptor.idProduct); | |
4170 | ||
4171 | err = snd_pcm_new(card, RTK_SCO_ID, 0, 1, 1, &pSCOSnd->pcm); | |
4172 | if (err < 0) { | |
4173 | RTKBT_ERR("%s: sco snd card new pcm fail", __func__); | |
4174 | return NULL; | |
4175 | } | |
4176 | pSCOSnd->pcm->private_data = pSCOSnd; | |
4177 | sprintf(pSCOSnd->pcm->name, "sco_pcm:VID:0x%04x, PID:0x%04x", | |
4178 | id->idVendor, pSCOSnd->dev->descriptor.idProduct); | |
4179 | ||
4180 | snd_pcm_set_ops(pSCOSnd->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sco_playback_pcm_ops); | |
4181 | snd_pcm_set_ops(pSCOSnd->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sco_capture_pcm_ops); | |
4182 | ||
4183 | err = snd_card_register(card); | |
4184 | if (err < 0) { | |
4185 | RTKBT_ERR("%s: sco snd card register card fail", __func__); | |
4186 | return NULL; | |
4187 | } | |
4188 | ||
4189 | spin_lock_init(&pSCOSnd->capture_lock); | |
4190 | spin_lock_init(&pSCOSnd->playback_lock); | |
4191 | INIT_WORK(&pSCOSnd->send_sco_work, playback_work); | |
4192 | return pSCOSnd; | |
4193 | } | |
4194 | #endif | |
4195 | ||
4196 | static int btusb_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
4197 | { | |
4198 | struct usb_device *udev = interface_to_usbdev(intf); | |
4199 | struct usb_endpoint_descriptor *ep_desc; | |
4200 | struct btusb_data *data; | |
4201 | struct hci_dev *hdev; | |
4202 | firmware_info *fw_info; | |
4203 | int i, err=0; | |
4204 | ||
4205 | RTKBT_INFO("%s: usb_interface %p, bInterfaceNumber %d, idVendor 0x%04x, " | |
4206 | "idProduct 0x%04x", __func__, intf, | |
4207 | intf->cur_altsetting->desc.bInterfaceNumber, | |
4208 | id->idVendor, id->idProduct); | |
4209 | ||
4210 | /* interface numbers are hardcoded in the spec */ | |
4211 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) | |
4212 | return -ENODEV; | |
4213 | ||
4214 | RTKBT_DBG("%s: can wakeup = %x, may wakeup = %x", __func__, | |
4215 | device_can_wakeup(&udev->dev), device_may_wakeup(&udev->dev)); | |
4216 | ||
4217 | data = rtk_alloc(intf); | |
4218 | if (!data) | |
4219 | return -ENOMEM; | |
4220 | ||
4221 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { | |
4222 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; | |
4223 | ||
4224 | if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) { | |
4225 | data->intr_ep = ep_desc; | |
4226 | continue; | |
4227 | } | |
4228 | ||
4229 | if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) { | |
4230 | data->bulk_tx_ep = ep_desc; | |
4231 | continue; | |
4232 | } | |
4233 | ||
4234 | if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) { | |
4235 | data->bulk_rx_ep = ep_desc; | |
4236 | continue; | |
4237 | } | |
4238 | } | |
4239 | ||
4240 | if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) { | |
4241 | rtk_free(data); | |
4242 | return -ENODEV; | |
4243 | } | |
4244 | ||
4245 | data->cmdreq_type = USB_TYPE_CLASS; | |
4246 | ||
4247 | data->udev = udev; | |
4248 | data->intf = intf; | |
4249 | ||
4250 | dlfw_dis_state = 0; | |
4251 | spin_lock_init(&queue_lock); | |
4252 | spin_lock_init(&dlfw_lock); | |
4253 | spin_lock_init(&data->lock); | |
4254 | ||
4255 | INIT_WORK(&data->work, btusb_work); | |
4256 | INIT_WORK(&data->waker, btusb_waker); | |
4257 | spin_lock_init(&data->txlock); | |
4258 | ||
4259 | init_usb_anchor(&data->tx_anchor); | |
4260 | init_usb_anchor(&data->intr_anchor); | |
4261 | init_usb_anchor(&data->bulk_anchor); | |
4262 | init_usb_anchor(&data->isoc_anchor); | |
4263 | init_usb_anchor(&data->deferred); | |
4264 | ||
4265 | fw_info = firmware_info_init(intf); | |
4266 | if (fw_info) | |
4267 | data->fw_info = fw_info; | |
4268 | else { | |
4269 | RTKBT_WARN("%s: Failed to initialize fw info", __func__); | |
4270 | /* Skip download patch */ | |
4271 | goto end; | |
4272 | } | |
4273 | ||
4274 | hdev = hci_alloc_dev(); | |
4275 | if (!hdev) { | |
4276 | rtk_free(data); | |
4277 | data = NULL; | |
4278 | return -ENOMEM; | |
4279 | } | |
4280 | ||
4281 | HDEV_BUS = HCI_USB; | |
4282 | ||
4283 | data->hdev = hdev; | |
4284 | ||
4285 | SET_HCIDEV_DEV(hdev, &intf->dev); | |
4286 | ||
4287 | hdev->open = btusb_open; | |
4288 | hdev->close = btusb_close; | |
4289 | hdev->flush = btusb_flush; | |
4290 | hdev->send = btusb_send_frame; | |
4291 | hdev->notify = btusb_notify; | |
4292 | ||
4293 | #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0) | |
4294 | hci_set_drvdata(hdev, data); | |
4295 | #else | |
4296 | hdev->driver_data = data; | |
4297 | hdev->destruct = btusb_destruct; | |
4298 | hdev->owner = THIS_MODULE; | |
4299 | #endif | |
4300 | ||
4301 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 1) | |
4302 | if (!reset_on_close){ | |
4303 | /* set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks); */ | |
4304 | RTKBT_DBG("%s: Set HCI_QUIRK_RESET_ON_CLOSE", __func__); | |
4305 | } | |
4306 | #endif | |
4307 | ||
4308 | /* Interface numbers are hardcoded in the specification */ | |
4309 | data->isoc = usb_ifnum_to_if(data->udev, 1); | |
4310 | if (data->isoc) { | |
4311 | err = usb_driver_claim_interface(&btusb_driver, | |
4312 | data->isoc, data); | |
4313 | if (err < 0) { | |
4314 | hci_free_dev(hdev); | |
4315 | hdev = NULL; | |
4316 | rtk_free(data); | |
4317 | data = NULL; | |
4318 | return err; | |
4319 | } | |
4320 | #ifdef CONFIG_SCO_OVER_HCI | |
4321 | data->pSCOSnd = btusb_snd_init(intf, id, data); | |
4322 | #endif | |
4323 | } | |
4324 | ||
4325 | err = hci_register_dev(hdev); | |
4326 | if (err < 0) { | |
4327 | hci_free_dev(hdev); | |
4328 | hdev = NULL; | |
4329 | rtk_free(data); | |
4330 | data = NULL; | |
4331 | return err; | |
4332 | } | |
4333 | ||
4334 | usb_set_intfdata(intf, data); | |
4335 | ||
4336 | //#ifdef CONFIG_HAS_EARLYSUSPEND | |
4337 | #if 0 | |
4338 | data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN; | |
4339 | data->early_suspend.suspend = btusb_early_suspend; | |
4340 | data->early_suspend.resume = btusb_late_resume; | |
4341 | register_early_suspend(&data->early_suspend); | |
4342 | #else | |
4343 | data->pm_notifier.notifier_call = bt_pm_notify; | |
4344 | data->reboot_notifier.notifier_call = bt_reboot_notify; | |
4345 | register_pm_notifier(&data->pm_notifier); | |
4346 | register_reboot_notifier(&data->reboot_notifier); | |
4347 | #endif | |
4348 | ||
4349 | #if CONFIG_BLUEDROID | |
4350 | RTKBT_INFO("%s: Check bt reset flag %d", __func__, bt_reset); | |
4351 | /* Report hci hardware error after everthing is ready, | |
4352 | * especially hci register is completed. Or, btchr_poll | |
4353 | * will get null hci dev when hotplug in. | |
4354 | */ | |
4355 | if (bt_reset == 1) { | |
4356 | hci_hardware_error(); | |
4357 | bt_reset = 0; | |
4358 | } else | |
4359 | bt_reset = 0; /* Clear and reset it anyway */ | |
4360 | #endif | |
4361 | ||
4362 | end: | |
4363 | return 0; | |
4364 | } | |
4365 | ||
4366 | static void btusb_disconnect(struct usb_interface *intf) | |
4367 | { | |
4368 | struct btusb_data *data; | |
4369 | struct hci_dev *hdev = NULL; | |
4370 | ||
4371 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) | |
4372 | return; | |
4373 | ||
4374 | wait_event_interruptible(bt_dlfw_wait, (check_set_dlfw_state_value(2) == 2)); | |
4375 | ||
4376 | RTKBT_INFO("%s: usb_interface %p, bInterfaceNumber %d", | |
4377 | __func__, intf, intf->cur_altsetting->desc.bInterfaceNumber); | |
4378 | ||
4379 | data = usb_get_intfdata(intf); | |
4380 | ||
4381 | if (data) | |
4382 | hdev = data->hdev; | |
4383 | else { | |
4384 | RTKBT_WARN("%s: Failed to get bt usb data[Null]", __func__); | |
4385 | return; | |
4386 | } | |
4387 | ||
4388 | #ifdef CONFIG_SCO_OVER_HCI | |
4389 | if (intf->cur_altsetting->desc.bInterfaceNumber == 0) { | |
4390 | RTK_sco_card_t *pSCOSnd = data->pSCOSnd; | |
4391 | if(!pSCOSnd) { | |
4392 | RTKBT_ERR("%s: sco private data is null", __func__); | |
4393 | return; | |
4394 | } | |
4395 | set_bit(DISCONNECTED, &pSCOSnd->states); | |
4396 | snd_card_disconnect(pSCOSnd->card); | |
4397 | snd_card_free_when_closed(pSCOSnd->card); | |
4398 | } | |
4399 | #endif | |
4400 | ||
4401 | //#ifdef CONFIG_HAS_EARLYSUSPEND | |
4402 | #if 0 | |
4403 | unregister_early_suspend(&data->early_suspend); | |
4404 | #else | |
4405 | unregister_pm_notifier(&data->pm_notifier); | |
4406 | unregister_reboot_notifier(&data->reboot_notifier); | |
4407 | #endif | |
4408 | ||
4409 | firmware_info_destroy(intf); | |
4410 | ||
4411 | #if CONFIG_BLUEDROID | |
4412 | if (test_bit(HCI_RUNNING, &hdev->flags)) { | |
4413 | RTKBT_INFO("%s: Set BT reset flag", __func__); | |
4414 | bt_reset = 1; | |
4415 | } | |
4416 | #endif | |
4417 | ||
4418 | usb_set_intfdata(data->intf, NULL); | |
4419 | ||
4420 | if (data->isoc) | |
4421 | usb_set_intfdata(data->isoc, NULL); | |
4422 | ||
4423 | hci_unregister_dev(hdev); | |
4424 | ||
4425 | if (intf == data->isoc) | |
4426 | usb_driver_release_interface(&btusb_driver, data->intf); | |
4427 | else if (data->isoc) | |
4428 | usb_driver_release_interface(&btusb_driver, data->isoc); | |
4429 | ||
4430 | #if !CONFIG_BLUEDROID | |
4431 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0) | |
4432 | __hci_dev_put(hdev); | |
4433 | #endif | |
4434 | #endif | |
4435 | ||
4436 | hci_free_dev(hdev); | |
4437 | rtk_free(data); | |
4438 | data = NULL; | |
4439 | set_dlfw_state_value(0); | |
4440 | } | |
4441 | ||
4442 | #ifdef CONFIG_PM | |
4443 | static int btusb_suspend(struct usb_interface *intf, pm_message_t message) | |
4444 | { | |
4445 | struct btusb_data *data = usb_get_intfdata(intf); | |
4446 | firmware_info *fw_info = data->fw_info; | |
4447 | ||
4448 | RTKBT_INFO("%s: event 0x%x, suspend count %d", __func__, | |
4449 | message.event, data->suspend_count); | |
4450 | ||
4451 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) | |
4452 | return 0; | |
4453 | ||
4454 | if (!test_bit(HCI_RUNNING, &data->hdev->flags)) | |
4455 | set_bt_onoff(fw_info, 1); | |
4456 | ||
4457 | if (data->suspend_count++) | |
4458 | return 0; | |
4459 | ||
4460 | spin_lock_irq(&data->txlock); | |
4461 | if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) { | |
4462 | set_bit(BTUSB_SUSPENDING, &data->flags); | |
4463 | spin_unlock_irq(&data->txlock); | |
4464 | } else { | |
4465 | spin_unlock_irq(&data->txlock); | |
4466 | data->suspend_count--; | |
4467 | RTKBT_ERR("%s: Failed to enter suspend", __func__); | |
4468 | return -EBUSY; | |
4469 | } | |
4470 | ||
4471 | cancel_work_sync(&data->work); | |
4472 | ||
4473 | btusb_stop_traffic(data); | |
4474 | mdelay(URB_CANCELING_DELAY_MS); | |
4475 | usb_kill_anchored_urbs(&data->tx_anchor); | |
4476 | ||
4477 | #if SUSPNED_DW_FW | |
4478 | if(fw_info_4_suspend) { | |
4479 | download_suspend_patch(fw_info_4_suspend,1); | |
4480 | } | |
4481 | else | |
4482 | RTKBT_ERR("%s: Failed to download suspend fw", __func__); | |
4483 | #endif | |
4484 | ||
4485 | #if SET_WAKEUP_DEVICE | |
4486 | set_wakeup_device_from_conf(fw_info_4_suspend); | |
4487 | #endif | |
4488 | ||
4489 | return 0; | |
4490 | } | |
4491 | ||
4492 | static void play_deferred(struct btusb_data *data) | |
4493 | { | |
4494 | struct urb *urb; | |
4495 | int err; | |
4496 | ||
4497 | while ((urb = usb_get_from_anchor(&data->deferred))) { | |
4498 | usb_anchor_urb(urb, &data->tx_anchor); | |
4499 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
4500 | if (err < 0) { | |
4501 | RTKBT_ERR("%s: Failed to submit urb %p, err %d", | |
4502 | __func__, urb, err); | |
4503 | kfree(urb->setup_packet); | |
4504 | usb_unanchor_urb(urb); | |
4505 | } else { | |
4506 | usb_mark_last_busy(data->udev); | |
4507 | } | |
4508 | usb_free_urb(urb); | |
4509 | ||
4510 | data->tx_in_flight++; | |
4511 | } | |
4512 | mdelay(URB_CANCELING_DELAY_MS); | |
4513 | usb_scuttle_anchored_urbs(&data->deferred); | |
4514 | } | |
4515 | ||
4516 | static int btusb_resume(struct usb_interface *intf) | |
4517 | { | |
4518 | struct btusb_data *data = usb_get_intfdata(intf); | |
4519 | struct hci_dev *hdev = data->hdev; | |
4520 | firmware_info *fw_info = data->fw_info; | |
4521 | int err = 0; | |
4522 | ||
4523 | RTKBT_INFO("%s: Suspend count %d", __func__, data->suspend_count); | |
4524 | ||
4525 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) | |
4526 | return 0; | |
4527 | ||
4528 | if (--data->suspend_count) | |
4529 | return 0; | |
4530 | ||
4531 | /*check_fw_version to check the status of the BT Controller after USB Resume*/ | |
4532 | err = check_fw_version(fw_info, true); | |
4533 | if (err !=0) | |
4534 | { | |
4535 | RTKBT_INFO("%s: BT Controller Power OFF And Return hci_hardware_error:%d", __func__, err); | |
4536 | hci_hardware_error(); | |
4537 | } | |
4538 | ||
4539 | ||
4540 | if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) { | |
4541 | err = btusb_submit_intr_urb(hdev, GFP_NOIO); | |
4542 | if (err < 0) { | |
4543 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); | |
4544 | goto failed; | |
4545 | } | |
4546 | } | |
4547 | ||
4548 | if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) { | |
4549 | err = btusb_submit_bulk_urb(hdev, GFP_NOIO); | |
4550 | if (err < 0) { | |
4551 | clear_bit(BTUSB_BULK_RUNNING, &data->flags); | |
4552 | goto failed; | |
4553 | } | |
4554 | ||
4555 | btusb_submit_bulk_urb(hdev, GFP_NOIO); | |
4556 | } | |
4557 | ||
4558 | if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { | |
4559 | if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0) | |
4560 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); | |
4561 | else | |
4562 | btusb_submit_isoc_urb(hdev, GFP_NOIO); | |
4563 | } | |
4564 | ||
4565 | spin_lock_irq(&data->txlock); | |
4566 | play_deferred(data); | |
4567 | clear_bit(BTUSB_SUSPENDING, &data->flags); | |
4568 | spin_unlock_irq(&data->txlock); | |
4569 | schedule_work(&data->work); | |
4570 | ||
4571 | return 0; | |
4572 | ||
4573 | failed: | |
4574 | mdelay(URB_CANCELING_DELAY_MS); | |
4575 | usb_scuttle_anchored_urbs(&data->deferred); | |
4576 | spin_lock_irq(&data->txlock); | |
4577 | clear_bit(BTUSB_SUSPENDING, &data->flags); | |
4578 | spin_unlock_irq(&data->txlock); | |
4579 | ||
4580 | return err; | |
4581 | } | |
4582 | #endif | |
4583 | ||
4584 | static struct usb_driver btusb_driver = { | |
4585 | .name = "rtk_btusb", | |
4586 | .probe = btusb_probe, | |
4587 | .disconnect = btusb_disconnect, | |
4588 | #ifdef CONFIG_PM | |
4589 | .suspend = btusb_suspend, | |
4590 | .resume = btusb_resume, | |
4591 | #endif | |
4592 | #if CONFIG_RESET_RESUME | |
4593 | .reset_resume = btusb_resume, | |
4594 | #endif | |
4595 | .id_table = btusb_table, | |
4596 | .supports_autosuspend = 1, | |
4597 | #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1) | |
4598 | .disable_hub_initiated_lpm = 1, | |
4599 | #endif | |
4600 | }; | |
4601 | ||
4602 | static int __init btusb_init(void) | |
4603 | { | |
4604 | int err; | |
4605 | ||
4606 | RTKBT_INFO("RTKBT_RELEASE_NAME: %s",RTKBT_RELEASE_NAME); | |
4607 | RTKBT_INFO("Realtek Bluetooth USB driver module init, version %s", VERSION); | |
4608 | #if CONFIG_BLUEDROID | |
4609 | err = btchr_init(); | |
4610 | if (err < 0) { | |
4611 | /* usb register will go on, even bt char register failed */ | |
4612 | RTKBT_ERR("Failed to register usb char device interfaces"); | |
4613 | } else | |
4614 | bt_char_dev_registered = true; | |
4615 | #endif | |
4616 | err = usb_register(&btusb_driver); | |
4617 | if (err < 0) | |
4618 | RTKBT_ERR("Failed to register RTK bluetooth USB driver"); | |
4619 | return err; | |
4620 | } | |
4621 | ||
4622 | static void __exit btusb_exit(void) | |
4623 | { | |
4624 | struct hci_dev *hdev; | |
4625 | RTKBT_INFO("Realtek Bluetooth USB driver module exit"); | |
4626 | #if CONFIG_BLUEDROID | |
4627 | hdev = hci_dev_get(0); | |
4628 | if (bt_char_dev_registered) { | |
4629 | bt_char_dev_registered = false; | |
4630 | while(hdev && atomic_read(&hdev->promisc)) { | |
4631 | RTKBT_ERR("%s: rtkbt driver is being removed, but application is still running!", __func__); | |
4632 | RTKBT_ERR("%s: wait bt application to stop, or the driver can't be removed", __func__); | |
4633 | mdelay(100); | |
4634 | } | |
4635 | btchr_exit(); | |
4636 | } | |
4637 | #endif | |
4638 | usb_deregister(&btusb_driver); | |
4639 | } | |
4640 | ||
4641 | module_init(btusb_init); | |
4642 | module_exit(btusb_exit); | |
4643 | ||
4644 | ||
4645 | module_param(mp_drv_mode, int, 0644); | |
4646 | MODULE_PARM_DESC(mp_drv_mode, "0: NORMAL; 1: MP MODE"); | |
4647 | ||
4648 | ||
4649 | MODULE_AUTHOR("Realtek Corporation"); | |
4650 | MODULE_DESCRIPTION("Realtek Bluetooth USB driver version"); | |
4651 | MODULE_VERSION(VERSION); | |
4652 | MODULE_LICENSE("GPL"); |