blob: 2bb82ce3115d3bca104a6445bfafc6a4df82aa00 [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
Santosh Sajjan55efc7f2012-06-27 19:10:49 +053047unsigned int enableuartsleep;
48module_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");
113 gpio_set_value(bsi->ext_wake, 1);
114 msleep(20);
115 gpio_set_value(bsi->ext_wake, 0);
116 }
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{
277 unsigned long irq_flags;
278
279 if (!test_bit(BT_SLEEPENABLE, &flags))
280 return;
281 BT_DBG("Tx timer expired");
282 printk(KERN_INFO "Tx timer expired\n");
283
284 set_bit(BT_TXEXPIRED, &flags);
285}
286
287static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
288{
289 /* schedule a tasklet to handle the change in the host wake line */
290 tasklet_schedule(&hostwake_task);
291 return IRQ_HANDLED;
292}
293
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530294static struct hci_uart_proto athp = {
295 .id = HCI_UART_ATH3K,
296 .open = ath_open,
297 .close = ath_close,
298 .recv = ath_recv,
299 .enqueue = ath_enqueue,
300 .dequeue = ath_dequeue,
301 .flush = ath_flush,
302};
303
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530304static int __init bluesleep_probe(struct platform_device *pdev)
305{
306 int ret;
307 struct resource *res;
308
309 bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
310 if (!bsi) {
311 ret = -ENOMEM;
312 goto failed;
313 }
314
315 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
316 "gpio_host_wake");
317 if (!res) {
318 BT_ERR("couldn't find host_wake gpio\n");
319 ret = -ENODEV;
320 goto free_bsi;
321 }
322 bsi->host_wake = res->start;
323
324 ret = gpio_request(bsi->host_wake, "bt_host_wake");
325 if (ret)
326 goto free_bsi;
327
328 /* configure host_wake as input */
329 ret = gpio_direction_input(bsi->host_wake);
330 if (ret < 0) {
331 pr_err("%s: gpio_direction_input failed for GPIO %d, error %d\n",
332 __func__, bsi->host_wake, ret);
333 gpio_free(bsi->host_wake);
334 goto free_bsi;
335 }
336
337 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
338 "gpio_ext_wake");
339 if (!res) {
340 BT_ERR("couldn't find ext_wake gpio\n");
341 ret = -ENODEV;
342 goto free_bt_host_wake;
343 }
344 bsi->ext_wake = res->start;
345
346 ret = gpio_request(bsi->ext_wake, "bt_ext_wake");
347 if (ret)
348 goto free_bt_host_wake;
349
350 /* configure ext_wake as output mode*/
351 ret = gpio_direction_output(bsi->ext_wake, 1);
352 if (ret < 0) {
353 pr_err("%s: gpio_direction_output failed for GPIO %d, error %d\n",
354 __func__, bsi->ext_wake, ret);
355 gpio_free(bsi->ext_wake);
356 goto free_bt_host_wake;
357 }
358 gpio_set_value(bsi->ext_wake, 0);
359
360 bsi->host_wake_irq = platform_get_irq_byname(pdev, "host_wake");
361 if (bsi->host_wake_irq < 0) {
362 BT_ERR("couldn't find host_wake irq\n");
363 ret = -ENODEV;
364 goto free_bt_ext_wake;
365 }
366
367 bsi->irq_polarity = POLARITY_LOW; /* low edge (falling edge) */
368
369 /* Initialize spinlock. */
370 spin_lock_init(&rw_lock);
371
372 /* Initialize timer */
373 init_timer(&tx_timer);
374 tx_timer.function = bluesleep_tx_timer_expire;
375 tx_timer.data = 0;
376
377 /* initialize host wake tasklet */
378 tasklet_init(&hostwake_task, hostwake_interrupt, 0);
379
380 if (bsi->irq_polarity == POLARITY_LOW) {
381 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
382 IRQF_DISABLED | IRQF_TRIGGER_FALLING,
383 "bluetooth hostwake", NULL);
384 } else {
385 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
386 IRQF_DISABLED | IRQF_TRIGGER_RISING,
387 "bluetooth hostwake", NULL);
388 }
389 if (ret < 0) {
390 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
391 goto free_bt_timer;
392 }
393
394 ret = enable_irq_wake(bsi->host_wake_irq);
395 if (ret < 0) {
396 BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
397 free_irq(bsi->host_wake_irq, NULL);
398 goto free_bt_timer;
399 }
400
401 return 0;
402
403free_bt_timer:
404 del_timer(&tx_timer);
405free_bt_ext_wake:
406 gpio_free(bsi->ext_wake);
407free_bt_host_wake:
408 gpio_free(bsi->host_wake);
409free_bsi:
410 kfree(bsi);
411failed:
412 return ret;
413}
414
415static int bluesleep_remove(struct platform_device *pdev)
416{
417 /* assert bt wake */
418 gpio_set_value(bsi->ext_wake, 0);
419 if (disable_irq_wake(bsi->host_wake_irq))
420 BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
421 free_irq(bsi->host_wake_irq, NULL);
422 del_timer_sync(&tx_timer);
423 gpio_free(bsi->host_wake);
424 gpio_free(bsi->ext_wake);
425 kfree(bsi);
426 return 0;
427}
428
429static struct platform_driver bluesleep_driver = {
430 .remove = bluesleep_remove,
431 .driver = {
432 .name = "bluesleep",
433 .owner = THIS_MODULE,
434 },
435};
436
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300437int __init ath_init(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530438{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530439 int ret;
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530440
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530441 ret = hci_uart_register_proto(&athp);
442
443 if (!ret)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530444 BT_INFO("HCIATH3K protocol initialized");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530445 else {
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530446 BT_ERR("HCIATH3K protocol registration failed");
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530447 return ret;
448 }
449 ret = platform_driver_probe(&bluesleep_driver, bluesleep_probe);
450 if (ret)
451 return ret;
452 return 0;
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530453}
454
Gustavo F. Padovanf2b94bb2010-07-24 02:04:44 -0300455int __exit ath_deinit(void)
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530456{
Santosh Sajjan55efc7f2012-06-27 19:10:49 +0530457 platform_driver_unregister(&bluesleep_driver);
Suraj Sumangalab3190df2010-07-19 12:34:07 +0530458 return hci_uart_unregister_proto(&athp);
459}