hci_h4.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. *
  3. * Bluetooth HCI UART driver
  4. *
  5. * Copyright (C) 2000-2001 Qualcomm Incorporated
  6. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  7. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/poll.h>
  33. #include <linux/slab.h>
  34. #include <linux/tty.h>
  35. #include <linux/errno.h>
  36. #include <linux/string.h>
  37. #include <linux/signal.h>
  38. #include <linux/ioctl.h>
  39. #include <linux/skbuff.h>
  40. #include <asm/unaligned.h>
  41. #include <net/bluetooth/bluetooth.h>
  42. #include <net/bluetooth/hci_core.h>
  43. #include "hci_uart.h"
  44. struct h4_struct {
  45. struct sk_buff *rx_skb;
  46. struct sk_buff_head txq;
  47. };
  48. /* Initialize protocol */
  49. static int h4_open(struct hci_uart *hu)
  50. {
  51. struct h4_struct *h4;
  52. BT_DBG("hu %p", hu);
  53. h4 = kzalloc(sizeof(*h4), GFP_KERNEL);
  54. if (!h4)
  55. return -ENOMEM;
  56. skb_queue_head_init(&h4->txq);
  57. hu->priv = h4;
  58. return 0;
  59. }
  60. /* Flush protocol data */
  61. static int h4_flush(struct hci_uart *hu)
  62. {
  63. struct h4_struct *h4 = hu->priv;
  64. BT_DBG("hu %p", hu);
  65. skb_queue_purge(&h4->txq);
  66. return 0;
  67. }
  68. /* Close protocol */
  69. static int h4_close(struct hci_uart *hu)
  70. {
  71. struct h4_struct *h4 = hu->priv;
  72. hu->priv = NULL;
  73. BT_DBG("hu %p", hu);
  74. skb_queue_purge(&h4->txq);
  75. kfree_skb(h4->rx_skb);
  76. hu->priv = NULL;
  77. kfree(h4);
  78. return 0;
  79. }
  80. /* Enqueue frame for transmittion (padding, crc, etc) */
  81. static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  82. {
  83. struct h4_struct *h4 = hu->priv;
  84. BT_DBG("hu %p skb %p", hu, skb);
  85. /* Prepend skb with frame type */
  86. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  87. skb_queue_tail(&h4->txq, skb);
  88. return 0;
  89. }
  90. static const struct h4_recv_pkt h4_recv_pkts[] = {
  91. { H4_RECV_ACL, .recv = hci_recv_frame },
  92. { H4_RECV_SCO, .recv = hci_recv_frame },
  93. { H4_RECV_EVENT, .recv = hci_recv_frame },
  94. };
  95. /* Recv data */
  96. static int h4_recv(struct hci_uart *hu, const void *data, int count)
  97. {
  98. struct h4_struct *h4 = hu->priv;
  99. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  100. return -EUNATCH;
  101. h4->rx_skb = h4_recv_buf(hu->hdev, h4->rx_skb, data, count,
  102. h4_recv_pkts, ARRAY_SIZE(h4_recv_pkts));
  103. if (IS_ERR(h4->rx_skb)) {
  104. int err = PTR_ERR(h4->rx_skb);
  105. bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
  106. h4->rx_skb = NULL;
  107. return err;
  108. }
  109. return count;
  110. }
  111. static struct sk_buff *h4_dequeue(struct hci_uart *hu)
  112. {
  113. struct h4_struct *h4 = hu->priv;
  114. return skb_dequeue(&h4->txq);
  115. }
  116. static const struct hci_uart_proto h4p = {
  117. .id = HCI_UART_H4,
  118. .name = "H4",
  119. .open = h4_open,
  120. .close = h4_close,
  121. .recv = h4_recv,
  122. .enqueue = h4_enqueue,
  123. .dequeue = h4_dequeue,
  124. .flush = h4_flush,
  125. };
  126. int __init h4_init(void)
  127. {
  128. return hci_uart_register_proto(&h4p);
  129. }
  130. int __exit h4_deinit(void)
  131. {
  132. return hci_uart_unregister_proto(&h4p);
  133. }
  134. struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
  135. const unsigned char *buffer, int count,
  136. const struct h4_recv_pkt *pkts, int pkts_count)
  137. {
  138. struct hci_uart *hu = hci_get_drvdata(hdev);
  139. u8 alignment = hu->alignment ? hu->alignment : 1;
  140. /* Check for error from previous call */
  141. if (IS_ERR(skb))
  142. skb = NULL;
  143. while (count) {
  144. int i, len;
  145. /* remove padding bytes from buffer */
  146. for (; hu->padding && count > 0; hu->padding--) {
  147. count--;
  148. buffer++;
  149. }
  150. if (!count)
  151. break;
  152. if (!skb) {
  153. for (i = 0; i < pkts_count; i++) {
  154. if (buffer[0] != (&pkts[i])->type)
  155. continue;
  156. skb = bt_skb_alloc((&pkts[i])->maxlen,
  157. GFP_ATOMIC);
  158. if (!skb)
  159. return ERR_PTR(-ENOMEM);
  160. hci_skb_pkt_type(skb) = (&pkts[i])->type;
  161. hci_skb_expect(skb) = (&pkts[i])->hlen;
  162. break;
  163. }
  164. /* Check for invalid packet type */
  165. if (!skb)
  166. return ERR_PTR(-EILSEQ);
  167. count -= 1;
  168. buffer += 1;
  169. }
  170. len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
  171. skb_put_data(skb, buffer, len);
  172. count -= len;
  173. buffer += len;
  174. /* Check for partial packet */
  175. if (skb->len < hci_skb_expect(skb))
  176. continue;
  177. for (i = 0; i < pkts_count; i++) {
  178. if (hci_skb_pkt_type(skb) == (&pkts[i])->type)
  179. break;
  180. }
  181. if (i >= pkts_count) {
  182. kfree_skb(skb);
  183. return ERR_PTR(-EILSEQ);
  184. }
  185. if (skb->len == (&pkts[i])->hlen) {
  186. u16 dlen;
  187. switch ((&pkts[i])->lsize) {
  188. case 0:
  189. /* No variable data length */
  190. dlen = 0;
  191. break;
  192. case 1:
  193. /* Single octet variable length */
  194. dlen = skb->data[(&pkts[i])->loff];
  195. hci_skb_expect(skb) += dlen;
  196. if (skb_tailroom(skb) < dlen) {
  197. kfree_skb(skb);
  198. return ERR_PTR(-EMSGSIZE);
  199. }
  200. break;
  201. case 2:
  202. /* Double octet variable length */
  203. dlen = get_unaligned_le16(skb->data +
  204. (&pkts[i])->loff);
  205. hci_skb_expect(skb) += dlen;
  206. if (skb_tailroom(skb) < dlen) {
  207. kfree_skb(skb);
  208. return ERR_PTR(-EMSGSIZE);
  209. }
  210. break;
  211. default:
  212. /* Unsupported variable length */
  213. kfree_skb(skb);
  214. return ERR_PTR(-EILSEQ);
  215. }
  216. if (!dlen) {
  217. hu->padding = (skb->len - 1) % alignment;
  218. hu->padding = (alignment - hu->padding) % alignment;
  219. /* No more data, complete frame */
  220. (&pkts[i])->recv(hdev, skb);
  221. skb = NULL;
  222. }
  223. } else {
  224. hu->padding = (skb->len - 1) % alignment;
  225. hu->padding = (alignment - hu->padding) % alignment;
  226. /* Complete frame */
  227. (&pkts[i])->recv(hdev, skb);
  228. skb = NULL;
  229. }
  230. }
  231. return skb;
  232. }
  233. EXPORT_SYMBOL_GPL(h4_recv_buf);