hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/base/soc.c
....@@ -17,9 +17,9 @@
1717
1818 static DEFINE_IDA(soc_ida);
1919
20
-static ssize_t soc_info_get(struct device *dev,
21
- struct device_attribute *attr,
22
- char *buf);
20
+/* Prototype to allow declarations of DEVICE_ATTR(<foo>) before soc_info_show */
21
+static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
22
+ char *buf);
2323
2424 struct soc_device {
2525 struct device dev;
....@@ -31,63 +31,65 @@
3131 .name = "soc",
3232 };
3333
34
-static DEVICE_ATTR(machine, S_IRUGO, soc_info_get, NULL);
35
-static DEVICE_ATTR(family, S_IRUGO, soc_info_get, NULL);
36
-static DEVICE_ATTR(soc_id, S_IRUGO, soc_info_get, NULL);
37
-static DEVICE_ATTR(revision, S_IRUGO, soc_info_get, NULL);
34
+static DEVICE_ATTR(machine, 0444, soc_info_show, NULL);
35
+static DEVICE_ATTR(family, 0444, soc_info_show, NULL);
36
+static DEVICE_ATTR(serial_number, 0444, soc_info_show, NULL);
37
+static DEVICE_ATTR(soc_id, 0444, soc_info_show, NULL);
38
+static DEVICE_ATTR(revision, 0444, soc_info_show, NULL);
3839
3940 struct device *soc_device_to_device(struct soc_device *soc_dev)
4041 {
4142 return &soc_dev->dev;
4243 }
43
-EXPORT_SYMBOL_GPL(soc_device_to_device);
4444
4545 static umode_t soc_attribute_mode(struct kobject *kobj,
4646 struct attribute *attr,
4747 int index)
4848 {
49
- struct device *dev = container_of(kobj, struct device, kobj);
49
+ struct device *dev = kobj_to_dev(kobj);
5050 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
5151
52
- if ((attr == &dev_attr_machine.attr)
53
- && (soc_dev->attr->machine != NULL))
52
+ if ((attr == &dev_attr_machine.attr) && soc_dev->attr->machine)
5453 return attr->mode;
55
- if ((attr == &dev_attr_family.attr)
56
- && (soc_dev->attr->family != NULL))
54
+ if ((attr == &dev_attr_family.attr) && soc_dev->attr->family)
5755 return attr->mode;
58
- if ((attr == &dev_attr_revision.attr)
59
- && (soc_dev->attr->revision != NULL))
56
+ if ((attr == &dev_attr_revision.attr) && soc_dev->attr->revision)
6057 return attr->mode;
61
- if ((attr == &dev_attr_soc_id.attr)
62
- && (soc_dev->attr->soc_id != NULL))
58
+ if ((attr == &dev_attr_serial_number.attr) && soc_dev->attr->serial_number)
59
+ return attr->mode;
60
+ if ((attr == &dev_attr_soc_id.attr) && soc_dev->attr->soc_id)
6361 return attr->mode;
6462
65
- /* Unknown or unfilled attribute. */
63
+ /* Unknown or unfilled attribute */
6664 return 0;
6765 }
6866
69
-static ssize_t soc_info_get(struct device *dev,
70
- struct device_attribute *attr,
71
- char *buf)
67
+static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
68
+ char *buf)
7269 {
7370 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
71
+ const char *output;
7472
7573 if (attr == &dev_attr_machine)
76
- return sprintf(buf, "%s\n", soc_dev->attr->machine);
77
- if (attr == &dev_attr_family)
78
- return sprintf(buf, "%s\n", soc_dev->attr->family);
79
- if (attr == &dev_attr_revision)
80
- return sprintf(buf, "%s\n", soc_dev->attr->revision);
81
- if (attr == &dev_attr_soc_id)
82
- return sprintf(buf, "%s\n", soc_dev->attr->soc_id);
74
+ output = soc_dev->attr->machine;
75
+ else if (attr == &dev_attr_family)
76
+ output = soc_dev->attr->family;
77
+ else if (attr == &dev_attr_revision)
78
+ output = soc_dev->attr->revision;
79
+ else if (attr == &dev_attr_serial_number)
80
+ output = soc_dev->attr->serial_number;
81
+ else if (attr == &dev_attr_soc_id)
82
+ output = soc_dev->attr->soc_id;
83
+ else
84
+ return -EINVAL;
8385
84
- return -EINVAL;
85
-
86
+ return sysfs_emit(buf, "%s\n", output);
8687 }
8788
8889 static struct attribute *soc_attr[] = {
8990 &dev_attr_machine.attr,
9091 &dev_attr_family.attr,
92
+ &dev_attr_serial_number.attr,
9193 &dev_attr_soc_id.attr,
9294 &dev_attr_revision.attr,
9395 NULL,
....@@ -98,15 +100,12 @@
98100 .is_visible = soc_attribute_mode,
99101 };
100102
101
-static const struct attribute_group *soc_attr_groups[] = {
102
- &soc_attr_group,
103
- NULL,
104
-};
105
-
106103 static void soc_release(struct device *dev)
107104 {
108105 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
109106
107
+ ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
108
+ kfree(soc_dev->dev.groups);
110109 kfree(soc_dev);
111110 }
112111
....@@ -115,6 +114,7 @@
115114 struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr)
116115 {
117116 struct soc_device *soc_dev;
117
+ const struct attribute_group **soc_attr_groups;
118118 int ret;
119119
120120 if (!soc_bus_type.p) {
....@@ -130,10 +130,18 @@
130130 goto out1;
131131 }
132132
133
+ soc_attr_groups = kcalloc(3, sizeof(*soc_attr_groups), GFP_KERNEL);
134
+ if (!soc_attr_groups) {
135
+ ret = -ENOMEM;
136
+ goto out2;
137
+ }
138
+ soc_attr_groups[0] = &soc_attr_group;
139
+ soc_attr_groups[1] = soc_dev_attr->custom_attr_group;
140
+
133141 /* Fetch a unique (reclaimable) SOC ID. */
134142 ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL);
135143 if (ret < 0)
136
- goto out2;
144
+ goto out3;
137145 soc_dev->soc_dev_num = ret;
138146
139147 soc_dev->attr = soc_dev_attr;
....@@ -144,15 +152,15 @@
144152 dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
145153
146154 ret = device_register(&soc_dev->dev);
147
- if (ret)
148
- goto out3;
155
+ if (ret) {
156
+ put_device(&soc_dev->dev);
157
+ return ERR_PTR(ret);
158
+ }
149159
150160 return soc_dev;
151161
152162 out3:
153
- ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
154
- put_device(&soc_dev->dev);
155
- soc_dev = NULL;
163
+ kfree(soc_attr_groups);
156164 out2:
157165 kfree(soc_dev);
158166 out1:
....@@ -163,8 +171,6 @@
163171 /* Ensure soc_dev->attr is freed prior to calling soc_device_unregister. */
164172 void soc_device_unregister(struct soc_device *soc_dev)
165173 {
166
- ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
167
-
168174 device_unregister(&soc_dev->dev);
169175 early_soc_dev_attr = NULL;
170176 }