]>
Commit | Line | Data |
---|---|---|
70ddf80a PS |
1 | /* |
2 | * Texas Instrument's Bluetooth Driver For Shared Transport. | |
3 | * | |
4 | * Bluetooth Driver acts as interface between HCI CORE and | |
5 | * TI Shared Transport Layer. | |
6 | * | |
7 | * Copyright (C) 2009 Texas Instruments | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <net/bluetooth/bluetooth.h> | |
25 | #include <net/bluetooth/hci_core.h> | |
26 | ||
e5558679 | 27 | #include <linux/ti_wilink_st.h> |
70ddf80a PS |
28 | #include "bt_drv.h" |
29 | ||
30 | /* Define this macro to get debug msg */ | |
31 | #undef DEBUG | |
32 | ||
33 | #ifdef DEBUG | |
34 | #define BT_DRV_DBG(fmt, arg...) printk(KERN_INFO "(btdrv):"fmt"\n" , ## arg) | |
35 | #define BTDRV_API_START() printk(KERN_INFO "(btdrv): %s Start\n", \ | |
36 | __func__) | |
37 | #define BTDRV_API_EXIT(errno) printk(KERN_INFO "(btdrv): %s Exit(%d)\n", \ | |
38 | __func__, errno) | |
39 | #else | |
40 | #define BT_DRV_DBG(fmt, arg...) | |
41 | #define BTDRV_API_START() | |
42 | #define BTDRV_API_EXIT(errno) | |
43 | #endif | |
44 | ||
45 | #define BT_DRV_ERR(fmt, arg...) printk(KERN_ERR "(btdrv):"fmt"\n" , ## arg) | |
46 | ||
47 | static int reset; | |
48 | static struct hci_st *hst; | |
49 | ||
50 | /* Increments HCI counters based on pocket ID (cmd,acl,sco) */ | |
51 | static inline void hci_st_tx_complete(struct hci_st *hst, int pkt_type) | |
52 | { | |
53 | struct hci_dev *hdev; | |
54 | ||
55 | BTDRV_API_START(); | |
56 | ||
57 | hdev = hst->hdev; | |
58 | ||
59 | /* Update HCI stat counters */ | |
60 | switch (pkt_type) { | |
61 | case HCI_COMMAND_PKT: | |
62 | hdev->stat.cmd_tx++; | |
63 | break; | |
64 | ||
65 | case HCI_ACLDATA_PKT: | |
66 | hdev->stat.acl_tx++; | |
67 | break; | |
68 | ||
69 | case HCI_SCODATA_PKT: | |
70 | hdev->stat.cmd_tx++; | |
71 | break; | |
72 | } | |
73 | ||
74 | BTDRV_API_EXIT(0); | |
75 | } | |
76 | ||
77 | /* ------- Interfaces to Shared Transport ------ */ | |
78 | ||
79 | /* Called by ST layer to indicate protocol registration completion | |
80 | * status.hci_st_open() function will wait for signal from this | |
81 | * API when st_register() function returns ST_PENDING. | |
82 | */ | |
bb8f3c06 | 83 | static void hci_st_registration_completion_cb(void *priv_data, char data) |
70ddf80a | 84 | { |
bb8f3c06 | 85 | struct hci_st *lhst = (struct hci_st *)priv_data; |
70ddf80a PS |
86 | BTDRV_API_START(); |
87 | ||
88 | /* hci_st_open() function needs value of 'data' to know | |
89 | * the registration status(success/fail),So have a back | |
90 | * up of it. | |
91 | */ | |
bb8f3c06 | 92 | lhst->streg_cbdata = data; |
70ddf80a PS |
93 | |
94 | /* Got a feedback from ST for BT driver registration | |
95 | * request.Wackup hci_st_open() function to continue | |
96 | * it's open operation. | |
97 | */ | |
bb8f3c06 | 98 | complete(&lhst->wait_for_btdrv_reg_completion); |
70ddf80a PS |
99 | |
100 | BTDRV_API_EXIT(0); | |
101 | } | |
102 | ||
103 | /* Called by Shared Transport layer when receive data is | |
104 | * available */ | |
bb8f3c06 | 105 | static long hci_st_receive(void *priv_data, struct sk_buff *skb) |
70ddf80a PS |
106 | { |
107 | int err; | |
108 | int len; | |
bb8f3c06 | 109 | struct hci_st *lhst = (struct hci_st *)priv_data; |
70ddf80a PS |
110 | |
111 | BTDRV_API_START(); | |
112 | ||
113 | err = 0; | |
114 | len = 0; | |
115 | ||
116 | if (skb == NULL) { | |
117 | BT_DRV_ERR("Invalid SKB received from ST"); | |
118 | BTDRV_API_EXIT(-EFAULT); | |
119 | return -EFAULT; | |
120 | } | |
bb8f3c06 | 121 | if (!lhst) { |
70ddf80a PS |
122 | kfree_skb(skb); |
123 | BT_DRV_ERR("Invalid hci_st memory,freeing SKB"); | |
124 | BTDRV_API_EXIT(-EFAULT); | |
125 | return -EFAULT; | |
126 | } | |
bb8f3c06 | 127 | if (!test_bit(BT_DRV_RUNNING, &lhst->flags)) { |
70ddf80a PS |
128 | kfree_skb(skb); |
129 | BT_DRV_ERR("Device is not running,freeing SKB"); | |
130 | BTDRV_API_EXIT(-EINVAL); | |
131 | return -EINVAL; | |
132 | } | |
133 | ||
134 | len = skb->len; | |
bb8f3c06 | 135 | skb->dev = (struct net_device *)lhst->hdev; |
70ddf80a PS |
136 | |
137 | /* Forward skb to HCI CORE layer */ | |
138 | err = hci_recv_frame(skb); | |
139 | if (err) { | |
140 | kfree_skb(skb); | |
141 | BT_DRV_ERR("Unable to push skb to HCI CORE(%d),freeing SKB", | |
142 | err); | |
143 | BTDRV_API_EXIT(err); | |
144 | return err; | |
145 | } | |
bb8f3c06 | 146 | lhst->hdev->stat.byte_rx += len; |
70ddf80a PS |
147 | |
148 | BTDRV_API_EXIT(0); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | /* ------- Interfaces to HCI layer ------ */ | |
153 | ||
154 | /* Called from HCI core to initialize the device */ | |
155 | static int hci_st_open(struct hci_dev *hdev) | |
156 | { | |
157 | static struct st_proto_s hci_st_proto; | |
158 | unsigned long timeleft; | |
159 | int err; | |
160 | ||
161 | BTDRV_API_START(); | |
162 | ||
163 | err = 0; | |
164 | ||
165 | BT_DRV_DBG("%s %p", hdev->name, hdev); | |
166 | ||
167 | /* Already registered with ST ? */ | |
168 | if (test_bit(BT_ST_REGISTERED, &hst->flags)) { | |
169 | BT_DRV_ERR("Registered with ST already,open called again?"); | |
170 | BTDRV_API_EXIT(0); | |
171 | return 0; | |
172 | } | |
173 | ||
174 | /* Populate BT driver info required by ST */ | |
175 | memset(&hci_st_proto, 0, sizeof(hci_st_proto)); | |
176 | ||
177 | /* BT driver ID */ | |
178 | hci_st_proto.type = ST_BT; | |
179 | ||
180 | /* Receive function which called from ST */ | |
181 | hci_st_proto.recv = hci_st_receive; | |
182 | ||
183 | /* Packet match function may used in future */ | |
184 | hci_st_proto.match_packet = NULL; | |
185 | ||
186 | /* Callback to be called when registration is pending */ | |
187 | hci_st_proto.reg_complete_cb = hci_st_registration_completion_cb; | |
188 | ||
189 | /* This is write function pointer of ST. BT driver will make use of this | |
190 | * for sending any packets to chip. ST will assign and give to us, so | |
191 | * make it as NULL */ | |
192 | hci_st_proto.write = NULL; | |
193 | ||
bb8f3c06 PS |
194 | /* send in the hst to be received at registration complete callback |
195 | * and during st's receive | |
196 | */ | |
197 | hci_st_proto.priv_data = hst; | |
198 | ||
70ddf80a PS |
199 | /* Register with ST layer */ |
200 | err = st_register(&hci_st_proto); | |
320920cb | 201 | if (err == -EINPROGRESS) { |
70ddf80a PS |
202 | /* Prepare wait-for-completion handler data structures. |
203 | * Needed to syncronize this and st_registration_completion_cb() | |
204 | * functions. | |
205 | */ | |
206 | init_completion(&hst->wait_for_btdrv_reg_completion); | |
207 | ||
208 | /* Reset ST registration callback status flag , this value | |
209 | * will be updated in hci_st_registration_completion_cb() | |
210 | * function whenever it called from ST driver. | |
211 | */ | |
212 | hst->streg_cbdata = -EINPROGRESS; | |
213 | ||
214 | /* ST is busy with other protocol registration(may be busy with | |
215 | * firmware download).So,Wait till the registration callback | |
216 | * (passed as a argument to st_register() function) getting | |
217 | * called from ST. | |
218 | */ | |
219 | BT_DRV_DBG(" %s waiting for reg completion signal from ST", | |
220 | __func__); | |
221 | ||
222 | timeleft = | |
223 | wait_for_completion_timeout | |
224 | (&hst->wait_for_btdrv_reg_completion, | |
225 | msecs_to_jiffies(BT_REGISTER_TIMEOUT)); | |
226 | if (!timeleft) { | |
227 | BT_DRV_ERR("Timeout(%ld sec),didn't get reg" | |
228 | "completion signal from ST", | |
229 | BT_REGISTER_TIMEOUT / 1000); | |
230 | BTDRV_API_EXIT(-ETIMEDOUT); | |
231 | return -ETIMEDOUT; | |
232 | } | |
233 | ||
234 | /* Is ST registration callback called with ERROR value? */ | |
235 | if (hst->streg_cbdata != 0) { | |
236 | BT_DRV_ERR("ST reg completion CB called with invalid" | |
237 | "status %d", hst->streg_cbdata); | |
238 | BTDRV_API_EXIT(-EAGAIN); | |
239 | return -EAGAIN; | |
240 | } | |
241 | err = 0; | |
320920cb | 242 | } else if (err == -1) { |
70ddf80a PS |
243 | BT_DRV_ERR("st_register failed %d", err); |
244 | BTDRV_API_EXIT(-EAGAIN); | |
245 | return -EAGAIN; | |
246 | } | |
247 | ||
248 | /* Do we have proper ST write function? */ | |
249 | if (hci_st_proto.write != NULL) { | |
250 | /* We need this pointer for sending any Bluetooth pkts */ | |
251 | hst->st_write = hci_st_proto.write; | |
252 | } else { | |
253 | BT_DRV_ERR("failed to get ST write func pointer"); | |
254 | ||
255 | /* Undo registration with ST */ | |
256 | err = st_unregister(ST_BT); | |
257 | if (err < 0) | |
258 | BT_DRV_ERR("st_unregister failed %d", err); | |
259 | ||
260 | hst->st_write = NULL; | |
261 | BTDRV_API_EXIT(-EAGAIN); | |
262 | return -EAGAIN; | |
263 | } | |
264 | ||
265 | /* Registration with ST layer is completed successfully, | |
266 | * now chip is ready to accept commands from HCI CORE. | |
267 | * Mark HCI Device flag as RUNNING | |
268 | */ | |
269 | set_bit(HCI_RUNNING, &hdev->flags); | |
270 | ||
271 | /* Registration with ST successful */ | |
272 | set_bit(BT_ST_REGISTERED, &hst->flags); | |
273 | ||
274 | BTDRV_API_EXIT(err); | |
275 | return err; | |
276 | } | |
277 | ||
278 | /* Close device */ | |
279 | static int hci_st_close(struct hci_dev *hdev) | |
280 | { | |
281 | int err; | |
282 | ||
283 | BTDRV_API_START(); | |
284 | ||
285 | err = 0; | |
286 | ||
287 | /* Unregister from ST layer */ | |
288 | if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) { | |
289 | err = st_unregister(ST_BT); | |
320920cb | 290 | if (err != 0) { |
70ddf80a PS |
291 | BT_DRV_ERR("st_unregister failed %d", err); |
292 | BTDRV_API_EXIT(-EBUSY); | |
293 | return -EBUSY; | |
294 | } | |
295 | } | |
296 | ||
297 | hst->st_write = NULL; | |
298 | ||
299 | /* ST layer would have moved chip to inactive state. | |
300 | * So,clear HCI device RUNNING flag. | |
301 | */ | |
302 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) { | |
303 | BTDRV_API_EXIT(0); | |
304 | return 0; | |
305 | } | |
306 | ||
307 | BTDRV_API_EXIT(err); | |
308 | return err; | |
309 | } | |
310 | ||
311 | /* Called from HCI CORE , Sends frames to Shared Transport */ | |
312 | static int hci_st_send_frame(struct sk_buff *skb) | |
313 | { | |
314 | struct hci_dev *hdev; | |
315 | struct hci_st *hst; | |
316 | long len; | |
317 | ||
318 | BTDRV_API_START(); | |
319 | ||
320 | if (skb == NULL) { | |
321 | BT_DRV_ERR("Invalid skb received from HCI CORE"); | |
322 | BTDRV_API_EXIT(-ENOMEM); | |
323 | return -ENOMEM; | |
324 | } | |
325 | hdev = (struct hci_dev *)skb->dev; | |
326 | if (!hdev) { | |
327 | BT_DRV_ERR("SKB received for invalid HCI Device (hdev=NULL)"); | |
328 | BTDRV_API_EXIT(-ENODEV); | |
329 | return -ENODEV; | |
330 | } | |
331 | if (!test_bit(HCI_RUNNING, &hdev->flags)) { | |
332 | BT_DRV_ERR("Device is not running"); | |
333 | BTDRV_API_EXIT(-EBUSY); | |
334 | return -EBUSY; | |
335 | } | |
336 | ||
337 | hst = (struct hci_st *)hdev->driver_data; | |
338 | ||
339 | /* Prepend skb with frame type */ | |
340 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); | |
341 | ||
342 | BT_DRV_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, | |
343 | skb->len); | |
344 | ||
345 | /* Insert skb to shared transport layer's transmit queue. | |
346 | * Freeing skb memory is taken care in shared transport layer, | |
347 | * so don't free skb memory here. | |
348 | */ | |
349 | if (!hst->st_write) { | |
350 | kfree_skb(skb); | |
351 | BT_DRV_ERR(" Can't write to ST, st_write null?"); | |
352 | BTDRV_API_EXIT(-EAGAIN); | |
353 | return -EAGAIN; | |
354 | } | |
355 | len = hst->st_write(skb); | |
356 | if (len < 0) { | |
357 | /* Something went wrong in st write , free skb memory */ | |
358 | kfree_skb(skb); | |
359 | BT_DRV_ERR(" ST write failed (%ld)", len); | |
360 | BTDRV_API_EXIT(-EAGAIN); | |
361 | return -EAGAIN; | |
362 | } | |
363 | ||
364 | /* ST accepted our skb. So, Go ahead and do rest */ | |
365 | hdev->stat.byte_tx += len; | |
366 | hci_st_tx_complete(hst, bt_cb(skb)->pkt_type); | |
367 | ||
368 | BTDRV_API_EXIT(0); | |
369 | return 0; | |
370 | } | |
371 | ||
372 | static void hci_st_destruct(struct hci_dev *hdev) | |
373 | { | |
374 | BTDRV_API_START(); | |
375 | ||
376 | if (!hdev) { | |
377 | BT_DRV_ERR("Destruct called with invalid HCI Device" | |
378 | "(hdev=NULL)"); | |
379 | BTDRV_API_EXIT(0); | |
380 | return; | |
381 | } | |
382 | ||
383 | BT_DRV_DBG("%s", hdev->name); | |
384 | ||
385 | /* free hci_st memory */ | |
386 | if (hdev->driver_data != NULL) | |
387 | kfree(hdev->driver_data); | |
388 | ||
389 | BTDRV_API_EXIT(0); | |
390 | return; | |
391 | } | |
392 | ||
393 | /* Creates new HCI device */ | |
394 | static int hci_st_register_dev(struct hci_st *hst) | |
395 | { | |
396 | struct hci_dev *hdev; | |
397 | ||
398 | BTDRV_API_START(); | |
399 | ||
400 | /* Initialize and register HCI device */ | |
401 | hdev = hci_alloc_dev(); | |
402 | if (!hdev) { | |
403 | BT_DRV_ERR("Can't allocate HCI device"); | |
404 | BTDRV_API_EXIT(-ENOMEM); | |
405 | return -ENOMEM; | |
406 | } | |
407 | BT_DRV_DBG(" HCI device allocated. hdev= %p", hdev); | |
408 | ||
409 | hst->hdev = hdev; | |
410 | hdev->bus = HCI_UART; | |
411 | hdev->driver_data = hst; | |
412 | hdev->open = hci_st_open; | |
413 | hdev->close = hci_st_close; | |
414 | hdev->flush = NULL; | |
415 | hdev->send = hci_st_send_frame; | |
416 | hdev->destruct = hci_st_destruct; | |
417 | hdev->owner = THIS_MODULE; | |
418 | ||
419 | if (reset) | |
420 | set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); | |
421 | ||
422 | if (hci_register_dev(hdev) < 0) { | |
423 | BT_DRV_ERR("Can't register HCI device"); | |
424 | hci_free_dev(hdev); | |
425 | BTDRV_API_EXIT(-ENODEV); | |
426 | return -ENODEV; | |
427 | } | |
428 | ||
429 | BT_DRV_DBG(" HCI device registered. hdev= %p", hdev); | |
430 | BTDRV_API_EXIT(0); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | /* ------- Module Init interface ------ */ | |
435 | ||
436 | static int __init bt_drv_init(void) | |
437 | { | |
438 | int err; | |
439 | ||
440 | BTDRV_API_START(); | |
441 | ||
442 | err = 0; | |
443 | ||
444 | BT_DRV_DBG(" Bluetooth Driver Version %s", VERSION); | |
445 | ||
446 | /* Allocate local resource memory */ | |
447 | hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL); | |
448 | if (!hst) { | |
449 | BT_DRV_ERR("Can't allocate control structure"); | |
450 | BTDRV_API_EXIT(-ENFILE); | |
451 | return -ENFILE; | |
452 | } | |
453 | ||
454 | /* Expose "hciX" device to user space */ | |
455 | err = hci_st_register_dev(hst); | |
456 | if (err) { | |
457 | /* Release local resource memory */ | |
458 | kfree(hst); | |
459 | ||
460 | BT_DRV_ERR("Unable to expose hci0 device(%d)", err); | |
461 | BTDRV_API_EXIT(err); | |
462 | return err; | |
463 | } | |
464 | set_bit(BT_DRV_RUNNING, &hst->flags); | |
465 | ||
466 | BTDRV_API_EXIT(err); | |
467 | return err; | |
468 | } | |
469 | ||
470 | /* ------- Module Exit interface ------ */ | |
471 | ||
472 | static void __exit bt_drv_exit(void) | |
473 | { | |
474 | BTDRV_API_START(); | |
475 | ||
476 | /* Deallocate local resource's memory */ | |
477 | if (hst) { | |
478 | struct hci_dev *hdev = hst->hdev; | |
479 | ||
480 | if (hdev == NULL) { | |
481 | BT_DRV_ERR("Invalid hdev memory"); | |
482 | kfree(hst); | |
483 | } else { | |
484 | hci_st_close(hdev); | |
485 | if (test_and_clear_bit(BT_DRV_RUNNING, &hst->flags)) { | |
486 | /* Remove HCI device (hciX) created | |
487 | * in module init. | |
488 | */ | |
489 | hci_unregister_dev(hdev); | |
490 | ||
491 | /* Free HCI device memory */ | |
492 | hci_free_dev(hdev); | |
493 | } | |
494 | } | |
495 | } | |
496 | BTDRV_API_EXIT(0); | |
497 | } | |
498 | ||
499 | module_init(bt_drv_init); | |
500 | module_exit(bt_drv_exit); | |
501 | ||
502 | /* ------ Module Info ------ */ | |
503 | ||
504 | module_param(reset, bool, 0644); | |
505 | MODULE_PARM_DESC(reset, "Send HCI reset command on initialization"); | |
506 | MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>"); | |
507 | MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION); | |
508 | MODULE_VERSION(VERSION); | |
509 | MODULE_LICENSE("GPL"); |