add MTK-combo-module,continue with commit 17f39ed917874e77e80411f33faba1b7ee8138c8
[firefly-linux-kernel-4.4.55.git] / drivers / mtk_wcn_combo / drv_bt / linux / hci_stp.c
1 /* Copyright Statement:
2  *
3  * This software/firmware and related documentation ("MediaTek Software") are
4  * protected under relevant copyright laws. The information contained herein
5  * is confidential and proprietary to MediaTek Inc. and/or its licensors.
6  * Without the prior written permission of MediaTek inc. and/or its licensors,
7  * any reproduction, modification, use or disclosure of MediaTek Software,
8  * and information contained herein, in whole or in part, shall be strictly prohibited.
9  *
10  * MediaTek Inc. (C) 2010. All rights reserved.
11  *
12  * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
13  * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
14  * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
15  * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
18  * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
19  * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
20  * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
21  * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
22  * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
23  * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
24  * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
25  * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
26  * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
27  * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
28  * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
29  * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
30  *
31  * The following software/firmware and/or related documentation ("MediaTek Software")
32  * have been modified by MediaTek Inc. All revisions are subject to any receiver's
33  * applicable license agreements with MediaTek Inc.
34  */
35
36 /*
37  *
38  *  Bluetooth HCI UART driver
39  *
40  *  Copyright (C) 2000-2001  Qualcomm Incorporated
41  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
42  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
43  *
44  *
45  *  This program is free software; you can redistribute it and/or modify
46  *  it under the terms of the GNU General Public License as published by
47  *  the Free Software Foundation; either version 2 of the License, or
48  *  (at your option) any later version.
49  *
50  *  This program is distributed in the hope that it will be useful,
51  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
52  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
53  *  GNU General Public License for more details.
54  *
55  *  You should have received a copy of the GNU General Public License
56  *  along with this program; if not, write to the Free Software
57  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
58  *
59  */
60
61 /*******************************************************************************
62 *                         C O M P I L E R   F L A G S
63 ********************************************************************************
64 */
65 /* move compile flags to hci_stp.h file. */
66
67 /*******************************************************************************
68 *                    E X T E R N A L   R E F E R E N C E S
69 ********************************************************************************
70 */
71
72 #include "hci_stp.h"
73 #include "stp_exp.h"
74 #include "wmt_exp.h"
75
76 /*******************************************************************************
77 *                              C O N S T A N T S
78 ********************************************************************************
79 */
80
81 /* Debugging Purpose */
82 #define PFX "[HCI-STP]"
83 #define BT_LOG_LOUD (4)
84 #define BT_LOG_DBG (3)
85 #define BT_LOG_INFO (2)
86 #define BT_LOG_WARN (1)
87 #define BT_LOG_ERR (0)
88
89 #define VERSION "1.0"
90 #define CUSTOM_BT_CFG_FILE          "/data/BT.cfg"
91 #define INTERNAL_BT_CFG_FILE        "/data/bluetooth/BT.cfg"
92 #define BUILTIN_BT_CFG_FILE         "/system/etc/firmware/BT.cfg"
93
94 /* H4 receiver States */
95 #define H4_W4_PACKET_TYPE (0)
96 #define H4_W4_EVENT_HDR (1)
97 #define H4_W4_ACL_HDR (2)
98 #define H4_W4_SCO_HDR (3)
99 #define H4_W4_DATA (4)
100
101 #define HCI_STP_TXQ_IN_BLZ (0)
102 /* access txq in BlueZ tx tasklet context */
103 #define HCI_STP_TXQ_IN_HCISTP (1)
104 /* access txq in HCI-STP context,  defined by compile flag: HCI_STP_TX */
105
106 /*******************************************************************************
107 *                            P U B L I C   D A T A
108 ********************************************************************************
109 */
110 unsigned int gHciStpDbgLevel = BT_LOG_ERR;//Modify loglevel
111
112 /*******************************************************************************
113 *                           P R I V A T E   D A T A
114 ********************************************************************************
115 */
116 /* Allow one BT driver */
117 static struct hci_dev *hdev = NULL;
118 static int reset = 0;
119
120 /* maybe struct hci_stp is a better place to put these data */
121 #if (HCI_STP_TX == HCI_STP_TX_TASKLET)
122 static struct tasklet_struct hci_tx_tasklet;
123
124 #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
125 static DEFINE_RWLOCK(hci_stp_txqlock);
126
127 #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
128 static spinlock_t hci_stp_txqlock;
129
130 #endif
131 #endif
132
133 #if (HCI_STP_TX == HCI_STP_TX_THRD)
134 static spinlock_t hci_stp_txqlock;
135 struct task_struct * hci_stp_tx_thrd = NULL;
136 wait_queue_head_t hci_stp_tx_thrd_wq;
137 #endif
138
139 /* oringinal code path:
140 system/bluetooth/bluedroid/bluetooth.c
141 external/bluetooth/bluez/tools/hciattach.c
142 mtk/src/custom/mt6516_evb/kernel/core/src/board.c // set power gpio configuration
143 */
144 static unsigned char bt_bd_addr[10] =
145     {0x01, 0x1a, 0xfc, 0x06, 0x01, 0x20, 0x66, 0x46, 0x00, 0x00};
146 static unsigned char bt_bd_addr_evt[] =
147     {0x04, 0x0e, 0x04, 0x01, 0x1a, 0xfc, 0x00};
148 static unsigned char bt_link_key_type[5]=
149     {0x01, 0x1b, 0xfc, 0x01, 0x01};
150 static unsigned char bt_link_key_type_evt[] =
151     {0x04, 0x0e, 0x04, 0x01, 0x1b, 0xfc, 0x00};
152 static unsigned char bt_unit_key[20] =
153     {0x01, 0x75, 0xfc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
154 static unsigned char bt_unit_key_evt[] =
155     {0x04, 0x0e, 0x04, 0x01, 0x75, 0xfc, 0x00};
156 static unsigned char bt_encrypt[7] =
157     {0x01, 0x76, 0xfc, 0x03, 0x00, 0x02, 0x10};
158 static unsigned char bt_encrypt_evt[] =
159     {0x04, 0x0e, 0x04, 0x01, 0x76, 0xfc, 0x00};
160 static unsigned char bt_pin_code_type[5] =
161     {0x01, 0x0a, 0x0c, 0x01, 0x00};
162 static unsigned char bt_pin_code_type_evt[] =
163     {0x04, 0x0e, 0x04, 0x01, 0x0a, 0x0c, 0x00};
164 static unsigned char bt_voice[6] =
165     {0x01, 0x26, 0x0c, 0x02, 0x60, 0x00};
166 static unsigned char bt_voice_evt[] =
167     {0x04, 0x0e, 0x04, 0x01, 0x26, 0x0c, 0x00};
168 static unsigned char bt_codec[8] =
169     {0x01, 0x72, 0xfc, 0x04, 0x23, 0x10, 0x00, 0x00};
170 static unsigned char bt_codec_evt[] =
171     {0x04, 0x0e, 0x04, 0x01, 0x72, 0xfc, 0x00};
172 static unsigned char bt_radio[10] =
173     {0x01, 0x79, 0xfc, 0x06, 0x06, 0x80, 0x00, 0x06, 0x03, 0x06};
174 static unsigned char bt_radio_evt[] =
175     {0x04, 0x0e, 0x04, 0x01, 0x79, 0xfc, 0x00};
176 static unsigned char bt_tx_pwr_offset[7] =
177     {0x01, 0x93, 0xfc, 0x03, 0xff, 0xff, 0xff};
178 static unsigned char bt_tx_pwr_offset_evt[] =
179     {0x04, 0x0e, 0x04, 0x01, 0x93, 0xfc, 0x00};
180 static unsigned char bt_sleep[11] =
181     {0x01, 0x7a, 0xfc, 0x07, 0x03, 0x40, 0x1f, 0x40, 0x1f, 0x00, 0x04};
182 static unsigned char bt_sleep_evt[] =
183     {0x04, 0x0e, 0x04, 0x01, 0x7a, 0xfc, 0x00};
184 static unsigned char bt_feature[6] =
185     {0x01, 0x7d, 0xfc, 0x02, 0x80, 0x0};
186 static unsigned char bt_feature_evt[] =
187     {0x04, 0x0e, 0x04, 0x01, 0x7d, 0xfc, 0x00};
188 static unsigned char bt_OSC[9] =
189     {0x01, 0x7b, 0xfc, 0x05, 0x01, 0x01, 0x14, 0x0a, 0x05};
190 static unsigned char bt_OSC_evt[] =
191     {0x04, 0x0e, 0x04, 0x01, 0x7b, 0xfc, 0x00};
192 static unsigned char bt_LPO[14] =
193     {0x01, 0x7c, 0xfc, 0x0a, 0x01, 0xfa, 0x0a, 0x02, 0x00, 0xa6, 0x0e, 0x00, 0x40, 0x00};
194 static unsigned char bt_LPO_evt[] =
195     {0x04, 0x0e, 0x04, 0x01, 0x7c, 0xfc, 0x00};
196 static unsigned char bt_legacy_PTA[14] =
197     {0x01, 0x74, 0xfc, 0x0a, 0xc9, 0x8b, 0xbf, 0x00, 0x00, 0x52, 0x0e, 0x0e, 0x1f, 0x1b};
198 static unsigned char bt_legacy_PTA_evt[] =
199     {0x04, 0x0e, 0x04, 0x01, 0x74, 0xfc, 0x00};
200 static unsigned char bt_BLE_PTA[9] =
201     {0x01, 0xfc, 0xfc, 0x05, 0x16, 0x0e, 0x0e, 0x00, 0x07};
202 static unsigned char bt_BLE_PTA_evt[] =
203     {0x04, 0x0e, 0x04, 0x01, 0xfc, 0xfc, 0x00};
204 static unsigned char bt_RF_desence[10] =
205     {0x01, 0x20, 0xfc, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
206 static unsigned char bt_RF_desence_evt[] =
207     {0x04, 0x0e, 0x04, 0x01, 0x20, 0xfc, 0x00};
208 static unsigned char bt_reset[4] =
209     {0x01, 0x03, 0x0c, 0x0};
210 static unsigned char bt_reset_evt[] =
211     {0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00};
212 static unsigned char bt_intern_PTA_1[19] =
213     {0x01, 0xfb, 0xfc, 0x0f, 0x00, 0x01, 0x0f, 0x0f, 0x01, 0x0f, 0x0f, 0x01, 0x0f, 0x0f, 0x01, 0x0f, 0x0f, 0x02, 0x01};
214 static unsigned char bt_intern_PTA_1_evt[] =
215     {0x04, 0x0e, 0x04, 0x01, 0xfb, 0xfc, 0x00};
216 static unsigned char bt_intern_PTA_2[11] =
217     {0x01, 0xfb, 0xfc, 0x07, 0x01, 0x19, 0x19, 0x07, 0xd0, 0x00, 0x01};
218 static unsigned char bt_intern_PTA_2_evt[] =
219     {0x04, 0x0e, 0x04, 0x01, 0xfb, 0xfc, 0x00};
220 static unsigned char bt_SLP_control_reg[12] =
221     {0x01, 0xd0, 0xfc, 0x08, 0x74, 0x00, 0x01, 0x81, 0xe2, 0x29, 0x0, 0x0};
222 static unsigned char bt_SLP_control_reg_evt[] =
223     {0x04, 0x0e, 0x04, 0x01, 0xd0, 0xfc, 0x00};
224 static unsigned char bt_SLP_LDOD_reg[12] =
225     {0x01, 0xd0, 0xfc, 0x08, 0x1c, 0x00, 0x02, 0x81, 0x79, 0x08, 0x0, 0x0};
226 static unsigned char bt_SLP_LDOD_reg_evt[] =
227     {0x04, 0x0e, 0x04, 0x01, 0xd0, 0xfc, 0x00};
228 static unsigned char bt_RF_reg[10] =
229     {0x01, 0xb0, 0xfc, 0x06, 0x64, 0x01, 0x02, 0x00, 0x00, 0x00};
230 static unsigned char bt_RF_reg_evt[] =
231     {0x04, 0x0e, 0x04, 0x01, 0xb0, 0xfc, 0x00};
232
233 #if (HCI_STP_DEV_INIT == HCI_STP_DEV_INIT_THRD)
234 /* use this array to store received event content temporarily */
235 static struct hci_stp_init_cmd init_table[] =
236 {
237     /* do init in sequence, cmd and cmd##_evt array */
238     hci_stp_init_entry(bt_bd_addr),
239     hci_stp_init_entry(bt_link_key_type),
240     hci_stp_init_entry(bt_unit_key),
241     hci_stp_init_entry(bt_encrypt),
242     hci_stp_init_entry(bt_pin_code_type),
243     hci_stp_init_entry(bt_voice),
244     hci_stp_init_entry(bt_codec),
245     hci_stp_init_entry(bt_radio),
246     hci_stp_init_entry(bt_tx_pwr_offset),
247     hci_stp_init_entry(bt_sleep),
248     hci_stp_init_entry(bt_feature),
249     hci_stp_init_entry(bt_OSC),
250     hci_stp_init_entry(bt_LPO),
251     hci_stp_init_entry(bt_legacy_PTA),
252     hci_stp_init_entry(bt_BLE_PTA),
253     hci_stp_init_entry(bt_RF_desence),
254     hci_stp_init_entry(bt_reset),
255     hci_stp_init_entry(bt_intern_PTA_1),
256     hci_stp_init_entry(bt_intern_PTA_2),
257     hci_stp_init_entry(bt_SLP_control_reg),
258     hci_stp_init_entry(bt_SLP_LDOD_reg),
259     hci_stp_init_entry(bt_RF_reg),
260 };
261 #endif
262
263 /*******************************************************************************
264 *                             D A T A   T Y P E S
265 ********************************************************************************
266 */
267
268 /*******************************************************************************
269 *                                 M A C R O S
270 ********************************************************************************
271 */
272
273 #define BT_LOUD_FUNC(fmt, arg...)    if(gHciStpDbgLevel >= BT_LOG_LOUD){ printk(PFX "[L]%s:"  fmt, __FUNCTION__ ,##arg);}
274 #define BT_DBG_FUNC(fmt, arg...)    if(gHciStpDbgLevel >= BT_LOG_DBG){ printk(PFX "[D]%s:"  fmt, __FUNCTION__ ,##arg);}
275 #define BT_INFO_FUNC(fmt, arg...)   if(gHciStpDbgLevel >= BT_LOG_INFO){ printk(PFX "[I]%s:"  fmt, __FUNCTION__ ,##arg);}
276 #define BT_WARN_FUNC(fmt, arg...)   if(gHciStpDbgLevel >= BT_LOG_WARN){ printk(PFX "[W]%s:"  fmt, __FUNCTION__ ,##arg);}
277 #define BT_ERR_FUNC(fmt, arg...)    if(gHciStpDbgLevel >= BT_LOG_ERR){  printk(PFX "[E]%s:"   fmt, __FUNCTION__ ,##arg);}
278 #define BT_TRC_FUNC(f)              if(gHciStpDbgLevel >= BT_LOG_LOUD){printk(PFX "[T]%s:%d\n", __FUNCTION__, __LINE__);}
279
280 #if HCI_STP_SAFE_RESET
281 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
282 /* HCI_RESET bit definition in linux/include/net/bluetooth/hci.h since 2.6.39:
283     http://lxr.free-electrons.com/source/include/net/bluetooth/hci.h?v=2.6.39;a=arm
284 */
285 #define BT_GET_HDEV_RST_FG(hdev) (test_bit(HCI_RESET, &hdev->flags))
286 #else
287 #define BT_GET_HDEV_RST_FG(hdev) (0) /* no HCI_RESET bit available */
288 #endif
289 #endif
290
291 /*******************************************************************************
292 *                  F U N C T I O N   D E C L A R A T I O N S
293 ********************************************************************************
294 */
295 /* Functions to be implemted by all HCI_STP_TX_* methods */
296 void hci_stp_tx_init (struct hci_stp *hu);
297 void hci_stp_tx_deinit (struct hci_stp *hu);
298 void hci_stp_txq_lock (unsigned int ctx);
299 void hci_stp_txq_unlock (unsigned int ctx);
300 void hci_stp_tx_kick (void);
301
302 /* Functions to be implemented by all HCI_STP_INIT_* methods*/
303 static int hci_stp_dev_init (struct hci_stp *phu);
304
305 #if (HCI_STP_TX == HCI_STP_TX_TASKLET)
306 static int hci_stp_tx_wakeup (struct hci_stp *hu);
307 static void hci_stp_tx_tasklet_func (unsigned long data);
308 #endif
309
310 #if (HCI_STP_TX == HCI_STP_TX_THRD)
311 static int hci_stp_tx_thrd_func (void *pdata);
312 #endif
313
314 /*******************************************************************************
315 *                              F U N C T I O N S
316 ********************************************************************************
317 */
318
319 static inline void hci_stp_tx_skb_comp (struct hci_stp *hu, struct sk_buff *skb)
320 {
321     struct hci_dev *hdev;
322     int pkt_type;
323
324
325     hdev = hu->hdev;
326     hdev->stat.byte_tx += skb->len;
327
328     pkt_type = bt_cb(skb)->pkt_type;
329     /* Update HCI stat counters */
330     switch (pkt_type) {
331        case HCI_COMMAND_PKT:
332             hdev->stat.cmd_tx++;
333             break;
334
335        case HCI_ACLDATA_PKT:
336             hdev->stat.acl_tx++;
337             break;
338
339        case HCI_SCODATA_PKT:
340             hdev->stat.cmd_tx++;
341             break;
342     }
343 }
344
345 #if (HCI_STP_TX == HCI_STP_TX_TASKLET)
346 void hci_stp_tx_init (struct hci_stp *hu)
347 {
348     tasklet_init(&hci_tx_tasklet, hci_stp_tx_tasklet_func, (unsigned long)hu);
349
350     #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
351     rwlock_init(&hci_stp_txqlock);
352     #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
353     spin_lock_init(&hci_stp_txqlock);
354     #endif
355 }
356
357 void hci_stp_tx_deinit (struct hci_stp *hu)
358 {
359     tasklet_kill(&hci_tx_tasklet);
360
361     return;
362 }
363
364 void hci_stp_txq_lock (unsigned int ctx)
365 {
366     if (ctx == HCI_STP_TXQ_IN_BLZ) {
367         /* lock txq in BlueZ tx tasklet context */
368     #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
369         write_lock_bh(&hci_stp_txqlock);
370     #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
371         spin_lock_bh(&hci_stp_txqlock);
372     #else
373     #error "HCI_STP_TX_TASKLET_LOCK"
374     #endif
375     }
376     else {
377         /* lock txq in HCI-STP context(defined by compile flag: HCI_STP_TX) */
378     #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
379         write_lock_bh(&hci_stp_txqlock);
380     #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
381         spin_lock_bh(&hci_stp_txqlock);
382     #else
383     #error "HCI_STP_TX_TASKLET_LOCK"
384     #endif
385     }
386 }
387
388 void hci_stp_txq_unlock (unsigned int ctx)
389 {
390     if (ctx == HCI_STP_TXQ_IN_BLZ) {
391         /* lock txq in BlueZ tx tasklet context with hci_stp_tx */
392     #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
393         write_unlock_bh(&hci_stp_txqlock);
394     #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
395         spin_unlock_bh(&hci_stp_txqlock);
396     #else
397     #error "HCI_STP_TX_TASKLET_LOCK"
398     #endif
399     }
400     else {
401         /* lock txq in HCI-STP context(defined by compile flag: HCI_STP_TX) */
402     #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
403         write_unlock_bh(&hci_stp_txqlock);
404     #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
405         spin_unlock_bh(&hci_stp_txqlock);
406     #else
407     #error "HCI_STP_TX_TASKLET_LOCK"
408     #endif
409     }
410 }
411
412 void hci_stp_tx_kick (void)
413 {
414     tasklet_schedule(&hci_tx_tasklet);
415 }
416
417 static void hci_stp_tx_tasklet_func(unsigned long data) {
418
419     struct hci_stp *hu = (struct hci_stp *)data;
420
421     /* sanity check to see if status is still correct? */
422     if (unlikely(hdev == NULL)) {
423         BT_ERR_FUNC("Null hdev!\n");
424         BUG_ON(hdev == NULL);
425         return;
426     }
427
428     if (unlikely(hu != hdev->driver_data)) {
429         BT_ERR_FUNC("hu(0x%p) != hdev->driver_data(0x%p)\n",
430             hu, hdev->driver_data);
431         BUG_ON(hu != hdev->driver_data);
432         return;
433     }
434
435     //read_lock(&hci_stp_txq_lock);
436     hci_stp_txq_lock(HCI_STP_TXQ_IN_HCISTP);
437
438     hci_stp_tx_wakeup(hu);
439
440     //read_unlock(&hci_stp_txq_lock);
441     hci_stp_txq_unlock(HCI_STP_TXQ_IN_HCISTP);
442 }
443
444 /* George: HCI_STP_SENDING and HCI_STP_TX_WAKEUP flags in this function seem
445 * to be redundant.
446 */
447 static int hci_stp_tx_wakeup(struct hci_stp *hu)
448 {
449 //    struct hci_dev *hdev = hu->hdev;
450     struct sk_buff *skb;
451     int j = 0;
452
453     BT_TRC_FUNC();
454
455     if (test_and_set_bit(HCI_STP_SENDING, &hu->tx_state)) {
456         set_bit(HCI_STP_TX_WAKEUP, &hu->tx_state);
457         printk("[BT] enqueue and return\n");
458         return 0;
459     }
460
461     BT_DBG_FUNC("hci_stp_tx_wakeup %d\n", __LINE__);
462
463 restart:
464     clear_bit(HCI_STP_TX_WAKEUP, &hu->tx_state);
465
466     while ((skb = skb_dequeue(&hu->txq))) {
467         int len;
468         BT_DBG_FUNC("dqueue times = %d\n", ++j);
469
470         /* hci reset cmd check */
471 #if HCI_STP_SAFE_RESET
472         if (unlikely(skb->len == ARRAY_SIZE(bt_reset))) {
473             if (unlikely(!memcmp(bt_reset, skb->data, ARRAY_SIZE(bt_reset)))) {
474                 atomic_inc(&hu->reset_count);
475                 BT_DBG_FUNC("hci reset cmd,f(%d),c(%d)\n",
476                     BT_GET_HDEV_RST_FG(hdev),
477                     atomic_read(&hu->reset_count));
478             }
479         }
480 #endif
481
482         if ((len = mtk_wcn_stp_send_data(skb->data, skb->len, BT_TASK_INDX)) == 0 ) {
483             /* can not send */
484             BT_ERR_FUNC("mtk_wcn_stp_send_data can not send\n");
485             BT_ERR_FUNC("Error %s %d\n", __FUNCTION__, __LINE__);
486
487             skb_queue_head(&hu->txq, skb);//Put back to queue head
488             goto END;
489         }
490
491         //hdev->stat.byte_tx += len; // moved into hci_stp_tx_skb_comp()
492         //hci_stp_tx_skb_comp(hu, bt_cb(skb)->pkt_type);
493         hci_stp_tx_skb_comp(hu, skb);
494         kfree_skb(skb);
495     }
496
497 END:
498     if (test_bit(HCI_STP_TX_WAKEUP, &hu->tx_state))
499           goto restart;
500
501     clear_bit(HCI_STP_SENDING, &hu->tx_state);
502
503     return 0;
504 }
505
506
507 #elif (HCI_STP_TX == HCI_STP_TX_THRD)
508
509 void hci_stp_tx_init (struct hci_stp *hu)
510 {
511     BT_DBG_FUNC("start:0x%p\n", hu);
512
513     spin_lock_init(&hci_stp_txqlock);
514     init_waitqueue_head(&hci_stp_tx_thrd_wq);
515
516     hci_stp_tx_thrd = kthread_create(hci_stp_tx_thrd_func, (void *)hu, "hci_stpd");
517     if (NULL == hci_stp_tx_thrd) {
518         BT_ERR_FUNC("kthread_create hci_stpd fail!\n");
519     }
520     wake_up_process(hci_stp_tx_thrd);
521
522     BT_DBG_FUNC("done\n");
523     return;
524 }
525
526 void hci_stp_tx_deinit (struct hci_stp *hu)
527 {
528     BT_DBG_FUNC("start:0x%p\n", hu);
529
530     kthread_stop(hci_stp_tx_thrd);
531     hci_stp_tx_thrd = NULL;
532
533     BT_DBG_FUNC("done\n");
534     return;
535 }
536
537 void hci_stp_txq_lock (unsigned int ctx)
538 {
539     if (ctx == HCI_STP_TXQ_IN_BLZ) {
540         /* lock txq in BlueZ tx tasklet context */
541         spin_lock(&hci_stp_txqlock);
542     }
543     else {
544         /* lock txq in HCI-STP context(defined by compile flag: HCI_STP_TX) */
545         spin_lock_bh(&hci_stp_txqlock);
546     }
547 }
548
549 void hci_stp_txq_unlock (unsigned int ctx)
550 {
551     if (ctx == HCI_STP_TXQ_IN_BLZ) {
552         spin_unlock(&hci_stp_txqlock);
553     }
554     else {
555         /* lock txq in HCI-STP context(defined by compile flag: HCI_STP_TX) */
556         spin_unlock_bh(&hci_stp_txqlock);
557     }
558 }
559
560 static int
561 hci_stp_tx_thrd_func (void *pdata)
562 {
563     struct hci_stp *hu;
564     struct hci_dev *hdev;
565     struct sk_buff *skb;
566     int len;
567
568     hu = (struct hci_stp *)pdata;
569     hdev = hu->hdev;
570
571     /* sanity check to see if status is still correct? */
572     if (unlikely(hdev == NULL)) {
573         BT_ERR_FUNC("Null hdev!\n");
574         BUG_ON(hdev == NULL);
575         return -1;
576     }
577
578     if (unlikely(hu != hdev->driver_data)) {
579         BT_ERR_FUNC("hu(0x%p) != hdev->driver_data(0x%p)\n",
580             hu, hdev->driver_data);
581         BUG_ON(hu != hdev->driver_data);
582         return -1;
583     }
584
585     smp_mb(); /* sync shared data */
586
587     for (;;) {
588         wait_event_interruptible(hci_stp_tx_thrd_wq,
589             (!skb_queue_empty(&hu->txq) || kthread_should_stop()));
590
591         if (unlikely(kthread_should_stop())) {
592             BT_INFO_FUNC("hci_stpd thread should stop now... \n");
593             break;
594         }
595
596         hci_stp_txq_lock(HCI_STP_TXQ_IN_HCISTP);
597         while ((skb = skb_dequeue(&hu->txq))) {
598             /* protect txq only */
599             hci_stp_txq_unlock(HCI_STP_TXQ_IN_HCISTP);
600
601             /* hci reset cmd check */
602     #if HCI_STP_SAFE_RESET
603             if (unlikely(skb->len == ARRAY_SIZE(bt_reset))) {
604                 if (unlikely(!memcmp(bt_reset, skb->data, ARRAY_SIZE(bt_reset)))) {
605                     atomic_inc(&hu->reset_count);
606                     BT_DBG_FUNC("hci reset cmd,f(%d),c(%d)\n",
607                         BT_GET_HDEV_RST_FG(hdev),
608                         atomic_read(&hu->reset_count));
609                 }
610             }
611     #endif
612
613             len = mtk_wcn_stp_send_data(skb->data, skb->len, BT_TASK_INDX);
614             if (unlikely(len != skb->len)) {
615                 /* can not send */
616                 BT_ERR_FUNC("mtk_wcn_stp_send_data fail, enqueue again!(%d, %d)\n",
617                     len, skb->len);
618
619                 hci_stp_txq_lock(HCI_STP_TXQ_IN_HCISTP);
620                 skb_queue_head(&hu->txq, skb);//Put back to queue head
621                 /* do hci_stp_txq_unlock outside while loop */
622                 break;
623             }
624             //hdev->stat.byte_tx += len; // moved into hci_stp_tx_skb_comp()
625             hci_stp_tx_skb_comp(hu, skb);
626             kfree_skb(skb);
627
628             hci_stp_txq_lock(HCI_STP_TXQ_IN_HCISTP);
629         }
630         hci_stp_txq_unlock(HCI_STP_TXQ_IN_HCISTP);
631
632         /* back to wait */
633     }
634
635     BT_INFO_FUNC("hci_stpd stop!\n");
636     return 0;
637 }
638
639 void hci_stp_tx_kick (void)
640 {
641     wake_up_interruptible(&hci_stp_tx_thrd_wq);
642 }
643
644 #else
645 #error "Not implemented HCI_STP_TX"
646 #endif
647
648 #if (HCI_STP_DEV_INIT == HCI_STP_DEV_INIT_OPEN_CTX)
649 static int hci_stp_dev_init (struct hci_stp *phu)
650 {
651     BT_INFO_FUNC("Reset bd_addr =>\n");
652     mtk_wcn_stp_send_data(bt_bd_addr, 10, BT_TASK_INDX);
653     msleep(BT_CMD_DELAY_MS_COMM);
654     BT_INFO_FUNC("Reset bd_addr <=\n");
655
656     BT_INFO_FUNC("Reset bt_link_key_type =>\n");
657     mtk_wcn_stp_send_data(bt_link_key_type, 5, BT_TASK_INDX);
658     msleep(BT_CMD_DELAY_MS_COMM);
659     BT_INFO_FUNC("Reset bt_link_key_type <=\n");
660
661     BT_INFO_FUNC("Reset bt_unit_key =>\n");
662     mtk_wcn_stp_send_data(bt_unit_key, 20, BT_TASK_INDX);
663     msleep(BT_CMD_DELAY_MS_COMM);
664     BT_INFO_FUNC("Reset bt_unit_key <=\n");
665
666     BT_INFO_FUNC("Reset bt_encrypt =>\n");
667     mtk_wcn_stp_send_data(bt_encrypt, 7, BT_TASK_INDX);
668     msleep(BT_CMD_DELAY_MS_COMM);
669     BT_INFO_FUNC("Reset bt_encrypt <=\n");
670
671     BT_INFO_FUNC("Reset bt_pin_code_type =>\n");
672     mtk_wcn_stp_send_data(bt_pin_code_type, 5, BT_TASK_INDX);
673     msleep(BT_CMD_DELAY_MS_COMM);
674     BT_INFO_FUNC("Reset bt_pin_code_type <=\n");
675
676     BT_INFO_FUNC("Reset bt_voice =>\n");
677     mtk_wcn_stp_send_data(bt_voice, 6, BT_TASK_INDX);
678     msleep(BT_CMD_DELAY_MS_COMM);
679     BT_INFO_FUNC("Reset bt_voice <=\n");
680
681     BT_INFO_FUNC("Reset bt_codec =>\n");
682     mtk_wcn_stp_send_data(bt_codec, 8, BT_TASK_INDX);
683     msleep(BT_CMD_DELAY_MS_COMM);
684     BT_INFO_FUNC("Reset bt_codec <=\n");
685
686     BT_INFO_FUNC("Reset bt_radio =>\n");
687     mtk_wcn_stp_send_data(bt_radio, 10, BT_TASK_INDX);
688     msleep(BT_CMD_DELAY_MS_COMM);
689     BT_INFO_FUNC("Reset bt_radio <=\n");
690
691     BT_INFO_FUNC("Reset bt_tx_pwr_offset =>\n");
692     mtk_wcn_stp_send_data(bt_tx_pwr_offset, 7, BT_TASK_INDX);
693     msleep(BT_CMD_DELAY_MS_COMM);
694     BT_INFO_FUNC("Reset bt_tx_pwr_offset <=\n");
695
696     BT_INFO_FUNC("Reset bt_sleep =>\n");
697     mtk_wcn_stp_send_data(bt_sleep, 11, BT_TASK_INDX);
698     msleep(BT_CMD_DELAY_MS_COMM);
699     BT_INFO_FUNC("Reset bt_sleep <=\n");
700
701     BT_INFO_FUNC("Reset bt_feature =>\n");
702     mtk_wcn_stp_send_data(bt_feature, 6, BT_TASK_INDX);
703     msleep(BT_CMD_DELAY_MS_COMM);
704     BT_INFO_FUNC("Reset bt_feature <=\n");
705
706     BT_INFO_FUNC("Reset bt_OSC =>\n");
707     mtk_wcn_stp_send_data(bt_OSC, 9, BT_TASK_INDX);
708     msleep(BT_CMD_DELAY_MS_COMM);
709     BT_INFO_FUNC("Reset bt_OSC <=\n");
710
711     BT_INFO_FUNC("Reset bt_LPO =>\n");
712     mtk_wcn_stp_send_data(bt_LPO, 14, BT_TASK_INDX);
713     msleep(BT_CMD_DELAY_MS_COMM);
714     BT_INFO_FUNC("Reset bt_LPO <=\n");
715
716     BT_INFO_FUNC("Reset bt_legacy_PTA =>\n");
717     mtk_wcn_stp_send_data(bt_legacy_PTA, 14, BT_TASK_INDX);
718     msleep(BT_CMD_DELAY_MS_COMM);
719     BT_INFO_FUNC("Reset bt_legacy_PTA <=\n");
720
721     BT_INFO_FUNC("Reset bt_BLE_PTA =>\n");
722     mtk_wcn_stp_send_data(bt_BLE_PTA, 9, BT_TASK_INDX);
723     msleep(BT_CMD_DELAY_MS_COMM);
724     BT_INFO_FUNC("Reset bt_BLE_PTA <=\n");
725
726     BT_INFO_FUNC("Reset bt_RF_desence =>\n");
727     mtk_wcn_stp_send_data(bt_RF_desence, 10, BT_TASK_INDX);
728     msleep(BT_CMD_DELAY_MS_COMM);
729     BT_INFO_FUNC("Reset bt_RF_desence <=\n");
730
731     BT_INFO_FUNC("Reset bt_reset =>\n");
732     mtk_wcn_stp_send_data(bt_reset, 4, BT_TASK_INDX);
733     msleep(BT_CMD_DELAY_MS_RESET);
734     BT_INFO_FUNC("Reset bt_reset <=\n");
735
736     BT_INFO_FUNC("Reset bt_intern_PTA_1 =>\n");
737     mtk_wcn_stp_send_data(bt_intern_PTA_1, 19, BT_TASK_INDX);
738     msleep(BT_CMD_DELAY_MS_COMM);
739     BT_INFO_FUNC("Reset bt_intern_PTA_1 <=\n");
740
741     BT_INFO_FUNC("Reset bt_intern_PTA_2 =>\n");
742     mtk_wcn_stp_send_data(bt_intern_PTA_2, 11, BT_TASK_INDX);
743     msleep(BT_CMD_DELAY_MS_COMM);
744     BT_INFO_FUNC("Reset bt_intern_PTA_2 <=\n");
745
746     BT_INFO_FUNC("Reset bt_SLP_control_reg =>\n");
747     mtk_wcn_stp_send_data(bt_SLP_control_reg, 12, BT_TASK_INDX);
748     msleep(BT_CMD_DELAY_MS_COMM);
749     BT_INFO_FUNC("Reset bt_SLP_control_reg <=\n");
750
751     BT_INFO_FUNC("Reset bt_SLP_LDOD_reg =>\n");
752     mtk_wcn_stp_send_data(bt_SLP_LDOD_reg, 12, BT_TASK_INDX);
753     msleep(BT_CMD_DELAY_MS_COMM);
754     BT_INFO_FUNC("Reset bt_SLP_LDOD_reg <=\n");
755
756     BT_INFO_FUNC("Reset bt_RF_reg =>\n");
757     mtk_wcn_stp_send_data(bt_RF_reg, 10, BT_TASK_INDX);
758     msleep(BT_CMD_DELAY_MS_COMM);
759     BT_INFO_FUNC("Reset bt_RF_reg <=\n");
760
761     return 0;
762 }
763
764 #elif (HCI_STP_DEV_INIT == HCI_STP_DEV_INIT_THRD)
765
766 void hci_stp_dev_init_rx_cb (const UINT8 *data, INT32 count)
767 {
768     struct hci_stp *hu;
769     unsigned int idx;
770
771     if (unlikely(!hdev)) {
772         BT_ERR_FUNC("null hdev!\n");
773         return;
774     }
775     if (unlikely(!hdev->driver_data)) {
776         BT_ERR_FUNC("null hdev->driver_data!\n");
777         return;
778     }
779
780     /* get hci_stp from global variable */
781     hu = (struct hci_stp *)hdev->driver_data;
782     idx = hu->init_cmd_idx;
783
784     if (unlikely(count != init_table[idx].evtSz)) {
785         hu->init_evt_rx_flag = -1; /* size mismatch */
786     }
787     else if (unlikely(memcmp(data, init_table[idx].hci_evt, count))) {
788         hu->init_evt_rx_flag = -2; /* content mismatch */
789     }
790     else {
791         hu->init_evt_rx_flag = 1; /* ok */
792         BT_DBG_FUNC("EVT i(%d),rx(%d) ok\n", idx, count);
793     }
794
795     if (unlikely(1 != hu->init_evt_rx_flag)) {
796         int i;
797         BT_WARN_FUNC("EVT i(%d),rx(%d)buf:[", idx, count);
798         for (i = 0; i < count; ++i) {
799             printk("0x%02x ", data[i]);
800         }
801         printk("]\n");
802         BT_WARN_FUNC("EVT i(%d),exp(%d)buf:[", idx, init_table[idx].evtSz);
803         for (i = 0; i < count; ++i) {
804             printk("0x%02x ", init_table[idx].hci_evt[i]);
805         }
806         printk("]\n");
807
808     }
809
810     smp_mb(); /* sync shared data */
811     wake_up(hu->p_init_evt_wq); /* wake up dev_init_work */
812 }
813
814 static void hci_stp_dev_init_work (struct work_struct *work)
815 {
816     struct hci_stp *phu;
817     unsigned int idx;
818     long ret;
819     long to;
820
821     BT_DBG_FUNC("++\n");
822
823     /* get client's information */
824     phu = container_of(work, struct hci_stp, init_work);
825
826     for (idx = 0; idx < ARRAY_SIZE(init_table); ++idx) {
827         phu->init_cmd_idx = idx;
828         phu->init_evt_rx_flag = 0;
829         to = (init_table[idx].hci_cmd == bt_reset) ? BT_CMD_DELAY_MS_RESET : BT_CMD_DELAY_MS_COMM;
830         /* safe waiting time in case running on a busy system */
831         to = msecs_to_jiffies(to * BT_CMD_DELAY_SAFE_GUARD);
832
833         BT_DBG_FUNC("CMD(%d),(%s),t/o(%ld))=>\n", idx, init_table[idx].str, to);
834         smp_wmb(); /* sync shared data */
835
836         /* Send hci command */
837         mtk_wcn_stp_send_data(init_table[idx].hci_cmd, init_table[idx].cmdSz, BT_TASK_INDX);
838         /* Wait rx hci event */
839         /* no need to lock init_lock here for wq, for that it will be freed
840          * only after we call complete(phu->p_init_comp); in this function.
841          */
842         ret = wait_event_timeout((*phu->p_init_evt_wq), phu->init_evt_rx_flag != 0, to);
843
844         /* Check result */
845         if (likely(1 == phu->init_evt_rx_flag)) {
846             BT_DBG_FUNC("EVT(%d) ret(%u) ok<=\n", idx, jiffies_to_msecs(ret));
847             /* process next cmd */
848             continue;
849         }
850         else {
851             BT_WARN_FUNC("EVT(%d) ret(%u) to(%ld) fg(%d)<=\n",
852                 idx, jiffies_to_msecs(ret), to, phu->init_evt_rx_flag);
853             /* stop processing and skip next cmd */
854             break;
855         }
856     }
857
858     if (phu->p_init_comp) {
859         complete(phu->p_init_comp);
860     }
861
862     BT_DBG_FUNC("--\n");
863 }
864
865 static int hci_stp_dev_init (struct hci_stp *phu)
866 {
867     DECLARE_COMPLETION_ONSTACK(hci_stp_dev_init_comp);
868     DECLARE_WAIT_QUEUE_HEAD_ONSTACK(hci_stp_dev_init_wq);
869
870     spin_lock(&phu->init_lock);
871     phu->p_init_comp = &hci_stp_dev_init_comp;
872     phu->p_init_evt_wq = &hci_stp_dev_init_wq;
873     spin_unlock(&phu->init_lock);
874
875     /* unregister rx event callback */
876     mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
877     /* register direct rx callback for init only */
878     mtk_wcn_stp_register_if_rx(hci_stp_dev_init_rx_cb);
879     /* use bluez mode */
880     mtk_wcn_stp_set_bluez(1);
881
882     /* Schedule to call hci_stp_dev_init_work(). init_work is initialized in
883      * hci_stp_init().
884      */
885     schedule_work(&phu->init_work);
886
887     //ret = wait_for_completion_interruptible_timeout(&hci_stp_dev_init_comp, msecs_to_jiffies(HCI_STP_INIT_TO_MSEC)); /* 3 seconds? */
888     wait_for_completion(&hci_stp_dev_init_comp);
889
890     spin_lock(&phu->init_lock);
891     /* clear references to stack variables */
892     phu->p_init_comp = NULL;
893     phu->p_init_evt_wq = NULL;
894     spin_unlock(&phu->init_lock);
895
896     /* check result */
897     /* flag: (to be replaced by a constant value)
898         1 rx event correctly,
899         0 no response in time,
900         -1 unequal rx event length,
901         -2 unequal rx event content.
902     */
903     if (likely(1 == phu->init_evt_rx_flag)) {
904         return 0;
905     }
906     else {
907         /* return non-zero value for error */
908         return (phu->init_evt_rx_flag + 256);
909     }
910 }
911 #else
912 #error "Not implemented HCI_STP_DEV_INIT"
913 #endif
914
915
916 static ssize_t file_read(char *filename, char *buf, size_t len, loff_t *offset)
917 {
918     struct file *fp;
919     mm_segment_t old_fs;
920     ssize_t retLen;
921
922     fp = filp_open(filename, O_RDONLY, 0);
923     if (IS_ERR(fp)) {
924         BT_ERR_FUNC("Failed to open %s!\n", filename);
925         return -1;
926     }
927
928     old_fs = get_fs();
929     set_fs(KERNEL_DS);
930
931     if ((fp->f_op == NULL) || (fp->f_op->read == NULL)){
932         BT_ERR_FUNC("File can not be read!\n");
933         set_fs(old_fs);
934         filp_close(fp, NULL);
935         return -1;
936     }
937
938     retLen = fp->f_op->read(fp, buf, len, offset);
939
940     set_fs(old_fs);
941     filp_close(fp, NULL);
942
943     return retLen;
944 }
945
946 static ssize_t file_write(char *filename, char *buf, size_t len, loff_t *offset)
947 {
948     struct file *fp;
949     mm_segment_t old_fs;
950     ssize_t retLen;
951
952     fp = filp_open(filename, O_WRONLY | O_CREAT, 0644);
953     if (IS_ERR(fp)) {
954         BT_ERR_FUNC("Failed to open %s!\n", filename);
955         return -1;
956     }
957
958     old_fs = get_fs();
959     set_fs(KERNEL_DS);
960
961     if ((fp->f_op == NULL) || (fp->f_op->write == NULL)){
962         BT_ERR_FUNC("File can not be write!\n");
963         set_fs(old_fs);
964         filp_close(fp, NULL);
965         return -1;
966     }
967
968     retLen = fp->f_op->write(fp, buf, len, offset);
969
970     set_fs(old_fs);
971     filp_close(fp, NULL);
972
973     return retLen;
974 }
975
976 int load_custom_bt_conf(struct btradio_conf_data *cfg)
977 {
978    /*
979     This method depends on customer's platform configuration data
980     store machenism.
981     Customer may use NVRAM, data file, or other patterns.
982     Here RECOMMEND and GIVE AN EXAMPLE to push configuration data
983     under /data/BT.cfg
984     */
985
986     struct btradio_conf_data temp;
987     loff_t pos = 0;
988     ssize_t retLen;
989
990     retLen = file_read(CUSTOM_BT_CFG_FILE,
991                        (char*)&temp,
992                        sizeof(temp),
993                        &pos);
994
995     if (retLen < 0)
996         return -1;
997
998     if(retLen < sizeof(temp)){
999         BT_ERR_FUNC("File read length error %d\n", retLen);
1000         return -1;
1001     }
1002
1003     memcpy(cfg, &temp, retLen);
1004
1005     return 0;
1006 }
1007
1008 int load_internal_bt_conf(struct btradio_conf_data *cfg)
1009 {
1010     struct btradio_conf_data temp;
1011     loff_t pos = 0;
1012     ssize_t retLen;
1013     ssize_t written;
1014
1015     // default BD_ADDR
1016     unsigned char addr[6] = {0x00, 0x00, 0x46, 0x66, 0x20, 0x01};
1017     unsigned long randNum;
1018
1019     retLen = file_read(INTERNAL_BT_CFG_FILE,
1020                        (char*)&temp,
1021                        sizeof(temp),
1022                        &pos);
1023
1024     if (retLen < 0){
1025         BT_INFO_FUNC("No internal BT config file, generate from built-in BT config\n");
1026
1027         pos = 0;
1028         retLen = file_read(BUILTIN_BT_CFG_FILE,
1029                            (char*)&temp,
1030                            sizeof(temp),
1031                            &pos);
1032         if (retLen < 0){
1033             BT_ERR_FUNC("Try to read built-in BT config, error\n");
1034             return -1;
1035         }
1036         else if(retLen < sizeof(temp)){
1037             BT_ERR_FUNC("File read length error %d\n", retLen);
1038             return -1;
1039         }
1040
1041         if (0 == memcmp(temp.addr, addr, 6)){
1042             BT_DBG_FUNC("Built-in config use default BD address, enable auto-gen mechanism!\n");
1043             get_random_bytes(&randNum, sizeof(unsigned long));
1044             BT_DBG_FUNC("Get random number: %lu\n", randNum);
1045
1046             temp.addr[0] = (((randNum>>24 | randNum>>16) & (0xFE)) | (0x02));
1047             temp.addr[1] = ((randNum>>8) & 0xFF);
1048             temp.addr[5] = (randNum & 0xFF);
1049
1050             // Generate internal BT config file
1051             pos = 0;
1052             written = file_write(INTERNAL_BT_CFG_FILE,
1053                                  (char*)&temp,
1054                                  sizeof(temp),
1055                                  &pos);
1056             if (written < 0){
1057                 BT_ERR_FUNC("Try to create internal BT config, error\n");
1058             }
1059             else if(written < sizeof(temp)){
1060                 BT_ERR_FUNC("File write length error %d\n", written);
1061             }
1062             else{
1063                 BT_INFO_FUNC("Internal BT config generated\n");
1064             }
1065         }
1066     }
1067     else if(retLen < sizeof(temp)){
1068         BT_ERR_FUNC("File read length error %d\n", retLen);
1069         return -1;
1070     }
1071
1072     memcpy(cfg, &temp, retLen);
1073
1074     return 0;
1075 }
1076
1077 /* Invoked when there is ONE received BT packet */
1078 void stp_tx_event_cb(void)
1079 {
1080 #if 0
1081     struct hci_stp *hu;
1082     if (unlikely(hdev == NULL)) {
1083         BT_ERR_FUNC("Null hdev!\n");
1084         BUG_ON(hdev == NULL);
1085         return;
1086     }
1087     hu = (struct hci_stp *) hdev->driver_data;
1088 #endif
1089     /* George: [FixMe] can we call hci_stp_tx_wakeup() directly in STP-CORE's
1090      * context? It seems to be dangerous! Replace it with suitable one according
1091      * to HCI_STP_TX compile flag.
1092      */
1093     hci_stp_tx_kick(); /* George: adapt different tx_kick function */
1094 }
1095
1096 /*
1097   Direct delivery of bluez not changed hands through the stp buffer
1098 */
1099 void stp_rx_event_cb_directly(const UINT8 *data, INT32 count)
1100 {
1101     register const UINT8 *ptr;
1102     struct hci_event_hdr *eh;
1103     struct hci_acl_hdr   *ah;
1104     struct hci_sco_hdr   *sh;
1105     register int len, type, dlen;
1106     int while_count; /* = 0; Is while_count redundant? */
1107     //static unsigned long rx_count; /* Is it ok w/o an initial value??? */
1108     static unsigned int rx_count = 0;
1109     //static unsigned long rx_state; /* Is it ok w/o an initial value??? */
1110     static unsigned int rx_state = H4_W4_PACKET_TYPE;
1111     struct  sk_buff *rx_skb = NULL; /* Is it ok to use non-static skb??? */
1112     register int room;
1113 #if HCI_STP_SAFE_RESET
1114     struct hci_stp *hu;
1115 #endif
1116
1117     BT_LOUD_FUNC("count(%d)rx_state(%d)rx_count(%d)\n",
1118         count, rx_state, rx_count);
1119
1120     if (data == NULL) {
1121         BT_ERR_FUNC("Data is Null\n");
1122         return;
1123     }
1124
1125     if (count > 5000) {
1126         BT_WARN_FUNC("abnormal count(%d)\n", count);
1127     }
1128
1129     ptr = data;
1130     /*Add statistic*/
1131     hdev->stat.byte_rx += count;
1132
1133 #if HCI_STP_SAFE_RESET
1134     hu = (struct hci_stp *)hdev->driver_data;
1135     /* is waiting hci reset event? */
1136     if (unlikely(atomic_read(&hu->reset_count))) {
1137         if (count == ARRAY_SIZE(bt_reset_evt)) {
1138             if (!memcmp(bt_reset_evt, data, ARRAY_SIZE(bt_reset_evt))) {
1139                 BT_DBG_FUNC("hci reset evt,f(%d),c(%d)\n",
1140                     BT_GET_HDEV_RST_FG(hdev),
1141                     atomic_read(&hu->reset_count));
1142
1143                 atomic_dec(&hu->reset_count);
1144                 wake_up(&hu->reset_wq);
1145             }
1146         }
1147     }
1148 #endif
1149
1150     while_count = 0;
1151     while (count > 0) { /* while (count) */
1152         /* Is while_count redundant? */
1153         //while_count++;
1154         if (++while_count > 5000) {
1155             BT_WARN_FUNC("abnormal while_count(%d)\n", while_count);
1156         }
1157
1158         if (rx_count) {
1159             len = min_t(unsigned int, rx_count, count);
1160             memcpy(skb_put(rx_skb, len), ptr, len);
1161             rx_count -= len;
1162             count -= len;
1163             ptr += len;
1164
1165             if (rx_count)
1166                 continue;
1167             /* rx_count==0, ready to indicate to hci_core */
1168
1169             switch (rx_state) {
1170             case H4_W4_DATA:
1171                 BT_LOUD_FUNC("Complete data\n");
1172                 hci_recv_frame(rx_skb);
1173                 rx_state = H4_W4_PACKET_TYPE;
1174                 rx_skb = NULL;
1175                 continue;
1176
1177             case H4_W4_EVENT_HDR:
1178                 eh = hci_event_hdr(rx_skb);
1179                 //BT_DBG_FUNC("Event header:evt(0x%2.2x)plen(%d)\n", eh->evt, eh->plen);
1180                 room = skb_tailroom(rx_skb);
1181                 //BT_DBG_FUNC("len(%d)room(%d)\n", eh->plen, room);
1182                 BT_LOUD_FUNC("Event header:evt(0x%2.2x)plen(%d)room(%d)\n",
1183                     eh->evt, eh->plen, room);
1184
1185                 if (!eh->plen) {
1186                     hci_recv_frame(rx_skb);
1187                     rx_state = H4_W4_PACKET_TYPE;
1188                     rx_skb   = NULL;
1189                     rx_count = 0; /* redundant? here rx_count is 0 already */
1190                 }
1191                 else if (eh->plen > room) {
1192                     //BT_ERR("Data length is too large\n");
1193                     BT_ERR_FUNC("too large data length:eh->plen(%d)>room(%d)\n",
1194                         eh->plen, room);
1195                     kfree_skb(rx_skb);
1196                     rx_state = H4_W4_PACKET_TYPE;
1197                     rx_skb = NULL;
1198                     rx_count = 0; /* redundant? here rx_count is 0 already */
1199                 }
1200                 else {
1201                     rx_state = H4_W4_DATA;
1202                     rx_count = eh->plen;
1203                 }
1204                 continue;
1205
1206             case H4_W4_ACL_HDR:
1207                 ah = hci_acl_hdr(rx_skb);
1208                 dlen = __le16_to_cpu(ah->dlen);
1209
1210                 room = skb_tailroom(rx_skb);
1211                 BT_LOUD_FUNC("ACL header:dlen(%d)room(%d)\n", dlen, room);
1212                 if (!dlen) {
1213                     hci_recv_frame(rx_skb);
1214                     rx_state = H4_W4_PACKET_TYPE;
1215                     rx_skb = NULL;
1216                     rx_count = 0;
1217                 }
1218                 else if (dlen > room) {
1219                     //BT_ERR_FUNC("Data length is too large\n");
1220                     BT_ERR_FUNC("too large data length:dlen(%d)>room(%d)\n",
1221                         dlen, room);
1222                     kfree_skb(rx_skb);
1223                     rx_state = H4_W4_PACKET_TYPE;
1224                     rx_skb = NULL;
1225                     rx_count = 0;
1226                 }
1227                 else {
1228                     rx_state = H4_W4_DATA;
1229                     rx_count = dlen;
1230                 }
1231                 continue;
1232
1233             case H4_W4_SCO_HDR:
1234                 sh = hci_sco_hdr(rx_skb);
1235                 room = skb_tailroom(rx_skb);
1236                 BT_LOUD_FUNC("SCO header:dlen(%d)room(%d)\n", sh->dlen, room);
1237
1238                 if (!sh->dlen) {
1239                     hci_recv_frame(rx_skb);
1240                     rx_state = H4_W4_PACKET_TYPE;
1241                     rx_skb = NULL;
1242                     rx_count = 0;
1243                 }
1244                 else if (sh->dlen > room) {
1245                     BT_ERR_FUNC("Data length is too large\n");
1246                     BT_ERR_FUNC("too large data length:sh->dlen(%d)>room(%d)\n",
1247                         sh->dlen , room);
1248                     kfree_skb(rx_skb);
1249                     rx_state = H4_W4_PACKET_TYPE;
1250                     rx_skb = NULL;
1251                     rx_count = 0;
1252                 }
1253                 else {
1254                     rx_state = H4_W4_DATA;
1255                     rx_count = sh->dlen;
1256                 }
1257                 continue;
1258             }
1259         }
1260
1261         /* H4_W4_PACKET_TYPE */
1262         switch (*ptr) {
1263             case HCI_EVENT_PKT:
1264                 BT_LOUD_FUNC("Event packet\n");
1265                 rx_state = H4_W4_EVENT_HDR;
1266                 rx_count = HCI_EVENT_HDR_SIZE;
1267                 type = HCI_EVENT_PKT;
1268                 break;
1269
1270             case HCI_ACLDATA_PKT:
1271                 BT_LOUD_FUNC("ACL packet\n");
1272                 rx_state = H4_W4_ACL_HDR;
1273                 rx_count = HCI_ACL_HDR_SIZE;
1274                 type = HCI_ACLDATA_PKT;
1275                 break;
1276
1277             case HCI_SCODATA_PKT:
1278                 BT_LOUD_FUNC("SCO packet\n");
1279                 rx_state = H4_W4_SCO_HDR;
1280                 rx_count = HCI_SCO_HDR_SIZE;
1281                 type = HCI_SCODATA_PKT;
1282                 break;
1283
1284             default:
1285                 BT_ERR_FUNC("Unknown HCI packet type %2.2x\n", (__u8)*ptr);
1286                 ++(hdev->stat.err_rx);
1287                 ++ptr;
1288                 --count;
1289                 continue;
1290         };
1291
1292         ++ptr;
1293         --count;
1294
1295         /* Allocate packet */
1296         rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1297         if (!rx_skb) {
1298            BT_ERR_FUNC("bt_skb_alloc(%d, GFP_ATOMIC) fail!\n", HCI_MAX_FRAME_SIZE);
1299            rx_state = H4_W4_PACKET_TYPE;
1300            rx_count = 0;
1301            return;
1302         }
1303
1304         rx_skb->dev = (void *) hdev;
1305         bt_cb(rx_skb)->pkt_type = type;
1306     }
1307
1308     return;
1309 }
1310
1311 /* ------- Interface to HCI layer ------ */
1312 /* Initialize device */
1313 static int hci_stp_open(struct hci_dev *hdev)
1314 {
1315     struct hci_stp *hu;
1316     struct btradio_conf_data cfg;
1317     int ret;
1318
1319     if (unlikely(!hdev)) {
1320         BT_ERR_FUNC("null hdev\n");
1321         return -ENODEV;
1322     }
1323
1324     if (unlikely(!hdev->driver_data)) {
1325         BT_ERR_FUNC("null hdev\n");
1326         return -ENODEV;
1327     }
1328
1329     hu = (struct hci_stp *)hdev->driver_data;
1330     BT_INFO_FUNC("%s(0x%p)\n", hdev->name, hdev);
1331
1332     /* clear txq and free all skb in it */
1333     hci_stp_txq_lock(HCI_STP_TXQ_IN_BLZ);
1334     skb_queue_purge(&hu->txq);
1335     hci_stp_txq_unlock(HCI_STP_TXQ_IN_BLZ);
1336
1337     /* turn on BT */
1338     if (unlikely(MTK_WCN_BOOL_FALSE == mtk_wcn_wmt_func_on(WMTDRV_TYPE_BT))) {
1339         BT_WARN_FUNC("WMT turn on BT fail!\n");
1340         return -ENODEV;
1341     }
1342
1343     BT_INFO_FUNC("WMT turn on BT OK!\n");
1344
1345     if (likely(mtk_wcn_stp_is_ready())) {
1346
1347         BT_LOUD_FUNC("STP is ready!\n");
1348
1349         memset(&cfg, 0, sizeof(struct btradio_conf_data));
1350         if (load_custom_bt_conf(&cfg) < 0){
1351             BT_INFO_FUNC("No custom BT config\n");
1352
1353             if (load_internal_bt_conf(&cfg) < 0){
1354                 BT_ERR_FUNC("Load internal BT config failed!\n");
1355                 goto init_start;
1356             }
1357             else{
1358                 BT_INFO_FUNC("Load internal BT config success\n");
1359             }
1360         }
1361         else{
1362             BT_INFO_FUNC("Load custom BT config success\n");
1363         }
1364 #if 1 /* refine output format */
1365         BT_DBG_FUNC("Read BT config data:\n");
1366         BT_DBG_FUNC("[BD addr %02x-%02x-%02x-%02x-%02x-%02x]\n",
1367             cfg.addr[0], cfg.addr[1], cfg.addr[2], cfg.addr[3], cfg.addr[4], cfg.addr[5]);
1368         BT_DBG_FUNC("[voice %02x %02x][codec %02x %02x %02x %02x]\n",
1369             cfg.voice[0], cfg.voice[1],
1370             cfg.codec[0], cfg.codec[1], cfg.codec[2], cfg.codec[3]);
1371         BT_DBG_FUNC("[radio %02x %02x %02x %02x %02x %02x]\n",
1372             cfg.radio[0], cfg.radio[1], cfg.radio[2], cfg.radio[3], cfg.radio[4], cfg.radio[5]);
1373         BT_DBG_FUNC("[sleep %02x %02x %02x %02x %02x %02x %02x]\n",
1374             cfg.sleep[0], cfg.sleep[1], cfg.sleep[2], cfg.sleep[3], cfg.sleep[4], cfg.sleep[5], cfg.sleep[6]);
1375         BT_DBG_FUNC("[feature %02x %02x][tx power offset %02x %02x %02x]\n",
1376             cfg.feature[0], cfg.feature[1],
1377             cfg.tx_pwr_offset[0], cfg.tx_pwr_offset[1], cfg.tx_pwr_offset[2]);
1378 #else
1379         BT_DBG_FUNC("Read BT config data: [BD addr %02x-%02x-%02x-%02x-%02x-%02x][voice %02x %02x] \
1380                     [codec %02x %02x %02x %02x][radio %02x %02x %02x %02x %02x %02x] \
1381                     [sleep %02x %02x %02x %02x %02x %02x %02x][feature %02x %02x] \
1382                     [tx power offset %02x %02x %02x]\n",
1383                     cfg.addr[0], cfg.addr[1], cfg.addr[2], cfg.addr[3], cfg.addr[4], cfg.addr[5],
1384                     cfg.voice[0], cfg.voice[1], cfg.codec[0], cfg.codec[1], cfg.codec[2], cfg.codec[3],
1385                     cfg.radio[0], cfg.radio[1], cfg.radio[2], cfg.radio[3], cfg.radio[4], cfg.radio[5],
1386                     cfg.sleep[0], cfg.sleep[1], cfg.sleep[2], cfg.sleep[3], cfg.sleep[4], cfg.sleep[5], cfg.sleep[6],
1387                     cfg.feature[0], cfg.feature[1],
1388                     cfg.tx_pwr_offset[0], cfg.tx_pwr_offset[1], cfg.tx_pwr_offset[2]);
1389 #endif
1390         bt_bd_addr[4] = cfg.addr[5];
1391         bt_bd_addr[5] = cfg.addr[4];
1392         bt_bd_addr[6] = cfg.addr[3];
1393         bt_bd_addr[7] = cfg.addr[2];
1394         bt_bd_addr[8] = cfg.addr[1];
1395         bt_bd_addr[9] = cfg.addr[0];
1396         memcpy(&bt_voice[4], cfg.voice, 2);
1397         memcpy(&bt_codec[4], cfg.codec, 4);
1398         memcpy(&bt_radio[4], cfg.radio, 6);
1399         memcpy(&bt_tx_pwr_offset[4], cfg.tx_pwr_offset, 3);
1400         memcpy(&bt_sleep[4], cfg.sleep, 7);
1401         memcpy(&bt_feature[4], cfg.feature, 2);
1402
1403 init_start:
1404
1405         ret = hci_stp_dev_init(hu);
1406         /* error handling: turn of BT */
1407         if (unlikely(ret)) {
1408             BT_WARN_FUNC("hci_stp_dev_init fail(%d)!\n", ret);
1409             if (MTK_WCN_BOOL_FALSE == mtk_wcn_wmt_func_off(WMTDRV_TYPE_BT)) {
1410                 BT_WARN_FUNC("WMT turn off BT fail!\n");
1411             }
1412             else {
1413                 BT_INFO_FUNC("WMT turn off BT ok!\n");
1414             }
1415             return -ENODEV;
1416         }
1417
1418         BT_INFO_FUNC("hci_stp_dev_init ok\n");
1419
1420         set_bit(HCI_RUNNING, &hdev->flags);
1421
1422         /*registered tx/rx path*/
1423         mtk_wcn_stp_register_if_rx(stp_rx_event_cb_directly);
1424
1425         mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
1426         mtk_wcn_stp_register_tx_event_cb(BT_TASK_INDX, stp_tx_event_cb);
1427
1428         /*use bluez*/
1429         mtk_wcn_stp_set_bluez(1);
1430
1431         return 0;
1432     }
1433     else {
1434         BT_WARN_FUNC("STP is not ready!\n");
1435         return -ENODEV;
1436     }
1437 }
1438
1439 /* Reset device */
1440 static int hci_stp_flush(struct hci_dev *hdev)
1441 {
1442     struct hci_stp *hu;
1443
1444     BT_DBG_FUNC("start\n");
1445     if (!hdev || !hdev->driver_data) {
1446         BT_WARN_FUNC("invalid hdev(0x%p) or drv data(0x%p)\n",
1447             hdev, (hdev) ? hdev->driver_data : hdev);
1448         return -EFAULT;
1449     }
1450
1451     hu = (struct hci_stp *)hdev->driver_data;
1452
1453     /* clear txq and free all skb in it */
1454     hci_stp_txq_lock(HCI_STP_TXQ_IN_BLZ);
1455     skb_queue_purge(&hu->txq);
1456     hci_stp_txq_unlock(HCI_STP_TXQ_IN_BLZ);
1457
1458     BT_INFO_FUNC("done\n");
1459     return 0;
1460 }
1461
1462 /* Close device */
1463 static int hci_stp_close(struct hci_dev *hdev)
1464 {
1465 #if HCI_STP_SAFE_RESET
1466     struct hci_stp *hu;
1467     long ret;
1468 #endif
1469
1470     BT_INFO_FUNC("hdev(0x%p)\n", hdev);
1471
1472     if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
1473           return 0;
1474     }
1475
1476     hci_stp_flush(hdev);
1477     hdev->flush = NULL;
1478
1479 #if HCI_STP_SAFE_RESET
1480     hu = (struct hci_stp *)hdev->driver_data;
1481     if (hu) {
1482         /* double waiting time in case of busy system */
1483         ret = wait_event_timeout((hu->reset_wq),
1484             (atomic_read(&hu->reset_count) == 0),
1485             msecs_to_jiffies(BT_CMD_DELAY_MS_RESET*2));
1486         if ( (!ret) || (atomic_read(&hu->reset_count) != 0) ) {
1487             BT_WARN_FUNC("wait on-going reset finish fail,f(%d),c(%d),ret(%u)\n",
1488                 BT_GET_HDEV_RST_FG(hdev),
1489                 atomic_read(&hu->reset_count),
1490                 jiffies_to_msecs(ret));
1491         }
1492         else {
1493             BT_DBG_FUNC("check reset log,f(%d),c(%d),ret(%u)\n",
1494                 BT_GET_HDEV_RST_FG(hdev),
1495                 atomic_read(&hu->reset_count),
1496                 jiffies_to_msecs(ret));
1497         }
1498     }
1499 #endif
1500
1501     /* clear txq and free all skb in it */
1502     hci_stp_txq_lock(HCI_STP_TXQ_IN_BLZ);
1503     skb_queue_purge(&hu->txq);
1504     hci_stp_txq_unlock(HCI_STP_TXQ_IN_BLZ);
1505
1506     /*unregistered tx/rx path*/
1507     mtk_wcn_stp_register_if_rx(NULL);
1508
1509     mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
1510     mtk_wcn_stp_register_tx_event_cb(BT_TASK_INDX, NULL);
1511
1512     /*not use bluez*/
1513     mtk_wcn_stp_set_bluez(0);
1514
1515     if (MTK_WCN_BOOL_FALSE == mtk_wcn_wmt_func_off(WMTDRV_TYPE_BT)) {
1516         BT_WARN_FUNC("WMT turn off BT fail!\n");
1517     }
1518     else {
1519         BT_INFO_FUNC("WMT turn off BT OK!\n");
1520     }
1521
1522     return 0;
1523 }
1524
1525 /* Send frames from HCI layer */
1526 static int hci_stp_send_frame(struct sk_buff *skb)
1527 {
1528     struct hci_dev* hdev = (struct hci_dev *) skb->dev;
1529     struct hci_stp *hu;
1530
1531     BT_TRC_FUNC();
1532     if (!hdev) {
1533         BT_ERR_FUNC("Null hdev in skb\n");
1534         return -ENODEV;
1535     }
1536
1537     if (!test_bit(HCI_RUNNING, &hdev->flags)) {
1538         BT_ERR_FUNC("no HCI_RUNNING in hdev->flags(0x%lx)\n", hdev->flags);
1539         return -EBUSY;
1540     }
1541
1542     hu = (struct hci_stp *) hdev->driver_data;
1543
1544     BT_LOUD_FUNC("%s:type(%d)len(%d)\n",
1545         hdev->name, bt_cb(skb)->pkt_type, skb->len);
1546
1547 #if 0 /* just timestamp?? */
1548     if (gHciStpDbgLevel >= BT_LOG_DBG)
1549     {
1550         struct timeval now;
1551         do_gettimeofday(&now);
1552         printk("%s:  sec = %ld, --> usec --> %ld\n",
1553              __FUNCTION__, now.tv_sec, now.tv_usec);
1554     }
1555 #endif
1556
1557     /* Prepend skb with frame type. Is it safe to do skb_push? */
1558     memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1559
1560     /* George: is no lock ok?? Add hci_stp_txq_lock and unlock! */
1561     hci_stp_txq_lock(HCI_STP_TXQ_IN_BLZ);
1562     /*Queue a buffer at the end of a list. This function takes no locks
1563     *  and you must therefore hold required locks before calling it.
1564     */
1565     skb_queue_tail(&hu->txq, skb);
1566     hci_stp_txq_unlock(HCI_STP_TXQ_IN_BLZ);
1567
1568     /* George: adapt different tx_kick function */
1569     hci_stp_tx_kick();
1570
1571     return 0;
1572 }
1573
1574 static void hci_stp_destruct(struct hci_dev *hdev)
1575 {
1576     BT_INFO_FUNC("start hdev(0x%p)\n", hdev);
1577     if (!hdev) {
1578         return;
1579     }
1580
1581     BT_DBG_FUNC("%s\n", hdev->name);
1582     hci_stp_tx_deinit((struct hci_stp *)hdev->driver_data);
1583     kfree(hdev->driver_data);
1584
1585     BT_DBG_FUNC("done\n");
1586 }
1587
1588 static int __init hci_stp_init(void)
1589 {
1590     struct hci_stp *hu = NULL;
1591
1592     BT_DBG_FUNC("HCI STP driver ver %s\n", VERSION);
1593
1594     if (!(hu = kzalloc(sizeof(struct hci_stp), /*GFP_KERNEL*/GFP_ATOMIC))) {
1595         BT_ERR_FUNC("Can't allocate control structure\n");
1596         return -ENOMEM;
1597     }
1598
1599     BT_DBG_FUNC("hu 0x%08x\n", (int)hu);
1600
1601     /*
1602        used to stored pending skb
1603      */
1604     skb_queue_head_init(&hu->txq);
1605
1606     /*
1607        Initialize and register HCI device
1608      */
1609     hdev = hci_alloc_dev();
1610     if (!hdev) {
1611         if (hu){
1612             kfree(hu);
1613             hu = NULL;
1614         }
1615
1616         BT_ERR_FUNC("Can't allocate HCI device\n");
1617         return -ENOMEM;
1618     }
1619
1620     hu->hdev = hdev;
1621
1622 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34))
1623     hdev->type = HCI_UART;
1624 #else
1625     hdev->bus = HCI_UART;
1626     hdev->dev_type = HCI_BREDR;
1627 #endif
1628     hdev->driver_data = hu;
1629
1630     BT_DBG_FUNC("hdev->driver_data 0x%08x\n", (int)hdev->driver_data);
1631     hdev->open  = hci_stp_open;
1632     hdev->close = hci_stp_close;
1633     hdev->flush = hci_stp_flush;
1634     hdev->send  = hci_stp_send_frame;
1635     hdev->destruct = hci_stp_destruct;
1636
1637     hdev->owner = THIS_MODULE;
1638
1639     BT_DBG_FUNC("HCI_QUIRK_NO_RESET\n");
1640
1641     set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1642
1643     if (hci_register_dev(hdev) < 0) {
1644         BT_ERR_FUNC("Can't register HCI device\n");
1645         kfree(hu);
1646         hu = NULL;
1647         hci_free_dev(hdev);
1648         hdev = NULL;
1649         return -ENODEV;
1650      }
1651
1652 #if HCI_STP_SAFE_RESET
1653     atomic_set(&hu->reset_count, 0);
1654     init_waitqueue_head(&hu->reset_wq);
1655 #endif
1656
1657     /* George: adapt different tx_init function */
1658     hci_stp_tx_init(hu);
1659
1660 #if (HCI_STP_DEV_INIT == HCI_STP_DEV_INIT_THRD)
1661     /* init_work in heap */
1662     INIT_WORK(&hu->init_work, hci_stp_dev_init_work);
1663     spin_lock_init(&hu->init_lock);
1664 #endif
1665
1666     mtk_wcn_stp_register_if_rx(NULL);
1667     mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
1668     mtk_wcn_stp_register_tx_event_cb(BT_TASK_INDX, NULL);
1669
1670     BT_INFO_FUNC("HCI-STP Drv(%s),dev(0x%p),data(0x%p) init done\n", VERSION, hdev, hdev->driver_data);
1671     return 0;
1672 }
1673
1674 static void __exit hci_stp_exit(void)
1675 {
1676     struct hci_stp *hu = (struct hci_stp *)hdev->driver_data;
1677     BT_TRC_FUNC();
1678
1679     mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
1680     mtk_wcn_stp_register_tx_event_cb(BT_TASK_INDX, NULL);
1681
1682     hci_unregister_dev(hdev);
1683
1684     /* George: adapt different tx_deinit function */
1685     //tasklet_kill(&hci_tx_tasklet);
1686
1687     skb_queue_purge(&hu->txq);
1688
1689     /* hci_stp_destruct does this */
1690     /*kfree(hdev->driver_data);*/
1691     hci_free_dev(hdev);
1692
1693     hdev = NULL;
1694     BT_INFO_FUNC("done\n");
1695 }
1696
1697 module_init(hci_stp_init);
1698 module_exit(hci_stp_exit);
1699
1700 module_param(reset, bool, 0644);
1701 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1702
1703 MODULE_AUTHOR("Mediatek Inc.");
1704 MODULE_DESCRIPTION("Bluetooth HCI STP driver ver " VERSION);
1705 MODULE_VERSION(VERSION);
1706 MODULE_LICENSE("GPL");
1707
1708