hci_h4.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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), &bt_cb(skb)->pkt_type, 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_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, 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. while (count) {
  139. int i, len;
  140. if (!skb) {
  141. for (i = 0; i < pkts_count; i++) {
  142. if (buffer[0] != (&pkts[i])->type)
  143. continue;
  144. skb = bt_skb_alloc((&pkts[i])->maxlen,
  145. GFP_ATOMIC);
  146. if (!skb)
  147. return ERR_PTR(-ENOMEM);
  148. bt_cb(skb)->pkt_type = (&pkts[i])->type;
  149. bt_cb(skb)->expect = (&pkts[i])->hlen;
  150. break;
  151. }
  152. /* Check for invalid packet type */
  153. if (!skb)
  154. return ERR_PTR(-EILSEQ);
  155. count -= 1;
  156. buffer += 1;
  157. }
  158. len = min_t(uint, bt_cb(skb)->expect - skb->len, count);
  159. memcpy(skb_put(skb, len), buffer, len);
  160. count -= len;
  161. buffer += len;
  162. /* Check for partial packet */
  163. if (skb->len < bt_cb(skb)->expect)
  164. continue;
  165. for (i = 0; i < pkts_count; i++) {
  166. if (bt_cb(skb)->pkt_type == (&pkts[i])->type)
  167. break;
  168. }
  169. if (i >= pkts_count) {
  170. kfree_skb(skb);
  171. return ERR_PTR(-EILSEQ);
  172. }
  173. if (skb->len == (&pkts[i])->hlen) {
  174. u16 dlen;
  175. switch ((&pkts[i])->lsize) {
  176. case 0:
  177. /* No variable data length */
  178. dlen = 0;
  179. break;
  180. case 1:
  181. /* Single octet variable length */
  182. dlen = skb->data[(&pkts[i])->loff];
  183. bt_cb(skb)->expect += dlen;
  184. if (skb_tailroom(skb) < dlen) {
  185. kfree_skb(skb);
  186. return ERR_PTR(-EMSGSIZE);
  187. }
  188. break;
  189. case 2:
  190. /* Double octet variable length */
  191. dlen = get_unaligned_le16(skb->data +
  192. (&pkts[i])->loff);
  193. bt_cb(skb)->expect += dlen;
  194. if (skb_tailroom(skb) < dlen) {
  195. kfree_skb(skb);
  196. return ERR_PTR(-EMSGSIZE);
  197. }
  198. break;
  199. default:
  200. /* Unsupported variable length */
  201. kfree_skb(skb);
  202. return ERR_PTR(-EILSEQ);
  203. }
  204. if (!dlen) {
  205. /* No more data, complete frame */
  206. (&pkts[i])->recv(hdev, skb);
  207. skb = NULL;
  208. }
  209. } else {
  210. /* Complete frame */
  211. (&pkts[i])->recv(hdev, skb);
  212. skb = NULL;
  213. }
  214. }
  215. return skb;
  216. }
  217. EXPORT_SYMBOL_GPL(h4_recv_buf);