3 * Bluetooth HCI UART driver
5 * Copyright (C) 2000-2001 Qualcomm Incorporated
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/fcntl.h>
32 #include <linux/interrupt.h>
33 #include <linux/ptrace.h>
34 #include <linux/poll.h>
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/errno.h>
39 #include <linux/string.h>
40 #include <linux/signal.h>
41 #include <linux/ioctl.h>
42 #include <linux/skbuff.h>
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
47 #if defined(CONFIG_MT5931_MT6622)
48 #include "../mtk_wcn_bt/bt_hwctl.h"
51 #define BT_DBG(fmt, arg...) printk(KERN_INFO "%s" fmt "\n", __FUNCTION__, ##arg)
60 unsigned long rx_state;
61 unsigned long rx_count;
62 struct sk_buff *rx_skb;
63 struct sk_buff_head txq;
65 #if defined(CONFIG_MT5931_MT6622)
68 struct timer_list rxTime;
69 unsigned long last_jiffies;
70 unsigned long ulMagic;
75 /* H4 receiver States */
76 #define H4_W4_PACKET_TYPE 0
77 #define H4_W4_EVENT_HDR 1
78 #define H4_W4_ACL_HDR 2
79 #define H4_W4_SCO_HDR 3
83 /* Initialize protocol */
84 static int h4_open(struct hci_uart *hu)
90 h4 = kzalloc(sizeof(*h4), GFP_ATOMIC);
94 skb_queue_head_init(&h4->txq);
100 /* Flush protocol data */
101 static int h4_flush(struct hci_uart *hu)
103 struct h4_struct *h4 = hu->priv;
107 skb_queue_purge(&h4->txq);
113 static int h4_close(struct hci_uart *hu)
115 struct h4_struct *h4 = hu->priv;
121 skb_queue_purge(&h4->txq);
123 kfree_skb(h4->rx_skb);
131 /* Enqueue frame for transmittion (padding, crc, etc) */
132 static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
134 struct h4_struct *h4 = hu->priv;
135 #if defined(CONFIG_MT5931_MT6622)
136 unsigned long lCurrentTime = 0; /* in msec */
137 struct sk_buff *skbMagic = NULL; /* used to store magic skb */
138 unsigned char ucMagic = 0xFF;
140 BT_DBG("hu %p skb %p", hu, skb);
142 #if defined(CONFIG_MT5931_MT6622)
143 if(bt_cb(skb)->pkt_type == 1){
144 unsigned short usOpCode = 0;
145 usOpCode = (((unsigned short)(skb->data[1])) << 8) |
146 ((unsigned short)(skb->data[0]));
148 BT_DBG("Command 0x%04x\n", (int)usOpCode);
150 if(usOpCode == 0xFCC1){
151 /* Prepend skb with frame type */
152 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
153 skb_queue_head(&h4->txq, skb);
155 /* because controller has waken host. Host assume device is ready to process. */
156 clear_bit(1, &h4->ulMagic); /* allow TX to run */
162 lCurrentTime = jiffies_to_msecs(h4->last_jiffies);
164 if((jiffies_to_msecs(jiffies) - lCurrentTime) > 4 * 1000){
165 BT_DBG(" h4_enqueue idle more than 4s 0x%08lx 0x%08lx\n",
166 jiffies, h4->last_jiffies);
168 /* Allocate packet */
169 skbMagic = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC); /* only 0xFF is required */
172 BT_ERR("Can't allocate mem for new packet"); /* how to handle? */
175 skbMagic->pkt_type = 1;
176 skbMagic->dev = (void *) hu->hdev;
178 memcpy(skb_put(skbMagic, 1), &ucMagic, 1);
179 skb_queue_tail(&h4->txq, skbMagic);
181 h4->last_jiffies = jiffies;
184 /* Prepend skb with frame type */
185 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
186 skb_queue_tail(&h4->txq, skb);
191 static inline int h4_check_data_len(struct h4_struct *h4, int len)
193 register int room = skb_tailroom(h4->rx_skb);
195 BT_DBG("len %d room %d", len, room);
198 hci_recv_frame(h4->rx_skb);
199 } else if (len > room) {
200 BT_ERR("Data length is too large");
201 kfree_skb(h4->rx_skb);
203 h4->rx_state = H4_W4_DATA;
207 h4->rx_state = H4_W4_PACKET_TYPE;
215 static int h4_recv(struct hci_uart *hu, void *data, int count)
217 #if !defined(CONFIG_MT5931_MT6622)
220 ret = hci_recv_stream_fragment(hu->hdev, data, count);
222 BT_ERR("Frame Reassembly Failed");
228 struct h4_struct *h4 = hu->priv;
230 struct hci_event_hdr *eh;
231 struct hci_acl_hdr *ah;
232 struct hci_sco_hdr *sh;
233 register int len, type, dlen;
237 BT_DBG("hu %p count %d rx_state %ld rx_count %ld",
238 hu, count, h4->rx_state, h4->rx_count);
244 if(while_count > 5000){
245 BT_ERR("h4_recv while_count %d\n", while_count);
249 len = min_t(unsigned int, h4->rx_count, count);
250 memcpy(skb_put(h4->rx_skb, len), ptr, len);
251 h4->rx_count -= len; count -= len; ptr += len;
256 switch (h4->rx_state) {
258 iDiscard = 0; /* default not to drop packet */
259 if(HCI_EVENT_PKT == bt_cb(h4->rx_skb)->pkt_type){
260 unsigned short usOpCode = 0;
262 eh = hci_event_hdr(h4->rx_skb);
265 case HCI_EV_CMD_COMPLETE:
266 usOpCode = (((unsigned short)(h4->rx_skb->data[4])) << 8) |
267 ((unsigned short)(h4->rx_skb->data[3]));
269 if(usOpCode == 0xFCC0){
271 clear_bit(1, &h4->ulMagic); /* allow TX to run */
272 BT_DBG("recv event 0x%04x\n", (int)usOpCode);
273 /* flag is cleared. we may resume TX. or later? */
274 hci_uart_tx_wakeup(hu);
277 if(usOpCode == 0xFCC1){
278 BT_DBG("recv host awake command event 0x%04x\n", (int)usOpCode);
279 //mt_bt_enable_irq();
286 hci_recv_frame(h4->rx_skb);
289 kfree_skb(h4->rx_skb);
292 h4->rx_state = H4_W4_PACKET_TYPE;
296 case H4_W4_EVENT_HDR:
297 eh = hci_event_hdr(h4->rx_skb);
299 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
301 h4_check_data_len(h4, eh->plen);
305 ah = hci_acl_hdr(h4->rx_skb);
306 dlen = __le16_to_cpu(ah->dlen);
308 BT_DBG("ACL header: dlen %d", dlen);
309 h4_check_data_len(h4, dlen);
313 sh = hci_sco_hdr(h4->rx_skb);
315 BT_DBG("SCO header: dlen %d", sh->dlen);
317 h4_check_data_len(h4, sh->dlen);
322 /* H4_W4_PACKET_TYPE */
325 BT_DBG("Event packet");
326 h4->rx_state = H4_W4_EVENT_HDR;
327 h4->rx_count = HCI_EVENT_HDR_SIZE;
328 type = HCI_EVENT_PKT;
331 case HCI_ACLDATA_PKT:
332 BT_DBG("ACL packet");
333 h4->rx_state = H4_W4_ACL_HDR;
334 h4->rx_count = HCI_ACL_HDR_SIZE;
335 type = HCI_ACLDATA_PKT;
338 case HCI_SCODATA_PKT:
339 BT_DBG("SCO packet");
340 h4->rx_state = H4_W4_SCO_HDR;
341 h4->rx_count = HCI_SCO_HDR_SIZE;
342 type = HCI_SCODATA_PKT;
346 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
347 hu->hdev->stat.err_rx++;
353 /* Allocate packet */
354 h4->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
356 BT_ERR("Can't allocate mem for new packet");
357 h4->rx_state = H4_W4_PACKET_TYPE;
362 h4->rx_skb->dev = (void *) hu->hdev;
363 bt_cb(h4->rx_skb)->pkt_type = type;
370 static struct sk_buff *h4_dequeue(struct hci_uart *hu)
372 struct h4_struct *h4 = hu->priv;
373 #if !defined(CONFIG_MT5931_MT6622)
374 return skb_dequeue(&h4->txq);
376 struct sk_buff *skb = NULL;
378 if(test_bit(1, &h4->ulMagic)){
379 BT_DBG("magic number is being performed\n");
383 skb = skb_dequeue(&h4->txq);
386 if((skb->pkt_type == 1) && (skb->len == 1) && (skb->data[0] == 0xFF)){
387 BT_DBG("h4_dequeue magic number\n");
388 set_bit(1, &h4->ulMagic);
395 static struct hci_uart_proto h4p = {
400 .enqueue = h4_enqueue,
401 .dequeue = h4_dequeue,
405 int __init h4_init(void)
407 int err = hci_uart_register_proto(&h4p);
410 BT_INFO("HCI H4 protocol initialized");
412 BT_ERR("HCI H4 protocol registration failed");
417 int __exit h4_deinit(void)
419 return hci_uart_unregister_proto(&h4p);