blob: c948e8dcc5536b4973d4ec8fda8b1d8c1eaf861e [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>
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020056
57#include "hci_uart.h"
58
David Lechner7c6ca122017-12-03 21:21:21 -060059/* Vendor-specific HCI commands */
David Lechneraa099392017-12-12 15:59:16 -060060#define HCI_VS_WRITE_BD_ADDR 0xfc06
David Lechner7c6ca122017-12-03 21:21:21 -060061#define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36
62
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020063/* HCILL commands */
64#define HCILL_GO_TO_SLEEP_IND 0x30
65#define HCILL_GO_TO_SLEEP_ACK 0x31
66#define HCILL_WAKE_UP_IND 0x32
67#define HCILL_WAKE_UP_ACK 0x33
68
69/* HCILL receiver States */
70#define HCILL_W4_PACKET_TYPE 0
71#define HCILL_W4_EVENT_HDR 1
72#define HCILL_W4_ACL_HDR 2
73#define HCILL_W4_SCO_HDR 3
74#define HCILL_W4_DATA 4
75
76/* HCILL states */
77enum hcill_states_e {
78 HCILL_ASLEEP,
79 HCILL_ASLEEP_TO_AWAKE,
80 HCILL_AWAKE,
81 HCILL_AWAKE_TO_ASLEEP
82};
83
84struct hcill_cmd {
85 u8 cmd;
Gustavo F. Padovan81ca4052010-07-19 13:54:05 -030086} __packed;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020087
Rob Herring37180552017-04-13 10:03:52 -050088struct ll_device {
89 struct hci_uart hu;
90 struct serdev_device *serdev;
91 struct gpio_desc *enable_gpio;
Ulf Hansson43d3d092017-06-07 11:08:21 +020092 struct clk *ext_clk;
Rob Herring37180552017-04-13 10:03:52 -050093};
94
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +020095struct ll_struct {
96 unsigned long rx_state;
97 unsigned long rx_count;
98 struct sk_buff *rx_skb;
99 struct sk_buff_head txq;
100 spinlock_t hcill_lock; /* HCILL state lock */
101 unsigned long hcill_state; /* HCILL power state */
102 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
103};
104
105/*
106 * Builds and sends an HCILL command packet.
107 * These are very simple packets with only 1 cmd byte
108 */
109static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
110{
111 int err = 0;
112 struct sk_buff *skb = NULL;
113 struct ll_struct *ll = hu->priv;
114 struct hcill_cmd *hcill_packet;
115
116 BT_DBG("hu %p cmd 0x%x", hu, cmd);
117
118 /* allocate packet */
119 skb = bt_skb_alloc(1, GFP_ATOMIC);
120 if (!skb) {
121 BT_ERR("cannot allocate memory for HCILL packet");
122 err = -ENOMEM;
123 goto out;
124 }
125
126 /* prepare packet */
Johannes Berg4df864c2017-06-16 14:29:21 +0200127 hcill_packet = skb_put(skb, 1);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200128 hcill_packet->cmd = cmd;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200129
130 /* send packet */
131 skb_queue_tail(&ll->txq, skb);
132out:
133 return err;
134}
135
136/* Initialize protocol */
137static int ll_open(struct hci_uart *hu)
138{
139 struct ll_struct *ll;
140
141 BT_DBG("hu %p", hu);
142
David Herrmann9eb648c2012-01-07 15:19:37 +0100143 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200144 if (!ll)
145 return -ENOMEM;
146
147 skb_queue_head_init(&ll->txq);
148 skb_queue_head_init(&ll->tx_wait_q);
149 spin_lock_init(&ll->hcill_lock);
150
151 ll->hcill_state = HCILL_AWAKE;
152
153 hu->priv = ll;
154
Ulf Hansson43d3d092017-06-07 11:08:21 +0200155 if (hu->serdev) {
156 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
Rob Herring37180552017-04-13 10:03:52 -0500157 serdev_device_open(hu->serdev);
Ulf Hansson43d3d092017-06-07 11:08:21 +0200158 if (!IS_ERR(lldev->ext_clk))
159 clk_prepare_enable(lldev->ext_clk);
160 }
Rob Herring37180552017-04-13 10:03:52 -0500161
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200162 return 0;
163}
164
165/* Flush protocol data */
166static int ll_flush(struct hci_uart *hu)
167{
168 struct ll_struct *ll = hu->priv;
169
170 BT_DBG("hu %p", hu);
171
172 skb_queue_purge(&ll->tx_wait_q);
173 skb_queue_purge(&ll->txq);
174
175 return 0;
176}
177
178/* Close protocol */
179static int ll_close(struct hci_uart *hu)
180{
181 struct ll_struct *ll = hu->priv;
182
183 BT_DBG("hu %p", hu);
184
185 skb_queue_purge(&ll->tx_wait_q);
186 skb_queue_purge(&ll->txq);
187
Wei Yongjunb1fb0682009-02-25 18:09:33 +0800188 kfree_skb(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200189
Rob Herring37180552017-04-13 10:03:52 -0500190 if (hu->serdev) {
191 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
192 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
193
Ulf Hansson43d3d092017-06-07 11:08:21 +0200194 clk_disable_unprepare(lldev->ext_clk);
195
Rob Herring37180552017-04-13 10:03:52 -0500196 serdev_device_close(hu->serdev);
197 }
198
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200199 hu->priv = NULL;
200
201 kfree(ll);
202
203 return 0;
204}
205
206/*
207 * internal function, which does common work of the device wake up process:
208 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
209 * 2. changes internal state to HCILL_AWAKE.
210 * Note: assumes that hcill_lock spinlock is taken,
211 * shouldn't be called otherwise!
212 */
213static void __ll_do_awake(struct ll_struct *ll)
214{
215 struct sk_buff *skb = NULL;
216
217 while ((skb = skb_dequeue(&ll->tx_wait_q)))
218 skb_queue_tail(&ll->txq, skb);
219
220 ll->hcill_state = HCILL_AWAKE;
221}
222
223/*
224 * Called upon a wake-up-indication from the device
225 */
226static void ll_device_want_to_wakeup(struct hci_uart *hu)
227{
228 unsigned long flags;
229 struct ll_struct *ll = hu->priv;
230
231 BT_DBG("hu %p", hu);
232
233 /* lock hcill state */
234 spin_lock_irqsave(&ll->hcill_lock, flags);
235
236 switch (ll->hcill_state) {
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800237 case HCILL_ASLEEP_TO_AWAKE:
238 /*
239 * This state means that both the host and the BRF chip
240 * have simultaneously sent a wake-up-indication packet.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300241 * Traditionally, in this case, receiving a wake-up-indication
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800242 * was enough and an additional wake-up-ack wasn't needed.
243 * This has changed with the BRF6350, which does require an
244 * explicit wake-up-ack. Other BRF versions, which do not
245 * require an explicit ack here, do accept it, thus it is
246 * perfectly safe to always send one.
247 */
248 BT_DBG("dual wake-up-indication");
Gustavo A. R. Silvafac72b22017-10-12 17:24:02 -0500249 /* fall through */
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200250 case HCILL_ASLEEP:
251 /* acknowledge device wake up */
252 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
253 BT_ERR("cannot acknowledge device wake up");
254 goto out;
255 }
256 break;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200257 default:
Ohad Ben-Cohen5c548222008-01-10 22:24:43 -0800258 /* any other state is illegal */
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200259 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
260 break;
261 }
262
263 /* send pending packets and change state to HCILL_AWAKE */
264 __ll_do_awake(ll);
265
266out:
267 spin_unlock_irqrestore(&ll->hcill_lock, flags);
268
269 /* actually send the packets */
270 hci_uart_tx_wakeup(hu);
271}
272
273/*
274 * Called upon a sleep-indication from the device
275 */
276static void ll_device_want_to_sleep(struct hci_uart *hu)
277{
278 unsigned long flags;
279 struct ll_struct *ll = hu->priv;
280
281 BT_DBG("hu %p", hu);
282
283 /* lock hcill state */
284 spin_lock_irqsave(&ll->hcill_lock, flags);
285
286 /* sanity check */
287 if (ll->hcill_state != HCILL_AWAKE)
288 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
289
290 /* acknowledge device sleep */
291 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
292 BT_ERR("cannot acknowledge device sleep");
293 goto out;
294 }
295
296 /* update state */
297 ll->hcill_state = HCILL_ASLEEP;
298
299out:
300 spin_unlock_irqrestore(&ll->hcill_lock, flags);
301
302 /* actually send the sleep ack packet */
303 hci_uart_tx_wakeup(hu);
304}
305
306/*
307 * Called upon wake-up-acknowledgement from the device
308 */
309static void ll_device_woke_up(struct hci_uart *hu)
310{
311 unsigned long flags;
312 struct ll_struct *ll = hu->priv;
313
314 BT_DBG("hu %p", hu);
315
316 /* lock hcill state */
317 spin_lock_irqsave(&ll->hcill_lock, flags);
318
319 /* sanity check */
320 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
321 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
322
323 /* send pending packets and change state to HCILL_AWAKE */
324 __ll_do_awake(ll);
325
326 spin_unlock_irqrestore(&ll->hcill_lock, flags);
327
328 /* actually send the packets */
329 hci_uart_tx_wakeup(hu);
330}
331
332/* Enqueue frame for transmittion (padding, crc, etc) */
333/* may be called from two simultaneous tasklets */
334static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
335{
336 unsigned long flags = 0;
337 struct ll_struct *ll = hu->priv;
338
339 BT_DBG("hu %p skb %p", hu, skb);
340
341 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100342 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200343
344 /* lock hcill state */
345 spin_lock_irqsave(&ll->hcill_lock, flags);
346
347 /* act according to current state */
348 switch (ll->hcill_state) {
349 case HCILL_AWAKE:
350 BT_DBG("device awake, sending normally");
351 skb_queue_tail(&ll->txq, skb);
352 break;
353 case HCILL_ASLEEP:
354 BT_DBG("device asleep, waking up and queueing packet");
355 /* save packet for later */
356 skb_queue_tail(&ll->tx_wait_q, skb);
357 /* awake device */
358 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
359 BT_ERR("cannot wake up device");
360 break;
361 }
362 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
363 break;
364 case HCILL_ASLEEP_TO_AWAKE:
365 BT_DBG("device waking up, queueing packet");
366 /* transient state; just keep packet for later */
367 skb_queue_tail(&ll->tx_wait_q, skb);
368 break;
369 default:
370 BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
371 kfree_skb(skb);
372 break;
373 }
374
375 spin_unlock_irqrestore(&ll->hcill_lock, flags);
376
377 return 0;
378}
379
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700380static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200381{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -0300382 int room = skb_tailroom(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200383
384 BT_DBG("len %d room %d", len, room);
385
386 if (!len) {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700387 hci_recv_frame(hdev, ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200388 } else if (len > room) {
389 BT_ERR("Data length is too large");
390 kfree_skb(ll->rx_skb);
391 } else {
392 ll->rx_state = HCILL_W4_DATA;
393 ll->rx_count = len;
394 return len;
395 }
396
397 ll->rx_state = HCILL_W4_PACKET_TYPE;
398 ll->rx_skb = NULL;
399 ll->rx_count = 0;
400
401 return 0;
402}
403
404/* Recv data */
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700405static int ll_recv(struct hci_uart *hu, const void *data, int count)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200406{
407 struct ll_struct *ll = hu->priv;
Marcel Holtmann9d1c40e2015-04-04 20:59:41 -0700408 const char *ptr;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200409 struct hci_event_hdr *eh;
410 struct hci_acl_hdr *ah;
411 struct hci_sco_hdr *sh;
Gustavo Padovanfc5fef62012-05-23 04:04:19 -0300412 int len, type, dlen;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200413
414 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
415
416 ptr = data;
417 while (count) {
418 if (ll->rx_count) {
419 len = min_t(unsigned int, ll->rx_count, count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200420 skb_put_data(ll->rx_skb, ptr, len);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200421 ll->rx_count -= len; count -= len; ptr += len;
422
423 if (ll->rx_count)
424 continue;
425
426 switch (ll->rx_state) {
427 case HCILL_W4_DATA:
428 BT_DBG("Complete data");
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700429 hci_recv_frame(hu->hdev, ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200430
431 ll->rx_state = HCILL_W4_PACKET_TYPE;
432 ll->rx_skb = NULL;
433 continue;
434
435 case HCILL_W4_EVENT_HDR:
Gustavo F. Padovanacce90d2010-05-01 16:15:34 -0300436 eh = hci_event_hdr(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200437
438 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
439
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700440 ll_check_data_len(hu->hdev, ll, eh->plen);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200441 continue;
442
443 case HCILL_W4_ACL_HDR:
Gustavo F. Padovanacce90d2010-05-01 16:15:34 -0300444 ah = hci_acl_hdr(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200445 dlen = __le16_to_cpu(ah->dlen);
446
447 BT_DBG("ACL header: dlen %d", dlen);
448
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700449 ll_check_data_len(hu->hdev, ll, dlen);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200450 continue;
451
452 case HCILL_W4_SCO_HDR:
Gustavo F. Padovanacce90d2010-05-01 16:15:34 -0300453 sh = hci_sco_hdr(ll->rx_skb);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200454
455 BT_DBG("SCO header: dlen %d", sh->dlen);
456
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700457 ll_check_data_len(hu->hdev, ll, sh->dlen);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200458 continue;
459 }
460 }
461
462 /* HCILL_W4_PACKET_TYPE */
463 switch (*ptr) {
464 case HCI_EVENT_PKT:
465 BT_DBG("Event packet");
466 ll->rx_state = HCILL_W4_EVENT_HDR;
467 ll->rx_count = HCI_EVENT_HDR_SIZE;
468 type = HCI_EVENT_PKT;
469 break;
470
471 case HCI_ACLDATA_PKT:
472 BT_DBG("ACL packet");
473 ll->rx_state = HCILL_W4_ACL_HDR;
474 ll->rx_count = HCI_ACL_HDR_SIZE;
475 type = HCI_ACLDATA_PKT;
476 break;
477
478 case HCI_SCODATA_PKT:
479 BT_DBG("SCO packet");
480 ll->rx_state = HCILL_W4_SCO_HDR;
481 ll->rx_count = HCI_SCO_HDR_SIZE;
482 type = HCI_SCODATA_PKT;
483 break;
484
485 /* HCILL signals */
486 case HCILL_GO_TO_SLEEP_IND:
487 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
488 ll_device_want_to_sleep(hu);
489 ptr++; count--;
490 continue;
491
492 case HCILL_GO_TO_SLEEP_ACK:
493 /* shouldn't happen */
494 BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
495 ptr++; count--;
496 continue;
497
498 case HCILL_WAKE_UP_IND:
499 BT_DBG("HCILL_WAKE_UP_IND packet");
500 ll_device_want_to_wakeup(hu);
501 ptr++; count--;
502 continue;
503
504 case HCILL_WAKE_UP_ACK:
505 BT_DBG("HCILL_WAKE_UP_ACK packet");
506 ll_device_woke_up(hu);
507 ptr++; count--;
508 continue;
509
510 default:
511 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
512 hu->hdev->stat.err_rx++;
513 ptr++; count--;
514 continue;
Peter Senna Tschudind650cca2012-09-07 17:24:42 +0200515 }
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200516
517 ptr++; count--;
518
519 /* Allocate packet */
520 ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
521 if (!ll->rx_skb) {
522 BT_ERR("Can't allocate mem for new packet");
523 ll->rx_state = HCILL_W4_PACKET_TYPE;
524 ll->rx_count = 0;
Gustavo F. Padovanfe1aff72010-05-01 16:15:34 -0300525 return -ENOMEM;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200526 }
527
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100528 hci_skb_pkt_type(ll->rx_skb) = type;
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200529 }
530
531 return count;
532}
533
534static struct sk_buff *ll_dequeue(struct hci_uart *hu)
535{
536 struct ll_struct *ll = hu->priv;
537 return skb_dequeue(&ll->txq);
538}
539
Rob Herring37180552017-04-13 10:03:52 -0500540#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
541static int read_local_version(struct hci_dev *hdev)
542{
543 int err = 0;
544 unsigned short version = 0;
545 struct sk_buff *skb;
546 struct hci_rp_read_local_version *ver;
547
548 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, HCI_INIT_TIMEOUT);
549 if (IS_ERR(skb)) {
550 bt_dev_err(hdev, "Reading TI version information failed (%ld)",
551 PTR_ERR(skb));
Sebastian Reichelf2edd9f2017-04-15 23:54:13 +0200552 return PTR_ERR(skb);
Rob Herring37180552017-04-13 10:03:52 -0500553 }
554 if (skb->len != sizeof(*ver)) {
555 err = -EILSEQ;
556 goto out;
557 }
558
559 ver = (struct hci_rp_read_local_version *)skb->data;
560 if (le16_to_cpu(ver->manufacturer) != 13) {
561 err = -ENODEV;
562 goto out;
563 }
564
565 version = le16_to_cpu(ver->lmp_subver);
566
567out:
568 if (err) bt_dev_err(hdev, "Failed to read TI version info: %d", err);
569 kfree_skb(skb);
570 return err ? err : version;
571}
572
573/**
574 * download_firmware -
575 * internal function which parses through the .bts firmware
576 * script file intreprets SEND, DELAY actions only as of now
577 */
578static int download_firmware(struct ll_device *lldev)
579{
580 unsigned short chip, min_ver, maj_ver;
581 int version, err, len;
582 unsigned char *ptr, *action_ptr;
583 unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
584 const struct firmware *fw;
585 struct sk_buff *skb;
586 struct hci_command *cmd;
587
588 version = read_local_version(lldev->hu.hdev);
589 if (version < 0)
590 return version;
591
592 chip = (version & 0x7C00) >> 10;
593 min_ver = (version & 0x007F);
594 maj_ver = (version & 0x0380) >> 7;
595 if (version & 0x8000)
596 maj_ver |= 0x0008;
597
598 snprintf(bts_scr_name, sizeof(bts_scr_name),
599 "ti-connectivity/TIInit_%d.%d.%d.bts",
600 chip, maj_ver, min_ver);
601
602 err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
603 if (err || !fw->data || !fw->size) {
604 bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
605 err, bts_scr_name);
606 return -EINVAL;
607 }
608 ptr = (void *)fw->data;
609 len = fw->size;
610 /* bts_header to remove out magic number and
611 * version
612 */
613 ptr += sizeof(struct bts_header);
614 len -= sizeof(struct bts_header);
615
616 while (len > 0 && ptr) {
617 bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
618 ((struct bts_action *)ptr)->size,
619 ((struct bts_action *)ptr)->type);
620
621 action_ptr = &(((struct bts_action *)ptr)->data[0]);
622
623 switch (((struct bts_action *)ptr)->type) {
624 case ACTION_SEND_COMMAND: /* action send */
625 bt_dev_dbg(lldev->hu.hdev, "S");
626 cmd = (struct hci_command *)action_ptr;
David Lechner7c6ca122017-12-03 21:21:21 -0600627 if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
Rob Herring37180552017-04-13 10:03:52 -0500628 /* ignore remote change
Derek Robsond98422c2017-07-22 13:47:07 +1200629 * baud rate HCI VS command
630 */
Rob Herring37180552017-04-13 10:03:52 -0500631 bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
632 break;
633 }
634 if (cmd->prefix != 1)
David Lechner059fb822017-12-02 21:01:58 -0600635 bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
Rob Herring37180552017-04-13 10:03:52 -0500636
637 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
638 if (IS_ERR(skb)) {
David Lechner059fb822017-12-02 21:01:58 -0600639 bt_dev_err(lldev->hu.hdev, "send command failed");
Guodong Xu823b8422017-05-22 21:50:42 +0800640 err = PTR_ERR(skb);
Rob Herring37180552017-04-13 10:03:52 -0500641 goto out_rel_fw;
642 }
643 kfree_skb(skb);
644 break;
645 case ACTION_WAIT_EVENT: /* wait */
646 /* no need to wait as command was synchronous */
647 bt_dev_dbg(lldev->hu.hdev, "W");
648 break;
649 case ACTION_DELAY: /* sleep */
650 bt_dev_info(lldev->hu.hdev, "sleep command in scr");
651 mdelay(((struct bts_action_delay *)action_ptr)->msec);
652 break;
653 }
654 len -= (sizeof(struct bts_action) +
655 ((struct bts_action *)ptr)->size);
656 ptr += sizeof(struct bts_action) +
657 ((struct bts_action *)ptr)->size;
658 }
659
660out_rel_fw:
661 /* fw download complete */
662 release_firmware(fw);
663 return err;
664}
665
David Lechneraa099392017-12-12 15:59:16 -0600666static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
667{
668 bdaddr_t bdaddr_swapped;
669 struct sk_buff *skb;
670
671 /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
672 * address to be MSB first, but bdaddr_t has the convention of being
673 * LSB first.
674 */
675 baswap(&bdaddr_swapped, bdaddr);
676 skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
677 &bdaddr_swapped, HCI_INIT_TIMEOUT);
678 if (!IS_ERR(skb))
679 kfree_skb(skb);
680
681 return PTR_ERR_OR_ZERO(skb);
682}
683
Rob Herring37180552017-04-13 10:03:52 -0500684static int ll_setup(struct hci_uart *hu)
685{
686 int err, retry = 3;
687 struct ll_device *lldev;
688 struct serdev_device *serdev = hu->serdev;
689 u32 speed;
690
691 if (!serdev)
692 return 0;
693
694 lldev = serdev_device_get_drvdata(serdev);
695
David Lechneraa099392017-12-12 15:59:16 -0600696 hu->hdev->set_bdaddr = ll_set_bdaddr;
697
Rob Herring37180552017-04-13 10:03:52 -0500698 serdev_device_set_flow_control(serdev, true);
699
700 do {
David Lechnerd54fdcf2017-12-02 20:43:55 -0600701 /* Reset the Bluetooth device */
Rob Herring37180552017-04-13 10:03:52 -0500702 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
703 msleep(5);
704 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
David Lechnerd54fdcf2017-12-02 20:43:55 -0600705 err = serdev_device_wait_for_cts(serdev, true, 200);
706 if (err) {
707 bt_dev_err(hu->hdev, "Failed to get CTS");
708 return err;
709 }
Rob Herring37180552017-04-13 10:03:52 -0500710
711 err = download_firmware(lldev);
712 if (!err)
713 break;
714
715 /* Toggle BT_EN and retry */
716 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
717 } while (retry--);
718
719 if (err)
720 return err;
721
722 /* Operational speed if any */
723 if (hu->oper_speed)
724 speed = hu->oper_speed;
725 else if (hu->proto->oper_speed)
726 speed = hu->proto->oper_speed;
727 else
728 speed = 0;
729
730 if (speed) {
David Lechnerc30b93e2017-12-07 20:22:19 -0600731 __le32 speed_le = cpu_to_le32(speed);
David Lechner7c6ca122017-12-03 21:21:21 -0600732 struct sk_buff *skb;
733
734 skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
David Lechnerc30b93e2017-12-07 20:22:19 -0600735 sizeof(speed_le), &speed_le,
736 HCI_INIT_TIMEOUT);
Rob Herring37180552017-04-13 10:03:52 -0500737 if (!IS_ERR(skb)) {
738 kfree_skb(skb);
739 serdev_device_set_baudrate(serdev, speed);
740 }
741 }
742
743 return 0;
744}
745
746static const struct hci_uart_proto llp;
747
748static int hci_ti_probe(struct serdev_device *serdev)
749{
750 struct hci_uart *hu;
751 struct ll_device *lldev;
752 u32 max_speed = 3000000;
753
754 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
755 if (!lldev)
756 return -ENOMEM;
757 hu = &lldev->hu;
758
759 serdev_device_set_drvdata(serdev, lldev);
760 lldev->serdev = hu->serdev = serdev;
761
762 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW);
763 if (IS_ERR(lldev->enable_gpio))
764 return PTR_ERR(lldev->enable_gpio);
765
Ulf Hansson43d3d092017-06-07 11:08:21 +0200766 lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
767 if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
768 return PTR_ERR(lldev->ext_clk);
769
Rob Herring37180552017-04-13 10:03:52 -0500770 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
771 hci_uart_set_speeds(hu, 115200, max_speed);
772
773 return hci_uart_register_device(hu, &llp);
774}
775
776static void hci_ti_remove(struct serdev_device *serdev)
777{
778 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
Rob Herring37180552017-04-13 10:03:52 -0500779
Ian Molton37f52582017-07-08 17:37:43 +0100780 hci_uart_unregister_device(&lldev->hu);
Rob Herring37180552017-04-13 10:03:52 -0500781}
782
783static const struct of_device_id hci_ti_of_match[] = {
Sebastian Reichelc127a872017-06-08 22:58:45 +0200784 { .compatible = "ti,wl1271-st" },
785 { .compatible = "ti,wl1273-st" },
786 { .compatible = "ti,wl1281-st" },
787 { .compatible = "ti,wl1283-st" },
788 { .compatible = "ti,wl1285-st" },
789 { .compatible = "ti,wl1801-st" },
790 { .compatible = "ti,wl1805-st" },
791 { .compatible = "ti,wl1807-st" },
Rob Herring37180552017-04-13 10:03:52 -0500792 { .compatible = "ti,wl1831-st" },
793 { .compatible = "ti,wl1835-st" },
794 { .compatible = "ti,wl1837-st" },
795 {},
796};
797MODULE_DEVICE_TABLE(of, hci_ti_of_match);
798
799static struct serdev_device_driver hci_ti_drv = {
800 .driver = {
801 .name = "hci-ti",
802 .of_match_table = of_match_ptr(hci_ti_of_match),
803 },
804 .probe = hci_ti_probe,
805 .remove = hci_ti_remove,
806};
807#else
808#define ll_setup NULL
809#endif
810
Marcel Holtmann4ee7ef12015-04-04 22:11:43 -0700811static const struct hci_uart_proto llp = {
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200812 .id = HCI_UART_LL,
Marcel Holtmann7c40fb82015-04-04 22:27:34 -0700813 .name = "LL",
Rob Herring37180552017-04-13 10:03:52 -0500814 .setup = ll_setup,
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200815 .open = ll_open,
816 .close = ll_close,
817 .recv = ll_recv,
818 .enqueue = ll_enqueue,
819 .dequeue = ll_dequeue,
820 .flush = ll_flush,
821};
822
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300823int __init ll_init(void)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200824{
Rob Herring37180552017-04-13 10:03:52 -0500825 serdev_device_driver_register(&hci_ti_drv);
826
Marcel Holtmann01009ee2015-04-04 22:27:35 -0700827 return hci_uart_register_proto(&llp);
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200828}
829
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300830int __exit ll_deinit(void)
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200831{
Rob Herring37180552017-04-13 10:03:52 -0500832 serdev_device_driver_unregister(&hci_ti_drv);
833
Ohad Ben-Cohen166d2f62007-10-20 13:42:36 +0200834 return hci_uart_unregister_proto(&llp);
835}