Thomas Gleixner | 97fb5e8 | 2019-05-29 07:17:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, Linaro Ltd. |
| 4 | * Copyright (c) 2015, Sony Mobile Communications Inc. |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/slab.h> |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 9 | #include <linux/rpmsg.h> |
Loic Poulain | 766154b | 2017-09-08 15:57:55 +0200 | [diff] [blame] | 10 | #include <linux/of.h> |
| 11 | |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 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 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 23 | struct rpmsg_endpoint *acl_channel; |
| 24 | struct rpmsg_endpoint *cmd_channel; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 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 | /* Use GFP_ATOMIC as we're in IRQ context */ |
| 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; |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 40 | skb_put_data(skb, data, count); |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 41 | |
| 42 | return hci_recv_frame(hdev, skb); |
| 43 | } |
| 44 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 45 | static int btqcomsmd_acl_callback(struct rpmsg_device *rpdev, void *data, |
| 46 | int count, void *priv, u32 addr) |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 47 | { |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 48 | struct btqcomsmd *btq = priv; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 49 | |
| 50 | btq->hdev->stat.byte_rx += count; |
| 51 | return btqcomsmd_recv(btq->hdev, HCI_ACLDATA_PKT, data, count); |
| 52 | } |
| 53 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 54 | static int btqcomsmd_cmd_callback(struct rpmsg_device *rpdev, void *data, |
| 55 | int count, void *priv, u32 addr) |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 56 | { |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 57 | struct btqcomsmd *btq = priv; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 58 | |
Loic Poulain | 61a1ecf | 2018-04-06 11:23:45 +0200 | [diff] [blame] | 59 | btq->hdev->stat.byte_rx += count; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 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: |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 70 | ret = rpmsg_send(btq->acl_channel, skb->data, skb->len); |
Loic Poulain | 61a1ecf | 2018-04-06 11:23:45 +0200 | [diff] [blame] | 71 | if (ret) { |
| 72 | hdev->stat.err_tx++; |
| 73 | break; |
| 74 | } |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 75 | hdev->stat.acl_tx++; |
| 76 | hdev->stat.byte_tx += skb->len; |
| 77 | break; |
| 78 | case HCI_COMMAND_PKT: |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 79 | ret = rpmsg_send(btq->cmd_channel, skb->data, skb->len); |
Loic Poulain | 61a1ecf | 2018-04-06 11:23:45 +0200 | [diff] [blame] | 80 | if (ret) { |
| 81 | hdev->stat.err_tx++; |
| 82 | break; |
| 83 | } |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 84 | hdev->stat.cmd_tx++; |
Loic Poulain | 61a1ecf | 2018-04-06 11:23:45 +0200 | [diff] [blame] | 85 | hdev->stat.byte_tx += skb->len; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 86 | break; |
| 87 | default: |
| 88 | ret = -EILSEQ; |
| 89 | break; |
| 90 | } |
| 91 | |
Loic Poulain | 67b8fbe | 2017-11-22 15:03:17 +0100 | [diff] [blame] | 92 | if (!ret) |
| 93 | kfree_skb(skb); |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 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 | |
Loic Poulain | 6e51811 | 2017-09-05 12:26:03 +0200 | [diff] [blame] | 108 | static int btqcomsmd_setup(struct hci_dev *hdev) |
| 109 | { |
Loic Poulain | 6e51811 | 2017-09-05 12:26:03 +0200 | [diff] [blame] | 110 | struct sk_buff *skb; |
Loic Poulain | 6e51811 | 2017-09-05 12:26:03 +0200 | [diff] [blame] | 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 | |
Matthias Kaehlcke | de79a9d | 2019-02-19 12:05:58 -0800 | [diff] [blame] | 117 | /* Devices do not have persistent storage for BD address. Retrieve |
| 118 | * it from the firmware node property. |
Loic Poulain | 6e51811 | 2017-09-05 12:26:03 +0200 | [diff] [blame] | 119 | */ |
Matthias Kaehlcke | de79a9d | 2019-02-19 12:05:58 -0800 | [diff] [blame] | 120 | set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks); |
Loic Poulain | 6e51811 | 2017-09-05 12:26:03 +0200 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 125 | static int btqcomsmd_probe(struct platform_device *pdev) |
| 126 | { |
| 127 | struct btqcomsmd *btq; |
| 128 | struct hci_dev *hdev; |
| 129 | void *wcnss; |
| 130 | int ret; |
| 131 | |
| 132 | btq = devm_kzalloc(&pdev->dev, sizeof(*btq), GFP_KERNEL); |
| 133 | if (!btq) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | wcnss = dev_get_drvdata(pdev->dev.parent); |
| 137 | |
| 138 | btq->acl_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_ACL", |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 139 | btqcomsmd_acl_callback, btq); |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 140 | if (IS_ERR(btq->acl_channel)) |
| 141 | return PTR_ERR(btq->acl_channel); |
| 142 | |
| 143 | btq->cmd_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_CMD", |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 144 | btqcomsmd_cmd_callback, btq); |
Christophe JAILLET | 9a39a92 | 2020-12-12 10:46:58 +0100 | [diff] [blame] | 145 | if (IS_ERR(btq->cmd_channel)) { |
| 146 | ret = PTR_ERR(btq->cmd_channel); |
| 147 | goto destroy_acl_channel; |
| 148 | } |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 149 | |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 150 | hdev = hci_alloc_dev(); |
Christophe JAILLET | 9a39a92 | 2020-12-12 10:46:58 +0100 | [diff] [blame] | 151 | if (!hdev) { |
| 152 | ret = -ENOMEM; |
| 153 | goto destroy_cmd_channel; |
| 154 | } |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 155 | |
| 156 | hci_set_drvdata(hdev, btq); |
| 157 | btq->hdev = hdev; |
| 158 | SET_HCIDEV_DEV(hdev, &pdev->dev); |
| 159 | |
| 160 | hdev->bus = HCI_SMD; |
| 161 | hdev->open = btqcomsmd_open; |
| 162 | hdev->close = btqcomsmd_close; |
| 163 | hdev->send = btqcomsmd_send; |
Loic Poulain | 6e51811 | 2017-09-05 12:26:03 +0200 | [diff] [blame] | 164 | hdev->setup = btqcomsmd_setup; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 165 | hdev->set_bdaddr = qca_set_bdaddr_rome; |
| 166 | |
| 167 | ret = hci_register_dev(hdev); |
Christophe JAILLET | 9a39a92 | 2020-12-12 10:46:58 +0100 | [diff] [blame] | 168 | if (ret < 0) |
| 169 | goto hci_free_dev; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 170 | |
| 171 | platform_set_drvdata(pdev, btq); |
| 172 | |
| 173 | return 0; |
Christophe JAILLET | 9a39a92 | 2020-12-12 10:46:58 +0100 | [diff] [blame] | 174 | |
| 175 | hci_free_dev: |
| 176 | hci_free_dev(hdev); |
| 177 | destroy_cmd_channel: |
| 178 | rpmsg_destroy_ept(btq->cmd_channel); |
| 179 | destroy_acl_channel: |
| 180 | rpmsg_destroy_ept(btq->acl_channel); |
| 181 | |
| 182 | return ret; |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static int btqcomsmd_remove(struct platform_device *pdev) |
| 186 | { |
| 187 | struct btqcomsmd *btq = platform_get_drvdata(pdev); |
| 188 | |
| 189 | hci_unregister_dev(btq->hdev); |
| 190 | hci_free_dev(btq->hdev); |
| 191 | |
Bjorn Andersson | 5052de8 | 2017-03-27 22:26:33 -0700 | [diff] [blame] | 192 | rpmsg_destroy_ept(btq->cmd_channel); |
| 193 | rpmsg_destroy_ept(btq->acl_channel); |
| 194 | |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static const struct of_device_id btqcomsmd_of_match[] = { |
| 199 | { .compatible = "qcom,wcnss-bt", }, |
| 200 | { }, |
| 201 | }; |
Javier Martinez Canillas | 747d3f1 | 2017-01-02 10:09:56 -0300 | [diff] [blame] | 202 | MODULE_DEVICE_TABLE(of, btqcomsmd_of_match); |
Bjorn Andersson | 1511cc7 | 2016-08-12 17:01:28 -0700 | [diff] [blame] | 203 | |
| 204 | static struct platform_driver btqcomsmd_driver = { |
| 205 | .probe = btqcomsmd_probe, |
| 206 | .remove = btqcomsmd_remove, |
| 207 | .driver = { |
| 208 | .name = "btqcomsmd", |
| 209 | .of_match_table = btqcomsmd_of_match, |
| 210 | }, |
| 211 | }; |
| 212 | |
| 213 | module_platform_driver(btqcomsmd_driver); |
| 214 | |
| 215 | MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>"); |
| 216 | MODULE_DESCRIPTION("Qualcomm SMD HCI driver"); |
| 217 | MODULE_LICENSE("GPL v2"); |