Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Atheros Communication Bluetooth HCIATH3K UART protocol |
| 3 | * |
| 4 | * HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's |
| 5 | * power management protocol extension to H4 to support AR300x Bluetooth Chip. |
| 6 | * |
| 7 | * Copyright (c) 2009-2010 Atheros Communications Inc. |
| 8 | * |
| 9 | * Acknowledgements: |
| 10 | * This file is based on hci_h4.c, which was written |
| 11 | * by Maxim Krasnyansky and Marcel Holtmann. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/kernel.h> |
| 31 | |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/tty.h> |
| 35 | #include <linux/errno.h> |
| 36 | #include <linux/ioctl.h> |
| 37 | #include <linux/skbuff.h> |
| 38 | |
| 39 | #include <net/bluetooth/bluetooth.h> |
| 40 | #include <net/bluetooth/hci_core.h> |
| 41 | |
| 42 | #include "hci_uart.h" |
| 43 | |
| 44 | struct ath_struct { |
| 45 | struct hci_uart *hu; |
| 46 | unsigned int cur_sleep; |
| 47 | |
Marcel Holtmann | d90aa68 | 2015-04-04 21:59:26 -0700 | [diff] [blame] | 48 | struct sk_buff *rx_skb; |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 49 | struct sk_buff_head txq; |
| 50 | struct work_struct ctxtsw; |
| 51 | }; |
| 52 | |
Loic Poulain | 13df500 | 2017-10-19 14:29:10 +0200 | [diff] [blame] | 53 | #define OP_WRITE_TAG 0x01 |
| 54 | |
| 55 | #define INDEX_BDADDR 0x01 |
| 56 | |
| 57 | struct ath_vendor_cmd { |
| 58 | __u8 opcode; |
| 59 | __le16 index; |
| 60 | __u8 len; |
| 61 | __u8 data[251]; |
| 62 | } __packed; |
| 63 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 64 | static int ath_wakeup_ar3k(struct tty_struct *tty) |
| 65 | { |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 66 | int status = tty->driver->ops->tiocmget(tty); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 67 | |
| 68 | if (status & TIOCM_CTS) |
| 69 | return status; |
| 70 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 71 | /* Clear RTS first */ |
Peter Hurley | 632f32e | 2015-01-25 14:44:54 -0500 | [diff] [blame] | 72 | tty->driver->ops->tiocmget(tty); |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 73 | tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS); |
Jia-Ju Bai | 688d624 | 2018-01-27 18:17:38 +0800 | [diff] [blame] | 74 | msleep(20); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 75 | |
| 76 | /* Set RTS, wake up board */ |
Peter Hurley | 632f32e | 2015-01-25 14:44:54 -0500 | [diff] [blame] | 77 | tty->driver->ops->tiocmget(tty); |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 78 | tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00); |
Jia-Ju Bai | 688d624 | 2018-01-27 18:17:38 +0800 | [diff] [blame] | 79 | msleep(20); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 80 | |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 81 | status = tty->driver->ops->tiocmget(tty); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 82 | return status; |
| 83 | } |
| 84 | |
| 85 | static void ath_hci_uart_work(struct work_struct *work) |
| 86 | { |
| 87 | int status; |
| 88 | struct ath_struct *ath; |
| 89 | struct hci_uart *hu; |
| 90 | struct tty_struct *tty; |
| 91 | |
| 92 | ath = container_of(work, struct ath_struct, ctxtsw); |
| 93 | |
| 94 | hu = ath->hu; |
| 95 | tty = hu->tty; |
| 96 | |
| 97 | /* verify and wake up controller */ |
| 98 | if (ath->cur_sleep) { |
| 99 | status = ath_wakeup_ar3k(tty); |
| 100 | if (!(status & TIOCM_CTS)) |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | /* Ready to send Data */ |
| 105 | clear_bit(HCI_UART_SENDING, &hu->tx_state); |
| 106 | hci_uart_tx_wakeup(hu); |
| 107 | } |
| 108 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 109 | static int ath_open(struct hci_uart *hu) |
| 110 | { |
| 111 | struct ath_struct *ath; |
| 112 | |
| 113 | BT_DBG("hu %p", hu); |
| 114 | |
David Herrmann | f5fd5ba | 2012-01-07 15:19:40 +0100 | [diff] [blame] | 115 | ath = kzalloc(sizeof(*ath), GFP_KERNEL); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 116 | if (!ath) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | skb_queue_head_init(&ath->txq); |
| 120 | |
| 121 | hu->priv = ath; |
| 122 | ath->hu = hu; |
| 123 | |
| 124 | INIT_WORK(&ath->ctxtsw, ath_hci_uart_work); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 129 | static int ath_close(struct hci_uart *hu) |
| 130 | { |
| 131 | struct ath_struct *ath = hu->priv; |
| 132 | |
| 133 | BT_DBG("hu %p", hu); |
| 134 | |
| 135 | skb_queue_purge(&ath->txq); |
| 136 | |
Marcel Holtmann | d90aa68 | 2015-04-04 21:59:26 -0700 | [diff] [blame] | 137 | kfree_skb(ath->rx_skb); |
| 138 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 139 | cancel_work_sync(&ath->ctxtsw); |
| 140 | |
| 141 | hu->priv = NULL; |
| 142 | kfree(ath); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Marcel Holtmann | c0ba7ac | 2015-04-11 05:35:37 -0700 | [diff] [blame] | 147 | static int ath_flush(struct hci_uart *hu) |
| 148 | { |
| 149 | struct ath_struct *ath = hu->priv; |
| 150 | |
| 151 | BT_DBG("hu %p", hu); |
| 152 | |
| 153 | skb_queue_purge(&ath->txq); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Loic Poulain | 13df500 | 2017-10-19 14:29:10 +0200 | [diff] [blame] | 158 | static int ath_vendor_cmd(struct hci_dev *hdev, uint8_t opcode, uint16_t index, |
| 159 | const void *data, size_t dlen) |
Marcel Holtmann | 4c876c0 | 2015-04-11 05:35:38 -0700 | [diff] [blame] | 160 | { |
| 161 | struct sk_buff *skb; |
Loic Poulain | 13df500 | 2017-10-19 14:29:10 +0200 | [diff] [blame] | 162 | struct ath_vendor_cmd cmd; |
Marcel Holtmann | 4c876c0 | 2015-04-11 05:35:38 -0700 | [diff] [blame] | 163 | |
Loic Poulain | 13df500 | 2017-10-19 14:29:10 +0200 | [diff] [blame] | 164 | if (dlen > sizeof(cmd.data)) |
| 165 | return -EINVAL; |
Marcel Holtmann | 4c876c0 | 2015-04-11 05:35:38 -0700 | [diff] [blame] | 166 | |
Loic Poulain | 13df500 | 2017-10-19 14:29:10 +0200 | [diff] [blame] | 167 | cmd.opcode = opcode; |
| 168 | cmd.index = cpu_to_le16(index); |
| 169 | cmd.len = dlen; |
| 170 | memcpy(cmd.data, data, dlen); |
| 171 | |
| 172 | skb = __hci_cmd_sync(hdev, 0xfc0b, dlen + 4, &cmd, HCI_INIT_TIMEOUT); |
| 173 | if (IS_ERR(skb)) |
| 174 | return PTR_ERR(skb); |
Marcel Holtmann | 4c876c0 | 2015-04-11 05:35:38 -0700 | [diff] [blame] | 175 | kfree_skb(skb); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Loic Poulain | 13df500 | 2017-10-19 14:29:10 +0200 | [diff] [blame] | 180 | static int ath_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
| 181 | { |
| 182 | return ath_vendor_cmd(hdev, OP_WRITE_TAG, INDEX_BDADDR, bdaddr, |
| 183 | sizeof(*bdaddr)); |
| 184 | } |
| 185 | |
Marcel Holtmann | 4c876c0 | 2015-04-11 05:35:38 -0700 | [diff] [blame] | 186 | static int ath_setup(struct hci_uart *hu) |
| 187 | { |
| 188 | BT_DBG("hu %p", hu); |
| 189 | |
| 190 | hu->hdev->set_bdaddr = ath_set_bdaddr; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Marcel Holtmann | c0ba7ac | 2015-04-11 05:35:37 -0700 | [diff] [blame] | 195 | static const struct h4_recv_pkt ath_recv_pkts[] = { |
| 196 | { H4_RECV_ACL, .recv = hci_recv_frame }, |
| 197 | { H4_RECV_SCO, .recv = hci_recv_frame }, |
| 198 | { H4_RECV_EVENT, .recv = hci_recv_frame }, |
| 199 | }; |
| 200 | |
| 201 | static int ath_recv(struct hci_uart *hu, const void *data, int count) |
| 202 | { |
| 203 | struct ath_struct *ath = hu->priv; |
| 204 | |
| 205 | ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count, |
| 206 | ath_recv_pkts, ARRAY_SIZE(ath_recv_pkts)); |
| 207 | if (IS_ERR(ath->rx_skb)) { |
| 208 | int err = PTR_ERR(ath->rx_skb); |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 209 | bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); |
Chan-yeol Park | 3713416 | 2015-06-17 21:10:39 +0900 | [diff] [blame] | 210 | ath->rx_skb = NULL; |
Marcel Holtmann | c0ba7ac | 2015-04-11 05:35:37 -0700 | [diff] [blame] | 211 | return err; |
| 212 | } |
| 213 | |
| 214 | return count; |
| 215 | } |
| 216 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 217 | #define HCI_OP_ATH_SLEEP 0xFC04 |
| 218 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 219 | static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 220 | { |
| 221 | struct ath_struct *ath = hu->priv; |
| 222 | |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 223 | if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) { |
Dan Carpenter | 4ebaa4e | 2010-07-23 12:11:04 +0200 | [diff] [blame] | 224 | kfree_skb(skb); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Marcel Holtmann | c0ba7ac | 2015-04-11 05:35:37 -0700 | [diff] [blame] | 228 | /* Update power management enable flag with parameters of |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 229 | * HCI sleep enable vendor specific HCI command. |
| 230 | */ |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 231 | if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) { |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 232 | struct hci_command_hdr *hdr = (void *)skb->data; |
| 233 | |
| 234 | if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) |
| 235 | ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE]; |
| 236 | } |
| 237 | |
| 238 | BT_DBG("hu %p skb %p", hu, skb); |
| 239 | |
| 240 | /* Prepend skb with frame type */ |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 241 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 242 | |
| 243 | skb_queue_tail(&ath->txq, skb); |
| 244 | set_bit(HCI_UART_SENDING, &hu->tx_state); |
| 245 | |
| 246 | schedule_work(&ath->ctxtsw); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static struct sk_buff *ath_dequeue(struct hci_uart *hu) |
| 252 | { |
| 253 | struct ath_struct *ath = hu->priv; |
| 254 | |
| 255 | return skb_dequeue(&ath->txq); |
| 256 | } |
| 257 | |
Marcel Holtmann | 4ee7ef1 | 2015-04-04 22:11:43 -0700 | [diff] [blame] | 258 | static const struct hci_uart_proto athp = { |
Marcel Holtmann | 7c40fb8 | 2015-04-04 22:27:34 -0700 | [diff] [blame] | 259 | .id = HCI_UART_ATH3K, |
| 260 | .name = "ATH3K", |
Marcel Holtmann | aee61f7 | 2015-10-20 21:30:45 +0200 | [diff] [blame] | 261 | .manufacturer = 69, |
Marcel Holtmann | 7c40fb8 | 2015-04-04 22:27:34 -0700 | [diff] [blame] | 262 | .open = ath_open, |
| 263 | .close = ath_close, |
Marcel Holtmann | c0ba7ac | 2015-04-11 05:35:37 -0700 | [diff] [blame] | 264 | .flush = ath_flush, |
Marcel Holtmann | 4c876c0 | 2015-04-11 05:35:38 -0700 | [diff] [blame] | 265 | .setup = ath_setup, |
Marcel Holtmann | 7c40fb8 | 2015-04-04 22:27:34 -0700 | [diff] [blame] | 266 | .recv = ath_recv, |
| 267 | .enqueue = ath_enqueue, |
| 268 | .dequeue = ath_dequeue, |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 269 | }; |
| 270 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 271 | int __init ath_init(void) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 272 | { |
Marcel Holtmann | 01009ee | 2015-04-04 22:27:35 -0700 | [diff] [blame] | 273 | return hci_uart_register_proto(&athp); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 274 | } |
| 275 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 276 | int __exit ath_deinit(void) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 277 | { |
| 278 | return hci_uart_unregister_proto(&athp); |
| 279 | } |