hci_ath.c 5.9 KB

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