hci_h5.c 12 KB

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