From d2c96f488c7d719f1e0f10248d67f95b3ac94f65 Mon Sep 17 00:00:00 2001
From: Florian Pose <fp@igh-essen.com>
Date: Wed, 7 Mar 2007 11:46:02 +0000
Subject: [PATCH] Layed out PDO structures to own files.

---
 master/Kbuild      |   6 +--
 master/Makefile.am |   1 +
 master/pdo.c       | 109 +++++++++++++++++++++++++++++++++++++++++++++
 master/pdo.h       | 102 ++++++++++++++++++++++++++++++++++++++++++
 master/slave.c     |  18 ++------
 master/slave.h     |  47 +------------------
 6 files changed, 220 insertions(+), 63 deletions(-)
 create mode 100644 master/pdo.c
 create mode 100644 master/pdo.h

diff --git a/master/Kbuild b/master/Kbuild
index a0ed6989..1daabc87 100644
--- a/master/Kbuild
+++ b/master/Kbuild
@@ -35,9 +35,9 @@ include $(src)/../config.kbuild
 
 obj-m := ec_master.o
 
-ec_master-objs := module.o master.o device.o device_id.o slave.o datagram.o \
-    domain.o mailbox.o canopen.o ethernet.o fsm_sii.o fsm_change.o fsm_coe.o \
-    fsm_slave.o fsm_master.o xmldev.o
+ec_master-objs := module.o master.o device.o device_id.o pdo.o slave.o \
+    datagram.o domain.o mailbox.o canopen.o ethernet.o fsm_sii.o fsm_change.o \
+    fsm_coe.o fsm_slave.o fsm_master.o xmldev.o
 
 ifeq ($(EC_DBG_IF),1)
 	ec_master-objs += debug.o
diff --git a/master/Makefile.am b/master/Makefile.am
index 24cc9440..83d36447 100644
--- a/master/Makefile.am
+++ b/master/Makefile.am
@@ -38,6 +38,7 @@ EXTRA_DIST = \
 	debug.c	debug.h \
 	device.c device.h \
 	device_id.c device_id.h \
+	pdo.c pdo.h \
 	domain.c domain.h \
 	doxygen.c \
 	ethernet.c ethernet.h \
diff --git a/master/pdo.c b/master/pdo.c
new file mode 100644
index 00000000..3db54bca
--- /dev/null
+++ b/master/pdo.c
@@ -0,0 +1,109 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it
+ *  and/or modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2 of the
+ *  License, or (at your option) any later version.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be
+ *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  The right to use EtherCAT Technology is granted and comes free of
+ *  charge under condition of compatibility of product made by
+ *  Licensee. People intending to distribute/sell products based on the
+ *  code, have to sign an agreement to guarantee that products using
+ *  software based on IgH EtherCAT master stay compatible with the actual
+ *  EtherCAT specification (which are released themselves as an open
+ *  standard) as the (only) precondition to have the right to use EtherCAT
+ *  Technology, IP and trade marks.
+ *
+ *****************************************************************************/
+
+/**
+   \file
+   EtherCAT process data object methods.
+*/
+
+/*****************************************************************************/
+
+#include <linux/slab.h>
+
+#include "pdo.h"
+
+/*****************************************************************************/
+
+/**
+ * PDO constructor.
+ */
+
+void ec_pdo_init(ec_pdo_t *pdo /**< EtherCAT PDO */)
+{
+    pdo->name = NULL;
+    INIT_LIST_HEAD(&pdo->entries);
+}
+
+/*****************************************************************************/
+
+/**
+ * PDO destructor.
+ */
+
+void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT PDO */)
+{
+    ec_pdo_entry_t *entry, *next;
+
+    // free all PDO entries
+    list_for_each_entry_safe(entry, next, &pdo->entries, list) {
+        list_del(&entry->list);
+        kfree(entry);
+    }
+}
+
+/*****************************************************************************/
+
+/**
+ * Makes a deep copy of a PDO.
+ */
+
+int ec_pdo_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo)
+{
+    ec_pdo_entry_t *entry, *other_entry, *next;
+
+    // make flat copy
+    *pdo = *other_pdo;
+
+    INIT_LIST_HEAD(&pdo->entries);
+    list_for_each_entry(other_entry, &other_pdo->entries, list) {
+        if (!(entry = (ec_pdo_entry_t *)
+                    kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
+            EC_ERR("Failed to allocate memory for PDO entry copy.\n");
+            goto out_free;
+        }
+
+        *entry = *other_entry; // flat copy is sufficient
+        list_add_tail(&entry->list, &pdo->entries);
+    }
+
+    return 0;
+
+out_free:
+    list_for_each_entry_safe(entry, next, &pdo->entries, list) {
+        list_del(&entry->list);
+        kfree(entry);
+    }
+    return -1;
+}
+
+/*****************************************************************************/
diff --git a/master/pdo.h b/master/pdo.h
new file mode 100644
index 00000000..492fe9e1
--- /dev/null
+++ b/master/pdo.h
@@ -0,0 +1,102 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it
+ *  and/or modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2 of the
+ *  License, or (at your option) any later version.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be
+ *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  The right to use EtherCAT Technology is granted and comes free of
+ *  charge under condition of compatibility of product made by
+ *  Licensee. People intending to distribute/sell products based on the
+ *  code, have to sign an agreement to guarantee that products using
+ *  software based on IgH EtherCAT master stay compatible with the actual
+ *  EtherCAT specification (which are released themselves as an open
+ *  standard) as the (only) precondition to have the right to use EtherCAT
+ *  Technology, IP and trade marks.
+ *
+ *****************************************************************************/
+
+/**
+   \file
+   EtherCAT Process data object structure.
+*/
+
+/*****************************************************************************/
+
+#ifndef _EC_PDO_H_
+#define _EC_PDO_H_
+
+#include <linux/list.h>
+
+#include "globals.h"
+
+/*****************************************************************************/
+
+/**
+ * PDO type.
+ */
+
+typedef enum
+{
+    EC_RX_PDO, /**< Reveive PDO */
+    EC_TX_PDO /**< Transmit PDO */
+}
+ec_pdo_type_t;
+
+/*****************************************************************************/
+
+/**
+ * PDO description.
+ */
+
+typedef struct
+{
+    struct list_head list; /**< list item */
+    ec_pdo_type_t type; /**< PDO type */
+    uint16_t index; /**< PDO index */
+    int8_t sync_index; /**< assigned sync manager */
+    char *name; /**< PDO name */
+    struct list_head entries; /**< entry list */
+}
+ec_pdo_t;
+
+/*****************************************************************************/
+
+/**
+ * PDO entry description.
+ */
+
+typedef struct
+{
+    struct list_head list; /**< list item */
+    uint16_t index; /**< PDO entry index */
+    uint8_t subindex; /**< PDO entry subindex */
+    char *name; /**< entry name */
+    uint8_t bit_length; /**< entry length in bit */
+}
+ec_pdo_entry_t;
+
+/*****************************************************************************/
+
+void ec_pdo_init(ec_pdo_t *);
+void ec_pdo_clear(ec_pdo_t *);
+int ec_pdo_copy(ec_pdo_t *, const ec_pdo_t *);
+
+/*****************************************************************************/
+
+#endif
diff --git a/master/slave.c b/master/slave.c
index eab68e68..00b34938 100644
--- a/master/slave.c
+++ b/master/slave.c
@@ -242,7 +242,6 @@ void ec_slave_clear(struct kobject *kobj /**< kobject of the slave */)
 {
     ec_slave_t *slave;
     ec_pdo_t *pdo, *next_pdo;
-    ec_pdo_entry_t *entry, *next_ent;
     ec_sdo_data_t *sdodata, *next_sdodata;
     unsigned int i;
 
@@ -261,13 +260,7 @@ void ec_slave_clear(struct kobject *kobj /**< kobject of the slave */)
     // free all PDOs
     list_for_each_entry_safe(pdo, next_pdo, &slave->sii_pdos, list) {
         list_del(&pdo->list);
-
-        // free all PDO entries
-        list_for_each_entry_safe(entry, next_ent, &pdo->entries, list) {
-            list_del(&entry->list);
-            kfree(entry);
-        }
-
+        ec_pdo_clear(pdo);
         kfree(pdo);
     }
 
@@ -532,14 +525,12 @@ int ec_slave_fetch_sii_pdos(
             return -1;
         }
 
-        INIT_LIST_HEAD(&pdo->entries);
+        ec_pdo_init(pdo);
         pdo->type = pdo_type;
-
         pdo->index = EC_READ_U16(data);
         entry_count = EC_READ_U8(data + 2);
         pdo->sync_index = EC_READ_U8(data + 3);
         pdo->name = ec_slave_sii_string(slave, EC_READ_U8(data + 5));
-
         list_add_tail(&pdo->list, &slave->sii_pdos);
 
         word_count -= 4;
@@ -555,7 +546,6 @@ int ec_slave_fetch_sii_pdos(
             entry->subindex = EC_READ_U8(data + 2);
             entry->name = ec_slave_sii_string(slave, EC_READ_U8(data + 3));
             entry->bit_length = EC_READ_U8(data + 5);
-
             list_add_tail(&entry->list, &pdo->entries);
 
             word_count -= 4;
@@ -1071,8 +1061,8 @@ uint16_t ec_slave_calc_sync_size(const ec_slave_t *slave,
                                  /**< sync manager */
                                  )
 {
-    ec_pdo_t *pdo;
-    ec_pdo_entry_t *pdo_entry;
+    const ec_pdo_t *pdo;
+    const ec_pdo_entry_t *pdo_entry;
     unsigned int bit_size, byte_size;
 
     if (sync->length) return sync->length;
diff --git a/master/slave.h b/master/slave.h
index 36d49716..bd95314c 100644
--- a/master/slave.h
+++ b/master/slave.h
@@ -48,6 +48,7 @@
 
 #include "globals.h"
 #include "datagram.h"
+#include "pdo.h"
 
 /*****************************************************************************/
 
@@ -121,52 +122,6 @@ ec_sync_t;
 
 /*****************************************************************************/
 
-/**
-   PDO type.
-*/
-
-typedef enum
-{
-    EC_RX_PDO, /**< Reveive PDO */
-    EC_TX_PDO /**< Transmit PDO */
-}
-ec_pdo_type_t;
-
-/*****************************************************************************/
-
-/**
-   PDO description.
-*/
-
-typedef struct
-{
-    struct list_head list; /**< list item */
-    ec_pdo_type_t type; /**< PDO type */
-    uint16_t index; /**< PDO index */
-    uint8_t sync_index; /**< assigned sync manager */
-    char *name; /**< PDO name */
-    struct list_head entries; /**< entry list */
-}
-ec_pdo_t;
-
-/*****************************************************************************/
-
-/**
-   PDO entry description.
-*/
-
-typedef struct
-{
-    struct list_head list; /**< list item */
-    uint16_t index; /**< PDO index */
-    uint8_t subindex; /**< entry subindex */
-    char *name; /**< entry name */
-    uint8_t bit_length; /**< entry length in bit */
-}
-ec_pdo_entry_t;
-
-/*****************************************************************************/
-
 /**
    FMMU configuration.
 */
-- 
GitLab