Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Texas Instruments' Bluetooth HCILL UART protocol
0004  *
0005  *  HCILL (HCI Low Level) is a Texas Instruments' power management
0006  *  protocol extension to H4.
0007  *
0008  *  Copyright (C) 2007 Texas Instruments, Inc.
0009  *
0010  *  Written by Ohad Ben-Cohen <ohad@bencohen.org>
0011  *
0012  *  Acknowledgements:
0013  *  This file is based on hci_h4.c, which was written
0014  *  by Maxim Krasnyansky and Marcel Holtmann.
0015  */
0016 
0017 #include <linux/module.h>
0018 #include <linux/kernel.h>
0019 
0020 #include <linux/init.h>
0021 #include <linux/sched.h>
0022 #include <linux/types.h>
0023 #include <linux/fcntl.h>
0024 #include <linux/firmware.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/ptrace.h>
0027 #include <linux/poll.h>
0028 
0029 #include <linux/slab.h>
0030 #include <linux/errno.h>
0031 #include <linux/string.h>
0032 #include <linux/signal.h>
0033 #include <linux/ioctl.h>
0034 #include <linux/of.h>
0035 #include <linux/serdev.h>
0036 #include <linux/skbuff.h>
0037 #include <linux/ti_wilink_st.h>
0038 #include <linux/clk.h>
0039 
0040 #include <net/bluetooth/bluetooth.h>
0041 #include <net/bluetooth/hci_core.h>
0042 #include <linux/gpio/consumer.h>
0043 #include <linux/nvmem-consumer.h>
0044 
0045 #include "hci_uart.h"
0046 
0047 /* Vendor-specific HCI commands */
0048 #define HCI_VS_WRITE_BD_ADDR            0xfc06
0049 #define HCI_VS_UPDATE_UART_HCI_BAUDRATE     0xff36
0050 
0051 /* HCILL commands */
0052 #define HCILL_GO_TO_SLEEP_IND   0x30
0053 #define HCILL_GO_TO_SLEEP_ACK   0x31
0054 #define HCILL_WAKE_UP_IND   0x32
0055 #define HCILL_WAKE_UP_ACK   0x33
0056 
0057 /* HCILL states */
0058 enum hcill_states_e {
0059     HCILL_ASLEEP,
0060     HCILL_ASLEEP_TO_AWAKE,
0061     HCILL_AWAKE,
0062     HCILL_AWAKE_TO_ASLEEP
0063 };
0064 
0065 struct ll_device {
0066     struct hci_uart hu;
0067     struct serdev_device *serdev;
0068     struct gpio_desc *enable_gpio;
0069     struct clk *ext_clk;
0070     bdaddr_t bdaddr;
0071 };
0072 
0073 struct ll_struct {
0074     struct sk_buff *rx_skb;
0075     struct sk_buff_head txq;
0076     spinlock_t hcill_lock;      /* HCILL state lock */
0077     unsigned long hcill_state;  /* HCILL power state    */
0078     struct sk_buff_head tx_wait_q;  /* HCILL wait queue */
0079 };
0080 
0081 /*
0082  * Builds and sends an HCILL command packet.
0083  * These are very simple packets with only 1 cmd byte
0084  */
0085 static int send_hcill_cmd(u8 cmd, struct hci_uart *hu)
0086 {
0087     int err = 0;
0088     struct sk_buff *skb = NULL;
0089     struct ll_struct *ll = hu->priv;
0090 
0091     BT_DBG("hu %p cmd 0x%x", hu, cmd);
0092 
0093     /* allocate packet */
0094     skb = bt_skb_alloc(1, GFP_ATOMIC);
0095     if (!skb) {
0096         BT_ERR("cannot allocate memory for HCILL packet");
0097         err = -ENOMEM;
0098         goto out;
0099     }
0100 
0101     /* prepare packet */
0102     skb_put_u8(skb, cmd);
0103 
0104     /* send packet */
0105     skb_queue_tail(&ll->txq, skb);
0106 out:
0107     return err;
0108 }
0109 
0110 /* Initialize protocol */
0111 static int ll_open(struct hci_uart *hu)
0112 {
0113     struct ll_struct *ll;
0114 
0115     BT_DBG("hu %p", hu);
0116 
0117     ll = kzalloc(sizeof(*ll), GFP_KERNEL);
0118     if (!ll)
0119         return -ENOMEM;
0120 
0121     skb_queue_head_init(&ll->txq);
0122     skb_queue_head_init(&ll->tx_wait_q);
0123     spin_lock_init(&ll->hcill_lock);
0124 
0125     ll->hcill_state = HCILL_AWAKE;
0126 
0127     hu->priv = ll;
0128 
0129     if (hu->serdev) {
0130         struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
0131 
0132         if (!IS_ERR(lldev->ext_clk))
0133             clk_prepare_enable(lldev->ext_clk);
0134     }
0135 
0136     return 0;
0137 }
0138 
0139 /* Flush protocol data */
0140 static int ll_flush(struct hci_uart *hu)
0141 {
0142     struct ll_struct *ll = hu->priv;
0143 
0144     BT_DBG("hu %p", hu);
0145 
0146     skb_queue_purge(&ll->tx_wait_q);
0147     skb_queue_purge(&ll->txq);
0148 
0149     return 0;
0150 }
0151 
0152 /* Close protocol */
0153 static int ll_close(struct hci_uart *hu)
0154 {
0155     struct ll_struct *ll = hu->priv;
0156 
0157     BT_DBG("hu %p", hu);
0158 
0159     skb_queue_purge(&ll->tx_wait_q);
0160     skb_queue_purge(&ll->txq);
0161 
0162     kfree_skb(ll->rx_skb);
0163 
0164     if (hu->serdev) {
0165         struct ll_device *lldev = serdev_device_get_drvdata(hu->serdev);
0166 
0167         gpiod_set_value_cansleep(lldev->enable_gpio, 0);
0168 
0169         clk_disable_unprepare(lldev->ext_clk);
0170     }
0171 
0172     hu->priv = NULL;
0173 
0174     kfree(ll);
0175 
0176     return 0;
0177 }
0178 
0179 /*
0180  * internal function, which does common work of the device wake up process:
0181  * 1. places all pending packets (waiting in tx_wait_q list) in txq list.
0182  * 2. changes internal state to HCILL_AWAKE.
0183  * Note: assumes that hcill_lock spinlock is taken,
0184  * shouldn't be called otherwise!
0185  */
0186 static void __ll_do_awake(struct ll_struct *ll)
0187 {
0188     struct sk_buff *skb = NULL;
0189 
0190     while ((skb = skb_dequeue(&ll->tx_wait_q)))
0191         skb_queue_tail(&ll->txq, skb);
0192 
0193     ll->hcill_state = HCILL_AWAKE;
0194 }
0195 
0196 /*
0197  * Called upon a wake-up-indication from the device
0198  */
0199 static void ll_device_want_to_wakeup(struct hci_uart *hu)
0200 {
0201     unsigned long flags;
0202     struct ll_struct *ll = hu->priv;
0203 
0204     BT_DBG("hu %p", hu);
0205 
0206     /* lock hcill state */
0207     spin_lock_irqsave(&ll->hcill_lock, flags);
0208 
0209     switch (ll->hcill_state) {
0210     case HCILL_ASLEEP_TO_AWAKE:
0211         /*
0212          * This state means that both the host and the BRF chip
0213          * have simultaneously sent a wake-up-indication packet.
0214          * Traditionally, in this case, receiving a wake-up-indication
0215          * was enough and an additional wake-up-ack wasn't needed.
0216          * This has changed with the BRF6350, which does require an
0217          * explicit wake-up-ack. Other BRF versions, which do not
0218          * require an explicit ack here, do accept it, thus it is
0219          * perfectly safe to always send one.
0220          */
0221         BT_DBG("dual wake-up-indication");
0222         fallthrough;
0223     case HCILL_ASLEEP:
0224         /* acknowledge device wake up */
0225         if (send_hcill_cmd(HCILL_WAKE_UP_ACK, hu) < 0) {
0226             BT_ERR("cannot acknowledge device wake up");
0227             goto out;
0228         }
0229         break;
0230     default:
0231         /* any other state is illegal */
0232         BT_ERR("received HCILL_WAKE_UP_IND in state %ld",
0233                ll->hcill_state);
0234         break;
0235     }
0236 
0237     /* send pending packets and change state to HCILL_AWAKE */
0238     __ll_do_awake(ll);
0239 
0240 out:
0241     spin_unlock_irqrestore(&ll->hcill_lock, flags);
0242 
0243     /* actually send the packets */
0244     hci_uart_tx_wakeup(hu);
0245 }
0246 
0247 /*
0248  * Called upon a sleep-indication from the device
0249  */
0250 static void ll_device_want_to_sleep(struct hci_uart *hu)
0251 {
0252     unsigned long flags;
0253     struct ll_struct *ll = hu->priv;
0254 
0255     BT_DBG("hu %p", hu);
0256 
0257     /* lock hcill state */
0258     spin_lock_irqsave(&ll->hcill_lock, flags);
0259 
0260     /* sanity check */
0261     if (ll->hcill_state != HCILL_AWAKE)
0262         BT_ERR("ERR: HCILL_GO_TO_SLEEP_IND in state %ld",
0263                ll->hcill_state);
0264 
0265     /* acknowledge device sleep */
0266     if (send_hcill_cmd(HCILL_GO_TO_SLEEP_ACK, hu) < 0) {
0267         BT_ERR("cannot acknowledge device sleep");
0268         goto out;
0269     }
0270 
0271     /* update state */
0272     ll->hcill_state = HCILL_ASLEEP;
0273 
0274 out:
0275     spin_unlock_irqrestore(&ll->hcill_lock, flags);
0276 
0277     /* actually send the sleep ack packet */
0278     hci_uart_tx_wakeup(hu);
0279 }
0280 
0281 /*
0282  * Called upon wake-up-acknowledgement from the device
0283  */
0284 static void ll_device_woke_up(struct hci_uart *hu)
0285 {
0286     unsigned long flags;
0287     struct ll_struct *ll = hu->priv;
0288 
0289     BT_DBG("hu %p", hu);
0290 
0291     /* lock hcill state */
0292     spin_lock_irqsave(&ll->hcill_lock, flags);
0293 
0294     /* sanity check */
0295     if (ll->hcill_state != HCILL_ASLEEP_TO_AWAKE)
0296         BT_ERR("received HCILL_WAKE_UP_ACK in state %ld",
0297                ll->hcill_state);
0298 
0299     /* send pending packets and change state to HCILL_AWAKE */
0300     __ll_do_awake(ll);
0301 
0302     spin_unlock_irqrestore(&ll->hcill_lock, flags);
0303 
0304     /* actually send the packets */
0305     hci_uart_tx_wakeup(hu);
0306 }
0307 
0308 /* Enqueue frame for transmittion (padding, crc, etc) */
0309 /* may be called from two simultaneous tasklets */
0310 static int ll_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0311 {
0312     unsigned long flags = 0;
0313     struct ll_struct *ll = hu->priv;
0314 
0315     BT_DBG("hu %p skb %p", hu, skb);
0316 
0317     /* Prepend skb with frame type */
0318     memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
0319 
0320     /* lock hcill state */
0321     spin_lock_irqsave(&ll->hcill_lock, flags);
0322 
0323     /* act according to current state */
0324     switch (ll->hcill_state) {
0325     case HCILL_AWAKE:
0326         BT_DBG("device awake, sending normally");
0327         skb_queue_tail(&ll->txq, skb);
0328         break;
0329     case HCILL_ASLEEP:
0330         BT_DBG("device asleep, waking up and queueing packet");
0331         /* save packet for later */
0332         skb_queue_tail(&ll->tx_wait_q, skb);
0333         /* awake device */
0334         if (send_hcill_cmd(HCILL_WAKE_UP_IND, hu) < 0) {
0335             BT_ERR("cannot wake up device");
0336             break;
0337         }
0338         ll->hcill_state = HCILL_ASLEEP_TO_AWAKE;
0339         break;
0340     case HCILL_ASLEEP_TO_AWAKE:
0341         BT_DBG("device waking up, queueing packet");
0342         /* transient state; just keep packet for later */
0343         skb_queue_tail(&ll->tx_wait_q, skb);
0344         break;
0345     default:
0346         BT_ERR("illegal hcill state: %ld (losing packet)",
0347                ll->hcill_state);
0348         kfree_skb(skb);
0349         break;
0350     }
0351 
0352     spin_unlock_irqrestore(&ll->hcill_lock, flags);
0353 
0354     return 0;
0355 }
0356 
0357 static int ll_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
0358 {
0359     struct hci_uart *hu = hci_get_drvdata(hdev);
0360     struct ll_struct *ll = hu->priv;
0361 
0362     switch (hci_skb_pkt_type(skb)) {
0363     case HCILL_GO_TO_SLEEP_IND:
0364         BT_DBG("HCILL_GO_TO_SLEEP_IND packet");
0365         ll_device_want_to_sleep(hu);
0366         break;
0367     case HCILL_GO_TO_SLEEP_ACK:
0368         /* shouldn't happen */
0369         bt_dev_err(hdev, "received HCILL_GO_TO_SLEEP_ACK in state %ld",
0370                ll->hcill_state);
0371         break;
0372     case HCILL_WAKE_UP_IND:
0373         BT_DBG("HCILL_WAKE_UP_IND packet");
0374         ll_device_want_to_wakeup(hu);
0375         break;
0376     case HCILL_WAKE_UP_ACK:
0377         BT_DBG("HCILL_WAKE_UP_ACK packet");
0378         ll_device_woke_up(hu);
0379         break;
0380     }
0381 
0382     kfree_skb(skb);
0383     return 0;
0384 }
0385 
0386 #define LL_RECV_SLEEP_IND \
0387     .type = HCILL_GO_TO_SLEEP_IND, \
0388     .hlen = 0, \
0389     .loff = 0, \
0390     .lsize = 0, \
0391     .maxlen = 0
0392 
0393 #define LL_RECV_SLEEP_ACK \
0394     .type = HCILL_GO_TO_SLEEP_ACK, \
0395     .hlen = 0, \
0396     .loff = 0, \
0397     .lsize = 0, \
0398     .maxlen = 0
0399 
0400 #define LL_RECV_WAKE_IND \
0401     .type = HCILL_WAKE_UP_IND, \
0402     .hlen = 0, \
0403     .loff = 0, \
0404     .lsize = 0, \
0405     .maxlen = 0
0406 
0407 #define LL_RECV_WAKE_ACK \
0408     .type = HCILL_WAKE_UP_ACK, \
0409     .hlen = 0, \
0410     .loff = 0, \
0411     .lsize = 0, \
0412     .maxlen = 0
0413 
0414 static const struct h4_recv_pkt ll_recv_pkts[] = {
0415     { H4_RECV_ACL,       .recv = hci_recv_frame },
0416     { H4_RECV_SCO,       .recv = hci_recv_frame },
0417     { H4_RECV_EVENT,     .recv = hci_recv_frame },
0418     { LL_RECV_SLEEP_IND, .recv = ll_recv_frame  },
0419     { LL_RECV_SLEEP_ACK, .recv = ll_recv_frame  },
0420     { LL_RECV_WAKE_IND,  .recv = ll_recv_frame  },
0421     { LL_RECV_WAKE_ACK,  .recv = ll_recv_frame  },
0422 };
0423 
0424 /* Recv data */
0425 static int ll_recv(struct hci_uart *hu, const void *data, int count)
0426 {
0427     struct ll_struct *ll = hu->priv;
0428 
0429     if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
0430         return -EUNATCH;
0431 
0432     ll->rx_skb = h4_recv_buf(hu->hdev, ll->rx_skb, data, count,
0433                  ll_recv_pkts, ARRAY_SIZE(ll_recv_pkts));
0434     if (IS_ERR(ll->rx_skb)) {
0435         int err = PTR_ERR(ll->rx_skb);
0436         bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
0437         ll->rx_skb = NULL;
0438         return err;
0439     }
0440 
0441     return count;
0442 }
0443 
0444 static struct sk_buff *ll_dequeue(struct hci_uart *hu)
0445 {
0446     struct ll_struct *ll = hu->priv;
0447 
0448     return skb_dequeue(&ll->txq);
0449 }
0450 
0451 #if IS_ENABLED(CONFIG_SERIAL_DEV_BUS)
0452 static int read_local_version(struct hci_dev *hdev)
0453 {
0454     int err = 0;
0455     unsigned short version = 0;
0456     struct sk_buff *skb;
0457     struct hci_rp_read_local_version *ver;
0458 
0459     skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
0460                  HCI_INIT_TIMEOUT);
0461     if (IS_ERR(skb)) {
0462         bt_dev_err(hdev, "Reading TI version information failed (%ld)",
0463                PTR_ERR(skb));
0464         return PTR_ERR(skb);
0465     }
0466     if (skb->len != sizeof(*ver)) {
0467         err = -EILSEQ;
0468         goto out;
0469     }
0470 
0471     ver = (struct hci_rp_read_local_version *)skb->data;
0472     if (le16_to_cpu(ver->manufacturer) != 13) {
0473         err = -ENODEV;
0474         goto out;
0475     }
0476 
0477     version = le16_to_cpu(ver->lmp_subver);
0478 
0479 out:
0480     if (err)
0481         bt_dev_err(hdev, "Failed to read TI version info: %d", err);
0482     kfree_skb(skb);
0483     return err ? err : version;
0484 }
0485 
0486 static int send_command_from_firmware(struct ll_device *lldev,
0487                       struct hci_command *cmd)
0488 {
0489     struct sk_buff *skb;
0490 
0491     if (cmd->opcode == HCI_VS_UPDATE_UART_HCI_BAUDRATE) {
0492         /* ignore remote change
0493          * baud rate HCI VS command
0494          */
0495         bt_dev_warn(lldev->hu.hdev,
0496                 "change remote baud rate command in firmware");
0497         return 0;
0498     }
0499     if (cmd->prefix != 1)
0500         bt_dev_dbg(lldev->hu.hdev, "command type %d", cmd->prefix);
0501 
0502     skb = __hci_cmd_sync(lldev->hu.hdev, cmd->opcode, cmd->plen,
0503                  &cmd->speed, HCI_INIT_TIMEOUT);
0504     if (IS_ERR(skb)) {
0505         bt_dev_err(lldev->hu.hdev, "send command failed");
0506         return PTR_ERR(skb);
0507     }
0508     kfree_skb(skb);
0509     return 0;
0510 }
0511 
0512 /*
0513  * download_firmware -
0514  *  internal function which parses through the .bts firmware
0515  *  script file intreprets SEND, DELAY actions only as of now
0516  */
0517 static int download_firmware(struct ll_device *lldev)
0518 {
0519     unsigned short chip, min_ver, maj_ver;
0520     int version, err, len;
0521     unsigned char *ptr, *action_ptr;
0522     unsigned char bts_scr_name[40]; /* 40 char long bts scr name? */
0523     const struct firmware *fw;
0524     struct hci_command *cmd;
0525 
0526     version = read_local_version(lldev->hu.hdev);
0527     if (version < 0)
0528         return version;
0529 
0530     chip = (version & 0x7C00) >> 10;
0531     min_ver = (version & 0x007F);
0532     maj_ver = (version & 0x0380) >> 7;
0533     if (version & 0x8000)
0534         maj_ver |= 0x0008;
0535 
0536     snprintf(bts_scr_name, sizeof(bts_scr_name),
0537          "ti-connectivity/TIInit_%d.%d.%d.bts",
0538          chip, maj_ver, min_ver);
0539 
0540     err = request_firmware(&fw, bts_scr_name, &lldev->serdev->dev);
0541     if (err || !fw->data || !fw->size) {
0542         bt_dev_err(lldev->hu.hdev, "request_firmware failed(errno %d) for %s",
0543                err, bts_scr_name);
0544         return -EINVAL;
0545     }
0546     ptr = (void *)fw->data;
0547     len = fw->size;
0548     /* bts_header to remove out magic number and
0549      * version
0550      */
0551     ptr += sizeof(struct bts_header);
0552     len -= sizeof(struct bts_header);
0553 
0554     while (len > 0 && ptr) {
0555         bt_dev_dbg(lldev->hu.hdev, " action size %d, type %d ",
0556                ((struct bts_action *)ptr)->size,
0557                ((struct bts_action *)ptr)->type);
0558 
0559         action_ptr = &(((struct bts_action *)ptr)->data[0]);
0560 
0561         switch (((struct bts_action *)ptr)->type) {
0562         case ACTION_SEND_COMMAND:   /* action send */
0563             bt_dev_dbg(lldev->hu.hdev, "S");
0564             cmd = (struct hci_command *)action_ptr;
0565             err = send_command_from_firmware(lldev, cmd);
0566             if (err)
0567                 goto out_rel_fw;
0568             break;
0569         case ACTION_WAIT_EVENT:  /* wait */
0570             /* no need to wait as command was synchronous */
0571             bt_dev_dbg(lldev->hu.hdev, "W");
0572             break;
0573         case ACTION_DELAY:  /* sleep */
0574             bt_dev_info(lldev->hu.hdev, "sleep command in scr");
0575             msleep(((struct bts_action_delay *)action_ptr)->msec);
0576             break;
0577         }
0578         len -= (sizeof(struct bts_action) +
0579             ((struct bts_action *)ptr)->size);
0580         ptr += sizeof(struct bts_action) +
0581             ((struct bts_action *)ptr)->size;
0582     }
0583 
0584 out_rel_fw:
0585     /* fw download complete */
0586     release_firmware(fw);
0587     return err;
0588 }
0589 
0590 static int ll_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
0591 {
0592     bdaddr_t bdaddr_swapped;
0593     struct sk_buff *skb;
0594 
0595     /* HCI_VS_WRITE_BD_ADDR (at least on a CC2560A chip) expects the BD
0596      * address to be MSB first, but bdaddr_t has the convention of being
0597      * LSB first.
0598      */
0599     baswap(&bdaddr_swapped, bdaddr);
0600     skb = __hci_cmd_sync(hdev, HCI_VS_WRITE_BD_ADDR, sizeof(bdaddr_t),
0601                  &bdaddr_swapped, HCI_INIT_TIMEOUT);
0602     if (!IS_ERR(skb))
0603         kfree_skb(skb);
0604 
0605     return PTR_ERR_OR_ZERO(skb);
0606 }
0607 
0608 static int ll_setup(struct hci_uart *hu)
0609 {
0610     int err, retry = 3;
0611     struct ll_device *lldev;
0612     struct serdev_device *serdev = hu->serdev;
0613     u32 speed;
0614 
0615     if (!serdev)
0616         return 0;
0617 
0618     lldev = serdev_device_get_drvdata(serdev);
0619 
0620     hu->hdev->set_bdaddr = ll_set_bdaddr;
0621 
0622     serdev_device_set_flow_control(serdev, true);
0623 
0624     do {
0625         /* Reset the Bluetooth device */
0626         gpiod_set_value_cansleep(lldev->enable_gpio, 0);
0627         msleep(5);
0628         gpiod_set_value_cansleep(lldev->enable_gpio, 1);
0629         mdelay(100);
0630         err = serdev_device_wait_for_cts(serdev, true, 200);
0631         if (err) {
0632             bt_dev_err(hu->hdev, "Failed to get CTS");
0633             return err;
0634         }
0635 
0636         err = download_firmware(lldev);
0637         if (!err)
0638             break;
0639 
0640         /* Toggle BT_EN and retry */
0641         bt_dev_err(hu->hdev, "download firmware failed, retrying...");
0642     } while (retry--);
0643 
0644     if (err)
0645         return err;
0646 
0647     /* Set BD address if one was specified at probe */
0648     if (!bacmp(&lldev->bdaddr, BDADDR_NONE)) {
0649         /* This means that there was an error getting the BD address
0650          * during probe, so mark the device as having a bad address.
0651          */
0652         set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
0653     } else if (bacmp(&lldev->bdaddr, BDADDR_ANY)) {
0654         err = ll_set_bdaddr(hu->hdev, &lldev->bdaddr);
0655         if (err)
0656             set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
0657     }
0658 
0659     /* Operational speed if any */
0660     if (hu->oper_speed)
0661         speed = hu->oper_speed;
0662     else if (hu->proto->oper_speed)
0663         speed = hu->proto->oper_speed;
0664     else
0665         speed = 0;
0666 
0667     if (speed) {
0668         __le32 speed_le = cpu_to_le32(speed);
0669         struct sk_buff *skb;
0670 
0671         skb = __hci_cmd_sync(hu->hdev, HCI_VS_UPDATE_UART_HCI_BAUDRATE,
0672                      sizeof(speed_le), &speed_le,
0673                      HCI_INIT_TIMEOUT);
0674         if (!IS_ERR(skb)) {
0675             kfree_skb(skb);
0676             serdev_device_set_baudrate(serdev, speed);
0677         }
0678     }
0679 
0680     return 0;
0681 }
0682 
0683 static const struct hci_uart_proto llp;
0684 
0685 static int hci_ti_probe(struct serdev_device *serdev)
0686 {
0687     struct hci_uart *hu;
0688     struct ll_device *lldev;
0689     struct nvmem_cell *bdaddr_cell;
0690     u32 max_speed = 3000000;
0691 
0692     lldev = devm_kzalloc(&serdev->dev, sizeof(struct ll_device), GFP_KERNEL);
0693     if (!lldev)
0694         return -ENOMEM;
0695     hu = &lldev->hu;
0696 
0697     serdev_device_set_drvdata(serdev, lldev);
0698     lldev->serdev = hu->serdev = serdev;
0699 
0700     lldev->enable_gpio = devm_gpiod_get_optional(&serdev->dev,
0701                              "enable",
0702                              GPIOD_OUT_LOW);
0703     if (IS_ERR(lldev->enable_gpio))
0704         return PTR_ERR(lldev->enable_gpio);
0705 
0706     lldev->ext_clk = devm_clk_get(&serdev->dev, "ext_clock");
0707     if (IS_ERR(lldev->ext_clk) && PTR_ERR(lldev->ext_clk) != -ENOENT)
0708         return PTR_ERR(lldev->ext_clk);
0709 
0710     of_property_read_u32(serdev->dev.of_node, "max-speed", &max_speed);
0711     hci_uart_set_speeds(hu, 115200, max_speed);
0712 
0713     /* optional BD address from nvram */
0714     bdaddr_cell = nvmem_cell_get(&serdev->dev, "bd-address");
0715     if (IS_ERR(bdaddr_cell)) {
0716         int err = PTR_ERR(bdaddr_cell);
0717 
0718         if (err == -EPROBE_DEFER)
0719             return err;
0720 
0721         /* ENOENT means there is no matching nvmem cell and ENOSYS
0722          * means that nvmem is not enabled in the kernel configuration.
0723          */
0724         if (err != -ENOENT && err != -ENOSYS) {
0725             /* If there was some other error, give userspace a
0726              * chance to fix the problem instead of failing to load
0727              * the driver. Using BDADDR_NONE as a flag that is
0728              * tested later in the setup function.
0729              */
0730             dev_warn(&serdev->dev,
0731                  "Failed to get \"bd-address\" nvmem cell (%d)\n",
0732                  err);
0733             bacpy(&lldev->bdaddr, BDADDR_NONE);
0734         }
0735     } else {
0736         bdaddr_t *bdaddr;
0737         size_t len;
0738 
0739         bdaddr = nvmem_cell_read(bdaddr_cell, &len);
0740         nvmem_cell_put(bdaddr_cell);
0741         if (IS_ERR(bdaddr)) {
0742             dev_err(&serdev->dev, "Failed to read nvmem bd-address\n");
0743             return PTR_ERR(bdaddr);
0744         }
0745         if (len != sizeof(bdaddr_t)) {
0746             dev_err(&serdev->dev, "Invalid nvmem bd-address length\n");
0747             kfree(bdaddr);
0748             return -EINVAL;
0749         }
0750 
0751         /* As per the device tree bindings, the value from nvmem is
0752          * expected to be MSB first, but in the kernel it is expected
0753          * that bdaddr_t is LSB first.
0754          */
0755         baswap(&lldev->bdaddr, bdaddr);
0756         kfree(bdaddr);
0757     }
0758 
0759     return hci_uart_register_device(hu, &llp);
0760 }
0761 
0762 static void hci_ti_remove(struct serdev_device *serdev)
0763 {
0764     struct ll_device *lldev = serdev_device_get_drvdata(serdev);
0765 
0766     hci_uart_unregister_device(&lldev->hu);
0767 }
0768 
0769 static const struct of_device_id hci_ti_of_match[] = {
0770     { .compatible = "ti,cc2560" },
0771     { .compatible = "ti,wl1271-st" },
0772     { .compatible = "ti,wl1273-st" },
0773     { .compatible = "ti,wl1281-st" },
0774     { .compatible = "ti,wl1283-st" },
0775     { .compatible = "ti,wl1285-st" },
0776     { .compatible = "ti,wl1801-st" },
0777     { .compatible = "ti,wl1805-st" },
0778     { .compatible = "ti,wl1807-st" },
0779     { .compatible = "ti,wl1831-st" },
0780     { .compatible = "ti,wl1835-st" },
0781     { .compatible = "ti,wl1837-st" },
0782     {},
0783 };
0784 MODULE_DEVICE_TABLE(of, hci_ti_of_match);
0785 
0786 static struct serdev_device_driver hci_ti_drv = {
0787     .driver     = {
0788         .name   = "hci-ti",
0789         .of_match_table = of_match_ptr(hci_ti_of_match),
0790     },
0791     .probe  = hci_ti_probe,
0792     .remove = hci_ti_remove,
0793 };
0794 #else
0795 #define ll_setup NULL
0796 #endif
0797 
0798 static const struct hci_uart_proto llp = {
0799     .id     = HCI_UART_LL,
0800     .name       = "LL",
0801     .setup      = ll_setup,
0802     .open       = ll_open,
0803     .close      = ll_close,
0804     .recv       = ll_recv,
0805     .enqueue    = ll_enqueue,
0806     .dequeue    = ll_dequeue,
0807     .flush      = ll_flush,
0808 };
0809 
0810 int __init ll_init(void)
0811 {
0812     serdev_device_driver_register(&hci_ti_drv);
0813 
0814     return hci_uart_register_proto(&llp);
0815 }
0816 
0817 int __exit ll_deinit(void)
0818 {
0819     serdev_device_driver_unregister(&hci_ti_drv);
0820 
0821     return hci_uart_unregister_proto(&llp);
0822 }