Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 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> |
| 27 | |
| 28 | #include <mach/sps.h> |
| 29 | #include <mach/bam_dmux.h> |
Jeff Hugo | ade1f84 | 2011-08-03 15:53:59 -0600 | [diff] [blame] | 30 | #include <mach/msm_smsm.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | |
| 32 | #define BAM_CH_LOCAL_OPEN 0x1 |
| 33 | #define BAM_CH_REMOTE_OPEN 0x2 |
| 34 | |
| 35 | #define BAM_MUX_HDR_MAGIC_NO 0x33fc |
| 36 | |
| 37 | #define BAM_MUX_HDR_CMD_DATA 0 |
| 38 | #define BAM_MUX_HDR_CMD_OPEN 1 |
| 39 | #define BAM_MUX_HDR_CMD_CLOSE 2 |
| 40 | |
| 41 | #define RX_STATE_HDR_QUEUED 0 |
| 42 | #define RX_STATE_DATA_QUEUED 1 |
| 43 | |
| 44 | |
| 45 | static int msm_bam_dmux_debug_enable; |
| 46 | module_param_named(debug_enable, msm_bam_dmux_debug_enable, |
| 47 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 48 | |
| 49 | #if defined(DEBUG) |
| 50 | static uint32_t bam_dmux_read_cnt; |
| 51 | static uint32_t bam_dmux_write_cnt; |
| 52 | static uint32_t bam_dmux_write_cpy_cnt; |
| 53 | static uint32_t bam_dmux_write_cpy_bytes; |
| 54 | |
| 55 | #define DBG(x...) do { \ |
| 56 | if (msm_bam_dmux_debug_enable) \ |
| 57 | pr_debug(x); \ |
| 58 | } while (0) |
| 59 | |
| 60 | #define DBG_INC_READ_CNT(x) do { \ |
| 61 | bam_dmux_read_cnt += (x); \ |
| 62 | if (msm_bam_dmux_debug_enable) \ |
| 63 | pr_debug("%s: total read bytes %u\n", \ |
| 64 | __func__, bam_dmux_read_cnt); \ |
| 65 | } while (0) |
| 66 | |
| 67 | #define DBG_INC_WRITE_CNT(x) do { \ |
| 68 | bam_dmux_write_cnt += (x); \ |
| 69 | if (msm_bam_dmux_debug_enable) \ |
| 70 | pr_debug("%s: total written bytes %u\n", \ |
| 71 | __func__, bam_dmux_write_cnt); \ |
| 72 | } while (0) |
| 73 | |
| 74 | #define DBG_INC_WRITE_CPY(x) do { \ |
| 75 | bam_dmux_write_cpy_bytes += (x); \ |
| 76 | bam_dmux_write_cpy_cnt++; \ |
| 77 | if (msm_bam_dmux_debug_enable) \ |
| 78 | pr_debug("%s: total write copy cnt %u, bytes %u\n", \ |
| 79 | __func__, bam_dmux_write_cpy_cnt, \ |
| 80 | bam_dmux_write_cpy_bytes); \ |
| 81 | } while (0) |
| 82 | #else |
| 83 | #define DBG(x...) do { } while (0) |
| 84 | #define DBG_INC_READ_CNT(x...) do { } while (0) |
| 85 | #define DBG_INC_WRITE_CNT(x...) do { } while (0) |
| 86 | #define DBG_INC_WRITE_CPY(x...) do { } while (0) |
| 87 | #endif |
| 88 | |
| 89 | struct bam_ch_info { |
| 90 | uint32_t status; |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 91 | void (*notify)(void *, int, unsigned long); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | void *priv; |
| 93 | spinlock_t lock; |
Jeff Hugo | 7960abd | 2011-08-02 15:39:38 -0600 | [diff] [blame] | 94 | struct platform_device *pdev; |
| 95 | char name[BAM_DMUX_CH_NAME_MAX_LEN]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct tx_pkt_info { |
| 99 | struct sk_buff *skb; |
| 100 | dma_addr_t dma_address; |
| 101 | char is_cmd; |
| 102 | uint32_t len; |
| 103 | struct work_struct work; |
| 104 | }; |
| 105 | |
| 106 | struct rx_pkt_info { |
| 107 | struct sk_buff *skb; |
| 108 | dma_addr_t dma_address; |
| 109 | struct work_struct work; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | #define A2_NUM_PIPES 6 |
| 113 | #define A2_SUMMING_THRESHOLD 4096 |
| 114 | #define A2_DEFAULT_DESCRIPTORS 32 |
| 115 | #define A2_PHYS_BASE 0x124C2000 |
| 116 | #define A2_PHYS_SIZE 0x2000 |
| 117 | #define BUFFER_SIZE 2048 |
| 118 | #define NUM_BUFFERS 32 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 119 | static struct sps_bam_props a2_props; |
| 120 | static struct sps_pipe *bam_tx_pipe; |
| 121 | static struct sps_pipe *bam_rx_pipe; |
| 122 | static struct sps_connect tx_connection; |
| 123 | static struct sps_connect rx_connection; |
| 124 | static struct sps_mem_buffer tx_desc_mem_buf; |
| 125 | static struct sps_mem_buffer rx_desc_mem_buf; |
| 126 | static struct sps_register_event tx_register_event; |
Jeff Hugo | 33dbc00 | 2011-08-25 15:52:53 -0600 | [diff] [blame^] | 127 | static struct sps_register_event rx_register_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | |
| 129 | static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS]; |
| 130 | static int bam_mux_initialized; |
| 131 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 132 | struct bam_mux_hdr { |
| 133 | uint16_t magic_num; |
| 134 | uint8_t reserved; |
| 135 | uint8_t cmd; |
| 136 | uint8_t pad_len; |
| 137 | uint8_t ch_id; |
| 138 | uint16_t pkt_len; |
| 139 | }; |
| 140 | |
| 141 | static void bam_mux_write_done(struct work_struct *work); |
| 142 | static void handle_bam_mux_cmd(struct work_struct *work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | |
| 144 | static DEFINE_MUTEX(bam_mux_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | |
| 146 | static struct workqueue_struct *bam_mux_rx_workqueue; |
| 147 | static struct workqueue_struct *bam_mux_tx_workqueue; |
| 148 | |
| 149 | #define bam_ch_is_open(x) \ |
| 150 | (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN)) |
| 151 | |
| 152 | #define bam_ch_is_local_open(x) \ |
| 153 | (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN) |
| 154 | |
| 155 | #define bam_ch_is_remote_open(x) \ |
| 156 | (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN) |
| 157 | |
| 158 | static void queue_rx(void) |
| 159 | { |
| 160 | void *ptr; |
| 161 | struct rx_pkt_info *info; |
| 162 | |
| 163 | info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL); |
| 164 | if (!info) |
| 165 | return; /*need better way to handle this */ |
| 166 | |
| 167 | INIT_WORK(&info->work, handle_bam_mux_cmd); |
| 168 | |
| 169 | info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL); |
| 170 | ptr = skb_put(info->skb, BUFFER_SIZE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | /* need a way to handle error case */ |
| 172 | info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE, |
| 173 | DMA_FROM_DEVICE); |
| 174 | sps_transfer_one(bam_rx_pipe, info->dma_address, |
Jeff Hugo | 33dbc00 | 2011-08-25 15:52:53 -0600 | [diff] [blame^] | 175 | BUFFER_SIZE, info, |
| 176 | SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static void bam_mux_process_data(struct sk_buff *rx_skb) |
| 180 | { |
| 181 | unsigned long flags; |
| 182 | struct bam_mux_hdr *rx_hdr; |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 183 | unsigned long event_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 184 | |
| 185 | rx_hdr = (struct bam_mux_hdr *)rx_skb->data; |
| 186 | |
| 187 | rx_skb->data = (unsigned char *)(rx_hdr + 1); |
| 188 | rx_skb->tail = rx_skb->data + rx_hdr->pkt_len; |
| 189 | rx_skb->len = rx_hdr->pkt_len; |
| 190 | |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 191 | event_data = (unsigned long)(rx_skb); |
| 192 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 193 | spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags); |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 194 | if (bam_ch[rx_hdr->ch_id].notify) |
| 195 | bam_ch[rx_hdr->ch_id].notify( |
| 196 | bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE, |
| 197 | event_data); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | else |
| 199 | dev_kfree_skb_any(rx_skb); |
| 200 | spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags); |
| 201 | |
| 202 | queue_rx(); |
| 203 | } |
| 204 | |
| 205 | static void handle_bam_mux_cmd(struct work_struct *work) |
| 206 | { |
| 207 | unsigned long flags; |
| 208 | struct bam_mux_hdr *rx_hdr; |
| 209 | struct rx_pkt_info *info; |
| 210 | struct sk_buff *rx_skb; |
Jeff Hugo | 7960abd | 2011-08-02 15:39:38 -0600 | [diff] [blame] | 211 | int ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 212 | |
| 213 | info = container_of(work, struct rx_pkt_info, work); |
| 214 | rx_skb = info->skb; |
| 215 | kfree(info); |
| 216 | |
| 217 | rx_hdr = (struct bam_mux_hdr *)rx_skb->data; |
| 218 | |
| 219 | DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr)); |
| 220 | DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__, |
| 221 | rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd, |
| 222 | rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len); |
| 223 | if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) { |
| 224 | pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d" |
| 225 | " pad %d ch %d len %d\n", __func__, |
| 226 | rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd, |
| 227 | rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len); |
| 228 | dev_kfree_skb_any(rx_skb); |
| 229 | queue_rx(); |
| 230 | return; |
| 231 | } |
| 232 | switch (rx_hdr->cmd) { |
| 233 | case BAM_MUX_HDR_CMD_DATA: |
| 234 | DBG_INC_READ_CNT(rx_hdr->pkt_len); |
| 235 | bam_mux_process_data(rx_skb); |
| 236 | break; |
| 237 | case BAM_MUX_HDR_CMD_OPEN: |
| 238 | spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags); |
| 239 | bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN; |
| 240 | spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags); |
| 241 | dev_kfree_skb_any(rx_skb); |
| 242 | queue_rx(); |
Jeff Hugo | 7960abd | 2011-08-02 15:39:38 -0600 | [diff] [blame] | 243 | ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev); |
| 244 | if (ret) |
| 245 | pr_err("%s: platform_device_add() error: %d\n", |
| 246 | __func__, ret); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 247 | break; |
| 248 | case BAM_MUX_HDR_CMD_CLOSE: |
| 249 | /* probably should drop pending write */ |
| 250 | spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags); |
| 251 | bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN; |
| 252 | spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags); |
| 253 | dev_kfree_skb_any(rx_skb); |
| 254 | queue_rx(); |
Jeff Hugo | 7960abd | 2011-08-02 15:39:38 -0600 | [diff] [blame] | 255 | platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev); |
| 256 | bam_ch[rx_hdr->ch_id].pdev = |
| 257 | platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2); |
| 258 | if (!bam_ch[rx_hdr->ch_id].pdev) |
| 259 | pr_err("%s: platform_device_alloc failed\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 260 | break; |
| 261 | default: |
| 262 | pr_err("%s: dropping invalid hdr. magic %x reserved %d cmd %d" |
| 263 | " pad %d ch %d len %d\n", __func__, |
| 264 | rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd, |
| 265 | rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len); |
| 266 | dev_kfree_skb_any(rx_skb); |
| 267 | queue_rx(); |
| 268 | return; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static int bam_mux_write_cmd(void *data, uint32_t len) |
| 273 | { |
| 274 | int rc; |
| 275 | struct tx_pkt_info *pkt; |
| 276 | dma_addr_t dma_address; |
| 277 | |
| 278 | mutex_lock(&bam_mux_lock); |
| 279 | pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_KERNEL); |
| 280 | if (pkt == NULL) { |
| 281 | pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__); |
| 282 | rc = -ENOMEM; |
| 283 | mutex_unlock(&bam_mux_lock); |
| 284 | return rc; |
| 285 | } |
| 286 | |
| 287 | dma_address = dma_map_single(NULL, data, len, |
| 288 | DMA_TO_DEVICE); |
| 289 | if (!dma_address) { |
| 290 | pr_err("%s: dma_map_single() failed\n", __func__); |
| 291 | rc = -ENOMEM; |
| 292 | mutex_unlock(&bam_mux_lock); |
| 293 | return rc; |
| 294 | } |
| 295 | pkt->skb = (struct sk_buff *)(data); |
| 296 | pkt->len = len; |
| 297 | pkt->dma_address = dma_address; |
| 298 | pkt->is_cmd = 1; |
| 299 | rc = sps_transfer_one(bam_tx_pipe, dma_address, len, |
| 300 | pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT); |
| 301 | |
| 302 | mutex_unlock(&bam_mux_lock); |
| 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | static void bam_mux_write_done(struct work_struct *work) |
| 307 | { |
| 308 | struct sk_buff *skb; |
| 309 | struct bam_mux_hdr *hdr; |
| 310 | struct tx_pkt_info *info; |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 311 | unsigned long event_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 312 | |
| 313 | info = container_of(work, struct tx_pkt_info, work); |
| 314 | skb = info->skb; |
| 315 | kfree(info); |
| 316 | hdr = (struct bam_mux_hdr *)skb->data; |
| 317 | DBG_INC_WRITE_CNT(skb->data_len); |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 318 | event_data = (unsigned long)(skb); |
| 319 | if (bam_ch[hdr->ch_id].notify) |
| 320 | bam_ch[hdr->ch_id].notify( |
| 321 | bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE, |
| 322 | event_data); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 323 | else |
| 324 | dev_kfree_skb_any(skb); |
| 325 | } |
| 326 | |
| 327 | int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb) |
| 328 | { |
| 329 | int rc = 0; |
| 330 | struct bam_mux_hdr *hdr; |
| 331 | unsigned long flags; |
| 332 | struct sk_buff *new_skb = NULL; |
| 333 | dma_addr_t dma_address; |
| 334 | struct tx_pkt_info *pkt; |
| 335 | |
| 336 | if (id >= BAM_DMUX_NUM_CHANNELS) |
| 337 | return -EINVAL; |
| 338 | if (!skb) |
| 339 | return -EINVAL; |
| 340 | if (!bam_mux_initialized) |
| 341 | return -ENODEV; |
| 342 | |
| 343 | DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len); |
| 344 | spin_lock_irqsave(&bam_ch[id].lock, flags); |
| 345 | if (!bam_ch_is_open(id)) { |
| 346 | spin_unlock_irqrestore(&bam_ch[id].lock, flags); |
| 347 | pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status); |
| 348 | return -ENODEV; |
| 349 | } |
| 350 | spin_unlock_irqrestore(&bam_ch[id].lock, flags); |
| 351 | |
| 352 | /* if skb do not have any tailroom for padding, |
| 353 | copy the skb into a new expanded skb */ |
| 354 | if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) { |
| 355 | /* revisit, probably dev_alloc_skb and memcpy is effecient */ |
| 356 | new_skb = skb_copy_expand(skb, skb_headroom(skb), |
| 357 | 4 - (skb->len & 0x3), GFP_ATOMIC); |
| 358 | if (new_skb == NULL) { |
| 359 | pr_err("%s: cannot allocate skb\n", __func__); |
| 360 | return -ENOMEM; |
| 361 | } |
| 362 | dev_kfree_skb_any(skb); |
| 363 | skb = new_skb; |
| 364 | DBG_INC_WRITE_CPY(skb->len); |
| 365 | } |
| 366 | |
| 367 | hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr)); |
| 368 | |
| 369 | /* caller should allocate for hdr and padding |
| 370 | hdr is fine, padding is tricky */ |
| 371 | hdr->magic_num = BAM_MUX_HDR_MAGIC_NO; |
| 372 | hdr->cmd = BAM_MUX_HDR_CMD_DATA; |
| 373 | hdr->reserved = 0; |
| 374 | hdr->ch_id = id; |
| 375 | hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr); |
| 376 | if (skb->len & 0x3) |
| 377 | skb_put(skb, 4 - (skb->len & 0x3)); |
| 378 | |
| 379 | hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len); |
| 380 | |
| 381 | DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n", |
| 382 | __func__, skb->data, skb->tail, skb->len, |
| 383 | hdr->pkt_len, hdr->pad_len); |
| 384 | |
| 385 | pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC); |
| 386 | if (pkt == NULL) { |
| 387 | pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__); |
| 388 | if (new_skb) |
| 389 | dev_kfree_skb_any(new_skb); |
| 390 | return -ENOMEM; |
| 391 | } |
| 392 | |
| 393 | dma_address = dma_map_single(NULL, skb->data, skb->len, |
| 394 | DMA_TO_DEVICE); |
| 395 | if (!dma_address) { |
| 396 | pr_err("%s: dma_map_single() failed\n", __func__); |
| 397 | if (new_skb) |
| 398 | dev_kfree_skb_any(new_skb); |
| 399 | kfree(pkt); |
| 400 | return -ENOMEM; |
| 401 | } |
| 402 | pkt->skb = skb; |
| 403 | pkt->dma_address = dma_address; |
| 404 | pkt->is_cmd = 0; |
| 405 | INIT_WORK(&pkt->work, bam_mux_write_done); |
| 406 | rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len, |
| 407 | pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT); |
| 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | int msm_bam_dmux_open(uint32_t id, void *priv, |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 412 | void (*notify)(void *, int, unsigned long)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | { |
| 414 | struct bam_mux_hdr *hdr; |
| 415 | unsigned long flags; |
| 416 | int rc = 0; |
| 417 | |
| 418 | DBG("%s: opening ch %d\n", __func__, id); |
| 419 | if (!bam_mux_initialized) |
| 420 | return -ENODEV; |
| 421 | if (id >= BAM_DMUX_NUM_CHANNELS) |
| 422 | return -EINVAL; |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 423 | if (notify == NULL) |
| 424 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 425 | |
| 426 | hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL); |
| 427 | if (hdr == NULL) { |
| 428 | pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id); |
| 429 | return -ENOMEM; |
| 430 | } |
| 431 | spin_lock_irqsave(&bam_ch[id].lock, flags); |
| 432 | if (bam_ch_is_open(id)) { |
| 433 | DBG("%s: Already opened %d\n", __func__, id); |
| 434 | spin_unlock_irqrestore(&bam_ch[id].lock, flags); |
| 435 | kfree(hdr); |
| 436 | goto open_done; |
| 437 | } |
| 438 | if (!bam_ch_is_remote_open(id)) { |
| 439 | DBG("%s: Remote not open; ch: %d\n", __func__, id); |
| 440 | spin_unlock_irqrestore(&bam_ch[id].lock, flags); |
| 441 | kfree(hdr); |
| 442 | rc = -ENODEV; |
| 443 | goto open_done; |
| 444 | } |
| 445 | |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 446 | bam_ch[id].notify = notify; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 447 | bam_ch[id].priv = priv; |
| 448 | bam_ch[id].status |= BAM_CH_LOCAL_OPEN; |
| 449 | spin_unlock_irqrestore(&bam_ch[id].lock, flags); |
| 450 | |
| 451 | hdr->magic_num = BAM_MUX_HDR_MAGIC_NO; |
| 452 | hdr->cmd = BAM_MUX_HDR_CMD_OPEN; |
| 453 | hdr->reserved = 0; |
| 454 | hdr->ch_id = id; |
| 455 | hdr->pkt_len = 0; |
| 456 | hdr->pad_len = 0; |
| 457 | |
| 458 | rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr)); |
| 459 | |
| 460 | open_done: |
| 461 | DBG("%s: opened ch %d\n", __func__, id); |
| 462 | return rc; |
| 463 | } |
| 464 | |
| 465 | int msm_bam_dmux_close(uint32_t id) |
| 466 | { |
| 467 | struct bam_mux_hdr *hdr; |
| 468 | unsigned long flags; |
| 469 | int rc; |
| 470 | |
| 471 | if (id >= BAM_DMUX_NUM_CHANNELS) |
| 472 | return -EINVAL; |
| 473 | DBG("%s: closing ch %d\n", __func__, id); |
| 474 | if (!bam_mux_initialized) |
| 475 | return -ENODEV; |
| 476 | spin_lock_irqsave(&bam_ch[id].lock, flags); |
| 477 | |
Jeff Hugo | 1c4531c | 2011-08-02 14:55:37 -0600 | [diff] [blame] | 478 | bam_ch[id].notify = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 479 | bam_ch[id].priv = NULL; |
| 480 | bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN; |
| 481 | spin_unlock_irqrestore(&bam_ch[id].lock, flags); |
| 482 | |
| 483 | hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL); |
| 484 | if (hdr == NULL) { |
| 485 | pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id); |
| 486 | return -ENOMEM; |
| 487 | } |
| 488 | hdr->magic_num = BAM_MUX_HDR_MAGIC_NO; |
| 489 | hdr->cmd = BAM_MUX_HDR_CMD_CLOSE; |
| 490 | hdr->reserved = 0; |
| 491 | hdr->ch_id = id; |
| 492 | hdr->pkt_len = 0; |
| 493 | hdr->pad_len = 0; |
| 494 | |
| 495 | rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr)); |
| 496 | |
| 497 | DBG("%s: closed ch %d\n", __func__, id); |
| 498 | return rc; |
| 499 | } |
| 500 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 501 | static void bam_mux_tx_notify(struct sps_event_notify *notify) |
| 502 | { |
| 503 | struct tx_pkt_info *pkt; |
| 504 | |
| 505 | DBG("%s: event %d notified\n", __func__, notify->event_id); |
| 506 | |
| 507 | switch (notify->event_id) { |
| 508 | case SPS_EVENT_EOT: |
| 509 | pkt = notify->data.transfer.user; |
| 510 | if (!pkt->is_cmd) { |
| 511 | dma_unmap_single(NULL, pkt->dma_address, |
| 512 | pkt->skb->len, |
| 513 | DMA_TO_DEVICE); |
| 514 | queue_work(bam_mux_tx_workqueue, &pkt->work); |
| 515 | } else { |
| 516 | dma_unmap_single(NULL, pkt->dma_address, |
| 517 | pkt->len, |
| 518 | DMA_TO_DEVICE); |
| 519 | kfree(pkt->skb); |
| 520 | kfree(pkt); |
| 521 | } |
| 522 | break; |
| 523 | default: |
| 524 | pr_err("%s: recieved unexpected event id %d\n", __func__, |
| 525 | notify->event_id); |
| 526 | } |
| 527 | } |
| 528 | |
Jeff Hugo | 33dbc00 | 2011-08-25 15:52:53 -0600 | [diff] [blame^] | 529 | static void bam_mux_rx_notify(struct sps_event_notify *notify) |
| 530 | { |
| 531 | struct rx_pkt_info *info; |
| 532 | |
| 533 | DBG("%s: event %d notified\n", __func__, notify->event_id); |
| 534 | |
| 535 | info = (struct rx_pkt_info *)(notify->data.transfer.user); |
| 536 | |
| 537 | switch (notify->event_id) { |
| 538 | case SPS_EVENT_EOT: |
| 539 | dma_unmap_single(NULL, info->dma_address, |
| 540 | BUFFER_SIZE, DMA_FROM_DEVICE); |
| 541 | queue_work(bam_mux_rx_workqueue, &info->work); |
| 542 | break; |
| 543 | default: |
| 544 | pr_err("%s: recieved unexpected event id %d\n", __func__, |
| 545 | notify->event_id); |
| 546 | } |
| 547 | } |
| 548 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 549 | #ifdef CONFIG_DEBUG_FS |
| 550 | |
| 551 | static int debug_tbl(char *buf, int max) |
| 552 | { |
| 553 | int i = 0; |
| 554 | int j; |
| 555 | |
| 556 | for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) { |
| 557 | i += scnprintf(buf + i, max - i, |
| 558 | "ch%02d local open=%s remote open=%s\n", |
| 559 | j, bam_ch_is_local_open(j) ? "Y" : "N", |
| 560 | bam_ch_is_remote_open(j) ? "Y" : "N"); |
| 561 | } |
| 562 | |
| 563 | return i; |
| 564 | } |
| 565 | |
| 566 | #define DEBUG_BUFMAX 4096 |
| 567 | static char debug_buffer[DEBUG_BUFMAX]; |
| 568 | |
| 569 | static ssize_t debug_read(struct file *file, char __user *buf, |
| 570 | size_t count, loff_t *ppos) |
| 571 | { |
| 572 | int (*fill)(char *buf, int max) = file->private_data; |
| 573 | int bsize = fill(debug_buffer, DEBUG_BUFMAX); |
| 574 | return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize); |
| 575 | } |
| 576 | |
| 577 | static int debug_open(struct inode *inode, struct file *file) |
| 578 | { |
| 579 | file->private_data = inode->i_private; |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | |
| 584 | static const struct file_operations debug_ops = { |
| 585 | .read = debug_read, |
| 586 | .open = debug_open, |
| 587 | }; |
| 588 | |
| 589 | static void debug_create(const char *name, mode_t mode, |
| 590 | struct dentry *dent, |
| 591 | int (*fill)(char *buf, int max)) |
| 592 | { |
| 593 | debugfs_create_file(name, mode, dent, fill, &debug_ops); |
| 594 | } |
| 595 | |
| 596 | #endif |
| 597 | |
Jeff Hugo | ade1f84 | 2011-08-03 15:53:59 -0600 | [diff] [blame] | 598 | static void bam_init(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 599 | { |
| 600 | u32 h; |
| 601 | dma_addr_t dma_addr; |
| 602 | int ret; |
| 603 | void *a2_virt_addr; |
| 604 | int i; |
| 605 | |
| 606 | /* init BAM */ |
| 607 | a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE); |
| 608 | if (!a2_virt_addr) { |
| 609 | pr_err("%s: ioremap failed\n", __func__); |
| 610 | ret = -ENOMEM; |
| 611 | goto register_bam_failed; |
| 612 | } |
| 613 | a2_props.phys_addr = A2_PHYS_BASE; |
| 614 | a2_props.virt_addr = a2_virt_addr; |
| 615 | a2_props.virt_size = A2_PHYS_SIZE; |
| 616 | a2_props.irq = A2_BAM_IRQ; |
| 617 | a2_props.num_pipes = A2_NUM_PIPES; |
| 618 | a2_props.summing_threshold = A2_SUMMING_THRESHOLD; |
| 619 | /* need to free on tear down */ |
| 620 | ret = sps_register_bam_device(&a2_props, &h); |
| 621 | if (ret < 0) { |
| 622 | pr_err("%s: register bam error %d\n", __func__, ret); |
| 623 | goto register_bam_failed; |
| 624 | } |
| 625 | |
| 626 | bam_tx_pipe = sps_alloc_endpoint(); |
| 627 | if (bam_tx_pipe == NULL) { |
| 628 | pr_err("%s: tx alloc endpoint failed\n", __func__); |
| 629 | ret = -ENOMEM; |
| 630 | goto register_bam_failed; |
| 631 | } |
| 632 | ret = sps_get_config(bam_tx_pipe, &tx_connection); |
| 633 | if (ret) { |
| 634 | pr_err("%s: tx get config failed %d\n", __func__, ret); |
| 635 | goto tx_get_config_failed; |
| 636 | } |
| 637 | |
| 638 | tx_connection.source = SPS_DEV_HANDLE_MEM; |
| 639 | tx_connection.src_pipe_index = 0; |
| 640 | tx_connection.destination = h; |
| 641 | tx_connection.dest_pipe_index = 4; |
| 642 | tx_connection.mode = SPS_MODE_DEST; |
| 643 | tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT; |
| 644 | tx_desc_mem_buf.size = 0x800; /* 2k */ |
| 645 | tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size, |
| 646 | &dma_addr, 0); |
| 647 | if (tx_desc_mem_buf.base == NULL) { |
| 648 | pr_err("%s: tx memory alloc failed\n", __func__); |
| 649 | ret = -ENOMEM; |
| 650 | goto tx_mem_failed; |
| 651 | } |
| 652 | tx_desc_mem_buf.phys_base = dma_addr; |
| 653 | memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size); |
| 654 | tx_connection.desc = tx_desc_mem_buf; |
| 655 | tx_connection.event_thresh = 0x10; |
| 656 | |
| 657 | ret = sps_connect(bam_tx_pipe, &tx_connection); |
| 658 | if (ret < 0) { |
| 659 | pr_err("%s: tx connect error %d\n", __func__, ret); |
| 660 | goto tx_connect_failed; |
| 661 | } |
| 662 | |
| 663 | bam_rx_pipe = sps_alloc_endpoint(); |
| 664 | if (bam_rx_pipe == NULL) { |
| 665 | pr_err("%s: rx alloc endpoint failed\n", __func__); |
| 666 | ret = -ENOMEM; |
| 667 | goto tx_connect_failed; |
| 668 | } |
| 669 | ret = sps_get_config(bam_rx_pipe, &rx_connection); |
| 670 | if (ret) { |
| 671 | pr_err("%s: rx get config failed %d\n", __func__, ret); |
| 672 | goto rx_get_config_failed; |
| 673 | } |
| 674 | |
| 675 | rx_connection.source = h; |
| 676 | rx_connection.src_pipe_index = 5; |
| 677 | rx_connection.destination = SPS_DEV_HANDLE_MEM; |
| 678 | rx_connection.dest_pipe_index = 1; |
| 679 | rx_connection.mode = SPS_MODE_SRC; |
Jeff Hugo | 33dbc00 | 2011-08-25 15:52:53 -0600 | [diff] [blame^] | 680 | rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 681 | rx_desc_mem_buf.size = 0x800; /* 2k */ |
| 682 | rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size, |
| 683 | &dma_addr, 0); |
| 684 | if (rx_desc_mem_buf.base == NULL) { |
| 685 | pr_err("%s: rx memory alloc failed\n", __func__); |
| 686 | ret = -ENOMEM; |
| 687 | goto rx_mem_failed; |
| 688 | } |
| 689 | rx_desc_mem_buf.phys_base = dma_addr; |
| 690 | memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size); |
| 691 | rx_connection.desc = rx_desc_mem_buf; |
| 692 | rx_connection.event_thresh = 0x10; |
| 693 | |
| 694 | ret = sps_connect(bam_rx_pipe, &rx_connection); |
| 695 | if (ret < 0) { |
| 696 | pr_err("%s: rx connect error %d\n", __func__, ret); |
| 697 | goto rx_connect_failed; |
| 698 | } |
| 699 | |
| 700 | tx_register_event.options = SPS_O_EOT; |
| 701 | tx_register_event.mode = SPS_TRIGGER_CALLBACK; |
| 702 | tx_register_event.xfer_done = NULL; |
| 703 | tx_register_event.callback = bam_mux_tx_notify; |
| 704 | tx_register_event.user = NULL; |
| 705 | ret = sps_register_event(bam_tx_pipe, &tx_register_event); |
| 706 | if (ret < 0) { |
| 707 | pr_err("%s: tx register event error %d\n", __func__, ret); |
| 708 | goto rx_event_reg_failed; |
| 709 | } |
| 710 | |
Jeff Hugo | 33dbc00 | 2011-08-25 15:52:53 -0600 | [diff] [blame^] | 711 | rx_register_event.options = SPS_O_EOT; |
| 712 | rx_register_event.mode = SPS_TRIGGER_CALLBACK; |
| 713 | rx_register_event.xfer_done = NULL; |
| 714 | rx_register_event.callback = bam_mux_rx_notify; |
| 715 | rx_register_event.user = NULL; |
| 716 | ret = sps_register_event(bam_rx_pipe, &rx_register_event); |
| 717 | if (ret < 0) { |
| 718 | pr_err("%s: tx register event error %d\n", __func__, ret); |
| 719 | goto rx_event_reg_failed; |
| 720 | } |
| 721 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 722 | bam_mux_initialized = 1; |
| 723 | for (i = 0; i < NUM_BUFFERS; ++i) |
| 724 | queue_rx(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 725 | return; |
| 726 | |
| 727 | rx_event_reg_failed: |
| 728 | sps_disconnect(bam_rx_pipe); |
| 729 | rx_connect_failed: |
| 730 | dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base, |
| 731 | rx_desc_mem_buf.phys_base); |
| 732 | rx_mem_failed: |
| 733 | sps_disconnect(bam_tx_pipe); |
| 734 | rx_get_config_failed: |
| 735 | sps_free_endpoint(bam_rx_pipe); |
| 736 | tx_connect_failed: |
| 737 | dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base, |
| 738 | tx_desc_mem_buf.phys_base); |
| 739 | tx_get_config_failed: |
| 740 | sps_free_endpoint(bam_tx_pipe); |
| 741 | tx_mem_failed: |
| 742 | sps_deregister_bam_device(h); |
| 743 | register_bam_failed: |
| 744 | /*destroy_workqueue(bam_mux_workqueue);*/ |
| 745 | /*return ret;*/ |
| 746 | return; |
| 747 | } |
Jeff Hugo | ade1f84 | 2011-08-03 15:53:59 -0600 | [diff] [blame] | 748 | |
| 749 | static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state) |
| 750 | { |
| 751 | DBG("%s: smsm activity\n", __func__); |
| 752 | if (bam_mux_initialized) |
| 753 | pr_err("%s: bam_dmux already initialized\n", __func__); |
| 754 | else if (new_state & SMSM_A2_POWER_CONTROL) |
| 755 | bam_init(); |
| 756 | else |
| 757 | pr_err("%s: unsupported state change\n", __func__); |
| 758 | |
| 759 | } |
| 760 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 761 | static int bam_dmux_probe(struct platform_device *pdev) |
| 762 | { |
| 763 | int rc; |
| 764 | |
| 765 | DBG("%s probe called\n", __func__); |
| 766 | if (bam_mux_initialized) |
| 767 | return 0; |
| 768 | |
| 769 | bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx"); |
| 770 | if (!bam_mux_rx_workqueue) |
| 771 | return -ENOMEM; |
| 772 | |
| 773 | bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx"); |
| 774 | if (!bam_mux_tx_workqueue) { |
| 775 | destroy_workqueue(bam_mux_rx_workqueue); |
| 776 | return -ENOMEM; |
| 777 | } |
| 778 | |
Jeff Hugo | 7960abd | 2011-08-02 15:39:38 -0600 | [diff] [blame] | 779 | for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 780 | spin_lock_init(&bam_ch[rc].lock); |
Jeff Hugo | 7960abd | 2011-08-02 15:39:38 -0600 | [diff] [blame] | 781 | scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN, |
| 782 | "bam_dmux_ch_%d", rc); |
| 783 | /* bus 2, ie a2 stream 2 */ |
| 784 | bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2); |
| 785 | if (!bam_ch[rc].pdev) { |
| 786 | pr_err("%s: platform device alloc failed\n", __func__); |
| 787 | destroy_workqueue(bam_mux_rx_workqueue); |
| 788 | destroy_workqueue(bam_mux_tx_workqueue); |
| 789 | return -ENOMEM; |
| 790 | } |
| 791 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 792 | |
Jeff Hugo | ade1f84 | 2011-08-03 15:53:59 -0600 | [diff] [blame] | 793 | rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL, |
| 794 | bam_dmux_smsm_cb, NULL); |
| 795 | |
| 796 | if (rc) { |
| 797 | destroy_workqueue(bam_mux_rx_workqueue); |
| 798 | destroy_workqueue(bam_mux_tx_workqueue); |
| 799 | pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc); |
| 800 | return -ENOMEM; |
| 801 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static struct platform_driver bam_dmux_driver = { |
| 807 | .probe = bam_dmux_probe, |
| 808 | .driver = { |
| 809 | .name = "BAM_RMNT", |
| 810 | .owner = THIS_MODULE, |
| 811 | }, |
| 812 | }; |
| 813 | |
| 814 | static int __init bam_dmux_init(void) |
| 815 | { |
| 816 | #ifdef CONFIG_DEBUG_FS |
| 817 | struct dentry *dent; |
| 818 | |
| 819 | dent = debugfs_create_dir("bam_dmux", 0); |
| 820 | if (!IS_ERR(dent)) |
| 821 | debug_create("tbl", 0444, dent, debug_tbl); |
| 822 | #endif |
| 823 | return platform_driver_register(&bam_dmux_driver); |
| 824 | } |
| 825 | |
Jeff Hugo | ade1f84 | 2011-08-03 15:53:59 -0600 | [diff] [blame] | 826 | late_initcall(bam_dmux_init); /* needs to init after SMD */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 827 | MODULE_DESCRIPTION("MSM BAM DMUX"); |
| 828 | MODULE_LICENSE("GPL v2"); |