| 1 | |
| 2 | |
| 3 | * Copyright (c) 2016, Linaro Ltd.
|
| 4 | * Copyright (c) 2015, Sony Mobile Communications Inc.
|
| 5 |
|
| 6 |
|
| 7 | #include <linux/module.h>
|
| 8 | #include <linux/slab.h>
|
| 9 | #include <linux/rpmsg.h>
|
| 10 | #include <linux/of.h>
|
| 11 |
|
| 12 | #include <linux/soc/qcom/wcnss_ctrl.h>
|
| 13 | #include <linux/platform_device.h>
|
| 14 |
|
| 15 | #include <net/bluetooth/bluetooth.h>
|
| 16 | #include <net/bluetooth/hci_core.h>
|
| 17 |
|
| 18 | #include "btqca.h"
|
| 19 |
|
| 20 | struct btqcomsmd {
|
| 21 | struct hci_dev *hdev;
|
| 22 |
|
| 23 | struct rpmsg_endpoint *acl_channel;
|
| 24 | struct rpmsg_endpoint *cmd_channel;
|
| 25 | };
|
| 26 |
|
| 27 | static int btqcomsmd_recv(struct hci_dev *hdev, unsigned int type,
|
| 28 | const void *data, size_t count)
|
| 29 | {
|
| 30 | struct sk_buff *skb;
|
| 31 |
|
| 32 |
|
| 33 | skb = bt_skb_alloc(count, GFP_ATOMIC);
|
| 34 | if (!skb) {
|
| 35 | hdev->stat.err_rx++;
|
| 36 | return -ENOMEM;
|
| 37 | }
|
| 38 |
|
| 39 | hci_skb_pkt_type(skb) = type;
|
| 40 | skb_put_data(skb, data, count);
|
| 41 |
|
| 42 | return hci_recv_frame(hdev, skb);
|
| 43 | }
|
| 44 |
|
| 45 | static int btqcomsmd_acl_callback(struct rpmsg_device *rpdev, void *data,
|
| 46 | int count, void *priv, u32 addr)
|
| 47 | {
|
| 48 | struct btqcomsmd *btq = priv;
|
| 49 |
|
| 50 | btq->hdev->stat.byte_rx += count;
|
| 51 | return btqcomsmd_recv(btq->hdev, HCI_ACLDATA_PKT, data, count);
|
| 52 | }
|
| 53 |
|
| 54 | static int btqcomsmd_cmd_callback(struct rpmsg_device *rpdev, void *data,
|
| 55 | int count, void *priv, u32 addr)
|
| 56 | {
|
| 57 | struct btqcomsmd *btq = priv;
|
| 58 |
|
| 59 | btq->hdev->stat.byte_rx += count;
|
| 60 | return btqcomsmd_recv(btq->hdev, HCI_EVENT_PKT, data, count);
|
| 61 | }
|
| 62 |
|
| 63 | static int btqcomsmd_send(struct hci_dev *hdev, struct sk_buff *skb)
|
| 64 | {
|
| 65 | struct btqcomsmd *btq = hci_get_drvdata(hdev);
|
| 66 | int ret;
|
| 67 |
|
| 68 | switch (hci_skb_pkt_type(skb)) {
|
| 69 | case HCI_ACLDATA_PKT:
|
| 70 | ret = rpmsg_send(btq->acl_channel, skb->data, skb->len);
|
| 71 | if (ret) {
|
| 72 | hdev->stat.err_tx++;
|
| 73 | break;
|
| 74 | }
|
| 75 | hdev->stat.acl_tx++;
|
| 76 | hdev->stat.byte_tx += skb->len;
|
| 77 | break;
|
| 78 | case HCI_COMMAND_PKT:
|
| 79 | ret = rpmsg_send(btq->cmd_channel, skb->data, skb->len);
|
| 80 | if (ret) {
|
| 81 | hdev->stat.err_tx++;
|
| 82 | break;
|
| 83 | }
|
| 84 | hdev->stat.cmd_tx++;
|
| 85 | hdev->stat.byte_tx += skb->len;
|
| 86 | break;
|
| 87 | default:
|
| 88 | ret = -EILSEQ;
|
| 89 | break;
|
| 90 | }
|
| 91 |
|
| 92 | if (!ret)
|
| 93 | kfree_skb(skb);
|
| 94 |
|
| 95 | return ret;
|
| 96 | }
|
| 97 |
|
| 98 | static int btqcomsmd_open(struct hci_dev *hdev)
|
| 99 | {
|
| 100 | return 0;
|
| 101 | }
|
| 102 |
|
| 103 | static int btqcomsmd_close(struct hci_dev *hdev)
|
| 104 | {
|
| 105 | return 0;
|
| 106 | }
|
| 107 |
|
| 108 | static int btqcomsmd_setup(struct hci_dev *hdev)
|
| 109 | {
|
| 110 | struct sk_buff *skb;
|
| 111 |
|
| 112 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
|
| 113 | if (IS_ERR(skb))
|
| 114 | return PTR_ERR(skb);
|
| 115 | kfree_skb(skb);
|
| 116 |
|
| 117 | |
| 118 | * it from the firmware node property.
|
| 119 |
|
| 120 | hci_set_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY);
|
| 121 |
|
| 122 | return 0;
|
| 123 | }
|
| 124 |
|
| 125 | static int btqcomsmd_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
|
| 126 | {
|
| 127 | int ret;
|
| 128 |
|
| 129 | ret = qca_set_bdaddr_rome(hdev, bdaddr);
|
| 130 | if (ret)
|
| 131 | return ret;
|
| 132 |
|
| 133 | |
| 134 | * causing timeouts for subsequent commands. Sleep a bit to avoid this.
|
| 135 |
|
| 136 | usleep_range(1000, 10000);
|
| 137 | return 0;
|
| 138 | }
|
| 139 |
|
| 140 | static int btqcomsmd_probe(struct platform_device *pdev)
|
| 141 | {
|
| 142 | struct btqcomsmd *btq;
|
| 143 | struct hci_dev *hdev;
|
| 144 | void *wcnss;
|
| 145 | int ret;
|
| 146 |
|
| 147 | btq = devm_kzalloc(&pdev->dev, sizeof(*btq), GFP_KERNEL);
|
| 148 | if (!btq)
|
| 149 | return -ENOMEM;
|
| 150 |
|
| 151 | wcnss = dev_get_drvdata(pdev->dev.parent);
|
| 152 |
|
| 153 | btq->acl_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_ACL",
|
| 154 | btqcomsmd_acl_callback, btq);
|
| 155 | if (IS_ERR(btq->acl_channel))
|
| 156 | return PTR_ERR(btq->acl_channel);
|
| 157 |
|
| 158 | btq->cmd_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_CMD",
|
| 159 | btqcomsmd_cmd_callback, btq);
|
| 160 | if (IS_ERR(btq->cmd_channel)) {
|
| 161 | ret = PTR_ERR(btq->cmd_channel);
|
| 162 | goto destroy_acl_channel;
|
| 163 | }
|
| 164 |
|
| 165 | hdev = hci_alloc_dev();
|
| 166 | if (!hdev) {
|
| 167 | ret = -ENOMEM;
|
| 168 | goto destroy_cmd_channel;
|
| 169 | }
|
| 170 |
|
| 171 | hci_set_drvdata(hdev, btq);
|
| 172 | btq->hdev = hdev;
|
| 173 | SET_HCIDEV_DEV(hdev, &pdev->dev);
|
| 174 |
|
| 175 | hdev->bus = HCI_SMD;
|
| 176 | hdev->open = btqcomsmd_open;
|
| 177 | hdev->close = btqcomsmd_close;
|
| 178 | hdev->send = btqcomsmd_send;
|
| 179 | hdev->setup = btqcomsmd_setup;
|
| 180 | hdev->set_bdaddr = btqcomsmd_set_bdaddr;
|
| 181 |
|
| 182 | ret = hci_register_dev(hdev);
|
| 183 | if (ret < 0)
|
| 184 | goto hci_free_dev;
|
| 185 |
|
| 186 | platform_set_drvdata(pdev, btq);
|
| 187 |
|
| 188 | return 0;
|
| 189 |
|
| 190 | hci_free_dev:
|
| 191 | rpmsg_destroy_ept(btq->cmd_channel);
|
| 192 | rpmsg_destroy_ept(btq->acl_channel);
|
| 193 | hci_free_dev(hdev);
|
| 194 | return ret;
|
| 195 | destroy_cmd_channel:
|
| 196 | rpmsg_destroy_ept(btq->cmd_channel);
|
| 197 | destroy_acl_channel:
|
| 198 | rpmsg_destroy_ept(btq->acl_channel);
|
| 199 |
|
| 200 | return ret;
|
| 201 | }
|
| 202 |
|
| 203 | static void btqcomsmd_remove(struct platform_device *pdev)
|
| 204 | {
|
| 205 | struct btqcomsmd *btq = platform_get_drvdata(pdev);
|
| 206 |
|
| 207 | hci_unregister_dev(btq->hdev);
|
| 208 |
|
| 209 | rpmsg_destroy_ept(btq->cmd_channel);
|
| 210 | rpmsg_destroy_ept(btq->acl_channel);
|
| 211 |
|
| 212 | hci_free_dev(btq->hdev);
|
| 213 | }
|
| 214 |
|
| 215 | static const struct of_device_id btqcomsmd_of_match[] = {
|
| 216 | { .compatible = "qcom,wcnss-bt", },
|
| 217 | { },
|
| 218 | };
|
| 219 | MODULE_DEVICE_TABLE(of, btqcomsmd_of_match);
|
| 220 |
|
| 221 | static struct platform_driver btqcomsmd_driver = {
|
| 222 | .probe = btqcomsmd_probe,
|
| 223 | .remove = btqcomsmd_remove,
|
| 224 | .driver = {
|
| 225 | .name = "btqcomsmd",
|
| 226 | .of_match_table = btqcomsmd_of_match,
|
| 227 | },
|
| 228 | };
|
| 229 |
|
| 230 | module_platform_driver(btqcomsmd_driver);
|
| 231 |
|
| 232 | MODULE_AUTHOR("Bjorn Andersson <[email protected]>");
|
| 233 | MODULE_DESCRIPTION("Qualcomm SMD HCI driver");
|
| 234 | MODULE_LICENSE("GPL v2");
|