fp@792: /****************************************************************************** fp@792: * fp@792: * $Id$ fp@792: * fp@1326: * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH fp@792: * fp@792: * This file is part of the IgH EtherCAT Master. fp@792: * fp@1326: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1326: * modify it under the terms of the GNU General Public License version 2, as fp@1326: * published by the Free Software Foundation. fp@1326: * fp@1326: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1326: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1326: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1326: * Public License for more details. fp@1326: * fp@1326: * You should have received a copy of the GNU General Public License along fp@1326: * 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@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@792: * fp@792: *****************************************************************************/ fp@792: fp@792: /** fp@792: \file fp@1327: 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@1327: /** PDO list constructor. fp@879: */ fp@879: void ec_pdo_list_init( fp@1327: 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@1327: /** PDO list destructor. fp@1327: */ fp@1327: 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@1327: /** Clears the list of mapped PDOs. fp@1327: */ fp@1327: 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@1327: /** 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@1327: 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@1327: /** Add a new PDO to the list. fp@1327: * fp@1327: * \return Pointer to new PDO, otherwise an ERR_PTR() code. fp@842: */ fp@879: ec_pdo_t *ec_pdo_list_add_pdo( fp@1327: ec_pdo_list_t *pl, /**< PDO list. */ fp@1327: 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@1327: EC_ERR("Failed to allocate memory for PDO.\n"); fp@1313: return ERR_PTR(-ENOMEM); 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@1327: /** 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@1327: ec_pdo_list_t *pl, /**< PDO list. */ fp@1327: const ec_pdo_t *pdo /**< PDO to add. */ fp@792: ) fp@792: { fp@792: ec_pdo_t *mapped_pdo; fp@1313: int ret; fp@792: fp@1327: // PDO already mapped? fp@879: list_for_each_entry(mapped_pdo, &pl->list, list) { fp@792: if (mapped_pdo->index != pdo->index) continue; fp@1327: EC_ERR("PDO 0x%04X is already mapped!\n", pdo->index); fp@1313: return -EEXIST; fp@792: } fp@2421: fp@792: if (!(mapped_pdo = kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) { fp@1327: EC_ERR("Failed to allocate PDO memory.\n"); fp@1313: return -ENOMEM; fp@1313: } fp@1313: fp@1313: ret = ec_pdo_init_copy(mapped_pdo, pdo); fp@1313: if (ret < 0) { fp@792: kfree(mapped_pdo); fp@1313: return ret; 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@1327: /** 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@1327: ec_pdo_list_t *pl, /**< PDO list. */ fp@1327: const ec_pdo_list_t *other /**< PDO list to copy from. */ fp@792: ) fp@792: { fp@792: ec_pdo_t *other_pdo; fp@1313: int ret; fp@792: fp@879: ec_pdo_list_clear_pdos(pl); fp@792: fp@1327: // PDO already mapped? fp@879: list_for_each_entry(other_pdo, &other->list, list) { fp@1313: ret = ec_pdo_list_add_pdo_copy(pl, other_pdo); fp@1313: if (ret) fp@1313: return ret; fp@792: } fp@2421: fp@792: return 0; fp@792: } fp@792: fp@792: /*****************************************************************************/ fp@792: fp@1327: /** Compares two PDO lists. fp@1327: * fp@1327: * Only the list is compared, not the PDO entries (i. e. the PDO fp@1049: * mapping). fp@792: * fp@1327: * \retval 1 The given PDO lists are equal. fp@1327: * \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@1327: /** Finds a PDO with the given index. fp@2522: * fp@2522: * \return Search result, or NULL. fp@842: */ fp@879: ec_pdo_t *ec_pdo_list_find_pdo( fp@1327: const ec_pdo_list_t *pl, /**< PDO list. */ fp@1327: 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@1327: /** Finds a PDO with the given index and returns a const pointer. fp@2522: * fp@2522: * \return Search result, or NULL. fp@842: */ fp@879: const ec_pdo_t *ec_pdo_list_find_pdo_const( fp@1327: const ec_pdo_list_t *pl, /**< PDO list. */ fp@1327: 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@1327: /** Finds a PDO via its position in the list. fp@932: * fp@932: * Const version. fp@2522: * fp@2522: * \return Zero on success, otherwise a negative error code. fp@932: */ fp@932: const ec_pdo_t *ec_pdo_list_find_pdo_by_pos_const( fp@1327: 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@1327: /** Get the number of PDOs in the list. fp@1327: * fp@1327: * \return Number of PDOs. fp@931: */ fp@931: unsigned int ec_pdo_list_count( fp@1327: 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@1327: /** Outputs the PDOs in the list. fp@1051: */ fp@1051: void ec_pdo_list_print( fp@1327: const ec_pdo_list_t *pl /**< PDO list. */ fp@1051: ) fp@1051: { fp@1051: const ec_pdo_t *pdo; fp@1051: fp@1823: if (list_empty(&pl->list)) { fp@1823: printk("(none)"); fp@1823: } else { fp@1823: list_for_each_entry(pdo, &pl->list, list) { fp@1823: printk("0x%04X", pdo->index); fp@1823: if (pdo->list.next != &pl->list) fp@1823: printk(" "); fp@1823: } fp@1823: } fp@1823: } fp@1823: fp@1823: /*****************************************************************************/