blob: e6fdc1f841c6f3cd5b81303e07e9ec04a4a8dc10 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
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.
Duy Truonge833aca2013-02-12 13:35:08 -08008 * Copyright (c) 2010, 2012 The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07009 *
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
31#include <linux/module.h>
32#include <linux/kernel.h>
33
34#include <linux/init.h>
35#include <linux/sched.h>
36#include <linux/types.h>
37#include <linux/fcntl.h>
38#include <linux/interrupt.h>
39#include <linux/ptrace.h>
40#include <linux/poll.h>
41
42#include <linux/slab.h>
43#include <linux/tty.h>
44#include <linux/errno.h>
45#include <linux/string.h>
46#include <linux/signal.h>
47#include <linux/ioctl.h>
48#include <linux/timer.h>
49#include <linux/skbuff.h>
50#include <linux/serial_core.h>
51
52#ifdef CONFIG_SERIAL_MSM_HS
53#include <mach/msm_serial_hs.h>
54#endif
55
56#include <net/bluetooth/bluetooth.h>
57#include <net/bluetooth/hci_core.h>
58
59#include "hci_uart.h"
60
61/* HCI_IBS protocol messages */
62#define HCI_IBS_SLEEP_IND 0xFE
63#define HCI_IBS_WAKE_IND 0xFD
64#define HCI_IBS_WAKE_ACK 0xFC
65
66/* HCI_IBS receiver States */
67#define HCI_IBS_W4_PACKET_TYPE 0
68#define HCI_IBS_W4_EVENT_HDR 1
69#define HCI_IBS_W4_ACL_HDR 2
70#define HCI_IBS_W4_SCO_HDR 3
71#define HCI_IBS_W4_DATA 4
72
73/* HCI_IBS transmit side sleep protocol states */
74enum tx_ibs_states_e {
75 HCI_IBS_TX_ASLEEP,
76 HCI_IBS_TX_WAKING,
77 HCI_IBS_TX_AWAKE,
78};
79
80/* HCI_IBS receive side sleep protocol states */
81enum rx_states_e {
82 HCI_IBS_RX_ASLEEP,
83 HCI_IBS_RX_AWAKE,
84};
85
86/* HCI_IBS transmit and receive side clock state vote */
87enum hci_ibs_clock_state_vote_e {
88 HCI_IBS_VOTE_STATS_UPDATE,
89 HCI_IBS_TX_VOTE_CLOCK_ON,
90 HCI_IBS_TX_VOTE_CLOCK_OFF,
91 HCI_IBS_RX_VOTE_CLOCK_ON,
92 HCI_IBS_RX_VOTE_CLOCK_OFF,
93};
94
95static unsigned long wake_retrans = 1;
96static unsigned long tx_idle_delay = (HZ * 2);
97
98struct hci_ibs_cmd {
99 u8 cmd;
100} __attribute__((packed));
101
102struct ibs_struct {
103 unsigned long rx_state;
104 unsigned long rx_count;
105 struct sk_buff *rx_skb;
106 struct sk_buff_head txq;
107 struct sk_buff_head tx_wait_q; /* HCI_IBS wait queue */
108 spinlock_t hci_ibs_lock; /* HCI_IBS state lock */
109 unsigned long tx_ibs_state; /* HCI_IBS transmit side power state */
110 unsigned long rx_ibs_state; /* HCI_IBS receive side power state */
111 unsigned long tx_vote; /* clock must be on for TX */
112 unsigned long rx_vote; /* clock must be on for RX */
113 struct timer_list tx_idle_timer;
114 struct timer_list wake_retrans_timer;
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530115 struct workqueue_struct *workqueue;
116 struct work_struct ws_awake_rx;
117 struct work_struct ws_awake_device;
118 struct work_struct ws_rx_vote_off;
119 struct work_struct ws_tx_vote_off;
120 void *ibs_hu; /* keeps the hci_uart pointer for reference */
121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 /* debug */
123 unsigned long ibs_sent_wacks;
124 unsigned long ibs_sent_slps;
125 unsigned long ibs_sent_wakes;
126 unsigned long ibs_recv_wacks;
127 unsigned long ibs_recv_slps;
128 unsigned long ibs_recv_wakes;
129 unsigned long vote_last_jif;
130 unsigned long vote_on_ticks;
131 unsigned long vote_off_ticks;
132 unsigned long tx_votes_on;
133 unsigned long rx_votes_on;
134 unsigned long tx_votes_off;
135 unsigned long rx_votes_off;
136 unsigned long votes_on;
137 unsigned long votes_off;
138};
139
140#ifdef CONFIG_SERIAL_MSM_HS
141static void __ibs_msm_serial_clock_on(struct tty_struct *tty)
142{
143 struct uart_state *state = tty->driver_data;
144 struct uart_port *port = state->uart_port;
145
146 msm_hs_request_clock_on(port);
147}
148
149static void __ibs_msm_serial_clock_request_off(struct tty_struct *tty)
150{
151 struct uart_state *state = tty->driver_data;
152 struct uart_port *port = state->uart_port;
153
154 msm_hs_request_clock_off(port);
155}
156#else
157static inline void __ibs_msm_serial_clock_on(struct tty_struct *tty) {}
158static inline void __ibs_msm_serial_clock_request_off(struct tty_struct *tty) {}
159#endif
160
161/* clock_vote needs to be called with the ibs lock held */
162static void ibs_msm_serial_clock_vote(unsigned long vote, struct hci_uart *hu)
163{
164 struct ibs_struct *ibs = hu->priv;
165
166 unsigned long old_vote = (ibs->tx_vote | ibs->rx_vote);
167 unsigned long new_vote;
168
169 switch (vote) {
170 default: /* error */
171 BT_ERR("voting irregularity");
172 return;
173 case HCI_IBS_VOTE_STATS_UPDATE:
174 if (old_vote)
175 ibs->vote_off_ticks += (jiffies - ibs->vote_last_jif);
176 else
177 ibs->vote_on_ticks += (jiffies - ibs->vote_last_jif);
178 return;
179 case HCI_IBS_TX_VOTE_CLOCK_ON:
180 ibs->tx_vote = 1;
181 ibs->tx_votes_on++;
182 new_vote = 1;
183 break;
184 case HCI_IBS_RX_VOTE_CLOCK_ON:
185 ibs->rx_vote = 1;
186 ibs->rx_votes_on++;
187 new_vote = 1;
188 break;
189 case HCI_IBS_TX_VOTE_CLOCK_OFF:
190 ibs->tx_vote = 0;
191 ibs->tx_votes_off++;
192 new_vote = ibs->rx_vote | ibs->tx_vote;
193 break;
194 case HCI_IBS_RX_VOTE_CLOCK_OFF:
195 ibs->rx_vote = 0;
196 ibs->rx_votes_off++;
197 new_vote = ibs->rx_vote | ibs->tx_vote;
198 break;
199 }
200 if (new_vote != old_vote) {
201 if (new_vote)
202 __ibs_msm_serial_clock_on(hu->tty);
203 else
204 __ibs_msm_serial_clock_request_off(hu->tty);
205
206 BT_DBG("HCIUART_IBS: vote msm_serial_hs clock %lu(%lu)",
207 new_vote, vote);
208 /* debug */
209 if (new_vote) {
210 ibs->votes_on++;
211 ibs->vote_off_ticks += (jiffies - ibs->vote_last_jif);
212 } else {
213 ibs->votes_off++;
214 ibs->vote_on_ticks += (jiffies - ibs->vote_last_jif);
215 }
216 ibs->vote_last_jif = jiffies;
217 }
218}
219
220/*
221 * Builds and sends an HCI_IBS command packet.
222 * These are very simple packets with only 1 cmd byte
223 */
224static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
225{
226 int err = 0;
227 struct sk_buff *skb = NULL;
228 struct ibs_struct *ibs = hu->priv;
229 struct hci_ibs_cmd *hci_ibs_packet;
230
231 BT_DBG("hu %p cmd 0x%x", hu, cmd);
232
233 /* allocate packet */
234 skb = bt_skb_alloc(1, GFP_ATOMIC);
235 if (!skb) {
236 BT_ERR("cannot allocate memory for HCI_IBS packet");
237 err = -ENOMEM;
238 goto out;
239 }
240
241 /* prepare packet */
242 hci_ibs_packet = (struct hci_ibs_cmd *) skb_put(skb, 1);
243 hci_ibs_packet->cmd = cmd;
244 skb->dev = (void *) hu->hdev;
245
246 /* send packet */
247 skb_queue_tail(&ibs->txq, skb);
248out:
249 return err;
250}
251
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530252static void ibs_wq_awake_device(struct work_struct *work)
253{
254 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
255 ws_awake_device);
256 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
257
258 BT_DBG(" %p ", hu);
259
260 /* Vote for serial clock */
261 ibs_msm_serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
262
263 spin_lock(&ibs->hci_ibs_lock);
264
265 /* send wake indication to device */
266 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
267 BT_ERR("cannot send WAKE to device");
268
269 ibs->ibs_sent_wakes++; /* debug */
270
271 /* start retransmit timer */
272 mod_timer(&ibs->wake_retrans_timer, jiffies + wake_retrans);
273
274 spin_unlock(&ibs->hci_ibs_lock);
275}
276
277static void ibs_wq_awake_rx(struct work_struct *work)
278{
279 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
280 ws_awake_rx);
281 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
282
283 BT_DBG(" %p ", hu);
284
285 ibs_msm_serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
286
287 spin_lock(&ibs->hci_ibs_lock);
288 ibs->rx_ibs_state = HCI_IBS_RX_AWAKE;
289 /* Always acknowledge device wake up,
290 * sending IBS message doesn't count as TX ON
291 */
292 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
293 BT_ERR("cannot acknowledge device wake up");
294
295 ibs->ibs_sent_wacks++; /* debug */
296
297 spin_unlock(&ibs->hci_ibs_lock);
298 /* actually send the packets */
299 hci_uart_tx_wakeup(hu);
300
301}
302
303static void ibs_wq_serial_rx_clock_vote_off(struct work_struct *work)
304{
305 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
306 ws_rx_vote_off);
307 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
308
309 BT_DBG(" %p ", hu);
310
311 ibs_msm_serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
312
313}
314
315static void ibs_wq_serial_tx_clock_vote_off(struct work_struct *work)
316{
317 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
318 ws_tx_vote_off);
319 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
320
321 BT_DBG(" %p ", hu);
322
323 hci_uart_tx_wakeup(hu); /* run HCI tx handling unlocked */
324
325 /* now that message queued to tty driver, vote for tty clocks off */
326 /* It is up to the tty driver to pend the clocks off until tx done. */
327 ibs_msm_serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
328
329}
330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331static void hci_ibs_tx_idle_timeout(unsigned long arg)
332{
333 struct hci_uart *hu = (struct hci_uart *) arg;
334 struct ibs_struct *ibs = hu->priv;
335 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336
337 BT_DBG("hu %p idle timeout in %lu state", hu, ibs->tx_ibs_state);
338
339 spin_lock_irqsave_nested(&ibs->hci_ibs_lock,
340 flags, SINGLE_DEPTH_NESTING);
341
342 switch (ibs->tx_ibs_state) {
343 default:
344 case HCI_IBS_TX_ASLEEP:
345 case HCI_IBS_TX_WAKING:
346 BT_ERR("spurrious timeout in tx state %ld", ibs->tx_ibs_state);
347 goto out;
348 case HCI_IBS_TX_AWAKE: /* TX_IDLE, go to SLEEP */
349 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
350 BT_ERR("cannot send SLEEP to device");
351 goto out;
352 }
353 ibs->tx_ibs_state = HCI_IBS_TX_ASLEEP;
354 ibs->ibs_sent_slps++; /* debug */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355 break;
356 }
357
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530358 queue_work(ibs->workqueue, &ibs->ws_tx_vote_off);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360out:
361 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
362}
363
364static void hci_ibs_wake_retrans_timeout(unsigned long arg)
365{
366 struct hci_uart *hu = (struct hci_uart *) arg;
367 struct ibs_struct *ibs = hu->priv;
368 unsigned long flags;
369 unsigned long retransmit = 0;
370
371 BT_DBG("hu %p wake retransmit timeout in %lu state",
372 hu, ibs->tx_ibs_state);
373
374 spin_lock_irqsave_nested(&ibs->hci_ibs_lock,
375 flags, SINGLE_DEPTH_NESTING);
376
377 switch (ibs->tx_ibs_state) {
378 default:
379 case HCI_IBS_TX_ASLEEP:
380 case HCI_IBS_TX_AWAKE:
381 BT_ERR("spurrious timeout tx state %ld", ibs->tx_ibs_state);
382 goto out;
383 case HCI_IBS_TX_WAKING: /* No WAKE_ACK, retransmit WAKE */
384 retransmit = 1;
385 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
386 BT_ERR("cannot acknowledge device wake up");
387 goto out;
388 }
389 ibs->ibs_sent_wakes++; /* debug */
390 mod_timer(&ibs->wake_retrans_timer, jiffies + wake_retrans);
391 break;
392 }
393out:
394 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
395 if (retransmit)
396 hci_uart_tx_wakeup(hu);
397}
398
399/* Initialize protocol */
400static int ibs_open(struct hci_uart *hu)
401{
402 struct ibs_struct *ibs;
403
404 BT_DBG("hu %p", hu);
405
406 ibs = kzalloc(sizeof(*ibs), GFP_ATOMIC);
407 if (!ibs)
408 return -ENOMEM;
409
410 skb_queue_head_init(&ibs->txq);
411 skb_queue_head_init(&ibs->tx_wait_q);
412 spin_lock_init(&ibs->hci_ibs_lock);
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530413 ibs->workqueue = create_singlethread_workqueue("ibs_wq");
414 if (!ibs->workqueue) {
415 BT_ERR("IBS Workqueue not initialized properly");
416 kfree(ibs);
417 return -ENOMEM;
418 }
419
420 INIT_WORK(&ibs->ws_awake_rx, ibs_wq_awake_rx);
421 INIT_WORK(&ibs->ws_awake_device, ibs_wq_awake_device);
422 INIT_WORK(&ibs->ws_rx_vote_off, ibs_wq_serial_rx_clock_vote_off);
423 INIT_WORK(&ibs->ws_tx_vote_off, ibs_wq_serial_tx_clock_vote_off);
424
425 ibs->ibs_hu = (void *)hu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426
427 /* Assume we start with both sides asleep -- extra wakes OK */
428 ibs->tx_ibs_state = HCI_IBS_TX_ASLEEP;
429 ibs->rx_ibs_state = HCI_IBS_RX_ASLEEP;
430 /* clocks actually on, but we start votes off */
431 ibs->tx_vote = 0;
432 ibs->rx_vote = 0;
433
434 /* debug */
435 ibs->ibs_sent_wacks = 0;
436 ibs->ibs_sent_slps = 0;
437 ibs->ibs_sent_wakes = 0;
438 ibs->ibs_recv_wacks = 0;
439 ibs->ibs_recv_slps = 0;
440 ibs->ibs_recv_wakes = 0;
441 ibs->vote_last_jif = jiffies;
442 ibs->vote_on_ticks = 0;
443 ibs->vote_off_ticks = 0;
444 ibs->votes_on = 0;
445 ibs->votes_off = 0;
446 ibs->tx_votes_on = 0;
447 ibs->tx_votes_off = 0;
448 ibs->rx_votes_on = 0;
449 ibs->rx_votes_off = 0;
450
451 hu->priv = ibs;
452
453 init_timer(&ibs->wake_retrans_timer);
454 ibs->wake_retrans_timer.function = hci_ibs_wake_retrans_timeout;
455 ibs->wake_retrans_timer.data = (u_long) hu;
456
457 init_timer(&ibs->tx_idle_timer);
458 ibs->tx_idle_timer.function = hci_ibs_tx_idle_timeout;
459 ibs->tx_idle_timer.data = (u_long) hu;
460
461 BT_INFO("HCI_IBS open, tx_idle_delay=%lu, wake_retrans=%lu",
462 tx_idle_delay, wake_retrans);
463
464 return 0;
465}
466
467void ibs_log_local_stats(struct ibs_struct *ibs)
468{
469 BT_INFO("HCI_IBS stats: tx_idle_delay=%lu, wake_retrans=%lu",
470 tx_idle_delay, wake_retrans);
471
472 BT_INFO("HCI_IBS stats: tx_ibs_state=%lu, rx_ibs_state=%lu",
473 ibs->tx_ibs_state, ibs->rx_ibs_state);
474 BT_INFO("HCI_IBS stats: sent: sleep=%lu, wake=%lu, wake_ack=%lu",
475 ibs->ibs_sent_slps, ibs->ibs_sent_wakes, ibs->ibs_sent_wacks);
476 BT_INFO("HCI_IBS stats: recv: sleep=%lu, wake=%lu, wake_ack=%lu",
477 ibs->ibs_recv_slps, ibs->ibs_recv_wakes, ibs->ibs_recv_wacks);
478
479 BT_INFO("HCI_IBS stats: queues: txq=%s, txwaitq=%s",
480 skb_queue_empty(&(ibs->txq)) ? "empty" : "full",
481 skb_queue_empty(&(ibs->tx_wait_q)) ? "empty" : "full");
482
483 BT_INFO("HCI_IBS stats: vote state: tx=%lu, rx=%lu",
484 ibs->tx_vote, ibs->rx_vote);
485 BT_INFO("HCI_IBS stats: tx votes cast: on=%lu, off=%lu",
486 ibs->tx_votes_on, ibs->tx_votes_off);
487 BT_INFO("HCI_IBS stats: rx votes cast: on=%lu, off=%lu",
488 ibs->rx_votes_on, ibs->rx_votes_off);
489 BT_INFO("HCI_IBS stats: msm_clock votes cast: on=%lu, off=%lu",
490 ibs->votes_on, ibs->votes_off);
491 BT_INFO("HCI_IBS stats: vote ticks: on=%lu, off=%lu",
492 ibs->vote_on_ticks, ibs->vote_off_ticks);
493}
494
495/* Flush protocol data */
496static int ibs_flush(struct hci_uart *hu)
497{
498 struct ibs_struct *ibs = hu->priv;
499
500 BT_DBG("hu %p", hu);
501
502 skb_queue_purge(&ibs->tx_wait_q);
503 skb_queue_purge(&ibs->txq);
504
505 return 0;
506}
507
508/* Close protocol */
509static int ibs_close(struct hci_uart *hu)
510{
511 struct ibs_struct *ibs = hu->priv;
512
513 BT_DBG("hu %p", hu);
514
515 ibs_msm_serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
516 ibs_log_local_stats(ibs);
517
518 skb_queue_purge(&ibs->tx_wait_q);
519 skb_queue_purge(&ibs->txq);
520 del_timer(&ibs->tx_idle_timer);
521 del_timer(&ibs->wake_retrans_timer);
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530522 destroy_workqueue(ibs->workqueue);
523 ibs->ibs_hu = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524
525 kfree_skb(ibs->rx_skb);
526
527 hu->priv = NULL;
528
529 kfree(ibs);
530
531 return 0;
532}
533
534/*
535 * Called upon a wake-up-indication from the device
536 */
537static void ibs_device_want_to_wakeup(struct hci_uart *hu)
538{
539 unsigned long flags;
540 struct ibs_struct *ibs = hu->priv;
541
542 BT_DBG("hu %p", hu);
543
544 /* lock hci_ibs state */
545 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
546
547 /* debug */
548 ibs->ibs_recv_wakes++;
549
550 switch (ibs->rx_ibs_state) {
551 case HCI_IBS_RX_ASLEEP:
552 /* Make sure clock is on - we may have turned clock off since
553 * receiving the wake up indicator
554 */
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530555 /* awake rx clock */
556 queue_work(ibs->workqueue, &ibs->ws_awake_rx);
557 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
558 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 case HCI_IBS_RX_AWAKE:
560 /* Always acknowledge device wake up,
561 * sending IBS message doesn't count as TX ON.
562 */
563 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
564 BT_ERR("cannot acknowledge device wake up");
565 goto out;
566 }
567 ibs->ibs_sent_wacks++; /* debug */
568 break;
569 default:
570 /* any other state is illegal */
571 BT_ERR("received HCI_IBS_WAKE_IND in rx state %ld",
572 ibs->rx_ibs_state);
573 break;
574 }
575
576out:
577 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
578
579 /* actually send the packets */
580 hci_uart_tx_wakeup(hu);
581}
582
583/*
584 * Called upon a sleep-indication from the device
585 */
586static void ibs_device_want_to_sleep(struct hci_uart *hu)
587{
588 unsigned long flags;
589 struct ibs_struct *ibs = hu->priv;
590
591 BT_DBG("hu %p", hu);
592
593 /* lock hci_ibs state */
594 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
595
596 /* debug */
597 ibs->ibs_recv_slps++;
598
599 switch (ibs->rx_ibs_state) {
600 case HCI_IBS_RX_AWAKE:
601 /* update state */
602 ibs->rx_ibs_state = HCI_IBS_RX_ASLEEP;
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530603 /* vote off rx clock under workqueue */
604 queue_work(ibs->workqueue, &ibs->ws_rx_vote_off);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 break;
606 case HCI_IBS_RX_ASLEEP:
607 /* deliberate fall-through */
608 default:
609 /* any other state is illegal */
610 BT_ERR("received HCI_IBS_SLEEP_IND in rx state %ld",
611 ibs->rx_ibs_state);
612 break;
613 }
614
615 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
616}
617
618/*
619 * Called upon wake-up-acknowledgement from the device
620 */
621static void ibs_device_woke_up(struct hci_uart *hu)
622{
623 unsigned long flags;
624 struct ibs_struct *ibs = hu->priv;
625 struct sk_buff *skb = NULL;
626
627 BT_DBG("hu %p", hu);
628
629 /* lock hci_ibs state */
630 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
631
632 /* debug */
633 ibs->ibs_recv_wacks++;
634
635 switch (ibs->tx_ibs_state) {
636 case HCI_IBS_TX_ASLEEP:
637 /* This could be spurrious rx wake on the BT chip.
638 * Send it another SLEEP othwise it will stay awake. */
639 default:
640 BT_ERR("received HCI_IBS_WAKE_ACK in tx state %ld",
641 ibs->tx_ibs_state);
642 break;
643 case HCI_IBS_TX_AWAKE:
644 /* expect one if we send 2 WAKEs */
645 BT_DBG("received HCI_IBS_WAKE_ACK in tx state %ld",
646 ibs->tx_ibs_state);
647 break;
648 case HCI_IBS_TX_WAKING:
649 /* send pending packets */
650 while ((skb = skb_dequeue(&ibs->tx_wait_q)))
651 skb_queue_tail(&ibs->txq, skb);
652 /* switch timers and change state to HCI_IBS_TX_AWAKE */
653 del_timer(&ibs->wake_retrans_timer);
654 mod_timer(&ibs->tx_idle_timer, jiffies + tx_idle_delay);
655 ibs->tx_ibs_state = HCI_IBS_TX_AWAKE;
656 }
657
658 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
659
660 /* actually send the packets */
661 hci_uart_tx_wakeup(hu);
662}
663
664/* Enqueue frame for transmittion (padding, crc, etc) */
665/* may be called from two simultaneous tasklets */
666static int ibs_enqueue(struct hci_uart *hu, struct sk_buff *skb)
667{
668 unsigned long flags = 0;
669 struct ibs_struct *ibs = hu->priv;
670
671 BT_DBG("hu %p skb %p", hu, skb);
672
673 /* Prepend skb with frame type */
674 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
675
676 /* lock hci_ibs state */
677 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
678
679 /* act according to current state */
680 switch (ibs->tx_ibs_state) {
681 case HCI_IBS_TX_AWAKE:
682 BT_DBG("device awake, sending normally");
683 skb_queue_tail(&ibs->txq, skb);
684 mod_timer(&ibs->tx_idle_timer, jiffies + tx_idle_delay);
685 break;
686
687 case HCI_IBS_TX_ASLEEP:
688 BT_DBG("device asleep, waking up and queueing packet");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 /* save packet for later */
690 skb_queue_tail(&ibs->tx_wait_q, skb);
Ram Mohan Korukonda1aa44762012-07-24 18:50:56 +0530691
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 ibs->tx_ibs_state = HCI_IBS_TX_WAKING;
Ram Mohan Korukondad01321c2012-08-19 13:44:57 +0530693 /* schedule a work queue to wake up device */
694 queue_work(ibs->workqueue, &ibs->ws_awake_device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 break;
696
697 case HCI_IBS_TX_WAKING:
698 BT_DBG("device waking up, queueing packet");
699 /* transient state; just keep packet for later */
700 skb_queue_tail(&ibs->tx_wait_q, skb);
701 break;
702
703 default:
704 BT_ERR("illegal tx state: %ld (losing packet)",
705 ibs->tx_ibs_state);
706 kfree_skb(skb);
707 break;
708 }
709
710 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
711
712 return 0;
713}
714
715static inline int ibs_check_data_len(struct ibs_struct *ibs, int len)
716{
717 register int room = skb_tailroom(ibs->rx_skb);
718
719 BT_DBG("len %d room %d", len, room);
720
721 if (!len) {
722 hci_recv_frame(ibs->rx_skb);
723 } else if (len > room) {
724 BT_ERR("Data length is too large");
725 kfree_skb(ibs->rx_skb);
726 } else {
727 ibs->rx_state = HCI_IBS_W4_DATA;
728 ibs->rx_count = len;
729 return len;
730 }
731
732 ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
733 ibs->rx_skb = NULL;
734 ibs->rx_count = 0;
735
736 return 0;
737}
738
739/* Recv data */
740static int ibs_recv(struct hci_uart *hu, void *data, int count)
741{
742 struct ibs_struct *ibs = hu->priv;
743 register char *ptr;
744 struct hci_event_hdr *eh;
745 struct hci_acl_hdr *ah;
746 struct hci_sco_hdr *sh;
747 register int len, type, dlen;
748
749 BT_DBG("hu %p count %d rx_state %ld rx_count %ld",
750 hu, count, ibs->rx_state, ibs->rx_count);
751
752 ptr = data;
753 while (count) {
754 if (ibs->rx_count) {
755 len = min_t(unsigned int, ibs->rx_count, count);
756 memcpy(skb_put(ibs->rx_skb, len), ptr, len);
757 ibs->rx_count -= len; count -= len; ptr += len;
758
759 if (ibs->rx_count)
760 continue;
761
762 switch (ibs->rx_state) {
763 case HCI_IBS_W4_DATA:
764 BT_DBG("Complete data");
765 hci_recv_frame(ibs->rx_skb);
766
767 ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
768 ibs->rx_skb = NULL;
769 continue;
770
771 case HCI_IBS_W4_EVENT_HDR:
772 eh = (struct hci_event_hdr *) ibs->rx_skb->data;
773
774 BT_DBG("Event header: evt 0x%2.2x plen %d",
775 eh->evt, eh->plen);
776
777 ibs_check_data_len(ibs, eh->plen);
778 continue;
779
780 case HCI_IBS_W4_ACL_HDR:
781 ah = (struct hci_acl_hdr *) ibs->rx_skb->data;
782 dlen = __le16_to_cpu(ah->dlen);
783
784 BT_DBG("ACL header: dlen %d", dlen);
785
786 ibs_check_data_len(ibs, dlen);
787 continue;
788
789 case HCI_IBS_W4_SCO_HDR:
790 sh = (struct hci_sco_hdr *) ibs->rx_skb->data;
791
792 BT_DBG("SCO header: dlen %d", sh->dlen);
793
794 ibs_check_data_len(ibs, sh->dlen);
795 continue;
796 }
797 }
798
799 /* HCI_IBS_W4_PACKET_TYPE */
800 switch (*ptr) {
801 case HCI_EVENT_PKT:
802 BT_DBG("Event packet");
803 ibs->rx_state = HCI_IBS_W4_EVENT_HDR;
804 ibs->rx_count = HCI_EVENT_HDR_SIZE;
805 type = HCI_EVENT_PKT;
806 break;
807
808 case HCI_ACLDATA_PKT:
809 BT_DBG("ACL packet");
810 ibs->rx_state = HCI_IBS_W4_ACL_HDR;
811 ibs->rx_count = HCI_ACL_HDR_SIZE;
812 type = HCI_ACLDATA_PKT;
813 break;
814
815 case HCI_SCODATA_PKT:
816 BT_DBG("SCO packet");
817 ibs->rx_state = HCI_IBS_W4_SCO_HDR;
818 ibs->rx_count = HCI_SCO_HDR_SIZE;
819 type = HCI_SCODATA_PKT;
820 break;
821
822 /* HCI_IBS signals */
823 case HCI_IBS_SLEEP_IND:
824 BT_DBG("HCI_IBS_SLEEP_IND packet");
825 ibs_device_want_to_sleep(hu);
826 ptr++; count--;
827 continue;
828
829 case HCI_IBS_WAKE_IND:
830 BT_DBG("HCI_IBS_WAKE_IND packet");
831 ibs_device_want_to_wakeup(hu);
832 ptr++; count--;
833 continue;
834
835 case HCI_IBS_WAKE_ACK:
836 BT_DBG("HCI_IBS_WAKE_ACK packet");
837 ibs_device_woke_up(hu);
838 ptr++; count--;
839 continue;
840
841 default:
842 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
843 hu->hdev->stat.err_rx++;
844 ptr++; count--;
845 continue;
846 };
847
848 ptr++; count--;
849
850 /* Allocate packet */
851 ibs->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
852 if (!ibs->rx_skb) {
853 BT_ERR("Can't allocate mem for new packet");
854 ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
855 ibs->rx_count = 0;
856 return 0;
857 }
858
859 ibs->rx_skb->dev = (void *) hu->hdev;
860 bt_cb(ibs->rx_skb)->pkt_type = type;
861 }
862
863 return count;
864}
865
866static struct sk_buff *ibs_dequeue(struct hci_uart *hu)
867{
868 struct ibs_struct *ibs = hu->priv;
869 return skb_dequeue(&ibs->txq);
870}
871
872static struct hci_uart_proto ibs_p = {
873 .id = HCI_UART_IBS,
874 .open = ibs_open,
875 .close = ibs_close,
876 .recv = ibs_recv,
877 .enqueue = ibs_enqueue,
878 .dequeue = ibs_dequeue,
879 .flush = ibs_flush,
880};
881
882int ibs_init(void)
883{
884 int err = hci_uart_register_proto(&ibs_p);
885
886 if (!err)
887 BT_INFO("HCI_IBS protocol initialized");
888 else
889 BT_ERR("HCI_IBS protocol registration failed");
890
891 return err;
892}
893
894int ibs_deinit(void)
895{
896 return hci_uart_unregister_proto(&ibs_p);
897}
898
899module_param(wake_retrans, ulong, 0644);
900MODULE_PARM_DESC(wake_retrans, "Delay (1/HZ) to retransmit WAKE_IND");
901
902module_param(tx_idle_delay, ulong, 0644);
903MODULE_PARM_DESC(tx_idle_delay, "Delay (1/HZ) since last tx for SLEEP_IND");