Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <hsweeten@visionengravers.com>
Reviewed-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

H Hartley Sweeten and committed by
Greg Kroah-Hartman
b42ca86a fa74d136

+35 -25
+35 -25
drivers/staging/comedi/drivers/ni_tio.c
··· 183 183 ni_tio_write(counter, GI_RESET(cidx), NITIO_RESET_REG(cidx)); 184 184 } 185 185 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) 188 189 { 189 190 u64 clock_period_ps; 190 191 ··· 220 219 clock_period_ps *= 8; 221 220 break; 222 221 default: 223 - BUG(); 224 - break; 222 + return -EINVAL; 225 223 } 226 - return clock_period_ps; 224 + *period_ps = clock_period_ps; 225 + return 0; 227 226 } 228 227 229 228 static void ni_tio_set_bits_transient(struct ni_gpct *counter, ··· 305 304 return bits; 306 305 } 307 306 308 - static unsigned int ni_m_series_clock_src_select(const struct ni_gpct *counter) 307 + static int ni_m_series_clock_src_select(const struct ni_gpct *counter, 308 + unsigned int *clk_src) 309 309 { 310 310 struct ni_gpct_device *counter_dev = counter->counter_dev; 311 311 unsigned int cidx = counter->counter_index; ··· 364 362 } 365 363 if (i <= NI_M_MAX_PFI_CHAN) 366 364 break; 367 - BUG(); 368 - break; 365 + return -EINVAL; 369 366 } 370 367 clock_source |= ni_tio_clock_src_modifiers(counter); 371 - return clock_source; 368 + *clk_src = clock_source; 369 + return 0; 372 370 } 373 371 374 - static unsigned int ni_660x_clock_src_select(const struct ni_gpct *counter) 372 + static int ni_660x_clock_src_select(const struct ni_gpct *counter, 373 + unsigned int *clk_src) 375 374 { 376 375 unsigned int clock_source = 0; 377 376 unsigned int cidx = counter->counter_index; ··· 422 419 } 423 420 if (i <= NI_660X_MAX_SRC_PIN) 424 421 break; 425 - BUG(); 426 - break; 422 + return -EINVAL; 427 423 } 428 424 clock_source |= ni_tio_clock_src_modifiers(counter); 429 - return clock_source; 425 + *clk_src = clock_source; 426 + return 0; 430 427 } 431 428 432 - static unsigned int 433 - ni_tio_generic_clock_src_select(const struct ni_gpct *counter) 429 + static int ni_tio_generic_clock_src_select(const struct ni_gpct *counter, 430 + unsigned int *clk_src) 434 431 { 435 432 switch (counter->counter_dev->variant) { 436 433 case ni_gpct_variant_e_series: 437 434 case ni_gpct_variant_m_series: 438 435 default: 439 - return ni_m_series_clock_src_select(counter); 436 + return ni_m_series_clock_src_select(counter, clk_src); 440 437 case ni_gpct_variant_660x: 441 - return ni_660x_clock_src_select(counter); 438 + return ni_660x_clock_src_select(counter, clk_src); 442 439 } 443 440 } 444 441 ··· 451 448 unsigned int bits = 0; 452 449 unsigned int reg; 453 450 unsigned int mode; 451 + unsigned int clk_src; 454 452 u64 ps; 455 453 bool force_alt_sync; 456 454 ··· 482 478 break; 483 479 } 484 480 485 - ps = ni_tio_clock_period_ps(counter, 486 - ni_tio_generic_clock_src_select(counter)); 481 + ni_tio_generic_clock_src_select(counter, &clk_src); 482 + ni_tio_clock_period_ps(counter, clk_src, &ps); 487 483 488 484 /* 489 485 * It's not clear what we should do if clock_period is unknown, so we ··· 804 800 return 0; 805 801 } 806 802 807 - static void ni_tio_get_clock_src(struct ni_gpct *counter, 808 - unsigned int *clock_source, 809 - unsigned int *period_ns) 803 + static int ni_tio_get_clock_src(struct ni_gpct *counter, 804 + unsigned int *clock_source, 805 + unsigned int *period_ns) 810 806 { 811 807 u64 temp64; 808 + int ret; 812 809 813 - *clock_source = ni_tio_generic_clock_src_select(counter); 814 - temp64 = ni_tio_clock_period_ps(counter, *clock_source); 810 + ret = ni_tio_generic_clock_src_select(counter, clock_source); 811 + if (ret) 812 + return ret; 813 + ret = ni_tio_clock_period_ps(counter, *clock_source, &temp64); 814 + if (ret) 815 + return ret; 815 816 do_div(temp64, 1000); /* ps to ns */ 816 817 *period_ns = temp64; 818 + return 0; 817 819 } 818 820 819 821 static int ni_660x_set_gate(struct ni_gpct *counter, unsigned int gate_source) ··· 1330 1320 ret = ni_tio_set_clock_src(counter, data[1], data[2]); 1331 1321 break; 1332 1322 case INSN_CONFIG_GET_CLOCK_SRC: 1333 - ni_tio_get_clock_src(counter, &data[1], &data[2]); 1323 + ret = ni_tio_get_clock_src(counter, &data[1], &data[2]); 1334 1324 break; 1335 1325 case INSN_CONFIG_SET_GATE_SRC: 1336 1326 ret = ni_tio_set_gate_src(counter, data[1], data[2]);