hci_rtk_h5.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. *
  3. * Bluetooth HCI UART driver
  4. *
  5. * Copyright (C) 2011-2014 wifi_fae<wifi_fae@realtek.com.tw>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/poll.h>
  31. #include <linux/slab.h>
  32. #include <linux/tty.h>
  33. #include <linux/errno.h>
  34. #include <linux/string.h>
  35. #include <linux/signal.h>
  36. #include <linux/ioctl.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/bitrev.h>
  39. #include <asm/unaligned.h>
  40. #include <net/bluetooth/bluetooth.h>
  41. #include <net/bluetooth/hci_core.h>
  42. #include <linux/version.h>
  43. #include "hci_uart.h"
  44. #ifdef BTCOEX
  45. #include "rtk_coex.h"
  46. #endif
  47. //#define VERSION "1.0"
  48. static int txcrc = 1;
  49. //static int hciextn = 1;
  50. #define H5_TXWINSIZE 4
  51. #define H5_ACK_PKT 0x00
  52. #define H5_LE_PKT 0x0F
  53. #define H5_VDRSPEC_PKT 0x0E
  54. struct h5_struct {
  55. struct sk_buff_head unack; /* Unack'ed packets queue */
  56. struct sk_buff_head rel; /* Reliable packets queue */
  57. struct sk_buff_head unrel; /* Unreliable packets queue */
  58. unsigned long rx_count;
  59. struct sk_buff *rx_skb;
  60. struct delayed_work retrans_work;
  61. struct hci_uart *hu; /* Parent HCI UART */
  62. enum {
  63. H5_W4_PKT_DELIMITER,
  64. H5_W4_PKT_START,
  65. H5_W4_HDR,
  66. H5_W4_DATA,
  67. H5_W4_CRC
  68. } rx_state;
  69. enum {
  70. H5_ESCSTATE_NOESC,
  71. H5_ESCSTATE_ESC
  72. } rx_esc_state;
  73. u16 message_crc;
  74. u8 use_crc;
  75. u8 rxack; /* Last packet sent by us that the peer ack'ed */
  76. u8 rxseq_txack; /* rxseq == txack. */
  77. u8 txack_req; /* Do we need to send ack's to the peer? */
  78. /* Reliable packet sequence number - used to assign seq to each rel pkt. */
  79. u8 msgq_txseq;
  80. /* The spin lock protects seq, ack and ack req */
  81. spinlock_t lock;
  82. };
  83. /* ---- H5 CRC calculation ---- */
  84. /* Table for calculating CRC for polynomial 0x1021, LSB processed first,
  85. initial value 0xffff, bits shifted in reverse order. */
  86. static const u16 crc_table[] = {
  87. 0x0000, 0x1081, 0x2102, 0x3183,
  88. 0x4204, 0x5285, 0x6306, 0x7387,
  89. 0x8408, 0x9489, 0xa50a, 0xb58b,
  90. 0xc60c, 0xd68d, 0xe70e, 0xf78f
  91. };
  92. /* Initialise the crc calculator */
  93. #define H5_CRC_INIT(x) x = 0xffff
  94. /*
  95. Update crc with next data byte
  96. Implementation note
  97. The data byte is treated as two nibbles. The crc is generated
  98. in reverse, i.e., bits are fed into the register from the top.
  99. */
  100. static void h5_crc_update(u16 * crc, u8 d)
  101. {
  102. u16 reg = *crc;
  103. reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
  104. reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
  105. *crc = reg;
  106. }
  107. /* ---- H5 core ---- */
  108. static void h5_slip_msgdelim(struct sk_buff *skb)
  109. {
  110. const char pkt_delim = 0xc0;
  111. memcpy(skb_put(skb, 1), &pkt_delim, 1);
  112. }
  113. static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
  114. {
  115. const char esc_c0[2] = { 0xdb, 0xdc };
  116. const char esc_db[2] = { 0xdb, 0xdd };
  117. const char esc_11[2] = { 0xdb, 0xde };
  118. const char esc_13[2] = { 0xdb, 0xdf };
  119. switch (c) {
  120. case 0xc0:
  121. memcpy(skb_put(skb, 2), &esc_c0, 2);
  122. break;
  123. case 0xdb:
  124. memcpy(skb_put(skb, 2), &esc_db, 2);
  125. break;
  126. case 0x11:
  127. memcpy(skb_put(skb, 2), &esc_11, 2);
  128. break;
  129. case 0x13:
  130. memcpy(skb_put(skb, 2), &esc_13, 2);
  131. break;
  132. default:
  133. memcpy(skb_put(skb, 1), &c, 1);
  134. }
  135. }
  136. static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  137. {
  138. struct h5_struct *h5 = hu->priv;
  139. if (skb->len > 0xFFF) { //Pkt length must be less than 4095 bytes
  140. BT_ERR("Packet too long");
  141. kfree_skb(skb);
  142. return 0;
  143. }
  144. switch (bt_cb(skb)->pkt_type) {
  145. case HCI_ACLDATA_PKT:
  146. case HCI_COMMAND_PKT:
  147. skb_queue_tail(&h5->rel, skb);
  148. break;
  149. case HCI_SCODATA_PKT:
  150. skb_queue_tail(&h5->unrel, skb);
  151. break;
  152. case H5_LE_PKT:
  153. case H5_ACK_PKT:
  154. case H5_VDRSPEC_PKT:
  155. skb_queue_tail(&h5->unrel, skb); /* 3-wire LinkEstablishment */
  156. break;
  157. default:
  158. BT_ERR("Unknown packet type");
  159. kfree_skb(skb);
  160. break;
  161. }
  162. return 0;
  163. }
  164. static struct sk_buff *h5_prepare_pkt(struct h5_struct *h5, u8 * data,
  165. int len, int pkt_type)
  166. {
  167. struct sk_buff *nskb;
  168. u8 hdr[4], chan;
  169. u16 H5_CRC_INIT(h5_txmsg_crc);
  170. int rel, i;
  171. u8 tmp;
  172. unsigned long flags;
  173. switch (pkt_type) {
  174. case HCI_ACLDATA_PKT:
  175. chan = 2; /* 3-wire ACL channel */
  176. rel = 1; /* reliable channel */
  177. break;
  178. case HCI_COMMAND_PKT:
  179. chan = 1; /* 3-wire cmd channel */
  180. rel = 1; /* reliable channel */
  181. break;
  182. case HCI_EVENT_PKT:
  183. chan = 4; /* 3-wire cmd channel */
  184. rel = 1; /* reliable channel */
  185. break;
  186. case HCI_SCODATA_PKT:
  187. chan = 3; /* 3-wire SCO channel */
  188. rel = 0; /* unreliable channel */
  189. break;
  190. case H5_LE_PKT:
  191. chan = 15; /* 3-wire LinkEstablishment channel */
  192. rel = 0; /* unreliable channel */
  193. break;
  194. case H5_ACK_PKT:
  195. chan = 0; /* 3-wire ACK channel */
  196. rel = 0; /* unreliable channel */
  197. break;
  198. case H5_VDRSPEC_PKT:
  199. chan = 14; /* 3-wire Vendor Specific channel */
  200. rel = 0; /* unreliable channel */
  201. break;
  202. default:
  203. BT_ERR("Unknown packet type");
  204. return NULL;
  205. }
  206. /* Max len of packet: (original len +4(h5 hdr) +2(crc))*2
  207. (because bytes 0xc0 and 0xdb are escaped, worst case is
  208. when the packet is all made of 0xc0 and 0xdb :) )
  209. + 2 (0xc0 delimiters at start and end). */
  210. nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  211. if (!nskb)
  212. return NULL;
  213. bt_cb(nskb)->pkt_type = pkt_type;
  214. h5_slip_msgdelim(nskb);
  215. spin_lock_irqsave(&h5->lock, flags);
  216. tmp = h5->rxseq_txack;
  217. hdr[0] = h5->rxseq_txack << 3;
  218. h5->txack_req = 0;
  219. spin_unlock_irqrestore(&h5->lock, flags);
  220. BT_DBG("We request packet no %u to card", tmp);
  221. if (rel) {
  222. spin_lock_irqsave(&h5->lock, flags);
  223. tmp = h5->msgq_txseq;
  224. hdr[0] |= 0x80 + h5->msgq_txseq;
  225. h5->msgq_txseq = (h5->msgq_txseq + 1) & 0x07;
  226. spin_unlock_irqrestore(&h5->lock, flags);
  227. BT_DBG("Sending packet with seqno %u", tmp);
  228. }
  229. if (h5->use_crc)
  230. hdr[0] |= 0x40;
  231. hdr[1] = ((len << 4) & 0xff) | chan;
  232. hdr[2] = len >> 4;
  233. hdr[3] = ~(hdr[0] + hdr[1] + hdr[2]);
  234. /* Put H5 header */
  235. for (i = 0; i < 4; i++) {
  236. h5_slip_one_byte(nskb, hdr[i]);
  237. if (h5->use_crc)
  238. h5_crc_update(&h5_txmsg_crc, hdr[i]);
  239. }
  240. /* Put payload */
  241. for (i = 0; i < len; i++) {
  242. h5_slip_one_byte(nskb, data[i]);
  243. if (h5->use_crc)
  244. h5_crc_update(&h5_txmsg_crc, data[i]);
  245. }
  246. /* Put CRC */
  247. if (h5->use_crc) {
  248. h5_txmsg_crc = bitrev16(h5_txmsg_crc);
  249. h5_slip_one_byte(nskb, (u8) ((h5_txmsg_crc >> 8) & 0x00ff));
  250. h5_slip_one_byte(nskb, (u8) (h5_txmsg_crc & 0x00ff));
  251. }
  252. h5_slip_msgdelim(nskb);
  253. return nskb;
  254. }
  255. /* This is a rewrite of pkt_avail in AH5 */
  256. static struct sk_buff *h5_dequeue(struct hci_uart *hu)
  257. {
  258. struct h5_struct *h5 = hu->priv;
  259. unsigned long flags;
  260. struct sk_buff *skb;
  261. /* First of all, check for unreliable messages in the queue,
  262. since they have priority */
  263. if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
  264. struct sk_buff *nskb =
  265. h5_prepare_pkt(h5, skb->data, skb->len,
  266. bt_cb(skb)->pkt_type);
  267. if (nskb) {
  268. kfree_skb(skb);
  269. return nskb;
  270. } else {
  271. skb_queue_head(&h5->unrel, skb);
  272. BT_ERR
  273. ("Could not dequeue pkt because alloc_skb failed");
  274. }
  275. }
  276. /* Now, try to send a reliable pkt. We can only send a
  277. reliable packet if the number of packets sent but not yet ack'ed
  278. is < than the winsize */
  279. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  280. if (h5->unack.qlen < H5_TXWINSIZE
  281. && (skb = skb_dequeue(&h5->rel)) != NULL) {
  282. struct sk_buff *nskb =
  283. h5_prepare_pkt(h5, skb->data, skb->len,
  284. bt_cb(skb)->pkt_type);
  285. if (nskb) {
  286. __skb_queue_tail(&h5->unack, skb);
  287. schedule_delayed_work(&h5->retrans_work, HZ / 4);
  288. spin_unlock_irqrestore(&h5->unack.lock, flags);
  289. return nskb;
  290. } else {
  291. skb_queue_head(&h5->rel, skb);
  292. BT_ERR
  293. ("Could not dequeue pkt because alloc_skb failed");
  294. }
  295. }
  296. spin_unlock_irqrestore(&h5->unack.lock, flags);
  297. /* We could not send a reliable packet, either because there are
  298. none or because there are too many unack'ed pkts. Did we receive
  299. any packets we have not acknowledged yet ? */
  300. if (h5->txack_req) {
  301. /* if so, craft an empty ACK pkt and send it on H5 unreliable
  302. channel 0 */
  303. struct sk_buff *nskb = h5_prepare_pkt(h5, NULL, 0, H5_ACK_PKT);
  304. return nskb;
  305. }
  306. /* We have nothing to send */
  307. return NULL;
  308. }
  309. static int h5_flush(struct hci_uart *hu)
  310. {
  311. BT_DBG("hu %p", hu);
  312. return 0;
  313. }
  314. /* Remove ack'ed packets */
  315. static void h5_pkt_cull(struct h5_struct *h5)
  316. {
  317. struct sk_buff *skb, *tmp;
  318. unsigned long flags;
  319. int i, pkts_to_be_removed;
  320. u8 seqno;
  321. spin_lock_irqsave(&h5->unack.lock, flags);
  322. pkts_to_be_removed = skb_queue_len(&h5->unack);
  323. seqno = h5->msgq_txseq;
  324. while (pkts_to_be_removed) {
  325. if (h5->rxack == seqno)
  326. break;
  327. pkts_to_be_removed--;
  328. seqno = (seqno - 1) & 0x07;
  329. }
  330. if (h5->rxack != seqno)
  331. BT_ERR("Peer acked invalid packet");
  332. BT_DBG("Removing %u pkts out of %u, up to seqno %u",
  333. pkts_to_be_removed, skb_queue_len(&h5->unack),
  334. (seqno - 1) & 0x07);
  335. i = 0;
  336. skb_queue_walk_safe(&h5->unack, skb, tmp) {
  337. if (i >= pkts_to_be_removed)
  338. break;
  339. i++;
  340. __skb_unlink(skb, &h5->unack);
  341. kfree_skb(skb);
  342. }
  343. if (skb_queue_empty(&h5->unack))
  344. cancel_delayed_work(&h5->retrans_work);
  345. spin_unlock_irqrestore(&h5->unack.lock, flags);
  346. if (i != pkts_to_be_removed)
  347. BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
  348. }
  349. /* Handle H5 link-establishment packets. When we
  350. detect a "sync" packet, symptom that the BT module has reset,
  351. we do nothing :) (yet) */
  352. #if 0
  353. static void h5_handle_le_pkt(struct hci_uart *hu)
  354. {
  355. struct h5_struct *h5 = hu->priv;
  356. u8 conf_pkt[2] = { 0x03, 0xfc };
  357. u8 conf_rsp_pkt[3] = { 0x04, 0x7b, 0x00 };
  358. u8 sync_pkt[2] = { 0x01, 0x7e };
  359. u8 sync_rsp_pkt[2] = { 0x02, 0x7d };
  360. u8 wakeup_pkt[2] = { 0x05, 0xfa };
  361. u8 woken_pkt[2] = { 0x06, 0xf9 };
  362. u8 sleep_pkt[2] = { 0x07, 0x78 };
  363. /* spot "conf" pkts and reply with a "conf rsp" pkt */
  364. if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  365. !memcmp(&h5->rx_skb->data[4], conf_pkt, 2)) {
  366. struct sk_buff *nskb = alloc_skb(3, GFP_ATOMIC);
  367. BT_DBG("Found a LE conf pkt");
  368. if (!nskb)
  369. return;
  370. conf_rsp_pkt[2] |= txcrc << 0x4; //crc check enable, version no = 0. needed to be as avariable.
  371. memcpy(skb_put(nskb, 3), conf_rsp_pkt, 3);
  372. bt_cb(nskb)->pkt_type = H5_LE_PKT;
  373. skb_queue_head(&h5->unrel, nskb);
  374. hci_uart_tx_wakeup(hu);
  375. }
  376. /* spot "conf resp" pkts */
  377. else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  378. !memcmp(&h5->rx_skb->data[4], conf_rsp_pkt, 2)) {
  379. BT_DBG("Found a LE conf resp pkt, device go into active state");
  380. txcrc = (h5->rx_skb->data[6] >> 0x4) & 0x1;
  381. }
  382. /* Spot "sync" pkts. If we find one...disaster! */
  383. else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  384. !memcmp(&h5->rx_skb->data[4], sync_pkt, 2)) {
  385. BT_ERR("Found a LE sync pkt, card has reset");
  386. //DO Something here
  387. }
  388. /* Spot "sync resp" pkts. If we find one...disaster! */
  389. else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  390. !memcmp(&h5->rx_skb->data[4], sync_rsp_pkt, 2)) {
  391. BT_ERR
  392. ("Found a LE sync resp pkt, device go into initialized state");
  393. // DO Something here
  394. }
  395. /* Spot "wakeup" pkts. reply woken message when in active mode */
  396. else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  397. !memcmp(&h5->rx_skb->data[4], wakeup_pkt, 2)) {
  398. struct sk_buff *nskb = alloc_skb(2, GFP_ATOMIC);
  399. BT_ERR("Found a LE Wakeup pkt, and reply woken message");
  400. // DO Something here
  401. memcpy(skb_put(nskb, 2), woken_pkt, 2);
  402. bt_cb(nskb)->pkt_type = H5_LE_PKT;
  403. skb_queue_head(&h5->unrel, nskb);
  404. hci_uart_tx_wakeup(hu);
  405. }
  406. /* Spot "woken" pkts. receive woken message from device */
  407. else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  408. !memcmp(&h5->rx_skb->data[4], woken_pkt, 2)) {
  409. BT_ERR("Found a LE woken pkt from device");
  410. // DO Something here
  411. }
  412. /* Spot "Sleep" pkts */
  413. else if (h5->rx_skb->data[1] >> 4 == 2 && h5->rx_skb->data[2] == 0 &&
  414. !memcmp(&h5->rx_indent: Standard input:620: Error:Unmatched 'else'
  415. skb->data[4], sleep_pkt, 2)) {
  416. BT_ERR("Found a LE Sleep pkt");
  417. // DO Something here
  418. }
  419. }
  420. #endif
  421. static inline void h5_unslip_one_byte(struct h5_struct *h5, unsigned char byte)
  422. {
  423. const u8 c0 = 0xc0, db = 0xdb;
  424. const u8 oof1 = 0x11, oof2 = 0x13;
  425. switch (h5->rx_esc_state) {
  426. case H5_ESCSTATE_NOESC:
  427. switch (byte) {
  428. case 0xdb:
  429. h5->rx_esc_state = H5_ESCSTATE_ESC;
  430. break;
  431. default:
  432. memcpy(skb_put(h5->rx_skb, 1), &byte, 1);
  433. if ((h5->rx_skb->data[0] & 0x40) != 0 &&
  434. h5->rx_state != H5_W4_CRC)
  435. h5_crc_update(&h5->message_crc, byte);
  436. h5->rx_count--;
  437. }
  438. break;
  439. case H5_ESCSTATE_ESC:
  440. switch (byte) {
  441. case 0xdc:
  442. memcpy(skb_put(h5->rx_skb, 1), &c0, 1);
  443. if ((h5->rx_skb->data[0] & 0x40) != 0 &&
  444. h5->rx_state != H5_W4_CRC)
  445. h5_crc_update(&h5->message_crc, 0xc0);
  446. h5->rx_esc_state = H5_ESCSTATE_NOESC;
  447. h5->rx_count--;
  448. break;
  449. case 0xdd:
  450. memcpy(skb_put(h5->rx_skb, 1), &db, 1);
  451. if ((h5->rx_skb->data[0] & 0x40) != 0 &&
  452. h5->rx_state != H5_W4_CRC)
  453. h5_crc_update(&h5->message_crc, 0xdb);
  454. h5->rx_esc_state = H5_ESCSTATE_NOESC;
  455. h5->rx_count--;
  456. break;
  457. case 0xde:
  458. memcpy(skb_put(h5->rx_skb, 1), &oof1, 1);
  459. if ((h5->rx_skb->data[0] & 0x40) != 0
  460. && h5->rx_state != H5_W4_CRC)
  461. h5_crc_update(&h5->message_crc, oof1);
  462. h5->rx_esc_state = H5_ESCSTATE_NOESC;
  463. h5->rx_count--;
  464. break;
  465. case 0xdf:
  466. memcpy(skb_put(h5->rx_skb, 1), &oof2, 1);
  467. if ((h5->rx_skb->data[0] & 0x40) != 0
  468. && h5->rx_state != H5_W4_CRC)
  469. h5_crc_update(&h5->message_crc, oof2);
  470. h5->rx_esc_state = H5_ESCSTATE_NOESC;
  471. h5->rx_count--;
  472. break;
  473. default:
  474. BT_ERR("Invalid byte %02x after esc byte", byte);
  475. kfree_skb(h5->rx_skb);
  476. h5->rx_skb = NULL;
  477. h5->rx_state = H5_W4_PKT_DELIMITER;
  478. h5->rx_count = 0;
  479. }
  480. }
  481. }
  482. static void h5_complete_rx_pkt(struct hci_uart *hu)
  483. {
  484. struct h5_struct *h5 = hu->priv;
  485. int pass_up;
  486. if (h5->rx_skb->data[0] & 0x80) { /* reliable pkt */
  487. unsigned long flags;
  488. u8 rxseq;
  489. spin_lock_irqsave(&h5->lock, flags);
  490. rxseq = h5->rxseq_txack;
  491. h5->rxseq_txack++;
  492. h5->rxseq_txack %= 0x8;
  493. h5->txack_req = 1;
  494. spin_unlock_irqrestore(&h5->lock, flags);
  495. BT_DBG("Received seqno %u from card", rxseq);
  496. }
  497. h5->rxack = (h5->rx_skb->data[0] >> 3) & 0x07;
  498. BT_DBG("Request for pkt %u from card", h5->rxack);
  499. h5_pkt_cull(h5);
  500. hci_uart_tx_wakeup(hu);
  501. if ((h5->rx_skb->data[1] & 0x0f) == 2 && h5->rx_skb->data[0] & 0x80) {
  502. bt_cb(h5->rx_skb)->pkt_type = HCI_ACLDATA_PKT;
  503. pass_up = 1;
  504. } else if ((h5->rx_skb->data[1] & 0x0f) == 4 &&
  505. h5->rx_skb->data[0] & 0x80) {
  506. bt_cb(h5->rx_skb)->pkt_type = HCI_EVENT_PKT;
  507. pass_up = 1;
  508. } else if ((h5->rx_skb->data[1] & 0x0f) == 3) {
  509. bt_cb(h5->rx_skb)->pkt_type = HCI_SCODATA_PKT;
  510. pass_up = 1;
  511. } else if ((h5->rx_skb->data[1] & 0x0f) == 15 &&
  512. !(h5->rx_skb->data[0] & 0x80)) {
  513. //h5_handle_le_pkt(hu);//Link Establishment Pkt
  514. pass_up = 0;
  515. } else if ((h5->rx_skb->data[1] & 0x0f) == 1 &&
  516. h5->rx_skb->data[0] & 0x80) {
  517. bt_cb(h5->rx_skb)->pkt_type = HCI_COMMAND_PKT;
  518. pass_up = 1;
  519. } else if ((h5->rx_skb->data[1] & 0x0f) == 14) {
  520. bt_cb(h5->rx_skb)->pkt_type = H5_VDRSPEC_PKT;
  521. pass_up = 1;
  522. } else
  523. pass_up = 0;
  524. if (!pass_up) {
  525. /* struct hci_event_hdr hdr; */
  526. u8 desc = (h5->rx_skb->data[1] & 0x0f);
  527. if (desc != H5_ACK_PKT && desc != H5_LE_PKT) {
  528. /* if (hciextn) {
  529. * desc |= 0xc0;
  530. * skb_pull(h5->rx_skb, 4);
  531. * memcpy(skb_push(h5->rx_skb, 1), &desc, 1);
  532. * hdr.evt = 0xff;
  533. * hdr.plen = h5->rx_skb->len;
  534. * memcpy(skb_push(h5->rx_skb, HCI_EVENT_HDR_SIZE),
  535. * &hdr, HCI_EVENT_HDR_SIZE);
  536. * bt_cb(h5->rx_skb)->pkt_type = HCI_EVENT_PKT;
  537. * hci_recv_frame(h5->rx_skb);
  538. * } else { */
  539. BT_ERR("Packet for unknown channel (%u %s)",
  540. h5->rx_skb->data[1] & 0x0f,
  541. h5->rx_skb->data[0] & 0x80 ?
  542. "reliable" : "unreliable");
  543. kfree_skb(h5->rx_skb);
  544. /* } */
  545. } else
  546. kfree_skb(h5->rx_skb);
  547. } else {
  548. /* Pull out H5 hdr */
  549. skb_pull(h5->rx_skb, 4);
  550. #ifdef BTCOEX
  551. if (bt_cb(h5->rx_skb)->pkt_type == HCI_EVENT_PKT)
  552. rtk_btcoex_parse_event(h5->rx_skb->data,
  553. h5->rx_skb->len);
  554. if (bt_cb(h5->rx_skb)->pkt_type == HCI_ACLDATA_PKT)
  555. rtk_btcoex_parse_l2cap_data_rx(h5->rx_skb->data,
  556. h5->rx_skb->len);
  557. #endif
  558. #if HCI_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
  559. hci_recv_frame(h5->rx_skb);
  560. #else
  561. hci_recv_frame(hu->hdev, h5->rx_skb);
  562. #endif
  563. }
  564. h5->rx_state = H5_W4_PKT_DELIMITER;
  565. h5->rx_skb = NULL;
  566. }
  567. static u16 bscp_get_crc(struct h5_struct *h5) {
  568. return get_unaligned_be16(&h5->rx_skb->
  569. data[h5->rx_skb->len - 2]);
  570. }
  571. /* Recv data */
  572. static int h5_recv(struct hci_uart *hu, void *data, int count)
  573. {
  574. struct h5_struct *h5 = hu->priv;
  575. register unsigned char *ptr;
  576. u8 rxseq;
  577. unsigned long flags;
  578. BT_DBG("hu %p count %d rx_state %d rx_count %ld",
  579. hu, count, h5->rx_state, h5->rx_count);
  580. ptr = data;
  581. while (count) {
  582. if (h5->rx_count) {
  583. if (*ptr == 0xc0) {
  584. BT_ERR("Short H5 packet");
  585. kfree_skb(h5->rx_skb);
  586. h5->rx_state = H5_W4_PKT_START;
  587. h5->rx_count = 0;
  588. } else
  589. h5_unslip_one_byte(h5, *ptr);
  590. ptr++;
  591. count--;
  592. continue;
  593. }
  594. switch (h5->rx_state) {
  595. case H5_W4_HDR:
  596. if ((0xff & (u8) ~
  597. (h5->rx_skb->data[0] +
  598. h5->rx_skb->data[1] +
  599. h5->rx_skb->data[2])) != h5->rx_skb->data[3]) {
  600. BT_ERR("Error in H5 hdr checksum");
  601. kfree_skb(h5->rx_skb);
  602. h5->rx_state = H5_W4_PKT_DELIMITER;
  603. h5->rx_count = 0;
  604. continue;
  605. }
  606. rxseq = h5->rxseq_txack;
  607. if (h5->rx_skb->data[0] & 0x80 /* reliable pkt */
  608. && (h5->rx_skb->data[0] & 0x07) != rxseq) {
  609. BT_ERR("Out-of-order packet arrived, got %u expected %u",
  610. h5->rx_skb->data[0] & 0x07, rxseq);
  611. spin_lock_irqsave(&h5->lock, flags);
  612. h5->txack_req = 1;
  613. spin_unlock_irqrestore(&h5->lock, flags);
  614. hci_uart_tx_wakeup(hu);
  615. kfree_skb(h5->rx_skb);
  616. h5->rx_state = H5_W4_PKT_DELIMITER;
  617. h5->rx_count = 0;
  618. continue;
  619. }
  620. h5->rx_state = H5_W4_DATA;
  621. h5->rx_count = (h5->rx_skb->data[1] >> 4) + (h5->rx_skb->data[2] << 4); /* May be 0 */
  622. continue;
  623. case H5_W4_DATA:
  624. if (h5->rx_skb->data[0] & 0x40) { /* pkt with crc */
  625. h5->rx_state = H5_W4_CRC;
  626. h5->rx_count = 2;
  627. } else
  628. h5_complete_rx_pkt(hu);
  629. continue;
  630. case H5_W4_CRC:
  631. if (bitrev16(h5->message_crc) != bscp_get_crc(h5)) {
  632. BT_ERR
  633. ("Checksum failed: computed %04x received %04x",
  634. bitrev16(h5->message_crc),
  635. bscp_get_crc(h5));
  636. kfree_skb(h5->rx_skb);
  637. h5->rx_state = H5_W4_PKT_DELIMITER;
  638. h5->rx_count = 0;
  639. continue;
  640. }
  641. skb_trim(h5->rx_skb, h5->rx_skb->len - 2);
  642. h5_complete_rx_pkt(hu);
  643. continue;
  644. case H5_W4_PKT_DELIMITER:
  645. switch (*ptr) {
  646. case 0xc0:
  647. h5->rx_state = H5_W4_PKT_START;
  648. break;
  649. default:
  650. /*BT_ERR("Ignoring byte %02x", *ptr); */
  651. break;
  652. }
  653. ptr++;
  654. count--;
  655. break;
  656. case H5_W4_PKT_START:
  657. switch (*ptr) {
  658. case 0xc0:
  659. ptr++;
  660. count--;
  661. break;
  662. default:
  663. h5->rx_state = H5_W4_HDR;
  664. h5->rx_count = 4;
  665. h5->rx_esc_state = H5_ESCSTATE_NOESC;
  666. H5_CRC_INIT(h5->message_crc);
  667. /* Do not increment ptr or decrement count
  668. * Allocate packet. Max len of a H5 pkt=
  669. * 0xFFF (payload) +4 (header) +2 (crc) */
  670. h5->rx_skb = bt_skb_alloc(0x1005, GFP_ATOMIC);
  671. if (!h5->rx_skb) {
  672. BT_ERR
  673. ("Can't allocate mem for new packet");
  674. h5->rx_state = H5_W4_PKT_DELIMITER;
  675. h5->rx_count = 0;
  676. return 0;
  677. }
  678. h5->rx_skb->dev = (void *)hu->hdev;
  679. break;
  680. }
  681. break;
  682. }
  683. }
  684. return count;
  685. }
  686. /* Arrange to retransmit all messages in the relq. */
  687. static void h5_timed_event(struct work_struct *work)
  688. {
  689. struct h5_struct *h5;
  690. struct hci_uart *hu;
  691. unsigned long flags;
  692. unsigned long flags2;
  693. struct sk_buff *skb;
  694. h5 = container_of(work, struct h5_struct, retrans_work.work);
  695. hu = h5->hu;
  696. BT_INFO("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
  697. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  698. /* Move the pkt from unack queue to the head of reliable tx queue and
  699. * roll back the tx seq number
  700. */
  701. while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
  702. spin_lock_irqsave(&h5->lock, flags2);
  703. h5->msgq_txseq = (h5->msgq_txseq - 1) & 0x07;
  704. spin_unlock_irqrestore(&h5->lock, flags2);
  705. skb_queue_head(&h5->rel, skb);
  706. }
  707. spin_unlock_irqrestore(&h5->unack.lock, flags);
  708. hci_uart_tx_wakeup(hu);
  709. }
  710. static int h5_open(struct hci_uart *hu)
  711. {
  712. struct h5_struct *h5;
  713. BT_DBG("hu %p", hu);
  714. BT_INFO("h5_open");
  715. h5 = kzalloc(sizeof(*h5), GFP_ATOMIC);
  716. if (!h5)
  717. return -ENOMEM;
  718. hu->priv = h5;
  719. skb_queue_head_init(&h5->unack);
  720. skb_queue_head_init(&h5->rel);
  721. skb_queue_head_init(&h5->unrel);
  722. spin_lock_init(&h5->lock);
  723. h5->hu = hu;
  724. INIT_DELAYED_WORK(&h5->retrans_work, (void *)h5_timed_event);
  725. h5->rx_state = H5_W4_PKT_DELIMITER;
  726. if (txcrc)
  727. h5->use_crc = 1;
  728. return 0;
  729. }
  730. static int h5_close(struct hci_uart *hu)
  731. {
  732. struct h5_struct *h5 = hu->priv;
  733. BT_INFO("h5_close");
  734. cancel_delayed_work_sync(&h5->retrans_work);
  735. hu->priv = NULL;
  736. skb_queue_purge(&h5->unack);
  737. skb_queue_purge(&h5->rel);
  738. skb_queue_purge(&h5->unrel);
  739. kfree(h5);
  740. return 0;
  741. }
  742. static struct hci_uart_proto h5 = {
  743. .id = HCI_UART_3WIRE,
  744. .open = h5_open,
  745. .close = h5_close,
  746. .enqueue = h5_enqueue,
  747. .dequeue = h5_dequeue,
  748. .recv = h5_recv,
  749. .flush = h5_flush
  750. };
  751. int h5_init(void)
  752. {
  753. int err = hci_uart_register_proto(&h5);
  754. if (!err)
  755. BT_INFO("HCI Realtek H5 protocol initialized");
  756. else
  757. BT_ERR("HCI Realtek H5 protocol registration failed");
  758. return err;
  759. }
  760. int h5_deinit(void)
  761. {
  762. return hci_uart_unregister_proto(&h5);
  763. }