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@792: /** 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@792: /** Adds a Pdo to the mapping. fp@792: * fp@792: * \return 0 on success, else < 0 fp@792: */ fp@792: int ec_pdo_mapping_add_pdo( fp@792: ec_pdo_mapping_t *pm, /**< Pdo mapping. */ fp@792: const ec_pdo_t *pdo /**< PDO to add. */ fp@792: ) fp@792: { fp@792: ec_pdo_t *mapped_pdo; fp@792: fp@792: // PDO already mapped? fp@792: list_for_each_entry(mapped_pdo, &pm->pdos, list) { fp@792: if (mapped_pdo->index != pdo->index) continue; fp@792: 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@792: EC_ERR("Failed to allocate memory for PDO mapping.\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: /** Add a Pdo to the mapping. fp@792: * fp@792: * The first call of this method will clear the default mapping. fp@792: * fp@792: * \retval 0 Success. fp@792: * \retval -1 Error. fp@792: */ fp@792: int ec_pdo_mapping_add_pdo_info( fp@792: ec_pdo_mapping_t *pm, /**< Pdo mapping. */ fp@792: const ec_pdo_info_t *pdo_info, /**< Pdo information. */ fp@792: const ec_slave_config_t *config /**< Slave configuration, to load fp@792: default entries. */ fp@792: ) fp@792: { fp@792: unsigned int i; fp@792: ec_pdo_t *pdo; fp@792: ec_pdo_entry_t *entry; fp@792: const ec_pdo_entry_info_t *entry_info; fp@792: fp@792: if (pm->default_mapping) { fp@792: pm->default_mapping = 0; fp@792: ec_pdo_mapping_clear_pdos(pm); fp@792: } fp@792: fp@792: if (!(pdo = (ec_pdo_t *) kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) { fp@792: EC_ERR("Failed to allocate memory for Pdo.\n"); fp@792: goto out_return; fp@792: } fp@792: fp@792: ec_pdo_init(pdo); fp@792: pdo->dir = pdo_info->dir; fp@792: pdo->index = pdo_info->index; fp@792: fp@792: if (config->master->debug_level) fp@792: EC_INFO("Adding Pdo 0x%04X to mapping.\n", pdo->index); fp@792: fp@793: if (pdo_info->n_entries && pdo_info->entries) { // configuration provided fp@792: if (config->master->debug_level) fp@792: EC_INFO(" Pdo configuration provided.\n"); fp@792: fp@792: for (i = 0; i < pdo_info->n_entries; i++) { fp@792: entry_info = &pdo_info->entries[i]; fp@792: fp@792: if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { fp@792: EC_ERR("Failed to allocate memory for PDO entry.\n"); fp@792: goto out_free; fp@792: } fp@792: fp@792: ec_pdo_entry_init(entry); fp@792: entry->index = entry_info->index; fp@792: entry->subindex = entry_info->subindex; fp@792: entry->bit_length = entry_info->bit_length; fp@792: list_add_tail(&entry->list, &pdo->entries); fp@792: } fp@792: } else { // use default Pdo configuration fp@792: if (config->master->debug_level) fp@792: EC_INFO(" Using default Pdo configuration.\n"); fp@792: fp@792: if (config->slave) { fp@792: ec_sync_t *sync; fp@792: ec_pdo_t *default_pdo; fp@792: fp@792: if ((sync = ec_slave_get_pdo_sync(config->slave, pdo->dir))) { fp@792: list_for_each_entry(default_pdo, &sync->mapping.pdos, list) { fp@792: if (default_pdo->index != pdo->index) fp@792: continue; fp@792: if (config->master->debug_level) fp@792: EC_INFO(" Found Pdo name \"%s\".\n", fp@792: default_pdo->name); fp@792: // try to take Pdo name from mapped one fp@792: if (ec_pdo_set_name(pdo, default_pdo->name)) fp@792: goto out_free; fp@792: // copy entries (= default Pdo configuration) fp@792: if (ec_pdo_copy_entries(pdo, default_pdo)) fp@792: goto out_free; fp@792: if (config->master->debug_level) { fp@792: const ec_pdo_entry_t *entry; fp@792: list_for_each_entry(entry, &pdo->entries, list) { fp@792: EC_INFO(" Entry 0x%04X:%u.\n", fp@792: entry->index, entry->subindex); fp@792: } fp@792: } fp@792: } fp@792: } else { fp@792: EC_WARN("Slave %u does not provide a default Pdo" fp@792: " configuration!\n", config->slave->ring_position); fp@792: } fp@792: } else { fp@792: EC_WARN("Failed to load default Pdo configuration for %u:%u:" fp@792: " Slave not found.\n", config->alias, config->position); fp@792: } fp@792: } fp@792: fp@792: list_add_tail(&pdo->list, &pm->pdos); fp@792: return 0; fp@792: fp@792: out_free: fp@792: ec_pdo_clear(pdo); fp@792: kfree(pdo); fp@792: out_return: fp@792: return -1; 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@792: 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@792: // PDO already mapped? fp@792: list_for_each_entry(other_pdo, &other->pdos, list) { fp@792: if (ec_pdo_mapping_add_pdo(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@792: if (l1 == h1 && l2 == h2) // 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@799: /** Finds a Pdo with the given index. fp@799: */ fp@799: const 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@799: 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: /*****************************************************************************/