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 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
10 * for more details.
11 *
12 *
13 * Copyright (C) 2006-2007 - Motorola
14 * Copyright (c) 2008-2010, Code Aurora Forum. All rights reserved.
15 *
16 * Date Author Comment
17 * ----------- -------------- --------------------------------
18 * 2006-Apr-28 Motorola The kernel module for running the Bluetooth(R)
19 * Sleep-Mode Protocol from the Host side
20 * 2006-Sep-08 Motorola Added workqueue for handling sleep work.
21 * 2007-Jan-24 Motorola Added mbm_handle_ioi() call to ISR.
22 *
23 */
24
25 #define DEBUG
26
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/notifier.h>
33 #include <linux/proc_fs.h>
34 #include <linux/spinlock.h>
35 #include <linux/timer.h>
36 #include <linux/uaccess.h>
37 #include <linux/version.h>
38 #include <linux/workqueue.h>
39 #include <linux/platform_device.h>
40
41 #include <linux/irq.h>
42 #include <linux/param.h>
43 #include <linux/bitops.h>
44 #include <linux/termios.h>
45 #include <linux/gpio.h>
46 #include <linux/of_gpio.h>
47 #include <linux/of_platform.h>
48 #include <linux/pm_wakeirq.h>
49
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <linux/serial_core.h>
53
54 /*
55 * #define BT_SLEEP_DBG
56 */
57 #undef BT_DBG
58 #undef BT_ERR
59 #ifdef BT_SLEEP_DBG
60 #define BT_DBG(fmt, arg...) pr_debug("[BT_LPM] %s: " fmt "\n",\
61 __func__, ## arg)
62 #else
63 #define BT_DBG(fmt, arg...)
64 #endif
65 #define BT_ERR(fmt, arg...) pr_err("[BT_LPM] %s: " fmt "\n",\
66 __func__, ## arg)
67
68 /*
69 * Defines
70 */
71
72 #define VERSION "1.3.3"
73 #define PROC_DIR "bluetooth/sleep"
74
75 #define DEFAULT_UART_INDEX 1
76 #define BT_BLUEDROID_SUPPORT 1
77 static int bluesleep_start(void);
78 static void bluesleep_stop(void);
79
80 struct bluesleep_info {
81 unsigned int wakeup_enable;
82 unsigned host_wake;
83 unsigned ext_wake;
84 unsigned host_wake_irq;
85 struct wakeup_source *ws;
86 struct uart_port *uport;
87 unsigned host_wake_assert:1;
88 unsigned ext_wake_assert:1;
89 struct platform_device *pdev;
90 };
91
92 /* work function */
93 static void bluesleep_sleep_work(struct work_struct *work);
94 static void bluesleep_tx_allow_sleep(void);
95
96 /* work queue */
97 DECLARE_DELAYED_WORK(sleep_workqueue, bluesleep_sleep_work);
98
99 /* Macros for handling sleep work */
100 #define bluesleep_rx_busy() schedule_delayed_work(&sleep_workqueue, 0)
101 #define bluesleep_tx_busy() schedule_delayed_work(&sleep_workqueue, 0)
102 #define bluesleep_rx_idle() schedule_delayed_work(&sleep_workqueue, 0)
103 #define bluesleep_tx_idle() schedule_delayed_work(&sleep_workqueue, 0)
104
105 /* 1 second timeout */
106 #define RX_TIMER_INTERVAL 1
107
108 /* state variable names and bit positions */
109 #define BT_PROTO 0x01
110 #define BT_TXDATA 0x02
111 #define BT_ASLEEP 0x04
112 #define BT_RXTIMER 0x20
113
114 #if BT_BLUEDROID_SUPPORT
115 static bool has_lpm_enabled;
116 #else
117 /* global pointer to a single hci device. */
118 static struct hci_dev *bluesleep_hdev;
119 #endif
120
121 #if BT_BLUEDROID_SUPPORT
122 static struct platform_device *bluesleep_uart_dev;
123 #endif
124 static struct bluesleep_info *bsi;
125
126 /* module usage */
127 static atomic_t open_count = ATOMIC_INIT(1);
128
129 /*
130 * Local function prototypes
131 */
132
133 #if !BT_BLUEDROID_SUPPORT
134 static int bluesleep_hci_event(struct notifier_block *this,
135 unsigned long event, void *data);
136 #endif
137
138 /*
139 * Global variables
140 */
141
142 /** Global state flags */
143 static unsigned long flags;
144
145 /** Tasklet to respond to change in hostwake line */
146 static struct tasklet_struct hostwake_task;
147
148 /** Reception timer */
149 static void bluesleep_rx_timer_expire(struct timer_list *t);
150 static struct timer_list rx_timer;
151
152 /** Lock for state transitions */
153 static spinlock_t rw_lock;
154
155 #if !BT_BLUEDROID_SUPPORT
156 /** Notifier block for HCI events */
157 struct notifier_block hci_event_nblock = {
158 .notifier_call = bluesleep_hci_event,
159 };
160 #endif
161
162 struct proc_dir_entry *bluetooth_dir, *sleep_dir;
163
164 /*
165 * Local functions
166 */
167
168 /*
169 * bt go to sleep will call this function tell uart stop data interactive
170 */
hsuart_power(int on)171 static void hsuart_power(int on)
172 {
173 if (bsi->uport != NULL) {
174 if (on)
175 bsi->uport->ops->set_mctrl(bsi->uport, TIOCM_RTS);
176 else
177 bsi->uport->ops->set_mctrl(bsi->uport, 0);
178 } else {
179 BT_ERR("bsi->uport = NULL, has_lpm_enabled = %d", has_lpm_enabled);
180 }
181 }
182
183 /**
184 * @return 1 if the Host can go to sleep, 0 otherwise.
185 */
bluesleep_can_sleep(void)186 static inline int bluesleep_can_sleep(void)
187 {
188 /* check if HOST_WAKE_BT_GPIO and BT_WAKE_HOST_GPIO
189 * are both deasserted
190 */
191 return (gpio_get_value(bsi->ext_wake) != bsi->ext_wake_assert) &&
192 (gpio_get_value(bsi->host_wake) != bsi->host_wake_assert) &&
193 (!test_bit(BT_RXTIMER, &flags)) && (bsi->uport != NULL);
194 }
195
196 /**
197 * @brief@ main sleep work handling function which update the flags
198 * and activate and deactivate UART ,check FIFO.
199 */
bluesleep_sleep_work(struct work_struct * work)200 static void bluesleep_sleep_work(struct work_struct *work)
201 {
202 if (bluesleep_can_sleep()) {
203 /* already asleep, this is an error case */
204 if (test_bit(BT_ASLEEP, &flags)) {
205 BT_DBG("already asleep");
206 return;
207 }
208 if (bsi->uport->ops->tx_empty(bsi->uport)) {
209 BT_DBG("going to sleep...");
210 set_bit(BT_ASLEEP, &flags);
211 /*Deactivating UART */
212 hsuart_power(0);
213 __pm_wakeup_event(bsi->ws, HZ / 2);
214 } else {
215 BT_DBG("This should never happen.\n");
216 return;
217 }
218 } else if (test_bit(BT_ASLEEP, &flags)) {
219 __pm_stay_awake(bsi->ws);
220 clear_bit(BT_ASLEEP, &flags);
221
222 /* Add a timer to make sure that UART
223 * would not be turned on&off very frequentently
224 */
225 mod_timer(&rx_timer, jiffies + (RX_TIMER_INTERVAL * HZ));
226
227 set_bit(BT_RXTIMER, &flags);
228 hsuart_power(1);
229 } else {
230 BT_DBG("do nothing\n");
231 }
232 }
233
234 /**
235 * A tasklet function that runs in tasklet context and reads the value
236 * of the HOST_WAKE GPIO pin and further defer the work.
237 * @param data Not used.
238 */
bluesleep_hostwake_task(unsigned long data)239 static void bluesleep_hostwake_task(unsigned long data)
240 {
241 BT_DBG("hostwake line change");
242 spin_lock(&rw_lock);
243
244 if (gpio_get_value(bsi->host_wake) == bsi->host_wake_assert)
245 bluesleep_rx_busy();
246 else
247 bluesleep_rx_idle();
248
249 spin_unlock(&rw_lock);
250 }
251
252 /**
253 * Handles proper timer action when outgoing data is delivered to the
254 * HCI line discipline. Sets BT_TXDATA.
255 */
bluesleep_outgoing_data(void)256 static void bluesleep_outgoing_data(void)
257 {
258 unsigned long irq_flags;
259 int power_on_uart = 0;
260
261 spin_lock_irqsave(&rw_lock, irq_flags);
262
263 /* if the tx side is sleeping... */
264 if (gpio_get_value(bsi->ext_wake) != bsi->ext_wake_assert) {
265 BT_DBG("tx was sleeping, wakeup it");
266 __pm_stay_awake(bsi->ws);
267 gpio_set_value(bsi->ext_wake, bsi->ext_wake_assert);
268 clear_bit(BT_ASLEEP, &flags);
269 power_on_uart = 1;
270 }
271
272 spin_unlock_irqrestore(&rw_lock, irq_flags);
273 if (power_on_uart == 1)
274 hsuart_power(1);
275 }
276
277 #if BT_BLUEDROID_SUPPORT
bluesleep_get_uart_port(void)278 static struct uart_port *bluesleep_get_uart_port(void)
279 {
280 struct uart_port *uport = NULL;
281
282 if (bluesleep_uart_dev) {
283 uport = platform_get_drvdata(bluesleep_uart_dev);
284 if (uport)
285 BT_DBG(
286 "%s get uart_port from blusleep_uart_dev: %s, port irq: %d",
287 __func__, bluesleep_uart_dev->name, uport->irq);
288 }
289 return uport;
290 }
291
bluesleep_lpm_proc_show(struct seq_file * m,void * v)292 static int bluesleep_lpm_proc_show(struct seq_file *m, void *v)
293 {
294 seq_printf(m, "lpm enable: %d\n", has_lpm_enabled);
295 return 0;
296 }
297
bluesleep_lpm_proc_open(struct inode * inode,struct file * file)298 static int bluesleep_lpm_proc_open(struct inode *inode, struct file *file)
299 {
300 return single_open(file, bluesleep_lpm_proc_show, NULL);
301 }
302
bluesleep_write_proc_lpm(struct file * file,const char __user * buffer,size_t count,loff_t * pos)303 static ssize_t bluesleep_write_proc_lpm(struct file *file,
304 const char __user *buffer,
305 size_t count, loff_t *pos)
306 {
307 char b;
308
309 if (count < 1)
310 return -EINVAL;
311
312 if (copy_from_user(&b, buffer, 1))
313 return -EFAULT;
314
315 if (b == '0') {
316 /* HCI_DEV_UNREG */
317 bluesleep_stop();
318 has_lpm_enabled = false;
319 bsi->uport = NULL;
320 } else {
321 /* HCI_DEV_REG */
322 if (!has_lpm_enabled) {
323 has_lpm_enabled = true;
324 if (bluesleep_uart_dev)
325 bsi->uport = bluesleep_get_uart_port();
326
327 /* if bluetooth started, start bluesleep*/
328 bluesleep_start();
329 }
330 }
331
332 return count;
333 }
334
bluesleep_btwrite_proc_show(struct seq_file * m,void * v)335 static int bluesleep_btwrite_proc_show(struct seq_file *m, void *v)
336 {
337 seq_puts(m, "it's not support\n");
338 return 0;
339 }
340
bluesleep_btwrite_proc_open(struct inode * inode,struct file * file)341 static int bluesleep_btwrite_proc_open(struct inode *inode, struct file *file)
342 {
343 return single_open(file, bluesleep_btwrite_proc_show, NULL);
344 }
345
bluesleep_write_proc_btwrite(struct file * file,const char __user * buffer,size_t count,loff_t * pos)346 static ssize_t bluesleep_write_proc_btwrite(struct file *file,
347 const char __user *buffer,
348 size_t count, loff_t *pos)
349 {
350 char b;
351
352 if (count < 1)
353 return -EINVAL;
354
355 if (copy_from_user(&b, buffer, 1))
356 return -EFAULT;
357
358 /* HCI_DEV_WRITE */
359 if (b != '0')
360 bluesleep_outgoing_data();
361 else
362 bluesleep_tx_allow_sleep();
363
364 return count;
365 }
366
367 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
368 static const struct proc_ops lpm_fops = {
369 .proc_open = bluesleep_lpm_proc_open,
370 .proc_read = seq_read,
371 .proc_lseek = seq_lseek,
372 .proc_release = single_release,
373 .proc_write = bluesleep_write_proc_lpm,
374 };
375
376 static const struct proc_ops btwrite_fops = {
377 .proc_open = bluesleep_btwrite_proc_open,
378 .proc_read = seq_read,
379 .proc_lseek = seq_lseek,
380 .proc_release = single_release,
381 .proc_write = bluesleep_write_proc_btwrite,
382 };
383
384 #else
385
386 static const struct file_operations lpm_fops = {
387 .owner = THIS_MODULE,
388 .open = bluesleep_lpm_proc_open,
389 .read = seq_read,
390 .llseek = seq_lseek,
391 .release = single_release,
392 .write = bluesleep_write_proc_lpm,
393 };
394 static const struct file_operations btwrite_fops = {
395 .owner = THIS_MODULE,
396 .open = bluesleep_btwrite_proc_open,
397 .read = seq_read,
398 .llseek = seq_lseek,
399 .release = single_release,
400 .write = bluesleep_write_proc_btwrite,
401 };
402 #endif
403
404 #else
405 /**
406 * Handles HCI device events.
407 * @param this Not used.
408 * @param event The event that occurred.
409 * @param data The HCI device associated with the event.
410 * @return <code>NOTIFY_DONE</code>.
411 */
bluesleep_hci_event(struct notifier_block * this,unsigned long event,void * data)412 static int bluesleep_hci_event(struct notifier_block *this,
413 unsigned long event, void *data)
414 {
415 struct hci_dev *hdev = (struct hci_dev *) data;
416 struct hci_uart *hu;
417 struct uart_state *state;
418
419 if (!hdev)
420 return NOTIFY_DONE;
421
422 switch (event) {
423 case HCI_DEV_REG:
424 if (!bluesleep_hdev) {
425 bluesleep_hdev = hdev;
426 hu = (struct hci_uart *) hdev->driver_data;
427 state = (struct uart_state *) hu->tty->driver_data;
428 bsi->uport = state->uart_port;
429 }
430 break;
431 case HCI_DEV_UNREG:
432 bluesleep_hdev = NULL;
433 bsi->uport = NULL;
434 break;
435 case HCI_DEV_WRITE:
436 bluesleep_outgoing_data();
437 break;
438 }
439
440 return NOTIFY_DONE;
441 }
442 #endif
443
444 /**
445 * Function to check wheather bluetooth can sleep when btwrite was deasserted
446 * by bluedroid.
447 */
bluesleep_tx_allow_sleep(void)448 static void bluesleep_tx_allow_sleep(void)
449 {
450 unsigned long irq_flags;
451 BT_DBG("Tx has been idle\n");
452 spin_lock_irqsave(&rw_lock, irq_flags);
453 gpio_set_value(bsi->ext_wake, !bsi->ext_wake_assert);
454 bluesleep_tx_idle();
455 spin_unlock_irqrestore(&rw_lock, irq_flags);
456 }
457
458
459 /* Handles reception timer expiration.
460 * Clear BT_RXTIMER.
461 * @param data Not used.
462 */
bluesleep_rx_timer_expire(struct timer_list * t)463 static void bluesleep_rx_timer_expire(struct timer_list *t)
464 {
465 BT_DBG("bluesleep_rx_timer_expire");
466 clear_bit(BT_RXTIMER, &flags);
467 bluesleep_rx_idle();
468 }
469
470 /**
471 * Schedules a tasklet to run when receiving an interrupt on the
472 * <code>HOST_WAKE</code> GPIO pin.
473 * @param irq Not used.
474 * @param dev_id Not used.
475 */
bluesleep_hostwake_isr(int irq,void * dev_id)476 static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
477 {
478 /* schedule a tasklet to handle the change in the host wake line */
479 tasklet_schedule(&hostwake_task);
480 return IRQ_HANDLED;
481 }
482
483 /**
484 * Starts the Sleep-Mode Protocol on the Host.
485 * @return On success, 0. On error, -1, and <code>errno</code> is set
486 * appropriately.
487 */
bluesleep_start(void)488 static int bluesleep_start(void)
489 {
490 int retval;
491 unsigned long irq_flags;
492
493 spin_lock_irqsave(&rw_lock, irq_flags);
494
495 if (test_bit(BT_PROTO, &flags)) {
496 spin_unlock_irqrestore(&rw_lock, irq_flags);
497 return 0;
498 }
499
500 spin_unlock_irqrestore(&rw_lock, irq_flags);
501
502 if (!atomic_dec_and_test(&open_count)) {
503 atomic_inc(&open_count);
504 return -EBUSY;
505 }
506
507 /* start the timer */
508 mod_timer(&rx_timer, jiffies + (RX_TIMER_INTERVAL*HZ));
509
510 /* assert BT_WAKE */
511 gpio_set_value(bsi->ext_wake, bsi->ext_wake_assert);
512 retval = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
513 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
514 "bluetooth hostwake", &bsi->pdev->dev);
515 if (retval < 0) {
516 BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
517 goto fail;
518 }
519
520 set_bit(BT_PROTO, &flags);
521 __pm_stay_awake(bsi->ws);
522
523 return 0;
524 fail:
525 del_timer(&rx_timer);
526 atomic_inc(&open_count);
527
528 return retval;
529 }
530
531 /**
532 * Stops the Sleep-Mode Protocol on the Host.
533 */
bluesleep_stop(void)534 static void bluesleep_stop(void)
535 {
536 unsigned long irq_flags;
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 gpio_set_value(bsi->ext_wake, bsi->ext_wake_assert);
547
548 del_timer(&rx_timer);
549 clear_bit(BT_PROTO, &flags);
550
551 if (test_bit(BT_ASLEEP, &flags)) {
552 clear_bit(BT_ASLEEP, &flags);
553 hsuart_power(1);
554 }
555
556 atomic_inc(&open_count);
557
558 spin_unlock_irqrestore(&rw_lock, irq_flags);
559 free_irq(bsi->host_wake_irq, &bsi->pdev->dev);
560 __pm_wakeup_event(bsi->ws, HZ / 2);
561 }
562 #if 0
563 /**
564 * Read the <code>BT_WAKE</code> GPIO pin value via the proc interface.
565 * When this function returns, <code>page</code> will contain a 1 if the
566 * pin is high, 0 otherwise.
567 * @param page Buffer for writing data.
568 * @param start Not used.
569 * @param offset Not used.
570 * @param count Not used.
571 * @param eof Whether or not there is more data to be read.
572 * @param data Not used.
573 * @return The number of bytes written.
574 */
575 static int bluepower_read_proc_btwake(char *page, char **start, off_t offset,
576 int count, int *eof, void *data)
577 {
578 *eof = 1;
579 return sprintf(page, "btwake:%u\n",
580 (gpio_get_value(bsi->ext_wake) == bsi->ext_wake_assert));
581 }
582
583 /**
584 * Write the <code>BT_WAKE</code> GPIO pin value via the proc interface.
585 * @param file Not used.
586 * @param buffer The buffer to read from.
587 * @param count The number of bytes to be written.
588 * @param data Not used.
589 * @return On success, the number of bytes written. On error, -1, and
590 * <code>errno</code> is set appropriately.
591 */
592 static int bluepower_write_proc_btwake(struct file *file, const char *buffer,
593 unsigned long count, void *data)
594 {
595 char *buf;
596
597 if (count < 1)
598 return -EINVAL;
599
600 buf = kmalloc(count, GFP_KERNEL);
601 if (!buf)
602 return -ENOMEM;
603
604 if (copy_from_user(buf, buffer, count)) {
605 kfree(buf);
606 return -EFAULT;
607 }
608
609 if (buf[0] == '0') {
610 gpio_set_value(bsi->ext_wake, !bsi->ext_wake_assert);
611 } else if (buf[0] == '1') {
612 gpio_set_value(bsi->ext_wake, bsi->ext_wake_assert);
613 } else {
614 kfree(buf);
615 return -EINVAL;
616 }
617
618 kfree(buf);
619 return count;
620 }
621
622 /**
623 * Read the <code>BT_HOST_WAKE</code> GPIO pin value via the proc interface.
624 * When this function returns, <code>page</code> will contain a 1 if the pin
625 * is high, 0 otherwise.
626 * @param page Buffer for writing data.
627 * @param start Not used.
628 * @param offset Not used.
629 * @param count Not used.
630 * @param eof Whether or not there is more data to be read.
631 * @param data Not used.
632 * @return The number of bytes written.
633 */
634 static int bluepower_read_proc_hostwake(char *page, char **start, off_t offset,
635 int count, int *eof, void *data)
636 {
637 *eof = 1;
638 return sprintf(page, "hostwake: %u\n",
639 (gpio_get_value(bsi->host_wake) == bsi->host_wake_assert));
640 }
641
642
643 /**
644 * Read the low-power status of the Host via the proc interface.
645 * When this function returns, <code>page</code> contains a 1 if the Host
646 * is asleep, 0 otherwise.
647 * @param page Buffer for writing data.
648 * @param start Not used.
649 * @param offset Not used.
650 * @param count Not used.
651 * @param eof Whether or not there is more data to be read.
652 * @param data Not used.
653 * @return The number of bytes written.
654 */
655 static int bluesleep_read_proc_asleep(char *page, char **start, off_t offset,
656 int count, int *eof, void *data)
657 {
658 unsigned int asleep;
659
660 asleep = test_bit(BT_ASLEEP, &flags) ? 1 : 0;
661 *eof = 1;
662 return sprintf(page, "asleep: %u\n", asleep);
663 }
664
665 /**
666 * Read the low-power protocol being used by the Host via the proc interface.
667 * When this function returns, <code>page</code> will contain a 1 if the Host
668 * is using the Sleep Mode Protocol, 0 otherwise.
669 * @param page Buffer for writing data.
670 * @param start Not used.
671 * @param offset Not used.
672 * @param count Not used.
673 * @param eof Whether or not there is more data to be read.
674 * @param data Not used.
675 * @return The number of bytes written.
676 */
677 static int bluesleep_read_proc_proto(char *page, char **start, off_t offset,
678 int count, int *eof, void *data)
679 {
680 unsigned int proto;
681
682 proto = test_bit(BT_PROTO, &flags) ? 1 : 0;
683 *eof = 1;
684 return sprintf(page, "proto: %u\n", proto);
685 }
686
687 /**
688 * Modify the low-power protocol used by the Host via the proc interface.
689 * @param file Not used.
690 * @param buffer The buffer to read from.
691 * @param count The number of bytes to be written.
692 * @param data Not used.
693 * @return On success, the number of bytes written. On error, -1, and
694 * <code>errno</code> is set appropriately.
695 */
696 static int bluesleep_write_proc_proto(struct file *file, const char *buffer,
697 unsigned long count, void *data)
698 {
699 char proto;
700
701 if (count < 1)
702 return -EINVAL;
703
704 if (copy_from_user(&proto, buffer, 1))
705 return -EFAULT;
706
707 if (proto == '0')
708 bluesleep_stop();
709 else
710 bluesleep_start();
711
712 /* claim that we wrote everything */
713 return count;
714 }
715 #endif
716
717 static int assert_level = -1;
718 module_param(assert_level, int, S_IRUGO);
719 MODULE_PARM_DESC(assert_level, "BT_LPM hostwake/btwake assert level");
720
sw_uart_get_pdev(int id)721 static struct platform_device *sw_uart_get_pdev(int id)
722 {
723 struct device_node *np;
724 char match[20];
725 sprintf(match, "uart%d", id);
726 np = of_find_node_by_type(NULL, match);
727 return of_find_device_by_node(np);
728 }
729
bluesleep_probe(struct platform_device * pdev)730 static int __init bluesleep_probe(struct platform_device *pdev)
731 {
732 struct device_node *np = pdev->dev.of_node;
733 struct device *dev = &pdev->dev;
734 enum of_gpio_flags config;
735 int ret, uart_index;
736 u32 val;
737
738 bsi = devm_kzalloc(&pdev->dev, sizeof(struct bluesleep_info),
739 GFP_KERNEL);
740 if (!bsi)
741 return -ENOMEM;
742
743 bsi->host_wake = of_get_named_gpio_flags(np, "bt_hostwake", 0, &config);
744 if (!gpio_is_valid(bsi->host_wake)) {
745 BT_ERR("get gpio bt_hostwake failed\n");
746 return -EINVAL;
747 }
748
749 /* set host_wake_assert */
750 bsi->host_wake_assert = (config == OF_GPIO_ACTIVE_LOW) ? 0 : 1;
751 BT_DBG("bt_hostwake gpio=%d assert=%d\n", bsi->host_wake, bsi->host_wake_assert);
752
753 if (assert_level != -1) {
754 bsi->host_wake_assert = (assert_level & 0x02) > 0;
755 BT_DBG("override host_wake assert to %d", bsi->host_wake_assert);
756 }
757
758 ret = devm_gpio_request(dev, bsi->host_wake, "bt_hostwake");
759 if (ret < 0) {
760 BT_ERR("can't request bt_hostwake gpio %d\n",
761 bsi->host_wake);
762 return ret;
763 }
764 ret = gpio_direction_input(bsi->host_wake);
765 if (ret < 0) {
766 BT_ERR("can't request input direction bt_wake gpio %d\n",
767 bsi->host_wake);
768 return ret;
769 }
770
771 if (!of_property_read_bool(np, "wakeup-source")) {
772 BT_DBG("wakeup source is disabled!\n");
773 } else {
774 ret = device_init_wakeup(dev, true);
775 if (ret < 0) {
776 BT_ERR("device init wakeup failed!\n");
777 return ret;
778 }
779 ret = dev_pm_set_wake_irq(dev, gpio_to_irq(bsi->host_wake));
780 if (ret < 0) {
781 BT_ERR("can't enable wakeup src for bt_hostwake %d\n",
782 bsi->host_wake);
783 return ret;
784 }
785 bsi->wakeup_enable = 1;
786 }
787
788 bsi->ext_wake = of_get_named_gpio_flags(np, "bt_wake", 0, &config);
789 if (!gpio_is_valid(bsi->ext_wake)) {
790 BT_ERR("get gpio bt_wake failed\n");
791 return -EINVAL;
792 }
793
794 ret = devm_gpio_request(dev, bsi->ext_wake, "bt_wake");
795 if (ret < 0) {
796 BT_ERR("can't request bt_wake gpio %d\n",
797 bsi->ext_wake);
798 return ret;
799 }
800
801 /* set ext_wake_assert */
802 bsi->ext_wake_assert = (config == OF_GPIO_ACTIVE_LOW) ? 0 : 1;
803 BT_DBG("bt_wake gpio=%d assert=%d\n", bsi->ext_wake, bsi->ext_wake_assert);
804
805 if (assert_level != -1) {
806 bsi->ext_wake_assert = (assert_level & 0x01) > 0;
807 BT_DBG("override ext_wake assert to %d", bsi->ext_wake_assert);
808 }
809
810 /* 1.set bt_wake as output and the level is assert, assert bt wake */
811 ret = gpio_direction_output(bsi->ext_wake, bsi->ext_wake_assert);
812 if (ret < 0) {
813 BT_ERR("can't request output direction bt_wake gpio %d\n",
814 bsi->ext_wake);
815 return ret;
816 }
817
818 /* 2.get bt_host_wake gpio irq */
819 bsi->host_wake_irq = gpio_to_irq(bsi->host_wake);
820 if (bsi->host_wake_irq < 0) {
821 BT_ERR("map gpio [%d] to virq failed, errno = %d\n",
822 bsi->host_wake, bsi->host_wake_irq);
823 ret = -ENODEV;
824 return ret;
825 }
826
827 uart_index = DEFAULT_UART_INDEX;
828 if (!of_property_read_u32(np, "uart_index", &val)) {
829 switch (val) {
830 case 0:
831 case 1:
832 case 2:
833 uart_index = val;
834 break;
835 default:
836 BT_ERR("unsupported uart_index (%u)\n", val);
837 }
838 }
839 BT_DBG("uart_index (%u)\n", uart_index);
840 bluesleep_uart_dev = sw_uart_get_pdev(uart_index);
841
842 bsi->ws = wakeup_source_register(dev, "bluesleep");
843 bsi->pdev = pdev;
844 return 0;
845 }
846
bluesleep_remove(struct platform_device * pdev)847 static int bluesleep_remove(struct platform_device *pdev)
848 {
849 /* assert bt wake */
850 gpio_set_value(bsi->ext_wake, bsi->ext_wake_assert);
851 if (test_bit(BT_PROTO, &flags)) {
852 if (disable_irq_wake(bsi->host_wake_irq))
853 BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
854 free_irq(bsi->host_wake_irq, &bsi->pdev->dev);
855 del_timer(&rx_timer);
856 if (test_bit(BT_ASLEEP, &flags))
857 hsuart_power(1);
858 }
859
860 wakeup_source_unregister(bsi->ws);
861 if (bsi->wakeup_enable) {
862 BT_DBG("Deinit wakeup source");
863 device_init_wakeup(&pdev->dev, false);
864 dev_pm_clear_wake_irq(&pdev->dev);
865 }
866
867 return 0;
868 }
869
870 static const struct of_device_id sunxi_btlpm_ids[] = {
871 { .compatible = "allwinner,sunxi-btlpm" },
872 { /* Sentinel */ }
873 };
874
875 static struct platform_driver bluesleep_driver = {
876 .remove = bluesleep_remove,
877 .driver = {
878 .owner = THIS_MODULE,
879 .name = "sunxi-btlpm",
880 .of_match_table = sunxi_btlpm_ids,
881 },
882 };
883
884 /**
885 * Initializes the module.
886 * @return On success, 0. On error, -1, and <code>errno</code> is set
887 * appropriately.
888 */
bluesleep_init(void)889 static int __init bluesleep_init(void)
890 {
891 int retval;
892 struct proc_dir_entry *ent;
893
894 BT_DBG("BlueSleep Mode Driver Ver %s", VERSION);
895
896 retval = platform_driver_probe(&bluesleep_driver, bluesleep_probe);
897 if (retval)
898 return retval;
899
900 #if !BT_BLUEDROID_SUPPORT
901 bluesleep_hdev = NULL;
902 #endif
903
904 bluetooth_dir = proc_mkdir("bluetooth", NULL);
905 if (bluetooth_dir == NULL) {
906 BT_ERR("Unable to create /proc/bluetooth directory");
907 return -ENOMEM;
908 }
909
910 sleep_dir = proc_mkdir("sleep", bluetooth_dir);
911 if (sleep_dir == NULL) {
912 BT_ERR("Unable to create /proc/%s directory", PROC_DIR);
913 return -ENOMEM;
914 }
915 #if 0
916 /* Creating read/write "btwake" entry */
917 ent = create_proc_entry("btwake", 0, sleep_dir);
918 if (ent == NULL) {
919 BT_ERR("Unable to create /proc/%s/btwake entry", PROC_DIR);
920 retval = -ENOMEM;
921 goto fail;
922 }
923 ent->read_proc = bluepower_read_proc_btwake;
924 ent->write_proc = bluepower_write_proc_btwake;
925
926 /* read only proc entries */
927 if (create_proc_read_entry("hostwake", 0, sleep_dir,
928 bluepower_read_proc_hostwake, NULL) == NULL) {
929 BT_ERR("Unable to create /proc/%s/hostwake entry", PROC_DIR);
930 retval = -ENOMEM;
931 goto fail;
932 }
933
934 /* read/write proc entries */
935 ent = create_proc_entry("proto", 0666, sleep_dir);
936 if (ent == NULL) {
937 BT_ERR("Unable to create /proc/%s/proto entry", PROC_DIR);
938 retval = -ENOMEM;
939 goto fail;
940 }
941 ent->read_proc = bluesleep_read_proc_proto;
942 ent->write_proc = bluesleep_write_proc_proto;
943
944 /* read only proc entries */
945 if (create_proc_read_entry("asleep", 0,
946 sleep_dir, bluesleep_read_proc_asleep, NULL) == NULL) {
947 BT_ERR("Unable to create /proc/%s/asleep entry", PROC_DIR);
948 retval = -ENOMEM;
949 goto fail;
950 }
951 #endif
952 #if BT_BLUEDROID_SUPPORT
953 /* read/write proc entries */
954 ent = proc_create("lpm", 0660, sleep_dir, &lpm_fops);
955 if (ent == NULL) {
956 BT_ERR("Unable to create /proc/%s/lpm entry", PROC_DIR);
957 retval = -ENOMEM;
958 goto fail;
959 }
960
961 ent = proc_create("btwrite", 0660, sleep_dir, &btwrite_fops);
962 if (ent == NULL) {
963 BT_ERR("Unable to create /proc/%s/btwrite entry", PROC_DIR);
964 retval = -ENOMEM;
965 goto fail;
966 }
967 #endif
968
969 flags = 0; /* clear all status bits */
970
971 /* Initialize spinlock. */
972 spin_lock_init(&rw_lock);
973
974 /* Initialize timer */
975 timer_setup(&rx_timer, bluesleep_rx_timer_expire, 0);
976
977 /* initialize host wake tasklet */
978 tasklet_init(&hostwake_task, bluesleep_hostwake_task, 0);
979
980 #if !BT_BLUEDROID_SUPPORT
981 hci_register_notifier(&hci_event_nblock);
982 #endif
983
984 return 0;
985
986 fail:
987 #if BT_BLUEDROID_SUPPORT
988 remove_proc_entry("btwrite", sleep_dir);
989 remove_proc_entry("lpm", sleep_dir);
990 #endif
991 #if 0
992 remove_proc_entry("asleep", sleep_dir);
993 remove_proc_entry("proto", sleep_dir);
994 remove_proc_entry("hostwake", sleep_dir);
995 remove_proc_entry("btwake", sleep_dir);
996 #endif
997 remove_proc_entry("sleep", bluetooth_dir);
998 remove_proc_entry("bluetooth", 0);
999 return retval;
1000 }
1001
1002 /**
1003 * Cleans up the module.
1004 */
bluesleep_exit(void)1005 static void __exit bluesleep_exit(void)
1006 {
1007 #if !BT_BLUEDROID_SUPPORT
1008 hci_unregister_notifier(&hci_event_nblock);
1009 #endif
1010 platform_driver_unregister(&bluesleep_driver);
1011
1012 #if BT_BLUEDROID_SUPPORT
1013 remove_proc_entry("btwrite", sleep_dir);
1014 remove_proc_entry("lpm", sleep_dir);
1015 #endif
1016 #if 0
1017 remove_proc_entry("asleep", sleep_dir);
1018 remove_proc_entry("proto", sleep_dir);
1019 remove_proc_entry("hostwake", sleep_dir);
1020 remove_proc_entry("btwake", sleep_dir);
1021 #endif
1022 remove_proc_entry("sleep", bluetooth_dir);
1023 remove_proc_entry("bluetooth", 0);
1024 }
1025
1026 module_init(bluesleep_init);
1027 module_exit(bluesleep_exit);
1028
1029 MODULE_DESCRIPTION("Bluetooth Sleep Mode Driver ver %s " VERSION);
1030 #ifdef MODULE_LICENSE
1031 MODULE_LICENSE("GPL");
1032 #endif
1033