hci_h5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. *
  3. * Bluetooth HCI Three-wire UART driver
  4. *
  5. * Copyright (C) 2012 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 <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "hci_uart.h"
  29. #define HCI_3WIRE_ACK_PKT 0
  30. #define HCI_3WIRE_LINK_PKT 15
  31. #define H5_TXWINSIZE 4
  32. #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
  33. /*
  34. * Maximum Three-wire packet:
  35. * 4 byte header + max value for 12-bit length + 2 bytes for CRC
  36. */
  37. #define H5_MAX_LEN (4 + 0xfff + 2)
  38. #define SLIP_DELIMITER 0xc0
  39. #define SLIP_ESC 0xdb
  40. #define SLIP_ESC_DELIM 0xdc
  41. #define SLIP_ESC_ESC 0xdd
  42. struct h5 {
  43. struct sk_buff_head unack; /* Unack'ed packets queue */
  44. struct sk_buff_head rel; /* Reliable packets queue */
  45. struct sk_buff_head unrel; /* Unreliable packets queue */
  46. struct sk_buff *rx_skb; /* Receive buffer */
  47. size_t rx_pending; /* Expecting more bytes */
  48. bool rx_esc; /* SLIP escape mode */
  49. int (*rx_func) (struct hci_uart *hu, u8 c);
  50. struct timer_list timer; /* Retransmission timer */
  51. bool txack_req;
  52. u8 next_ack;
  53. u8 next_seq;
  54. };
  55. static void h5_reset_rx(struct h5 *h5);
  56. static void h5_timed_event(unsigned long arg)
  57. {
  58. struct hci_uart *hu = (struct hci_uart *) arg;
  59. struct h5 *h5 = hu->priv;
  60. struct sk_buff *skb;
  61. unsigned long flags;
  62. BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
  63. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  64. while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
  65. h5->next_seq = (h5->next_seq - 1) & 0x07;
  66. skb_queue_head(&h5->rel, skb);
  67. }
  68. spin_unlock_irqrestore(&h5->unack.lock, flags);
  69. hci_uart_tx_wakeup(hu);
  70. }
  71. static int h5_open(struct hci_uart *hu)
  72. {
  73. struct h5 *h5;
  74. BT_DBG("hu %p", hu);
  75. h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
  76. if (!h5)
  77. return -ENOMEM;
  78. hu->priv = h5;
  79. skb_queue_head_init(&h5->unack);
  80. skb_queue_head_init(&h5->rel);
  81. skb_queue_head_init(&h5->unrel);
  82. h5_reset_rx(h5);
  83. init_timer(&h5->timer);
  84. h5->timer.function = h5_timed_event;
  85. h5->timer.data = (unsigned long) hu;
  86. return 0;
  87. }
  88. static int h5_close(struct hci_uart *hu)
  89. {
  90. struct h5 *h5 = hu->priv;
  91. skb_queue_purge(&h5->unack);
  92. skb_queue_purge(&h5->rel);
  93. skb_queue_purge(&h5->unrel);
  94. del_timer(&h5->timer);
  95. kfree(h5);
  96. return 0;
  97. }
  98. static void h5_handle_internal_rx(struct hci_uart *hu)
  99. {
  100. BT_DBG("%s", hu->hdev->name);
  101. }
  102. static void h5_complete_rx_pkt(struct hci_uart *hu)
  103. {
  104. struct h5 *h5 = hu->priv;
  105. u8 pkt_type;
  106. BT_DBG("%s", hu->hdev->name);
  107. pkt_type = h5->rx_skb->data[1] & 0x0f;
  108. switch (pkt_type) {
  109. case HCI_EVENT_PKT:
  110. case HCI_ACLDATA_PKT:
  111. case HCI_SCODATA_PKT:
  112. bt_cb(h5->rx_skb)->pkt_type = pkt_type;
  113. /* Remove Three-wire header */
  114. skb_pull(h5->rx_skb, 4);
  115. hci_recv_frame(h5->rx_skb);
  116. h5->rx_skb = NULL;
  117. break;
  118. default:
  119. h5_handle_internal_rx(hu);
  120. break;
  121. }
  122. h5_reset_rx(h5);
  123. }
  124. static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
  125. {
  126. struct h5 *h5 = hu->priv;
  127. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  128. h5_complete_rx_pkt(hu);
  129. h5_reset_rx(h5);
  130. return 0;
  131. }
  132. static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
  133. {
  134. struct h5 *h5 = hu->priv;
  135. const unsigned char *hdr = h5->rx_skb->data;
  136. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  137. if ((hdr[0] >> 4) & 0x01) {
  138. h5->rx_func = h5_rx_crc;
  139. h5->rx_pending = 2;
  140. } else {
  141. h5_complete_rx_pkt(hu);
  142. h5_reset_rx(h5);
  143. }
  144. return 0;
  145. }
  146. static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
  147. {
  148. struct h5 *h5 = hu->priv;
  149. const unsigned char *hdr = h5->rx_skb->data;
  150. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  151. if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
  152. BT_ERR("Invalid header checksum");
  153. h5_reset_rx(h5);
  154. return 0;
  155. }
  156. h5->rx_func = h5_rx_payload;
  157. h5->rx_pending = ((hdr[1] >> 4) & 0xff) + (hdr[2] << 4);
  158. return 0;
  159. }
  160. static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
  161. {
  162. struct h5 *h5 = hu->priv;
  163. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  164. if (c == SLIP_DELIMITER)
  165. return 1;
  166. h5->rx_func = h5_rx_3wire_hdr;
  167. h5->rx_pending = 4;
  168. h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
  169. if (!h5->rx_skb) {
  170. BT_ERR("Can't allocate mem for new packet");
  171. h5_reset_rx(h5);
  172. return -ENOMEM;
  173. }
  174. h5->rx_skb->dev = (void *) hu->hdev;
  175. return 0;
  176. }
  177. static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
  178. {
  179. struct h5 *h5 = hu->priv;
  180. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  181. if (c == SLIP_DELIMITER)
  182. h5->rx_func = h5_rx_pkt_start;
  183. return 1;
  184. }
  185. static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
  186. {
  187. const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
  188. const u8 *byte = &c;
  189. if (!h5->rx_esc && c == SLIP_ESC) {
  190. h5->rx_esc = true;
  191. return;
  192. }
  193. if (h5->rx_esc) {
  194. switch (c) {
  195. case SLIP_ESC_DELIM:
  196. byte = &delim;
  197. break;
  198. case SLIP_ESC_ESC:
  199. byte = &esc;
  200. break;
  201. default:
  202. BT_ERR("Invalid esc byte 0x%02hhx", c);
  203. h5_reset_rx(h5);
  204. return;
  205. }
  206. h5->rx_esc = false;
  207. }
  208. memcpy(skb_put(h5->rx_skb, 1), byte, 1);
  209. h5->rx_pending--;
  210. BT_DBG("unsliped 0x%02hhx", *byte);
  211. }
  212. static void h5_reset_rx(struct h5 *h5)
  213. {
  214. if (h5->rx_skb) {
  215. kfree_skb(h5->rx_skb);
  216. h5->rx_skb = NULL;
  217. }
  218. h5->rx_func = h5_rx_delimiter;
  219. h5->rx_pending = 0;
  220. h5->rx_esc = false;
  221. }
  222. static int h5_recv(struct hci_uart *hu, void *data, int count)
  223. {
  224. struct h5 *h5 = hu->priv;
  225. unsigned char *ptr = data;
  226. BT_DBG("%s count %d", hu->hdev->name, count);
  227. while (count > 0) {
  228. int processed;
  229. if (h5->rx_pending > 0) {
  230. if (*ptr == SLIP_DELIMITER) {
  231. BT_ERR("Too short H5 packet");
  232. h5_reset_rx(h5);
  233. continue;
  234. }
  235. h5_unslip_one_byte(h5, *ptr);
  236. ptr++; count--;
  237. continue;
  238. }
  239. processed = h5->rx_func(hu, *ptr);
  240. if (processed < 0)
  241. return processed;
  242. ptr += processed;
  243. count -= processed;
  244. }
  245. return 0;
  246. }
  247. static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  248. {
  249. struct h5 *h5 = hu->priv;
  250. if (skb->len > 0xfff) {
  251. BT_ERR("Packet too long (%u bytes)", skb->len);
  252. kfree_skb(skb);
  253. return 0;
  254. }
  255. switch (bt_cb(skb)->pkt_type) {
  256. case HCI_ACLDATA_PKT:
  257. case HCI_COMMAND_PKT:
  258. skb_queue_tail(&h5->rel, skb);
  259. break;
  260. case HCI_SCODATA_PKT:
  261. skb_queue_tail(&h5->unrel, skb);
  262. break;
  263. default:
  264. BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
  265. kfree_skb(skb);
  266. break;
  267. }
  268. return 0;
  269. }
  270. static void h5_slip_delim(struct sk_buff *skb)
  271. {
  272. const char delim = SLIP_DELIMITER;
  273. memcpy(skb_put(skb, 1), &delim, 1);
  274. }
  275. static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
  276. {
  277. const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
  278. const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
  279. switch (c) {
  280. case SLIP_DELIMITER:
  281. memcpy(skb_put(skb, 2), &esc_delim, 2);
  282. break;
  283. case SLIP_ESC:
  284. memcpy(skb_put(skb, 2), &esc_esc, 2);
  285. break;
  286. default:
  287. memcpy(skb_put(skb, 1), &c, 1);
  288. }
  289. }
  290. static struct sk_buff *h5_build_pkt(struct h5 *h5, bool rel, u8 pkt_type,
  291. const u8 *data, size_t len)
  292. {
  293. struct sk_buff *nskb;
  294. u8 hdr[4];
  295. int i;
  296. /*
  297. * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
  298. * (because bytes 0xc0 and 0xdb are escaped, worst case is when
  299. * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
  300. * delimiters at start and end).
  301. */
  302. nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  303. if (!nskb)
  304. return NULL;
  305. bt_cb(nskb)->pkt_type = pkt_type;
  306. h5_slip_delim(nskb);
  307. hdr[0] = h5->next_ack << 3;
  308. h5->txack_req = false;
  309. if (rel) {
  310. hdr[0] |= 1 << 7;
  311. hdr[0] |= h5->next_seq;
  312. h5->next_seq = (h5->next_seq + 1) % 8;
  313. }
  314. hdr[1] = pkt_type | ((len & 0x0f) << 4);
  315. hdr[2] = len >> 4;
  316. hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
  317. for (i = 0; i < 4; i++)
  318. h5_slip_one_byte(nskb, hdr[i]);
  319. for (i = 0; i < len; i++)
  320. h5_slip_one_byte(nskb, data[i]);
  321. h5_slip_delim(nskb);
  322. return nskb;
  323. }
  324. static struct sk_buff *h5_prepare_pkt(struct h5 *h5, u8 pkt_type,
  325. const u8 *data, size_t len)
  326. {
  327. bool rel;
  328. switch (pkt_type) {
  329. case HCI_ACLDATA_PKT:
  330. case HCI_COMMAND_PKT:
  331. rel = true;
  332. break;
  333. case HCI_SCODATA_PKT:
  334. case HCI_3WIRE_LINK_PKT:
  335. case HCI_3WIRE_ACK_PKT:
  336. rel = false;
  337. break;
  338. default:
  339. BT_ERR("Unknown packet type %u", pkt_type);
  340. return NULL;
  341. }
  342. return h5_build_pkt(h5, rel, pkt_type, data, len);
  343. }
  344. static struct sk_buff *h5_prepare_ack(struct h5 *h5)
  345. {
  346. h5->txack_req = false;
  347. return NULL;
  348. }
  349. static struct sk_buff *h5_dequeue(struct hci_uart *hu)
  350. {
  351. struct h5 *h5 = hu->priv;
  352. unsigned long flags;
  353. struct sk_buff *skb, *nskb;
  354. if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
  355. nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type,
  356. skb->data, skb->len);
  357. if (nskb) {
  358. kfree_skb(skb);
  359. return nskb;
  360. }
  361. skb_queue_head(&h5->unrel, skb);
  362. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  363. }
  364. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  365. if (h5->unack.qlen >= H5_TXWINSIZE)
  366. goto unlock;
  367. if ((skb = skb_dequeue(&h5->rel)) != NULL) {
  368. nskb = h5_prepare_pkt(h5, bt_cb(skb)->pkt_type,
  369. skb->data, skb->len);
  370. if (nskb) {
  371. __skb_queue_tail(&h5->unack, skb);
  372. mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
  373. spin_unlock_irqrestore(&h5->unack.lock, flags);
  374. return nskb;
  375. }
  376. skb_queue_head(&h5->rel, skb);
  377. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  378. }
  379. unlock:
  380. spin_unlock_irqrestore(&h5->unack.lock, flags);
  381. if (h5->txack_req)
  382. return h5_prepare_ack(h5);
  383. return NULL;
  384. }
  385. static int h5_flush(struct hci_uart *hu)
  386. {
  387. BT_DBG("hu %p", hu);
  388. return 0;
  389. }
  390. static struct hci_uart_proto h5p = {
  391. .id = HCI_UART_3WIRE,
  392. .open = h5_open,
  393. .close = h5_close,
  394. .recv = h5_recv,
  395. .enqueue = h5_enqueue,
  396. .dequeue = h5_dequeue,
  397. .flush = h5_flush,
  398. };
  399. int __init h5_init(void)
  400. {
  401. int err = hci_uart_register_proto(&h5p);
  402. if (!err)
  403. BT_INFO("HCI Three-wire UART (H5) protocol initialized");
  404. else
  405. BT_ERR("HCI Three-wire UART (H5) protocol init failed");
  406. return err;
  407. }
  408. int __exit h5_deinit(void)
  409. {
  410. return hci_uart_unregister_proto(&h5p);
  411. }