hci_h5.c 16 KB

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