• Nuno Sá's avatar
    iio: inkern: only return error codes in iio_channel_get_*() APIs · ed5e5ed4
    Nuno Sá authored
    APIs like of_iio_channel_get_by_name() and of_iio_channel_get_all() were
    returning a mix of NULL and pointers with NULL being the way to
    "notify" that we should do a "system" lookup for channels. This make
    it very confusing and prone to errors as commit 9f63cc0921ec
    ("iio: inkern: fix return value in devm_of_iio_channel_get_by_name()")
    proves. On top of this, patterns like 'if (channel != NULL) return
    channel' were being used where channel could actually be an error code
    which makes the code hard to read.
    
    This change also makes some functional changes on how errors were being
    handled. In the original behavior, even if we get an error like '-ENOMEM',
    we still continue with the search. We should only continue to lookup for
    the channel when it makes sense to do so. Hence, the main error handling
    in 'of_iio_channel_get_by_name()' is changed to the following logic:
    
     * If a channel 'name' is provided and we do find it via
    'io-channel-names', we should be able to get it. If we get any error,
    we should not proceed with the lookup. Moreover, we should return an error
    so that callers won't proceed with a system lookup.
     * If a channel 'name' is provided and we cannot find it ('index < 0'),
    'of_parse_phandle_with_args()' is expected to fail with '-EINVAL'. Hence,
    we should only continue if we get that error.
     * If a channel 'name' is not provided we should only carry on with the
    search if 'of_parse_phandle_with_args()' returns '-ENOENT'.
    
    Also note that a system channel lookup is only done if the returned
    error code (from 'of_iio_channel_get_by_name()' or
    'of_iio_channel_get_all()' is -ENODEV.
    Signed-off-by: default avatarNuno Sá <nuno.sa@analog.com>
    Reviewed-by: default avatarAndy Shevchenko <andy.shevchenko@gmail.com>
    Link: https://lore.kernel.org/r/20220715122903.332535-4-nuno.sa@analog.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
    ed5e5ed4
inkern.c 23.7 KB