3 * Bluetooth HCI UART driver
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>
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.
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.
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
26 #include <linux/module.h>
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>
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>
44 #include <net/bluetooth/bluetooth.h>
45 #include <net/bluetooth/hci_core.h>
47 #include "hci_uart_bcm.h"
48 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
49 #include <linux/fm.h> // add for fm
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"),
61 /*******************************************************************************
62 ** Local type definitions
63 *******************************************************************************/
64 struct hci_uart *g_hu = NULL;
66 static int is_print_reg_error = 1;
67 static unsigned long jiffi1, jiffi2;
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);
76 static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
78 int hci_uart_register_proto(struct hci_uart_proto *p)
80 if (p->id >= HCI_UART_MAX_PROTO)
91 int hci_uart_unregister_proto(struct hci_uart_proto *p)
93 if (p->id >= HCI_UART_MAX_PROTO)
103 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
105 void hci_uart_route_frame(enum proto_type protoid, struct hci_uart *hu,
110 if (protoid == PROTO_SH_FM)
111 BT_INFO(" %s(prot:%d) ", __func__, protoid);
113 BT_DBG(" %s(prot:%d) ", __func__, protoid);
116 (hu == NULL || skb == NULL
117 || hu->list[protoid] == NULL))
119 BT_ERR("protocol %d not registered, no data to send?",
121 if (hu != NULL && skb != NULL)
127 * this shouldn't take long
128 * - should be just skb_queue_tail for the
129 * protocol stack driver
131 if (likely(hu->list[protoid]->recv != NULL))
133 err = hu->list[protoid]->recv(hu->list[protoid]->priv_data, skb);
135 if (unlikely(err != 0)) {
136 BT_ERR(" proto stack %d's ->recv failed", protoid);
138 if (protoid != PROTO_SH_BT)
146 BT_ERR(" proto stack %d's ->recv null", protoid);
152 static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
154 if (id >= HCI_UART_MAX_PROTO)
160 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
162 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
163 struct hci_dev *hdev = hu->hdev;
165 /* Update HCI stat counters */
168 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
169 case HCI_COMMAND_PKT:
170 BT_DBG("HCI command packet txed");
173 case HCI_ACLDATA_PKT:
174 BT_DBG("HCI ACL DATA packet txed");
177 case HCI_SCODATA_PKT:
178 BT_DBG("HCI SCO DATA packet txed");
181 case HCI_COMMAND_PKT:
185 case HCI_ACLDATA_PKT:
189 case HCI_SCODATA_PKT:
196 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
198 struct sk_buff *skb = hu->tx_skb;
201 skb = hu->proto->dequeue(hu);
208 int hci_uart_tx_wakeup(struct hci_uart *hu)
210 if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
211 set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
217 schedule_work(&hu->write_work);
222 static void hci_uart_write_work(struct work_struct *work)
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;
229 /* REVISIT: should we cope with bad skbs or ->write() returning
234 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
236 while ((skb = hci_uart_dequeue(hu))) {
239 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
243 for (i = 0; i < skb->len; i++)
245 BT_ERR ("%x ", skb->data[i]);
250 len = tty->ops->write(tty, skb->data, skb->len);
251 hdev->stat.byte_tx += len;
259 hci_uart_tx_complete(hu, bt_cb(skb)->pkt_type);
263 if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
266 clear_bit(HCI_UART_SENDING, &hu->tx_state);
269 static void hci_uart_init_work(struct work_struct *work)
271 struct hci_uart *hu = container_of(work, struct hci_uart, init_ready);
274 if (!test_and_clear_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
277 err = hci_register_dev(hu->hdev);
279 BT_ERR("Can't register HCI device");
280 hci_free_dev(hu->hdev);
282 hu->proto->close(hu);
285 set_bit(HCI_UART_REGISTERED, &hu->flags);
288 int hci_uart_init_ready(struct hci_uart *hu)
290 if (!test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
293 schedule_work(&hu->init_ready);
298 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
299 /*******************************************************************************
301 ** Function - hci_sh_ldisc_register()
303 ** Description - Register protocol
304 ** called from upper layer protocol stack drivers (BT or FM)
306 ** Returns - 0 if success; errno otherwise
308 *******************************************************************************/
309 long hci_ldisc_register(struct sh_proto_s *new_proto)
312 unsigned long flags, diff;
313 if(new_proto->type != 0)
315 BT_INFO("%s(%d) ", __func__, new_proto->type);
317 spin_lock_irqsave(®_lock, flags);
318 if (g_hu == NULL || g_hu->priv == NULL ||
319 new_proto == NULL || new_proto->recv == NULL)
321 spin_unlock_irqrestore(®_lock, flags);
322 if (is_print_reg_error)
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");
327 is_print_reg_error = 0;
332 diff = (long)jiffi2 - (long)jiffi1;
333 if ( ((diff *1000)/HZ) >= 1000)
334 is_print_reg_error = 1;
339 if (new_proto->type < PROTO_SH_BT || new_proto->type >= PROTO_SH_MAX)
341 spin_unlock_irqrestore(®_lock, flags);
342 BT_ERR("protocol %d not supported", new_proto->type);
343 return -EPROTONOSUPPORT;
346 if (g_hu->list[new_proto->type] != NULL)
348 spin_unlock_irqrestore(®_lock, flags);
349 BT_ERR("protocol %d already registered", new_proto->type);
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(®_lock, flags);
359 BT_INFO("done %s(%d) ", __func__, new_proto->type);
362 EXPORT_SYMBOL(hci_ldisc_register);
364 /*******************************************************************************
366 ** Function - hci_sh_ldisc_unregister()
368 ** Description - UnRegister protocol
369 ** called from upper layer protocol stack drivers (BT or FM)
371 ** Returns - 0 if success; errno otherwise
373 *******************************************************************************/
374 long hci_ldisc_unregister(enum proto_type type)
379 BT_INFO("%s: %d ", __func__, type);
381 if (type < PROTO_SH_BT || type >= PROTO_SH_MAX)
383 BT_ERR(" protocol %d not supported", type);
384 return -EPROTONOSUPPORT;
387 spin_lock_irqsave(®_lock, flags);
389 if (g_hu == NULL || g_hu->list[type] == NULL)
391 spin_unlock_irqrestore(®_lock, flags);
392 BT_ERR(" protocol %d not registered", type);
393 return -EPROTONOSUPPORT;
396 g_hu->protos_registered--;
397 g_hu->list[type] = NULL;
400 hci_uart_tty_flush_buffer(g_hu->tty);
402 spin_unlock_irqrestore(®_lock, flags);
406 EXPORT_SYMBOL(hci_ldisc_unregister);
409 /*******************************************************************************
411 ** Function - brcm_sh_ldisc_write()
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
417 ** Returns - 0 if success; errno otherwise
419 *******************************************************************************/
420 long hci_ldisc_write(struct sk_buff *skb)
422 enum proto_type protoid = PROTO_SH_MAX;
425 struct hci_uart *hu = g_hu;
427 BT_DBG("%s", __func__);
429 if (unlikely(skb == NULL))
431 BT_ERR("data unavailable to perform write");
435 if (unlikely(hu == NULL || hu->tty == NULL))
437 BT_ERR("tty unavailable to perform write");
441 switch (sh_ldisc_cb(skb)->pkt_type)
443 case HCI_COMMAND_PKT:
444 case HCI_ACLDATA_PKT:
445 case HCI_SCODATA_PKT:
446 protoid = PROTO_SH_BT;
449 protoid = PROTO_SH_FM;
450 BT_INFO("%s: FM_CH8_PKT", __func__);
454 if (unlikely(hu->list[protoid] == NULL))
456 BT_ERR(" protocol %d not registered, and writing? ",
461 BT_DBG("%d to be written, type %d", skb->len, sh_ldisc_cb(skb)->pkt_type );
464 hu->proto->enqueue(hu, skb);
466 hci_uart_tx_wakeup(hu);
468 /* return number of bytes written */
472 static void hci_uart_tty_flush_buffer(struct tty_struct *tty)
474 struct hci_uart *hu = (void *)tty->disc_data;
476 BT_DBG("%s", __func__);
478 if (!hu || tty != hu->tty)
481 if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
484 spin_lock_irqsave(&hu->rx_lock, flags);
485 hu->proto->flush(hu);
486 spin_unlock(&hu->rx_lock);
488 if(tty->ldisc->ops->flush_buffer)
489 tty->ldisc->ops->flush_buffer(tty);
494 static void hci_st_registration_completion_cb(void *priv_data, char data)
496 struct hci_st *lhst = (struct hci_st *)priv_data;
497 BT_DBG("%s", __func__);
499 /* hci_st_open() function needs value of 'data' to know
500 * the registration status(success/fail),So have a back
503 lhst->streg_cbdata = data;
505 /* Got a feedback from ST for BT driver registration
506 * request.Wackup hci_st_open() function to continue
507 * it's open operation.
509 complete(&lhst->wait_for_btdrv_reg_completion);
513 /* Called by Shared Transport layer when receive data is
515 static long hci_st_receive(void *priv_data, struct sk_buff *skb)
519 struct hci_st *lhst = (struct hci_st *)priv_data;
521 BT_DBG("%s", __func__);
527 BT_ERR("Invalid SKB received from ST");
532 BT_ERR("Invalid hci_st memory,freeing SKB");
535 if (!test_bit(BT_DRV_RUNNING, &lhst->flags)) {
537 BT_ERR("Device is not running,freeing SKB");
542 skb->dev = (struct net_device *)lhst->hdev;
544 /* Forward skb to HCI CORE layer */
545 err = hci_recv_frame(lhst->hdev, skb);
547 BT_ERR("Unable to push skb to HCI CORE(%d)",
551 lhst->hdev->stat.byte_rx += len;
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)
561 static struct sh_proto_s hci_st_proto;
562 unsigned long timeleft;
569 BT_DBG("%s %p", hdev->name, hdev);
571 /* Already registered with ST ? */
572 if (test_bit(BT_ST_REGISTERED, &hst->flags)) {
573 BT_ERR("Registered with ST already,open called again?");
577 /* Populate BT driver info required by ST */
578 memset(&hci_st_proto, 0, sizeof(hci_st_proto));
581 hci_st_proto.type = PROTO_SH_BT;
583 /* Receive function which called from ST */
584 hci_st_proto.recv = hci_st_receive;
586 /* Packet match function may used in future */
587 hci_st_proto.match_packet = NULL;
589 /* Callback to be called when registration is pending */
590 hci_st_proto.reg_complete_cb = hci_st_registration_completion_cb;
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
595 hci_st_proto.write = NULL;
597 /* send in the hst to be received at registration complete callback
598 * and during st's receive
600 hci_st_proto.priv_data = hst;
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()
609 init_completion(&hst->wait_for_btdrv_reg_completion);
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.
615 hst->streg_cbdata = -EINPROGRESS;
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
622 BT_ERR(" %s waiting for reg completion signal from ST",
626 wait_for_completion_timeout
627 (&hst->wait_for_btdrv_reg_completion,
628 msecs_to_jiffies(BT_REGISTER_TIMEOUT));
630 BT_ERR("Timeout(%ld sec),didn't get reg"
631 "completion signal from ST",
632 BT_REGISTER_TIMEOUT / 1000);
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);
643 } else if (err == -1) {
644 if (is_print_reg_error)
646 BT_ERR("st_register failed %d", err);
648 is_print_reg_error = 0;
653 diff = (long)jiffi2 - (long)jiffi1;
654 if ( ((diff *1000)/HZ) >= 1000)
655 is_print_reg_error = 1;
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;
665 BT_ERR("failed to get ST write func pointer");
667 /* Undo registration with ST */
668 err = hci_ldisc_unregister(PROTO_SH_BT);
670 BT_ERR("st_unregister failed %d", err);
672 hst->st_write = NULL;
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
680 set_bit(HCI_RUNNING, &hdev->flags);
682 /* Registration with ST successful */
683 set_bit(BT_ST_REGISTERED, &hst->flags);
688 /* ------- Interface to HCI layer ------ */
689 /* Initialize device */
690 static int hci_uart_open(struct hci_dev *hdev)
692 BT_DBG("%s %p", hdev->name, hdev);
694 /* Nothing to do for UART driver */
696 set_bit(HCI_RUNNING, &hdev->flags);
702 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
704 static int hci_uart_flush(struct hci_dev *hdev)
706 struct hci_uart *hu = hci_get_drvdata(hdev);
707 struct tty_struct *tty = hu->tty;
709 BT_DBG("hdev %p tty %p", hdev, tty);
712 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
715 /* Flush any pending characters in the driver and discipline. */
716 tty_ldisc_flush(tty);
717 tty_driver_flush_buffer(tty);
719 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
720 hu->proto->flush(hu);
726 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
728 static int hci_uart_close(struct hci_dev *hdev)
732 BT_DBG("hdev %p", hdev);
736 /* Unregister from ST layer */
737 if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
738 err = hci_ldisc_unregister(PROTO_SH_BT);
740 BT_ERR("st_unregister failed %d", err);
745 hst->st_write = NULL;
747 /* ST layer would have moved chip to inactive state.
748 * So,clear HCI device RUNNING flag.
750 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
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)
760 struct hci_dev *hdev;
764 /* Update HCI stat counters */
766 case HCI_COMMAND_PKT:
770 case HCI_ACLDATA_PKT:
774 case HCI_SCODATA_PKT:
782 /* Send frames from HCI layer */
783 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
789 BT_ERR("Invalid skb received from HCI CORE");
794 BT_ERR("SKB received for invalid HCI Device (hdev=NULL)");
797 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
798 BT_ERR("Device is not running");
802 hst = hci_get_drvdata(hdev);
804 /* Prepend skb with frame type */
805 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
807 BT_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
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.
814 if (!hst->st_write) {
816 BT_ERR(" Can't write to ST, st_write null?");
819 len = hst->st_write(skb);
821 /* Something went wrong in st write , free skb memory */
823 BT_ERR(" ST write failed (%ld)", len);
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);
836 static int hci_uart_close(struct hci_dev *hdev)
838 BT_DBG("hdev %p", hdev);
840 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
843 hci_uart_flush(hdev);
848 /* Send frames from HCI layer */
849 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
851 struct hci_uart *hu = hci_get_drvdata(hdev);
853 if (!test_bit(HCI_RUNNING, &hdev->flags))
856 BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
858 hu->proto->enqueue(hu, skb);
860 hci_uart_tx_wakeup(hu);
866 /* ------ LDISC part ------ */
869 * Called when line discipline changed to HCI_UART.
872 * tty pointer to tty info structure
874 * 0 if success, otherwise error code
876 static int hci_uart_tty_open(struct tty_struct *tty)
878 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
879 struct hci_uart *hu = (void *) tty->disc_data;
881 BT_INFO("tty open %p", tty);
887 BT_DBG("tty %p", tty);
889 /* Error if the tty has no write op instead of leaving an exploitable
891 if (tty->ops->write == NULL)
894 hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
896 BT_ERR("Can't allocate control structure");
902 tty->receive_room = 65536;
903 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
904 spin_lock_irqsave(®_lock, flags);
906 spin_unlock_irqrestore(®_lock, flags);
908 INIT_WORK(&hu->init_ready, hci_uart_init_work);
909 INIT_WORK(&hu->write_work, hci_uart_write_work);
911 spin_lock_init(&hu->rx_lock);
913 /* Flush any pending characters in the driver and line discipline. */
915 /* FIXME: why is this needed. Note don't use ldisc_ref here as the
916 open path is before the ldisc is referencable */
918 if (tty->ldisc->ops->flush_buffer)
919 tty->ldisc->ops->flush_buffer(tty);
920 tty_driver_flush_buffer(tty);
924 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
925 /* hci_uart_tty_close()
927 * Called when the line discipline is changed to something
928 * else, the tty is closed, or the tty detects a hangup.
930 static void hci_uart_tty_close(struct tty_struct *tty)
932 struct hci_uart *hu = (void *)tty->disc_data;
933 struct hci_dev *hdev;
938 BT_INFO("tty close %p", tty);
940 /* Detach from the tty */
941 tty->disc_data = NULL;
943 if (tty->ldisc->ops->flush_buffer)
944 tty->ldisc->ops->flush_buffer(tty);
945 tty_driver_flush_buffer(tty);
949 if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags))
953 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
954 hci_unregister_dev(hdev);
957 hu->proto->close(hu);
958 for (i = PROTO_SH_BT; i < PROTO_SH_MAX; i++)
960 if (hu->list[i] != NULL)
961 BT_ERR("%d not un-registered", i);
967 spin_lock_irqsave(®_lock, flags);
969 spin_unlock_irqrestore(®_lock, flags);
972 static void hci_uart_tty_close(struct tty_struct *tty)
974 struct hci_uart *hu = (void *)tty->disc_data;
975 struct hci_dev *hdev;
977 BT_DBG("tty %p", tty);
979 /* Detach from the tty */
980 tty->disc_data = NULL;
987 hci_uart_close(hdev);
989 cancel_work_sync(&hu->write_work);
991 if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
993 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
994 hci_unregister_dev(hdev);
997 hu->proto->close(hu);
1003 /* hci_uart_tty_wakeup()
1005 * Callback for transmit wakeup. Called when low level
1006 * device driver can accept more send data.
1008 * Arguments: tty pointer to associated tty instance data
1009 * Return Value: None
1011 static void hci_uart_tty_wakeup(struct tty_struct *tty)
1013 struct hci_uart *hu = (void *)tty->disc_data;
1020 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1025 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1026 hci_uart_tx_wakeup(hu);
1029 /* hci_uart_tty_receive()
1031 * Called by tty low level driver when receive data is
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
1039 * Return Value: None
1041 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
1043 struct hci_uart *hu = (void *)tty->disc_data;
1045 if (!hu || tty != hu->tty)
1048 if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
1051 spin_lock(&hu->rx_lock);
1052 hu->proto->recv(hu, (void *) data, count);
1054 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
1056 hu->hdev->stat.byte_rx += count;
1058 spin_unlock(&hu->rx_lock);
1060 tty_unthrottle(tty);
1063 static int hci_uart_register_dev(struct hci_uart *hu)
1065 struct hci_dev *hdev;
1068 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1069 BT_ERR("hci_uart_register_dev");
1071 /* Allocate local resource memory */
1072 hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
1074 BT_ERR("Can't allocate control structure");
1079 /* Initialize and register HCI device */
1080 hdev = hci_alloc_dev();
1082 BT_ERR("Can't allocate HCI device");
1088 hdev->bus = HCI_UART;
1089 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1092 hci_set_drvdata(hdev, hst);
1094 hci_set_drvdata(hdev, hu);
1096 hdev->open = hci_uart_open;
1097 hdev->close = hci_uart_close;
1098 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1101 hdev->flush = hci_uart_flush;
1103 hdev->send = hci_uart_send_frame;
1104 SET_HCIDEV_DEV(hdev, hu->tty->dev);
1106 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
1107 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1109 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
1110 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
1112 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
1113 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1115 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
1116 hdev->dev_type = HCI_AMP;
1118 hdev->dev_type = HCI_BREDR;
1120 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
1123 if (hci_register_dev(hdev) < 0) {
1124 BT_ERR("Can't register HCI device");
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);
1136 static int hci_uart_set_proto(struct hci_uart *hu, int id)
1138 struct hci_uart_proto *p;
1141 p = hci_uart_get_proto(id);
1143 return -EPROTONOSUPPORT;
1151 err = hci_uart_register_dev(hu);
1160 static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
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);
1168 if ((flags & ~valid_flags))
1171 hu->hdev_flags = flags;
1176 /* hci_uart_tty_ioctl()
1178 * Process IOCTL system call for the tty device.
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)
1187 * Return Value: Command dependent
1189 static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
1190 unsigned int cmd, unsigned long arg)
1192 struct hci_uart *hu = (void *)tty->disc_data;
1197 /* Verify the status of the device */
1202 case HCIUARTSETPROTO:
1203 if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1204 err = hci_uart_set_proto(hu, arg);
1206 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
1213 case HCIUARTGETPROTO:
1214 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1215 return hu->proto->id;
1218 case HCIUARTGETDEVICE:
1219 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1220 return hu->hdev->id;
1223 case HCIUARTSETFLAGS:
1224 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1226 err = hci_uart_set_flags(hu, arg);
1231 case HCIUARTGETFLAGS:
1232 return hu->hdev_flags;
1235 err = n_tty_ioctl_helper(tty, file, cmd, arg);
1243 * We don't provide read/write/poll interface for user space.
1245 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
1246 unsigned char __user *buf, size_t nr)
1251 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
1252 const unsigned char *data, size_t count)
1257 static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
1258 struct file *filp, poll_table *wait)
1263 static int __init hci_uart_init(void)
1265 static struct tty_ldisc_ops hci_uart_ldisc;
1268 BT_INFO("HCI UART driver ver %s", VERSION);
1270 /* Register the tty discipline */
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;
1285 err = tty_register_ldisc(N_HCI, &hci_uart_ldisc);
1287 BT_ERR("HCI line discipline registration failed. (%d)", err);
1291 #ifdef CONFIG_BT_HCI_BCM_UART
1294 #ifdef CONFIG_BT_HCIUART_BCSP
1297 #ifdef CONFIG_BT_HCIUART_LL
1300 #ifdef CONFIG_BT_HCIUART_ATH3K
1303 #ifdef CONFIG_BT_HCIUART_3WIRE
1306 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1307 /* initialize register lock spinlock */
1308 spin_lock_init(®_lock);
1313 static void __exit hci_uart_exit(void)
1317 #ifdef CONFIG_BT_HCI_BCM_UART
1320 #ifdef CONFIG_BT_HCIUART_BCSP
1323 #ifdef CONFIG_BT_HCIUART_LL
1326 #ifdef CONFIG_BT_HCIUART_ATH3K
1329 #ifdef CONFIG_BT_HCIUART_3WIRE
1333 /* Release tty registration of line discipline */
1334 err = tty_unregister_ldisc(N_HCI);
1336 BT_ERR("Can't unregister HCI line discipline (%d)", err);
1339 module_init(hci_uart_init);
1340 module_exit(hci_uart_exit);
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);