1 /*
2 *
3 * Bluetooth HCI UART driver for marvell devices
4 *
5 * Copyright (C) 2016 Marvell International Ltd.
6 * Copyright (C) 2016 Intel Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/skbuff.h>
27 #include <linux/firmware.h>
28 #include <linux/module.h>
29 #include <linux/tty.h>
30
31 #include <net/bluetooth/bluetooth.h>
32 #include <net/bluetooth/hci_core.h>
33
34 #include "hci_uart.h"
35
36 #define HCI_FW_REQ_PKT 0xA5
37 #define HCI_CHIP_VER_PKT 0xAA
38
39 #define MRVL_ACK 0x5A
40 #define MRVL_NAK 0xBF
41 #define MRVL_RAW_DATA 0x1F
42
43 enum {
44 STATE_CHIP_VER_PENDING,
45 STATE_FW_REQ_PENDING,
46 };
47
48 struct mrvl_data {
49 struct sk_buff *rx_skb;
50 struct sk_buff_head txq;
51 struct sk_buff_head rawq;
52 unsigned long flags;
53 unsigned int tx_len;
54 u8 id, rev;
55 };
56
57 struct hci_mrvl_pkt {
58 __le16 lhs;
59 __le16 rhs;
60 } __packed;
61 #define HCI_MRVL_PKT_SIZE 4
62
mrvl_open(struct hci_uart * hu)63 static int mrvl_open(struct hci_uart *hu)
64 {
65 struct mrvl_data *mrvl;
66
67 BT_DBG("hu %p", hu);
68
69 if (!hci_uart_has_flow_control(hu))
70 return -EOPNOTSUPP;
71
72 mrvl = kzalloc(sizeof(*mrvl), GFP_KERNEL);
73 if (!mrvl)
74 return -ENOMEM;
75
76 skb_queue_head_init(&mrvl->txq);
77 skb_queue_head_init(&mrvl->rawq);
78
79 set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
80
81 hu->priv = mrvl;
82 return 0;
83 }
84
mrvl_close(struct hci_uart * hu)85 static int mrvl_close(struct hci_uart *hu)
86 {
87 struct mrvl_data *mrvl = hu->priv;
88
89 BT_DBG("hu %p", hu);
90
91 skb_queue_purge(&mrvl->txq);
92 skb_queue_purge(&mrvl->rawq);
93 kfree_skb(mrvl->rx_skb);
94 kfree(mrvl);
95
96 hu->priv = NULL;
97 return 0;
98 }
99
mrvl_flush(struct hci_uart * hu)100 static int mrvl_flush(struct hci_uart *hu)
101 {
102 struct mrvl_data *mrvl = hu->priv;
103
104 BT_DBG("hu %p", hu);
105
106 skb_queue_purge(&mrvl->txq);
107 skb_queue_purge(&mrvl->rawq);
108
109 return 0;
110 }
111
mrvl_dequeue(struct hci_uart * hu)112 static struct sk_buff *mrvl_dequeue(struct hci_uart *hu)
113 {
114 struct mrvl_data *mrvl = hu->priv;
115 struct sk_buff *skb;
116
117 skb = skb_dequeue(&mrvl->txq);
118 if (!skb) {
119 /* Any raw data ? */
120 skb = skb_dequeue(&mrvl->rawq);
121 } else {
122 /* Prepend skb with frame type */
123 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
124 }
125
126 return skb;
127 }
128
mrvl_enqueue(struct hci_uart * hu,struct sk_buff * skb)129 static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb)
130 {
131 struct mrvl_data *mrvl = hu->priv;
132
133 skb_queue_tail(&mrvl->txq, skb);
134 return 0;
135 }
136
mrvl_send_ack(struct hci_uart * hu,unsigned char type)137 static void mrvl_send_ack(struct hci_uart *hu, unsigned char type)
138 {
139 struct mrvl_data *mrvl = hu->priv;
140 struct sk_buff *skb;
141
142 /* No H4 payload, only 1 byte header */
143 skb = bt_skb_alloc(0, GFP_ATOMIC);
144 if (!skb) {
145 bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet");
146 return;
147 }
148 hci_skb_pkt_type(skb) = type;
149
150 skb_queue_tail(&mrvl->txq, skb);
151 hci_uart_tx_wakeup(hu);
152 }
153
mrvl_recv_fw_req(struct hci_dev * hdev,struct sk_buff * skb)154 static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb)
155 {
156 struct hci_mrvl_pkt *pkt = (void *)skb->data;
157 struct hci_uart *hu = hci_get_drvdata(hdev);
158 struct mrvl_data *mrvl = hu->priv;
159 int ret = 0;
160
161 if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
162 bt_dev_err(hdev, "Corrupted mrvl header");
163 mrvl_send_ack(hu, MRVL_NAK);
164 ret = -EINVAL;
165 goto done;
166 }
167 mrvl_send_ack(hu, MRVL_ACK);
168
169 if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) {
170 bt_dev_err(hdev, "Received unexpected firmware request");
171 ret = -EINVAL;
172 goto done;
173 }
174
175 mrvl->tx_len = le16_to_cpu(pkt->lhs);
176
177 clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
178 smp_mb__after_atomic();
179 wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING);
180
181 done:
182 kfree_skb(skb);
183 return ret;
184 }
185
mrvl_recv_chip_ver(struct hci_dev * hdev,struct sk_buff * skb)186 static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb)
187 {
188 struct hci_mrvl_pkt *pkt = (void *)skb->data;
189 struct hci_uart *hu = hci_get_drvdata(hdev);
190 struct mrvl_data *mrvl = hu->priv;
191 u16 version = le16_to_cpu(pkt->lhs);
192 int ret = 0;
193
194 if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
195 bt_dev_err(hdev, "Corrupted mrvl header");
196 mrvl_send_ack(hu, MRVL_NAK);
197 ret = -EINVAL;
198 goto done;
199 }
200 mrvl_send_ack(hu, MRVL_ACK);
201
202 if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) {
203 bt_dev_err(hdev, "Received unexpected chip version");
204 goto done;
205 }
206
207 mrvl->id = version;
208 mrvl->rev = version >> 8;
209
210 bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev);
211
212 clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
213 smp_mb__after_atomic();
214 wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING);
215
216 done:
217 kfree_skb(skb);
218 return ret;
219 }
220
221 #define HCI_RECV_CHIP_VER \
222 .type = HCI_CHIP_VER_PKT, \
223 .hlen = HCI_MRVL_PKT_SIZE, \
224 .loff = 0, \
225 .lsize = 0, \
226 .maxlen = HCI_MRVL_PKT_SIZE
227
228 #define HCI_RECV_FW_REQ \
229 .type = HCI_FW_REQ_PKT, \
230 .hlen = HCI_MRVL_PKT_SIZE, \
231 .loff = 0, \
232 .lsize = 0, \
233 .maxlen = HCI_MRVL_PKT_SIZE
234
235 static const struct h4_recv_pkt mrvl_recv_pkts[] = {
236 { H4_RECV_ACL, .recv = hci_recv_frame },
237 { H4_RECV_SCO, .recv = hci_recv_frame },
238 { H4_RECV_EVENT, .recv = hci_recv_frame },
239 { HCI_RECV_FW_REQ, .recv = mrvl_recv_fw_req },
240 { HCI_RECV_CHIP_VER, .recv = mrvl_recv_chip_ver },
241 };
242
mrvl_recv(struct hci_uart * hu,const void * data,int count)243 static int mrvl_recv(struct hci_uart *hu, const void *data, int count)
244 {
245 struct mrvl_data *mrvl = hu->priv;
246
247 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
248 return -EUNATCH;
249
250 mrvl->rx_skb = h4_recv_buf(hu->hdev, mrvl->rx_skb, data, count,
251 mrvl_recv_pkts,
252 ARRAY_SIZE(mrvl_recv_pkts));
253 if (IS_ERR(mrvl->rx_skb)) {
254 int err = PTR_ERR(mrvl->rx_skb);
255 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
256 mrvl->rx_skb = NULL;
257 return err;
258 }
259
260 return count;
261 }
262
mrvl_load_firmware(struct hci_dev * hdev,const char * name)263 static int mrvl_load_firmware(struct hci_dev *hdev, const char *name)
264 {
265 struct hci_uart *hu = hci_get_drvdata(hdev);
266 struct mrvl_data *mrvl = hu->priv;
267 const struct firmware *fw = NULL;
268 const u8 *fw_ptr, *fw_max;
269 int err;
270
271 err = request_firmware(&fw, name, &hdev->dev);
272 if (err < 0) {
273 bt_dev_err(hdev, "Failed to load firmware file %s", name);
274 return err;
275 }
276
277 fw_ptr = fw->data;
278 fw_max = fw->data + fw->size;
279
280 bt_dev_info(hdev, "Loading %s", name);
281
282 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
283
284 while (fw_ptr <= fw_max) {
285 struct sk_buff *skb;
286
287 /* Controller drives the firmware load by sending firmware
288 * request packets containing the expected fragment size.
289 */
290 err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING,
291 TASK_INTERRUPTIBLE,
292 msecs_to_jiffies(2000));
293 if (err == 1) {
294 bt_dev_err(hdev, "Firmware load interrupted");
295 err = -EINTR;
296 break;
297 } else if (err) {
298 bt_dev_err(hdev, "Firmware request timeout");
299 err = -ETIMEDOUT;
300 break;
301 }
302
303 bt_dev_dbg(hdev, "Firmware request, expecting %d bytes",
304 mrvl->tx_len);
305
306 if (fw_ptr == fw_max) {
307 /* Controller requests a null size once firmware is
308 * fully loaded. If controller expects more data, there
309 * is an issue.
310 */
311 if (!mrvl->tx_len) {
312 bt_dev_info(hdev, "Firmware loading complete");
313 } else {
314 bt_dev_err(hdev, "Firmware loading failure");
315 err = -EINVAL;
316 }
317 break;
318 }
319
320 if (fw_ptr + mrvl->tx_len > fw_max) {
321 mrvl->tx_len = fw_max - fw_ptr;
322 bt_dev_dbg(hdev, "Adjusting tx_len to %d",
323 mrvl->tx_len);
324 }
325
326 skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL);
327 if (!skb) {
328 bt_dev_err(hdev, "Failed to alloc mem for FW packet");
329 err = -ENOMEM;
330 break;
331 }
332 bt_cb(skb)->pkt_type = MRVL_RAW_DATA;
333
334 skb_put_data(skb, fw_ptr, mrvl->tx_len);
335 fw_ptr += mrvl->tx_len;
336
337 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
338
339 skb_queue_tail(&mrvl->rawq, skb);
340 hci_uart_tx_wakeup(hu);
341 }
342
343 release_firmware(fw);
344 return err;
345 }
346
mrvl_setup(struct hci_uart * hu)347 static int mrvl_setup(struct hci_uart *hu)
348 {
349 int err;
350
351 hci_uart_set_flow_control(hu, true);
352
353 err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin");
354 if (err) {
355 bt_dev_err(hu->hdev, "Unable to download firmware helper");
356 return -EINVAL;
357 }
358
359 hci_uart_set_baudrate(hu, 3000000);
360 hci_uart_set_flow_control(hu, false);
361
362 err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin");
363 if (err)
364 return err;
365
366 return 0;
367 }
368
369 static const struct hci_uart_proto mrvl_proto = {
370 .id = HCI_UART_MRVL,
371 .name = "Marvell",
372 .init_speed = 115200,
373 .open = mrvl_open,
374 .close = mrvl_close,
375 .flush = mrvl_flush,
376 .setup = mrvl_setup,
377 .recv = mrvl_recv,
378 .enqueue = mrvl_enqueue,
379 .dequeue = mrvl_dequeue,
380 };
381
mrvl_init(void)382 int __init mrvl_init(void)
383 {
384 return hci_uart_register_proto(&mrvl_proto);
385 }
386
mrvl_deinit(void)387 int __exit mrvl_deinit(void)
388 {
389 return hci_uart_unregister_proto(&mrvl_proto);
390 }
391