hci_h5.c 23 KB

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