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. |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 8 | * Copyright (c) 2012-2013 The Linux Foundation. All rights reserved. |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 9 | * |
| 10 | * Acknowledgements: |
| 11 | * This file is based on hci_h4.c, which was written |
| 12 | * by Maxim Krasnyansky and Marcel Holtmann. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/kernel.h> |
| 32 | |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/tty.h> |
| 36 | #include <linux/errno.h> |
| 37 | #include <linux/ioctl.h> |
| 38 | #include <linux/skbuff.h> |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 39 | #include <linux/platform_device.h> |
| 40 | #include <linux/gpio.h> |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 41 | #include <linux/of_gpio.h> |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 42 | #include <net/bluetooth/bluetooth.h> |
| 43 | #include <net/bluetooth/hci_core.h> |
| 44 | |
| 45 | #include "hci_uart.h" |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 46 | #ifdef CONFIG_SERIAL_MSM_HS |
| 47 | #include <mach/msm_serial_hs.h> |
| 48 | #endif |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 49 | |
Ram Mohan Korukonda | eb530d19 | 2012-08-08 16:54:51 +0530 | [diff] [blame] | 50 | unsigned int enableuartsleep = 1; |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 51 | module_param(enableuartsleep, uint, 0644); |
| 52 | /* |
| 53 | * Global variables |
| 54 | */ |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 55 | |
| 56 | /** Device table */ |
| 57 | static struct of_device_id bluesleep_match_table[] = { |
| 58 | { .compatible = "qca,ar3002_bluesleep" }, |
| 59 | {} |
| 60 | }; |
| 61 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 62 | /** Global state flags */ |
| 63 | static unsigned long flags; |
| 64 | |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 65 | /** Workqueue to respond to change in hostwake line */ |
| 66 | static void wakeup_host_work(struct work_struct *work); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 67 | |
| 68 | /** Transmission timer */ |
| 69 | static void bluesleep_tx_timer_expire(unsigned long data); |
| 70 | static DEFINE_TIMER(tx_timer, bluesleep_tx_timer_expire, 0, 0); |
| 71 | |
| 72 | /** Lock for state transitions */ |
| 73 | static spinlock_t rw_lock; |
| 74 | |
| 75 | #define POLARITY_LOW 0 |
| 76 | #define POLARITY_HIGH 1 |
| 77 | |
| 78 | struct bluesleep_info { |
| 79 | unsigned host_wake; /* wake up host */ |
| 80 | unsigned ext_wake; /* wake up device */ |
| 81 | unsigned host_wake_irq; |
| 82 | int irq_polarity; |
| 83 | }; |
| 84 | |
| 85 | /* 1 second timeout */ |
| 86 | #define TX_TIMER_INTERVAL 1 |
| 87 | |
| 88 | /* state variable names and bit positions */ |
| 89 | #define BT_TXEXPIRED 0x01 |
| 90 | #define BT_SLEEPENABLE 0x02 |
| 91 | #define BT_SLEEPCMD 0x03 |
| 92 | |
| 93 | /* global pointer to a single hci device. */ |
| 94 | static struct bluesleep_info *bsi; |
| 95 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 96 | struct ath_struct { |
| 97 | struct hci_uart *hu; |
| 98 | unsigned int cur_sleep; |
| 99 | |
| 100 | struct sk_buff_head txq; |
| 101 | struct work_struct ctxtsw; |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 102 | struct work_struct ws_sleep; |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 103 | }; |
| 104 | |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 105 | static void hsuart_serial_clock_on(struct tty_struct *tty) |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 106 | { |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 107 | struct uart_state *state = tty->driver_data; |
| 108 | struct uart_port *port = state->uart_port; |
| 109 | BT_DBG(""); |
| 110 | msm_hs_request_clock_on(port); |
| 111 | } |
| 112 | |
| 113 | static void hsuart_serial_clock_off(struct tty_struct *tty) |
| 114 | { |
| 115 | struct uart_state *state = tty->driver_data; |
| 116 | struct uart_port *port = state->uart_port; |
| 117 | BT_DBG(""); |
| 118 | msm_hs_request_clock_off(port); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void modify_timer_task(void) |
| 122 | { |
| 123 | spin_lock(&rw_lock); |
| 124 | mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ)); |
| 125 | clear_bit(BT_TXEXPIRED, &flags); |
| 126 | spin_unlock(&rw_lock); |
| 127 | |
| 128 | } |
| 129 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 130 | static int ath_wakeup_ar3k(struct tty_struct *tty) |
| 131 | { |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 132 | int status = 0; |
| 133 | if (test_bit(BT_TXEXPIRED, &flags)) { |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 134 | hsuart_serial_clock_on(tty); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 135 | BT_INFO("wakeup device\n"); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 136 | gpio_set_value(bsi->ext_wake, 0); |
Ram Mohan Korukonda | eb530d19 | 2012-08-08 16:54:51 +0530 | [diff] [blame] | 137 | msleep(20); |
| 138 | gpio_set_value(bsi->ext_wake, 1); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 139 | } |
| 140 | modify_timer_task(); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 141 | return status; |
| 142 | } |
| 143 | |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 144 | static void wakeup_host_work(struct work_struct *work) |
| 145 | { |
| 146 | struct ath_struct *ath = |
| 147 | container_of(work, struct ath_struct, ws_sleep); |
| 148 | |
| 149 | BT_INFO("wake up host"); |
| 150 | if (test_bit(BT_SLEEPENABLE, &flags)) { |
| 151 | if (test_bit(BT_TXEXPIRED, &flags)) |
| 152 | hsuart_serial_clock_on(ath->hu->tty); |
| 153 | } |
| 154 | modify_timer_task(); |
| 155 | } |
| 156 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 157 | static void ath_hci_uart_work(struct work_struct *work) |
| 158 | { |
| 159 | int status; |
| 160 | struct ath_struct *ath; |
| 161 | struct hci_uart *hu; |
| 162 | struct tty_struct *tty; |
| 163 | |
| 164 | ath = container_of(work, struct ath_struct, ctxtsw); |
| 165 | |
| 166 | hu = ath->hu; |
| 167 | tty = hu->tty; |
| 168 | |
| 169 | /* verify and wake up controller */ |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 170 | if (test_bit(BT_SLEEPENABLE, &flags)) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 171 | status = ath_wakeup_ar3k(tty); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 172 | /* Ready to send Data */ |
| 173 | clear_bit(HCI_UART_SENDING, &hu->tx_state); |
| 174 | hci_uart_tx_wakeup(hu); |
| 175 | } |
| 176 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 177 | static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id) |
| 178 | { |
| 179 | /* schedule a tasklet to handle the change in the host wake line */ |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 180 | struct ath_struct *ath = (struct ath_struct *)dev_id; |
| 181 | |
| 182 | schedule_work(&ath->ws_sleep); |
| 183 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 184 | return IRQ_HANDLED; |
| 185 | } |
| 186 | |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 187 | static int ath_bluesleep_gpio_config(struct ath_struct *ath, int on) |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 188 | { |
| 189 | int ret = 0; |
| 190 | |
| 191 | BT_INFO("%s config: %d", __func__, on); |
| 192 | if (!on) { |
| 193 | if (disable_irq_wake(bsi->host_wake_irq)) |
| 194 | BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n"); |
| 195 | goto free_host_wake_irq; |
| 196 | } |
| 197 | |
| 198 | ret = gpio_request(bsi->host_wake, "bt_host_wake"); |
| 199 | if (ret < 0) { |
| 200 | BT_ERR("failed to request gpio pin %d, error %d\n", |
| 201 | bsi->host_wake, ret); |
| 202 | goto gpio_config_failed; |
| 203 | } |
| 204 | |
| 205 | /* configure host_wake as input */ |
| 206 | ret = gpio_direction_input(bsi->host_wake); |
| 207 | if (ret < 0) { |
| 208 | BT_ERR("failed to config GPIO %d as input pin, err %d\n", |
| 209 | bsi->host_wake, ret); |
| 210 | goto gpio_host_wake; |
| 211 | } |
| 212 | |
| 213 | ret = gpio_request(bsi->ext_wake, "bt_ext_wake"); |
| 214 | if (ret < 0) { |
| 215 | BT_ERR("failed to request gpio pin %d, error %d\n", |
| 216 | bsi->ext_wake, ret); |
| 217 | goto gpio_host_wake; |
| 218 | } |
| 219 | |
| 220 | ret = gpio_direction_output(bsi->ext_wake, 1); |
| 221 | if (ret < 0) { |
| 222 | BT_ERR("failed to config GPIO %d as output pin, err %d\n", |
| 223 | bsi->ext_wake, ret); |
| 224 | goto gpio_ext_wake; |
| 225 | } |
| 226 | |
| 227 | gpio_set_value(bsi->ext_wake, 1); |
| 228 | |
| 229 | /* Initialize spinlock. */ |
| 230 | spin_lock_init(&rw_lock); |
| 231 | |
| 232 | /* Initialize timer */ |
| 233 | init_timer(&tx_timer); |
| 234 | tx_timer.function = bluesleep_tx_timer_expire; |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 235 | tx_timer.data = (u_long)ath->hu; |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 236 | |
| 237 | if (bsi->irq_polarity == POLARITY_LOW) { |
| 238 | ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr, |
| 239 | IRQF_DISABLED | IRQF_TRIGGER_FALLING, |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 240 | "bluetooth hostwake", (void *)ath); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 241 | } else { |
| 242 | ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr, |
| 243 | IRQF_DISABLED | IRQF_TRIGGER_RISING, |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 244 | "bluetooth hostwake", (void *)ath); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 245 | } |
| 246 | if (ret < 0) { |
| 247 | BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ"); |
| 248 | goto delete_timer; |
| 249 | } |
| 250 | |
| 251 | ret = enable_irq_wake(bsi->host_wake_irq); |
| 252 | if (ret < 0) { |
| 253 | BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt"); |
| 254 | goto free_host_wake_irq; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | |
| 259 | free_host_wake_irq: |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 260 | free_irq(bsi->host_wake_irq, (void *)ath); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 261 | delete_timer: |
| 262 | del_timer(&tx_timer); |
| 263 | gpio_ext_wake: |
| 264 | gpio_free(bsi->ext_wake); |
| 265 | gpio_host_wake: |
| 266 | gpio_free(bsi->host_wake); |
| 267 | gpio_config_failed: |
| 268 | return ret; |
| 269 | } |
| 270 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 271 | /* Initialize protocol */ |
| 272 | static int ath_open(struct hci_uart *hu) |
| 273 | { |
| 274 | struct ath_struct *ath; |
| 275 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 276 | BT_DBG("hu %p, bsi %p", hu, bsi); |
| 277 | |
| 278 | if (!bsi) |
| 279 | return -EIO; |
| 280 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 281 | ath = kzalloc(sizeof(*ath), GFP_ATOMIC); |
| 282 | if (!ath) |
| 283 | return -ENOMEM; |
| 284 | |
| 285 | skb_queue_head_init(&ath->txq); |
| 286 | |
| 287 | hu->priv = ath; |
| 288 | ath->hu = hu; |
| 289 | |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 290 | if (ath_bluesleep_gpio_config(ath, 1) < 0) { |
| 291 | BT_ERR("HCIATH3K GPIO Config failed"); |
| 292 | hu->priv = NULL; |
| 293 | kfree(ath); |
| 294 | return -EIO; |
| 295 | } |
| 296 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 297 | ath->cur_sleep = enableuartsleep; |
| 298 | if (ath->cur_sleep == 1) { |
| 299 | set_bit(BT_SLEEPENABLE, &flags); |
| 300 | modify_timer_task(); |
| 301 | } |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 302 | INIT_WORK(&ath->ctxtsw, ath_hci_uart_work); |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 303 | INIT_WORK(&ath->ws_sleep, wakeup_host_work); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /* Flush protocol data */ |
| 308 | static int ath_flush(struct hci_uart *hu) |
| 309 | { |
| 310 | struct ath_struct *ath = hu->priv; |
| 311 | |
| 312 | BT_DBG("hu %p", hu); |
| 313 | |
| 314 | skb_queue_purge(&ath->txq); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /* Close protocol */ |
| 320 | static int ath_close(struct hci_uart *hu) |
| 321 | { |
| 322 | struct ath_struct *ath = hu->priv; |
| 323 | |
| 324 | BT_DBG("hu %p", hu); |
| 325 | |
| 326 | skb_queue_purge(&ath->txq); |
| 327 | |
| 328 | cancel_work_sync(&ath->ctxtsw); |
| 329 | |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 330 | cancel_work_sync(&ath->ws_sleep); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 331 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 332 | if (bsi) |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 333 | ath_bluesleep_gpio_config(ath, 0); |
| 334 | |
| 335 | hu->priv = NULL; |
| 336 | kfree(ath); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 337 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | #define HCI_OP_ATH_SLEEP 0xFC04 |
| 342 | |
| 343 | /* Enqueue frame for transmittion */ |
| 344 | static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb) |
| 345 | { |
| 346 | struct ath_struct *ath = hu->priv; |
| 347 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 348 | BT_DBG(""); |
| 349 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 350 | if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) { |
Dan Carpenter | 4ebaa4e | 2010-07-23 12:11:04 +0200 | [diff] [blame] | 351 | kfree_skb(skb); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Update power management enable flag with parameters of |
| 357 | * HCI sleep enable vendor specific HCI command. |
| 358 | */ |
| 359 | if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { |
| 360 | struct hci_command_hdr *hdr = (void *)skb->data; |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 361 | if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) { |
| 362 | set_bit(BT_SLEEPCMD, &flags); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 363 | ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE]; |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 364 | } |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | BT_DBG("hu %p skb %p", hu, skb); |
| 368 | |
| 369 | /* Prepend skb with frame type */ |
| 370 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
| 371 | |
| 372 | skb_queue_tail(&ath->txq, skb); |
| 373 | set_bit(HCI_UART_SENDING, &hu->tx_state); |
| 374 | |
| 375 | schedule_work(&ath->ctxtsw); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static struct sk_buff *ath_dequeue(struct hci_uart *hu) |
| 381 | { |
| 382 | struct ath_struct *ath = hu->priv; |
| 383 | |
| 384 | return skb_dequeue(&ath->txq); |
| 385 | } |
| 386 | |
| 387 | /* Recv data */ |
| 388 | static int ath_recv(struct hci_uart *hu, void *data, int count) |
| 389 | { |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 390 | struct ath_struct *ath = hu->priv; |
| 391 | unsigned int type; |
Jiejing Zhang | 78b4a56 | 2011-04-07 20:37:06 +0800 | [diff] [blame] | 392 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 393 | BT_DBG(""); |
| 394 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 395 | if (hci_recv_stream_fragment(hu->hdev, data, count) < 0) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 396 | BT_ERR("Frame Reassembly Failed"); |
| 397 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 398 | if (count & test_bit(BT_SLEEPCMD, &flags)) { |
| 399 | struct sk_buff *skb = hu->hdev->reassembly[0]; |
| 400 | |
| 401 | if (!skb) { |
| 402 | struct { char type; } *pkt; |
| 403 | |
| 404 | /* Start of the frame */ |
| 405 | pkt = data; |
| 406 | type = pkt->type; |
| 407 | } else |
| 408 | type = bt_cb(skb)->pkt_type; |
| 409 | |
| 410 | if (type == HCI_EVENT_PKT) { |
| 411 | clear_bit(BT_SLEEPCMD, &flags); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 412 | BT_INFO("cur_sleep:%d\n", ath->cur_sleep); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 413 | if (ath->cur_sleep == 1) |
| 414 | set_bit(BT_SLEEPENABLE, &flags); |
| 415 | else |
| 416 | clear_bit(BT_SLEEPENABLE, &flags); |
| 417 | } |
| 418 | if (test_bit(BT_SLEEPENABLE, &flags)) |
| 419 | modify_timer_task(); |
| 420 | } |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 421 | return count; |
| 422 | } |
| 423 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 424 | static void bluesleep_tx_timer_expire(unsigned long data) |
| 425 | { |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 426 | struct hci_uart *hu = (struct hci_uart *) data; |
| 427 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 428 | if (!test_bit(BT_SLEEPENABLE, &flags)) |
| 429 | return; |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 430 | BT_INFO("Tx timer expired\n"); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 431 | |
| 432 | set_bit(BT_TXEXPIRED, &flags); |
Ram Mohan Korukonda | eff0e07 | 2012-11-19 16:05:52 +0530 | [diff] [blame] | 433 | hsuart_serial_clock_off(hu->tty); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 434 | } |
| 435 | |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 436 | static struct hci_uart_proto athp = { |
| 437 | .id = HCI_UART_ATH3K, |
| 438 | .open = ath_open, |
| 439 | .close = ath_close, |
| 440 | .recv = ath_recv, |
| 441 | .enqueue = ath_enqueue, |
| 442 | .dequeue = ath_dequeue, |
| 443 | .flush = ath_flush, |
| 444 | }; |
| 445 | |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 446 | |
| 447 | static int bluesleep_populate_dt_pinfo(struct platform_device *pdev) |
| 448 | { |
| 449 | BT_DBG(""); |
| 450 | |
| 451 | if (!bsi) |
| 452 | return -ENOMEM; |
| 453 | |
| 454 | bsi->host_wake = of_get_named_gpio(pdev->dev.of_node, |
| 455 | "host-wake-gpio", 0); |
| 456 | if (bsi->host_wake < 0) { |
| 457 | BT_ERR("couldn't find host_wake gpio\n"); |
| 458 | return -ENODEV; |
| 459 | } |
| 460 | |
| 461 | bsi->ext_wake = of_get_named_gpio(pdev->dev.of_node, |
| 462 | "ext-wake-gpio", 0); |
| 463 | if (bsi->ext_wake < 0) { |
| 464 | BT_ERR("couldn't find ext_wake gpio\n"); |
| 465 | return -ENODEV; |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int bluesleep_populate_pinfo(struct platform_device *pdev) |
| 472 | { |
| 473 | struct resource *res; |
| 474 | |
| 475 | BT_DBG(""); |
| 476 | |
| 477 | res = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 478 | "gpio_host_wake"); |
| 479 | if (!res) { |
| 480 | BT_ERR("couldn't find host_wake gpio\n"); |
| 481 | return -ENODEV; |
| 482 | } |
| 483 | bsi->host_wake = res->start; |
| 484 | |
| 485 | res = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 486 | "gpio_ext_wake"); |
| 487 | if (!res) { |
| 488 | BT_ERR("couldn't find ext_wake gpio\n"); |
| 489 | return -ENODEV; |
| 490 | } |
| 491 | bsi->ext_wake = res->start; |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int __devinit bluesleep_probe(struct platform_device *pdev) |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 497 | { |
| 498 | int ret; |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 499 | |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 500 | BT_DBG(""); |
| 501 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 502 | bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL); |
| 503 | if (!bsi) { |
| 504 | ret = -ENOMEM; |
| 505 | goto failed; |
| 506 | } |
| 507 | |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 508 | if (pdev->dev.of_node) { |
| 509 | ret = bluesleep_populate_dt_pinfo(pdev); |
| 510 | if (ret < 0) { |
| 511 | BT_ERR("Failed to populate device tree info"); |
| 512 | goto free_bsi; |
| 513 | } |
| 514 | } else { |
| 515 | ret = bluesleep_populate_pinfo(pdev); |
| 516 | if (ret < 0) { |
| 517 | BT_ERR("Failed to populate device info"); |
| 518 | goto free_bsi; |
| 519 | } |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 520 | } |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 521 | |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 522 | BT_DBG("host_wake_gpio: %d ext_wake_gpio: %d", |
| 523 | bsi->host_wake, bsi->ext_wake); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 524 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 525 | bsi->host_wake_irq = platform_get_irq_byname(pdev, "host_wake"); |
| 526 | if (bsi->host_wake_irq < 0) { |
| 527 | BT_ERR("couldn't find host_wake irq\n"); |
| 528 | ret = -ENODEV; |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 529 | goto free_bsi; |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | bsi->irq_polarity = POLARITY_LOW; /* low edge (falling edge) */ |
| 533 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 534 | return 0; |
| 535 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 536 | free_bsi: |
| 537 | kfree(bsi); |
Ram Mohan Korukonda | e359d86 | 2012-08-18 21:12:17 +0530 | [diff] [blame] | 538 | bsi = NULL; |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 539 | failed: |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | static int bluesleep_remove(struct platform_device *pdev) |
| 544 | { |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 545 | kfree(bsi); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static struct platform_driver bluesleep_driver = { |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 550 | .probe = bluesleep_probe, |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 551 | .remove = bluesleep_remove, |
| 552 | .driver = { |
| 553 | .name = "bluesleep", |
| 554 | .owner = THIS_MODULE, |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 555 | .of_match_table = bluesleep_match_table, |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 556 | }, |
| 557 | }; |
| 558 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 559 | int __init ath_init(void) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 560 | { |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 561 | int ret; |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 562 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 563 | ret = hci_uart_register_proto(&athp); |
| 564 | |
| 565 | if (!ret) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 566 | BT_INFO("HCIATH3K protocol initialized"); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 567 | else { |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 568 | BT_ERR("HCIATH3K protocol registration failed"); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 569 | return ret; |
| 570 | } |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 571 | |
| 572 | ret = platform_driver_register(&bluesleep_driver); |
| 573 | if (ret) { |
| 574 | BT_ERR("Failed to register bluesleep driver"); |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 575 | return ret; |
Ram Mohan Korukonda | 0eadc59 | 2013-03-19 22:29:45 +0530 | [diff] [blame] | 576 | } |
| 577 | |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 578 | return 0; |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 579 | } |
| 580 | |
Gustavo F. Padovan | f2b94bb | 2010-07-24 02:04:44 -0300 | [diff] [blame] | 581 | int __exit ath_deinit(void) |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 582 | { |
Santosh Sajjan | 55efc7f | 2012-06-27 19:10:49 +0530 | [diff] [blame] | 583 | platform_driver_unregister(&bluesleep_driver); |
Suraj Sumangala | b3190df | 2010-07-19 12:34:07 +0530 | [diff] [blame] | 584 | return hci_uart_unregister_proto(&athp); |
| 585 | } |