blob: 7c55a9f77808a61db3200937c73f3732c74cd581 [file] [log] [blame]
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +02001/*
2 * Texas Instruments' Bluetooth HCILL UART protocol
3 *
4 * HCILL (HCI Low Level) is a Texas Instruments' power management
5 * protocol extension to H4.
6 *
7 * Copyright (C) 2007 Texas Instruments, Inc.
8 *
9 * Written by Ohad Ben-Cohen <ohad@bencohen.org>
10 *
11 * Acknowledgements:
12 * This file is based on hci_h4.c, which was written
13 * by Maxim Krasnyansky and Marcel Holtmann.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2
17 * as published by the Free Software Foundation
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30#include <linux/module.h>
31#include <linux/kernel.h>
32
33#include <linux/init.h>
34#include <linux/sched.h>
35#include <linux/types.h>
36#include <linux/fcntl.h>
Rob Herring37180552017-04-13 10:03:52 -050037#include <linux/firmware.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020038#include <linux/interrupt.h>
39#include <linux/ptrace.h>
40#include <linux/poll.h>
41
42#include <linux/slab.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020043#include <linux/errno.h>
44#include <linux/string.h>
45#include <linux/signal.h>
46#include <linux/ioctl.h>
Rob Herring37180552017-04-13 10:03:52 -050047#include <linux/of.h>
48#include <linux/serdev.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020049#include <linux/skbuff.h>
Rob Herring37180552017-04-13 10:03:52 -050050#include <linux/ti_wilink_st.h>
Ulf Hansson43d3d092017-06-07 11:08:21 +020051#include <linux/clk.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020052
53#include <net/bluetooth/bluetooth.h>
54#include <net/bluetooth/hci_core.h>
Rob Herring37180552017-04-13 10:03:52 -050055#include <linux/gpio/consumer.h>
David Lechner0e58d0c2017-12-12 17:54:12 -060056#include <linux/nvmem-consumer.h>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020057
58#include "hci_uart.h"
59
David Lechner7c6ca122017-12-03 21:21:21 -060060/* Vendor-specific HCI commands */
David Lechneraa099392017-12-12 15:59:16 -060061#define HCI_VS_WRITE_BD_ADDR 0xfc06
David Lechner7c6ca122017-12-03 21:21:21 -060062#define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36
63
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020064/* HCILL commands */
65#define HCILL_GO_TO_SLEEP_IND 0x30
66#define HCILL_GO_TO_SLEEP_ACK 0x31
67#define HCILL_WAKE_UP_IND 0x32
68#define HCILL_WAKE_UP_ACK 0x33
69
70/* HCILL receiver States */
71#define HCILL_W4_PACKET_TYPE 0
72#define HCILL_W4_EVENT_HDR 1
73#define HCILL_W4_ACL_HDR 2
74#define HCILL_W4_SCO_HDR 3
75#define HCILL_W4_DATA 4
76
77/* HCILL states */
78enum hcill_states_e {
79 HCILL_ASLEEP,
80 HCILL_ASLEEP_TO_AWAKE,
81 HCILL_AWAKE,
82 HCILL_AWAKE_TO_ASLEEP
83};
84
Rob Herring37180552017-04-13 10:03:52 -050085struct ll_device {
86 struct hci_uart hu;
87 struct serdev_device *serdev;
88 struct gpio_desc *enable_gpio;
Ulf Hansson43d3d092017-06-07 11:08:21 +020089 struct clk *ext_clk;
David Lechner0e58d0c2017-12-12 17:54:12 -060090 bdaddr_t bdaddr;
Rob Herring37180552017-04-13 10:03:52 -050091};
92
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020093struct ll_struct {
94 unsigned long rx_state;
95 unsigned long rx_count;
96 struct sk_buff *rx_skb;
97 struct sk_buff_head txq;
98 spinlock_t hcill_lock; /* HCILL state lock */
99 unsigned long hcill_state; /* HCILL power state */
100 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
101};
102
103/*
104 * Builds and sends an HCILL command packet.
105 * These are very simple packets with only 1 cmd byte
106 */
107static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
108{
109 int err = 0;
110 struct sk_buff *skb = NULL;
111 struct ll_struct *ll = hu->priv;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200112
113 BT_DBG("hu %p cmd 0x%x", hu, cmd);
114
115 /* allocate packet */
116 skb = bt_skb_alloc(1, GFP_ATOMIC);
117 if (!skb) {
118 BT_ERR("cannot allocate memory for HCILL packet");
119 err = -ENOMEM;
120 goto out;
121 }
122
123 /* prepare packet */
Marcel Holtmann9bef22f2018-03-20 09:31:04 +0100124 skb_put_u8(skb, cmd);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200125
126 /* send packet */
127 skb_queue_tail(&ll->txq, skb);
128out:
129 return err;
130}
131
132/* Initialize protocol */
133static int ll_open(struct hci_uart *hu)
134{
135 struct ll_struct *ll;
136
137 BT_DBG("hu %p", hu);
138
David Herrmann9eb648c2012-01-07 15:19:37 +0100139 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200140 if (!ll)
141 return -ENOMEM;
142
143 skb_queue_head_init(&ll->txq);
144 skb_queue_head_init(&ll->tx_wait_q);
145 spin_lock_init(&ll->hcill_lock);
146
147 ll->hcill_state = HCILL_AWAKE;
148
149 hu->priv = ll;
150
Ulf Hansson43d3d092017-06-07 11:08:21 +0200151 if (hu->serdev) {
152 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
Rob Herring37180552017-04-13 10:03:52 -0500153 serdev_device_open(hu->serdev);
Ulf Hansson43d3d092017-06-07 11:08:21 +0200154 if (!IS_ERR(lldev->ext_clk))
155 clk_prepare_enable(lldev->ext_clk);
156 }
Rob Herring37180552017-04-13 10:03:52 -0500157
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200158 return 0;
159}
160
161/* Flush protocol data */
162static int ll_flush(struct hci_uart *hu)
163{
164 struct ll_struct *ll = hu->priv;
165
166 BT_DBG("hu %p", hu);
167
168 skb_queue_purge(&ll->tx_wait_q);
169 skb_queue_purge(&ll->txq);
170
171 return 0;
172}
173
174/* Close protocol */
175static int ll_close(struct hci_uart *hu)
176{
177 struct ll_struct *ll = hu->priv;
178
179 BT_DBG("hu %p", hu);
180
181 skb_queue_purge(&ll->tx_wait_q);
182 skb_queue_purge(&ll->txq);
183
Wei Yongjunb1fb0682009-02-25 18:09:33 +0800184 kfree_skb(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200185
Rob Herring37180552017-04-13 10:03:52 -0500186 if (hu->serdev) {
187 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
188 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
189
Ulf Hansson43d3d092017-06-07 11:08:21 +0200190 clk_disable_unprepare(lldev->ext_clk);
191
Rob Herring37180552017-04-13 10:03:52 -0500192 serdev_device_close(hu->serdev);
193 }
194
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200195 hu->priv = NULL;
196
197 kfree(ll);
198
199 return 0;
200}
201
202/*
203 * internal function, which does common work of the device wake up process:
204 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
205 * 2. changes internal state to HCILL_AWAKE.
206 * Note: assumes that hcill_lock spinlock is taken,
207 * shouldn't be called otherwise!
208 */
209static void __ll_do_awake(struct ll_struct *ll)
210{
211 struct sk_buff *skb = NULL;
212
213 while ((skb = skb_dequeue(&ll->tx_wait_q)))
214 skb_queue_tail(&ll->txq, skb);
215
216 ll->hcill_state = HCILL_AWAKE;
217}
218
219/*
220 * Called upon a wake-up-indication from the device
221 */
222static void ll_device_want_to_wakeup(struct hci_uart *hu)
223{
224 unsigned long flags;
225 struct ll_struct *ll = hu->priv;
226
227 BT_DBG("hu %p", hu);
228
229 /* lock hcill state */
230 spin_lock_irqsave(&ll->hcill_lock, flags);
231
232 switch (ll->hcill_state) {
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800233 case HCILL_ASLEEP_TO_AWAKE:
234 /*
235 * This state means that both the host and the BRF chip
236 * have simultaneously sent a wake-up-indication packet.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300237 * Traditionally, in this case, receiving a wake-up-indication
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800238 * was enough and an additional wake-up-ack wasn't needed.
239 * This has changed with the BRF6350, which does require an
240 * explicit wake-up-ack. Other BRF versions, which do not
241 * require an explicit ack here, do accept it, thus it is
242 * perfectly safe to always send one.
243 */
244 BT_DBG("dual wake-up-indication");
Gustavo A. R. Silvafac72b22017-10-12 17:24:02 -0500245 /* fall through */
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200246 case HCILL_ASLEEP:
247 /* acknowledge device wake up */
248 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
249 BT_ERR("cannot acknowledge device wake up");
250 goto out;
251 }
252 break;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200253 default:
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800254 /* any other state is illegal */
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200255 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
256 break;
257 }
258
259 /* send pending packets and change state to HCILL_AWAKE */
260 __ll_do_awake(ll);
261
262out:
263 spin_unlock_irqrestore(&ll->hcill_lock, flags);
264
265 /* actually send the packets */
266 hci_uart_tx_wakeup(hu);
267}
268
269/*
270 * Called upon a sleep-indication from the device
271 */
272static void ll_device_want_to_sleep(struct hci_uart *hu)
273{
274 unsigned long flags;
275 struct ll_struct *ll = hu->priv;
276
277 BT_DBG("hu %p", hu);
278
279 /* lock hcill state */
280 spin_lock_irqsave(&ll->hcill_lock, flags);
281
282 /* sanity check */
283 if (ll->hcill_state != HCILL_AWAKE)
284 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
285
286 /* acknowledge device sleep */
287 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
288 BT_ERR("cannot acknowledge device sleep");
289 goto out;
290 }
291
292 /* update state */
293 ll->hcill_state = HCILL_ASLEEP;
294
295out:
296 spin_unlock_irqrestore(&ll->hcill_lock, flags);
297
298 /* actually send the sleep ack packet */
299 hci_uart_tx_wakeup(hu);
300}
301
302/*
303 * Called upon wake-up-acknowledgement from the device
304 */
305static void ll_device_woke_up(struct hci_uart *hu)
306{
307 unsigned long flags;
308 struct ll_struct *ll = hu->priv;
309
310 BT_DBG("hu %p", hu);
311
312 /* lock hcill state */
313 spin_lock_irqsave(&ll->hcill_lock, flags);
314
315 /* sanity check */
316 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
317 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
318
319 /* send pending packets and change state to HCILL_AWAKE */
320 __ll_do_awake(ll);
321
322 spin_unlock_irqrestore(&ll->hcill_lock, flags);
323
324 /* actually send the packets */
325 hci_uart_tx_wakeup(hu);
326}
327
328/* Enqueue frame for transmittion (padding, crc, etc) */
329/* may be called from two simultaneous tasklets */
330static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
331{
332 unsigned long flags = 0;
333 struct ll_struct *ll = hu->priv;
334
335 BT_DBG("hu %p skb %p", hu, skb);
336
337 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100338 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200339
340 /* lock hcill state */
341 spin_lock_irqsave(&ll->hcill_lock, flags);
342
343 /* act according to current state */
344 switch (ll->hcill_state) {
345 case HCILL_AWAKE:
346 BT_DBG("device awake, sending normally");
347 skb_queue_tail(&ll->txq, skb);
348 break;
349 case HCILL_ASLEEP:
350 BT_DBG("device asleep, waking up and queueing packet");
351 /* save packet for later */
352 skb_queue_tail(&ll->tx_wait_q, skb);
353 /* awake device */
354 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
355 BT_ERR("cannot wake up device");
356 break;
357 }
358 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
359 break;
360 case HCILL_ASLEEP_TO_AWAKE:
361 BT_DBG("device waking up, queueing packet");
362 /* transient state; just keep packet for later */
363 skb_queue_tail(&ll->tx_wait_q, skb);
364 break;
365 default:
366 BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
367 kfree_skb(skb);
368 break;
369 }
370
371 spin_unlock_irqrestore(&ll->hcill_lock, flags);
372
373 return 0;
374}
375
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700376static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200377{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -0300378 int room = skb_tailroom(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200379
380 BT_DBG("len %d room %d", len, room);
381
382 if (!len) {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700383 hci_recv_frame(hdev, ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200384 } else if (len > room) {
385 BT_ERR("Data length is too large");
386 kfree_skb(ll->rx_skb);
387 } else {
388 ll->rx_state = HCILL_W4_DATA;
389 ll->rx_count = len;
390 return len;
391 }
392
393 ll->rx_state = HCILL_W4_PACKET_TYPE;
394 ll->rx_skb = NULL;
395 ll->rx_count = 0;
396
397 return 0;
398}
399
400/* Recv data */
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700401static int ll_recv(struct hci_uart *hu, const void *data, int count)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200402{
403 struct ll_struct *ll = hu->priv;
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700404 const char *ptr;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200405 struct hci_event_hdr *eh;
406 struct hci_acl_hdr *ah;
407 struct hci_sco_hdr *sh;
Gustavo Padovanfc5fef62012-05-23 04:04:19 -0300408 int len, type, dlen;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200409
410 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
411
412 ptr = data;
413 while (count) {
414 if (ll->rx_count) {
415 len = min_t(unsigned int, ll->rx_count, count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200416 skb_put_data(ll->rx_skb, ptr, len);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200417 ll->rx_count -= len; count -= len; ptr += len;
418
419 if (ll->rx_count)
420 continue;
421
422 switch (ll->rx_state) {
423 case HCILL_W4_DATA:
424 BT_DBG("Complete data");
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700425 hci_recv_frame(hu->hdev, ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200426
427 ll->rx_state = HCILL_W4_PACKET_TYPE;
428 ll->rx_skb = NULL;
429 continue;
430
431 case HCILL_W4_EVENT_HDR:
Gustavo F. Padovanacce90d2010-05-01 16:15:34 -0300432 eh = hci_event_hdr(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200433
434 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
435
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700436 ll_check_data_len(hu->hdev, ll, eh->plen);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200437 continue;
438
439 case HCILL_W4_ACL_HDR:
Gustavo F. Padovanacce90d2010-05-01 16:15:34 -0300440 ah = hci_acl_hdr(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200441 dlen = __le16_to_cpu(ah->dlen);
442
443 BT_DBG("ACL header: dlen %d", dlen);
444
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700445 ll_check_data_len(hu->hdev, ll, dlen);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200446 continue;
447
448 case HCILL_W4_SCO_HDR:
Gustavo F. Padovanacce90d2010-05-01 16:15:34 -0300449 sh = hci_sco_hdr(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200450
451 BT_DBG("SCO header: dlen %d", sh->dlen);
452
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700453 ll_check_data_len(hu->hdev, ll, sh->dlen);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200454 continue;
455 }
456 }
457
458 /* HCILL_W4_PACKET_TYPE */
459 switch (*ptr) {
460 case HCI_EVENT_PKT:
461 BT_DBG("Event packet");
462 ll->rx_state = HCILL_W4_EVENT_HDR;
463 ll->rx_count = HCI_EVENT_HDR_SIZE;
464 type = HCI_EVENT_PKT;
465 break;
466
467 case HCI_ACLDATA_PKT:
468 BT_DBG("ACL packet");
469 ll->rx_state = HCILL_W4_ACL_HDR;
470 ll->rx_count = HCI_ACL_HDR_SIZE;
471 type = HCI_ACLDATA_PKT;
472 break;
473
474 case HCI_SCODATA_PKT:
475 BT_DBG("SCO packet");
476 ll->rx_state = HCILL_W4_SCO_HDR;
477 ll->rx_count = HCI_SCO_HDR_SIZE;
478 type = HCI_SCODATA_PKT;
479 break;
480
481 /* HCILL signals */
482 case HCILL_GO_TO_SLEEP_IND:
483 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
484 ll_device_want_to_sleep(hu);
485 ptr++; count--;
486 continue;
487
488 case HCILL_GO_TO_SLEEP_ACK:
489 /* shouldn't happen */
490 BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
491 ptr++; count--;
492 continue;
493
494 case HCILL_WAKE_UP_IND:
495 BT_DBG("HCILL_WAKE_UP_IND packet");
496 ll_device_want_to_wakeup(hu);
497 ptr++; count--;
498 continue;
499
500 case HCILL_WAKE_UP_ACK:
501 BT_DBG("HCILL_WAKE_UP_ACK packet");
502 ll_device_woke_up(hu);
503 ptr++; count--;
504 continue;
505
506 default:
507 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
508 hu->hdev->stat.err_rx++;
509 ptr++; count--;
510 continue;
Peter Senna Tschudind650cca2012-09-07 17:24:42 +0200511 }
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200512
513 ptr++; count--;
514
515 /* Allocate packet */
516 ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
517 if (!ll->rx_skb) {
518 BT_ERR("Can't allocate mem for new packet");
519 ll->rx_state = HCILL_W4_PACKET_TYPE;
520 ll->rx_count = 0;
Gustavo F. Padovanfe1aff72010-05-01 16:15:34 -0300521 return -ENOMEM;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200522 }
523
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100524 hci_skb_pkt_type(ll->rx_skb) = type;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200525 }
526
527 return count;
528}
529
530static struct sk_buff *ll_dequeue(struct hci_uart *hu)
531{
532 struct ll_struct *ll = hu->priv;
533 return skb_dequeue(&ll->txq);
534}
535
Rob Herring37180552017-04-13 10:03:52 -0500536#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
537static int read_local_version(struct hci_dev *hdev)
538{
539 int err = 0;
540 unsigned short version = 0;
541 struct sk_buff *skb;
542 struct hci_rp_read_local_version *ver;
543
544 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT);
545 if (IS_ERR(skb)) {
546 bt_dev_err(hdev, "Reading TI version information failed (%ld)",
547 PTR_ERR(skb));
Sebastian Reichelf2edd9f2017-04-15 23:54:13 +0200548 return PTR_ERR(skb);
Rob Herring37180552017-04-13 10:03:52 -0500549 }
550 if (skb->len != sizeof(*ver)) {
551 err = -EILSEQ;
552 goto out;
553 }
554
555 ver = (struct hci_rp_read_local_version *)skb->data;
556 if (le16_to_cpu(ver->manufacturer) != 13) {
557 err = -ENODEV;
558 goto out;
559 }
560
561 version = le16_to_cpu(ver->lmp_subver);
562
563out:
564 if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err);
565 kfree_skb(skb);
566 return err ? err : version;
567}
568
569/**
570 * download_firmware -
571 * internal function which parses through the .bts firmware
572 * script file intreprets SEND, DELAY actions only as of now
573 */
574static int download_firmware(struct ll_device *lldev)
575{
576 unsigned short chip, min_ver, maj_ver;
577 int version, err, len;
578 unsigned char *ptr, *action_ptr;
579 unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
580 const struct firmware *fw;
581 struct sk_buff *skb;
582 struct hci_command *cmd;
583
584 version = read_local_version(lldev->hu.hdev);
585 if (version < 0)
586 return version;
587
588 chip = (version & 0x7C00) >> 10;
589 min_ver = (version & 0x007F);
590 maj_ver = (version & 0x0380) >> 7;
591 if (version & 0x8000)
592 maj_ver |= 0x0008;
593
594 snprintf(bts_scr_name, sizeof(bts_scr_name),
595 "ti-connectivity/TIInit_%d.%d.%d.bts",
596 chip, maj_ver, min_ver);
597
598 err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
599 if (err || !fw->data || !fw->size) {
600 bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
601 err, bts_scr_name);
602 return -EINVAL;
603 }
604 ptr = (void *)fw->data;
605 len = fw->size;
606 /* bts_header to remove out magic number and
607 * version
608 */
609 ptr += sizeof(struct bts_header);
610 len -= sizeof(struct bts_header);
611
612 while (len > 0 && ptr) {
613 bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
614 ((struct bts_action *)ptr)->size,
615 ((struct bts_action *)ptr)->type);
616
617 action_ptr = &(((struct bts_action *)ptr)->data[0]);
618
619 switch (((struct bts_action *)ptr)->type) {
620 case ACTION_SEND_COMMAND: /* action send */
621 bt_dev_dbg(lldev->hu.hdev, "S");
622 cmd = (struct hci_command *)action_ptr;
David Lechner7c6ca122017-12-03 21:21:21 -0600623 if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
Rob Herring37180552017-04-13 10:03:52 -0500624 /* ignore remote change
Derek Robsond98422c2017-07-22 13:47:07 +1200625 * baud rate HCI VS command
626 */
Rob Herring37180552017-04-13 10:03:52 -0500627 bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
628 break;
629 }
630 if (cmd->prefix != 1)
David Lechner059fb822017-12-02 21:01:58 -0600631 bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
Rob Herring37180552017-04-13 10:03:52 -0500632
633 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
634 if (IS_ERR(skb)) {
David Lechner059fb822017-12-02 21:01:58 -0600635 bt_dev_err(lldev->hu.hdev, "send command failed");
Guodong Xu823b8422017-05-22 21:50:42 +0800636 err = PTR_ERR(skb);
Rob Herring37180552017-04-13 10:03:52 -0500637 goto out_rel_fw;
638 }
639 kfree_skb(skb);
640 break;
641 case ACTION_WAIT_EVENT: /* wait */
642 /* no need to wait as command was synchronous */
643 bt_dev_dbg(lldev->hu.hdev, "W");
644 break;
645 case ACTION_DELAY: /* sleep */
646 bt_dev_info(lldev->hu.hdev, "sleep command in scr");
Jia-Ju Bai06633ee2018-01-27 18:03:52 +0800647 msleep(((struct bts_action_delay *)action_ptr)->msec);
Rob Herring37180552017-04-13 10:03:52 -0500648 break;
649 }
650 len -= (sizeof(struct bts_action) +
651 ((struct bts_action *)ptr)->size);
652 ptr += sizeof(struct bts_action) +
653 ((struct bts_action *)ptr)->size;
654 }
655
656out_rel_fw:
657 /* fw download complete */
658 release_firmware(fw);
659 return err;
660}
661
David Lechneraa099392017-12-12 15:59:16 -0600662static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
663{
664 bdaddr_t bdaddr_swapped;
665 struct sk_buff *skb;
666
667 /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
668 * address to be MSB first, but bdaddr_t has the convention of being
669 * LSB first.
670 */
671 baswap(&bdaddr_swapped, bdaddr);
672 skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
673 &bdaddr_swapped, HCI_INIT_TIMEOUT);
674 if (!IS_ERR(skb))
675 kfree_skb(skb);
676
677 return PTR_ERR_OR_ZERO(skb);
678}
679
Rob Herring37180552017-04-13 10:03:52 -0500680static int ll_setup(struct hci_uart *hu)
681{
682 int err, retry = 3;
683 struct ll_device *lldev;
684 struct serdev_device *serdev = hu->serdev;
685 u32 speed;
686
687 if (!serdev)
688 return 0;
689
690 lldev = serdev_device_get_drvdata(serdev);
691
David Lechneraa099392017-12-12 15:59:16 -0600692 hu->hdev->set_bdaddr = ll_set_bdaddr;
693
Rob Herring37180552017-04-13 10:03:52 -0500694 serdev_device_set_flow_control(serdev, true);
695
696 do {
David Lechnerd54fdcf2017-12-02 20:43:55 -0600697 /* Reset the Bluetooth device */
Rob Herring37180552017-04-13 10:03:52 -0500698 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
699 msleep(5);
700 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
David Lechnerd54fdcf2017-12-02 20:43:55 -0600701 err = serdev_device_wait_for_cts(serdev, true, 200);
702 if (err) {
703 bt_dev_err(hu->hdev, "Failed to get CTS");
704 return err;
705 }
Rob Herring37180552017-04-13 10:03:52 -0500706
707 err = download_firmware(lldev);
708 if (!err)
709 break;
710
711 /* Toggle BT_EN and retry */
712 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
713 } while (retry--);
714
715 if (err)
716 return err;
717
David Lechner0e58d0c2017-12-12 17:54:12 -0600718 /* Set BD address if one was specified at probe */
719 if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
720 /* This means that there was an error getting the BD address
721 * during probe, so mark the device as having a bad address.
722 */
723 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
724 } else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
725 err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
726 if (err)
727 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
728 }
729
Rob Herring37180552017-04-13 10:03:52 -0500730 /* Operational speed if any */
731 if (hu->oper_speed)
732 speed = hu->oper_speed;
733 else if (hu->proto->oper_speed)
734 speed = hu->proto->oper_speed;
735 else
736 speed = 0;
737
738 if (speed) {
David Lechnerc30b93e2017-12-07 20:22:19 -0600739 __le32 speed_le = cpu_to_le32(speed);
David Lechner7c6ca122017-12-03 21:21:21 -0600740 struct sk_buff *skb;
741
742 skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
David Lechnerc30b93e2017-12-07 20:22:19 -0600743 sizeof(speed_le), &speed_le,
744 HCI_INIT_TIMEOUT);
Rob Herring37180552017-04-13 10:03:52 -0500745 if (!IS_ERR(skb)) {
746 kfree_skb(skb);
747 serdev_device_set_baudrate(serdev, speed);
748 }
749 }
750
751 return 0;
752}
753
754static const struct hci_uart_proto llp;
755
756static int hci_ti_probe(struct serdev_device *serdev)
757{
758 struct hci_uart *hu;
759 struct ll_device *lldev;
David Lechner0e58d0c2017-12-12 17:54:12 -0600760 struct nvmem_cell *bdaddr_cell;
Rob Herring37180552017-04-13 10:03:52 -0500761 u32 max_speed = 3000000;
762
763 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
764 if (!lldev)
765 return -ENOMEM;
766 hu = &lldev->hu;
767
768 serdev_device_set_drvdata(serdev, lldev);
769 lldev->serdev = hu->serdev = serdev;
770
771 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW);
772 if (IS_ERR(lldev->enable_gpio))
773 return PTR_ERR(lldev->enable_gpio);
774
Ulf Hansson43d3d092017-06-07 11:08:21 +0200775 lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
776 if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
777 return PTR_ERR(lldev->ext_clk);
778
Rob Herring37180552017-04-13 10:03:52 -0500779 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
780 hci_uart_set_speeds(hu, 115200, max_speed);
781
David Lechner0e58d0c2017-12-12 17:54:12 -0600782 /* optional BD address from nvram */
783 bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
784 if (IS_ERR(bdaddr_cell)) {
785 int err = PTR_ERR(bdaddr_cell);
786
787 if (err == -EPROBE_DEFER)
788 return err;
789
790 /* ENOENT means there is no matching nvmem cell and ENOSYS
791 * means that nvmem is not enabled in the kernel configuration.
792 */
793 if (err != -ENOENT && err != -ENOSYS) {
794 /* If there was some other error, give userspace a
795 * chance to fix the problem instead of failing to load
796 * the driver. Using BDADDR_NONE as a flag that is
797 * tested later in the setup function.
798 */
799 dev_warn(&serdev->dev,
800 "Failed to get \"bd-address\" nvmem cell (%d)\n",
801 err);
802 bacpy(&lldev->bdaddr, BDADDR_NONE);
803 }
804 } else {
805 bdaddr_t *bdaddr;
806 size_t len;
807
808 bdaddr = nvmem_cell_read(bdaddr_cell, &len);
809 nvmem_cell_put(bdaddr_cell);
810 if (IS_ERR(bdaddr)) {
811 dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
812 return PTR_ERR(bdaddr);
813 }
814 if (len != sizeof(bdaddr_t)) {
815 dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
816 kfree(bdaddr);
817 return -EINVAL;
818 }
819
820 /* As per the device tree bindings, the value from nvmem is
821 * expected to be MSB first, but in the kernel it is expected
822 * that bdaddr_t is LSB first.
823 */
824 baswap(&lldev->bdaddr, bdaddr);
825 kfree(bdaddr);
826 }
827
Rob Herring37180552017-04-13 10:03:52 -0500828 return hci_uart_register_device(hu, &llp);
829}
830
831static void hci_ti_remove(struct serdev_device *serdev)
832{
833 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
Rob Herring37180552017-04-13 10:03:52 -0500834
Ian Molton37f52582017-07-08 17:37:43 +0100835 hci_uart_unregister_device(&lldev->hu);
Rob Herring37180552017-04-13 10:03:52 -0500836}
837
838static const struct of_device_id hci_ti_of_match[] = {
David Lechner41664932017-12-12 18:29:31 -0600839 { .compatible = "ti,cc2560" },
Sebastian Reichelc127a872017-06-08 22:58:45 +0200840 { .compatible = "ti,wl1271-st" },
841 { .compatible = "ti,wl1273-st" },
842 { .compatible = "ti,wl1281-st" },
843 { .compatible = "ti,wl1283-st" },
844 { .compatible = "ti,wl1285-st" },
845 { .compatible = "ti,wl1801-st" },
846 { .compatible = "ti,wl1805-st" },
847 { .compatible = "ti,wl1807-st" },
Rob Herring37180552017-04-13 10:03:52 -0500848 { .compatible = "ti,wl1831-st" },
849 { .compatible = "ti,wl1835-st" },
850 { .compatible = "ti,wl1837-st" },
851 {},
852};
853MODULE_DEVICE_TABLE(of, hci_ti_of_match);
854
855static struct serdev_device_driver hci_ti_drv = {
856 .driver = {
857 .name = "hci-ti",
858 .of_match_table = of_match_ptr(hci_ti_of_match),
859 },
860 .probe = hci_ti_probe,
861 .remove = hci_ti_remove,
862};
863#else
864#define ll_setup NULL
865#endif
866
Marcel Holtmann4ee7ef12015-04-04 22:11:43 -0700867static const struct hci_uart_proto llp = {
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200868 .id = HCI_UART_LL,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700869 .name = "LL",
Rob Herring37180552017-04-13 10:03:52 -0500870 .setup = ll_setup,
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200871 .open = ll_open,
872 .close = ll_close,
873 .recv = ll_recv,
874 .enqueue = ll_enqueue,
875 .dequeue = ll_dequeue,
876 .flush = ll_flush,
877};
878
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300879int __init ll_init(void)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200880{
Rob Herring37180552017-04-13 10:03:52 -0500881 serdev_device_driver_register(&hci_ti_drv);
882
Marcel Holtmann01009ee2015-04-04 22:27:35 -0700883 return hci_uart_register_proto(&llp);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200884}
885
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300886int __exit ll_deinit(void)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200887{
Rob Herring37180552017-04-13 10:03:52 -0500888 serdev_device_driver_unregister(&hci_ti_drv);
889
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200890 return hci_uart_unregister_proto(&llp);
891}