xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bluetooth_uart_driver/hci_h4.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *
3  *  Bluetooth HCI UART driver
4  *
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>
8  *
9  *
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.
14  *
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.
19  *
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
23  *
24  */
25 
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/fcntl.h>
31 #include <linux/interrupt.h>
32 #include <linux/ptrace.h>
33 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/tty.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
38 #include <linux/signal.h>
39 #include <linux/ioctl.h>
40 #include <linux/skbuff.h>
41 #include <net/bluetooth/bluetooth.h>
42 #include <net/bluetooth/hci_core.h>
43 #include <linux/version.h>
44 
45 #include "hci_uart.h"
46 
47 #ifdef BTCOEX
48 #include "rtk_coex.h"
49 #endif
50 
51 //#define VERSION "1.2"
52 
53 struct h4_struct {
54 	unsigned long rx_state;
55 	unsigned long rx_count;
56 	struct sk_buff *rx_skb;
57 	struct sk_buff_head txq;
58 };
59 
60 /* H4 receiver States */
61 #define H4_W4_PACKET_TYPE	0
62 #define H4_W4_EVENT_HDR		1
63 #define H4_W4_ACL_HDR		2
64 #define H4_W4_SCO_HDR		3
65 #define H4_W4_DATA		4
66 
67 /* Initialize protocol */
h4_open(struct hci_uart * hu)68 static int h4_open(struct hci_uart *hu)
69 {
70 	struct h4_struct *h4;
71 
72 	BT_DBG("hu %p", hu);
73 
74 	h4 = kzalloc(sizeof(*h4), GFP_ATOMIC);
75 	if (!h4)
76 		return -ENOMEM;
77 
78 	skb_queue_head_init(&h4->txq);
79 
80 	hu->priv = h4;
81 	return 0;
82 }
83 
84 /* Flush protocol data */
h4_flush(struct hci_uart * hu)85 static int h4_flush(struct hci_uart *hu)
86 {
87 	struct h4_struct *h4 = hu->priv;
88 
89 	BT_DBG("hu %p", hu);
90 
91 	skb_queue_purge(&h4->txq);
92 
93 	return 0;
94 }
95 
96 /* Close protocol */
h4_close(struct hci_uart * hu)97 static int h4_close(struct hci_uart *hu)
98 {
99 	struct h4_struct *h4 = hu->priv;
100 
101 	hu->priv = NULL;
102 
103 	BT_DBG("hu %p", hu);
104 
105 	skb_queue_purge(&h4->txq);
106 
107 	kfree_skb(h4->rx_skb);
108 
109 	hu->priv = NULL;
110 	kfree(h4);
111 
112 	return 0;
113 }
114 
115 /* Enqueue frame for transmittion (padding, crc, etc) */
h4_enqueue(struct hci_uart * hu,struct sk_buff * skb)116 static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
117 {
118 	struct h4_struct *h4 = hu->priv;
119 
120 	BT_DBG("hu %p skb %p", hu, skb);
121 
122 	/* Prepend skb with frame type */
123 	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
124 	skb_queue_tail(&h4->txq, skb);
125 
126 	return 0;
127 }
128 
129 #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
h4_check_data_len(struct h4_struct * h4,int len)130 static inline int h4_check_data_len(struct h4_struct *h4, int len)
131 #else
132 static inline int h4_check_data_len(struct hci_dev *hdev, struct h4_struct *h4, int len)
133 #endif
134 {
135 	register int room = skb_tailroom(h4->rx_skb);
136 
137 	BT_DBG("len %d room %d", len, room);
138 
139 	if (!len) {
140 #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
141 		hci_recv_frame(h4->rx_skb);
142 #else
143 		hci_recv_frame(hdev, h4->rx_skb);
144 #endif
145 	} else if (len > room) {
146 		BT_ERR("Data length is too large");
147 		kfree_skb(h4->rx_skb);
148 	} else {
149 		h4->rx_state = H4_W4_DATA;
150 		h4->rx_count = len;
151 		return len;
152 	}
153 
154 	h4->rx_state = H4_W4_PACKET_TYPE;
155 	h4->rx_skb   = NULL;
156 	h4->rx_count = 0;
157 
158 	return 0;
159 }
160 
161 /* Recv data */
h4_recv(struct hci_uart * hu,void * data,int count)162 static int h4_recv(struct hci_uart *hu, void *data, int count)
163 {
164 	struct h4_struct *h4 = hu->priv;
165 	register char *ptr;
166 	struct hci_event_hdr *eh;
167 	struct hci_acl_hdr   *ah;
168 	struct hci_sco_hdr   *sh;
169 	register int len, type, dlen;
170 
171 	BT_DBG("hu %p count %d rx_state %ld rx_count %ld",
172 			hu, count, h4->rx_state, h4->rx_count);
173 
174 	ptr = data;
175 	while (count) {
176 		if (h4->rx_count) {
177 			len = min_t(unsigned int, h4->rx_count, count);
178 			memcpy(skb_put(h4->rx_skb, len), ptr, len);
179 			h4->rx_count -= len; count -= len; ptr += len;
180 
181 			if (h4->rx_count)
182 				continue;
183 
184 			switch (h4->rx_state) {
185 			case H4_W4_DATA:
186 				BT_DBG("Complete data");
187 #ifdef BTCOEX
188 				if(bt_cb(h4->rx_skb)->pkt_type == HCI_EVENT_PKT)
189 					rtk_btcoex_parse_event(
190 							h4->rx_skb->data,
191 							h4->rx_skb->len);
192 
193 				if(bt_cb(h4->rx_skb)->pkt_type == HCI_ACLDATA_PKT)
194 					rtk_btcoex_parse_l2cap_data_rx(
195 							h4->rx_skb->data,
196 							h4->rx_skb->len);
197 #endif
198 
199 #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
200 				hci_recv_frame(h4->rx_skb);
201 #else
202 				hci_recv_frame(hu->hdev, h4->rx_skb);
203 #endif
204 
205 				h4->rx_state = H4_W4_PACKET_TYPE;
206 				h4->rx_skb = NULL;
207 				continue;
208 
209 			case H4_W4_EVENT_HDR:
210 				eh = hci_event_hdr(h4->rx_skb);
211 
212 				BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
213 
214 #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
215 				h4_check_data_len(h4, eh->plen);
216 #else
217 				h4_check_data_len(hu->hdev, h4, eh->plen);
218 #endif
219 				continue;
220 
221 			case H4_W4_ACL_HDR:
222 				ah = hci_acl_hdr(h4->rx_skb);
223 				dlen = __le16_to_cpu(ah->dlen);
224 
225 				BT_DBG("ACL header: dlen %d", dlen);
226 
227 #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
228 				h4_check_data_len(h4, dlen);
229 #else
230 				h4_check_data_len(hu->hdev, h4, dlen);
231 #endif
232 				continue;
233 
234 			case H4_W4_SCO_HDR:
235 				sh = hci_sco_hdr(h4->rx_skb);
236 
237 				BT_DBG("SCO header: dlen %d", sh->dlen);
238 
239 #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
240 				h4_check_data_len(h4, sh->dlen);
241 #else
242 				h4_check_data_len(hu->hdev, h4, sh->dlen);
243 #endif
244 				continue;
245 			}
246 		}
247 
248 		/* H4_W4_PACKET_TYPE */
249 		switch (*ptr) {
250 		case HCI_EVENT_PKT:
251 			BT_DBG("Event packet");
252 			h4->rx_state = H4_W4_EVENT_HDR;
253 			h4->rx_count = HCI_EVENT_HDR_SIZE;
254 			type = HCI_EVENT_PKT;
255 			break;
256 
257 		case HCI_ACLDATA_PKT:
258 			BT_DBG("ACL packet");
259 			h4->rx_state = H4_W4_ACL_HDR;
260 			h4->rx_count = HCI_ACL_HDR_SIZE;
261 			type = HCI_ACLDATA_PKT;
262 			break;
263 
264 		case HCI_SCODATA_PKT:
265 			BT_DBG("SCO packet");
266 			h4->rx_state = H4_W4_SCO_HDR;
267 			h4->rx_count = HCI_SCO_HDR_SIZE;
268 			type = HCI_SCODATA_PKT;
269 			break;
270 
271 		default:
272 			BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
273 			hu->hdev->stat.err_rx++;
274 			ptr++; count--;
275 			continue;
276 		};
277 
278 		ptr++; count--;
279 
280 		/* Allocate packet */
281 		h4->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
282 		if (!h4->rx_skb) {
283 			BT_ERR("Can't allocate mem for new packet");
284 			h4->rx_state = H4_W4_PACKET_TYPE;
285 			h4->rx_count = 0;
286 			return -ENOMEM;
287 		}
288 
289 		h4->rx_skb->dev = (void *) hu->hdev;
290 		bt_cb(h4->rx_skb)->pkt_type = type;
291 	}
292 
293 	return count;
294 }
295 
h4_dequeue(struct hci_uart * hu)296 static struct sk_buff *h4_dequeue(struct hci_uart *hu)
297 {
298 	struct h4_struct *h4 = hu->priv;
299 	return skb_dequeue(&h4->txq);
300 }
301 
302 static struct hci_uart_proto h4p = {
303 	.id		= HCI_UART_H4,
304 	.open		= h4_open,
305 	.close		= h4_close,
306 	.recv		= h4_recv,
307 	.enqueue	= h4_enqueue,
308 	.dequeue	= h4_dequeue,
309 	.flush		= h4_flush,
310 };
311 
h4_init(void)312 int __init h4_init(void)
313 {
314 	int err = hci_uart_register_proto(&h4p);
315 
316 	if (!err)
317 		BT_INFO("HCI H4 protocol initialized");
318 	else
319 		BT_ERR("HCI H4 protocol registration failed");
320 
321 	return err;
322 }
323 
h4_deinit(void)324 int __exit h4_deinit(void)
325 {
326 	return hci_uart_unregister_proto(&h4p);
327 }
328