]> Pileus Git - ~andy/linux/blob - drivers/staging/ti-st/bt_drv.c
Staging: bluetooth: BT driver using ST for TI combo devices
[~andy/linux] / drivers / staging / ti-st / bt_drv.c
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
27 #include "st.h"
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  */
83 static void hci_st_registration_completion_cb(char data)
84 {
85         BTDRV_API_START();
86
87         /* hci_st_open() function needs value of 'data' to know
88          * the registration status(success/fail),So have a back
89          * up of it.
90          */
91         hst->streg_cbdata = data;
92
93         /* Got a feedback from ST for BT driver registration
94          * request.Wackup hci_st_open() function to continue
95          * it's open operation.
96          */
97         complete(&hst->wait_for_btdrv_reg_completion);
98
99         BTDRV_API_EXIT(0);
100 }
101
102 /* Called by Shared Transport layer when receive data is
103  * available */
104 static long hci_st_receive(struct sk_buff *skb)
105 {
106         int err;
107         int len;
108
109         BTDRV_API_START();
110
111         err = 0;
112         len = 0;
113
114         if (skb == NULL) {
115                 BT_DRV_ERR("Invalid SKB received from ST");
116                 BTDRV_API_EXIT(-EFAULT);
117                 return -EFAULT;
118         }
119         if (!hst) {
120                 kfree_skb(skb);
121                 BT_DRV_ERR("Invalid hci_st memory,freeing SKB");
122                 BTDRV_API_EXIT(-EFAULT);
123                 return -EFAULT;
124         }
125         if (!test_bit(BT_DRV_RUNNING, &hst->flags)) {
126                 kfree_skb(skb);
127                 BT_DRV_ERR("Device is not running,freeing SKB");
128                 BTDRV_API_EXIT(-EINVAL);
129                 return -EINVAL;
130         }
131
132         len = skb->len;
133         skb->dev = (struct net_device *)hst->hdev;
134
135         /* Forward skb to HCI CORE layer */
136         err = hci_recv_frame(skb);
137         if (err) {
138                 kfree_skb(skb);
139                 BT_DRV_ERR("Unable to push skb to HCI CORE(%d),freeing SKB",
140                            err);
141                 BTDRV_API_EXIT(err);
142                 return err;
143         }
144         hst->hdev->stat.byte_rx += len;
145
146         BTDRV_API_EXIT(0);
147         return 0;
148 }
149
150 /* ------- Interfaces to HCI layer ------ */
151
152 /* Called from HCI core to initialize the device */
153 static int hci_st_open(struct hci_dev *hdev)
154 {
155         static struct st_proto_s hci_st_proto;
156         unsigned long timeleft;
157         int err;
158
159         BTDRV_API_START();
160
161         err = 0;
162
163         BT_DRV_DBG("%s %p", hdev->name, hdev);
164
165         /* Already registered with ST ? */
166         if (test_bit(BT_ST_REGISTERED, &hst->flags)) {
167                 BT_DRV_ERR("Registered with ST already,open called again?");
168                 BTDRV_API_EXIT(0);
169                 return 0;
170         }
171
172         /* Populate BT driver info required by ST */
173         memset(&hci_st_proto, 0, sizeof(hci_st_proto));
174
175         /* BT driver ID */
176         hci_st_proto.type = ST_BT;
177
178         /* Receive function which called from ST */
179         hci_st_proto.recv = hci_st_receive;
180
181         /* Packet match function may used in future */
182         hci_st_proto.match_packet = NULL;
183
184         /* Callback to be called when registration is pending */
185         hci_st_proto.reg_complete_cb = hci_st_registration_completion_cb;
186
187         /* This is write function pointer of ST. BT driver will make use of this
188          * for sending any packets to chip. ST will assign and give to us, so
189          * make it as NULL */
190         hci_st_proto.write = NULL;
191
192         /* Register with ST layer */
193         err = st_register(&hci_st_proto);
194         if (err == ST_ERR_PENDING) {
195                 /* Prepare wait-for-completion handler data structures.
196                  * Needed to syncronize this and st_registration_completion_cb()
197                  * functions.
198                  */
199                 init_completion(&hst->wait_for_btdrv_reg_completion);
200
201                 /* Reset ST registration callback status flag , this value
202                  * will be updated in hci_st_registration_completion_cb()
203                  * function whenever it called from ST driver.
204                  */
205                 hst->streg_cbdata = -EINPROGRESS;
206
207                 /* ST is busy with other protocol registration(may be busy with
208                  * firmware download).So,Wait till the registration callback
209                  * (passed as a argument to st_register() function) getting
210                  * called from ST.
211                  */
212                 BT_DRV_DBG(" %s waiting for reg completion signal from ST",
213                            __func__);
214
215                 timeleft =
216                     wait_for_completion_timeout
217                     (&hst->wait_for_btdrv_reg_completion,
218                      msecs_to_jiffies(BT_REGISTER_TIMEOUT));
219                 if (!timeleft) {
220                         BT_DRV_ERR("Timeout(%ld sec),didn't get reg"
221                                    "completion signal from ST",
222                                    BT_REGISTER_TIMEOUT / 1000);
223                         BTDRV_API_EXIT(-ETIMEDOUT);
224                         return -ETIMEDOUT;
225                 }
226
227                 /* Is ST registration callback called with ERROR value? */
228                 if (hst->streg_cbdata != 0) {
229                         BT_DRV_ERR("ST reg completion CB called with invalid"
230                                    "status %d", hst->streg_cbdata);
231                         BTDRV_API_EXIT(-EAGAIN);
232                         return -EAGAIN;
233                 }
234                 err = 0;
235         } else if (err == ST_ERR_FAILURE) {
236                 BT_DRV_ERR("st_register failed %d", err);
237                 BTDRV_API_EXIT(-EAGAIN);
238                 return -EAGAIN;
239         }
240
241         /* Do we have proper ST write function? */
242         if (hci_st_proto.write != NULL) {
243                 /* We need this pointer for sending any Bluetooth pkts */
244                 hst->st_write = hci_st_proto.write;
245         } else {
246                 BT_DRV_ERR("failed to get ST write func pointer");
247
248                 /* Undo registration with ST */
249                 err = st_unregister(ST_BT);
250                 if (err < 0)
251                         BT_DRV_ERR("st_unregister failed %d", err);
252
253                 hst->st_write = NULL;
254                 BTDRV_API_EXIT(-EAGAIN);
255                 return -EAGAIN;
256         }
257
258         /* Registration with ST layer is completed successfully,
259          * now chip is ready to accept commands from HCI CORE.
260          * Mark HCI Device flag as RUNNING
261          */
262         set_bit(HCI_RUNNING, &hdev->flags);
263
264         /* Registration with ST successful */
265         set_bit(BT_ST_REGISTERED, &hst->flags);
266
267         BTDRV_API_EXIT(err);
268         return err;
269 }
270
271 /* Close device */
272 static int hci_st_close(struct hci_dev *hdev)
273 {
274         int err;
275
276         BTDRV_API_START();
277
278         err = 0;
279
280         /* Unregister from ST layer */
281         if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
282                 err = st_unregister(ST_BT);
283                 if (err != ST_SUCCESS) {
284                         BT_DRV_ERR("st_unregister failed %d", err);
285                         BTDRV_API_EXIT(-EBUSY);
286                         return -EBUSY;
287                 }
288         }
289
290         hst->st_write = NULL;
291
292         /* ST layer would have moved chip to inactive state.
293          * So,clear HCI device RUNNING flag.
294          */
295         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
296                 BTDRV_API_EXIT(0);
297                 return 0;
298         }
299
300         BTDRV_API_EXIT(err);
301         return err;
302 }
303
304 /* Called from HCI CORE , Sends frames to Shared Transport */
305 static int hci_st_send_frame(struct sk_buff *skb)
306 {
307         struct hci_dev *hdev;
308         struct hci_st *hst;
309         long len;
310
311         BTDRV_API_START();
312
313         if (skb == NULL) {
314                 BT_DRV_ERR("Invalid skb received from HCI CORE");
315                 BTDRV_API_EXIT(-ENOMEM);
316                 return -ENOMEM;
317         }
318         hdev = (struct hci_dev *)skb->dev;
319         if (!hdev) {
320                 BT_DRV_ERR("SKB received for invalid HCI Device (hdev=NULL)");
321                 BTDRV_API_EXIT(-ENODEV);
322                 return -ENODEV;
323         }
324         if (!test_bit(HCI_RUNNING, &hdev->flags)) {
325                 BT_DRV_ERR("Device is not running");
326                 BTDRV_API_EXIT(-EBUSY);
327                 return -EBUSY;
328         }
329
330         hst = (struct hci_st *)hdev->driver_data;
331
332         /* Prepend skb with frame type */
333         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
334
335         BT_DRV_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
336                    skb->len);
337
338         /* Insert skb to shared transport layer's transmit queue.
339          * Freeing skb memory is taken care in shared transport layer,
340          * so don't free skb memory here.
341          */
342         if (!hst->st_write) {
343                 kfree_skb(skb);
344                 BT_DRV_ERR(" Can't write to ST, st_write null?");
345                 BTDRV_API_EXIT(-EAGAIN);
346                 return -EAGAIN;
347         }
348         len = hst->st_write(skb);
349         if (len < 0) {
350                 /* Something went wrong in st write , free skb memory */
351                 kfree_skb(skb);
352                 BT_DRV_ERR(" ST write failed (%ld)", len);
353                 BTDRV_API_EXIT(-EAGAIN);
354                 return -EAGAIN;
355         }
356
357         /* ST accepted our skb. So, Go ahead and do rest */
358         hdev->stat.byte_tx += len;
359         hci_st_tx_complete(hst, bt_cb(skb)->pkt_type);
360
361         BTDRV_API_EXIT(0);
362         return 0;
363 }
364
365 static void hci_st_destruct(struct hci_dev *hdev)
366 {
367         BTDRV_API_START();
368
369         if (!hdev) {
370                 BT_DRV_ERR("Destruct called with invalid HCI Device"
371                            "(hdev=NULL)");
372                 BTDRV_API_EXIT(0);
373                 return;
374         }
375
376         BT_DRV_DBG("%s", hdev->name);
377
378         /* free hci_st memory */
379         if (hdev->driver_data != NULL)
380                 kfree(hdev->driver_data);
381
382         BTDRV_API_EXIT(0);
383         return;
384 }
385
386 /* Creates new HCI device */
387 static int hci_st_register_dev(struct hci_st *hst)
388 {
389         struct hci_dev *hdev;
390
391         BTDRV_API_START();
392
393         /* Initialize and register HCI device */
394         hdev = hci_alloc_dev();
395         if (!hdev) {
396                 BT_DRV_ERR("Can't allocate HCI device");
397                 BTDRV_API_EXIT(-ENOMEM);
398                 return -ENOMEM;
399         }
400         BT_DRV_DBG(" HCI device allocated. hdev= %p", hdev);
401
402         hst->hdev = hdev;
403         hdev->bus = HCI_UART;
404         hdev->driver_data = hst;
405         hdev->open = hci_st_open;
406         hdev->close = hci_st_close;
407         hdev->flush = NULL;
408         hdev->send = hci_st_send_frame;
409         hdev->destruct = hci_st_destruct;
410         hdev->owner = THIS_MODULE;
411
412         if (reset)
413                 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
414
415         if (hci_register_dev(hdev) < 0) {
416                 BT_DRV_ERR("Can't register HCI device");
417                 hci_free_dev(hdev);
418                 BTDRV_API_EXIT(-ENODEV);
419                 return -ENODEV;
420         }
421
422         BT_DRV_DBG(" HCI device registered. hdev= %p", hdev);
423         BTDRV_API_EXIT(0);
424         return 0;
425 }
426
427 /* ------- Module Init interface ------ */
428
429 static int __init bt_drv_init(void)
430 {
431         int err;
432
433         BTDRV_API_START();
434
435         err = 0;
436
437         BT_DRV_DBG(" Bluetooth Driver Version %s", VERSION);
438
439         /* Allocate local resource memory */
440         hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
441         if (!hst) {
442                 BT_DRV_ERR("Can't allocate control structure");
443                 BTDRV_API_EXIT(-ENFILE);
444                 return -ENFILE;
445         }
446
447         /* Expose "hciX" device to user space */
448         err = hci_st_register_dev(hst);
449         if (err) {
450                 /* Release local resource memory */
451                 kfree(hst);
452
453                 BT_DRV_ERR("Unable to expose hci0 device(%d)", err);
454                 BTDRV_API_EXIT(err);
455                 return err;
456         }
457         set_bit(BT_DRV_RUNNING, &hst->flags);
458
459         BTDRV_API_EXIT(err);
460         return err;
461 }
462
463 /* ------- Module Exit interface ------ */
464
465 static void __exit bt_drv_exit(void)
466 {
467         BTDRV_API_START();
468
469         /* Deallocate local resource's memory  */
470         if (hst) {
471                 struct hci_dev *hdev = hst->hdev;
472
473                 if (hdev == NULL) {
474                         BT_DRV_ERR("Invalid hdev memory");
475                         kfree(hst);
476                 } else {
477                         hci_st_close(hdev);
478                         if (test_and_clear_bit(BT_DRV_RUNNING, &hst->flags)) {
479                                 /* Remove HCI device (hciX) created
480                                  * in module init.
481                                  */
482                                 hci_unregister_dev(hdev);
483
484                                 /* Free HCI device memory */
485                                 hci_free_dev(hdev);
486                         }
487                 }
488         }
489         BTDRV_API_EXIT(0);
490 }
491
492 module_init(bt_drv_init);
493 module_exit(bt_drv_exit);
494
495 /* ------ Module Info ------ */
496
497 module_param(reset, bool, 0644);
498 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
499 MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
500 MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
501 MODULE_VERSION(VERSION);
502 MODULE_LICENSE("GPL");