Skip to content

Commit b42ca86

Browse files
bigguinessgregkh
authored andcommitted
staging: comedi: ni_tio: remove BUG() checks for ni_tio_get_clock_src()
This function calls some helper functions to convert the counter variant specific clock select bits into the generic enum ni_gpct_clock_source_bits equivelent. These helper functions currently BUG() if the clock select bits are invalid. It then calls ni_tio_clock_period_ps() to figure out the clock period based on the generic clock source. This function could also BUG() if the prescale bits are invalid. In reality this should never happen but refactor the code to return -EINVAL instead and remove the BUG() checks. These functions are also called by ni_tio_set_sync_mode(). When this function is called by ni_tio_set_clock_src() the counter select bits have already been validated. Signed-off-by: H Hartley Sweeten <[email protected]> Reviewed-by: Ian Abbott <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent fa74d13 commit b42ca86

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

drivers/staging/comedi/drivers/ni_tio.c

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ static void ni_tio_reset_count_and_disarm(struct ni_gpct *counter)
183183
ni_tio_write(counter, GI_RESET(cidx), NITIO_RESET_REG(cidx));
184184
}
185185

186-
static u64 ni_tio_clock_period_ps(const struct ni_gpct *counter,
187-
unsigned int generic_clock_source)
186+
static int ni_tio_clock_period_ps(const struct ni_gpct *counter,
187+
unsigned int generic_clock_source,
188+
u64 *period_ps)
188189
{
189190
u64 clock_period_ps;
190191

@@ -219,10 +220,10 @@ static u64 ni_tio_clock_period_ps(const struct ni_gpct *counter,
219220
clock_period_ps *= 8;
220221
break;
221222
default:
222-
BUG();
223-
break;
223+
return -EINVAL;
224224
}
225-
return clock_period_ps;
225+
*period_ps = clock_period_ps;
226+
return 0;
226227
}
227228

228229
static void ni_tio_set_bits_transient(struct ni_gpct *counter,
@@ -304,7 +305,8 @@ static unsigned int ni_tio_clock_src_modifiers(const struct ni_gpct *counter)
304305
return bits;
305306
}
306307

307-
static unsigned int ni_m_series_clock_src_select(const struct ni_gpct *counter)
308+
static int ni_m_series_clock_src_select(const struct ni_gpct *counter,
309+
unsigned int *clk_src)
308310
{
309311
struct ni_gpct_device *counter_dev = counter->counter_dev;
310312
unsigned int cidx = counter->counter_index;
@@ -362,14 +364,15 @@ static unsigned int ni_m_series_clock_src_select(const struct ni_gpct *counter)
362364
}
363365
if (i <= NI_M_MAX_PFI_CHAN)
364366
break;
365-
BUG();
366-
break;
367+
return -EINVAL;
367368
}
368369
clock_source |= ni_tio_clock_src_modifiers(counter);
369-
return clock_source;
370+
*clk_src = clock_source;
371+
return 0;
370372
}
371373

372-
static unsigned int ni_660x_clock_src_select(const struct ni_gpct *counter)
374+
static int ni_660x_clock_src_select(const struct ni_gpct *counter,
375+
unsigned int *clk_src)
373376
{
374377
unsigned int clock_source = 0;
375378
unsigned int cidx = counter->counter_index;
@@ -419,23 +422,23 @@ static unsigned int ni_660x_clock_src_select(const struct ni_gpct *counter)
419422
}
420423
if (i <= NI_660X_MAX_SRC_PIN)
421424
break;
422-
BUG();
423-
break;
425+
return -EINVAL;
424426
}
425427
clock_source |= ni_tio_clock_src_modifiers(counter);
426-
return clock_source;
428+
*clk_src = clock_source;
429+
return 0;
427430
}
428431

429-
static unsigned int
430-
ni_tio_generic_clock_src_select(const struct ni_gpct *counter)
432+
static int ni_tio_generic_clock_src_select(const struct ni_gpct *counter,
433+
unsigned int *clk_src)
431434
{
432435
switch (counter->counter_dev->variant) {
433436
case ni_gpct_variant_e_series:
434437
case ni_gpct_variant_m_series:
435438
default:
436-
return ni_m_series_clock_src_select(counter);
439+
return ni_m_series_clock_src_select(counter, clk_src);
437440
case ni_gpct_variant_660x:
438-
return ni_660x_clock_src_select(counter);
441+
return ni_660x_clock_src_select(counter, clk_src);
439442
}
440443
}
441444

@@ -448,6 +451,7 @@ static void ni_tio_set_sync_mode(struct ni_gpct *counter)
448451
unsigned int bits = 0;
449452
unsigned int reg;
450453
unsigned int mode;
454+
unsigned int clk_src;
451455
u64 ps;
452456
bool force_alt_sync;
453457

@@ -478,8 +482,8 @@ static void ni_tio_set_sync_mode(struct ni_gpct *counter)
478482
break;
479483
}
480484

481-
ps = ni_tio_clock_period_ps(counter,
482-
ni_tio_generic_clock_src_select(counter));
485+
ni_tio_generic_clock_src_select(counter, &clk_src);
486+
ni_tio_clock_period_ps(counter, clk_src, &ps);
483487

484488
/*
485489
* It's not clear what we should do if clock_period is unknown, so we
@@ -800,16 +804,22 @@ static int ni_tio_set_clock_src(struct ni_gpct *counter,
800804
return 0;
801805
}
802806

803-
static void ni_tio_get_clock_src(struct ni_gpct *counter,
804-
unsigned int *clock_source,
805-
unsigned int *period_ns)
807+
static int ni_tio_get_clock_src(struct ni_gpct *counter,
808+
unsigned int *clock_source,
809+
unsigned int *period_ns)
806810
{
807811
u64 temp64;
812+
int ret;
808813

809-
*clock_source = ni_tio_generic_clock_src_select(counter);
810-
temp64 = ni_tio_clock_period_ps(counter, *clock_source);
814+
ret = ni_tio_generic_clock_src_select(counter, clock_source);
815+
if (ret)
816+
return ret;
817+
ret = ni_tio_clock_period_ps(counter, *clock_source, &temp64);
818+
if (ret)
819+
return ret;
811820
do_div(temp64, 1000); /* ps to ns */
812821
*period_ns = temp64;
822+
return 0;
813823
}
814824

815825
static int ni_660x_set_gate(struct ni_gpct *counter, unsigned int gate_source)
@@ -1320,7 +1330,7 @@ int ni_tio_insn_config(struct comedi_device *dev,
13201330
ret = ni_tio_set_clock_src(counter, data[1], data[2]);
13211331
break;
13221332
case INSN_CONFIG_GET_CLOCK_SRC:
1323-
ni_tio_get_clock_src(counter, &data[1], &data[2]);
1333+
ret = ni_tio_get_clock_src(counter, &data[1], &data[2]);
13241334
break;
13251335
case INSN_CONFIG_SET_GATE_SRC:
13261336
ret = ni_tio_set_gate_src(counter, data[1], data[2]);

0 commit comments

Comments
 (0)