90fdd6b16e04236a18039a17c8ac11014629719d
[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     /* Registration with ST layer is completed successfully,
677     * now chip is ready to accept commands from HCI CORE.
678     * Mark HCI Device flag as RUNNING
679     */
680     set_bit(HCI_RUNNING, &hdev->flags);
681
682     /* Registration with ST successful */
683     set_bit(BT_ST_REGISTERED, &hst->flags);
684
685     return err;
686 }
687 #else
688 /* ------- Interface to HCI layer ------ */
689 /* Initialize device */
690 static int hci_uart_open(struct hci_dev *hdev)
691 {
692         BT_DBG("%s %p", hdev->name, hdev);
693
694         /* Nothing to do for UART driver */
695
696         set_bit(HCI_RUNNING, &hdev->flags);
697
698         return 0;
699 }
700 #endif
701
702 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
703 /* Reset device */
704 static int hci_uart_flush(struct hci_dev *hdev)
705 {
706         struct hci_uart *hu  = hci_get_drvdata(hdev);
707         struct tty_struct *tty = hu->tty;
708
709         BT_DBG("hdev %p tty %p", hdev, tty);
710
711         if (hu->tx_skb) {
712                 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
713         }
714
715         /* Flush any pending characters in the driver and discipline. */
716         tty_ldisc_flush(tty);
717         tty_driver_flush_buffer(tty);
718
719         if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
720                 hu->proto->flush(hu);
721
722         return 0;
723 }
724 #endif
725
726 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
727 /* Close device */
728 static int hci_uart_close(struct hci_dev *hdev)
729 {
730     int err;
731
732     BT_DBG("hdev %p", hdev);
733
734     err = 0;
735
736     /* Unregister from ST layer */
737     if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
738         err = hci_ldisc_unregister(PROTO_SH_BT);
739         if (err != 0) {
740             BT_ERR("st_unregister failed %d", err);
741             return -EBUSY;
742         }
743     }
744
745     hst->st_write = NULL;
746
747     /* ST layer would have moved chip to inactive state.
748     * So,clear HCI device RUNNING flag.
749     */
750     if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
751         return 0;
752     }
753     return err;
754
755 }
756
757 /* Increments HCI counters based on pocket ID (cmd,acl,sco) */
758 static inline void hci_st_tx_complete(struct hci_st *hst, int pkt_type)
759 {
760     struct hci_dev *hdev;
761
762     hdev = hst->hdev;
763
764     /* Update HCI stat counters */
765     switch (pkt_type) {
766         case HCI_COMMAND_PKT:
767             hdev->stat.cmd_tx++;
768             break;
769
770         case HCI_ACLDATA_PKT:
771             hdev->stat.acl_tx++;
772             break;
773
774         case HCI_SCODATA_PKT:
775             hdev->stat.cmd_tx++;
776             break;
777     }
778
779 }
780
781
782 /* Send frames from HCI layer */
783 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
784 {
785     struct hci_st *hst;
786     long len;
787
788     if (skb == NULL) {
789         BT_ERR("Invalid skb received from HCI CORE");
790         return -ENOMEM;
791     }
792
793     if (!hdev) {
794         BT_ERR("SKB received for invalid HCI Device (hdev=NULL)");
795         return -ENODEV;
796     }
797     if (!test_bit(HCI_RUNNING, &hdev->flags)) {
798         BT_ERR("Device is not running");
799         return -EBUSY;
800     }
801
802     hst = hci_get_drvdata(hdev);
803
804     /* Prepend skb with frame type */
805     memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
806
807     BT_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
808                                                 skb->len);
809
810     /* Insert skb to shared transport layer's transmit queue.
811     * Freeing skb memory is taken care in shared transport layer,
812     * so don't free skb memory here.
813     */
814     if (!hst->st_write) {
815         kfree_skb(skb);
816         BT_ERR(" Can't write to ST, st_write null?");
817         return -EAGAIN;
818     }
819     len = hst->st_write(skb);
820     if (len < 0) {
821         /* Something went wrong in st write , free skb memory */
822         kfree_skb(skb);
823         BT_ERR(" ST write failed (%ld)", len);
824         return -EAGAIN;
825     }
826
827     /* ST accepted our skb. So, Go ahead and do rest */
828     hdev->stat.byte_tx += len;
829     hci_st_tx_complete(hst, bt_cb(skb)->pkt_type);
830
831     return 0;
832
833 }
834 #else
835 /* Close device */
836 static int hci_uart_close(struct hci_dev *hdev)
837 {
838         BT_DBG("hdev %p", hdev);
839
840         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
841                 return 0;
842
843         hci_uart_flush(hdev);
844         hdev->flush = NULL;
845         return 0;
846 }
847
848 /* Send frames from HCI layer */
849 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
850 {
851         struct hci_uart *hu = hci_get_drvdata(hdev);
852
853         if (!test_bit(HCI_RUNNING, &hdev->flags))
854                 return -EBUSY;
855
856         BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
857
858         hu->proto->enqueue(hu, skb);
859
860         hci_uart_tx_wakeup(hu);
861
862         return 0;
863 }
864
865 #endif
866 /* ------ LDISC part ------ */
867 /* hci_uart_tty_open
868  *
869  *     Called when line discipline changed to HCI_UART.
870  *
871  * Arguments:
872  *     tty    pointer to tty info structure
873  * Return Value:
874  *     0 if success, otherwise error code
875  */
876 static int hci_uart_tty_open(struct tty_struct *tty)
877 {
878 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
879    struct hci_uart *hu = (void *) tty->disc_data;
880    unsigned long flags;
881    BT_INFO("tty open %p", tty);
882
883    if (hu)
884        return -EEXIST;
885 #else
886     struct hci_uart *hu;
887         BT_DBG("tty %p", tty);
888 #endif
889         /* Error if the tty has no write op instead of leaving an exploitable
890            hole */
891         if (tty->ops->write == NULL)
892                 return -EOPNOTSUPP;
893
894         hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
895         if (!hu) {
896                 BT_ERR("Can't allocate control structure");
897                 return -ENFILE;
898         }
899
900         tty->disc_data = hu;
901         hu->tty = tty;
902         tty->receive_room = 65536;
903 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
904    spin_lock_irqsave(&reg_lock, flags);
905    g_hu = hu;
906    spin_unlock_irqrestore(&reg_lock, flags);
907 #endif
908         INIT_WORK(&hu->init_ready, hci_uart_init_work);
909         INIT_WORK(&hu->write_work, hci_uart_write_work);
910
911         spin_lock_init(&hu->rx_lock);
912
913         /* Flush any pending characters in the driver and line discipline. */
914
915         /* FIXME: why is this needed. Note don't use ldisc_ref here as the
916            open path is before the ldisc is referencable */
917
918         if (tty->ldisc->ops->flush_buffer)
919                 tty->ldisc->ops->flush_buffer(tty);
920         tty_driver_flush_buffer(tty);
921
922         return 0;
923 }
924 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
925 /* hci_uart_tty_close()
926  *
927  *    Called when the line discipline is changed to something
928  *    else, the tty is closed, or the tty detects a hangup.
929  */
930 static void hci_uart_tty_close(struct tty_struct *tty)
931 {
932    struct hci_uart *hu = (void *)tty->disc_data;
933         struct hci_dev *hdev;
934
935    int i;
936    unsigned long flags;
937
938    BT_INFO("tty close %p", tty);
939
940    /* Detach from the tty */
941    tty->disc_data = NULL;
942
943    if (tty->ldisc->ops->flush_buffer)
944      tty->ldisc->ops->flush_buffer(tty);
945    tty_driver_flush_buffer(tty);
946
947    if (hu)
948    {
949      if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags))
950      {
951          hdev = hu->hdev;
952          if (hdev) {
953             if (test_bit(HCI_UART_REGISTERED, &hu->flags))
954                hci_unregister_dev(hdev);
955             hci_free_dev(hdev);
956          }
957          hu->proto->close(hu);
958          for (i = PROTO_SH_BT; i < PROTO_SH_MAX; i++)
959          {
960              if (hu->list[i] != NULL)
961              BT_ERR("%d not un-registered", i);
962          }
963          kfree(hu);
964      }
965    }
966
967    spin_lock_irqsave(&reg_lock, flags);
968    g_hu = NULL;
969    spin_unlock_irqrestore(&reg_lock, flags);
970 }
971 #else
972 static void hci_uart_tty_close(struct tty_struct *tty)
973 {
974         struct hci_uart *hu = (void *)tty->disc_data;
975         struct hci_dev *hdev;
976
977         BT_DBG("tty %p", tty);
978
979         /* Detach from the tty */
980         tty->disc_data = NULL;
981
982         if (!hu)
983                 return;
984
985         hdev = hu->hdev;
986         if (hdev)
987                 hci_uart_close(hdev);
988
989         cancel_work_sync(&hu->write_work);
990
991         if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
992                 if (hdev) {
993                         if (test_bit(HCI_UART_REGISTERED, &hu->flags))
994                                 hci_unregister_dev(hdev);
995                         hci_free_dev(hdev);
996                 }
997                 hu->proto->close(hu);
998         }
999
1000         kfree(hu);
1001 }
1002 #endif
1003 /* hci_uart_tty_wakeup()
1004  *
1005  *    Callback for transmit wakeup. Called when low level
1006  *    device driver can accept more send data.
1007  *
1008  * Arguments:        tty    pointer to associated tty instance data
1009  * Return Value:    None
1010  */
1011 static void hci_uart_tty_wakeup(struct tty_struct *tty)
1012 {
1013         struct hci_uart *hu = (void *)tty->disc_data;
1014
1015         BT_DBG("");
1016
1017         if (!hu)
1018                 return;
1019
1020         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1021
1022         if (tty != hu->tty)
1023                 return;
1024
1025         if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1026                 hci_uart_tx_wakeup(hu);
1027 }
1028
1029 /* hci_uart_tty_receive()
1030  *
1031  *     Called by tty low level driver when receive data is
1032  *     available.
1033  *
1034  * Arguments:  tty          pointer to tty isntance data
1035  *             data         pointer to received data
1036  *             flags        pointer to flags for data
1037  *             count        count of received data in bytes
1038  *
1039  * Return Value:    None
1040  */
1041 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
1042 {
1043         struct hci_uart *hu = (void *)tty->disc_data;
1044
1045         if (!hu || tty != hu->tty)
1046                 return;
1047
1048         if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
1049                 return;
1050
1051         spin_lock(&hu->rx_lock);
1052         hu->proto->recv(hu, (void *) data, count);
1053
1054 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
1055         if (hu->hdev)
1056                 hu->hdev->stat.byte_rx += count;
1057 #endif
1058         spin_unlock(&hu->rx_lock);
1059
1060         tty_unthrottle(tty);
1061 }
1062
1063 static int hci_uart_register_dev(struct hci_uart *hu)
1064 {
1065         struct hci_dev *hdev;
1066
1067         BT_DBG("");
1068 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1069         BT_ERR("hci_uart_register_dev");
1070
1071    /* Allocate local resource memory */
1072    hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
1073    if (!hst) {
1074        BT_ERR("Can't allocate control structure");
1075        return -ENFILE;
1076    }
1077 #endif
1078
1079         /* Initialize and register HCI device */
1080         hdev = hci_alloc_dev();
1081         if (!hdev) {
1082                 BT_ERR("Can't allocate HCI device");
1083                 return -ENOMEM;
1084         }
1085
1086         hu->hdev = hdev;
1087
1088         hdev->bus = HCI_UART;
1089 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1090    hst->hdev = hdev;
1091
1092    hci_set_drvdata(hdev, hst);
1093 #else
1094         hci_set_drvdata(hdev, hu);
1095 #endif
1096         hdev->open  = hci_uart_open;
1097         hdev->close = hci_uart_close;
1098 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1099         hdev->flush = NULL;
1100 #else
1101     hdev->flush = hci_uart_flush;
1102 #endif
1103         hdev->send  = hci_uart_send_frame;
1104         SET_HCIDEV_DEV(hdev, hu->tty->dev);
1105
1106         if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
1107                 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1108
1109         if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
1110                 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
1111
1112         if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
1113                 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1114
1115         if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
1116                 hdev->dev_type = HCI_AMP;
1117         else
1118                 hdev->dev_type = HCI_BREDR;
1119
1120         if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
1121                 return 0;
1122
1123         if (hci_register_dev(hdev) < 0) {
1124                 BT_ERR("Can't register HCI device");
1125                 hci_free_dev(hdev);
1126                 return -ENODEV;
1127         }
1128
1129         set_bit(HCI_UART_REGISTERED, &hu->flags);
1130 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1131    set_bit(BT_DRV_RUNNING, &hst->flags);
1132 #endif
1133         return 0;
1134 }
1135
1136 static int hci_uart_set_proto(struct hci_uart *hu, int id)
1137 {
1138         struct hci_uart_proto *p;
1139         int err;
1140
1141         p = hci_uart_get_proto(id);
1142         if (!p)
1143                 return -EPROTONOSUPPORT;
1144
1145         err = p->open(hu);
1146         if (err)
1147                 return err;
1148
1149         hu->proto = p;
1150
1151         err = hci_uart_register_dev(hu);
1152         if (err) {
1153                 p->close(hu);
1154                 return err;
1155         }
1156
1157         return 0;
1158 }
1159
1160 static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
1161 {
1162         unsigned long valid_flags = BIT(HCI_UART_RAW_DEVICE) |
1163                                     BIT(HCI_UART_RESET_ON_INIT) |
1164                                     BIT(HCI_UART_CREATE_AMP) |
1165                                     BIT(HCI_UART_INIT_PENDING) |
1166                                     BIT(HCI_UART_EXT_CONFIG);
1167
1168         if ((flags & ~valid_flags))
1169                 return -EINVAL;
1170
1171         hu->hdev_flags = flags;
1172
1173         return 0;
1174 }
1175
1176 /* hci_uart_tty_ioctl()
1177  *
1178  *    Process IOCTL system call for the tty device.
1179  *
1180  * Arguments:
1181  *
1182  *    tty        pointer to tty instance data
1183  *    file       pointer to open file object for device
1184  *    cmd        IOCTL command code
1185  *    arg        argument for IOCTL call (cmd dependent)
1186  *
1187  * Return Value:    Command dependent
1188  */
1189 static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
1190                                         unsigned int cmd, unsigned long arg)
1191 {
1192         struct hci_uart *hu = (void *)tty->disc_data;
1193         int err = 0;
1194
1195         BT_DBG("");
1196
1197         /* Verify the status of the device */
1198         if (!hu)
1199                 return -EBADF;
1200
1201         switch (cmd) {
1202         case HCIUARTSETPROTO:
1203                 if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1204                         err = hci_uart_set_proto(hu, arg);
1205                         if (err) {
1206                                 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
1207                                 return err;
1208                         }
1209                 } else
1210                         return -EBUSY;
1211                 break;
1212
1213         case HCIUARTGETPROTO:
1214                 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1215                         return hu->proto->id;
1216                 return -EUNATCH;
1217
1218         case HCIUARTGETDEVICE:
1219                 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1220                         return hu->hdev->id;
1221                 return -EUNATCH;
1222
1223         case HCIUARTSETFLAGS:
1224                 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1225                         return -EBUSY;
1226                 err = hci_uart_set_flags(hu, arg);
1227                 if (err)
1228                         return err;
1229                 break;
1230
1231         case HCIUARTGETFLAGS:
1232                 return hu->hdev_flags;
1233
1234         default:
1235                 err = n_tty_ioctl_helper(tty, file, cmd, arg);
1236                 break;
1237         }
1238
1239         return err;
1240 }
1241
1242 /*
1243  * We don't provide read/write/poll interface for user space.
1244  */
1245 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
1246                                         unsigned char __user *buf, size_t nr)
1247 {
1248         return 0;
1249 }
1250
1251 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
1252                                         const unsigned char *data, size_t count)
1253 {
1254         return 0;
1255 }
1256
1257 static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
1258                                         struct file *filp, poll_table *wait)
1259 {
1260         return 0;
1261 }
1262
1263 static int __init hci_uart_init(void)
1264 {
1265         static struct tty_ldisc_ops hci_uart_ldisc;
1266         int err;
1267
1268         BT_INFO("HCI UART driver ver %s", VERSION);
1269
1270         /* Register the tty discipline */
1271
1272         memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
1273         hci_uart_ldisc.magic            = TTY_LDISC_MAGIC;
1274         hci_uart_ldisc.name             = "n_hci";
1275         hci_uart_ldisc.open             = hci_uart_tty_open;
1276         hci_uart_ldisc.close            = hci_uart_tty_close;
1277         hci_uart_ldisc.read             = hci_uart_tty_read;
1278         hci_uart_ldisc.write            = hci_uart_tty_write;
1279         hci_uart_ldisc.ioctl            = hci_uart_tty_ioctl;
1280         hci_uart_ldisc.poll             = hci_uart_tty_poll;
1281         hci_uart_ldisc.receive_buf      = hci_uart_tty_receive;
1282         hci_uart_ldisc.write_wakeup     = hci_uart_tty_wakeup;
1283         hci_uart_ldisc.owner            = THIS_MODULE;
1284
1285         err = tty_register_ldisc(N_HCI, &hci_uart_ldisc);
1286         if (err) {
1287                 BT_ERR("HCI line discipline registration failed. (%d)", err);
1288                 return err;
1289         }
1290
1291 #ifdef CONFIG_BT_HCI_BCM_UART
1292         h4_init();
1293 #endif
1294 #ifdef CONFIG_BT_HCIUART_BCSP
1295         bcsp_init();
1296 #endif
1297 #ifdef CONFIG_BT_HCIUART_LL
1298         ll_init();
1299 #endif
1300 #ifdef CONFIG_BT_HCIUART_ATH3K
1301         ath_init();
1302 #endif
1303 #ifdef CONFIG_BT_HCIUART_3WIRE
1304         h5_init();
1305 #endif
1306 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1307    /* initialize register lock spinlock */
1308    spin_lock_init(&reg_lock);
1309 #endif
1310         return 0;
1311 }
1312
1313 static void __exit hci_uart_exit(void)
1314 {
1315         int err;
1316
1317 #ifdef CONFIG_BT_HCI_BCM_UART
1318         h4_deinit();
1319 #endif
1320 #ifdef CONFIG_BT_HCIUART_BCSP
1321         bcsp_deinit();
1322 #endif
1323 #ifdef CONFIG_BT_HCIUART_LL
1324         ll_deinit();
1325 #endif
1326 #ifdef CONFIG_BT_HCIUART_ATH3K
1327         ath_deinit();
1328 #endif
1329 #ifdef CONFIG_BT_HCIUART_3WIRE
1330         h5_deinit();
1331 #endif
1332
1333         /* Release tty registration of line discipline */
1334         err = tty_unregister_ldisc(N_HCI);
1335         if (err)
1336                 BT_ERR("Can't unregister HCI line discipline (%d)", err);
1337 }
1338
1339 module_init(hci_uart_init);
1340 module_exit(hci_uart_exit);
1341
1342 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1343 MODULE_DESCRIPTION("Bluetooth HCI UART driver ver " VERSION);
1344 MODULE_VERSION(VERSION);
1345 MODULE_LICENSE("GPL");
1346 MODULE_ALIAS_LDISC(N_HCI);