bluesleep.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * You should have received a copy of the GNU General Public License
  7. * along with this program; if not, write to the Free Software
  8. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. *
  15. * Copyright (C) 2006-2007 - Motorola
  16. * Copyright (c) 2008-2010, The Linux Foundation. All rights reserved.
  17. *
  18. * Date Author Comment
  19. * ----------- -------------- --------------------------------
  20. * 2006-Apr-28 Motorola The kernel module for running the Bluetooth(R)
  21. * Sleep-Mode Protocol from the Host side
  22. * 2006-Sep-08 Motorola Added workqueue for handling sleep work.
  23. * 2007-Jan-24 Motorola Added mbm_handle_ioi() call to ISR.
  24. * 2009-Aug-10 Motorola Changed "add_timer" to "mod_timer" to solve
  25. * race when flurry of queued work comes in.
  26. */
  27. #include <linux/module.h> /* kernel module definitions */
  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. #include <linux/irq.h>
  41. #include <linux/ioport.h>
  42. #include <linux/param.h>
  43. #include <linux/bitops.h>
  44. #include <linux/termios.h>
  45. #include <linux/wakelock.h>
  46. #include <linux/gpio.h>
  47. #include <linux/serial_core.h>
  48. #include <mach/msm_serial_hs.h>
  49. #include <net/bluetooth/bluetooth.h>
  50. #include <net/bluetooth/hci_core.h> /* event notifications */
  51. #include "hci_uart.h"
  52. #include <linux/barcode_emul.h>
  53. #include <linux/jiffies.h>
  54. #define BT_SLEEP_DBG
  55. #ifndef BT_SLEEP_DBG
  56. #define BT_DBG(fmt, arg...)
  57. #endif
  58. #undef BT_DBG
  59. #undef BT_ERR
  60. #define BT_DBG(fmt, arg...) pr_err(fmt " [BT]\n", ##arg)
  61. #define BT_ERR(fmt, arg...) pr_err(fmt " [BT]\n", ##arg)
  62. /*
  63. * Defines
  64. */
  65. #define VERSION "1.1"
  66. #define PROC_DIR "bluetooth/sleep"
  67. #define POLARITY_LOW 0
  68. #define POLARITY_HIGH 1
  69. enum msm_hs_clk_states_e {
  70. MSM_HS_CLK_PORT_OFF, /* port not in use */
  71. MSM_HS_CLK_OFF, /* clock disabled */
  72. MSM_HS_CLK_REQUEST_OFF, /* disable after TX and RX flushed */
  73. MSM_HS_CLK_ON, /* clock enabled */
  74. };
  75. struct bluesleep_info {
  76. unsigned host_wake;
  77. unsigned ext_wake;
  78. unsigned host_wake_irq;
  79. struct uart_port *uport;
  80. struct wake_lock wake_lock;
  81. int irq_polarity;
  82. int has_ext_wake;
  83. };
  84. /* work function */
  85. static void bluesleep_sleep_work(struct work_struct *work);
  86. static void bluesleep_uart_awake_work(struct work_struct *work);
  87. /* work queue */
  88. DECLARE_DELAYED_WORK(sleep_workqueue, bluesleep_sleep_work);
  89. DECLARE_DELAYED_WORK(uart_awake_workqueue, bluesleep_uart_awake_work);
  90. /* Macros for handling sleep work */
  91. #define bluesleep_rx_busy() schedule_delayed_work(&sleep_workqueue, 0)
  92. #define bluesleep_tx_busy() schedule_delayed_work(&sleep_workqueue, 0)
  93. #define bluesleep_rx_idle() schedule_delayed_work(&sleep_workqueue, 0)
  94. #define bluesleep_tx_idle() schedule_delayed_work(&sleep_workqueue, 0)
  95. #define bluesleep_uart_work() schedule_delayed_work(&uart_awake_workqueue, 0)
  96. /* 3 second timeout */
  97. #define TX_TIMER_INTERVAL 3
  98. /* state variable names and bit positions */
  99. #define BT_PROTO 0x01
  100. #define BT_TXDATA 0x02
  101. #define BT_ASLEEP 0x04
  102. #define BT_EXT_WAKE 0x08
  103. #define BT_SUSPEND 0x10
  104. static bool bt_enabled;
  105. static struct platform_device *bluesleep_uart_dev;
  106. static struct bluesleep_info *bsi;
  107. /*
  108. * Local function prototypes
  109. */
  110. /*
  111. * Global variables
  112. */
  113. /** Global state flags */
  114. static unsigned long flags;
  115. /** Tasklet to respond to change in hostwake line */
  116. static struct tasklet_struct hostwake_task;
  117. /** Transmission timer */
  118. static void bluesleep_tx_timer_expire(unsigned long data);
  119. static DEFINE_TIMER(tx_timer, bluesleep_tx_timer_expire, 0, 0);
  120. /** Lock for state transitions */
  121. struct mutex bluesleep_mutex;
  122. struct proc_dir_entry *bluetooth_dir, *sleep_dir;
  123. /*
  124. * Local functions
  125. */
  126. static int bluesleep_get_uart_state(void)
  127. {
  128. int state = 0;
  129. state = msm_hs_get_clock_state(bsi->uport);
  130. return state;
  131. }
  132. static void bluesleep_uart_awake_work(struct work_struct *work)
  133. {
  134. int clk_state;
  135. if (!bsi->uport) {
  136. BT_DBG("hsuart_power called. But uport is null");
  137. return;
  138. }
  139. clk_state = bluesleep_get_uart_state();
  140. if (clk_state == MSM_HS_CLK_OFF) {
  141. BT_DBG("bluesleep_uart_awake_work : hsuart_power on");
  142. msm_hs_request_clock_on(bsi->uport);
  143. msm_hs_set_mctrl(bsi->uport, TIOCM_RTS);
  144. }else if(clk_state == MSM_HS_CLK_REQUEST_OFF){
  145. bluesleep_uart_work();
  146. }
  147. }
  148. static void hsuart_power(int on)
  149. {
  150. int clk_state;
  151. if (test_bit(BT_SUSPEND, &flags)) {
  152. BT_DBG("it's suspend state. waiting for resume.");
  153. return;
  154. }
  155. if (!bsi->uport) {
  156. BT_DBG("hsuart_power called. But uport is null");
  157. return;
  158. }
  159. if (on) {
  160. if(test_bit(BT_TXDATA, &flags)) {
  161. BT_DBG("hsuart_power on");
  162. msm_hs_request_clock_on(bsi->uport);
  163. msm_hs_set_mctrl(bsi->uport, TIOCM_RTS);
  164. return;
  165. }
  166. clk_state = bluesleep_get_uart_state();
  167. if(clk_state == MSM_HS_CLK_REQUEST_OFF) {
  168. BT_DBG("hsuart_power wait");
  169. bluesleep_uart_work();
  170. } else {
  171. BT_DBG("hsuart_power on");
  172. msm_hs_request_clock_on(bsi->uport);
  173. msm_hs_set_mctrl(bsi->uport, TIOCM_RTS);
  174. }
  175. } else {
  176. BT_DBG("hsuart_power off");
  177. msm_hs_set_mctrl(bsi->uport, 0);
  178. msm_hs_request_clock_off(bsi->uport);
  179. }
  180. }
  181. /**
  182. * @return 1 if the Host can go to sleep, 0 otherwise.
  183. */
  184. int bluesleep_can_sleep(void)
  185. {
  186. /* check if WAKE_BT_GPIO and BT_WAKE_GPIO are both deasserted */
  187. return (!gpio_get_value(bsi->host_wake) &&
  188. (bsi->uport != NULL));
  189. }
  190. void bluesleep_sleep_wakeup(void)
  191. {
  192. if (test_bit(BT_ASLEEP, &flags)) {
  193. BT_DBG("waking up...");
  194. /*Activating UART */
  195. hsuart_power(1);
  196. wake_lock(&bsi->wake_lock);
  197. /* Start the timer */
  198. mod_timer(&tx_timer,
  199. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  200. if (bsi->has_ext_wake == 1) {
  201. gpio_set_value(bsi->ext_wake, 1);
  202. }
  203. set_bit(BT_EXT_WAKE, &flags);
  204. clear_bit(BT_ASLEEP, &flags);
  205. }
  206. else {
  207. BT_DBG("bluesleep_sleep_wakeup : already wake up, so start timer...");
  208. mod_timer(&tx_timer,
  209. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  210. }
  211. }
  212. static void bluesleep_tx_data_wakeup(void)
  213. {
  214. if (test_bit(BT_ASLEEP, &flags)) {
  215. BT_DBG("waking up from BT Write...");
  216. wake_lock(&bsi->wake_lock);
  217. /* Start the timer */
  218. mod_timer(&tx_timer,
  219. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  220. if (bsi->has_ext_wake == 1) {
  221. gpio_set_value(bsi->ext_wake, 1);
  222. }
  223. set_bit(BT_EXT_WAKE, &flags);
  224. clear_bit(BT_ASLEEP, &flags);
  225. }
  226. else {
  227. BT_DBG("bluesleep_tx_data_wakeup : already wake up, so start timer...");
  228. mod_timer(&tx_timer,
  229. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  230. }
  231. }
  232. /**
  233. * @brief@ main sleep work handling function which update the flags
  234. * and activate and deactivate UART ,check FIFO.
  235. */
  236. static void bluesleep_sleep_work(struct work_struct *work)
  237. {
  238. if (mutex_is_locked(&bluesleep_mutex))
  239. BT_DBG("Wait for mutex unlock in bluesleep_sleep_work");
  240. if (bsi->uport == NULL) {
  241. BT_DBG("bluesleep_sleep_work - uport is null");
  242. return;
  243. }
  244. if (bsi->uport->state == NULL) {
  245. BT_DBG("bluesleep_sleep_work - bsi->uport->state is null");
  246. return;
  247. }
  248. if (bsi->uport->state->port.tty == NULL) {
  249. BT_DBG("bluesleep_sleep_work - bsi->uport->state->port.tty is null");
  250. return;
  251. }
  252. mutex_lock(&bluesleep_mutex);
  253. if (bluesleep_can_sleep()) {
  254. /* already asleep, this is an error case */
  255. if (test_bit(BT_ASLEEP, &flags)) {
  256. BT_DBG("already asleep");
  257. mutex_unlock(&bluesleep_mutex);
  258. return;
  259. }
  260. if (msm_hs_tx_empty(bsi->uport)) {
  261. if (test_bit(BT_TXDATA, &flags)) {
  262. BT_DBG("TXDATA remained. Wait until timer expires.");
  263. mod_timer(&tx_timer,
  264. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  265. mutex_unlock(&bluesleep_mutex);
  266. return;
  267. }
  268. BT_DBG("going to sleep...");
  269. set_bit(BT_ASLEEP, &flags);
  270. /*Deactivating UART */
  271. hsuart_power(0);
  272. /* Moved from Timer expired */
  273. if (bsi->has_ext_wake == 1)
  274. gpio_set_value(bsi->ext_wake, 0);
  275. clear_bit(BT_EXT_WAKE, &flags);
  276. /*Deactivating UART */
  277. /* UART clk is not turned off immediately. Release
  278. * wakelock after 500 ms.
  279. */
  280. wake_lock_timeout(&bsi->wake_lock, msecs_to_jiffies(500));
  281. } else {
  282. BT_DBG("host can enter sleep but some tx remained.");
  283. mod_timer(&tx_timer,
  284. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  285. mutex_unlock(&bluesleep_mutex);
  286. return;
  287. }
  288. } else if (!test_bit(BT_EXT_WAKE, &flags)
  289. && !test_bit(BT_ASLEEP, &flags)) {
  290. BT_DBG("host_wake high and BT_EXT_WAKE & BT_ASLEEP already freed.");
  291. mod_timer(&tx_timer,
  292. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  293. if (bsi->has_ext_wake == 1) {
  294. gpio_set_value(bsi->ext_wake, 1);
  295. }
  296. set_bit(BT_EXT_WAKE, &flags);
  297. } else {
  298. bluesleep_sleep_wakeup();
  299. }
  300. mutex_unlock(&bluesleep_mutex);
  301. }
  302. /**
  303. * A tasklet function that runs in tasklet context and reads the value
  304. * of the HOST_WAKE GPIO pin and further defer the work.
  305. * @param data Not used.
  306. */
  307. static void bluesleep_hostwake_task(unsigned long data)
  308. {
  309. BT_DBG("hostwake line change");
  310. if ((gpio_get_value(bsi->host_wake) == bsi->irq_polarity))
  311. bluesleep_rx_busy();
  312. else
  313. bluesleep_rx_idle();
  314. }
  315. /**
  316. * Handles proper timer action when outgoing data is delivered to the
  317. * HCI line discipline. Sets BT_TXDATA.
  318. */
  319. static void bluesleep_outgoing_data(void)
  320. {
  321. if (mutex_is_locked(&bluesleep_mutex))
  322. BT_DBG("Wait for mutex unlock in bluesleep_outgoing_data");
  323. mutex_lock(&bluesleep_mutex);
  324. /* log data passing by */
  325. set_bit(BT_TXDATA, &flags);
  326. BT_DBG("bluesleep_outgoing_data.");
  327. if (!test_bit(BT_EXT_WAKE, &flags))
  328. BT_DBG("BT_EXT_WAKE freed");
  329. if (!test_bit(BT_ASLEEP, &flags))
  330. BT_DBG("BT_ASLEEP freed");
  331. /*
  332. ** Uart Clk should be enabled promptly
  333. ** before bluedroid write TX data.
  334. */
  335. hsuart_power(1);
  336. bluesleep_tx_data_wakeup();
  337. mutex_unlock(&bluesleep_mutex);
  338. }
  339. /**
  340. * Starts the Sleep-Mode Protocol on the Host.
  341. */
  342. static void bluesleep_start(void)
  343. {
  344. int retval;
  345. if (test_bit(BT_PROTO, &flags)) {
  346. return;
  347. }
  348. /* start the timer */
  349. mod_timer(&tx_timer, jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  350. /* assert BT_WAKE */
  351. if (bsi->has_ext_wake == 1) {
  352. gpio_set_value(bsi->ext_wake, 1);
  353. }
  354. set_bit(BT_EXT_WAKE, &flags);
  355. retval = enable_irq_wake(bsi->host_wake_irq);
  356. if (retval < 0) {
  357. BT_ERR("Couldn't enable BT_HOST_WAKE as wakeup interrupt");
  358. goto fail;
  359. }
  360. set_bit(BT_PROTO, &flags);
  361. wake_lock(&bsi->wake_lock);
  362. return;
  363. fail:
  364. del_timer(&tx_timer);
  365. return;
  366. }
  367. static void bluesleep_abnormal_stop(void)
  368. {
  369. BT_ERR("bluesleep_abnormal_stop");
  370. if (!test_bit(BT_PROTO, &flags)) {
  371. BT_ERR("(bluesleep_abnormal_stop) proto is not set. Failed to stop bluesleep");
  372. bsi->uport = NULL;
  373. return;
  374. }
  375. del_timer(&tx_timer);
  376. clear_bit(BT_PROTO, &flags);
  377. if (disable_irq_wake(bsi->host_wake_irq))
  378. BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
  379. wake_lock_timeout(&bsi->wake_lock, msecs_to_jiffies(500));
  380. clear_bit(BT_TXDATA, &flags);
  381. bsi->uport = NULL;
  382. }
  383. /**
  384. * Stops the Sleep-Mode Protocol on the Host.
  385. */
  386. static void bluesleep_stop(void)
  387. {
  388. if (!test_bit(BT_PROTO, &flags)) {
  389. BT_ERR("(bluesleep_stop_wq) proto is not set. Failed to stop bluesleep");
  390. bsi->uport = NULL;
  391. return;
  392. }
  393. /* assert BT_WAKE */
  394. if (bsi->has_ext_wake == 1) {
  395. gpio_set_value(bsi->ext_wake, 1);
  396. }
  397. set_bit(BT_EXT_WAKE, &flags);
  398. del_timer(&tx_timer);
  399. clear_bit(BT_PROTO, &flags);
  400. if (test_bit(BT_ASLEEP, &flags)) {
  401. clear_bit(BT_ASLEEP, &flags);
  402. hsuart_power(1);
  403. }
  404. if (disable_irq_wake(bsi->host_wake_irq))
  405. BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
  406. wake_lock_timeout(&bsi->wake_lock, msecs_to_jiffies(500));
  407. bsi->uport = NULL;
  408. }
  409. struct uart_port *bluesleep_get_uart_port(void)
  410. {
  411. struct uart_port *uport = NULL;
  412. uport = msm_hs_get_uart_port(0);
  413. if(uport == NULL)
  414. BT_ERR("(bluesleep_get_uart_port) uport is null");
  415. return uport;
  416. }
  417. static int bluesleep_read_proc_lpm(char *page, char **start, off_t offset,
  418. int count, int *eof, void *data)
  419. {
  420. *eof = 1;
  421. return snprintf(page, count, "lpm: %u\n", bt_enabled?1:0 );
  422. }
  423. static int bluesleep_write_proc_lpm(struct file *file, const char *buffer,
  424. unsigned long count, void *data)
  425. {
  426. char b;
  427. if (count < 1)
  428. return -EINVAL;
  429. if (copy_from_user(&b, buffer, 1))
  430. return -EFAULT;
  431. if (b == '0') {
  432. BT_ERR("(bluesleep_write_proc_lpm) Unreg HCI notifier.");
  433. /* HCI_DEV_UNREG */
  434. bluesleep_stop();
  435. bt_enabled = false;
  436. //bsi->uport = NULL;
  437. } else if (b == '1') {
  438. BT_ERR("(bluesleep_write_proc_lpm) Reg HCI notifier.");
  439. /* HCI_DEV_REG */
  440. if (!bt_enabled) {
  441. bt_enabled = true;
  442. bsi->uport = bluesleep_get_uart_port();
  443. /* if bluetooth started, start bluesleep*/
  444. bluesleep_start();
  445. }
  446. } else if (b == '2') {
  447. BT_ERR("(bluesleep_write_proc_lpm) don`t control ext_wake & uart clk");
  448. if(bt_enabled) {
  449. bt_enabled = false;
  450. bluesleep_abnormal_stop();
  451. }
  452. }
  453. return count;
  454. }
  455. static int bluesleep_read_proc_btwrite(char *page, char **start, off_t offset,
  456. int count, int *eof, void *data)
  457. {
  458. *eof = 1;
  459. return snprintf(page, count, "unsupported to read\n");
  460. }
  461. static int bluesleep_write_proc_btwrite(struct file *file, const char *buffer,
  462. unsigned long count, void *data)
  463. {
  464. char b;
  465. if (count < 1)
  466. return -EINVAL;
  467. if (copy_from_user(&b, buffer, 1))
  468. return -EFAULT;
  469. /* HCI_DEV_WRITE */
  470. if (b != '0')
  471. bluesleep_outgoing_data();
  472. return count;
  473. }
  474. /**
  475. * Handles transmission timer expiration.
  476. * @param data Not used.
  477. */
  478. static void bluesleep_tx_timer_expire(unsigned long data)
  479. {
  480. /* were we silent during the last timeout? */
  481. if (!test_bit(BT_TXDATA, &flags)) {
  482. BT_DBG("Tx has been idle");
  483. bluesleep_tx_idle();
  484. } else {
  485. BT_DBG("Tx data during last period");
  486. mod_timer(&tx_timer,
  487. jiffies + msecs_to_jiffies(TX_TIMER_INTERVAL * 1000));
  488. }
  489. /* clear the incoming data flag */
  490. clear_bit(BT_TXDATA, &flags);
  491. }
  492. /**
  493. * Schedules a tasklet to run when receiving an interrupt on the
  494. * <code>HOST_WAKE</code> GPIO pin.
  495. * @param irq Not used.
  496. * @param dev_id Not used.
  497. */
  498. static irqreturn_t bluesleep_hostwake_isr(int irq, void *dev_id)
  499. {
  500. /* schedule a tasklet to handle the change in the host wake line */
  501. tasklet_schedule(&hostwake_task);
  502. return IRQ_HANDLED;
  503. }
  504. /**
  505. * Read the <code>BT_WAKE</code> GPIO pin value via the proc interface.
  506. * When this function returns, <code>page</code> will contain a 1 if the
  507. * pin is high, 0 otherwise.
  508. * @param page Buffer for writing data.
  509. * @param start Not used.
  510. * @param offset Not used.
  511. * @param count Not used.
  512. * @param eof Whether or not there is more data to be read.
  513. * @param data Not used.
  514. * @return The number of bytes written.
  515. */
  516. static int bluepower_read_proc_btwake(char *page, char **start, off_t offset,
  517. int count, int *eof, void *data)
  518. {
  519. *eof = 1;
  520. return snprintf(page, count, "btwake:%u\n", \
  521. test_bit(BT_EXT_WAKE, &flags));
  522. }
  523. /**
  524. * Write the <code>BT_WAKE</code> GPIO pin value via the proc interface.
  525. * @param file Not used.
  526. * @param buffer The buffer to read from.
  527. * @param count The number of bytes to be written.
  528. * @param data Not used.
  529. * @return On success, the number of bytes written. On error, -1, and
  530. * <code>errno</code> is set appropriately.
  531. */
  532. static int bluepower_write_proc_btwake(struct file *file, const char *buffer,
  533. unsigned long count, void *data)
  534. {
  535. char *buf;
  536. if (count < 1)
  537. return -EINVAL;
  538. buf = kmalloc(count, GFP_KERNEL);
  539. if (!buf)
  540. return -ENOMEM;
  541. if (copy_from_user(buf, buffer, count)) {
  542. kfree(buf);
  543. return -EFAULT;
  544. }
  545. if (buf[0] == '0') {
  546. if (bsi->has_ext_wake == 1) {
  547. gpio_set_value(bsi->ext_wake, 0);
  548. }
  549. clear_bit(BT_EXT_WAKE, &flags);
  550. } else if (buf[0] == '1') {
  551. if (bsi->has_ext_wake == 1) {
  552. gpio_set_value(bsi->ext_wake, 1);
  553. }
  554. set_bit(BT_EXT_WAKE, &flags);
  555. } else {
  556. kfree(buf);
  557. return -EINVAL;
  558. }
  559. kfree(buf);
  560. return count;
  561. }
  562. /**
  563. * Read the <code>BT_HOST_WAKE</code> GPIO pin value via the proc interface.
  564. * When this function returns, <code>page</code> will contain a 1 if the pin
  565. * is high, 0 otherwise.
  566. * @param page Buffer for writing data.
  567. * @param start Not used.
  568. * @param offset Not used.
  569. * @param count Not used.
  570. * @param eof Whether or not there is more data to be read.
  571. * @param data Not used.
  572. * @return The number of bytes written.
  573. */
  574. static int bluepower_read_proc_hostwake(char *page, char **start, off_t offset,
  575. int count, int *eof, void *data)
  576. {
  577. *eof = 1;
  578. return snprintf(page, count, "hostwake: %u\n", \
  579. gpio_get_value(bsi->host_wake));
  580. }
  581. /**
  582. * Read the low-power status of the Host via the proc interface.
  583. * When this function returns, <code>page</code> contains a 1 if the Host
  584. * is asleep, 0 otherwise.
  585. * @param page Buffer for writing data.
  586. * @param start Not used.
  587. * @param offset Not used.
  588. * @param count Not used.
  589. * @param eof Whether or not there is more data to be read.
  590. * @param data Not used.
  591. * @return The number of bytes written.
  592. */
  593. static int bluesleep_read_proc_asleep(char *page, char **start, off_t offset,
  594. int count, int *eof, void *data)
  595. {
  596. unsigned int asleep;
  597. asleep = test_bit(BT_ASLEEP, &flags) ? 1 : 0;
  598. *eof = 1;
  599. return snprintf(page, count, "asleep: %u\n", asleep);
  600. }
  601. /**
  602. * Read the low-power protocol being used by the Host via the proc interface.
  603. * When this function returns, <code>page</code> will contain a 1 if the Host
  604. * is using the Sleep Mode Protocol, 0 otherwise.
  605. * @param page Buffer for writing data.
  606. * @param start Not used.
  607. * @param offset Not used.
  608. * @param count Not used.
  609. * @param eof Whether or not there is more data to be read.
  610. * @param data Not used.
  611. * @return The number of bytes written.
  612. */
  613. static int bluesleep_read_proc_proto(char *page, char **start, off_t offset,
  614. int count, int *eof, void *data)
  615. {
  616. unsigned int proto;
  617. proto = test_bit(BT_PROTO, &flags) ? 1 : 0;
  618. *eof = 1;
  619. return snprintf(page, count, "proto: %u\n", proto);
  620. }
  621. /**
  622. * Modify the low-power protocol used by the Host via the proc interface.
  623. * @param file Not used.
  624. * @param buffer The buffer to read from.
  625. * @param count The number of bytes to be written.
  626. * @param data Not used.
  627. * @return On success, the number of bytes written. On error, -1, and
  628. * <code>errno</code> is set appropriately.
  629. */
  630. static int bluesleep_write_proc_proto(struct file *file, const char *buffer,
  631. unsigned long count, void *data)
  632. {
  633. char proto;
  634. if (count < 1)
  635. return -EINVAL;
  636. if (copy_from_user(&proto, buffer, 1))
  637. return -EFAULT;
  638. if (proto == '0')
  639. bluesleep_stop();
  640. else
  641. bluesleep_start();
  642. /* claim that we wrote everything */
  643. return count;
  644. }
  645. void bluesleep_setup_uart_port(struct platform_device *uart_dev)
  646. {
  647. bluesleep_uart_dev = uart_dev;
  648. }
  649. static int bluesleep_probe(struct platform_device *pdev)
  650. {
  651. int ret;
  652. struct resource *res;
  653. BT_ERR("bluesleep probe\n");
  654. bsi = kzalloc(sizeof(struct bluesleep_info), GFP_KERNEL);
  655. if (!bsi) {
  656. BT_ERR("failed to allocate memory to bsi\n");
  657. return -ENOMEM;
  658. }
  659. res = platform_get_resource_byname(pdev, IORESOURCE_IO,
  660. "gpio_host_wake");
  661. if (!res) {
  662. BT_ERR("couldn't find host_wake gpio\n");
  663. ret = -ENODEV;
  664. goto free_bsi;
  665. }
  666. bsi->host_wake = res->start;
  667. ret = gpio_request(bsi->host_wake, "bt_host_wake");
  668. if (ret)
  669. {
  670. BT_ERR("bluesleep_probe gpio_request for host_wake is failed");
  671. goto free_bsi;
  672. }
  673. /* configure host_wake as input */
  674. #if !defined(CONFIG_BT_BCM4354)
  675. BT_ERR("configure input direction\n");
  676. gpio_tlmm_config(GPIO_CFG(bsi->host_wake, 0, GPIO_CFG_INPUT,
  677. GPIO_CFG_NO_PULL, GPIO_CFG_16MA), GPIO_CFG_ENABLE);
  678. ret = gpio_direction_input(bsi->host_wake);
  679. if (ret < 0) {
  680. BT_ERR("gpio-keys: failed to configure input direction for GPIO %d, error %d",
  681. bsi->host_wake, ret);
  682. gpio_free(bsi->host_wake);
  683. goto free_bsi;
  684. }
  685. #endif
  686. res = platform_get_resource_byname(pdev, IORESOURCE_IO,
  687. "gpio_ext_wake");
  688. if (!res)
  689. bsi->has_ext_wake = 0;
  690. else
  691. bsi->has_ext_wake = 1;
  692. if (bsi->has_ext_wake) {
  693. bsi->ext_wake = res->start;
  694. ret = gpio_request(bsi->ext_wake, "bt_ext_wake");
  695. if (ret)
  696. {
  697. BT_ERR("bluesleep_probe gpio_request for bt_ext_wake is failed");
  698. goto free_bt_host_wake;
  699. }
  700. /* configure ext_wake as output mode*/
  701. ret = gpio_direction_output(bsi->ext_wake, 0);
  702. if (ret < 0) {
  703. BT_ERR("gpio-keys: failed to configure output direction for GPIO %d, error %d",
  704. bsi->ext_wake, ret);
  705. gpio_free(bsi->ext_wake);
  706. goto free_bt_host_wake;
  707. }
  708. } else
  709. set_bit(BT_EXT_WAKE, &flags);
  710. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  711. "host_wake");
  712. if (!res) {
  713. BT_ERR("couldn't find host_wake irq\n");
  714. ret = -ENODEV;
  715. goto free_bt_host_wake;
  716. }
  717. bsi->host_wake_irq = res->start;
  718. if (bsi->host_wake_irq < 0) {
  719. BT_ERR("couldn't find host_wake irq\n");
  720. ret = -ENODEV;
  721. goto free_bt_ext_wake;
  722. }
  723. if (res->flags & IORESOURCE_IRQ_LOWEDGE)
  724. bsi->irq_polarity = POLARITY_LOW;/*low edge (falling edge)*/
  725. else
  726. bsi->irq_polarity = POLARITY_HIGH;/*anything else*/
  727. wake_lock_init(&bsi->wake_lock, WAKE_LOCK_SUSPEND, "bluesleep");
  728. clear_bit(BT_SUSPEND, &flags);
  729. if (bsi->irq_polarity == POLARITY_LOW) {
  730. ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
  731. IRQF_DISABLED | IRQF_TRIGGER_FALLING,
  732. "bluetooth_hostwake", NULL);
  733. } else {
  734. ret = request_irq(bsi->host_wake_irq, bluesleep_hostwake_isr,
  735. IRQF_DISABLED | IRQF_TRIGGER_RISING,
  736. "bluetooth_hostwake", NULL);
  737. }
  738. if (ret < 0) {
  739. BT_ERR("Couldn't acquire BT_HOST_WAKE IRQ");
  740. goto free_bt_ext_wake;
  741. }
  742. return 0;
  743. free_bt_ext_wake:
  744. gpio_free(bsi->ext_wake);
  745. free_bt_host_wake:
  746. gpio_free(bsi->host_wake);
  747. free_bsi:
  748. kfree(bsi);
  749. bsi = NULL;
  750. return ret;
  751. }
  752. static int bluesleep_remove(struct platform_device *pdev)
  753. {
  754. free_irq(bsi->host_wake_irq, NULL);
  755. gpio_free(bsi->host_wake);
  756. gpio_free(bsi->ext_wake);
  757. wake_lock_destroy(&bsi->wake_lock);
  758. kfree(bsi);
  759. bsi = NULL;
  760. return 0;
  761. }
  762. static int bluesleep_resume(struct platform_device *pdev)
  763. {
  764. if (test_bit(BT_SUSPEND, &flags)) {
  765. #ifndef CONFIG_BT_BCM4354
  766. if (!bt_enabled) {
  767. gpio_tlmm_config(GPIO_CFG(bsi->host_wake, 0, GPIO_CFG_INPUT,
  768. GPIO_CFG_NO_PULL, GPIO_CFG_16MA), GPIO_CFG_ENABLE);
  769. }
  770. #endif
  771. clear_bit(BT_SUSPEND, &flags);
  772. if ((bsi->uport != NULL) &&
  773. (gpio_get_value(bsi->host_wake) == bsi->irq_polarity)) {
  774. BT_DBG("bluesleep resume form BT event...");
  775. hsuart_power(1);
  776. }
  777. }
  778. return 0;
  779. }
  780. static int bluesleep_suspend(struct platform_device *pdev, pm_message_t state)
  781. {
  782. #ifndef CONFIG_BT_BCM4354
  783. if (!bt_enabled) {
  784. gpio_tlmm_config(GPIO_CFG(bsi->host_wake, 0, GPIO_CFG_INPUT,
  785. GPIO_CFG_PULL_DOWN, GPIO_CFG_16MA), GPIO_CFG_ENABLE);
  786. }
  787. #endif
  788. set_bit(BT_SUSPEND, &flags);
  789. return 0;
  790. }
  791. static struct platform_driver bluesleep_driver = {
  792. .probe = bluesleep_probe,
  793. .remove = bluesleep_remove,
  794. .suspend = bluesleep_suspend,
  795. .resume = bluesleep_resume,
  796. .driver = {
  797. .name = "bluesleep",
  798. .owner = THIS_MODULE,
  799. },
  800. };
  801. /**
  802. * Initializes the module.
  803. * @return On success, 0. On error, -1, and <code>errno</code> is set
  804. * appropriately.
  805. */
  806. static int __init bluesleep_init(void)
  807. {
  808. int retval;
  809. struct proc_dir_entry *ent;
  810. BT_INFO("BlueSleep Mode Driver Ver %s", VERSION);
  811. bt_enabled = false;
  812. retval = platform_driver_register(&bluesleep_driver);
  813. if (retval)
  814. return retval;
  815. if (bsi == NULL)
  816. {
  817. BT_ERR("bluesleep_init failed. bsi is NULL!!");
  818. return -1;
  819. }
  820. bluetooth_dir = proc_mkdir("bluetooth", NULL);
  821. if (bluetooth_dir == NULL) {
  822. BT_ERR("Unable to create /proc/bluetooth directory");
  823. return -ENOMEM;
  824. }
  825. sleep_dir = proc_mkdir("sleep", bluetooth_dir);
  826. if (sleep_dir == NULL) {
  827. BT_ERR("Unable to create /proc/%s directory", PROC_DIR);
  828. return -ENOMEM;
  829. }
  830. /* Creating read/write "btwake" entry */
  831. ent = create_proc_entry("btwake", 0, sleep_dir);
  832. if (ent == NULL) {
  833. BT_ERR("Unable to create /proc/%s/btwake entry", PROC_DIR);
  834. retval = -ENOMEM;
  835. goto fail;
  836. }
  837. ent->read_proc = bluepower_read_proc_btwake;
  838. ent->write_proc = bluepower_write_proc_btwake;
  839. /* read only proc entries */
  840. if (create_proc_read_entry("hostwake", 0, sleep_dir,
  841. bluepower_read_proc_hostwake, NULL) == NULL) {
  842. BT_ERR("Unable to create /proc/%s/hostwake entry", PROC_DIR);
  843. retval = -ENOMEM;
  844. goto fail;
  845. }
  846. /* read/write proc entries */
  847. ent = create_proc_entry("proto", 0, sleep_dir);
  848. if (ent == NULL) {
  849. BT_ERR("Unable to create /proc/%s/proto entry", PROC_DIR);
  850. retval = -ENOMEM;
  851. goto fail;
  852. }
  853. ent->read_proc = bluesleep_read_proc_proto;
  854. ent->write_proc = bluesleep_write_proc_proto;
  855. /* read only proc entries */
  856. if (create_proc_read_entry("asleep", 0,
  857. sleep_dir, bluesleep_read_proc_asleep, NULL) == NULL) {
  858. BT_ERR("Unable to create /proc/%s/asleep entry", PROC_DIR);
  859. retval = -ENOMEM;
  860. goto fail;
  861. }
  862. /* read/write proc entries */
  863. ent = create_proc_entry("lpm", 0, sleep_dir);
  864. if (ent == NULL) {
  865. BT_ERR("Unable to create /proc/%s/lpm entry", PROC_DIR);
  866. retval = -ENOMEM;
  867. goto fail;
  868. }
  869. ent->read_proc = bluesleep_read_proc_lpm;
  870. ent->write_proc = bluesleep_write_proc_lpm;
  871. /* read/write proc entries */
  872. ent = create_proc_entry("btwrite", 0, sleep_dir);
  873. if (ent == NULL) {
  874. BT_ERR("Unable to create /proc/%s/btwrite entry", PROC_DIR);
  875. retval = -ENOMEM;
  876. goto fail;
  877. }
  878. ent->read_proc = bluesleep_read_proc_btwrite;
  879. ent->write_proc = bluesleep_write_proc_btwrite;
  880. flags = 0; /* clear all status bits */
  881. /* Initialize spinlock. */
  882. mutex_init(&bluesleep_mutex);
  883. /* Initialize timer */
  884. init_timer(&tx_timer);
  885. tx_timer.function = bluesleep_tx_timer_expire;
  886. tx_timer.data = 0;
  887. /* initialize host wake tasklet */
  888. tasklet_init(&hostwake_task, bluesleep_hostwake_task, 0);
  889. /* assert bt wake */
  890. /* block code for FPGA to be set-up
  891. if (bsi->has_ext_wake == 1) {
  892. ret = ice_gpiox_set(bsi->ext_wake, 1);
  893. if (ret)
  894. BT_ERR("(bluesleep_init) failed to set ext_wake 1.");
  895. }
  896. */
  897. set_bit(BT_EXT_WAKE, &flags);
  898. return 0;
  899. fail:
  900. remove_proc_entry("btwrite", sleep_dir);
  901. remove_proc_entry("lpm", sleep_dir);
  902. remove_proc_entry("asleep", sleep_dir);
  903. remove_proc_entry("proto", sleep_dir);
  904. remove_proc_entry("hostwake", sleep_dir);
  905. remove_proc_entry("btwake", sleep_dir);
  906. remove_proc_entry("sleep", bluetooth_dir);
  907. remove_proc_entry("bluetooth", 0);
  908. return retval;
  909. }
  910. /**
  911. * Cleans up the module.
  912. */
  913. static void __exit bluesleep_exit(void)
  914. {
  915. if (bsi == NULL)
  916. return;
  917. /* assert bt wake */
  918. if (bsi->has_ext_wake == 1) {
  919. gpio_set_value(bsi->ext_wake, 1);
  920. }
  921. set_bit(BT_EXT_WAKE, &flags);
  922. if (test_bit(BT_PROTO, &flags)) {
  923. if (disable_irq_wake(bsi->host_wake_irq))
  924. BT_ERR("Couldn't disable hostwake IRQ wakeup mode\n");
  925. free_irq(bsi->host_wake_irq, NULL);
  926. del_timer(&tx_timer);
  927. if (test_bit(BT_ASLEEP, &flags))
  928. hsuart_power(1);
  929. }
  930. platform_driver_unregister(&bluesleep_driver);
  931. remove_proc_entry("btwrite", sleep_dir);
  932. remove_proc_entry("lpm", sleep_dir);
  933. remove_proc_entry("asleep", sleep_dir);
  934. remove_proc_entry("proto", sleep_dir);
  935. remove_proc_entry("hostwake", sleep_dir);
  936. remove_proc_entry("btwake", sleep_dir);
  937. remove_proc_entry("sleep", bluetooth_dir);
  938. remove_proc_entry("bluetooth", 0);
  939. mutex_destroy(&bluesleep_mutex);
  940. }
  941. module_init(bluesleep_init);
  942. module_exit(bluesleep_exit);
  943. MODULE_DESCRIPTION("Bluetooth Sleep Mode Driver ver %s " VERSION);
  944. #ifdef MODULE_LICENSE
  945. MODULE_LICENSE("GPL");
  946. #endif