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