bluesleep_fpga.c 30 KB

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