Skip to content

Commit d9a6257

Browse files
commodojic23
authored andcommitted
iio: core: merge buffer/ & scan_elements/ attributes
With this change, we create a new directory for the IIO device called buffer0, under which both the old buffer/ and scan_elements/ are stored. This is done to simplify the addition of multiple IIO buffers per IIO device. Otherwise we would need to add a bufferX/ and scan_elementsX/ directory for each IIO buffer. With the current way of storing attribute groups, we can't have directories stored under each other (i.e. scan_elements/ under buffer/), so the best approach moving forward is to merge their attributes. The old/legacy buffer/ & scan_elements/ groups are not stored on the opaque IIO device object. This way the IIO buffer can have just a single attribute_group object, saving a bit of memory when adding multiple IIO buffers. Signed-off-by: Alexandru Ardelean <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent e2b4d7a commit d9a6257

File tree

3 files changed

+95
-33
lines changed

3 files changed

+95
-33
lines changed

drivers/iio/industrialio-buffer.c

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,6 @@ static ssize_t iio_buffer_store_enable(struct device *dev,
11751175
return (ret < 0) ? ret : len;
11761176
}
11771177

1178-
static const char * const iio_scan_elements_group_name = "scan_elements";
1179-
11801178
static ssize_t iio_buffer_show_watermark(struct device *dev,
11811179
struct device_attribute *attr,
11821180
char *buf)
@@ -1252,8 +1250,68 @@ static struct attribute *iio_buffer_attrs[] = {
12521250
&dev_attr_data_available.attr,
12531251
};
12541252

1253+
static int iio_buffer_register_legacy_sysfs_groups(struct iio_dev *indio_dev,
1254+
struct attribute **buffer_attrs,
1255+
int buffer_attrcount,
1256+
int scan_el_attrcount)
1257+
{
1258+
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1259+
struct attribute_group *group;
1260+
struct attribute **attrs;
1261+
int ret;
1262+
1263+
attrs = kcalloc(buffer_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1264+
if (!attrs)
1265+
return -ENOMEM;
1266+
1267+
memcpy(attrs, buffer_attrs, buffer_attrcount * sizeof(*attrs));
1268+
1269+
group = &iio_dev_opaque->legacy_buffer_group;
1270+
group->attrs = attrs;
1271+
group->name = "buffer";
1272+
1273+
ret = iio_device_register_sysfs_group(indio_dev, group);
1274+
if (ret)
1275+
goto error_free_buffer_attrs;
1276+
1277+
attrs = kcalloc(scan_el_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1278+
if (!attrs) {
1279+
ret = -ENOMEM;
1280+
goto error_free_buffer_attrs;
1281+
}
1282+
1283+
memcpy(attrs, &buffer_attrs[buffer_attrcount],
1284+
scan_el_attrcount * sizeof(*attrs));
1285+
1286+
group = &iio_dev_opaque->legacy_scan_el_group;
1287+
group->attrs = attrs;
1288+
group->name = "scan_elements";
1289+
1290+
ret = iio_device_register_sysfs_group(indio_dev, group);
1291+
if (ret)
1292+
goto error_free_scan_el_attrs;
1293+
1294+
return 0;
1295+
1296+
error_free_buffer_attrs:
1297+
kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1298+
error_free_scan_el_attrs:
1299+
kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1300+
1301+
return ret;
1302+
}
1303+
1304+
static void iio_buffer_unregister_legacy_sysfs_groups(struct iio_dev *indio_dev)
1305+
{
1306+
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1307+
1308+
kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1309+
kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1310+
}
1311+
12551312
static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
1256-
struct iio_dev *indio_dev)
1313+
struct iio_dev *indio_dev,
1314+
int index)
12571315
{
12581316
struct iio_dev_attr *p;
12591317
struct attribute **attr;
@@ -1294,8 +1352,8 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
12941352
}
12951353
}
12961354

1297-
attr = kcalloc(buffer_attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
1298-
sizeof(*attr), GFP_KERNEL);
1355+
attrn = buffer_attrcount + scan_el_attrcount + ARRAY_SIZE(iio_buffer_attrs);
1356+
attr = kcalloc(attrn + 1, sizeof(* attr), GFP_KERNEL);
12991357
if (!attr) {
13001358
ret = -ENOMEM;
13011359
goto error_free_scan_mask;
@@ -1313,37 +1371,38 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
13131371
sizeof(struct attribute *) * buffer_attrcount);
13141372

13151373
buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs);
1316-
attr[buffer_attrcount] = NULL;
1317-
1318-
buffer->buffer_group.name = "buffer";
1319-
buffer->buffer_group.attrs = attr;
13201374

1321-
ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
1322-
if (ret)
1323-
goto error_free_buffer_attrs;
1375+
attrn = buffer_attrcount;
13241376

1325-
buffer->scan_el_group.name = iio_scan_elements_group_name;
1377+
list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
1378+
attr[attrn++] = &p->dev_attr.attr;
13261379

1327-
buffer->scan_el_group.attrs = kcalloc(scan_el_attrcount + 1,
1328-
sizeof(buffer->scan_el_group.attrs[0]),
1329-
GFP_KERNEL);
1330-
if (buffer->scan_el_group.attrs == NULL) {
1380+
buffer->buffer_group.name = kasprintf(GFP_KERNEL, "buffer%d", index);
1381+
if (!buffer->buffer_group.name) {
13311382
ret = -ENOMEM;
1332-
goto error_free_scan_mask;
1383+
goto error_free_buffer_attrs;
13331384
}
1334-
attrn = 0;
13351385

1336-
list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
1337-
buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
1386+
buffer->buffer_group.attrs = attr;
13381387

1339-
ret = iio_device_register_sysfs_group(indio_dev, &buffer->scan_el_group);
1388+
ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
13401389
if (ret)
1341-
goto error_free_scan_el_attrs;
1390+
goto error_free_buffer_attr_group_name;
1391+
1392+
/* we only need to register the legacy groups for the first buffer */
1393+
if (index > 0)
1394+
return 0;
1395+
1396+
ret = iio_buffer_register_legacy_sysfs_groups(indio_dev, attr,
1397+
buffer_attrcount,
1398+
scan_el_attrcount);
1399+
if (ret)
1400+
goto error_free_buffer_attr_group_name;
13421401

13431402
return 0;
13441403

1345-
error_free_scan_el_attrs:
1346-
kfree(buffer->scan_el_group.attrs);
1404+
error_free_buffer_attr_group_name:
1405+
kfree(buffer->buffer_group.name);
13471406
error_free_buffer_attrs:
13481407
kfree(buffer->buffer_group.attrs);
13491408
error_free_scan_mask:
@@ -1372,14 +1431,14 @@ int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
13721431
if (!buffer)
13731432
return 0;
13741433

1375-
return __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev);
1434+
return __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, 0);
13761435
}
13771436

13781437
static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
13791438
{
13801439
bitmap_free(buffer->scan_mask);
1440+
kfree(buffer->buffer_group.name);
13811441
kfree(buffer->buffer_group.attrs);
1382-
kfree(buffer->scan_el_group.attrs);
13831442
iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
13841443
}
13851444

@@ -1390,6 +1449,8 @@ void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
13901449
if (!buffer)
13911450
return;
13921451

1452+
iio_buffer_unregister_legacy_sysfs_groups(indio_dev);
1453+
13931454
__iio_buffer_free_sysfs_and_mask(buffer);
13941455
}
13951456

include/linux/iio/buffer_impl.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,11 @@ struct iio_buffer {
100100
/* @scan_el_dev_attr_list: List of scan element related attributes. */
101101
struct list_head scan_el_dev_attr_list;
102102

103-
/* @buffer_group: Attributes of the buffer group. */
104-
struct attribute_group buffer_group;
105-
106103
/*
107-
* @scan_el_group: Attribute group for those attributes not
108-
* created from the iio_chan_info array.
104+
* @buffer_group: Attributes of the new buffer group.
105+
* Includes scan elements attributes.
109106
*/
110-
struct attribute_group scan_el_group;
107+
struct attribute_group buffer_group;
111108

112109
/* @attrs: Standard attributes of the buffer. */
113110
const struct attribute **attrs;

include/linux/iio/iio-opaque.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* @ioctl_handlers: ioctl handlers registered with the core handler
1515
* @groups: attribute groups
1616
* @groupcounter: index of next attribute group
17+
* @legacy_scan_el_group: attribute group for legacy scan elements attribute group
18+
* @legacy_buffer_group: attribute group for legacy buffer attributes group
1719
* @debugfs_dentry: device specific debugfs dentry
1820
* @cached_reg_addr: cached register address for debugfs reads
1921
* @read_buf: read buffer to be used for the initial reg read
@@ -28,6 +30,8 @@ struct iio_dev_opaque {
2830
struct list_head ioctl_handlers;
2931
const struct attribute_group **groups;
3032
int groupcounter;
33+
struct attribute_group legacy_scan_el_group;
34+
struct attribute_group legacy_buffer_group;
3135
#if defined(CONFIG_DEBUG_FS)
3236
struct dentry *debugfs_dentry;
3337
unsigned cached_reg_addr;

0 commit comments

Comments
 (0)