1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
|
/*
* Atheros Communication Bluetooth HCIATH3K UART protocol
*
* HCIATH3K (HCI Atheros AR300x Protocol) is a Atheros Communication's
* power management protocol extension to H4 to support AR300x Bluetooth Chip.
*
* Copyright (c) 2009-2010 Atheros Communications Inc.
* Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
*
* Acknowledgements:
* This file is based on hci_h4.c, which was written
* by Maxim Krasnyansky and Marcel Holtmann.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/skbuff.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/proc_fs.h>
#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>
#include "hci_uart.h"
#ifdef CONFIG_SERIAL_MSM_HS
#include <linux/platform_data/msm_serial_hs.h>
#endif
static int enableuartsleep = 1;
module_param(enableuartsleep, int, 0644);
MODULE_PARM_DESC(enableuartsleep, "Enable Atheros Sleep Protocol");
/*
* Global variables
*/
/** Device table */
static struct of_device_id bluesleep_match_table[] = {
{ .compatible = "qca,ar3002_bluesleep" },
{}
};
/** Global state flags */
static unsigned long flags;
/** To Check LPM is enabled */
static bool is_lpm_enabled;
/** Workqueue to respond to change in hostwake line */
static void wakeup_host_work(struct work_struct *work);
/** Transmission timer */
static void bluesleep_tx_timer_expire(unsigned long data);
static DEFINE_TIMER(tx_timer, bluesleep_tx_timer_expire, 0, 0);
/** Lock for state transitions */
static spinlock_t rw_lock;
#define PROC_DIR "bluetooth/sleep"
#define POLARITY_LOW 0
#define POLARITY_HIGH 1
struct bluesleep_info {
unsigned host_wake; /* wake up host */
unsigned ext_wake; /* wake up device */
unsigned host_wake_irq;
int irq_polarity;
struct uart_port *uport;
};
struct work_struct ws_sleep;
/* 1 second timeout */
#define TX_TIMER_INTERVAL 1
/* state variable names and bit positions */
#define BT_TXEXPIRED 0x01
#define BT_SLEEPENABLE 0x02
#define BT_SLEEPCMD 0x03
/* global pointer to a single hci device. */
static struct bluesleep_info *bsi;
struct ath_struct {
struct hci_uart *hu;
unsigned int cur_sleep;
struct sk_buff_head txq;
struct work_struct ctxtsw;
};
static void hsuart_serial_clock_on(struct uart_port *port)
{
BT_DBG("");
if (port)
msm_hs_request_clock_on(port);
else
BT_INFO("Uart has not voted for Clock ON");
}
static void hsuart_serial_clock_off(struct uart_port *port)
{
BT_DBG("");
if (port)
msm_hs_request_clock_off(port);
else
BT_INFO("Uart has not voted for Clock OFF");
}
static void modify_timer_task(void)
{
spin_lock(&rw_lock);
mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
clear_bit(BT_TXEXPIRED, &flags);
spin_unlock(&rw_lock);
}
static int ath_wakeup_ar3k(void)
{
int status = 0;
if (test_bit(BT_TXEXPIRED, &flags)) {
hsuart_serial_clock_on(bsi->uport);
BT_DBG("wakeup device\n");
gpio_set_value(bsi->ext_wake, 0);
msleep(20);
gpio_set_value(bsi->ext_wake, 1);
}
if (!is_lpm_enabled)
modify_timer_task();
return status;
}
static void wakeup_host_work(struct work_struct *work)
{
BT_DBG("wake up host");
if (test_bit(BT_SLEEPENABLE, &flags)) {
if (test_bit(BT_TXEXPIRED, &flags))
hsuart_serial_clock_on(bsi->uport);
}
if (!is_lpm_enabled)
modify_timer_task();
}
static void ath_hci_uart_work(struct work_struct *work)
{
int status;
struct ath_struct *ath;
struct hci_uart *hu;
ath = container_of(work, struct ath_struct, ctxtsw);
hu = ath->hu;
/* verify and wake up controller */
if (test_bit(BT_SLEEPENABLE, &flags))
status = ath_wakeup_ar3k();
/* Ready to send Data */
clear_bit(HCI_UART_SENDING, &hu->tx_state);
hci_uart_tx_wakeup(hu);
}
static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
{
/* schedule a work to global shared workqueue to handle
* the change in the host wake line
*/
schedule_work(&ws_sleep);
return IRQ_HANDLED;
}
static int ath_bluesleep_gpio_config(int on)
{
int ret = 0;
BT_INFO("%s config: %d", __func__, on);
if (!on) {
if (disable_irq_wake(bsi->host_wake_irq))
BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
goto free_host_wake_irq;
}
ret = gpio_request(bsi->host_wake, "bt_host_wake");
if (ret < 0) {
BT_ERR("failed to request gpio pin %d, error %d\n",
bsi->host_wake, ret);
goto gpio_config_failed;
}
/* configure host_wake as input */
ret = gpio_direction_input(bsi->host_wake);
if (ret < 0) {
BT_ERR("failed to config GPIO %d as input pin, err %d\n",
bsi->host_wake, ret);
goto gpio_host_wake;
}
ret = gpio_request(bsi->ext_wake, "bt_ext_wake");
if (ret < 0) {
BT_ERR("failed to request gpio pin %d, error %d\n",
bsi->ext_wake, ret);
goto gpio_host_wake;
}
ret = gpio_direction_output(bsi->ext_wake, 1);
if (ret < 0) {
BT_ERR("failed to config GPIO %d as output pin, err %d\n",
bsi->ext_wake, ret);
goto gpio_ext_wake;
}
gpio_set_value(bsi->ext_wake, 1);
/* Initialize spinlock. */
spin_lock_init(&rw_lock);
/* Initialize timer */
init_timer(&tx_timer);
tx_timer.function = bluesleep_tx_timer_expire;
tx_timer.data = 0;
if (bsi->irq_polarity == POLARITY_LOW) {
ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
IRQF_DISABLED | IRQF_TRIGGER_FALLING,
"bluetooth hostwake", NULL);
} else {
ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
IRQF_DISABLED | IRQF_TRIGGER_RISING,
"bluetooth hostwake", NULL);
}
if (ret < 0) {
BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
goto delete_timer;
}
ret = enable_irq_wake(bsi->host_wake_irq);
if (ret < 0) {
BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
goto free_host_wake_irq;
}
return 0;
free_host_wake_irq:
free_irq(bsi->host_wake_irq, NULL);
delete_timer:
del_timer(&tx_timer);
gpio_ext_wake:
gpio_free(bsi->ext_wake);
gpio_host_wake:
gpio_free(bsi->host_wake);
gpio_config_failed:
return ret;
}
static int ath_lpm_start(void)
{
BT_DBG("Start LPM mode");
if (!bsi) {
BT_ERR("HCIATH3K bluesleep info does not exist");
return -EIO;
}
bsi->uport = msm_hs_get_uart_port(0);
if (!bsi->uport) {
BT_ERR("UART Port is not available");
return -ENODEV;
}
INIT_WORK(&ws_sleep, wakeup_host_work);
if (ath_bluesleep_gpio_config(1) < 0) {
BT_ERR("HCIATH3K GPIO Config failed");
return -EIO;
}
return 0;
}
static int ath_lpm_stop(void)
{
BT_DBG("Stop LPM mode");
cancel_work_sync(&ws_sleep);
if (bsi) {
bsi->uport = NULL;
ath_bluesleep_gpio_config(0);
}
return 0;
}
/* Initialize protocol */
static int ath_open(struct hci_uart *hu)
{
struct ath_struct *ath;
struct uart_state *state;
BT_DBG("hu %p, bsi %p", hu, bsi);
if (!bsi) {
BT_ERR("HCIATH3K bluesleep info does not exist");
return -EIO;
}
ath = kzalloc(sizeof(*ath), GFP_ATOMIC);
if (!ath) {
BT_ERR("HCIATH3K Memory not enough to init driver");
return -ENOMEM;
}
skb_queue_head_init(&ath->txq);
hu->priv = ath;
ath->hu = hu;
state = hu->tty->driver_data;
if (!state) {
BT_ERR("HCIATH3K tty driver data does not exist");
return -ENXIO;
}
bsi->uport = state->uart_port;
if (ath_bluesleep_gpio_config(1) < 0) {
BT_ERR("HCIATH3K GPIO Config failed");
hu->priv = NULL;
kfree(ath);
return -EIO;
}
ath->cur_sleep = enableuartsleep;
if (ath->cur_sleep == 1) {
set_bit(BT_SLEEPENABLE, &flags);
modify_timer_task();
}
INIT_WORK(&ath->ctxtsw, ath_hci_uart_work);
INIT_WORK(&ws_sleep, wakeup_host_work);
return 0;
}
/* Flush protocol data */
static int ath_flush(struct hci_uart *hu)
{
struct ath_struct *ath = hu->priv;
BT_DBG("hu %p", hu);
skb_queue_purge(&ath->txq);
return 0;
}
/* Close protocol */
static int ath_close(struct hci_uart *hu)
{
struct ath_struct *ath = hu->priv;
BT_DBG("hu %p", hu);
skb_queue_purge(&ath->txq);
cancel_work_sync(&ath->ctxtsw);
cancel_work_sync(&ws_sleep);
if (bsi)
ath_bluesleep_gpio_config(0);
hu->priv = NULL;
bsi->uport = NULL;
kfree(ath);
return 0;
}
#define HCI_OP_ATH_SLEEP 0xFC04
/* Enqueue frame for transmittion */
static int ath_enqueue(struct hci_uart *hu, struct sk_buff *skb)
{
struct ath_struct *ath = hu->priv;
BT_DBG("");
if (bt_cb(skb)->pkt_type == HCI_SCODATA_PKT) {
kfree_skb(skb);
return 0;
}
/*
* Update power management enable flag with parameters of
* HCI sleep enable vendor specific HCI command.
*/
if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
struct hci_command_hdr *hdr = (void *)skb->data;
if (__le16_to_cpu(hdr->opcode) == HCI_OP_ATH_SLEEP) {
set_bit(BT_SLEEPCMD, &flags);
ath->cur_sleep = skb->data[HCI_COMMAND_HDR_SIZE];
}
}
BT_DBG("hu %p skb %p", hu, skb);
/* Prepend skb with frame type */
memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
skb_queue_tail(&ath->txq, skb);
set_bit(HCI_UART_SENDING, &hu->tx_state);
schedule_work(&ath->ctxtsw);
return 0;
}
static struct sk_buff *ath_dequeue(struct hci_uart *hu)
{
struct ath_struct *ath = hu->priv;
return skb_dequeue(&ath->txq);
}
/* Recv data */
static int ath_recv(struct hci_uart *hu, void *data, int count)
{
struct ath_struct *ath = hu->priv;
unsigned int type;
BT_DBG("");
if (hci_recv_stream_fragment(hu->hdev, data, count) < 0)
BT_ERR("Frame Reassembly Failed");
if (count & test_bit(BT_SLEEPCMD, &flags)) {
struct sk_buff *skb = hu->hdev->reassembly[0];
if (!skb) {
struct { char type; } *pkt;
/* Start of the frame */
pkt = data;
type = pkt->type;
} else
type = bt_cb(skb)->pkt_type;
if (type == HCI_EVENT_PKT) {
clear_bit(BT_SLEEPCMD, &flags);
BT_INFO("cur_sleep:%d\n", ath->cur_sleep);
if (ath->cur_sleep == 1)
set_bit(BT_SLEEPENABLE, &flags);
else
clear_bit(BT_SLEEPENABLE, &flags);
}
if (test_bit(BT_SLEEPENABLE, &flags))
modify_timer_task();
}
return count;
}
static void bluesleep_tx_timer_expire(unsigned long data)
{
if (!test_bit(BT_SLEEPENABLE, &flags))
return;
BT_INFO("Tx timer expired\n");
set_bit(BT_TXEXPIRED, &flags);
hsuart_serial_clock_off(bsi->uport);
}
static struct hci_uart_proto athp = {
.id = HCI_UART_ATH3K,
.open = ath_open,
.close = ath_close,
.recv = ath_recv,
.enqueue = ath_enqueue,
.dequeue = ath_dequeue,
.flush = ath_flush,
};
static int lpm_enabled;
static int bluesleep_lpm_set(const char *val, const struct kernel_param *kp)
{
int ret;
ret = param_set_int(val, kp);
if (ret) {
BT_ERR("HCIATH3K: lpm enable parameter set failed");
return ret;
}
BT_DBG("lpm : %d", lpm_enabled);
if ((lpm_enabled == 0) && is_lpm_enabled) {
ath_lpm_stop();
clear_bit(BT_SLEEPENABLE, &flags);
is_lpm_enabled = false;
} else if ((lpm_enabled == 1) && !is_lpm_enabled) {
if (ath_lpm_start() < 0) {
BT_ERR("HCIATH3K LPM mode failed");
return -EIO;
}
set_bit(BT_SLEEPENABLE, &flags);
is_lpm_enabled = true;
} else {
BT_ERR("HCIATH3K invalid lpm value");
return -EINVAL;
}
return 0;
}
static struct kernel_param_ops bluesleep_lpm_ops = {
.set = bluesleep_lpm_set,
.get = param_get_int,
};
module_param_cb(ath_lpm, &bluesleep_lpm_ops,
&lpm_enabled, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(ath_lpm, "Enable Atheros LPM sleep Protocol");
static int lpm_btwrite;
static int bluesleep_lpm_btwrite(const char *val, const struct kernel_param *kp)
{
int ret;
ret = param_set_int(val, kp);
if (ret) {
BT_ERR("HCIATH3K: lpm btwrite parameter set failed");
return ret;
}
BT_DBG("btwrite : %d", lpm_btwrite);
if (is_lpm_enabled) {
if (lpm_btwrite == 0) {
/*Setting TXEXPIRED bit to make it
compatible with current solution*/
set_bit(BT_TXEXPIRED, &flags);
hsuart_serial_clock_off(bsi->uport);
} else if (lpm_btwrite == 1) {
ath_wakeup_ar3k();
clear_bit(BT_TXEXPIRED, &flags);
} else {
BT_ERR("HCIATH3K invalid btwrite value");
return -EINVAL;
}
}
return 0;
}
static struct kernel_param_ops bluesleep_lpm_btwrite_ops = {
.set = bluesleep_lpm_btwrite,
.get = param_get_int,
};
module_param_cb(ath_btwrite, &bluesleep_lpm_btwrite_ops,
&lpm_btwrite, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(ath_lpm, "Assert/Deassert the sleep");
static int bluesleep_populate_dt_pinfo(struct platform_device *pdev)
{
BT_DBG("");
if (!bsi)
return -ENOMEM;
bsi->host_wake = of_get_named_gpio(pdev->dev.of_node,
"host-wake-gpio", 0);
if (bsi->host_wake < 0) {
BT_ERR("couldn't find host_wake gpio\n");
return -ENODEV;
}
bsi->ext_wake = of_get_named_gpio(pdev->dev.of_node,
"ext-wake-gpio", 0);
if (bsi->ext_wake < 0) {
BT_ERR("couldn't find ext_wake gpio\n");
return -ENODEV;
}
return 0;
}
static int bluesleep_populate_pinfo(struct platform_device *pdev)
{
struct resource *res;
BT_DBG("");
res = platform_get_resource_byname(pdev, IORESOURCE_IO,
"gpio_host_wake");
if (!res) {
BT_ERR("couldn't find host_wake gpio\n");
return -ENODEV;
}
bsi->host_wake = res->start;
res = platform_get_resource_byname(pdev, IORESOURCE_IO,
"gpio_ext_wake");
if (!res) {
BT_ERR("couldn't find ext_wake gpio\n");
return -ENODEV;
}
bsi->ext_wake = res->start;
return 0;
}
static int bluesleep_probe(struct platform_device *pdev)
{
int ret;
BT_DBG("");
bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
if (!bsi) {
ret = -ENOMEM;
goto failed;
}
if (pdev->dev.of_node) {
ret = bluesleep_populate_dt_pinfo(pdev);
if (ret < 0) {
BT_ERR("Failed to populate device tree info");
goto free_bsi;
}
} else {
ret = bluesleep_populate_pinfo(pdev);
if (ret < 0) {
BT_ERR("Failed to populate device info");
goto free_bsi;
}
}
BT_DBG("host_wake_gpio: %d ext_wake_gpio: %d",
bsi->host_wake, bsi->ext_wake);
bsi->host_wake_irq = platform_get_irq_byname(pdev, "host_wake");
if (bsi->host_wake_irq < 0) {
BT_ERR("couldn't find host_wake irq\n");
ret = -ENODEV;
goto free_bsi;
}
bsi->irq_polarity = POLARITY_LOW; /* low edge (falling edge) */
return 0;
free_bsi:
kfree(bsi);
bsi = NULL;
failed:
return ret;
}
static int bluesleep_remove(struct platform_device *pdev)
{
kfree(bsi);
return 0;
}
static struct platform_driver bluesleep_driver = {
.probe = bluesleep_probe,
.remove = bluesleep_remove,
.driver = {
.name = "bluesleep",
.owner = THIS_MODULE,
.of_match_table = bluesleep_match_table,
},
};
int __init ath_init(void)
{
int ret;
ret = hci_uart_register_proto(&athp);
if (!ret)
BT_INFO("HCIATH3K protocol initialized");
else {
BT_ERR("HCIATH3K protocol registration failed");
return ret;
}
ret = platform_driver_register(&bluesleep_driver);
if (ret) {
BT_ERR("Failed to register bluesleep driver");
return ret;
}
return 0;
}
int __exit ath_deinit(void)
{
platform_driver_unregister(&bluesleep_driver);
return hci_uart_unregister_proto(&athp);
}
|