hci_h5.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth HCI Three-wire UART driver
  5. *
  6. * Copyright (C) 2012 Intel Corporation
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/bitrev.h>
  10. #include <linux/crc-ccitt.h>
  11. #include <linux/errno.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/of.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/serdev.h>
  18. #include <linux/skbuff.h>
  19. #include <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include "btrtl.h"
  22. #include "hci_uart.h"
  23. #define SUSPEND_TIMEOUT_MS 6000
  24. #define HCI_3WIRE_ACK_PKT 0
  25. #define HCI_3WIRE_LINK_PKT 15
  26. /* Sliding window size */
  27. #define H5_TX_WIN_MAX 4
  28. #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
  29. #define H5_SYNC_TIMEOUT msecs_to_jiffies(100)
  30. /*
  31. * Maximum Three-wire packet:
  32. * 4 byte header + max value for 12-bit length + 2 bytes for CRC
  33. */
  34. #define H5_MAX_LEN (4 + 0xfff + 2)
  35. /* Convenience macros for reading Three-wire header values */
  36. #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
  37. #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
  38. #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
  39. #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
  40. #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
  41. #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0x0f) + ((hdr)[2] << 4))
  42. #define SLIP_DELIMITER 0xc0
  43. #define SLIP_ESC 0xdb
  44. #define SLIP_ESC_DELIM 0xdc
  45. #define SLIP_ESC_ESC 0xdd
  46. /* H5 state flags */
  47. enum {
  48. H5_RX_ESC, /* SLIP escape mode */
  49. H5_TX_ACK_REQ, /* Pending ack to send */
  50. H5_WAKEUP_DISABLE, /* Device cannot wake host */
  51. H5_HW_FLOW_CONTROL, /* Use HW flow control */
  52. H5_CRC, /* Use CRC */
  53. };
  54. struct h5 {
  55. /* Must be the first member, hci_serdev.c expects this. */
  56. struct hci_uart serdev_hu;
  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. struct hci_uart *hu; /* Parent HCI UART */
  67. u8 tx_seq; /* Next seq number to send */
  68. u8 tx_ack; /* Next ack number to send */
  69. u8 tx_win; /* Sliding window size */
  70. enum {
  71. H5_UNINITIALIZED,
  72. H5_INITIALIZED,
  73. H5_ACTIVE,
  74. } state;
  75. enum {
  76. H5_AWAKE,
  77. H5_SLEEPING,
  78. H5_WAKING_UP,
  79. } sleep;
  80. const struct h5_vnd *vnd;
  81. const char *id;
  82. struct gpio_desc *enable_gpio;
  83. struct gpio_desc *device_wake_gpio;
  84. };
  85. enum h5_driver_info {
  86. H5_INFO_WAKEUP_DISABLE = BIT(0),
  87. };
  88. struct h5_vnd {
  89. int (*setup)(struct h5 *h5);
  90. void (*open)(struct h5 *h5);
  91. void (*close)(struct h5 *h5);
  92. int (*suspend)(struct h5 *h5);
  93. int (*resume)(struct h5 *h5);
  94. const struct acpi_gpio_mapping *acpi_gpio_map;
  95. int sizeof_priv;
  96. };
  97. struct h5_device_data {
  98. uint32_t driver_info;
  99. struct h5_vnd *vnd;
  100. };
  101. static void h5_reset_rx(struct h5 *h5);
  102. static void h5_link_control(struct hci_uart *hu, const void *data, size_t len)
  103. {
  104. struct h5 *h5 = hu->priv;
  105. struct sk_buff *nskb;
  106. nskb = alloc_skb(3, GFP_ATOMIC);
  107. if (!nskb)
  108. return;
  109. hci_skb_pkt_type(nskb) = HCI_3WIRE_LINK_PKT;
  110. skb_put_data(nskb, data, len);
  111. skb_queue_tail(&h5->unrel, nskb);
  112. }
  113. static u8 h5_cfg_field(struct h5 *h5)
  114. {
  115. /* Sliding window size (first 3 bits) and CRC request (fifth bit). */
  116. return (h5->tx_win & 0x07) | 0x10;
  117. }
  118. static void h5_timed_event(struct timer_list *t)
  119. {
  120. const unsigned char sync_req[] = { 0x01, 0x7e };
  121. unsigned char conf_req[3] = { 0x03, 0xfc };
  122. struct h5 *h5 = timer_container_of(h5, t, timer);
  123. struct hci_uart *hu = h5->hu;
  124. struct sk_buff *skb;
  125. unsigned long flags;
  126. BT_DBG("%s", hu->hdev->name);
  127. if (h5->state == H5_UNINITIALIZED)
  128. h5_link_control(hu, sync_req, sizeof(sync_req));
  129. if (h5->state == H5_INITIALIZED) {
  130. conf_req[2] = h5_cfg_field(h5);
  131. h5_link_control(hu, conf_req, sizeof(conf_req));
  132. }
  133. if (h5->state != H5_ACTIVE) {
  134. mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
  135. goto wakeup;
  136. }
  137. if (h5->sleep != H5_AWAKE) {
  138. h5->sleep = H5_SLEEPING;
  139. goto wakeup;
  140. }
  141. BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
  142. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  143. while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
  144. h5->tx_seq = (h5->tx_seq - 1) & 0x07;
  145. skb_queue_head(&h5->rel, skb);
  146. }
  147. spin_unlock_irqrestore(&h5->unack.lock, flags);
  148. wakeup:
  149. hci_uart_tx_wakeup(hu);
  150. }
  151. static void h5_peer_reset(struct hci_uart *hu)
  152. {
  153. struct h5 *h5 = hu->priv;
  154. bt_dev_err(hu->hdev, "Peer device has reset");
  155. h5->state = H5_UNINITIALIZED;
  156. timer_delete(&h5->timer);
  157. skb_queue_purge(&h5->rel);
  158. skb_queue_purge(&h5->unrel);
  159. skb_queue_purge(&h5->unack);
  160. h5->tx_seq = 0;
  161. h5->tx_ack = 0;
  162. /* Send reset request to upper stack */
  163. hci_reset_dev(hu->hdev);
  164. }
  165. static int h5_open(struct hci_uart *hu)
  166. {
  167. struct h5 *h5;
  168. BT_DBG("hu %p", hu);
  169. if (hu->serdev) {
  170. h5 = serdev_device_get_drvdata(hu->serdev);
  171. } else {
  172. h5 = kzalloc_obj(*h5);
  173. if (!h5)
  174. return -ENOMEM;
  175. }
  176. hu->priv = h5;
  177. h5->hu = hu;
  178. skb_queue_head_init(&h5->unack);
  179. skb_queue_head_init(&h5->rel);
  180. skb_queue_head_init(&h5->unrel);
  181. h5_reset_rx(h5);
  182. timer_setup(&h5->timer, h5_timed_event, 0);
  183. h5->tx_win = H5_TX_WIN_MAX;
  184. if (h5->vnd && h5->vnd->open)
  185. h5->vnd->open(h5);
  186. set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags);
  187. /*
  188. * Wait one jiffy because the UART layer won't set HCI_UART_PROTO_READY,
  189. * which allows us to send link packets, until this function returns.
  190. */
  191. mod_timer(&h5->timer, jiffies + 1);
  192. return 0;
  193. }
  194. static int h5_close(struct hci_uart *hu)
  195. {
  196. struct h5 *h5 = hu->priv;
  197. timer_delete_sync(&h5->timer);
  198. skb_queue_purge(&h5->unack);
  199. skb_queue_purge(&h5->rel);
  200. skb_queue_purge(&h5->unrel);
  201. kfree_skb(h5->rx_skb);
  202. h5->rx_skb = NULL;
  203. if (h5->vnd && h5->vnd->close)
  204. h5->vnd->close(h5);
  205. if (!hu->serdev)
  206. kfree(h5);
  207. return 0;
  208. }
  209. static int h5_setup(struct hci_uart *hu)
  210. {
  211. struct h5 *h5 = hu->priv;
  212. if (h5->vnd && h5->vnd->setup)
  213. return h5->vnd->setup(h5);
  214. return 0;
  215. }
  216. static void h5_pkt_cull(struct h5 *h5)
  217. {
  218. struct sk_buff *skb, *tmp;
  219. unsigned long flags;
  220. int i, to_remove;
  221. u8 seq;
  222. spin_lock_irqsave(&h5->unack.lock, flags);
  223. to_remove = skb_queue_len(&h5->unack);
  224. if (to_remove == 0)
  225. goto unlock;
  226. seq = h5->tx_seq;
  227. while (to_remove > 0) {
  228. if (h5->rx_ack == seq)
  229. break;
  230. to_remove--;
  231. seq = (seq - 1) & 0x07;
  232. }
  233. if (seq != h5->rx_ack)
  234. BT_ERR("Controller acked invalid packet");
  235. i = 0;
  236. skb_queue_walk_safe(&h5->unack, skb, tmp) {
  237. if (i++ >= to_remove)
  238. break;
  239. __skb_unlink(skb, &h5->unack);
  240. dev_kfree_skb_irq(skb);
  241. }
  242. if (skb_queue_empty(&h5->unack))
  243. timer_delete(&h5->timer);
  244. unlock:
  245. spin_unlock_irqrestore(&h5->unack.lock, flags);
  246. }
  247. static void h5_handle_internal_rx(struct hci_uart *hu)
  248. {
  249. struct h5 *h5 = hu->priv;
  250. const unsigned char sync_req[] = { 0x01, 0x7e };
  251. const unsigned char sync_rsp[] = { 0x02, 0x7d };
  252. unsigned char conf_req[3] = { 0x03, 0xfc };
  253. const unsigned char conf_rsp[] = { 0x04, 0x7b };
  254. const unsigned char wakeup_req[] = { 0x05, 0xfa };
  255. const unsigned char woken_req[] = { 0x06, 0xf9 };
  256. const unsigned char sleep_req[] = { 0x07, 0x78 };
  257. const unsigned char *hdr = h5->rx_skb->data;
  258. const unsigned char *data = &h5->rx_skb->data[4];
  259. BT_DBG("%s", hu->hdev->name);
  260. if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT)
  261. return;
  262. if (H5_HDR_LEN(hdr) < 2)
  263. return;
  264. conf_req[2] = h5_cfg_field(h5);
  265. if (memcmp(data, sync_req, 2) == 0) {
  266. if (h5->state == H5_ACTIVE)
  267. h5_peer_reset(hu);
  268. h5_link_control(hu, sync_rsp, 2);
  269. } else if (memcmp(data, sync_rsp, 2) == 0) {
  270. if (h5->state == H5_ACTIVE)
  271. h5_peer_reset(hu);
  272. h5->state = H5_INITIALIZED;
  273. h5_link_control(hu, conf_req, 3);
  274. } else if (memcmp(data, conf_req, 2) == 0) {
  275. h5_link_control(hu, conf_rsp, 2);
  276. h5_link_control(hu, conf_req, 3);
  277. } else if (memcmp(data, conf_rsp, 2) == 0) {
  278. if (H5_HDR_LEN(hdr) > 2) {
  279. h5->tx_win = (data[2] & 0x07);
  280. assign_bit(H5_CRC, &h5->flags, data[2] & 0x10);
  281. }
  282. BT_DBG("Three-wire init complete. tx_win %u", h5->tx_win);
  283. h5->state = H5_ACTIVE;
  284. hci_uart_init_ready(hu);
  285. return;
  286. } else if (memcmp(data, sleep_req, 2) == 0) {
  287. BT_DBG("Peer went to sleep");
  288. h5->sleep = H5_SLEEPING;
  289. return;
  290. } else if (memcmp(data, woken_req, 2) == 0) {
  291. BT_DBG("Peer woke up");
  292. h5->sleep = H5_AWAKE;
  293. } else if (memcmp(data, wakeup_req, 2) == 0) {
  294. BT_DBG("Peer requested wakeup");
  295. h5_link_control(hu, woken_req, 2);
  296. h5->sleep = H5_AWAKE;
  297. } else {
  298. BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
  299. return;
  300. }
  301. hci_uart_tx_wakeup(hu);
  302. }
  303. static void h5_complete_rx_pkt(struct hci_uart *hu)
  304. {
  305. struct h5 *h5 = hu->priv;
  306. const unsigned char *hdr = h5->rx_skb->data;
  307. if (H5_HDR_RELIABLE(hdr)) {
  308. h5->tx_ack = (h5->tx_ack + 1) % 8;
  309. set_bit(H5_TX_ACK_REQ, &h5->flags);
  310. hci_uart_tx_wakeup(hu);
  311. }
  312. h5->rx_ack = H5_HDR_ACK(hdr);
  313. h5_pkt_cull(h5);
  314. switch (H5_HDR_PKT_TYPE(hdr)) {
  315. case HCI_EVENT_PKT:
  316. case HCI_ACLDATA_PKT:
  317. case HCI_SCODATA_PKT:
  318. case HCI_ISODATA_PKT:
  319. hci_skb_pkt_type(h5->rx_skb) = H5_HDR_PKT_TYPE(hdr);
  320. /* Remove Three-wire header */
  321. skb_pull(h5->rx_skb, 4);
  322. hci_recv_frame(hu->hdev, h5->rx_skb);
  323. h5->rx_skb = NULL;
  324. break;
  325. default:
  326. h5_handle_internal_rx(hu);
  327. break;
  328. }
  329. h5_reset_rx(h5);
  330. }
  331. static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
  332. {
  333. struct h5 *h5 = hu->priv;
  334. const unsigned char *hdr = h5->rx_skb->data;
  335. u16 crc;
  336. __be16 crc_be;
  337. crc = crc_ccitt(0xffff, hdr, 4 + H5_HDR_LEN(hdr));
  338. crc = bitrev16(crc);
  339. crc_be = cpu_to_be16(crc);
  340. if (memcmp(&crc_be, hdr + 4 + H5_HDR_LEN(hdr), 2) != 0) {
  341. bt_dev_err(hu->hdev, "Received packet with invalid CRC");
  342. h5_reset_rx(h5);
  343. } else {
  344. /* Remove CRC bytes */
  345. skb_trim(h5->rx_skb, 4 + H5_HDR_LEN(hdr));
  346. h5_complete_rx_pkt(hu);
  347. }
  348. return 0;
  349. }
  350. static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
  351. {
  352. struct h5 *h5 = hu->priv;
  353. const unsigned char *hdr = h5->rx_skb->data;
  354. if (H5_HDR_CRC(hdr)) {
  355. h5->rx_func = h5_rx_crc;
  356. h5->rx_pending = 2;
  357. } else {
  358. h5_complete_rx_pkt(hu);
  359. }
  360. return 0;
  361. }
  362. static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
  363. {
  364. struct h5 *h5 = hu->priv;
  365. const unsigned char *hdr = h5->rx_skb->data;
  366. BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u",
  367. hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
  368. H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
  369. H5_HDR_LEN(hdr));
  370. if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
  371. bt_dev_err(hu->hdev, "Invalid header checksum");
  372. h5_reset_rx(h5);
  373. return 0;
  374. }
  375. if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
  376. bt_dev_err(hu->hdev, "Out-of-order packet arrived (%u != %u)",
  377. H5_HDR_SEQ(hdr), h5->tx_ack);
  378. set_bit(H5_TX_ACK_REQ, &h5->flags);
  379. hci_uart_tx_wakeup(hu);
  380. h5_reset_rx(h5);
  381. return 0;
  382. }
  383. if (h5->state != H5_ACTIVE &&
  384. H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) {
  385. bt_dev_err(hu->hdev, "Non-link packet received in non-active state");
  386. h5_reset_rx(h5);
  387. return 0;
  388. }
  389. h5->rx_func = h5_rx_payload;
  390. h5->rx_pending = H5_HDR_LEN(hdr);
  391. return 0;
  392. }
  393. static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
  394. {
  395. struct h5 *h5 = hu->priv;
  396. if (c == SLIP_DELIMITER)
  397. return 1;
  398. h5->rx_func = h5_rx_3wire_hdr;
  399. h5->rx_pending = 4;
  400. h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
  401. if (!h5->rx_skb) {
  402. bt_dev_err(hu->hdev, "Can't allocate mem for new packet");
  403. h5_reset_rx(h5);
  404. return -ENOMEM;
  405. }
  406. h5->rx_skb->dev = (void *)hu->hdev;
  407. return 0;
  408. }
  409. static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
  410. {
  411. struct h5 *h5 = hu->priv;
  412. if (c == SLIP_DELIMITER)
  413. h5->rx_func = h5_rx_pkt_start;
  414. return 1;
  415. }
  416. static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
  417. {
  418. const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
  419. const u8 *byte = &c;
  420. if (!test_bit(H5_RX_ESC, &h5->flags) && c == SLIP_ESC) {
  421. set_bit(H5_RX_ESC, &h5->flags);
  422. return;
  423. }
  424. if (test_and_clear_bit(H5_RX_ESC, &h5->flags)) {
  425. switch (c) {
  426. case SLIP_ESC_DELIM:
  427. byte = &delim;
  428. break;
  429. case SLIP_ESC_ESC:
  430. byte = &esc;
  431. break;
  432. default:
  433. BT_ERR("Invalid esc byte 0x%02hhx", c);
  434. h5_reset_rx(h5);
  435. return;
  436. }
  437. }
  438. skb_put_data(h5->rx_skb, byte, 1);
  439. h5->rx_pending--;
  440. BT_DBG("unslipped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
  441. }
  442. static void h5_reset_rx(struct h5 *h5)
  443. {
  444. if (h5->rx_skb) {
  445. kfree_skb(h5->rx_skb);
  446. h5->rx_skb = NULL;
  447. }
  448. h5->rx_func = h5_rx_delimiter;
  449. h5->rx_pending = 0;
  450. clear_bit(H5_RX_ESC, &h5->flags);
  451. clear_bit(H5_CRC, &h5->flags);
  452. }
  453. static int h5_recv(struct hci_uart *hu, const void *data, int count)
  454. {
  455. struct h5 *h5 = hu->priv;
  456. const unsigned char *ptr = data;
  457. BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending,
  458. count);
  459. while (count > 0) {
  460. int processed;
  461. if (h5->rx_pending > 0) {
  462. if (*ptr == SLIP_DELIMITER) {
  463. bt_dev_err(hu->hdev, "Too short H5 packet");
  464. h5_reset_rx(h5);
  465. continue;
  466. }
  467. h5_unslip_one_byte(h5, *ptr);
  468. ptr++; count--;
  469. continue;
  470. }
  471. processed = h5->rx_func(hu, *ptr);
  472. if (processed < 0)
  473. return processed;
  474. ptr += processed;
  475. count -= processed;
  476. }
  477. if (hu->serdev) {
  478. pm_runtime_get(&hu->serdev->dev);
  479. pm_runtime_put_autosuspend(&hu->serdev->dev);
  480. }
  481. return 0;
  482. }
  483. static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  484. {
  485. struct h5 *h5 = hu->priv;
  486. if (skb->len > 0xfff) {
  487. bt_dev_err(hu->hdev, "Packet too long (%u bytes)", skb->len);
  488. kfree_skb(skb);
  489. return 0;
  490. }
  491. if (h5->state != H5_ACTIVE) {
  492. bt_dev_err(hu->hdev, "Ignoring HCI data in non-active state");
  493. kfree_skb(skb);
  494. return 0;
  495. }
  496. switch (hci_skb_pkt_type(skb)) {
  497. case HCI_ACLDATA_PKT:
  498. case HCI_COMMAND_PKT:
  499. skb_queue_tail(&h5->rel, skb);
  500. break;
  501. case HCI_SCODATA_PKT:
  502. case HCI_ISODATA_PKT:
  503. skb_queue_tail(&h5->unrel, skb);
  504. break;
  505. default:
  506. bt_dev_err(hu->hdev, "Unknown packet type %u", hci_skb_pkt_type(skb));
  507. kfree_skb(skb);
  508. break;
  509. }
  510. if (hu->serdev) {
  511. pm_runtime_get_sync(&hu->serdev->dev);
  512. pm_runtime_put_autosuspend(&hu->serdev->dev);
  513. }
  514. return 0;
  515. }
  516. static void h5_slip_delim(struct sk_buff *skb)
  517. {
  518. const char delim = SLIP_DELIMITER;
  519. skb_put_data(skb, &delim, 1);
  520. }
  521. static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
  522. {
  523. const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
  524. const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
  525. switch (c) {
  526. case SLIP_DELIMITER:
  527. skb_put_data(skb, &esc_delim, 2);
  528. break;
  529. case SLIP_ESC:
  530. skb_put_data(skb, &esc_esc, 2);
  531. break;
  532. default:
  533. skb_put_data(skb, &c, 1);
  534. }
  535. }
  536. static bool valid_packet_type(u8 type)
  537. {
  538. switch (type) {
  539. case HCI_ACLDATA_PKT:
  540. case HCI_COMMAND_PKT:
  541. case HCI_SCODATA_PKT:
  542. case HCI_ISODATA_PKT:
  543. case HCI_3WIRE_LINK_PKT:
  544. case HCI_3WIRE_ACK_PKT:
  545. return true;
  546. default:
  547. return false;
  548. }
  549. }
  550. static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
  551. const u8 *data, size_t len)
  552. {
  553. struct h5 *h5 = hu->priv;
  554. struct sk_buff *nskb;
  555. u8 hdr[4];
  556. u16 crc;
  557. int i;
  558. if (!valid_packet_type(pkt_type)) {
  559. bt_dev_err(hu->hdev, "Unknown packet type %u", pkt_type);
  560. return NULL;
  561. }
  562. /*
  563. * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
  564. * (because bytes 0xc0 and 0xdb are escaped, worst case is when
  565. * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
  566. * delimiters at start and end).
  567. */
  568. nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
  569. if (!nskb)
  570. return NULL;
  571. hci_skb_pkt_type(nskb) = pkt_type;
  572. h5_slip_delim(nskb);
  573. hdr[0] = h5->tx_ack << 3;
  574. clear_bit(H5_TX_ACK_REQ, &h5->flags);
  575. /* Reliable packet? */
  576. if (pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) {
  577. hdr[0] |= 1 << 7;
  578. hdr[0] |= (test_bit(H5_CRC, &h5->flags) && 1) << 6;
  579. hdr[0] |= h5->tx_seq;
  580. h5->tx_seq = (h5->tx_seq + 1) % 8;
  581. }
  582. hdr[1] = pkt_type | ((len & 0x0f) << 4);
  583. hdr[2] = len >> 4;
  584. hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
  585. BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u",
  586. hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
  587. H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
  588. H5_HDR_LEN(hdr));
  589. for (i = 0; i < 4; i++)
  590. h5_slip_one_byte(nskb, hdr[i]);
  591. for (i = 0; i < len; i++)
  592. h5_slip_one_byte(nskb, data[i]);
  593. if (H5_HDR_CRC(hdr)) {
  594. crc = crc_ccitt(0xffff, hdr, 4);
  595. crc = crc_ccitt(crc, data, len);
  596. crc = bitrev16(crc);
  597. h5_slip_one_byte(nskb, (crc >> 8) & 0xff);
  598. h5_slip_one_byte(nskb, crc & 0xff);
  599. }
  600. h5_slip_delim(nskb);
  601. return nskb;
  602. }
  603. static struct sk_buff *h5_dequeue(struct hci_uart *hu)
  604. {
  605. struct h5 *h5 = hu->priv;
  606. unsigned long flags;
  607. struct sk_buff *skb, *nskb;
  608. if (h5->sleep != H5_AWAKE) {
  609. const unsigned char wakeup_req[] = { 0x05, 0xfa };
  610. if (h5->sleep == H5_WAKING_UP)
  611. return NULL;
  612. h5->sleep = H5_WAKING_UP;
  613. BT_DBG("Sending wakeup request");
  614. mod_timer(&h5->timer, jiffies + HZ / 100);
  615. return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2);
  616. }
  617. skb = skb_dequeue(&h5->unrel);
  618. if (skb) {
  619. nskb = h5_prepare_pkt(hu, hci_skb_pkt_type(skb),
  620. skb->data, skb->len);
  621. if (nskb) {
  622. kfree_skb(skb);
  623. return nskb;
  624. }
  625. skb_queue_head(&h5->unrel, skb);
  626. bt_dev_err(hu->hdev, "Could not dequeue pkt because alloc_skb failed");
  627. }
  628. spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
  629. if (h5->unack.qlen >= h5->tx_win)
  630. goto unlock;
  631. skb = skb_dequeue(&h5->rel);
  632. if (skb) {
  633. nskb = h5_prepare_pkt(hu, hci_skb_pkt_type(skb),
  634. skb->data, skb->len);
  635. if (nskb) {
  636. __skb_queue_tail(&h5->unack, skb);
  637. mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
  638. spin_unlock_irqrestore(&h5->unack.lock, flags);
  639. return nskb;
  640. }
  641. skb_queue_head(&h5->rel, skb);
  642. bt_dev_err(hu->hdev, "Could not dequeue pkt because alloc_skb failed");
  643. }
  644. unlock:
  645. spin_unlock_irqrestore(&h5->unack.lock, flags);
  646. if (test_bit(H5_TX_ACK_REQ, &h5->flags))
  647. return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
  648. return NULL;
  649. }
  650. static int h5_flush(struct hci_uart *hu)
  651. {
  652. BT_DBG("hu %p", hu);
  653. return 0;
  654. }
  655. static const struct hci_uart_proto h5p = {
  656. .id = HCI_UART_3WIRE,
  657. .name = "Three-wire (H5)",
  658. .open = h5_open,
  659. .close = h5_close,
  660. .setup = h5_setup,
  661. .recv = h5_recv,
  662. .enqueue = h5_enqueue,
  663. .dequeue = h5_dequeue,
  664. .flush = h5_flush,
  665. };
  666. static int h5_serdev_probe(struct serdev_device *serdev)
  667. {
  668. struct device *dev = &serdev->dev;
  669. struct h5 *h5;
  670. const struct h5_device_data *data;
  671. h5 = devm_kzalloc(dev, sizeof(*h5), GFP_KERNEL);
  672. if (!h5)
  673. return -ENOMEM;
  674. h5->hu = &h5->serdev_hu;
  675. h5->serdev_hu.serdev = serdev;
  676. serdev_device_set_drvdata(serdev, h5);
  677. if (has_acpi_companion(dev)) {
  678. const struct acpi_device_id *match;
  679. match = acpi_match_device(dev->driver->acpi_match_table, dev);
  680. if (!match)
  681. return -ENODEV;
  682. data = (const struct h5_device_data *)match->driver_data;
  683. h5->vnd = data->vnd;
  684. h5->id = (char *)match->id;
  685. if (h5->vnd->acpi_gpio_map)
  686. devm_acpi_dev_add_driver_gpios(dev,
  687. h5->vnd->acpi_gpio_map);
  688. } else {
  689. data = of_device_get_match_data(dev);
  690. if (!data)
  691. return -ENODEV;
  692. h5->vnd = data->vnd;
  693. }
  694. if (data->driver_info & H5_INFO_WAKEUP_DISABLE)
  695. set_bit(H5_WAKEUP_DISABLE, &h5->flags);
  696. h5->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
  697. if (IS_ERR(h5->enable_gpio))
  698. return PTR_ERR(h5->enable_gpio);
  699. h5->device_wake_gpio = devm_gpiod_get_optional(dev, "device-wake",
  700. GPIOD_OUT_LOW);
  701. if (IS_ERR(h5->device_wake_gpio))
  702. return PTR_ERR(h5->device_wake_gpio);
  703. return hci_uart_register_device_priv(&h5->serdev_hu, &h5p,
  704. h5->vnd->sizeof_priv);
  705. }
  706. static void h5_serdev_remove(struct serdev_device *serdev)
  707. {
  708. struct h5 *h5 = serdev_device_get_drvdata(serdev);
  709. hci_uart_unregister_device(&h5->serdev_hu);
  710. }
  711. static int __maybe_unused h5_serdev_suspend(struct device *dev)
  712. {
  713. struct h5 *h5 = dev_get_drvdata(dev);
  714. int ret = 0;
  715. if (h5->vnd && h5->vnd->suspend)
  716. ret = h5->vnd->suspend(h5);
  717. return ret;
  718. }
  719. static int __maybe_unused h5_serdev_resume(struct device *dev)
  720. {
  721. struct h5 *h5 = dev_get_drvdata(dev);
  722. int ret = 0;
  723. if (h5->vnd && h5->vnd->resume)
  724. ret = h5->vnd->resume(h5);
  725. return ret;
  726. }
  727. #ifdef CONFIG_BT_HCIUART_RTL
  728. static int h5_btrtl_setup(struct h5 *h5)
  729. {
  730. struct btrtl_device_info *btrtl_dev;
  731. struct sk_buff *skb;
  732. __le32 baudrate_data;
  733. u32 device_baudrate;
  734. unsigned int controller_baudrate;
  735. bool flow_control;
  736. int err;
  737. btrtl_dev = btrtl_initialize(h5->hu->hdev, h5->id);
  738. if (IS_ERR(btrtl_dev))
  739. return PTR_ERR(btrtl_dev);
  740. err = btrtl_get_uart_settings(h5->hu->hdev, btrtl_dev,
  741. &controller_baudrate, &device_baudrate,
  742. &flow_control);
  743. if (err)
  744. goto out_free;
  745. baudrate_data = cpu_to_le32(device_baudrate);
  746. skb = __hci_cmd_sync(h5->hu->hdev, 0xfc17, sizeof(baudrate_data),
  747. &baudrate_data, HCI_INIT_TIMEOUT);
  748. if (IS_ERR(skb)) {
  749. rtl_dev_err(h5->hu->hdev, "set baud rate command failed\n");
  750. err = PTR_ERR(skb);
  751. goto out_free;
  752. } else {
  753. kfree_skb(skb);
  754. }
  755. /* Give the device some time to set up the new baudrate. */
  756. usleep_range(10000, 20000);
  757. serdev_device_set_baudrate(h5->hu->serdev, controller_baudrate);
  758. serdev_device_set_flow_control(h5->hu->serdev, flow_control);
  759. if (flow_control)
  760. set_bit(H5_HW_FLOW_CONTROL, &h5->flags);
  761. err = btrtl_download_firmware(h5->hu->hdev, btrtl_dev);
  762. /* Give the device some time before the hci-core sends it a reset */
  763. usleep_range(10000, 20000);
  764. if (err)
  765. goto out_free;
  766. btrtl_set_quirks(h5->hu->hdev, btrtl_dev);
  767. out_free:
  768. btrtl_free(btrtl_dev);
  769. return err;
  770. }
  771. static void h5_btrtl_open(struct h5 *h5)
  772. {
  773. /*
  774. * Since h5_btrtl_resume() does a device_reprobe() the suspend handling
  775. * done by the hci_suspend_notifier is not necessary; it actually causes
  776. * delays and a bunch of errors to get logged, so disable it.
  777. */
  778. if (test_bit(H5_WAKEUP_DISABLE, &h5->flags))
  779. set_bit(HCI_UART_NO_SUSPEND_NOTIFIER, &h5->hu->flags);
  780. /* Devices always start with these fixed parameters */
  781. serdev_device_set_flow_control(h5->hu->serdev, false);
  782. serdev_device_set_parity(h5->hu->serdev, SERDEV_PARITY_EVEN);
  783. serdev_device_set_baudrate(h5->hu->serdev, 115200);
  784. if (!test_bit(H5_WAKEUP_DISABLE, &h5->flags)) {
  785. pm_runtime_set_active(&h5->hu->serdev->dev);
  786. pm_runtime_use_autosuspend(&h5->hu->serdev->dev);
  787. pm_runtime_set_autosuspend_delay(&h5->hu->serdev->dev,
  788. SUSPEND_TIMEOUT_MS);
  789. pm_runtime_enable(&h5->hu->serdev->dev);
  790. }
  791. /* The controller needs reset to startup */
  792. gpiod_set_value_cansleep(h5->enable_gpio, 0);
  793. gpiod_set_value_cansleep(h5->device_wake_gpio, 0);
  794. msleep(100);
  795. /* The controller needs up to 500ms to wakeup */
  796. gpiod_set_value_cansleep(h5->enable_gpio, 1);
  797. gpiod_set_value_cansleep(h5->device_wake_gpio, 1);
  798. msleep(500);
  799. }
  800. static void h5_btrtl_close(struct h5 *h5)
  801. {
  802. if (!test_bit(H5_WAKEUP_DISABLE, &h5->flags))
  803. pm_runtime_disable(&h5->hu->serdev->dev);
  804. gpiod_set_value_cansleep(h5->device_wake_gpio, 0);
  805. gpiod_set_value_cansleep(h5->enable_gpio, 0);
  806. }
  807. /* Suspend/resume support. On many devices the RTL BT device loses power during
  808. * suspend/resume, causing it to lose its firmware and all state. So we simply
  809. * turn it off on suspend and reprobe on resume. This mirrors how RTL devices
  810. * are handled in the USB driver, where the BTUSB_WAKEUP_DISABLE is used which
  811. * also causes a reprobe on resume.
  812. */
  813. static int h5_btrtl_suspend(struct h5 *h5)
  814. {
  815. serdev_device_set_flow_control(h5->hu->serdev, false);
  816. gpiod_set_value_cansleep(h5->device_wake_gpio, 0);
  817. if (test_bit(H5_WAKEUP_DISABLE, &h5->flags))
  818. gpiod_set_value_cansleep(h5->enable_gpio, 0);
  819. return 0;
  820. }
  821. struct h5_btrtl_reprobe {
  822. struct device *dev;
  823. struct work_struct work;
  824. };
  825. static void h5_btrtl_reprobe_worker(struct work_struct *work)
  826. {
  827. struct h5_btrtl_reprobe *reprobe =
  828. container_of(work, struct h5_btrtl_reprobe, work);
  829. int ret;
  830. ret = device_reprobe(reprobe->dev);
  831. if (ret && ret != -EPROBE_DEFER)
  832. dev_err(reprobe->dev, "Reprobe error %d\n", ret);
  833. put_device(reprobe->dev);
  834. kfree(reprobe);
  835. module_put(THIS_MODULE);
  836. }
  837. static int h5_btrtl_resume(struct h5 *h5)
  838. {
  839. if (test_bit(H5_WAKEUP_DISABLE, &h5->flags)) {
  840. struct h5_btrtl_reprobe *reprobe;
  841. reprobe = kzalloc_obj(*reprobe);
  842. if (!reprobe)
  843. return -ENOMEM;
  844. __module_get(THIS_MODULE);
  845. INIT_WORK(&reprobe->work, h5_btrtl_reprobe_worker);
  846. reprobe->dev = get_device(&h5->hu->serdev->dev);
  847. queue_work(system_long_wq, &reprobe->work);
  848. } else {
  849. gpiod_set_value_cansleep(h5->device_wake_gpio, 1);
  850. if (test_bit(H5_HW_FLOW_CONTROL, &h5->flags))
  851. serdev_device_set_flow_control(h5->hu->serdev, true);
  852. }
  853. return 0;
  854. }
  855. static const struct acpi_gpio_params btrtl_device_wake_gpios = { 0, 0, false };
  856. static const struct acpi_gpio_params btrtl_enable_gpios = { 1, 0, false };
  857. static const struct acpi_gpio_params btrtl_host_wake_gpios = { 2, 0, false };
  858. static const struct acpi_gpio_mapping acpi_btrtl_gpios[] = {
  859. { "device-wake-gpios", &btrtl_device_wake_gpios, 1 },
  860. { "enable-gpios", &btrtl_enable_gpios, 1 },
  861. { "host-wake-gpios", &btrtl_host_wake_gpios, 1 },
  862. {},
  863. };
  864. static struct h5_vnd rtl_vnd = {
  865. .setup = h5_btrtl_setup,
  866. .open = h5_btrtl_open,
  867. .close = h5_btrtl_close,
  868. .suspend = h5_btrtl_suspend,
  869. .resume = h5_btrtl_resume,
  870. .acpi_gpio_map = acpi_btrtl_gpios,
  871. .sizeof_priv = sizeof(struct btrealtek_data),
  872. };
  873. static const struct h5_device_data h5_data_rtl8822cs = {
  874. .vnd = &rtl_vnd,
  875. };
  876. static const struct h5_device_data h5_data_rtl8723bs = {
  877. .driver_info = H5_INFO_WAKEUP_DISABLE,
  878. .vnd = &rtl_vnd,
  879. };
  880. #endif
  881. #ifdef CONFIG_ACPI
  882. static const struct acpi_device_id h5_acpi_match[] = {
  883. #ifdef CONFIG_BT_HCIUART_RTL
  884. { "OBDA0623", (kernel_ulong_t)&h5_data_rtl8723bs },
  885. { "OBDA8723", (kernel_ulong_t)&h5_data_rtl8723bs },
  886. #endif
  887. { },
  888. };
  889. MODULE_DEVICE_TABLE(acpi, h5_acpi_match);
  890. #endif
  891. static const struct dev_pm_ops h5_serdev_pm_ops = {
  892. SET_SYSTEM_SLEEP_PM_OPS(h5_serdev_suspend, h5_serdev_resume)
  893. SET_RUNTIME_PM_OPS(h5_serdev_suspend, h5_serdev_resume, NULL)
  894. };
  895. static const struct of_device_id rtl_bluetooth_of_match[] = {
  896. #ifdef CONFIG_BT_HCIUART_RTL
  897. { .compatible = "realtek,rtl8822cs-bt",
  898. .data = (const void *)&h5_data_rtl8822cs },
  899. { .compatible = "realtek,rtl8723bs-bt",
  900. .data = (const void *)&h5_data_rtl8723bs },
  901. { .compatible = "realtek,rtl8723cs-bt",
  902. .data = (const void *)&h5_data_rtl8723bs },
  903. { .compatible = "realtek,rtl8723ds-bt",
  904. .data = (const void *)&h5_data_rtl8723bs },
  905. #endif
  906. { },
  907. };
  908. MODULE_DEVICE_TABLE(of, rtl_bluetooth_of_match);
  909. static struct serdev_device_driver h5_serdev_driver = {
  910. .probe = h5_serdev_probe,
  911. .remove = h5_serdev_remove,
  912. .driver = {
  913. .name = "hci_uart_h5",
  914. .acpi_match_table = ACPI_PTR(h5_acpi_match),
  915. .pm = &h5_serdev_pm_ops,
  916. .of_match_table = rtl_bluetooth_of_match,
  917. },
  918. };
  919. int __init h5_init(void)
  920. {
  921. serdev_device_driver_register(&h5_serdev_driver);
  922. return hci_uart_register_proto(&h5p);
  923. }
  924. int __exit h5_deinit(void)
  925. {
  926. serdev_device_driver_unregister(&h5_serdev_driver);
  927. return hci_uart_unregister_proto(&h5p);
  928. }