xref: /openbmc/linux/drivers/bluetooth/hci_h4.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *
40372a662SMarcel Holtmann  *  Bluetooth HCI UART driver
50372a662SMarcel Holtmann  *
60372a662SMarcel Holtmann  *  Copyright (C) 2000-2001  Qualcomm Incorporated
70372a662SMarcel Holtmann  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
80372a662SMarcel Holtmann  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/kernel.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/fcntl.h>
171da177e4SLinus Torvalds #include <linux/interrupt.h>
181da177e4SLinus Torvalds #include <linux/ptrace.h>
191da177e4SLinus Torvalds #include <linux/poll.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/tty.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/signal.h>
261da177e4SLinus Torvalds #include <linux/ioctl.h>
271da177e4SLinus Torvalds #include <linux/skbuff.h>
2879b8df93SMarcel Holtmann #include <asm/unaligned.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
311da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
320372a662SMarcel Holtmann 
331da177e4SLinus Torvalds #include "hci_uart.h"
341da177e4SLinus Torvalds 
350372a662SMarcel Holtmann struct h4_struct {
36c27799f9SMarcel Holtmann 	struct sk_buff *rx_skb;
370372a662SMarcel Holtmann 	struct sk_buff_head txq;
380372a662SMarcel Holtmann };
390372a662SMarcel Holtmann 
401da177e4SLinus Torvalds /* Initialize protocol */
h4_open(struct hci_uart * hu)411da177e4SLinus Torvalds static int h4_open(struct hci_uart *hu)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	struct h4_struct *h4;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	BT_DBG("hu %p", hu);
461da177e4SLinus Torvalds 
47fdcd1661SDavid Herrmann 	h4 = kzalloc(sizeof(*h4), GFP_KERNEL);
481da177e4SLinus Torvalds 	if (!h4)
491da177e4SLinus Torvalds 		return -ENOMEM;
500372a662SMarcel Holtmann 
511da177e4SLinus Torvalds 	skb_queue_head_init(&h4->txq);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	hu->priv = h4;
541da177e4SLinus Torvalds 	return 0;
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* Flush protocol data */
h4_flush(struct hci_uart * hu)581da177e4SLinus Torvalds static int h4_flush(struct hci_uart *hu)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct h4_struct *h4 = hu->priv;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	BT_DBG("hu %p", hu);
630372a662SMarcel Holtmann 
641da177e4SLinus Torvalds 	skb_queue_purge(&h4->txq);
650372a662SMarcel Holtmann 
661da177e4SLinus Torvalds 	return 0;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* Close protocol */
h4_close(struct hci_uart * hu)701da177e4SLinus Torvalds static int h4_close(struct hci_uart *hu)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct h4_struct *h4 = hu->priv;
730372a662SMarcel Holtmann 
741da177e4SLinus Torvalds 	BT_DBG("hu %p", hu);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	skb_queue_purge(&h4->txq);
770372a662SMarcel Holtmann 
78c27799f9SMarcel Holtmann 	kfree_skb(h4->rx_skb);
79c27799f9SMarcel Holtmann 
801da177e4SLinus Torvalds 	hu->priv = NULL;
811da177e4SLinus Torvalds 	kfree(h4);
820372a662SMarcel Holtmann 
831da177e4SLinus Torvalds 	return 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
866bd023c4SChristophe JAILLET /* Enqueue frame for transmission (padding, crc, etc) */
h4_enqueue(struct hci_uart * hu,struct sk_buff * skb)871da177e4SLinus Torvalds static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	struct h4_struct *h4 = hu->priv;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	BT_DBG("hu %p skb %p", hu, skb);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	/* Prepend skb with frame type */
94618e8bc2SMarcel Holtmann 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
951da177e4SLinus Torvalds 	skb_queue_tail(&h4->txq, skb);
960372a662SMarcel Holtmann 
971da177e4SLinus Torvalds 	return 0;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
10079b8df93SMarcel Holtmann static const struct h4_recv_pkt h4_recv_pkts[] = {
10179b8df93SMarcel Holtmann 	{ H4_RECV_ACL,   .recv = hci_recv_frame },
10279b8df93SMarcel Holtmann 	{ H4_RECV_SCO,   .recv = hci_recv_frame },
10379b8df93SMarcel Holtmann 	{ H4_RECV_EVENT, .recv = hci_recv_frame },
104ef564119SLuiz Augusto von Dentz 	{ H4_RECV_ISO,   .recv = hci_recv_frame },
10579b8df93SMarcel Holtmann };
10679b8df93SMarcel Holtmann 
1071da177e4SLinus Torvalds /* Recv data */
h4_recv(struct hci_uart * hu,const void * data,int count)1089d1c40ebSMarcel Holtmann static int h4_recv(struct hci_uart *hu, const void *data, int count)
1091da177e4SLinus Torvalds {
110c27799f9SMarcel Holtmann 	struct h4_struct *h4 = hu->priv;
111b86ed368SGustavo F. Padovan 
112c2578202SChan-yeol Park 	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
113c2578202SChan-yeol Park 		return -EUNATCH;
114c2578202SChan-yeol Park 
11579b8df93SMarcel Holtmann 	h4->rx_skb = h4_recv_buf(hu->hdev, h4->rx_skb, data, count,
11679b8df93SMarcel Holtmann 				 h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
117c27799f9SMarcel Holtmann 	if (IS_ERR(h4->rx_skb)) {
118c27799f9SMarcel Holtmann 		int err = PTR_ERR(h4->rx_skb);
1192064ee33SMarcel Holtmann 		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
12037134167SChan-yeol Park 		h4->rx_skb = NULL;
121c27799f9SMarcel Holtmann 		return err;
122b86ed368SGustavo F. Padovan 	}
1230372a662SMarcel Holtmann 
1241da177e4SLinus Torvalds 	return count;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
h4_dequeue(struct hci_uart * hu)1271da177e4SLinus Torvalds static struct sk_buff *h4_dequeue(struct hci_uart *hu)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct h4_struct *h4 = hu->priv;
1301da177e4SLinus Torvalds 	return skb_dequeue(&h4->txq);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1334ee7ef19SMarcel Holtmann static const struct hci_uart_proto h4p = {
1341da177e4SLinus Torvalds 	.id		= HCI_UART_H4,
1357c40fb8dSMarcel Holtmann 	.name		= "H4",
1361da177e4SLinus Torvalds 	.open		= h4_open,
1371da177e4SLinus Torvalds 	.close		= h4_close,
1381da177e4SLinus Torvalds 	.recv		= h4_recv,
1391da177e4SLinus Torvalds 	.enqueue	= h4_enqueue,
1401da177e4SLinus Torvalds 	.dequeue	= h4_dequeue,
1411da177e4SLinus Torvalds 	.flush		= h4_flush,
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
h4_init(void)144f2b94bb9SGustavo F. Padovan int __init h4_init(void)
1451da177e4SLinus Torvalds {
14601009eecSMarcel Holtmann 	return hci_uart_register_proto(&h4p);
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
h4_deinit(void)149f2b94bb9SGustavo F. Padovan int __exit h4_deinit(void)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	return hci_uart_unregister_proto(&h4p);
1521da177e4SLinus Torvalds }
153e1a38d70SMarcel Holtmann 
h4_recv_buf(struct hci_dev * hdev,struct sk_buff * skb,const unsigned char * buffer,int count,const struct h4_recv_pkt * pkts,int pkts_count)154e1a38d70SMarcel Holtmann struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
15579b8df93SMarcel Holtmann 			    const unsigned char *buffer, int count,
15679b8df93SMarcel Holtmann 			    const struct h4_recv_pkt *pkts, int pkts_count)
157e1a38d70SMarcel Holtmann {
158aeac3014SSebastian Reichel 	struct hci_uart *hu = hci_get_drvdata(hdev);
159fb776481SLoic Poulain 	u8 alignment = hu->alignment ? hu->alignment : 1;
160aeac3014SSebastian Reichel 
1611dc2d785SMyungho Jung 	/* Check for error from previous call */
1621dc2d785SMyungho Jung 	if (IS_ERR(skb))
1631dc2d785SMyungho Jung 		skb = NULL;
1641dc2d785SMyungho Jung 
165e1a38d70SMarcel Holtmann 	while (count) {
16679b8df93SMarcel Holtmann 		int i, len;
167e1a38d70SMarcel Holtmann 
168aeac3014SSebastian Reichel 		/* remove padding bytes from buffer */
169aeac3014SSebastian Reichel 		for (; hu->padding && count > 0; hu->padding--) {
170aeac3014SSebastian Reichel 			count--;
171aeac3014SSebastian Reichel 			buffer++;
172aeac3014SSebastian Reichel 		}
173aeac3014SSebastian Reichel 		if (!count)
174aeac3014SSebastian Reichel 			break;
175aeac3014SSebastian Reichel 
176e1a38d70SMarcel Holtmann 		if (!skb) {
17779b8df93SMarcel Holtmann 			for (i = 0; i < pkts_count; i++) {
17879b8df93SMarcel Holtmann 				if (buffer[0] != (&pkts[i])->type)
17979b8df93SMarcel Holtmann 					continue;
18079b8df93SMarcel Holtmann 
18179b8df93SMarcel Holtmann 				skb = bt_skb_alloc((&pkts[i])->maxlen,
182e1a38d70SMarcel Holtmann 						   GFP_ATOMIC);
183e1a38d70SMarcel Holtmann 				if (!skb)
184e1a38d70SMarcel Holtmann 					return ERR_PTR(-ENOMEM);
185e1a38d70SMarcel Holtmann 
186618e8bc2SMarcel Holtmann 				hci_skb_pkt_type(skb) = (&pkts[i])->type;
187618e8bc2SMarcel Holtmann 				hci_skb_expect(skb) = (&pkts[i])->hlen;
188e1a38d70SMarcel Holtmann 				break;
189e1a38d70SMarcel Holtmann 			}
190e1a38d70SMarcel Holtmann 
19179b8df93SMarcel Holtmann 			/* Check for invalid packet type */
19279b8df93SMarcel Holtmann 			if (!skb)
19379b8df93SMarcel Holtmann 				return ERR_PTR(-EILSEQ);
19479b8df93SMarcel Holtmann 
195e1a38d70SMarcel Holtmann 			count -= 1;
196e1a38d70SMarcel Holtmann 			buffer += 1;
197e1a38d70SMarcel Holtmann 		}
198e1a38d70SMarcel Holtmann 
199618e8bc2SMarcel Holtmann 		len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
20059ae1d12SJohannes Berg 		skb_put_data(skb, buffer, len);
201e1a38d70SMarcel Holtmann 
202e1a38d70SMarcel Holtmann 		count -= len;
203e1a38d70SMarcel Holtmann 		buffer += len;
204e1a38d70SMarcel Holtmann 
20579b8df93SMarcel Holtmann 		/* Check for partial packet */
206618e8bc2SMarcel Holtmann 		if (skb->len < hci_skb_expect(skb))
20779b8df93SMarcel Holtmann 			continue;
208e1a38d70SMarcel Holtmann 
20979b8df93SMarcel Holtmann 		for (i = 0; i < pkts_count; i++) {
210618e8bc2SMarcel Holtmann 			if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
211e1a38d70SMarcel Holtmann 				break;
212e1a38d70SMarcel Holtmann 		}
213e1a38d70SMarcel Holtmann 
21479b8df93SMarcel Holtmann 		if (i >= pkts_count) {
21579b8df93SMarcel Holtmann 			kfree_skb(skb);
21679b8df93SMarcel Holtmann 			return ERR_PTR(-EILSEQ);
21779b8df93SMarcel Holtmann 		}
21879b8df93SMarcel Holtmann 
21979b8df93SMarcel Holtmann 		if (skb->len == (&pkts[i])->hlen) {
22079b8df93SMarcel Holtmann 			u16 dlen;
22179b8df93SMarcel Holtmann 
22279b8df93SMarcel Holtmann 			switch ((&pkts[i])->lsize) {
22379b8df93SMarcel Holtmann 			case 0:
22479b8df93SMarcel Holtmann 				/* No variable data length */
225e5499167SLoic Poulain 				dlen = 0;
22679b8df93SMarcel Holtmann 				break;
22779b8df93SMarcel Holtmann 			case 1:
22879b8df93SMarcel Holtmann 				/* Single octet variable length */
22979b8df93SMarcel Holtmann 				dlen = skb->data[(&pkts[i])->loff];
230618e8bc2SMarcel Holtmann 				hci_skb_expect(skb) += dlen;
23179b8df93SMarcel Holtmann 
23279b8df93SMarcel Holtmann 				if (skb_tailroom(skb) < dlen) {
23379b8df93SMarcel Holtmann 					kfree_skb(skb);
23479b8df93SMarcel Holtmann 					return ERR_PTR(-EMSGSIZE);
23579b8df93SMarcel Holtmann 				}
23679b8df93SMarcel Holtmann 				break;
23779b8df93SMarcel Holtmann 			case 2:
23879b8df93SMarcel Holtmann 				/* Double octet variable length */
23979b8df93SMarcel Holtmann 				dlen = get_unaligned_le16(skb->data +
24079b8df93SMarcel Holtmann 							  (&pkts[i])->loff);
241618e8bc2SMarcel Holtmann 				hci_skb_expect(skb) += dlen;
24279b8df93SMarcel Holtmann 
24379b8df93SMarcel Holtmann 				if (skb_tailroom(skb) < dlen) {
24479b8df93SMarcel Holtmann 					kfree_skb(skb);
24579b8df93SMarcel Holtmann 					return ERR_PTR(-EMSGSIZE);
24679b8df93SMarcel Holtmann 				}
24779b8df93SMarcel Holtmann 				break;
24879b8df93SMarcel Holtmann 			default:
24979b8df93SMarcel Holtmann 				/* Unsupported variable length */
25079b8df93SMarcel Holtmann 				kfree_skb(skb);
25179b8df93SMarcel Holtmann 				return ERR_PTR(-EILSEQ);
25279b8df93SMarcel Holtmann 			}
253e5499167SLoic Poulain 
254e5499167SLoic Poulain 			if (!dlen) {
255*dd2ac1d6SZijun Hu 				hu->padding = (skb->len + 1) % alignment;
256aeac3014SSebastian Reichel 				hu->padding = (alignment - hu->padding) % alignment;
257aeac3014SSebastian Reichel 
258e5499167SLoic Poulain 				/* No more data, complete frame */
259e5499167SLoic Poulain 				(&pkts[i])->recv(hdev, skb);
260e5499167SLoic Poulain 				skb = NULL;
261e5499167SLoic Poulain 			}
26279b8df93SMarcel Holtmann 		} else {
263*dd2ac1d6SZijun Hu 			hu->padding = (skb->len + 1) % alignment;
264aeac3014SSebastian Reichel 			hu->padding = (alignment - hu->padding) % alignment;
265aeac3014SSebastian Reichel 
266e1a38d70SMarcel Holtmann 			/* Complete frame */
26779b8df93SMarcel Holtmann 			(&pkts[i])->recv(hdev, skb);
268e1a38d70SMarcel Holtmann 			skb = NULL;
269e1a38d70SMarcel Holtmann 		}
270e1a38d70SMarcel Holtmann 	}
271e1a38d70SMarcel Holtmann 
272e1a38d70SMarcel Holtmann 	return skb;
273e1a38d70SMarcel Holtmann }
274943cc592SMarcel Holtmann EXPORT_SYMBOL_GPL(h4_recv_buf);
275