hci_bcsp.c 19 KB

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