1e45217307b4fafa923c77f0d9898a4fd1d64659
[openwrt/staging/robimarko.git] /
1 From 4a84eacedc55e78c8f64a5a4f9ade6e285844b85 Mon Sep 17 00:00:00 2001
2 From: Ioana Ciornei <ioana.ciornei@nxp.com>
3 Date: Mon, 25 Jun 2018 13:19:53 +0300
4 Subject: [PATCH] drivers/base: add sysfs entries for suppliers and consumers
5
6 Instead of scraping dmesg for messages such as 'Linked as a consumer to'
7 or 'Dropping the link to' export two new sysfs entries in the device
8 folder that list the consumer and supplier devices.
9
10 Signed-off-by: Ioana Ciornei <ioana.ciornei@nxp.com>
11 ---
12 Documentation/ABI/testing/sysfs-devices-links | 13 +++++++++
13 drivers/base/core.c | 42 +++++++++++++++++++++++++++
14 2 files changed, 55 insertions(+)
15 create mode 100644 Documentation/ABI/testing/sysfs-devices-links
16
17 --- /dev/null
18 +++ b/Documentation/ABI/testing/sysfs-devices-links
19 @@ -0,0 +1,13 @@
20 +What: /sys/devices/.../consumers
21 +Date: October 2018
22 +Contact: Ioana Ciornei <ioana.ciornei@nxp.com>
23 +Description:
24 + Read-only attribute that lists the current "consumers" of
25 + a specific device.
26 +
27 +What: /sys/devices/.../suppliers
28 +Date: October 2018
29 +Contact: Ioana Ciornei <ioana.ciornei@nxp.com>
30 +Description:
31 + Read-only attribute that lists the current "suppliers" of
32 + a specific device.
33 --- a/drivers/base/core.c
34 +++ b/drivers/base/core.c
35 @@ -1320,6 +1320,34 @@ static ssize_t online_store(struct devic
36 }
37 static DEVICE_ATTR_RW(online);
38
39 +static ssize_t suppliers_show(struct device *dev, struct device_attribute *attr,
40 + char *buf)
41 +{
42 + struct device_link *link;
43 + size_t count = 0;
44 +
45 + list_for_each_entry(link, &dev->links.suppliers, c_node)
46 + count += scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
47 + dev_name(link->supplier));
48 +
49 + return count;
50 +}
51 +static DEVICE_ATTR_RO(suppliers);
52 +
53 +static ssize_t consumers_show(struct device *dev, struct device_attribute *attr,
54 + char *buf)
55 +{
56 + struct device_link *link;
57 + size_t count = 0;
58 +
59 + list_for_each_entry(link, &dev->links.consumers, s_node)
60 + count += scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
61 + dev_name(link->consumer));
62 +
63 + return count;
64 +}
65 +static DEVICE_ATTR_RO(consumers);
66 +
67 int device_add_groups(struct device *dev, const struct attribute_group **groups)
68 {
69 return sysfs_create_groups(&dev->kobj, groups);
70 @@ -1491,8 +1519,20 @@ static int device_add_attrs(struct devic
71 goto err_remove_dev_groups;
72 }
73
74 + error = device_create_file(dev, &dev_attr_suppliers);
75 + if (error)
76 + goto err_remove_online;
77 +
78 + error = device_create_file(dev, &dev_attr_consumers);
79 + if (error)
80 + goto err_remove_suppliers;
81 +
82 return 0;
83
84 + err_remove_suppliers:
85 + device_remove_file(dev, &dev_attr_suppliers);
86 + err_remove_online:
87 + device_remove_file(dev, &dev_attr_online);
88 err_remove_dev_groups:
89 device_remove_groups(dev, dev->groups);
90 err_remove_type_groups:
91 @@ -1510,6 +1550,8 @@ static void device_remove_attrs(struct d
92 struct class *class = dev->class;
93 const struct device_type *type = dev->type;
94
95 + device_remove_file(dev, &dev_attr_consumers);
96 + device_remove_file(dev, &dev_attr_suppliers);
97 device_remove_file(dev, &dev_attr_online);
98 device_remove_groups(dev, dev->groups);
99