hci_h4.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <[email protected]>
  8. * Copyright (C) 2004-2005 Marcel Holtmann <[email protected]>
  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 <asm/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(sizeof(*h4), GFP_KERNEL);
  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. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  85. return -EUNATCH;
  86. h4->rx_skb = h4_recv_buf(hu->hdev, h4->rx_skb, data, count,
  87. h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
  88. if (IS_ERR(h4->rx_skb)) {
  89. int err = PTR_ERR(h4->rx_skb);
  90. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  91. h4->rx_skb = NULL;
  92. return err;
  93. }
  94. return count;
  95. }
  96. static struct sk_buff *h4_dequeue(struct hci_uart *hu)
  97. {
  98. struct h4_struct *h4 = hu->priv;
  99. return skb_dequeue(&h4->txq);
  100. }
  101. static const struct hci_uart_proto h4p = {
  102. .id = HCI_UART_H4,
  103. .name = "H4",
  104. .open = h4_open,
  105. .close = h4_close,
  106. .recv = h4_recv,
  107. .enqueue = h4_enqueue,
  108. .dequeue = h4_dequeue,
  109. .flush = h4_flush,
  110. };
  111. int __init h4_init(void)
  112. {
  113. return hci_uart_register_proto(&h4p);
  114. }
  115. int __exit h4_deinit(void)
  116. {
  117. return hci_uart_unregister_proto(&h4p);
  118. }
  119. struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
  120. const unsigned char *buffer, int count,
  121. const struct h4_recv_pkt *pkts, int pkts_count)
  122. {
  123. struct hci_uart *hu = hci_get_drvdata(hdev);
  124. u8 alignment = hu->alignment ? hu->alignment : 1;
  125. /* Check for error from previous call */
  126. if (IS_ERR(skb))
  127. skb = NULL;
  128. while (count) {
  129. int i, len;
  130. /* remove padding bytes from buffer */
  131. for (; hu->padding && count > 0; hu->padding--) {
  132. count--;
  133. buffer++;
  134. }
  135. if (!count)
  136. break;
  137. if (!skb) {
  138. for (i = 0; i < pkts_count; i++) {
  139. if (buffer[0] != (&pkts[i])->type)
  140. continue;
  141. skb = bt_skb_alloc((&pkts[i])->maxlen,
  142. GFP_ATOMIC);
  143. if (!skb)
  144. return ERR_PTR(-ENOMEM);
  145. hci_skb_pkt_type(skb) = (&pkts[i])->type;
  146. hci_skb_expect(skb) = (&pkts[i])->hlen;
  147. break;
  148. }
  149. /* Check for invalid packet type */
  150. if (!skb)
  151. return ERR_PTR(-EILSEQ);
  152. count -= 1;
  153. buffer += 1;
  154. }
  155. len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
  156. skb_put_data(skb, buffer, len);
  157. count -= len;
  158. buffer += len;
  159. /* Check for partial packet */
  160. if (skb->len < hci_skb_expect(skb))
  161. continue;
  162. for (i = 0; i < pkts_count; i++) {
  163. if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
  164. break;
  165. }
  166. if (i >= pkts_count) {
  167. kfree_skb(skb);
  168. return ERR_PTR(-EILSEQ);
  169. }
  170. if (skb->len == (&pkts[i])->hlen) {
  171. u16 dlen;
  172. switch ((&pkts[i])->lsize) {
  173. case 0:
  174. /* No variable data length */
  175. dlen = 0;
  176. break;
  177. case 1:
  178. /* Single octet variable length */
  179. dlen = skb->data[(&pkts[i])->loff];
  180. hci_skb_expect(skb) += dlen;
  181. if (skb_tailroom(skb) < dlen) {
  182. kfree_skb(skb);
  183. return ERR_PTR(-EMSGSIZE);
  184. }
  185. break;
  186. case 2:
  187. /* Double octet variable length */
  188. dlen = get_unaligned_le16(skb->data +
  189. (&pkts[i])->loff);
  190. hci_skb_expect(skb) += dlen;
  191. if (skb_tailroom(skb) < dlen) {
  192. kfree_skb(skb);
  193. return ERR_PTR(-EMSGSIZE);
  194. }
  195. break;
  196. default:
  197. /* Unsupported variable length */
  198. kfree_skb(skb);
  199. return ERR_PTR(-EILSEQ);
  200. }
  201. if (!dlen) {
  202. hu->padding = (skb->len + 1) % alignment;
  203. hu->padding = (alignment - hu->padding) % alignment;
  204. /* No more data, complete frame */
  205. (&pkts[i])->recv(hdev, skb);
  206. skb = NULL;
  207. }
  208. } else {
  209. hu->padding = (skb->len + 1) % alignment;
  210. hu->padding = (alignment - hu->padding) % alignment;
  211. /* Complete frame */
  212. (&pkts[i])->recv(hdev, skb);
  213. skb = NULL;
  214. }
  215. }
  216. return skb;
  217. }
  218. EXPORT_SYMBOL_GPL(h4_recv_buf);