New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
authorFlorian Pose <fp@igh-essen.com>
Tue, 20 Feb 2007 11:28:15 +0000
changeset 576 158c5a3d0a2a
parent 575 9a2121b500b1
child 577 db34078f34cc
New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
master/master.c
master/master.h
master/module.c
script/lsec
--- a/master/master.c	Tue Feb 20 09:43:23 2007 +0000
+++ b/master/master.c	Tue Feb 20 11:28:15 2007 +0000
@@ -101,11 +101,12 @@
 */
 
 int ec_master_init(ec_master_t *master, /**< EtherCAT master */
-                   unsigned int index, /**< master index */
-                   const ec_device_id_t *main_id, /**< ID of main device */
-                   const ec_device_id_t *backup_id, /**< ID of main device */
-                   unsigned int eoeif_count /**< number of EoE interfaces */
-                   )
+        struct kobject *module_kobj, /**< kobject of the master module */
+        unsigned int index, /**< master index */
+        const ec_device_id_t *main_id, /**< ID of main device */
+        const ec_device_id_t *backup_id, /**< ID of main device */
+        unsigned int eoeif_count /**< number of EoE interfaces */
+        )
 {
     ec_eoe_t *eoe, *next_eoe;
     unsigned int i;
@@ -192,13 +193,16 @@
     memset(&master->kobj, 0x00, sizeof(struct kobject));
     kobject_init(&master->kobj);
     master->kobj.ktype = &ktype_ec_master;
-    if (kobject_set_name(&master->kobj, "ethercat%i", index)) {
-        EC_ERR("Failed to set kobj name.\n");
+    master->kobj.parent = module_kobj;
+    
+    if (kobject_set_name(&master->kobj, "master%i", index)) {
+        EC_ERR("Failed to set master kobject name.\n");
         kobject_put(&master->kobj);
         return -1;
     }
+    
     if (kobject_add(&master->kobj)) {
-        EC_ERR("Failed to add master kobj.\n");
+        EC_ERR("Failed to add master kobject.\n");
         kobject_put(&master->kobj);
         return -1;
     }
--- a/master/master.h	Tue Feb 20 09:43:23 2007 +0000
+++ b/master/master.h	Tue Feb 20 11:28:15 2007 +0000
@@ -150,7 +150,7 @@
 /*****************************************************************************/
 
 // master creation/deletion
-int ec_master_init(ec_master_t *, unsigned int,
+int ec_master_init(ec_master_t *, struct kobject *, unsigned int,
         const ec_device_id_t *, const ec_device_id_t *, unsigned int);
 void ec_master_destroy(ec_master_t *);
 
--- a/master/module.c	Tue Feb 20 09:43:23 2007 +0000
+++ b/master/module.c	Tue Feb 20 11:28:15 2007 +0000
@@ -52,7 +52,36 @@
 int __init ec_init_module(void);
 void __exit ec_cleanup_module(void);
 
-/*****************************************************************************/
+ssize_t ec_show_attribute(struct kobject *, struct attribute *, char *);
+
+/*****************************************************************************/
+
+/** \cond */
+
+EC_SYSFS_READ_ATTR(info);
+
+static struct attribute *ec_def_attrs[] = {
+    &attr_info,
+    NULL,
+};
+
+static struct sysfs_ops ec_sysfs_ops = {
+    .show = &ec_show_attribute,
+    .store = NULL 
+};
+
+static struct kobj_type ktype_ec_module = {
+    .release = NULL, // this is ok, because the module can not be unloaded
+                     // if the reference count is greater zero.
+    .sysfs_ops = &ec_sysfs_ops,
+    .default_attrs = ec_def_attrs
+};
+
+/** \endcond */
+
+/*****************************************************************************/
+
+struct kobject ec_kobj; /**< kobject for master module */
 
 static char *main; /**< main devices parameter */
 static char *backup; /**< backup devices parameter */
@@ -97,9 +126,24 @@
 
     EC_INFO("Master driver %s\n", EC_MASTER_VERSION);
 
+    // init kobject and add it to the hierarchy
+    memset(&ec_kobj, 0x00, sizeof(struct kobject));
+    kobject_init(&ec_kobj);
+    ec_kobj.ktype = &ktype_ec_module;
+    
+    if (kobject_set_name(&ec_kobj, "ethercat")) {
+        EC_ERR("Failed to set module kobject name.\n");
+        goto out_put;
+    }
+    
+    if (kobject_add(&ec_kobj)) {
+        EC_ERR("Failed to add module kobject.\n");
+        goto out_put;
+    }
+    
     if (alloc_chrdev_region(&device_number, 0, 1, "EtherCAT")) {
         EC_ERR("Failed to obtain device number!\n");
-        goto out_return;
+        goto out_del;
     }
 
     if (ec_device_id_process_params(main, backup, &main_ids, &backup_ids))
@@ -121,7 +165,7 @@
                 goto out_free_masters;
             }
 
-            if (ec_master_init(master, master_index,
+            if (ec_master_init(master, &ec_kobj, master_index,
                         main_dev_id, backup_dev_id, 0))
                 goto out_free_masters;
 
@@ -153,7 +197,10 @@
     ec_device_id_clear_list(&backup_ids);
 out_cdev:
     unregister_chrdev_region(device_number, 1);
-out_return:
+out_del:
+    kobject_del(&ec_kobj);
+out_put:
+    kobject_put(&ec_kobj);
     return -1;
 }
 
@@ -178,6 +225,8 @@
     ec_device_id_clear_list(&main_ids);
     ec_device_id_clear_list(&backup_ids);
     unregister_chrdev_region(device_number, 1);
+    kobject_del(&ec_kobj);
+    kobject_put(&ec_kobj);
 
     EC_INFO("Master module cleaned up.\n");
 }
@@ -185,6 +234,42 @@
 /*****************************************************************************/
 
 /**
+   Formats module information for SysFS read access.
+   \return number of bytes written
+*/
+
+ssize_t ec_info(char *buffer /**< memory to store data */)
+{
+    off_t off = 0;
+
+    off += sprintf(buffer + off, "\nVersion: %s", ec_master_version_str);
+    off += sprintf(buffer + off, "\n");
+
+    return off;
+}
+
+/*****************************************************************************/
+
+/**
+   Formats attribute data for SysFS read access.
+   \return number of bytes to read
+*/
+
+ssize_t ec_show_attribute(struct kobject *kobj, /**< kobject */
+        struct attribute *attr, /**< attribute */
+        char *buffer /**< memory to store data */
+        )
+{
+    if (attr == &attr_info)
+        return ec_info(buffer);
+
+    return 0;
+}
+
+
+/*****************************************************************************/
+
+/**
    Gets a handle to a certain master.
    \returns pointer to master
 */
--- a/script/lsec	Tue Feb 20 09:43:23 2007 +0000
+++ b/script/lsec	Tue Feb 20 11:28:15 2007 +0000
@@ -44,28 +44,20 @@
 
 my %opt;
 my $master_index;
-my $master_dir;
 my $term_width;
 
 #------------------------------------------------------------------------------
 
 $term_width = &get_terminal_width;
 &get_options;
-&query_master;
+&query;
 exit 0;
 
 #------------------------------------------------------------------------------
 
-sub query_master
-{
-    $master_dir = "/sys/ethercat" . $master_index;
-    &query_slaves;
-}
-
-#------------------------------------------------------------------------------
-
-sub query_slaves
-{
+sub query
+{
+    my $master_dir;
     my $dirhandle;
     my $entry;
     my @slaves;
@@ -77,6 +69,8 @@
     my $fmt;
     my $cols;
 
+    $master_dir = "/sys/ethercat/master" . $master_index;
+
     unless (opendir $dirhandle, $master_dir) {
 		print "Failed to open directory \"$master_dir\".\n";
 		exit 1;