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

iio: Handle enumerated properties with gaps

Some enums might have gaps or reserved values in the middle of their value
range. E.g. consider a 2-bit enum where the values 0, 1 and 3 have a
meaning, but 2 is a reserved value and can not be used.

Add support for such enums to the IIO enum helper functions. A reserved
values is marked by setting its entry in the items array to NULL rather
than the normal descriptive string value.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20210107112049.10815-1-alexandru.ardelean@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
d9a0e73c 3cc718bc

+36 -3
+36 -3
drivers/iio/industrialio-core.c
··· 169 169 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient", 170 170 }; 171 171 172 + /** 173 + * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps 174 + * @array: array of strings 175 + * @n: number of strings in the array 176 + * @str: string to match with 177 + * 178 + * Returns index of @str in the @array or -EINVAL, similar to match_string(). 179 + * Uses sysfs_streq instead of strcmp for matching. 180 + * 181 + * This routine will look for a string in an array of strings. 182 + * The search will continue until the element is found or the n-th element 183 + * is reached, regardless of any NULL elements in the array. 184 + */ 185 + static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n, 186 + const char *str) 187 + { 188 + const char *item; 189 + int index; 190 + 191 + for (index = 0; index < n; index++) { 192 + item = array[index]; 193 + if (!item) 194 + continue; 195 + if (sysfs_streq(item, str)) 196 + return index; 197 + } 198 + 199 + return -EINVAL; 200 + } 201 + 172 202 #if defined(CONFIG_DEBUG_FS) 173 203 /* 174 204 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for ··· 500 470 if (!e->num_items) 501 471 return 0; 502 472 503 - for (i = 0; i < e->num_items; ++i) 473 + for (i = 0; i < e->num_items; ++i) { 474 + if (!e->items[i]) 475 + continue; 504 476 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 477 + } 505 478 506 479 /* replace last space with a newline */ 507 480 buf[len - 1] = '\n'; ··· 525 492 i = e->get(indio_dev, chan); 526 493 if (i < 0) 527 494 return i; 528 - else if (i >= e->num_items) 495 + else if (i >= e->num_items || !e->items[i]) 529 496 return -EINVAL; 530 497 531 498 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); ··· 542 509 if (!e->set) 543 510 return -EINVAL; 544 511 545 - ret = __sysfs_match_string(e->items, e->num_items, buf); 512 + ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf); 546 513 if (ret < 0) 547 514 return ret; 548 515