Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Bluetooth HCI UART H4 driver with Nokia Extensions AKA Nokia H4+
0004  *
0005  *  Copyright (C) 2015 Marcel Holtmann <marcel@holtmann.org>
0006  *  Copyright (C) 2015-2017 Sebastian Reichel <sre@kernel.org>
0007  */
0008 
0009 #include <linux/clk.h>
0010 #include <linux/errno.h>
0011 #include <linux/firmware.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/serdev.h>
0019 #include <linux/skbuff.h>
0020 #include <linux/slab.h>
0021 #include <linux/string.h>
0022 #include <linux/types.h>
0023 #include <asm/unaligned.h>
0024 #include <net/bluetooth/bluetooth.h>
0025 #include <net/bluetooth/hci_core.h>
0026 
0027 #include "hci_uart.h"
0028 #include "btbcm.h"
0029 
0030 #define VERSION "0.1"
0031 
0032 #define NOKIA_ID_BCM2048    0x04
0033 #define NOKIA_ID_TI1271     0x31
0034 
0035 #define FIRMWARE_BCM2048    "nokia/bcmfw.bin"
0036 #define FIRMWARE_TI1271     "nokia/ti1273.bin"
0037 
0038 #define HCI_NOKIA_NEG_PKT   0x06
0039 #define HCI_NOKIA_ALIVE_PKT 0x07
0040 #define HCI_NOKIA_RADIO_PKT 0x08
0041 
0042 #define HCI_NOKIA_NEG_HDR_SIZE      1
0043 #define HCI_NOKIA_MAX_NEG_SIZE      255
0044 #define HCI_NOKIA_ALIVE_HDR_SIZE    1
0045 #define HCI_NOKIA_MAX_ALIVE_SIZE    255
0046 #define HCI_NOKIA_RADIO_HDR_SIZE    2
0047 #define HCI_NOKIA_MAX_RADIO_SIZE    255
0048 
0049 #define NOKIA_PROTO_PKT     0x44
0050 #define NOKIA_PROTO_BYTE    0x4c
0051 
0052 #define NOKIA_NEG_REQ       0x00
0053 #define NOKIA_NEG_ACK       0x20
0054 #define NOKIA_NEG_NAK       0x40
0055 
0056 #define H4_TYPE_SIZE        1
0057 
0058 #define NOKIA_RECV_ALIVE \
0059     .type = HCI_NOKIA_ALIVE_PKT, \
0060     .hlen = HCI_NOKIA_ALIVE_HDR_SIZE, \
0061     .loff = 0, \
0062     .lsize = 1, \
0063     .maxlen = HCI_NOKIA_MAX_ALIVE_SIZE \
0064 
0065 #define NOKIA_RECV_NEG \
0066     .type = HCI_NOKIA_NEG_PKT, \
0067     .hlen = HCI_NOKIA_NEG_HDR_SIZE, \
0068     .loff = 0, \
0069     .lsize = 1, \
0070     .maxlen = HCI_NOKIA_MAX_NEG_SIZE \
0071 
0072 #define NOKIA_RECV_RADIO \
0073     .type = HCI_NOKIA_RADIO_PKT, \
0074     .hlen = HCI_NOKIA_RADIO_HDR_SIZE, \
0075     .loff = 1, \
0076     .lsize = 1, \
0077     .maxlen = HCI_NOKIA_MAX_RADIO_SIZE \
0078 
0079 struct hci_nokia_neg_hdr {
0080     u8  dlen;
0081 } __packed;
0082 
0083 struct hci_nokia_neg_cmd {
0084     u8  ack;
0085     u16 baud;
0086     u16 unused1;
0087     u8  proto;
0088     u16 sys_clk;
0089     u16 unused2;
0090 } __packed;
0091 
0092 #define NOKIA_ALIVE_REQ   0x55
0093 #define NOKIA_ALIVE_RESP  0xcc
0094 
0095 struct hci_nokia_alive_hdr {
0096     u8  dlen;
0097 } __packed;
0098 
0099 struct hci_nokia_alive_pkt {
0100     u8  mid;
0101     u8  unused;
0102 } __packed;
0103 
0104 struct hci_nokia_neg_evt {
0105     u8  ack;
0106     u16 baud;
0107     u16 unused1;
0108     u8  proto;
0109     u16 sys_clk;
0110     u16 unused2;
0111     u8  man_id;
0112     u8  ver_id;
0113 } __packed;
0114 
0115 #define MAX_BAUD_RATE       3692300
0116 #define SETUP_BAUD_RATE     921600
0117 #define INIT_BAUD_RATE      120000
0118 
0119 struct hci_nokia_radio_hdr {
0120     u8  evt;
0121     u8  dlen;
0122 } __packed;
0123 
0124 struct nokia_bt_dev {
0125     struct hci_uart hu;
0126     struct serdev_device *serdev;
0127 
0128     struct gpio_desc *reset;
0129     struct gpio_desc *wakeup_host;
0130     struct gpio_desc *wakeup_bt;
0131     unsigned long sysclk_speed;
0132 
0133     int wake_irq;
0134     struct sk_buff *rx_skb;
0135     struct sk_buff_head txq;
0136     bdaddr_t bdaddr;
0137 
0138     int init_error;
0139     struct completion init_completion;
0140 
0141     u8 man_id;
0142     u8 ver_id;
0143 
0144     bool initialized;
0145     bool tx_enabled;
0146     bool rx_enabled;
0147 };
0148 
0149 static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb);
0150 
0151 static void nokia_flow_control(struct serdev_device *serdev, bool enable)
0152 {
0153     if (enable) {
0154         serdev_device_set_rts(serdev, true);
0155         serdev_device_set_flow_control(serdev, true);
0156     } else {
0157         serdev_device_set_flow_control(serdev, false);
0158         serdev_device_set_rts(serdev, false);
0159     }
0160 }
0161 
0162 static irqreturn_t wakeup_handler(int irq, void *data)
0163 {
0164     struct nokia_bt_dev *btdev = data;
0165     struct device *dev = &btdev->serdev->dev;
0166     int wake_state = gpiod_get_value(btdev->wakeup_host);
0167 
0168     if (btdev->rx_enabled == wake_state)
0169         return IRQ_HANDLED;
0170 
0171     if (wake_state)
0172         pm_runtime_get(dev);
0173     else
0174         pm_runtime_put(dev);
0175 
0176     btdev->rx_enabled = wake_state;
0177 
0178     return IRQ_HANDLED;
0179 }
0180 
0181 static int nokia_reset(struct hci_uart *hu)
0182 {
0183     struct nokia_bt_dev *btdev = hu->priv;
0184     struct device *dev = &btdev->serdev->dev;
0185     int err;
0186 
0187     /* reset routine */
0188     gpiod_set_value_cansleep(btdev->reset, 1);
0189     gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
0190 
0191     msleep(100);
0192 
0193     /* safety check */
0194     err = gpiod_get_value_cansleep(btdev->wakeup_host);
0195     if (err == 1) {
0196         dev_err(dev, "reset: host wakeup not low!");
0197         return -EPROTO;
0198     }
0199 
0200     /* flush queue */
0201     serdev_device_write_flush(btdev->serdev);
0202 
0203     /* init uart */
0204     nokia_flow_control(btdev->serdev, false);
0205     serdev_device_set_baudrate(btdev->serdev, INIT_BAUD_RATE);
0206 
0207     gpiod_set_value_cansleep(btdev->reset, 0);
0208 
0209     /* wait for cts */
0210     err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
0211     if (err < 0) {
0212         dev_err(dev, "CTS not received: %d", err);
0213         return err;
0214     }
0215 
0216     nokia_flow_control(btdev->serdev, true);
0217 
0218     return 0;
0219 }
0220 
0221 static int nokia_send_alive_packet(struct hci_uart *hu)
0222 {
0223     struct nokia_bt_dev *btdev = hu->priv;
0224     struct device *dev = &btdev->serdev->dev;
0225     struct hci_nokia_alive_hdr *hdr;
0226     struct hci_nokia_alive_pkt *pkt;
0227     struct sk_buff *skb;
0228     int len;
0229 
0230     init_completion(&btdev->init_completion);
0231 
0232     len = H4_TYPE_SIZE + sizeof(*hdr) + sizeof(*pkt);
0233     skb = bt_skb_alloc(len, GFP_KERNEL);
0234     if (!skb)
0235         return -ENOMEM;
0236 
0237     hci_skb_pkt_type(skb) = HCI_NOKIA_ALIVE_PKT;
0238     memset(skb->data, 0x00, len);
0239 
0240     hdr = skb_put(skb, sizeof(*hdr));
0241     hdr->dlen = sizeof(*pkt);
0242     pkt = skb_put(skb, sizeof(*pkt));
0243     pkt->mid = NOKIA_ALIVE_REQ;
0244 
0245     nokia_enqueue(hu, skb);
0246     hci_uart_tx_wakeup(hu);
0247 
0248     dev_dbg(dev, "Alive sent");
0249 
0250     if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
0251         msecs_to_jiffies(1000))) {
0252         return -ETIMEDOUT;
0253     }
0254 
0255     if (btdev->init_error < 0)
0256         return btdev->init_error;
0257 
0258     return 0;
0259 }
0260 
0261 static int nokia_send_negotiation(struct hci_uart *hu)
0262 {
0263     struct nokia_bt_dev *btdev = hu->priv;
0264     struct device *dev = &btdev->serdev->dev;
0265     struct hci_nokia_neg_cmd *neg_cmd;
0266     struct hci_nokia_neg_hdr *neg_hdr;
0267     struct sk_buff *skb;
0268     int len, err;
0269     u16 baud = DIV_ROUND_CLOSEST(btdev->sysclk_speed * 10, SETUP_BAUD_RATE);
0270     int sysclk = btdev->sysclk_speed / 1000;
0271 
0272     len = H4_TYPE_SIZE + sizeof(*neg_hdr) + sizeof(*neg_cmd);
0273     skb = bt_skb_alloc(len, GFP_KERNEL);
0274     if (!skb)
0275         return -ENOMEM;
0276 
0277     hci_skb_pkt_type(skb) = HCI_NOKIA_NEG_PKT;
0278 
0279     neg_hdr = skb_put(skb, sizeof(*neg_hdr));
0280     neg_hdr->dlen = sizeof(*neg_cmd);
0281 
0282     neg_cmd = skb_put(skb, sizeof(*neg_cmd));
0283     neg_cmd->ack = NOKIA_NEG_REQ;
0284     neg_cmd->baud = cpu_to_le16(baud);
0285     neg_cmd->unused1 = 0x0000;
0286     neg_cmd->proto = NOKIA_PROTO_BYTE;
0287     neg_cmd->sys_clk = cpu_to_le16(sysclk);
0288     neg_cmd->unused2 = 0x0000;
0289 
0290     btdev->init_error = 0;
0291     init_completion(&btdev->init_completion);
0292 
0293     nokia_enqueue(hu, skb);
0294     hci_uart_tx_wakeup(hu);
0295 
0296     dev_dbg(dev, "Negotiation sent");
0297 
0298     if (!wait_for_completion_interruptible_timeout(&btdev->init_completion,
0299         msecs_to_jiffies(10000))) {
0300         return -ETIMEDOUT;
0301     }
0302 
0303     if (btdev->init_error < 0)
0304         return btdev->init_error;
0305 
0306     /* Change to previously negotiated speed. Flow Control
0307      * is disabled until bluetooth adapter is ready to avoid
0308      * broken bytes being received.
0309      */
0310     nokia_flow_control(btdev->serdev, false);
0311     serdev_device_set_baudrate(btdev->serdev, SETUP_BAUD_RATE);
0312     err = serdev_device_wait_for_cts(btdev->serdev, true, 200);
0313     if (err < 0) {
0314         dev_err(dev, "CTS not received: %d", err);
0315         return err;
0316     }
0317     nokia_flow_control(btdev->serdev, true);
0318 
0319     dev_dbg(dev, "Negotiation successful");
0320 
0321     return 0;
0322 }
0323 
0324 static int nokia_setup_fw(struct hci_uart *hu)
0325 {
0326     struct nokia_bt_dev *btdev = hu->priv;
0327     struct device *dev = &btdev->serdev->dev;
0328     const char *fwname;
0329     const struct firmware *fw;
0330     const u8 *fw_ptr;
0331     size_t fw_size;
0332     int err;
0333 
0334     dev_dbg(dev, "setup firmware");
0335 
0336     if (btdev->man_id == NOKIA_ID_BCM2048) {
0337         fwname = FIRMWARE_BCM2048;
0338     } else if (btdev->man_id == NOKIA_ID_TI1271) {
0339         fwname = FIRMWARE_TI1271;
0340     } else {
0341         dev_err(dev, "Unsupported bluetooth device!");
0342         return -ENODEV;
0343     }
0344 
0345     err = request_firmware(&fw, fwname, dev);
0346     if (err < 0) {
0347         dev_err(dev, "%s: Failed to load Nokia firmware file (%d)",
0348             hu->hdev->name, err);
0349         return err;
0350     }
0351 
0352     fw_ptr = fw->data;
0353     fw_size = fw->size;
0354 
0355     while (fw_size >= 4) {
0356         u16 pkt_size = get_unaligned_le16(fw_ptr);
0357         u8 pkt_type = fw_ptr[2];
0358         const struct hci_command_hdr *cmd;
0359         u16 opcode;
0360         struct sk_buff *skb;
0361 
0362         switch (pkt_type) {
0363         case HCI_COMMAND_PKT:
0364             cmd = (struct hci_command_hdr *)(fw_ptr + 3);
0365             opcode = le16_to_cpu(cmd->opcode);
0366 
0367             skb = __hci_cmd_sync(hu->hdev, opcode, cmd->plen,
0368                          fw_ptr + 3 + HCI_COMMAND_HDR_SIZE,
0369                          HCI_INIT_TIMEOUT);
0370             if (IS_ERR(skb)) {
0371                 err = PTR_ERR(skb);
0372                 dev_err(dev, "%s: FW command %04x failed (%d)",
0373                        hu->hdev->name, opcode, err);
0374                 goto done;
0375             }
0376             kfree_skb(skb);
0377             break;
0378         case HCI_NOKIA_RADIO_PKT:
0379         case HCI_NOKIA_NEG_PKT:
0380         case HCI_NOKIA_ALIVE_PKT:
0381             break;
0382         }
0383 
0384         fw_ptr += pkt_size + 2;
0385         fw_size -= pkt_size + 2;
0386     }
0387 
0388 done:
0389     release_firmware(fw);
0390     return err;
0391 }
0392 
0393 static int nokia_setup(struct hci_uart *hu)
0394 {
0395     struct nokia_bt_dev *btdev = hu->priv;
0396     struct device *dev = &btdev->serdev->dev;
0397     int err;
0398 
0399     btdev->initialized = false;
0400 
0401     nokia_flow_control(btdev->serdev, false);
0402 
0403     pm_runtime_get_sync(dev);
0404 
0405     if (btdev->tx_enabled) {
0406         gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
0407         pm_runtime_put(&btdev->serdev->dev);
0408         btdev->tx_enabled = false;
0409     }
0410 
0411     dev_dbg(dev, "protocol setup");
0412 
0413     /* 0. reset connection */
0414     err = nokia_reset(hu);
0415     if (err < 0) {
0416         dev_err(dev, "Reset failed: %d", err);
0417         goto out;
0418     }
0419 
0420     /* 1. negotiate speed etc */
0421     err = nokia_send_negotiation(hu);
0422     if (err < 0) {
0423         dev_err(dev, "Negotiation failed: %d", err);
0424         goto out;
0425     }
0426 
0427     /* 2. verify correct setup using alive packet */
0428     err = nokia_send_alive_packet(hu);
0429     if (err < 0) {
0430         dev_err(dev, "Alive check failed: %d", err);
0431         goto out;
0432     }
0433 
0434     /* 3. send firmware */
0435     err = nokia_setup_fw(hu);
0436     if (err < 0) {
0437         dev_err(dev, "Could not setup FW: %d", err);
0438         goto out;
0439     }
0440 
0441     nokia_flow_control(btdev->serdev, false);
0442     serdev_device_set_baudrate(btdev->serdev, MAX_BAUD_RATE);
0443     nokia_flow_control(btdev->serdev, true);
0444 
0445     if (btdev->man_id == NOKIA_ID_BCM2048) {
0446         hu->hdev->set_bdaddr = btbcm_set_bdaddr;
0447         set_bit(HCI_QUIRK_INVALID_BDADDR, &hu->hdev->quirks);
0448         dev_dbg(dev, "bcm2048 has invalid bluetooth address!");
0449     }
0450 
0451     dev_dbg(dev, "protocol setup done!");
0452 
0453     gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
0454     pm_runtime_put(dev);
0455     btdev->tx_enabled = false;
0456     btdev->initialized = true;
0457 
0458     return 0;
0459 out:
0460     pm_runtime_put(dev);
0461 
0462     return err;
0463 }
0464 
0465 static int nokia_open(struct hci_uart *hu)
0466 {
0467     struct device *dev = &hu->serdev->dev;
0468 
0469     dev_dbg(dev, "protocol open");
0470 
0471     pm_runtime_enable(dev);
0472 
0473     return 0;
0474 }
0475 
0476 static int nokia_flush(struct hci_uart *hu)
0477 {
0478     struct nokia_bt_dev *btdev = hu->priv;
0479 
0480     dev_dbg(&btdev->serdev->dev, "flush device");
0481 
0482     skb_queue_purge(&btdev->txq);
0483 
0484     return 0;
0485 }
0486 
0487 static int nokia_close(struct hci_uart *hu)
0488 {
0489     struct nokia_bt_dev *btdev = hu->priv;
0490     struct device *dev = &btdev->serdev->dev;
0491 
0492     dev_dbg(dev, "close device");
0493 
0494     btdev->initialized = false;
0495 
0496     skb_queue_purge(&btdev->txq);
0497 
0498     kfree_skb(btdev->rx_skb);
0499 
0500     /* disable module */
0501     gpiod_set_value(btdev->reset, 1);
0502     gpiod_set_value(btdev->wakeup_bt, 0);
0503 
0504     pm_runtime_disable(&btdev->serdev->dev);
0505 
0506     return 0;
0507 }
0508 
0509 /* Enqueue frame for transmittion (padding, crc, etc) */
0510 static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0511 {
0512     struct nokia_bt_dev *btdev = hu->priv;
0513     int err;
0514 
0515     /* Prepend skb with frame type */
0516     memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
0517 
0518     /* Packets must be word aligned */
0519     if (skb->len % 2) {
0520         err = skb_pad(skb, 1);
0521         if (err)
0522             return err;
0523         skb_put(skb, 1);
0524     }
0525 
0526     skb_queue_tail(&btdev->txq, skb);
0527 
0528     return 0;
0529 }
0530 
0531 static int nokia_recv_negotiation_packet(struct hci_dev *hdev,
0532                      struct sk_buff *skb)
0533 {
0534     struct hci_uart *hu = hci_get_drvdata(hdev);
0535     struct nokia_bt_dev *btdev = hu->priv;
0536     struct device *dev = &btdev->serdev->dev;
0537     struct hci_nokia_neg_hdr *hdr;
0538     struct hci_nokia_neg_evt *evt;
0539     int ret = 0;
0540 
0541     hdr = (struct hci_nokia_neg_hdr *)skb->data;
0542     if (hdr->dlen != sizeof(*evt)) {
0543         btdev->init_error = -EIO;
0544         ret = -EIO;
0545         goto finish_neg;
0546     }
0547 
0548     evt = skb_pull(skb, sizeof(*hdr));
0549 
0550     if (evt->ack != NOKIA_NEG_ACK) {
0551         dev_err(dev, "Negotiation received: wrong reply");
0552         btdev->init_error = -EINVAL;
0553         ret = -EINVAL;
0554         goto finish_neg;
0555     }
0556 
0557     btdev->man_id = evt->man_id;
0558     btdev->ver_id = evt->ver_id;
0559 
0560     dev_dbg(dev, "Negotiation received: baud=%u:clk=%u:manu=%u:vers=%u",
0561         evt->baud, evt->sys_clk, evt->man_id, evt->ver_id);
0562 
0563 finish_neg:
0564     complete(&btdev->init_completion);
0565     kfree_skb(skb);
0566     return ret;
0567 }
0568 
0569 static int nokia_recv_alive_packet(struct hci_dev *hdev, struct sk_buff *skb)
0570 {
0571     struct hci_uart *hu = hci_get_drvdata(hdev);
0572     struct nokia_bt_dev *btdev = hu->priv;
0573     struct device *dev = &btdev->serdev->dev;
0574     struct hci_nokia_alive_hdr *hdr;
0575     struct hci_nokia_alive_pkt *pkt;
0576     int ret = 0;
0577 
0578     hdr = (struct hci_nokia_alive_hdr *)skb->data;
0579     if (hdr->dlen != sizeof(*pkt)) {
0580         dev_err(dev, "Corrupted alive message");
0581         btdev->init_error = -EIO;
0582         ret = -EIO;
0583         goto finish_alive;
0584     }
0585 
0586     pkt = skb_pull(skb, sizeof(*hdr));
0587 
0588     if (pkt->mid != NOKIA_ALIVE_RESP) {
0589         dev_err(dev, "Alive received: invalid response: 0x%02x!",
0590             pkt->mid);
0591         btdev->init_error = -EINVAL;
0592         ret = -EINVAL;
0593         goto finish_alive;
0594     }
0595 
0596     dev_dbg(dev, "Alive received");
0597 
0598 finish_alive:
0599     complete(&btdev->init_completion);
0600     kfree_skb(skb);
0601     return ret;
0602 }
0603 
0604 static int nokia_recv_radio(struct hci_dev *hdev, struct sk_buff *skb)
0605 {
0606     /* Packets received on the dedicated radio channel are
0607      * HCI events and so feed them back into the core.
0608      */
0609     hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
0610     return hci_recv_frame(hdev, skb);
0611 }
0612 
0613 /* Recv data */
0614 static const struct h4_recv_pkt nokia_recv_pkts[] = {
0615     { H4_RECV_ACL,      .recv = hci_recv_frame },
0616     { H4_RECV_SCO,      .recv = hci_recv_frame },
0617     { H4_RECV_EVENT,    .recv = hci_recv_frame },
0618     { NOKIA_RECV_ALIVE, .recv = nokia_recv_alive_packet },
0619     { NOKIA_RECV_NEG,   .recv = nokia_recv_negotiation_packet },
0620     { NOKIA_RECV_RADIO, .recv = nokia_recv_radio },
0621 };
0622 
0623 static int nokia_recv(struct hci_uart *hu, const void *data, int count)
0624 {
0625     struct nokia_bt_dev *btdev = hu->priv;
0626     struct device *dev = &btdev->serdev->dev;
0627     int err;
0628 
0629     if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
0630         return -EUNATCH;
0631 
0632     btdev->rx_skb = h4_recv_buf(hu->hdev, btdev->rx_skb, data, count,
0633                   nokia_recv_pkts, ARRAY_SIZE(nokia_recv_pkts));
0634     if (IS_ERR(btdev->rx_skb)) {
0635         err = PTR_ERR(btdev->rx_skb);
0636         dev_err(dev, "Frame reassembly failed (%d)", err);
0637         btdev->rx_skb = NULL;
0638         return err;
0639     }
0640 
0641     return count;
0642 }
0643 
0644 static struct sk_buff *nokia_dequeue(struct hci_uart *hu)
0645 {
0646     struct nokia_bt_dev *btdev = hu->priv;
0647     struct device *dev = &btdev->serdev->dev;
0648     struct sk_buff *result = skb_dequeue(&btdev->txq);
0649 
0650     if (!btdev->initialized)
0651         return result;
0652 
0653     if (btdev->tx_enabled == !!result)
0654         return result;
0655 
0656     if (result) {
0657         pm_runtime_get_sync(dev);
0658         gpiod_set_value_cansleep(btdev->wakeup_bt, 1);
0659     } else {
0660         serdev_device_wait_until_sent(btdev->serdev, 0);
0661         gpiod_set_value_cansleep(btdev->wakeup_bt, 0);
0662         pm_runtime_put(dev);
0663     }
0664 
0665     btdev->tx_enabled = !!result;
0666 
0667     return result;
0668 }
0669 
0670 static const struct hci_uart_proto nokia_proto = {
0671     .id     = HCI_UART_NOKIA,
0672     .name       = "Nokia",
0673     .open       = nokia_open,
0674     .close      = nokia_close,
0675     .recv       = nokia_recv,
0676     .enqueue    = nokia_enqueue,
0677     .dequeue    = nokia_dequeue,
0678     .flush      = nokia_flush,
0679     .setup      = nokia_setup,
0680     .manufacturer   = 1,
0681 };
0682 
0683 static int nokia_bluetooth_serdev_probe(struct serdev_device *serdev)
0684 {
0685     struct device *dev = &serdev->dev;
0686     struct nokia_bt_dev *btdev;
0687     struct clk *sysclk;
0688     int err = 0;
0689 
0690     btdev = devm_kzalloc(dev, sizeof(*btdev), GFP_KERNEL);
0691     if (!btdev)
0692         return -ENOMEM;
0693 
0694     btdev->hu.serdev = btdev->serdev = serdev;
0695     serdev_device_set_drvdata(serdev, btdev);
0696 
0697     btdev->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
0698     if (IS_ERR(btdev->reset)) {
0699         err = PTR_ERR(btdev->reset);
0700         dev_err(dev, "could not get reset gpio: %d", err);
0701         return err;
0702     }
0703 
0704     btdev->wakeup_host = devm_gpiod_get(dev, "host-wakeup", GPIOD_IN);
0705     if (IS_ERR(btdev->wakeup_host)) {
0706         err = PTR_ERR(btdev->wakeup_host);
0707         dev_err(dev, "could not get host wakeup gpio: %d", err);
0708         return err;
0709     }
0710 
0711     btdev->wake_irq = gpiod_to_irq(btdev->wakeup_host);
0712 
0713     err = devm_request_threaded_irq(dev, btdev->wake_irq, NULL,
0714         wakeup_handler,
0715         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0716         "wakeup", btdev);
0717     if (err) {
0718         dev_err(dev, "could request wakeup irq: %d", err);
0719         return err;
0720     }
0721 
0722     btdev->wakeup_bt = devm_gpiod_get(dev, "bluetooth-wakeup",
0723                        GPIOD_OUT_LOW);
0724     if (IS_ERR(btdev->wakeup_bt)) {
0725         err = PTR_ERR(btdev->wakeup_bt);
0726         dev_err(dev, "could not get BT wakeup gpio: %d", err);
0727         return err;
0728     }
0729 
0730     sysclk = devm_clk_get(dev, "sysclk");
0731     if (IS_ERR(sysclk)) {
0732         err = PTR_ERR(sysclk);
0733         dev_err(dev, "could not get sysclk: %d", err);
0734         return err;
0735     }
0736 
0737     clk_prepare_enable(sysclk);
0738     btdev->sysclk_speed = clk_get_rate(sysclk);
0739     clk_disable_unprepare(sysclk);
0740 
0741     skb_queue_head_init(&btdev->txq);
0742 
0743     btdev->hu.priv = btdev;
0744     btdev->hu.alignment = 2; /* Nokia H4+ is word aligned */
0745 
0746     err = hci_uart_register_device(&btdev->hu, &nokia_proto);
0747     if (err) {
0748         dev_err(dev, "could not register bluetooth uart: %d", err);
0749         return err;
0750     }
0751 
0752     return 0;
0753 }
0754 
0755 static void nokia_bluetooth_serdev_remove(struct serdev_device *serdev)
0756 {
0757     struct nokia_bt_dev *btdev = serdev_device_get_drvdata(serdev);
0758 
0759     hci_uart_unregister_device(&btdev->hu);
0760 }
0761 
0762 static int nokia_bluetooth_runtime_suspend(struct device *dev)
0763 {
0764     struct serdev_device *serdev = to_serdev_device(dev);
0765 
0766     nokia_flow_control(serdev, false);
0767     return 0;
0768 }
0769 
0770 static int nokia_bluetooth_runtime_resume(struct device *dev)
0771 {
0772     struct serdev_device *serdev = to_serdev_device(dev);
0773 
0774     nokia_flow_control(serdev, true);
0775     return 0;
0776 }
0777 
0778 static const struct dev_pm_ops nokia_bluetooth_pm_ops = {
0779     SET_RUNTIME_PM_OPS(nokia_bluetooth_runtime_suspend,
0780                nokia_bluetooth_runtime_resume,
0781                NULL)
0782 };
0783 
0784 #ifdef CONFIG_OF
0785 static const struct of_device_id nokia_bluetooth_of_match[] = {
0786     { .compatible = "nokia,h4p-bluetooth", },
0787     {},
0788 };
0789 MODULE_DEVICE_TABLE(of, nokia_bluetooth_of_match);
0790 #endif
0791 
0792 static struct serdev_device_driver nokia_bluetooth_serdev_driver = {
0793     .probe = nokia_bluetooth_serdev_probe,
0794     .remove = nokia_bluetooth_serdev_remove,
0795     .driver = {
0796         .name = "nokia-bluetooth",
0797         .pm = &nokia_bluetooth_pm_ops,
0798         .of_match_table = of_match_ptr(nokia_bluetooth_of_match),
0799     },
0800 };
0801 
0802 module_serdev_device_driver(nokia_bluetooth_serdev_driver);
0803 
0804 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
0805 MODULE_DESCRIPTION("Bluetooth HCI UART Nokia H4+ driver ver " VERSION);
0806 MODULE_VERSION(VERSION);
0807 MODULE_LICENSE("GPL");