File heci_add_sysfs.diff of Package intel-iamt-heci
diff -Nur a/src/HECI-3.2.0.24/src/heci_main.c b/src/HECI-3.2.0.24/src/heci_main.c
--- a/src/HECI-3.2.0.24/src/heci_main.c 2008-01-24 07:04:22.000000000 -0500
+++ b/src/HECI-3.2.0.24/src/heci_main.c 2008-02-14 11:18:55.000000000 -0500
@@ -55,9 +55,11 @@
#include <linux/kdev_t.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
+#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
+
#include "kcompat.h"
#include "heci.h"
#include "heci_interface.h"
@@ -82,15 +84,18 @@
DEF_PARM(int, debug, 0, 0644, "Debug enabled or not");
#endif
+#define LEGACY_DEV_NAME "iamthif"
+#define HECI_DEV_NAME "heci"
+
/* heci char device for registration */
static struct cdev heci_cdev = {
- .kobj = {.name = "heci", },
+ .kobj = {.name = HECI_DEV_NAME, },
.owner = THIS_MODULE,
};
/* iamt legacy char device for registration */
static struct cdev iamt_legacy_cdev = {
- .kobj = {.name = "iamt_legacy", },
+ .kobj = {.name = LEGACY_DEV_NAME, },
.owner = THIS_MODULE,
};
@@ -208,7 +213,119 @@
return ret;
}
+struct class *heci_class;
+struct class_device *iamt_legacy_class_dev;
+struct class_device *heci_class_dev;
+
+/* Display the version of heci driver. */
+static ssize_t version_show(struct class *dev, char *buf)
+{
+ return sprintf(buf, "%s %s.\n",
+ heci_driver_string, heci_driver_version);
+}
+
+static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
+
+/**
+ * heci_sysfs_create - creates a struct class to contain heci info
+ * @owner - pointer to the module that is to "own" heci sysfs class
+ * @name - pointer to a string for the name of this class
+ *
+ * @return :
+ * valid pointer to a struct class on success
+ * false pointer on failure
+ */
+static struct class *heci_sysfs_create(struct module *owner, char *name)
+{
+ struct class *class;
+ int err = 0;
+
+ class = class_create(owner, name);
+ if (IS_ERR(class)) {
+ err = PTR_ERR(class);
+ goto error;
+ }
+
+ err = class_create_file(class, &class_attr_version);
+ if (err)
+ goto destroy_class;
+
+ return class;
+
+destroy_class:
+ class_destroy(class);
+error:
+ return ERR_PTR(err);
+}
+
+/**
+ * heci_sysfs_destroy - destroys a struct class of heci info
+ * @cs - pointer to the struct class that is to be destroyed
+ *
+ * @return :
+ * none;
+ */
+static void heci_sysfs_destroy(struct class *class)
+{
+ if ((class == NULL) || (IS_ERR(class)))
+ return;
+
+ class_remove_file(class, &class_attr_version);
+ class_destroy(class);
+}
+
+/**
+ * heci_sysfs_device_create - adds two devices to sysfs for chararcter devices
+ * @cs - pointer to the struct class that the device to be registered on
+ *
+ * @return :
+ * 0 on success,
+ * negative on failure
+ */
+static int heci_sysfs_device_create(struct class *cs)
+{
+ int err = 0;
+
+ if ((cs == NULL) || (IS_ERR(cs))) {
+ err = -EINVAL;
+ goto err_out;
+ }
+
+ iamt_legacy_class_dev = class_device_create(cs, NULL,
+ iamt_legacy_cdev.dev,
+ NULL,
+ LEGACY_DEV_NAME);
+ if (IS_ERR(iamt_legacy_class_dev)) {
+ err = PTR_ERR(iamt_legacy_class_dev);
+ goto err_out;
+ }
+
+ heci_class_dev = class_device_create(cs, NULL,
+ heci_cdev.dev,
+ NULL,
+ HECI_DEV_NAME);
+ if (IS_ERR(heci_class_dev)) {
+ class_device_unregister(iamt_legacy_class_dev);
+ err = PTR_ERR(heci_class_dev);
+ }
+err_out:
+ return err;
+}
+
+/**
+ * heci_sysfs_device_remove - unregisters the two device entries on sysfs
+ *
+ * @return :
+ * none;
+ */
+static void heci_sysfs_device_remove(void)
+{
+ if (iamt_legacy_class_dev)
+ class_device_unregister(iamt_legacy_class_dev);
+ if (heci_class_dev)
+ class_device_unregister(heci_class_dev);
+}
/**
* heci_init_module - Driver Registration Routine
@@ -237,20 +354,40 @@
ret = alloc_chrdev_region(&dev, 0, MINORS_COUNT, "heci");
heci_major = MAJOR(dev);
+
+ /* rgisteration in sysfs interface */
+ heci_class = heci_sysfs_create(THIS_MODULE, "heci");
+ if (IS_ERR(heci_class)) {
+ printk(KERN_ERR "HECI: Error creating heci class.\n");
+ ret = PTR_ERR(heci_class);
+ goto unregister;
+ }
+
/* Now registration two cdevs. */
ret = heci_registration_cdev(&iamt_legacy_cdev, LEGACY_MINOR_NUMBER,
&iamt_legacy_fops);
if (ret)
- goto unregister;
+ goto destroy_sysfs;
ret = heci_registration_cdev(&heci_cdev, HECI_MINOR_NUMBER,
&heci_fops);
if (ret) {
cdev_del(&iamt_legacy_cdev);
- goto unregister;
+ goto destroy_sysfs;
+ }
+
+ if (heci_sysfs_device_create(heci_class)) {
+ cdev_del(&iamt_legacy_cdev);
+ cdev_del(&heci_cdev);
+ ret = -EAGAIN;
+ goto destroy_sysfs;
}
+
return ret;
+destroy_sysfs:
+ heci_sysfs_destroy(heci_class);
+
unregister:
pci_unregister_driver(&heci_driver);
unregister_chrdev_region(MKDEV(heci_major, 0), MINORS_COUNT);
@@ -278,6 +415,9 @@
/* Now unregister two cdevs. */
cdev_del(&iamt_legacy_cdev);
cdev_del(&heci_cdev);
+
+ heci_sysfs_device_remove();
+ heci_sysfs_destroy(heci_class);
unregister_chrdev_region(MKDEV(heci_major, 0), MINORS_COUNT);
}