hci_ath.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 *rx_skb;
  43. struct sk_buff_head txq;
  44. struct work_struct ctxtsw;
  45. };
  46. static int ath_wakeup_ar3k(struct tty_struct *tty)
  47. {
  48. int status = tty->driver->ops->tiocmget(tty);
  49. if (status & TIOCM_CTS)
  50. return status;
  51. /* Clear RTS first */
  52. tty->driver->ops->tiocmget(tty);
  53. tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
  54. mdelay(20);
  55. /* Set RTS, wake up board */
  56. tty->driver->ops->tiocmget(tty);
  57. tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
  58. mdelay(20);
  59. status = tty->driver->ops->tiocmget(tty);
  60. return status;
  61. }
  62. static void ath_hci_uart_work(struct work_struct *work)
  63. {
  64. int status;
  65. struct ath_struct *ath;
  66. struct hci_uart *hu;
  67. struct tty_struct *tty;
  68. ath = container_of(work, struct ath_struct, ctxtsw);
  69. hu = ath->hu;
  70. tty = hu->tty;
  71. /* verify and wake up controller */
  72. if (ath->cur_sleep) {
  73. status = ath_wakeup_ar3k(tty);
  74. if (!(status & TIOCM_CTS))
  75. return;
  76. }
  77. /* Ready to send Data */
  78. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  79. hci_uart_tx_wakeup(hu);
  80. }
  81. static int ath_open(struct hci_uart *hu)
  82. {
  83. struct ath_struct *ath;
  84. BT_DBG("hu %p", hu);
  85. ath = kzalloc(sizeof(*ath), GFP_KERNEL);
  86. if (!ath)
  87. return -ENOMEM;
  88. skb_queue_head_init(&ath->txq);
  89. hu->priv = ath;
  90. ath->hu = hu;
  91. INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
  92. return 0;
  93. }
  94. static int ath_close(struct hci_uart *hu)
  95. {
  96. struct ath_struct *ath = hu->priv;
  97. BT_DBG("hu %p", hu);
  98. skb_queue_purge(&ath->txq);
  99. kfree_skb(ath->rx_skb);
  100. cancel_work_sync(&ath->ctxtsw);
  101. hu->priv = NULL;
  102. kfree(ath);
  103. return 0;
  104. }
  105. static int ath_flush(struct hci_uart *hu)
  106. {
  107. struct ath_struct *ath = hu->priv;
  108. BT_DBG("hu %p", hu);
  109. skb_queue_purge(&ath->txq);
  110. return 0;
  111. }
  112. static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  113. {
  114. struct sk_buff *skb;
  115. u8 buf[10];
  116. int err;
  117. buf[0] = 0x01;
  118. buf[1] = 0x01;
  119. buf[2] = 0x00;
  120. buf[3] = sizeof(bdaddr_t);
  121. memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
  122. skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
  123. if (IS_ERR(skb)) {
  124. err = PTR_ERR(skb);
  125. BT_ERR("%s: Change address command failed (%d)",
  126. hdev->name, err);
  127. return err;
  128. }
  129. kfree_skb(skb);
  130. return 0;
  131. }
  132. static int ath_setup(struct hci_uart *hu)
  133. {
  134. BT_DBG("hu %p", hu);
  135. hu->hdev->set_bdaddr = ath_set_bdaddr;
  136. return 0;
  137. }
  138. static const struct h4_recv_pkt ath_recv_pkts[] = {
  139. { H4_RECV_ACL, .recv = hci_recv_frame },
  140. { H4_RECV_SCO, .recv = hci_recv_frame },
  141. { H4_RECV_EVENT, .recv = hci_recv_frame },
  142. };
  143. static int ath_recv(struct hci_uart *hu, const void *data, int count)
  144. {
  145. struct ath_struct *ath = hu->priv;
  146. ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
  147. ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
  148. if (IS_ERR(ath->rx_skb)) {
  149. int err = PTR_ERR(ath->rx_skb);
  150. BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
  151. ath->rx_skb = NULL;
  152. return err;
  153. }
  154. return count;
  155. }
  156. #define HCI_OP_ATH_SLEEP 0xFC04
  157. static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  158. {
  159. struct ath_struct *ath = hu->priv;
  160. if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) {
  161. kfree_skb(skb);
  162. return 0;
  163. }
  164. /* Update power management enable flag with parameters of
  165. * HCI sleep enable vendor specific HCI command.
  166. */
  167. if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
  168. struct hci_command_hdr *hdr = (void *)skb->data;
  169. if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
  170. ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
  171. }
  172. BT_DBG("hu %p skb %p", hu, skb);
  173. /* Prepend skb with frame type */
  174. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  175. skb_queue_tail(&ath->txq, skb);
  176. set_bit(HCI_UART_SENDING, &hu->tx_state);
  177. schedule_work(&ath->ctxtsw);
  178. return 0;
  179. }
  180. static struct sk_buff *ath_dequeue(struct hci_uart *hu)
  181. {
  182. struct ath_struct *ath = hu->priv;
  183. return skb_dequeue(&ath->txq);
  184. }
  185. static const struct hci_uart_proto athp = {
  186. .id = HCI_UART_ATH3K,
  187. .name = "ATH3K",
  188. .manufacturer = 69,
  189. .open = ath_open,
  190. .close = ath_close,
  191. .flush = ath_flush,
  192. .setup = ath_setup,
  193. .recv = ath_recv,
  194. .enqueue = ath_enqueue,
  195. .dequeue = ath_dequeue,
  196. };
  197. int __init ath_init(void)
  198. {
  199. return hci_uart_register_proto(&athp);
  200. }
  201. int __exit ath_deinit(void)
  202. {
  203. return hci_uart_unregister_proto(&athp);
  204. }