]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/bluetooth/hci_ll.c
pinctrl: sh-pfc: sh7734: Fix duplicate TCLK1_B
[mirror_ubuntu-bionic-kernel.git] / drivers / bluetooth / hci_ll.c
CommitLineData
166d2f6a
OBC
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>
37180552 37#include <linux/firmware.h>
166d2f6a
OBC
38#include <linux/interrupt.h>
39#include <linux/ptrace.h>
40#include <linux/poll.h>
41
42#include <linux/slab.h>
166d2f6a
OBC
43#include <linux/errno.h>
44#include <linux/string.h>
45#include <linux/signal.h>
46#include <linux/ioctl.h>
37180552
RH
47#include <linux/of.h>
48#include <linux/serdev.h>
166d2f6a 49#include <linux/skbuff.h>
37180552 50#include <linux/ti_wilink_st.h>
43d3d092 51#include <linux/clk.h>
166d2f6a
OBC
52
53#include <net/bluetooth/bluetooth.h>
54#include <net/bluetooth/hci_core.h>
37180552 55#include <linux/gpio/consumer.h>
166d2f6a
OBC
56
57#include "hci_uart.h"
58
59/* HCILL commands */
60#define HCILL_GO_TO_SLEEP_IND 0x30
61#define HCILL_GO_TO_SLEEP_ACK 0x31
62#define HCILL_WAKE_UP_IND 0x32
63#define HCILL_WAKE_UP_ACK 0x33
64
65/* HCILL receiver States */
66#define HCILL_W4_PACKET_TYPE 0
67#define HCILL_W4_EVENT_HDR 1
68#define HCILL_W4_ACL_HDR 2
69#define HCILL_W4_SCO_HDR 3
70#define HCILL_W4_DATA 4
71
72/* HCILL states */
73enum hcill_states_e {
74 HCILL_ASLEEP,
75 HCILL_ASLEEP_TO_AWAKE,
76 HCILL_AWAKE,
77 HCILL_AWAKE_TO_ASLEEP
78};
79
80struct hcill_cmd {
81 u8 cmd;
81ca405a 82} __packed;
166d2f6a 83
37180552
RH
84struct ll_device {
85 struct hci_uart hu;
86 struct serdev_device *serdev;
87 struct gpio_desc *enable_gpio;
43d3d092 88 struct clk *ext_clk;
37180552
RH
89};
90
166d2f6a
OBC
91struct ll_struct {
92 unsigned long rx_state;
93 unsigned long rx_count;
94 struct sk_buff *rx_skb;
95 struct sk_buff_head txq;
96 spinlock_t hcill_lock; /* HCILL state lock */
97 unsigned long hcill_state; /* HCILL power state */
98 struct sk_buff_head tx_wait_q; /* HCILL wait queue */
99};
100
101/*
102 * Builds and sends an HCILL command packet.
103 * These are very simple packets with only 1 cmd byte
104 */
105static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
106{
107 int err = 0;
108 struct sk_buff *skb = NULL;
109 struct ll_struct *ll = hu->priv;
110 struct hcill_cmd *hcill_packet;
111
112 BT_DBG("hu %p cmd 0x%x", hu, cmd);
113
114 /* allocate packet */
115 skb = bt_skb_alloc(1, GFP_ATOMIC);
116 if (!skb) {
117 BT_ERR("cannot allocate memory for HCILL packet");
118 err = -ENOMEM;
119 goto out;
120 }
121
122 /* prepare packet */
4df864c1 123 hcill_packet = skb_put(skb, 1);
166d2f6a 124 hcill_packet->cmd = cmd;
166d2f6a
OBC
125
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
9eb648c3 139 ll = kzalloc(sizeof(*ll), GFP_KERNEL);
166d2f6a
OBC
140 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
43d3d092
UH
151 if (hu->serdev) {
152 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
37180552 153 serdev_device_open(hu->serdev);
43d3d092
UH
154 if (!IS_ERR(lldev->ext_clk))
155 clk_prepare_enable(lldev->ext_clk);
156 }
37180552 157
166d2f6a
OBC
158 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
b1fb0683 184 kfree_skb(ll->rx_skb);
166d2f6a 185
37180552
RH
186 if (hu->serdev) {
187 struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
188 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
189
43d3d092
UH
190 clk_disable_unprepare(lldev->ext_clk);
191
37180552
RH
192 serdev_device_close(hu->serdev);
193 }
194
166d2f6a
OBC
195 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) {
5c548226
OBC
233 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.
25985edc 237 * Traditionally, in this case, receiving a wake-up-indication
5c548226
OBC
238 * 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");
fac72b24 245 /* fall through */
166d2f6a
OBC
246 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;
166d2f6a 253 default:
5c548226 254 /* any other state is illegal */
166d2f6a
OBC
255 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 */
618e8bc2 338 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
166d2f6a
OBC
339
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
e1a26170 376static inline int ll_check_data_len(struct hci_dev *hdev, struct ll_struct *ll, int len)
166d2f6a 377{
fc5fef61 378 int room = skb_tailroom(ll->rx_skb);
166d2f6a
OBC
379
380 BT_DBG("len %d room %d", len, room);
381
382 if (!len) {
e1a26170 383 hci_recv_frame(hdev, ll->rx_skb);
166d2f6a
OBC
384 } 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 */
9d1c40eb 401static int ll_recv(struct hci_uart *hu, const void *data, int count)
166d2f6a
OBC
402{
403 struct ll_struct *ll = hu->priv;
9d1c40eb 404 const char *ptr;
166d2f6a
OBC
405 struct hci_event_hdr *eh;
406 struct hci_acl_hdr *ah;
407 struct hci_sco_hdr *sh;
fc5fef61 408 int len, type, dlen;
166d2f6a
OBC
409
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);
59ae1d12 416 skb_put_data(ll->rx_skb, ptr, len);
166d2f6a
OBC
417 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");
e1a26170 425 hci_recv_frame(hu->hdev, ll->rx_skb);
166d2f6a
OBC
426
427 ll->rx_state = HCILL_W4_PACKET_TYPE;
428 ll->rx_skb = NULL;
429 continue;
430
431 case HCILL_W4_EVENT_HDR:
acce90d6 432 eh = hci_event_hdr(ll->rx_skb);
166d2f6a
OBC
433
434 BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
435
e1a26170 436 ll_check_data_len(hu->hdev, ll, eh->plen);
166d2f6a
OBC
437 continue;
438
439 case HCILL_W4_ACL_HDR:
acce90d6 440 ah = hci_acl_hdr(ll->rx_skb);
166d2f6a
OBC
441 dlen = __le16_to_cpu(ah->dlen);
442
443 BT_DBG("ACL header: dlen %d", dlen);
444
e1a26170 445 ll_check_data_len(hu->hdev, ll, dlen);
166d2f6a
OBC
446 continue;
447
448 case HCILL_W4_SCO_HDR:
acce90d6 449 sh = hci_sco_hdr(ll->rx_skb);
166d2f6a
OBC
450
451 BT_DBG("SCO header: dlen %d", sh->dlen);
452
e1a26170 453 ll_check_data_len(hu->hdev, ll, sh->dlen);
166d2f6a
OBC
454 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;
d650ccae 511 }
166d2f6a
OBC
512
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;
fe1aff71 521 return -ENOMEM;
166d2f6a
OBC
522 }
523
618e8bc2 524 hci_skb_pkt_type(ll->rx_skb) = type;
166d2f6a
OBC
525 }
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
37180552
RH
536#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));
f2edd9f6 548 return PTR_ERR(skb);
37180552
RH
549 }
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;
623 if (cmd->opcode == 0xff36) {
624 /* ignore remote change
d98422cb
DR
625 * baud rate HCI VS command
626 */
37180552
RH
627 bt_dev_warn(lldev->hu.hdev, "change remote baud rate command in firmware");
628 break;
629 }
630 if (cmd->prefix != 1)
631 bt_dev_dbg(lldev->hu.hdev, "command type %d\n", cmd->prefix);
632
633 skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen, &cmd->speed, HCI_INIT_TIMEOUT);
634 if (IS_ERR(skb)) {
635 bt_dev_err(lldev->hu.hdev, "send command failed\n");
823b8420 636 err = PTR_ERR(skb);
37180552
RH
637 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");
647 mdelay(((struct bts_action_delay *)action_ptr)->msec);
648 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
662static int ll_setup(struct hci_uart *hu)
663{
664 int err, retry = 3;
665 struct ll_device *lldev;
666 struct serdev_device *serdev = hu->serdev;
667 u32 speed;
668
669 if (!serdev)
670 return 0;
671
672 lldev = serdev_device_get_drvdata(serdev);
673
674 serdev_device_set_flow_control(serdev, true);
675
676 do {
677 /* Configure BT_EN to HIGH state */
678 gpiod_set_value_cansleep(lldev->enable_gpio, 0);
679 msleep(5);
680 gpiod_set_value_cansleep(lldev->enable_gpio, 1);
681 msleep(100);
682
683 err = download_firmware(lldev);
684 if (!err)
685 break;
686
687 /* Toggle BT_EN and retry */
688 bt_dev_err(hu->hdev, "download firmware failed, retrying...");
689 } while (retry--);
690
691 if (err)
692 return err;
693
694 /* Operational speed if any */
695 if (hu->oper_speed)
696 speed = hu->oper_speed;
697 else if (hu->proto->oper_speed)
698 speed = hu->proto->oper_speed;
699 else
700 speed = 0;
701
702 if (speed) {
703 struct sk_buff *skb = __hci_cmd_sync(hu->hdev, 0xff36, sizeof(speed), &speed, HCI_INIT_TIMEOUT);
704 if (!IS_ERR(skb)) {
705 kfree_skb(skb);
706 serdev_device_set_baudrate(serdev, speed);
707 }
708 }
709
710 return 0;
711}
712
713static const struct hci_uart_proto llp;
714
715static int hci_ti_probe(struct serdev_device *serdev)
716{
717 struct hci_uart *hu;
718 struct ll_device *lldev;
719 u32 max_speed = 3000000;
720
721 lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
722 if (!lldev)
723 return -ENOMEM;
724 hu = &lldev->hu;
725
726 serdev_device_set_drvdata(serdev, lldev);
727 lldev->serdev = hu->serdev = serdev;
728
729 lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev, "enable", GPIOD_OUT_LOW);
730 if (IS_ERR(lldev->enable_gpio))
731 return PTR_ERR(lldev->enable_gpio);
732
43d3d092
UH
733 lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
734 if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
735 return PTR_ERR(lldev->ext_clk);
736
37180552
RH
737 of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
738 hci_uart_set_speeds(hu, 115200, max_speed);
739
740 return hci_uart_register_device(hu, &llp);
741}
742
743static void hci_ti_remove(struct serdev_device *serdev)
744{
745 struct ll_device *lldev = serdev_device_get_drvdata(serdev);
37180552 746
37f5258d 747 hci_uart_unregister_device(&lldev->hu);
37180552
RH
748}
749
750static const struct of_device_id hci_ti_of_match[] = {
c127a871
SR
751 { .compatible = "ti,wl1271-st" },
752 { .compatible = "ti,wl1273-st" },
753 { .compatible = "ti,wl1281-st" },
754 { .compatible = "ti,wl1283-st" },
755 { .compatible = "ti,wl1285-st" },
756 { .compatible = "ti,wl1801-st" },
757 { .compatible = "ti,wl1805-st" },
758 { .compatible = "ti,wl1807-st" },
37180552
RH
759 { .compatible = "ti,wl1831-st" },
760 { .compatible = "ti,wl1835-st" },
761 { .compatible = "ti,wl1837-st" },
762 {},
763};
764MODULE_DEVICE_TABLE(of, hci_ti_of_match);
765
766static struct serdev_device_driver hci_ti_drv = {
767 .driver = {
768 .name = "hci-ti",
769 .of_match_table = of_match_ptr(hci_ti_of_match),
770 },
771 .probe = hci_ti_probe,
772 .remove = hci_ti_remove,
773};
774#else
775#define ll_setup NULL
776#endif
777
4ee7ef19 778static const struct hci_uart_proto llp = {
166d2f6a 779 .id = HCI_UART_LL,
7c40fb8d 780 .name = "LL",
37180552 781 .setup = ll_setup,
166d2f6a
OBC
782 .open = ll_open,
783 .close = ll_close,
784 .recv = ll_recv,
785 .enqueue = ll_enqueue,
786 .dequeue = ll_dequeue,
787 .flush = ll_flush,
788};
789
f2b94bb9 790int __init ll_init(void)
166d2f6a 791{
37180552
RH
792 serdev_device_driver_register(&hci_ti_drv);
793
01009eec 794 return hci_uart_register_proto(&llp);
166d2f6a
OBC
795}
796
f2b94bb9 797int __exit ll_deinit(void)
166d2f6a 798{
37180552
RH
799 serdev_device_driver_unregister(&hci_ti_drv);
800
166d2f6a
OBC
801 return hci_uart_unregister_proto(&llp);
802}