video: sprdfd: disable ESD feature
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / bluetooth / hci_ldisc_bcm.c
1 /*
2  *
3  *  Bluetooth HCI UART driver
4  *
5  *  Copyright (C) 2000-2001  Qualcomm Incorporated
6  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
7  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
8  *
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  */
25
26 #include <linux/module.h>
27
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/fcntl.h>
32 #include <linux/interrupt.h>
33 #include <linux/ptrace.h>
34 #include <linux/poll.h>
35
36 #include <linux/slab.h>
37 #include <linux/tty.h>
38 #include <linux/errno.h>
39 #include <linux/string.h>
40 #include <linux/signal.h>
41 #include <linux/ioctl.h>
42 #include <linux/skbuff.h>
43
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
46
47 #include "hci_uart_bcm.h"
48 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
49 #include <linux/fm.h> // add for fm
50 #endif
51
52 #define VERSION "2.2"
53 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
54 #define PROTO_ENTRY(type, name) name
55 const unsigned char *protocol_strngs[] = {
56     PROTO_ENTRY(ST_BT, "Bluetooth"),
57     PROTO_ENTRY(ST_FM, "FM"),
58     PROTO_ENTRY(ST_GPS, "GPS"),
59 };
60
61 /*******************************************************************************
62 **  Local type definitions
63 *******************************************************************************/
64 struct hci_uart *g_hu = NULL;
65 spinlock_t  reg_lock;
66 static int is_print_reg_error = 1;
67 static unsigned long jiffi1, jiffi2;
68
69 static struct hci_st *hst;
70 #define BT_REGISTER_TIMEOUT   msecs_to_jiffies(6000)    /* 6 sec */
71 /*******************************************************************************
72 **  Function forward-declarations and Function callback declarations
73 *******************************************************************************/
74 static void hci_uart_tty_flush_buffer(struct tty_struct *tty);
75 #endif
76 static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
77
78 int hci_uart_register_proto(struct hci_uart_proto *p)
79 {
80         if (p->id >= HCI_UART_MAX_PROTO)
81                 return -EINVAL;
82
83         if (hup[p->id])
84                 return -EEXIST;
85
86         hup[p->id] = p;
87
88         return 0;
89 }
90
91 int hci_uart_unregister_proto(struct hci_uart_proto *p)
92 {
93         if (p->id >= HCI_UART_MAX_PROTO)
94                 return -EINVAL;
95
96         if (!hup[p->id])
97                 return -EINVAL;
98
99         hup[p->id] = NULL;
100
101         return 0;
102 }
103 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
104 // add for V4L2
105 void hci_uart_route_frame(enum proto_type protoid, struct hci_uart *hu,
106                     struct sk_buff *skb)
107 {
108     int err;
109
110     if (protoid == PROTO_SH_FM)
111         BT_INFO(" %s(prot:%d) ", __func__, protoid);
112     else
113         BT_DBG(" %s(prot:%d) ", __func__, protoid);
114
115     if (unlikely
116             (hu == NULL || skb == NULL
117                 || hu->list[protoid] == NULL))
118     {
119         BT_ERR("protocol %d not registered, no data to send?",
120                             protoid);
121         if (hu != NULL && skb != NULL)
122             kfree_skb(skb);
123
124         return;
125     }
126     /* this cannot fail
127     * this shouldn't take long
128     * - should be just skb_queue_tail for the
129     *   protocol stack driver
130     */
131     if (likely(hu->list[protoid]->recv != NULL))
132     {
133         err = hu->list[protoid]->recv(hu->list[protoid]->priv_data, skb);
134
135         if (unlikely(err != 0)) {
136                 BT_ERR(" proto stack %d's ->recv failed", protoid);
137
138                 if (protoid != PROTO_SH_BT)
139                         kfree_skb(skb);
140
141             return;
142         }
143     }
144     else
145     {
146         BT_ERR(" proto stack %d's ->recv null", protoid);
147         kfree_skb(skb);
148     }
149     return;
150 }
151 #endif
152 static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
153 {
154         if (id >= HCI_UART_MAX_PROTO)
155                 return NULL;
156
157         return hup[id];
158 }
159
160 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
161 {
162 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
163         struct hci_dev *hdev = hu->hdev;
164 #endif
165    /* Update HCI stat counters */
166    switch (pkt_type)
167    {
168 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
169                 case HCI_COMMAND_PKT:
170                         BT_DBG("HCI command packet txed");
171                         break;
172
173                 case HCI_ACLDATA_PKT:
174                         BT_DBG("HCI ACL DATA packet txed");
175                         break;
176
177                 case HCI_SCODATA_PKT:
178                         BT_DBG("HCI SCO DATA packet txed");
179                         break;
180 #else
181                 case HCI_COMMAND_PKT:
182                         hdev->stat.cmd_tx++;
183                         break;
184
185                 case HCI_ACLDATA_PKT:
186                         hdev->stat.acl_tx++;
187                         break;
188
189                 case HCI_SCODATA_PKT:
190                         hdev->stat.sco_tx++;
191                         break;
192 #endif
193         }
194 }
195
196 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
197 {
198         struct sk_buff *skb = hu->tx_skb;
199
200         if (!skb)
201                 skb = hu->proto->dequeue(hu);
202         else
203                 hu->tx_skb = NULL;
204
205         return skb;
206 }
207
208 int hci_uart_tx_wakeup(struct hci_uart *hu)
209 {
210         if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
211                 set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
212                 return 0;
213         }
214
215         BT_DBG("");
216
217         schedule_work(&hu->write_work);
218
219         return 0;
220 }
221
222 static void hci_uart_write_work(struct work_struct *work)
223 {
224         struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
225         struct tty_struct *tty = hu->tty;
226         struct hci_dev *hdev = hu->hdev;
227         struct sk_buff *skb;
228
229         /* REVISIT: should we cope with bad skbs or ->write() returning
230          * and error value ?
231          */
232
233 restart:
234         clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
235
236         while ((skb = hci_uart_dequeue(hu))) {
237                 int len;
238
239                 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
240
241       /* dump packet */
242 #if 0
243       for (i = 0; i < skb->len; i++)
244       {
245           BT_ERR ("%x ", skb->data[i]);
246       }
247       BT_ERR ("\n");
248 #endif
249
250                 len = tty->ops->write(tty, skb->data, skb->len);
251                 hdev->stat.byte_tx += len;
252
253                 skb_pull(skb, len);
254                 if (skb->len) {
255                         hu->tx_skb = skb;
256                         break;
257                 }
258
259                 hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
260                 kfree_skb(skb);
261         }
262
263         if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
264                 goto restart;
265
266         clear_bit(HCI_UART_SENDING, &hu->tx_state);
267 }
268
269 static void hci_uart_init_work(struct work_struct *work)
270 {
271         struct hci_uart *hu = container_of(work, struct hci_uart, init_ready);
272         int err;
273
274         if (!test_and_clear_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
275                 return;
276
277         err = hci_register_dev(hu->hdev);
278         if (err < 0) {
279                 BT_ERR("Can't register HCI device");
280                 hci_free_dev(hu->hdev);
281                 hu->hdev = NULL;
282                 hu->proto->close(hu);
283         }
284
285         set_bit(HCI_UART_REGISTERED, &hu->flags);
286 }
287
288 int hci_uart_init_ready(struct hci_uart *hu)
289 {
290         if (!test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
291                 return -EALREADY;
292
293         schedule_work(&hu->init_ready);
294
295         return 0;
296 }
297
298 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
299 /*******************************************************************************
300 **
301 ** Function - hci_sh_ldisc_register()
302 **
303 ** Description - Register protocol
304 **              called from upper layer protocol stack drivers (BT or FM)
305 **
306 ** Returns - 0 if success; errno otherwise
307 **
308 *******************************************************************************/
309 long hci_ldisc_register(struct sh_proto_s *new_proto)
310 {
311     long err = 0;
312     unsigned long flags, diff;
313     if(new_proto->type != 0)
314     {
315         BT_INFO("%s(%d) ", __func__, new_proto->type);
316     }
317     spin_lock_irqsave(&reg_lock, flags);
318     if (g_hu == NULL || g_hu->priv == NULL ||
319                 new_proto == NULL || new_proto->recv == NULL)
320     {
321                 spin_unlock_irqrestore(&reg_lock, flags);
322         if (is_print_reg_error)
323         {
324             BT_INFO("%s(%d) ", __func__, new_proto->type);
325             BT_ERR("g_hu/g_hu priv/new_proto/recv or reg_complete_cb not ready");
326             jiffi1 = jiffies;
327             is_print_reg_error = 0;
328         }
329         else
330         {
331             jiffi2 = jiffies;
332             diff = (long)jiffi2 - (long)jiffi1;
333             if ( ((diff *1000)/HZ) >= 1000)
334                 is_print_reg_error = 1;
335         }
336         return -1;
337     }
338
339     if (new_proto->type < PROTO_SH_BT || new_proto->type >= PROTO_SH_MAX)
340     {
341                 spin_unlock_irqrestore(&reg_lock, flags);
342         BT_ERR("protocol %d not supported", new_proto->type);
343         return -EPROTONOSUPPORT;
344     }
345
346     if (g_hu->list[new_proto->type] != NULL)
347     {
348                 spin_unlock_irqrestore(&reg_lock, flags);
349         BT_ERR("protocol %d already registered", new_proto->type);
350         return -EALREADY;
351     }
352
353     g_hu->list[new_proto->type] = new_proto;
354     g_hu->list[new_proto->type]->priv_data = new_proto->priv_data;
355     g_hu->protos_registered++;
356     new_proto->write = hci_ldisc_write;
357         spin_unlock_irqrestore(&reg_lock, flags);
358
359     BT_INFO("done %s(%d) ", __func__, new_proto->type);
360     return err;
361 }
362 EXPORT_SYMBOL(hci_ldisc_register);
363
364 /*******************************************************************************
365 **
366 ** Function - hci_sh_ldisc_unregister()
367 **
368 ** Description - UnRegister protocol
369 **              called from upper layer protocol stack drivers (BT or FM)
370 **
371 ** Returns - 0 if success; errno otherwise
372 **
373 *******************************************************************************/
374 long hci_ldisc_unregister(enum proto_type type)
375 {
376     long err = 0;
377     unsigned long flags;
378
379     BT_INFO("%s: %d ", __func__, type);
380
381     if (type < PROTO_SH_BT || type >= PROTO_SH_MAX)
382     {
383         BT_ERR(" protocol %d not supported", type);
384         return -EPROTONOSUPPORT;
385     }
386
387     spin_lock_irqsave(&reg_lock, flags);
388
389      if (g_hu == NULL || g_hu->list[type] == NULL)
390     {
391                 spin_unlock_irqrestore(&reg_lock, flags);
392         BT_ERR(" protocol %d not registered", type);
393         return -EPROTONOSUPPORT;
394     }
395
396     g_hu->protos_registered--;
397     g_hu->list[type] = NULL;
398
399     if (g_hu->tty)
400         hci_uart_tty_flush_buffer(g_hu->tty);
401
402     spin_unlock_irqrestore(&reg_lock, flags);
403
404     return err;
405 }
406 EXPORT_SYMBOL(hci_ldisc_unregister);
407
408
409 /*******************************************************************************
410 **
411 ** Function - brcm_sh_ldisc_write()
412 **
413 ** Description - Function to write to the shared line discipline driver
414 **              called from upper layer protocol stack drivers (BT or FM)
415 **              via the write function pointer
416 **
417 ** Returns - 0 if success; errno otherwise
418 **
419 *******************************************************************************/
420 long hci_ldisc_write(struct sk_buff *skb)
421 {
422     enum proto_type protoid = PROTO_SH_MAX;
423     long len;
424
425     struct hci_uart *hu = g_hu;
426
427     BT_DBG("%s", __func__);
428
429     if (unlikely(skb == NULL))
430     {
431         BT_ERR("data unavailable to perform write");
432         return -1;
433     }
434
435     if (unlikely(hu == NULL || hu->tty == NULL))
436     {
437         BT_ERR("tty unavailable to perform write");
438         return -1;
439     }
440
441     switch (sh_ldisc_cb(skb)->pkt_type)
442     {
443         case HCI_COMMAND_PKT:
444         case HCI_ACLDATA_PKT:
445         case HCI_SCODATA_PKT:
446             protoid = PROTO_SH_BT;
447             break;
448         case FM_CH8_PKT:
449             protoid = PROTO_SH_FM;
450             BT_INFO("%s: FM_CH8_PKT", __func__);
451             break;
452     }
453
454     if (unlikely(hu->list[protoid] == NULL))
455     {
456         BT_ERR(" protocol %d not registered, and writing? ",
457                             protoid);
458         return -1;
459     }
460
461     BT_DBG("%d to be written, type %d", skb->len, sh_ldisc_cb(skb)->pkt_type );
462     len = skb->len;
463
464     hu->proto->enqueue(hu, skb);
465
466     hci_uart_tx_wakeup(hu);
467
468     /* return number of bytes written */
469     return len;
470 }
471
472 static void hci_uart_tty_flush_buffer(struct tty_struct *tty)
473 {
474     struct hci_uart *hu = (void *)tty->disc_data;
475     unsigned long flags;
476     BT_DBG("%s", __func__);
477
478     if (!hu || tty != hu->tty)
479         return;
480
481     if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
482         return;
483
484     spin_lock_irqsave(&hu->rx_lock, flags);
485     hu->proto->flush(hu);
486     spin_unlock(&hu->rx_lock);
487
488     if(tty->ldisc->ops->flush_buffer)
489         tty->ldisc->ops->flush_buffer(tty);
490
491     return;
492 }
493
494 static void hci_st_registration_completion_cb(void *priv_data, char data)
495 {
496     struct hci_st *lhst = (struct hci_st *)priv_data;
497     BT_DBG("%s", __func__);
498
499     /* hci_st_open() function needs value of 'data' to know
500     * the registration status(success/fail),So have a back
501     * up of it.
502     */
503     lhst->streg_cbdata = data;
504
505     /* Got a feedback from ST for BT driver registration
506     * request.Wackup hci_st_open() function to continue
507     * it's open operation.
508     */
509     complete(&lhst->wait_for_btdrv_reg_completion);
510
511 }
512
513 /* Called by Shared Transport layer when receive data is
514  * available */
515 static long hci_st_receive(void *priv_data, struct sk_buff *skb)
516 {
517     int err;
518     int len;
519     struct hci_st *lhst = (struct hci_st *)priv_data;
520
521     BT_DBG("%s", __func__);
522
523     err = 0;
524     len = 0;
525
526     if (skb == NULL) {
527         BT_ERR("Invalid SKB received from ST");
528         return -EFAULT;
529     }
530     if (!lhst) {
531         kfree_skb(skb);
532         BT_ERR("Invalid hci_st memory,freeing SKB");
533         return -EFAULT;
534     }
535     if (!test_bit(BT_DRV_RUNNING, &lhst->flags)) {
536         kfree_skb(skb);
537         BT_ERR("Device is not running,freeing SKB");
538         return -EINVAL;
539     }
540
541     len = skb->len;
542     skb->dev = (struct net_device *)lhst->hdev;
543
544     /* Forward skb to HCI CORE layer */
545     err = hci_recv_frame(lhst->hdev, skb);
546     if (err) {
547         BT_ERR("Unable to push skb to HCI CORE(%d)",
548                                                     err);
549         return err;
550     }
551     lhst->hdev->stat.byte_rx += len;
552
553     return 0;
554 }
555 #endif
556 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
557 /* ------- Interface to HCI layer ------ */
558 /* Initialize device */
559 static int hci_uart_open(struct hci_dev *hdev)
560 {
561    static struct sh_proto_s hci_st_proto;
562    unsigned long timeleft;
563    unsigned long diff;
564
565    int err;
566
567    err = 0;
568
569    BT_DBG("%s %p", hdev->name, hdev);
570
571     /* Already registered with ST ? */
572     if (test_bit(BT_ST_REGISTERED, &hst->flags)) {
573         BT_ERR("Registered with ST already,open called again?");
574         return 0;
575     }
576
577     /* Populate BT driver info required by ST */
578     memset(&hci_st_proto, 0, sizeof(hci_st_proto));
579
580     /* BT driver ID */
581     hci_st_proto.type = PROTO_SH_BT;
582
583     /* Receive function which called from ST */
584     hci_st_proto.recv = hci_st_receive;
585
586     /* Packet match function may used in future */
587     hci_st_proto.match_packet = NULL;
588
589     /* Callback to be called when registration is pending */
590     hci_st_proto.reg_complete_cb = hci_st_registration_completion_cb;
591
592     /* This is write function pointer of ST. BT driver will make use of this
593     * for sending any packets to chip. ST will assign and give to us, so
594     * make it as NULL */
595     hci_st_proto.write = NULL;
596
597     /* send in the hst to be received at registration complete callback
598     * and during st's receive
599     */
600     hci_st_proto.priv_data = hst;
601
602     /* Register with ST layer */
603     err = hci_ldisc_register(&hci_st_proto);
604     if (err == -EINPROGRESS) {
605         /* Prepare wait-for-completion handler data structures.
606         * Needed to syncronize this and st_registration_completion_cb()
607         * functions.
608         */
609         init_completion(&hst->wait_for_btdrv_reg_completion);
610
611         /* Reset ST registration callback status flag , this value
612         * will be updated in hci_st_registration_completion_cb()
613         * function whenever it called from ST driver.
614         */
615         hst->streg_cbdata = -EINPROGRESS;
616
617         /* ST is busy with other protocol registration(may be busy with
618         * firmware download).So,Wait till the registration callback
619         * (passed as a argument to st_register() function) getting
620         * called from ST.
621         */
622         BT_ERR(" %s waiting for reg completion signal from ST",
623                                                             __func__);
624
625         timeleft =
626                 wait_for_completion_timeout
627                 (&hst->wait_for_btdrv_reg_completion,
628                             msecs_to_jiffies(BT_REGISTER_TIMEOUT));
629         if (!timeleft) {
630             BT_ERR("Timeout(%ld sec),didn't get reg"
631                                             "completion signal from ST",
632                                                 BT_REGISTER_TIMEOUT / 1000);
633             return -ETIMEDOUT;
634         }
635
636         /* Is ST registration callback called with ERROR value? */
637         if (hst->streg_cbdata != 0) {
638             BT_ERR("ST reg completion CB called with invalid"
639             "status %d", hst->streg_cbdata);
640             return -EAGAIN;
641         }
642         err = 0;
643     } else if (err == -1) {
644         if (is_print_reg_error)
645         {
646             BT_ERR("st_register failed %d", err);
647             jiffi1 = jiffies;
648             is_print_reg_error = 0;
649         }
650         else
651         {
652             jiffi2 = jiffies;
653             diff = (long)jiffi2 - (long)jiffi1;
654             if ( ((diff *1000)/HZ) >= 1000)
655                 is_print_reg_error = 1;
656         }
657         return -EAGAIN;
658     }
659
660     /* Do we have proper ST write function? */
661     if (hci_st_proto.write != NULL) {
662         /* We need this pointer for sending any Bluetooth pkts */
663         hst->st_write = hci_st_proto.write;
664     } else {
665         BT_ERR("failed to get ST write func pointer");
666
667         /* Undo registration with ST */
668         err = hci_ldisc_unregister(PROTO_SH_BT);
669         if (err < 0)
670             BT_ERR("st_unregister failed %d", err);
671
672         hst->st_write = NULL;
673         return -EAGAIN;
674     }
675
676         /* BEGIN TIZEN_Bluetooth */
677         /* FIXME: in order to wait enough until uart init complete,
678            the below routine is added.
679         */
680
681         msleep(100);
682         /* END TIZEN_Bluetooth */
683
684
685     /* Registration with ST layer is completed successfully,
686     * now chip is ready to accept commands from HCI CORE.
687     * Mark HCI Device flag as RUNNING
688     */
689     set_bit(HCI_RUNNING, &hdev->flags);
690
691     /* Registration with ST successful */
692     set_bit(BT_ST_REGISTERED, &hst->flags);
693
694     return err;
695 }
696 #else
697 /* ------- Interface to HCI layer ------ */
698 /* Initialize device */
699 static int hci_uart_open(struct hci_dev *hdev)
700 {
701         BT_DBG("%s %p", hdev->name, hdev);
702
703         /* Nothing to do for UART driver */
704
705         set_bit(HCI_RUNNING, &hdev->flags);
706
707         return 0;
708 }
709 #endif
710
711 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
712 /* Reset device */
713 static int hci_uart_flush(struct hci_dev *hdev)
714 {
715         struct hci_uart *hu  = hci_get_drvdata(hdev);
716         struct tty_struct *tty = hu->tty;
717
718         BT_DBG("hdev %p tty %p", hdev, tty);
719
720         if (hu->tx_skb) {
721                 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
722         }
723
724         /* Flush any pending characters in the driver and discipline. */
725         tty_ldisc_flush(tty);
726         tty_driver_flush_buffer(tty);
727
728         if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
729                 hu->proto->flush(hu);
730
731         return 0;
732 }
733 #endif
734
735 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
736 /* Close device */
737 static int hci_uart_close(struct hci_dev *hdev)
738 {
739     int err;
740
741     BT_DBG("hdev %p", hdev);
742
743     err = 0;
744
745     /* Unregister from ST layer */
746     if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
747         err = hci_ldisc_unregister(PROTO_SH_BT);
748         if (err != 0) {
749             BT_ERR("st_unregister failed %d", err);
750             return -EBUSY;
751         }
752     }
753
754     hst->st_write = NULL;
755
756     /* ST layer would have moved chip to inactive state.
757     * So,clear HCI device RUNNING flag.
758     */
759     if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
760         return 0;
761     }
762     return err;
763
764 }
765
766 /* Increments HCI counters based on pocket ID (cmd,acl,sco) */
767 static inline void hci_st_tx_complete(struct hci_st *hst, int pkt_type)
768 {
769     struct hci_dev *hdev;
770
771     hdev = hst->hdev;
772
773     /* Update HCI stat counters */
774     switch (pkt_type) {
775         case HCI_COMMAND_PKT:
776             hdev->stat.cmd_tx++;
777             break;
778
779         case HCI_ACLDATA_PKT:
780             hdev->stat.acl_tx++;
781             break;
782
783         case HCI_SCODATA_PKT:
784             hdev->stat.cmd_tx++;
785             break;
786     }
787
788 }
789
790
791 /* Send frames from HCI layer */
792 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
793 {
794     struct hci_st *hst;
795     long len;
796
797     if (skb == NULL) {
798         BT_ERR("Invalid skb received from HCI CORE");
799         return -ENOMEM;
800     }
801
802     if (!hdev) {
803         BT_ERR("SKB received for invalid HCI Device (hdev=NULL)");
804         return -ENODEV;
805     }
806     if (!test_bit(HCI_RUNNING, &hdev->flags)) {
807         BT_ERR("Device is not running");
808         return -EBUSY;
809     }
810
811     hst = hci_get_drvdata(hdev);
812
813     /* Prepend skb with frame type */
814     memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
815
816     BT_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
817                                                 skb->len);
818
819     /* Insert skb to shared transport layer's transmit queue.
820     * Freeing skb memory is taken care in shared transport layer,
821     * so don't free skb memory here.
822     */
823     if (!hst->st_write) {
824         kfree_skb(skb);
825         BT_ERR(" Can't write to ST, st_write null?");
826         return -EAGAIN;
827     }
828     len = hst->st_write(skb);
829     if (len < 0) {
830         /* Something went wrong in st write , free skb memory */
831         kfree_skb(skb);
832         BT_ERR(" ST write failed (%ld)", len);
833         return -EAGAIN;
834     }
835
836     /* ST accepted our skb. So, Go ahead and do rest */
837     hdev->stat.byte_tx += len;
838     hci_st_tx_complete(hst, bt_cb(skb)->pkt_type);
839
840     return 0;
841
842 }
843 #else
844 /* Close device */
845 static int hci_uart_close(struct hci_dev *hdev)
846 {
847         BT_DBG("hdev %p", hdev);
848
849         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
850                 return 0;
851
852         hci_uart_flush(hdev);
853         hdev->flush = NULL;
854         return 0;
855 }
856
857 /* Send frames from HCI layer */
858 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
859 {
860         struct hci_uart *hu = hci_get_drvdata(hdev);
861
862         if (!test_bit(HCI_RUNNING, &hdev->flags))
863                 return -EBUSY;
864
865         BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
866
867         hu->proto->enqueue(hu, skb);
868
869         hci_uart_tx_wakeup(hu);
870
871         return 0;
872 }
873
874 #endif
875 /* ------ LDISC part ------ */
876 /* hci_uart_tty_open
877  *
878  *     Called when line discipline changed to HCI_UART.
879  *
880  * Arguments:
881  *     tty    pointer to tty info structure
882  * Return Value:
883  *     0 if success, otherwise error code
884  */
885 static int hci_uart_tty_open(struct tty_struct *tty)
886 {
887 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
888    struct hci_uart *hu = (void *) tty->disc_data;
889    unsigned long flags;
890    BT_INFO("tty open %p", tty);
891
892    if (hu)
893        return -EEXIST;
894 #else
895     struct hci_uart *hu;
896         BT_DBG("tty %p", tty);
897 #endif
898         /* Error if the tty has no write op instead of leaving an exploitable
899            hole */
900         if (tty->ops->write == NULL)
901                 return -EOPNOTSUPP;
902
903         hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
904         if (!hu) {
905                 BT_ERR("Can't allocate control structure");
906                 return -ENFILE;
907         }
908
909         tty->disc_data = hu;
910         hu->tty = tty;
911         tty->receive_room = 65536;
912 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
913    spin_lock_irqsave(&reg_lock, flags);
914    g_hu = hu;
915    spin_unlock_irqrestore(&reg_lock, flags);
916 #endif
917         INIT_WORK(&hu->init_ready, hci_uart_init_work);
918         INIT_WORK(&hu->write_work, hci_uart_write_work);
919
920         spin_lock_init(&hu->rx_lock);
921
922         /* Flush any pending characters in the driver and line discipline. */
923
924         /* FIXME: why is this needed. Note don't use ldisc_ref here as the
925            open path is before the ldisc is referencable */
926
927         if (tty->ldisc->ops->flush_buffer)
928                 tty->ldisc->ops->flush_buffer(tty);
929         tty_driver_flush_buffer(tty);
930
931         return 0;
932 }
933 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
934 /* hci_uart_tty_close()
935  *
936  *    Called when the line discipline is changed to something
937  *    else, the tty is closed, or the tty detects a hangup.
938  */
939 static void hci_uart_tty_close(struct tty_struct *tty)
940 {
941    struct hci_uart *hu = (void *)tty->disc_data;
942    struct hci_dev *hdev = hu->hdev;
943
944    int i;
945    unsigned long flags;
946
947    BT_INFO("tty close %p", tty);
948
949    /* Detach from the tty */
950    tty->disc_data = NULL;
951
952    if (tty->ldisc->ops->flush_buffer)
953      tty->ldisc->ops->flush_buffer(tty);
954    tty_driver_flush_buffer(tty);
955
956    if (hu)
957    {
958      if (hdev)
959         hci_uart_close(hdev);
960
961      cancel_work_sync(&hu->write_work);
962
963      if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags))
964      {
965          if (hdev) {
966             if (test_bit(HCI_UART_REGISTERED, &hu->flags))
967                hci_unregister_dev(hdev);
968             hci_free_dev(hdev);
969          }
970          hu->proto->close(hu);
971          for (i = PROTO_SH_BT; i < PROTO_SH_MAX; i++)
972          {
973              if (hu->list[i] != NULL)
974              BT_ERR("%d not un-registered", i);
975          }
976          kfree(hu);
977      }
978    }
979
980    spin_lock_irqsave(&reg_lock, flags);
981    g_hu = NULL;
982    spin_unlock_irqrestore(&reg_lock, flags);
983 }
984 #else
985 static void hci_uart_tty_close(struct tty_struct *tty)
986 {
987         struct hci_uart *hu = (void *)tty->disc_data;
988         struct hci_dev *hdev;
989
990         BT_DBG("tty %p", tty);
991
992         /* Detach from the tty */
993         tty->disc_data = NULL;
994
995         if (!hu)
996                 return;
997
998         hdev = hu->hdev;
999         if (hdev)
1000                 hci_uart_close(hdev);
1001
1002         cancel_work_sync(&hu->write_work);
1003
1004         if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1005                 if (hdev) {
1006                         if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1007                                 hci_unregister_dev(hdev);
1008                         hci_free_dev(hdev);
1009                 }
1010                 hu->proto->close(hu);
1011         }
1012
1013         kfree(hu);
1014 }
1015 #endif
1016 /* hci_uart_tty_wakeup()
1017  *
1018  *    Callback for transmit wakeup. Called when low level
1019  *    device driver can accept more send data.
1020  *
1021  * Arguments:        tty    pointer to associated tty instance data
1022  * Return Value:    None
1023  */
1024 static void hci_uart_tty_wakeup(struct tty_struct *tty)
1025 {
1026         struct hci_uart *hu = (void *)tty->disc_data;
1027
1028         BT_DBG("");
1029
1030         if (!hu)
1031                 return;
1032
1033         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1034
1035         if (tty != hu->tty)
1036                 return;
1037
1038         if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1039                 hci_uart_tx_wakeup(hu);
1040 }
1041
1042 /* hci_uart_tty_receive()
1043  *
1044  *     Called by tty low level driver when receive data is
1045  *     available.
1046  *
1047  * Arguments:  tty          pointer to tty isntance data
1048  *             data         pointer to received data
1049  *             flags        pointer to flags for data
1050  *             count        count of received data in bytes
1051  *
1052  * Return Value:    None
1053  */
1054 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
1055 {
1056         struct hci_uart *hu = (void *)tty->disc_data;
1057
1058         if (!hu || tty != hu->tty)
1059                 return;
1060
1061         if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
1062                 return;
1063
1064         spin_lock(&hu->rx_lock);
1065         hu->proto->recv(hu, (void *) data, count);
1066
1067 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
1068         if (hu->hdev)
1069                 hu->hdev->stat.byte_rx += count;
1070 #endif
1071         spin_unlock(&hu->rx_lock);
1072
1073         tty_unthrottle(tty);
1074 }
1075
1076 static int hci_uart_register_dev(struct hci_uart *hu)
1077 {
1078         struct hci_dev *hdev;
1079
1080         BT_DBG("");
1081 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1082         BT_ERR("hci_uart_register_dev");
1083
1084    /* Allocate local resource memory */
1085    hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
1086    if (!hst) {
1087        BT_ERR("Can't allocate control structure");
1088        return -ENFILE;
1089    }
1090 #endif
1091
1092         /* Initialize and register HCI device */
1093         hdev = hci_alloc_dev();
1094         if (!hdev) {
1095                 BT_ERR("Can't allocate HCI device");
1096                 return -ENOMEM;
1097         }
1098
1099         hu->hdev = hdev;
1100
1101         hdev->bus = HCI_UART;
1102 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1103    hst->hdev = hdev;
1104
1105    hci_set_drvdata(hdev, hst);
1106 #else
1107         hci_set_drvdata(hdev, hu);
1108 #endif
1109         hdev->open  = hci_uart_open;
1110         hdev->close = hci_uart_close;
1111 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1112         hdev->flush = NULL;
1113 #else
1114     hdev->flush = hci_uart_flush;
1115 #endif
1116         hdev->send  = hci_uart_send_frame;
1117         SET_HCIDEV_DEV(hdev, hu->tty->dev);
1118
1119         if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
1120                 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1121
1122         if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
1123                 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
1124
1125         if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
1126                 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1127
1128         if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
1129                 hdev->dev_type = HCI_AMP;
1130         else
1131                 hdev->dev_type = HCI_BREDR;
1132
1133         if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
1134                 return 0;
1135
1136         if (hci_register_dev(hdev) < 0) {
1137                 BT_ERR("Can't register HCI device");
1138                 hci_free_dev(hdev);
1139                 return -ENODEV;
1140         }
1141
1142         set_bit(HCI_UART_REGISTERED, &hu->flags);
1143 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1144    set_bit(BT_DRV_RUNNING, &hst->flags);
1145 #endif
1146         return 0;
1147 }
1148
1149 static int hci_uart_set_proto(struct hci_uart *hu, int id)
1150 {
1151         struct hci_uart_proto *p;
1152         int err;
1153
1154         p = hci_uart_get_proto(id);
1155         if (!p)
1156                 return -EPROTONOSUPPORT;
1157
1158         err = p->open(hu);
1159         if (err)
1160                 return err;
1161
1162         hu->proto = p;
1163
1164         err = hci_uart_register_dev(hu);
1165         if (err) {
1166                 p->close(hu);
1167                 return err;
1168         }
1169
1170         return 0;
1171 }
1172
1173 static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
1174 {
1175         unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
1176                                     BIT(HCI_UART_RESET_ON_INIT) |
1177                                     BIT(HCI_UART_CREATE_AMP) |
1178                                     BIT(HCI_UART_INIT_PENDING) |
1179                                     BIT(HCI_UART_EXT_CONFIG);
1180
1181         if ((flags & ~valid_flags))
1182                 return -EINVAL;
1183
1184         hu->hdev_flags = flags;
1185
1186         return 0;
1187 }
1188
1189 /* hci_uart_tty_ioctl()
1190  *
1191  *    Process IOCTL system call for the tty device.
1192  *
1193  * Arguments:
1194  *
1195  *    tty        pointer to tty instance data
1196  *    file       pointer to open file object for device
1197  *    cmd        IOCTL command code
1198  *    arg        argument for IOCTL call (cmd dependent)
1199  *
1200  * Return Value:    Command dependent
1201  */
1202 static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
1203                                         unsigned int cmd, unsigned long arg)
1204 {
1205         struct hci_uart *hu = (void *)tty->disc_data;
1206         int err = 0;
1207
1208         BT_DBG("");
1209
1210         /* Verify the status of the device */
1211         if (!hu)
1212                 return -EBADF;
1213
1214         switch (cmd) {
1215         case HCIUARTSETPROTO:
1216                 if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1217                         err = hci_uart_set_proto(hu, arg);
1218                         if (err) {
1219                                 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
1220                                 return err;
1221                         }
1222                 } else
1223                         return -EBUSY;
1224                 break;
1225
1226         case HCIUARTGETPROTO:
1227                 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1228                         return hu->proto->id;
1229                 return -EUNATCH;
1230
1231         case HCIUARTGETDEVICE:
1232                 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1233                         return hu->hdev->id;
1234                 return -EUNATCH;
1235
1236         case HCIUARTSETFLAGS:
1237                 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1238                         return -EBUSY;
1239                 err = hci_uart_set_flags(hu, arg);
1240                 if (err)
1241                         return err;
1242                 break;
1243
1244         case HCIUARTGETFLAGS:
1245                 return hu->hdev_flags;
1246
1247         default:
1248                 err = n_tty_ioctl_helper(tty, file, cmd, arg);
1249                 break;
1250         }
1251
1252         return err;
1253 }
1254
1255 /*
1256  * We don't provide read/write/poll interface for user space.
1257  */
1258 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
1259                                         unsigned char __user *buf, size_t nr)
1260 {
1261         return 0;
1262 }
1263
1264 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
1265                                         const unsigned char *data, size_t count)
1266 {
1267         return 0;
1268 }
1269
1270 static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
1271                                         struct file *filp, poll_table *wait)
1272 {
1273         return 0;
1274 }
1275
1276 static int __init hci_uart_init(void)
1277 {
1278         static struct tty_ldisc_ops hci_uart_ldisc;
1279         int err;
1280
1281         BT_INFO("HCI UART driver ver %s", VERSION);
1282
1283         /* Register the tty discipline */
1284
1285         memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
1286         hci_uart_ldisc.magic            = TTY_LDISC_MAGIC;
1287         hci_uart_ldisc.name             = "n_hci";
1288         hci_uart_ldisc.open             = hci_uart_tty_open;
1289         hci_uart_ldisc.close            = hci_uart_tty_close;
1290         hci_uart_ldisc.read             = hci_uart_tty_read;
1291         hci_uart_ldisc.write            = hci_uart_tty_write;
1292         hci_uart_ldisc.ioctl            = hci_uart_tty_ioctl;
1293         hci_uart_ldisc.poll             = hci_uart_tty_poll;
1294         hci_uart_ldisc.receive_buf      = hci_uart_tty_receive;
1295         hci_uart_ldisc.write_wakeup     = hci_uart_tty_wakeup;
1296         hci_uart_ldisc.owner            = THIS_MODULE;
1297
1298         err = tty_register_ldisc(N_HCI, &hci_uart_ldisc);
1299         if (err) {
1300                 BT_ERR("HCI line discipline registration failed. (%d)", err);
1301                 return err;
1302         }
1303
1304 #ifdef CONFIG_BT_HCI_BCM_UART
1305         h4_init();
1306 #endif
1307 #ifdef CONFIG_BT_HCIUART_BCSP
1308         bcsp_init();
1309 #endif
1310 #ifdef CONFIG_BT_HCIUART_LL
1311         ll_init();
1312 #endif
1313 #ifdef CONFIG_BT_HCIUART_ATH3K
1314         ath_init();
1315 #endif
1316 #ifdef CONFIG_BT_HCIUART_3WIRE
1317         h5_init();
1318 #endif
1319 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1320    /* initialize register lock spinlock */
1321    spin_lock_init(&reg_lock);
1322 #endif
1323         return 0;
1324 }
1325
1326 static void __exit hci_uart_exit(void)
1327 {
1328         int err;
1329
1330 #ifdef CONFIG_BT_HCI_BCM_UART
1331         h4_deinit();
1332 #endif
1333 #ifdef CONFIG_BT_HCIUART_BCSP
1334         bcsp_deinit();
1335 #endif
1336 #ifdef CONFIG_BT_HCIUART_LL
1337         ll_deinit();
1338 #endif
1339 #ifdef CONFIG_BT_HCIUART_ATH3K
1340         ath_deinit();
1341 #endif
1342 #ifdef CONFIG_BT_HCIUART_3WIRE
1343         h5_deinit();
1344 #endif
1345
1346         /* Release tty registration of line discipline */
1347         err = tty_unregister_ldisc(N_HCI);
1348         if (err)
1349                 BT_ERR("Can't unregister HCI line discipline (%d)", err);
1350 }
1351
1352 module_init(hci_uart_init);
1353 module_exit(hci_uart_exit);
1354
1355 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1356 MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
1357 MODULE_VERSION(VERSION);
1358 MODULE_LICENSE("GPL");
1359 MODULE_ALIAS_LDISC(N_HCI);