fp@792: /****************************************************************************** fp@792: * fp@792: * $Id$ fp@792: * fp@1685: * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH fp@792: * fp@792: * This file is part of the IgH EtherCAT Master. fp@792: * fp@1685: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1685: * modify it under the terms of the GNU General Public License version 2, as fp@1685: * published by the Free Software Foundation. fp@1685: * fp@1685: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1685: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1685: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1685: * Public License for more details. fp@1685: * fp@1685: * You should have received a copy of the GNU General Public License along fp@1685: * with the IgH EtherCAT Master; if not, write to the Free Software fp@792: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@792: * fp@1685: * Using the EtherCAT technology and brand is permitted in compliance with fp@1685: * the industrial property and similar rights of Beckhoff Automation GmbH. fp@792: * fp@792: *****************************************************************************/ fp@792: fp@792: /** fp@792: \file fp@879: EtherCAT Pdo list methods. fp@792: */ fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: #include fp@792: fp@792: #include "globals.h" fp@792: #include "pdo.h" fp@792: #include "slave_config.h" fp@792: #include "master.h" fp@792: fp@879: #include "pdo_list.h" fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: /** Pdo list constructor. fp@879: */ fp@879: void ec_pdo_list_init( fp@879: ec_pdo_list_t *pl /**< Pdo list. */ fp@879: ) fp@879: { fp@879: INIT_LIST_HEAD(&pl->list); fp@879: } fp@879: fp@879: /*****************************************************************************/ fp@879: fp@879: /** Pdo list destructor. fp@879: */ fp@879: void ec_pdo_list_clear(ec_pdo_list_t *pl /**< Pdo list. */) fp@879: { fp@879: ec_pdo_list_clear_pdos(pl); fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: /** Clears the list of mapped Pdos. fp@792: */ fp@879: void ec_pdo_list_clear_pdos(ec_pdo_list_t *pl /**< Pdo list. */) fp@792: { fp@792: ec_pdo_t *pdo, *next; fp@792: fp@879: list_for_each_entry_safe(pdo, next, &pl->list, list) { fp@792: list_del_init(&pdo->list); fp@792: ec_pdo_clear(pdo); fp@792: kfree(pdo); fp@792: } fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@814: /** Calculates the total size of the mapped Pdo entries. fp@792: * fp@792: * \retval Data size in byte. fp@792: */ fp@879: uint16_t ec_pdo_list_total_size( fp@879: const ec_pdo_list_t *pl /**< Pdo list. */ fp@792: ) fp@792: { fp@792: unsigned int bit_size; fp@792: const ec_pdo_t *pdo; fp@792: const ec_pdo_entry_t *pdo_entry; fp@792: uint16_t byte_size; fp@792: fp@792: bit_size = 0; fp@879: list_for_each_entry(pdo, &pl->list, list) { fp@792: list_for_each_entry(pdo_entry, &pdo->entries, list) { fp@792: bit_size += pdo_entry->bit_length; fp@792: } fp@792: } fp@792: fp@792: if (bit_size % 8) // round up to full bytes fp@792: byte_size = bit_size / 8 + 1; fp@792: else fp@792: byte_size = bit_size / 8; fp@792: fp@792: return byte_size; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@879: /** Add a new Pdo to the list. fp@842: * fp@842: * \retval >0 Pointer to new Pdo. fp@842: * \retval NULL No memory. fp@842: */ fp@879: ec_pdo_t *ec_pdo_list_add_pdo( fp@879: ec_pdo_list_t *pl, /**< Pdo list. */ fp@844: uint16_t index /**< Pdo index. */ fp@842: ) fp@842: { fp@842: ec_pdo_t *pdo; fp@842: fp@842: if (!(pdo = (ec_pdo_t *) kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) { fp@842: EC_ERR("Failed to allocate memory for Pdo.\n"); fp@842: return NULL; fp@842: } fp@842: fp@842: ec_pdo_init(pdo); fp@842: pdo->index = index; fp@879: list_add_tail(&pdo->list, &pl->list); fp@842: return pdo; fp@842: } fp@842: fp@842: /*****************************************************************************/ fp@842: fp@879: /** Add the copy of an existing Pdo to the list. fp@792: * fp@792: * \return 0 on success, else < 0 fp@792: */ fp@879: int ec_pdo_list_add_pdo_copy( fp@879: ec_pdo_list_t *pl, /**< Pdo list. */ fp@814: const ec_pdo_t *pdo /**< Pdo to add. */ fp@792: ) fp@792: { fp@792: ec_pdo_t *mapped_pdo; fp@792: fp@814: // Pdo already mapped? fp@879: list_for_each_entry(mapped_pdo, &pl->list, list) { fp@792: if (mapped_pdo->index != pdo->index) continue; fp@814: EC_ERR("Pdo 0x%04X is already mapped!\n", pdo->index); fp@792: return -1; fp@792: } fp@792: fp@792: if (!(mapped_pdo = kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) { fp@842: EC_ERR("Failed to allocate Pdo memory.\n"); fp@792: return -1; fp@792: } fp@792: fp@792: if (ec_pdo_init_copy(mapped_pdo, pdo)) { fp@792: kfree(mapped_pdo); fp@792: return -1; fp@792: } fp@792: fp@879: list_add_tail(&mapped_pdo->list, &pl->list); fp@792: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@879: /** Makes a deep copy of another Pdo list. fp@792: * fp@792: * \return 0 on success, else < 0 fp@792: */ fp@879: int ec_pdo_list_copy( fp@879: ec_pdo_list_t *pl, /**< Pdo list. */ fp@879: const ec_pdo_list_t *other /**< Pdo list to copy from. */ fp@792: ) fp@792: { fp@792: ec_pdo_t *other_pdo; fp@792: fp@879: ec_pdo_list_clear_pdos(pl); fp@792: fp@814: // Pdo already mapped? fp@879: list_for_each_entry(other_pdo, &other->list, list) { fp@879: if (ec_pdo_list_add_pdo_copy(pl, other_pdo)) fp@792: return -1; fp@792: } fp@792: fp@792: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@879: /** Compares two Pdo lists. fp@879: * fp@879: * Only the list is compared, not the Pdo entries (i. e. the Pdo fp@1049: * mapping). fp@792: * fp@879: * \retval 1 The given Pdo lists are equal. fp@879: * \retval 0 The given Pdo lists differ. fp@879: */ fp@879: int ec_pdo_list_equal( fp@879: const ec_pdo_list_t *pl1, /**< First list. */ fp@879: const ec_pdo_list_t *pl2 /**< Second list. */ fp@792: ) fp@792: { fp@792: const struct list_head *h1, *h2, *l1, *l2; fp@792: const ec_pdo_t *p1, *p2; fp@792: fp@879: h1 = l1 = &pl1->list; fp@879: h2 = l2 = &pl2->list; fp@792: fp@792: while (1) { fp@792: l1 = l1->next; fp@792: l2 = l2->next; fp@792: fp@792: if ((l1 == h1) ^ (l2 == h2)) // unequal lengths fp@792: return 0; fp@826: if (l1 == h1) // both finished fp@792: break; fp@792: fp@792: p1 = list_entry(l1, ec_pdo_t, list); fp@792: p2 = list_entry(l2, ec_pdo_t, list); fp@792: fp@792: if (p1->index != p2->index) fp@792: return 0; fp@792: } fp@792: fp@792: return 1; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@799: fp@842: /** Finds a Pdo with the given index. fp@842: */ fp@879: ec_pdo_t *ec_pdo_list_find_pdo( fp@879: const ec_pdo_list_t *pl, /**< Pdo list. */ fp@799: uint16_t index /**< Pdo index. */ fp@799: ) fp@799: { fp@842: ec_pdo_t *pdo; fp@799: fp@879: list_for_each_entry(pdo, &pl->list, list) { fp@799: if (pdo->index != index) fp@799: continue; fp@799: return pdo; fp@799: } fp@799: fp@799: return NULL; fp@799: } fp@799: fp@799: /*****************************************************************************/ fp@842: fp@842: /** Finds a Pdo with the given index and returns a const pointer. fp@842: */ fp@879: const ec_pdo_t *ec_pdo_list_find_pdo_const( fp@879: const ec_pdo_list_t *pl, /**< Pdo list. */ fp@842: uint16_t index /**< Pdo index. */ fp@842: ) fp@842: { fp@842: const ec_pdo_t *pdo; fp@842: fp@879: list_for_each_entry(pdo, &pl->list, list) { fp@842: if (pdo->index != index) fp@842: continue; fp@842: return pdo; fp@842: } fp@842: fp@842: return NULL; fp@842: } fp@842: fp@842: /*****************************************************************************/ fp@931: fp@932: /** Finds a Pdo via its position in the list. fp@932: * fp@932: * Const version. fp@932: */ fp@932: const ec_pdo_t *ec_pdo_list_find_pdo_by_pos_const( fp@932: const ec_pdo_list_t *pl, /**< Pdo list. */ fp@932: unsigned int pos /**< Position in the list. */ fp@932: ) fp@932: { fp@932: const ec_pdo_t *pdo; fp@932: fp@932: list_for_each_entry(pdo, &pl->list, list) { fp@932: if (pos--) fp@932: continue; fp@932: return pdo; fp@932: } fp@932: fp@932: return NULL; fp@932: } fp@932: fp@932: /*****************************************************************************/ fp@932: fp@1092: /** Get the number of Pdos in the list. fp@1092: * fp@1092: * \return Number of Pdos. fp@931: */ fp@931: unsigned int ec_pdo_list_count( fp@931: const ec_pdo_list_t *pl /**< Pdo list. */ fp@931: ) fp@931: { fp@931: const ec_pdo_t *pdo; fp@931: unsigned int num = 0; fp@931: fp@931: list_for_each_entry(pdo, &pl->list, list) { fp@931: num++; fp@931: } fp@931: fp@931: return num; fp@931: } fp@931: fp@931: /*****************************************************************************/ fp@1051: fp@1051: /** Outputs the Pdos in the list. fp@1051: */ fp@1051: void ec_pdo_list_print( fp@1051: const ec_pdo_list_t *pl /**< Pdo list. */ fp@1051: ) fp@1051: { fp@1051: const ec_pdo_t *pdo; fp@1051: fp@1051: list_for_each_entry(pdo, &pl->list, list) { fp@1051: printk("0x%04X", pdo->index); fp@1051: if (pdo->list.next != &pl->list) fp@1051: printk(" "); fp@1051: } fp@1051: } fp@1051: fp@1051: /*****************************************************************************/