master/pdo.c
changeset 1327 4d179b06dd3c
parent 1326 ef907b0b5125
child 1332 d62633fea8ed
equal deleted inserted replaced
1326:ef907b0b5125 1327:4d179b06dd3c
    36 
    36 
    37 #include "pdo.h"
    37 #include "pdo.h"
    38 
    38 
    39 /*****************************************************************************/
    39 /*****************************************************************************/
    40 
    40 
    41 /** Pdo constructor.
    41 /** PDO constructor.
    42  */
    42  */
    43 void ec_pdo_init(
    43 void ec_pdo_init(
    44         ec_pdo_t *pdo /**< EtherCAT Pdo */
    44         ec_pdo_t *pdo /**< EtherCAT PDO */
    45         )
    45         )
    46 {
    46 {
    47     pdo->sync_index = -1; // not assigned 
    47     pdo->sync_index = -1; // not assigned 
    48     pdo->name = NULL;
    48     pdo->name = NULL;
    49     INIT_LIST_HEAD(&pdo->entries);
    49     INIT_LIST_HEAD(&pdo->entries);
    50 }
    50 }
    51 
    51 
    52 /*****************************************************************************/
    52 /*****************************************************************************/
    53 
    53 
    54 /** Pdo copy constructor.
    54 /** PDO copy constructor.
    55  *
    55  *
    56  * \retval  0 Success.
    56  * \retval  0 Success.
    57  * \retval <0 Error code.
    57  * \retval <0 Error code.
    58  */
    58  */
    59 int ec_pdo_init_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo)
    59 int ec_pdo_init_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo)
    81     return ret;
    81     return ret;
    82 }
    82 }
    83 
    83 
    84 /*****************************************************************************/
    84 /*****************************************************************************/
    85 
    85 
    86 /** Pdo destructor.
    86 /** PDO destructor.
    87  */
    87  */
    88 void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT Pdo. */)
    88 void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT PDO. */)
    89 {
    89 {
    90     if (pdo->name)
    90     if (pdo->name)
    91         kfree(pdo->name);
    91         kfree(pdo->name);
    92 
    92 
    93     ec_pdo_clear_entries(pdo);
    93     ec_pdo_clear_entries(pdo);
    94 }
    94 }
    95 
    95 
    96 /*****************************************************************************/
    96 /*****************************************************************************/
    97 
    97 
    98 /** Clear Pdo entry list.
    98 /** Clear PDO entry list.
    99  */
    99  */
   100 void ec_pdo_clear_entries(ec_pdo_t *pdo /**< EtherCAT Pdo. */)
   100 void ec_pdo_clear_entries(ec_pdo_t *pdo /**< EtherCAT PDO. */)
   101 {
   101 {
   102     ec_pdo_entry_t *entry, *next;
   102     ec_pdo_entry_t *entry, *next;
   103 
   103 
   104     // free all Pdo entries
   104     // free all PDO entries
   105     list_for_each_entry_safe(entry, next, &pdo->entries, list) {
   105     list_for_each_entry_safe(entry, next, &pdo->entries, list) {
   106         list_del(&entry->list);
   106         list_del(&entry->list);
   107         ec_pdo_entry_clear(entry);
   107         ec_pdo_entry_clear(entry);
   108         kfree(entry);
   108         kfree(entry);
   109     }
   109     }
   110 }
   110 }
   111 
   111 
   112 /*****************************************************************************/
   112 /*****************************************************************************/
   113 
   113 
   114 /** Set Pdo name.
   114 /** Set PDO name.
   115  *
   115  *
   116  * \retval  0 Success.
   116  * \retval  0 Success.
   117  * \retval <0 Error code.
   117  * \retval <0 Error code.
   118  */
   118  */
   119 int ec_pdo_set_name(
   119 int ec_pdo_set_name(
   120         ec_pdo_t *pdo, /**< Pdo. */
   120         ec_pdo_t *pdo, /**< PDO. */
   121         const char *name /**< New name. */
   121         const char *name /**< New name. */
   122         )
   122         )
   123 {
   123 {
   124     unsigned int len;
   124     unsigned int len;
   125 
   125 
   129     if (pdo->name)
   129     if (pdo->name)
   130         kfree(pdo->name);
   130         kfree(pdo->name);
   131 
   131 
   132     if (name && (len = strlen(name))) {
   132     if (name && (len = strlen(name))) {
   133         if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) {
   133         if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) {
   134             EC_ERR("Failed to allocate Pdo name.\n");
   134             EC_ERR("Failed to allocate PDO name.\n");
   135             return -ENOMEM;
   135             return -ENOMEM;
   136         }
   136         }
   137         memcpy(pdo->name, name, len + 1);
   137         memcpy(pdo->name, name, len + 1);
   138     } else {
   138     } else {
   139         pdo->name = NULL;
   139         pdo->name = NULL;
   142     return 0;
   142     return 0;
   143 }
   143 }
   144 
   144 
   145 /*****************************************************************************/
   145 /*****************************************************************************/
   146 
   146 
   147 /** Add a new Pdo entry to the configuration.
   147 /** Add a new PDO entry to the configuration.
   148  *
   148  *
   149  * \retval Pointer to the added entry, otherwise a ERR_PTR() code.
   149  * \retval Pointer to the added entry, otherwise a ERR_PTR() code.
   150  */
   150  */
   151 ec_pdo_entry_t *ec_pdo_add_entry(
   151 ec_pdo_entry_t *ec_pdo_add_entry(
   152         ec_pdo_t *pdo,
   152         ec_pdo_t *pdo,
   156         )
   156         )
   157 {
   157 {
   158     ec_pdo_entry_t *entry;
   158     ec_pdo_entry_t *entry;
   159 
   159 
   160     if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   160     if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   161         EC_ERR("Failed to allocate memory for Pdo entry.\n");
   161         EC_ERR("Failed to allocate memory for PDO entry.\n");
   162         return ERR_PTR(-ENOMEM);
   162         return ERR_PTR(-ENOMEM);
   163     }
   163     }
   164 
   164 
   165     ec_pdo_entry_init(entry);
   165     ec_pdo_entry_init(entry);
   166     entry->index = index;
   166     entry->index = index;
   170     return entry;
   170     return entry;
   171 }
   171 }
   172 
   172 
   173 /*****************************************************************************/
   173 /*****************************************************************************/
   174 
   174 
   175 /** Copy Pdo entries from another Pdo.
   175 /** Copy PDO entries from another PDO.
   176  *
   176  *
   177  * \retval  0 Success.
   177  * \retval  0 Success.
   178  * \retval <0 Error code.
   178  * \retval <0 Error code.
   179  */
   179  */
   180 int ec_pdo_copy_entries(ec_pdo_t *pdo, const ec_pdo_t *other)
   180 int ec_pdo_copy_entries(ec_pdo_t *pdo, const ec_pdo_t *other)
   185     ec_pdo_clear_entries(pdo);
   185     ec_pdo_clear_entries(pdo);
   186 
   186 
   187     list_for_each_entry(other_entry, &other->entries, list) {
   187     list_for_each_entry(other_entry, &other->entries, list) {
   188         if (!(entry = (ec_pdo_entry_t *)
   188         if (!(entry = (ec_pdo_entry_t *)
   189                     kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   189                     kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   190             EC_ERR("Failed to allocate memory for Pdo entry copy.\n");
   190             EC_ERR("Failed to allocate memory for PDO entry copy.\n");
   191             return -ENOMEM;
   191             return -ENOMEM;
   192         }
   192         }
   193 
   193 
   194         ret = ec_pdo_entry_init_copy(entry, other_entry);
   194         ret = ec_pdo_entry_init_copy(entry, other_entry);
   195         if (ret < 0) {
   195         if (ret < 0) {
   203     return 0;
   203     return 0;
   204 }
   204 }
   205 
   205 
   206 /*****************************************************************************/
   206 /*****************************************************************************/
   207 
   207 
   208 /** Compares the entries of two Pdos.
   208 /** Compares the entries of two PDOs.
   209  *
   209  *
   210  * \retval 1 The entries of the given Pdos are equal.
   210  * \retval 1 The entries of the given PDOs are equal.
   211  * \retval 0 The entries of the given Pdos differ.
   211  * \retval 0 The entries of the given PDOs differ.
   212  */
   212  */
   213 int ec_pdo_equal_entries(
   213 int ec_pdo_equal_entries(
   214         const ec_pdo_t *pdo1, /**< First Pdo. */
   214         const ec_pdo_t *pdo1, /**< First PDO. */
   215         const ec_pdo_t *pdo2 /**< Second Pdo. */
   215         const ec_pdo_t *pdo2 /**< Second PDO. */
   216         )
   216         )
   217 {
   217 {
   218     const struct list_head *head1, *head2, *item1, *item2;
   218     const struct list_head *head1, *head2, *item1, *item2;
   219     const ec_pdo_entry_t *entry1, *entry2;
   219     const ec_pdo_entry_t *entry1, *entry2;
   220 
   220 
   239     return 1;
   239     return 1;
   240 }
   240 }
   241 
   241 
   242 /*****************************************************************************/
   242 /*****************************************************************************/
   243 
   243 
   244 /** Get the number of Pdo entries.
   244 /** Get the number of PDO entries.
   245  *
   245  *
   246  * \return Number of Pdo entries.
   246  * \return Number of PDO entries.
   247  */
   247  */
   248 unsigned int ec_pdo_entry_count(
   248 unsigned int ec_pdo_entry_count(
   249         const ec_pdo_t *pdo /**< Pdo. */
   249         const ec_pdo_t *pdo /**< PDO. */
   250         )
   250         )
   251 {
   251 {
   252     const ec_pdo_entry_t *entry;
   252     const ec_pdo_entry_t *entry;
   253     unsigned int num = 0;
   253     unsigned int num = 0;
   254 
   254 
   259     return num;
   259     return num;
   260 }
   260 }
   261 
   261 
   262 /*****************************************************************************/
   262 /*****************************************************************************/
   263 
   263 
   264 /** Finds a Pdo entry via its position in the list.
   264 /** Finds a PDO entry via its position in the list.
   265  *
   265  *
   266  * Const version.
   266  * Const version.
   267  */
   267  */
   268 const ec_pdo_entry_t *ec_pdo_find_entry_by_pos_const(
   268 const ec_pdo_entry_t *ec_pdo_find_entry_by_pos_const(
   269         const ec_pdo_t *pdo, /**< Pdo. */
   269         const ec_pdo_t *pdo, /**< PDO. */
   270         unsigned int pos /**< Position in the list. */
   270         unsigned int pos /**< Position in the list. */
   271         )
   271         )
   272 {
   272 {
   273     const ec_pdo_entry_t *entry;
   273     const ec_pdo_entry_t *entry;
   274 
   274