blob: c04f5f9e1ed0137cbc831ec20942cfbabee608c5 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +02002/*
3 * Texas Instruments' Bluetooth HCILL UART protocol
4 *
5 * HCILL (HCI Low Level) is a Texas Instruments' power management
6 * protocol extension to H4.
7 *
8 * Copyright (C) 2007 Texas Instruments, Inc.
9 *
10 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
11 *
12 * Acknowledgements:
13 * This file is based on hci_h4.c, which was written
14 * by Maxim Krasnyansky and Marcel Holtmann.
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020015 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19
20#include <linux/init.h>
21#include <linux/sched.h>
22#include <linux/types.h>
23#include <linux/fcntl.h>
Rob Herring37180552017-04-13 10:03:52 -050024#include <linux/firmware.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020025#include <linux/interrupt.h>
26#include <linux/ptrace.h>
27#include <linux/poll.h>
28
29#include <linux/slab.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020030#include <linux/errno.h>
31#include <linux/string.h>
32#include <linux/signal.h>
33#include <linux/ioctl.h>
Rob Herring37180552017-04-13 10:03:52 -050034#include <linux/of.h>
35#include <linux/serdev.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020036#include <linux/skbuff.h>
Rob Herring37180552017-04-13 10:03:52 -050037#include <linux/ti_wilink_st.h>
Ulf Hansson43d3d092017-06-07 11:08:21 +020038#include <linux/clk.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020039
40#include <net/bluetooth/bluetooth.h>
41#include <net/bluetooth/hci_core.h>
Rob Herring37180552017-04-13 10:03:52 -050042#include <linux/gpio/consumer.h>
David Lechner0e58d0c2017-12-12 17:54:12 -060043#include <linux/nvmem-consumer.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020044
45#include "hci_uart.h"
46
David Lechner7c6ca122017-12-03 21:21:21 -060047/* Vendor-specific HCI commands */
David Lechneraa099392017-12-12 15:59:16 -060048#define HCI_VS_WRITE_BD_ADDR 0xfc06
David Lechner7c6ca122017-12-03 21:21:21 -060049#define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36
50
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020051/* HCILL commands */
52#define HCILL_GO_TO_SLEEP_IND 0x30
53#define HCILL_GO_TO_SLEEP_ACK 0x31
54#define HCILL_WAKE_UP_IND 0x32
55#define HCILL_WAKE_UP_ACK 0x33
56
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020057/* HCILL states */
58enum hcill_states_e {
59 HCILL_ASLEEP,
60 HCILL_ASLEEP_TO_AWAKE,
61 HCILL_AWAKE,
62 HCILL_AWAKE_TO_ASLEEP
63};
64
Rob Herring37180552017-04-13 10:03:52 -050065struct ll_device {
66 struct hci_uart hu;
67 struct serdev_device *serdev;
68 struct gpio_desc *enable_gpio;
Ulf Hansson43d3d092017-06-07 11:08:21 +020069 struct clk *ext_clk;
David Lechner0e58d0c2017-12-12 17:54:12 -060070 bdaddr_t bdaddr;
Rob Herring37180552017-04-13 10:03:52 -050071};
72
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020073struct ll_struct {
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020074 struct sk_buff *rx_skb;
75 struct sk_buff_head txq;
76 spinlock_t hcill_lock; /* HCILL state lock */
77 unsigned long hcill_state; /* HCILL power state */
78 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
79};
80
81/*
82 * Builds and sends an HCILL command packet.
83 * These are very simple packets with only 1 cmd byte
84 */
85static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
86{
87 int err = 0;
88 struct sk_buff *skb = NULL;
89 struct ll_struct *ll = hu->priv;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020090
91 BT_DBG("hu %p cmd 0x%x", hu, cmd);
92
93 /* allocate packet */
94 skb = bt_skb_alloc(1, GFP_ATOMIC);
95 if (!skb) {
96 BT_ERR("cannot allocate memory for HCILL packet");
97 err = -ENOMEM;
98 goto out;
99 }
100
101 /* prepare packet */
Marcel Holtmann9bef22f2018-03-20 09:31:04 +0100102 skb_put_u8(skb, cmd);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200103
104 /* send packet */
105 skb_queue_tail(&ll->txq, skb);
106out:
107 return err;
108}
109
110/* Initialize protocol */
111static int ll_open(struct hci_uart *hu)
112{
113 struct ll_struct *ll;
114
115 BT_DBG("hu %p", hu);
116
David Herrmann9eb648c2012-01-07 15:19:37 +0100117 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200118 if (!ll)
119 return -ENOMEM;
120
121 skb_queue_head_init(&ll->txq);
122 skb_queue_head_init(&ll->tx_wait_q);
123 spin_lock_init(&ll->hcill_lock);
124
125 ll->hcill_state = HCILL_AWAKE;
126
127 hu->priv = ll;
128
Ulf Hansson43d3d092017-06-07 11:08:21 +0200129 if (hu->serdev) {
130 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
Ulf Hansson43d3d092017-06-07 11:08:21 +0200131 if (!IS_ERR(lldev->ext_clk))
132 clk_prepare_enable(lldev->ext_clk);
133 }
Rob Herring37180552017-04-13 10:03:52 -0500134
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200135 return 0;
136}
137
138/* Flush protocol data */
139static int ll_flush(struct hci_uart *hu)
140{
141 struct ll_struct *ll = hu->priv;
142
143 BT_DBG("hu %p", hu);
144
145 skb_queue_purge(&ll->tx_wait_q);
146 skb_queue_purge(&ll->txq);
147
148 return 0;
149}
150
151/* Close protocol */
152static int ll_close(struct hci_uart *hu)
153{
154 struct ll_struct *ll = hu->priv;
155
156 BT_DBG("hu %p", hu);
157
158 skb_queue_purge(&ll->tx_wait_q);
159 skb_queue_purge(&ll->txq);
160
Wei Yongjunb1fb0682009-02-25 18:09:33 +0800161 kfree_skb(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200162
Rob Herring37180552017-04-13 10:03:52 -0500163 if (hu->serdev) {
164 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
165 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
166
Ulf Hansson43d3d092017-06-07 11:08:21 +0200167 clk_disable_unprepare(lldev->ext_clk);
Rob Herring37180552017-04-13 10:03:52 -0500168 }
169
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200170 hu->priv = NULL;
171
172 kfree(ll);
173
174 return 0;
175}
176
177/*
178 * internal function, which does common work of the device wake up process:
179 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
180 * 2. changes internal state to HCILL_AWAKE.
181 * Note: assumes that hcill_lock spinlock is taken,
182 * shouldn't be called otherwise!
183 */
184static void __ll_do_awake(struct ll_struct *ll)
185{
186 struct sk_buff *skb = NULL;
187
188 while ((skb = skb_dequeue(&ll->tx_wait_q)))
189 skb_queue_tail(&ll->txq, skb);
190
191 ll->hcill_state = HCILL_AWAKE;
192}
193
194/*
195 * Called upon a wake-up-indication from the device
196 */
197static void ll_device_want_to_wakeup(struct hci_uart *hu)
198{
199 unsigned long flags;
200 struct ll_struct *ll = hu->priv;
201
202 BT_DBG("hu %p", hu);
203
204 /* lock hcill state */
205 spin_lock_irqsave(&ll->hcill_lock, flags);
206
207 switch (ll->hcill_state) {
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800208 case HCILL_ASLEEP_TO_AWAKE:
209 /*
210 * This state means that both the host and the BRF chip
211 * have simultaneously sent a wake-up-indication packet.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300212 * Traditionally, in this case, receiving a wake-up-indication
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800213 * was enough and an additional wake-up-ack wasn't needed.
214 * This has changed with the BRF6350, which does require an
215 * explicit wake-up-ack. Other BRF versions, which do not
216 * require an explicit ack here, do accept it, thus it is
217 * perfectly safe to always send one.
218 */
219 BT_DBG("dual wake-up-indication");
Gustavo A. R. Silvafac72b22017-10-12 17:24:02 -0500220 /* fall through */
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200221 case HCILL_ASLEEP:
222 /* acknowledge device wake up */
223 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
224 BT_ERR("cannot acknowledge device wake up");
225 goto out;
226 }
227 break;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200228 default:
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800229 /* any other state is illegal */
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200230 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
231 break;
232 }
233
234 /* send pending packets and change state to HCILL_AWAKE */
235 __ll_do_awake(ll);
236
237out:
238 spin_unlock_irqrestore(&ll->hcill_lock, flags);
239
240 /* actually send the packets */
241 hci_uart_tx_wakeup(hu);
242}
243
244/*
245 * Called upon a sleep-indication from the device
246 */
247static void ll_device_want_to_sleep(struct hci_uart *hu)
248{
249 unsigned long flags;
250 struct ll_struct *ll = hu->priv;
251
252 BT_DBG("hu %p", hu);
253
254 /* lock hcill state */
255 spin_lock_irqsave(&ll->hcill_lock, flags);
256
257 /* sanity check */
258 if (ll->hcill_state != HCILL_AWAKE)
259 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
260
261 /* acknowledge device sleep */
262 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
263 BT_ERR("cannot acknowledge device sleep");
264 goto out;
265 }
266
267 /* update state */
268 ll->hcill_state = HCILL_ASLEEP;
269
270out:
271 spin_unlock_irqrestore(&ll->hcill_lock, flags);
272
273 /* actually send the sleep ack packet */
274 hci_uart_tx_wakeup(hu);
275}
276
277/*
278 * Called upon wake-up-acknowledgement from the device
279 */
280static void ll_device_woke_up(struct hci_uart *hu)
281{
282 unsigned long flags;
283 struct ll_struct *ll = hu->priv;
284
285 BT_DBG("hu %p", hu);
286
287 /* lock hcill state */
288 spin_lock_irqsave(&ll->hcill_lock, flags);
289
290 /* sanity check */
291 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
292 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
293
294 /* send pending packets and change state to HCILL_AWAKE */
295 __ll_do_awake(ll);
296
297 spin_unlock_irqrestore(&ll->hcill_lock, flags);
298
299 /* actually send the packets */
300 hci_uart_tx_wakeup(hu);
301}
302
303/* Enqueue frame for transmittion (padding, crc, etc) */
304/* may be called from two simultaneous tasklets */
305static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
306{
307 unsigned long flags = 0;
308 struct ll_struct *ll = hu->priv;
309
310 BT_DBG("hu %p skb %p", hu, skb);
311
312 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100313 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200314
315 /* lock hcill state */
316 spin_lock_irqsave(&ll->hcill_lock, flags);
317
318 /* act according to current state */
319 switch (ll->hcill_state) {
320 case HCILL_AWAKE:
321 BT_DBG("device awake, sending normally");
322 skb_queue_tail(&ll->txq, skb);
323 break;
324 case HCILL_ASLEEP:
325 BT_DBG("device asleep, waking up and queueing packet");
326 /* save packet for later */
327 skb_queue_tail(&ll->tx_wait_q, skb);
328 /* awake device */
329 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
330 BT_ERR("cannot wake up device");
331 break;
332 }
333 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
334 break;
335 case HCILL_ASLEEP_TO_AWAKE:
336 BT_DBG("device waking up, queueing packet");
337 /* transient state; just keep packet for later */
338 skb_queue_tail(&ll->tx_wait_q, skb);
339 break;
340 default:
341 BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
342 kfree_skb(skb);
343 break;
344 }
345
346 spin_unlock_irqrestore(&ll->hcill_lock, flags);
347
348 return 0;
349}
350
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100351static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200352{
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100353 struct hci_uart *hu = hci_get_drvdata(hdev);
354 struct ll_struct *ll = hu->priv;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200355
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100356 switch (hci_skb_pkt_type(skb)) {
357 case HCILL_GO_TO_SLEEP_IND:
358 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
359 ll_device_want_to_sleep(hu);
360 break;
361 case HCILL_GO_TO_SLEEP_ACK:
362 /* shouldn't happen */
363 bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
364 ll->hcill_state);
365 break;
366 case HCILL_WAKE_UP_IND:
367 BT_DBG("HCILL_WAKE_UP_IND packet");
368 ll_device_want_to_wakeup(hu);
369 break;
370 case HCILL_WAKE_UP_ACK:
371 BT_DBG("HCILL_WAKE_UP_ACK packet");
372 ll_device_woke_up(hu);
373 break;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200374 }
375
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100376 kfree_skb(skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200377 return 0;
378}
379
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100380#define LL_RECV_SLEEP_IND \
381 .type = HCILL_GO_TO_SLEEP_IND, \
382 .hlen = 0, \
383 .loff = 0, \
384 .lsize = 0, \
385 .maxlen = 0
386
387#define LL_RECV_SLEEP_ACK \
388 .type = HCILL_GO_TO_SLEEP_ACK, \
389 .hlen = 0, \
390 .loff = 0, \
391 .lsize = 0, \
392 .maxlen = 0
393
394#define LL_RECV_WAKE_IND \
395 .type = HCILL_WAKE_UP_IND, \
396 .hlen = 0, \
397 .loff = 0, \
398 .lsize = 0, \
399 .maxlen = 0
400
401#define LL_RECV_WAKE_ACK \
402 .type = HCILL_WAKE_UP_ACK, \
403 .hlen = 0, \
404 .loff = 0, \
405 .lsize = 0, \
406 .maxlen = 0
407
408static const struct h4_recv_pkt ll_recv_pkts[] = {
409 { H4_RECV_ACL, .recv = hci_recv_frame },
410 { H4_RECV_SCO, .recv = hci_recv_frame },
411 { H4_RECV_EVENT, .recv = hci_recv_frame },
412 { LL_RECV_SLEEP_IND, .recv = ll_recv_frame },
413 { LL_RECV_SLEEP_ACK, .recv = ll_recv_frame },
414 { LL_RECV_WAKE_IND, .recv = ll_recv_frame },
415 { LL_RECV_WAKE_ACK, .recv = ll_recv_frame },
416};
417
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200418/* Recv data */
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700419static int ll_recv(struct hci_uart *hu, const void *data, int count)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200420{
421 struct ll_struct *ll = hu->priv;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200422
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100423 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
424 return -EUNATCH;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200425
Marcel Holtmannf9d7c8f2018-03-24 10:19:52 +0100426 ll->rx_skb = h4_recv_buf(hu->hdev, ll->rx_skb, data, count,
427 ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
428 if (IS_ERR(ll->rx_skb)) {
429 int err = PTR_ERR(ll->rx_skb);
430 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
431 ll->rx_skb = NULL;
432 return err;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200433 }
434
435 return count;
436}
437
438static struct sk_buff *ll_dequeue(struct hci_uart *hu)
439{
440 struct ll_struct *ll = hu->priv;
441 return skb_dequeue(&ll->txq);
442}
443
Rob Herring37180552017-04-13 10:03:52 -0500444#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
445static int read_local_version(struct hci_dev *hdev)
446{
447 int err = 0;
448 unsigned short version = 0;
449 struct sk_buff *skb;
450 struct hci_rp_read_local_version *ver;
451
452 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT);
453 if (IS_ERR(skb)) {
454 bt_dev_err(hdev, "Reading TI version information failed (%ld)",
455 PTR_ERR(skb));
Sebastian Reichelf2edd9f2017-04-15 23:54:13 +0200456 return PTR_ERR(skb);
Rob Herring37180552017-04-13 10:03:52 -0500457 }
458 if (skb->len != sizeof(*ver)) {
459 err = -EILSEQ;
460 goto out;
461 }
462
463 ver = (struct hci_rp_read_local_version *)skb->data;
464 if (le16_to_cpu(ver->manufacturer) != 13) {
465 err = -ENODEV;
466 goto out;
467 }
468
469 version = le16_to_cpu(ver->lmp_subver);
470
471out:
472 if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err);
473 kfree_skb(skb);
474 return err ? err : version;
475}
476
477/**
478 * download_firmware -
479 * internal function which parses through the .bts firmware
480 * script file intreprets SEND, DELAY actions only as of now
481 */
482static int download_firmware(struct ll_device *lldev)
483{
484 unsigned short chip, min_ver, maj_ver;
485 int version, err, len;
486 unsigned char *ptr, *action_ptr;
487 unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
488 const struct firmware *fw;
489 struct sk_buff *skb;
490 struct hci_command *cmd;
491
492 version = read_local_version(lldev->hu.hdev);
493 if (version < 0)
494 return version;
495
496 chip = (version & 0x7C00) >> 10;
497 min_ver = (version & 0x007F);
498 maj_ver = (version & 0x0380) >> 7;
499 if (version & 0x8000)
500 maj_ver |= 0x0008;
501
502 snprintf(bts_scr_name, sizeof(bts_scr_name),
503 "ti-connectivity/TIInit_%d.%d.%d.bts",
504 chip, maj_ver, min_ver);
505
506 err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
507 if (err || !fw->data || !fw->size) {
508 bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
509 err, bts_scr_name);
510 return -EINVAL;
511 }
512 ptr = (void *)fw->data;
513 len = fw->size;
514 /* bts_header to remove out magic number and
515 * version
516 */
517 ptr += sizeof(struct bts_header);
518 len -= sizeof(struct bts_header);
519
520 while (len > 0 && ptr) {
521 bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
522 ((struct bts_action *)ptr)->size,
523 ((struct bts_action *)ptr)->type);
524
525 action_ptr = &(((struct bts_action *)ptr)->data[0]);
526
527 switch (((struct bts_action *)ptr)->type) {
528 case ACTION_SEND_COMMAND: /* action send */
529 bt_dev_dbg(lldev->hu.hdev, "S");
530 cmd = (struct hci_command *)action_ptr;
David Lechner7c6ca122017-12-03 21:21:21 -0600531 if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
Rob Herring37180552017-04-13 10:03:52 -0500532 /* ignore remote change
Derek Robsond98422c2017-07-22 13:47:07 +1200533 * baud rate HCI VS command
534 */
Rob Herring37180552017-04-13 10:03:52 -0500535 bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
536 break;
537 }
538 if (cmd->prefix != 1)
David Lechner059fb822017-12-02 21:01:58 -0600539 bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
Rob Herring37180552017-04-13 10:03:52 -0500540
541 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
542 if (IS_ERR(skb)) {
David Lechner059fb822017-12-02 21:01:58 -0600543 bt_dev_err(lldev->hu.hdev, "send command failed");
Guodong Xu823b8422017-05-22 21:50:42 +0800544 err = PTR_ERR(skb);
Rob Herring37180552017-04-13 10:03:52 -0500545 goto out_rel_fw;
546 }
547 kfree_skb(skb);
548 break;
549 case ACTION_WAIT_EVENT: /* wait */
550 /* no need to wait as command was synchronous */
551 bt_dev_dbg(lldev->hu.hdev, "W");
552 break;
553 case ACTION_DELAY: /* sleep */
554 bt_dev_info(lldev->hu.hdev, "sleep command in scr");
Jia-Ju Bai06633ee2018-01-27 18:03:52 +0800555 msleep(((struct bts_action_delay *)action_ptr)->msec);
Rob Herring37180552017-04-13 10:03:52 -0500556 break;
557 }
558 len -= (sizeof(struct bts_action) +
559 ((struct bts_action *)ptr)->size);
560 ptr += sizeof(struct bts_action) +
561 ((struct bts_action *)ptr)->size;
562 }
563
564out_rel_fw:
565 /* fw download complete */
566 release_firmware(fw);
567 return err;
568}
569
David Lechneraa099392017-12-12 15:59:16 -0600570static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
571{
572 bdaddr_t bdaddr_swapped;
573 struct sk_buff *skb;
574
575 /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
576 * address to be MSB first, but bdaddr_t has the convention of being
577 * LSB first.
578 */
579 baswap(&bdaddr_swapped, bdaddr);
580 skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
581 &bdaddr_swapped, HCI_INIT_TIMEOUT);
582 if (!IS_ERR(skb))
583 kfree_skb(skb);
584
585 return PTR_ERR_OR_ZERO(skb);
586}
587
Rob Herring37180552017-04-13 10:03:52 -0500588static int ll_setup(struct hci_uart *hu)
589{
590 int err, retry = 3;
591 struct ll_device *lldev;
592 struct serdev_device *serdev = hu->serdev;
593 u32 speed;
594
595 if (!serdev)
596 return 0;
597
598 lldev = serdev_device_get_drvdata(serdev);
599
David Lechneraa099392017-12-12 15:59:16 -0600600 hu->hdev->set_bdaddr = ll_set_bdaddr;
601
Rob Herring37180552017-04-13 10:03:52 -0500602 serdev_device_set_flow_control(serdev, true);
603
604 do {
David Lechnerd54fdcf2017-12-02 20:43:55 -0600605 /* Reset the Bluetooth device */
Rob Herring37180552017-04-13 10:03:52 -0500606 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
607 msleep(5);
608 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
David Lechnerd54fdcf2017-12-02 20:43:55 -0600609 err = serdev_device_wait_for_cts(serdev, true, 200);
610 if (err) {
611 bt_dev_err(hu->hdev, "Failed to get CTS");
612 return err;
613 }
Rob Herring37180552017-04-13 10:03:52 -0500614
615 err = download_firmware(lldev);
616 if (!err)
617 break;
618
619 /* Toggle BT_EN and retry */
620 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
621 } while (retry--);
622
623 if (err)
624 return err;
625
David Lechner0e58d0c2017-12-12 17:54:12 -0600626 /* Set BD address if one was specified at probe */
627 if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
628 /* This means that there was an error getting the BD address
629 * during probe, so mark the device as having a bad address.
630 */
631 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
632 } else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
633 err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
634 if (err)
635 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
636 }
637
Rob Herring37180552017-04-13 10:03:52 -0500638 /* Operational speed if any */
639 if (hu->oper_speed)
640 speed = hu->oper_speed;
641 else if (hu->proto->oper_speed)
642 speed = hu->proto->oper_speed;
643 else
644 speed = 0;
645
646 if (speed) {
David Lechnerc30b93e2017-12-07 20:22:19 -0600647 __le32 speed_le = cpu_to_le32(speed);
David Lechner7c6ca122017-12-03 21:21:21 -0600648 struct sk_buff *skb;
649
650 skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
David Lechnerc30b93e2017-12-07 20:22:19 -0600651 sizeof(speed_le), &speed_le,
652 HCI_INIT_TIMEOUT);
Rob Herring37180552017-04-13 10:03:52 -0500653 if (!IS_ERR(skb)) {
654 kfree_skb(skb);
655 serdev_device_set_baudrate(serdev, speed);
656 }
657 }
658
659 return 0;
660}
661
662static const struct hci_uart_proto llp;
663
664static int hci_ti_probe(struct serdev_device *serdev)
665{
666 struct hci_uart *hu;
667 struct ll_device *lldev;
David Lechner0e58d0c2017-12-12 17:54:12 -0600668 struct nvmem_cell *bdaddr_cell;
Rob Herring37180552017-04-13 10:03:52 -0500669 u32 max_speed = 3000000;
670
671 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
672 if (!lldev)
673 return -ENOMEM;
674 hu = &lldev->hu;
675
676 serdev_device_set_drvdata(serdev, lldev);
677 lldev->serdev = hu->serdev = serdev;
678
679 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW);
680 if (IS_ERR(lldev->enable_gpio))
681 return PTR_ERR(lldev->enable_gpio);
682
Ulf Hansson43d3d092017-06-07 11:08:21 +0200683 lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
684 if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
685 return PTR_ERR(lldev->ext_clk);
686
Rob Herring37180552017-04-13 10:03:52 -0500687 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
688 hci_uart_set_speeds(hu, 115200, max_speed);
689
David Lechner0e58d0c2017-12-12 17:54:12 -0600690 /* optional BD address from nvram */
691 bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
692 if (IS_ERR(bdaddr_cell)) {
693 int err = PTR_ERR(bdaddr_cell);
694
695 if (err == -EPROBE_DEFER)
696 return err;
697
698 /* ENOENT means there is no matching nvmem cell and ENOSYS
699 * means that nvmem is not enabled in the kernel configuration.
700 */
701 if (err != -ENOENT && err != -ENOSYS) {
702 /* If there was some other error, give userspace a
703 * chance to fix the problem instead of failing to load
704 * the driver. Using BDADDR_NONE as a flag that is
705 * tested later in the setup function.
706 */
707 dev_warn(&serdev->dev,
708 "Failed to get \"bd-address\" nvmem cell (%d)\n",
709 err);
710 bacpy(&lldev->bdaddr, BDADDR_NONE);
711 }
712 } else {
713 bdaddr_t *bdaddr;
714 size_t len;
715
716 bdaddr = nvmem_cell_read(bdaddr_cell, &len);
717 nvmem_cell_put(bdaddr_cell);
718 if (IS_ERR(bdaddr)) {
719 dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
720 return PTR_ERR(bdaddr);
721 }
722 if (len != sizeof(bdaddr_t)) {
723 dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
724 kfree(bdaddr);
725 return -EINVAL;
726 }
727
728 /* As per the device tree bindings, the value from nvmem is
729 * expected to be MSB first, but in the kernel it is expected
730 * that bdaddr_t is LSB first.
731 */
732 baswap(&lldev->bdaddr, bdaddr);
733 kfree(bdaddr);
734 }
735
Rob Herring37180552017-04-13 10:03:52 -0500736 return hci_uart_register_device(hu, &llp);
737}
738
739static void hci_ti_remove(struct serdev_device *serdev)
740{
741 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
Rob Herring37180552017-04-13 10:03:52 -0500742
Ian Molton37f52582017-07-08 17:37:43 +0100743 hci_uart_unregister_device(&lldev->hu);
Rob Herring37180552017-04-13 10:03:52 -0500744}
745
746static const struct of_device_id hci_ti_of_match[] = {
David Lechner41664932017-12-12 18:29:31 -0600747 { .compatible = "ti,cc2560" },
Sebastian Reichelc127a872017-06-08 22:58:45 +0200748 { .compatible = "ti,wl1271-st" },
749 { .compatible = "ti,wl1273-st" },
750 { .compatible = "ti,wl1281-st" },
751 { .compatible = "ti,wl1283-st" },
752 { .compatible = "ti,wl1285-st" },
753 { .compatible = "ti,wl1801-st" },
754 { .compatible = "ti,wl1805-st" },
755 { .compatible = "ti,wl1807-st" },
Rob Herring37180552017-04-13 10:03:52 -0500756 { .compatible = "ti,wl1831-st" },
757 { .compatible = "ti,wl1835-st" },
758 { .compatible = "ti,wl1837-st" },
759 {},
760};
761MODULE_DEVICE_TABLE(of, hci_ti_of_match);
762
763static struct serdev_device_driver hci_ti_drv = {
764 .driver = {
765 .name = "hci-ti",
766 .of_match_table = of_match_ptr(hci_ti_of_match),
767 },
768 .probe = hci_ti_probe,
769 .remove = hci_ti_remove,
770};
771#else
772#define ll_setup NULL
773#endif
774
Marcel Holtmann4ee7ef12015-04-04 22:11:43 -0700775static const struct hci_uart_proto llp = {
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200776 .id = HCI_UART_LL,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700777 .name = "LL",
Rob Herring37180552017-04-13 10:03:52 -0500778 .setup = ll_setup,
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200779 .open = ll_open,
780 .close = ll_close,
781 .recv = ll_recv,
782 .enqueue = ll_enqueue,
783 .dequeue = ll_dequeue,
784 .flush = ll_flush,
785};
786
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300787int __init ll_init(void)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200788{
Rob Herring37180552017-04-13 10:03:52 -0500789 serdev_device_driver_register(&hci_ti_drv);
790
Marcel Holtmann01009ee2015-04-04 22:27:35 -0700791 return hci_uart_register_proto(&llp);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200792}
793
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300794int __exit ll_deinit(void)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200795{
Rob Herring37180552017-04-13 10:03:52 -0500796 serdev_device_driver_unregister(&hci_ti_drv);
797
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200798 return hci_uart_unregister_proto(&llp);
799}