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 /* BEGIN TIZEN_Bluetooth */
677 /* FIXME: in order to wait enough until uart init complete,
678 the below routine is added.
682 /* END TIZEN_Bluetooth */
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
689 set_bit(HCI_RUNNING, &hdev->flags);
691 /* Registration with ST successful */
692 set_bit(BT_ST_REGISTERED, &hst->flags);
697 /* ------- Interface to HCI layer ------ */
698 /* Initialize device */
699 static int hci_uart_open(struct hci_dev *hdev)
701 BT_DBG("%s %p", hdev->name, hdev);
703 /* Nothing to do for UART driver */
705 set_bit(HCI_RUNNING, &hdev->flags);
711 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
713 static int hci_uart_flush(struct hci_dev *hdev)
715 struct hci_uart *hu = hci_get_drvdata(hdev);
716 struct tty_struct *tty = hu->tty;
718 BT_DBG("hdev %p tty %p", hdev, tty);
721 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
724 /* Flush any pending characters in the driver and discipline. */
725 tty_ldisc_flush(tty);
726 tty_driver_flush_buffer(tty);
728 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
729 hu->proto->flush(hu);
735 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
737 static int hci_uart_close(struct hci_dev *hdev)
741 BT_DBG("hdev %p", hdev);
745 /* Unregister from ST layer */
746 if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
747 err = hci_ldisc_unregister(PROTO_SH_BT);
749 BT_ERR("st_unregister failed %d", err);
754 hst->st_write = NULL;
756 /* ST layer would have moved chip to inactive state.
757 * So,clear HCI device RUNNING flag.
759 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
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)
769 struct hci_dev *hdev;
773 /* Update HCI stat counters */
775 case HCI_COMMAND_PKT:
779 case HCI_ACLDATA_PKT:
783 case HCI_SCODATA_PKT:
791 /* Send frames from HCI layer */
792 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
798 BT_ERR("Invalid skb received from HCI CORE");
803 BT_ERR("SKB received for invalid HCI Device (hdev=NULL)");
806 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
807 BT_ERR("Device is not running");
811 hst = hci_get_drvdata(hdev);
813 /* Prepend skb with frame type */
814 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
816 BT_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
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.
823 if (!hst->st_write) {
825 BT_ERR(" Can't write to ST, st_write null?");
828 len = hst->st_write(skb);
830 /* Something went wrong in st write , free skb memory */
832 BT_ERR(" ST write failed (%ld)", len);
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);
845 static int hci_uart_close(struct hci_dev *hdev)
847 BT_DBG("hdev %p", hdev);
849 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
852 hci_uart_flush(hdev);
857 /* Send frames from HCI layer */
858 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
860 struct hci_uart *hu = hci_get_drvdata(hdev);
862 if (!test_bit(HCI_RUNNING, &hdev->flags))
865 BT_DBG("%s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
867 hu->proto->enqueue(hu, skb);
869 hci_uart_tx_wakeup(hu);
875 /* ------ LDISC part ------ */
878 * Called when line discipline changed to HCI_UART.
881 * tty pointer to tty info structure
883 * 0 if success, otherwise error code
885 static int hci_uart_tty_open(struct tty_struct *tty)
887 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
888 struct hci_uart *hu = (void *) tty->disc_data;
890 BT_INFO("tty open %p", tty);
896 BT_DBG("tty %p", tty);
898 /* Error if the tty has no write op instead of leaving an exploitable
900 if (tty->ops->write == NULL)
903 hu = kzalloc(sizeof(struct hci_uart), GFP_KERNEL);
905 BT_ERR("Can't allocate control structure");
911 tty->receive_room = 65536;
912 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
913 spin_lock_irqsave(®_lock, flags);
915 spin_unlock_irqrestore(®_lock, flags);
917 INIT_WORK(&hu->init_ready, hci_uart_init_work);
918 INIT_WORK(&hu->write_work, hci_uart_write_work);
920 spin_lock_init(&hu->rx_lock);
922 /* Flush any pending characters in the driver and line discipline. */
924 /* FIXME: why is this needed. Note don't use ldisc_ref here as the
925 open path is before the ldisc is referencable */
927 if (tty->ldisc->ops->flush_buffer)
928 tty->ldisc->ops->flush_buffer(tty);
929 tty_driver_flush_buffer(tty);
933 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
934 /* hci_uart_tty_close()
936 * Called when the line discipline is changed to something
937 * else, the tty is closed, or the tty detects a hangup.
939 static void hci_uart_tty_close(struct tty_struct *tty)
941 struct hci_uart *hu = (void *)tty->disc_data;
942 struct hci_dev *hdev = hu->hdev;
947 BT_INFO("tty close %p", tty);
949 /* Detach from the tty */
950 tty->disc_data = NULL;
952 if (tty->ldisc->ops->flush_buffer)
953 tty->ldisc->ops->flush_buffer(tty);
954 tty_driver_flush_buffer(tty);
959 hci_uart_close(hdev);
961 cancel_work_sync(&hu->write_work);
963 if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags))
966 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
967 hci_unregister_dev(hdev);
970 hu->proto->close(hu);
971 for (i = PROTO_SH_BT; i < PROTO_SH_MAX; i++)
973 if (hu->list[i] != NULL)
974 BT_ERR("%d not un-registered", i);
980 spin_lock_irqsave(®_lock, flags);
982 spin_unlock_irqrestore(®_lock, flags);
985 static void hci_uart_tty_close(struct tty_struct *tty)
987 struct hci_uart *hu = (void *)tty->disc_data;
988 struct hci_dev *hdev;
990 BT_DBG("tty %p", tty);
992 /* Detach from the tty */
993 tty->disc_data = NULL;
1000 hci_uart_close(hdev);
1002 cancel_work_sync(&hu->write_work);
1004 if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1006 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1007 hci_unregister_dev(hdev);
1010 hu->proto->close(hu);
1016 /* hci_uart_tty_wakeup()
1018 * Callback for transmit wakeup. Called when low level
1019 * device driver can accept more send data.
1021 * Arguments: tty pointer to associated tty instance data
1022 * Return Value: None
1024 static void hci_uart_tty_wakeup(struct tty_struct *tty)
1026 struct hci_uart *hu = (void *)tty->disc_data;
1033 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1038 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1039 hci_uart_tx_wakeup(hu);
1042 /* hci_uart_tty_receive()
1044 * Called by tty low level driver when receive data is
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
1052 * Return Value: None
1054 static void hci_uart_tty_receive(struct tty_struct *tty, const u8 *data, char *flags, int count)
1056 struct hci_uart *hu = (void *)tty->disc_data;
1058 if (!hu || tty != hu->tty)
1061 if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
1064 spin_lock(&hu->rx_lock);
1065 hu->proto->recv(hu, (void *) data, count);
1067 #if !defined(CONFIG_RADIO_BCM4343S) && !defined(CONFIG_RADIO_SC2331)
1069 hu->hdev->stat.byte_rx += count;
1071 spin_unlock(&hu->rx_lock);
1073 tty_unthrottle(tty);
1076 static int hci_uart_register_dev(struct hci_uart *hu)
1078 struct hci_dev *hdev;
1081 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1082 BT_ERR("hci_uart_register_dev");
1084 /* Allocate local resource memory */
1085 hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
1087 BT_ERR("Can't allocate control structure");
1092 /* Initialize and register HCI device */
1093 hdev = hci_alloc_dev();
1095 BT_ERR("Can't allocate HCI device");
1101 hdev->bus = HCI_UART;
1102 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1105 hci_set_drvdata(hdev, hst);
1107 hci_set_drvdata(hdev, hu);
1109 hdev->open = hci_uart_open;
1110 hdev->close = hci_uart_close;
1111 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1114 hdev->flush = hci_uart_flush;
1116 hdev->send = hci_uart_send_frame;
1117 SET_HCIDEV_DEV(hdev, hu->tty->dev);
1119 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
1120 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
1122 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
1123 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
1125 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
1126 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1128 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
1129 hdev->dev_type = HCI_AMP;
1131 hdev->dev_type = HCI_BREDR;
1133 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
1136 if (hci_register_dev(hdev) < 0) {
1137 BT_ERR("Can't register HCI device");
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);
1149 static int hci_uart_set_proto(struct hci_uart *hu, int id)
1151 struct hci_uart_proto *p;
1154 p = hci_uart_get_proto(id);
1156 return -EPROTONOSUPPORT;
1164 err = hci_uart_register_dev(hu);
1173 static int hci_uart_set_flags(struct hci_uart *hu, unsigned long flags)
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);
1181 if ((flags & ~valid_flags))
1184 hu->hdev_flags = flags;
1189 /* hci_uart_tty_ioctl()
1191 * Process IOCTL system call for the tty device.
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)
1200 * Return Value: Command dependent
1202 static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
1203 unsigned int cmd, unsigned long arg)
1205 struct hci_uart *hu = (void *)tty->disc_data;
1210 /* Verify the status of the device */
1215 case HCIUARTSETPROTO:
1216 if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
1217 err = hci_uart_set_proto(hu, arg);
1219 clear_bit(HCI_UART_PROTO_SET, &hu->flags);
1226 case HCIUARTGETPROTO:
1227 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1228 return hu->proto->id;
1231 case HCIUARTGETDEVICE:
1232 if (test_bit(HCI_UART_REGISTERED, &hu->flags))
1233 return hu->hdev->id;
1236 case HCIUARTSETFLAGS:
1237 if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
1239 err = hci_uart_set_flags(hu, arg);
1244 case HCIUARTGETFLAGS:
1245 return hu->hdev_flags;
1248 err = n_tty_ioctl_helper(tty, file, cmd, arg);
1256 * We don't provide read/write/poll interface for user space.
1258 static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file,
1259 unsigned char __user *buf, size_t nr)
1264 static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file,
1265 const unsigned char *data, size_t count)
1270 static unsigned int hci_uart_tty_poll(struct tty_struct *tty,
1271 struct file *filp, poll_table *wait)
1276 static int __init hci_uart_init(void)
1278 static struct tty_ldisc_ops hci_uart_ldisc;
1281 BT_INFO("HCI UART driver ver %s", VERSION);
1283 /* Register the tty discipline */
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;
1298 err = tty_register_ldisc(N_HCI, &hci_uart_ldisc);
1300 BT_ERR("HCI line discipline registration failed. (%d)", err);
1304 #ifdef CONFIG_BT_HCI_BCM_UART
1307 #ifdef CONFIG_BT_HCIUART_BCSP
1310 #ifdef CONFIG_BT_HCIUART_LL
1313 #ifdef CONFIG_BT_HCIUART_ATH3K
1316 #ifdef CONFIG_BT_HCIUART_3WIRE
1319 #if defined(CONFIG_RADIO_BCM4343S) || defined(CONFIG_RADIO_SC2331)
1320 /* initialize register lock spinlock */
1321 spin_lock_init(®_lock);
1326 static void __exit hci_uart_exit(void)
1330 #ifdef CONFIG_BT_HCI_BCM_UART
1333 #ifdef CONFIG_BT_HCIUART_BCSP
1336 #ifdef CONFIG_BT_HCIUART_LL
1339 #ifdef CONFIG_BT_HCIUART_ATH3K
1342 #ifdef CONFIG_BT_HCIUART_3WIRE
1346 /* Release tty registration of line discipline */
1347 err = tty_unregister_ldisc(N_HCI);
1349 BT_ERR("Can't unregister HCI line discipline (%d)", err);
1352 module_init(hci_uart_init);
1353 module_exit(hci_uart_exit);
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);