blob: cfcacff6636976845a561a71db3b0e08fef80ea5 [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 Korukondaeb530d12012-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{
96 printk(KERN_INFO " wakeup host\n");
97}
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)) {
112 printk(KERN_INFO "wakeup device\n");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530113 gpio_set_value(bsi->ext_wake, 0);
Ram Mohan Korukondaeb530d12012-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
141/* Initialize protocol */
142static int ath_open(struct hci_uart *hu)
143{
144 struct ath_struct *ath;
145
146 BT_DBG("hu %p", hu);
147
148 ath = kzalloc(sizeof(*ath), GFP_ATOMIC);
149 if (!ath)
150 return -ENOMEM;
151
152 skb_queue_head_init(&ath->txq);
153
154 hu->priv = ath;
155 ath->hu = hu;
156
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530157 ath->cur_sleep = enableuartsleep;
158 if (ath->cur_sleep == 1) {
159 set_bit(BT_SLEEPENABLE, &flags);
160 modify_timer_task();
161 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530162 INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
163
164 return 0;
165}
166
167/* Flush protocol data */
168static int ath_flush(struct hci_uart *hu)
169{
170 struct ath_struct *ath = hu->priv;
171
172 BT_DBG("hu %p", hu);
173
174 skb_queue_purge(&ath->txq);
175
176 return 0;
177}
178
179/* Close protocol */
180static int ath_close(struct hci_uart *hu)
181{
182 struct ath_struct *ath = hu->priv;
183
184 BT_DBG("hu %p", hu);
185
186 skb_queue_purge(&ath->txq);
187
188 cancel_work_sync(&ath->ctxtsw);
189
190 hu->priv = NULL;
191 kfree(ath);
192
193 return 0;
194}
195
196#define HCI_OP_ATH_SLEEP 0xFC04
197
198/* Enqueue frame for transmittion */
199static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
200{
201 struct ath_struct *ath = hu->priv;
202
203 if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) {
Dan Carpenter4ebaa4e2010-07-23 12:11:04 +0200204 kfree_skb(skb);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530205 return 0;
206 }
207
208 /*
209 * Update power management enable flag with parameters of
210 * HCI sleep enable vendor specific HCI command.
211 */
212 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
213 struct hci_command_hdr *hdr = (void *)skb->data;
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530214 if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) {
215 set_bit(BT_SLEEPCMD, &flags);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530216 ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530217 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530218 }
219
220 BT_DBG("hu %p skb %p", hu, skb);
221
222 /* Prepend skb with frame type */
223 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
224
225 skb_queue_tail(&ath->txq, skb);
226 set_bit(HCI_UART_SENDING, &hu->tx_state);
227
228 schedule_work(&ath->ctxtsw);
229
230 return 0;
231}
232
233static struct sk_buff *ath_dequeue(struct hci_uart *hu)
234{
235 struct ath_struct *ath = hu->priv;
236
237 return skb_dequeue(&ath->txq);
238}
239
240/* Recv data */
241static int ath_recv(struct hci_uart *hu, void *data, int count)
242{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530243 struct ath_struct *ath = hu->priv;
244 unsigned int type;
Jiejing Zhang78b4a562011-04-07 20:37:06 +0800245
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530246 if (hci_recv_stream_fragment(hu->hdev, data, count) < 0)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530247 BT_ERR("Frame Reassembly Failed");
248
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530249 if (count & test_bit(BT_SLEEPCMD, &flags)) {
250 struct sk_buff *skb = hu->hdev->reassembly[0];
251
252 if (!skb) {
253 struct { char type; } *pkt;
254
255 /* Start of the frame */
256 pkt = data;
257 type = pkt->type;
258 } else
259 type = bt_cb(skb)->pkt_type;
260
261 if (type == HCI_EVENT_PKT) {
262 clear_bit(BT_SLEEPCMD, &flags);
263 printk(KERN_INFO "cur_sleep:%d\n", ath->cur_sleep);
264 if (ath->cur_sleep == 1)
265 set_bit(BT_SLEEPENABLE, &flags);
266 else
267 clear_bit(BT_SLEEPENABLE, &flags);
268 }
269 if (test_bit(BT_SLEEPENABLE, &flags))
270 modify_timer_task();
271 }
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530272 return count;
273}
274
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530275static void bluesleep_tx_timer_expire(unsigned long data)
276{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530277 if (!test_bit(BT_SLEEPENABLE, &flags))
278 return;
279 BT_DBG("Tx timer expired");
280 printk(KERN_INFO "Tx timer expired\n");
281
282 set_bit(BT_TXEXPIRED, &flags);
283}
284
285static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
286{
287 /* schedule a tasklet to handle the change in the host wake line */
288 tasklet_schedule(&hostwake_task);
289 return IRQ_HANDLED;
290}
291
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530292static struct hci_uart_proto athp = {
293 .id = HCI_UART_ATH3K,
294 .open = ath_open,
295 .close = ath_close,
296 .recv = ath_recv,
297 .enqueue = ath_enqueue,
298 .dequeue = ath_dequeue,
299 .flush = ath_flush,
300};
301
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530302static int __init bluesleep_probe(struct platform_device *pdev)
303{
304 int ret;
305 struct resource *res;
306
307 bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
308 if (!bsi) {
309 ret = -ENOMEM;
310 goto failed;
311 }
312
313 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
314 "gpio_host_wake");
315 if (!res) {
316 BT_ERR("couldn't find host_wake gpio\n");
317 ret = -ENODEV;
318 goto free_bsi;
319 }
320 bsi->host_wake = res->start;
321
322 ret = gpio_request(bsi->host_wake, "bt_host_wake");
323 if (ret)
324 goto free_bsi;
325
326 /* configure host_wake as input */
327 ret = gpio_direction_input(bsi->host_wake);
328 if (ret < 0) {
329 pr_err("%s: gpio_direction_input failed for GPIO %d, error %d\n",
330 __func__, bsi->host_wake, ret);
331 gpio_free(bsi->host_wake);
332 goto free_bsi;
333 }
334
335 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
336 "gpio_ext_wake");
337 if (!res) {
338 BT_ERR("couldn't find ext_wake gpio\n");
339 ret = -ENODEV;
340 goto free_bt_host_wake;
341 }
342 bsi->ext_wake = res->start;
343
344 ret = gpio_request(bsi->ext_wake, "bt_ext_wake");
345 if (ret)
346 goto free_bt_host_wake;
347
348 /* configure ext_wake as output mode*/
349 ret = gpio_direction_output(bsi->ext_wake, 1);
350 if (ret < 0) {
351 pr_err("%s: gpio_direction_output failed for GPIO %d, error %d\n",
352 __func__, bsi->ext_wake, ret);
353 gpio_free(bsi->ext_wake);
354 goto free_bt_host_wake;
355 }
Ram Mohan Korukondaeb530d12012-08-08 16:54:51 +0530356 gpio_set_value(bsi->ext_wake, 1);
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530357
358 bsi->host_wake_irq = platform_get_irq_byname(pdev, "host_wake");
359 if (bsi->host_wake_irq < 0) {
360 BT_ERR("couldn't find host_wake irq\n");
361 ret = -ENODEV;
362 goto free_bt_ext_wake;
363 }
364
365 bsi->irq_polarity = POLARITY_LOW; /* low edge (falling edge) */
366
367 /* Initialize spinlock. */
368 spin_lock_init(&rw_lock);
369
370 /* Initialize timer */
371 init_timer(&tx_timer);
372 tx_timer.function = bluesleep_tx_timer_expire;
373 tx_timer.data = 0;
374
375 /* initialize host wake tasklet */
376 tasklet_init(&hostwake_task, hostwake_interrupt, 0);
377
378 if (bsi->irq_polarity == POLARITY_LOW) {
379 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
380 IRQF_DISABLED | IRQF_TRIGGER_FALLING,
381 "bluetooth hostwake", NULL);
382 } else {
383 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
384 IRQF_DISABLED | IRQF_TRIGGER_RISING,
385 "bluetooth hostwake", NULL);
386 }
387 if (ret < 0) {
388 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
389 goto free_bt_timer;
390 }
391
392 ret = enable_irq_wake(bsi->host_wake_irq);
393 if (ret < 0) {
394 BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
395 free_irq(bsi->host_wake_irq, NULL);
396 goto free_bt_timer;
397 }
398
399 return 0;
400
401free_bt_timer:
402 del_timer(&tx_timer);
403free_bt_ext_wake:
404 gpio_free(bsi->ext_wake);
405free_bt_host_wake:
406 gpio_free(bsi->host_wake);
407free_bsi:
408 kfree(bsi);
409failed:
410 return ret;
411}
412
413static int bluesleep_remove(struct platform_device *pdev)
414{
415 /* assert bt wake */
416 gpio_set_value(bsi->ext_wake, 0);
417 if (disable_irq_wake(bsi->host_wake_irq))
418 BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
419 free_irq(bsi->host_wake_irq, NULL);
420 del_timer_sync(&tx_timer);
421 gpio_free(bsi->host_wake);
422 gpio_free(bsi->ext_wake);
423 kfree(bsi);
424 return 0;
425}
426
427static struct platform_driver bluesleep_driver = {
428 .remove = bluesleep_remove,
429 .driver = {
430 .name = "bluesleep",
431 .owner = THIS_MODULE,
432 },
433};
434
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300435int __init ath_init(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530436{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530437 int ret;
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530438
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530439 ret = hci_uart_register_proto(&athp);
440
441 if (!ret)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530442 BT_INFO("HCIATH3K protocol initialized");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530443 else {
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530444 BT_ERR("HCIATH3K protocol registration failed");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530445 return ret;
446 }
447 ret = platform_driver_probe(&bluesleep_driver, bluesleep_probe);
448 if (ret)
449 return ret;
450 return 0;
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530451}
452
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300453int __exit ath_deinit(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530454{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530455 platform_driver_unregister(&bluesleep_driver);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530456 return hci_uart_unregister_proto(&athp);
457}