hci_h5.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. *
  3. * Bluetooth HCI Three-wire UART driver
  4. *
  5. * Copyright (C) 2012 Intel Corporation
  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/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "hci_uart.h"
  29. #define HCI_3WIRE_ACK_PKT 0
  30. #define HCI_3WIRE_LINK_PKT 15
  31. #define H5_TXWINSIZE 4
  32. #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
  33. #define H5_SYNC_TIMEOUT msecs_to_jiffies(100)
  34. /*
  35. * Maximum Three-wire packet:
  36. * 4 byte header + max value for 12-bit length + 2 bytes for CRC
  37. */
  38. #define H5_MAX_LEN (4 + 0xfff + 2)
  39. /* Convenience macros for reading Three-wire header values */
  40. #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
  41. #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
  42. #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
  43. #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
  44. #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
  45. #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0xff) + ((hdr)[2] << 4))
  46. #define SLIP_DELIMITER 0xc0
  47. #define SLIP_ESC 0xdb
  48. #define SLIP_ESC_DELIM 0xdc
  49. #define SLIP_ESC_ESC 0xdd
  50. struct h5 {
  51. struct sk_buff_head unack; /* Unack'ed packets queue */
  52. struct sk_buff_head rel; /* Reliable packets queue */
  53. struct sk_buff_head unrel; /* Unreliable packets queue */
  54. struct sk_buff *rx_skb; /* Receive buffer */
  55. size_t rx_pending; /* Expecting more bytes */
  56. bool rx_esc; /* SLIP escape mode */
  57. u8 rx_ack; /* Last ack number received */
  58. int (*rx_func) (struct hci_uart *hu, u8 c);
  59. struct timer_list timer; /* Retransmission timer */
  60. bool tx_ack_req; /* Pending ack to send */
  61. u8 tx_seq; /* Next seq number to send */
  62. u8 tx_ack; /* Next ack number to send */
  63. enum {
  64. H5_UNINITIALIZED,
  65. H5_INITIALIZED,
  66. H5_ACTIVE,
  67. } state;
  68. enum {
  69. H5_AWAKE,
  70. H5_SLEEPING,
  71. H5_WAKING_UP,
  72. } sleep;
  73. };
  74. static void h5_reset_rx(struct h5 *h5);
  75. static void h5_link_control(struct hci_uart *hu, const void *data, size_t len)
  76. {
  77. struct h5 *h5 = hu->priv;
  78. struct sk_buff *nskb;
  79. nskb = alloc_skb(3, GFP_ATOMIC);
  80. if (!nskb)
  81. return;
  82. bt_cb(nskb)->pkt_type = HCI_3WIRE_LINK_PKT;
  83. memcpy(skb_put(nskb, len), data, len);
  84. skb_queue_tail(&h5->unrel, nskb);
  85. }
  86. static void h5_timed_event(unsigned long arg)
  87. {
  88. const unsigned char sync_req[] = { 0x01, 0x7e };
  89. const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
  90. struct hci_uart *hu = (struct hci_uart *) arg;
  91. struct h5 *h5 = hu->priv;
  92. struct sk_buff *skb;
  93. unsigned long flags;
  94. BT_DBG("%s", hu->hdev->name);
  95. if (h5->state == H5_UNINITIALIZED)
  96. h5_link_control(hu, sync_req, sizeof(sync_req));
  97. if (h5->state == H5_INITIALIZED)
  98. h5_link_control(hu, conf_req, sizeof(conf_req));
  99. if (h5->state != H5_ACTIVE) {
  100. mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
  101. goto wakeup;
  102. }
  103. if (h5->sleep != H5_AWAKE) {
  104. h5->sleep = H5_SLEEPING;
  105. goto wakeup;
  106. }
  107. BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
  108. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  109. while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
  110. h5->tx_seq = (h5->tx_seq - 1) & 0x07;
  111. skb_queue_head(&h5->rel, skb);
  112. }
  113. spin_unlock_irqrestore(&h5->unack.lock, flags);
  114. wakeup:
  115. hci_uart_tx_wakeup(hu);
  116. }
  117. static int h5_open(struct hci_uart *hu)
  118. {
  119. struct h5 *h5;
  120. const unsigned char sync[] = { 0x01, 0x7e };
  121. BT_DBG("hu %p", hu);
  122. h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
  123. if (!h5)
  124. return -ENOMEM;
  125. hu->priv = h5;
  126. skb_queue_head_init(&h5->unack);
  127. skb_queue_head_init(&h5->rel);
  128. skb_queue_head_init(&h5->unrel);
  129. h5_reset_rx(h5);
  130. init_timer(&h5->timer);
  131. h5->timer.function = h5_timed_event;
  132. h5->timer.data = (unsigned long) hu;
  133. set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags);
  134. /* Send initial sync request */
  135. h5_link_control(hu, sync, sizeof(sync));
  136. mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
  137. return 0;
  138. }
  139. static int h5_close(struct hci_uart *hu)
  140. {
  141. struct h5 *h5 = hu->priv;
  142. skb_queue_purge(&h5->unack);
  143. skb_queue_purge(&h5->rel);
  144. skb_queue_purge(&h5->unrel);
  145. del_timer(&h5->timer);
  146. kfree(h5);
  147. return 0;
  148. }
  149. static void h5_pkt_cull(struct h5 *h5)
  150. {
  151. struct sk_buff *skb, *tmp;
  152. unsigned long flags;
  153. int i, to_remove;
  154. u8 seq;
  155. spin_lock_irqsave(&h5->unack.lock, flags);
  156. to_remove = skb_queue_len(&h5->unack);
  157. if (to_remove == 0)
  158. goto unlock;
  159. seq = h5->tx_seq;
  160. while (to_remove > 0) {
  161. if (h5->rx_ack == seq)
  162. break;
  163. to_remove--;
  164. seq = (seq - 1) % 8;
  165. }
  166. if (seq != h5->rx_ack)
  167. BT_ERR("Controller acked invalid packet");
  168. i = 0;
  169. skb_queue_walk_safe(&h5->unack, skb, tmp) {
  170. if (i++ >= to_remove)
  171. break;
  172. __skb_unlink(skb, &h5->unack);
  173. kfree_skb(skb);
  174. }
  175. if (skb_queue_empty(&h5->unack))
  176. del_timer(&h5->timer);
  177. unlock:
  178. spin_unlock_irqrestore(&h5->unack.lock, flags);
  179. }
  180. static void h5_handle_internal_rx(struct hci_uart *hu)
  181. {
  182. struct h5 *h5 = hu->priv;
  183. const unsigned char sync_req[] = { 0x01, 0x7e };
  184. const unsigned char sync_rsp[] = { 0x02, 0x7d };
  185. const unsigned char conf_req[] = { 0x03, 0xfc, 0x01 };
  186. const unsigned char conf_rsp[] = { 0x04, 0x7b, 0x01 };
  187. const unsigned char wakeup_req[] = { 0x05, 0xfa };
  188. const unsigned char woken_req[] = { 0x06, 0xf9 };
  189. const unsigned char sleep_req[] = { 0x07, 0x78 };
  190. const unsigned char *hdr = h5->rx_skb->data;
  191. const unsigned char *data = &h5->rx_skb->data[4];
  192. BT_DBG("%s", hu->hdev->name);
  193. if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT)
  194. return;
  195. if (H5_HDR_LEN(hdr) < 2)
  196. return;
  197. if (memcmp(data, sync_req, 2) == 0) {
  198. h5_link_control(hu, sync_rsp, 2);
  199. } else if (memcmp(data, sync_rsp, 2) == 0) {
  200. h5->state = H5_INITIALIZED;
  201. h5_link_control(hu, conf_req, 3);
  202. } else if (memcmp(data, conf_req, 2) == 0) {
  203. h5_link_control(hu, conf_rsp, 2);
  204. h5_link_control(hu, conf_req, 3);
  205. } else if (memcmp(data, conf_rsp, 2) == 0) {
  206. BT_DBG("Three-wire init sequence complete");
  207. h5->state = H5_ACTIVE;
  208. hci_uart_init_ready(hu);
  209. return;
  210. } else if (memcmp(data, sleep_req, 2) == 0) {
  211. BT_DBG("Peer went to sleep");
  212. h5->sleep = H5_SLEEPING;
  213. return;
  214. } else if (memcmp(data, woken_req, 2) == 0) {
  215. BT_DBG("Peer woke up");
  216. h5->sleep = H5_AWAKE;
  217. } else if (memcmp(data, wakeup_req, 2) == 0) {
  218. BT_DBG("Peer requested wakeup");
  219. h5_link_control(hu, woken_req, 2);
  220. h5->sleep = H5_AWAKE;
  221. } else {
  222. BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
  223. return;
  224. }
  225. hci_uart_tx_wakeup(hu);
  226. }
  227. static void h5_complete_rx_pkt(struct hci_uart *hu)
  228. {
  229. struct h5 *h5 = hu->priv;
  230. const unsigned char *hdr = h5->rx_skb->data;
  231. if (H5_HDR_RELIABLE(hdr)) {
  232. h5->tx_ack = (h5->tx_ack + 1) % 8;
  233. h5->tx_ack_req = true;
  234. hci_uart_tx_wakeup(hu);
  235. }
  236. h5->rx_ack = H5_HDR_ACK(hdr);
  237. h5_pkt_cull(h5);
  238. switch (H5_HDR_PKT_TYPE(hdr)) {
  239. case HCI_EVENT_PKT:
  240. case HCI_ACLDATA_PKT:
  241. case HCI_SCODATA_PKT:
  242. bt_cb(h5->rx_skb)->pkt_type = H5_HDR_PKT_TYPE(hdr);
  243. /* Remove Three-wire header */
  244. skb_pull(h5->rx_skb, 4);
  245. hci_recv_frame(h5->rx_skb);
  246. h5->rx_skb = NULL;
  247. break;
  248. default:
  249. h5_handle_internal_rx(hu);
  250. break;
  251. }
  252. h5_reset_rx(h5);
  253. }
  254. static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
  255. {
  256. struct h5 *h5 = hu->priv;
  257. h5_complete_rx_pkt(hu);
  258. h5_reset_rx(h5);
  259. return 0;
  260. }
  261. static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
  262. {
  263. struct h5 *h5 = hu->priv;
  264. const unsigned char *hdr = h5->rx_skb->data;
  265. if (H5_HDR_CRC(hdr)) {
  266. h5->rx_func = h5_rx_crc;
  267. h5->rx_pending = 2;
  268. } else {
  269. h5_complete_rx_pkt(hu);
  270. h5_reset_rx(h5);
  271. }
  272. return 0;
  273. }
  274. static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
  275. {
  276. struct h5 *h5 = hu->priv;
  277. const unsigned char *hdr = h5->rx_skb->data;
  278. BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u",
  279. hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
  280. H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
  281. H5_HDR_LEN(hdr));
  282. if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
  283. BT_ERR("Invalid header checksum");
  284. h5_reset_rx(h5);
  285. return 0;
  286. }
  287. if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
  288. BT_ERR("Out-of-order packet arrived (%u != %u)",
  289. H5_HDR_SEQ(hdr), h5->tx_ack);
  290. h5_reset_rx(h5);
  291. return 0;
  292. }
  293. if (h5->state != H5_ACTIVE &&
  294. H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) {
  295. BT_ERR("Non-link packet received in non-active state");
  296. h5_reset_rx(h5);
  297. }
  298. h5->rx_func = h5_rx_payload;
  299. h5->rx_pending = H5_HDR_LEN(hdr);
  300. return 0;
  301. }
  302. static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
  303. {
  304. struct h5 *h5 = hu->priv;
  305. if (c == SLIP_DELIMITER)
  306. return 1;
  307. h5->rx_func = h5_rx_3wire_hdr;
  308. h5->rx_pending = 4;
  309. h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
  310. if (!h5->rx_skb) {
  311. BT_ERR("Can't allocate mem for new packet");
  312. h5_reset_rx(h5);
  313. return -ENOMEM;
  314. }
  315. h5->rx_skb->dev = (void *) hu->hdev;
  316. return 0;
  317. }
  318. static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
  319. {
  320. struct h5 *h5 = hu->priv;
  321. if (c == SLIP_DELIMITER)
  322. h5->rx_func = h5_rx_pkt_start;
  323. return 1;
  324. }
  325. static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
  326. {
  327. const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
  328. const u8 *byte = &c;
  329. if (!h5->rx_esc && c == SLIP_ESC) {
  330. h5->rx_esc = true;
  331. return;
  332. }
  333. if (h5->rx_esc) {
  334. switch (c) {
  335. case SLIP_ESC_DELIM:
  336. byte = &delim;
  337. break;
  338. case SLIP_ESC_ESC:
  339. byte = &esc;
  340. break;
  341. default:
  342. BT_ERR("Invalid esc byte 0x%02hhx", c);
  343. h5_reset_rx(h5);
  344. return;
  345. }
  346. h5->rx_esc = false;
  347. }
  348. memcpy(skb_put(h5->rx_skb, 1), byte, 1);
  349. h5->rx_pending--;
  350. BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
  351. }
  352. static void h5_reset_rx(struct h5 *h5)
  353. {
  354. if (h5->rx_skb) {
  355. kfree_skb(h5->rx_skb);
  356. h5->rx_skb = NULL;
  357. }
  358. h5->rx_func = h5_rx_delimiter;
  359. h5->rx_pending = 0;
  360. h5->rx_esc = false;
  361. }
  362. static int h5_recv(struct hci_uart *hu, void *data, int count)
  363. {
  364. struct h5 *h5 = hu->priv;
  365. unsigned char *ptr = data;
  366. BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending,
  367. count);
  368. while (count > 0) {
  369. int processed;
  370. if (h5->rx_pending > 0) {
  371. if (*ptr == SLIP_DELIMITER) {
  372. BT_ERR("Too short H5 packet");
  373. h5_reset_rx(h5);
  374. continue;
  375. }
  376. h5_unslip_one_byte(h5, *ptr);
  377. ptr++; count--;
  378. continue;
  379. }
  380. processed = h5->rx_func(hu, *ptr);
  381. if (processed < 0)
  382. return processed;
  383. ptr += processed;
  384. count -= processed;
  385. }
  386. return 0;
  387. }
  388. static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  389. {
  390. struct h5 *h5 = hu->priv;
  391. if (skb->len > 0xfff) {
  392. BT_ERR("Packet too long (%u bytes)", skb->len);
  393. kfree_skb(skb);
  394. return 0;
  395. }
  396. if (h5->state != H5_ACTIVE) {
  397. BT_ERR("Ignoring HCI data in non-active state");
  398. kfree_skb(skb);
  399. return 0;
  400. }
  401. switch (bt_cb(skb)->pkt_type) {
  402. case HCI_ACLDATA_PKT:
  403. case HCI_COMMAND_PKT:
  404. skb_queue_tail(&h5->rel, skb);
  405. break;
  406. case HCI_SCODATA_PKT:
  407. skb_queue_tail(&h5->unrel, skb);
  408. break;
  409. default:
  410. BT_ERR("Unknown packet type %u", bt_cb(skb)->pkt_type);
  411. kfree_skb(skb);
  412. break;
  413. }
  414. return 0;
  415. }
  416. static void h5_slip_delim(struct sk_buff *skb)
  417. {
  418. const char delim = SLIP_DELIMITER;
  419. memcpy(skb_put(skb, 1), &delim, 1);
  420. }
  421. static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
  422. {
  423. const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
  424. const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
  425. switch (c) {
  426. case SLIP_DELIMITER:
  427. memcpy(skb_put(skb, 2), &esc_delim, 2);
  428. break;
  429. case SLIP_ESC:
  430. memcpy(skb_put(skb, 2), &esc_esc, 2);
  431. break;
  432. default:
  433. memcpy(skb_put(skb, 1), &c, 1);
  434. }
  435. }
  436. static bool valid_packet_type(u8 type)
  437. {
  438. switch (type) {
  439. case HCI_ACLDATA_PKT:
  440. case HCI_COMMAND_PKT:
  441. case HCI_SCODATA_PKT:
  442. case HCI_3WIRE_LINK_PKT:
  443. case HCI_3WIRE_ACK_PKT:
  444. return true;
  445. default:
  446. return false;
  447. }
  448. }
  449. static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
  450. const u8 *data, size_t len)
  451. {
  452. struct h5 *h5 = hu->priv;
  453. struct sk_buff *nskb;
  454. u8 hdr[4];
  455. int i;
  456. if (!valid_packet_type(pkt_type)) {
  457. BT_ERR("Unknown packet type %u", pkt_type);
  458. return NULL;
  459. }
  460. /*
  461. * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
  462. * (because bytes 0xc0 and 0xdb are escaped, worst case is when
  463. * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
  464. * delimiters at start and end).
  465. */
  466. nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  467. if (!nskb)
  468. return NULL;
  469. bt_cb(nskb)->pkt_type = pkt_type;
  470. h5_slip_delim(nskb);
  471. hdr[0] = h5->tx_ack << 3;
  472. h5->tx_ack_req = false;
  473. /* Reliable packet? */
  474. if (pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) {
  475. hdr[0] |= 1 << 7;
  476. hdr[0] |= h5->tx_seq;
  477. h5->tx_seq = (h5->tx_seq + 1) % 8;
  478. }
  479. hdr[1] = pkt_type | ((len & 0x0f) << 4);
  480. hdr[2] = len >> 4;
  481. hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
  482. BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u",
  483. hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
  484. H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
  485. H5_HDR_LEN(hdr));
  486. for (i = 0; i < 4; i++)
  487. h5_slip_one_byte(nskb, hdr[i]);
  488. for (i = 0; i < len; i++)
  489. h5_slip_one_byte(nskb, data[i]);
  490. h5_slip_delim(nskb);
  491. return nskb;
  492. }
  493. static struct sk_buff *h5_dequeue(struct hci_uart *hu)
  494. {
  495. struct h5 *h5 = hu->priv;
  496. unsigned long flags;
  497. struct sk_buff *skb, *nskb;
  498. if (h5->sleep != H5_AWAKE) {
  499. const unsigned char wakeup_req[] = { 0x05, 0xfa };
  500. if (h5->sleep == H5_WAKING_UP)
  501. return NULL;
  502. h5->sleep = H5_WAKING_UP;
  503. BT_DBG("Sending wakeup request");
  504. mod_timer(&h5->timer, jiffies + HZ / 100);
  505. return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2);
  506. }
  507. if ((skb = skb_dequeue(&h5->unrel)) != NULL) {
  508. nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
  509. skb->data, skb->len);
  510. if (nskb) {
  511. kfree_skb(skb);
  512. return nskb;
  513. }
  514. skb_queue_head(&h5->unrel, skb);
  515. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  516. }
  517. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  518. if (h5->unack.qlen >= H5_TXWINSIZE)
  519. goto unlock;
  520. if ((skb = skb_dequeue(&h5->rel)) != NULL) {
  521. nskb = h5_prepare_pkt(hu, bt_cb(skb)->pkt_type,
  522. skb->data, skb->len);
  523. if (nskb) {
  524. __skb_queue_tail(&h5->unack, skb);
  525. mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
  526. spin_unlock_irqrestore(&h5->unack.lock, flags);
  527. return nskb;
  528. }
  529. skb_queue_head(&h5->rel, skb);
  530. BT_ERR("Could not dequeue pkt because alloc_skb failed");
  531. }
  532. unlock:
  533. spin_unlock_irqrestore(&h5->unack.lock, flags);
  534. if (h5->tx_ack_req)
  535. return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
  536. return NULL;
  537. }
  538. static int h5_flush(struct hci_uart *hu)
  539. {
  540. BT_DBG("hu %p", hu);
  541. return 0;
  542. }
  543. static struct hci_uart_proto h5p = {
  544. .id = HCI_UART_3WIRE,
  545. .open = h5_open,
  546. .close = h5_close,
  547. .recv = h5_recv,
  548. .enqueue = h5_enqueue,
  549. .dequeue = h5_dequeue,
  550. .flush = h5_flush,
  551. };
  552. int __init h5_init(void)
  553. {
  554. int err = hci_uart_register_proto(&h5p);
  555. if (!err)
  556. BT_INFO("HCI Three-wire UART (H5) protocol initialized");
  557. else
  558. BT_ERR("HCI Three-wire UART (H5) protocol init failed");
  559. return err;
  560. }
  561. int __exit h5_deinit(void)
  562. {
  563. return hci_uart_unregister_proto(&h5p);
  564. }