3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License version 2 as
5 published by the Free Software Foundation.
7 You should have received a copy of the GNU General Public License
8 along with this program; if not, write to the Free Software
9 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Copyright (C) 2006-2007 - Motorola
18 Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
21 ----------- -------------- --------------------------------
22 2006-Apr-28 Motorola The kernel module for running the Bluetooth(R)
23 Sleep-Mode Protocol from the Host side
24 2006-Sep-08 Motorola Added workqueue for handling sleep work.
25 2007-Jan-24 Motorola Added mbm_handle_ioi() call to ISR.
26 2009-Aug-10 Motorola Changed "add_timer" to "mod_timer" to solve
27 race when flurry of queued work comes in.
30 #include <linux/module.h> /* kernel module definitions */
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <linux/notifier.h>
36 #include <linux/proc_fs.h>
37 #include <linux/spinlock.h>
38 #include <linux/timer.h>
39 #include <linux/uaccess.h>
40 #include <linux/version.h>
41 #include <linux/workqueue.h>
42 #include <linux/platform_device.h>
44 #include <linux/irq.h>
45 #include <linux/ioport.h>
46 #include <linux/param.h>
47 #include <linux/bitops.h>
48 #include <linux/termios.h>
49 #include <linux/wakelock.h>
50 //#include <mach/gpio.h>
51 #include <linux/gpio.h>
52 #include <linux/of_gpio.h>
53 //#include <mach/serial.h>
54 #include <linux/serial_core.h>
55 #include <net/bluetooth/bluetooth.h>
56 #include <net/bluetooth/hci_core.h> /* event notifications */
58 #include <linux/seq_file.h>
61 //#define BT_SLEEP_DBG pr_debug
62 #define BT_SLEEP_DBG BT_ERR
68 #define PROC_DIR "bluetooth/sleep"
70 #define POLARITY_LOW 0
71 #define POLARITY_HIGH 1
75 /* enable/disable wake-on-bluetooth */
76 #define BT_ENABLE_IRQ_WAKE 1
78 #define BT_BLUEDROID_SUPPORT 1
81 DEBUG_USER_STATE = 1U << 0,
82 DEBUG_SUSPEND = 1U << 1,
83 DEBUG_BTWAKE = 1U << 2,
84 DEBUG_VERBOSE = 1U << 3,
86 struct workqueue_struct *hostwake_work_queue;
88 static int debug_mask = DEBUG_USER_STATE;
89 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
91 struct bluesleep_info {
94 unsigned host_wake_irq;
95 struct uart_port *uport;
96 // struct wake_lock wake_lock;
97 struct wake_lock BT_wakelock;
98 struct wake_lock host_wakelock;
104 static void bluesleep_sleep_work(struct work_struct *work);
107 DECLARE_DELAYED_WORK(sleep_workqueue, bluesleep_sleep_work);
109 /* Macros for handling sleep work */
110 #define bluesleep_rx_busy() schedule_delayed_work(&sleep_workqueue, 0)
111 #define bluesleep_tx_busy() schedule_delayed_work(&sleep_workqueue, 0)
112 #define bluesleep_rx_idle() schedule_delayed_work(&sleep_workqueue, 0)
113 #define bluesleep_tx_idle() schedule_delayed_work(&sleep_workqueue, 0)
115 /* 1 second timeout */
116 #define TX_TIMER_INTERVAL 8 //after 10S DEV pin change,the BT can sleep
118 /* state variable names and bit positions */
119 #define BT_PROTO 0x01
120 #define BT_TXDATA 0x02
121 #define BT_ASLEEP 0x04
122 #define BT_EXT_WAKE 0x08
123 #define BT_SUSPEND 0x10
125 #if BT_BLUEDROID_SUPPORT
126 static bool has_lpm_enabled = false;
128 /* global pointer to a single hci device. */
129 static struct hci_dev *bluesleep_hdev;
132 static struct platform_device *bluesleep_uart_dev;
133 static struct bluesleep_info *bsi;
134 static bool is_bt_stoped=0;
137 static atomic_t open_count = ATOMIC_INIT(1);
140 * Local function prototypes
142 #if !BT_BLUEDROID_SUPPORT
143 static int bluesleep_hci_event(struct notifier_block *this,
144 unsigned long event, void *data);
146 static int bluesleep_start(void);
147 static void bluesleep_stop(void);
148 extern struct uart_port * serial_get_uart_port(int uart_index);
153 /** Global state flags */
154 static unsigned long flags;
156 /** Tasklet to respond to change in hostwake line */
157 static struct tasklet_struct hostwake_task;
159 /** Transmission timer */
160 static void bluesleep_tx_timer_expire(unsigned long data);
161 static DEFINE_TIMER(tx_timer, bluesleep_tx_timer_expire, 0, 0);
163 /** Lock for state transitions */
164 static spinlock_t rw_lock;
166 #if !BT_BLUEDROID_SUPPORT
167 /** Notifier block for HCI events */
168 struct notifier_block hci_event_nblock = {
169 .notifier_call = bluesleep_hci_event,
173 struct proc_dir_entry *bluetooth_dir, *sleep_dir;
179 static void hsuart_power(int on)
180 {BT_ERR("hsuart_power");
181 if (test_bit(BT_SUSPEND, &flags))
184 //msm_hs_request_clock_on(bsi->uport);
185 // msm_hs_set_mctrl(bsi->uport, TIOCM_RTS);
187 // msm_hs_set_mctrl(bsi->uport, 0);
188 // msm_hs_request_clock_off(bsi->uport);
190 BT_SLEEP_DBG("uart power %d\n", on);
195 * @return 1 if the Host can go to sleep, 0 otherwise.
197 int bluesleep_can_sleep(void)
199 /* check if WAKE_BT_GPIO and BT_WAKE_GPIO are both deasserted */
200 BT_SLEEP_DBG("bt_wake %d, host_wake %d, uport %p"
201 , gpio_get_value(bsi->ext_wake)
202 , gpio_get_value(bsi->host_wake)
204 return ((gpio_get_value(bsi->host_wake) != bsi->irq_polarity) &&
205 (test_bit(BT_EXT_WAKE, &flags)) &&
206 (bsi->uport != NULL));
209 void bluesleep_sleep_wakeup(void)
210 { BT_ERR("bluesleep_sleep_wakeup");
211 if (test_bit(BT_ASLEEP, &flags)) {
212 BT_SLEEP_DBG("waking up...");
213 // wake_lock(&bsi->wake_lock); //wake
214 /* Start the timer */
215 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
216 if (bsi->has_ext_wake == 1)
217 gpio_set_value(bsi->ext_wake, 0);
218 clear_bit(BT_EXT_WAKE, &flags);
219 clear_bit(BT_ASLEEP, &flags);
226 * @brief@ main sleep work handling function which update the flags
227 * and activate and deactivate UART ,check FIFO.
229 static void bluesleep_sleep_work(struct work_struct *work)
231 BT_ERR("bluesleep_sleep_work");
232 if (bluesleep_can_sleep()) {
233 BT_SLEEP_DBG("can sleep...");
234 /* already asleep, this is an error case */
235 if (test_bit(BT_ASLEEP, &flags)) {
236 BT_SLEEP_DBG("already asleep");
240 if (bsi->uport->ops->tx_empty(bsi->uport)) {
241 BT_SLEEP_DBG("going to sleep...");
242 set_bit(BT_ASLEEP, &flags);
243 /*Deactivating UART */
245 /* UART clk is not turned off immediately. Release
246 * wakelock after 500 ms.
248 // wake_lock_timeout(&bsi->wake_lock, HZ / 2);
250 BT_SLEEP_DBG("tx buffer is not empty, modify timer...");
253 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
256 } else if (test_bit(BT_EXT_WAKE, &flags)
257 && !test_bit(BT_ASLEEP, &flags)) {
258 BT_SLEEP_DBG("can not sleep, bt_wake %d", gpio_get_value(bsi->ext_wake) );
259 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
260 gpio_set_value(bsi->ext_wake, 0);
261 clear_bit(BT_EXT_WAKE, &flags);
263 bluesleep_sleep_wakeup();
268 * A workqueue that runs in workqueue context and reads the value
269 * of the HOST_WAKE GPIO pin and further defer the work.
270 * @param work Not used.
272 static void bluesleep_hostwake_task(unsigned long data)
274 BT_ERR("hostwake line change");
275 BT_ERR("irq_polarity=%d\n",bsi->irq_polarity);
276 BT_ERR("bsi->host_wake=%d\n",gpio_get_value(bsi->host_wake));
279 if ((gpio_get_value(bsi->host_wake) == bsi->irq_polarity))
284 spin_unlock(&rw_lock);
288 * Handles proper timer action when outgoing data is delivered to the
289 * HCI line discipline. Sets BT_TXDATA.
291 static void bluesleep_outgoing_data(void)
293 unsigned long irq_flags;
294 BT_ERR("bluesleep_outgoing_data\n");
295 spin_lock_irqsave(&rw_lock, irq_flags);
296 wake_lock(&bsi->BT_wakelock);
299 if (bsi->has_ext_wake == 1) //wsh
300 gpio_set_value(bsi->ext_wake, 0); //wsh
301 set_bit(BT_EXT_WAKE, &flags); //wsh
304 /* log data passing by */
305 set_bit(BT_TXDATA, &flags);
307 spin_unlock_irqrestore(&rw_lock, irq_flags);
308 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ)); //add
310 /* if the tx side is sleeping... */
311 if (test_bit(BT_EXT_WAKE, &flags)) {
313 BT_SLEEP_DBG("tx was sleeping");
314 bluesleep_sleep_wakeup();
319 static ssize_t bluesleep_write_proc_lpm(struct file *file, const char __user *buffer,
320 size_t count, loff_t *pos)
323 BT_ERR("bluesleep_write_proc_lpm\n");
328 if (copy_from_user(&b, buffer, 1))
330 BT_ERR("bluesleep_write_proc_lpm_b=%d\n",b);
334 has_lpm_enabled = false;
338 if (!has_lpm_enabled) {
339 has_lpm_enabled = true;
340 bsi->uport = serial_get_uart_port(BT_PORT_ID);
341 /* if bluetooth started, start bluesleep*/
350 static int lpm_proc_show(struct seq_file * m,void * v)
353 BT_ERR("bluesleep_read_proc_lpm\n");
354 seq_printf(m, "unsupported to read\n");
358 static int bluesleep_open_proc_lpm(struct inode *inode, struct file *file)
360 return single_open(file, lpm_proc_show, PDE_DATA(inode));
363 static ssize_t bluesleep_write_proc_btwrite(struct file *file, const char __user *buffer,size_t count, loff_t *pos)
366 BT_ERR("bluesleep_write_proc_btwrite");
373 if (copy_from_user(&b, buffer, 1))
375 BT_ERR("bluesleep_write_proc_btwrite=%d",b);
379 bluesleep_outgoing_data();
383 static int btwrite_proc_show(struct seq_file * m,void * v)
385 //unsigned int btwrite;
386 BT_ERR("bluesleep_read_proc_lpm\n");
387 seq_printf(m, "unsupported to read\n");
391 static int bluesleep_open_proc_btwrite(struct inode *inode, struct file *file)
393 return single_open(file, btwrite_proc_show, PDE_DATA(inode));
398 * Handles transmission timer expiration.
399 * @param data Not used.
401 static void bluesleep_tx_timer_expire(unsigned long data)
405 if (bsi->uport->ops->tx_empty(bsi->uport))
407 gpio_set_value(bsi->ext_wake, 1);
408 wake_unlock(&bsi->BT_wakelock);
413 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL*HZ));
416 //gpio_set_value(bsi->ext_wake, 1);
418 unsigned long irq_flags;
419 BT_ERR("bluesleep_tx_timer_expire");
421 spin_lock_irqsave(&rw_lock, irq_flags);
423 BT_SLEEP_DBG("Tx timer expired");
425 /* were we silent during the last timeout? */
426 if (!test_bit(BT_TXDATA, &flags)) {
427 BT_SLEEP_DBG("Tx has been idle");
428 if (bsi->has_ext_wake == 1)
429 gpio_set_value(bsi->ext_wake, 1);
433 BT_SLEEP_DBG("Tx data during last period");
434 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL*HZ));
437 /* clear the incoming data flag */
438 clear_bit(BT_TXDATA, &flags);
440 spin_unlock_irqrestore(&rw_lock, irq_flags);
445 * Schedules a workqueue to run when receiving an interrupt on the
446 * <code>HOST_WAKE</code> GPIO pin.
447 * @param irq Not used.
448 * @param dev_id Not used.
450 static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
452 /* schedule a tasklet to handle the change in the host wake line */
454 int ext_wake, host_wake;
455 BT_ERR("bluesleep_hostwake_isr\n");
456 ext_wake = gpio_get_value(bsi->ext_wake);
457 host_wake = gpio_get_value(bsi->host_wake);
458 BT_DBG("ext_wake : %d, host_wake : %d", ext_wake, host_wake);
461 wake_lock(&bsi->host_wakelock);
462 irq_set_irq_type(irq, IRQF_TRIGGER_HIGH);
467 //wake_unlock(&bsi->host_wakelock);
468 wake_lock_timeout(&bsi->host_wakelock, HZ*5);
469 irq_set_irq_type(irq,IRQF_TRIGGER_LOW);
472 // irq_set_irq_type(irq, host_wake ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH);
473 if (host_wake == 0) {
474 BT_DBG("bluesleep_hostwake_isr : Registration Tasklet");
475 // tasklet_schedule(&hostwake_task);
482 * Starts the Sleep-Mode Protocol on the Host.
483 * @return On success, 0. On error, -1, and <code>errno</code> is set
486 static int bluesleep_start(void)
489 unsigned long irq_flags;
490 BT_ERR("bluesleep_start\n");
492 spin_lock_irqsave(&rw_lock, irq_flags);
494 if (test_bit(BT_PROTO, &flags)) {
495 spin_unlock_irqrestore(&rw_lock, irq_flags);
499 spin_unlock_irqrestore(&rw_lock, irq_flags);
501 if (!atomic_dec_and_test(&open_count)) {
502 atomic_inc(&open_count);
506 /* start the timer */
507 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
510 if (bsi->has_ext_wake == 1)
511 gpio_set_value(bsi->ext_wake, 0);
512 clear_bit(BT_EXT_WAKE, &flags);
513 #if BT_ENABLE_IRQ_WAKE
514 retval = enable_irq_wake(bsi->host_wake_irq);
516 BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
520 set_bit(BT_PROTO, &flags);
521 //wake_lock(&bsi->wake_lock);
524 del_timer(&tx_timer);
525 atomic_inc(&open_count);
531 * Stops the Sleep-Mode Protocol on the Host.
533 static void bluesleep_stop(void)
535 unsigned long irq_flags;
536 BT_ERR("bluesleep_stop\n");
538 spin_lock_irqsave(&rw_lock, irq_flags);
540 if (!test_bit(BT_PROTO, &flags)) {
541 spin_unlock_irqrestore(&rw_lock, irq_flags);
546 if (bsi->has_ext_wake == 1)
547 gpio_set_value(bsi->ext_wake, 0);
548 clear_bit(BT_EXT_WAKE, &flags);
549 del_timer(&tx_timer);
550 clear_bit(BT_PROTO, &flags);
552 if (test_bit(BT_ASLEEP, &flags)) {
553 clear_bit(BT_ASLEEP, &flags);
554 spin_unlock_irqrestore(&rw_lock, irq_flags);
557 spin_unlock_irqrestore(&rw_lock, irq_flags);
560 atomic_inc(&open_count);
562 #if BT_ENABLE_IRQ_WAKE
563 if (disable_irq_wake(bsi->host_wake_irq))
564 BT_SLEEP_DBG("Couldn't disable hostwake IRQ wakeup mode\n");
566 wake_unlock(&bsi->host_wakelock);
567 wake_unlock(&bsi->BT_wakelock);
569 // wake_lock_timeout(&bsi->wake_lock, HZ / 2);
573 * Write the <code>BT_WAKE</code> GPIO pin value via the proc interface.
574 * @param file Not used.
575 * @param buffer The buffer to read from.
576 * @param count The number of bytes to be written.
577 * @param data Not used.
578 * @return On success, the number of bytes written. On error, -1, and
579 * <code>errno</code> is set appropriately.
581 static ssize_t bluepower_write_proc_btwake(struct file *file, const char __user *buffer,
582 size_t count, loff_t *pos)
585 BT_ERR("bluepower_write_proc_btwake\n");
590 buf = kmalloc(count, GFP_KERNEL);
594 if (copy_from_user(buf, buffer, count)) {
600 if (bsi->has_ext_wake == 1)
601 gpio_set_value(bsi->ext_wake, 0);
602 clear_bit(BT_EXT_WAKE, &flags);
604 else if (buf[0] == '1') {
605 if (bsi->has_ext_wake == 1)
606 gpio_set_value(bsi->ext_wake, 1);
607 set_bit(BT_EXT_WAKE, &flags);
617 static int btwake_proc_show(struct seq_file *m, void *v)
620 btwake = test_bit(BT_EXT_WAKE, &flags) ? 1 : 0;
621 // return sprintf(buff, "asleep: %u\n", asleep); //cat: asleep:0
622 seq_printf(m, "%u\n", btwake);
626 static int bluepower_open_proc_btwake(struct inode *inode, struct file *file)
628 return single_open(file, btwake_proc_show, PDE_DATA(inode));
634 static int hostwake_proc_show(struct seq_file *m, void *v)
636 unsigned int hostwake;
637 hostwake = gpio_get_value(bsi->host_wake);
638 seq_printf(m, "%u\n", hostwake);
642 static int bluepower_open_proc_hostwake(struct inode *inode, struct file *file)
644 return single_open(file, hostwake_proc_show, PDE_DATA(inode));
650 static int bluesleep_proc_show(struct seq_file *m, void *v)
652 // seq_printf(m, "%d\n", val);
654 asleep = test_bit(BT_ASLEEP, &flags) ? 1 : 0;
655 // return sprintf(buff, "asleep: %u\n", asleep); //cat: asleep:0
656 seq_printf(m, "%u\n", asleep);
662 static int bluesleep_open_proc_asleep(struct inode *inode, struct file *file)
664 return single_open(file, bluesleep_proc_show, PDE_DATA(inode));
668 static ssize_t bluesleep_write_proc_proto(struct file *filp,const char __user *buff, size_t count, loff_t *pos)
671 BT_ERR("bluesleep_write_proc_proto\n");
674 if (copy_from_user(&proto, buff, 1)) //or get_user(proto, buffer)
677 BT_SLEEP_DBG("write proto %c", proto);
685 /* claim that we wrote everything */
690 static int proto_proc_show(struct seq_file *m, void *v)
693 proto = test_bit(BT_PROTO, &flags) ? 1 : 0;
694 seq_printf(m, "%u\n", proto);
698 static int bluesleep_open_proc_proto(struct inode *inode, struct file *file)
700 return single_open(file, proto_proc_show, PDE_DATA(inode));
706 void bluesleep_setup_uart_port(struct platform_device *uart_dev)
708 bluesleep_uart_dev = uart_dev;
711 static int bluesleep_populate_dt_pinfo(struct platform_device *pdev)
713 struct device_node *np = pdev->dev.of_node;
716 tmp = of_get_named_gpio(np, "host-wake-gpio", 0);
718 BT_ERR("couldn't find host_wake gpio");
721 bsi->host_wake = tmp;
723 tmp = of_get_named_gpio(np, "bt-wake-gpio", 0);
725 bsi->has_ext_wake = 0;
727 bsi->has_ext_wake = 1;
729 if (bsi->has_ext_wake)
732 BT_INFO("bt_host_wake %d, bt-wake-gpio %d",
738 static int bluesleep_populate_pinfo(struct platform_device *pdev)
740 struct resource *res;
742 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
745 BT_ERR("couldn't find host_wake gpio");
748 bsi->host_wake = res->start;
750 res = platform_get_resource_byname(pdev, IORESOURCE_IO,
753 bsi->has_ext_wake = 0;
755 bsi->has_ext_wake = 1;
757 if (bsi->has_ext_wake)
758 bsi->ext_wake = res->start;
763 static int bluesleep_probe(struct platform_device *pdev)
765 //struct resource *res;
768 bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
772 if (pdev->dev.of_node) {
773 ret = bluesleep_populate_dt_pinfo(pdev);
775 BT_ERR("couldn't populate info from dt");
779 ret = bluesleep_populate_pinfo(pdev);
781 BT_ERR("couldn't populate info");
786 /* configure host_wake as input */
787 ret = gpio_request_one(bsi->host_wake, GPIOF_IN, "bt_host_wake");
789 BT_ERR("failed to configure input"
790 " direction for GPIO %d, error %d",
791 bsi->host_wake, ret);
795 if (debug_mask & DEBUG_BTWAKE)
796 pr_info("BT WAKE: set to wake\n");
797 if (bsi->has_ext_wake) {
798 /* configure ext_wake as output mode*/
799 ret = gpio_request_one(bsi->ext_wake,
800 GPIOF_OUT_INIT_LOW, "bt_ext_wake");
802 BT_ERR("failed to configure output"
803 " direction for GPIO %d, error %d",
805 goto free_bt_host_wake;
808 clear_bit(BT_EXT_WAKE, &flags);
810 BT_SLEEP_DBG("allocat irq hostwake = %d\n", bsi->host_wake);
811 //bsi->host_wake_irq = sprd_alloc_gpio_irq(bsi->host_wake);
812 bsi->host_wake_irq = gpio_to_irq(bsi->host_wake);
813 BT_SLEEP_DBG("irq = bsi->host_wake_irq %d", bsi->host_wake_irq);
814 if (bsi->host_wake_irq < 0) {
815 BT_SLEEP_DBG("couldn't find host_wake irq\n");
817 goto free_bt_ext_wake;
821 bsi->irq_polarity = POLARITY_LOW;/*low edge (falling edge)*/
823 //wake_lock_init(&bsi->wake_lock, WAKE_LOCK_SUSPEND, "bluesleep");
825 wake_lock_init(&bsi->BT_wakelock, WAKE_LOCK_SUSPEND, "bluesleep1");
826 wake_lock_init(&bsi->host_wakelock, WAKE_LOCK_SUSPEND, "bluesleep2");
827 clear_bit(BT_SUSPEND, &flags);
829 BT_INFO("host_wake_irq %d, polarity %d",
833 ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
834 //IRQF_TRIGGER_FALLING |IRQF_NO_SUSPEND, remove yan
835 (IRQF_TRIGGER_LOW|IRQF_NO_SUSPEND), //add low level
836 "bt_host_wake", bsi);
839 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
840 goto free_bt_ext_wake;
846 gpio_free(bsi->ext_wake);
848 gpio_free(bsi->host_wake);
854 static int bluesleep_remove(struct platform_device *pdev)
855 { BT_ERR("bluesleep_remove");
856 free_irq(bsi->host_wake_irq, NULL);
857 gpio_free(bsi->host_wake);
858 gpio_free(bsi->ext_wake);
859 // wake_lock_destroy(&bsi->wake_lock);
860 wake_lock_destroy(&bsi->BT_wakelock);
861 wake_lock_destroy(&bsi->host_wakelock);
868 static int bluesleep_resume(struct platform_device *pdev)
869 { BT_ERR("bluesleep_resume");
870 if (test_bit(BT_SUSPEND, &flags)) {
871 if (debug_mask & DEBUG_SUSPEND)
872 BT_ERR("bluesleep resuming...\n");
873 if ((bsi->uport != NULL) &&
874 (gpio_get_value(bsi->host_wake) == bsi->irq_polarity)) {
875 if (debug_mask & DEBUG_SUSPEND)
876 BT_ERR("bluesleep resume from BT event...\n");
877 // msm_hs_request_clock_on(bsi->uport); ???????????????????
878 // msm_hs_set_mctrl(bsi->uport, TIOCM_RTS);????????????????
880 clear_bit(BT_SUSPEND, &flags);
885 static int bluesleep_suspend(struct platform_device *pdev, pm_message_t state)
887 if (debug_mask & DEBUG_SUSPEND)
888 pr_info("bluesleep suspending...\n");
889 set_bit(BT_SUSPEND, &flags);
893 static struct of_device_id bluesleep_match_table[] = {
894 { .compatible = "broadcom,bluesleep" },
898 static struct platform_driver bluesleep_driver = {
899 .probe = bluesleep_probe,
900 .remove = bluesleep_remove,
901 .suspend = bluesleep_suspend,
902 .resume = bluesleep_resume,
905 .owner = THIS_MODULE,
906 .of_match_table = bluesleep_match_table,
911 static const struct file_operations lpm_proc_btwake_fops = {
912 .owner = THIS_MODULE,
913 .open = bluepower_open_proc_btwake,
915 .write = bluepower_write_proc_btwake,
916 .release = single_release,
920 static const struct file_operations lpm_proc_hostwake_fops = {
921 .owner = THIS_MODULE,
922 .open = bluepower_open_proc_hostwake,
924 .release = single_release,
927 static const struct file_operations lpm_proc_proto_fops = {
928 .owner = THIS_MODULE,
929 .open= bluesleep_open_proc_proto,
931 .write = bluesleep_write_proc_proto,
932 .release = single_release,
935 static const struct file_operations lpm_proc_asleep_fops = {
936 .owner = THIS_MODULE,
937 .open= bluesleep_open_proc_asleep,
939 // .read = bluesleep_read_proc_asleep,
940 .release = single_release,
943 static const struct file_operations lpm_proc_lpm_fops = {
944 .owner = THIS_MODULE,
945 .open= bluesleep_open_proc_lpm,
947 .write = bluesleep_write_proc_lpm,
948 .release = single_release,
951 static const struct file_operations lpm_proc_btwrite_fops = {
952 .owner = THIS_MODULE,
953 .open = bluesleep_open_proc_btwrite,
955 .write = bluesleep_write_proc_btwrite,
956 .release = single_release,
965 * Initializes the module.
966 * @return On success, 0. On error, -1, and <code>errno</code> is set
969 static int __init bluesleep_init(void)
972 struct proc_dir_entry *ent;
975 BT_SLEEP_DBG("BlueSleep Mode Driver Ver %s", VERSION);
976 /***************************************************************/
977 tasklet_init(&hostwake_task, bluesleep_hostwake_task, (unsigned long)bsi);
978 flags = 0; /* clear all status bits */
980 /* Initialize spinlock. */
981 spin_lock_init(&rw_lock);
983 /* Initialize timer */
984 init_timer(&tx_timer);
985 tx_timer.function = bluesleep_tx_timer_expire;
987 /*********************************************************************/
988 retval = platform_driver_register(&bluesleep_driver);
995 #if !BT_BLUEDROID_SUPPORT
996 bluesleep_hdev = NULL;
999 bluetooth_dir = proc_mkdir("bluetooth", NULL);
1000 if (bluetooth_dir == NULL) {
1001 BT_SLEEP_DBG("Unable to create /proc/bluetooth directory");
1005 sleep_dir = proc_mkdir("sleep", bluetooth_dir);
1006 if (sleep_dir == NULL) {
1007 BT_SLEEP_DBG("Unable to create /proc/%s directory", PROC_DIR);
1011 /* Creating read/write entry */
1012 ent=proc_create("btwake", S_IRUGO | S_IWUSR | S_IWGRP, sleep_dir,&lpm_proc_btwake_fops); /*read/write */
1013 ent=proc_create("hostwake", S_IRUGO, sleep_dir,&lpm_proc_hostwake_fops); /* only read */
1014 ent=proc_create("proto", S_IRUGO | S_IWUSR | S_IWGRP, sleep_dir,&lpm_proc_proto_fops); /* read/write */
1015 ent=proc_create("asleep", S_IRUGO, sleep_dir,&lpm_proc_asleep_fops); /* only read */
1016 #if BT_BLUEDROID_SUPPORT
1017 ent=proc_create("lpm", S_IRUGO | S_IWUSR | S_IWGRP, sleep_dir,&lpm_proc_lpm_fops); /* read/write*/
1018 ent=proc_create("btwrite", S_IRUGO | S_IWUSR | S_IWGRP, sleep_dir,&lpm_proc_btwrite_fops); /*read/write */
1020 BT_SLEEP_DBG("Unable to create /proc/%s/btwake entry", PROC_DIR);
1028 /* assert bt wake */
1029 if (bsi->has_ext_wake == 1)
1030 gpio_set_value(bsi->ext_wake, 0);
1031 clear_bit(BT_EXT_WAKE, &flags);
1032 BT_ERR("a=%d,b=%d\n",bsi->has_ext_wake,bsi->ext_wake);
1034 #if !BT_BLUEDROID_SUPPORT
1035 hci_register_notifier(&hci_event_nblock);
1041 #if BT_BLUEDROID_SUPPORT
1042 remove_proc_entry("btwrite", sleep_dir);
1043 remove_proc_entry("lpm", sleep_dir);
1045 remove_proc_entry("asleep", sleep_dir);
1046 remove_proc_entry("proto", sleep_dir);
1047 remove_proc_entry("hostwake", sleep_dir);
1048 remove_proc_entry("btwake", sleep_dir);
1049 remove_proc_entry("sleep", bluetooth_dir);
1050 remove_proc_entry("bluetooth", 0);
1055 * Cleans up the module.
1057 static void __exit bluesleep_exit(void)
1061 /* assert bt wake */
1062 if (bsi->has_ext_wake == 1)
1063 gpio_set_value(bsi->ext_wake, 0);
1064 clear_bit(BT_EXT_WAKE, &flags);
1065 if (test_bit(BT_PROTO, &flags)) {
1066 if (disable_irq_wake(bsi->host_wake_irq))
1067 BT_SLEEP_DBG("Couldn't disable hostwake IRQ wakeup mode \n");
1068 free_irq(bsi->host_wake_irq, NULL);
1069 del_timer(&tx_timer);
1070 if (test_bit(BT_ASLEEP, &flags))
1074 #if !BT_BLUEDROID_SUPPORT
1075 hci_unregister_notifier(&hci_event_nblock);
1077 platform_driver_unregister(&bluesleep_driver);
1078 #if BT_BLUEDROID_SUPPORT
1079 remove_proc_entry("btwrite", sleep_dir);
1080 remove_proc_entry("lpm", sleep_dir);
1082 remove_proc_entry("asleep", sleep_dir);
1083 remove_proc_entry("proto", sleep_dir);
1084 remove_proc_entry("hostwake", sleep_dir);
1085 remove_proc_entry("btwake", sleep_dir);
1086 remove_proc_entry("sleep", bluetooth_dir);
1087 remove_proc_entry("bluetooth", 0);
1088 // destroy_workqueue(hostwake_work_queue);
1091 module_init(bluesleep_init);
1092 module_exit(bluesleep_exit);
1094 MODULE_DESCRIPTION("Bluetooth Sleep Mode Driver ver %s " VERSION);
1095 #ifdef MODULE_LICENSE
1096 MODULE_LICENSE("GPL");