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 #include "hci_uart_bcm.h"
48 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
54 unsigned long rx_state;
55 unsigned long rx_count;
56 struct sk_buff *rx_skb;
57 struct sk_buff_head txq;
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
66 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
67 #define FM_W4_EVENT_HDR 5
70 /* Initialize protocol */
71 static int h4_open(struct hci_uart *hu)
77 h4 = kzalloc(sizeof(*h4), GFP_KERNEL);
81 skb_queue_head_init(&h4->txq);
87 /* Flush protocol data */
88 static int h4_flush(struct hci_uart *hu)
90 struct h4_struct *h4 = hu->priv;
94 skb_queue_purge(&h4->txq);
100 static int h4_close(struct hci_uart *hu)
102 struct h4_struct *h4 = hu->priv;
108 skb_queue_purge(&h4->txq);
110 kfree_skb(h4->rx_skb);
118 /* Enqueue frame for transmittion (padding, crc, etc) */
119 static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
121 struct h4_struct *h4 = hu->priv;
123 BT_DBG("hu %p skb %p", hu, skb);
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);
129 skb_queue_tail(&h4->txq, skb);
134 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
136 * Function to check data length
138 static inline int h4_check_data_len(struct h4_struct *h4,
139 struct hci_uart *hu, int protoid,int len)
141 register int room = skb_tailroom(h4->rx_skb);
143 BT_DBG("len %d room %d", len, room);
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);
151 h4->rx_state = H4_W4_DATA;
156 h4->rx_state = H4_W4_PACKET_TYPE;
164 static int h4_recv(struct hci_uart *hu, void *data, int count)
166 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
167 struct h4_struct *h4 = hu->priv;
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;
176 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, h4->rx_state, 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;
188 switch (h4->rx_state) {
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]);
194 BT_DBG("Complete data");
195 #if defined(CONFIG_RADIO_BCM4343S)
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 )
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))
205 BT_INFO("FM Event change to FM CH8 packet");
207 h4->rx_skb->cb[0] = FM_CH8_PKT;
208 h4->rx_state = FM_W4_EVENT_HDR;
209 protoid = PROTO_SH_FM;
212 hci_uart_route_frame(protoid, hu, h4->rx_skb);
214 h4->rx_state = H4_W4_PACKET_TYPE;
216 protoid = PROTO_SH_MAX;
219 case H4_W4_EVENT_HDR:
220 eh = hci_event_hdr(h4->rx_skb);
222 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
224 h4_check_data_len(h4, hu, protoid, eh->plen);
228 ah = hci_acl_hdr(h4->rx_skb);
230 dlen = __le16_to_cpu(ah->dlen);
232 BT_DBG("ACL header: dlen %d", dlen);
234 h4_check_data_len(h4, hu, protoid, dlen);
238 sh = hci_sco_hdr(h4->rx_skb);
240 BT_DBG("SCO header: dlen %d", sh->dlen);
242 h4_check_data_len(h4, hu, protoid, sh->dlen);
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);
255 /* H4_W4_PACKET_TYPE */
258 BT_DBG("Event packet");
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 )
264 BT_DBG("FM Event change to FM CH8 packet");
266 h4->rx_state = FM_W4_EVENT_HDR;
267 h4->rx_count = FM_EVENT_HDR_SIZE;
268 protoid = PROTO_SH_FM;
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;
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;
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;
296 /* Channel 8(FM) packet */
298 BT_DBG("FM CH8 packet");
300 h4->rx_state = FM_W4_EVENT_HDR;
301 h4->rx_count = FM_EVENT_HDR_SIZE;
302 protoid = PROTO_SH_FM;
306 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
307 hu->hdev->stat.err_rx++;
318 /* Allocate new packet to hold received data */
319 h4->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
322 BT_ERR("Can't allocate mem for new packet");
323 h4->rx_state = H4_W4_PACKET_TYPE;
327 h4->rx_skb->dev = (void *) hu->hdev;
328 sh_ldisc_cb(h4->rx_skb)->pkt_type = type;
331 case PROTO_SH_FM: /* for FM */
332 h4->rx_skb = bt_skb_alloc(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
335 BT_ERR("Can't allocate mem for new packet");
336 h4->rx_state = H4_W4_PACKET_TYPE;
340 /* place holder 0x08 */
341 /* skb_reserve(h4->rx_skb, 1); */
342 sh_ldisc_cb(h4->rx_skb)->pkt_type = FM_CH8_PKT;
357 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
360 ret = hci_recv_stream_fragment(hu->hdev, data, count);
362 BT_ERR("Frame Reassembly Failed");
370 static struct sk_buff *h4_dequeue(struct hci_uart *hu)
372 struct h4_struct *h4 = hu->priv;
373 return skb_dequeue(&h4->txq);
376 static struct hci_uart_proto h4p = {
381 .enqueue = h4_enqueue,
382 .dequeue = h4_dequeue,
386 int __init h4_init(void)
388 int err = hci_uart_register_proto(&h4p);
391 BT_INFO("HCI H4 protocol initialized");
393 BT_ERR("HCI H4 protocol registration failed");
398 int __exit h4_deinit(void)
400 return hci_uart_unregister_proto(&h4p);