tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / bluetooth / brcm_lpm.c
1 /*
2
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.
6
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
10
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
14    for more details.
15
16
17    Copyright (C) 2006-2007 - Motorola
18    Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
19
20    Date         Author           Comment
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.
28 */
29
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>
43
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 */
57 #include "hci_uart.h"
58 #include <linux/seq_file.h>
59
60
61 //#define BT_SLEEP_DBG  pr_debug
62 #define BT_SLEEP_DBG BT_ERR
63 /*
64  * Defines
65  */
66
67 #define VERSION         "1.1"
68 #define PROC_DIR        "bluetooth/sleep"
69
70 #define POLARITY_LOW 0
71 #define POLARITY_HIGH 1
72
73 #define BT_PORT_ID      0
74
75 /* enable/disable wake-on-bluetooth */
76 #define BT_ENABLE_IRQ_WAKE 1
77
78 #define BT_BLUEDROID_SUPPORT 1
79
80 enum {
81         DEBUG_USER_STATE = 1U << 0,
82         DEBUG_SUSPEND = 1U << 1,
83         DEBUG_BTWAKE = 1U << 2,
84         DEBUG_VERBOSE = 1U << 3,
85 };
86 struct workqueue_struct *hostwake_work_queue;
87
88 static int debug_mask = DEBUG_USER_STATE;
89 module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
90
91 struct bluesleep_info {
92         unsigned host_wake;
93         unsigned ext_wake;
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;
99             int irq_polarity;
100             int has_ext_wake;
101 };
102
103 /* work function */
104 static void bluesleep_sleep_work(struct work_struct *work);
105
106 /* work queue */
107 DECLARE_DELAYED_WORK(sleep_workqueue, bluesleep_sleep_work);
108
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)
114
115 /* 1 second timeout */
116 #define TX_TIMER_INTERVAL      8   //after 10S  DEV pin change,the BT can sleep
117
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
124
125 #if BT_BLUEDROID_SUPPORT
126 static bool has_lpm_enabled = false;
127 #else
128 /* global pointer to a single hci device. */
129 static struct hci_dev *bluesleep_hdev;
130 #endif
131
132 static struct platform_device *bluesleep_uart_dev;
133 static struct bluesleep_info *bsi;
134 static bool is_bt_stoped=0;
135
136 /* module usage */
137 static atomic_t open_count = ATOMIC_INIT(1);
138
139 /*
140  * Local function prototypes
141  */
142 #if !BT_BLUEDROID_SUPPORT
143 static int bluesleep_hci_event(struct notifier_block *this,
144                         unsigned long event, void *data);
145 #endif
146 static int bluesleep_start(void);
147 static void bluesleep_stop(void);
148 extern struct uart_port * serial_get_uart_port(int uart_index);
149 /*
150  * Global variables
151  */
152
153 /** Global state flags */
154 static unsigned long flags;
155
156 /** Tasklet to respond to change in hostwake line */
157 static struct tasklet_struct hostwake_task;
158
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);
162
163 /** Lock for state transitions */
164 static spinlock_t rw_lock;
165
166 #if !BT_BLUEDROID_SUPPORT
167 /** Notifier block for HCI events */
168 struct notifier_block hci_event_nblock = {
169         .notifier_call = bluesleep_hci_event,
170 };
171 #endif
172
173 struct proc_dir_entry *bluetooth_dir, *sleep_dir;
174
175 /*
176  * Local functions
177  */
178
179 static void hsuart_power(int on)
180 {BT_ERR("hsuart_power");
181         if (test_bit(BT_SUSPEND, &flags))
182                 return;
183         if (on) {
184           //msm_hs_request_clock_on(bsi->uport);
185           //    msm_hs_set_mctrl(bsi->uport, TIOCM_RTS);
186         } else {
187           //    msm_hs_set_mctrl(bsi->uport, 0);
188           //    msm_hs_request_clock_off(bsi->uport);
189         }
190         BT_SLEEP_DBG("uart power %d\n", on);
191 }
192
193
194 /**
195  * @return 1 if the Host can go to sleep, 0 otherwise.
196  */
197 int bluesleep_can_sleep(void)
198 {
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)
203                               , bsi->uport);
204         return ((gpio_get_value(bsi->host_wake) != bsi->irq_polarity) &&
205                 (test_bit(BT_EXT_WAKE, &flags)) &&
206                 (bsi->uport != NULL));
207 }
208
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);
220                 /*Activating UART */
221                 hsuart_power(1);
222         }
223 }
224
225 /**
226  * @brief@  main sleep work handling function which update the flags
227  * and activate and deactivate UART ,check FIFO.
228  */
229 static void bluesleep_sleep_work(struct work_struct *work)
230 {
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");
237                         return;
238                 }
239
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 */
244                         hsuart_power(0);
245                         /* UART clk is not turned off immediately. Release
246                          * wakelock after 500 ms.
247                          */
248                        // wake_lock_timeout(&bsi->wake_lock, HZ / 2);
249                 } else {
250                         BT_SLEEP_DBG("tx buffer is not empty, modify timer...");
251                         /*lgh add*/
252                         /*lgh add end*/
253                         mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
254                         return;
255                 }
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);
262         }else {
263                 bluesleep_sleep_wakeup();
264         }
265 }
266
267 /**
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.
271  */
272 static void bluesleep_hostwake_task(unsigned long data)
273 {
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));
277
278         spin_lock(&rw_lock);
279         if ((gpio_get_value(bsi->host_wake) == bsi->irq_polarity))
280                 bluesleep_rx_busy();
281         else
282                 bluesleep_rx_idle();
283
284         spin_unlock(&rw_lock);
285 }
286
287 /**
288  * Handles proper timer action when outgoing data is delivered to the
289  * HCI line discipline. Sets BT_TXDATA.
290  */
291 static void bluesleep_outgoing_data(void)
292 {      
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);
297
298
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
302                 
303
304         /* log data passing by */
305         set_bit(BT_TXDATA, &flags);
306
307         spin_unlock_irqrestore(&rw_lock, irq_flags);
308         mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ)); //add
309         #if 0
310         /* if the tx side is sleeping... */
311         if (test_bit(BT_EXT_WAKE, &flags)) {
312
313                 BT_SLEEP_DBG("tx was sleeping");
314                 bluesleep_sleep_wakeup();
315         }
316         #endif
317 }
318
319 static ssize_t bluesleep_write_proc_lpm(struct file *file, const char __user *buffer,
320                                         size_t count, loff_t *pos)
321 {
322         char b;
323         BT_ERR("bluesleep_write_proc_lpm\n");
324
325         if (count < 1)
326                 return -EINVAL;
327
328         if (copy_from_user(&b, buffer, 1))
329                 return -EFAULT;
330    BT_ERR("bluesleep_write_proc_lpm_b=%d\n",b);
331         if (b == '0') {
332                 /* HCI_DEV_UNREG */
333                 bluesleep_stop();
334                 has_lpm_enabled = false;
335                 bsi->uport = NULL;
336         } else {
337                 /* HCI_DEV_REG */
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*/
342                         bluesleep_start();
343                 }
344         }
345
346         return count;
347 }
348
349
350 static int lpm_proc_show(struct seq_file * m,void * v)
351 {
352         //unsigned int lpm;
353         BT_ERR("bluesleep_read_proc_lpm\n");
354         seq_printf(m, "unsupported to read\n");
355         return 0;
356 }
357
358 static int bluesleep_open_proc_lpm(struct inode *inode, struct file *file)
359 {
360         return single_open(file, lpm_proc_show, PDE_DATA(inode));
361
362 }
363 static ssize_t bluesleep_write_proc_btwrite(struct file *file, const char __user *buffer,size_t count, loff_t *pos)
364 {                           
365         char b;
366         BT_ERR("bluesleep_write_proc_btwrite");
367
368         if (count < 1)
369                 return -EINVAL;
370         if (is_bt_stoped==1)
371                 return count;
372
373         if (copy_from_user(&b, buffer, 1))
374                 return -EFAULT;
375         BT_ERR("bluesleep_write_proc_btwrite=%d",b);
376
377         /* HCI_DEV_WRITE */
378         if (b != '0')
379                 bluesleep_outgoing_data();
380
381         return count;
382 }
383 static int btwrite_proc_show(struct seq_file * m,void * v)
384 {
385         //unsigned int btwrite;
386         BT_ERR("bluesleep_read_proc_lpm\n");
387         seq_printf(m, "unsupported to read\n");
388         return 0;
389 }
390
391 static int bluesleep_open_proc_btwrite(struct inode *inode, struct file *file)
392 {
393         return single_open(file, btwrite_proc_show, PDE_DATA(inode));
394
395 }
396
397 /**
398  * Handles transmission timer expiration.
399  * @param data Not used.
400  */
401 static void bluesleep_tx_timer_expire(unsigned long data)
402 {   
403         if(bsi->uport==NULL)
404                 return;
405         if (bsi->uport->ops->tx_empty(bsi->uport))
406         {  BT_ERR("empty");
407            gpio_set_value(bsi->ext_wake, 1);
408                    wake_unlock(&bsi->BT_wakelock);
409         }
410         else
411                 {
412                   BT_ERR("not  empty");
413                   mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL*HZ));
414                 }
415
416      //gpio_set_value(bsi->ext_wake, 1);
417 #if 0
418         unsigned long irq_flags;
419                 BT_ERR("bluesleep_tx_timer_expire");
420
421         spin_lock_irqsave(&rw_lock, irq_flags);
422
423         BT_SLEEP_DBG("Tx timer expired");
424
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);
430
431                 bluesleep_tx_idle();
432         } else {
433                 BT_SLEEP_DBG("Tx data during last period");
434                 mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL*HZ));
435         }
436
437         /* clear the incoming data flag */
438         clear_bit(BT_TXDATA, &flags);
439
440         spin_unlock_irqrestore(&rw_lock, irq_flags);
441 #endif
442 }
443
444 /**
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.
449  */
450 static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
451 {
452         /* schedule a tasklet to handle the change in the host wake line */
453
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);
459     if(host_wake== 0)
460         {
461           wake_lock(&bsi->host_wakelock);
462                   irq_set_irq_type(irq, IRQF_TRIGGER_HIGH);  
463
464         }
465         else
466                 {
467                   //wake_unlock(&bsi->host_wakelock);
468                   wake_lock_timeout(&bsi->host_wakelock, HZ*5);
469                   irq_set_irq_type(irq,IRQF_TRIGGER_LOW);
470                 }
471                 
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);
476         }
477         
478         return IRQ_HANDLED;
479 }
480
481 /**
482  * Starts the Sleep-Mode Protocol on the Host.
483  * @return On success, 0. On error, -1, and <code>errno</code> is set
484  * appropriately.
485  */
486 static int bluesleep_start(void)
487 {
488         int retval;
489         unsigned long irq_flags;
490         BT_ERR("bluesleep_start\n");
491                 is_bt_stoped=0;
492         spin_lock_irqsave(&rw_lock, irq_flags);
493
494         if (test_bit(BT_PROTO, &flags)) {
495                 spin_unlock_irqrestore(&rw_lock, irq_flags);
496                 return 0;
497         }
498
499         spin_unlock_irqrestore(&rw_lock, irq_flags);
500
501         if (!atomic_dec_and_test(&open_count)) {
502                 atomic_inc(&open_count);
503                 return -EBUSY;
504         }
505
506         /* start the timer */
507         mod_timer(&tx_timer, jiffies + (TX_TIMER_INTERVAL * HZ));
508
509         /* assert BT_WAKE */
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);
515         if (retval < 0) {
516                 BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
517                 goto fail;
518         }
519 #endif
520         set_bit(BT_PROTO, &flags);
521         //wake_lock(&bsi->wake_lock);
522         return 0;
523 fail:
524         del_timer(&tx_timer);
525         atomic_inc(&open_count);
526
527         return retval;
528 }
529
530 /**
531  * Stops the Sleep-Mode Protocol on the Host.
532  */
533 static void bluesleep_stop(void)
534 {
535         unsigned long irq_flags;
536                 BT_ERR("bluesleep_stop\n");
537
538         spin_lock_irqsave(&rw_lock, irq_flags);
539
540         if (!test_bit(BT_PROTO, &flags)) {
541                 spin_unlock_irqrestore(&rw_lock, irq_flags);
542                 return;
543         }
544
545         /* assert BT_WAKE */
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);
551
552         if (test_bit(BT_ASLEEP, &flags)) {
553                 clear_bit(BT_ASLEEP, &flags);
554                 spin_unlock_irqrestore(&rw_lock, irq_flags);
555                 hsuart_power(1);
556         } else {
557                 spin_unlock_irqrestore(&rw_lock, irq_flags);
558         }
559
560         atomic_inc(&open_count);
561
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");
565 #endif
566         wake_unlock(&bsi->host_wakelock);
567         wake_unlock(&bsi->BT_wakelock);
568         is_bt_stoped=1;
569      //  wake_lock_timeout(&bsi->wake_lock, HZ / 2);
570 }
571
572 /**
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.
580  */
581 static ssize_t bluepower_write_proc_btwake(struct file *file, const char __user *buffer,
582                                             size_t count, loff_t *pos)
583 {
584         char *buf;
585                 BT_ERR("bluepower_write_proc_btwake\n");
586
587         if (count < 1)
588               return -EINVAL;
589
590         buf = kmalloc(count, GFP_KERNEL);
591         if (!buf)
592               return -ENOMEM;
593
594         if (copy_from_user(buf, buffer, count)) {
595               kfree(buf);
596               return -EFAULT;
597         }
598
599         if (buf[0] == '0') {
600                 if (bsi->has_ext_wake == 1)
601               gpio_set_value(bsi->ext_wake, 0);
602                 clear_bit(BT_EXT_WAKE, &flags);
603         } 
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);
608         } else {
609               kfree(buf);
610               return -EINVAL;
611         }
612
613         kfree(buf);
614
615         return count;
616 }
617 static int btwake_proc_show(struct seq_file *m, void *v)
618 {
619         unsigned int btwake;
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);
623         return 0;
624 }
625
626 static int bluepower_open_proc_btwake(struct inode *inode, struct file *file)
627 {
628         return single_open(file, btwake_proc_show, PDE_DATA(inode));
629
630 }
631
632
633
634 static int hostwake_proc_show(struct seq_file *m, void *v)
635 {
636         unsigned int hostwake;
637         hostwake = gpio_get_value(bsi->host_wake);
638     seq_printf(m, "%u\n", hostwake);
639         return 0;
640 }
641
642 static int bluepower_open_proc_hostwake(struct inode *inode, struct file *file)
643 {
644         return single_open(file, hostwake_proc_show, PDE_DATA(inode));
645
646 }
647
648
649
650 static int bluesleep_proc_show(struct seq_file *m, void *v)
651 {
652 //      seq_printf(m, "%d\n", val);
653         unsigned int asleep;
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);
657 //      seq_puts()
658
659         return 0;
660 }
661
662 static int bluesleep_open_proc_asleep(struct inode *inode, struct file *file)
663 {
664         return single_open(file, bluesleep_proc_show, PDE_DATA(inode));
665
666 }
667
668 static ssize_t bluesleep_write_proc_proto(struct file *filp,const char __user *buff, size_t count, loff_t *pos)
669 {
670         char proto;
671                 BT_ERR("bluesleep_write_proc_proto\n");
672
673        if(count>0){
674         if (copy_from_user(&proto, buff, 1))    //or get_user(proto, buffer)
675             return -EFAULT;
676                 
677              BT_SLEEP_DBG("write proto %c", proto);
678
679         if (proto == '0')
680                 bluesleep_stop();
681         else
682                 bluesleep_start();
683         }
684
685         /* claim that we wrote everything */
686         return count; 
687 }
688
689
690 static int proto_proc_show(struct seq_file *m, void *v)
691 {
692         unsigned int proto;
693         proto = test_bit(BT_PROTO, &flags) ? 1 : 0;
694     seq_printf(m, "%u\n", proto);
695         return 0;
696 }
697
698 static int bluesleep_open_proc_proto(struct inode *inode, struct file *file)
699 {
700         return single_open(file, proto_proc_show, PDE_DATA(inode));
701
702 }
703
704
705
706 void bluesleep_setup_uart_port(struct platform_device *uart_dev)
707 {
708         bluesleep_uart_dev = uart_dev;
709 }
710
711 static int bluesleep_populate_dt_pinfo(struct platform_device *pdev)
712 {
713         struct device_node *np = pdev->dev.of_node;
714         int tmp;
715
716         tmp = of_get_named_gpio(np, "host-wake-gpio", 0);
717         if (tmp < 0) {
718                 BT_ERR("couldn't find host_wake gpio");
719                 return -ENODEV;
720         }
721         bsi->host_wake = tmp;
722
723         tmp = of_get_named_gpio(np, "bt-wake-gpio", 0);
724         if (tmp < 0)
725                 bsi->has_ext_wake = 0;
726         else
727                 bsi->has_ext_wake = 1;
728
729         if (bsi->has_ext_wake)
730                 bsi->ext_wake = tmp;
731
732         BT_INFO("bt_host_wake %d, bt-wake-gpio %d",
733                         bsi->host_wake,
734                         bsi->ext_wake);
735         return 0;
736 }
737
738 static int bluesleep_populate_pinfo(struct platform_device *pdev)
739 {
740         struct resource *res;
741
742         res = platform_get_resource_byname(pdev, IORESOURCE_IO,
743                                 "gpio_host_wake");
744         if (!res) {
745                 BT_ERR("couldn't find host_wake gpio");
746                 return -ENODEV;
747         }
748         bsi->host_wake = res->start;
749
750         res = platform_get_resource_byname(pdev, IORESOURCE_IO,
751                                 "gpio_ext_wake");
752         if (!res)
753                 bsi->has_ext_wake = 0;
754         else
755                 bsi->has_ext_wake = 1;
756
757         if (bsi->has_ext_wake)
758                 bsi->ext_wake = res->start;
759
760         return 0;
761 }
762
763 static int bluesleep_probe(struct platform_device *pdev)
764 {
765         //struct resource *res;
766         int ret;
767
768         bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
769         if (!bsi)
770                 return -ENOMEM;
771
772         if (pdev->dev.of_node) {
773                 ret = bluesleep_populate_dt_pinfo(pdev);
774                 if (ret < 0) {
775                         BT_ERR("couldn't populate info from dt");
776                         return ret;
777                 }
778         } else {
779                 ret = bluesleep_populate_pinfo(pdev);
780                 if (ret < 0) {
781                         BT_ERR("couldn't populate info");
782                         return ret;
783                 }
784         }
785
786         /* configure host_wake as input */
787         ret = gpio_request_one(bsi->host_wake, GPIOF_IN, "bt_host_wake");
788         if (ret < 0) {
789                 BT_ERR("failed to configure input"
790                                 " direction for GPIO %d, error %d",
791                                 bsi->host_wake, ret);
792                 goto free_bsi;
793         }
794
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");
801                 if (ret < 0) {
802                         BT_ERR("failed to configure output"
803                                 " direction for GPIO %d, error %d",
804                                   bsi->ext_wake, ret);
805                         goto free_bt_host_wake;
806                 }
807         }
808         clear_bit(BT_EXT_WAKE, &flags);
809
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");
816            ret = -ENODEV;
817            goto free_bt_ext_wake;
818                                 }
819
820
821         bsi->irq_polarity = POLARITY_LOW;/*low edge (falling edge)*/
822
823         //wake_lock_init(&bsi->wake_lock, WAKE_LOCK_SUSPEND, "bluesleep");
824         
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);
828
829         BT_INFO("host_wake_irq %d, polarity %d",
830                         bsi->host_wake_irq,
831                         bsi->irq_polarity);
832
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);
837
838         if (ret  < 0) {
839                 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
840                 goto free_bt_ext_wake;
841         }
842
843         return 0;
844
845 free_bt_ext_wake:
846         gpio_free(bsi->ext_wake);
847 free_bt_host_wake:
848         gpio_free(bsi->host_wake);
849 free_bsi:
850         kfree(bsi);
851         return ret;
852 }
853
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);
862       
863         kfree(bsi);
864         return 0;
865 }
866
867
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);????????????????
879                 }
880                 clear_bit(BT_SUSPEND, &flags);
881         }
882         return 0;
883 }
884
885 static int bluesleep_suspend(struct platform_device *pdev, pm_message_t state)
886 {
887         if (debug_mask & DEBUG_SUSPEND)
888                 pr_info("bluesleep suspending...\n");
889         set_bit(BT_SUSPEND, &flags);
890         return 0;
891 }
892
893 static struct of_device_id bluesleep_match_table[] = {
894         { .compatible = "broadcom,bluesleep" },
895         {}
896 };
897
898 static struct platform_driver bluesleep_driver = {
899         .probe = bluesleep_probe,
900         .remove = bluesleep_remove,
901         .suspend = bluesleep_suspend,
902         .resume = bluesleep_resume,
903         .driver = {
904                 .name = "bluesleep",
905                 .owner = THIS_MODULE,
906                 .of_match_table = bluesleep_match_table,
907         },
908 };
909
910
911 static const struct file_operations lpm_proc_btwake_fops = {
912         .owner = THIS_MODULE,
913         .open = bluepower_open_proc_btwake,
914         .read = seq_read,
915         .write = bluepower_write_proc_btwake,
916         .release = single_release,
917 };
918
919
920 static const struct file_operations lpm_proc_hostwake_fops = {
921                 .owner = THIS_MODULE,
922                 .open = bluepower_open_proc_hostwake,
923                 .read = seq_read,
924                 .release = single_release,
925         };
926
927 static const struct file_operations lpm_proc_proto_fops = {
928         .owner = THIS_MODULE,
929         .open= bluesleep_open_proc_proto,
930         .read=seq_read,
931         .write = bluesleep_write_proc_proto,
932         .release = single_release,
933 };
934
935 static const struct file_operations lpm_proc_asleep_fops = {
936         .owner = THIS_MODULE,
937         .open= bluesleep_open_proc_asleep,
938         .read= seq_read,
939 //      .read = bluesleep_read_proc_asleep,
940         .release = single_release,
941 };
942
943 static const struct file_operations lpm_proc_lpm_fops = {
944         .owner = THIS_MODULE,
945         .open= bluesleep_open_proc_lpm,
946         .read = seq_read,
947         .write = bluesleep_write_proc_lpm,
948         .release = single_release,
949 };
950
951 static const struct file_operations lpm_proc_btwrite_fops = {
952         .owner = THIS_MODULE,
953         .open = bluesleep_open_proc_btwrite,
954         .read = seq_read,
955         .write = bluesleep_write_proc_btwrite,
956         .release = single_release,
957 };
958
959
960
961
962
963
964 /**
965  * Initializes the module.
966  * @return On success, 0. On error, -1, and <code>errno</code> is set
967  * appropriately.
968  */
969 static int __init bluesleep_init(void)
970 {
971         int retval;
972         struct proc_dir_entry *ent;
973 //          char name[64];
974
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 */
979
980         /* Initialize spinlock. */
981         spin_lock_init(&rw_lock);
982
983         /* Initialize timer */
984         init_timer(&tx_timer);
985         tx_timer.function = bluesleep_tx_timer_expire;
986         tx_timer.data = 0;
987 /*********************************************************************/
988         retval = platform_driver_register(&bluesleep_driver);
989         if (retval)
990             return retval;
991
992         if (bsi == NULL)
993                 return 0;
994
995 #if !BT_BLUEDROID_SUPPORT
996         bluesleep_hdev = NULL;
997 #endif
998
999         bluetooth_dir = proc_mkdir("bluetooth", NULL);
1000         if (bluetooth_dir == NULL) {
1001                 BT_SLEEP_DBG("Unable to create /proc/bluetooth directory");
1002                 return -ENOMEM;
1003         }
1004
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);
1008                 return -ENOMEM;
1009         }
1010
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 */
1019                 if (ent == NULL) {
1020                     BT_SLEEP_DBG("Unable to create /proc/%s/btwake entry", PROC_DIR);
1021                         retval = -ENOMEM;
1022                     goto fail;
1023                           }
1024
1025 #endif
1026
1027
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);
1033         
1034 #if !BT_BLUEDROID_SUPPORT
1035         hci_register_notifier(&hci_event_nblock);
1036 #endif
1037
1038         return 0;
1039
1040 fail:
1041 #if BT_BLUEDROID_SUPPORT
1042         remove_proc_entry("btwrite", sleep_dir);
1043         remove_proc_entry("lpm", sleep_dir);
1044 #endif
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);
1051         return retval;
1052 }
1053
1054 /**
1055  * Cleans up the module.
1056  */
1057 static void __exit bluesleep_exit(void)
1058 {
1059         if (bsi == NULL)
1060                 return;
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))
1071                         hsuart_power(1);
1072         }
1073
1074 #if !BT_BLUEDROID_SUPPORT
1075         hci_unregister_notifier(&hci_event_nblock);
1076 #endif
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);
1081 #endif
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);
1089 }
1090
1091 module_init(bluesleep_init);
1092 module_exit(bluesleep_exit);
1093
1094 MODULE_DESCRIPTION("Bluetooth Sleep Mode Driver ver %s " VERSION);
1095 #ifdef MODULE_LICENSE
1096 MODULE_LICENSE("GPL");
1097 #endif
1098