hci_h5.c 11 KB

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