1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* GPIO testing driver based on configfs.
*
* Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/bitmap.h>
#include <linux/completion.h>
#include <linux/configfs.h>
#include <linux/device.h>
#include <linux/gpio/driver.h>
#include <linux/gpio/machine.h>
#include <linux/idr.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irq_sim.h>
#include <linux/list.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/notifier.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/string_helpers.h>
#include <linux/sysfs.h>
#include "gpiolib.h"
#define GPIO_SIM_PROP_MAX 4 /* Max 3 properties + sentinel. */
#define GPIO_SIM_NUM_ATTRS 3 /* value, pull and sentinel */
static DEFINE_IDA(gpio_sim_ida);
struct gpio_sim_chip {
struct gpio_chip gc;
unsigned long *direction_map;
unsigned long *value_map;
unsigned long *pull_map;
struct irq_domain *irq_sim;
struct mutex lock;
const struct attribute_group **attr_groups;
};
struct gpio_sim_attribute {
struct device_attribute dev_attr;
unsigned int offset;
};
static struct gpio_sim_attribute *
to_gpio_sim_attr(struct device_attribute *dev_attr)
{
return container_of(dev_attr, struct gpio_sim_attribute, dev_attr);
}
static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
unsigned int offset, int value)
{
int irq, irq_type, ret;
struct gpio_desc *desc;
struct gpio_chip *gc;
gc = &chip->gc;
desc = &gc->gpiodev->descs[offset];
mutex_lock(&chip->lock);
if (test_bit(FLAG_REQUESTED, &desc->flags) &&
!test_bit(FLAG_IS_OUT, &desc->flags)) {
if (value == !!test_bit(offset, chip->value_map))
goto set_pull;
/*
* This is fine - it just means, nobody is listening
* for interrupts on this line, otherwise
* irq_create_mapping() would have been called from
* the to_irq() callback.
*/
irq = irq_find_mapping(chip->irq_sim, offset);
if (!irq)
goto set_value;
irq_type = irq_get_trigger_type(irq);
if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
(!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
true);
if (ret)
goto set_pull;
}
}
set_value:
/* Change the value unless we're actively driving the line. */
if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
!test_bit(FLAG_IS_OUT, &desc->flags))
__assign_bit(offset, chip->value_map, value);
set_pull:
__assign_bit(offset, chip->pull_map, value);
mutex_unlock(&chip->lock);
return 0;
}
static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
int ret;
mutex_lock(&chip->lock);
ret = !!test_bit(offset, chip->value_map);
mutex_unlock(&chip->lock);
return ret;
}
static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
mutex_lock(&chip->lock);
__assign_bit(offset, chip->value_map, value);
mutex_unlock(&chip->lock);
}
static int gpio_sim_get_multiple(struct gpio_chip *gc,
unsigned long *mask, unsigned long *bits)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
mutex_lock(&chip->lock);
bitmap_replace(bits, bits, chip->value_map, mask, gc->ngpio);
mutex_unlock(&chip->lock);
return 0;
}
static void gpio_sim_set_multiple(struct gpio_chip *gc,
unsigned long *mask, unsigned long *bits)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
mutex_lock(&chip->lock);
bitmap_replace(chip->value_map, chip->value_map, bits, mask, gc->ngpio);
mutex_unlock(&chip->lock);
}
static int gpio_sim_direction_output(struct gpio_chip *gc,
unsigned int offset, int value)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
mutex_lock(&chip->lock);
__clear_bit(offset, chip->direction_map);
__assign_bit(offset, chip->value_map, value);
mutex_unlock(&chip->lock);
return 0;
}
static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
mutex_lock(&chip->lock);
__set_bit(offset, chip->direction_map);
mutex_unlock(&chip->lock);
return 0;
}
static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
int direction;
mutex_lock(&chip->lock);
direction = !!test_bit(offset, chip->direction_map);
mutex_unlock(&chip->lock);
return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
}
static int gpio_sim_set_config(struct gpio_chip *gc,
unsigned int offset, unsigned long config)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
switch (pinconf_to_config_param(config)) {
case PIN_CONFIG_BIAS_PULL_UP:
return gpio_sim_apply_pull(chip, offset, 1);
case PIN_CONFIG_BIAS_PULL_DOWN:
return gpio_sim_apply_pull(chip, offset, 0);
default:
break;
}
return -ENOTSUPP;
}
static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
return irq_create_mapping(chip->irq_sim, offset);
}
static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset)
{
struct gpio_sim_chip *chip = gpiochip_get_data(gc);
mutex_lock(&chip->lock);
__assign_bit(offset, chip->value_map, !!test_bit(offset, chip->pull_map));
mutex_unlock(&chip->lock);
}
static ssize_t gpio_sim_sysfs_val_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
struct gpio_sim_chip *chip = dev_get_drvdata(dev);
int val;
mutex_lock(&chip->lock);
val = !!test_bit(line_attr->offset, chip->value_map);
mutex_unlock(&chip->lock);
return sysfs_emit(buf, "%d\n", val);
}
static ssize_t gpio_sim_sysfs_val_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
/*
* Not assigning this function will result in write() returning -EIO
* which is confusing. Return -EPERM explicitly.
*/
return -EPERM;
}
static const char *const gpio_sim_sysfs_pull_strings[] = {
[0] = "pull-down",
[1] = "pull-up",
};
static ssize_t gpio_sim_sysfs_pull_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
struct gpio_sim_chip *chip = dev_get_drvdata(dev);
int pull;
mutex_lock(&chip->lock);
pull = !!test_bit(line_attr->offset, chip->pull_map);
mutex_unlock(&chip->lock);
return sysfs_emit(buf, "%s\n", gpio_sim_sysfs_pull_strings[pull]);
}
static ssize_t gpio_sim_sysfs_pull_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
struct gpio_sim_chip *chip = dev_get_drvdata(dev);
int ret, pull;
pull = sysfs_match_string(gpio_sim_sysfs_pull_strings, buf);
if (pull < 0)
return pull;
ret = gpio_sim_apply_pull(chip, line_attr->offset, pull);
if (ret)
return ret;
return len;
}
static void gpio_sim_mutex_destroy(void *data)
{
struct mutex *lock = data;
mutex_destroy(lock);
}
static void gpio_sim_dispose_mappings(void *data)
{
struct gpio_sim_chip *chip = data;
unsigned int i;
for (i = 0; i < chip->gc.ngpio; i++)
irq_dispose_mapping(irq_find_mapping(chip->irq_sim, i));
}
static void gpio_sim_sysfs_remove(void *data)
{
struct gpio_sim_chip *chip = data;
sysfs_remove_groups(&chip->gc.gpiodev->dev.kobj, chip->attr_groups);
}
static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
{
struct device_attribute *val_dev_attr, *pull_dev_attr;
struct gpio_sim_attribute *val_attr, *pull_attr;
unsigned int num_lines = chip->gc.ngpio;
struct device *dev = chip->gc.parent;
struct attribute_group *attr_group;
struct attribute **attrs;
int i, ret;
chip->attr_groups = devm_kcalloc(dev, sizeof(*chip->attr_groups),
num_lines + 1, GFP_KERNEL);
if (!chip->attr_groups)
return -ENOMEM;
for (i = 0; i < num_lines; i++) {
attr_group = devm_kzalloc(dev, sizeof(*attr_group), GFP_KERNEL);
attrs = devm_kcalloc(dev, GPIO_SIM_NUM_ATTRS, sizeof(*attrs),
GFP_KERNEL);
val_attr = devm_kzalloc(dev, sizeof(*val_attr), GFP_KERNEL);
pull_attr = devm_kzalloc(dev, sizeof(*pull_attr), GFP_KERNEL);
if (!attr_group || !attrs || !val_attr || !pull_attr)
return -ENOMEM;
attr_group->name = devm_kasprintf(dev, GFP_KERNEL,
"sim_gpio%u", i);
if (!attr_group->name)
return -ENOMEM;
val_attr->offset = pull_attr->offset = i;
val_dev_attr = &val_attr->dev_attr;
pull_dev_attr = &pull_attr->dev_attr;
sysfs_attr_init(&val_dev_attr->attr);
sysfs_attr_init(&pull_dev_attr->attr);
val_dev_attr->attr.name = "value";
pull_dev_attr->attr.name = "pull";
val_dev_attr->attr.mode = pull_dev_attr->attr.mode = 0644;
val_dev_attr->show = gpio_sim_sysfs_val_show;
val_dev_attr->store = gpio_sim_sysfs_val_store;
pull_dev_attr->show = gpio_sim_sysfs_pull_show;
pull_dev_attr->store = gpio_sim_sysfs_pull_store;
attrs[0] = &val_dev_attr->attr;
attrs[1] = &pull_dev_attr->attr;
attr_group->attrs = attrs;
chip->attr_groups[i] = attr_group;
}
ret = sysfs_create_groups(&chip->gc.gpiodev->dev.kobj,
chip->attr_groups);
if (ret)
return ret;
return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip);
}
static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
{
struct gpio_sim_chip *chip;
struct gpio_chip *gc;
const char *label;
u32 num_lines;
int ret;
ret = fwnode_property_read_u32(swnode, "ngpios", &num_lines);
if (ret)
return ret;
ret = fwnode_property_read_string(swnode, "gpio-sim,label", &label);
if (ret) {
label = devm_kasprintf(dev, GFP_KERNEL, "%s-%s",
dev_name(dev), fwnode_get_name(swnode));
if (!label)
return -ENOMEM;
}
chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
chip->direction_map = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL);
if (!chip->direction_map)
return -ENOMEM;
/* Default to input mode. */
bitmap_fill(chip->direction_map, num_lines);
chip->value_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
if (!chip->value_map)
return -ENOMEM;
chip->pull_map = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
if (!chip->pull_map)
return -ENOMEM;
chip->irq_sim = devm_irq_domain_create_sim(dev, swnode, num_lines);
if (IS_ERR(chip->irq_sim))
return PTR_ERR(chip->irq_sim);
ret = devm_add_action_or_reset(dev, gpio_sim_dispose_mappings, chip);
if (ret)
return ret;
mutex_init(&chip->lock);
ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy,
&chip->lock);
if (ret)
return ret;
gc = &chip->gc;
gc->base = -1;
gc->ngpio = num_lines;
gc->label = label;
gc->owner = THIS_MODULE;
gc->parent = dev;
gc->fwnode = swnode;
gc->get = gpio_sim_get;
gc->set = gpio_sim_set;
gc->get_multiple = gpio_sim_get_multiple;
gc->set_multiple = gpio_sim_set_multiple;
gc->direction_output = gpio_sim_direction_output;
gc->direction_input = gpio_sim_direction_input;
gc->get_direction = gpio_sim_get_direction;
gc->set_config = gpio_sim_set_config;
gc->to_irq = gpio_sim_to_irq;
gc->free = gpio_sim_free;
gc->can_sleep = true;
ret = devm_gpiochip_add_data(dev, gc, chip);
if (ret)
return ret;
/* Used by sysfs and configfs callbacks. */
dev_set_drvdata(&gc->gpiodev->dev, chip);
return gpio_sim_setup_sysfs(chip);
}
static int gpio_sim_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct fwnode_handle *swnode;
int ret;
device_for_each_child_node(dev, swnode) {
ret = gpio_sim_add_bank(swnode, dev);
if (ret) {
fwnode_handle_put(swnode);
return ret;
}
}
return 0;
}
static const struct of_device_id gpio_sim_of_match[] = {
{ .compatible = "gpio-simulator" },
{ }
};
MODULE_DEVICE_TABLE(of, gpio_sim_of_match);
static struct platform_driver gpio_sim_driver = {
.driver = {
.name = "gpio-sim",
.of_match_table = gpio_sim_of_match,
},
.probe = gpio_sim_probe,
};
struct gpio_sim_device {
struct config_group group;
/*
* If pdev is NULL, the device is 'pending' (waiting for configuration).
* Once the pointer is assigned, the device has been created and the
* item is 'live'.
*/
struct platform_device *pdev;
int id;
/*
* Each configfs filesystem operation is protected with the subsystem
* mutex. Each separate attribute is protected with the buffer mutex.
* This structure however can be modified by callbacks of different
* attributes so we need another lock.
*
* We use this lock fo protecting all data structures owned by this
* object too.
*/
struct mutex lock;
/*
* This is used to synchronously wait for the driver's probe to complete
* and notify the user-space about any errors.
*/
struct notifier_block bus_notifier;
struct completion probe_completion;
bool driver_bound;
struct gpiod_hog *hogs;
struct list_head bank_list;
};
/* This is called with dev->lock already taken. */
static int gpio_sim_bus_notifier_call(struct notifier_block *nb,
unsigned long action, void *data)
{
struct gpio_sim_device *simdev = container_of(nb,
struct gpio_sim_device,
bus_notifier);
struct device *dev = data;
char devname[32];
snprintf(devname, sizeof(devname), "gpio-sim.%u", simdev->id);
if (strcmp(dev_name(dev), devname) == 0) {
if (action == BUS_NOTIFY_BOUND_DRIVER)
simdev->driver_bound = true;
else if (action == BUS_NOTIFY_DRIVER_NOT_BOUND)
simdev->driver_bound = false;
else
return NOTIFY_DONE;
complete(&simdev->probe_completion);
return NOTIFY_OK;
}
return NOTIFY_DONE;
}
static struct gpio_sim_device *to_gpio_sim_device(struct config_item *item)
{
struct config_group *group = to_config_group(item);
return container_of(group, struct gpio_sim_device, group);
}
struct gpio_sim_bank {
struct config_group group;
/*
* We could have used the ci_parent field of the config_item but
* configfs is stupid and calls the item's release callback after
* already having cleared the parent pointer even though the parent
* is guaranteed to survive the child...
*
* So we need to store the pointer to the parent struct here. We can
* dereference it anywhere we need with no checks and no locking as
* it's guaranteed to survive the children and protected by configfs
* locks.
*
* Same for other structures.
*/
struct gpio_sim_device *parent;
struct list_head siblings;
char *label;
unsigned int num_lines;
struct list_head line_list;
struct fwnode_handle *swnode;
};
static struct gpio_sim_bank *to_gpio_sim_bank(struct config_item *item)
{
struct config_group *group = to_config_group(item);
return container_of(group, struct gpio_sim_bank, group);
}
static bool gpio_sim_bank_has_label(struct gpio_sim_bank *bank)
{
return bank->label && *bank->label;
}
static struct gpio_sim_device *
gpio_sim_bank_get_device(struct gpio_sim_bank *bank)
{
return bank->parent;
}
struct gpio_sim_hog;
struct gpio_sim_line {
struct config_group group;
struct gpio_sim_bank *parent;
struct list_head siblings;
unsigned int offset;
char *name;
/* There can only be one hog per line. */
struct gpio_sim_hog *hog;
};
static struct gpio_sim_line *to_gpio_sim_line(struct config_item *item)
{
struct config_group *group = to_config_group(item);
return container_of(group, struct gpio_sim_line, group);
}
static struct gpio_sim_device *
gpio_sim_line_get_device(struct gpio_sim_line *line)
{
struct gpio_sim_bank *bank = line->parent;
return gpio_sim_bank_get_device(bank);
}
struct gpio_sim_hog {
struct config_item item;
struct gpio_sim_line *parent;
char *name;
int dir;
};
static struct gpio_sim_hog *to_gpio_sim_hog(struct config_item *item)
{
return container_of(item, struct gpio_sim_hog, item);
}
static struct gpio_sim_device *gpio_sim_hog_get_device(struct gpio_sim_hog *hog)
{
struct gpio_sim_line *line = hog->parent;
return gpio_sim_line_get_device(line);
}
static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev)
{
return !!dev->pdev;
}
static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
{
char *dup, *trimmed;
dup = kstrndup(str, count, GFP_KERNEL);
if (!dup)
return NULL;
trimmed = strstrip(dup);
memmove(dup, trimmed, strlen(trimmed) + 1);
return dup;
}
static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
char *page)
{
struct gpio_sim_device *dev = to_gpio_sim_device(item);
struct platform_device *pdev;
int ret;
mutex_lock(&dev->lock);
pdev = dev->pdev;
if (pdev)
ret = sprintf(page, "%s\n", dev_name(&pdev->dev));
else
ret = sprintf(page, "gpio-sim.%d\n", dev->id);
mutex_unlock(&dev->lock);
return ret;
}
CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
static ssize_t
gpio_sim_device_config_live_show(struct config_item *item, char *page)
{
struct gpio_sim_device *dev = to_gpio_sim_device(item);
bool live;
mutex_lock(&dev->lock);
live = gpio_sim_device_is_live_unlocked(dev);
mutex_unlock(&dev->lock);
return sprintf(page, "%c\n", live ? '1' : '0');
}
static char **gpio_sim_make_line_names(struct gpio_sim_bank *bank,
unsigned int *line_names_size)
{
unsigned int max_offset = 0;
bool has_line_names = false;
struct gpio_sim_line *line;
char **line_names;
list_for_each_entry(line, &bank->line_list, siblings) {
if (line->name) {
if (line->offset > max_offset)
max_offset = line->offset;
/*
* max_offset can stay at 0 so it's not an indicator
* of whether line names were configured at all.
*/
has_line_names = true;
}
}
if (!has_line_names)
/*
* This is not an error - NULL means, there are no line
* names configured.
*/
return NULL;
*line_names_size = max_offset + 1;
line_names = kcalloc(*line_names_size, sizeof(*line_names), GFP_KERNEL);
if (!line_names)
return ERR_PTR(-ENOMEM);
list_for_each_entry(line, &bank->line_list, siblings) {
if (line->name && (line->offset <= max_offset))
line_names[line->offset] = line->name;
}
return line_names;
}
static void gpio_sim_remove_hogs(struct gpio_sim_device *dev)
{
struct gpiod_hog *hog;
if (!dev->hogs)
return;
gpiod_remove_hogs(dev->hogs);
for (hog = dev->hogs; hog->chip_label; hog++) {
kfree(hog->chip_label);
kfree(hog->line_name);
}
kfree(dev->hogs);
dev->hogs = NULL;
}
static int gpio_sim_add_hogs(struct gpio_sim_device *dev)
{
unsigned int num_hogs = 0, idx = 0;
struct gpio_sim_bank *bank;
struct gpio_sim_line *line;
struct gpiod_hog *hog;
list_for_each_entry(bank, &dev->bank_list, siblings) {
list_for_each_entry(line, &bank->line_list, siblings) {
if (line->hog)
num_hogs++;
}
}
if (!num_hogs)
return 0;
/* Allocate one more for the sentinel. */
dev->hogs = kcalloc(num_hogs + 1, sizeof(*dev->hogs), GFP_KERNEL);
if (!dev->hogs)
return -ENOMEM;
list_for_each_entry(bank, &dev->bank_list, siblings) {
list_for_each_entry(line, &bank->line_list, siblings) {
if (!line->hog)
continue;
hog = &dev->hogs[idx++];
/*
* We need to make this string manually because at this
* point the device doesn't exist yet and so dev_name()
* is not available.
*/
if (gpio_sim_bank_has_label(bank))
hog->chip_label = kstrdup(bank->label,
GFP_KERNEL);
else
hog->chip_label = kasprintf(GFP_KERNEL,
"gpio-sim.%u-%s",
dev->id,
fwnode_get_name(
bank->swnode));
if (!hog->chip_label) {
gpio_sim_remove_hogs(dev);
return -ENOMEM;
}
/*
* We need to duplicate this because the hog config
* item can be removed at any time (and we can't block
* it) and gpiolib doesn't make a deep copy of the hog
* data.
*/
if (line->hog->name) {
hog->line_name = kstrdup(line->hog->name,
GFP_KERNEL);
if (!hog->line_name) {
gpio_sim_remove_hogs(dev);
return -ENOMEM;
}
}
hog->chip_hwnum = line->offset;
hog->dflags = line->hog->dir;
}
}
gpiod_add_hogs(dev->hogs);
return 0;
}
static struct fwnode_handle *
gpio_sim_make_bank_swnode(struct gpio_sim_bank *bank,
struct fwnode_handle *parent)
{
struct property_entry properties[GPIO_SIM_PROP_MAX];
unsigned int prop_idx = 0, line_names_size = 0;
struct fwnode_handle *swnode;
char **line_names;
memset(properties, 0, sizeof(properties));
properties[prop_idx++] = PROPERTY_ENTRY_U32("ngpios", bank->num_lines);
if (gpio_sim_bank_has_label(bank))
properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
bank->label);
line_names = gpio_sim_make_line_names(bank, &line_names_size);
if (IS_ERR(line_names))
return ERR_CAST(line_names);
if (line_names)
properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
"gpio-line-names",
line_names, line_names_size);
swnode = fwnode_create_software_node(properties, parent);
kfree(line_names);
return swnode;
}
static void gpio_sim_remove_swnode_recursive(struct fwnode_handle *swnode)
{
struct fwnode_handle *child;
fwnode_for_each_child_node(swnode, child)
fwnode_remove_software_node(child);
fwnode_remove_software_node(swnode);
}
static bool gpio_sim_bank_labels_non_unique(struct gpio_sim_device *dev)
{
struct gpio_sim_bank *this, *pos;
list_for_each_entry(this, &dev->bank_list, siblings) {
list_for_each_entry(pos, &dev->bank_list, siblings) {
if (this == pos || (!this->label || !pos->label))
continue;
if (strcmp(this->label, pos->label) == 0)
return true;
}
}
return false;
}
static int gpio_sim_device_activate_unlocked(struct gpio_sim_device *dev)
{
struct platform_device_info pdevinfo;
struct fwnode_handle *swnode;
struct platform_device *pdev;
struct gpio_sim_bank *bank;
int ret;
if (list_empty(&dev->bank_list))
return -ENODATA;
/*
* Non-unique GPIO device labels are a corner-case we don't support
* as it would interfere with machine hogging mechanism and has little
* use in real life.
*/
if (gpio_sim_bank_labels_non_unique(dev))
return -EINVAL;
memset(&pdevinfo, 0, sizeof(pdevinfo));
swnode = fwnode_create_software_node(NULL, NULL);
if (IS_ERR(swnode))
return PTR_ERR(swnode);
list_for_each_entry(bank, &dev->bank_list, siblings) {
bank->swnode = gpio_sim_make_bank_swnode(bank, swnode);
if (IS_ERR(bank->swnode)) {
ret = PTR_ERR(bank->swnode);
gpio_sim_remove_swnode_recursive(swnode);
return ret;
}
}
ret = gpio_sim_add_hogs(dev);
if (ret) {
gpio_sim_remove_swnode_recursive(swnode);
return ret;
}
pdevinfo.name = "gpio-sim";
pdevinfo.fwnode = swnode;
pdevinfo.id = dev->id;
reinit_completion(&dev->probe_completion);
dev->driver_bound = false;
bus_register_notifier(&platform_bus_type, &dev->bus_notifier);
pdev = platform_device_register_full(&pdevinfo);
if (IS_ERR(pdev)) {
bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
gpio_sim_remove_hogs(dev);
gpio_sim_remove_swnode_recursive(swnode);
return PTR_ERR(pdev);
}
wait_for_completion(&dev->probe_completion);
bus_unregister_notifier(&platform_bus_type, &dev->bus_notifier);
if (!dev->driver_bound) {
/* Probe failed, check kernel log. */
platform_device_unregister(pdev);
gpio_sim_remove_hogs(dev);
gpio_sim_remove_swnode_recursive(swnode);
return -ENXIO;
}
dev->pdev = pdev;
return 0;
}
static void gpio_sim_device_deactivate_unlocked(struct gpio_sim_device *dev)
{
struct fwnode_handle *swnode;
swnode = dev_fwnode(&dev->pdev->dev);
platform_device_unregister(dev->pdev);
gpio_sim_remove_swnode_recursive(swnode);
dev->pdev = NULL;
gpio_sim_remove_hogs(dev);
}
static ssize_t
gpio_sim_device_config_live_store(struct config_item *item,
const char *page, size_t count)
{
struct gpio_sim_device *dev = to_gpio_sim_device(item);
bool live;
int ret;
ret = kstrtobool(page, &live);
if (ret)
return ret;
mutex_lock(&dev->lock);
if ((!live && !gpio_sim_device_is_live_unlocked(dev)) ||
(live && gpio_sim_device_is_live_unlocked(dev)))
ret = -EPERM;
else if (live)
ret = gpio_sim_device_activate_unlocked(dev);
else
gpio_sim_device_deactivate_unlocked(dev);
mutex_unlock(&dev->lock);
return ret ?: count;
}
CONFIGFS_ATTR(gpio_sim_device_config_, live);
static struct configfs_attribute *gpio_sim_device_config_attrs[] = {
&gpio_sim_device_config_attr_dev_name,
&gpio_sim_device_config_attr_live,
NULL
};
struct gpio_sim_chip_name_ctx {
struct fwnode_handle *swnode;
char *page;
};
static int gpio_sim_emit_chip_name(struct device *dev, void *data)
{
struct gpio_sim_chip_name_ctx *ctx = data;
/* This would be the sysfs device exported in /sys/class/gpio. */
if (dev->class)
return 0;
if (device_match_fwnode(dev, ctx->swnode))
return sprintf(ctx->page, "%s\n", dev_name(dev));
return 0;
}
static ssize_t gpio_sim_bank_config_chip_name_show(struct config_item *item,
char *page)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
struct gpio_sim_chip_name_ctx ctx = { bank->swnode, page };
int ret;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev))
ret = device_for_each_child(&dev->pdev->dev, &ctx,
gpio_sim_emit_chip_name);
else
ret = sprintf(page, "none\n");
mutex_unlock(&dev->lock);
return ret;
}
CONFIGFS_ATTR_RO(gpio_sim_bank_config_, chip_name);
static ssize_t
gpio_sim_bank_config_label_show(struct config_item *item, char *page)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
int ret;
mutex_lock(&dev->lock);
ret = sprintf(page, "%s\n", bank->label ?: "");
mutex_unlock(&dev->lock);
return ret;
}
static ssize_t gpio_sim_bank_config_label_store(struct config_item *item,
const char *page, size_t count)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
char *trimmed;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return -EBUSY;
}
trimmed = gpio_sim_strdup_trimmed(page, count);
if (!trimmed) {
mutex_unlock(&dev->lock);
return -ENOMEM;
}
kfree(bank->label);
bank->label = trimmed;
mutex_unlock(&dev->lock);
return count;
}
CONFIGFS_ATTR(gpio_sim_bank_config_, label);
static ssize_t
gpio_sim_bank_config_num_lines_show(struct config_item *item, char *page)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
int ret;
mutex_lock(&dev->lock);
ret = sprintf(page, "%u\n", bank->num_lines);
mutex_unlock(&dev->lock);
return ret;
}
static ssize_t
gpio_sim_bank_config_num_lines_store(struct config_item *item,
const char *page, size_t count)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
unsigned int num_lines;
int ret;
ret = kstrtouint(page, 0, &num_lines);
if (ret)
return ret;
if (num_lines == 0)
return -EINVAL;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return -EBUSY;
}
bank->num_lines = num_lines;
mutex_unlock(&dev->lock);
return count;
}
CONFIGFS_ATTR(gpio_sim_bank_config_, num_lines);
static struct configfs_attribute *gpio_sim_bank_config_attrs[] = {
&gpio_sim_bank_config_attr_chip_name,
&gpio_sim_bank_config_attr_label,
&gpio_sim_bank_config_attr_num_lines,
NULL
};
static ssize_t
gpio_sim_line_config_name_show(struct config_item *item, char *page)
{
struct gpio_sim_line *line = to_gpio_sim_line(item);
struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
int ret;
mutex_lock(&dev->lock);
ret = sprintf(page, "%s\n", line->name ?: "");
mutex_unlock(&dev->lock);
return ret;
}
static ssize_t gpio_sim_line_config_name_store(struct config_item *item,
const char *page, size_t count)
{
struct gpio_sim_line *line = to_gpio_sim_line(item);
struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
char *trimmed;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return -EBUSY;
}
trimmed = gpio_sim_strdup_trimmed(page, count);
if (!trimmed) {
mutex_unlock(&dev->lock);
return -ENOMEM;
}
kfree(line->name);
line->name = trimmed;
mutex_unlock(&dev->lock);
return count;
}
CONFIGFS_ATTR(gpio_sim_line_config_, name);
static struct configfs_attribute *gpio_sim_line_config_attrs[] = {
&gpio_sim_line_config_attr_name,
NULL
};
static ssize_t gpio_sim_hog_config_name_show(struct config_item *item,
char *page)
{
struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
int ret;
mutex_lock(&dev->lock);
ret = sprintf(page, "%s\n", hog->name ?: "");
mutex_unlock(&dev->lock);
return ret;
}
static ssize_t gpio_sim_hog_config_name_store(struct config_item *item,
const char *page, size_t count)
{
struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
char *trimmed;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return -EBUSY;
}
trimmed = gpio_sim_strdup_trimmed(page, count);
if (!trimmed) {
mutex_unlock(&dev->lock);
return -ENOMEM;
}
kfree(hog->name);
hog->name = trimmed;
mutex_unlock(&dev->lock);
return count;
}
CONFIGFS_ATTR(gpio_sim_hog_config_, name);
static ssize_t gpio_sim_hog_config_direction_show(struct config_item *item,
char *page)
{
struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
char *repr;
int dir;
mutex_lock(&dev->lock);
dir = hog->dir;
mutex_unlock(&dev->lock);
switch (dir) {
case GPIOD_IN:
repr = "input";
break;
case GPIOD_OUT_HIGH:
repr = "output-high";
break;
case GPIOD_OUT_LOW:
repr = "output-low";
break;
default:
/* This would be a programmer bug. */
WARN(1, "Unexpected hog direction value: %d", dir);
return -EINVAL;
}
return sprintf(page, "%s\n", repr);
}
static ssize_t
gpio_sim_hog_config_direction_store(struct config_item *item,
const char *page, size_t count)
{
struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
char *trimmed;
int dir;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return -EBUSY;
}
trimmed = gpio_sim_strdup_trimmed(page, count);
if (!trimmed) {
mutex_unlock(&dev->lock);
return -ENOMEM;
}
if (strcmp(trimmed, "input") == 0)
dir = GPIOD_IN;
else if (strcmp(trimmed, "output-high") == 0)
dir = GPIOD_OUT_HIGH;
else if (strcmp(trimmed, "output-low") == 0)
dir = GPIOD_OUT_LOW;
else
dir = -EINVAL;
kfree(trimmed);
if (dir < 0) {
mutex_unlock(&dev->lock);
return dir;
}
hog->dir = dir;
mutex_unlock(&dev->lock);
return count;
}
CONFIGFS_ATTR(gpio_sim_hog_config_, direction);
static struct configfs_attribute *gpio_sim_hog_config_attrs[] = {
&gpio_sim_hog_config_attr_name,
&gpio_sim_hog_config_attr_direction,
NULL
};
static void gpio_sim_hog_config_item_release(struct config_item *item)
{
struct gpio_sim_hog *hog = to_gpio_sim_hog(item);
struct gpio_sim_line *line = hog->parent;
struct gpio_sim_device *dev = gpio_sim_hog_get_device(hog);
mutex_lock(&dev->lock);
line->hog = NULL;
mutex_unlock(&dev->lock);
kfree(hog->name);
kfree(hog);
}
static struct configfs_item_operations gpio_sim_hog_config_item_ops = {
.release = gpio_sim_hog_config_item_release,
};
static const struct config_item_type gpio_sim_hog_config_type = {
.ct_item_ops = &gpio_sim_hog_config_item_ops,
.ct_attrs = gpio_sim_hog_config_attrs,
.ct_owner = THIS_MODULE,
};
static struct config_item *
gpio_sim_line_config_make_hog_item(struct config_group *group, const char *name)
{
struct gpio_sim_line *line = to_gpio_sim_line(&group->cg_item);
struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
struct gpio_sim_hog *hog;
if (strcmp(name, "hog") != 0)
return ERR_PTR(-EINVAL);
mutex_lock(&dev->lock);
hog = kzalloc(sizeof(*hog), GFP_KERNEL);
if (!hog) {
mutex_unlock(&dev->lock);
return ERR_PTR(-ENOMEM);
}
config_item_init_type_name(&hog->item, name,
&gpio_sim_hog_config_type);
hog->dir = GPIOD_IN;
hog->name = NULL;
hog->parent = line;
line->hog = hog;
mutex_unlock(&dev->lock);
return &hog->item;
}
static void gpio_sim_line_config_group_release(struct config_item *item)
{
struct gpio_sim_line *line = to_gpio_sim_line(item);
struct gpio_sim_device *dev = gpio_sim_line_get_device(line);
mutex_lock(&dev->lock);
list_del(&line->siblings);
mutex_unlock(&dev->lock);
kfree(line->name);
kfree(line);
}
static struct configfs_item_operations gpio_sim_line_config_item_ops = {
.release = gpio_sim_line_config_group_release,
};
static struct configfs_group_operations gpio_sim_line_config_group_ops = {
.make_item = gpio_sim_line_config_make_hog_item,
};
static const struct config_item_type gpio_sim_line_config_type = {
.ct_item_ops = &gpio_sim_line_config_item_ops,
.ct_group_ops = &gpio_sim_line_config_group_ops,
.ct_attrs = gpio_sim_line_config_attrs,
.ct_owner = THIS_MODULE,
};
static struct config_group *
gpio_sim_bank_config_make_line_group(struct config_group *group,
const char *name)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(&group->cg_item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
struct gpio_sim_line *line;
unsigned int offset;
int ret, nchar;
ret = sscanf(name, "line%u%n", &offset, &nchar);
if (ret != 1 || nchar != strlen(name))
return ERR_PTR(-EINVAL);
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return ERR_PTR(-EBUSY);
}
line = kzalloc(sizeof(*line), GFP_KERNEL);
if (!line) {
mutex_unlock(&dev->lock);
return ERR_PTR(-ENOMEM);
}
config_group_init_type_name(&line->group, name,
&gpio_sim_line_config_type);
line->parent = bank;
line->offset = offset;
list_add_tail(&line->siblings, &bank->line_list);
mutex_unlock(&dev->lock);
return &line->group;
}
static void gpio_sim_bank_config_group_release(struct config_item *item)
{
struct gpio_sim_bank *bank = to_gpio_sim_bank(item);
struct gpio_sim_device *dev = gpio_sim_bank_get_device(bank);
mutex_lock(&dev->lock);
list_del(&bank->siblings);
mutex_unlock(&dev->lock);
kfree(bank->label);
kfree(bank);
}
static struct configfs_item_operations gpio_sim_bank_config_item_ops = {
.release = gpio_sim_bank_config_group_release,
};
static struct configfs_group_operations gpio_sim_bank_config_group_ops = {
.make_group = gpio_sim_bank_config_make_line_group,
};
static const struct config_item_type gpio_sim_bank_config_group_type = {
.ct_item_ops = &gpio_sim_bank_config_item_ops,
.ct_group_ops = &gpio_sim_bank_config_group_ops,
.ct_attrs = gpio_sim_bank_config_attrs,
.ct_owner = THIS_MODULE,
};
static struct config_group *
gpio_sim_device_config_make_bank_group(struct config_group *group,
const char *name)
{
struct gpio_sim_device *dev = to_gpio_sim_device(&group->cg_item);
struct gpio_sim_bank *bank;
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev)) {
mutex_unlock(&dev->lock);
return ERR_PTR(-EBUSY);
}
bank = kzalloc(sizeof(*bank), GFP_KERNEL);
if (!bank) {
mutex_unlock(&dev->lock);
return ERR_PTR(-ENOMEM);
}
config_group_init_type_name(&bank->group, name,
&gpio_sim_bank_config_group_type);
bank->num_lines = 1;
bank->parent = dev;
INIT_LIST_HEAD(&bank->line_list);
list_add_tail(&bank->siblings, &dev->bank_list);
mutex_unlock(&dev->lock);
return &bank->group;
}
static void gpio_sim_device_config_group_release(struct config_item *item)
{
struct gpio_sim_device *dev = to_gpio_sim_device(item);
mutex_lock(&dev->lock);
if (gpio_sim_device_is_live_unlocked(dev))
gpio_sim_device_deactivate_unlocked(dev);
mutex_unlock(&dev->lock);
mutex_destroy(&dev->lock);
ida_free(&gpio_sim_ida, dev->id);
kfree(dev);
}
static struct configfs_item_operations gpio_sim_device_config_item_ops = {
.release = gpio_sim_device_config_group_release,
};
static struct configfs_group_operations gpio_sim_device_config_group_ops = {
.make_group = gpio_sim_device_config_make_bank_group,
};
static const struct config_item_type gpio_sim_device_config_group_type = {
.ct_item_ops = &gpio_sim_device_config_item_ops,
.ct_group_ops = &gpio_sim_device_config_group_ops,
.ct_attrs = gpio_sim_device_config_attrs,
.ct_owner = THIS_MODULE,
};
static struct config_group *
gpio_sim_config_make_device_group(struct config_group *group, const char *name)
{
struct gpio_sim_device *dev;
int id;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return ERR_PTR(-ENOMEM);
id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
if (id < 0) {
kfree(dev);
return ERR_PTR(id);
}
config_group_init_type_name(&dev->group, name,
&gpio_sim_device_config_group_type);
dev->id = id;
mutex_init(&dev->lock);
INIT_LIST_HEAD(&dev->bank_list);
dev->bus_notifier.notifier_call = gpio_sim_bus_notifier_call;
init_completion(&dev->probe_completion);
return &dev->group;
}
static struct configfs_group_operations gpio_sim_config_group_ops = {
.make_group = gpio_sim_config_make_device_group,
};
static const struct config_item_type gpio_sim_config_type = {
.ct_group_ops = &gpio_sim_config_group_ops,
.ct_owner = THIS_MODULE,
};
static struct configfs_subsystem gpio_sim_config_subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = "gpio-sim",
.ci_type = &gpio_sim_config_type,
},
},
};
static int __init gpio_sim_init(void)
{
int ret;
ret = platform_driver_register(&gpio_sim_driver);
if (ret) {
pr_err("Error %d while registering the platform driver\n", ret);
return ret;
}
config_group_init(&gpio_sim_config_subsys.su_group);
mutex_init(&gpio_sim_config_subsys.su_mutex);
ret = configfs_register_subsystem(&gpio_sim_config_subsys);
if (ret) {
pr_err("Error %d while registering the configfs subsystem %s\n",
ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
mutex_destroy(&gpio_sim_config_subsys.su_mutex);
platform_driver_unregister(&gpio_sim_driver);
return ret;
}
return 0;
}
module_init(gpio_sim_init);
static void __exit gpio_sim_exit(void)
{
configfs_unregister_subsystem(&gpio_sim_config_subsys);
mutex_destroy(&gpio_sim_config_subsys.su_mutex);
platform_driver_unregister(&gpio_sim_driver);
}
module_exit(gpio_sim_exit);
MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl");
MODULE_DESCRIPTION("GPIO Simulator Module");
MODULE_LICENSE("GPL");
|