0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/acpi.h>
0010 #include <linux/errno.h>
0011 #include <linux/gpio/consumer.h>
0012 #include <linux/kernel.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/of_device.h>
0015 #include <linux/pm_runtime.h>
0016 #include <linux/serdev.h>
0017 #include <linux/skbuff.h>
0018
0019 #include <net/bluetooth/bluetooth.h>
0020 #include <net/bluetooth/hci_core.h>
0021
0022 #include "btrtl.h"
0023 #include "hci_uart.h"
0024
0025 #define SUSPEND_TIMEOUT_MS 6000
0026
0027 #define HCI_3WIRE_ACK_PKT 0
0028 #define HCI_3WIRE_LINK_PKT 15
0029
0030
0031 #define H5_TX_WIN_MAX 4
0032
0033 #define H5_ACK_TIMEOUT msecs_to_jiffies(250)
0034 #define H5_SYNC_TIMEOUT msecs_to_jiffies(100)
0035
0036
0037
0038
0039
0040 #define H5_MAX_LEN (4 + 0xfff + 2)
0041
0042
0043 #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
0044 #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
0045 #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
0046 #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
0047 #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
0048 #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0x0f) + ((hdr)[2] << 4))
0049
0050 #define SLIP_DELIMITER 0xc0
0051 #define SLIP_ESC 0xdb
0052 #define SLIP_ESC_DELIM 0xdc
0053 #define SLIP_ESC_ESC 0xdd
0054
0055
0056 enum {
0057 H5_RX_ESC,
0058 H5_TX_ACK_REQ,
0059 H5_WAKEUP_DISABLE,
0060 H5_HW_FLOW_CONTROL,
0061 };
0062
0063 struct h5 {
0064
0065 struct hci_uart serdev_hu;
0066
0067 struct sk_buff_head unack;
0068 struct sk_buff_head rel;
0069 struct sk_buff_head unrel;
0070
0071 unsigned long flags;
0072
0073 struct sk_buff *rx_skb;
0074 size_t rx_pending;
0075 u8 rx_ack;
0076
0077 int (*rx_func)(struct hci_uart *hu, u8 c);
0078
0079 struct timer_list timer;
0080 struct hci_uart *hu;
0081
0082 u8 tx_seq;
0083 u8 tx_ack;
0084 u8 tx_win;
0085
0086 enum {
0087 H5_UNINITIALIZED,
0088 H5_INITIALIZED,
0089 H5_ACTIVE,
0090 } state;
0091
0092 enum {
0093 H5_AWAKE,
0094 H5_SLEEPING,
0095 H5_WAKING_UP,
0096 } sleep;
0097
0098 const struct h5_vnd *vnd;
0099 const char *id;
0100
0101 struct gpio_desc *enable_gpio;
0102 struct gpio_desc *device_wake_gpio;
0103 };
0104
0105 enum h5_driver_info {
0106 H5_INFO_WAKEUP_DISABLE = BIT(0),
0107 };
0108
0109 struct h5_vnd {
0110 int (*setup)(struct h5 *h5);
0111 void (*open)(struct h5 *h5);
0112 void (*close)(struct h5 *h5);
0113 int (*suspend)(struct h5 *h5);
0114 int (*resume)(struct h5 *h5);
0115 const struct acpi_gpio_mapping *acpi_gpio_map;
0116 };
0117
0118 struct h5_device_data {
0119 uint32_t driver_info;
0120 struct h5_vnd *vnd;
0121 };
0122
0123 static void h5_reset_rx(struct h5 *h5);
0124
0125 static void h5_link_control(struct hci_uart *hu, const void *data, size_t len)
0126 {
0127 struct h5 *h5 = hu->priv;
0128 struct sk_buff *nskb;
0129
0130 nskb = alloc_skb(3, GFP_ATOMIC);
0131 if (!nskb)
0132 return;
0133
0134 hci_skb_pkt_type(nskb) = HCI_3WIRE_LINK_PKT;
0135
0136 skb_put_data(nskb, data, len);
0137
0138 skb_queue_tail(&h5->unrel, nskb);
0139 }
0140
0141 static u8 h5_cfg_field(struct h5 *h5)
0142 {
0143
0144 return h5->tx_win & 0x07;
0145 }
0146
0147 static void h5_timed_event(struct timer_list *t)
0148 {
0149 const unsigned char sync_req[] = { 0x01, 0x7e };
0150 unsigned char conf_req[3] = { 0x03, 0xfc };
0151 struct h5 *h5 = from_timer(h5, t, timer);
0152 struct hci_uart *hu = h5->hu;
0153 struct sk_buff *skb;
0154 unsigned long flags;
0155
0156 BT_DBG("%s", hu->hdev->name);
0157
0158 if (h5->state == H5_UNINITIALIZED)
0159 h5_link_control(hu, sync_req, sizeof(sync_req));
0160
0161 if (h5->state == H5_INITIALIZED) {
0162 conf_req[2] = h5_cfg_field(h5);
0163 h5_link_control(hu, conf_req, sizeof(conf_req));
0164 }
0165
0166 if (h5->state != H5_ACTIVE) {
0167 mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
0168 goto wakeup;
0169 }
0170
0171 if (h5->sleep != H5_AWAKE) {
0172 h5->sleep = H5_SLEEPING;
0173 goto wakeup;
0174 }
0175
0176 BT_DBG("hu %p retransmitting %u pkts", hu, h5->unack.qlen);
0177
0178 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
0179
0180 while ((skb = __skb_dequeue_tail(&h5->unack)) != NULL) {
0181 h5->tx_seq = (h5->tx_seq - 1) & 0x07;
0182 skb_queue_head(&h5->rel, skb);
0183 }
0184
0185 spin_unlock_irqrestore(&h5->unack.lock, flags);
0186
0187 wakeup:
0188 hci_uart_tx_wakeup(hu);
0189 }
0190
0191 static void h5_peer_reset(struct hci_uart *hu)
0192 {
0193 struct h5 *h5 = hu->priv;
0194
0195 bt_dev_err(hu->hdev, "Peer device has reset");
0196
0197 h5->state = H5_UNINITIALIZED;
0198
0199 del_timer(&h5->timer);
0200
0201 skb_queue_purge(&h5->rel);
0202 skb_queue_purge(&h5->unrel);
0203 skb_queue_purge(&h5->unack);
0204
0205 h5->tx_seq = 0;
0206 h5->tx_ack = 0;
0207
0208
0209 hci_reset_dev(hu->hdev);
0210 }
0211
0212 static int h5_open(struct hci_uart *hu)
0213 {
0214 struct h5 *h5;
0215 const unsigned char sync[] = { 0x01, 0x7e };
0216
0217 BT_DBG("hu %p", hu);
0218
0219 if (hu->serdev) {
0220 h5 = serdev_device_get_drvdata(hu->serdev);
0221 } else {
0222 h5 = kzalloc(sizeof(*h5), GFP_KERNEL);
0223 if (!h5)
0224 return -ENOMEM;
0225 }
0226
0227 hu->priv = h5;
0228 h5->hu = hu;
0229
0230 skb_queue_head_init(&h5->unack);
0231 skb_queue_head_init(&h5->rel);
0232 skb_queue_head_init(&h5->unrel);
0233
0234 h5_reset_rx(h5);
0235
0236 timer_setup(&h5->timer, h5_timed_event, 0);
0237
0238 h5->tx_win = H5_TX_WIN_MAX;
0239
0240 if (h5->vnd && h5->vnd->open)
0241 h5->vnd->open(h5);
0242
0243 set_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags);
0244
0245
0246 h5_link_control(hu, sync, sizeof(sync));
0247 mod_timer(&h5->timer, jiffies + H5_SYNC_TIMEOUT);
0248
0249 return 0;
0250 }
0251
0252 static int h5_close(struct hci_uart *hu)
0253 {
0254 struct h5 *h5 = hu->priv;
0255
0256 del_timer_sync(&h5->timer);
0257
0258 skb_queue_purge(&h5->unack);
0259 skb_queue_purge(&h5->rel);
0260 skb_queue_purge(&h5->unrel);
0261
0262 kfree_skb(h5->rx_skb);
0263 h5->rx_skb = NULL;
0264
0265 if (h5->vnd && h5->vnd->close)
0266 h5->vnd->close(h5);
0267
0268 if (!hu->serdev)
0269 kfree(h5);
0270
0271 return 0;
0272 }
0273
0274 static int h5_setup(struct hci_uart *hu)
0275 {
0276 struct h5 *h5 = hu->priv;
0277
0278 if (h5->vnd && h5->vnd->setup)
0279 return h5->vnd->setup(h5);
0280
0281 return 0;
0282 }
0283
0284 static void h5_pkt_cull(struct h5 *h5)
0285 {
0286 struct sk_buff *skb, *tmp;
0287 unsigned long flags;
0288 int i, to_remove;
0289 u8 seq;
0290
0291 spin_lock_irqsave(&h5->unack.lock, flags);
0292
0293 to_remove = skb_queue_len(&h5->unack);
0294 if (to_remove == 0)
0295 goto unlock;
0296
0297 seq = h5->tx_seq;
0298
0299 while (to_remove > 0) {
0300 if (h5->rx_ack == seq)
0301 break;
0302
0303 to_remove--;
0304 seq = (seq - 1) & 0x07;
0305 }
0306
0307 if (seq != h5->rx_ack)
0308 BT_ERR("Controller acked invalid packet");
0309
0310 i = 0;
0311 skb_queue_walk_safe(&h5->unack, skb, tmp) {
0312 if (i++ >= to_remove)
0313 break;
0314
0315 __skb_unlink(skb, &h5->unack);
0316 kfree_skb(skb);
0317 }
0318
0319 if (skb_queue_empty(&h5->unack))
0320 del_timer(&h5->timer);
0321
0322 unlock:
0323 spin_unlock_irqrestore(&h5->unack.lock, flags);
0324 }
0325
0326 static void h5_handle_internal_rx(struct hci_uart *hu)
0327 {
0328 struct h5 *h5 = hu->priv;
0329 const unsigned char sync_req[] = { 0x01, 0x7e };
0330 const unsigned char sync_rsp[] = { 0x02, 0x7d };
0331 unsigned char conf_req[3] = { 0x03, 0xfc };
0332 const unsigned char conf_rsp[] = { 0x04, 0x7b };
0333 const unsigned char wakeup_req[] = { 0x05, 0xfa };
0334 const unsigned char woken_req[] = { 0x06, 0xf9 };
0335 const unsigned char sleep_req[] = { 0x07, 0x78 };
0336 const unsigned char *hdr = h5->rx_skb->data;
0337 const unsigned char *data = &h5->rx_skb->data[4];
0338
0339 BT_DBG("%s", hu->hdev->name);
0340
0341 if (H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT)
0342 return;
0343
0344 if (H5_HDR_LEN(hdr) < 2)
0345 return;
0346
0347 conf_req[2] = h5_cfg_field(h5);
0348
0349 if (memcmp(data, sync_req, 2) == 0) {
0350 if (h5->state == H5_ACTIVE)
0351 h5_peer_reset(hu);
0352 h5_link_control(hu, sync_rsp, 2);
0353 } else if (memcmp(data, sync_rsp, 2) == 0) {
0354 if (h5->state == H5_ACTIVE)
0355 h5_peer_reset(hu);
0356 h5->state = H5_INITIALIZED;
0357 h5_link_control(hu, conf_req, 3);
0358 } else if (memcmp(data, conf_req, 2) == 0) {
0359 h5_link_control(hu, conf_rsp, 2);
0360 h5_link_control(hu, conf_req, 3);
0361 } else if (memcmp(data, conf_rsp, 2) == 0) {
0362 if (H5_HDR_LEN(hdr) > 2)
0363 h5->tx_win = (data[2] & 0x07);
0364 BT_DBG("Three-wire init complete. tx_win %u", h5->tx_win);
0365 h5->state = H5_ACTIVE;
0366 hci_uart_init_ready(hu);
0367 return;
0368 } else if (memcmp(data, sleep_req, 2) == 0) {
0369 BT_DBG("Peer went to sleep");
0370 h5->sleep = H5_SLEEPING;
0371 return;
0372 } else if (memcmp(data, woken_req, 2) == 0) {
0373 BT_DBG("Peer woke up");
0374 h5->sleep = H5_AWAKE;
0375 } else if (memcmp(data, wakeup_req, 2) == 0) {
0376 BT_DBG("Peer requested wakeup");
0377 h5_link_control(hu, woken_req, 2);
0378 h5->sleep = H5_AWAKE;
0379 } else {
0380 BT_DBG("Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
0381 return;
0382 }
0383
0384 hci_uart_tx_wakeup(hu);
0385 }
0386
0387 static void h5_complete_rx_pkt(struct hci_uart *hu)
0388 {
0389 struct h5 *h5 = hu->priv;
0390 const unsigned char *hdr = h5->rx_skb->data;
0391
0392 if (H5_HDR_RELIABLE(hdr)) {
0393 h5->tx_ack = (h5->tx_ack + 1) % 8;
0394 set_bit(H5_TX_ACK_REQ, &h5->flags);
0395 hci_uart_tx_wakeup(hu);
0396 }
0397
0398 h5->rx_ack = H5_HDR_ACK(hdr);
0399
0400 h5_pkt_cull(h5);
0401
0402 switch (H5_HDR_PKT_TYPE(hdr)) {
0403 case HCI_EVENT_PKT:
0404 case HCI_ACLDATA_PKT:
0405 case HCI_SCODATA_PKT:
0406 case HCI_ISODATA_PKT:
0407 hci_skb_pkt_type(h5->rx_skb) = H5_HDR_PKT_TYPE(hdr);
0408
0409
0410 skb_pull(h5->rx_skb, 4);
0411
0412 hci_recv_frame(hu->hdev, h5->rx_skb);
0413 h5->rx_skb = NULL;
0414
0415 break;
0416
0417 default:
0418 h5_handle_internal_rx(hu);
0419 break;
0420 }
0421
0422 h5_reset_rx(h5);
0423 }
0424
0425 static int h5_rx_crc(struct hci_uart *hu, unsigned char c)
0426 {
0427 h5_complete_rx_pkt(hu);
0428
0429 return 0;
0430 }
0431
0432 static int h5_rx_payload(struct hci_uart *hu, unsigned char c)
0433 {
0434 struct h5 *h5 = hu->priv;
0435 const unsigned char *hdr = h5->rx_skb->data;
0436
0437 if (H5_HDR_CRC(hdr)) {
0438 h5->rx_func = h5_rx_crc;
0439 h5->rx_pending = 2;
0440 } else {
0441 h5_complete_rx_pkt(hu);
0442 }
0443
0444 return 0;
0445 }
0446
0447 static int h5_rx_3wire_hdr(struct hci_uart *hu, unsigned char c)
0448 {
0449 struct h5 *h5 = hu->priv;
0450 const unsigned char *hdr = h5->rx_skb->data;
0451
0452 BT_DBG("%s rx: seq %u ack %u crc %u rel %u type %u len %u",
0453 hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
0454 H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
0455 H5_HDR_LEN(hdr));
0456
0457 if (((hdr[0] + hdr[1] + hdr[2] + hdr[3]) & 0xff) != 0xff) {
0458 bt_dev_err(hu->hdev, "Invalid header checksum");
0459 h5_reset_rx(h5);
0460 return 0;
0461 }
0462
0463 if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5->tx_ack) {
0464 bt_dev_err(hu->hdev, "Out-of-order packet arrived (%u != %u)",
0465 H5_HDR_SEQ(hdr), h5->tx_ack);
0466 h5_reset_rx(h5);
0467 return 0;
0468 }
0469
0470 if (h5->state != H5_ACTIVE &&
0471 H5_HDR_PKT_TYPE(hdr) != HCI_3WIRE_LINK_PKT) {
0472 bt_dev_err(hu->hdev, "Non-link packet received in non-active state");
0473 h5_reset_rx(h5);
0474 return 0;
0475 }
0476
0477 h5->rx_func = h5_rx_payload;
0478 h5->rx_pending = H5_HDR_LEN(hdr);
0479
0480 return 0;
0481 }
0482
0483 static int h5_rx_pkt_start(struct hci_uart *hu, unsigned char c)
0484 {
0485 struct h5 *h5 = hu->priv;
0486
0487 if (c == SLIP_DELIMITER)
0488 return 1;
0489
0490 h5->rx_func = h5_rx_3wire_hdr;
0491 h5->rx_pending = 4;
0492
0493 h5->rx_skb = bt_skb_alloc(H5_MAX_LEN, GFP_ATOMIC);
0494 if (!h5->rx_skb) {
0495 bt_dev_err(hu->hdev, "Can't allocate mem for new packet");
0496 h5_reset_rx(h5);
0497 return -ENOMEM;
0498 }
0499
0500 h5->rx_skb->dev = (void *)hu->hdev;
0501
0502 return 0;
0503 }
0504
0505 static int h5_rx_delimiter(struct hci_uart *hu, unsigned char c)
0506 {
0507 struct h5 *h5 = hu->priv;
0508
0509 if (c == SLIP_DELIMITER)
0510 h5->rx_func = h5_rx_pkt_start;
0511
0512 return 1;
0513 }
0514
0515 static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
0516 {
0517 const u8 delim = SLIP_DELIMITER, esc = SLIP_ESC;
0518 const u8 *byte = &c;
0519
0520 if (!test_bit(H5_RX_ESC, &h5->flags) && c == SLIP_ESC) {
0521 set_bit(H5_RX_ESC, &h5->flags);
0522 return;
0523 }
0524
0525 if (test_and_clear_bit(H5_RX_ESC, &h5->flags)) {
0526 switch (c) {
0527 case SLIP_ESC_DELIM:
0528 byte = &delim;
0529 break;
0530 case SLIP_ESC_ESC:
0531 byte = &esc;
0532 break;
0533 default:
0534 BT_ERR("Invalid esc byte 0x%02hhx", c);
0535 h5_reset_rx(h5);
0536 return;
0537 }
0538 }
0539
0540 skb_put_data(h5->rx_skb, byte, 1);
0541 h5->rx_pending--;
0542
0543 BT_DBG("unslipped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
0544 }
0545
0546 static void h5_reset_rx(struct h5 *h5)
0547 {
0548 if (h5->rx_skb) {
0549 kfree_skb(h5->rx_skb);
0550 h5->rx_skb = NULL;
0551 }
0552
0553 h5->rx_func = h5_rx_delimiter;
0554 h5->rx_pending = 0;
0555 clear_bit(H5_RX_ESC, &h5->flags);
0556 }
0557
0558 static int h5_recv(struct hci_uart *hu, const void *data, int count)
0559 {
0560 struct h5 *h5 = hu->priv;
0561 const unsigned char *ptr = data;
0562
0563 BT_DBG("%s pending %zu count %d", hu->hdev->name, h5->rx_pending,
0564 count);
0565
0566 while (count > 0) {
0567 int processed;
0568
0569 if (h5->rx_pending > 0) {
0570 if (*ptr == SLIP_DELIMITER) {
0571 bt_dev_err(hu->hdev, "Too short H5 packet");
0572 h5_reset_rx(h5);
0573 continue;
0574 }
0575
0576 h5_unslip_one_byte(h5, *ptr);
0577
0578 ptr++; count--;
0579 continue;
0580 }
0581
0582 processed = h5->rx_func(hu, *ptr);
0583 if (processed < 0)
0584 return processed;
0585
0586 ptr += processed;
0587 count -= processed;
0588 }
0589
0590 if (hu->serdev) {
0591 pm_runtime_get(&hu->serdev->dev);
0592 pm_runtime_mark_last_busy(&hu->serdev->dev);
0593 pm_runtime_put_autosuspend(&hu->serdev->dev);
0594 }
0595
0596 return 0;
0597 }
0598
0599 static int h5_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0600 {
0601 struct h5 *h5 = hu->priv;
0602
0603 if (skb->len > 0xfff) {
0604 bt_dev_err(hu->hdev, "Packet too long (%u bytes)", skb->len);
0605 kfree_skb(skb);
0606 return 0;
0607 }
0608
0609 if (h5->state != H5_ACTIVE) {
0610 bt_dev_err(hu->hdev, "Ignoring HCI data in non-active state");
0611 kfree_skb(skb);
0612 return 0;
0613 }
0614
0615 switch (hci_skb_pkt_type(skb)) {
0616 case HCI_ACLDATA_PKT:
0617 case HCI_COMMAND_PKT:
0618 skb_queue_tail(&h5->rel, skb);
0619 break;
0620
0621 case HCI_SCODATA_PKT:
0622 case HCI_ISODATA_PKT:
0623 skb_queue_tail(&h5->unrel, skb);
0624 break;
0625
0626 default:
0627 bt_dev_err(hu->hdev, "Unknown packet type %u", hci_skb_pkt_type(skb));
0628 kfree_skb(skb);
0629 break;
0630 }
0631
0632 if (hu->serdev) {
0633 pm_runtime_get_sync(&hu->serdev->dev);
0634 pm_runtime_mark_last_busy(&hu->serdev->dev);
0635 pm_runtime_put_autosuspend(&hu->serdev->dev);
0636 }
0637
0638 return 0;
0639 }
0640
0641 static void h5_slip_delim(struct sk_buff *skb)
0642 {
0643 const char delim = SLIP_DELIMITER;
0644
0645 skb_put_data(skb, &delim, 1);
0646 }
0647
0648 static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
0649 {
0650 const char esc_delim[2] = { SLIP_ESC, SLIP_ESC_DELIM };
0651 const char esc_esc[2] = { SLIP_ESC, SLIP_ESC_ESC };
0652
0653 switch (c) {
0654 case SLIP_DELIMITER:
0655 skb_put_data(skb, &esc_delim, 2);
0656 break;
0657 case SLIP_ESC:
0658 skb_put_data(skb, &esc_esc, 2);
0659 break;
0660 default:
0661 skb_put_data(skb, &c, 1);
0662 }
0663 }
0664
0665 static bool valid_packet_type(u8 type)
0666 {
0667 switch (type) {
0668 case HCI_ACLDATA_PKT:
0669 case HCI_COMMAND_PKT:
0670 case HCI_SCODATA_PKT:
0671 case HCI_ISODATA_PKT:
0672 case HCI_3WIRE_LINK_PKT:
0673 case HCI_3WIRE_ACK_PKT:
0674 return true;
0675 default:
0676 return false;
0677 }
0678 }
0679
0680 static struct sk_buff *h5_prepare_pkt(struct hci_uart *hu, u8 pkt_type,
0681 const u8 *data, size_t len)
0682 {
0683 struct h5 *h5 = hu->priv;
0684 struct sk_buff *nskb;
0685 u8 hdr[4];
0686 int i;
0687
0688 if (!valid_packet_type(pkt_type)) {
0689 bt_dev_err(hu->hdev, "Unknown packet type %u", pkt_type);
0690 return NULL;
0691 }
0692
0693
0694
0695
0696
0697
0698
0699 nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
0700 if (!nskb)
0701 return NULL;
0702
0703 hci_skb_pkt_type(nskb) = pkt_type;
0704
0705 h5_slip_delim(nskb);
0706
0707 hdr[0] = h5->tx_ack << 3;
0708 clear_bit(H5_TX_ACK_REQ, &h5->flags);
0709
0710
0711 if (pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) {
0712 hdr[0] |= 1 << 7;
0713 hdr[0] |= h5->tx_seq;
0714 h5->tx_seq = (h5->tx_seq + 1) % 8;
0715 }
0716
0717 hdr[1] = pkt_type | ((len & 0x0f) << 4);
0718 hdr[2] = len >> 4;
0719 hdr[3] = ~((hdr[0] + hdr[1] + hdr[2]) & 0xff);
0720
0721 BT_DBG("%s tx: seq %u ack %u crc %u rel %u type %u len %u",
0722 hu->hdev->name, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
0723 H5_HDR_CRC(hdr), H5_HDR_RELIABLE(hdr), H5_HDR_PKT_TYPE(hdr),
0724 H5_HDR_LEN(hdr));
0725
0726 for (i = 0; i < 4; i++)
0727 h5_slip_one_byte(nskb, hdr[i]);
0728
0729 for (i = 0; i < len; i++)
0730 h5_slip_one_byte(nskb, data[i]);
0731
0732 h5_slip_delim(nskb);
0733
0734 return nskb;
0735 }
0736
0737 static struct sk_buff *h5_dequeue(struct hci_uart *hu)
0738 {
0739 struct h5 *h5 = hu->priv;
0740 unsigned long flags;
0741 struct sk_buff *skb, *nskb;
0742
0743 if (h5->sleep != H5_AWAKE) {
0744 const unsigned char wakeup_req[] = { 0x05, 0xfa };
0745
0746 if (h5->sleep == H5_WAKING_UP)
0747 return NULL;
0748
0749 h5->sleep = H5_WAKING_UP;
0750 BT_DBG("Sending wakeup request");
0751
0752 mod_timer(&h5->timer, jiffies + HZ / 100);
0753 return h5_prepare_pkt(hu, HCI_3WIRE_LINK_PKT, wakeup_req, 2);
0754 }
0755
0756 skb = skb_dequeue(&h5->unrel);
0757 if (skb) {
0758 nskb = h5_prepare_pkt(hu, hci_skb_pkt_type(skb),
0759 skb->data, skb->len);
0760 if (nskb) {
0761 kfree_skb(skb);
0762 return nskb;
0763 }
0764
0765 skb_queue_head(&h5->unrel, skb);
0766 bt_dev_err(hu->hdev, "Could not dequeue pkt because alloc_skb failed");
0767 }
0768
0769 spin_lock_irqsave_nested(&h5->unack.lock, flags, SINGLE_DEPTH_NESTING);
0770
0771 if (h5->unack.qlen >= h5->tx_win)
0772 goto unlock;
0773
0774 skb = skb_dequeue(&h5->rel);
0775 if (skb) {
0776 nskb = h5_prepare_pkt(hu, hci_skb_pkt_type(skb),
0777 skb->data, skb->len);
0778 if (nskb) {
0779 __skb_queue_tail(&h5->unack, skb);
0780 mod_timer(&h5->timer, jiffies + H5_ACK_TIMEOUT);
0781 spin_unlock_irqrestore(&h5->unack.lock, flags);
0782 return nskb;
0783 }
0784
0785 skb_queue_head(&h5->rel, skb);
0786 bt_dev_err(hu->hdev, "Could not dequeue pkt because alloc_skb failed");
0787 }
0788
0789 unlock:
0790 spin_unlock_irqrestore(&h5->unack.lock, flags);
0791
0792 if (test_bit(H5_TX_ACK_REQ, &h5->flags))
0793 return h5_prepare_pkt(hu, HCI_3WIRE_ACK_PKT, NULL, 0);
0794
0795 return NULL;
0796 }
0797
0798 static int h5_flush(struct hci_uart *hu)
0799 {
0800 BT_DBG("hu %p", hu);
0801 return 0;
0802 }
0803
0804 static const struct hci_uart_proto h5p = {
0805 .id = HCI_UART_3WIRE,
0806 .name = "Three-wire (H5)",
0807 .open = h5_open,
0808 .close = h5_close,
0809 .setup = h5_setup,
0810 .recv = h5_recv,
0811 .enqueue = h5_enqueue,
0812 .dequeue = h5_dequeue,
0813 .flush = h5_flush,
0814 };
0815
0816 static int h5_serdev_probe(struct serdev_device *serdev)
0817 {
0818 struct device *dev = &serdev->dev;
0819 struct h5 *h5;
0820 const struct h5_device_data *data;
0821
0822 h5 = devm_kzalloc(dev, sizeof(*h5), GFP_KERNEL);
0823 if (!h5)
0824 return -ENOMEM;
0825
0826 h5->hu = &h5->serdev_hu;
0827 h5->serdev_hu.serdev = serdev;
0828 serdev_device_set_drvdata(serdev, h5);
0829
0830 if (has_acpi_companion(dev)) {
0831 const struct acpi_device_id *match;
0832
0833 match = acpi_match_device(dev->driver->acpi_match_table, dev);
0834 if (!match)
0835 return -ENODEV;
0836
0837 data = (const struct h5_device_data *)match->driver_data;
0838 h5->vnd = data->vnd;
0839 h5->id = (char *)match->id;
0840
0841 if (h5->vnd->acpi_gpio_map)
0842 devm_acpi_dev_add_driver_gpios(dev,
0843 h5->vnd->acpi_gpio_map);
0844 } else {
0845 data = of_device_get_match_data(dev);
0846 if (!data)
0847 return -ENODEV;
0848
0849 h5->vnd = data->vnd;
0850 }
0851
0852 if (data->driver_info & H5_INFO_WAKEUP_DISABLE)
0853 set_bit(H5_WAKEUP_DISABLE, &h5->flags);
0854
0855 h5->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
0856 if (IS_ERR(h5->enable_gpio))
0857 return PTR_ERR(h5->enable_gpio);
0858
0859 h5->device_wake_gpio = devm_gpiod_get_optional(dev, "device-wake",
0860 GPIOD_OUT_LOW);
0861 if (IS_ERR(h5->device_wake_gpio))
0862 return PTR_ERR(h5->device_wake_gpio);
0863
0864 return hci_uart_register_device(&h5->serdev_hu, &h5p);
0865 }
0866
0867 static void h5_serdev_remove(struct serdev_device *serdev)
0868 {
0869 struct h5 *h5 = serdev_device_get_drvdata(serdev);
0870
0871 hci_uart_unregister_device(&h5->serdev_hu);
0872 }
0873
0874 static int __maybe_unused h5_serdev_suspend(struct device *dev)
0875 {
0876 struct h5 *h5 = dev_get_drvdata(dev);
0877 int ret = 0;
0878
0879 if (h5->vnd && h5->vnd->suspend)
0880 ret = h5->vnd->suspend(h5);
0881
0882 return ret;
0883 }
0884
0885 static int __maybe_unused h5_serdev_resume(struct device *dev)
0886 {
0887 struct h5 *h5 = dev_get_drvdata(dev);
0888 int ret = 0;
0889
0890 if (h5->vnd && h5->vnd->resume)
0891 ret = h5->vnd->resume(h5);
0892
0893 return ret;
0894 }
0895
0896 #ifdef CONFIG_BT_HCIUART_RTL
0897 static int h5_btrtl_setup(struct h5 *h5)
0898 {
0899 struct btrtl_device_info *btrtl_dev;
0900 struct sk_buff *skb;
0901 __le32 baudrate_data;
0902 u32 device_baudrate;
0903 unsigned int controller_baudrate;
0904 bool flow_control;
0905 int err;
0906
0907 btrtl_dev = btrtl_initialize(h5->hu->hdev, h5->id);
0908 if (IS_ERR(btrtl_dev))
0909 return PTR_ERR(btrtl_dev);
0910
0911 err = btrtl_get_uart_settings(h5->hu->hdev, btrtl_dev,
0912 &controller_baudrate, &device_baudrate,
0913 &flow_control);
0914 if (err)
0915 goto out_free;
0916
0917 baudrate_data = cpu_to_le32(device_baudrate);
0918 skb = __hci_cmd_sync(h5->hu->hdev, 0xfc17, sizeof(baudrate_data),
0919 &baudrate_data, HCI_INIT_TIMEOUT);
0920 if (IS_ERR(skb)) {
0921 rtl_dev_err(h5->hu->hdev, "set baud rate command failed\n");
0922 err = PTR_ERR(skb);
0923 goto out_free;
0924 } else {
0925 kfree_skb(skb);
0926 }
0927
0928 usleep_range(10000, 20000);
0929
0930 serdev_device_set_baudrate(h5->hu->serdev, controller_baudrate);
0931 serdev_device_set_flow_control(h5->hu->serdev, flow_control);
0932
0933 if (flow_control)
0934 set_bit(H5_HW_FLOW_CONTROL, &h5->flags);
0935
0936 err = btrtl_download_firmware(h5->hu->hdev, btrtl_dev);
0937
0938 usleep_range(10000, 20000);
0939
0940 btrtl_set_quirks(h5->hu->hdev, btrtl_dev);
0941
0942 out_free:
0943 btrtl_free(btrtl_dev);
0944
0945 return err;
0946 }
0947
0948 static void h5_btrtl_open(struct h5 *h5)
0949 {
0950
0951
0952
0953
0954
0955 if (test_bit(H5_WAKEUP_DISABLE, &h5->flags))
0956 set_bit(HCI_UART_NO_SUSPEND_NOTIFIER, &h5->hu->flags);
0957
0958
0959 serdev_device_set_flow_control(h5->hu->serdev, false);
0960 serdev_device_set_parity(h5->hu->serdev, SERDEV_PARITY_EVEN);
0961 serdev_device_set_baudrate(h5->hu->serdev, 115200);
0962
0963 if (!test_bit(H5_WAKEUP_DISABLE, &h5->flags)) {
0964 pm_runtime_set_active(&h5->hu->serdev->dev);
0965 pm_runtime_use_autosuspend(&h5->hu->serdev->dev);
0966 pm_runtime_set_autosuspend_delay(&h5->hu->serdev->dev,
0967 SUSPEND_TIMEOUT_MS);
0968 pm_runtime_enable(&h5->hu->serdev->dev);
0969 }
0970
0971
0972 gpiod_set_value_cansleep(h5->enable_gpio, 0);
0973 gpiod_set_value_cansleep(h5->device_wake_gpio, 0);
0974 msleep(100);
0975
0976
0977 gpiod_set_value_cansleep(h5->enable_gpio, 1);
0978 gpiod_set_value_cansleep(h5->device_wake_gpio, 1);
0979 msleep(500);
0980 }
0981
0982 static void h5_btrtl_close(struct h5 *h5)
0983 {
0984 if (!test_bit(H5_WAKEUP_DISABLE, &h5->flags))
0985 pm_runtime_disable(&h5->hu->serdev->dev);
0986
0987 gpiod_set_value_cansleep(h5->device_wake_gpio, 0);
0988 gpiod_set_value_cansleep(h5->enable_gpio, 0);
0989 }
0990
0991
0992
0993
0994
0995
0996
0997 static int h5_btrtl_suspend(struct h5 *h5)
0998 {
0999 serdev_device_set_flow_control(h5->hu->serdev, false);
1000 gpiod_set_value_cansleep(h5->device_wake_gpio, 0);
1001
1002 if (test_bit(H5_WAKEUP_DISABLE, &h5->flags))
1003 gpiod_set_value_cansleep(h5->enable_gpio, 0);
1004
1005 return 0;
1006 }
1007
1008 struct h5_btrtl_reprobe {
1009 struct device *dev;
1010 struct work_struct work;
1011 };
1012
1013 static void h5_btrtl_reprobe_worker(struct work_struct *work)
1014 {
1015 struct h5_btrtl_reprobe *reprobe =
1016 container_of(work, struct h5_btrtl_reprobe, work);
1017 int ret;
1018
1019 ret = device_reprobe(reprobe->dev);
1020 if (ret && ret != -EPROBE_DEFER)
1021 dev_err(reprobe->dev, "Reprobe error %d\n", ret);
1022
1023 put_device(reprobe->dev);
1024 kfree(reprobe);
1025 module_put(THIS_MODULE);
1026 }
1027
1028 static int h5_btrtl_resume(struct h5 *h5)
1029 {
1030 if (test_bit(H5_WAKEUP_DISABLE, &h5->flags)) {
1031 struct h5_btrtl_reprobe *reprobe;
1032
1033 reprobe = kzalloc(sizeof(*reprobe), GFP_KERNEL);
1034 if (!reprobe)
1035 return -ENOMEM;
1036
1037 __module_get(THIS_MODULE);
1038
1039 INIT_WORK(&reprobe->work, h5_btrtl_reprobe_worker);
1040 reprobe->dev = get_device(&h5->hu->serdev->dev);
1041 queue_work(system_long_wq, &reprobe->work);
1042 } else {
1043 gpiod_set_value_cansleep(h5->device_wake_gpio, 1);
1044
1045 if (test_bit(H5_HW_FLOW_CONTROL, &h5->flags))
1046 serdev_device_set_flow_control(h5->hu->serdev, true);
1047 }
1048
1049 return 0;
1050 }
1051
1052 static const struct acpi_gpio_params btrtl_device_wake_gpios = { 0, 0, false };
1053 static const struct acpi_gpio_params btrtl_enable_gpios = { 1, 0, false };
1054 static const struct acpi_gpio_params btrtl_host_wake_gpios = { 2, 0, false };
1055 static const struct acpi_gpio_mapping acpi_btrtl_gpios[] = {
1056 { "device-wake-gpios", &btrtl_device_wake_gpios, 1 },
1057 { "enable-gpios", &btrtl_enable_gpios, 1 },
1058 { "host-wake-gpios", &btrtl_host_wake_gpios, 1 },
1059 {},
1060 };
1061
1062 static struct h5_vnd rtl_vnd = {
1063 .setup = h5_btrtl_setup,
1064 .open = h5_btrtl_open,
1065 .close = h5_btrtl_close,
1066 .suspend = h5_btrtl_suspend,
1067 .resume = h5_btrtl_resume,
1068 .acpi_gpio_map = acpi_btrtl_gpios,
1069 };
1070
1071 static const struct h5_device_data h5_data_rtl8822cs = {
1072 .vnd = &rtl_vnd,
1073 };
1074
1075 static const struct h5_device_data h5_data_rtl8723bs = {
1076 .driver_info = H5_INFO_WAKEUP_DISABLE,
1077 .vnd = &rtl_vnd,
1078 };
1079 #endif
1080
1081 #ifdef CONFIG_ACPI
1082 static const struct acpi_device_id h5_acpi_match[] = {
1083 #ifdef CONFIG_BT_HCIUART_RTL
1084 { "OBDA0623", (kernel_ulong_t)&h5_data_rtl8723bs },
1085 { "OBDA8723", (kernel_ulong_t)&h5_data_rtl8723bs },
1086 #endif
1087 { },
1088 };
1089 MODULE_DEVICE_TABLE(acpi, h5_acpi_match);
1090 #endif
1091
1092 static const struct dev_pm_ops h5_serdev_pm_ops = {
1093 SET_SYSTEM_SLEEP_PM_OPS(h5_serdev_suspend, h5_serdev_resume)
1094 SET_RUNTIME_PM_OPS(h5_serdev_suspend, h5_serdev_resume, NULL)
1095 };
1096
1097 static const struct of_device_id rtl_bluetooth_of_match[] = {
1098 #ifdef CONFIG_BT_HCIUART_RTL
1099 { .compatible = "realtek,rtl8822cs-bt",
1100 .data = (const void *)&h5_data_rtl8822cs },
1101 { .compatible = "realtek,rtl8723bs-bt",
1102 .data = (const void *)&h5_data_rtl8723bs },
1103 { .compatible = "realtek,rtl8723ds-bt",
1104 .data = (const void *)&h5_data_rtl8723bs },
1105 #endif
1106 { },
1107 };
1108 MODULE_DEVICE_TABLE(of, rtl_bluetooth_of_match);
1109
1110 static struct serdev_device_driver h5_serdev_driver = {
1111 .probe = h5_serdev_probe,
1112 .remove = h5_serdev_remove,
1113 .driver = {
1114 .name = "hci_uart_h5",
1115 .acpi_match_table = ACPI_PTR(h5_acpi_match),
1116 .pm = &h5_serdev_pm_ops,
1117 .of_match_table = rtl_bluetooth_of_match,
1118 },
1119 };
1120
1121 int __init h5_init(void)
1122 {
1123 serdev_device_driver_register(&h5_serdev_driver);
1124 return hci_uart_register_proto(&h5p);
1125 }
1126
1127 int __exit h5_deinit(void)
1128 {
1129 serdev_device_driver_unregister(&h5_serdev_driver);
1130 return hci_uart_unregister_proto(&h5p);
1131 }