blob: 19ba52005009fdf9c0be865519004e6dce00407f [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Marcel Holtmann0372a662005-10-28 19:20:45 +02004 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12
13#include <linux/kernel.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Marcel Holtmann79b8df92015-04-05 23:44:59 -070028#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
Marcel Holtmann0372a662005-10-28 19:20:45 +020032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "hci_uart.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Marcel Holtmann0372a662005-10-28 19:20:45 +020035struct h4_struct {
Marcel Holtmannc27799f2015-04-04 21:59:25 -070036 struct sk_buff *rx_skb;
Marcel Holtmann0372a662005-10-28 19:20:45 +020037 struct sk_buff_head txq;
38};
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* Initialize protocol */
41static int h4_open(struct hci_uart *hu)
42{
43 struct h4_struct *h4;
Marcel Holtmann0372a662005-10-28 19:20:45 +020044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 BT_DBG("hu %p", hu);
Marcel Holtmann0372a662005-10-28 19:20:45 +020046
David Herrmannfdcd1662012-01-07 15:19:38 +010047 h4 = kzalloc(sizeof(*h4), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (!h4)
49 return -ENOMEM;
Marcel Holtmann0372a662005-10-28 19:20:45 +020050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 skb_queue_head_init(&h4->txq);
52
53 hu->priv = h4;
54 return 0;
55}
56
57/* Flush protocol data */
58static int h4_flush(struct hci_uart *hu)
59{
60 struct h4_struct *h4 = hu->priv;
61
62 BT_DBG("hu %p", hu);
Marcel Holtmann0372a662005-10-28 19:20:45 +020063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 skb_queue_purge(&h4->txq);
Marcel Holtmann0372a662005-10-28 19:20:45 +020065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67}
68
69/* Close protocol */
70static int h4_close(struct hci_uart *hu)
71{
72 struct h4_struct *h4 = hu->priv;
Marcel Holtmann0372a662005-10-28 19:20:45 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 hu->priv = NULL;
75
76 BT_DBG("hu %p", hu);
77
78 skb_queue_purge(&h4->txq);
Marcel Holtmann0372a662005-10-28 19:20:45 +020079
Marcel Holtmannc27799f2015-04-04 21:59:25 -070080 kfree_skb(h4->rx_skb);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 hu->priv = NULL;
83 kfree(h4);
Marcel Holtmann0372a662005-10-28 19:20:45 +020084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86}
87
88/* Enqueue frame for transmittion (padding, crc, etc) */
89static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
90{
91 struct h4_struct *h4 = hu->priv;
92
93 BT_DBG("hu %p skb %p", hu, skb);
94
95 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +010096 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 skb_queue_tail(&h4->txq, skb);
Marcel Holtmann0372a662005-10-28 19:20:45 +020098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100}
101
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700102static const struct h4_recv_pkt h4_recv_pkts[] = {
103 { H4_RECV_ACL, .recv = hci_recv_frame },
104 { H4_RECV_SCO, .recv = hci_recv_frame },
105 { H4_RECV_EVENT, .recv = hci_recv_frame },
106};
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Recv data */
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700109static int h4_recv(struct hci_uart *hu, const void *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Marcel Holtmannc27799f2015-04-04 21:59:25 -0700111 struct h4_struct *h4 = hu->priv;
Gustavo F. Padovanb86ed362011-04-07 18:53:45 -0300112
Chan-yeol Parkc2578202013-04-02 21:24:21 +0900113 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
114 return -EUNATCH;
115
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700116 h4->rx_skb = h4_recv_buf(hu->hdev, h4->rx_skb, data, count,
117 h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
Marcel Holtmannc27799f2015-04-04 21:59:25 -0700118 if (IS_ERR(h4->rx_skb)) {
119 int err = PTR_ERR(h4->rx_skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100120 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Chan-yeol Park37134162015-06-17 21:10:39 +0900121 h4->rx_skb = NULL;
Marcel Holtmannc27799f2015-04-04 21:59:25 -0700122 return err;
Gustavo F. Padovanb86ed362011-04-07 18:53:45 -0300123 }
Marcel Holtmann0372a662005-10-28 19:20:45 +0200124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return count;
126}
127
128static struct sk_buff *h4_dequeue(struct hci_uart *hu)
129{
130 struct h4_struct *h4 = hu->priv;
131 return skb_dequeue(&h4->txq);
132}
133
Marcel Holtmann4ee7ef12015-04-04 22:11:43 -0700134static const struct hci_uart_proto h4p = {
Marcel Holtmann0372a662005-10-28 19:20:45 +0200135 .id = HCI_UART_H4,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700136 .name = "H4",
Marcel Holtmann0372a662005-10-28 19:20:45 +0200137 .open = h4_open,
138 .close = h4_close,
139 .recv = h4_recv,
140 .enqueue = h4_enqueue,
141 .dequeue = h4_dequeue,
142 .flush = h4_flush,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
Marcel Holtmann0372a662005-10-28 19:20:45 +0200144
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300145int __init h4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Marcel Holtmann01009ee2015-04-04 22:27:35 -0700147 return hci_uart_register_proto(&h4p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300150int __exit h4_deinit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 return hci_uart_unregister_proto(&h4p);
153}
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700154
155struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700156 const unsigned char *buffer, int count,
157 const struct h4_recv_pkt *pkts, int pkts_count)
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700158{
Sebastian Reichelaeac3012017-03-28 17:59:34 +0200159 struct hci_uart *hu = hci_get_drvdata(hdev);
Loic Poulainfb776482017-07-29 19:32:31 +0200160 u8 alignment = hu->alignment ? hu->alignment : 1;
Sebastian Reichelaeac3012017-03-28 17:59:34 +0200161
Myungho Jung1dc2d782019-01-22 00:33:26 -0800162 /* Check for error from previous call */
163 if (IS_ERR(skb))
164 skb = NULL;
165
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700166 while (count) {
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700167 int i, len;
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700168
Sebastian Reichelaeac3012017-03-28 17:59:34 +0200169 /* remove padding bytes from buffer */
170 for (; hu->padding && count > 0; hu->padding--) {
171 count--;
172 buffer++;
173 }
174 if (!count)
175 break;
176
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700177 if (!skb) {
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700178 for (i = 0; i < pkts_count; i++) {
179 if (buffer[0] != (&pkts[i])->type)
180 continue;
181
182 skb = bt_skb_alloc((&pkts[i])->maxlen,
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700183 GFP_ATOMIC);
184 if (!skb)
185 return ERR_PTR(-ENOMEM);
186
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100187 hci_skb_pkt_type(skb) = (&pkts[i])->type;
188 hci_skb_expect(skb) = (&pkts[i])->hlen;
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700189 break;
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700190 }
191
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700192 /* Check for invalid packet type */
193 if (!skb)
194 return ERR_PTR(-EILSEQ);
195
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700196 count -= 1;
197 buffer += 1;
198 }
199
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100200 len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200201 skb_put_data(skb, buffer, len);
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700202
203 count -= len;
204 buffer += len;
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700205
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700206 /* Check for partial packet */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100207 if (skb->len < hci_skb_expect(skb))
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700208 continue;
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700209
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700210 for (i = 0; i < pkts_count; i++) {
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100211 if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700212 break;
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700213 }
214
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700215 if (i >= pkts_count) {
216 kfree_skb(skb);
217 return ERR_PTR(-EILSEQ);
218 }
219
220 if (skb->len == (&pkts[i])->hlen) {
221 u16 dlen;
222
223 switch ((&pkts[i])->lsize) {
224 case 0:
225 /* No variable data length */
Loic Poulaine54991672015-08-24 18:57:57 +0200226 dlen = 0;
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700227 break;
228 case 1:
229 /* Single octet variable length */
230 dlen = skb->data[(&pkts[i])->loff];
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100231 hci_skb_expect(skb) += dlen;
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700232
233 if (skb_tailroom(skb) < dlen) {
234 kfree_skb(skb);
235 return ERR_PTR(-EMSGSIZE);
236 }
237 break;
238 case 2:
239 /* Double octet variable length */
240 dlen = get_unaligned_le16(skb->data +
241 (&pkts[i])->loff);
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100242 hci_skb_expect(skb) += dlen;
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700243
244 if (skb_tailroom(skb) < dlen) {
245 kfree_skb(skb);
246 return ERR_PTR(-EMSGSIZE);
247 }
248 break;
249 default:
250 /* Unsupported variable length */
251 kfree_skb(skb);
252 return ERR_PTR(-EILSEQ);
253 }
Loic Poulaine54991672015-08-24 18:57:57 +0200254
255 if (!dlen) {
Sebastian Reichelaeac3012017-03-28 17:59:34 +0200256 hu->padding = (skb->len - 1) % alignment;
257 hu->padding = (alignment - hu->padding) % alignment;
258
Loic Poulaine54991672015-08-24 18:57:57 +0200259 /* No more data, complete frame */
260 (&pkts[i])->recv(hdev, skb);
261 skb = NULL;
262 }
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700263 } else {
Sebastian Reichelaeac3012017-03-28 17:59:34 +0200264 hu->padding = (skb->len - 1) % alignment;
265 hu->padding = (alignment - hu->padding) % alignment;
266
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700267 /* Complete frame */
Marcel Holtmann79b8df92015-04-05 23:44:59 -0700268 (&pkts[i])->recv(hdev, skb);
Marcel Holtmanne1a38d72015-04-04 21:59:24 -0700269 skb = NULL;
270 }
271 }
272
273 return skb;
274}
Marcel Holtmann943cc592015-10-08 03:06:53 +0200275EXPORT_SYMBOL_GPL(h4_recv_buf);