Loading...
1/*
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>
37#include <linux/interrupt.h>
38#include <linux/ptrace.h>
39#include <linux/poll.h>
40
41#include <linux/slab.h>
42#include <linux/tty.h>
43#include <linux/errno.h>
44#include <linux/string.h>
45#include <linux/signal.h>
46#include <linux/ioctl.h>
47#include <linux/skbuff.h>
48
49#include <net/bluetooth/bluetooth.h>
50#include <net/bluetooth/hci_core.h>
51
52#include "hci_uart.h"
53
54/* HCILL commands */
55#define HCILL_GO_TO_SLEEP_IND 0x30
56#define HCILL_GO_TO_SLEEP_ACK 0x31
57#define HCILL_WAKE_UP_IND 0x32
58#define HCILL_WAKE_UP_ACK 0x33
59
60/* HCILL receiver States */
61#define HCILL_W4_PACKET_TYPE 0
62#define HCILL_W4_EVENT_HDR 1
63#define HCILL_W4_ACL_HDR 2
64#define HCILL_W4_SCO_HDR 3
65#define HCILL_W4_DATA 4
66
67/* HCILL states */
68enum hcill_states_e {
69 HCILL_ASLEEP,
70 HCILL_ASLEEP_TO_AWAKE,
71 HCILL_AWAKE,
72 HCILL_AWAKE_TO_ASLEEP
73};
74
75struct hcill_cmd {
76 u8 cmd;
77} __packed;
78
79struct ll_struct {
80 unsigned long rx_state;
81 unsigned long rx_count;
82 struct sk_buff *rx_skb;
83 struct sk_buff_head txq;
84 spinlock_t hcill_lock; /* HCILL state lock */
85 unsigned long hcill_state; /* HCILL power state */
86 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
87};
88
89/*
90 * Builds and sends an HCILL command packet.
91 * These are very simple packets with only 1 cmd byte
92 */
93static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
94{
95 int err = 0;
96 struct sk_buff *skb = NULL;
97 struct ll_struct *ll = hu->priv;
98 struct hcill_cmd *hcill_packet;
99
100 BT_DBG("hu %p cmd 0x%x", hu, cmd);
101
102 /* allocate packet */
103 skb = bt_skb_alloc(1, GFP_ATOMIC);
104 if (!skb) {
105 BT_ERR("cannot allocate memory for HCILL packet");
106 err = -ENOMEM;
107 goto out;
108 }
109
110 /* prepare packet */
111 hcill_packet = (struct hcill_cmd *) skb_put(skb, 1);
112 hcill_packet->cmd = cmd;
113 skb->dev = (void *) hu->hdev;
114
115 /* send packet */
116 skb_queue_tail(&ll->txq, skb);
117out:
118 return err;
119}
120
121/* Initialize protocol */
122static int ll_open(struct hci_uart *hu)
123{
124 struct ll_struct *ll;
125
126 BT_DBG("hu %p", hu);
127
128 ll = kzalloc(sizeof(*ll), GFP_ATOMIC);
129 if (!ll)
130 return -ENOMEM;
131
132 skb_queue_head_init(&ll->txq);
133 skb_queue_head_init(&ll->tx_wait_q);
134 spin_lock_init(&ll->hcill_lock);
135
136 ll->hcill_state = HCILL_AWAKE;
137
138 hu->priv = ll;
139
140 return 0;
141}
142
143/* Flush protocol data */
144static int ll_flush(struct hci_uart *hu)
145{
146 struct ll_struct *ll = hu->priv;
147
148 BT_DBG("hu %p", hu);
149
150 skb_queue_purge(&ll->tx_wait_q);
151 skb_queue_purge(&ll->txq);
152
153 return 0;
154}
155
156/* Close protocol */
157static int ll_close(struct hci_uart *hu)
158{
159 struct ll_struct *ll = hu->priv;
160
161 BT_DBG("hu %p", hu);
162
163 skb_queue_purge(&ll->tx_wait_q);
164 skb_queue_purge(&ll->txq);
165
166 kfree_skb(ll->rx_skb);
167
168 hu->priv = NULL;
169
170 kfree(ll);
171
172 return 0;
173}
174
175/*
176 * internal function, which does common work of the device wake up process:
177 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
178 * 2. changes internal state to HCILL_AWAKE.
179 * Note: assumes that hcill_lock spinlock is taken,
180 * shouldn't be called otherwise!
181 */
182static void __ll_do_awake(struct ll_struct *ll)
183{
184 struct sk_buff *skb = NULL;
185
186 while ((skb = skb_dequeue(&ll->tx_wait_q)))
187 skb_queue_tail(&ll->txq, skb);
188
189 ll->hcill_state = HCILL_AWAKE;
190}
191
192/*
193 * Called upon a wake-up-indication from the device
194 */
195static void ll_device_want_to_wakeup(struct hci_uart *hu)
196{
197 unsigned long flags;
198 struct ll_struct *ll = hu->priv;
199
200 BT_DBG("hu %p", hu);
201
202 /* lock hcill state */
203 spin_lock_irqsave(&ll->hcill_lock, flags);
204
205 switch (ll->hcill_state) {
206 case HCILL_ASLEEP_TO_AWAKE:
207 /*
208 * This state means that both the host and the BRF chip
209 * have simultaneously sent a wake-up-indication packet.
210 * Traditionally, in this case, receiving a wake-up-indication
211 * was enough and an additional wake-up-ack wasn't needed.
212 * This has changed with the BRF6350, which does require an
213 * explicit wake-up-ack. Other BRF versions, which do not
214 * require an explicit ack here, do accept it, thus it is
215 * perfectly safe to always send one.
216 */
217 BT_DBG("dual wake-up-indication");
218 /* deliberate fall-through - do not add break */
219 case HCILL_ASLEEP:
220 /* acknowledge device wake up */
221 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
222 BT_ERR("cannot acknowledge device wake up");
223 goto out;
224 }
225 break;
226 default:
227 /* any other state is illegal */
228 BT_ERR("received HCILL_WAKE_UP_IND in state %ld", ll->hcill_state);
229 break;
230 }
231
232 /* send pending packets and change state to HCILL_AWAKE */
233 __ll_do_awake(ll);
234
235out:
236 spin_unlock_irqrestore(&ll->hcill_lock, flags);
237
238 /* actually send the packets */
239 hci_uart_tx_wakeup(hu);
240}
241
242/*
243 * Called upon a sleep-indication from the device
244 */
245static void ll_device_want_to_sleep(struct hci_uart *hu)
246{
247 unsigned long flags;
248 struct ll_struct *ll = hu->priv;
249
250 BT_DBG("hu %p", hu);
251
252 /* lock hcill state */
253 spin_lock_irqsave(&ll->hcill_lock, flags);
254
255 /* sanity check */
256 if (ll->hcill_state != HCILL_AWAKE)
257 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld", ll->hcill_state);
258
259 /* acknowledge device sleep */
260 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
261 BT_ERR("cannot acknowledge device sleep");
262 goto out;
263 }
264
265 /* update state */
266 ll->hcill_state = HCILL_ASLEEP;
267
268out:
269 spin_unlock_irqrestore(&ll->hcill_lock, flags);
270
271 /* actually send the sleep ack packet */
272 hci_uart_tx_wakeup(hu);
273}
274
275/*
276 * Called upon wake-up-acknowledgement from the device
277 */
278static void ll_device_woke_up(struct hci_uart *hu)
279{
280 unsigned long flags;
281 struct ll_struct *ll = hu->priv;
282
283 BT_DBG("hu %p", hu);
284
285 /* lock hcill state */
286 spin_lock_irqsave(&ll->hcill_lock, flags);
287
288 /* sanity check */
289 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
290 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld", ll->hcill_state);
291
292 /* send pending packets and change state to HCILL_AWAKE */
293 __ll_do_awake(ll);
294
295 spin_unlock_irqrestore(&ll->hcill_lock, flags);
296
297 /* actually send the packets */
298 hci_uart_tx_wakeup(hu);
299}
300
301/* Enqueue frame for transmittion (padding, crc, etc) */
302/* may be called from two simultaneous tasklets */
303static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
304{
305 unsigned long flags = 0;
306 struct ll_struct *ll = hu->priv;
307
308 BT_DBG("hu %p skb %p", hu, skb);
309
310 /* Prepend skb with frame type */
311 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
312
313 /* lock hcill state */
314 spin_lock_irqsave(&ll->hcill_lock, flags);
315
316 /* act according to current state */
317 switch (ll->hcill_state) {
318 case HCILL_AWAKE:
319 BT_DBG("device awake, sending normally");
320 skb_queue_tail(&ll->txq, skb);
321 break;
322 case HCILL_ASLEEP:
323 BT_DBG("device asleep, waking up and queueing packet");
324 /* save packet for later */
325 skb_queue_tail(&ll->tx_wait_q, skb);
326 /* awake device */
327 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
328 BT_ERR("cannot wake up device");
329 break;
330 }
331 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
332 break;
333 case HCILL_ASLEEP_TO_AWAKE:
334 BT_DBG("device waking up, queueing packet");
335 /* transient state; just keep packet for later */
336 skb_queue_tail(&ll->tx_wait_q, skb);
337 break;
338 default:
339 BT_ERR("illegal hcill state: %ld (losing packet)", ll->hcill_state);
340 kfree_skb(skb);
341 break;
342 }
343
344 spin_unlock_irqrestore(&ll->hcill_lock, flags);
345
346 return 0;
347}
348
349static inline int ll_check_data_len(struct ll_struct *ll, int len)
350{
351 register int room = skb_tailroom(ll->rx_skb);
352
353 BT_DBG("len %d room %d", len, room);
354
355 if (!len) {
356 hci_recv_frame(ll->rx_skb);
357 } else if (len > room) {
358 BT_ERR("Data length is too large");
359 kfree_skb(ll->rx_skb);
360 } else {
361 ll->rx_state = HCILL_W4_DATA;
362 ll->rx_count = len;
363 return len;
364 }
365
366 ll->rx_state = HCILL_W4_PACKET_TYPE;
367 ll->rx_skb = NULL;
368 ll->rx_count = 0;
369
370 return 0;
371}
372
373/* Recv data */
374static int ll_recv(struct hci_uart *hu, void *data, int count)
375{
376 struct ll_struct *ll = hu->priv;
377 register char *ptr;
378 struct hci_event_hdr *eh;
379 struct hci_acl_hdr *ah;
380 struct hci_sco_hdr *sh;
381 register int len, type, dlen;
382
383 BT_DBG("hu %p count %d rx_state %ld rx_count %ld", hu, count, ll->rx_state, ll->rx_count);
384
385 ptr = data;
386 while (count) {
387 if (ll->rx_count) {
388 len = min_t(unsigned int, ll->rx_count, count);
389 memcpy(skb_put(ll->rx_skb, len), ptr, len);
390 ll->rx_count -= len; count -= len; ptr += len;
391
392 if (ll->rx_count)
393 continue;
394
395 switch (ll->rx_state) {
396 case HCILL_W4_DATA:
397 BT_DBG("Complete data");
398 hci_recv_frame(ll->rx_skb);
399
400 ll->rx_state = HCILL_W4_PACKET_TYPE;
401 ll->rx_skb = NULL;
402 continue;
403
404 case HCILL_W4_EVENT_HDR:
405 eh = hci_event_hdr(ll->rx_skb);
406
407 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
408
409 ll_check_data_len(ll, eh->plen);
410 continue;
411
412 case HCILL_W4_ACL_HDR:
413 ah = hci_acl_hdr(ll->rx_skb);
414 dlen = __le16_to_cpu(ah->dlen);
415
416 BT_DBG("ACL header: dlen %d", dlen);
417
418 ll_check_data_len(ll, dlen);
419 continue;
420
421 case HCILL_W4_SCO_HDR:
422 sh = hci_sco_hdr(ll->rx_skb);
423
424 BT_DBG("SCO header: dlen %d", sh->dlen);
425
426 ll_check_data_len(ll, sh->dlen);
427 continue;
428 }
429 }
430
431 /* HCILL_W4_PACKET_TYPE */
432 switch (*ptr) {
433 case HCI_EVENT_PKT:
434 BT_DBG("Event packet");
435 ll->rx_state = HCILL_W4_EVENT_HDR;
436 ll->rx_count = HCI_EVENT_HDR_SIZE;
437 type = HCI_EVENT_PKT;
438 break;
439
440 case HCI_ACLDATA_PKT:
441 BT_DBG("ACL packet");
442 ll->rx_state = HCILL_W4_ACL_HDR;
443 ll->rx_count = HCI_ACL_HDR_SIZE;
444 type = HCI_ACLDATA_PKT;
445 break;
446
447 case HCI_SCODATA_PKT:
448 BT_DBG("SCO packet");
449 ll->rx_state = HCILL_W4_SCO_HDR;
450 ll->rx_count = HCI_SCO_HDR_SIZE;
451 type = HCI_SCODATA_PKT;
452 break;
453
454 /* HCILL signals */
455 case HCILL_GO_TO_SLEEP_IND:
456 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
457 ll_device_want_to_sleep(hu);
458 ptr++; count--;
459 continue;
460
461 case HCILL_GO_TO_SLEEP_ACK:
462 /* shouldn't happen */
463 BT_ERR("received HCILL_GO_TO_SLEEP_ACK (in state %ld)", ll->hcill_state);
464 ptr++; count--;
465 continue;
466
467 case HCILL_WAKE_UP_IND:
468 BT_DBG("HCILL_WAKE_UP_IND packet");
469 ll_device_want_to_wakeup(hu);
470 ptr++; count--;
471 continue;
472
473 case HCILL_WAKE_UP_ACK:
474 BT_DBG("HCILL_WAKE_UP_ACK packet");
475 ll_device_woke_up(hu);
476 ptr++; count--;
477 continue;
478
479 default:
480 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
481 hu->hdev->stat.err_rx++;
482 ptr++; count--;
483 continue;
484 };
485
486 ptr++; count--;
487
488 /* Allocate packet */
489 ll->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
490 if (!ll->rx_skb) {
491 BT_ERR("Can't allocate mem for new packet");
492 ll->rx_state = HCILL_W4_PACKET_TYPE;
493 ll->rx_count = 0;
494 return -ENOMEM;
495 }
496
497 ll->rx_skb->dev = (void *) hu->hdev;
498 bt_cb(ll->rx_skb)->pkt_type = type;
499 }
500
501 return count;
502}
503
504static struct sk_buff *ll_dequeue(struct hci_uart *hu)
505{
506 struct ll_struct *ll = hu->priv;
507 return skb_dequeue(&ll->txq);
508}
509
510static struct hci_uart_proto llp = {
511 .id = HCI_UART_LL,
512 .open = ll_open,
513 .close = ll_close,
514 .recv = ll_recv,
515 .enqueue = ll_enqueue,
516 .dequeue = ll_dequeue,
517 .flush = ll_flush,
518};
519
520int __init ll_init(void)
521{
522 int err = hci_uart_register_proto(&llp);
523
524 if (!err)
525 BT_INFO("HCILL protocol initialized");
526 else
527 BT_ERR("HCILL protocol registration failed");
528
529 return err;
530}
531
532int __exit ll_deinit(void)
533{
534 return hci_uart_unregister_proto(&llp);
535}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
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.
15 */
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>
24#include <linux/firmware.h>
25#include <linux/interrupt.h>
26#include <linux/ptrace.h>
27#include <linux/poll.h>
28
29#include <linux/slab.h>
30#include <linux/errno.h>
31#include <linux/string.h>
32#include <linux/signal.h>
33#include <linux/ioctl.h>
34#include <linux/of.h>
35#include <linux/serdev.h>
36#include <linux/skbuff.h>
37#include <linux/ti_wilink_st.h>
38#include <linux/clk.h>
39
40#include <net/bluetooth/bluetooth.h>
41#include <net/bluetooth/hci_core.h>
42#include <linux/gpio/consumer.h>
43#include <linux/nvmem-consumer.h>
44
45#include "hci_uart.h"
46
47/* Vendor-specific HCI commands */
48#define HCI_VS_WRITE_BD_ADDR 0xfc06
49#define HCI_VS_UPDATE_UART_HCI_BAUDRATE 0xff36
50
51/* 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
57/* HCILL states */
58enum hcill_states_e {
59 HCILL_ASLEEP,
60 HCILL_ASLEEP_TO_AWAKE,
61 HCILL_AWAKE,
62 HCILL_AWAKE_TO_ASLEEP
63};
64
65struct ll_device {
66 struct hci_uart hu;
67 struct serdev_device *serdev;
68 struct gpio_desc *enable_gpio;
69 struct clk *ext_clk;
70 bdaddr_t bdaddr;
71};
72
73struct ll_struct {
74 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;
90
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 */
102 skb_put_u8(skb, cmd);
103
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
117 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
118 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
129 if (hu->serdev) {
130 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
131
132 if (!IS_ERR(lldev->ext_clk))
133 clk_prepare_enable(lldev->ext_clk);
134 }
135
136 return 0;
137}
138
139/* Flush protocol data */
140static int ll_flush(struct hci_uart *hu)
141{
142 struct ll_struct *ll = hu->priv;
143
144 BT_DBG("hu %p", hu);
145
146 skb_queue_purge(&ll->tx_wait_q);
147 skb_queue_purge(&ll->txq);
148
149 return 0;
150}
151
152/* Close protocol */
153static int ll_close(struct hci_uart *hu)
154{
155 struct ll_struct *ll = hu->priv;
156
157 BT_DBG("hu %p", hu);
158
159 skb_queue_purge(&ll->tx_wait_q);
160 skb_queue_purge(&ll->txq);
161
162 kfree_skb(ll->rx_skb);
163
164 if (hu->serdev) {
165 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
166
167 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
168
169 clk_disable_unprepare(lldev->ext_clk);
170 }
171
172 hu->priv = NULL;
173
174 kfree(ll);
175
176 return 0;
177}
178
179/*
180 * internal function, which does common work of the device wake up process:
181 * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
182 * 2. changes internal state to HCILL_AWAKE.
183 * Note: assumes that hcill_lock spinlock is taken,
184 * shouldn't be called otherwise!
185 */
186static void __ll_do_awake(struct ll_struct *ll)
187{
188 struct sk_buff *skb = NULL;
189
190 while ((skb = skb_dequeue(&ll->tx_wait_q)))
191 skb_queue_tail(&ll->txq, skb);
192
193 ll->hcill_state = HCILL_AWAKE;
194}
195
196/*
197 * Called upon a wake-up-indication from the device
198 */
199static void ll_device_want_to_wakeup(struct hci_uart *hu)
200{
201 unsigned long flags;
202 struct ll_struct *ll = hu->priv;
203
204 BT_DBG("hu %p", hu);
205
206 /* lock hcill state */
207 spin_lock_irqsave(&ll->hcill_lock, flags);
208
209 switch (ll->hcill_state) {
210 case HCILL_ASLEEP_TO_AWAKE:
211 /*
212 * This state means that both the host and the BRF chip
213 * have simultaneously sent a wake-up-indication packet.
214 * Traditionally, in this case, receiving a wake-up-indication
215 * was enough and an additional wake-up-ack wasn't needed.
216 * This has changed with the BRF6350, which does require an
217 * explicit wake-up-ack. Other BRF versions, which do not
218 * require an explicit ack here, do accept it, thus it is
219 * perfectly safe to always send one.
220 */
221 BT_DBG("dual wake-up-indication");
222 fallthrough;
223 case HCILL_ASLEEP:
224 /* acknowledge device wake up */
225 if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
226 BT_ERR("cannot acknowledge device wake up");
227 goto out;
228 }
229 break;
230 default:
231 /* any other state is illegal */
232 BT_ERR("received HCILL_WAKE_UP_IND in state %ld",
233 ll->hcill_state);
234 break;
235 }
236
237 /* send pending packets and change state to HCILL_AWAKE */
238 __ll_do_awake(ll);
239
240out:
241 spin_unlock_irqrestore(&ll->hcill_lock, flags);
242
243 /* actually send the packets */
244 hci_uart_tx_wakeup(hu);
245}
246
247/*
248 * Called upon a sleep-indication from the device
249 */
250static void ll_device_want_to_sleep(struct hci_uart *hu)
251{
252 unsigned long flags;
253 struct ll_struct *ll = hu->priv;
254
255 BT_DBG("hu %p", hu);
256
257 /* lock hcill state */
258 spin_lock_irqsave(&ll->hcill_lock, flags);
259
260 /* sanity check */
261 if (ll->hcill_state != HCILL_AWAKE)
262 BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld",
263 ll->hcill_state);
264
265 /* acknowledge device sleep */
266 if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
267 BT_ERR("cannot acknowledge device sleep");
268 goto out;
269 }
270
271 /* update state */
272 ll->hcill_state = HCILL_ASLEEP;
273
274out:
275 spin_unlock_irqrestore(&ll->hcill_lock, flags);
276
277 /* actually send the sleep ack packet */
278 hci_uart_tx_wakeup(hu);
279}
280
281/*
282 * Called upon wake-up-acknowledgement from the device
283 */
284static void ll_device_woke_up(struct hci_uart *hu)
285{
286 unsigned long flags;
287 struct ll_struct *ll = hu->priv;
288
289 BT_DBG("hu %p", hu);
290
291 /* lock hcill state */
292 spin_lock_irqsave(&ll->hcill_lock, flags);
293
294 /* sanity check */
295 if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
296 BT_ERR("received HCILL_WAKE_UP_ACK in state %ld",
297 ll->hcill_state);
298
299 /* send pending packets and change state to HCILL_AWAKE */
300 __ll_do_awake(ll);
301
302 spin_unlock_irqrestore(&ll->hcill_lock, flags);
303
304 /* actually send the packets */
305 hci_uart_tx_wakeup(hu);
306}
307
308/* Enqueue frame for transmission (padding, crc, etc) */
309/* may be called from two simultaneous tasklets */
310static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
311{
312 unsigned long flags = 0;
313 struct ll_struct *ll = hu->priv;
314
315 BT_DBG("hu %p skb %p", hu, skb);
316
317 /* Prepend skb with frame type */
318 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
319
320 /* lock hcill state */
321 spin_lock_irqsave(&ll->hcill_lock, flags);
322
323 /* act according to current state */
324 switch (ll->hcill_state) {
325 case HCILL_AWAKE:
326 BT_DBG("device awake, sending normally");
327 skb_queue_tail(&ll->txq, skb);
328 break;
329 case HCILL_ASLEEP:
330 BT_DBG("device asleep, waking up and queueing packet");
331 /* save packet for later */
332 skb_queue_tail(&ll->tx_wait_q, skb);
333 /* awake device */
334 if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
335 BT_ERR("cannot wake up device");
336 break;
337 }
338 ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
339 break;
340 case HCILL_ASLEEP_TO_AWAKE:
341 BT_DBG("device waking up, queueing packet");
342 /* transient state; just keep packet for later */
343 skb_queue_tail(&ll->tx_wait_q, skb);
344 break;
345 default:
346 BT_ERR("illegal hcill state: %ld (losing packet)",
347 ll->hcill_state);
348 dev_kfree_skb_irq(skb);
349 break;
350 }
351
352 spin_unlock_irqrestore(&ll->hcill_lock, flags);
353
354 return 0;
355}
356
357static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
358{
359 struct hci_uart *hu = hci_get_drvdata(hdev);
360 struct ll_struct *ll = hu->priv;
361
362 switch (hci_skb_pkt_type(skb)) {
363 case HCILL_GO_TO_SLEEP_IND:
364 BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
365 ll_device_want_to_sleep(hu);
366 break;
367 case HCILL_GO_TO_SLEEP_ACK:
368 /* shouldn't happen */
369 bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
370 ll->hcill_state);
371 break;
372 case HCILL_WAKE_UP_IND:
373 BT_DBG("HCILL_WAKE_UP_IND packet");
374 ll_device_want_to_wakeup(hu);
375 break;
376 case HCILL_WAKE_UP_ACK:
377 BT_DBG("HCILL_WAKE_UP_ACK packet");
378 ll_device_woke_up(hu);
379 break;
380 }
381
382 kfree_skb(skb);
383 return 0;
384}
385
386#define LL_RECV_SLEEP_IND \
387 .type = HCILL_GO_TO_SLEEP_IND, \
388 .hlen = 0, \
389 .loff = 0, \
390 .lsize = 0, \
391 .maxlen = 0
392
393#define LL_RECV_SLEEP_ACK \
394 .type = HCILL_GO_TO_SLEEP_ACK, \
395 .hlen = 0, \
396 .loff = 0, \
397 .lsize = 0, \
398 .maxlen = 0
399
400#define LL_RECV_WAKE_IND \
401 .type = HCILL_WAKE_UP_IND, \
402 .hlen = 0, \
403 .loff = 0, \
404 .lsize = 0, \
405 .maxlen = 0
406
407#define LL_RECV_WAKE_ACK \
408 .type = HCILL_WAKE_UP_ACK, \
409 .hlen = 0, \
410 .loff = 0, \
411 .lsize = 0, \
412 .maxlen = 0
413
414static const struct h4_recv_pkt ll_recv_pkts[] = {
415 { H4_RECV_ACL, .recv = hci_recv_frame },
416 { H4_RECV_SCO, .recv = hci_recv_frame },
417 { H4_RECV_EVENT, .recv = hci_recv_frame },
418 { LL_RECV_SLEEP_IND, .recv = ll_recv_frame },
419 { LL_RECV_SLEEP_ACK, .recv = ll_recv_frame },
420 { LL_RECV_WAKE_IND, .recv = ll_recv_frame },
421 { LL_RECV_WAKE_ACK, .recv = ll_recv_frame },
422};
423
424/* Recv data */
425static int ll_recv(struct hci_uart *hu, const void *data, int count)
426{
427 struct ll_struct *ll = hu->priv;
428
429 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
430 return -EUNATCH;
431
432 ll->rx_skb = h4_recv_buf(hu->hdev, ll->rx_skb, data, count,
433 ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
434 if (IS_ERR(ll->rx_skb)) {
435 int err = PTR_ERR(ll->rx_skb);
436 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
437 ll->rx_skb = NULL;
438 return err;
439 }
440
441 return count;
442}
443
444static struct sk_buff *ll_dequeue(struct hci_uart *hu)
445{
446 struct ll_struct *ll = hu->priv;
447
448 return skb_dequeue(&ll->txq);
449}
450
451#if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
452static int read_local_version(struct hci_dev *hdev)
453{
454 int err = 0;
455 unsigned short version = 0;
456 struct sk_buff *skb;
457 struct hci_rp_read_local_version *ver;
458
459 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
460 HCI_INIT_TIMEOUT);
461 if (IS_ERR(skb)) {
462 bt_dev_err(hdev, "Reading TI version information failed (%ld)",
463 PTR_ERR(skb));
464 return PTR_ERR(skb);
465 }
466 if (skb->len != sizeof(*ver)) {
467 err = -EILSEQ;
468 goto out;
469 }
470
471 ver = (struct hci_rp_read_local_version *)skb->data;
472 if (le16_to_cpu(ver->manufacturer) != 13) {
473 err = -ENODEV;
474 goto out;
475 }
476
477 version = le16_to_cpu(ver->lmp_subver);
478
479out:
480 if (err)
481 bt_dev_err(hdev, "Failed to read TI version info: %d", err);
482 kfree_skb(skb);
483 return err ? err : version;
484}
485
486static int send_command_from_firmware(struct ll_device *lldev,
487 struct hci_command *cmd)
488{
489 struct sk_buff *skb;
490
491 if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
492 /* ignore remote change
493 * baud rate HCI VS command
494 */
495 bt_dev_warn(lldev->hu.hdev,
496 "change remote baud rate command in firmware");
497 return 0;
498 }
499 if (cmd->prefix != 1)
500 bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
501
502 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen,
503 &cmd->speed, HCI_INIT_TIMEOUT);
504 if (IS_ERR(skb)) {
505 bt_dev_err(lldev->hu.hdev, "send command failed");
506 return PTR_ERR(skb);
507 }
508 kfree_skb(skb);
509 return 0;
510}
511
512/*
513 * download_firmware -
514 * internal function which parses through the .bts firmware
515 * script file intreprets SEND, DELAY actions only as of now
516 */
517static int download_firmware(struct ll_device *lldev)
518{
519 unsigned short chip, min_ver, maj_ver;
520 int version, err, len;
521 unsigned char *ptr, *action_ptr;
522 unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
523 const struct firmware *fw;
524 struct hci_command *cmd;
525
526 version = read_local_version(lldev->hu.hdev);
527 if (version < 0)
528 return version;
529
530 chip = (version & 0x7C00) >> 10;
531 min_ver = (version & 0x007F);
532 maj_ver = (version & 0x0380) >> 7;
533 if (version & 0x8000)
534 maj_ver |= 0x0008;
535
536 snprintf(bts_scr_name, sizeof(bts_scr_name),
537 "ti-connectivity/TIInit_%d.%d.%d.bts",
538 chip, maj_ver, min_ver);
539
540 err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
541 if (err || !fw->data || !fw->size) {
542 bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
543 err, bts_scr_name);
544 return -EINVAL;
545 }
546 ptr = (void *)fw->data;
547 len = fw->size;
548 /* bts_header to remove out magic number and
549 * version
550 */
551 ptr += sizeof(struct bts_header);
552 len -= sizeof(struct bts_header);
553
554 while (len > 0 && ptr) {
555 bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
556 ((struct bts_action *)ptr)->size,
557 ((struct bts_action *)ptr)->type);
558
559 action_ptr = &(((struct bts_action *)ptr)->data[0]);
560
561 switch (((struct bts_action *)ptr)->type) {
562 case ACTION_SEND_COMMAND: /* action send */
563 bt_dev_dbg(lldev->hu.hdev, "S");
564 cmd = (struct hci_command *)action_ptr;
565 err = send_command_from_firmware(lldev, cmd);
566 if (err)
567 goto out_rel_fw;
568 break;
569 case ACTION_WAIT_EVENT: /* wait */
570 /* no need to wait as command was synchronous */
571 bt_dev_dbg(lldev->hu.hdev, "W");
572 break;
573 case ACTION_DELAY: /* sleep */
574 bt_dev_info(lldev->hu.hdev, "sleep command in scr");
575 msleep(((struct bts_action_delay *)action_ptr)->msec);
576 break;
577 }
578 len -= (sizeof(struct bts_action) +
579 ((struct bts_action *)ptr)->size);
580 ptr += sizeof(struct bts_action) +
581 ((struct bts_action *)ptr)->size;
582 }
583
584out_rel_fw:
585 /* fw download complete */
586 release_firmware(fw);
587 return err;
588}
589
590static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
591{
592 bdaddr_t bdaddr_swapped;
593 struct sk_buff *skb;
594
595 /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
596 * address to be MSB first, but bdaddr_t has the convention of being
597 * LSB first.
598 */
599 baswap(&bdaddr_swapped, bdaddr);
600 skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
601 &bdaddr_swapped, HCI_INIT_TIMEOUT);
602 if (!IS_ERR(skb))
603 kfree_skb(skb);
604
605 return PTR_ERR_OR_ZERO(skb);
606}
607
608static int ll_setup(struct hci_uart *hu)
609{
610 int err, retry = 3;
611 struct ll_device *lldev;
612 struct serdev_device *serdev = hu->serdev;
613 u32 speed;
614
615 if (!serdev)
616 return 0;
617
618 lldev = serdev_device_get_drvdata(serdev);
619
620 hu->hdev->set_bdaddr = ll_set_bdaddr;
621
622 serdev_device_set_flow_control(serdev, true);
623
624 do {
625 /* Reset the Bluetooth device */
626 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
627 msleep(5);
628 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
629 mdelay(100);
630 err = serdev_device_wait_for_cts(serdev, true, 200);
631 if (err) {
632 bt_dev_err(hu->hdev, "Failed to get CTS");
633 return err;
634 }
635
636 err = download_firmware(lldev);
637 if (!err)
638 break;
639
640 /* Toggle BT_EN and retry */
641 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
642 } while (retry--);
643
644 if (err)
645 return err;
646
647 /* Set BD address if one was specified at probe */
648 if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
649 /* This means that there was an error getting the BD address
650 * during probe, so mark the device as having a bad address.
651 */
652 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
653 } else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
654 err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
655 if (err)
656 set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
657 }
658
659 /* Operational speed if any */
660 if (hu->oper_speed)
661 speed = hu->oper_speed;
662 else if (hu->proto->oper_speed)
663 speed = hu->proto->oper_speed;
664 else
665 speed = 0;
666
667 if (speed) {
668 __le32 speed_le = cpu_to_le32(speed);
669 struct sk_buff *skb;
670
671 skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
672 sizeof(speed_le), &speed_le,
673 HCI_INIT_TIMEOUT);
674 if (!IS_ERR(skb)) {
675 kfree_skb(skb);
676 serdev_device_set_baudrate(serdev, speed);
677 }
678 }
679
680 return 0;
681}
682
683static const struct hci_uart_proto llp;
684
685static int hci_ti_probe(struct serdev_device *serdev)
686{
687 struct hci_uart *hu;
688 struct ll_device *lldev;
689 struct nvmem_cell *bdaddr_cell;
690 u32 max_speed = 3000000;
691
692 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
693 if (!lldev)
694 return -ENOMEM;
695 hu = &lldev->hu;
696
697 serdev_device_set_drvdata(serdev, lldev);
698 lldev->serdev = hu->serdev = serdev;
699
700 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev,
701 "enable",
702 GPIOD_OUT_LOW);
703 if (IS_ERR(lldev->enable_gpio))
704 return PTR_ERR(lldev->enable_gpio);
705
706 lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
707 if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
708 return PTR_ERR(lldev->ext_clk);
709
710 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
711 hci_uart_set_speeds(hu, 115200, max_speed);
712
713 /* optional BD address from nvram */
714 bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
715 if (IS_ERR(bdaddr_cell)) {
716 int err = PTR_ERR(bdaddr_cell);
717
718 if (err == -EPROBE_DEFER)
719 return err;
720
721 /* ENOENT means there is no matching nvmem cell and ENOSYS
722 * means that nvmem is not enabled in the kernel configuration.
723 */
724 if (err != -ENOENT && err != -ENOSYS) {
725 /* If there was some other error, give userspace a
726 * chance to fix the problem instead of failing to load
727 * the driver. Using BDADDR_NONE as a flag that is
728 * tested later in the setup function.
729 */
730 dev_warn(&serdev->dev,
731 "Failed to get \"bd-address\" nvmem cell (%d)\n",
732 err);
733 bacpy(&lldev->bdaddr, BDADDR_NONE);
734 }
735 } else {
736 bdaddr_t *bdaddr;
737 size_t len;
738
739 bdaddr = nvmem_cell_read(bdaddr_cell, &len);
740 nvmem_cell_put(bdaddr_cell);
741 if (IS_ERR(bdaddr)) {
742 dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
743 return PTR_ERR(bdaddr);
744 }
745 if (len != sizeof(bdaddr_t)) {
746 dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
747 kfree(bdaddr);
748 return -EINVAL;
749 }
750
751 /* As per the device tree bindings, the value from nvmem is
752 * expected to be MSB first, but in the kernel it is expected
753 * that bdaddr_t is LSB first.
754 */
755 baswap(&lldev->bdaddr, bdaddr);
756 kfree(bdaddr);
757 }
758
759 return hci_uart_register_device(hu, &llp);
760}
761
762static void hci_ti_remove(struct serdev_device *serdev)
763{
764 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
765
766 hci_uart_unregister_device(&lldev->hu);
767}
768
769static const struct of_device_id hci_ti_of_match[] = {
770 { .compatible = "ti,cc2560" },
771 { .compatible = "ti,wl1271-st" },
772 { .compatible = "ti,wl1273-st" },
773 { .compatible = "ti,wl1281-st" },
774 { .compatible = "ti,wl1283-st" },
775 { .compatible = "ti,wl1285-st" },
776 { .compatible = "ti,wl1801-st" },
777 { .compatible = "ti,wl1805-st" },
778 { .compatible = "ti,wl1807-st" },
779 { .compatible = "ti,wl1831-st" },
780 { .compatible = "ti,wl1835-st" },
781 { .compatible = "ti,wl1837-st" },
782 {},
783};
784MODULE_DEVICE_TABLE(of, hci_ti_of_match);
785
786static struct serdev_device_driver hci_ti_drv = {
787 .driver = {
788 .name = "hci-ti",
789 .of_match_table = hci_ti_of_match,
790 },
791 .probe = hci_ti_probe,
792 .remove = hci_ti_remove,
793};
794#else
795#define ll_setup NULL
796#endif
797
798static const struct hci_uart_proto llp = {
799 .id = HCI_UART_LL,
800 .name = "LL",
801 .setup = ll_setup,
802 .open = ll_open,
803 .close = ll_close,
804 .recv = ll_recv,
805 .enqueue = ll_enqueue,
806 .dequeue = ll_dequeue,
807 .flush = ll_flush,
808};
809
810int __init ll_init(void)
811{
812 serdev_device_driver_register(&hci_ti_drv);
813
814 return hci_uart_register_proto(&llp);
815}
816
817int __exit ll_deinit(void)
818{
819 serdev_device_driver_unregister(&hci_ti_drv);
820
821 return hci_uart_unregister_proto(&llp);
822}