hci_ath.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Atheros Communication Bluetooth HCIATH3K UART protocol
  3. *
  4. * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
  5. * power management protocol extension to H4 to support AR300x Bluetooth Chip.
  6. *
  7. * Copyright (c) 2009-2010 Atheros Communications Inc.
  8. *
  9. * Acknowledgements:
  10. * This file is based on hci_h4.c, which was written
  11. * by Maxim Krasnyansky and Marcel Holtmann.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/tty.h>
  33. #include <linux/errno.h>
  34. #include <linux/ioctl.h>
  35. #include <linux/skbuff.h>
  36. #include <net/bluetooth/bluetooth.h>
  37. #include <net/bluetooth/hci_core.h>
  38. #include "hci_uart.h"
  39. struct ath_struct {
  40. struct hci_uart *hu;
  41. unsigned int cur_sleep;
  42. struct sk_buff_head txq;
  43. struct work_struct ctxtsw;
  44. };
  45. static int ath_wakeup_ar3k(struct tty_struct *tty)
  46. {
  47. struct ktermios ktermios;
  48. int status = tty->driver->ops->tiocmget(tty);
  49. if (status & TIOCM_CTS)
  50. return status;
  51. /* Disable Automatic RTSCTS */
  52. memcpy(&ktermios, tty->termios, sizeof(ktermios));
  53. ktermios.c_cflag &= ~CRTSCTS;
  54. tty_set_termios(tty, &ktermios);
  55. /* Clear RTS first */
  56. status = tty->driver->ops->tiocmget(tty);
  57. tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
  58. mdelay(20);
  59. /* Set RTS, wake up board */
  60. status = tty->driver->ops->tiocmget(tty);
  61. tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
  62. mdelay(20);
  63. status = tty->driver->ops->tiocmget(tty);
  64. /* Disable Automatic RTSCTS */
  65. ktermios.c_cflag |= CRTSCTS;
  66. status = tty_set_termios(tty, &ktermios);
  67. return status;
  68. }
  69. static void ath_hci_uart_work(struct work_struct *work)
  70. {
  71. int status;
  72. struct ath_struct *ath;
  73. struct hci_uart *hu;
  74. struct tty_struct *tty;
  75. ath = container_of(work, struct ath_struct, ctxtsw);
  76. hu = ath->hu;
  77. tty = hu->tty;
  78. /* verify and wake up controller */
  79. if (ath->cur_sleep) {
  80. status = ath_wakeup_ar3k(tty);
  81. if (!(status & TIOCM_CTS))
  82. return;
  83. }
  84. /* Ready to send Data */
  85. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  86. hci_uart_tx_wakeup(hu);
  87. }
  88. /* Initialize protocol */
  89. static int ath_open(struct hci_uart *hu)
  90. {
  91. struct ath_struct *ath;
  92. BT_DBG("hu %p", hu);
  93. ath = kzalloc(sizeof(*ath), GFP_ATOMIC);
  94. if (!ath)
  95. return -ENOMEM;
  96. skb_queue_head_init(&ath->txq);
  97. hu->priv = ath;
  98. ath->hu = hu;
  99. INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
  100. return 0;
  101. }
  102. /* Flush protocol data */
  103. static int ath_flush(struct hci_uart *hu)
  104. {
  105. struct ath_struct *ath = hu->priv;
  106. BT_DBG("hu %p", hu);
  107. skb_queue_purge(&ath->txq);
  108. return 0;
  109. }
  110. /* Close protocol */
  111. static int ath_close(struct hci_uart *hu)
  112. {
  113. struct ath_struct *ath = hu->priv;
  114. BT_DBG("hu %p", hu);
  115. skb_queue_purge(&ath->txq);
  116. cancel_work_sync(&ath->ctxtsw);
  117. hu->priv = NULL;
  118. kfree(ath);
  119. return 0;
  120. }
  121. #define HCI_OP_ATH_SLEEP 0xFC04
  122. /* Enqueue frame for transmittion */
  123. static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  124. {
  125. struct ath_struct *ath = hu->priv;
  126. if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) {
  127. kfree_skb(skb);
  128. return 0;
  129. }
  130. /*
  131. * Update power management enable flag with parameters of
  132. * HCI sleep enable vendor specific HCI command.
  133. */
  134. if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
  135. struct hci_command_hdr *hdr = (void *)skb->data;
  136. if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
  137. ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
  138. }
  139. BT_DBG("hu %p skb %p", hu, skb);
  140. /* Prepend skb with frame type */
  141. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  142. skb_queue_tail(&ath->txq, skb);
  143. set_bit(HCI_UART_SENDING, &hu->tx_state);
  144. schedule_work(&ath->ctxtsw);
  145. return 0;
  146. }
  147. static struct sk_buff *ath_dequeue(struct hci_uart *hu)
  148. {
  149. struct ath_struct *ath = hu->priv;
  150. return skb_dequeue(&ath->txq);
  151. }
  152. /* Recv data */
  153. static int ath_recv(struct hci_uart *hu, void *data, int count)
  154. {
  155. int ret;
  156. ret = hci_recv_stream_fragment(hu->hdev, data, count);
  157. if (ret < 0) {
  158. BT_ERR("Frame Reassembly Failed");
  159. return ret;
  160. }
  161. return count;
  162. }
  163. static struct hci_uart_proto athp = {
  164. .id = HCI_UART_ATH3K,
  165. .open = ath_open,
  166. .close = ath_close,
  167. .recv = ath_recv,
  168. .enqueue = ath_enqueue,
  169. .dequeue = ath_dequeue,
  170. .flush = ath_flush,
  171. };
  172. int __init ath_init(void)
  173. {
  174. int err = hci_uart_register_proto(&athp);
  175. if (!err)
  176. BT_INFO("HCIATH3K protocol initialized");
  177. else
  178. BT_ERR("HCIATH3K protocol registration failed");
  179. return err;
  180. }
  181. int __exit ath_deinit(void)
  182. {
  183. return hci_uart_unregister_proto(&athp);
  184. }