Skip to content

Commit

Permalink
dma: use user data in callback
Browse files Browse the repository at this point in the history
Use user data to replace DMA's device pointer in
the callback function so that the user can retrieve
its context by that private data.

Signed-off-by: Jun Li <jun.r.li@intel.com>
  • Loading branch information
jli157 authored and nashif committed Oct 16, 2018
1 parent e092fe2 commit 4982fa9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 15 deletions.
6 changes: 4 additions & 2 deletions drivers/dma/dma_cavs.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static void dw_dma_isr(void *arg)
* freed in the user callback function once
* all the blocks are transferred.
*/
chan_data->dma_blkcallback(dev, channel, 0);
chan_data->dma_blkcallback(chan_data->blkcallback_arg, channel, 0);
}
}

Expand All @@ -108,7 +108,7 @@ static void dw_dma_isr(void *arg)
k_free(chan_data->lli);
chan_data->lli = NULL;
if (chan_data->dma_tfrcallback) {
chan_data->dma_tfrcallback(dev, channel, 0);
chan_data->dma_tfrcallback(chan_data->tfrcallback_arg, channel, 0);
}
}
}
Expand Down Expand Up @@ -262,8 +262,10 @@ static int dw_dma_config(struct device *dev, u32_t channel,
*/
if (cfg->complete_callback_en) {
chan_data->dma_blkcallback = cfg->dma_callback;
chan_data->blkcallback_arg = cfg->callback_arg;
} else {
chan_data->dma_tfrcallback = cfg->dma_callback;
chan_data->tfrcallback_arg = cfg->callback_arg;
}

return 0;
Expand Down
6 changes: 4 additions & 2 deletions drivers/dma/dma_cavs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ struct dma_chan_data {
struct dw_lli2 *lli;
u32_t cfg_lo;
u32_t cfg_hi;
void (*dma_blkcallback)(struct device *dev, u32_t channel,
void *blkcallback_arg;
void (*dma_blkcallback)(void *arg, u32_t channel,
int error_code);
void (*dma_tfrcallback)(struct device *dev, u32_t channel,
void *tfrcallback_arg;
void (*dma_tfrcallback)(void *arg, u32_t channel,
int error_code);
};

Expand Down
6 changes: 4 additions & 2 deletions drivers/dma/dma_nios2_msgdma.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct nios2_msgdma_dev_cfg {
alt_msgdma_standard_descriptor desc;
u32_t direction;
struct k_sem sem_lock;
void (*dma_callback)(struct device *dev, u32_t id,
void *callback_arg;
void (*dma_callback)(void *arg, u32_t id,
int error_code);
};

Expand Down Expand Up @@ -61,7 +62,7 @@ static void nios2_msgdma_callback(void *context)

LOG_DBG("msgdma csr status Reg: 0x%x", status);

dev_cfg->dma_callback((struct device *)context, 0, err_code);
dev_cfg->dma_callback(dev_cfg->callback_arg, 0, err_code);
}

static int nios2_msgdma_config(struct device *dev, u32_t channel,
Expand Down Expand Up @@ -103,6 +104,7 @@ static int nios2_msgdma_config(struct device *dev, u32_t channel,

k_sem_take(&dev_cfg->sem_lock, K_FOREVER);
dev_cfg->dma_callback = cfg->dma_callback;
dev_cfg->callback_arg = cfg->callback_arg;
dev_cfg->direction = cfg->channel_direction;
dma_block = cfg->head_block;
control = ALTERA_MSGDMA_DESCRIPTOR_CONTROL_TRANSFER_COMPLETE_IRQ_MASK |
Expand Down
6 changes: 4 additions & 2 deletions drivers/dma/dma_qmsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct dma_qmsi_driver_data {
u32_t device_power_state;
qm_dma_context_t saved_ctx;
#endif
void (*dma_user_callback[QM_DMA_CHANNEL_NUM])(struct device *dev,
void (*dma_user_callback[QM_DMA_CHANNEL_NUM])(void *arg,
u32_t channel_id,
int error_code);
};
Expand All @@ -60,7 +60,8 @@ static void dma_drv_callback(void *callback_context, u32_t len,
channel = context->index;
data = context->dev->driver_data;

data->dma_user_callback[channel](context->dev, channel, error_code);
data->dma_user_callback[channel](data->callback_data[channel],
channel, error_code);
}

static int width_index(u32_t num_bytes, u32_t *index)
Expand Down Expand Up @@ -171,6 +172,7 @@ static int dma_qmsi_chan_config(struct device *dev, u32_t channel,
/* TODO: add support for using other DMA transfer types. */
qmsi_cfg.transfer_type = QM_DMA_TYPE_SINGLE;

data->callback_data[channel] = config->callback_arg;
data->dma_user_callback[channel] = config->dma_callback;

dma_context[channel].index = channel;
Expand Down
5 changes: 4 additions & 1 deletion drivers/dma/dma_sam_xdmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ LOG_MODULE_REGISTER(dma_sam_xdmac)

/* DMA channel configuration */
struct sam_xdmac_channel_cfg {
void *callback_arg;
dma_callback callback;
};

Expand Down Expand Up @@ -73,7 +74,8 @@ static void sam_xdmac_isr(void *arg)

/* Execute callback */
if (channel_cfg->callback) {
channel_cfg->callback(dev, channel, err);
channel_cfg->callback(channel_cfg->callback_arg,
channel, err);
}
}
}
Expand Down Expand Up @@ -254,6 +256,7 @@ static int sam_xdmac_config(struct device *dev, u32_t channel,
}

dev_data->dma_channels[channel].callback = cfg->dma_callback;
dev_data->dma_channels[channel].callback_arg = cfg->callback_arg;

(void)memset(&transfer_cfg, 0, sizeof(transfer_cfg));
transfer_cfg.sa = cfg->head_block->source_address;
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/dma_sam_xdmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
#endif

/** DMA transfer callback */
typedef void (*dma_callback)(struct device *dev, u32_t channel, int error_code);
typedef void (*dma_callback)(void *arg, u32_t channel, int error_code);

/* XDMA_MBR_UBC */
#define XDMA_UBC_NDE (0x1u << 24)
Expand Down
9 changes: 5 additions & 4 deletions drivers/dma/dma_stm32f4x.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ struct dma_stm32_stream {
struct device *dev;
struct dma_stm32_stream_reg regs;
bool busy;

void (*dma_callback)(struct device *dev, u32_t id,
void *callback_arg;
void (*dma_callback)(void *arg, u32_t id,
int error_code);
};

Expand Down Expand Up @@ -249,12 +249,12 @@ static void dma_stm32_irq_handler(void *arg, u32_t id)
if ((irqstatus & DMA_STM32_TCI) && (config & DMA_STM32_SCR_TCIE)) {
dma_stm32_irq_clear(ddata, id, DMA_STM32_TCI);

stream->dma_callback(stream->dev, id, 0);
stream->dma_callback(stream->callback_arg, id, 0);
} else {
LOG_ERR("Internal error: IRQ status: 0x%x\n", irqstatus);
dma_stm32_irq_clear(ddata, id, irqstatus);

stream->dma_callback(stream->dev, id, -EIO);
stream->dma_callback(stream->callback_arg, id, -EIO);
}
}

Expand Down Expand Up @@ -389,6 +389,7 @@ static int dma_stm32_config(struct device *dev, u32_t id,
stream->busy = true;
stream->dma_callback = config->dma_callback;
stream->direction = config->channel_direction;
stream->callback_arg = config->callback_arg;

if (stream->direction == MEMORY_TO_PERIPHERAL) {
regs->sm0ar = (u32_t)config->head_block->source_address;
Expand Down
2 changes: 1 addition & 1 deletion include/dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct dma_config {
u32_t dest_burst_length : 16;
u32_t block_count;
struct dma_block_config *head_block;
void * callback_arg;
void *callback_arg;
void (*dma_callback)(void *callback_arg, u32_t channel,
int error_code);
};
Expand Down

0 comments on commit 4982fa9

Please sign in to comment.