Commit 7e4a858d authored by Federico Vaga's avatar Federico Vaga

kernel: check overflow in a single place

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent 9064247e
......@@ -341,6 +341,42 @@ static const struct zio_sysfs_operations zfad_s_op = {
};
static inline int zfat_overflow_detection(struct zio_ti *ti)
{
struct fa_dev *fa = ti->cset->zdev->priv_d;
struct zio_attribute *ti_zattr = ti->zattr_set.std_zattr;
uint32_t nshot_t, nsamples;
size_t shot_size;
if (ti->cset->trig != &zfat_type)
nshot_t = 1; /* with any other trigger work in one-shot mode */
else
nshot_t = ti_zattr[ZIO_ATTR_TRIG_N_SHOTS].value;
/*
* +2 because of the timetag at the end
*/
nsamples = ti_zattr[ZIO_ATTR_TRIG_PRE_SAMP].value +
ti_zattr[ZIO_ATTR_TRIG_POST_SAMP].value;
shot_size = ((nsamples + 2) * ti->cset->ssize) * FA100M14B4C_NCHAN;
if ( (shot_size * nshot_t) > FA100M14B4C_MAX_ACQ_BYTE ) {
dev_err(&ti->head.dev, "Cannot acquire, dev memory overflow\n");
return -ENOMEM;
}
/* in case of multi shot, each shot cannot exceed the dpram size */
if ( (nshot_t > 1) &&
(nsamples > fa->mshot_max_samples) ) {
dev_err(&ti->head.dev, "Cannot acquire such amount of samples "
"(req: %d , max: %d) in multi shot mode."
"dev memory overflow\n",
nsamples, fa->mshot_max_samples);
return -ENOMEM;
}
return 0;
}
/*
* zfad_input_cset_software
* @fa the adc instance to use
......@@ -355,12 +391,7 @@ static const struct zio_sysfs_operations zfad_s_op = {
static int zfad_input_cset_software(struct fa_dev *fa, struct zio_cset *cset)
{
struct zfad_block *tmp;
int err;
/* Check if device memory allows this acquisition */
err = zfat_overflow_detection(cset->ti);
if (err)
return err;
tmp = kzalloc(sizeof(struct zfad_block), GFP_ATOMIC);
if (!tmp)
return -ENOMEM;
......@@ -391,6 +422,12 @@ static int zfad_input_cset_software(struct fa_dev *fa, struct zio_cset *cset)
static int zfad_input_cset(struct zio_cset *cset)
{
struct fa_dev *fa = cset->zdev->priv_d;
int err;
/* Check if device memory allows this acquisition */
err = zfat_overflow_detection(cset->ti);
if (err)
return err;
dev_dbg(&fa->fmc->dev, "Ready to acquire\n");
/* ZIO should configure only the interleaved channel */
......
......@@ -307,11 +307,6 @@ static int zfat_arm_trigger(struct zio_ti *ti)
dev_dbg(msgdev, "Arming trigger\n");
/* Check if device memory allows this trigger configuration */
err = zfat_overflow_detection(ti);
if (err)
return err;
/* Update the current control: sequence, nsamples and tstamp */
interleave->current_ctrl->nsamples = ti->nsamples;
......
......@@ -401,40 +401,6 @@ struct zfad_block {
/* Global variable exported by fa-zio-trg.c */
extern struct zio_trigger_type zfat_type;
static inline int zfat_overflow_detection(struct zio_ti *ti)
{
struct fa_dev *fa = ti->cset->zdev->priv_d;
struct zio_attribute *ti_zattr = ti->zattr_set.std_zattr;
uint32_t nshot_t, nsamples;
size_t shot_size;
if (ti->cset->trig != &zfat_type)
nshot_t = 1; /* with any other trigger work in one-shot mode */
else
nshot_t = ti_zattr[ZIO_ATTR_TRIG_N_SHOTS].value;
/*
* +2 because of the timetag at the end
*/
nsamples = ti_zattr[ZIO_ATTR_TRIG_PRE_SAMP].value +
ti_zattr[ZIO_ATTR_TRIG_POST_SAMP].value;
shot_size = ((nsamples + 2) * ti->cset->ssize) * FA100M14B4C_NCHAN;
if ( (shot_size * nshot_t) > FA100M14B4C_MAX_ACQ_BYTE ) {
dev_err(&ti->head.dev, "Cannot acquire, dev memory overflow\n");
return -ENOMEM;
}
/* in case of multi shot, each shot cannot exceed the dpram size */
if ( (nshot_t > 1) &&
(nsamples > fa->mshot_max_samples) ) {
dev_err(&ti->head.dev, "Cannot acquire such amount of samples "
"(req: %d , max: %d) in multi shot mode."
"dev memory overflow\n",
nsamples, fa->mshot_max_samples);
return -ENOMEM;
}
return 0;
}
static inline struct fa_dev *get_zfadc(struct device *dev)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment