xref: /linux/drivers/bluetooth/hci_h4.c (revision 0ffac654e95c1bdfe2d4edf28fb18d6ba1f103e6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Bluetooth HCI UART driver
5  *
6  *  Copyright (C) 2000-2001  Qualcomm Incorporated
7  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
8  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/fcntl.h>
17 #include <linux/interrupt.h>
18 #include <linux/ptrace.h>
19 #include <linux/poll.h>
20 
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/signal.h>
26 #include <linux/ioctl.h>
27 #include <linux/skbuff.h>
28 #include <linux/unaligned.h>
29 
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32 
33 #include "hci_uart.h"
34 
35 struct h4_struct {
36 	struct sk_buff *rx_skb;
37 	struct sk_buff_head txq;
38 };
39 
40 /* Initialize protocol */
41 static int h4_open(struct hci_uart *hu)
42 {
43 	struct h4_struct *h4;
44 
45 	BT_DBG("hu %p", hu);
46 
47 	h4 = kzalloc_obj(*h4);
48 	if (!h4)
49 		return -ENOMEM;
50 
51 	skb_queue_head_init(&h4->txq);
52 
53 	hu->priv = h4;
54 	return 0;
55 }
56 
57 /* Flush protocol data */
58 static int h4_flush(struct hci_uart *hu)
59 {
60 	struct h4_struct *h4 = hu->priv;
61 
62 	BT_DBG("hu %p", hu);
63 
64 	skb_queue_purge(&h4->txq);
65 
66 	return 0;
67 }
68 
69 /* Close protocol */
70 static int h4_close(struct hci_uart *hu)
71 {
72 	struct h4_struct *h4 = hu->priv;
73 
74 	BT_DBG("hu %p", hu);
75 
76 	skb_queue_purge(&h4->txq);
77 
78 	kfree_skb(h4->rx_skb);
79 
80 	hu->priv = NULL;
81 	kfree(h4);
82 
83 	return 0;
84 }
85 
86 /* Enqueue frame for transmission (padding, crc, etc) */
87 static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
88 {
89 	struct h4_struct *h4 = hu->priv;
90 
91 	BT_DBG("hu %p skb %p", hu, skb);
92 
93 	/* Prepend skb with frame type */
94 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
95 	skb_queue_tail(&h4->txq, skb);
96 
97 	return 0;
98 }
99 
100 static const struct h4_recv_pkt h4_recv_pkts[] = {
101 	{ H4_RECV_ACL,   .recv = hci_recv_frame },
102 	{ H4_RECV_SCO,   .recv = hci_recv_frame },
103 	{ H4_RECV_EVENT, .recv = hci_recv_frame },
104 	{ H4_RECV_ISO,   .recv = hci_recv_frame },
105 };
106 
107 /* Recv data */
108 static int h4_recv(struct hci_uart *hu, const void *data, int count)
109 {
110 	struct h4_struct *h4 = hu->priv;
111 
112 	h4->rx_skb = h4_recv_buf(hu, h4->rx_skb, data, count,
113 				 h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
114 	if (IS_ERR(h4->rx_skb)) {
115 		int err = PTR_ERR(h4->rx_skb);
116 		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
117 		h4->rx_skb = NULL;
118 		return err;
119 	}
120 
121 	return count;
122 }
123 
124 static struct sk_buff *h4_dequeue(struct hci_uart *hu)
125 {
126 	struct h4_struct *h4 = hu->priv;
127 	return skb_dequeue(&h4->txq);
128 }
129 
130 static const struct hci_uart_proto h4p = {
131 	.id		= HCI_UART_H4,
132 	.name		= "H4",
133 	.open		= h4_open,
134 	.close		= h4_close,
135 	.recv		= h4_recv,
136 	.enqueue	= h4_enqueue,
137 	.dequeue	= h4_dequeue,
138 	.flush		= h4_flush,
139 };
140 
141 int __init h4_init(void)
142 {
143 	return hci_uart_register_proto(&h4p);
144 }
145 
146 int __exit h4_deinit(void)
147 {
148 	return hci_uart_unregister_proto(&h4p);
149 }
150 
151 struct sk_buff *h4_recv_buf(struct hci_uart *hu, struct sk_buff *skb,
152 			    const unsigned char *buffer, int count,
153 			    const struct h4_recv_pkt *pkts, int pkts_count)
154 {
155 	u8 alignment = hu->alignment ? hu->alignment : 1;
156 	struct hci_dev *hdev = hu->hdev;
157 
158 	/* Check for error from previous call */
159 	if (IS_ERR(skb))
160 		skb = NULL;
161 
162 	while (count) {
163 		int i, len;
164 
165 		/* remove padding bytes from buffer */
166 		for (; hu->padding && count > 0; hu->padding--) {
167 			count--;
168 			buffer++;
169 		}
170 		if (!count)
171 			break;
172 
173 		if (!skb) {
174 			for (i = 0; i < pkts_count; i++) {
175 				if (buffer[0] != (&pkts[i])->type)
176 					continue;
177 
178 				skb = bt_skb_alloc((&pkts[i])->maxlen,
179 						   GFP_ATOMIC);
180 				if (!skb)
181 					return ERR_PTR(-ENOMEM);
182 
183 				hci_skb_pkt_type(skb) = (&pkts[i])->type;
184 				hci_skb_expect(skb) = (&pkts[i])->hlen;
185 				break;
186 			}
187 
188 			/* Check for invalid packet type */
189 			if (!skb)
190 				return ERR_PTR(-EILSEQ);
191 
192 			count -= 1;
193 			buffer += 1;
194 		}
195 
196 		len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
197 		skb_put_data(skb, buffer, len);
198 
199 		count -= len;
200 		buffer += len;
201 
202 		/* Check for partial packet */
203 		if (skb->len < hci_skb_expect(skb))
204 			continue;
205 
206 		for (i = 0; i < pkts_count; i++) {
207 			if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
208 				break;
209 		}
210 
211 		if (i >= pkts_count) {
212 			kfree_skb(skb);
213 			return ERR_PTR(-EILSEQ);
214 		}
215 
216 		if (skb->len == (&pkts[i])->hlen) {
217 			u16 dlen;
218 
219 			switch ((&pkts[i])->lsize) {
220 			case 0:
221 				/* No variable data length */
222 				dlen = 0;
223 				break;
224 			case 1:
225 				/* Single octet variable length */
226 				dlen = skb->data[(&pkts[i])->loff];
227 				hci_skb_expect(skb) += dlen;
228 
229 				if (skb_tailroom(skb) < dlen) {
230 					kfree_skb(skb);
231 					return ERR_PTR(-EMSGSIZE);
232 				}
233 				break;
234 			case 2:
235 				/* Double octet variable length */
236 				dlen = get_unaligned_le16(skb->data +
237 							  (&pkts[i])->loff);
238 				hci_skb_expect(skb) += dlen;
239 
240 				if (skb_tailroom(skb) < dlen) {
241 					kfree_skb(skb);
242 					return ERR_PTR(-EMSGSIZE);
243 				}
244 				break;
245 			default:
246 				/* Unsupported variable length */
247 				kfree_skb(skb);
248 				return ERR_PTR(-EILSEQ);
249 			}
250 
251 			if (!dlen) {
252 				hu->padding = (skb->len + 1) % alignment;
253 				hu->padding = (alignment - hu->padding) % alignment;
254 
255 				/* No more data, complete frame */
256 				(&pkts[i])->recv(hdev, skb);
257 				skb = NULL;
258 			}
259 		} else {
260 			hu->padding = (skb->len + 1) % alignment;
261 			hu->padding = (alignment - hu->padding) % alignment;
262 
263 			/* Complete frame */
264 			(&pkts[i])->recv(hdev, skb);
265 			skb = NULL;
266 		}
267 	}
268 
269 	return skb;
270 }
271 EXPORT_SYMBOL_GPL(h4_recv_buf);
272