blob: 663bed63b87159e6c069d084f9654e9e2f872c28 [file] [log] [blame]
Bjorn Andersson1511cc72016-08-12 17:01:28 -07001/*
2 * Copyright (c) 2016, Linaro Ltd.
3 * Copyright (c) 2015, Sony Mobile Communications Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/module.h>
16#include <linux/slab.h>
Bjorn Andersson5052de82017-03-27 22:26:33 -070017#include <linux/rpmsg.h>
Loic Poulain766154b2017-09-08 15:57:55 +020018#include <linux/of.h>
19
Bjorn Andersson1511cc72016-08-12 17:01:28 -070020#include <linux/soc/qcom/wcnss_ctrl.h>
21#include <linux/platform_device.h>
22
23#include <net/bluetooth/bluetooth.h>
24#include <net/bluetooth/hci_core.h>
25
26#include "btqca.h"
27
28struct btqcomsmd {
29 struct hci_dev *hdev;
30
Loic Poulain6e518112017-09-05 12:26:03 +020031 bdaddr_t bdaddr;
Bjorn Andersson5052de82017-03-27 22:26:33 -070032 struct rpmsg_endpoint *acl_channel;
33 struct rpmsg_endpoint *cmd_channel;
Bjorn Andersson1511cc72016-08-12 17:01:28 -070034};
35
36static int btqcomsmd_recv(struct hci_dev *hdev, unsigned int type,
37 const void *data, size_t count)
38{
39 struct sk_buff *skb;
40
41 /* Use GFP_ATOMIC as we're in IRQ context */
42 skb = bt_skb_alloc(count, GFP_ATOMIC);
43 if (!skb) {
44 hdev->stat.err_rx++;
45 return -ENOMEM;
46 }
47
48 hci_skb_pkt_type(skb) = type;
Johannes Berg59ae1d12017-06-16 14:29:20 +020049 skb_put_data(skb, data, count);
Bjorn Andersson1511cc72016-08-12 17:01:28 -070050
51 return hci_recv_frame(hdev, skb);
52}
53
Bjorn Andersson5052de82017-03-27 22:26:33 -070054static int btqcomsmd_acl_callback(struct rpmsg_device *rpdev, void *data,
55 int count, void *priv, u32 addr)
Bjorn Andersson1511cc72016-08-12 17:01:28 -070056{
Bjorn Andersson5052de82017-03-27 22:26:33 -070057 struct btqcomsmd *btq = priv;
Bjorn Andersson1511cc72016-08-12 17:01:28 -070058
59 btq->hdev->stat.byte_rx += count;
60 return btqcomsmd_recv(btq->hdev, HCI_ACLDATA_PKT, data, count);
61}
62
Bjorn Andersson5052de82017-03-27 22:26:33 -070063static int btqcomsmd_cmd_callback(struct rpmsg_device *rpdev, void *data,
64 int count, void *priv, u32 addr)
Bjorn Andersson1511cc72016-08-12 17:01:28 -070065{
Bjorn Andersson5052de82017-03-27 22:26:33 -070066 struct btqcomsmd *btq = priv;
Bjorn Andersson1511cc72016-08-12 17:01:28 -070067
68 return btqcomsmd_recv(btq->hdev, HCI_EVENT_PKT, data, count);
69}
70
71static int btqcomsmd_send(struct hci_dev *hdev, struct sk_buff *skb)
72{
73 struct btqcomsmd *btq = hci_get_drvdata(hdev);
74 int ret;
75
76 switch (hci_skb_pkt_type(skb)) {
77 case HCI_ACLDATA_PKT:
Bjorn Andersson5052de82017-03-27 22:26:33 -070078 ret = rpmsg_send(btq->acl_channel, skb->data, skb->len);
Bjorn Andersson1511cc72016-08-12 17:01:28 -070079 hdev->stat.acl_tx++;
80 hdev->stat.byte_tx += skb->len;
81 break;
82 case HCI_COMMAND_PKT:
Bjorn Andersson5052de82017-03-27 22:26:33 -070083 ret = rpmsg_send(btq->cmd_channel, skb->data, skb->len);
Bjorn Andersson1511cc72016-08-12 17:01:28 -070084 hdev->stat.cmd_tx++;
85 break;
86 default:
87 ret = -EILSEQ;
88 break;
89 }
90
91 kfree_skb(skb);
92
93 return ret;
94}
95
96static int btqcomsmd_open(struct hci_dev *hdev)
97{
98 return 0;
99}
100
101static int btqcomsmd_close(struct hci_dev *hdev)
102{
103 return 0;
104}
105
Loic Poulain6e518112017-09-05 12:26:03 +0200106static int btqcomsmd_setup(struct hci_dev *hdev)
107{
108 struct btqcomsmd *btq = hci_get_drvdata(hdev);
109 struct sk_buff *skb;
110 int err;
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 /* Devices do not have persistent storage for BD address. If no
118 * BD address has been retrieved during probe, mark the device
119 * as having an invalid BD address.
120 */
121 if (!bacmp(&btq->bdaddr, BDADDR_ANY)) {
122 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
123 return 0;
124 }
125
126 /* When setting a configured BD address fails, mark the device
127 * as having an invalid BD address.
128 */
129 err = qca_set_bdaddr_rome(hdev, &btq->bdaddr);
130 if (err) {
131 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
132 return 0;
133 }
134
135 return 0;
136}
137
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700138static int btqcomsmd_probe(struct platform_device *pdev)
139{
140 struct btqcomsmd *btq;
141 struct hci_dev *hdev;
142 void *wcnss;
143 int ret;
144
145 btq = devm_kzalloc(&pdev->dev, sizeof(*btq), GFP_KERNEL);
146 if (!btq)
147 return -ENOMEM;
148
149 wcnss = dev_get_drvdata(pdev->dev.parent);
150
151 btq->acl_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_ACL",
Bjorn Andersson5052de82017-03-27 22:26:33 -0700152 btqcomsmd_acl_callback, btq);
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700153 if (IS_ERR(btq->acl_channel))
154 return PTR_ERR(btq->acl_channel);
155
156 btq->cmd_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_CMD",
Bjorn Andersson5052de82017-03-27 22:26:33 -0700157 btqcomsmd_cmd_callback, btq);
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700158 if (IS_ERR(btq->cmd_channel))
159 return PTR_ERR(btq->cmd_channel);
160
Loic Poulain766154b2017-09-08 15:57:55 +0200161 /* The local-bd-address property is usually injected by the
162 * bootloader which has access to the allocated BD address.
163 */
164 if (!of_property_read_u8_array(pdev->dev.of_node, "local-bd-address",
165 (u8 *)&btq->bdaddr, sizeof(bdaddr_t))) {
166 dev_info(&pdev->dev, "BD address %pMR retrieved from device-tree",
167 &btq->bdaddr);
168 }
169
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700170 hdev = hci_alloc_dev();
171 if (!hdev)
172 return -ENOMEM;
173
174 hci_set_drvdata(hdev, btq);
175 btq->hdev = hdev;
176 SET_HCIDEV_DEV(hdev, &pdev->dev);
177
178 hdev->bus = HCI_SMD;
179 hdev->open = btqcomsmd_open;
180 hdev->close = btqcomsmd_close;
181 hdev->send = btqcomsmd_send;
Loic Poulain6e518112017-09-05 12:26:03 +0200182 hdev->setup = btqcomsmd_setup;
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700183 hdev->set_bdaddr = qca_set_bdaddr_rome;
184
185 ret = hci_register_dev(hdev);
186 if (ret < 0) {
187 hci_free_dev(hdev);
188 return ret;
189 }
190
191 platform_set_drvdata(pdev, btq);
192
193 return 0;
194}
195
196static int btqcomsmd_remove(struct platform_device *pdev)
197{
198 struct btqcomsmd *btq = platform_get_drvdata(pdev);
199
200 hci_unregister_dev(btq->hdev);
201 hci_free_dev(btq->hdev);
202
Bjorn Andersson5052de82017-03-27 22:26:33 -0700203 rpmsg_destroy_ept(btq->cmd_channel);
204 rpmsg_destroy_ept(btq->acl_channel);
205
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700206 return 0;
207}
208
209static const struct of_device_id btqcomsmd_of_match[] = {
210 { .compatible = "qcom,wcnss-bt", },
211 { },
212};
Javier Martinez Canillas747d3f12017-01-02 10:09:56 -0300213MODULE_DEVICE_TABLE(of, btqcomsmd_of_match);
Bjorn Andersson1511cc72016-08-12 17:01:28 -0700214
215static struct platform_driver btqcomsmd_driver = {
216 .probe = btqcomsmd_probe,
217 .remove = btqcomsmd_remove,
218 .driver = {
219 .name = "btqcomsmd",
220 .of_match_table = btqcomsmd_of_match,
221 },
222};
223
224module_platform_driver(btqcomsmd_driver);
225
226MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
227MODULE_DESCRIPTION("Qualcomm SMD HCI driver");
228MODULE_LICENSE("GPL v2");