h4_recv.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *
  3. * Generic Bluetooth HCI UART driver
  4. *
  5. * Copyright (C) 2015-2018 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 <asm/unaligned.h>
  24. struct h4_recv_pkt {
  25. u8 type; /* Packet type */
  26. u8 hlen; /* Header length */
  27. u8 loff; /* Data length offset in header */
  28. u8 lsize; /* Data length field size */
  29. u16 maxlen; /* Max overall packet length */
  30. int (*recv)(struct hci_dev *hdev, struct sk_buff *skb);
  31. };
  32. #define H4_RECV_ACL \
  33. .type = HCI_ACLDATA_PKT, \
  34. .hlen = HCI_ACL_HDR_SIZE, \
  35. .loff = 2, \
  36. .lsize = 2, \
  37. .maxlen = HCI_MAX_FRAME_SIZE \
  38. #define H4_RECV_SCO \
  39. .type = HCI_SCODATA_PKT, \
  40. .hlen = HCI_SCO_HDR_SIZE, \
  41. .loff = 2, \
  42. .lsize = 1, \
  43. .maxlen = HCI_MAX_SCO_SIZE
  44. #define H4_RECV_EVENT \
  45. .type = HCI_EVENT_PKT, \
  46. .hlen = HCI_EVENT_HDR_SIZE, \
  47. .loff = 1, \
  48. .lsize = 1, \
  49. .maxlen = HCI_MAX_EVENT_SIZE
  50. static inline struct sk_buff *h4_recv_buf(struct hci_dev *hdev,
  51. struct sk_buff *skb,
  52. const unsigned char *buffer,
  53. int count,
  54. const struct h4_recv_pkt *pkts,
  55. int pkts_count)
  56. {
  57. /* Check for error from previous call */
  58. if (IS_ERR(skb))
  59. skb = NULL;
  60. while (count) {
  61. int i, len;
  62. if (!count)
  63. break;
  64. if (!skb) {
  65. for (i = 0; i < pkts_count; i++) {
  66. if (buffer[0] != (&pkts[i])->type)
  67. continue;
  68. skb = bt_skb_alloc((&pkts[i])->maxlen,
  69. GFP_ATOMIC);
  70. if (!skb)
  71. return ERR_PTR(-ENOMEM);
  72. hci_skb_pkt_type(skb) = (&pkts[i])->type;
  73. hci_skb_expect(skb) = (&pkts[i])->hlen;
  74. break;
  75. }
  76. /* Check for invalid packet type */
  77. if (!skb)
  78. return ERR_PTR(-EILSEQ);
  79. count -= 1;
  80. buffer += 1;
  81. }
  82. len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
  83. skb_put_data(skb, buffer, len);
  84. count -= len;
  85. buffer += len;
  86. /* Check for partial packet */
  87. if (skb->len < hci_skb_expect(skb))
  88. continue;
  89. for (i = 0; i < pkts_count; i++) {
  90. if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
  91. break;
  92. }
  93. if (i >= pkts_count) {
  94. kfree_skb(skb);
  95. return ERR_PTR(-EILSEQ);
  96. }
  97. if (skb->len == (&pkts[i])->hlen) {
  98. u16 dlen;
  99. switch ((&pkts[i])->lsize) {
  100. case 0:
  101. /* No variable data length */
  102. dlen = 0;
  103. break;
  104. case 1:
  105. /* Single octet variable length */
  106. dlen = skb->data[(&pkts[i])->loff];
  107. hci_skb_expect(skb) += dlen;
  108. if (skb_tailroom(skb) < dlen) {
  109. kfree_skb(skb);
  110. return ERR_PTR(-EMSGSIZE);
  111. }
  112. break;
  113. case 2:
  114. /* Double octet variable length */
  115. dlen = get_unaligned_le16(skb->data +
  116. (&pkts[i])->loff);
  117. hci_skb_expect(skb) += dlen;
  118. if (skb_tailroom(skb) < dlen) {
  119. kfree_skb(skb);
  120. return ERR_PTR(-EMSGSIZE);
  121. }
  122. break;
  123. default:
  124. /* Unsupported variable length */
  125. kfree_skb(skb);
  126. return ERR_PTR(-EILSEQ);
  127. }
  128. if (!dlen) {
  129. /* No more data, complete frame */
  130. (&pkts[i])->recv(hdev, skb);
  131. skb = NULL;
  132. }
  133. } else {
  134. /* Complete frame */
  135. (&pkts[i])->recv(hdev, skb);
  136. skb = NULL;
  137. }
  138. }
  139. return skb;
  140. }