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

iio: dummy: Stop using iio_device_claim_direct_scoped()

This complex cleanup.h use case of conditional guards has proved
to be more trouble that it is worth in terms of false positive compiler
warnings and hard to read code.

Move directly to the new claim/release_direct() that allow sparse
to check for unbalanced context. Introduce two new utility functions
to allow for direct returns with claim and release of direct mode
in the caller.

Reviewed-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Nuno Sa <nuno.sa@analog.com>
Link: https://patch.msgid.link/20250209180624.701140-25-jic23@kernel.org
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

+70 -49
+70 -49
drivers/iio/dummy/iio_simple_dummy.c
··· 267 267 }, 268 268 }; 269 269 270 + static int __iio_dummy_read_raw(struct iio_dev *indio_dev, 271 + struct iio_chan_spec const *chan, 272 + int *val) 273 + { 274 + struct iio_dummy_state *st = iio_priv(indio_dev); 275 + 276 + guard(mutex)(&st->lock); 277 + switch (chan->type) { 278 + case IIO_VOLTAGE: 279 + if (chan->output) { 280 + /* Set integer part to cached value */ 281 + *val = st->dac_val; 282 + return IIO_VAL_INT; 283 + } else if (chan->differential) { 284 + if (chan->channel == 1) 285 + *val = st->differential_adc_val[0]; 286 + else 287 + *val = st->differential_adc_val[1]; 288 + return IIO_VAL_INT; 289 + } else { 290 + *val = st->single_ended_adc_val; 291 + return IIO_VAL_INT; 292 + } 293 + 294 + case IIO_ACCEL: 295 + *val = st->accel_val; 296 + return IIO_VAL_INT; 297 + default: 298 + return -EINVAL; 299 + } 300 + } 301 + 302 + static int __iio_dummy_read_processed(struct iio_dev *indio_dev, 303 + struct iio_chan_spec const *chan, 304 + int *val) 305 + { 306 + struct iio_dummy_state *st = iio_priv(indio_dev); 307 + 308 + guard(mutex)(&st->lock); 309 + switch (chan->type) { 310 + case IIO_STEPS: 311 + *val = st->steps; 312 + return IIO_VAL_INT; 313 + case IIO_ACTIVITY: 314 + switch (chan->channel2) { 315 + case IIO_MOD_RUNNING: 316 + *val = st->activity_running; 317 + return IIO_VAL_INT; 318 + case IIO_MOD_WALKING: 319 + *val = st->activity_walking; 320 + return IIO_VAL_INT; 321 + default: 322 + return -EINVAL; 323 + } 324 + default: 325 + return -EINVAL; 326 + } 327 + } 328 + 270 329 /** 271 330 * iio_dummy_read_raw() - data read function. 272 331 * @indio_dev: the struct iio_dev associated with this device instance ··· 342 283 long mask) 343 284 { 344 285 struct iio_dummy_state *st = iio_priv(indio_dev); 286 + int ret; 345 287 346 288 switch (mask) { 347 289 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */ 348 - iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 349 - guard(mutex)(&st->lock); 350 - switch (chan->type) { 351 - case IIO_VOLTAGE: 352 - if (chan->output) { 353 - /* Set integer part to cached value */ 354 - *val = st->dac_val; 355 - return IIO_VAL_INT; 356 - } else if (chan->differential) { 357 - if (chan->channel == 1) 358 - *val = st->differential_adc_val[0]; 359 - else 360 - *val = st->differential_adc_val[1]; 361 - return IIO_VAL_INT; 362 - } else { 363 - *val = st->single_ended_adc_val; 364 - return IIO_VAL_INT; 365 - } 366 - 367 - case IIO_ACCEL: 368 - *val = st->accel_val; 369 - return IIO_VAL_INT; 370 - default: 371 - return -EINVAL; 372 - } 373 - } 374 - unreachable(); 290 + if (!iio_device_claim_direct(indio_dev)) 291 + return -EBUSY; 292 + ret = __iio_dummy_read_raw(indio_dev, chan, val); 293 + iio_device_release_direct(indio_dev); 294 + return ret; 375 295 case IIO_CHAN_INFO_PROCESSED: 376 - iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 377 - guard(mutex)(&st->lock); 378 - switch (chan->type) { 379 - case IIO_STEPS: 380 - *val = st->steps; 381 - return IIO_VAL_INT; 382 - case IIO_ACTIVITY: 383 - switch (chan->channel2) { 384 - case IIO_MOD_RUNNING: 385 - *val = st->activity_running; 386 - return IIO_VAL_INT; 387 - case IIO_MOD_WALKING: 388 - *val = st->activity_walking; 389 - return IIO_VAL_INT; 390 - default: 391 - return -EINVAL; 392 - } 393 - default: 394 - return -EINVAL; 395 - } 396 - } 397 - unreachable(); 296 + if (!iio_device_claim_direct(indio_dev)) 297 + return -EBUSY; 298 + ret = __iio_dummy_read_processed(indio_dev, chan, val); 299 + iio_device_release_direct(indio_dev); 300 + return ret; 398 301 case IIO_CHAN_INFO_OFFSET: 399 302 /* only single ended adc -> 7 */ 400 303 *val = 7;