blob: 50b33621783135725e80530de275cce27a966f4c [file] [log] [blame]
Suraj Sumangalab3190df2010-07-19 12:34:07 +05301/*
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.
Santosh Sajjan55efc7f2012-06-27 19:10:49 +05308 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
Suraj Sumangalab3190df2010-07-19 12:34:07 +05309 *
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 Sajjan55efc7f2012-06-27 19:10:49 +053039#include <linux/platform_device.h>
40#include <linux/gpio.h>
Suraj Sumangalab3190df2010-07-19 12:34:07 +053041
42#include <net/bluetooth/bluetooth.h>
43#include <net/bluetooth/hci_core.h>
44
45#include "hci_uart.h"
46
Ram Mohan Korukondaeb530d192012-08-08 16:54:51 +053047unsigned int enableuartsleep = 1;
Santosh Sajjan55efc7f2012-06-27 19:10:49 +053048module_param(enableuartsleep, uint, 0644);
49/*
50 * Global variables
51 */
52/** Global state flags */
53static unsigned long flags;
54
55/** Tasklet to respond to change in hostwake line */
56static struct tasklet_struct hostwake_task;
57
58/** Transmission timer */
59static void bluesleep_tx_timer_expire(unsigned long data);
60static DEFINE_TIMER(tx_timer, bluesleep_tx_timer_expire, 0, 0);
61
62/** Lock for state transitions */
63static spinlock_t rw_lock;
64
65#define POLARITY_LOW 0
66#define POLARITY_HIGH 1
67
68struct bluesleep_info {
69 unsigned host_wake; /* wake up host */
70 unsigned ext_wake; /* wake up device */
71 unsigned host_wake_irq;
72 int irq_polarity;
73};
74
75/* 1 second timeout */
76#define TX_TIMER_INTERVAL 1
77
78/* state variable names and bit positions */
79#define BT_TXEXPIRED 0x01
80#define BT_SLEEPENABLE 0x02
81#define BT_SLEEPCMD 0x03
82
83/* global pointer to a single hci device. */
84static struct bluesleep_info *bsi;
85
Suraj Sumangalab3190df2010-07-19 12:34:07 +053086struct ath_struct {
87 struct hci_uart *hu;
88 unsigned int cur_sleep;
89
90 struct sk_buff_head txq;
91 struct work_struct ctxtsw;
92};
93
Santosh Sajjan55efc7f2012-06-27 19:10:49 +053094static void hostwake_interrupt(unsigned long data)
95{
Ram Mohan Korukondae359d862012-08-18 21:12:17 +053096 BT_INFO(" wakeup host\n");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +053097}
98
99static void modify_timer_task(void)
100{
101 spin_lock(&rw_lock);
102 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
103 clear_bit(BT_TXEXPIRED, &flags);
104 spin_unlock(&rw_lock);
105
106}
107
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530108static int ath_wakeup_ar3k(struct tty_struct *tty)
109{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530110 int status = 0;
111 if (test_bit(BT_TXEXPIRED, &flags)) {
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530112 BT_INFO("wakeup device\n");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530113 gpio_set_value(bsi->ext_wake, 0);
Ram Mohan Korukondaeb530d192012-08-08 16:54:51 +0530114 msleep(20);
115 gpio_set_value(bsi->ext_wake, 1);
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530116 }
117 modify_timer_task();
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530118 return status;
119}
120
121static void ath_hci_uart_work(struct work_struct *work)
122{
123 int status;
124 struct ath_struct *ath;
125 struct hci_uart *hu;
126 struct tty_struct *tty;
127
128 ath = container_of(work, struct ath_struct, ctxtsw);
129
130 hu = ath->hu;
131 tty = hu->tty;
132
133 /* verify and wake up controller */
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530134 if (test_bit(BT_SLEEPENABLE, &flags))
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530135 status = ath_wakeup_ar3k(tty);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530136 /* Ready to send Data */
137 clear_bit(HCI_UART_SENDING, &hu->tx_state);
138 hci_uart_tx_wakeup(hu);
139}
140
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530141static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
142{
143 /* schedule a tasklet to handle the change in the host wake line */
144 tasklet_schedule(&hostwake_task);
145 return IRQ_HANDLED;
146}
147
148static int ath_bluesleep_gpio_config(int on)
149{
150 int ret = 0;
151
152 BT_INFO("%s config: %d", __func__, on);
153 if (!on) {
154 if (disable_irq_wake(bsi->host_wake_irq))
155 BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
156 goto free_host_wake_irq;
157 }
158
159 ret = gpio_request(bsi->host_wake, "bt_host_wake");
160 if (ret < 0) {
161 BT_ERR("failed to request gpio pin %d, error %d\n",
162 bsi->host_wake, ret);
163 goto gpio_config_failed;
164 }
165
166 /* configure host_wake as input */
167 ret = gpio_direction_input(bsi->host_wake);
168 if (ret < 0) {
169 BT_ERR("failed to config GPIO %d as input pin, err %d\n",
170 bsi->host_wake, ret);
171 goto gpio_host_wake;
172 }
173
174 ret = gpio_request(bsi->ext_wake, "bt_ext_wake");
175 if (ret < 0) {
176 BT_ERR("failed to request gpio pin %d, error %d\n",
177 bsi->ext_wake, ret);
178 goto gpio_host_wake;
179 }
180
181 ret = gpio_direction_output(bsi->ext_wake, 1);
182 if (ret < 0) {
183 BT_ERR("failed to config GPIO %d as output pin, err %d\n",
184 bsi->ext_wake, ret);
185 goto gpio_ext_wake;
186 }
187
188 gpio_set_value(bsi->ext_wake, 1);
189
190 /* Initialize spinlock. */
191 spin_lock_init(&rw_lock);
192
193 /* Initialize timer */
194 init_timer(&tx_timer);
195 tx_timer.function = bluesleep_tx_timer_expire;
196 tx_timer.data = 0;
197
198 /* initialize host wake tasklet */
199 tasklet_init(&hostwake_task, hostwake_interrupt, 0);
200
201 if (bsi->irq_polarity == POLARITY_LOW) {
202 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
203 IRQF_DISABLED | IRQF_TRIGGER_FALLING,
204 "bluetooth hostwake", NULL);
205 } else {
206 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
207 IRQF_DISABLED | IRQF_TRIGGER_RISING,
208 "bluetooth hostwake", NULL);
209 }
210 if (ret < 0) {
211 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
212 goto delete_timer;
213 }
214
215 ret = enable_irq_wake(bsi->host_wake_irq);
216 if (ret < 0) {
217 BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
218 goto free_host_wake_irq;
219 }
220
221 return 0;
222
223free_host_wake_irq:
224 free_irq(bsi->host_wake_irq, NULL);
225delete_timer:
226 del_timer(&tx_timer);
227gpio_ext_wake:
228 gpio_free(bsi->ext_wake);
229gpio_host_wake:
230 gpio_free(bsi->host_wake);
231gpio_config_failed:
232 return ret;
233}
234
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530235/* Initialize protocol */
236static int ath_open(struct hci_uart *hu)
237{
238 struct ath_struct *ath;
239
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530240 BT_DBG("hu %p, bsi %p", hu, bsi);
241
242 if (!bsi)
243 return -EIO;
244
245 if (ath_bluesleep_gpio_config(1) < 0) {
246 BT_ERR("HCIATH3K GPIO Config failed");
247 return -EIO;
248 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530249
250 ath = kzalloc(sizeof(*ath), GFP_ATOMIC);
251 if (!ath)
252 return -ENOMEM;
253
254 skb_queue_head_init(&ath->txq);
255
256 hu->priv = ath;
257 ath->hu = hu;
258
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530259 ath->cur_sleep = enableuartsleep;
260 if (ath->cur_sleep == 1) {
261 set_bit(BT_SLEEPENABLE, &flags);
262 modify_timer_task();
263 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530264 INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
265
266 return 0;
267}
268
269/* Flush protocol data */
270static int ath_flush(struct hci_uart *hu)
271{
272 struct ath_struct *ath = hu->priv;
273
274 BT_DBG("hu %p", hu);
275
276 skb_queue_purge(&ath->txq);
277
278 return 0;
279}
280
281/* Close protocol */
282static int ath_close(struct hci_uart *hu)
283{
284 struct ath_struct *ath = hu->priv;
285
286 BT_DBG("hu %p", hu);
287
288 skb_queue_purge(&ath->txq);
289
290 cancel_work_sync(&ath->ctxtsw);
291
292 hu->priv = NULL;
293 kfree(ath);
294
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530295 if (bsi)
296 ath_bluesleep_gpio_config(0);
297
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530298 return 0;
299}
300
301#define HCI_OP_ATH_SLEEP 0xFC04
302
303/* Enqueue frame for transmittion */
304static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
305{
306 struct ath_struct *ath = hu->priv;
307
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530308 BT_DBG("");
309
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530310 if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) {
Dan Carpenter4ebaa4e2010-07-23 12:11:04 +0200311 kfree_skb(skb);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530312 return 0;
313 }
314
315 /*
316 * Update power management enable flag with parameters of
317 * HCI sleep enable vendor specific HCI command.
318 */
319 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
320 struct hci_command_hdr *hdr = (void *)skb->data;
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530321 if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) {
322 set_bit(BT_SLEEPCMD, &flags);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530323 ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530324 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530325 }
326
327 BT_DBG("hu %p skb %p", hu, skb);
328
329 /* Prepend skb with frame type */
330 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
331
332 skb_queue_tail(&ath->txq, skb);
333 set_bit(HCI_UART_SENDING, &hu->tx_state);
334
335 schedule_work(&ath->ctxtsw);
336
337 return 0;
338}
339
340static struct sk_buff *ath_dequeue(struct hci_uart *hu)
341{
342 struct ath_struct *ath = hu->priv;
343
344 return skb_dequeue(&ath->txq);
345}
346
347/* Recv data */
348static int ath_recv(struct hci_uart *hu, void *data, int count)
349{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530350 struct ath_struct *ath = hu->priv;
351 unsigned int type;
Jiejing Zhang78b4a562011-04-07 20:37:06 +0800352
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530353 BT_DBG("");
354
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530355 if (hci_recv_stream_fragment(hu->hdev, data, count) < 0)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530356 BT_ERR("Frame Reassembly Failed");
357
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530358 if (count & test_bit(BT_SLEEPCMD, &flags)) {
359 struct sk_buff *skb = hu->hdev->reassembly[0];
360
361 if (!skb) {
362 struct { char type; } *pkt;
363
364 /* Start of the frame */
365 pkt = data;
366 type = pkt->type;
367 } else
368 type = bt_cb(skb)->pkt_type;
369
370 if (type == HCI_EVENT_PKT) {
371 clear_bit(BT_SLEEPCMD, &flags);
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530372 BT_INFO("cur_sleep:%d\n", ath->cur_sleep);
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530373 if (ath->cur_sleep == 1)
374 set_bit(BT_SLEEPENABLE, &flags);
375 else
376 clear_bit(BT_SLEEPENABLE, &flags);
377 }
378 if (test_bit(BT_SLEEPENABLE, &flags))
379 modify_timer_task();
380 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530381 return count;
382}
383
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530384static void bluesleep_tx_timer_expire(unsigned long data)
385{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530386 if (!test_bit(BT_SLEEPENABLE, &flags))
387 return;
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530388 BT_INFO("Tx timer expired\n");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530389
390 set_bit(BT_TXEXPIRED, &flags);
391}
392
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530393static struct hci_uart_proto athp = {
394 .id = HCI_UART_ATH3K,
395 .open = ath_open,
396 .close = ath_close,
397 .recv = ath_recv,
398 .enqueue = ath_enqueue,
399 .dequeue = ath_dequeue,
400 .flush = ath_flush,
401};
402
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530403static int __init bluesleep_probe(struct platform_device *pdev)
404{
405 int ret;
406 struct resource *res;
407
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530408 BT_DBG("");
409
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530410 bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
411 if (!bsi) {
412 ret = -ENOMEM;
413 goto failed;
414 }
415
416 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
417 "gpio_host_wake");
418 if (!res) {
419 BT_ERR("couldn't find host_wake gpio\n");
420 ret = -ENODEV;
421 goto free_bsi;
422 }
423 bsi->host_wake = res->start;
424
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530425 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
426 "gpio_ext_wake");
427 if (!res) {
428 BT_ERR("couldn't find ext_wake gpio\n");
429 ret = -ENODEV;
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530430 goto free_bsi;
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530431 }
432 bsi->ext_wake = res->start;
433
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530434 bsi->host_wake_irq = platform_get_irq_byname(pdev, "host_wake");
435 if (bsi->host_wake_irq < 0) {
436 BT_ERR("couldn't find host_wake irq\n");
437 ret = -ENODEV;
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530438 goto free_bsi;
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530439 }
440
441 bsi->irq_polarity = POLARITY_LOW; /* low edge (falling edge) */
442
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530443 return 0;
444
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530445free_bsi:
446 kfree(bsi);
Ram Mohan Korukondae359d862012-08-18 21:12:17 +0530447 bsi = NULL;
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530448failed:
449 return ret;
450}
451
452static int bluesleep_remove(struct platform_device *pdev)
453{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530454 kfree(bsi);
455 return 0;
456}
457
458static struct platform_driver bluesleep_driver = {
459 .remove = bluesleep_remove,
460 .driver = {
461 .name = "bluesleep",
462 .owner = THIS_MODULE,
463 },
464};
465
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300466int __init ath_init(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530467{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530468 int ret;
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530469
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530470 ret = hci_uart_register_proto(&athp);
471
472 if (!ret)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530473 BT_INFO("HCIATH3K protocol initialized");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530474 else {
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530475 BT_ERR("HCIATH3K protocol registration failed");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530476 return ret;
477 }
478 ret = platform_driver_probe(&bluesleep_driver, bluesleep_probe);
479 if (ret)
480 return ret;
481 return 0;
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530482}
483
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300484int __exit ath_deinit(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530485{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530486 platform_driver_unregister(&bluesleep_driver);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530487 return hci_uart_unregister_proto(&athp);
488}