hci_h5.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. *
  3. * Bluetooth HCI Three-wire UART driver
  4. *
  5. * Copyright (C) 2012 Intel Corporation
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "hci_uart.h"
  29. #define H5_TXWINSIZE 4
  30. #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
  31. struct h5 {
  32. struct sk_buff_head unack; /* Unack'ed packets queue */
  33. struct sk_buff_head rel; /* Reliable packets queue */
  34. struct sk_buff_head unrel; /* Unreliable packets queue */
  35. struct sk_buff *rx_skb;
  36. struct timer_list timer; /* Retransmission timer */
  37. bool txack_req;
  38. u8 msgq_txseq;
  39. };
  40. static void h5_timed_event(unsigned long arg)
  41. {
  42. struct hci_uart *hu = (struct hci_uart *) arg;
  43. struct h5 *h5 = hu->priv;
  44. struct sk_buff *skb;
  45. unsigned long flags;
  46. BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
  47. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  48. while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
  49. h5->msgq_txseq = (h5->msgq_txseq - 1) & 0x07;
  50. skb_queue_head(&h5->rel, skb);
  51. }
  52. spin_unlock_irqrestore(&h5->unack.lock, flags);
  53. hci_uart_tx_wakeup(hu);
  54. }
  55. static int h5_open(struct hci_uart *hu)
  56. {
  57. struct h5 *h5;
  58. BT_DBG("hu %p", hu);
  59. h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
  60. if (!h5)
  61. return -ENOMEM;
  62. hu->priv = h5;
  63. skb_queue_head_init(&h5->unack);
  64. skb_queue_head_init(&h5->rel);
  65. skb_queue_head_init(&h5->unrel);
  66. init_timer(&h5->timer);
  67. h5->timer.function = h5_timed_event;
  68. h5->timer.data = (unsigned long) hu;
  69. return 0;
  70. }
  71. static int h5_close(struct hci_uart *hu)
  72. {
  73. struct h5 *h5 = hu->priv;
  74. skb_queue_purge(&h5->unack);
  75. skb_queue_purge(&h5->rel);
  76. skb_queue_purge(&h5->unrel);
  77. del_timer(&h5->timer);
  78. kfree(h5);
  79. return 0;
  80. }
  81. static int h5_recv(struct hci_uart *hu, void *data, int count)
  82. {
  83. return -ENOSYS;
  84. }
  85. static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  86. {
  87. struct h5 *h5 = hu->priv;
  88. if (skb->len > 0xfff) {
  89. BT_ERR("Packet too long (%u bytes)", skb->len);
  90. kfree_skb(skb);
  91. return 0;
  92. }
  93. switch (bt_cb(skb)->pkt_type) {
  94. case HCI_ACLDATA_PKT:
  95. case HCI_COMMAND_PKT:
  96. skb_queue_tail(&h5->rel, skb);
  97. break;
  98. case HCI_SCODATA_PKT:
  99. skb_queue_tail(&h5->unrel, skb);
  100. break;
  101. default:
  102. BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
  103. kfree_skb(skb);
  104. break;
  105. }
  106. return 0;
  107. }
  108. static struct sk_buff *h5_prepare_pkt(struct h5 *h5, struct sk_buff *skb)
  109. {
  110. h5->txack_req = false;
  111. return NULL;
  112. }
  113. static struct sk_buff *h5_prepare_ack(struct h5 *h5)
  114. {
  115. h5->txack_req = false;
  116. return NULL;
  117. }
  118. static struct sk_buff *h5_dequeue(struct hci_uart *hu)
  119. {
  120. struct h5 *h5 = hu->priv;
  121. unsigned long flags;
  122. struct sk_buff *skb, *nskb;
  123. if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
  124. nskb = h5_prepare_pkt(h5, skb);
  125. if (nskb) {
  126. kfree_skb(skb);
  127. return nskb;
  128. }
  129. skb_queue_head(&h5->unrel, skb);
  130. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  131. }
  132. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  133. if (h5->unack.qlen >= H5_TXWINSIZE)
  134. goto unlock;
  135. if ((skb = skb_dequeue(&h5->rel)) != NULL) {
  136. nskb = h5_prepare_pkt(h5, skb);
  137. if (nskb) {
  138. __skb_queue_tail(&h5->unack, skb);
  139. mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
  140. spin_unlock_irqrestore(&h5->unack.lock, flags);
  141. return nskb;
  142. }
  143. skb_queue_head(&h5->rel, skb);
  144. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  145. }
  146. unlock:
  147. spin_unlock_irqrestore(&h5->unack.lock, flags);
  148. if (h5->txack_req)
  149. return h5_prepare_ack(h5);
  150. return NULL;
  151. }
  152. static int h5_flush(struct hci_uart *hu)
  153. {
  154. BT_DBG("hu %p", hu);
  155. return 0;
  156. }
  157. static struct hci_uart_proto h5p = {
  158. .id = HCI_UART_3WIRE,
  159. .open = h5_open,
  160. .close = h5_close,
  161. .recv = h5_recv,
  162. .enqueue = h5_enqueue,
  163. .dequeue = h5_dequeue,
  164. .flush = h5_flush,
  165. };
  166. int __init h5_init(void)
  167. {
  168. int err = hci_uart_register_proto(&h5p);
  169. if (!err)
  170. BT_INFO("HCI Three-wire UART (H5) protocol initialized");
  171. else
  172. BT_ERR("HCI Three-wire UART (H5) protocol init failed");
  173. return err;
  174. }
  175. int __exit h5_deinit(void)
  176. {
  177. return hci_uart_unregister_proto(&h5p);
  178. }