xref: /linux/drivers/bluetooth/btrsi.c (revision b693b51e0829b96a5c43f45c3fba3d11f6f09d2f)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2017 Redpine Signals Inc.
4  *
5  */
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
10 #include <linux/unaligned.h>
11 #include <net/rsi_91x.h>
12 
13 #define RSI_DMA_ALIGN	8
14 #define RSI_FRAME_DESC_SIZE	16
15 #define RSI_HEADROOM_FOR_BT_HAL	(RSI_FRAME_DESC_SIZE + RSI_DMA_ALIGN)
16 
17 struct rsi_hci_adapter {
18 	void *priv;
19 	struct rsi_proto_ops *proto_ops;
20 	struct hci_dev *hdev;
21 };
22 
23 static int rsi_hci_open(struct hci_dev *hdev)
24 {
25 	return 0;
26 }
27 
28 static int rsi_hci_close(struct hci_dev *hdev)
29 {
30 	return 0;
31 }
32 
33 static int rsi_hci_flush(struct hci_dev *hdev)
34 {
35 	return 0;
36 }
37 
38 static int rsi_hci_send_pkt(struct hci_dev *hdev, struct sk_buff *skb)
39 {
40 	struct rsi_hci_adapter *h_adapter = hci_get_drvdata(hdev);
41 	struct sk_buff *new_skb = NULL;
42 
43 	switch (hci_skb_pkt_type(skb)) {
44 	case HCI_COMMAND_PKT:
45 		hdev->stat.cmd_tx++;
46 		break;
47 	case HCI_ACLDATA_PKT:
48 		hdev->stat.acl_tx++;
49 		break;
50 	case HCI_SCODATA_PKT:
51 		hdev->stat.sco_tx++;
52 		break;
53 	}
54 
55 	if (skb_headroom(skb) < RSI_HEADROOM_FOR_BT_HAL) {
56 		/* Insufficient skb headroom - allocate a new skb */
57 		new_skb = skb_realloc_headroom(skb, RSI_HEADROOM_FOR_BT_HAL);
58 		if (unlikely(!new_skb))
59 			return -ENOMEM;
60 		bt_cb(new_skb)->pkt_type = hci_skb_pkt_type(skb);
61 		kfree_skb(skb);
62 		skb = new_skb;
63 		if (!IS_ALIGNED((unsigned long)skb->data, RSI_DMA_ALIGN)) {
64 			u8 *skb_data = skb->data;
65 			int skb_len = skb->len;
66 
67 			skb_push(skb, RSI_DMA_ALIGN);
68 			skb_pull(skb, PTR_ALIGN(skb->data,
69 						RSI_DMA_ALIGN) - skb->data);
70 			memmove(skb->data, skb_data, skb_len);
71 			skb_trim(skb, skb_len);
72 		}
73 	}
74 
75 	return h_adapter->proto_ops->coex_send_pkt(h_adapter->priv, skb,
76 						   RSI_BT_Q);
77 }
78 
79 static int rsi_hci_recv_pkt(void *priv, const u8 *pkt)
80 {
81 	struct rsi_hci_adapter *h_adapter = priv;
82 	struct hci_dev *hdev = h_adapter->hdev;
83 	struct sk_buff *skb;
84 	int pkt_len = get_unaligned_le16(pkt) & 0x0fff;
85 
86 	skb = dev_alloc_skb(pkt_len);
87 	if (!skb)
88 		return -ENOMEM;
89 
90 	memcpy(skb->data, pkt + RSI_FRAME_DESC_SIZE, pkt_len);
91 	skb_put(skb, pkt_len);
92 	h_adapter->hdev->stat.byte_rx += skb->len;
93 
94 	hci_skb_pkt_type(skb) = pkt[14];
95 
96 	return hci_recv_frame(hdev, skb);
97 }
98 
99 static int rsi_hci_attach(void *priv, struct rsi_proto_ops *ops)
100 {
101 	struct rsi_hci_adapter *h_adapter = NULL;
102 	struct hci_dev *hdev;
103 	int err = 0;
104 
105 	h_adapter = kzalloc_obj(*h_adapter);
106 	if (!h_adapter)
107 		return -ENOMEM;
108 
109 	h_adapter->priv = priv;
110 	ops->set_bt_context(priv, h_adapter);
111 	h_adapter->proto_ops = ops;
112 
113 	hdev = hci_alloc_dev();
114 	if (!hdev) {
115 		BT_ERR("Failed to alloc HCI device");
116 		goto err;
117 	}
118 
119 	h_adapter->hdev = hdev;
120 
121 	if (ops->get_host_intf(priv) == RSI_HOST_INTF_SDIO)
122 		hdev->bus = HCI_SDIO;
123 	else
124 		hdev->bus = HCI_USB;
125 
126 	hci_set_drvdata(hdev, h_adapter);
127 	hdev->open = rsi_hci_open;
128 	hdev->close = rsi_hci_close;
129 	hdev->flush = rsi_hci_flush;
130 	hdev->send = rsi_hci_send_pkt;
131 
132 	err = hci_register_dev(hdev);
133 	if (err < 0) {
134 		BT_ERR("HCI registration failed with errcode %d", err);
135 		hci_free_dev(hdev);
136 		goto err;
137 	}
138 
139 	return 0;
140 err:
141 	h_adapter->hdev = NULL;
142 	kfree(h_adapter);
143 	return -EINVAL;
144 }
145 
146 static void rsi_hci_detach(void *priv)
147 {
148 	struct rsi_hci_adapter *h_adapter = priv;
149 	struct hci_dev *hdev;
150 
151 	if (!h_adapter)
152 		return;
153 
154 	hdev = h_adapter->hdev;
155 	if (hdev) {
156 		hci_unregister_dev(hdev);
157 		hci_free_dev(hdev);
158 		h_adapter->hdev = NULL;
159 	}
160 
161 	kfree(h_adapter);
162 }
163 
164 const struct rsi_mod_ops rsi_bt_ops = {
165 	.attach	= rsi_hci_attach,
166 	.detach	= rsi_hci_detach,
167 	.recv_pkt = rsi_hci_recv_pkt,
168 };
169 EXPORT_SYMBOL(rsi_bt_ops);
170 
171 static int rsi_91x_bt_module_init(void)
172 {
173 	return 0;
174 }
175 
176 static void rsi_91x_bt_module_exit(void)
177 {
178 	return;
179 }
180 
181 module_init(rsi_91x_bt_module_init);
182 module_exit(rsi_91x_bt_module_exit);
183 MODULE_AUTHOR("Redpine Signals Inc");
184 MODULE_DESCRIPTION("RSI BT driver");
185 MODULE_LICENSE("Dual BSD/GPL");
186