hci_ath.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Atheros Communication Bluetooth HCIATH3K UART protocol
  4. *
  5. * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
  6. * power management protocol extension to H4 to support AR300x Bluetooth Chip.
  7. *
  8. * Copyright (c) 2009-2010 Atheros Communications Inc.
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_h4.c, which was written
  12. * by Maxim Krasnyansky and Marcel Holtmann.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/tty.h>
  19. #include <linux/errno.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/skbuff.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #include "hci_uart.h"
  25. struct ath_struct {
  26. struct hci_uart *hu;
  27. unsigned int cur_sleep;
  28. struct sk_buff *rx_skb;
  29. struct sk_buff_head txq;
  30. struct work_struct ctxtsw;
  31. };
  32. #define OP_WRITE_TAG 0x01
  33. #define INDEX_BDADDR 0x01
  34. struct ath_vendor_cmd {
  35. __u8 opcode;
  36. __le16 index;
  37. __u8 len;
  38. __u8 data[251];
  39. } __packed;
  40. static int ath_wakeup_ar3k(struct tty_struct *tty)
  41. {
  42. int status = tty->driver->ops->tiocmget(tty);
  43. if (status & TIOCM_CTS)
  44. return status;
  45. /* Clear RTS first */
  46. tty->driver->ops->tiocmget(tty);
  47. tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
  48. msleep(20);
  49. /* Set RTS, wake up board */
  50. tty->driver->ops->tiocmget(tty);
  51. tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
  52. msleep(20);
  53. status = tty->driver->ops->tiocmget(tty);
  54. return status;
  55. }
  56. static void ath_hci_uart_work(struct work_struct *work)
  57. {
  58. int status;
  59. struct ath_struct *ath;
  60. struct hci_uart *hu;
  61. struct tty_struct *tty;
  62. ath = container_of(work, struct ath_struct, ctxtsw);
  63. hu = ath->hu;
  64. tty = hu->tty;
  65. /* verify and wake up controller */
  66. if (ath->cur_sleep) {
  67. status = ath_wakeup_ar3k(tty);
  68. if (!(status & TIOCM_CTS))
  69. return;
  70. }
  71. /* Ready to send Data */
  72. clear_bit(HCI_UART_SENDING, &hu->tx_state);
  73. hci_uart_tx_wakeup(hu);
  74. }
  75. static int ath_open(struct hci_uart *hu)
  76. {
  77. struct ath_struct *ath;
  78. BT_DBG("hu %p", hu);
  79. if (!hci_uart_has_flow_control(hu))
  80. return -EOPNOTSUPP;
  81. ath = kzalloc(sizeof(*ath), GFP_KERNEL);
  82. if (!ath)
  83. return -ENOMEM;
  84. skb_queue_head_init(&ath->txq);
  85. hu->priv = ath;
  86. ath->hu = hu;
  87. INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
  88. return 0;
  89. }
  90. static int ath_close(struct hci_uart *hu)
  91. {
  92. struct ath_struct *ath = hu->priv;
  93. BT_DBG("hu %p", hu);
  94. skb_queue_purge(&ath->txq);
  95. kfree_skb(ath->rx_skb);
  96. cancel_work_sync(&ath->ctxtsw);
  97. hu->priv = NULL;
  98. kfree(ath);
  99. return 0;
  100. }
  101. static int ath_flush(struct hci_uart *hu)
  102. {
  103. struct ath_struct *ath = hu->priv;
  104. BT_DBG("hu %p", hu);
  105. skb_queue_purge(&ath->txq);
  106. return 0;
  107. }
  108. static int ath_vendor_cmd(struct hci_dev *hdev, uint8_t opcode, uint16_t index,
  109. const void *data, size_t dlen)
  110. {
  111. struct sk_buff *skb;
  112. struct ath_vendor_cmd cmd;
  113. if (dlen > sizeof(cmd.data))
  114. return -EINVAL;
  115. cmd.opcode = opcode;
  116. cmd.index = cpu_to_le16(index);
  117. cmd.len = dlen;
  118. memcpy(cmd.data, data, dlen);
  119. skb = __hci_cmd_sync(hdev, 0xfc0b, dlen + 4, &cmd, HCI_INIT_TIMEOUT);
  120. if (IS_ERR(skb))
  121. return PTR_ERR(skb);
  122. kfree_skb(skb);
  123. return 0;
  124. }
  125. static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  126. {
  127. return ath_vendor_cmd(hdev, OP_WRITE_TAG, INDEX_BDADDR, bdaddr,
  128. sizeof(*bdaddr));
  129. }
  130. static int ath_setup(struct hci_uart *hu)
  131. {
  132. BT_DBG("hu %p", hu);
  133. hu->hdev->set_bdaddr = ath_set_bdaddr;
  134. return 0;
  135. }
  136. static const struct h4_recv_pkt ath_recv_pkts[] = {
  137. { H4_RECV_ACL, .recv = hci_recv_frame },
  138. { H4_RECV_SCO, .recv = hci_recv_frame },
  139. { H4_RECV_EVENT, .recv = hci_recv_frame },
  140. };
  141. static int ath_recv(struct hci_uart *hu, const void *data, int count)
  142. {
  143. struct ath_struct *ath = hu->priv;
  144. ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
  145. ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
  146. if (IS_ERR(ath->rx_skb)) {
  147. int err = PTR_ERR(ath->rx_skb);
  148. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  149. ath->rx_skb = NULL;
  150. return err;
  151. }
  152. return count;
  153. }
  154. #define HCI_OP_ATH_SLEEP 0xFC04
  155. static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  156. {
  157. struct ath_struct *ath = hu->priv;
  158. if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) {
  159. kfree_skb(skb);
  160. return 0;
  161. }
  162. /* Update power management enable flag with parameters of
  163. * HCI sleep enable vendor specific HCI command.
  164. */
  165. if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
  166. struct hci_command_hdr *hdr = (void *)skb->data;
  167. if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
  168. ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
  169. }
  170. BT_DBG("hu %p skb %p", hu, skb);
  171. /* Prepend skb with frame type */
  172. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  173. skb_queue_tail(&ath->txq, skb);
  174. set_bit(HCI_UART_SENDING, &hu->tx_state);
  175. schedule_work(&ath->ctxtsw);
  176. return 0;
  177. }
  178. static struct sk_buff *ath_dequeue(struct hci_uart *hu)
  179. {
  180. struct ath_struct *ath = hu->priv;
  181. return skb_dequeue(&ath->txq);
  182. }
  183. static const struct hci_uart_proto athp = {
  184. .id = HCI_UART_ATH3K,
  185. .name = "ATH3K",
  186. .manufacturer = 69,
  187. .open = ath_open,
  188. .close = ath_close,
  189. .flush = ath_flush,
  190. .setup = ath_setup,
  191. .recv = ath_recv,
  192. .enqueue = ath_enqueue,
  193. .dequeue = ath_dequeue,
  194. };
  195. int __init ath_init(void)
  196. {
  197. return hci_uart_register_proto(&athp);
  198. }
  199. int __exit ath_deinit(void)
  200. {
  201. return hci_uart_unregister_proto(&athp);
  202. }