hci_h5.c 14 KB

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