hci_ibs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Qualcomm's Bluetooth Software In-Band Sleep UART protocol
  3. *
  4. * HCI_IBS (HCI In-Band Sleep) is Qualcomm's power management
  5. * protocol extension to H4.
  6. *
  7. * Copyright (C) 2007 Texas Instruments, Inc.
  8. * Copyright (c) 2010, 2012 The Linux Foundation. All rights reserved.
  9. *
  10. * Acknowledgements:
  11. * This file is based on hci_ll.c, which was...
  12. * Written by Ohad Ben-Cohen <ohad@bencohen.org>
  13. * which was in turn based on hci_h4.c, which was written
  14. * by Maxim Krasnyansky and Marcel Holtmann.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2
  18. * as published by the Free Software Foundation
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include <linux/types.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/poll.h>
  39. #include <linux/slab.h>
  40. #include <linux/tty.h>
  41. #include <linux/errno.h>
  42. #include <linux/string.h>
  43. #include <linux/signal.h>
  44. #include <linux/ioctl.h>
  45. #include <linux/timer.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/serial_core.h>
  48. #include <linux/jiffies.h>
  49. #ifdef CONFIG_SERIAL_MSM_HS
  50. #include <mach/msm_serial_hs.h>
  51. #endif
  52. #include <net/bluetooth/bluetooth.h>
  53. #include <net/bluetooth/hci_core.h>
  54. #include "hci_uart.h"
  55. /* HCI_IBS protocol messages */
  56. #define HCI_IBS_SLEEP_IND 0xFE
  57. #define HCI_IBS_WAKE_IND 0xFD
  58. #define HCI_IBS_WAKE_ACK 0xFC
  59. /* HCI_IBS receiver States */
  60. #define HCI_IBS_W4_PACKET_TYPE 0
  61. #define HCI_IBS_W4_EVENT_HDR 1
  62. #define HCI_IBS_W4_ACL_HDR 2
  63. #define HCI_IBS_W4_SCO_HDR 3
  64. #define HCI_IBS_W4_DATA 4
  65. /* HCI_IBS transmit side sleep protocol states */
  66. enum tx_ibs_states_e {
  67. HCI_IBS_TX_ASLEEP,
  68. HCI_IBS_TX_WAKING,
  69. HCI_IBS_TX_AWAKE,
  70. };
  71. /* HCI_IBS receive side sleep protocol states */
  72. enum rx_states_e {
  73. HCI_IBS_RX_ASLEEP,
  74. HCI_IBS_RX_AWAKE,
  75. };
  76. /* HCI_IBS transmit and receive side clock state vote */
  77. enum hci_ibs_clock_state_vote_e {
  78. HCI_IBS_VOTE_STATS_UPDATE,
  79. HCI_IBS_TX_VOTE_CLOCK_ON,
  80. HCI_IBS_TX_VOTE_CLOCK_OFF,
  81. HCI_IBS_RX_VOTE_CLOCK_ON,
  82. HCI_IBS_RX_VOTE_CLOCK_OFF,
  83. };
  84. static unsigned long wake_retrans = 1;
  85. static unsigned long tx_idle_delay = msecs_to_jiffies(2000);
  86. struct hci_ibs_cmd {
  87. u8 cmd;
  88. } __attribute__((packed));
  89. struct ibs_struct {
  90. unsigned long rx_state;
  91. unsigned long rx_count;
  92. struct sk_buff *rx_skb;
  93. struct sk_buff_head txq;
  94. struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
  95. spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
  96. unsigned long tx_ibs_state; /* HCI_IBS transmit side power state */
  97. unsigned long rx_ibs_state; /* HCI_IBS receive side power state */
  98. unsigned long tx_vote; /* clock must be on for TX */
  99. unsigned long rx_vote; /* clock must be on for RX */
  100. struct timer_list tx_idle_timer;
  101. struct timer_list wake_retrans_timer;
  102. struct workqueue_struct *workqueue;
  103. struct work_struct ws_awake_rx;
  104. struct work_struct ws_awake_device;
  105. struct work_struct ws_rx_vote_off;
  106. struct work_struct ws_tx_vote_off;
  107. void *ibs_hu; /* keeps the hci_uart pointer for reference */
  108. /* debug */
  109. unsigned long ibs_sent_wacks;
  110. unsigned long ibs_sent_slps;
  111. unsigned long ibs_sent_wakes;
  112. unsigned long ibs_recv_wacks;
  113. unsigned long ibs_recv_slps;
  114. unsigned long ibs_recv_wakes;
  115. unsigned long vote_last_jif;
  116. unsigned long vote_on_ticks;
  117. unsigned long vote_off_ticks;
  118. unsigned long tx_votes_on;
  119. unsigned long rx_votes_on;
  120. unsigned long tx_votes_off;
  121. unsigned long rx_votes_off;
  122. unsigned long votes_on;
  123. unsigned long votes_off;
  124. };
  125. #ifdef CONFIG_SERIAL_MSM_HS
  126. static void __ibs_msm_serial_clock_on(struct tty_struct *tty)
  127. {
  128. struct uart_state *state = tty->driver_data;
  129. struct uart_port *port = state->uart_port;
  130. msm_hs_request_clock_on(port);
  131. }
  132. static void __ibs_msm_serial_clock_request_off(struct tty_struct *tty)
  133. {
  134. struct uart_state *state = tty->driver_data;
  135. struct uart_port *port = state->uart_port;
  136. msm_hs_request_clock_off(port);
  137. }
  138. #else
  139. static inline void __ibs_msm_serial_clock_on(struct tty_struct *tty) {}
  140. static inline void __ibs_msm_serial_clock_request_off(struct tty_struct *tty) {}
  141. #endif
  142. /* clock_vote needs to be called with the ibs lock held */
  143. static void ibs_msm_serial_clock_vote(unsigned long vote, struct hci_uart *hu)
  144. {
  145. struct ibs_struct *ibs = hu->priv;
  146. unsigned long old_vote = (ibs->tx_vote | ibs->rx_vote);
  147. unsigned long new_vote;
  148. switch (vote) {
  149. default: /* error */
  150. BT_ERR("voting irregularity");
  151. return;
  152. case HCI_IBS_VOTE_STATS_UPDATE:
  153. if (old_vote)
  154. ibs->vote_off_ticks += (jiffies - ibs->vote_last_jif);
  155. else
  156. ibs->vote_on_ticks += (jiffies - ibs->vote_last_jif);
  157. return;
  158. case HCI_IBS_TX_VOTE_CLOCK_ON:
  159. ibs->tx_vote = 1;
  160. ibs->tx_votes_on++;
  161. new_vote = 1;
  162. break;
  163. case HCI_IBS_RX_VOTE_CLOCK_ON:
  164. ibs->rx_vote = 1;
  165. ibs->rx_votes_on++;
  166. new_vote = 1;
  167. break;
  168. case HCI_IBS_TX_VOTE_CLOCK_OFF:
  169. ibs->tx_vote = 0;
  170. ibs->tx_votes_off++;
  171. new_vote = ibs->rx_vote | ibs->tx_vote;
  172. break;
  173. case HCI_IBS_RX_VOTE_CLOCK_OFF:
  174. ibs->rx_vote = 0;
  175. ibs->rx_votes_off++;
  176. new_vote = ibs->rx_vote | ibs->tx_vote;
  177. break;
  178. }
  179. if (new_vote != old_vote) {
  180. if (new_vote)
  181. __ibs_msm_serial_clock_on(hu->tty);
  182. else
  183. __ibs_msm_serial_clock_request_off(hu->tty);
  184. BT_DBG("HCIUART_IBS: vote msm_serial_hs clock %lu(%lu)",
  185. new_vote, vote);
  186. /* debug */
  187. if (new_vote) {
  188. ibs->votes_on++;
  189. ibs->vote_off_ticks += (jiffies - ibs->vote_last_jif);
  190. } else {
  191. ibs->votes_off++;
  192. ibs->vote_on_ticks += (jiffies - ibs->vote_last_jif);
  193. }
  194. ibs->vote_last_jif = jiffies;
  195. }
  196. }
  197. /*
  198. * Builds and sends an HCI_IBS command packet.
  199. * These are very simple packets with only 1 cmd byte
  200. */
  201. static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
  202. {
  203. int err = 0;
  204. struct sk_buff *skb = NULL;
  205. struct ibs_struct *ibs = hu->priv;
  206. struct hci_ibs_cmd *hci_ibs_packet;
  207. BT_DBG("hu %p cmd 0x%x", hu, cmd);
  208. /* allocate packet */
  209. skb = bt_skb_alloc(1, GFP_ATOMIC);
  210. if (!skb) {
  211. BT_ERR("cannot allocate memory for HCI_IBS packet");
  212. err = -ENOMEM;
  213. goto out;
  214. }
  215. /* prepare packet */
  216. hci_ibs_packet = (struct hci_ibs_cmd *) skb_put(skb, 1);
  217. hci_ibs_packet->cmd = cmd;
  218. skb->dev = (void *) hu->hdev;
  219. /* send packet */
  220. skb_queue_tail(&ibs->txq, skb);
  221. out:
  222. return err;
  223. }
  224. static void ibs_wq_awake_device(struct work_struct *work)
  225. {
  226. struct ibs_struct *ibs = container_of(work, struct ibs_struct,
  227. ws_awake_device);
  228. struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
  229. unsigned long flags;
  230. BT_DBG(" %p ", hu);
  231. /* Vote for serial clock */
  232. ibs_msm_serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
  233. spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
  234. /* send wake indication to device */
  235. if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
  236. BT_ERR("cannot send WAKE to device");
  237. ibs->ibs_sent_wakes++; /* debug */
  238. /* start retransmit timer */
  239. mod_timer(&ibs->wake_retrans_timer, jiffies + msecs_to_jiffies(wake_retrans));
  240. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  241. }
  242. static void ibs_wq_awake_rx(struct work_struct *work)
  243. {
  244. struct ibs_struct *ibs = container_of(work, struct ibs_struct,
  245. ws_awake_rx);
  246. struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
  247. unsigned long flags;
  248. BT_DBG(" %p ", hu);
  249. ibs_msm_serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
  250. spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
  251. ibs->rx_ibs_state = HCI_IBS_RX_AWAKE;
  252. /* Always acknowledge device wake up,
  253. * sending IBS message doesn't count as TX ON
  254. */
  255. if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
  256. BT_ERR("cannot acknowledge device wake up");
  257. ibs->ibs_sent_wacks++; /* debug */
  258. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  259. /* actually send the packets */
  260. hci_uart_tx_wakeup(hu);
  261. }
  262. static void ibs_wq_serial_rx_clock_vote_off(struct work_struct *work)
  263. {
  264. struct ibs_struct *ibs = container_of(work, struct ibs_struct,
  265. ws_rx_vote_off);
  266. struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
  267. BT_DBG(" %p ", hu);
  268. ibs_msm_serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
  269. }
  270. static void ibs_wq_serial_tx_clock_vote_off(struct work_struct *work)
  271. {
  272. struct ibs_struct *ibs = container_of(work, struct ibs_struct,
  273. ws_tx_vote_off);
  274. struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
  275. BT_DBG(" %p ", hu);
  276. hci_uart_tx_wakeup(hu); /* run HCI tx handling unlocked */
  277. /* now that message queued to tty driver, vote for tty clocks off */
  278. /* It is up to the tty driver to pend the clocks off until tx done. */
  279. ibs_msm_serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
  280. }
  281. static void hci_ibs_tx_idle_timeout(unsigned long arg)
  282. {
  283. struct hci_uart *hu = (struct hci_uart *) arg;
  284. struct ibs_struct *ibs = hu->priv;
  285. unsigned long flags;
  286. BT_DBG("hu %p idle timeout in %lu state", hu, ibs->tx_ibs_state);
  287. spin_lock_irqsave_nested(&ibs->hci_ibs_lock,
  288. flags, SINGLE_DEPTH_NESTING);
  289. switch (ibs->tx_ibs_state) {
  290. default:
  291. case HCI_IBS_TX_ASLEEP:
  292. case HCI_IBS_TX_WAKING:
  293. BT_ERR("spurrious timeout in tx state %ld", ibs->tx_ibs_state);
  294. goto out;
  295. case HCI_IBS_TX_AWAKE: /* TX_IDLE, go to SLEEP */
  296. if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
  297. BT_ERR("cannot send SLEEP to device");
  298. goto out;
  299. }
  300. ibs->tx_ibs_state = HCI_IBS_TX_ASLEEP;
  301. ibs->ibs_sent_slps++; /* debug */
  302. break;
  303. }
  304. queue_work(ibs->workqueue, &ibs->ws_tx_vote_off);
  305. out:
  306. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  307. }
  308. static void hci_ibs_wake_retrans_timeout(unsigned long arg)
  309. {
  310. struct hci_uart *hu = (struct hci_uart *) arg;
  311. struct ibs_struct *ibs = hu->priv;
  312. unsigned long flags;
  313. unsigned long retransmit = 0;
  314. BT_DBG("hu %p wake retransmit timeout in %lu state",
  315. hu, ibs->tx_ibs_state);
  316. spin_lock_irqsave_nested(&ibs->hci_ibs_lock,
  317. flags, SINGLE_DEPTH_NESTING);
  318. switch (ibs->tx_ibs_state) {
  319. default:
  320. case HCI_IBS_TX_ASLEEP:
  321. case HCI_IBS_TX_AWAKE:
  322. BT_ERR("spurrious timeout tx state %ld", ibs->tx_ibs_state);
  323. goto out;
  324. case HCI_IBS_TX_WAKING: /* No WAKE_ACK, retransmit WAKE */
  325. retransmit = 1;
  326. if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
  327. BT_ERR("cannot acknowledge device wake up");
  328. goto out;
  329. }
  330. ibs->ibs_sent_wakes++; /* debug */
  331. mod_timer(&ibs->wake_retrans_timer, jiffies + msecs_to_jiffies(wake_retrans));
  332. break;
  333. }
  334. out:
  335. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  336. if (retransmit)
  337. hci_uart_tx_wakeup(hu);
  338. }
  339. /* Initialize protocol */
  340. static int ibs_open(struct hci_uart *hu)
  341. {
  342. struct ibs_struct *ibs;
  343. BT_DBG("hu %p", hu);
  344. ibs = kzalloc(sizeof(*ibs), GFP_ATOMIC);
  345. if (!ibs)
  346. return -ENOMEM;
  347. skb_queue_head_init(&ibs->txq);
  348. skb_queue_head_init(&ibs->tx_wait_q);
  349. spin_lock_init(&ibs->hci_ibs_lock);
  350. ibs->workqueue = create_singlethread_workqueue("ibs_wq");
  351. if (!ibs->workqueue) {
  352. BT_ERR("IBS Workqueue not initialized properly");
  353. kfree(ibs);
  354. return -ENOMEM;
  355. }
  356. INIT_WORK(&ibs->ws_awake_rx, ibs_wq_awake_rx);
  357. INIT_WORK(&ibs->ws_awake_device, ibs_wq_awake_device);
  358. INIT_WORK(&ibs->ws_rx_vote_off, ibs_wq_serial_rx_clock_vote_off);
  359. INIT_WORK(&ibs->ws_tx_vote_off, ibs_wq_serial_tx_clock_vote_off);
  360. ibs->ibs_hu = (void *)hu;
  361. /* Assume we start with both sides asleep -- extra wakes OK */
  362. ibs->tx_ibs_state = HCI_IBS_TX_ASLEEP;
  363. ibs->rx_ibs_state = HCI_IBS_RX_ASLEEP;
  364. /* clocks actually on, but we start votes off */
  365. ibs->tx_vote = 0;
  366. ibs->rx_vote = 0;
  367. /* debug */
  368. ibs->ibs_sent_wacks = 0;
  369. ibs->ibs_sent_slps = 0;
  370. ibs->ibs_sent_wakes = 0;
  371. ibs->ibs_recv_wacks = 0;
  372. ibs->ibs_recv_slps = 0;
  373. ibs->ibs_recv_wakes = 0;
  374. ibs->vote_last_jif = jiffies;
  375. ibs->vote_on_ticks = 0;
  376. ibs->vote_off_ticks = 0;
  377. ibs->votes_on = 0;
  378. ibs->votes_off = 0;
  379. ibs->tx_votes_on = 0;
  380. ibs->tx_votes_off = 0;
  381. ibs->rx_votes_on = 0;
  382. ibs->rx_votes_off = 0;
  383. hu->priv = ibs;
  384. init_timer(&ibs->wake_retrans_timer);
  385. ibs->wake_retrans_timer.function = hci_ibs_wake_retrans_timeout;
  386. ibs->wake_retrans_timer.data = (u_long) hu;
  387. init_timer(&ibs->tx_idle_timer);
  388. ibs->tx_idle_timer.function = hci_ibs_tx_idle_timeout;
  389. ibs->tx_idle_timer.data = (u_long) hu;
  390. BT_INFO("HCI_IBS open, tx_idle_delay=%lu, wake_retrans=%lu",
  391. tx_idle_delay, wake_retrans);
  392. return 0;
  393. }
  394. void ibs_log_local_stats(struct ibs_struct *ibs)
  395. {
  396. BT_INFO("HCI_IBS stats: tx_idle_delay=%lu, wake_retrans=%lu",
  397. tx_idle_delay, wake_retrans);
  398. BT_INFO("HCI_IBS stats: tx_ibs_state=%lu, rx_ibs_state=%lu",
  399. ibs->tx_ibs_state, ibs->rx_ibs_state);
  400. BT_INFO("HCI_IBS stats: sent: sleep=%lu, wake=%lu, wake_ack=%lu",
  401. ibs->ibs_sent_slps, ibs->ibs_sent_wakes, ibs->ibs_sent_wacks);
  402. BT_INFO("HCI_IBS stats: recv: sleep=%lu, wake=%lu, wake_ack=%lu",
  403. ibs->ibs_recv_slps, ibs->ibs_recv_wakes, ibs->ibs_recv_wacks);
  404. BT_INFO("HCI_IBS stats: queues: txq=%s, txwaitq=%s",
  405. skb_queue_empty(&(ibs->txq)) ? "empty" : "full",
  406. skb_queue_empty(&(ibs->tx_wait_q)) ? "empty" : "full");
  407. BT_INFO("HCI_IBS stats: vote state: tx=%lu, rx=%lu",
  408. ibs->tx_vote, ibs->rx_vote);
  409. BT_INFO("HCI_IBS stats: tx votes cast: on=%lu, off=%lu",
  410. ibs->tx_votes_on, ibs->tx_votes_off);
  411. BT_INFO("HCI_IBS stats: rx votes cast: on=%lu, off=%lu",
  412. ibs->rx_votes_on, ibs->rx_votes_off);
  413. BT_INFO("HCI_IBS stats: msm_clock votes cast: on=%lu, off=%lu",
  414. ibs->votes_on, ibs->votes_off);
  415. BT_INFO("HCI_IBS stats: vote ticks: on=%lu, off=%lu",
  416. ibs->vote_on_ticks, ibs->vote_off_ticks);
  417. }
  418. /* Flush protocol data */
  419. static int ibs_flush(struct hci_uart *hu)
  420. {
  421. struct ibs_struct *ibs = hu->priv;
  422. BT_DBG("hu %p", hu);
  423. skb_queue_purge(&ibs->tx_wait_q);
  424. skb_queue_purge(&ibs->txq);
  425. return 0;
  426. }
  427. /* Close protocol */
  428. static int ibs_close(struct hci_uart *hu)
  429. {
  430. struct ibs_struct *ibs = hu->priv;
  431. BT_DBG("hu %p", hu);
  432. ibs_msm_serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
  433. ibs_log_local_stats(ibs);
  434. skb_queue_purge(&ibs->tx_wait_q);
  435. skb_queue_purge(&ibs->txq);
  436. del_timer(&ibs->tx_idle_timer);
  437. del_timer(&ibs->wake_retrans_timer);
  438. destroy_workqueue(ibs->workqueue);
  439. ibs->ibs_hu = NULL;
  440. kfree_skb(ibs->rx_skb);
  441. hu->priv = NULL;
  442. kfree(ibs);
  443. return 0;
  444. }
  445. /*
  446. * Called upon a wake-up-indication from the device
  447. */
  448. static void ibs_device_want_to_wakeup(struct hci_uart *hu)
  449. {
  450. unsigned long flags;
  451. struct ibs_struct *ibs = hu->priv;
  452. BT_DBG("hu %p", hu);
  453. /* lock hci_ibs state */
  454. spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
  455. /* debug */
  456. ibs->ibs_recv_wakes++;
  457. switch (ibs->rx_ibs_state) {
  458. case HCI_IBS_RX_ASLEEP:
  459. /* Make sure clock is on - we may have turned clock off since
  460. * receiving the wake up indicator
  461. */
  462. /* awake rx clock */
  463. queue_work(ibs->workqueue, &ibs->ws_awake_rx);
  464. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  465. return;
  466. case HCI_IBS_RX_AWAKE:
  467. /* Always acknowledge device wake up,
  468. * sending IBS message doesn't count as TX ON.
  469. */
  470. if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
  471. BT_ERR("cannot acknowledge device wake up");
  472. goto out;
  473. }
  474. ibs->ibs_sent_wacks++; /* debug */
  475. break;
  476. default:
  477. /* any other state is illegal */
  478. BT_ERR("received HCI_IBS_WAKE_IND in rx state %ld",
  479. ibs->rx_ibs_state);
  480. break;
  481. }
  482. out:
  483. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  484. /* actually send the packets */
  485. hci_uart_tx_wakeup(hu);
  486. }
  487. /*
  488. * Called upon a sleep-indication from the device
  489. */
  490. static void ibs_device_want_to_sleep(struct hci_uart *hu)
  491. {
  492. unsigned long flags;
  493. struct ibs_struct *ibs = hu->priv;
  494. BT_DBG("hu %p", hu);
  495. /* lock hci_ibs state */
  496. spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
  497. /* debug */
  498. ibs->ibs_recv_slps++;
  499. switch (ibs->rx_ibs_state) {
  500. case HCI_IBS_RX_AWAKE:
  501. /* update state */
  502. ibs->rx_ibs_state = HCI_IBS_RX_ASLEEP;
  503. /* vote off rx clock under workqueue */
  504. queue_work(ibs->workqueue, &ibs->ws_rx_vote_off);
  505. break;
  506. case HCI_IBS_RX_ASLEEP:
  507. /* deliberate fall-through */
  508. default:
  509. /* any other state is illegal */
  510. BT_ERR("received HCI_IBS_SLEEP_IND in rx state %ld",
  511. ibs->rx_ibs_state);
  512. break;
  513. }
  514. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  515. }
  516. /*
  517. * Called upon wake-up-acknowledgement from the device
  518. */
  519. static void ibs_device_woke_up(struct hci_uart *hu)
  520. {
  521. unsigned long flags;
  522. struct ibs_struct *ibs = hu->priv;
  523. struct sk_buff *skb = NULL;
  524. BT_DBG("hu %p", hu);
  525. /* lock hci_ibs state */
  526. spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
  527. /* debug */
  528. ibs->ibs_recv_wacks++;
  529. switch (ibs->tx_ibs_state) {
  530. case HCI_IBS_TX_ASLEEP:
  531. /* This could be spurrious rx wake on the BT chip.
  532. * Send it another SLEEP othwise it will stay awake. */
  533. default:
  534. BT_ERR("received HCI_IBS_WAKE_ACK in tx state %ld",
  535. ibs->tx_ibs_state);
  536. break;
  537. case HCI_IBS_TX_AWAKE:
  538. /* expect one if we send 2 WAKEs */
  539. BT_DBG("received HCI_IBS_WAKE_ACK in tx state %ld",
  540. ibs->tx_ibs_state);
  541. break;
  542. case HCI_IBS_TX_WAKING:
  543. /* send pending packets */
  544. while ((skb = skb_dequeue(&ibs->tx_wait_q)))
  545. skb_queue_tail(&ibs->txq, skb);
  546. /* switch timers and change state to HCI_IBS_TX_AWAKE */
  547. del_timer(&ibs->wake_retrans_timer);
  548. mod_timer(&ibs->tx_idle_timer, jiffies + msecs_to_jiffies(tx_idle_delay));
  549. ibs->tx_ibs_state = HCI_IBS_TX_AWAKE;
  550. }
  551. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  552. /* actually send the packets */
  553. hci_uart_tx_wakeup(hu);
  554. }
  555. /* Enqueue frame for transmittion (padding, crc, etc) */
  556. /* may be called from two simultaneous tasklets */
  557. static int ibs_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  558. {
  559. unsigned long flags = 0;
  560. struct ibs_struct *ibs = hu->priv;
  561. BT_DBG("hu %p skb %p", hu, skb);
  562. /* Prepend skb with frame type */
  563. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  564. /* lock hci_ibs state */
  565. spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
  566. /* act according to current state */
  567. switch (ibs->tx_ibs_state) {
  568. case HCI_IBS_TX_AWAKE:
  569. BT_DBG("device awake, sending normally");
  570. skb_queue_tail(&ibs->txq, skb);
  571. mod_timer(&ibs->tx_idle_timer, jiffies + msecs_to_jiffies(tx_idle_delay));
  572. break;
  573. case HCI_IBS_TX_ASLEEP:
  574. BT_DBG("device asleep, waking up and queueing packet");
  575. /* save packet for later */
  576. skb_queue_tail(&ibs->tx_wait_q, skb);
  577. ibs->tx_ibs_state = HCI_IBS_TX_WAKING;
  578. /* schedule a work queue to wake up device */
  579. queue_work(ibs->workqueue, &ibs->ws_awake_device);
  580. break;
  581. case HCI_IBS_TX_WAKING:
  582. BT_DBG("device waking up, queueing packet");
  583. /* transient state; just keep packet for later */
  584. skb_queue_tail(&ibs->tx_wait_q, skb);
  585. break;
  586. default:
  587. BT_ERR("illegal tx state: %ld (losing packet)",
  588. ibs->tx_ibs_state);
  589. kfree_skb(skb);
  590. break;
  591. }
  592. spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
  593. return 0;
  594. }
  595. static inline int ibs_check_data_len(struct ibs_struct *ibs, int len)
  596. {
  597. register int room = skb_tailroom(ibs->rx_skb);
  598. BT_DBG("len %d room %d", len, room);
  599. if (!len) {
  600. hci_recv_frame(ibs->rx_skb);
  601. } else if (len > room) {
  602. BT_ERR("Data length is too large");
  603. kfree_skb(ibs->rx_skb);
  604. } else {
  605. ibs->rx_state = HCI_IBS_W4_DATA;
  606. ibs->rx_count = len;
  607. return len;
  608. }
  609. ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
  610. ibs->rx_skb = NULL;
  611. ibs->rx_count = 0;
  612. return 0;
  613. }
  614. /* Recv data */
  615. static int ibs_recv(struct hci_uart *hu, void *data, int count)
  616. {
  617. struct ibs_struct *ibs = hu->priv;
  618. register char *ptr;
  619. struct hci_event_hdr *eh;
  620. struct hci_acl_hdr *ah;
  621. struct hci_sco_hdr *sh;
  622. register int len, type, dlen;
  623. BT_DBG("hu %p count %d rx_state %ld rx_count %ld",
  624. hu, count, ibs->rx_state, ibs->rx_count);
  625. ptr = data;
  626. while (count) {
  627. if (ibs->rx_count) {
  628. len = min_t(unsigned int, ibs->rx_count, count);
  629. memcpy(skb_put(ibs->rx_skb, len), ptr, len);
  630. ibs->rx_count -= len; count -= len; ptr += len;
  631. if (ibs->rx_count)
  632. continue;
  633. switch (ibs->rx_state) {
  634. case HCI_IBS_W4_DATA:
  635. BT_DBG("Complete data");
  636. hci_recv_frame(ibs->rx_skb);
  637. ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
  638. ibs->rx_skb = NULL;
  639. continue;
  640. case HCI_IBS_W4_EVENT_HDR:
  641. eh = (struct hci_event_hdr *) ibs->rx_skb->data;
  642. BT_DBG("Event header: evt 0x%2.2x plen %d",
  643. eh->evt, eh->plen);
  644. ibs_check_data_len(ibs, eh->plen);
  645. continue;
  646. case HCI_IBS_W4_ACL_HDR:
  647. ah = (struct hci_acl_hdr *) ibs->rx_skb->data;
  648. dlen = __le16_to_cpu(ah->dlen);
  649. BT_DBG("ACL header: dlen %d", dlen);
  650. ibs_check_data_len(ibs, dlen);
  651. continue;
  652. case HCI_IBS_W4_SCO_HDR:
  653. sh = (struct hci_sco_hdr *) ibs->rx_skb->data;
  654. BT_DBG("SCO header: dlen %d", sh->dlen);
  655. ibs_check_data_len(ibs, sh->dlen);
  656. continue;
  657. }
  658. }
  659. /* HCI_IBS_W4_PACKET_TYPE */
  660. switch (*ptr) {
  661. case HCI_EVENT_PKT:
  662. BT_DBG("Event packet");
  663. ibs->rx_state = HCI_IBS_W4_EVENT_HDR;
  664. ibs->rx_count = HCI_EVENT_HDR_SIZE;
  665. type = HCI_EVENT_PKT;
  666. break;
  667. case HCI_ACLDATA_PKT:
  668. BT_DBG("ACL packet");
  669. ibs->rx_state = HCI_IBS_W4_ACL_HDR;
  670. ibs->rx_count = HCI_ACL_HDR_SIZE;
  671. type = HCI_ACLDATA_PKT;
  672. break;
  673. case HCI_SCODATA_PKT:
  674. BT_DBG("SCO packet");
  675. ibs->rx_state = HCI_IBS_W4_SCO_HDR;
  676. ibs->rx_count = HCI_SCO_HDR_SIZE;
  677. type = HCI_SCODATA_PKT;
  678. break;
  679. /* HCI_IBS signals */
  680. case HCI_IBS_SLEEP_IND:
  681. BT_DBG("HCI_IBS_SLEEP_IND packet");
  682. ibs_device_want_to_sleep(hu);
  683. ptr++; count--;
  684. continue;
  685. case HCI_IBS_WAKE_IND:
  686. BT_DBG("HCI_IBS_WAKE_IND packet");
  687. ibs_device_want_to_wakeup(hu);
  688. ptr++; count--;
  689. continue;
  690. case HCI_IBS_WAKE_ACK:
  691. BT_DBG("HCI_IBS_WAKE_ACK packet");
  692. ibs_device_woke_up(hu);
  693. ptr++; count--;
  694. continue;
  695. default:
  696. BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
  697. hu->hdev->stat.err_rx++;
  698. ptr++; count--;
  699. continue;
  700. };
  701. ptr++; count--;
  702. /* Allocate packet */
  703. ibs->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
  704. if (!ibs->rx_skb) {
  705. BT_ERR("Can't allocate mem for new packet");
  706. ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
  707. ibs->rx_count = 0;
  708. return 0;
  709. }
  710. ibs->rx_skb->dev = (void *) hu->hdev;
  711. bt_cb(ibs->rx_skb)->pkt_type = type;
  712. }
  713. return count;
  714. }
  715. static struct sk_buff *ibs_dequeue(struct hci_uart *hu)
  716. {
  717. struct ibs_struct *ibs = hu->priv;
  718. return skb_dequeue(&ibs->txq);
  719. }
  720. static struct hci_uart_proto ibs_p = {
  721. .id = HCI_UART_IBS,
  722. .open = ibs_open,
  723. .close = ibs_close,
  724. .recv = ibs_recv,
  725. .enqueue = ibs_enqueue,
  726. .dequeue = ibs_dequeue,
  727. .flush = ibs_flush,
  728. };
  729. int ibs_init(void)
  730. {
  731. int err = hci_uart_register_proto(&ibs_p);
  732. if (!err)
  733. BT_INFO("HCI_IBS protocol initialized");
  734. else
  735. BT_ERR("HCI_IBS protocol registration failed");
  736. return err;
  737. }
  738. int ibs_deinit(void)
  739. {
  740. return hci_uart_unregister_proto(&ibs_p);
  741. }
  742. module_param(wake_retrans, ulong, 0644);
  743. MODULE_PARM_DESC(wake_retrans, "Delay (ms) to retransmit WAKE_IND");
  744. module_param(tx_idle_delay, ulong, 0644);
  745. MODULE_PARM_DESC(tx_idle_delay, "Delay (ms) since last tx for SLEEP_IND");