hci_h5.c 26 KB

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