hci_h4.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth HCI UART driver
  5. *
  6. * Copyright (C) 2000-2001 Qualcomm Incorporated
  7. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  8. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/signal.h>
  23. #include <linux/ioctl.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/unaligned.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "hci_uart.h"
  29. struct h4_struct {
  30. struct sk_buff *rx_skb;
  31. struct sk_buff_head txq;
  32. };
  33. /* Initialize protocol */
  34. static int h4_open(struct hci_uart *hu)
  35. {
  36. struct h4_struct *h4;
  37. BT_DBG("hu %p", hu);
  38. h4 = kzalloc_obj(*h4);
  39. if (!h4)
  40. return -ENOMEM;
  41. skb_queue_head_init(&h4->txq);
  42. hu->priv = h4;
  43. return 0;
  44. }
  45. /* Flush protocol data */
  46. static int h4_flush(struct hci_uart *hu)
  47. {
  48. struct h4_struct *h4 = hu->priv;
  49. BT_DBG("hu %p", hu);
  50. skb_queue_purge(&h4->txq);
  51. return 0;
  52. }
  53. /* Close protocol */
  54. static int h4_close(struct hci_uart *hu)
  55. {
  56. struct h4_struct *h4 = hu->priv;
  57. BT_DBG("hu %p", hu);
  58. skb_queue_purge(&h4->txq);
  59. kfree_skb(h4->rx_skb);
  60. hu->priv = NULL;
  61. kfree(h4);
  62. return 0;
  63. }
  64. /* Enqueue frame for transmission (padding, crc, etc) */
  65. static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  66. {
  67. struct h4_struct *h4 = hu->priv;
  68. BT_DBG("hu %p skb %p", hu, skb);
  69. /* Prepend skb with frame type */
  70. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  71. skb_queue_tail(&h4->txq, skb);
  72. return 0;
  73. }
  74. static const struct h4_recv_pkt h4_recv_pkts[] = {
  75. { H4_RECV_ACL, .recv = hci_recv_frame },
  76. { H4_RECV_SCO, .recv = hci_recv_frame },
  77. { H4_RECV_EVENT, .recv = hci_recv_frame },
  78. { H4_RECV_ISO, .recv = hci_recv_frame },
  79. };
  80. /* Recv data */
  81. static int h4_recv(struct hci_uart *hu, const void *data, int count)
  82. {
  83. struct h4_struct *h4 = hu->priv;
  84. h4->rx_skb = h4_recv_buf(hu, h4->rx_skb, data, count,
  85. h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
  86. if (IS_ERR(h4->rx_skb)) {
  87. int err = PTR_ERR(h4->rx_skb);
  88. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  89. h4->rx_skb = NULL;
  90. return err;
  91. }
  92. return count;
  93. }
  94. static struct sk_buff *h4_dequeue(struct hci_uart *hu)
  95. {
  96. struct h4_struct *h4 = hu->priv;
  97. return skb_dequeue(&h4->txq);
  98. }
  99. static const struct hci_uart_proto h4p = {
  100. .id = HCI_UART_H4,
  101. .name = "H4",
  102. .open = h4_open,
  103. .close = h4_close,
  104. .recv = h4_recv,
  105. .enqueue = h4_enqueue,
  106. .dequeue = h4_dequeue,
  107. .flush = h4_flush,
  108. };
  109. int __init h4_init(void)
  110. {
  111. return hci_uart_register_proto(&h4p);
  112. }
  113. int __exit h4_deinit(void)
  114. {
  115. return hci_uart_unregister_proto(&h4p);
  116. }
  117. struct sk_buff *h4_recv_buf(struct hci_uart *hu, struct sk_buff *skb,
  118. const unsigned char *buffer, int count,
  119. const struct h4_recv_pkt *pkts, int pkts_count)
  120. {
  121. u8 alignment = hu->alignment ? hu->alignment : 1;
  122. struct hci_dev *hdev = hu->hdev;
  123. /* Check for error from previous call */
  124. if (IS_ERR(skb))
  125. skb = NULL;
  126. while (count) {
  127. int i, len;
  128. /* remove padding bytes from buffer */
  129. for (; hu->padding && count > 0; hu->padding--) {
  130. count--;
  131. buffer++;
  132. }
  133. if (!count)
  134. break;
  135. if (!skb) {
  136. for (i = 0; i < pkts_count; i++) {
  137. if (buffer[0] != (&pkts[i])->type)
  138. continue;
  139. skb = bt_skb_alloc((&pkts[i])->maxlen,
  140. GFP_ATOMIC);
  141. if (!skb)
  142. return ERR_PTR(-ENOMEM);
  143. hci_skb_pkt_type(skb) = (&pkts[i])->type;
  144. hci_skb_expect(skb) = (&pkts[i])->hlen;
  145. break;
  146. }
  147. /* Check for invalid packet type */
  148. if (!skb)
  149. return ERR_PTR(-EILSEQ);
  150. count -= 1;
  151. buffer += 1;
  152. }
  153. len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
  154. skb_put_data(skb, buffer, len);
  155. count -= len;
  156. buffer += len;
  157. /* Check for partial packet */
  158. if (skb->len < hci_skb_expect(skb))
  159. continue;
  160. for (i = 0; i < pkts_count; i++) {
  161. if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
  162. break;
  163. }
  164. if (i >= pkts_count) {
  165. kfree_skb(skb);
  166. return ERR_PTR(-EILSEQ);
  167. }
  168. if (skb->len == (&pkts[i])->hlen) {
  169. u16 dlen;
  170. switch ((&pkts[i])->lsize) {
  171. case 0:
  172. /* No variable data length */
  173. dlen = 0;
  174. break;
  175. case 1:
  176. /* Single octet variable length */
  177. dlen = skb->data[(&pkts[i])->loff];
  178. hci_skb_expect(skb) += dlen;
  179. if (skb_tailroom(skb) < dlen) {
  180. kfree_skb(skb);
  181. return ERR_PTR(-EMSGSIZE);
  182. }
  183. break;
  184. case 2:
  185. /* Double octet variable length */
  186. dlen = get_unaligned_le16(skb->data +
  187. (&pkts[i])->loff);
  188. hci_skb_expect(skb) += dlen;
  189. if (skb_tailroom(skb) < dlen) {
  190. kfree_skb(skb);
  191. return ERR_PTR(-EMSGSIZE);
  192. }
  193. break;
  194. default:
  195. /* Unsupported variable length */
  196. kfree_skb(skb);
  197. return ERR_PTR(-EILSEQ);
  198. }
  199. if (!dlen) {
  200. hu->padding = (skb->len + 1) % alignment;
  201. hu->padding = (alignment - hu->padding) % alignment;
  202. /* No more data, complete frame */
  203. (&pkts[i])->recv(hdev, skb);
  204. skb = NULL;
  205. }
  206. } else {
  207. hu->padding = (skb->len + 1) % alignment;
  208. hu->padding = (alignment - hu->padding) % alignment;
  209. /* Complete frame */
  210. (&pkts[i])->recv(hdev, skb);
  211. skb = NULL;
  212. }
  213. }
  214. return skb;
  215. }
  216. EXPORT_SYMBOL_GPL(h4_recv_buf);