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