blob: f34f4f3f96374854c03d2c160df2e713efd76c25 [file] [log] [blame]
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Jeff Hugoae3a85e2011-12-02 17:10:18 -070028#include <linux/wakelock.h>
Eric Holmberg878923a2012-01-10 14:28:19 -070029#include <linux/kfifo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include <mach/sps.h>
32#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060033#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060034#include <mach/subsystem_notif.h>
Jeff Hugo75913c82011-12-05 15:59:01 -070035#include <mach/socinfo.h>
Jeff Hugo4838f412012-01-20 11:19:37 -070036#include <mach/subsystem_restart.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define BAM_CH_LOCAL_OPEN 0x1
39#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060040#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
42#define BAM_MUX_HDR_MAGIC_NO 0x33fc
43
Eric Holmberg006057d2012-01-11 10:10:42 -070044#define BAM_MUX_HDR_CMD_DATA 0
45#define BAM_MUX_HDR_CMD_OPEN 1
46#define BAM_MUX_HDR_CMD_CLOSE 2
47#define BAM_MUX_HDR_CMD_STATUS 3 /* unused */
48#define BAM_MUX_HDR_CMD_OPEN_NO_A2_PC 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
Jeff Hugo949080a2011-08-30 11:58:56 -060050#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
51#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
52#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070054#define LOW_WATERMARK 2
55#define HIGH_WATERMARK 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056
57static int msm_bam_dmux_debug_enable;
58module_param_named(debug_enable, msm_bam_dmux_debug_enable,
59 int, S_IRUGO | S_IWUSR | S_IWGRP);
60
61#if defined(DEBUG)
62static uint32_t bam_dmux_read_cnt;
63static uint32_t bam_dmux_write_cnt;
64static uint32_t bam_dmux_write_cpy_cnt;
65static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070066static uint32_t bam_dmux_tx_sps_failure_cnt;
Eric Holmberg6074aba2012-01-18 17:59:44 -070067static uint32_t bam_dmux_tx_stall_cnt;
Eric Holmberg1f1255d2012-02-22 13:37:21 -070068static atomic_t bam_dmux_ack_out_cnt = ATOMIC_INIT(0);
69static atomic_t bam_dmux_ack_in_cnt = ATOMIC_INIT(0);
70static atomic_t bam_dmux_a2_pwr_cntl_in_cnt = ATOMIC_INIT(0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071
72#define DBG(x...) do { \
73 if (msm_bam_dmux_debug_enable) \
74 pr_debug(x); \
75 } while (0)
76
77#define DBG_INC_READ_CNT(x) do { \
78 bam_dmux_read_cnt += (x); \
79 if (msm_bam_dmux_debug_enable) \
80 pr_debug("%s: total read bytes %u\n", \
81 __func__, bam_dmux_read_cnt); \
82 } while (0)
83
84#define DBG_INC_WRITE_CNT(x) do { \
85 bam_dmux_write_cnt += (x); \
86 if (msm_bam_dmux_debug_enable) \
87 pr_debug("%s: total written bytes %u\n", \
88 __func__, bam_dmux_write_cnt); \
89 } while (0)
90
91#define DBG_INC_WRITE_CPY(x) do { \
92 bam_dmux_write_cpy_bytes += (x); \
93 bam_dmux_write_cpy_cnt++; \
94 if (msm_bam_dmux_debug_enable) \
95 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
96 __func__, bam_dmux_write_cpy_cnt, \
97 bam_dmux_write_cpy_bytes); \
98 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070099
100#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
101 bam_dmux_tx_sps_failure_cnt++; \
102} while (0)
103
Eric Holmberg6074aba2012-01-18 17:59:44 -0700104#define DBG_INC_TX_STALL_CNT() do { \
105 bam_dmux_tx_stall_cnt++; \
106} while (0)
107
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700108#define DBG_INC_ACK_OUT_CNT() \
109 atomic_inc(&bam_dmux_ack_out_cnt)
110
111#define DBG_INC_A2_POWER_CONTROL_IN_CNT() \
112 atomic_inc(&bam_dmux_a2_pwr_cntl_in_cnt)
113
114#define DBG_INC_ACK_IN_CNT() \
115 atomic_inc(&bam_dmux_ack_in_cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116#else
117#define DBG(x...) do { } while (0)
118#define DBG_INC_READ_CNT(x...) do { } while (0)
119#define DBG_INC_WRITE_CNT(x...) do { } while (0)
120#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700121#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Eric Holmberg6074aba2012-01-18 17:59:44 -0700122#define DBG_INC_TX_STALL_CNT() do { } while (0)
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700123#define DBG_INC_ACK_OUT_CNT() do { } while (0)
124#define DBG_INC_A2_POWER_CONTROL_IN_CNT() \
125 do { } while (0)
126#define DBG_INC_ACK_IN_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127#endif
128
129struct bam_ch_info {
130 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600131 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 void *priv;
133 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600134 struct platform_device *pdev;
135 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700136 int num_tx_pkts;
137 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138};
139
140struct tx_pkt_info {
141 struct sk_buff *skb;
142 dma_addr_t dma_address;
143 char is_cmd;
144 uint32_t len;
145 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600146 struct list_head list_node;
Eric Holmberg878923a2012-01-10 14:28:19 -0700147 unsigned ts_sec;
148 unsigned long ts_nsec;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149};
150
151struct rx_pkt_info {
152 struct sk_buff *skb;
153 dma_addr_t dma_address;
154 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600155 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156};
157
158#define A2_NUM_PIPES 6
159#define A2_SUMMING_THRESHOLD 4096
160#define A2_DEFAULT_DESCRIPTORS 32
161#define A2_PHYS_BASE 0x124C2000
162#define A2_PHYS_SIZE 0x2000
163#define BUFFER_SIZE 2048
164#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600166static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167static struct sps_pipe *bam_tx_pipe;
168static struct sps_pipe *bam_rx_pipe;
169static struct sps_connect tx_connection;
170static struct sps_connect rx_connection;
171static struct sps_mem_buffer tx_desc_mem_buf;
172static struct sps_mem_buffer rx_desc_mem_buf;
173static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600174static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175
176static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
177static int bam_mux_initialized;
178
Jeff Hugo949080a2011-08-30 11:58:56 -0600179static int polling_mode;
180
181static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600182static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700183static int bam_rx_pool_len;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600184static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600185static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187struct bam_mux_hdr {
188 uint16_t magic_num;
189 uint8_t reserved;
190 uint8_t cmd;
191 uint8_t pad_len;
192 uint8_t ch_id;
193 uint16_t pkt_len;
194};
195
Jeff Hugod98b1082011-10-24 10:30:23 -0600196static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197static void bam_mux_write_done(struct work_struct *work);
198static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600199static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200
Jeff Hugo949080a2011-08-30 11:58:56 -0600201static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202
203static struct workqueue_struct *bam_mux_rx_workqueue;
204static struct workqueue_struct *bam_mux_tx_workqueue;
205
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600206/* A2 power collaspe */
207#define UL_TIMEOUT_DELAY 1000 /* in ms */
208static void toggle_apps_ack(void);
209static void reconnect_to_bam(void);
210static void disconnect_to_bam(void);
211static void ul_wakeup(void);
212static void ul_timeout(struct work_struct *work);
213static void vote_dfab(void);
214static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600215static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700216static void grab_wakelock(void);
217static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600218
219static int bam_is_connected;
220static DEFINE_MUTEX(wakeup_lock);
221static struct completion ul_wakeup_ack_completion;
222static struct completion bam_connection_completion;
223static struct delayed_work ul_timeout_work;
224static int ul_packet_written;
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700225static atomic_t ul_ondemand_vote = ATOMIC_INIT(0);
Stephen Boyd69d35e32012-02-14 15:33:30 -0800226static struct clk *dfab_clk, *xo_clk;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600227static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600228static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600229static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700230static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700231static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700232static int a2_pc_disabled;
233static DEFINE_MUTEX(dfab_status_lock);
234static int dfab_is_on;
235static int wait_for_dfab;
236static struct completion dfab_unvote_completion;
237static DEFINE_SPINLOCK(wakelock_reference_lock);
238static int wakelock_reference_count;
Eric Holmberg604ab252012-01-15 00:01:18 -0700239static struct delayed_work msm9615_bam_init_work;
Jeff Hugo583a6da2012-02-03 11:37:30 -0700240static int a2_pc_disabled_wakelock_skipped;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600241/* End A2 power collaspe */
242
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600243/* subsystem restart */
244static int restart_notifier_cb(struct notifier_block *this,
245 unsigned long code,
246 void *data);
247
248static struct notifier_block restart_notifier = {
249 .notifier_call = restart_notifier_cb,
250};
251static int in_global_reset;
252/* end subsystem restart */
253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254#define bam_ch_is_open(x) \
255 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
256
257#define bam_ch_is_local_open(x) \
258 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
259
260#define bam_ch_is_remote_open(x) \
261 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
262
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600263#define bam_ch_is_in_reset(x) \
264 (bam_ch[(x)].status & BAM_CH_IN_RESET)
265
Eric Holmberg878923a2012-01-10 14:28:19 -0700266#define LOG_MESSAGE_MAX_SIZE 80
267struct kfifo bam_dmux_state_log;
268static uint32_t bam_dmux_state_logging_disabled;
269static DEFINE_SPINLOCK(bam_dmux_logging_spinlock);
270static int bam_dmux_uplink_vote;
271static int bam_dmux_power_state;
272
273
274#define DMUX_LOG_KERR(fmt...) \
275do { \
276 bam_dmux_log(fmt); \
277 pr_err(fmt); \
278} while (0)
279
280/**
281 * Log a state change along with a small message.
282 *
283 * Complete size of messsage is limited to @todo.
284 */
285static void bam_dmux_log(const char *fmt, ...)
286{
287 char buff[LOG_MESSAGE_MAX_SIZE];
288 unsigned long flags;
289 va_list arg_list;
290 unsigned long long t_now;
291 unsigned long nanosec_rem;
292 int len = 0;
293
294 if (bam_dmux_state_logging_disabled)
295 return;
296
297 t_now = sched_clock();
298 nanosec_rem = do_div(t_now, 1000000000U);
299
300 /*
301 * States
Eric Holmberg006057d2012-01-11 10:10:42 -0700302 * D: 1 = Power collapse disabled
Eric Holmberg878923a2012-01-10 14:28:19 -0700303 * R: 1 = in global reset
304 * P: 1 = BAM is powered up
305 * A: 1 = BAM initialized and ready for data
306 *
307 * V: 1 = Uplink vote for power
308 * U: 1 = Uplink active
309 * W: 1 = Uplink Wait-for-ack
310 * A: 1 = Uplink ACK received
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700311 * #: >=1 On-demand uplink vote
Eric Holmberg878923a2012-01-10 14:28:19 -0700312 */
313 len += scnprintf(buff, sizeof(buff),
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700314 "<DMUX> %u.%09lu %c%c%c%c %c%c%c%c%d ",
Eric Holmberg878923a2012-01-10 14:28:19 -0700315 (unsigned)t_now, nanosec_rem,
Eric Holmberg006057d2012-01-11 10:10:42 -0700316 a2_pc_disabled ? 'D' : 'd',
Eric Holmberg878923a2012-01-10 14:28:19 -0700317 in_global_reset ? 'R' : 'r',
318 bam_dmux_power_state ? 'P' : 'p',
319 bam_connection_is_active ? 'A' : 'a',
320 bam_dmux_uplink_vote ? 'V' : 'v',
321 bam_is_connected ? 'U' : 'u',
322 wait_for_ack ? 'W' : 'w',
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700323 ul_wakeup_ack_completion.done ? 'A' : 'a',
324 atomic_read(&ul_ondemand_vote)
Eric Holmberg878923a2012-01-10 14:28:19 -0700325 );
326
327 va_start(arg_list, fmt);
328 len += vscnprintf(buff + len, sizeof(buff) - len, fmt, arg_list);
329 va_end(arg_list);
330 memset(buff + len, 0x0, sizeof(buff) - len);
331
332 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
333 if (kfifo_avail(&bam_dmux_state_log) < LOG_MESSAGE_MAX_SIZE) {
334 char junk[LOG_MESSAGE_MAX_SIZE];
335 int ret;
336
337 ret = kfifo_out(&bam_dmux_state_log, junk, sizeof(junk));
338 if (ret != LOG_MESSAGE_MAX_SIZE) {
339 pr_err("%s: unable to empty log %d\n", __func__, ret);
340 spin_unlock_irqrestore(&bam_dmux_logging_spinlock,
341 flags);
342 return;
343 }
344 }
345 kfifo_in(&bam_dmux_state_log, buff, sizeof(buff));
346 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
347}
348
349static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
350{
351 unsigned long long t_now;
352
353 t_now = sched_clock();
354 pkt->ts_nsec = do_div(t_now, 1000000000U);
355 pkt->ts_sec = (unsigned)t_now;
356}
357
358static inline void verify_tx_queue_is_empty(const char *func)
359{
360 unsigned long flags;
361 struct tx_pkt_info *info;
362 int reported = 0;
363
364 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
365 list_for_each_entry(info, &bam_tx_pool, list_node) {
366 if (!reported) {
Eric Holmberg454d9da2012-01-12 09:37:14 -0700367 bam_dmux_log("%s: tx pool not empty\n", func);
368 if (!in_global_reset)
369 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700370 reported = 1;
371 }
Eric Holmberg454d9da2012-01-12 09:37:14 -0700372 bam_dmux_log("%s: node=%p ts=%u.%09lu\n", __func__,
373 &info->list_node, info->ts_sec, info->ts_nsec);
374 if (!in_global_reset)
375 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
376 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700377 }
378 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
379}
380
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381static void queue_rx(void)
382{
383 void *ptr;
384 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700385 int ret;
386 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600387
Jeff Hugoc9749932011-11-02 17:50:40 -0600388 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700389 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600390 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600391
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700392 while (rx_len_cached < NUM_BUFFERS) {
393 if (in_global_reset)
394 goto fail;
395
396 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
397 if (!info) {
398 pr_err("%s: unable to alloc rx_pkt_info\n", __func__);
399 goto fail;
400 }
401
402 INIT_WORK(&info->work, handle_bam_mux_cmd);
403
404 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
405 if (info->skb == NULL) {
406 DMUX_LOG_KERR("%s: unable to alloc skb\n", __func__);
407 goto fail_info;
408 }
409 ptr = skb_put(info->skb, BUFFER_SIZE);
410
411 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
412 DMA_FROM_DEVICE);
413 if (info->dma_address == 0 || info->dma_address == ~0) {
414 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
415 __func__, (void *)info->dma_address, ptr);
416 goto fail_skb;
417 }
418
419 mutex_lock(&bam_rx_pool_mutexlock);
420 list_add_tail(&info->list_node, &bam_rx_pool);
421 rx_len_cached = ++bam_rx_pool_len;
422 mutex_unlock(&bam_rx_pool_mutexlock);
423
424 ret = sps_transfer_one(bam_rx_pipe, info->dma_address,
425 BUFFER_SIZE, info,
426 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
427
428 if (ret) {
429 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
430 __func__, ret);
431 goto fail_transfer;
432 }
433 }
434 return;
435
436fail_transfer:
437 mutex_lock(&bam_rx_pool_mutexlock);
438 list_del(&info->list_node);
439 --bam_rx_pool_len;
440 rx_len_cached = bam_rx_pool_len;
441 mutex_unlock(&bam_rx_pool_mutexlock);
442
443 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
444 DMA_FROM_DEVICE);
445
446fail_skb:
447 dev_kfree_skb_any(info->skb);
448
449fail_info:
450 kfree(info);
451
452fail:
453 if (rx_len_cached == 0) {
454 DMUX_LOG_KERR("%s: RX queue failure\n", __func__);
455 in_global_reset = 1;
456 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457}
458
459static void bam_mux_process_data(struct sk_buff *rx_skb)
460{
461 unsigned long flags;
462 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600463 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464
465 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
466
467 rx_skb->data = (unsigned char *)(rx_hdr + 1);
468 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
469 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600470 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600472 event_data = (unsigned long)(rx_skb);
473
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600475 if (bam_ch[rx_hdr->ch_id].notify)
476 bam_ch[rx_hdr->ch_id].notify(
477 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
478 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 else
480 dev_kfree_skb_any(rx_skb);
481 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
482
483 queue_rx();
484}
485
Eric Holmberg006057d2012-01-11 10:10:42 -0700486static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
487{
488 unsigned long flags;
489 int ret;
490
491 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
492 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
493 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
494 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
495 queue_rx();
496 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
497 if (ret)
498 pr_err("%s: platform_device_add() error: %d\n",
499 __func__, ret);
500}
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502static void handle_bam_mux_cmd(struct work_struct *work)
503{
504 unsigned long flags;
505 struct bam_mux_hdr *rx_hdr;
506 struct rx_pkt_info *info;
507 struct sk_buff *rx_skb;
508
509 info = container_of(work, struct rx_pkt_info, work);
510 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600511 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512 kfree(info);
513
514 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
515
516 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
517 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
518 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
519 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
520 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700521 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
522 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 " pad %d ch %d len %d\n", __func__,
524 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
525 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
526 dev_kfree_skb_any(rx_skb);
527 queue_rx();
528 return;
529 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700530
531 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700532 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
533 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700534 " pad %d ch %d len %d\n", __func__,
535 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
536 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
537 dev_kfree_skb_any(rx_skb);
538 queue_rx();
539 return;
540 }
541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 switch (rx_hdr->cmd) {
543 case BAM_MUX_HDR_CMD_DATA:
544 DBG_INC_READ_CNT(rx_hdr->pkt_len);
545 bam_mux_process_data(rx_skb);
546 break;
547 case BAM_MUX_HDR_CMD_OPEN:
Eric Holmberg006057d2012-01-11 10:10:42 -0700548 bam_dmux_log("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700549 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700550 handle_bam_mux_cmd_open(rx_hdr);
551 dev_kfree_skb_any(rx_skb);
552 break;
553 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
554 bam_dmux_log("%s: opening cid %d PC disabled\n", __func__,
555 rx_hdr->ch_id);
556
557 if (!a2_pc_disabled) {
558 a2_pc_disabled = 1;
559 schedule_delayed_work(&ul_timeout_work,
560 msecs_to_jiffies(UL_TIMEOUT_DELAY));
561 }
562
563 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600564 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 break;
566 case BAM_MUX_HDR_CMD_CLOSE:
567 /* probably should drop pending write */
Eric Holmberg878923a2012-01-10 14:28:19 -0700568 bam_dmux_log("%s: closing cid %d\n", __func__,
569 rx_hdr->ch_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
571 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
572 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600574 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
575 bam_ch[rx_hdr->ch_id].pdev =
576 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
577 if (!bam_ch[rx_hdr->ch_id].pdev)
578 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600579 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 break;
581 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700582 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
583 " reserved %d cmd %d pad %d ch %d len %d\n",
584 __func__, rx_hdr->magic_num, rx_hdr->reserved,
585 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
586 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 dev_kfree_skb_any(rx_skb);
588 queue_rx();
589 return;
590 }
591}
592
593static int bam_mux_write_cmd(void *data, uint32_t len)
594{
595 int rc;
596 struct tx_pkt_info *pkt;
597 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700598 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600600 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 if (pkt == NULL) {
602 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
603 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 return rc;
605 }
606
607 dma_address = dma_map_single(NULL, data, len,
608 DMA_TO_DEVICE);
609 if (!dma_address) {
610 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700611 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700612 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613 return rc;
614 }
615 pkt->skb = (struct sk_buff *)(data);
616 pkt->len = len;
617 pkt->dma_address = dma_address;
618 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700619 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600620 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700621 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600622 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
624 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600625 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700626 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
627 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600628 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700629 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700630 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700631 dma_unmap_single(NULL, pkt->dma_address,
632 pkt->len,
633 DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600634 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700635 } else {
636 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600637 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600639 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 return rc;
641}
642
643static void bam_mux_write_done(struct work_struct *work)
644{
645 struct sk_buff *skb;
646 struct bam_mux_hdr *hdr;
647 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700648 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600649 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700650 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600652 if (in_global_reset)
653 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700654
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700656
657 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
658 info_expected = list_first_entry(&bam_tx_pool,
659 struct tx_pkt_info, list_node);
660 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700661 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700662
Eric Holmberg878923a2012-01-10 14:28:19 -0700663 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
664 " list_node=%p, ts=%u.%09lu\n",
665 __func__, bam_tx_pool.next, &info->list_node,
666 info->ts_sec, info->ts_nsec
667 );
668
669 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
670 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
671 &errant_pkt->list_node, errant_pkt->ts_sec,
672 errant_pkt->ts_nsec);
673
674 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700675 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
676 BUG();
677 }
678 list_del(&info->list_node);
679 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
680
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600681 if (info->is_cmd) {
682 kfree(info->skb);
683 kfree(info);
684 return;
685 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 skb = info->skb;
687 kfree(info);
688 hdr = (struct bam_mux_hdr *)skb->data;
Eric Holmberg9fdef262012-02-14 11:46:05 -0700689 DBG_INC_WRITE_CNT(skb->len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600690 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700691 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
692 bam_ch[hdr->ch_id].num_tx_pkts--;
693 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600694 if (bam_ch[hdr->ch_id].notify)
695 bam_ch[hdr->ch_id].notify(
696 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
697 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 else
699 dev_kfree_skb_any(skb);
700}
701
702int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
703{
704 int rc = 0;
705 struct bam_mux_hdr *hdr;
706 unsigned long flags;
707 struct sk_buff *new_skb = NULL;
708 dma_addr_t dma_address;
709 struct tx_pkt_info *pkt;
710
711 if (id >= BAM_DMUX_NUM_CHANNELS)
712 return -EINVAL;
713 if (!skb)
714 return -EINVAL;
715 if (!bam_mux_initialized)
716 return -ENODEV;
717
718 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
719 spin_lock_irqsave(&bam_ch[id].lock, flags);
720 if (!bam_ch_is_open(id)) {
721 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
722 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
723 return -ENODEV;
724 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700725
726 if (bam_ch[id].use_wm &&
727 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
728 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
729 pr_err("%s: watermark exceeded: %d\n", __func__, id);
730 return -EAGAIN;
731 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
733
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600734 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600735 if (!bam_is_connected) {
736 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600737 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700738 if (unlikely(in_global_reset == 1))
739 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600740 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600741 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600742 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600743
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 /* if skb do not have any tailroom for padding,
745 copy the skb into a new expanded skb */
746 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
747 /* revisit, probably dev_alloc_skb and memcpy is effecient */
748 new_skb = skb_copy_expand(skb, skb_headroom(skb),
749 4 - (skb->len & 0x3), GFP_ATOMIC);
750 if (new_skb == NULL) {
751 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600752 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 }
754 dev_kfree_skb_any(skb);
755 skb = new_skb;
756 DBG_INC_WRITE_CPY(skb->len);
757 }
758
759 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
760
761 /* caller should allocate for hdr and padding
762 hdr is fine, padding is tricky */
763 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
764 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
765 hdr->reserved = 0;
766 hdr->ch_id = id;
767 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
768 if (skb->len & 0x3)
769 skb_put(skb, 4 - (skb->len & 0x3));
770
771 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
772
773 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
774 __func__, skb->data, skb->tail, skb->len,
775 hdr->pkt_len, hdr->pad_len);
776
777 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
778 if (pkt == NULL) {
779 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600780 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 }
782
783 dma_address = dma_map_single(NULL, skb->data, skb->len,
784 DMA_TO_DEVICE);
785 if (!dma_address) {
786 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600787 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 }
789 pkt->skb = skb;
790 pkt->dma_address = dma_address;
791 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700792 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700794 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600795 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
797 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600798 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700799 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
800 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600801 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700802 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700803 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700804 dma_unmap_single(NULL, pkt->dma_address,
805 pkt->skb->len, DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600806 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700807 if (new_skb)
808 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700809 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700810 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700811 spin_lock_irqsave(&bam_ch[id].lock, flags);
812 bam_ch[id].num_tx_pkts++;
813 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600814 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600815 ul_packet_written = 1;
816 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600818
819write_fail3:
820 kfree(pkt);
821write_fail2:
822 if (new_skb)
823 dev_kfree_skb_any(new_skb);
824write_fail:
825 read_unlock(&ul_wakeup_lock);
826 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827}
828
829int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600830 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831{
832 struct bam_mux_hdr *hdr;
833 unsigned long flags;
834 int rc = 0;
835
836 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700837 if (!bam_mux_initialized) {
838 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700840 }
841 if (id >= BAM_DMUX_NUM_CHANNELS) {
842 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700844 }
845 if (notify == NULL) {
846 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600847 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700848 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849
850 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
851 if (hdr == NULL) {
852 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
853 return -ENOMEM;
854 }
855 spin_lock_irqsave(&bam_ch[id].lock, flags);
856 if (bam_ch_is_open(id)) {
857 DBG("%s: Already opened %d\n", __func__, id);
858 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
859 kfree(hdr);
860 goto open_done;
861 }
862 if (!bam_ch_is_remote_open(id)) {
863 DBG("%s: Remote not open; ch: %d\n", __func__, id);
864 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
865 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700866 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 }
868
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600869 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 bam_ch[id].priv = priv;
871 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700872 bam_ch[id].num_tx_pkts = 0;
873 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
875
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600876 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600877 if (!bam_is_connected) {
878 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600879 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700880 if (unlikely(in_global_reset == 1))
881 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600882 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600883 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600884 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600885
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
887 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
888 hdr->reserved = 0;
889 hdr->ch_id = id;
890 hdr->pkt_len = 0;
891 hdr->pad_len = 0;
892
893 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600894 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895
896open_done:
897 DBG("%s: opened ch %d\n", __func__, id);
898 return rc;
899}
900
901int msm_bam_dmux_close(uint32_t id)
902{
903 struct bam_mux_hdr *hdr;
904 unsigned long flags;
905 int rc;
906
907 if (id >= BAM_DMUX_NUM_CHANNELS)
908 return -EINVAL;
909 DBG("%s: closing ch %d\n", __func__, id);
910 if (!bam_mux_initialized)
911 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600913 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600914 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600915 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600916 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700917 if (unlikely(in_global_reset == 1))
918 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600919 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600920 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600921 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600922
Jeff Hugo061ce672011-10-21 17:15:32 -0600923 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600924 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 bam_ch[id].priv = NULL;
926 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
927 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
928
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600929 if (bam_ch_is_in_reset(id)) {
930 read_unlock(&ul_wakeup_lock);
931 bam_ch[id].status &= ~BAM_CH_IN_RESET;
932 return 0;
933 }
934
Jeff Hugobb5802f2011-11-02 17:10:29 -0600935 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 if (hdr == NULL) {
937 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600938 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 return -ENOMEM;
940 }
941 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
942 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
943 hdr->reserved = 0;
944 hdr->ch_id = id;
945 hdr->pkt_len = 0;
946 hdr->pad_len = 0;
947
948 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600949 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950
951 DBG("%s: closed ch %d\n", __func__, id);
952 return rc;
953}
954
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700955int msm_bam_dmux_is_ch_full(uint32_t id)
956{
957 unsigned long flags;
958 int ret;
959
960 if (id >= BAM_DMUX_NUM_CHANNELS)
961 return -EINVAL;
962
963 spin_lock_irqsave(&bam_ch[id].lock, flags);
964 bam_ch[id].use_wm = 1;
965 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
966 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
967 id, bam_ch[id].num_tx_pkts, ret);
968 if (!bam_ch_is_local_open(id)) {
969 ret = -ENODEV;
970 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
971 }
972 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
973
974 return ret;
975}
976
977int msm_bam_dmux_is_ch_low(uint32_t id)
978{
979 int ret;
980
981 if (id >= BAM_DMUX_NUM_CHANNELS)
982 return -EINVAL;
983
984 bam_ch[id].use_wm = 1;
985 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
986 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
987 id, bam_ch[id].num_tx_pkts, ret);
988 if (!bam_ch_is_local_open(id)) {
989 ret = -ENODEV;
990 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
991 }
992
993 return ret;
994}
995
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700996static void rx_switch_to_interrupt_mode(void)
997{
998 struct sps_connect cur_rx_conn;
999 struct sps_iovec iov;
1000 struct rx_pkt_info *info;
1001 int ret;
1002
1003 /*
1004 * Attempt to enable interrupts - if this fails,
1005 * continue polling and we will retry later.
1006 */
1007 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1008 if (ret) {
1009 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
1010 goto fail;
1011 }
1012
1013 rx_register_event.options = SPS_O_EOT;
1014 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1015 if (ret) {
1016 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1017 goto fail;
1018 }
1019
1020 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1021 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1022 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1023 if (ret) {
1024 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1025 goto fail;
1026 }
1027 polling_mode = 0;
Eric Holmberg006057d2012-01-11 10:10:42 -07001028 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001029
1030 /* handle any rx packets before interrupt was enabled */
1031 while (bam_connection_is_active && !polling_mode) {
1032 ret = sps_get_iovec(bam_rx_pipe, &iov);
1033 if (ret) {
1034 pr_err("%s: sps_get_iovec failed %d\n",
1035 __func__, ret);
1036 break;
1037 }
1038 if (iov.addr == 0)
1039 break;
1040
1041 mutex_lock(&bam_rx_pool_mutexlock);
1042 if (unlikely(list_empty(&bam_rx_pool))) {
1043 mutex_unlock(&bam_rx_pool_mutexlock);
1044 continue;
1045 }
1046 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1047 list_node);
1048 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001049 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001050 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001051 if (info->dma_address != iov.addr)
1052 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1053 __func__,
1054 (void *)info->dma_address, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001055 handle_bam_mux_cmd(&info->work);
1056 }
1057 return;
1058
1059fail:
1060 pr_err("%s: reverting to polling\n", __func__);
1061 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1062}
1063
Jeff Hugo949080a2011-08-30 11:58:56 -06001064static void rx_timer_work_func(struct work_struct *work)
1065{
1066 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001067 struct rx_pkt_info *info;
1068 int inactive_cycles = 0;
1069 int ret;
Jeff Hugo949080a2011-08-30 11:58:56 -06001070
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001071 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001072 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001073 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001074 if (in_global_reset)
1075 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001076
1077 ret = sps_get_iovec(bam_rx_pipe, &iov);
1078 if (ret) {
1079 pr_err("%s: sps_get_iovec failed %d\n",
1080 __func__, ret);
1081 break;
1082 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001083 if (iov.addr == 0)
1084 break;
1085 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001086 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001087 if (unlikely(list_empty(&bam_rx_pool))) {
1088 mutex_unlock(&bam_rx_pool_mutexlock);
1089 continue;
1090 }
1091 info = list_first_entry(&bam_rx_pool,
1092 struct rx_pkt_info, list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001093 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001094 list_del(&info->list_node);
Jeff Hugoc9749932011-11-02 17:50:40 -06001095 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001096 handle_bam_mux_cmd(&info->work);
1097 }
1098
1099 if (inactive_cycles == POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001100 rx_switch_to_interrupt_mode();
1101 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001102 }
1103
1104 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1105 }
1106}
1107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108static void bam_mux_tx_notify(struct sps_event_notify *notify)
1109{
1110 struct tx_pkt_info *pkt;
1111
1112 DBG("%s: event %d notified\n", __func__, notify->event_id);
1113
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001114 if (in_global_reset)
1115 return;
1116
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001117 switch (notify->event_id) {
1118 case SPS_EVENT_EOT:
1119 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001120 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 dma_unmap_single(NULL, pkt->dma_address,
1122 pkt->skb->len,
1123 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001124 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125 dma_unmap_single(NULL, pkt->dma_address,
1126 pkt->len,
1127 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001128 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 break;
1130 default:
1131 pr_err("%s: recieved unexpected event id %d\n", __func__,
1132 notify->event_id);
1133 }
1134}
1135
Jeff Hugo33dbc002011-08-25 15:52:53 -06001136static void bam_mux_rx_notify(struct sps_event_notify *notify)
1137{
Jeff Hugo949080a2011-08-30 11:58:56 -06001138 int ret;
1139 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001140
1141 DBG("%s: event %d notified\n", __func__, notify->event_id);
1142
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001143 if (in_global_reset)
1144 return;
1145
Jeff Hugo33dbc002011-08-25 15:52:53 -06001146 switch (notify->event_id) {
1147 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001148 /* attempt to disable interrupts in this pipe */
1149 if (!polling_mode) {
1150 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1151 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001152 pr_err("%s: sps_get_config() failed %d, interrupts"
1153 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001154 break;
1155 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001156 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001157 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1158 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1159 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001160 pr_err("%s: sps_set_config() failed %d, interrupts"
1161 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001162 break;
1163 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001164 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001165 polling_mode = 1;
1166 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1167 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001168 break;
1169 default:
1170 pr_err("%s: recieved unexpected event id %d\n", __func__,
1171 notify->event_id);
1172 }
1173}
1174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175#ifdef CONFIG_DEBUG_FS
1176
1177static int debug_tbl(char *buf, int max)
1178{
1179 int i = 0;
1180 int j;
1181
1182 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1183 i += scnprintf(buf + i, max - i,
1184 "ch%02d local open=%s remote open=%s\n",
1185 j, bam_ch_is_local_open(j) ? "Y" : "N",
1186 bam_ch_is_remote_open(j) ? "Y" : "N");
1187 }
1188
1189 return i;
1190}
1191
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001192static int debug_ul_pkt_cnt(char *buf, int max)
1193{
1194 struct list_head *p;
1195 unsigned long flags;
1196 int n = 0;
1197
1198 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1199 __list_for_each(p, &bam_tx_pool) {
1200 ++n;
1201 }
1202 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1203
1204 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1205}
1206
1207static int debug_stats(char *buf, int max)
1208{
1209 int i = 0;
1210
1211 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001212 "skb read cnt: %u\n"
1213 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001214 "skb copy cnt: %u\n"
1215 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001216 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001217 "sps tx stalls: %u\n"
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001218 "rx queue len: %d\n"
1219 "a2 ack out cnt: %d\n"
1220 "a2 ack in cnt: %d\n"
1221 "a2 pwr cntl in: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001222 bam_dmux_read_cnt,
1223 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001224 bam_dmux_write_cpy_cnt,
1225 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001226 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001227 bam_dmux_tx_stall_cnt,
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001228 bam_rx_pool_len,
1229 atomic_read(&bam_dmux_ack_out_cnt),
1230 atomic_read(&bam_dmux_ack_in_cnt),
1231 atomic_read(&bam_dmux_a2_pwr_cntl_in_cnt)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001232 );
1233
1234 return i;
1235}
1236
Eric Holmberg878923a2012-01-10 14:28:19 -07001237static int debug_log(char *buff, int max, loff_t *ppos)
1238{
1239 unsigned long flags;
1240 int i = 0;
1241
1242 if (bam_dmux_state_logging_disabled) {
1243 i += scnprintf(buff - i, max - i, "Logging disabled\n");
1244 return i;
1245 }
1246
1247 if (*ppos == 0) {
1248 i += scnprintf(buff - i, max - i,
1249 "<DMUX> timestamp FLAGS [Message]\n"
1250 "FLAGS:\n"
Eric Holmberg006057d2012-01-11 10:10:42 -07001251 "\tD: 1 = Power collapse disabled\n"
Eric Holmberg878923a2012-01-10 14:28:19 -07001252 "\tR: 1 = in global reset\n"
1253 "\tP: 1 = BAM is powered up\n"
1254 "\tA: 1 = BAM initialized and ready for data\n"
1255 "\n"
1256 "\tV: 1 = Uplink vote for power\n"
1257 "\tU: 1 = Uplink active\n"
1258 "\tW: 1 = Uplink Wait-for-ack\n"
1259 "\tA: 1 = Uplink ACK received\n"
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001260 "\t#: >=1 On-demand uplink vote\n"
Eric Holmberg878923a2012-01-10 14:28:19 -07001261 );
1262 buff += i;
1263 }
1264
1265 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
1266 while (kfifo_len(&bam_dmux_state_log)
1267 && (i + LOG_MESSAGE_MAX_SIZE) < max) {
1268 int k_len;
1269 k_len = kfifo_out(&bam_dmux_state_log,
1270 buff, LOG_MESSAGE_MAX_SIZE);
1271 if (k_len != LOG_MESSAGE_MAX_SIZE) {
1272 pr_err("%s: retrieve failure %d\n", __func__, k_len);
1273 break;
1274 }
1275
1276 /* keep non-null portion of string and add line break */
1277 k_len = strnlen(buff, LOG_MESSAGE_MAX_SIZE);
1278 buff += k_len;
1279 i += k_len;
1280 if (k_len && *(buff - 1) != '\n') {
1281 *buff++ = '\n';
1282 ++i;
1283 }
1284 }
1285 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
1286
1287 return i;
1288}
1289
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290#define DEBUG_BUFMAX 4096
1291static char debug_buffer[DEBUG_BUFMAX];
1292
1293static ssize_t debug_read(struct file *file, char __user *buf,
1294 size_t count, loff_t *ppos)
1295{
1296 int (*fill)(char *buf, int max) = file->private_data;
1297 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1298 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1299}
1300
Eric Holmberg878923a2012-01-10 14:28:19 -07001301static ssize_t debug_read_multiple(struct file *file, char __user *buff,
1302 size_t count, loff_t *ppos)
1303{
1304 int (*util_func)(char *buf, int max, loff_t *) = file->private_data;
1305 char *buffer;
1306 int bsize;
1307
1308 buffer = kmalloc(count, GFP_KERNEL);
1309 if (!buffer)
1310 return -ENOMEM;
1311
1312 bsize = util_func(buffer, count, ppos);
1313
1314 if (bsize >= 0) {
1315 if (copy_to_user(buff, buffer, bsize)) {
1316 kfree(buffer);
1317 return -EFAULT;
1318 }
1319 *ppos += bsize;
1320 }
1321 kfree(buffer);
1322 return bsize;
1323}
1324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325static int debug_open(struct inode *inode, struct file *file)
1326{
1327 file->private_data = inode->i_private;
1328 return 0;
1329}
1330
1331
1332static const struct file_operations debug_ops = {
1333 .read = debug_read,
1334 .open = debug_open,
1335};
1336
Eric Holmberg878923a2012-01-10 14:28:19 -07001337static const struct file_operations debug_ops_multiple = {
1338 .read = debug_read_multiple,
1339 .open = debug_open,
1340};
1341
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342static void debug_create(const char *name, mode_t mode,
1343 struct dentry *dent,
1344 int (*fill)(char *buf, int max))
1345{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001346 struct dentry *file;
1347
1348 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1349 if (IS_ERR(file))
1350 pr_err("%s: debugfs create failed %d\n", __func__,
1351 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001352}
1353
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001354static void debug_create_multiple(const char *name, mode_t mode,
1355 struct dentry *dent,
1356 int (*fill)(char *buf, int max, loff_t *ppos))
1357{
1358 struct dentry *file;
1359
1360 file = debugfs_create_file(name, mode, dent, fill, &debug_ops_multiple);
1361 if (IS_ERR(file))
1362 pr_err("%s: debugfs create failed %d\n", __func__,
1363 (int)PTR_ERR(file));
1364}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001365#endif
1366
Jeff Hugod98b1082011-10-24 10:30:23 -06001367static void notify_all(int event, unsigned long data)
1368{
1369 int i;
1370
1371 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001372 if (bam_ch_is_open(i)) {
Jeff Hugod98b1082011-10-24 10:30:23 -06001373 bam_ch[i].notify(bam_ch[i].priv, event, data);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001374 bam_dmux_log("%s: cid=%d, event=%d, data=%lu\n",
1375 __func__, i, event, data);
1376 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001377 }
1378}
1379
1380static void kickoff_ul_wakeup_func(struct work_struct *work)
1381{
1382 read_lock(&ul_wakeup_lock);
1383 if (!bam_is_connected) {
1384 read_unlock(&ul_wakeup_lock);
1385 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001386 if (unlikely(in_global_reset == 1))
1387 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001388 read_lock(&ul_wakeup_lock);
1389 ul_packet_written = 1;
1390 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1391 }
1392 read_unlock(&ul_wakeup_lock);
1393}
1394
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001395int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001396{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001397 int is_connected;
1398
1399 read_lock(&ul_wakeup_lock);
1400 ul_packet_written = 1;
1401 is_connected = bam_is_connected;
1402 if (!is_connected)
1403 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1404 read_unlock(&ul_wakeup_lock);
1405
1406 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001407}
1408
Eric Holmberg878923a2012-01-10 14:28:19 -07001409static void power_vote(int vote)
1410{
1411 bam_dmux_log("%s: curr=%d, vote=%d\n", __func__,
1412 bam_dmux_uplink_vote, vote);
1413
1414 if (bam_dmux_uplink_vote == vote)
1415 bam_dmux_log("%s: warning - duplicate power vote\n", __func__);
1416
1417 bam_dmux_uplink_vote = vote;
1418 if (vote)
1419 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1420 else
1421 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1422}
1423
Eric Holmberg454d9da2012-01-12 09:37:14 -07001424/*
1425 * @note: Must be called with ul_wakeup_lock locked.
1426 */
1427static inline void ul_powerdown(void)
1428{
1429 bam_dmux_log("%s: powerdown\n", __func__);
1430 verify_tx_queue_is_empty(__func__);
1431
1432 if (a2_pc_disabled) {
1433 wait_for_dfab = 1;
1434 INIT_COMPLETION(dfab_unvote_completion);
1435 release_wakelock();
1436 } else {
1437 wait_for_ack = 1;
1438 INIT_COMPLETION(ul_wakeup_ack_completion);
1439 power_vote(0);
1440 }
1441 bam_is_connected = 0;
1442 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1443}
1444
1445static inline void ul_powerdown_finish(void)
1446{
1447 if (a2_pc_disabled && wait_for_dfab) {
1448 unvote_dfab();
1449 complete_all(&dfab_unvote_completion);
1450 wait_for_dfab = 0;
1451 }
1452}
1453
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001454/*
1455 * Votes for UL power and returns current power state.
1456 *
1457 * @returns true if currently connected
1458 */
1459int msm_bam_dmux_ul_power_vote(void)
1460{
1461 int is_connected;
1462
1463 read_lock(&ul_wakeup_lock);
1464 atomic_inc(&ul_ondemand_vote);
1465 is_connected = bam_is_connected;
1466 if (!is_connected)
1467 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1468 read_unlock(&ul_wakeup_lock);
1469
1470 return is_connected;
1471}
1472
1473/*
1474 * Unvotes for UL power.
1475 *
1476 * @returns true if vote count is 0 (UL shutdown possible)
1477 */
1478int msm_bam_dmux_ul_power_unvote(void)
1479{
1480 int vote;
1481
1482 read_lock(&ul_wakeup_lock);
1483 vote = atomic_dec_return(&ul_ondemand_vote);
1484 if (unlikely(vote) < 0)
1485 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1486 read_unlock(&ul_wakeup_lock);
1487
1488 return vote == 0;
1489}
1490
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001491static void ul_timeout(struct work_struct *work)
1492{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001493 unsigned long flags;
1494 int ret;
1495
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001496 if (in_global_reset)
1497 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001498 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1499 if (!ret) { /* failed to grab lock, reschedule and bail */
1500 schedule_delayed_work(&ul_timeout_work,
1501 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1502 return;
1503 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001504 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001505 if (!ul_packet_written) {
1506 spin_lock(&bam_tx_pool_spinlock);
1507 if (!list_empty(&bam_tx_pool)) {
1508 struct tx_pkt_info *info;
1509
1510 info = list_first_entry(&bam_tx_pool,
1511 struct tx_pkt_info, list_node);
1512 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1513 __func__, info->ts_sec, info->ts_nsec);
1514 DBG_INC_TX_STALL_CNT();
1515 ul_packet_written = 1;
1516 }
1517 spin_unlock(&bam_tx_pool_spinlock);
1518 }
1519
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001520 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
1521 bam_dmux_log("%s: pkt written %d\n",
1522 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001523 ul_packet_written = 0;
1524 schedule_delayed_work(&ul_timeout_work,
1525 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001526 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001527 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001528 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001529 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001530 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001531 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001532}
Jeff Hugo4838f412012-01-20 11:19:37 -07001533
1534static int ssrestart_check(void)
1535{
1536 /*
1537 * if the restart level is RESET_SOC, SSR is not on
1538 * so the crashed modem will end up crashing the system
1539 * anyways, so use BUG() to report the error
1540 * else prepare for the restart event which should
1541 * happen soon
1542 */
1543 DMUX_LOG_KERR("%s: modem timeout\n", __func__);
1544 if (get_restart_level() <= RESET_SOC) {
1545 BUG();
1546 return 0;
1547 } else {
1548 in_global_reset = 1;
1549 return 1;
1550 }
1551}
1552
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001553static void ul_wakeup(void)
1554{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001555 int ret;
1556
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001557 mutex_lock(&wakeup_lock);
1558 if (bam_is_connected) { /* bam got connected before lock grabbed */
Eric Holmberg878923a2012-01-10 14:28:19 -07001559 bam_dmux_log("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001560 mutex_unlock(&wakeup_lock);
1561 return;
1562 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001563
Eric Holmberg006057d2012-01-11 10:10:42 -07001564 if (a2_pc_disabled) {
1565 /*
1566 * don't grab the wakelock the first time because it is
1567 * already grabbed when a2 powers on
1568 */
Jeff Hugo583a6da2012-02-03 11:37:30 -07001569 if (likely(a2_pc_disabled_wakelock_skipped))
Eric Holmberg006057d2012-01-11 10:10:42 -07001570 grab_wakelock();
1571 else
Jeff Hugo583a6da2012-02-03 11:37:30 -07001572 a2_pc_disabled_wakelock_skipped = 1;
Eric Holmberg006057d2012-01-11 10:10:42 -07001573 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001574 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001575 &dfab_unvote_completion, HZ);
1576 BUG_ON(ret == 0);
1577 }
1578 vote_dfab();
1579 schedule_delayed_work(&ul_timeout_work,
1580 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1581 bam_is_connected = 1;
1582 mutex_unlock(&wakeup_lock);
1583 return;
1584 }
1585
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001586 /*
1587 * must wait for the previous power down request to have been acked
1588 * chances are it already came in and this will just fall through
1589 * instead of waiting
1590 */
1591 if (wait_for_ack) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001592 bam_dmux_log("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001593 ret = wait_for_completion_timeout(
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001594 &ul_wakeup_ack_completion, HZ);
Eric Holmberg006057d2012-01-11 10:10:42 -07001595 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001596 if (unlikely(ret == 0) && ssrestart_check()) {
1597 mutex_unlock(&wakeup_lock);
1598 bam_dmux_log("%s timeout previous ack\n", __func__);
1599 return;
1600 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001601 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001602 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001603 power_vote(1);
1604 bam_dmux_log("%s waiting for wakeup ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001605 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001606 if (unlikely(ret == 0) && ssrestart_check()) {
1607 mutex_unlock(&wakeup_lock);
1608 bam_dmux_log("%s timeout wakeup ack\n", __func__);
1609 return;
1610 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001611 bam_dmux_log("%s waiting completion\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001612 ret = wait_for_completion_timeout(&bam_connection_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001613 if (unlikely(ret == 0) && ssrestart_check()) {
1614 mutex_unlock(&wakeup_lock);
1615 bam_dmux_log("%s timeout power on\n", __func__);
1616 return;
1617 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001618
1619 bam_is_connected = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -07001620 bam_dmux_log("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001621 schedule_delayed_work(&ul_timeout_work,
1622 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1623 mutex_unlock(&wakeup_lock);
1624}
1625
1626static void reconnect_to_bam(void)
1627{
1628 int i;
1629
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001630 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001631 vote_dfab();
1632 i = sps_device_reset(a2_device_handle);
1633 if (i)
1634 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1635 i = sps_connect(bam_tx_pipe, &tx_connection);
1636 if (i)
1637 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1638 i = sps_connect(bam_rx_pipe, &rx_connection);
1639 if (i)
1640 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1641 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1642 if (i)
1643 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1644 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1645 if (i)
1646 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001647
1648 bam_connection_is_active = 1;
1649
1650 if (polling_mode)
1651 rx_switch_to_interrupt_mode();
1652
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001653 queue_rx();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001654
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001655 toggle_apps_ack();
1656 complete_all(&bam_connection_completion);
1657}
1658
1659static void disconnect_to_bam(void)
1660{
1661 struct list_head *node;
1662 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001663 unsigned long flags;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001664
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001665 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001666
1667 /* handle disconnect during active UL */
1668 write_lock_irqsave(&ul_wakeup_lock, flags);
1669 if (bam_is_connected) {
1670 bam_dmux_log("%s: UL active - forcing powerdown\n", __func__);
1671 ul_powerdown();
1672 }
1673 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1674 ul_powerdown_finish();
1675
1676 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001677 INIT_COMPLETION(bam_connection_completion);
1678 sps_disconnect(bam_tx_pipe);
1679 sps_disconnect(bam_rx_pipe);
1680 unvote_dfab();
1681 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1682 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001683
1684 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001685 while (!list_empty(&bam_rx_pool)) {
1686 node = bam_rx_pool.next;
1687 list_del(node);
1688 info = container_of(node, struct rx_pkt_info, list_node);
1689 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1690 DMA_FROM_DEVICE);
1691 dev_kfree_skb_any(info->skb);
1692 kfree(info);
1693 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001694 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001695 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001696
1697 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001698}
1699
1700static void vote_dfab(void)
1701{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001702 int rc;
1703
Eric Holmberg006057d2012-01-11 10:10:42 -07001704 bam_dmux_log("%s\n", __func__);
1705 mutex_lock(&dfab_status_lock);
1706 if (dfab_is_on) {
1707 bam_dmux_log("%s: dfab is already on\n", __func__);
1708 mutex_unlock(&dfab_status_lock);
1709 return;
1710 }
Jeff Hugo23a812b2012-01-13 13:43:42 -07001711 rc = clk_prepare_enable(dfab_clk);
Jeff Hugoca0caa82011-12-05 16:05:23 -07001712 if (rc)
Eric Holmberg006057d2012-01-11 10:10:42 -07001713 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n", rc);
Stephen Boyd69d35e32012-02-14 15:33:30 -08001714 rc = clk_prepare_enable(xo_clk);
1715 if (rc)
1716 DMUX_LOG_KERR("bam_dmux vote for xo failed rc = %d\n", rc);
Eric Holmberg006057d2012-01-11 10:10:42 -07001717 dfab_is_on = 1;
1718 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001719}
1720
1721static void unvote_dfab(void)
1722{
Eric Holmberg006057d2012-01-11 10:10:42 -07001723 bam_dmux_log("%s\n", __func__);
1724 mutex_lock(&dfab_status_lock);
1725 if (!dfab_is_on) {
1726 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1727 dump_stack();
1728 mutex_unlock(&dfab_status_lock);
1729 return;
1730 }
Jeff Hugo23a812b2012-01-13 13:43:42 -07001731 clk_disable_unprepare(dfab_clk);
Stephen Boyd69d35e32012-02-14 15:33:30 -08001732 clk_disable_unprepare(xo_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001733 dfab_is_on = 0;
1734 mutex_unlock(&dfab_status_lock);
1735}
1736
1737/* reference counting wrapper around wakelock */
1738static void grab_wakelock(void)
1739{
1740 unsigned long flags;
1741
1742 spin_lock_irqsave(&wakelock_reference_lock, flags);
1743 bam_dmux_log("%s: ref count = %d\n", __func__,
1744 wakelock_reference_count);
1745 if (wakelock_reference_count == 0)
1746 wake_lock(&bam_wakelock);
1747 ++wakelock_reference_count;
1748 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1749}
1750
1751static void release_wakelock(void)
1752{
1753 unsigned long flags;
1754
1755 spin_lock_irqsave(&wakelock_reference_lock, flags);
1756 if (wakelock_reference_count == 0) {
1757 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1758 dump_stack();
1759 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1760 return;
1761 }
1762 bam_dmux_log("%s: ref count = %d\n", __func__,
1763 wakelock_reference_count);
1764 --wakelock_reference_count;
1765 if (wakelock_reference_count == 0)
1766 wake_unlock(&bam_wakelock);
1767 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001768}
1769
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001770static int restart_notifier_cb(struct notifier_block *this,
1771 unsigned long code,
1772 void *data)
1773{
1774 int i;
1775 struct list_head *node;
1776 struct tx_pkt_info *info;
1777 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001778 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001779
1780 if (code != SUBSYS_AFTER_SHUTDOWN)
1781 return NOTIFY_DONE;
1782
Eric Holmberg878923a2012-01-10 14:28:19 -07001783 bam_dmux_log("%s: begin\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001784 in_global_reset = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001785
1786 /* Handle uplink Powerdown */
1787 write_lock_irqsave(&ul_wakeup_lock, flags);
1788 if (bam_is_connected) {
1789 ul_powerdown();
1790 wait_for_ack = 0;
1791 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001792 /*
1793 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1794 * reset to 0. harmless if bam_is_connected check above passes
1795 */
1796 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001797 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1798 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001799 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07001800 a2_pc_disabled_wakelock_skipped = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001801
1802 /* Cleanup Channel States */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001803 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1804 temp_remote_status = bam_ch_is_remote_open(i);
1805 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001806 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001807 if (bam_ch_is_local_open(i))
1808 bam_ch[i].status |= BAM_CH_IN_RESET;
1809 if (temp_remote_status) {
1810 platform_device_unregister(bam_ch[i].pdev);
1811 bam_ch[i].pdev = platform_device_alloc(
1812 bam_ch[i].name, 2);
1813 }
1814 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001815
1816 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001817 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001818 while (!list_empty(&bam_tx_pool)) {
1819 node = bam_tx_pool.next;
1820 list_del(node);
1821 info = container_of(node, struct tx_pkt_info,
1822 list_node);
1823 if (!info->is_cmd) {
1824 dma_unmap_single(NULL, info->dma_address,
1825 info->skb->len,
1826 DMA_TO_DEVICE);
1827 dev_kfree_skb_any(info->skb);
1828 } else {
1829 dma_unmap_single(NULL, info->dma_address,
1830 info->len,
1831 DMA_TO_DEVICE);
1832 kfree(info->skb);
1833 }
1834 kfree(info);
1835 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001836 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001837
Eric Holmberg878923a2012-01-10 14:28:19 -07001838 bam_dmux_log("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001839 return NOTIFY_DONE;
1840}
1841
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001842static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001843{
1844 u32 h;
1845 dma_addr_t dma_addr;
1846 int ret;
1847 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001848 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001850 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001851 /* init BAM */
1852 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1853 if (!a2_virt_addr) {
1854 pr_err("%s: ioremap failed\n", __func__);
1855 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07001856 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 }
1858 a2_props.phys_addr = A2_PHYS_BASE;
1859 a2_props.virt_addr = a2_virt_addr;
1860 a2_props.virt_size = A2_PHYS_SIZE;
1861 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001862 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 a2_props.num_pipes = A2_NUM_PIPES;
1864 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001865 if (cpu_is_msm9615())
1866 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001867 /* need to free on tear down */
1868 ret = sps_register_bam_device(&a2_props, &h);
1869 if (ret < 0) {
1870 pr_err("%s: register bam error %d\n", __func__, ret);
1871 goto register_bam_failed;
1872 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001873 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874
1875 bam_tx_pipe = sps_alloc_endpoint();
1876 if (bam_tx_pipe == NULL) {
1877 pr_err("%s: tx alloc endpoint failed\n", __func__);
1878 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001879 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880 }
1881 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1882 if (ret) {
1883 pr_err("%s: tx get config failed %d\n", __func__, ret);
1884 goto tx_get_config_failed;
1885 }
1886
1887 tx_connection.source = SPS_DEV_HANDLE_MEM;
1888 tx_connection.src_pipe_index = 0;
1889 tx_connection.destination = h;
1890 tx_connection.dest_pipe_index = 4;
1891 tx_connection.mode = SPS_MODE_DEST;
1892 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1893 tx_desc_mem_buf.size = 0x800; /* 2k */
1894 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1895 &dma_addr, 0);
1896 if (tx_desc_mem_buf.base == NULL) {
1897 pr_err("%s: tx memory alloc failed\n", __func__);
1898 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001899 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900 }
1901 tx_desc_mem_buf.phys_base = dma_addr;
1902 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1903 tx_connection.desc = tx_desc_mem_buf;
1904 tx_connection.event_thresh = 0x10;
1905
1906 ret = sps_connect(bam_tx_pipe, &tx_connection);
1907 if (ret < 0) {
1908 pr_err("%s: tx connect error %d\n", __func__, ret);
1909 goto tx_connect_failed;
1910 }
1911
1912 bam_rx_pipe = sps_alloc_endpoint();
1913 if (bam_rx_pipe == NULL) {
1914 pr_err("%s: rx alloc endpoint failed\n", __func__);
1915 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001916 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917 }
1918 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1919 if (ret) {
1920 pr_err("%s: rx get config failed %d\n", __func__, ret);
1921 goto rx_get_config_failed;
1922 }
1923
1924 rx_connection.source = h;
1925 rx_connection.src_pipe_index = 5;
1926 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1927 rx_connection.dest_pipe_index = 1;
1928 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001929 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1930 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931 rx_desc_mem_buf.size = 0x800; /* 2k */
1932 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1933 &dma_addr, 0);
1934 if (rx_desc_mem_buf.base == NULL) {
1935 pr_err("%s: rx memory alloc failed\n", __func__);
1936 ret = -ENOMEM;
1937 goto rx_mem_failed;
1938 }
1939 rx_desc_mem_buf.phys_base = dma_addr;
1940 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1941 rx_connection.desc = rx_desc_mem_buf;
1942 rx_connection.event_thresh = 0x10;
1943
1944 ret = sps_connect(bam_rx_pipe, &rx_connection);
1945 if (ret < 0) {
1946 pr_err("%s: rx connect error %d\n", __func__, ret);
1947 goto rx_connect_failed;
1948 }
1949
1950 tx_register_event.options = SPS_O_EOT;
1951 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1952 tx_register_event.xfer_done = NULL;
1953 tx_register_event.callback = bam_mux_tx_notify;
1954 tx_register_event.user = NULL;
1955 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1956 if (ret < 0) {
1957 pr_err("%s: tx register event error %d\n", __func__, ret);
1958 goto rx_event_reg_failed;
1959 }
1960
Jeff Hugo33dbc002011-08-25 15:52:53 -06001961 rx_register_event.options = SPS_O_EOT;
1962 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1963 rx_register_event.xfer_done = NULL;
1964 rx_register_event.callback = bam_mux_rx_notify;
1965 rx_register_event.user = NULL;
1966 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1967 if (ret < 0) {
1968 pr_err("%s: tx register event error %d\n", __func__, ret);
1969 goto rx_event_reg_failed;
1970 }
1971
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972 bam_mux_initialized = 1;
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001973 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001974 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001975 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001976 complete_all(&bam_connection_completion);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001977 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001978
1979rx_event_reg_failed:
1980 sps_disconnect(bam_rx_pipe);
1981rx_connect_failed:
1982 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1983 rx_desc_mem_buf.phys_base);
1984rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001985rx_get_config_failed:
1986 sps_free_endpoint(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001987rx_alloc_endpoint_failed:
1988 sps_disconnect(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989tx_connect_failed:
1990 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1991 tx_desc_mem_buf.phys_base);
1992tx_get_config_failed:
1993 sps_free_endpoint(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001994tx_alloc_endpoint_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001996 /*
1997 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
1998 * same handle below will cause a crash, so skip it if we've freed
1999 * the handle here.
2000 */
2001 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002002register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002003 if (!skip_iounmap)
2004 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07002005ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002006 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002007 return ret;
2008}
2009
2010static int bam_init_fallback(void)
2011{
2012 u32 h;
2013 int ret;
2014 void *a2_virt_addr;
2015
2016 unvote_dfab();
2017 /* init BAM */
2018 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
2019 if (!a2_virt_addr) {
2020 pr_err("%s: ioremap failed\n", __func__);
2021 ret = -ENOMEM;
2022 goto ioremap_failed;
2023 }
2024 a2_props.phys_addr = A2_PHYS_BASE;
2025 a2_props.virt_addr = a2_virt_addr;
2026 a2_props.virt_size = A2_PHYS_SIZE;
2027 a2_props.irq = A2_BAM_IRQ;
2028 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2029 a2_props.num_pipes = A2_NUM_PIPES;
2030 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2031 if (cpu_is_msm9615())
2032 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
2033 ret = sps_register_bam_device(&a2_props, &h);
2034 if (ret < 0) {
2035 pr_err("%s: register bam error %d\n", __func__, ret);
2036 goto register_bam_failed;
2037 }
2038 a2_device_handle = h;
2039
2040 return 0;
2041
2042register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002043 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002044ioremap_failed:
2045 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002046}
Jeff Hugoade1f842011-08-03 15:53:59 -06002047
Eric Holmberg604ab252012-01-15 00:01:18 -07002048static void msm9615_bam_init(struct work_struct *work)
2049{
2050 int ret = 0;
2051
2052 ret = bam_init();
2053 if (ret) {
2054 ret = bam_init_fallback();
2055 if (ret)
2056 pr_err("%s: bam init fallback failed: %d",
2057 __func__, ret);
2058 }
2059}
2060
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002061static void toggle_apps_ack(void)
2062{
2063 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002064
2065 bam_dmux_log("%s: apps ack %d->%d\n", __func__,
2066 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002067 smsm_change_state(SMSM_APPS_STATE,
2068 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2069 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2070 clear_bit = ~clear_bit;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002071 DBG_INC_ACK_OUT_CNT();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002072}
2073
Jeff Hugoade1f842011-08-03 15:53:59 -06002074static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2075{
Eric Holmberg878923a2012-01-10 14:28:19 -07002076 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002077 DBG_INC_A2_POWER_CONTROL_IN_CNT();
Eric Holmberg878923a2012-01-10 14:28:19 -07002078 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
2079 new_state);
2080
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002081 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002082 bam_dmux_log("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002083 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002084 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002085 } else if (bam_mux_initialized &&
2086 !(new_state & SMSM_A2_POWER_CONTROL)) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002087 bam_dmux_log("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002088 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002089 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002090 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002091 bam_dmux_log("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002092 grab_wakelock();
Eric Holmberg604ab252012-01-15 00:01:18 -07002093 if (cpu_is_msm9615()) {
2094 /*
2095 * even though a2 has signaled it is ready via the
2096 * SMSM_A2_POWER_CONTROL bit, it has not yet
2097 * enabled the pipes as needed by sps_connect
2098 * in satallite mode. Add a short delay to give modem
2099 * time to enable the pipes.
2100 */
2101 schedule_delayed_work(&msm9615_bam_init_work,
2102 msecs_to_jiffies(100));
2103 } else {
2104 bam_init();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002105 }
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002106 } else {
Eric Holmberg878923a2012-01-10 14:28:19 -07002107 bam_dmux_log("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002108 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002109 }
Jeff Hugoade1f842011-08-03 15:53:59 -06002110
2111}
2112
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002113static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2114 uint32_t new_state)
2115{
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002116 DBG_INC_ACK_IN_CNT();
Eric Holmberg878923a2012-01-10 14:28:19 -07002117 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
2118 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002119 complete_all(&ul_wakeup_ack_completion);
2120}
2121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002122static int bam_dmux_probe(struct platform_device *pdev)
2123{
2124 int rc;
2125
2126 DBG("%s probe called\n", __func__);
2127 if (bam_mux_initialized)
2128 return 0;
2129
Stephen Boyd69d35e32012-02-14 15:33:30 -08002130 xo_clk = clk_get(&pdev->dev, "xo");
2131 if (IS_ERR(xo_clk)) {
2132 pr_err("%s: did not get xo clock\n", __func__);
2133 return PTR_ERR(xo_clk);
2134 }
Stephen Boyd1c51a492011-10-26 12:11:47 -07002135 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002136 if (IS_ERR(dfab_clk)) {
2137 pr_err("%s: did not get dfab clock\n", __func__);
2138 return -EFAULT;
2139 }
2140
2141 rc = clk_set_rate(dfab_clk, 64000000);
2142 if (rc)
2143 pr_err("%s: unable to set dfab clock rate\n", __func__);
2144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002145 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
2146 if (!bam_mux_rx_workqueue)
2147 return -ENOMEM;
2148
2149 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2150 if (!bam_mux_tx_workqueue) {
2151 destroy_workqueue(bam_mux_rx_workqueue);
2152 return -ENOMEM;
2153 }
2154
Jeff Hugo7960abd2011-08-02 15:39:38 -06002155 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002156 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002157 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2158 "bam_dmux_ch_%d", rc);
2159 /* bus 2, ie a2 stream 2 */
2160 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2161 if (!bam_ch[rc].pdev) {
2162 pr_err("%s: platform device alloc failed\n", __func__);
2163 destroy_workqueue(bam_mux_rx_workqueue);
2164 destroy_workqueue(bam_mux_tx_workqueue);
2165 return -ENOMEM;
2166 }
2167 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002169 init_completion(&ul_wakeup_ack_completion);
2170 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002171 init_completion(&dfab_unvote_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002172 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Eric Holmberg604ab252012-01-15 00:01:18 -07002173 INIT_DELAYED_WORK(&msm9615_bam_init_work, msm9615_bam_init);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002174 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002175
Jeff Hugoade1f842011-08-03 15:53:59 -06002176 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2177 bam_dmux_smsm_cb, NULL);
2178
2179 if (rc) {
2180 destroy_workqueue(bam_mux_rx_workqueue);
2181 destroy_workqueue(bam_mux_tx_workqueue);
2182 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2183 return -ENOMEM;
2184 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002186 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2187 bam_dmux_smsm_ack_cb, NULL);
2188
2189 if (rc) {
2190 destroy_workqueue(bam_mux_rx_workqueue);
2191 destroy_workqueue(bam_mux_tx_workqueue);
2192 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2193 SMSM_A2_POWER_CONTROL,
2194 bam_dmux_smsm_cb, NULL);
2195 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2196 rc);
2197 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2198 platform_device_put(bam_ch[rc].pdev);
2199 return -ENOMEM;
2200 }
2201
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002202 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2203 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 return 0;
2206}
2207
2208static struct platform_driver bam_dmux_driver = {
2209 .probe = bam_dmux_probe,
2210 .driver = {
2211 .name = "BAM_RMNT",
2212 .owner = THIS_MODULE,
2213 },
2214};
2215
2216static int __init bam_dmux_init(void)
2217{
Eric Holmberg878923a2012-01-10 14:28:19 -07002218 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002219#ifdef CONFIG_DEBUG_FS
2220 struct dentry *dent;
2221
2222 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002223 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002224 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002225 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2226 debug_create("stats", 0444, dent, debug_stats);
Eric Holmberge4ac80b2012-01-12 09:21:59 -07002227 debug_create_multiple("log", 0444, dent, debug_log);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002228 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002229#endif
Eric Holmberg878923a2012-01-10 14:28:19 -07002230 ret = kfifo_alloc(&bam_dmux_state_log, PAGE_SIZE, GFP_KERNEL);
2231 if (ret) {
2232 pr_err("%s: failed to allocate log %d\n", __func__, ret);
2233 bam_dmux_state_logging_disabled = 1;
2234 }
2235
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002236 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002237 return platform_driver_register(&bam_dmux_driver);
2238}
2239
Jeff Hugoade1f842011-08-03 15:53:59 -06002240late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002241MODULE_DESCRIPTION("MSM BAM DMUX");
2242MODULE_LICENSE("GPL v2");