hci_bcsp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth HCI UART driver
  5. *
  6. * Copyright (C) 2002-2003 Fabrizio Gennari <fabrizio.gennari@philips.com>
  7. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/tty.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/signal.h>
  22. #include <linux/ioctl.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/bitrev.h>
  25. #include <asm/unaligned.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "hci_uart.h"
  29. static bool txcrc = true;
  30. static bool hciextn = true;
  31. #define BCSP_TXWINSIZE 4
  32. #define BCSP_ACK_PKT 0x05
  33. #define BCSP_LE_PKT 0x06
  34. struct bcsp_struct {
  35. struct sk_buff_head unack; /* Unack'ed packets queue */
  36. struct sk_buff_head rel; /* Reliable packets queue */
  37. struct sk_buff_head unrel; /* Unreliable packets queue */
  38. unsigned long rx_count;
  39. struct sk_buff *rx_skb;
  40. u8 rxseq_txack; /* rxseq == txack. */
  41. u8 rxack; /* Last packet sent by us that the peer ack'ed */
  42. struct timer_list tbcsp;
  43. struct hci_uart *hu;
  44. enum {
  45. BCSP_W4_PKT_DELIMITER,
  46. BCSP_W4_PKT_START,
  47. BCSP_W4_BCSP_HDR,
  48. BCSP_W4_DATA,
  49. BCSP_W4_CRC
  50. } rx_state;
  51. enum {
  52. BCSP_ESCSTATE_NOESC,
  53. BCSP_ESCSTATE_ESC
  54. } rx_esc_state;
  55. u8 use_crc;
  56. u16 message_crc;
  57. u8 txack_req; /* Do we need to send ack's to the peer? */
  58. /* Reliable packet sequence number - used to assign seq to each rel pkt. */
  59. u8 msgq_txseq;
  60. };
  61. /* ---- BCSP CRC calculation ---- */
  62. /* Table for calculating CRC for polynomial 0x1021, LSB processed first,
  63. * initial value 0xffff, bits shifted in reverse order.
  64. */
  65. static const u16 crc_table[] = {
  66. 0x0000, 0x1081, 0x2102, 0x3183,
  67. 0x4204, 0x5285, 0x6306, 0x7387,
  68. 0x8408, 0x9489, 0xa50a, 0xb58b,
  69. 0xc60c, 0xd68d, 0xe70e, 0xf78f
  70. };
  71. /* Initialise the crc calculator */
  72. #define BCSP_CRC_INIT(x) x = 0xffff
  73. /* Update crc with next data byte
  74. *
  75. * Implementation note
  76. * The data byte is treated as two nibbles. The crc is generated
  77. * in reverse, i.e., bits are fed into the register from the top.
  78. */
  79. static void bcsp_crc_update(u16 *crc, u8 d)
  80. {
  81. u16 reg = *crc;
  82. reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
  83. reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
  84. *crc = reg;
  85. }
  86. /* ---- BCSP core ---- */
  87. static void bcsp_slip_msgdelim(struct sk_buff *skb)
  88. {
  89. const char pkt_delim = 0xc0;
  90. skb_put_data(skb, &pkt_delim, 1);
  91. }
  92. static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
  93. {
  94. const char esc_c0[2] = { 0xdb, 0xdc };
  95. const char esc_db[2] = { 0xdb, 0xdd };
  96. switch (c) {
  97. case 0xc0:
  98. skb_put_data(skb, &esc_c0, 2);
  99. break;
  100. case 0xdb:
  101. skb_put_data(skb, &esc_db, 2);
  102. break;
  103. default:
  104. skb_put_data(skb, &c, 1);
  105. }
  106. }
  107. static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  108. {
  109. struct bcsp_struct *bcsp = hu->priv;
  110. if (skb->len > 0xFFF) {
  111. BT_ERR("Packet too long");
  112. kfree_skb(skb);
  113. return 0;
  114. }
  115. switch (hci_skb_pkt_type(skb)) {
  116. case HCI_ACLDATA_PKT:
  117. case HCI_COMMAND_PKT:
  118. skb_queue_tail(&bcsp->rel, skb);
  119. break;
  120. case HCI_SCODATA_PKT:
  121. skb_queue_tail(&bcsp->unrel, skb);
  122. break;
  123. default:
  124. BT_ERR("Unknown packet type");
  125. kfree_skb(skb);
  126. break;
  127. }
  128. return 0;
  129. }
  130. static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data,
  131. int len, int pkt_type)
  132. {
  133. struct sk_buff *nskb;
  134. u8 hdr[4], chan;
  135. u16 BCSP_CRC_INIT(bcsp_txmsg_crc);
  136. int rel, i;
  137. switch (pkt_type) {
  138. case HCI_ACLDATA_PKT:
  139. chan = 6; /* BCSP ACL channel */
  140. rel = 1; /* reliable channel */
  141. break;
  142. case HCI_COMMAND_PKT:
  143. chan = 5; /* BCSP cmd/evt channel */
  144. rel = 1; /* reliable channel */
  145. break;
  146. case HCI_SCODATA_PKT:
  147. chan = 7; /* BCSP SCO channel */
  148. rel = 0; /* unreliable channel */
  149. break;
  150. case BCSP_LE_PKT:
  151. chan = 1; /* BCSP LE channel */
  152. rel = 0; /* unreliable channel */
  153. break;
  154. case BCSP_ACK_PKT:
  155. chan = 0; /* BCSP internal channel */
  156. rel = 0; /* unreliable channel */
  157. break;
  158. default:
  159. BT_ERR("Unknown packet type");
  160. return NULL;
  161. }
  162. if (hciextn && chan == 5) {
  163. __le16 opcode = ((struct hci_command_hdr *)data)->opcode;
  164. /* Vendor specific commands */
  165. if (hci_opcode_ogf(__le16_to_cpu(opcode)) == 0x3f) {
  166. u8 desc = *(data + HCI_COMMAND_HDR_SIZE);
  167. if ((desc & 0xf0) == 0xc0) {
  168. data += HCI_COMMAND_HDR_SIZE + 1;
  169. len -= HCI_COMMAND_HDR_SIZE + 1;
  170. chan = desc & 0x0f;
  171. }
  172. }
  173. }
  174. /* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2
  175. * (because bytes 0xc0 and 0xdb are escaped, worst case is
  176. * when the packet is all made of 0xc0 and 0xdb :) )
  177. * + 2 (0xc0 delimiters at start and end).
  178. */
  179. nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  180. if (!nskb)
  181. return NULL;
  182. hci_skb_pkt_type(nskb) = pkt_type;
  183. bcsp_slip_msgdelim(nskb);
  184. hdr[0] = bcsp->rxseq_txack << 3;
  185. bcsp->txack_req = 0;
  186. BT_DBG("We request packet no %u to card", bcsp->rxseq_txack);
  187. if (rel) {
  188. hdr[0] |= 0x80 + bcsp->msgq_txseq;
  189. BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq);
  190. bcsp->msgq_txseq = (bcsp->msgq_txseq + 1) & 0x07;
  191. }
  192. if (bcsp->use_crc)
  193. hdr[0] |= 0x40;
  194. hdr[1] = ((len << 4) & 0xff) | chan;
  195. hdr[2] = len >> 4;
  196. hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
  197. /* Put BCSP header */
  198. for (i = 0; i < 4; i++) {
  199. bcsp_slip_one_byte(nskb, hdr[i]);
  200. if (bcsp->use_crc)
  201. bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
  202. }
  203. /* Put payload */
  204. for (i = 0; i < len; i++) {
  205. bcsp_slip_one_byte(nskb, data[i]);
  206. if (bcsp->use_crc)
  207. bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
  208. }
  209. /* Put CRC */
  210. if (bcsp->use_crc) {
  211. bcsp_txmsg_crc = bitrev16(bcsp_txmsg_crc);
  212. bcsp_slip_one_byte(nskb, (u8)((bcsp_txmsg_crc >> 8) & 0x00ff));
  213. bcsp_slip_one_byte(nskb, (u8)(bcsp_txmsg_crc & 0x00ff));
  214. }
  215. bcsp_slip_msgdelim(nskb);
  216. return nskb;
  217. }
  218. /* This is a rewrite of pkt_avail in ABCSP */
  219. static struct sk_buff *bcsp_dequeue(struct hci_uart *hu)
  220. {
  221. struct bcsp_struct *bcsp = hu->priv;
  222. unsigned long flags;
  223. struct sk_buff *skb;
  224. /* First of all, check for unreliable messages in the queue,
  225. * since they have priority
  226. */
  227. skb = skb_dequeue(&bcsp->unrel);
  228. if (skb != NULL) {
  229. struct sk_buff *nskb;
  230. nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len,
  231. hci_skb_pkt_type(skb));
  232. if (nskb) {
  233. kfree_skb(skb);
  234. return nskb;
  235. } else {
  236. skb_queue_head(&bcsp->unrel, skb);
  237. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  238. }
  239. }
  240. /* Now, try to send a reliable pkt. We can only send a
  241. * reliable packet if the number of packets sent but not yet ack'ed
  242. * is < than the winsize
  243. */
  244. spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
  245. if (bcsp->unack.qlen < BCSP_TXWINSIZE) {
  246. skb = skb_dequeue(&bcsp->rel);
  247. if (skb != NULL) {
  248. struct sk_buff *nskb;
  249. nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len,
  250. hci_skb_pkt_type(skb));
  251. if (nskb) {
  252. __skb_queue_tail(&bcsp->unack, skb);
  253. mod_timer(&bcsp->tbcsp, jiffies + HZ / 4);
  254. spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  255. return nskb;
  256. } else {
  257. skb_queue_head(&bcsp->rel, skb);
  258. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  259. }
  260. }
  261. }
  262. spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  263. /* We could not send a reliable packet, either because there are
  264. * none or because there are too many unack'ed pkts. Did we receive
  265. * any packets we have not acknowledged yet ?
  266. */
  267. if (bcsp->txack_req) {
  268. /* if so, craft an empty ACK pkt and send it on BCSP unreliable
  269. * channel 0
  270. */
  271. struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT);
  272. return nskb;
  273. }
  274. /* We have nothing to send */
  275. return NULL;
  276. }
  277. static int bcsp_flush(struct hci_uart *hu)
  278. {
  279. BT_DBG("hu %p", hu);
  280. return 0;
  281. }
  282. /* Remove ack'ed packets */
  283. static void bcsp_pkt_cull(struct bcsp_struct *bcsp)
  284. {
  285. struct sk_buff *skb, *tmp;
  286. unsigned long flags;
  287. int i, pkts_to_be_removed;
  288. u8 seqno;
  289. spin_lock_irqsave(&bcsp->unack.lock, flags);
  290. pkts_to_be_removed = skb_queue_len(&bcsp->unack);
  291. seqno = bcsp->msgq_txseq;
  292. while (pkts_to_be_removed) {
  293. if (bcsp->rxack == seqno)
  294. break;
  295. pkts_to_be_removed--;
  296. seqno = (seqno - 1) & 0x07;
  297. }
  298. if (bcsp->rxack != seqno)
  299. BT_ERR("Peer acked invalid packet");
  300. BT_DBG("Removing %u pkts out of %u, up to seqno %u",
  301. pkts_to_be_removed, skb_queue_len(&bcsp->unack),
  302. (seqno - 1) & 0x07);
  303. i = 0;
  304. skb_queue_walk_safe(&bcsp->unack, skb, tmp) {
  305. if (i >= pkts_to_be_removed)
  306. break;
  307. i++;
  308. __skb_unlink(skb, &bcsp->unack);
  309. kfree_skb(skb);
  310. }
  311. if (skb_queue_empty(&bcsp->unack))
  312. del_timer(&bcsp->tbcsp);
  313. spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  314. if (i != pkts_to_be_removed)
  315. BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
  316. }
  317. /* Handle BCSP link-establishment packets. When we
  318. * detect a "sync" packet, symptom that the BT module has reset,
  319. * we do nothing :) (yet)
  320. */
  321. static void bcsp_handle_le_pkt(struct hci_uart *hu)
  322. {
  323. struct bcsp_struct *bcsp = hu->priv;
  324. u8 conf_pkt[4] = { 0xad, 0xef, 0xac, 0xed };
  325. u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
  326. u8 sync_pkt[4] = { 0xda, 0xdc, 0xed, 0xed };
  327. /* spot "conf" pkts and reply with a "conf rsp" pkt */
  328. if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
  329. !memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) {
  330. struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC);
  331. BT_DBG("Found a LE conf pkt");
  332. if (!nskb)
  333. return;
  334. skb_put_data(nskb, conf_rsp_pkt, 4);
  335. hci_skb_pkt_type(nskb) = BCSP_LE_PKT;
  336. skb_queue_head(&bcsp->unrel, nskb);
  337. hci_uart_tx_wakeup(hu);
  338. }
  339. /* Spot "sync" pkts. If we find one...disaster! */
  340. else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
  341. !memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) {
  342. BT_ERR("Found a LE sync pkt, card has reset");
  343. }
  344. }
  345. static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte)
  346. {
  347. const u8 c0 = 0xc0, db = 0xdb;
  348. switch (bcsp->rx_esc_state) {
  349. case BCSP_ESCSTATE_NOESC:
  350. switch (byte) {
  351. case 0xdb:
  352. bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
  353. break;
  354. default:
  355. skb_put_data(bcsp->rx_skb, &byte, 1);
  356. if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
  357. bcsp->rx_state != BCSP_W4_CRC)
  358. bcsp_crc_update(&bcsp->message_crc, byte);
  359. bcsp->rx_count--;
  360. }
  361. break;
  362. case BCSP_ESCSTATE_ESC:
  363. switch (byte) {
  364. case 0xdc:
  365. skb_put_data(bcsp->rx_skb, &c0, 1);
  366. if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
  367. bcsp->rx_state != BCSP_W4_CRC)
  368. bcsp_crc_update(&bcsp->message_crc, 0xc0);
  369. bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
  370. bcsp->rx_count--;
  371. break;
  372. case 0xdd:
  373. skb_put_data(bcsp->rx_skb, &db, 1);
  374. if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
  375. bcsp->rx_state != BCSP_W4_CRC)
  376. bcsp_crc_update(&bcsp->message_crc, 0xdb);
  377. bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
  378. bcsp->rx_count--;
  379. break;
  380. default:
  381. BT_ERR("Invalid byte %02x after esc byte", byte);
  382. kfree_skb(bcsp->rx_skb);
  383. bcsp->rx_skb = NULL;
  384. bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  385. bcsp->rx_count = 0;
  386. }
  387. }
  388. }
  389. static void bcsp_complete_rx_pkt(struct hci_uart *hu)
  390. {
  391. struct bcsp_struct *bcsp = hu->priv;
  392. int pass_up = 0;
  393. if (bcsp->rx_skb->data[0] & 0x80) { /* reliable pkt */
  394. BT_DBG("Received seqno %u from card", bcsp->rxseq_txack);
  395. /* check the rx sequence number is as expected */
  396. if ((bcsp->rx_skb->data[0] & 0x07) == bcsp->rxseq_txack) {
  397. bcsp->rxseq_txack++;
  398. bcsp->rxseq_txack %= 0x8;
  399. } else {
  400. /* handle re-transmitted packet or
  401. * when packet was missed
  402. */
  403. BT_ERR("Out-of-order packet arrived, got %u expected %u",
  404. bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack);
  405. /* do not process out-of-order packet payload */
  406. pass_up = 2;
  407. }
  408. /* send current txack value to all received reliable packets */
  409. bcsp->txack_req = 1;
  410. /* If needed, transmit an ack pkt */
  411. hci_uart_tx_wakeup(hu);
  412. }
  413. bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07;
  414. BT_DBG("Request for pkt %u from card", bcsp->rxack);
  415. /* handle received ACK indications,
  416. * including those from out-of-order packets
  417. */
  418. bcsp_pkt_cull(bcsp);
  419. if (pass_up != 2) {
  420. if ((bcsp->rx_skb->data[1] & 0x0f) == 6 &&
  421. (bcsp->rx_skb->data[0] & 0x80)) {
  422. hci_skb_pkt_type(bcsp->rx_skb) = HCI_ACLDATA_PKT;
  423. pass_up = 1;
  424. } else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 &&
  425. (bcsp->rx_skb->data[0] & 0x80)) {
  426. hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT;
  427. pass_up = 1;
  428. } else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) {
  429. hci_skb_pkt_type(bcsp->rx_skb) = HCI_SCODATA_PKT;
  430. pass_up = 1;
  431. } else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 &&
  432. !(bcsp->rx_skb->data[0] & 0x80)) {
  433. bcsp_handle_le_pkt(hu);
  434. pass_up = 0;
  435. } else {
  436. pass_up = 0;
  437. }
  438. }
  439. if (pass_up == 0) {
  440. struct hci_event_hdr hdr;
  441. u8 desc = (bcsp->rx_skb->data[1] & 0x0f);
  442. if (desc != 0 && desc != 1) {
  443. if (hciextn) {
  444. desc |= 0xc0;
  445. skb_pull(bcsp->rx_skb, 4);
  446. memcpy(skb_push(bcsp->rx_skb, 1), &desc, 1);
  447. hdr.evt = 0xff;
  448. hdr.plen = bcsp->rx_skb->len;
  449. memcpy(skb_push(bcsp->rx_skb, HCI_EVENT_HDR_SIZE), &hdr, HCI_EVENT_HDR_SIZE);
  450. hci_skb_pkt_type(bcsp->rx_skb) = HCI_EVENT_PKT;
  451. hci_recv_frame(hu->hdev, bcsp->rx_skb);
  452. } else {
  453. BT_ERR("Packet for unknown channel (%u %s)",
  454. bcsp->rx_skb->data[1] & 0x0f,
  455. bcsp->rx_skb->data[0] & 0x80 ?
  456. "reliable" : "unreliable");
  457. kfree_skb(bcsp->rx_skb);
  458. }
  459. } else
  460. kfree_skb(bcsp->rx_skb);
  461. } else if (pass_up == 1) {
  462. /* Pull out BCSP hdr */
  463. skb_pull(bcsp->rx_skb, 4);
  464. hci_recv_frame(hu->hdev, bcsp->rx_skb);
  465. } else {
  466. /* ignore packet payload of already ACKed re-transmitted
  467. * packets or when a packet was missed in the BCSP window
  468. */
  469. kfree_skb(bcsp->rx_skb);
  470. }
  471. bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  472. bcsp->rx_skb = NULL;
  473. }
  474. static u16 bscp_get_crc(struct bcsp_struct *bcsp)
  475. {
  476. return get_unaligned_be16(&bcsp->rx_skb->data[bcsp->rx_skb->len - 2]);
  477. }
  478. /* Recv data */
  479. static int bcsp_recv(struct hci_uart *hu, const void *data, int count)
  480. {
  481. struct bcsp_struct *bcsp = hu->priv;
  482. const unsigned char *ptr;
  483. BT_DBG("hu %p count %d rx_state %d rx_count %ld",
  484. hu, count, bcsp->rx_state, bcsp->rx_count);
  485. ptr = data;
  486. while (count) {
  487. if (bcsp->rx_count) {
  488. if (*ptr == 0xc0) {
  489. BT_ERR("Short BCSP packet");
  490. kfree_skb(bcsp->rx_skb);
  491. bcsp->rx_skb = NULL;
  492. bcsp->rx_state = BCSP_W4_PKT_START;
  493. bcsp->rx_count = 0;
  494. } else
  495. bcsp_unslip_one_byte(bcsp, *ptr);
  496. ptr++; count--;
  497. continue;
  498. }
  499. switch (bcsp->rx_state) {
  500. case BCSP_W4_BCSP_HDR:
  501. if ((0xff & (u8)~(bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] +
  502. bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) {
  503. BT_ERR("Error in BCSP hdr checksum");
  504. kfree_skb(bcsp->rx_skb);
  505. bcsp->rx_skb = NULL;
  506. bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  507. bcsp->rx_count = 0;
  508. continue;
  509. }
  510. bcsp->rx_state = BCSP_W4_DATA;
  511. bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) +
  512. (bcsp->rx_skb->data[2] << 4); /* May be 0 */
  513. continue;
  514. case BCSP_W4_DATA:
  515. if (bcsp->rx_skb->data[0] & 0x40) { /* pkt with crc */
  516. bcsp->rx_state = BCSP_W4_CRC;
  517. bcsp->rx_count = 2;
  518. } else
  519. bcsp_complete_rx_pkt(hu);
  520. continue;
  521. case BCSP_W4_CRC:
  522. if (bitrev16(bcsp->message_crc) != bscp_get_crc(bcsp)) {
  523. BT_ERR("Checksum failed: computed %04x received %04x",
  524. bitrev16(bcsp->message_crc),
  525. bscp_get_crc(bcsp));
  526. kfree_skb(bcsp->rx_skb);
  527. bcsp->rx_skb = NULL;
  528. bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  529. bcsp->rx_count = 0;
  530. continue;
  531. }
  532. skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2);
  533. bcsp_complete_rx_pkt(hu);
  534. continue;
  535. case BCSP_W4_PKT_DELIMITER:
  536. switch (*ptr) {
  537. case 0xc0:
  538. bcsp->rx_state = BCSP_W4_PKT_START;
  539. break;
  540. default:
  541. /*BT_ERR("Ignoring byte %02x", *ptr);*/
  542. break;
  543. }
  544. ptr++; count--;
  545. break;
  546. case BCSP_W4_PKT_START:
  547. switch (*ptr) {
  548. case 0xc0:
  549. ptr++; count--;
  550. break;
  551. default:
  552. bcsp->rx_state = BCSP_W4_BCSP_HDR;
  553. bcsp->rx_count = 4;
  554. bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
  555. BCSP_CRC_INIT(bcsp->message_crc);
  556. /* Do not increment ptr or decrement count
  557. * Allocate packet. Max len of a BCSP pkt=
  558. * 0xFFF (payload) +4 (header) +2 (crc)
  559. */
  560. bcsp->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
  561. if (!bcsp->rx_skb) {
  562. BT_ERR("Can't allocate mem for new packet");
  563. bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  564. bcsp->rx_count = 0;
  565. return 0;
  566. }
  567. break;
  568. }
  569. break;
  570. }
  571. }
  572. return count;
  573. }
  574. /* Arrange to retransmit all messages in the relq. */
  575. static void bcsp_timed_event(struct timer_list *t)
  576. {
  577. struct bcsp_struct *bcsp = from_timer(bcsp, t, tbcsp);
  578. struct hci_uart *hu = bcsp->hu;
  579. struct sk_buff *skb;
  580. unsigned long flags;
  581. BT_DBG("hu %p retransmitting %u pkts", hu, bcsp->unack.qlen);
  582. spin_lock_irqsave_nested(&bcsp->unack.lock, flags, SINGLE_DEPTH_NESTING);
  583. while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) {
  584. bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07;
  585. skb_queue_head(&bcsp->rel, skb);
  586. }
  587. spin_unlock_irqrestore(&bcsp->unack.lock, flags);
  588. hci_uart_tx_wakeup(hu);
  589. }
  590. static int bcsp_open(struct hci_uart *hu)
  591. {
  592. struct bcsp_struct *bcsp;
  593. BT_DBG("hu %p", hu);
  594. bcsp = kzalloc(sizeof(*bcsp), GFP_KERNEL);
  595. if (!bcsp)
  596. return -ENOMEM;
  597. hu->priv = bcsp;
  598. bcsp->hu = hu;
  599. skb_queue_head_init(&bcsp->unack);
  600. skb_queue_head_init(&bcsp->rel);
  601. skb_queue_head_init(&bcsp->unrel);
  602. timer_setup(&bcsp->tbcsp, bcsp_timed_event, 0);
  603. bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
  604. if (txcrc)
  605. bcsp->use_crc = 1;
  606. return 0;
  607. }
  608. static int bcsp_close(struct hci_uart *hu)
  609. {
  610. struct bcsp_struct *bcsp = hu->priv;
  611. del_timer_sync(&bcsp->tbcsp);
  612. hu->priv = NULL;
  613. BT_DBG("hu %p", hu);
  614. skb_queue_purge(&bcsp->unack);
  615. skb_queue_purge(&bcsp->rel);
  616. skb_queue_purge(&bcsp->unrel);
  617. if (bcsp->rx_skb) {
  618. kfree_skb(bcsp->rx_skb);
  619. bcsp->rx_skb = NULL;
  620. }
  621. kfree(bcsp);
  622. return 0;
  623. }
  624. static const struct hci_uart_proto bcsp = {
  625. .id = HCI_UART_BCSP,
  626. .name = "BCSP",
  627. .open = bcsp_open,
  628. .close = bcsp_close,
  629. .enqueue = bcsp_enqueue,
  630. .dequeue = bcsp_dequeue,
  631. .recv = bcsp_recv,
  632. .flush = bcsp_flush
  633. };
  634. int __init bcsp_init(void)
  635. {
  636. return hci_uart_register_proto(&bcsp);
  637. }
  638. int __exit bcsp_deinit(void)
  639. {
  640. return hci_uart_unregister_proto(&bcsp);
  641. }
  642. module_param(txcrc, bool, 0644);
  643. MODULE_PARM_DESC(txcrc, "Transmit CRC with every BCSP packet");
  644. module_param(hciextn, bool, 0644);
  645. MODULE_PARM_DESC(hciextn, "Convert HCI Extensions into BCSP packets");