Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[firefly-linux-kernel-4.4.55.git] / drivers / bluetooth / hci_h4.c
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
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>
35
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>
43
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
46
47 #if defined(CONFIG_MT5931_MT6622)
48 #include "../mtk_wcn_bt/bt_hwctl.h"
49 #if 0//def BT_DBG
50 #undef BT_DBG
51 #define BT_DBG(fmt, arg...) printk(KERN_INFO "%s" fmt "\n", __FUNCTION__, ##arg)
52 #endif
53 #endif
54
55 #include "hci_uart.h"
56
57 #define VERSION "1.2"
58
59 struct h4_struct {
60         unsigned long rx_state;
61         unsigned long rx_count;
62         struct sk_buff *rx_skb;
63         struct sk_buff_head txq;
64
65 #if defined(CONFIG_MT5931_MT6622)
66         /* add for MT6622 */
67         int rxAck;
68         struct timer_list rxTime;
69         unsigned long last_jiffies;
70         unsigned long ulMagic;
71         spinlock_t ack_lock;
72 #endif
73 };
74
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
80 #define H4_W4_DATA              4
81
82
83 /* Initialize protocol */
84 static int h4_open(struct hci_uart *hu)
85 {
86         struct h4_struct *h4;
87
88         BT_DBG("hu %p", hu);
89
90         h4 = kzalloc(sizeof(*h4), GFP_ATOMIC);
91         if (!h4)
92                 return -ENOMEM;
93
94         skb_queue_head_init(&h4->txq);
95
96         hu->priv = h4;
97         return 0;
98 }
99
100 /* Flush protocol data */
101 static int h4_flush(struct hci_uart *hu)
102 {
103         struct h4_struct *h4 = hu->priv;
104
105         BT_DBG("hu %p", hu);
106
107         skb_queue_purge(&h4->txq);
108
109         return 0;
110 }
111
112 /* Close protocol */
113 static int h4_close(struct hci_uart *hu)
114 {
115         struct h4_struct *h4 = hu->priv;
116
117         hu->priv = NULL;
118
119         BT_DBG("hu %p", hu);
120
121         skb_queue_purge(&h4->txq);
122
123         kfree_skb(h4->rx_skb);
124
125         hu->priv = NULL;
126         kfree(h4);
127
128         return 0;
129 }
130
131 /* Enqueue frame for transmittion (padding, crc, etc) */
132 static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
133 {
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;
139 #endif
140         BT_DBG("hu %p skb %p", hu, skb);
141
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]));
147
148                 BT_DBG("Command 0x%04x\n", (int)usOpCode);
149
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);
154
155                         /* because controller has waken host. Host assume device is ready to process. */
156                         clear_bit(1, &h4->ulMagic); /* allow TX to run */
157                         return 0;
158                 }
159         }
160
161         /* wake up device */
162         lCurrentTime = jiffies_to_msecs(h4->last_jiffies);
163
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);
167
168                 /* Allocate packet */
169                 skbMagic = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC); /* only 0xFF is required */
170
171                 if (!skbMagic) {
172                         BT_ERR("Can't allocate mem for new packet"); /* how to handle? */
173                         return 0;
174                 }
175                 skbMagic->pkt_type = 1;
176                 skbMagic->dev = (void *) hu->hdev;
177
178                 memcpy(skb_put(skbMagic, 1), &ucMagic, 1);
179                 skb_queue_tail(&h4->txq, skbMagic);
180         }
181         h4->last_jiffies = jiffies;
182 #endif
183
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);
187
188         return 0;
189 }
190
191 static inline int h4_check_data_len(struct h4_struct *h4, int len)
192 {
193         register int room = skb_tailroom(h4->rx_skb);
194
195         BT_DBG("len %d room %d", len, room);
196
197         if (!len) {
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);
202         } else {
203                 h4->rx_state = H4_W4_DATA;
204                 h4->rx_count = len;
205                 return len;
206         }
207         h4->rx_state = H4_W4_PACKET_TYPE;
208         h4->rx_skb   = NULL;
209         h4->rx_count = 0;
210
211         return 0;
212 }
213
214 /* Recv data */
215 static int h4_recv(struct hci_uart *hu, void *data, int count)
216 {
217 #if !defined(CONFIG_MT5931_MT6622)
218         int ret;
219
220         ret = hci_recv_stream_fragment(hu->hdev, data, count);
221         if (ret < 0) {
222                 BT_ERR("Frame Reassembly Failed");
223                 return ret;
224         }
225
226         return count;
227 #else
228         struct h4_struct *h4 = hu->priv;
229         register char *ptr;
230         struct hci_event_hdr *eh;
231         struct hci_acl_hdr   *ah;
232         struct hci_sco_hdr   *sh;
233         register int len, type, dlen;
234         int iDiscard = 0;
235         int while_count = 0;
236
237         BT_DBG("hu %p count %d rx_state %ld rx_count %ld", 
238                 hu, count, h4->rx_state, h4->rx_count);
239
240         ptr = data;
241         while (count) {
242                 while_count ++;
243
244                 if(while_count > 5000){
245                         BT_ERR("h4_recv while_count %d\n", while_count);
246                 }
247
248                 if (h4->rx_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;
252
253                         if (h4->rx_count)
254                                 continue;
255
256                         switch (h4->rx_state) {
257                         case H4_W4_DATA:
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;
261
262                                         eh = hci_event_hdr(h4->rx_skb);
263                    
264                                         switch(eh->evt){
265                                         case HCI_EV_CMD_COMPLETE:
266                                                 usOpCode = (((unsigned short)(h4->rx_skb->data[4])) << 8) | 
267                                                         ((unsigned short)(h4->rx_skb->data[3]));
268
269                                                 if(usOpCode == 0xFCC0){
270                                                         iDiscard = 1;
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);
275                                                 }
276
277                                                 if(usOpCode == 0xFCC1){                        
278                                                         BT_DBG("recv host awake command event 0x%04x\n", (int)usOpCode);
279                                                         //mt_bt_enable_irq();
280                                                 }
281                                                 break;
282                                         }
283                                 }
284
285                                 if(!iDiscard){
286                                     hci_recv_frame(h4->rx_skb);
287                                 }
288                                 else{ 
289                                     kfree_skb(h4->rx_skb);
290                                 }
291
292                                 h4->rx_state = H4_W4_PACKET_TYPE;
293                                 h4->rx_skb = NULL;
294                                 continue;
295
296                         case H4_W4_EVENT_HDR:
297                                 eh = hci_event_hdr(h4->rx_skb);
298
299                                 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
300
301                                 h4_check_data_len(h4, eh->plen);
302                                 continue;
303
304                         case H4_W4_ACL_HDR:
305                                 ah = hci_acl_hdr(h4->rx_skb);
306                                 dlen = __le16_to_cpu(ah->dlen);
307
308                                 BT_DBG("ACL header: dlen %d", dlen);
309                                 h4_check_data_len(h4, dlen);
310                                 continue;
311
312                         case H4_W4_SCO_HDR:
313                                 sh = hci_sco_hdr(h4->rx_skb);
314
315                                 BT_DBG("SCO header: dlen %d", sh->dlen);
316
317                                 h4_check_data_len(h4, sh->dlen);
318                                 continue;
319                         }
320                 }
321
322                 /* H4_W4_PACKET_TYPE */
323                 switch (*ptr) {
324                 case HCI_EVENT_PKT:
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;
329                         break;
330
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;
336                         break;
337
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;
343                         break;
344
345                 default:
346                         BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
347                         hu->hdev->stat.err_rx++;
348                         ptr++; count--;
349                         continue;
350                 };
351
352                 ptr++; count--;
353                 /* Allocate packet */
354                 h4->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
355                 if (!h4->rx_skb) {
356                         BT_ERR("Can't allocate mem for new packet");
357                         h4->rx_state = H4_W4_PACKET_TYPE;
358                         h4->rx_count = 0;
359                         return 0;
360                 }
361
362                 h4->rx_skb->dev = (void *) hu->hdev;
363                 bt_cb(h4->rx_skb)->pkt_type = type;
364         }
365
366         return count;
367 #endif
368 }
369
370 static struct sk_buff *h4_dequeue(struct hci_uart *hu)
371 {
372         struct h4_struct *h4 = hu->priv;
373 #if !defined(CONFIG_MT5931_MT6622)
374         return skb_dequeue(&h4->txq);
375 #else
376         struct sk_buff *skb = NULL;
377
378         if(test_bit(1, &h4->ulMagic)){
379                 BT_DBG("magic number is being performed\n");
380                 return NULL;
381         }
382
383         skb = skb_dequeue(&h4->txq);
384
385         if(skb){
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);
389                 }
390         }
391         return skb;
392 #endif
393 }
394
395 static struct hci_uart_proto h4p = {
396         .id             = HCI_UART_H4,
397         .open           = h4_open,
398         .close          = h4_close,
399         .recv           = h4_recv,
400         .enqueue        = h4_enqueue,
401         .dequeue        = h4_dequeue,
402         .flush          = h4_flush,
403 };
404
405 int __init h4_init(void)
406 {
407         int err = hci_uart_register_proto(&h4p);
408
409         if (!err)
410                 BT_INFO("HCI H4 protocol initialized");
411         else
412                 BT_ERR("HCI H4 protocol registration failed");
413
414         return err;
415 }
416
417 int __exit h4_deinit(void)
418 {
419         return hci_uart_unregister_proto(&h4p);
420 }