@@ -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-
11801178static 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+
12551312static 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 );
13471406error_free_buffer_attrs :
13481407 kfree (buffer -> buffer_group .attrs );
13491408error_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
13781437static 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
0 commit comments