hci_bcsp.c 19 KB

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