0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/sysfs.h>
0009 #include <linux/init.h>
0010 #include <linux/stat.h>
0011 #include <linux/slab.h>
0012 #include <linux/idr.h>
0013 #include <linux/spinlock.h>
0014 #include <linux/sys_soc.h>
0015 #include <linux/err.h>
0016 #include <linux/glob.h>
0017
0018 static DEFINE_IDA(soc_ida);
0019
0020
0021 static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
0022 char *buf);
0023
0024 struct soc_device {
0025 struct device dev;
0026 struct soc_device_attribute *attr;
0027 int soc_dev_num;
0028 };
0029
0030 static struct bus_type soc_bus_type = {
0031 .name = "soc",
0032 };
0033
0034 static DEVICE_ATTR(machine, 0444, soc_info_show, NULL);
0035 static DEVICE_ATTR(family, 0444, soc_info_show, NULL);
0036 static DEVICE_ATTR(serial_number, 0444, soc_info_show, NULL);
0037 static DEVICE_ATTR(soc_id, 0444, soc_info_show, NULL);
0038 static DEVICE_ATTR(revision, 0444, soc_info_show, NULL);
0039
0040 struct device *soc_device_to_device(struct soc_device *soc_dev)
0041 {
0042 return &soc_dev->dev;
0043 }
0044
0045 static umode_t soc_attribute_mode(struct kobject *kobj,
0046 struct attribute *attr,
0047 int index)
0048 {
0049 struct device *dev = kobj_to_dev(kobj);
0050 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
0051
0052 if ((attr == &dev_attr_machine.attr) && soc_dev->attr->machine)
0053 return attr->mode;
0054 if ((attr == &dev_attr_family.attr) && soc_dev->attr->family)
0055 return attr->mode;
0056 if ((attr == &dev_attr_revision.attr) && soc_dev->attr->revision)
0057 return attr->mode;
0058 if ((attr == &dev_attr_serial_number.attr) && soc_dev->attr->serial_number)
0059 return attr->mode;
0060 if ((attr == &dev_attr_soc_id.attr) && soc_dev->attr->soc_id)
0061 return attr->mode;
0062
0063
0064 return 0;
0065 }
0066
0067 static ssize_t soc_info_show(struct device *dev, struct device_attribute *attr,
0068 char *buf)
0069 {
0070 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
0071 const char *output;
0072
0073 if (attr == &dev_attr_machine)
0074 output = soc_dev->attr->machine;
0075 else if (attr == &dev_attr_family)
0076 output = soc_dev->attr->family;
0077 else if (attr == &dev_attr_revision)
0078 output = soc_dev->attr->revision;
0079 else if (attr == &dev_attr_serial_number)
0080 output = soc_dev->attr->serial_number;
0081 else if (attr == &dev_attr_soc_id)
0082 output = soc_dev->attr->soc_id;
0083 else
0084 return -EINVAL;
0085
0086 return sysfs_emit(buf, "%s\n", output);
0087 }
0088
0089 static struct attribute *soc_attr[] = {
0090 &dev_attr_machine.attr,
0091 &dev_attr_family.attr,
0092 &dev_attr_serial_number.attr,
0093 &dev_attr_soc_id.attr,
0094 &dev_attr_revision.attr,
0095 NULL,
0096 };
0097
0098 static const struct attribute_group soc_attr_group = {
0099 .attrs = soc_attr,
0100 .is_visible = soc_attribute_mode,
0101 };
0102
0103 static void soc_release(struct device *dev)
0104 {
0105 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
0106
0107 ida_simple_remove(&soc_ida, soc_dev->soc_dev_num);
0108 kfree(soc_dev->dev.groups);
0109 kfree(soc_dev);
0110 }
0111
0112 static struct soc_device_attribute *early_soc_dev_attr;
0113
0114 struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr)
0115 {
0116 struct soc_device *soc_dev;
0117 const struct attribute_group **soc_attr_groups;
0118 int ret;
0119
0120 if (!soc_bus_type.p) {
0121 if (early_soc_dev_attr)
0122 return ERR_PTR(-EBUSY);
0123 early_soc_dev_attr = soc_dev_attr;
0124 return NULL;
0125 }
0126
0127 soc_dev = kzalloc(sizeof(*soc_dev), GFP_KERNEL);
0128 if (!soc_dev) {
0129 ret = -ENOMEM;
0130 goto out1;
0131 }
0132
0133 soc_attr_groups = kcalloc(3, sizeof(*soc_attr_groups), GFP_KERNEL);
0134 if (!soc_attr_groups) {
0135 ret = -ENOMEM;
0136 goto out2;
0137 }
0138 soc_attr_groups[0] = &soc_attr_group;
0139 soc_attr_groups[1] = soc_dev_attr->custom_attr_group;
0140
0141
0142 ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL);
0143 if (ret < 0)
0144 goto out3;
0145 soc_dev->soc_dev_num = ret;
0146
0147 soc_dev->attr = soc_dev_attr;
0148 soc_dev->dev.bus = &soc_bus_type;
0149 soc_dev->dev.groups = soc_attr_groups;
0150 soc_dev->dev.release = soc_release;
0151
0152 dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num);
0153
0154 ret = device_register(&soc_dev->dev);
0155 if (ret) {
0156 put_device(&soc_dev->dev);
0157 return ERR_PTR(ret);
0158 }
0159
0160 return soc_dev;
0161
0162 out3:
0163 kfree(soc_attr_groups);
0164 out2:
0165 kfree(soc_dev);
0166 out1:
0167 return ERR_PTR(ret);
0168 }
0169 EXPORT_SYMBOL_GPL(soc_device_register);
0170
0171
0172 void soc_device_unregister(struct soc_device *soc_dev)
0173 {
0174 device_unregister(&soc_dev->dev);
0175 early_soc_dev_attr = NULL;
0176 }
0177 EXPORT_SYMBOL_GPL(soc_device_unregister);
0178
0179 static int __init soc_bus_register(void)
0180 {
0181 int ret;
0182
0183 ret = bus_register(&soc_bus_type);
0184 if (ret)
0185 return ret;
0186
0187 if (early_soc_dev_attr)
0188 return PTR_ERR(soc_device_register(early_soc_dev_attr));
0189
0190 return 0;
0191 }
0192 core_initcall(soc_bus_register);
0193
0194 static int soc_device_match_attr(const struct soc_device_attribute *attr,
0195 const struct soc_device_attribute *match)
0196 {
0197 if (match->machine &&
0198 (!attr->machine || !glob_match(match->machine, attr->machine)))
0199 return 0;
0200
0201 if (match->family &&
0202 (!attr->family || !glob_match(match->family, attr->family)))
0203 return 0;
0204
0205 if (match->revision &&
0206 (!attr->revision || !glob_match(match->revision, attr->revision)))
0207 return 0;
0208
0209 if (match->soc_id &&
0210 (!attr->soc_id || !glob_match(match->soc_id, attr->soc_id)))
0211 return 0;
0212
0213 return 1;
0214 }
0215
0216 static int soc_device_match_one(struct device *dev, void *arg)
0217 {
0218 struct soc_device *soc_dev = container_of(dev, struct soc_device, dev);
0219
0220 return soc_device_match_attr(soc_dev->attr, arg);
0221 }
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 const struct soc_device_attribute *soc_device_match(
0242 const struct soc_device_attribute *matches)
0243 {
0244 int ret;
0245
0246 if (!matches)
0247 return NULL;
0248
0249 while (matches->machine || matches->family || matches->revision ||
0250 matches->soc_id) {
0251 ret = bus_for_each_dev(&soc_bus_type, NULL, (void *)matches,
0252 soc_device_match_one);
0253 if (ret < 0 && early_soc_dev_attr)
0254 ret = soc_device_match_attr(early_soc_dev_attr,
0255 matches);
0256 if (ret < 0)
0257 return NULL;
0258 if (ret)
0259 return matches;
0260
0261 matches++;
0262 }
0263 return NULL;
0264 }
0265 EXPORT_SYMBOL_GPL(soc_device_match);