h4_recv.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. while (count) {
  58. int i, len;
  59. if (!count)
  60. break;
  61. if (!skb) {
  62. for (i = 0; i < pkts_count; i++) {
  63. if (buffer[0] != (&pkts[i])->type)
  64. continue;
  65. skb = bt_skb_alloc((&pkts[i])->maxlen,
  66. GFP_ATOMIC);
  67. if (!skb)
  68. return ERR_PTR(-ENOMEM);
  69. hci_skb_pkt_type(skb) = (&pkts[i])->type;
  70. hci_skb_expect(skb) = (&pkts[i])->hlen;
  71. break;
  72. }
  73. /* Check for invalid packet type */
  74. if (!skb)
  75. return ERR_PTR(-EILSEQ);
  76. count -= 1;
  77. buffer += 1;
  78. }
  79. len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
  80. skb_put_data(skb, buffer, len);
  81. count -= len;
  82. buffer += len;
  83. /* Check for partial packet */
  84. if (skb->len < hci_skb_expect(skb))
  85. continue;
  86. for (i = 0; i < pkts_count; i++) {
  87. if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
  88. break;
  89. }
  90. if (i >= pkts_count) {
  91. kfree_skb(skb);
  92. return ERR_PTR(-EILSEQ);
  93. }
  94. if (skb->len == (&pkts[i])->hlen) {
  95. u16 dlen;
  96. switch ((&pkts[i])->lsize) {
  97. case 0:
  98. /* No variable data length */
  99. dlen = 0;
  100. break;
  101. case 1:
  102. /* Single octet variable length */
  103. dlen = skb->data[(&pkts[i])->loff];
  104. hci_skb_expect(skb) += dlen;
  105. if (skb_tailroom(skb) < dlen) {
  106. kfree_skb(skb);
  107. return ERR_PTR(-EMSGSIZE);
  108. }
  109. break;
  110. case 2:
  111. /* Double octet variable length */
  112. dlen = get_unaligned_le16(skb->data +
  113. (&pkts[i])->loff);
  114. hci_skb_expect(skb) += dlen;
  115. if (skb_tailroom(skb) < dlen) {
  116. kfree_skb(skb);
  117. return ERR_PTR(-EMSGSIZE);
  118. }
  119. break;
  120. default:
  121. /* Unsupported variable length */
  122. kfree_skb(skb);
  123. return ERR_PTR(-EILSEQ);
  124. }
  125. if (!dlen) {
  126. /* No more data, complete frame */
  127. (&pkts[i])->recv(hdev, skb);
  128. skb = NULL;
  129. }
  130. } else {
  131. /* Complete frame */
  132. (&pkts[i])->recv(hdev, skb);
  133. skb = NULL;
  134. }
  135. }
  136. return skb;
  137. }