From: Allen Hung <allen_hung@dell.com>
To: Jean Delvare <jdelvare@suse.com>, linux-kernel@vger.kernel.org
Cc: Mario Limonciello <mario_limonciello@dell.com>,
Allen Hung <allen_hung@dell.com>
Subject: [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting oem strings to sysfs
Date: Thu, 14 Jul 2016 16:01:23 +0800 [thread overview]
Message-ID: <1468483283-84766-3-git-send-email-allen_hung@dell.com> (raw)
In-Reply-To: <1468483283-84766-1-git-send-email-allen_hung@dell.com>
The oem strings in DMI system identification information of the BIOS have
been parsed and stored as dmi devices in dmi_scan.c but they are not
exported to userspace via sysfs.
The patch intends to export oem strings to sysfs device /sys/class/dmi/id.
As the number of oem strings are dynamic, a group "oem" is added to the
device and the strings will be added to the group as string1, string2, ...,
and stringN.
Signed-off-by: Allen Hung <allen_hung@dell.com>
---
drivers/firmware/dmi-id.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 108 insertions(+)
diff --git a/drivers/firmware/dmi-id.c b/drivers/firmware/dmi-id.c
index 44c0139..f284a07 100644
--- a/drivers/firmware/dmi-id.c
+++ b/drivers/firmware/dmi-id.c
@@ -58,6 +58,107 @@ DEFINE_DMI_ATTR_WITH_SHOW(chassis_version, 0444, DMI_CHASSIS_VERSION);
DEFINE_DMI_ATTR_WITH_SHOW(chassis_serial, 0400, DMI_CHASSIS_SERIAL);
DEFINE_DMI_ATTR_WITH_SHOW(chassis_asset_tag, 0444, DMI_CHASSIS_ASSET_TAG);
+static struct attribute *dmi_oem_attrs[] = {
+ NULL,
+};
+
+static const char oem_group[] = "oem";
+
+static struct attribute_group dmi_oem_attr_group = {
+ .attrs = dmi_oem_attrs,
+ .name = oem_group,
+};
+
+static LIST_HEAD(dmi_oem_attrs_list);
+
+struct dmi_oem_attribute {
+ struct device_attribute dev_attr;
+ const char *oem_string;
+ char buf[32];
+ bool is_added:1;
+ struct list_head list;
+};
+
+#define to_dmi_oem_attr(_dev_attr) \
+ container_of(_dev_attr, struct dmi_oem_attribute, dev_attr)
+
+static ssize_t sys_dmi_oem_show(struct device *dev,
+ struct device_attribute *attr,
+ char *page)
+{
+ struct dmi_oem_attribute *oa = to_dmi_oem_attr(attr);
+ ssize_t len;
+
+ strlcpy(page, oa->oem_string, PAGE_SIZE-1);
+ len = strlen(page);
+ page[len++] = '\n';
+ page[len] = 0;
+ return len;
+}
+
+static int __init dmi_id_init_oem_attr_group(void)
+{
+ int i, ret;
+ const struct dmi_device *dev;
+ struct dmi_oem_attribute *oa, *tmp;
+ struct device_attribute dev_attr_tmpl =
+ __ATTR(, 0444, sys_dmi_oem_show, NULL);
+
+ ret = sysfs_create_group(&dmi_dev->kobj, &dmi_oem_attr_group);
+ if (ret)
+ return ret;
+
+ /* All devices with type=DMI_DEV_TYPE_OEM_STRING will be found in
+ * the reverse order of what they were parsed in dmi_scan.c. However,
+ * we do want to expose the OEM strings to sysfs in the same order as
+ * what they were originally parsed. A linked list with 2-pass method
+ * is used here to reverse the reserved order.
+ *
+ * Pass 1: find out all "OEM string" devices and add each "oem string"
+ * to a linked list.
+ */
+ dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, NULL);
+ while (dev) {
+ oa = kzalloc(sizeof(*oa), GFP_KERNEL);
+ if (!oa) {
+ ret = -ENOMEM;
+ goto failed;
+ }
+ oa->dev_attr = dev_attr_tmpl;
+ oa->oem_string = dev->name;
+ list_add(&oa->list, &dmi_oem_attrs_list);
+ dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev);
+ }
+
+ /* Pass 2: traverse the list and add each string as a file to "oem"
+ * group
+ */
+ i = 0;
+ list_for_each_entry(oa, &dmi_oem_attrs_list, list) {
+ snprintf(oa->buf, sizeof(oa->buf), "string%d", ++i);
+ oa->dev_attr.attr.name = oa->buf;
+ ret = sysfs_add_file_to_group(
+ &dmi_dev->kobj, &oa->dev_attr.attr, oem_group);
+ if (ret)
+ goto failed;
+ oa->is_added = 1;
+ }
+
+ return 0;
+
+failed:
+ list_for_each_entry_safe(oa, tmp, &dmi_oem_attrs_list, list) {
+ if (oa->is_added)
+ sysfs_remove_file_from_group(
+ &dmi_dev->kobj, &oa->dev_attr.attr, oem_group);
+ list_del(&oa->list);
+ kfree(oa);
+ }
+ sysfs_remove_group(&dmi_dev->kobj, &dmi_oem_attr_group);
+
+ return ret;
+}
+
static void ascii_filter(char *d, const char *s)
{
/* Filter out characters we don't want to see in the modalias string */
@@ -231,8 +332,15 @@ static int __init dmi_id_init(void)
if (ret)
goto fail_put_dmi_dev;
+ ret = dmi_id_init_oem_attr_group();
+ if (ret)
+ goto fail_dev_unregister;
+
return 0;
+fail_dev_unregister:
+ device_unregister(dmi_dev);
+
fail_put_dmi_dev:
put_device(dmi_dev);
--
2.7.4
next prev parent reply other threads:[~2016-07-14 8:12 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-14 8:01 [PATCH 0/2] dmi-id: export oem strings to sysfs Allen Hung
2016-07-14 8:01 ` [PATCH 1/2] dmi-id: don't free dev structure after calling device_register Allen Hung
2016-07-18 17:09 ` Jean Delvare
2016-07-14 8:01 ` Allen Hung [this message]
2016-07-14 9:16 ` [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting oem strings to sysfs kbuild test robot
2016-07-19 9:03 ` Jean Delvare
2016-07-19 14:47 ` Mario_Limonciello
2016-08-02 13:43 ` Jean Delvare
2016-08-02 18:56 ` Mario_Limonciello
2016-08-15 9:55 ` Allen Hung
2016-08-24 8:05 ` Allen Hung
2016-07-26 21:03 ` Mario_Limonciello
2016-07-29 9:59 ` Allen Hung
2016-07-15 9:42 [PATCH 0/2] dmi-id: export " Allen Hung
2016-07-15 9:42 ` [PATCH 2/2] dmi-id: add dmi/id/oem group for exporting " Allen Hung
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1468483283-84766-3-git-send-email-allen_hung@dell.com \
--to=allen_hung@dell.com \
--cc=jdelvare@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mario_limonciello@dell.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).