blob: acff5a5593b63e297770c8178038140bb4497c75 [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.
Ram Mohan Korukonda2fed6b62012-05-22 17:05:43 +05308 * Copyright (c) 2010, 2012 Code Aurora Forum. 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 Korukondad0063b62012-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 Korukondad0063b62012-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;
Ram Mohan Korukonda1fd4e402012-10-04 22:49:18 +0530257 unsigned long flags;
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530258
259 BT_DBG(" %p ", hu);
260
261 /* Vote for serial clock */
262 ibs_msm_serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_ON, hu);
263
Ram Mohan Korukonda1fd4e402012-10-04 22:49:18 +0530264 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530265
266 /* send wake indication to device */
267 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0)
268 BT_ERR("cannot send WAKE to device");
269
270 ibs->ibs_sent_wakes++; /* debug */
271
272 /* start retransmit timer */
273 mod_timer(&ibs->wake_retrans_timer, jiffies + wake_retrans);
274
Ram Mohan Korukonda1fd4e402012-10-04 22:49:18 +0530275 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
276
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530277}
278
279static void ibs_wq_awake_rx(struct work_struct *work)
280{
281 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
282 ws_awake_rx);
283 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
Ram Mohan Korukonda1fd4e402012-10-04 22:49:18 +0530284 unsigned long flags;
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530285
286 BT_DBG(" %p ", hu);
287
288 ibs_msm_serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_ON, hu);
289
Ram Mohan Korukonda1fd4e402012-10-04 22:49:18 +0530290 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
291
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530292 ibs->rx_ibs_state = HCI_IBS_RX_AWAKE;
293 /* Always acknowledge device wake up,
294 * sending IBS message doesn't count as TX ON
295 */
296 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0)
297 BT_ERR("cannot acknowledge device wake up");
298
299 ibs->ibs_sent_wacks++; /* debug */
300
Ram Mohan Korukonda1fd4e402012-10-04 22:49:18 +0530301 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
302
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530303 /* actually send the packets */
304 hci_uart_tx_wakeup(hu);
305
306}
307
308static void ibs_wq_serial_rx_clock_vote_off(struct work_struct *work)
309{
310 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
311 ws_rx_vote_off);
312 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
313
314 BT_DBG(" %p ", hu);
315
316 ibs_msm_serial_clock_vote(HCI_IBS_RX_VOTE_CLOCK_OFF, hu);
317
318}
319
320static void ibs_wq_serial_tx_clock_vote_off(struct work_struct *work)
321{
322 struct ibs_struct *ibs = container_of(work, struct ibs_struct,
323 ws_tx_vote_off);
324 struct hci_uart *hu = (struct hci_uart *)ibs->ibs_hu;
325
326 BT_DBG(" %p ", hu);
327
328 hci_uart_tx_wakeup(hu); /* run HCI tx handling unlocked */
329
330 /* now that message queued to tty driver, vote for tty clocks off */
331 /* It is up to the tty driver to pend the clocks off until tx done. */
332 ibs_msm_serial_clock_vote(HCI_IBS_TX_VOTE_CLOCK_OFF, hu);
333
334}
335
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336static void hci_ibs_tx_idle_timeout(unsigned long arg)
337{
338 struct hci_uart *hu = (struct hci_uart *) arg;
339 struct ibs_struct *ibs = hu->priv;
340 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341
342 BT_DBG("hu %p idle timeout in %lu state", hu, ibs->tx_ibs_state);
343
344 spin_lock_irqsave_nested(&ibs->hci_ibs_lock,
345 flags, SINGLE_DEPTH_NESTING);
346
347 switch (ibs->tx_ibs_state) {
348 default:
349 case HCI_IBS_TX_ASLEEP:
350 case HCI_IBS_TX_WAKING:
351 BT_ERR("spurrious timeout in tx state %ld", ibs->tx_ibs_state);
352 goto out;
353 case HCI_IBS_TX_AWAKE: /* TX_IDLE, go to SLEEP */
354 if (send_hci_ibs_cmd(HCI_IBS_SLEEP_IND, hu) < 0) {
355 BT_ERR("cannot send SLEEP to device");
356 goto out;
357 }
358 ibs->tx_ibs_state = HCI_IBS_TX_ASLEEP;
359 ibs->ibs_sent_slps++; /* debug */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360 break;
361 }
362
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530363 queue_work(ibs->workqueue, &ibs->ws_tx_vote_off);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365out:
366 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
367}
368
369static void hci_ibs_wake_retrans_timeout(unsigned long arg)
370{
371 struct hci_uart *hu = (struct hci_uart *) arg;
372 struct ibs_struct *ibs = hu->priv;
373 unsigned long flags;
374 unsigned long retransmit = 0;
375
376 BT_DBG("hu %p wake retransmit timeout in %lu state",
377 hu, ibs->tx_ibs_state);
378
379 spin_lock_irqsave_nested(&ibs->hci_ibs_lock,
380 flags, SINGLE_DEPTH_NESTING);
381
382 switch (ibs->tx_ibs_state) {
383 default:
384 case HCI_IBS_TX_ASLEEP:
385 case HCI_IBS_TX_AWAKE:
386 BT_ERR("spurrious timeout tx state %ld", ibs->tx_ibs_state);
387 goto out;
388 case HCI_IBS_TX_WAKING: /* No WAKE_ACK, retransmit WAKE */
389 retransmit = 1;
390 if (send_hci_ibs_cmd(HCI_IBS_WAKE_IND, hu) < 0) {
391 BT_ERR("cannot acknowledge device wake up");
392 goto out;
393 }
394 ibs->ibs_sent_wakes++; /* debug */
395 mod_timer(&ibs->wake_retrans_timer, jiffies + wake_retrans);
396 break;
397 }
398out:
399 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
400 if (retransmit)
401 hci_uart_tx_wakeup(hu);
402}
403
404/* Initialize protocol */
405static int ibs_open(struct hci_uart *hu)
406{
407 struct ibs_struct *ibs;
408
409 BT_DBG("hu %p", hu);
410
411 ibs = kzalloc(sizeof(*ibs), GFP_ATOMIC);
412 if (!ibs)
413 return -ENOMEM;
414
415 skb_queue_head_init(&ibs->txq);
416 skb_queue_head_init(&ibs->tx_wait_q);
417 spin_lock_init(&ibs->hci_ibs_lock);
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530418 ibs->workqueue = create_singlethread_workqueue("ibs_wq");
419 if (!ibs->workqueue) {
420 BT_ERR("IBS Workqueue not initialized properly");
421 kfree(ibs);
422 return -ENOMEM;
423 }
424
425 INIT_WORK(&ibs->ws_awake_rx, ibs_wq_awake_rx);
426 INIT_WORK(&ibs->ws_awake_device, ibs_wq_awake_device);
427 INIT_WORK(&ibs->ws_rx_vote_off, ibs_wq_serial_rx_clock_vote_off);
428 INIT_WORK(&ibs->ws_tx_vote_off, ibs_wq_serial_tx_clock_vote_off);
429
430 ibs->ibs_hu = (void *)hu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431
432 /* Assume we start with both sides asleep -- extra wakes OK */
433 ibs->tx_ibs_state = HCI_IBS_TX_ASLEEP;
434 ibs->rx_ibs_state = HCI_IBS_RX_ASLEEP;
435 /* clocks actually on, but we start votes off */
436 ibs->tx_vote = 0;
437 ibs->rx_vote = 0;
438
439 /* debug */
440 ibs->ibs_sent_wacks = 0;
441 ibs->ibs_sent_slps = 0;
442 ibs->ibs_sent_wakes = 0;
443 ibs->ibs_recv_wacks = 0;
444 ibs->ibs_recv_slps = 0;
445 ibs->ibs_recv_wakes = 0;
446 ibs->vote_last_jif = jiffies;
447 ibs->vote_on_ticks = 0;
448 ibs->vote_off_ticks = 0;
449 ibs->votes_on = 0;
450 ibs->votes_off = 0;
451 ibs->tx_votes_on = 0;
452 ibs->tx_votes_off = 0;
453 ibs->rx_votes_on = 0;
454 ibs->rx_votes_off = 0;
455
456 hu->priv = ibs;
457
458 init_timer(&ibs->wake_retrans_timer);
459 ibs->wake_retrans_timer.function = hci_ibs_wake_retrans_timeout;
460 ibs->wake_retrans_timer.data = (u_long) hu;
461
462 init_timer(&ibs->tx_idle_timer);
463 ibs->tx_idle_timer.function = hci_ibs_tx_idle_timeout;
464 ibs->tx_idle_timer.data = (u_long) hu;
465
466 BT_INFO("HCI_IBS open, tx_idle_delay=%lu, wake_retrans=%lu",
467 tx_idle_delay, wake_retrans);
468
469 return 0;
470}
471
472void ibs_log_local_stats(struct ibs_struct *ibs)
473{
474 BT_INFO("HCI_IBS stats: tx_idle_delay=%lu, wake_retrans=%lu",
475 tx_idle_delay, wake_retrans);
476
477 BT_INFO("HCI_IBS stats: tx_ibs_state=%lu, rx_ibs_state=%lu",
478 ibs->tx_ibs_state, ibs->rx_ibs_state);
479 BT_INFO("HCI_IBS stats: sent: sleep=%lu, wake=%lu, wake_ack=%lu",
480 ibs->ibs_sent_slps, ibs->ibs_sent_wakes, ibs->ibs_sent_wacks);
481 BT_INFO("HCI_IBS stats: recv: sleep=%lu, wake=%lu, wake_ack=%lu",
482 ibs->ibs_recv_slps, ibs->ibs_recv_wakes, ibs->ibs_recv_wacks);
483
484 BT_INFO("HCI_IBS stats: queues: txq=%s, txwaitq=%s",
485 skb_queue_empty(&(ibs->txq)) ? "empty" : "full",
486 skb_queue_empty(&(ibs->tx_wait_q)) ? "empty" : "full");
487
488 BT_INFO("HCI_IBS stats: vote state: tx=%lu, rx=%lu",
489 ibs->tx_vote, ibs->rx_vote);
490 BT_INFO("HCI_IBS stats: tx votes cast: on=%lu, off=%lu",
491 ibs->tx_votes_on, ibs->tx_votes_off);
492 BT_INFO("HCI_IBS stats: rx votes cast: on=%lu, off=%lu",
493 ibs->rx_votes_on, ibs->rx_votes_off);
494 BT_INFO("HCI_IBS stats: msm_clock votes cast: on=%lu, off=%lu",
495 ibs->votes_on, ibs->votes_off);
496 BT_INFO("HCI_IBS stats: vote ticks: on=%lu, off=%lu",
497 ibs->vote_on_ticks, ibs->vote_off_ticks);
498}
499
500/* Flush protocol data */
501static int ibs_flush(struct hci_uart *hu)
502{
503 struct ibs_struct *ibs = hu->priv;
504
505 BT_DBG("hu %p", hu);
506
507 skb_queue_purge(&ibs->tx_wait_q);
508 skb_queue_purge(&ibs->txq);
509
510 return 0;
511}
512
513/* Close protocol */
514static int ibs_close(struct hci_uart *hu)
515{
516 struct ibs_struct *ibs = hu->priv;
517
518 BT_DBG("hu %p", hu);
519
520 ibs_msm_serial_clock_vote(HCI_IBS_VOTE_STATS_UPDATE, hu);
521 ibs_log_local_stats(ibs);
522
523 skb_queue_purge(&ibs->tx_wait_q);
524 skb_queue_purge(&ibs->txq);
525 del_timer(&ibs->tx_idle_timer);
526 del_timer(&ibs->wake_retrans_timer);
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530527 destroy_workqueue(ibs->workqueue);
528 ibs->ibs_hu = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
530 kfree_skb(ibs->rx_skb);
531
532 hu->priv = NULL;
533
534 kfree(ibs);
535
536 return 0;
537}
538
539/*
540 * Called upon a wake-up-indication from the device
541 */
542static void ibs_device_want_to_wakeup(struct hci_uart *hu)
543{
544 unsigned long flags;
545 struct ibs_struct *ibs = hu->priv;
546
547 BT_DBG("hu %p", hu);
548
549 /* lock hci_ibs state */
550 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
551
552 /* debug */
553 ibs->ibs_recv_wakes++;
554
555 switch (ibs->rx_ibs_state) {
556 case HCI_IBS_RX_ASLEEP:
557 /* Make sure clock is on - we may have turned clock off since
558 * receiving the wake up indicator
559 */
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530560 /* awake rx clock */
561 queue_work(ibs->workqueue, &ibs->ws_awake_rx);
562 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
563 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 case HCI_IBS_RX_AWAKE:
565 /* Always acknowledge device wake up,
566 * sending IBS message doesn't count as TX ON.
567 */
568 if (send_hci_ibs_cmd(HCI_IBS_WAKE_ACK, hu) < 0) {
569 BT_ERR("cannot acknowledge device wake up");
570 goto out;
571 }
572 ibs->ibs_sent_wacks++; /* debug */
573 break;
574 default:
575 /* any other state is illegal */
576 BT_ERR("received HCI_IBS_WAKE_IND in rx state %ld",
577 ibs->rx_ibs_state);
578 break;
579 }
580
581out:
582 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
583
584 /* actually send the packets */
585 hci_uart_tx_wakeup(hu);
586}
587
588/*
589 * Called upon a sleep-indication from the device
590 */
591static void ibs_device_want_to_sleep(struct hci_uart *hu)
592{
593 unsigned long flags;
594 struct ibs_struct *ibs = hu->priv;
595
596 BT_DBG("hu %p", hu);
597
598 /* lock hci_ibs state */
599 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
600
601 /* debug */
602 ibs->ibs_recv_slps++;
603
604 switch (ibs->rx_ibs_state) {
605 case HCI_IBS_RX_AWAKE:
606 /* update state */
607 ibs->rx_ibs_state = HCI_IBS_RX_ASLEEP;
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530608 /* vote off rx clock under workqueue */
609 queue_work(ibs->workqueue, &ibs->ws_rx_vote_off);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 break;
611 case HCI_IBS_RX_ASLEEP:
612 /* deliberate fall-through */
613 default:
614 /* any other state is illegal */
615 BT_ERR("received HCI_IBS_SLEEP_IND in rx state %ld",
616 ibs->rx_ibs_state);
617 break;
618 }
619
620 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
621}
622
623/*
624 * Called upon wake-up-acknowledgement from the device
625 */
626static void ibs_device_woke_up(struct hci_uart *hu)
627{
628 unsigned long flags;
629 struct ibs_struct *ibs = hu->priv;
630 struct sk_buff *skb = NULL;
631
632 BT_DBG("hu %p", hu);
633
634 /* lock hci_ibs state */
635 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
636
637 /* debug */
638 ibs->ibs_recv_wacks++;
639
640 switch (ibs->tx_ibs_state) {
641 case HCI_IBS_TX_ASLEEP:
642 /* This could be spurrious rx wake on the BT chip.
643 * Send it another SLEEP othwise it will stay awake. */
644 default:
645 BT_ERR("received HCI_IBS_WAKE_ACK in tx state %ld",
646 ibs->tx_ibs_state);
647 break;
648 case HCI_IBS_TX_AWAKE:
649 /* expect one if we send 2 WAKEs */
650 BT_DBG("received HCI_IBS_WAKE_ACK in tx state %ld",
651 ibs->tx_ibs_state);
652 break;
653 case HCI_IBS_TX_WAKING:
654 /* send pending packets */
655 while ((skb = skb_dequeue(&ibs->tx_wait_q)))
656 skb_queue_tail(&ibs->txq, skb);
657 /* switch timers and change state to HCI_IBS_TX_AWAKE */
658 del_timer(&ibs->wake_retrans_timer);
659 mod_timer(&ibs->tx_idle_timer, jiffies + tx_idle_delay);
660 ibs->tx_ibs_state = HCI_IBS_TX_AWAKE;
661 }
662
663 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
664
665 /* actually send the packets */
666 hci_uart_tx_wakeup(hu);
667}
668
669/* Enqueue frame for transmittion (padding, crc, etc) */
670/* may be called from two simultaneous tasklets */
671static int ibs_enqueue(struct hci_uart *hu, struct sk_buff *skb)
672{
673 unsigned long flags = 0;
674 struct ibs_struct *ibs = hu->priv;
675
676 BT_DBG("hu %p skb %p", hu, skb);
677
678 /* Prepend skb with frame type */
679 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
680
681 /* lock hci_ibs state */
682 spin_lock_irqsave(&ibs->hci_ibs_lock, flags);
683
684 /* act according to current state */
685 switch (ibs->tx_ibs_state) {
686 case HCI_IBS_TX_AWAKE:
687 BT_DBG("device awake, sending normally");
688 skb_queue_tail(&ibs->txq, skb);
689 mod_timer(&ibs->tx_idle_timer, jiffies + tx_idle_delay);
690 break;
691
692 case HCI_IBS_TX_ASLEEP:
693 BT_DBG("device asleep, waking up and queueing packet");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694 /* save packet for later */
695 skb_queue_tail(&ibs->tx_wait_q, skb);
Ram Mohan Korukonda1aa44762012-07-24 18:50:56 +0530696
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697 ibs->tx_ibs_state = HCI_IBS_TX_WAKING;
Ram Mohan Korukondad0063b62012-08-19 13:44:57 +0530698 /* schedule a work queue to wake up device */
699 queue_work(ibs->workqueue, &ibs->ws_awake_device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 break;
701
702 case HCI_IBS_TX_WAKING:
703 BT_DBG("device waking up, queueing packet");
704 /* transient state; just keep packet for later */
705 skb_queue_tail(&ibs->tx_wait_q, skb);
706 break;
707
708 default:
709 BT_ERR("illegal tx state: %ld (losing packet)",
710 ibs->tx_ibs_state);
711 kfree_skb(skb);
712 break;
713 }
714
715 spin_unlock_irqrestore(&ibs->hci_ibs_lock, flags);
716
717 return 0;
718}
719
720static inline int ibs_check_data_len(struct ibs_struct *ibs, int len)
721{
722 register int room = skb_tailroom(ibs->rx_skb);
723
724 BT_DBG("len %d room %d", len, room);
725
726 if (!len) {
727 hci_recv_frame(ibs->rx_skb);
728 } else if (len > room) {
729 BT_ERR("Data length is too large");
730 kfree_skb(ibs->rx_skb);
731 } else {
732 ibs->rx_state = HCI_IBS_W4_DATA;
733 ibs->rx_count = len;
734 return len;
735 }
736
737 ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
738 ibs->rx_skb = NULL;
739 ibs->rx_count = 0;
740
741 return 0;
742}
743
744/* Recv data */
745static int ibs_recv(struct hci_uart *hu, void *data, int count)
746{
747 struct ibs_struct *ibs = hu->priv;
748 register char *ptr;
749 struct hci_event_hdr *eh;
750 struct hci_acl_hdr *ah;
751 struct hci_sco_hdr *sh;
752 register int len, type, dlen;
753
754 BT_DBG("hu %p count %d rx_state %ld rx_count %ld",
755 hu, count, ibs->rx_state, ibs->rx_count);
756
757 ptr = data;
758 while (count) {
759 if (ibs->rx_count) {
760 len = min_t(unsigned int, ibs->rx_count, count);
761 memcpy(skb_put(ibs->rx_skb, len), ptr, len);
762 ibs->rx_count -= len; count -= len; ptr += len;
763
764 if (ibs->rx_count)
765 continue;
766
767 switch (ibs->rx_state) {
768 case HCI_IBS_W4_DATA:
769 BT_DBG("Complete data");
770 hci_recv_frame(ibs->rx_skb);
771
772 ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
773 ibs->rx_skb = NULL;
774 continue;
775
776 case HCI_IBS_W4_EVENT_HDR:
777 eh = (struct hci_event_hdr *) ibs->rx_skb->data;
778
779 BT_DBG("Event header: evt 0x%2.2x plen %d",
780 eh->evt, eh->plen);
781
782 ibs_check_data_len(ibs, eh->plen);
783 continue;
784
785 case HCI_IBS_W4_ACL_HDR:
786 ah = (struct hci_acl_hdr *) ibs->rx_skb->data;
787 dlen = __le16_to_cpu(ah->dlen);
788
789 BT_DBG("ACL header: dlen %d", dlen);
790
791 ibs_check_data_len(ibs, dlen);
792 continue;
793
794 case HCI_IBS_W4_SCO_HDR:
795 sh = (struct hci_sco_hdr *) ibs->rx_skb->data;
796
797 BT_DBG("SCO header: dlen %d", sh->dlen);
798
799 ibs_check_data_len(ibs, sh->dlen);
800 continue;
801 }
802 }
803
804 /* HCI_IBS_W4_PACKET_TYPE */
805 switch (*ptr) {
806 case HCI_EVENT_PKT:
807 BT_DBG("Event packet");
808 ibs->rx_state = HCI_IBS_W4_EVENT_HDR;
809 ibs->rx_count = HCI_EVENT_HDR_SIZE;
810 type = HCI_EVENT_PKT;
811 break;
812
813 case HCI_ACLDATA_PKT:
814 BT_DBG("ACL packet");
815 ibs->rx_state = HCI_IBS_W4_ACL_HDR;
816 ibs->rx_count = HCI_ACL_HDR_SIZE;
817 type = HCI_ACLDATA_PKT;
818 break;
819
820 case HCI_SCODATA_PKT:
821 BT_DBG("SCO packet");
822 ibs->rx_state = HCI_IBS_W4_SCO_HDR;
823 ibs->rx_count = HCI_SCO_HDR_SIZE;
824 type = HCI_SCODATA_PKT;
825 break;
826
827 /* HCI_IBS signals */
828 case HCI_IBS_SLEEP_IND:
829 BT_DBG("HCI_IBS_SLEEP_IND packet");
830 ibs_device_want_to_sleep(hu);
831 ptr++; count--;
832 continue;
833
834 case HCI_IBS_WAKE_IND:
835 BT_DBG("HCI_IBS_WAKE_IND packet");
836 ibs_device_want_to_wakeup(hu);
837 ptr++; count--;
838 continue;
839
840 case HCI_IBS_WAKE_ACK:
841 BT_DBG("HCI_IBS_WAKE_ACK packet");
842 ibs_device_woke_up(hu);
843 ptr++; count--;
844 continue;
845
846 default:
847 BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
848 hu->hdev->stat.err_rx++;
849 ptr++; count--;
850 continue;
851 };
852
853 ptr++; count--;
854
855 /* Allocate packet */
856 ibs->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
857 if (!ibs->rx_skb) {
858 BT_ERR("Can't allocate mem for new packet");
859 ibs->rx_state = HCI_IBS_W4_PACKET_TYPE;
860 ibs->rx_count = 0;
861 return 0;
862 }
863
864 ibs->rx_skb->dev = (void *) hu->hdev;
865 bt_cb(ibs->rx_skb)->pkt_type = type;
866 }
867
868 return count;
869}
870
871static struct sk_buff *ibs_dequeue(struct hci_uart *hu)
872{
873 struct ibs_struct *ibs = hu->priv;
874 return skb_dequeue(&ibs->txq);
875}
876
877static struct hci_uart_proto ibs_p = {
878 .id = HCI_UART_IBS,
879 .open = ibs_open,
880 .close = ibs_close,
881 .recv = ibs_recv,
882 .enqueue = ibs_enqueue,
883 .dequeue = ibs_dequeue,
884 .flush = ibs_flush,
885};
886
887int ibs_init(void)
888{
889 int err = hci_uart_register_proto(&ibs_p);
890
891 if (!err)
892 BT_INFO("HCI_IBS protocol initialized");
893 else
894 BT_ERR("HCI_IBS protocol registration failed");
895
896 return err;
897}
898
899int ibs_deinit(void)
900{
901 return hci_uart_unregister_proto(&ibs_p);
902}
903
904module_param(wake_retrans, ulong, 0644);
905MODULE_PARM_DESC(wake_retrans, "Delay (1/HZ) to retransmit WAKE_IND");
906
907module_param(tx_idle_delay, ulong, 0644);
908MODULE_PARM_DESC(tx_idle_delay, "Delay (1/HZ) since last tx for SLEEP_IND");