Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Atheros Communication Bluetooth HCIATH3K UART protocol
0004  *
0005  *  HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
0006  *  power management protocol extension to H4 to support AR300x Bluetooth Chip.
0007  *
0008  *  Copyright (c) 2009-2010 Atheros Communications Inc.
0009  *
0010  *  Acknowledgements:
0011  *  This file is based on hci_h4.c, which was written
0012  *  by Maxim Krasnyansky and Marcel Holtmann.
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/kernel.h>
0017 
0018 #include <linux/init.h>
0019 #include <linux/slab.h>
0020 #include <linux/tty.h>
0021 #include <linux/errno.h>
0022 #include <linux/ioctl.h>
0023 #include <linux/skbuff.h>
0024 
0025 #include <net/bluetooth/bluetooth.h>
0026 #include <net/bluetooth/hci_core.h>
0027 
0028 #include "hci_uart.h"
0029 
0030 struct ath_struct {
0031     struct hci_uart *hu;
0032     unsigned int cur_sleep;
0033 
0034     struct sk_buff *rx_skb;
0035     struct sk_buff_head txq;
0036     struct work_struct ctxtsw;
0037 };
0038 
0039 #define OP_WRITE_TAG    0x01
0040 
0041 #define INDEX_BDADDR    0x01
0042 
0043 struct ath_vendor_cmd {
0044     __u8 opcode;
0045     __le16 index;
0046     __u8 len;
0047     __u8 data[251];
0048 } __packed;
0049 
0050 static int ath_wakeup_ar3k(struct tty_struct *tty)
0051 {
0052     int status = tty->driver->ops->tiocmget(tty);
0053 
0054     if (status & TIOCM_CTS)
0055         return status;
0056 
0057     /* Clear RTS first */
0058     tty->driver->ops->tiocmget(tty);
0059     tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS);
0060     msleep(20);
0061 
0062     /* Set RTS, wake up board */
0063     tty->driver->ops->tiocmget(tty);
0064     tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00);
0065     msleep(20);
0066 
0067     status = tty->driver->ops->tiocmget(tty);
0068     return status;
0069 }
0070 
0071 static void ath_hci_uart_work(struct work_struct *work)
0072 {
0073     int status;
0074     struct ath_struct *ath;
0075     struct hci_uart *hu;
0076     struct tty_struct *tty;
0077 
0078     ath = container_of(work, struct ath_struct, ctxtsw);
0079 
0080     hu = ath->hu;
0081     tty = hu->tty;
0082 
0083     /* verify and wake up controller */
0084     if (ath->cur_sleep) {
0085         status = ath_wakeup_ar3k(tty);
0086         if (!(status & TIOCM_CTS))
0087             return;
0088     }
0089 
0090     /* Ready to send Data */
0091     clear_bit(HCI_UART_SENDING, &hu->tx_state);
0092     hci_uart_tx_wakeup(hu);
0093 }
0094 
0095 static int ath_open(struct hci_uart *hu)
0096 {
0097     struct ath_struct *ath;
0098 
0099     BT_DBG("hu %p", hu);
0100 
0101     if (!hci_uart_has_flow_control(hu))
0102         return -EOPNOTSUPP;
0103 
0104     ath = kzalloc(sizeof(*ath), GFP_KERNEL);
0105     if (!ath)
0106         return -ENOMEM;
0107 
0108     skb_queue_head_init(&ath->txq);
0109 
0110     hu->priv = ath;
0111     ath->hu = hu;
0112 
0113     INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
0114 
0115     return 0;
0116 }
0117 
0118 static int ath_close(struct hci_uart *hu)
0119 {
0120     struct ath_struct *ath = hu->priv;
0121 
0122     BT_DBG("hu %p", hu);
0123 
0124     skb_queue_purge(&ath->txq);
0125 
0126     kfree_skb(ath->rx_skb);
0127 
0128     cancel_work_sync(&ath->ctxtsw);
0129 
0130     hu->priv = NULL;
0131     kfree(ath);
0132 
0133     return 0;
0134 }
0135 
0136 static int ath_flush(struct hci_uart *hu)
0137 {
0138     struct ath_struct *ath = hu->priv;
0139 
0140     BT_DBG("hu %p", hu);
0141 
0142     skb_queue_purge(&ath->txq);
0143 
0144     return 0;
0145 }
0146 
0147 static int ath_vendor_cmd(struct hci_dev *hdev, uint8_t opcode, uint16_t index,
0148               const void *data, size_t dlen)
0149 {
0150     struct sk_buff *skb;
0151     struct ath_vendor_cmd cmd;
0152 
0153     if (dlen > sizeof(cmd.data))
0154         return -EINVAL;
0155 
0156     cmd.opcode = opcode;
0157     cmd.index = cpu_to_le16(index);
0158     cmd.len = dlen;
0159     memcpy(cmd.data, data, dlen);
0160 
0161     skb = __hci_cmd_sync(hdev, 0xfc0b, dlen + 4, &cmd, HCI_INIT_TIMEOUT);
0162     if (IS_ERR(skb))
0163         return PTR_ERR(skb);
0164     kfree_skb(skb);
0165 
0166     return 0;
0167 }
0168 
0169 static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
0170 {
0171     return ath_vendor_cmd(hdev, OP_WRITE_TAG, INDEX_BDADDR, bdaddr,
0172                   sizeof(*bdaddr));
0173 }
0174 
0175 static int ath_setup(struct hci_uart *hu)
0176 {
0177     BT_DBG("hu %p", hu);
0178 
0179     hu->hdev->set_bdaddr = ath_set_bdaddr;
0180 
0181     return 0;
0182 }
0183 
0184 static const struct h4_recv_pkt ath_recv_pkts[] = {
0185     { H4_RECV_ACL,   .recv = hci_recv_frame },
0186     { H4_RECV_SCO,   .recv = hci_recv_frame },
0187     { H4_RECV_EVENT, .recv = hci_recv_frame },
0188 };
0189 
0190 static int ath_recv(struct hci_uart *hu, const void *data, int count)
0191 {
0192     struct ath_struct *ath = hu->priv;
0193 
0194     ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count,
0195                   ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts));
0196     if (IS_ERR(ath->rx_skb)) {
0197         int err = PTR_ERR(ath->rx_skb);
0198         bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
0199         ath->rx_skb = NULL;
0200         return err;
0201     }
0202 
0203     return count;
0204 }
0205 
0206 #define HCI_OP_ATH_SLEEP 0xFC04
0207 
0208 static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0209 {
0210     struct ath_struct *ath = hu->priv;
0211 
0212     if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) {
0213         kfree_skb(skb);
0214         return 0;
0215     }
0216 
0217     /* Update power management enable flag with parameters of
0218      * HCI sleep enable vendor specific HCI command.
0219      */
0220     if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) {
0221         struct hci_command_hdr *hdr = (void *)skb->data;
0222 
0223         if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP)
0224             ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
0225     }
0226 
0227     BT_DBG("hu %p skb %p", hu, skb);
0228 
0229     /* Prepend skb with frame type */
0230     memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
0231 
0232     skb_queue_tail(&ath->txq, skb);
0233     set_bit(HCI_UART_SENDING, &hu->tx_state);
0234 
0235     schedule_work(&ath->ctxtsw);
0236 
0237     return 0;
0238 }
0239 
0240 static struct sk_buff *ath_dequeue(struct hci_uart *hu)
0241 {
0242     struct ath_struct *ath = hu->priv;
0243 
0244     return skb_dequeue(&ath->txq);
0245 }
0246 
0247 static const struct hci_uart_proto athp = {
0248     .id     = HCI_UART_ATH3K,
0249     .name       = "ATH3K",
0250     .manufacturer   = 69,
0251     .open       = ath_open,
0252     .close      = ath_close,
0253     .flush      = ath_flush,
0254     .setup      = ath_setup,
0255     .recv       = ath_recv,
0256     .enqueue    = ath_enqueue,
0257     .dequeue    = ath_dequeue,
0258 };
0259 
0260 int __init ath_init(void)
0261 {
0262     return hci_uart_register_proto(&athp);
0263 }
0264 
0265 int __exit ath_deinit(void)
0266 {
0267     return hci_uart_unregister_proto(&athp);
0268 }