1 /* Copyright Statement:
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.
10 * MediaTek Inc. (C) 2010. All rights reserved.
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.
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.
38 * Bluetooth HCI UART driver
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>
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.
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.
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
61 /*******************************************************************************
62 * C O M P I L E R F L A G S
63 ********************************************************************************
65 /* move compile flags to hci_stp.h file. */
67 /*******************************************************************************
68 * E X T E R N A L R E F E R E N C E S
69 ********************************************************************************
76 /*******************************************************************************
78 ********************************************************************************
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)
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"
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)
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 */
106 /*******************************************************************************
107 * P U B L I C D A T A
108 ********************************************************************************
110 unsigned int gHciStpDbgLevel = BT_LOG_ERR;//Modify loglevel
112 /*******************************************************************************
113 * P R I V A T E D A T A
114 ********************************************************************************
116 /* Allow one BT driver */
117 static struct hci_dev *hdev = NULL;
118 static int reset = 0;
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;
124 #if (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_RWLOCK)
125 static DEFINE_RWLOCK(hci_stp_txqlock);
127 #elif (HCI_STP_TX_TASKLET_LOCK == HCI_STP_TX_TASKLET_SPINLOCK)
128 static spinlock_t hci_stp_txqlock;
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;
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
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};
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[] =
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),
263 /*******************************************************************************
265 ********************************************************************************
268 /*******************************************************************************
270 ********************************************************************************
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__);}
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
285 #define BT_GET_HDEV_RST_FG(hdev) (test_bit(HCI_RESET, &hdev->flags))
287 #define BT_GET_HDEV_RST_FG(hdev) (0) /* no HCI_RESET bit available */
291 /*******************************************************************************
292 * F U N C T I O N D E C L A R A T I O N S
293 ********************************************************************************
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);
302 /* Functions to be implemented by all HCI_STP_INIT_* methods*/
303 static int hci_stp_dev_init (struct hci_stp *phu);
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);
310 #if (HCI_STP_TX == HCI_STP_TX_THRD)
311 static int hci_stp_tx_thrd_func (void *pdata);
314 /*******************************************************************************
316 ********************************************************************************
319 static inline void hci_stp_tx_skb_comp (struct hci_stp *hu, struct sk_buff *skb)
321 struct hci_dev *hdev;
326 hdev->stat.byte_tx += skb->len;
328 pkt_type = bt_cb(skb)->pkt_type;
329 /* Update HCI stat counters */
331 case HCI_COMMAND_PKT:
335 case HCI_ACLDATA_PKT:
339 case HCI_SCODATA_PKT:
345 #if (HCI_STP_TX == HCI_STP_TX_TASKLET)
346 void hci_stp_tx_init (struct hci_stp *hu)
348 tasklet_init(&hci_tx_tasklet, hci_stp_tx_tasklet_func, (unsigned long)hu);
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);
357 void hci_stp_tx_deinit (struct hci_stp *hu)
359 tasklet_kill(&hci_tx_tasklet);
364 void hci_stp_txq_lock (unsigned int ctx)
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);
373 #error "HCI_STP_TX_TASKLET_LOCK"
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);
383 #error "HCI_STP_TX_TASKLET_LOCK"
388 void hci_stp_txq_unlock (unsigned int ctx)
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);
397 #error "HCI_STP_TX_TASKLET_LOCK"
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);
407 #error "HCI_STP_TX_TASKLET_LOCK"
412 void hci_stp_tx_kick (void)
414 tasklet_schedule(&hci_tx_tasklet);
417 static void hci_stp_tx_tasklet_func(unsigned long data) {
419 struct hci_stp *hu = (struct hci_stp *)data;
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);
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);
435 //read_lock(&hci_stp_txq_lock);
436 hci_stp_txq_lock(HCI_STP_TXQ_IN_HCISTP);
438 hci_stp_tx_wakeup(hu);
440 //read_unlock(&hci_stp_txq_lock);
441 hci_stp_txq_unlock(HCI_STP_TXQ_IN_HCISTP);
444 /* George: HCI_STP_SENDING and HCI_STP_TX_WAKEUP flags in this function seem
447 static int hci_stp_tx_wakeup(struct hci_stp *hu)
449 // struct hci_dev *hdev = hu->hdev;
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");
461 BT_DBG_FUNC("hci_stp_tx_wakeup %d\n", __LINE__);
464 clear_bit(HCI_STP_TX_WAKEUP, &hu->tx_state);
466 while ((skb = skb_dequeue(&hu->txq))) {
468 BT_DBG_FUNC("dqueue times = %d\n", ++j);
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));
482 if ((len = mtk_wcn_stp_send_data(skb->data, skb->len, BT_TASK_INDX)) == 0 ) {
484 BT_ERR_FUNC("mtk_wcn_stp_send_data can not send\n");
485 BT_ERR_FUNC("Error %s %d\n", __FUNCTION__, __LINE__);
487 skb_queue_head(&hu->txq, skb);//Put back to queue head
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);
498 if (test_bit(HCI_STP_TX_WAKEUP, &hu->tx_state))
501 clear_bit(HCI_STP_SENDING, &hu->tx_state);
507 #elif (HCI_STP_TX == HCI_STP_TX_THRD)
509 void hci_stp_tx_init (struct hci_stp *hu)
511 BT_DBG_FUNC("start:0x%p\n", hu);
513 spin_lock_init(&hci_stp_txqlock);
514 init_waitqueue_head(&hci_stp_tx_thrd_wq);
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");
520 wake_up_process(hci_stp_tx_thrd);
522 BT_DBG_FUNC("done\n");
526 void hci_stp_tx_deinit (struct hci_stp *hu)
528 BT_DBG_FUNC("start:0x%p\n", hu);
530 kthread_stop(hci_stp_tx_thrd);
531 hci_stp_tx_thrd = NULL;
533 BT_DBG_FUNC("done\n");
537 void hci_stp_txq_lock (unsigned int ctx)
539 if (ctx == HCI_STP_TXQ_IN_BLZ) {
540 /* lock txq in BlueZ tx tasklet context */
541 spin_lock(&hci_stp_txqlock);
544 /* lock txq in HCI-STP context(defined by compile flag: HCI_STP_TX) */
545 spin_lock_bh(&hci_stp_txqlock);
549 void hci_stp_txq_unlock (unsigned int ctx)
551 if (ctx == HCI_STP_TXQ_IN_BLZ) {
552 spin_unlock(&hci_stp_txqlock);
555 /* lock txq in HCI-STP context(defined by compile flag: HCI_STP_TX) */
556 spin_unlock_bh(&hci_stp_txqlock);
561 hci_stp_tx_thrd_func (void *pdata)
564 struct hci_dev *hdev;
568 hu = (struct hci_stp *)pdata;
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);
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);
585 smp_mb(); /* sync shared data */
588 wait_event_interruptible(hci_stp_tx_thrd_wq,
589 (!skb_queue_empty(&hu->txq) || kthread_should_stop()));
591 if (unlikely(kthread_should_stop())) {
592 BT_INFO_FUNC("hci_stpd thread should stop now... \n");
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);
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));
613 len = mtk_wcn_stp_send_data(skb->data, skb->len, BT_TASK_INDX);
614 if (unlikely(len != skb->len)) {
616 BT_ERR_FUNC("mtk_wcn_stp_send_data fail, enqueue again!(%d, %d)\n",
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 */
624 //hdev->stat.byte_tx += len; // moved into hci_stp_tx_skb_comp()
625 hci_stp_tx_skb_comp(hu, skb);
628 hci_stp_txq_lock(HCI_STP_TXQ_IN_HCISTP);
630 hci_stp_txq_unlock(HCI_STP_TXQ_IN_HCISTP);
635 BT_INFO_FUNC("hci_stpd stop!\n");
639 void hci_stp_tx_kick (void)
641 wake_up_interruptible(&hci_stp_tx_thrd_wq);
645 #error "Not implemented HCI_STP_TX"
648 #if (HCI_STP_DEV_INIT == HCI_STP_DEV_INIT_OPEN_CTX)
649 static int hci_stp_dev_init (struct hci_stp *phu)
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
764 #elif (HCI_STP_DEV_INIT == HCI_STP_DEV_INIT_THRD)
766 void hci_stp_dev_init_rx_cb (const UINT8 *data, INT32 count)
771 if (unlikely(!hdev)) {
772 BT_ERR_FUNC("null hdev!\n");
775 if (unlikely(!hdev->driver_data)) {
776 BT_ERR_FUNC("null hdev->driver_data!\n");
780 /* get hci_stp from global variable */
781 hu = (struct hci_stp *)hdev->driver_data;
782 idx = hu->init_cmd_idx;
784 if (unlikely(count != init_table[idx].evtSz)) {
785 hu->init_evt_rx_flag = -1; /* size mismatch */
787 else if (unlikely(memcmp(data, init_table[idx].hci_evt, count))) {
788 hu->init_evt_rx_flag = -2; /* content mismatch */
791 hu->init_evt_rx_flag = 1; /* ok */
792 BT_DBG_FUNC("EVT i(%d),rx(%d) ok\n", idx, count);
795 if (unlikely(1 != hu->init_evt_rx_flag)) {
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]);
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]);
810 smp_mb(); /* sync shared data */
811 wake_up(hu->p_init_evt_wq); /* wake up dev_init_work */
814 static void hci_stp_dev_init_work (struct work_struct *work)
823 /* get client's information */
824 phu = container_of(work, struct hci_stp, init_work);
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);
833 BT_DBG_FUNC("CMD(%d),(%s),t/o(%ld))=>\n", idx, init_table[idx].str, to);
834 smp_wmb(); /* sync shared data */
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.
842 ret = wait_event_timeout((*phu->p_init_evt_wq), phu->init_evt_rx_flag != 0, to);
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 */
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 */
858 if (phu->p_init_comp) {
859 complete(phu->p_init_comp);
865 static int hci_stp_dev_init (struct hci_stp *phu)
867 DECLARE_COMPLETION_ONSTACK(hci_stp_dev_init_comp);
868 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(hci_stp_dev_init_wq);
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);
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);
880 mtk_wcn_stp_set_bluez(1);
882 /* Schedule to call hci_stp_dev_init_work(). init_work is initialized in
885 schedule_work(&phu->init_work);
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);
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);
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.
903 if (likely(1 == phu->init_evt_rx_flag)) {
907 /* return non-zero value for error */
908 return (phu->init_evt_rx_flag + 256);
912 #error "Not implemented HCI_STP_DEV_INIT"
916 static ssize_t file_read(char *filename, char *buf, size_t len, loff_t *offset)
922 fp = filp_open(filename, O_RDONLY, 0);
924 BT_ERR_FUNC("Failed to open %s!\n", filename);
931 if ((fp->f_op == NULL) || (fp->f_op->read == NULL)){
932 BT_ERR_FUNC("File can not be read!\n");
934 filp_close(fp, NULL);
938 retLen = fp->f_op->read(fp, buf, len, offset);
941 filp_close(fp, NULL);
946 static ssize_t file_write(char *filename, char *buf, size_t len, loff_t *offset)
952 fp = filp_open(filename, O_WRONLY | O_CREAT, 0644);
954 BT_ERR_FUNC("Failed to open %s!\n", filename);
961 if ((fp->f_op == NULL) || (fp->f_op->write == NULL)){
962 BT_ERR_FUNC("File can not be write!\n");
964 filp_close(fp, NULL);
968 retLen = fp->f_op->write(fp, buf, len, offset);
971 filp_close(fp, NULL);
976 int load_custom_bt_conf(struct btradio_conf_data *cfg)
979 This method depends on customer's platform configuration data
981 Customer may use NVRAM, data file, or other patterns.
982 Here RECOMMEND and GIVE AN EXAMPLE to push configuration data
986 struct btradio_conf_data temp;
990 retLen = file_read(CUSTOM_BT_CFG_FILE,
998 if(retLen < sizeof(temp)){
999 BT_ERR_FUNC("File read length error %d\n", retLen);
1003 memcpy(cfg, &temp, retLen);
1008 int load_internal_bt_conf(struct btradio_conf_data *cfg)
1010 struct btradio_conf_data temp;
1016 unsigned char addr[6] = {0x00, 0x00, 0x46, 0x66, 0x20, 0x01};
1017 unsigned long randNum;
1019 retLen = file_read(INTERNAL_BT_CFG_FILE,
1025 BT_INFO_FUNC("No internal BT config file, generate from built-in BT config\n");
1028 retLen = file_read(BUILTIN_BT_CFG_FILE,
1033 BT_ERR_FUNC("Try to read built-in BT config, error\n");
1036 else if(retLen < sizeof(temp)){
1037 BT_ERR_FUNC("File read length error %d\n", retLen);
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);
1046 temp.addr[0] = (((randNum>>24 | randNum>>16) & (0xFE)) | (0x02));
1047 temp.addr[1] = ((randNum>>8) & 0xFF);
1048 temp.addr[5] = (randNum & 0xFF);
1050 // Generate internal BT config file
1052 written = file_write(INTERNAL_BT_CFG_FILE,
1057 BT_ERR_FUNC("Try to create internal BT config, error\n");
1059 else if(written < sizeof(temp)){
1060 BT_ERR_FUNC("File write length error %d\n", written);
1063 BT_INFO_FUNC("Internal BT config generated\n");
1067 else if(retLen < sizeof(temp)){
1068 BT_ERR_FUNC("File read length error %d\n", retLen);
1072 memcpy(cfg, &temp, retLen);
1077 /* Invoked when there is ONE received BT packet */
1078 void stp_tx_event_cb(void)
1082 if (unlikely(hdev == NULL)) {
1083 BT_ERR_FUNC("Null hdev!\n");
1084 BUG_ON(hdev == NULL);
1087 hu = (struct hci_stp *) hdev->driver_data;
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.
1093 hci_stp_tx_kick(); /* George: adapt different tx_kick function */
1097 Direct delivery of bluez not changed hands through the stp buffer
1099 void stp_rx_event_cb_directly(const UINT8 *data, INT32 count)
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??? */
1113 #if HCI_STP_SAFE_RESET
1117 BT_LOUD_FUNC("count(%d)rx_state(%d)rx_count(%d)\n",
1118 count, rx_state, rx_count);
1121 BT_ERR_FUNC("Data is Null\n");
1126 BT_WARN_FUNC("abnormal count(%d)\n", count);
1131 hdev->stat.byte_rx += count;
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));
1143 atomic_dec(&hu->reset_count);
1144 wake_up(&hu->reset_wq);
1151 while (count > 0) { /* while (count) */
1152 /* Is while_count redundant? */
1154 if (++while_count > 5000) {
1155 BT_WARN_FUNC("abnormal while_count(%d)\n", while_count);
1159 len = min_t(unsigned int, rx_count, count);
1160 memcpy(skb_put(rx_skb, len), ptr, len);
1167 /* rx_count==0, ready to indicate to hci_core */
1171 BT_LOUD_FUNC("Complete data\n");
1172 hci_recv_frame(rx_skb);
1173 rx_state = H4_W4_PACKET_TYPE;
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);
1186 hci_recv_frame(rx_skb);
1187 rx_state = H4_W4_PACKET_TYPE;
1189 rx_count = 0; /* redundant? here rx_count is 0 already */
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",
1196 rx_state = H4_W4_PACKET_TYPE;
1198 rx_count = 0; /* redundant? here rx_count is 0 already */
1201 rx_state = H4_W4_DATA;
1202 rx_count = eh->plen;
1207 ah = hci_acl_hdr(rx_skb);
1208 dlen = __le16_to_cpu(ah->dlen);
1210 room = skb_tailroom(rx_skb);
1211 BT_LOUD_FUNC("ACL header:dlen(%d)room(%d)\n", dlen, room);
1213 hci_recv_frame(rx_skb);
1214 rx_state = H4_W4_PACKET_TYPE;
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",
1223 rx_state = H4_W4_PACKET_TYPE;
1228 rx_state = H4_W4_DATA;
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);
1239 hci_recv_frame(rx_skb);
1240 rx_state = H4_W4_PACKET_TYPE;
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",
1249 rx_state = H4_W4_PACKET_TYPE;
1254 rx_state = H4_W4_DATA;
1255 rx_count = sh->dlen;
1261 /* H4_W4_PACKET_TYPE */
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;
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;
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;
1285 BT_ERR_FUNC("Unknown HCI packet type %2.2x\n", (__u8)*ptr);
1286 ++(hdev->stat.err_rx);
1295 /* Allocate packet */
1296 rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1298 BT_ERR_FUNC("bt_skb_alloc(%d, GFP_ATOMIC) fail!\n", HCI_MAX_FRAME_SIZE);
1299 rx_state = H4_W4_PACKET_TYPE;
1304 rx_skb->dev = (void *) hdev;
1305 bt_cb(rx_skb)->pkt_type = type;
1311 /* ------- Interface to HCI layer ------ */
1312 /* Initialize device */
1313 static int hci_stp_open(struct hci_dev *hdev)
1316 struct btradio_conf_data cfg;
1319 if (unlikely(!hdev)) {
1320 BT_ERR_FUNC("null hdev\n");
1324 if (unlikely(!hdev->driver_data)) {
1325 BT_ERR_FUNC("null hdev\n");
1329 hu = (struct hci_stp *)hdev->driver_data;
1330 BT_INFO_FUNC("%s(0x%p)\n", hdev->name, hdev);
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);
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");
1343 BT_INFO_FUNC("WMT turn on BT OK!\n");
1345 if (likely(mtk_wcn_stp_is_ready())) {
1347 BT_LOUD_FUNC("STP is ready!\n");
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");
1353 if (load_internal_bt_conf(&cfg) < 0){
1354 BT_ERR_FUNC("Load internal BT config failed!\n");
1358 BT_INFO_FUNC("Load internal BT config success\n");
1362 BT_INFO_FUNC("Load custom BT config success\n");
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]);
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]);
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);
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");
1413 BT_INFO_FUNC("WMT turn off BT ok!\n");
1418 BT_INFO_FUNC("hci_stp_dev_init ok\n");
1420 set_bit(HCI_RUNNING, &hdev->flags);
1422 /*registered tx/rx path*/
1423 mtk_wcn_stp_register_if_rx(stp_rx_event_cb_directly);
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);
1429 mtk_wcn_stp_set_bluez(1);
1434 BT_WARN_FUNC("STP is not ready!\n");
1440 static int hci_stp_flush(struct hci_dev *hdev)
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);
1451 hu = (struct hci_stp *)hdev->driver_data;
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);
1458 BT_INFO_FUNC("done\n");
1463 static int hci_stp_close(struct hci_dev *hdev)
1465 #if HCI_STP_SAFE_RESET
1470 BT_INFO_FUNC("hdev(0x%p)\n", hdev);
1472 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
1476 hci_stp_flush(hdev);
1479 #if HCI_STP_SAFE_RESET
1480 hu = (struct hci_stp *)hdev->driver_data;
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));
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));
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);
1506 /*unregistered tx/rx path*/
1507 mtk_wcn_stp_register_if_rx(NULL);
1509 mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
1510 mtk_wcn_stp_register_tx_event_cb(BT_TASK_INDX, NULL);
1513 mtk_wcn_stp_set_bluez(0);
1515 if (MTK_WCN_BOOL_FALSE == mtk_wcn_wmt_func_off(WMTDRV_TYPE_BT)) {
1516 BT_WARN_FUNC("WMT turn off BT fail!\n");
1519 BT_INFO_FUNC("WMT turn off BT OK!\n");
1525 /* Send frames from HCI layer */
1526 static int hci_stp_send_frame(struct sk_buff *skb)
1528 struct hci_dev* hdev = (struct hci_dev *) skb->dev;
1533 BT_ERR_FUNC("Null hdev in skb\n");
1537 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
1538 BT_ERR_FUNC("no HCI_RUNNING in hdev->flags(0x%lx)\n", hdev->flags);
1542 hu = (struct hci_stp *) hdev->driver_data;
1544 BT_LOUD_FUNC("%s:type(%d)len(%d)\n",
1545 hdev->name, bt_cb(skb)->pkt_type, skb->len);
1547 #if 0 /* just timestamp?? */
1548 if (gHciStpDbgLevel >= BT_LOG_DBG)
1551 do_gettimeofday(&now);
1552 printk("%s: sec = %ld, --> usec --> %ld\n",
1553 __FUNCTION__, now.tv_sec, now.tv_usec);
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);
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.
1565 skb_queue_tail(&hu->txq, skb);
1566 hci_stp_txq_unlock(HCI_STP_TXQ_IN_BLZ);
1568 /* George: adapt different tx_kick function */
1574 static void hci_stp_destruct(struct hci_dev *hdev)
1576 BT_INFO_FUNC("start hdev(0x%p)\n", hdev);
1581 BT_DBG_FUNC("%s\n", hdev->name);
1582 hci_stp_tx_deinit((struct hci_stp *)hdev->driver_data);
1583 kfree(hdev->driver_data);
1585 BT_DBG_FUNC("done\n");
1588 static int __init hci_stp_init(void)
1590 struct hci_stp *hu = NULL;
1592 BT_DBG_FUNC("HCI STP driver ver %s\n", VERSION);
1594 if (!(hu = kzalloc(sizeof(struct hci_stp), /*GFP_KERNEL*/GFP_ATOMIC))) {
1595 BT_ERR_FUNC("Can't allocate control structure\n");
1599 BT_DBG_FUNC("hu 0x%08x\n", (int)hu);
1602 used to stored pending skb
1604 skb_queue_head_init(&hu->txq);
1607 Initialize and register HCI device
1609 hdev = hci_alloc_dev();
1616 BT_ERR_FUNC("Can't allocate HCI device\n");
1622 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34))
1623 hdev->type = HCI_UART;
1625 hdev->bus = HCI_UART;
1626 hdev->dev_type = HCI_BREDR;
1628 hdev->driver_data = hu;
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;
1637 hdev->owner = THIS_MODULE;
1639 BT_DBG_FUNC("HCI_QUIRK_NO_RESET\n");
1641 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1643 if (hci_register_dev(hdev) < 0) {
1644 BT_ERR_FUNC("Can't register HCI device\n");
1652 #if HCI_STP_SAFE_RESET
1653 atomic_set(&hu->reset_count, 0);
1654 init_waitqueue_head(&hu->reset_wq);
1657 /* George: adapt different tx_init function */
1658 hci_stp_tx_init(hu);
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);
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);
1670 BT_INFO_FUNC("HCI-STP Drv(%s),dev(0x%p),data(0x%p) init done\n", VERSION, hdev, hdev->driver_data);
1674 static void __exit hci_stp_exit(void)
1676 struct hci_stp *hu = (struct hci_stp *)hdev->driver_data;
1679 mtk_wcn_stp_register_event_cb(BT_TASK_INDX, NULL);
1680 mtk_wcn_stp_register_tx_event_cb(BT_TASK_INDX, NULL);
1682 hci_unregister_dev(hdev);
1684 /* George: adapt different tx_deinit function */
1685 //tasklet_kill(&hci_tx_tasklet);
1687 skb_queue_purge(&hu->txq);
1689 /* hci_stp_destruct does this */
1690 /*kfree(hdev->driver_data);*/
1694 BT_INFO_FUNC("done\n");
1697 module_init(hci_stp_init);
1698 module_exit(hci_stp_exit);
1700 module_param(reset, bool, 0644);
1701 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1703 MODULE_AUTHOR("Mediatek Inc.");
1704 MODULE_DESCRIPTION("Bluetooth HCI STP driver ver " VERSION);
1705 MODULE_VERSION(VERSION);
1706 MODULE_LICENSE("GPL");