| .. | .. |
|---|
| 17 | 17 | |
|---|
| 18 | 18 | static DEFINE_IDA(soc_ida); |
|---|
| 19 | 19 | |
|---|
| 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); |
|---|
| 23 | 23 | |
|---|
| 24 | 24 | struct soc_device { |
|---|
| 25 | 25 | struct device dev; |
|---|
| .. | .. |
|---|
| 31 | 31 | .name = "soc", |
|---|
| 32 | 32 | }; |
|---|
| 33 | 33 | |
|---|
| 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); |
|---|
| 38 | 39 | |
|---|
| 39 | 40 | struct device *soc_device_to_device(struct soc_device *soc_dev) |
|---|
| 40 | 41 | { |
|---|
| 41 | 42 | return &soc_dev->dev; |
|---|
| 42 | 43 | } |
|---|
| 43 | | -EXPORT_SYMBOL_GPL(soc_device_to_device); |
|---|
| 44 | 44 | |
|---|
| 45 | 45 | static umode_t soc_attribute_mode(struct kobject *kobj, |
|---|
| 46 | 46 | struct attribute *attr, |
|---|
| 47 | 47 | int index) |
|---|
| 48 | 48 | { |
|---|
| 49 | | - struct device *dev = container_of(kobj, struct device, kobj); |
|---|
| 49 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 50 | 50 | struct soc_device *soc_dev = container_of(dev, struct soc_device, dev); |
|---|
| 51 | 51 | |
|---|
| 52 | | - if ((attr == &dev_attr_machine.attr) |
|---|
| 53 | | - && (soc_dev->attr->machine != NULL)) |
|---|
| 52 | + if ((attr == &dev_attr_machine.attr) && soc_dev->attr->machine) |
|---|
| 54 | 53 | 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) |
|---|
| 57 | 55 | 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) |
|---|
| 60 | 57 | 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) |
|---|
| 63 | 61 | return attr->mode; |
|---|
| 64 | 62 | |
|---|
| 65 | | - /* Unknown or unfilled attribute. */ |
|---|
| 63 | + /* Unknown or unfilled attribute */ |
|---|
| 66 | 64 | return 0; |
|---|
| 67 | 65 | } |
|---|
| 68 | 66 | |
|---|
| 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) |
|---|
| 72 | 69 | { |
|---|
| 73 | 70 | struct soc_device *soc_dev = container_of(dev, struct soc_device, dev); |
|---|
| 71 | + const char *output; |
|---|
| 74 | 72 | |
|---|
| 75 | 73 | 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; |
|---|
| 83 | 85 | |
|---|
| 84 | | - return -EINVAL; |
|---|
| 85 | | - |
|---|
| 86 | + return sysfs_emit(buf, "%s\n", output); |
|---|
| 86 | 87 | } |
|---|
| 87 | 88 | |
|---|
| 88 | 89 | static struct attribute *soc_attr[] = { |
|---|
| 89 | 90 | &dev_attr_machine.attr, |
|---|
| 90 | 91 | &dev_attr_family.attr, |
|---|
| 92 | + &dev_attr_serial_number.attr, |
|---|
| 91 | 93 | &dev_attr_soc_id.attr, |
|---|
| 92 | 94 | &dev_attr_revision.attr, |
|---|
| 93 | 95 | NULL, |
|---|
| .. | .. |
|---|
| 98 | 100 | .is_visible = soc_attribute_mode, |
|---|
| 99 | 101 | }; |
|---|
| 100 | 102 | |
|---|
| 101 | | -static const struct attribute_group *soc_attr_groups[] = { |
|---|
| 102 | | - &soc_attr_group, |
|---|
| 103 | | - NULL, |
|---|
| 104 | | -}; |
|---|
| 105 | | - |
|---|
| 106 | 103 | static void soc_release(struct device *dev) |
|---|
| 107 | 104 | { |
|---|
| 108 | 105 | struct soc_device *soc_dev = container_of(dev, struct soc_device, dev); |
|---|
| 109 | 106 | |
|---|
| 107 | + ida_simple_remove(&soc_ida, soc_dev->soc_dev_num); |
|---|
| 108 | + kfree(soc_dev->dev.groups); |
|---|
| 110 | 109 | kfree(soc_dev); |
|---|
| 111 | 110 | } |
|---|
| 112 | 111 | |
|---|
| .. | .. |
|---|
| 115 | 114 | struct soc_device *soc_device_register(struct soc_device_attribute *soc_dev_attr) |
|---|
| 116 | 115 | { |
|---|
| 117 | 116 | struct soc_device *soc_dev; |
|---|
| 117 | + const struct attribute_group **soc_attr_groups; |
|---|
| 118 | 118 | int ret; |
|---|
| 119 | 119 | |
|---|
| 120 | 120 | if (!soc_bus_type.p) { |
|---|
| .. | .. |
|---|
| 130 | 130 | goto out1; |
|---|
| 131 | 131 | } |
|---|
| 132 | 132 | |
|---|
| 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 | + |
|---|
| 133 | 141 | /* Fetch a unique (reclaimable) SOC ID. */ |
|---|
| 134 | 142 | ret = ida_simple_get(&soc_ida, 0, 0, GFP_KERNEL); |
|---|
| 135 | 143 | if (ret < 0) |
|---|
| 136 | | - goto out2; |
|---|
| 144 | + goto out3; |
|---|
| 137 | 145 | soc_dev->soc_dev_num = ret; |
|---|
| 138 | 146 | |
|---|
| 139 | 147 | soc_dev->attr = soc_dev_attr; |
|---|
| .. | .. |
|---|
| 144 | 152 | dev_set_name(&soc_dev->dev, "soc%d", soc_dev->soc_dev_num); |
|---|
| 145 | 153 | |
|---|
| 146 | 154 | 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 | + } |
|---|
| 149 | 159 | |
|---|
| 150 | 160 | return soc_dev; |
|---|
| 151 | 161 | |
|---|
| 152 | 162 | 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); |
|---|
| 156 | 164 | out2: |
|---|
| 157 | 165 | kfree(soc_dev); |
|---|
| 158 | 166 | out1: |
|---|
| .. | .. |
|---|
| 163 | 171 | /* Ensure soc_dev->attr is freed prior to calling soc_device_unregister. */ |
|---|
| 164 | 172 | void soc_device_unregister(struct soc_device *soc_dev) |
|---|
| 165 | 173 | { |
|---|
| 166 | | - ida_simple_remove(&soc_ida, soc_dev->soc_dev_num); |
|---|
| 167 | | - |
|---|
| 168 | 174 | device_unregister(&soc_dev->dev); |
|---|
| 169 | 175 | early_soc_dev_attr = NULL; |
|---|
| 170 | 176 | } |
|---|