drm/cma: Cocci spatch "ptr_ret.spatch"
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / bluetooth / hci_h4_bcm.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 #include "hci_uart_bcm.h"
48 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
49 #include <linux/fm.h>
50 #endif
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 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
67 #define FM_W4_EVENT_HDR 5
68 #endif
69
70 /* Initialize protocol */
71 static int h4_open(struct hci_uart *hu)
72 {
73         struct h4_struct *h4;
74
75         BT_DBG("hu %p", hu);
76
77         h4 = kzalloc(sizeof(*h4), GFP_KERNEL);
78         if (!h4)
79                 return -ENOMEM;
80
81         skb_queue_head_init(&h4->txq);
82
83         hu->priv = h4;
84         return 0;
85 }
86
87 /* Flush protocol data */
88 static int h4_flush(struct hci_uart *hu)
89 {
90         struct h4_struct *h4 = hu->priv;
91
92         BT_DBG("hu %p", hu);
93
94         skb_queue_purge(&h4->txq);
95
96         return 0;
97 }
98
99 /* Close protocol */
100 static int h4_close(struct hci_uart *hu)
101 {
102         struct h4_struct *h4 = hu->priv;
103
104         hu->priv = NULL;
105
106         BT_DBG("hu %p", hu);
107
108         skb_queue_purge(&h4->txq);
109
110         kfree_skb(h4->rx_skb);
111
112         hu->priv = NULL;
113         kfree(h4);
114
115         return 0;
116 }
117
118 /* Enqueue frame for transmittion (padding, crc, etc) */
119 static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
120 {
121         struct h4_struct *h4 = hu->priv;
122
123         BT_DBG("hu %p skb %p", hu, skb);
124
125         /* Prepend skb with frame type */
126 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
127         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
128 #endif
129         skb_queue_tail(&h4->txq, skb);
130
131         return 0;
132 }
133
134 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
135 /*
136  * Function to check data length
137  */
138 static inline int h4_check_data_len(struct h4_struct *h4,
139                     struct hci_uart *hu, int protoid,int len)
140 {
141     register int room = skb_tailroom(h4->rx_skb);
142
143     BT_DBG("len %d room %d", len, room);
144
145     if (!len) {
146        hci_uart_route_frame(protoid, hu, h4->rx_skb);
147     } else if (len > room) {
148         BT_ERR("Data length is too large");
149         kfree_skb(h4->rx_skb);
150     } else {
151         h4->rx_state = H4_W4_DATA;
152         h4->rx_count = len;
153         return len;
154     }
155
156     h4->rx_state = H4_W4_PACKET_TYPE;
157     h4->rx_skb   = NULL;
158     h4->rx_count = 0;
159
160     return 0;
161 }
162 #endif
163 /* Recv data */
164 static int h4_recv(struct hci_uart *hu, void *data, int count)
165 {
166 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
167     struct h4_struct *h4 = hu->priv;
168     register char *ptr;
169     struct hci_event_hdr *eh;
170     struct hci_acl_hdr   *ah;
171     struct hci_sco_hdr   *sh;
172     struct fm_event_hdr *fm;
173     register int len, type, dlen;
174     static enum proto_type protoid = PROTO_SH_MAX;
175
176     BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, h4->rx_state, h4->rx_count);
177
178     ptr = (char *)data;
179     while (count) {
180         if (h4->rx_count) {
181             len = min_t(unsigned int, h4->rx_count, count);
182               memcpy(skb_put(h4->rx_skb, len), ptr, len);
183             h4->rx_count -= len; count -= len; ptr += len;
184
185             if (h4->rx_count)
186                 continue;
187
188             switch (h4->rx_state) {
189             case H4_W4_DATA:
190
191                 BT_DBG("%2x %2x %2x %2x",h4->rx_skb->data[0], h4->rx_skb->data[1], h4->rx_skb->data[2], h4->rx_skb->data[3]);
192                 BT_DBG("%2x %2x %2x %2x",h4->rx_skb->data[4], h4->rx_skb->data[5], h4->rx_skb->data[6], h4->rx_skb->data[7]);
193
194                 BT_DBG("Complete data");
195 #if defined(CONFIG_RADIO_BCM4343S)
196
197                 if ( ( h4->rx_skb->data[3] == 0x15 && h4->rx_skb->data[4] == 0xfc )
198                   || ( h4->rx_skb->data[0] == 0xff && h4->rx_skb->data[1] == 0x01 && h4->rx_skb->data[2] == 0x08 )
199                )
200 #elif defined(CONFIG_RADIO_SC2331)
201               if ((h4->rx_skb->data[3] == 0x8C && h4->rx_skb->data[4] == 0xfc)
202                         || (h4->rx_skb->data[0] == 0xff))
203 #endif
204                 {
205                    BT_INFO("FM Event change to FM CH8 packet");
206                    type = FM_CH8_PKT;
207                    h4->rx_skb->cb[0] = FM_CH8_PKT;
208                    h4->rx_state = FM_W4_EVENT_HDR;
209                    protoid = PROTO_SH_FM;
210                 }
211
212                 hci_uart_route_frame(protoid, hu, h4->rx_skb);
213
214                 h4->rx_state = H4_W4_PACKET_TYPE;
215                 h4->rx_skb = NULL;
216                 protoid = PROTO_SH_MAX;
217                 continue;
218
219             case H4_W4_EVENT_HDR:
220                 eh = hci_event_hdr(h4->rx_skb);
221
222                 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
223
224                 h4_check_data_len(h4, hu, protoid, eh->plen);
225                 continue;
226
227             case H4_W4_ACL_HDR:
228                 ah = hci_acl_hdr(h4->rx_skb);
229
230                 dlen = __le16_to_cpu(ah->dlen);
231
232                 BT_DBG("ACL header: dlen %d", dlen);
233
234                 h4_check_data_len(h4, hu, protoid, dlen);
235                 continue;
236
237             case H4_W4_SCO_HDR:
238                 sh = hci_sco_hdr(h4->rx_skb);
239
240                 BT_DBG("SCO header: dlen %d", sh->dlen);
241
242                 h4_check_data_len(h4, hu, protoid, sh->dlen);
243                 continue;
244
245             case FM_W4_EVENT_HDR:
246                 fm = (struct fm_event_hdr *)h4->rx_skb->data;
247                 BT_DBG("FM Header: evt 0x%2.2x plen %d",
248                     fm->event, fm->plen);
249                 h4_check_data_len(h4, hu, PROTO_SH_FM, fm->plen);
250                 continue;
251
252             }
253         }
254
255         /* H4_W4_PACKET_TYPE */
256         switch (*ptr) {
257         case HCI_EVENT_PKT:
258             BT_DBG("Event packet");
259
260             BT_DBG("%2x %2x %2x %2x",ptr[0], ptr[1], ptr[2], ptr[3]);
261             BT_DBG("%2x %2x %2x %2x",ptr[4], ptr[5], ptr[6], ptr[7]);
262             if ( ptr[4] == 0x15 && ptr[5] == 0xfc )
263             {
264                BT_DBG("FM Event change to FM CH8 packet");
265                type = FM_CH8_PKT;
266                h4->rx_state = FM_W4_EVENT_HDR;
267                h4->rx_count = FM_EVENT_HDR_SIZE;
268                protoid = PROTO_SH_FM;
269             }
270             else
271             {
272                BT_DBG("FM Event is not detected");
273                h4->rx_state = H4_W4_EVENT_HDR;
274                h4->rx_count = HCI_EVENT_HDR_SIZE;
275                type = HCI_EVENT_PKT;
276                protoid = PROTO_SH_BT;
277             }
278             break;
279
280         case HCI_ACLDATA_PKT:
281             BT_DBG("ACL packet");
282             h4->rx_state = H4_W4_ACL_HDR;
283             h4->rx_count = HCI_ACL_HDR_SIZE;
284             type = HCI_ACLDATA_PKT;
285             protoid = PROTO_SH_BT;
286             break;
287
288         case HCI_SCODATA_PKT:
289             BT_DBG("SCO packet");
290             h4->rx_state = H4_W4_SCO_HDR;
291             h4->rx_count = HCI_SCO_HDR_SIZE;
292             type = HCI_SCODATA_PKT;
293             protoid = PROTO_SH_BT;
294             break;
295
296         /* Channel 8(FM) packet */
297         case FM_CH8_PKT:
298             BT_DBG("FM CH8 packet");
299             type = FM_CH8_PKT;
300             h4->rx_state = FM_W4_EVENT_HDR;
301             h4->rx_count = FM_EVENT_HDR_SIZE;
302             protoid = PROTO_SH_FM;
303             break;
304
305         default:
306             BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
307             hu->hdev->stat.err_rx++;
308             ptr++; count--;
309             continue;
310         };
311
312         ptr++; count--;
313
314         switch (protoid)
315         {
316             case PROTO_SH_BT:
317             case PROTO_SH_FM:
318                 /* Allocate new packet to hold received data */
319                 h4->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
320                 if (!h4->rx_skb)
321                 {
322                     BT_ERR("Can't allocate mem for new packet");
323                     h4->rx_state = H4_W4_PACKET_TYPE;
324                     h4->rx_count = 0;
325                     return -ENOMEM;
326                 }
327                 h4->rx_skb->dev = (void *) hu->hdev;
328                 sh_ldisc_cb(h4->rx_skb)->pkt_type = type;
329                 break;
330 #if 0
331             case PROTO_SH_FM:    /* for FM */
332                 h4->rx_skb = bt_skb_alloc(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
333                 if (!h4->rx_skb)
334                 {
335                     BT_ERR("Can't allocate mem for new packet");
336                     h4->rx_state = H4_W4_PACKET_TYPE;
337                     h4->rx_count = 0;
338                     return -ENOMEM;
339                 }
340                 /* place holder 0x08 */
341                 /* skb_reserve(h4->rx_skb, 1); */
342                 sh_ldisc_cb(h4->rx_skb)->pkt_type = FM_CH8_PKT;
343                 break;
344 #endif
345             case PROTO_SH_MAX:
346             case PROTO_SH_GPS:
347                 break;
348         }
349
350     }
351
352     return count;
353
354 #else
355         int ret;
356
357         if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
358                 return -EUNATCH;
359
360         ret = hci_recv_stream_fragment(hu->hdev, data, count);
361         if (ret < 0) {
362                 BT_ERR("Frame Reassembly Failed");
363                 return ret;
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         return skb_dequeue(&h4->txq);
374 }
375
376 static struct hci_uart_proto h4p = {
377         .id             = HCI_UART_H4,
378         .open           = h4_open,
379         .close          = h4_close,
380         .recv           = h4_recv,
381         .enqueue        = h4_enqueue,
382         .dequeue        = h4_dequeue,
383         .flush          = h4_flush,
384 };
385
386 int __init h4_init(void)
387 {
388         int err = hci_uart_register_proto(&h4p);
389
390         if (!err)
391                 BT_INFO("HCI H4 protocol initialized");
392         else
393                 BT_ERR("HCI H4 protocol registration failed");
394
395         return err;
396 }
397
398 int __exit h4_deinit(void)
399 {
400         return hci_uart_unregister_proto(&h4p);
401 }