hci_bcsp.c 18 KB

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