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 | |
| 53 | static int ath_wakeup_ar3k(struct tty_struct *tty) |
| 54 | { |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 55 | int status = tty->driver->ops->tiocmget(tty); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 56 | |
| 57 | if (status & TIOCM_CTS) |
| 58 | return status; |
| 59 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 60 | /* Clear RTS first */ |
Peter Hurley | 632f32e | 2015-01-25 14:44:54 -0500 | [diff] [blame] | 61 | tty->driver->ops->tiocmget(tty); |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 62 | tty->driver->ops->tiocmset(tty, 0x00, TIOCM_RTS); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 63 | mdelay(20); |
| 64 | |
| 65 | /* Set RTS, wake up board */ |
Peter Hurley | 632f32e | 2015-01-25 14:44:54 -0500 | [diff] [blame] | 66 | tty->driver->ops->tiocmget(tty); |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 67 | tty->driver->ops->tiocmset(tty, TIOCM_RTS, 0x00); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 68 | mdelay(20); |
| 69 | |
Alan Cox | afaae08 | 2011-02-14 16:28:18 +0000 | [diff] [blame] | 70 | status = tty->driver->ops->tiocmget(tty); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 71 | return status; |
| 72 | } |
| 73 | |
| 74 | static void ath_hci_uart_work(struct work_struct *work) |
| 75 | { |
| 76 | int status; |
| 77 | struct ath_struct *ath; |
| 78 | struct hci_uart *hu; |
| 79 | struct tty_struct *tty; |
| 80 | |
| 81 | ath = container_of(work, struct ath_struct, ctxtsw); |
| 82 | |
| 83 | hu = ath->hu; |
| 84 | tty = hu->tty; |
| 85 | |
| 86 | /* verify and wake up controller */ |
| 87 | if (ath->cur_sleep) { |
| 88 | status = ath_wakeup_ar3k(tty); |
| 89 | if (!(status & TIOCM_CTS)) |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | /* Ready to send Data */ |
| 94 | clear_bit(HCI_UART_SENDING, &hu->tx_state); |
| 95 | hci_uart_tx_wakeup(hu); |
| 96 | } |
| 97 | |
| 98 | /* Initialize protocol */ |
| 99 | static int ath_open(struct hci_uart *hu) |
| 100 | { |
| 101 | struct ath_struct *ath; |
| 102 | |
| 103 | BT_DBG("hu %p", hu); |
| 104 | |
David Herrmann | f5fd5ba | 2012-01-07 15:19:40 +0100 | [diff] [blame] | 105 | ath = kzalloc(sizeof(*ath), GFP_KERNEL); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 106 | if (!ath) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | skb_queue_head_init(&ath->txq); |
| 110 | |
| 111 | hu->priv = ath; |
| 112 | ath->hu = hu; |
| 113 | |
| 114 | INIT_WORK(&ath->ctxtsw, ath_hci_uart_work); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* Flush protocol data */ |
| 120 | static int ath_flush(struct hci_uart *hu) |
| 121 | { |
| 122 | struct ath_struct *ath = hu->priv; |
| 123 | |
| 124 | BT_DBG("hu %p", hu); |
| 125 | |
| 126 | skb_queue_purge(&ath->txq); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* Close protocol */ |
| 132 | static int ath_close(struct hci_uart *hu) |
| 133 | { |
| 134 | struct ath_struct *ath = hu->priv; |
| 135 | |
| 136 | BT_DBG("hu %p", hu); |
| 137 | |
| 138 | skb_queue_purge(&ath->txq); |
| 139 | |
Marcel Holtmann | d90aa68 | 2015-04-04 21:59:26 -0700 | [diff] [blame] | 140 | kfree_skb(ath->rx_skb); |
| 141 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 142 | cancel_work_sync(&ath->ctxtsw); |
| 143 | |
| 144 | hu->priv = NULL; |
| 145 | kfree(ath); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | #define HCI_OP_ATH_SLEEP 0xFC04 |
| 151 | |
| 152 | /* Enqueue frame for transmittion */ |
| 153 | static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 154 | { |
| 155 | struct ath_struct *ath = hu->priv; |
| 156 | |
| 157 | if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) { |
Dan Carpenter | 4ebaa4e | 2010-07-23 12:11:04 +0200 | [diff] [blame] | 158 | kfree_skb(skb); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Update power management enable flag with parameters of |
| 164 | * HCI sleep enable vendor specific HCI command. |
| 165 | */ |
| 166 | if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { |
| 167 | struct hci_command_hdr *hdr = (void *)skb->data; |
| 168 | |
| 169 | if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) |
| 170 | ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE]; |
| 171 | } |
| 172 | |
| 173 | BT_DBG("hu %p skb %p", hu, skb); |
| 174 | |
| 175 | /* Prepend skb with frame type */ |
| 176 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
| 177 | |
| 178 | skb_queue_tail(&ath->txq, skb); |
| 179 | set_bit(HCI_UART_SENDING, &hu->tx_state); |
| 180 | |
| 181 | schedule_work(&ath->ctxtsw); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct sk_buff *ath_dequeue(struct hci_uart *hu) |
| 187 | { |
| 188 | struct ath_struct *ath = hu->priv; |
| 189 | |
| 190 | return skb_dequeue(&ath->txq); |
| 191 | } |
| 192 | |
| 193 | /* Recv data */ |
Marcel Holtmann | 9d1c40e | 2015-04-04 20:59:41 -0700 | [diff] [blame] | 194 | static int ath_recv(struct hci_uart *hu, const void *data, int count) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 195 | { |
Marcel Holtmann | d90aa68 | 2015-04-04 21:59:26 -0700 | [diff] [blame] | 196 | struct ath_struct *ath = hu->priv; |
Jiejing Zhang | 78b4a56 | 2011-04-07 20:37:06 +0800 | [diff] [blame] | 197 | |
Marcel Holtmann | d90aa68 | 2015-04-04 21:59:26 -0700 | [diff] [blame] | 198 | ath->rx_skb = h4_recv_buf(hu->hdev, ath->rx_skb, data, count); |
| 199 | if (IS_ERR(ath->rx_skb)) { |
| 200 | int err = PTR_ERR(ath->rx_skb); |
| 201 | BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err); |
| 202 | return err; |
Jiejing Zhang | 78b4a56 | 2011-04-07 20:37:06 +0800 | [diff] [blame] | 203 | } |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 204 | |
| 205 | return count; |
| 206 | } |
| 207 | |
Marcel Holtmann | 4ee7ef1 | 2015-04-04 22:11:43 -0700 | [diff] [blame] | 208 | static const struct hci_uart_proto athp = { |
Marcel Holtmann | 7c40fb8 | 2015-04-04 22:27:34 -0700 | [diff] [blame] | 209 | .id = HCI_UART_ATH3K, |
| 210 | .name = "ATH3K", |
| 211 | .open = ath_open, |
| 212 | .close = ath_close, |
| 213 | .recv = ath_recv, |
| 214 | .enqueue = ath_enqueue, |
| 215 | .dequeue = ath_dequeue, |
| 216 | .flush = ath_flush, |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 217 | }; |
| 218 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 219 | int __init ath_init(void) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 220 | { |
Marcel Holtmann | 01009ee | 2015-04-04 22:27:35 -0700 | [diff] [blame^] | 221 | return hci_uart_register_proto(&athp); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 222 | } |
| 223 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 224 | int __exit ath_deinit(void) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 225 | { |
| 226 | return hci_uart_unregister_proto(&athp); |
| 227 | } |