Bluetooth: hci_uart: Introduce h4_recv_buf helper function
[deliverable/linux.git] / drivers / bluetooth / hci_h4.c
CommitLineData
1da177e4 1/*
1da177e4 2 *
0372a662
MH
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 *
1da177e4 24 */
1da177e4 25
1da177e4
LT
26#include <linux/module.h>
27
28#include <linux/kernel.h>
29#include <linux/init.h>
1da177e4
LT
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>
0372a662 46
1da177e4 47#include "hci_uart.h"
1da177e4 48
0372a662
MH
49#define VERSION "1.2"
50
51struct h4_struct {
0372a662
MH
52 struct sk_buff_head txq;
53};
54
1da177e4
LT
55/* Initialize protocol */
56static int h4_open(struct hci_uart *hu)
57{
58 struct h4_struct *h4;
0372a662 59
1da177e4 60 BT_DBG("hu %p", hu);
0372a662 61
fdcd1661 62 h4 = kzalloc(sizeof(*h4), GFP_KERNEL);
1da177e4
LT
63 if (!h4)
64 return -ENOMEM;
0372a662 65
1da177e4
LT
66 skb_queue_head_init(&h4->txq);
67
68 hu->priv = h4;
69 return 0;
70}
71
72/* Flush protocol data */
73static int h4_flush(struct hci_uart *hu)
74{
75 struct h4_struct *h4 = hu->priv;
76
77 BT_DBG("hu %p", hu);
0372a662 78
1da177e4 79 skb_queue_purge(&h4->txq);
0372a662 80
1da177e4
LT
81 return 0;
82}
83
84/* Close protocol */
85static int h4_close(struct hci_uart *hu)
86{
87 struct h4_struct *h4 = hu->priv;
0372a662 88
1da177e4
LT
89 hu->priv = NULL;
90
91 BT_DBG("hu %p", hu);
92
93 skb_queue_purge(&h4->txq);
0372a662 94
1da177e4
LT
95 hu->priv = NULL;
96 kfree(h4);
0372a662 97
1da177e4
LT
98 return 0;
99}
100
101/* Enqueue frame for transmittion (padding, crc, etc) */
102static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
103{
104 struct h4_struct *h4 = hu->priv;
105
106 BT_DBG("hu %p skb %p", hu, skb);
107
108 /* Prepend skb with frame type */
0d48d939 109 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1da177e4 110 skb_queue_tail(&h4->txq, skb);
0372a662 111
1da177e4
LT
112 return 0;
113}
114
1da177e4 115/* Recv data */
9d1c40eb 116static int h4_recv(struct hci_uart *hu, const void *data, int count)
1da177e4 117{
b86ed368
GP
118 int ret;
119
c2578202
CP
120 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
121 return -EUNATCH;
122
b86ed368
GP
123 ret = hci_recv_stream_fragment(hu->hdev, data, count);
124 if (ret < 0) {
e9da101f 125 BT_ERR("Frame Reassembly Failed");
b86ed368
GP
126 return ret;
127 }
0372a662 128
1da177e4
LT
129 return count;
130}
131
132static struct sk_buff *h4_dequeue(struct hci_uart *hu)
133{
134 struct h4_struct *h4 = hu->priv;
135 return skb_dequeue(&h4->txq);
136}
137
138static struct hci_uart_proto h4p = {
0372a662
MH
139 .id = HCI_UART_H4,
140 .open = h4_open,
141 .close = h4_close,
142 .recv = h4_recv,
143 .enqueue = h4_enqueue,
144 .dequeue = h4_dequeue,
145 .flush = h4_flush,
1da177e4 146};
0372a662 147
f2b94bb9 148int __init h4_init(void)
1da177e4
LT
149{
150 int err = hci_uart_register_proto(&h4p);
0372a662 151
1da177e4
LT
152 if (!err)
153 BT_INFO("HCI H4 protocol initialized");
154 else
155 BT_ERR("HCI H4 protocol registration failed");
0372a662 156
1da177e4
LT
157 return err;
158}
159
f2b94bb9 160int __exit h4_deinit(void)
1da177e4
LT
161{
162 return hci_uart_unregister_proto(&h4p);
163}
e1a38d70
MH
164
165struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
166 const unsigned char *buffer, int count)
167{
168 while (count) {
169 int len;
170
171 if (!skb) {
172 switch (buffer[0]) {
173 case HCI_ACLDATA_PKT:
174 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE,
175 GFP_ATOMIC);
176 if (!skb)
177 return ERR_PTR(-ENOMEM);
178
179 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
180 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
181 break;
182 case HCI_SCODATA_PKT:
183 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE,
184 GFP_ATOMIC);
185 if (!skb)
186 return ERR_PTR(-ENOMEM);
187
188 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
189 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
190 break;
191 case HCI_EVENT_PKT:
192 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE,
193 GFP_ATOMIC);
194 if (!skb)
195 return ERR_PTR(-ENOMEM);
196
197 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
198 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
199 break;
200 default:
201 return ERR_PTR(-EILSEQ);
202 }
203
204 count -= 1;
205 buffer += 1;
206 }
207
208 len = min_t(uint, bt_cb(skb)->expect, count);
209 memcpy(skb_put(skb, len), buffer, len);
210
211 count -= len;
212 buffer += len;
213 bt_cb(skb)->expect -= len;
214
215 switch (bt_cb(skb)->pkt_type) {
216 case HCI_ACLDATA_PKT:
217 if (skb->len == HCI_ACL_HDR_SIZE) {
218 __le16 dlen = hci_acl_hdr(skb)->dlen;
219
220 /* Complete ACL header */
221 bt_cb(skb)->expect = __le16_to_cpu(dlen);
222
223 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
224 kfree_skb(skb);
225 return ERR_PTR(-EMSGSIZE);
226 }
227 }
228 break;
229 case HCI_SCODATA_PKT:
230 if (skb->len == HCI_SCO_HDR_SIZE) {
231 /* Complete SCO header */
232 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
233
234 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
235 kfree_skb(skb);
236 return ERR_PTR(-EMSGSIZE);
237 }
238 }
239 break;
240 case HCI_EVENT_PKT:
241 if (skb->len == HCI_EVENT_HDR_SIZE) {
242 /* Complete event header */
243 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
244
245 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
246 kfree_skb(skb);
247 return ERR_PTR(-EMSGSIZE);
248 }
249 }
250 break;
251 }
252
253 if (bt_cb(skb)->expect == 0) {
254 /* Complete frame */
255 hci_recv_frame(hdev, skb);
256 skb = NULL;
257 }
258 }
259
260 return skb;
261}
This page took 0.949667 seconds and 5 git commands to generate.