1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define DEBUG
26#include <linux/platform_device.h>
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/hci.h>
30
31#include <linux/ti_wilink_st.h>
32#include <linux/module.h>
33
34
35#define VERSION "1.0"
36#define MAX_BT_CHNL_IDS 3
37
38
39
40
41#define BT_REGISTER_TIMEOUT 6000
42
43
44
45
46
47
48
49
50
51
52struct ti_st {
53 struct hci_dev *hdev;
54 char reg_status;
55 long (*st_write) (struct sk_buff *);
56 struct completion wait_reg_completion;
57};
58
59
60static inline void ti_st_tx_complete(struct ti_st *hst, int pkt_type)
61{
62 struct hci_dev *hdev = hst->hdev;
63
64
65 switch (pkt_type) {
66 case HCI_COMMAND_PKT:
67 hdev->stat.cmd_tx++;
68 break;
69
70 case HCI_ACLDATA_PKT:
71 hdev->stat.acl_tx++;
72 break;
73
74 case HCI_SCODATA_PKT:
75 hdev->stat.sco_tx++;
76 break;
77 }
78}
79
80
81
82
83
84
85
86static void st_reg_completion_cb(void *priv_data, char data)
87{
88 struct ti_st *lhst = priv_data;
89
90
91 lhst->reg_status = data;
92
93 complete(&lhst->wait_reg_completion);
94}
95
96
97
98static long st_receive(void *priv_data, struct sk_buff *skb)
99{
100 struct ti_st *lhst = priv_data;
101 int err;
102
103 if (!skb)
104 return -EFAULT;
105
106 if (!lhst) {
107 kfree_skb(skb);
108 return -EFAULT;
109 }
110
111 skb->dev = (void *) lhst->hdev;
112
113
114 err = hci_recv_frame(skb);
115 if (err < 0) {
116 BT_ERR("Unable to push skb to HCI core(%d)", err);
117 return err;
118 }
119
120 lhst->hdev->stat.byte_rx += skb->len;
121
122 return 0;
123}
124
125
126
127static struct st_proto_s ti_st_proto[MAX_BT_CHNL_IDS] = {
128 {
129 .chnl_id = HCI_EVENT_PKT,
130 .hdr_len = sizeof(struct hci_event_hdr),
131 .offset_len_in_hdr = offsetof(struct hci_event_hdr, plen),
132 .len_size = 1,
133 .reserve = 8,
134 },
135 {
136 .chnl_id = HCI_ACLDATA_PKT,
137 .hdr_len = sizeof(struct hci_acl_hdr),
138 .offset_len_in_hdr = offsetof(struct hci_acl_hdr, dlen),
139 .len_size = 2,
140 .reserve = 8,
141 },
142 {
143 .chnl_id = HCI_SCODATA_PKT,
144 .hdr_len = sizeof(struct hci_sco_hdr),
145 .offset_len_in_hdr = offsetof(struct hci_sco_hdr, dlen),
146 .len_size = 1,
147 .reserve = 8,
148 },
149};
150
151
152static int ti_st_open(struct hci_dev *hdev)
153{
154 unsigned long timeleft;
155 struct ti_st *hst;
156 int err, i;
157
158 BT_DBG("%s %p", hdev->name, hdev);
159
160 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
161 return -EBUSY;
162
163
164 hst = hci_get_drvdata(hdev);
165
166 for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
167 ti_st_proto[i].priv_data = hst;
168 ti_st_proto[i].max_frame_size = HCI_MAX_FRAME_SIZE;
169 ti_st_proto[i].recv = st_receive;
170 ti_st_proto[i].reg_complete_cb = st_reg_completion_cb;
171
172
173 init_completion(&hst->wait_reg_completion);
174
175
176
177
178
179 hst->reg_status = -EINPROGRESS;
180
181 err = st_register(&ti_st_proto[i]);
182 if (!err)
183 goto done;
184
185 if (err != -EINPROGRESS) {
186 clear_bit(HCI_RUNNING, &hdev->flags);
187 BT_ERR("st_register failed %d", err);
188 return err;
189 }
190
191
192
193
194 BT_DBG("waiting for registration "
195 "completion signal from ST");
196 timeleft = wait_for_completion_timeout
197 (&hst->wait_reg_completion,
198 msecs_to_jiffies(BT_REGISTER_TIMEOUT));
199 if (!timeleft) {
200 clear_bit(HCI_RUNNING, &hdev->flags);
201 BT_ERR("Timeout(%d sec),didn't get reg "
202 "completion signal from ST",
203 BT_REGISTER_TIMEOUT / 1000);
204 return -ETIMEDOUT;
205 }
206
207
208
209 if (hst->reg_status != 0) {
210 clear_bit(HCI_RUNNING, &hdev->flags);
211 BT_ERR("ST registration completed with invalid "
212 "status %d", hst->reg_status);
213 return -EAGAIN;
214 }
215
216done:
217 hst->st_write = ti_st_proto[i].write;
218 if (!hst->st_write) {
219 BT_ERR("undefined ST write function");
220 clear_bit(HCI_RUNNING, &hdev->flags);
221 for (i = 0; i < MAX_BT_CHNL_IDS; i++) {
222
223 err = st_unregister(&ti_st_proto[i]);
224 if (err)
225 BT_ERR("st_unregister() failed with "
226 "error %d", err);
227 hst->st_write = NULL;
228 }
229 return -EIO;
230 }
231 }
232 return 0;
233}
234
235
236static int ti_st_close(struct hci_dev *hdev)
237{
238 int err, i;
239 struct ti_st *hst = hci_get_drvdata(hdev);
240
241 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
242 return 0;
243
244 for (i = MAX_BT_CHNL_IDS-1; i >= 0; i--) {
245 err = st_unregister(&ti_st_proto[i]);
246 if (err)
247 BT_ERR("st_unregister(%d) failed with error %d",
248 ti_st_proto[i].chnl_id, err);
249 }
250
251 hst->st_write = NULL;
252
253 return err;
254}
255
256static int ti_st_send_frame(struct sk_buff *skb)
257{
258 struct hci_dev *hdev;
259 struct ti_st *hst;
260 long len;
261
262 hdev = (struct hci_dev *)skb->dev;
263
264 if (!test_bit(HCI_RUNNING, &hdev->flags))
265 return -EBUSY;
266
267 hst = hci_get_drvdata(hdev);
268
269
270 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
271
272 BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
273 skb->len);
274
275
276
277
278
279 len = hst->st_write(skb);
280 if (len < 0) {
281 kfree_skb(skb);
282 BT_ERR("ST write failed (%ld)", len);
283
284 return -EAGAIN;
285 }
286
287
288 hdev->stat.byte_tx += len;
289 ti_st_tx_complete(hst, bt_cb(skb)->pkt_type);
290
291 return 0;
292}
293
294static int bt_ti_probe(struct platform_device *pdev)
295{
296 static struct ti_st *hst;
297 struct hci_dev *hdev;
298 int err;
299
300 hst = devm_kzalloc(&pdev->dev, sizeof(struct ti_st), GFP_KERNEL);
301 if (!hst)
302 return -ENOMEM;
303
304
305 hdev = hci_alloc_dev();
306 if (!hdev)
307 return -ENOMEM;
308
309 BT_DBG("hdev %p", hdev);
310
311 hst->hdev = hdev;
312 hdev->bus = HCI_UART;
313 hci_set_drvdata(hdev, hst);
314 hdev->open = ti_st_open;
315 hdev->close = ti_st_close;
316 hdev->flush = NULL;
317 hdev->send = ti_st_send_frame;
318
319 err = hci_register_dev(hdev);
320 if (err < 0) {
321 BT_ERR("Can't register HCI device error %d", err);
322 hci_free_dev(hdev);
323 return err;
324 }
325
326 BT_DBG("HCI device registered (hdev %p)", hdev);
327
328 dev_set_drvdata(&pdev->dev, hst);
329 return err;
330}
331
332static int bt_ti_remove(struct platform_device *pdev)
333{
334 struct hci_dev *hdev;
335 struct ti_st *hst = dev_get_drvdata(&pdev->dev);
336
337 if (!hst)
338 return -EFAULT;
339
340 BT_DBG("%s", hst->hdev->name);
341
342 hdev = hst->hdev;
343 ti_st_close(hdev);
344 hci_unregister_dev(hdev);
345
346 hci_free_dev(hdev);
347
348 dev_set_drvdata(&pdev->dev, NULL);
349 return 0;
350}
351
352static struct platform_driver btwilink_driver = {
353 .probe = bt_ti_probe,
354 .remove = bt_ti_remove,
355 .driver = {
356 .name = "btwilink",
357 .owner = THIS_MODULE,
358 },
359};
360
361module_platform_driver(btwilink_driver);
362
363
364
365MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
366MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
367MODULE_VERSION(VERSION);
368MODULE_LICENSE("GPL");
369