fp@792: /****************************************************************************** fp@792: * fp@792: * $Id$ fp@792: * fp@792: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@792: * fp@792: * This file is part of the IgH EtherCAT Master. fp@792: * fp@792: * The IgH EtherCAT Master is free software; you can redistribute it fp@792: * and/or modify it under the terms of the GNU General Public License fp@792: * as published by the Free Software Foundation; either version 2 of the fp@792: * License, or (at your option) any later version. fp@792: * fp@792: * The IgH EtherCAT Master is distributed in the hope that it will be fp@792: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@792: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@792: * GNU General Public License for more details. fp@792: * fp@792: * You should have received a copy of the GNU General Public License fp@792: * along 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@792: * The right to use EtherCAT Technology is granted and comes free of fp@792: * charge under condition of compatibility of product made by fp@792: * Licensee. People intending to distribute/sell products based on the fp@792: * code, have to sign an agreement to guarantee that products using fp@792: * software based on IgH EtherCAT master stay compatible with the actual fp@792: * EtherCAT specification (which are released themselves as an open fp@792: * standard) as the (only) precondition to have the right to use EtherCAT fp@792: * Technology, IP and trade marks. fp@792: * fp@792: *****************************************************************************/ fp@792: fp@792: /** fp@792: \file fp@792: EtherCAT Pdo mapping 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@792: #include "pdo_mapping.h" fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: /** Pdo mapping constructor. fp@792: */ fp@792: void ec_pdo_mapping_init( fp@792: ec_pdo_mapping_t *pm /**< Pdo mapping. */ fp@792: ) fp@792: { fp@792: INIT_LIST_HEAD(&pm->pdos); fp@792: pm->default_mapping = 1; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: /** Pdo mapping destructor. fp@792: */ fp@792: void ec_pdo_mapping_clear(ec_pdo_mapping_t *pm /**< Pdo mapping. */) fp@792: { fp@792: ec_pdo_mapping_clear_pdos(pm); fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: /** Clears the list of mapped Pdos. fp@792: */ fp@792: void ec_pdo_mapping_clear_pdos(ec_pdo_mapping_t *pm /**< Pdo mapping. */) fp@792: { fp@792: ec_pdo_t *pdo, *next; fp@792: fp@792: list_for_each_entry_safe(pdo, next, &pm->pdos, 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@792: uint16_t ec_pdo_mapping_total_size( fp@792: const ec_pdo_mapping_t *pm /**< Pdo mapping. */ 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@792: list_for_each_entry(pdo, &pm->pdos, 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@842: /** Add a new Pdo to the mapping. fp@842: * fp@842: * \retval >0 Pointer to new Pdo. fp@842: * \retval NULL No memory. fp@842: */ fp@842: ec_pdo_t *ec_pdo_mapping_add_pdo( fp@842: ec_pdo_mapping_t *pm, /**< Pdo mapping. */ fp@844: ec_direction_t dir, /**< Direction. */ 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->dir = dir; fp@842: pdo->index = index; fp@842: list_add_tail(&pdo->list, &pm->pdos); fp@842: return pdo; fp@842: } fp@842: fp@842: /*****************************************************************************/ fp@842: fp@842: /** Add the copy of an existing Pdo to the mapping. fp@792: * fp@792: * \return 0 on success, else < 0 fp@792: */ fp@838: int ec_pdo_mapping_add_pdo_copy( fp@792: ec_pdo_mapping_t *pm, /**< Pdo mapping. */ 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@792: list_for_each_entry(mapped_pdo, &pm->pdos, 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@792: list_add_tail(&mapped_pdo->list, &pm->pdos); fp@792: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: /** Makes a deep copy of another Pdo mapping. fp@792: * fp@792: * \return 0 on success, else < 0 fp@792: */ fp@792: int ec_pdo_mapping_copy( fp@792: ec_pdo_mapping_t *pm, /**< Pdo mapping. */ fp@814: const ec_pdo_mapping_t *other /**< Pdo mapping to copy from. */ fp@792: ) fp@792: { fp@792: ec_pdo_t *other_pdo; fp@792: fp@792: ec_pdo_mapping_clear_pdos(pm); fp@792: fp@814: // Pdo already mapped? fp@792: list_for_each_entry(other_pdo, &other->pdos, list) { fp@838: if (ec_pdo_mapping_add_pdo_copy(pm, other_pdo)) fp@792: return -1; fp@792: } fp@792: fp@792: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@792: /** Compares two Pdo mappings. fp@792: * fp@792: * Only the mapping is compared, not the Pdo entries (i. e. the Pdo fp@792: * configuration). fp@792: * fp@792: * \retval 1 The given Pdo mappings are equal. fp@792: * \retval 0 The given Pdo mappings differ. fp@792: */ fp@792: int ec_pdo_mapping_equal( fp@792: const ec_pdo_mapping_t *pm1, /**< First mapping. */ fp@792: const ec_pdo_mapping_t *pm2 /**< Second mapping. */ 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@792: h1 = l1 = &pm1->pdos; fp@792: h2 = l2 = &pm2->pdos; 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@842: ec_pdo_t *ec_pdo_mapping_find_pdo( fp@799: const ec_pdo_mapping_t *pm, /**< Pdo mapping. */ fp@799: uint16_t index /**< Pdo index. */ fp@799: ) fp@799: { fp@842: ec_pdo_t *pdo; fp@799: fp@799: list_for_each_entry(pdo, &pm->pdos, 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@842: const ec_pdo_t *ec_pdo_mapping_find_pdo_const( fp@842: const ec_pdo_mapping_t *pm, /**< Pdo mapping. */ fp@842: uint16_t index /**< Pdo index. */ fp@842: ) fp@842: { fp@842: const ec_pdo_t *pdo; fp@842: fp@842: list_for_each_entry(pdo, &pm->pdos, 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: /*****************************************************************************/