hci_h5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. BT_DBG("%s", hu->hdev->name);
  190. if (H5_HDR_RELIABLE(hdr)) {
  191. h5->tx_ack = (h5->tx_ack + 1) % 8;
  192. h5->tx_ack_req = true;
  193. hci_uart_tx_wakeup(hu);
  194. }
  195. h5->rx_ack = H5_HDR_ACK(hdr);
  196. h5_pkt_cull(h5);
  197. switch (H5_HDR_PKT_TYPE(hdr)) {
  198. case HCI_EVENT_PKT:
  199. case HCI_ACLDATA_PKT:
  200. case HCI_SCODATA_PKT:
  201. bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr);
  202. /* Remove Three-wire header */
  203. skb_pull(h5->rx_skb, 4);
  204. hci_recv_frame(h5->rx_skb);
  205. h5->rx_skb = NULL;
  206. break;
  207. default:
  208. h5_handle_internal_rx(hu);
  209. break;
  210. }
  211. h5_reset_rx(h5);
  212. }
  213. static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
  214. {
  215. struct h5 *h5 = hu->priv;
  216. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  217. h5_complete_rx_pkt(hu);
  218. h5_reset_rx(h5);
  219. return 0;
  220. }
  221. static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
  222. {
  223. struct h5 *h5 = hu->priv;
  224. const unsigned char *hdr = h5->rx_skb->data;
  225. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  226. if (H5_HDR_CRC(hdr)) {
  227. h5->rx_func = h5_rx_crc;
  228. h5->rx_pending = 2;
  229. } else {
  230. h5_complete_rx_pkt(hu);
  231. h5_reset_rx(h5);
  232. }
  233. return 0;
  234. }
  235. static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
  236. {
  237. struct h5 *h5 = hu->priv;
  238. const unsigned char *hdr = h5->rx_skb->data;
  239. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  240. BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u",
  241. hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
  242. H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
  243. H5_HDR_LEN(hdr));
  244. if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
  245. BT_ERR("Invalid header checksum");
  246. h5_reset_rx(h5);
  247. return 0;
  248. }
  249. if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
  250. BT_ERR("Out-of-order packet arrived (%u != %u)",
  251. H5_HDR_SEQ(hdr), h5->tx_ack);
  252. h5_reset_rx(h5);
  253. return 0;
  254. }
  255. h5->rx_func = h5_rx_payload;
  256. h5->rx_pending = H5_HDR_LEN(hdr);
  257. return 0;
  258. }
  259. static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
  260. {
  261. struct h5 *h5 = hu->priv;
  262. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  263. if (c == SLIP_DELIMITER)
  264. return 1;
  265. h5->rx_func = h5_rx_3wire_hdr;
  266. h5->rx_pending = 4;
  267. h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
  268. if (!h5->rx_skb) {
  269. BT_ERR("Can't allocate mem for new packet");
  270. h5_reset_rx(h5);
  271. return -ENOMEM;
  272. }
  273. h5->rx_skb->dev = (void *) hu->hdev;
  274. return 0;
  275. }
  276. static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
  277. {
  278. struct h5 *h5 = hu->priv;
  279. BT_DBG("%s 0x%02hhx", hu->hdev->name, c);
  280. if (c == SLIP_DELIMITER)
  281. h5->rx_func = h5_rx_pkt_start;
  282. return 1;
  283. }
  284. static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
  285. {
  286. const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
  287. const u8 *byte = &c;
  288. if (!h5->rx_esc && c == SLIP_ESC) {
  289. h5->rx_esc = true;
  290. return;
  291. }
  292. if (h5->rx_esc) {
  293. switch (c) {
  294. case SLIP_ESC_DELIM:
  295. byte = &delim;
  296. break;
  297. case SLIP_ESC_ESC:
  298. byte = &esc;
  299. break;
  300. default:
  301. BT_ERR("Invalid esc byte 0x%02hhx", c);
  302. h5_reset_rx(h5);
  303. return;
  304. }
  305. h5->rx_esc = false;
  306. }
  307. memcpy(skb_put(h5->rx_skb, 1), byte, 1);
  308. h5->rx_pending--;
  309. BT_DBG("unsliped 0x%02hhx", *byte);
  310. }
  311. static void h5_reset_rx(struct h5 *h5)
  312. {
  313. if (h5->rx_skb) {
  314. kfree_skb(h5->rx_skb);
  315. h5->rx_skb = NULL;
  316. }
  317. h5->rx_func = h5_rx_delimiter;
  318. h5->rx_pending = 0;
  319. h5->rx_esc = false;
  320. }
  321. static int h5_recv(struct hci_uart *hu, void *data, int count)
  322. {
  323. struct h5 *h5 = hu->priv;
  324. unsigned char *ptr = data;
  325. BT_DBG("%s count %d", hu->hdev->name, count);
  326. while (count > 0) {
  327. int processed;
  328. if (h5->rx_pending > 0) {
  329. if (*ptr == SLIP_DELIMITER) {
  330. BT_ERR("Too short H5 packet");
  331. h5_reset_rx(h5);
  332. continue;
  333. }
  334. h5_unslip_one_byte(h5, *ptr);
  335. ptr++; count--;
  336. continue;
  337. }
  338. processed = h5->rx_func(hu, *ptr);
  339. if (processed < 0)
  340. return processed;
  341. ptr += processed;
  342. count -= processed;
  343. }
  344. return 0;
  345. }
  346. static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  347. {
  348. struct h5 *h5 = hu->priv;
  349. if (skb->len > 0xfff) {
  350. BT_ERR("Packet too long (%u bytes)", skb->len);
  351. kfree_skb(skb);
  352. return 0;
  353. }
  354. switch (bt_cb(skb)->pkt_type) {
  355. case HCI_ACLDATA_PKT:
  356. case HCI_COMMAND_PKT:
  357. skb_queue_tail(&h5->rel, skb);
  358. break;
  359. case HCI_SCODATA_PKT:
  360. skb_queue_tail(&h5->unrel, skb);
  361. break;
  362. default:
  363. BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
  364. kfree_skb(skb);
  365. break;
  366. }
  367. return 0;
  368. }
  369. static void h5_slip_delim(struct sk_buff *skb)
  370. {
  371. const char delim = SLIP_DELIMITER;
  372. memcpy(skb_put(skb, 1), &delim, 1);
  373. }
  374. static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
  375. {
  376. const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
  377. const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
  378. switch (c) {
  379. case SLIP_DELIMITER:
  380. memcpy(skb_put(skb, 2), &esc_delim, 2);
  381. break;
  382. case SLIP_ESC:
  383. memcpy(skb_put(skb, 2), &esc_esc, 2);
  384. break;
  385. default:
  386. memcpy(skb_put(skb, 1), &c, 1);
  387. }
  388. }
  389. static struct sk_buff *h5_build_pkt(struct hci_uart *hu, bool rel, u8 pkt_type,
  390. const u8 *data, size_t len)
  391. {
  392. struct h5 *h5 = hu->priv;
  393. struct sk_buff *nskb;
  394. u8 hdr[4];
  395. int i;
  396. /*
  397. * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
  398. * (because bytes 0xc0 and 0xdb are escaped, worst case is when
  399. * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
  400. * delimiters at start and end).
  401. */
  402. nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  403. if (!nskb)
  404. return NULL;
  405. bt_cb(nskb)->pkt_type = pkt_type;
  406. h5_slip_delim(nskb);
  407. hdr[0] = h5->tx_ack << 3;
  408. h5->tx_ack_req = false;
  409. if (rel) {
  410. hdr[0] |= 1 << 7;
  411. hdr[0] |= h5->tx_seq;
  412. h5->tx_seq = (h5->tx_seq + 1) % 8;
  413. }
  414. hdr[1] = pkt_type | ((len & 0x0f) << 4);
  415. hdr[2] = len >> 4;
  416. hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
  417. BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u",
  418. hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
  419. H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
  420. H5_HDR_LEN(hdr));
  421. for (i = 0; i < 4; i++)
  422. h5_slip_one_byte(nskb, hdr[i]);
  423. for (i = 0; i < len; i++)
  424. h5_slip_one_byte(nskb, data[i]);
  425. h5_slip_delim(nskb);
  426. return nskb;
  427. }
  428. static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
  429. const u8 *data, size_t len)
  430. {
  431. bool rel;
  432. switch (pkt_type) {
  433. case HCI_ACLDATA_PKT:
  434. case HCI_COMMAND_PKT:
  435. rel = true;
  436. break;
  437. case HCI_SCODATA_PKT:
  438. case HCI_3WIRE_LINK_PKT:
  439. case HCI_3WIRE_ACK_PKT:
  440. rel = false;
  441. break;
  442. default:
  443. BT_ERR("Unknown packet type %u", pkt_type);
  444. return NULL;
  445. }
  446. return h5_build_pkt(hu, rel, pkt_type, data, len);
  447. }
  448. static struct sk_buff *h5_dequeue(struct hci_uart *hu)
  449. {
  450. struct h5 *h5 = hu->priv;
  451. unsigned long flags;
  452. struct sk_buff *skb, *nskb;
  453. if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
  454. nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
  455. skb->data, skb->len);
  456. if (nskb) {
  457. kfree_skb(skb);
  458. return nskb;
  459. }
  460. skb_queue_head(&h5->unrel, skb);
  461. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  462. }
  463. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  464. if (h5->unack.qlen >= H5_TXWINSIZE)
  465. goto unlock;
  466. if ((skb = skb_dequeue(&h5->rel)) != NULL) {
  467. nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
  468. skb->data, skb->len);
  469. if (nskb) {
  470. __skb_queue_tail(&h5->unack, skb);
  471. mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
  472. spin_unlock_irqrestore(&h5->unack.lock, flags);
  473. return nskb;
  474. }
  475. skb_queue_head(&h5->rel, skb);
  476. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  477. }
  478. unlock:
  479. spin_unlock_irqrestore(&h5->unack.lock, flags);
  480. if (h5->tx_ack_req)
  481. return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
  482. return NULL;
  483. }
  484. static int h5_flush(struct hci_uart *hu)
  485. {
  486. BT_DBG("hu %p", hu);
  487. return 0;
  488. }
  489. static struct hci_uart_proto h5p = {
  490. .id = HCI_UART_3WIRE,
  491. .open = h5_open,
  492. .close = h5_close,
  493. .recv = h5_recv,
  494. .enqueue = h5_enqueue,
  495. .dequeue = h5_dequeue,
  496. .flush = h5_flush,
  497. };
  498. int __init h5_init(void)
  499. {
  500. int err = hci_uart_register_proto(&h5p);
  501. if (!err)
  502. BT_INFO("HCI Three-wire UART (H5) protocol initialized");
  503. else
  504. BT_ERR("HCI Three-wire UART (H5) protocol init failed");
  505. return err;
  506. }
  507. int __exit h5_deinit(void)
  508. {
  509. return hci_uart_unregister_proto(&h5p);
  510. }