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