master/pdo.c
changeset 627 4793ca94f082
child 792 3778920f61e4
equal deleted inserted replaced
626:a5e838c30733 627:4793ca94f082
       
     1 /******************************************************************************
       
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     9  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    10  *  and/or modify it under the terms of the GNU General Public License
       
    11  *  as published by the Free Software Foundation; either version 2 of the
       
    12  *  License, or (at your option) any later version.
       
    13  *
       
    14  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    15  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  *  GNU General Public License for more details.
       
    18  *
       
    19  *  You should have received a copy of the GNU General Public License
       
    20  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    22  *
       
    23  *  The right to use EtherCAT Technology is granted and comes free of
       
    24  *  charge under condition of compatibility of product made by
       
    25  *  Licensee. People intending to distribute/sell products based on the
       
    26  *  code, have to sign an agreement to guarantee that products using
       
    27  *  software based on IgH EtherCAT master stay compatible with the actual
       
    28  *  EtherCAT specification (which are released themselves as an open
       
    29  *  standard) as the (only) precondition to have the right to use EtherCAT
       
    30  *  Technology, IP and trade marks.
       
    31  *
       
    32  *****************************************************************************/
       
    33 
       
    34 /**
       
    35    \file
       
    36    EtherCAT process data object methods.
       
    37 */
       
    38 
       
    39 /*****************************************************************************/
       
    40 
       
    41 #include <linux/slab.h>
       
    42 
       
    43 #include "pdo.h"
       
    44 
       
    45 /*****************************************************************************/
       
    46 
       
    47 /**
       
    48  * PDO constructor.
       
    49  */
       
    50 
       
    51 void ec_pdo_init(ec_pdo_t *pdo /**< EtherCAT PDO */)
       
    52 {
       
    53     pdo->name = NULL;
       
    54     INIT_LIST_HEAD(&pdo->entries);
       
    55 }
       
    56 
       
    57 /*****************************************************************************/
       
    58 
       
    59 /**
       
    60  * PDO destructor.
       
    61  */
       
    62 
       
    63 void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT PDO */)
       
    64 {
       
    65     ec_pdo_entry_t *entry, *next;
       
    66 
       
    67     // free all PDO entries
       
    68     list_for_each_entry_safe(entry, next, &pdo->entries, list) {
       
    69         list_del(&entry->list);
       
    70         kfree(entry);
       
    71     }
       
    72 }
       
    73 
       
    74 /*****************************************************************************/
       
    75 
       
    76 /**
       
    77  * Makes a deep copy of a PDO.
       
    78  */
       
    79 
       
    80 int ec_pdo_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo)
       
    81 {
       
    82     ec_pdo_entry_t *entry, *other_entry, *next;
       
    83 
       
    84     // make flat copy
       
    85     *pdo = *other_pdo;
       
    86 
       
    87     INIT_LIST_HEAD(&pdo->entries);
       
    88     list_for_each_entry(other_entry, &other_pdo->entries, list) {
       
    89         if (!(entry = (ec_pdo_entry_t *)
       
    90                     kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
       
    91             EC_ERR("Failed to allocate memory for PDO entry copy.\n");
       
    92             goto out_free;
       
    93         }
       
    94 
       
    95         *entry = *other_entry; // flat copy is sufficient
       
    96         list_add_tail(&entry->list, &pdo->entries);
       
    97     }
       
    98 
       
    99     return 0;
       
   100 
       
   101 out_free:
       
   102     list_for_each_entry_safe(entry, next, &pdo->entries, list) {
       
   103         list_del(&entry->list);
       
   104         kfree(entry);
       
   105     }
       
   106     return -1;
       
   107 }
       
   108 
       
   109 /*****************************************************************************/