blob: 0a56dd2efd162be790b37551677799d8d8fff773 [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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
69#define DBG(x...) do { \
70 if (msm_bam_dmux_debug_enable) \
71 pr_debug(x); \
72 } while (0)
73
74#define DBG_INC_READ_CNT(x) do { \
75 bam_dmux_read_cnt += (x); \
76 if (msm_bam_dmux_debug_enable) \
77 pr_debug("%s: total read bytes %u\n", \
78 __func__, bam_dmux_read_cnt); \
79 } while (0)
80
81#define DBG_INC_WRITE_CNT(x) do { \
82 bam_dmux_write_cnt += (x); \
83 if (msm_bam_dmux_debug_enable) \
84 pr_debug("%s: total written bytes %u\n", \
85 __func__, bam_dmux_write_cnt); \
86 } while (0)
87
88#define DBG_INC_WRITE_CPY(x) do { \
89 bam_dmux_write_cpy_bytes += (x); \
90 bam_dmux_write_cpy_cnt++; \
91 if (msm_bam_dmux_debug_enable) \
92 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
93 __func__, bam_dmux_write_cpy_cnt, \
94 bam_dmux_write_cpy_bytes); \
95 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070096
97#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
98 bam_dmux_tx_sps_failure_cnt++; \
99} while (0)
100
Eric Holmberg6074aba2012-01-18 17:59:44 -0700101#define DBG_INC_TX_STALL_CNT() do { \
102 bam_dmux_tx_stall_cnt++; \
103} while (0)
104
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105#else
106#define DBG(x...) do { } while (0)
107#define DBG_INC_READ_CNT(x...) do { } while (0)
108#define DBG_INC_WRITE_CNT(x...) do { } while (0)
109#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700110#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Eric Holmberg6074aba2012-01-18 17:59:44 -0700111#define DBG_INC_TX_STALL_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112#endif
113
114struct bam_ch_info {
115 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600116 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117 void *priv;
118 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600119 struct platform_device *pdev;
120 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700121 int num_tx_pkts;
122 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123};
124
125struct tx_pkt_info {
126 struct sk_buff *skb;
127 dma_addr_t dma_address;
128 char is_cmd;
129 uint32_t len;
130 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600131 struct list_head list_node;
Eric Holmberg878923a2012-01-10 14:28:19 -0700132 unsigned ts_sec;
133 unsigned long ts_nsec;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134};
135
136struct rx_pkt_info {
137 struct sk_buff *skb;
138 dma_addr_t dma_address;
139 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600140 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141};
142
143#define A2_NUM_PIPES 6
144#define A2_SUMMING_THRESHOLD 4096
145#define A2_DEFAULT_DESCRIPTORS 32
146#define A2_PHYS_BASE 0x124C2000
147#define A2_PHYS_SIZE 0x2000
148#define BUFFER_SIZE 2048
149#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600151static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152static struct sps_pipe *bam_tx_pipe;
153static struct sps_pipe *bam_rx_pipe;
154static struct sps_connect tx_connection;
155static struct sps_connect rx_connection;
156static struct sps_mem_buffer tx_desc_mem_buf;
157static struct sps_mem_buffer rx_desc_mem_buf;
158static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600159static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160
161static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
162static int bam_mux_initialized;
163
Jeff Hugo949080a2011-08-30 11:58:56 -0600164static int polling_mode;
165
166static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600167static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700168static int bam_rx_pool_len;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600169static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600170static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172struct bam_mux_hdr {
173 uint16_t magic_num;
174 uint8_t reserved;
175 uint8_t cmd;
176 uint8_t pad_len;
177 uint8_t ch_id;
178 uint16_t pkt_len;
179};
180
Jeff Hugod98b1082011-10-24 10:30:23 -0600181static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182static void bam_mux_write_done(struct work_struct *work);
183static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600184static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185
Jeff Hugo949080a2011-08-30 11:58:56 -0600186static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
188static struct workqueue_struct *bam_mux_rx_workqueue;
189static struct workqueue_struct *bam_mux_tx_workqueue;
190
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600191/* A2 power collaspe */
192#define UL_TIMEOUT_DELAY 1000 /* in ms */
193static void toggle_apps_ack(void);
194static void reconnect_to_bam(void);
195static void disconnect_to_bam(void);
196static void ul_wakeup(void);
197static void ul_timeout(struct work_struct *work);
198static void vote_dfab(void);
199static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600200static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700201static void grab_wakelock(void);
202static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600203
204static int bam_is_connected;
205static DEFINE_MUTEX(wakeup_lock);
206static struct completion ul_wakeup_ack_completion;
207static struct completion bam_connection_completion;
208static struct delayed_work ul_timeout_work;
209static int ul_packet_written;
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700210static atomic_t ul_ondemand_vote = ATOMIC_INIT(0);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600211static struct clk *dfab_clk;
212static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600213static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600214static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700215static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700216static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700217static int a2_pc_disabled;
218static DEFINE_MUTEX(dfab_status_lock);
219static int dfab_is_on;
220static int wait_for_dfab;
221static struct completion dfab_unvote_completion;
222static DEFINE_SPINLOCK(wakelock_reference_lock);
223static int wakelock_reference_count;
Eric Holmberg604ab252012-01-15 00:01:18 -0700224static struct delayed_work msm9615_bam_init_work;
Jeff Hugo583a6da2012-02-03 11:37:30 -0700225static int a2_pc_disabled_wakelock_skipped;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600226/* End A2 power collaspe */
227
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600228/* subsystem restart */
229static int restart_notifier_cb(struct notifier_block *this,
230 unsigned long code,
231 void *data);
232
233static struct notifier_block restart_notifier = {
234 .notifier_call = restart_notifier_cb,
235};
236static int in_global_reset;
237/* end subsystem restart */
238
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239#define bam_ch_is_open(x) \
240 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
241
242#define bam_ch_is_local_open(x) \
243 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
244
245#define bam_ch_is_remote_open(x) \
246 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
247
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600248#define bam_ch_is_in_reset(x) \
249 (bam_ch[(x)].status & BAM_CH_IN_RESET)
250
Eric Holmberg878923a2012-01-10 14:28:19 -0700251#define LOG_MESSAGE_MAX_SIZE 80
252struct kfifo bam_dmux_state_log;
253static uint32_t bam_dmux_state_logging_disabled;
254static DEFINE_SPINLOCK(bam_dmux_logging_spinlock);
255static int bam_dmux_uplink_vote;
256static int bam_dmux_power_state;
257
258
259#define DMUX_LOG_KERR(fmt...) \
260do { \
261 bam_dmux_log(fmt); \
262 pr_err(fmt); \
263} while (0)
264
265/**
266 * Log a state change along with a small message.
267 *
268 * Complete size of messsage is limited to @todo.
269 */
270static void bam_dmux_log(const char *fmt, ...)
271{
272 char buff[LOG_MESSAGE_MAX_SIZE];
273 unsigned long flags;
274 va_list arg_list;
275 unsigned long long t_now;
276 unsigned long nanosec_rem;
277 int len = 0;
278
279 if (bam_dmux_state_logging_disabled)
280 return;
281
282 t_now = sched_clock();
283 nanosec_rem = do_div(t_now, 1000000000U);
284
285 /*
286 * States
Eric Holmberg006057d2012-01-11 10:10:42 -0700287 * D: 1 = Power collapse disabled
Eric Holmberg878923a2012-01-10 14:28:19 -0700288 * R: 1 = in global reset
289 * P: 1 = BAM is powered up
290 * A: 1 = BAM initialized and ready for data
291 *
292 * V: 1 = Uplink vote for power
293 * U: 1 = Uplink active
294 * W: 1 = Uplink Wait-for-ack
295 * A: 1 = Uplink ACK received
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700296 * #: >=1 On-demand uplink vote
Eric Holmberg878923a2012-01-10 14:28:19 -0700297 */
298 len += scnprintf(buff, sizeof(buff),
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700299 "<DMUX> %u.%09lu %c%c%c%c %c%c%c%c%d ",
Eric Holmberg878923a2012-01-10 14:28:19 -0700300 (unsigned)t_now, nanosec_rem,
Eric Holmberg006057d2012-01-11 10:10:42 -0700301 a2_pc_disabled ? 'D' : 'd',
Eric Holmberg878923a2012-01-10 14:28:19 -0700302 in_global_reset ? 'R' : 'r',
303 bam_dmux_power_state ? 'P' : 'p',
304 bam_connection_is_active ? 'A' : 'a',
305 bam_dmux_uplink_vote ? 'V' : 'v',
306 bam_is_connected ? 'U' : 'u',
307 wait_for_ack ? 'W' : 'w',
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700308 ul_wakeup_ack_completion.done ? 'A' : 'a',
309 atomic_read(&ul_ondemand_vote)
Eric Holmberg878923a2012-01-10 14:28:19 -0700310 );
311
312 va_start(arg_list, fmt);
313 len += vscnprintf(buff + len, sizeof(buff) - len, fmt, arg_list);
314 va_end(arg_list);
315 memset(buff + len, 0x0, sizeof(buff) - len);
316
317 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
318 if (kfifo_avail(&bam_dmux_state_log) < LOG_MESSAGE_MAX_SIZE) {
319 char junk[LOG_MESSAGE_MAX_SIZE];
320 int ret;
321
322 ret = kfifo_out(&bam_dmux_state_log, junk, sizeof(junk));
323 if (ret != LOG_MESSAGE_MAX_SIZE) {
324 pr_err("%s: unable to empty log %d\n", __func__, ret);
325 spin_unlock_irqrestore(&bam_dmux_logging_spinlock,
326 flags);
327 return;
328 }
329 }
330 kfifo_in(&bam_dmux_state_log, buff, sizeof(buff));
331 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
332}
333
334static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
335{
336 unsigned long long t_now;
337
338 t_now = sched_clock();
339 pkt->ts_nsec = do_div(t_now, 1000000000U);
340 pkt->ts_sec = (unsigned)t_now;
341}
342
343static inline void verify_tx_queue_is_empty(const char *func)
344{
345 unsigned long flags;
346 struct tx_pkt_info *info;
347 int reported = 0;
348
349 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
350 list_for_each_entry(info, &bam_tx_pool, list_node) {
351 if (!reported) {
Eric Holmberg454d9da2012-01-12 09:37:14 -0700352 bam_dmux_log("%s: tx pool not empty\n", func);
353 if (!in_global_reset)
354 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700355 reported = 1;
356 }
Eric Holmberg454d9da2012-01-12 09:37:14 -0700357 bam_dmux_log("%s: node=%p ts=%u.%09lu\n", __func__,
358 &info->list_node, info->ts_sec, info->ts_nsec);
359 if (!in_global_reset)
360 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
361 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700362 }
363 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
364}
365
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366static void queue_rx(void)
367{
368 void *ptr;
369 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700370 int ret;
371 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600372
Jeff Hugoc9749932011-11-02 17:50:40 -0600373 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700374 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600375 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600376
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700377 while (rx_len_cached < NUM_BUFFERS) {
378 if (in_global_reset)
379 goto fail;
380
381 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
382 if (!info) {
383 pr_err("%s: unable to alloc rx_pkt_info\n", __func__);
384 goto fail;
385 }
386
387 INIT_WORK(&info->work, handle_bam_mux_cmd);
388
389 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
390 if (info->skb == NULL) {
391 DMUX_LOG_KERR("%s: unable to alloc skb\n", __func__);
392 goto fail_info;
393 }
394 ptr = skb_put(info->skb, BUFFER_SIZE);
395
396 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
397 DMA_FROM_DEVICE);
398 if (info->dma_address == 0 || info->dma_address == ~0) {
399 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
400 __func__, (void *)info->dma_address, ptr);
401 goto fail_skb;
402 }
403
404 mutex_lock(&bam_rx_pool_mutexlock);
405 list_add_tail(&info->list_node, &bam_rx_pool);
406 rx_len_cached = ++bam_rx_pool_len;
407 mutex_unlock(&bam_rx_pool_mutexlock);
408
409 ret = sps_transfer_one(bam_rx_pipe, info->dma_address,
410 BUFFER_SIZE, info,
411 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
412
413 if (ret) {
414 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
415 __func__, ret);
416 goto fail_transfer;
417 }
418 }
419 return;
420
421fail_transfer:
422 mutex_lock(&bam_rx_pool_mutexlock);
423 list_del(&info->list_node);
424 --bam_rx_pool_len;
425 rx_len_cached = bam_rx_pool_len;
426 mutex_unlock(&bam_rx_pool_mutexlock);
427
428 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
429 DMA_FROM_DEVICE);
430
431fail_skb:
432 dev_kfree_skb_any(info->skb);
433
434fail_info:
435 kfree(info);
436
437fail:
438 if (rx_len_cached == 0) {
439 DMUX_LOG_KERR("%s: RX queue failure\n", __func__);
440 in_global_reset = 1;
441 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442}
443
444static void bam_mux_process_data(struct sk_buff *rx_skb)
445{
446 unsigned long flags;
447 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600448 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
450 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
451
452 rx_skb->data = (unsigned char *)(rx_hdr + 1);
453 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
454 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600455 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600457 event_data = (unsigned long)(rx_skb);
458
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600460 if (bam_ch[rx_hdr->ch_id].notify)
461 bam_ch[rx_hdr->ch_id].notify(
462 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
463 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 else
465 dev_kfree_skb_any(rx_skb);
466 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
467
468 queue_rx();
469}
470
Eric Holmberg006057d2012-01-11 10:10:42 -0700471static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
472{
473 unsigned long flags;
474 int ret;
475
476 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
477 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
478 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
479 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
480 queue_rx();
481 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
482 if (ret)
483 pr_err("%s: platform_device_add() error: %d\n",
484 __func__, ret);
485}
486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487static void handle_bam_mux_cmd(struct work_struct *work)
488{
489 unsigned long flags;
490 struct bam_mux_hdr *rx_hdr;
491 struct rx_pkt_info *info;
492 struct sk_buff *rx_skb;
493
494 info = container_of(work, struct rx_pkt_info, work);
495 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600496 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 kfree(info);
498
499 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
500
501 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
502 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
503 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
504 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
505 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700506 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
507 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 " pad %d ch %d len %d\n", __func__,
509 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
510 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
511 dev_kfree_skb_any(rx_skb);
512 queue_rx();
513 return;
514 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700515
516 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700517 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
518 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700519 " pad %d ch %d len %d\n", __func__,
520 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
521 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
522 dev_kfree_skb_any(rx_skb);
523 queue_rx();
524 return;
525 }
526
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 switch (rx_hdr->cmd) {
528 case BAM_MUX_HDR_CMD_DATA:
529 DBG_INC_READ_CNT(rx_hdr->pkt_len);
530 bam_mux_process_data(rx_skb);
531 break;
532 case BAM_MUX_HDR_CMD_OPEN:
Eric Holmberg006057d2012-01-11 10:10:42 -0700533 bam_dmux_log("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700534 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700535 handle_bam_mux_cmd_open(rx_hdr);
536 dev_kfree_skb_any(rx_skb);
537 break;
538 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
539 bam_dmux_log("%s: opening cid %d PC disabled\n", __func__,
540 rx_hdr->ch_id);
541
542 if (!a2_pc_disabled) {
543 a2_pc_disabled = 1;
544 schedule_delayed_work(&ul_timeout_work,
545 msecs_to_jiffies(UL_TIMEOUT_DELAY));
546 }
547
548 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600549 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 break;
551 case BAM_MUX_HDR_CMD_CLOSE:
552 /* probably should drop pending write */
Eric Holmberg878923a2012-01-10 14:28:19 -0700553 bam_dmux_log("%s: closing cid %d\n", __func__,
554 rx_hdr->ch_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
556 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
557 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600559 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
560 bam_ch[rx_hdr->ch_id].pdev =
561 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
562 if (!bam_ch[rx_hdr->ch_id].pdev)
563 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600564 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 break;
566 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700567 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
568 " reserved %d cmd %d pad %d ch %d len %d\n",
569 __func__, rx_hdr->magic_num, rx_hdr->reserved,
570 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
571 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572 dev_kfree_skb_any(rx_skb);
573 queue_rx();
574 return;
575 }
576}
577
578static int bam_mux_write_cmd(void *data, uint32_t len)
579{
580 int rc;
581 struct tx_pkt_info *pkt;
582 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700583 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600585 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 if (pkt == NULL) {
587 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
588 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589 return rc;
590 }
591
592 dma_address = dma_map_single(NULL, data, len,
593 DMA_TO_DEVICE);
594 if (!dma_address) {
595 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700596 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598 return rc;
599 }
600 pkt->skb = (struct sk_buff *)(data);
601 pkt->len = len;
602 pkt->dma_address = dma_address;
603 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700604 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600605 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700606 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600607 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
609 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600610 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700611 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
612 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600613 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700614 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700615 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700616 dma_unmap_single(NULL, pkt->dma_address,
617 pkt->len,
618 DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600619 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700620 } else {
621 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600622 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600624 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 return rc;
626}
627
628static void bam_mux_write_done(struct work_struct *work)
629{
630 struct sk_buff *skb;
631 struct bam_mux_hdr *hdr;
632 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700633 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600634 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700635 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600637 if (in_global_reset)
638 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700639
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700641
642 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
643 info_expected = list_first_entry(&bam_tx_pool,
644 struct tx_pkt_info, list_node);
645 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700646 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700647
Eric Holmberg878923a2012-01-10 14:28:19 -0700648 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
649 " list_node=%p, ts=%u.%09lu\n",
650 __func__, bam_tx_pool.next, &info->list_node,
651 info->ts_sec, info->ts_nsec
652 );
653
654 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
655 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
656 &errant_pkt->list_node, errant_pkt->ts_sec,
657 errant_pkt->ts_nsec);
658
659 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700660 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
661 BUG();
662 }
663 list_del(&info->list_node);
664 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
665
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600666 if (info->is_cmd) {
667 kfree(info->skb);
668 kfree(info);
669 return;
670 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 skb = info->skb;
672 kfree(info);
673 hdr = (struct bam_mux_hdr *)skb->data;
Eric Holmberg9fdef262012-02-14 11:46:05 -0700674 DBG_INC_WRITE_CNT(skb->len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600675 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700676 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
677 bam_ch[hdr->ch_id].num_tx_pkts--;
678 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600679 if (bam_ch[hdr->ch_id].notify)
680 bam_ch[hdr->ch_id].notify(
681 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
682 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 else
684 dev_kfree_skb_any(skb);
685}
686
687int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
688{
689 int rc = 0;
690 struct bam_mux_hdr *hdr;
691 unsigned long flags;
692 struct sk_buff *new_skb = NULL;
693 dma_addr_t dma_address;
694 struct tx_pkt_info *pkt;
695
696 if (id >= BAM_DMUX_NUM_CHANNELS)
697 return -EINVAL;
698 if (!skb)
699 return -EINVAL;
700 if (!bam_mux_initialized)
701 return -ENODEV;
702
703 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
704 spin_lock_irqsave(&bam_ch[id].lock, flags);
705 if (!bam_ch_is_open(id)) {
706 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
707 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
708 return -ENODEV;
709 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700710
711 if (bam_ch[id].use_wm &&
712 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
713 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
714 pr_err("%s: watermark exceeded: %d\n", __func__, id);
715 return -EAGAIN;
716 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
718
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600719 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600720 if (!bam_is_connected) {
721 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600722 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700723 if (unlikely(in_global_reset == 1))
724 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600725 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600726 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600727 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600728
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 /* if skb do not have any tailroom for padding,
730 copy the skb into a new expanded skb */
731 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
732 /* revisit, probably dev_alloc_skb and memcpy is effecient */
733 new_skb = skb_copy_expand(skb, skb_headroom(skb),
734 4 - (skb->len & 0x3), GFP_ATOMIC);
735 if (new_skb == NULL) {
736 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600737 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 }
739 dev_kfree_skb_any(skb);
740 skb = new_skb;
741 DBG_INC_WRITE_CPY(skb->len);
742 }
743
744 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
745
746 /* caller should allocate for hdr and padding
747 hdr is fine, padding is tricky */
748 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
749 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
750 hdr->reserved = 0;
751 hdr->ch_id = id;
752 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
753 if (skb->len & 0x3)
754 skb_put(skb, 4 - (skb->len & 0x3));
755
756 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
757
758 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
759 __func__, skb->data, skb->tail, skb->len,
760 hdr->pkt_len, hdr->pad_len);
761
762 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
763 if (pkt == NULL) {
764 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600765 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 }
767
768 dma_address = dma_map_single(NULL, skb->data, skb->len,
769 DMA_TO_DEVICE);
770 if (!dma_address) {
771 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600772 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 }
774 pkt->skb = skb;
775 pkt->dma_address = dma_address;
776 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700777 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700779 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600780 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
782 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600783 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700784 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
785 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600786 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700787 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700788 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700789 dma_unmap_single(NULL, pkt->dma_address,
790 pkt->skb->len, DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600791 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700792 if (new_skb)
793 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700794 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700795 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700796 spin_lock_irqsave(&bam_ch[id].lock, flags);
797 bam_ch[id].num_tx_pkts++;
798 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600799 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600800 ul_packet_written = 1;
801 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600803
804write_fail3:
805 kfree(pkt);
806write_fail2:
807 if (new_skb)
808 dev_kfree_skb_any(new_skb);
809write_fail:
810 read_unlock(&ul_wakeup_lock);
811 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812}
813
814int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600815 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816{
817 struct bam_mux_hdr *hdr;
818 unsigned long flags;
819 int rc = 0;
820
821 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700822 if (!bam_mux_initialized) {
823 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700825 }
826 if (id >= BAM_DMUX_NUM_CHANNELS) {
827 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700829 }
830 if (notify == NULL) {
831 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600832 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700833 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834
835 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
836 if (hdr == NULL) {
837 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
838 return -ENOMEM;
839 }
840 spin_lock_irqsave(&bam_ch[id].lock, flags);
841 if (bam_ch_is_open(id)) {
842 DBG("%s: Already opened %d\n", __func__, id);
843 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
844 kfree(hdr);
845 goto open_done;
846 }
847 if (!bam_ch_is_remote_open(id)) {
848 DBG("%s: Remote not open; ch: %d\n", __func__, id);
849 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
850 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700851 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 }
853
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600854 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 bam_ch[id].priv = priv;
856 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700857 bam_ch[id].num_tx_pkts = 0;
858 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
860
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600861 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600862 if (!bam_is_connected) {
863 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600864 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700865 if (unlikely(in_global_reset == 1))
866 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600867 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600868 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600869 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600870
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
872 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
873 hdr->reserved = 0;
874 hdr->ch_id = id;
875 hdr->pkt_len = 0;
876 hdr->pad_len = 0;
877
878 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600879 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880
881open_done:
882 DBG("%s: opened ch %d\n", __func__, id);
883 return rc;
884}
885
886int msm_bam_dmux_close(uint32_t id)
887{
888 struct bam_mux_hdr *hdr;
889 unsigned long flags;
890 int rc;
891
892 if (id >= BAM_DMUX_NUM_CHANNELS)
893 return -EINVAL;
894 DBG("%s: closing ch %d\n", __func__, id);
895 if (!bam_mux_initialized)
896 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600898 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600899 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600900 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600901 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700902 if (unlikely(in_global_reset == 1))
903 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600904 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600905 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600906 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600907
Jeff Hugo061ce672011-10-21 17:15:32 -0600908 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600909 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910 bam_ch[id].priv = NULL;
911 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
912 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
913
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600914 if (bam_ch_is_in_reset(id)) {
915 read_unlock(&ul_wakeup_lock);
916 bam_ch[id].status &= ~BAM_CH_IN_RESET;
917 return 0;
918 }
919
Jeff Hugobb5802f2011-11-02 17:10:29 -0600920 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921 if (hdr == NULL) {
922 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600923 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 return -ENOMEM;
925 }
926 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
927 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
928 hdr->reserved = 0;
929 hdr->ch_id = id;
930 hdr->pkt_len = 0;
931 hdr->pad_len = 0;
932
933 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600934 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935
936 DBG("%s: closed ch %d\n", __func__, id);
937 return rc;
938}
939
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700940int msm_bam_dmux_is_ch_full(uint32_t id)
941{
942 unsigned long flags;
943 int ret;
944
945 if (id >= BAM_DMUX_NUM_CHANNELS)
946 return -EINVAL;
947
948 spin_lock_irqsave(&bam_ch[id].lock, flags);
949 bam_ch[id].use_wm = 1;
950 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
951 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
952 id, bam_ch[id].num_tx_pkts, ret);
953 if (!bam_ch_is_local_open(id)) {
954 ret = -ENODEV;
955 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
956 }
957 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
958
959 return ret;
960}
961
962int msm_bam_dmux_is_ch_low(uint32_t id)
963{
964 int ret;
965
966 if (id >= BAM_DMUX_NUM_CHANNELS)
967 return -EINVAL;
968
969 bam_ch[id].use_wm = 1;
970 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
971 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
972 id, bam_ch[id].num_tx_pkts, ret);
973 if (!bam_ch_is_local_open(id)) {
974 ret = -ENODEV;
975 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
976 }
977
978 return ret;
979}
980
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700981static void rx_switch_to_interrupt_mode(void)
982{
983 struct sps_connect cur_rx_conn;
984 struct sps_iovec iov;
985 struct rx_pkt_info *info;
986 int ret;
987
988 /*
989 * Attempt to enable interrupts - if this fails,
990 * continue polling and we will retry later.
991 */
992 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
993 if (ret) {
994 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
995 goto fail;
996 }
997
998 rx_register_event.options = SPS_O_EOT;
999 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1000 if (ret) {
1001 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1002 goto fail;
1003 }
1004
1005 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1006 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1007 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1008 if (ret) {
1009 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1010 goto fail;
1011 }
1012 polling_mode = 0;
Eric Holmberg006057d2012-01-11 10:10:42 -07001013 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001014
1015 /* handle any rx packets before interrupt was enabled */
1016 while (bam_connection_is_active && !polling_mode) {
1017 ret = sps_get_iovec(bam_rx_pipe, &iov);
1018 if (ret) {
1019 pr_err("%s: sps_get_iovec failed %d\n",
1020 __func__, ret);
1021 break;
1022 }
1023 if (iov.addr == 0)
1024 break;
1025
1026 mutex_lock(&bam_rx_pool_mutexlock);
1027 if (unlikely(list_empty(&bam_rx_pool))) {
1028 mutex_unlock(&bam_rx_pool_mutexlock);
1029 continue;
1030 }
1031 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1032 list_node);
1033 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001034 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001035 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001036 if (info->dma_address != iov.addr)
1037 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1038 __func__,
1039 (void *)info->dma_address, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001040 handle_bam_mux_cmd(&info->work);
1041 }
1042 return;
1043
1044fail:
1045 pr_err("%s: reverting to polling\n", __func__);
1046 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1047}
1048
Jeff Hugo949080a2011-08-30 11:58:56 -06001049static void rx_timer_work_func(struct work_struct *work)
1050{
1051 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001052 struct rx_pkt_info *info;
1053 int inactive_cycles = 0;
1054 int ret;
Jeff Hugo949080a2011-08-30 11:58:56 -06001055
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001056 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001057 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001058 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001059 if (in_global_reset)
1060 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001061
1062 ret = sps_get_iovec(bam_rx_pipe, &iov);
1063 if (ret) {
1064 pr_err("%s: sps_get_iovec failed %d\n",
1065 __func__, ret);
1066 break;
1067 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001068 if (iov.addr == 0)
1069 break;
1070 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001071 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001072 if (unlikely(list_empty(&bam_rx_pool))) {
1073 mutex_unlock(&bam_rx_pool_mutexlock);
1074 continue;
1075 }
1076 info = list_first_entry(&bam_rx_pool,
1077 struct rx_pkt_info, list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001078 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001079 list_del(&info->list_node);
Jeff Hugoc9749932011-11-02 17:50:40 -06001080 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001081 handle_bam_mux_cmd(&info->work);
1082 }
1083
1084 if (inactive_cycles == POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001085 rx_switch_to_interrupt_mode();
1086 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001087 }
1088
1089 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1090 }
1091}
1092
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093static void bam_mux_tx_notify(struct sps_event_notify *notify)
1094{
1095 struct tx_pkt_info *pkt;
1096
1097 DBG("%s: event %d notified\n", __func__, notify->event_id);
1098
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001099 if (in_global_reset)
1100 return;
1101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 switch (notify->event_id) {
1103 case SPS_EVENT_EOT:
1104 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001105 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 dma_unmap_single(NULL, pkt->dma_address,
1107 pkt->skb->len,
1108 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001109 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 dma_unmap_single(NULL, pkt->dma_address,
1111 pkt->len,
1112 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001113 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001114 break;
1115 default:
1116 pr_err("%s: recieved unexpected event id %d\n", __func__,
1117 notify->event_id);
1118 }
1119}
1120
Jeff Hugo33dbc002011-08-25 15:52:53 -06001121static void bam_mux_rx_notify(struct sps_event_notify *notify)
1122{
Jeff Hugo949080a2011-08-30 11:58:56 -06001123 int ret;
1124 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001125
1126 DBG("%s: event %d notified\n", __func__, notify->event_id);
1127
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001128 if (in_global_reset)
1129 return;
1130
Jeff Hugo33dbc002011-08-25 15:52:53 -06001131 switch (notify->event_id) {
1132 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001133 /* attempt to disable interrupts in this pipe */
1134 if (!polling_mode) {
1135 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1136 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001137 pr_err("%s: sps_get_config() failed %d, interrupts"
1138 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001139 break;
1140 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001141 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001142 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1143 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1144 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001145 pr_err("%s: sps_set_config() failed %d, interrupts"
1146 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001147 break;
1148 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001149 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001150 polling_mode = 1;
1151 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1152 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001153 break;
1154 default:
1155 pr_err("%s: recieved unexpected event id %d\n", __func__,
1156 notify->event_id);
1157 }
1158}
1159
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160#ifdef CONFIG_DEBUG_FS
1161
1162static int debug_tbl(char *buf, int max)
1163{
1164 int i = 0;
1165 int j;
1166
1167 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1168 i += scnprintf(buf + i, max - i,
1169 "ch%02d local open=%s remote open=%s\n",
1170 j, bam_ch_is_local_open(j) ? "Y" : "N",
1171 bam_ch_is_remote_open(j) ? "Y" : "N");
1172 }
1173
1174 return i;
1175}
1176
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001177static int debug_ul_pkt_cnt(char *buf, int max)
1178{
1179 struct list_head *p;
1180 unsigned long flags;
1181 int n = 0;
1182
1183 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1184 __list_for_each(p, &bam_tx_pool) {
1185 ++n;
1186 }
1187 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1188
1189 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1190}
1191
1192static int debug_stats(char *buf, int max)
1193{
1194 int i = 0;
1195
1196 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001197 "skb read cnt: %u\n"
1198 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001199 "skb copy cnt: %u\n"
1200 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001201 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001202 "sps tx stalls: %u\n"
1203 "rx queue len: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001204 bam_dmux_read_cnt,
1205 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001206 bam_dmux_write_cpy_cnt,
1207 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001208 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001209 bam_dmux_tx_stall_cnt,
1210 bam_rx_pool_len
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001211 );
1212
1213 return i;
1214}
1215
Eric Holmberg878923a2012-01-10 14:28:19 -07001216static int debug_log(char *buff, int max, loff_t *ppos)
1217{
1218 unsigned long flags;
1219 int i = 0;
1220
1221 if (bam_dmux_state_logging_disabled) {
1222 i += scnprintf(buff - i, max - i, "Logging disabled\n");
1223 return i;
1224 }
1225
1226 if (*ppos == 0) {
1227 i += scnprintf(buff - i, max - i,
1228 "<DMUX> timestamp FLAGS [Message]\n"
1229 "FLAGS:\n"
Eric Holmberg006057d2012-01-11 10:10:42 -07001230 "\tD: 1 = Power collapse disabled\n"
Eric Holmberg878923a2012-01-10 14:28:19 -07001231 "\tR: 1 = in global reset\n"
1232 "\tP: 1 = BAM is powered up\n"
1233 "\tA: 1 = BAM initialized and ready for data\n"
1234 "\n"
1235 "\tV: 1 = Uplink vote for power\n"
1236 "\tU: 1 = Uplink active\n"
1237 "\tW: 1 = Uplink Wait-for-ack\n"
1238 "\tA: 1 = Uplink ACK received\n"
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001239 "\t#: >=1 On-demand uplink vote\n"
Eric Holmberg878923a2012-01-10 14:28:19 -07001240 );
1241 buff += i;
1242 }
1243
1244 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
1245 while (kfifo_len(&bam_dmux_state_log)
1246 && (i + LOG_MESSAGE_MAX_SIZE) < max) {
1247 int k_len;
1248 k_len = kfifo_out(&bam_dmux_state_log,
1249 buff, LOG_MESSAGE_MAX_SIZE);
1250 if (k_len != LOG_MESSAGE_MAX_SIZE) {
1251 pr_err("%s: retrieve failure %d\n", __func__, k_len);
1252 break;
1253 }
1254
1255 /* keep non-null portion of string and add line break */
1256 k_len = strnlen(buff, LOG_MESSAGE_MAX_SIZE);
1257 buff += k_len;
1258 i += k_len;
1259 if (k_len && *(buff - 1) != '\n') {
1260 *buff++ = '\n';
1261 ++i;
1262 }
1263 }
1264 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
1265
1266 return i;
1267}
1268
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269#define DEBUG_BUFMAX 4096
1270static char debug_buffer[DEBUG_BUFMAX];
1271
1272static ssize_t debug_read(struct file *file, char __user *buf,
1273 size_t count, loff_t *ppos)
1274{
1275 int (*fill)(char *buf, int max) = file->private_data;
1276 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1277 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1278}
1279
Eric Holmberg878923a2012-01-10 14:28:19 -07001280static ssize_t debug_read_multiple(struct file *file, char __user *buff,
1281 size_t count, loff_t *ppos)
1282{
1283 int (*util_func)(char *buf, int max, loff_t *) = file->private_data;
1284 char *buffer;
1285 int bsize;
1286
1287 buffer = kmalloc(count, GFP_KERNEL);
1288 if (!buffer)
1289 return -ENOMEM;
1290
1291 bsize = util_func(buffer, count, ppos);
1292
1293 if (bsize >= 0) {
1294 if (copy_to_user(buff, buffer, bsize)) {
1295 kfree(buffer);
1296 return -EFAULT;
1297 }
1298 *ppos += bsize;
1299 }
1300 kfree(buffer);
1301 return bsize;
1302}
1303
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304static int debug_open(struct inode *inode, struct file *file)
1305{
1306 file->private_data = inode->i_private;
1307 return 0;
1308}
1309
1310
1311static const struct file_operations debug_ops = {
1312 .read = debug_read,
1313 .open = debug_open,
1314};
1315
Eric Holmberg878923a2012-01-10 14:28:19 -07001316static const struct file_operations debug_ops_multiple = {
1317 .read = debug_read_multiple,
1318 .open = debug_open,
1319};
1320
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321static void debug_create(const char *name, mode_t mode,
1322 struct dentry *dent,
1323 int (*fill)(char *buf, int max))
1324{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001325 struct dentry *file;
1326
1327 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1328 if (IS_ERR(file))
1329 pr_err("%s: debugfs create failed %d\n", __func__,
1330 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331}
1332
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001333static void debug_create_multiple(const char *name, mode_t mode,
1334 struct dentry *dent,
1335 int (*fill)(char *buf, int max, loff_t *ppos))
1336{
1337 struct dentry *file;
1338
1339 file = debugfs_create_file(name, mode, dent, fill, &debug_ops_multiple);
1340 if (IS_ERR(file))
1341 pr_err("%s: debugfs create failed %d\n", __func__,
1342 (int)PTR_ERR(file));
1343}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001344#endif
1345
Jeff Hugod98b1082011-10-24 10:30:23 -06001346static void notify_all(int event, unsigned long data)
1347{
1348 int i;
1349
1350 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001351 if (bam_ch_is_open(i)) {
Jeff Hugod98b1082011-10-24 10:30:23 -06001352 bam_ch[i].notify(bam_ch[i].priv, event, data);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001353 bam_dmux_log("%s: cid=%d, event=%d, data=%lu\n",
1354 __func__, i, event, data);
1355 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001356 }
1357}
1358
1359static void kickoff_ul_wakeup_func(struct work_struct *work)
1360{
1361 read_lock(&ul_wakeup_lock);
1362 if (!bam_is_connected) {
1363 read_unlock(&ul_wakeup_lock);
1364 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001365 if (unlikely(in_global_reset == 1))
1366 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001367 read_lock(&ul_wakeup_lock);
1368 ul_packet_written = 1;
1369 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1370 }
1371 read_unlock(&ul_wakeup_lock);
1372}
1373
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001374int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001375{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001376 int is_connected;
1377
1378 read_lock(&ul_wakeup_lock);
1379 ul_packet_written = 1;
1380 is_connected = bam_is_connected;
1381 if (!is_connected)
1382 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1383 read_unlock(&ul_wakeup_lock);
1384
1385 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001386}
1387
Eric Holmberg878923a2012-01-10 14:28:19 -07001388static void power_vote(int vote)
1389{
1390 bam_dmux_log("%s: curr=%d, vote=%d\n", __func__,
1391 bam_dmux_uplink_vote, vote);
1392
1393 if (bam_dmux_uplink_vote == vote)
1394 bam_dmux_log("%s: warning - duplicate power vote\n", __func__);
1395
1396 bam_dmux_uplink_vote = vote;
1397 if (vote)
1398 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1399 else
1400 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1401}
1402
Eric Holmberg454d9da2012-01-12 09:37:14 -07001403/*
1404 * @note: Must be called with ul_wakeup_lock locked.
1405 */
1406static inline void ul_powerdown(void)
1407{
1408 bam_dmux_log("%s: powerdown\n", __func__);
1409 verify_tx_queue_is_empty(__func__);
1410
1411 if (a2_pc_disabled) {
1412 wait_for_dfab = 1;
1413 INIT_COMPLETION(dfab_unvote_completion);
1414 release_wakelock();
1415 } else {
1416 wait_for_ack = 1;
1417 INIT_COMPLETION(ul_wakeup_ack_completion);
1418 power_vote(0);
1419 }
1420 bam_is_connected = 0;
1421 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1422}
1423
1424static inline void ul_powerdown_finish(void)
1425{
1426 if (a2_pc_disabled && wait_for_dfab) {
1427 unvote_dfab();
1428 complete_all(&dfab_unvote_completion);
1429 wait_for_dfab = 0;
1430 }
1431}
1432
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001433/*
1434 * Votes for UL power and returns current power state.
1435 *
1436 * @returns true if currently connected
1437 */
1438int msm_bam_dmux_ul_power_vote(void)
1439{
1440 int is_connected;
1441
1442 read_lock(&ul_wakeup_lock);
1443 atomic_inc(&ul_ondemand_vote);
1444 is_connected = bam_is_connected;
1445 if (!is_connected)
1446 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1447 read_unlock(&ul_wakeup_lock);
1448
1449 return is_connected;
1450}
1451
1452/*
1453 * Unvotes for UL power.
1454 *
1455 * @returns true if vote count is 0 (UL shutdown possible)
1456 */
1457int msm_bam_dmux_ul_power_unvote(void)
1458{
1459 int vote;
1460
1461 read_lock(&ul_wakeup_lock);
1462 vote = atomic_dec_return(&ul_ondemand_vote);
1463 if (unlikely(vote) < 0)
1464 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1465 read_unlock(&ul_wakeup_lock);
1466
1467 return vote == 0;
1468}
1469
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001470static void ul_timeout(struct work_struct *work)
1471{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001472 unsigned long flags;
1473 int ret;
1474
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001475 if (in_global_reset)
1476 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001477 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1478 if (!ret) { /* failed to grab lock, reschedule and bail */
1479 schedule_delayed_work(&ul_timeout_work,
1480 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1481 return;
1482 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001483 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001484 if (!ul_packet_written) {
1485 spin_lock(&bam_tx_pool_spinlock);
1486 if (!list_empty(&bam_tx_pool)) {
1487 struct tx_pkt_info *info;
1488
1489 info = list_first_entry(&bam_tx_pool,
1490 struct tx_pkt_info, list_node);
1491 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1492 __func__, info->ts_sec, info->ts_nsec);
1493 DBG_INC_TX_STALL_CNT();
1494 ul_packet_written = 1;
1495 }
1496 spin_unlock(&bam_tx_pool_spinlock);
1497 }
1498
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001499 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
1500 bam_dmux_log("%s: pkt written %d\n",
1501 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001502 ul_packet_written = 0;
1503 schedule_delayed_work(&ul_timeout_work,
1504 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001505 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001506 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001507 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001508 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001509 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001510 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001511}
Jeff Hugo4838f412012-01-20 11:19:37 -07001512
1513static int ssrestart_check(void)
1514{
1515 /*
1516 * if the restart level is RESET_SOC, SSR is not on
1517 * so the crashed modem will end up crashing the system
1518 * anyways, so use BUG() to report the error
1519 * else prepare for the restart event which should
1520 * happen soon
1521 */
1522 DMUX_LOG_KERR("%s: modem timeout\n", __func__);
1523 if (get_restart_level() <= RESET_SOC) {
1524 BUG();
1525 return 0;
1526 } else {
1527 in_global_reset = 1;
1528 return 1;
1529 }
1530}
1531
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001532static void ul_wakeup(void)
1533{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001534 int ret;
1535
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001536 mutex_lock(&wakeup_lock);
1537 if (bam_is_connected) { /* bam got connected before lock grabbed */
Eric Holmberg878923a2012-01-10 14:28:19 -07001538 bam_dmux_log("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001539 mutex_unlock(&wakeup_lock);
1540 return;
1541 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001542
Eric Holmberg006057d2012-01-11 10:10:42 -07001543 if (a2_pc_disabled) {
1544 /*
1545 * don't grab the wakelock the first time because it is
1546 * already grabbed when a2 powers on
1547 */
Jeff Hugo583a6da2012-02-03 11:37:30 -07001548 if (likely(a2_pc_disabled_wakelock_skipped))
Eric Holmberg006057d2012-01-11 10:10:42 -07001549 grab_wakelock();
1550 else
Jeff Hugo583a6da2012-02-03 11:37:30 -07001551 a2_pc_disabled_wakelock_skipped = 1;
Eric Holmberg006057d2012-01-11 10:10:42 -07001552 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001553 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001554 &dfab_unvote_completion, HZ);
1555 BUG_ON(ret == 0);
1556 }
1557 vote_dfab();
1558 schedule_delayed_work(&ul_timeout_work,
1559 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1560 bam_is_connected = 1;
1561 mutex_unlock(&wakeup_lock);
1562 return;
1563 }
1564
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001565 /*
1566 * must wait for the previous power down request to have been acked
1567 * chances are it already came in and this will just fall through
1568 * instead of waiting
1569 */
1570 if (wait_for_ack) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001571 bam_dmux_log("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001572 ret = wait_for_completion_timeout(
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001573 &ul_wakeup_ack_completion, HZ);
Eric Holmberg006057d2012-01-11 10:10:42 -07001574 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001575 if (unlikely(ret == 0) && ssrestart_check()) {
1576 mutex_unlock(&wakeup_lock);
1577 bam_dmux_log("%s timeout previous ack\n", __func__);
1578 return;
1579 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001580 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001581 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001582 power_vote(1);
1583 bam_dmux_log("%s waiting for wakeup ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001584 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001585 if (unlikely(ret == 0) && ssrestart_check()) {
1586 mutex_unlock(&wakeup_lock);
1587 bam_dmux_log("%s timeout wakeup ack\n", __func__);
1588 return;
1589 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001590 bam_dmux_log("%s waiting completion\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001591 ret = wait_for_completion_timeout(&bam_connection_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001592 if (unlikely(ret == 0) && ssrestart_check()) {
1593 mutex_unlock(&wakeup_lock);
1594 bam_dmux_log("%s timeout power on\n", __func__);
1595 return;
1596 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001597
1598 bam_is_connected = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -07001599 bam_dmux_log("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001600 schedule_delayed_work(&ul_timeout_work,
1601 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1602 mutex_unlock(&wakeup_lock);
1603}
1604
1605static void reconnect_to_bam(void)
1606{
1607 int i;
1608
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001609 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001610 vote_dfab();
1611 i = sps_device_reset(a2_device_handle);
1612 if (i)
1613 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1614 i = sps_connect(bam_tx_pipe, &tx_connection);
1615 if (i)
1616 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1617 i = sps_connect(bam_rx_pipe, &rx_connection);
1618 if (i)
1619 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1620 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1621 if (i)
1622 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1623 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1624 if (i)
1625 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001626
1627 bam_connection_is_active = 1;
1628
1629 if (polling_mode)
1630 rx_switch_to_interrupt_mode();
1631
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001632 queue_rx();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001633
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001634 toggle_apps_ack();
1635 complete_all(&bam_connection_completion);
1636}
1637
1638static void disconnect_to_bam(void)
1639{
1640 struct list_head *node;
1641 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001642 unsigned long flags;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001643
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001644 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001645
1646 /* handle disconnect during active UL */
1647 write_lock_irqsave(&ul_wakeup_lock, flags);
1648 if (bam_is_connected) {
1649 bam_dmux_log("%s: UL active - forcing powerdown\n", __func__);
1650 ul_powerdown();
1651 }
1652 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1653 ul_powerdown_finish();
1654
1655 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001656 INIT_COMPLETION(bam_connection_completion);
1657 sps_disconnect(bam_tx_pipe);
1658 sps_disconnect(bam_rx_pipe);
1659 unvote_dfab();
1660 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1661 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001662
1663 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001664 while (!list_empty(&bam_rx_pool)) {
1665 node = bam_rx_pool.next;
1666 list_del(node);
1667 info = container_of(node, struct rx_pkt_info, list_node);
1668 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1669 DMA_FROM_DEVICE);
1670 dev_kfree_skb_any(info->skb);
1671 kfree(info);
1672 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001673 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001674 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001675
1676 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001677}
1678
1679static void vote_dfab(void)
1680{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001681 int rc;
1682
Eric Holmberg006057d2012-01-11 10:10:42 -07001683 bam_dmux_log("%s\n", __func__);
1684 mutex_lock(&dfab_status_lock);
1685 if (dfab_is_on) {
1686 bam_dmux_log("%s: dfab is already on\n", __func__);
1687 mutex_unlock(&dfab_status_lock);
1688 return;
1689 }
Jeff Hugo23a812b2012-01-13 13:43:42 -07001690 rc = clk_prepare_enable(dfab_clk);
Jeff Hugoca0caa82011-12-05 16:05:23 -07001691 if (rc)
Eric Holmberg006057d2012-01-11 10:10:42 -07001692 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n", rc);
1693 dfab_is_on = 1;
1694 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001695}
1696
1697static void unvote_dfab(void)
1698{
Eric Holmberg006057d2012-01-11 10:10:42 -07001699 bam_dmux_log("%s\n", __func__);
1700 mutex_lock(&dfab_status_lock);
1701 if (!dfab_is_on) {
1702 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1703 dump_stack();
1704 mutex_unlock(&dfab_status_lock);
1705 return;
1706 }
Jeff Hugo23a812b2012-01-13 13:43:42 -07001707 clk_disable_unprepare(dfab_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001708 dfab_is_on = 0;
1709 mutex_unlock(&dfab_status_lock);
1710}
1711
1712/* reference counting wrapper around wakelock */
1713static void grab_wakelock(void)
1714{
1715 unsigned long flags;
1716
1717 spin_lock_irqsave(&wakelock_reference_lock, flags);
1718 bam_dmux_log("%s: ref count = %d\n", __func__,
1719 wakelock_reference_count);
1720 if (wakelock_reference_count == 0)
1721 wake_lock(&bam_wakelock);
1722 ++wakelock_reference_count;
1723 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1724}
1725
1726static void release_wakelock(void)
1727{
1728 unsigned long flags;
1729
1730 spin_lock_irqsave(&wakelock_reference_lock, flags);
1731 if (wakelock_reference_count == 0) {
1732 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1733 dump_stack();
1734 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1735 return;
1736 }
1737 bam_dmux_log("%s: ref count = %d\n", __func__,
1738 wakelock_reference_count);
1739 --wakelock_reference_count;
1740 if (wakelock_reference_count == 0)
1741 wake_unlock(&bam_wakelock);
1742 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001743}
1744
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001745static int restart_notifier_cb(struct notifier_block *this,
1746 unsigned long code,
1747 void *data)
1748{
1749 int i;
1750 struct list_head *node;
1751 struct tx_pkt_info *info;
1752 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001753 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001754
1755 if (code != SUBSYS_AFTER_SHUTDOWN)
1756 return NOTIFY_DONE;
1757
Eric Holmberg878923a2012-01-10 14:28:19 -07001758 bam_dmux_log("%s: begin\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001759 in_global_reset = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001760
1761 /* Handle uplink Powerdown */
1762 write_lock_irqsave(&ul_wakeup_lock, flags);
1763 if (bam_is_connected) {
1764 ul_powerdown();
1765 wait_for_ack = 0;
1766 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001767 /*
1768 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1769 * reset to 0. harmless if bam_is_connected check above passes
1770 */
1771 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001772 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1773 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001774 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07001775 a2_pc_disabled_wakelock_skipped = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001776
1777 /* Cleanup Channel States */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001778 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1779 temp_remote_status = bam_ch_is_remote_open(i);
1780 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001781 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001782 if (bam_ch_is_local_open(i))
1783 bam_ch[i].status |= BAM_CH_IN_RESET;
1784 if (temp_remote_status) {
1785 platform_device_unregister(bam_ch[i].pdev);
1786 bam_ch[i].pdev = platform_device_alloc(
1787 bam_ch[i].name, 2);
1788 }
1789 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001790
1791 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001792 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001793 while (!list_empty(&bam_tx_pool)) {
1794 node = bam_tx_pool.next;
1795 list_del(node);
1796 info = container_of(node, struct tx_pkt_info,
1797 list_node);
1798 if (!info->is_cmd) {
1799 dma_unmap_single(NULL, info->dma_address,
1800 info->skb->len,
1801 DMA_TO_DEVICE);
1802 dev_kfree_skb_any(info->skb);
1803 } else {
1804 dma_unmap_single(NULL, info->dma_address,
1805 info->len,
1806 DMA_TO_DEVICE);
1807 kfree(info->skb);
1808 }
1809 kfree(info);
1810 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001811 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001812
Eric Holmberg878923a2012-01-10 14:28:19 -07001813 bam_dmux_log("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001814 return NOTIFY_DONE;
1815}
1816
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001817static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001818{
1819 u32 h;
1820 dma_addr_t dma_addr;
1821 int ret;
1822 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001823 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001825 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001826 /* init BAM */
1827 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1828 if (!a2_virt_addr) {
1829 pr_err("%s: ioremap failed\n", __func__);
1830 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07001831 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001832 }
1833 a2_props.phys_addr = A2_PHYS_BASE;
1834 a2_props.virt_addr = a2_virt_addr;
1835 a2_props.virt_size = A2_PHYS_SIZE;
1836 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001837 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838 a2_props.num_pipes = A2_NUM_PIPES;
1839 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001840 if (cpu_is_msm9615())
1841 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 /* need to free on tear down */
1843 ret = sps_register_bam_device(&a2_props, &h);
1844 if (ret < 0) {
1845 pr_err("%s: register bam error %d\n", __func__, ret);
1846 goto register_bam_failed;
1847 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001848 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849
1850 bam_tx_pipe = sps_alloc_endpoint();
1851 if (bam_tx_pipe == NULL) {
1852 pr_err("%s: tx alloc endpoint failed\n", __func__);
1853 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001854 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001855 }
1856 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1857 if (ret) {
1858 pr_err("%s: tx get config failed %d\n", __func__, ret);
1859 goto tx_get_config_failed;
1860 }
1861
1862 tx_connection.source = SPS_DEV_HANDLE_MEM;
1863 tx_connection.src_pipe_index = 0;
1864 tx_connection.destination = h;
1865 tx_connection.dest_pipe_index = 4;
1866 tx_connection.mode = SPS_MODE_DEST;
1867 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1868 tx_desc_mem_buf.size = 0x800; /* 2k */
1869 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1870 &dma_addr, 0);
1871 if (tx_desc_mem_buf.base == NULL) {
1872 pr_err("%s: tx memory alloc failed\n", __func__);
1873 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001874 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 }
1876 tx_desc_mem_buf.phys_base = dma_addr;
1877 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1878 tx_connection.desc = tx_desc_mem_buf;
1879 tx_connection.event_thresh = 0x10;
1880
1881 ret = sps_connect(bam_tx_pipe, &tx_connection);
1882 if (ret < 0) {
1883 pr_err("%s: tx connect error %d\n", __func__, ret);
1884 goto tx_connect_failed;
1885 }
1886
1887 bam_rx_pipe = sps_alloc_endpoint();
1888 if (bam_rx_pipe == NULL) {
1889 pr_err("%s: rx alloc endpoint failed\n", __func__);
1890 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001891 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 }
1893 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1894 if (ret) {
1895 pr_err("%s: rx get config failed %d\n", __func__, ret);
1896 goto rx_get_config_failed;
1897 }
1898
1899 rx_connection.source = h;
1900 rx_connection.src_pipe_index = 5;
1901 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1902 rx_connection.dest_pipe_index = 1;
1903 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001904 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1905 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001906 rx_desc_mem_buf.size = 0x800; /* 2k */
1907 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1908 &dma_addr, 0);
1909 if (rx_desc_mem_buf.base == NULL) {
1910 pr_err("%s: rx memory alloc failed\n", __func__);
1911 ret = -ENOMEM;
1912 goto rx_mem_failed;
1913 }
1914 rx_desc_mem_buf.phys_base = dma_addr;
1915 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1916 rx_connection.desc = rx_desc_mem_buf;
1917 rx_connection.event_thresh = 0x10;
1918
1919 ret = sps_connect(bam_rx_pipe, &rx_connection);
1920 if (ret < 0) {
1921 pr_err("%s: rx connect error %d\n", __func__, ret);
1922 goto rx_connect_failed;
1923 }
1924
1925 tx_register_event.options = SPS_O_EOT;
1926 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1927 tx_register_event.xfer_done = NULL;
1928 tx_register_event.callback = bam_mux_tx_notify;
1929 tx_register_event.user = NULL;
1930 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1931 if (ret < 0) {
1932 pr_err("%s: tx register event error %d\n", __func__, ret);
1933 goto rx_event_reg_failed;
1934 }
1935
Jeff Hugo33dbc002011-08-25 15:52:53 -06001936 rx_register_event.options = SPS_O_EOT;
1937 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1938 rx_register_event.xfer_done = NULL;
1939 rx_register_event.callback = bam_mux_rx_notify;
1940 rx_register_event.user = NULL;
1941 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1942 if (ret < 0) {
1943 pr_err("%s: tx register event error %d\n", __func__, ret);
1944 goto rx_event_reg_failed;
1945 }
1946
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947 bam_mux_initialized = 1;
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001948 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001949 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001950 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001951 complete_all(&bam_connection_completion);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001952 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953
1954rx_event_reg_failed:
1955 sps_disconnect(bam_rx_pipe);
1956rx_connect_failed:
1957 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1958 rx_desc_mem_buf.phys_base);
1959rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960rx_get_config_failed:
1961 sps_free_endpoint(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001962rx_alloc_endpoint_failed:
1963 sps_disconnect(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001964tx_connect_failed:
1965 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1966 tx_desc_mem_buf.phys_base);
1967tx_get_config_failed:
1968 sps_free_endpoint(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001969tx_alloc_endpoint_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001971 /*
1972 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
1973 * same handle below will cause a crash, so skip it if we've freed
1974 * the handle here.
1975 */
1976 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001977register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001978 if (!skip_iounmap)
1979 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07001980ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001982 return ret;
1983}
1984
1985static int bam_init_fallback(void)
1986{
1987 u32 h;
1988 int ret;
1989 void *a2_virt_addr;
1990
1991 unvote_dfab();
1992 /* init BAM */
1993 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1994 if (!a2_virt_addr) {
1995 pr_err("%s: ioremap failed\n", __func__);
1996 ret = -ENOMEM;
1997 goto ioremap_failed;
1998 }
1999 a2_props.phys_addr = A2_PHYS_BASE;
2000 a2_props.virt_addr = a2_virt_addr;
2001 a2_props.virt_size = A2_PHYS_SIZE;
2002 a2_props.irq = A2_BAM_IRQ;
2003 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2004 a2_props.num_pipes = A2_NUM_PIPES;
2005 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2006 if (cpu_is_msm9615())
2007 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
2008 ret = sps_register_bam_device(&a2_props, &h);
2009 if (ret < 0) {
2010 pr_err("%s: register bam error %d\n", __func__, ret);
2011 goto register_bam_failed;
2012 }
2013 a2_device_handle = h;
2014
2015 return 0;
2016
2017register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002018 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002019ioremap_failed:
2020 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021}
Jeff Hugoade1f842011-08-03 15:53:59 -06002022
Eric Holmberg604ab252012-01-15 00:01:18 -07002023static void msm9615_bam_init(struct work_struct *work)
2024{
2025 int ret = 0;
2026
2027 ret = bam_init();
2028 if (ret) {
2029 ret = bam_init_fallback();
2030 if (ret)
2031 pr_err("%s: bam init fallback failed: %d",
2032 __func__, ret);
2033 }
2034}
2035
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002036static void toggle_apps_ack(void)
2037{
2038 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002039
2040 bam_dmux_log("%s: apps ack %d->%d\n", __func__,
2041 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002042 smsm_change_state(SMSM_APPS_STATE,
2043 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2044 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2045 clear_bit = ~clear_bit;
2046}
2047
Jeff Hugoade1f842011-08-03 15:53:59 -06002048static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2049{
Eric Holmberg878923a2012-01-10 14:28:19 -07002050 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
2051 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
2052 new_state);
2053
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002054 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002055 bam_dmux_log("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002056 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002057 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002058 } else if (bam_mux_initialized &&
2059 !(new_state & SMSM_A2_POWER_CONTROL)) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002060 bam_dmux_log("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002061 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002062 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002063 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002064 bam_dmux_log("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002065 grab_wakelock();
Eric Holmberg604ab252012-01-15 00:01:18 -07002066 if (cpu_is_msm9615()) {
2067 /*
2068 * even though a2 has signaled it is ready via the
2069 * SMSM_A2_POWER_CONTROL bit, it has not yet
2070 * enabled the pipes as needed by sps_connect
2071 * in satallite mode. Add a short delay to give modem
2072 * time to enable the pipes.
2073 */
2074 schedule_delayed_work(&msm9615_bam_init_work,
2075 msecs_to_jiffies(100));
2076 } else {
2077 bam_init();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002078 }
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002079 } else {
Eric Holmberg878923a2012-01-10 14:28:19 -07002080 bam_dmux_log("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002081 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002082 }
Jeff Hugoade1f842011-08-03 15:53:59 -06002083
2084}
2085
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002086static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2087 uint32_t new_state)
2088{
Eric Holmberg878923a2012-01-10 14:28:19 -07002089 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
2090 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002091 complete_all(&ul_wakeup_ack_completion);
2092}
2093
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094static int bam_dmux_probe(struct platform_device *pdev)
2095{
2096 int rc;
2097
2098 DBG("%s probe called\n", __func__);
2099 if (bam_mux_initialized)
2100 return 0;
2101
Stephen Boyd1c51a492011-10-26 12:11:47 -07002102 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002103 if (IS_ERR(dfab_clk)) {
2104 pr_err("%s: did not get dfab clock\n", __func__);
2105 return -EFAULT;
2106 }
2107
2108 rc = clk_set_rate(dfab_clk, 64000000);
2109 if (rc)
2110 pr_err("%s: unable to set dfab clock rate\n", __func__);
2111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002112 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
2113 if (!bam_mux_rx_workqueue)
2114 return -ENOMEM;
2115
2116 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2117 if (!bam_mux_tx_workqueue) {
2118 destroy_workqueue(bam_mux_rx_workqueue);
2119 return -ENOMEM;
2120 }
2121
Jeff Hugo7960abd2011-08-02 15:39:38 -06002122 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002123 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002124 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2125 "bam_dmux_ch_%d", rc);
2126 /* bus 2, ie a2 stream 2 */
2127 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2128 if (!bam_ch[rc].pdev) {
2129 pr_err("%s: platform device alloc failed\n", __func__);
2130 destroy_workqueue(bam_mux_rx_workqueue);
2131 destroy_workqueue(bam_mux_tx_workqueue);
2132 return -ENOMEM;
2133 }
2134 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002135
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002136 init_completion(&ul_wakeup_ack_completion);
2137 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002138 init_completion(&dfab_unvote_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002139 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Eric Holmberg604ab252012-01-15 00:01:18 -07002140 INIT_DELAYED_WORK(&msm9615_bam_init_work, msm9615_bam_init);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002141 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002142
Jeff Hugoade1f842011-08-03 15:53:59 -06002143 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2144 bam_dmux_smsm_cb, NULL);
2145
2146 if (rc) {
2147 destroy_workqueue(bam_mux_rx_workqueue);
2148 destroy_workqueue(bam_mux_tx_workqueue);
2149 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2150 return -ENOMEM;
2151 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002152
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002153 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2154 bam_dmux_smsm_ack_cb, NULL);
2155
2156 if (rc) {
2157 destroy_workqueue(bam_mux_rx_workqueue);
2158 destroy_workqueue(bam_mux_tx_workqueue);
2159 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2160 SMSM_A2_POWER_CONTROL,
2161 bam_dmux_smsm_cb, NULL);
2162 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2163 rc);
2164 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2165 platform_device_put(bam_ch[rc].pdev);
2166 return -ENOMEM;
2167 }
2168
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002169 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2170 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 return 0;
2173}
2174
2175static struct platform_driver bam_dmux_driver = {
2176 .probe = bam_dmux_probe,
2177 .driver = {
2178 .name = "BAM_RMNT",
2179 .owner = THIS_MODULE,
2180 },
2181};
2182
2183static int __init bam_dmux_init(void)
2184{
Eric Holmberg878923a2012-01-10 14:28:19 -07002185 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186#ifdef CONFIG_DEBUG_FS
2187 struct dentry *dent;
2188
2189 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002190 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002192 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2193 debug_create("stats", 0444, dent, debug_stats);
Eric Holmberge4ac80b2012-01-12 09:21:59 -07002194 debug_create_multiple("log", 0444, dent, debug_log);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002195 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196#endif
Eric Holmberg878923a2012-01-10 14:28:19 -07002197 ret = kfifo_alloc(&bam_dmux_state_log, PAGE_SIZE, GFP_KERNEL);
2198 if (ret) {
2199 pr_err("%s: failed to allocate log %d\n", __func__, ret);
2200 bam_dmux_state_logging_disabled = 1;
2201 }
2202
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002203 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204 return platform_driver_register(&bam_dmux_driver);
2205}
2206
Jeff Hugoade1f842011-08-03 15:53:59 -06002207late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208MODULE_DESCRIPTION("MSM BAM DMUX");
2209MODULE_LICENSE("GPL v2");