master/pdo.c
changeset 1313 ed15eef57d5c
parent 1186 ff481f097c97
child 1326 ef907b0b5125
equal deleted inserted replaced
1312:74853e018898 1313:ed15eef57d5c
    37 */
    37 */
    38 
    38 
    39 /*****************************************************************************/
    39 /*****************************************************************************/
    40 
    40 
    41 #include <linux/slab.h>
    41 #include <linux/slab.h>
       
    42 #include <linux/err.h>
    42 
    43 
    43 #include "pdo.h"
    44 #include "pdo.h"
    44 
    45 
    45 /*****************************************************************************/
    46 /*****************************************************************************/
    46 
    47 
    56 }
    57 }
    57 
    58 
    58 /*****************************************************************************/
    59 /*****************************************************************************/
    59 
    60 
    60 /** Pdo copy constructor.
    61 /** Pdo copy constructor.
       
    62  *
       
    63  * \retval  0 Success.
       
    64  * \retval <0 Error code.
    61  */
    65  */
    62 int ec_pdo_init_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo)
    66 int ec_pdo_init_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo)
    63 {
    67 {
       
    68     int ret = 0;
       
    69 
    64     pdo->index = other_pdo->index;
    70     pdo->index = other_pdo->index;
    65     pdo->sync_index = other_pdo->sync_index;
    71     pdo->sync_index = other_pdo->sync_index;
    66     pdo->name = NULL;
    72     pdo->name = NULL;
    67     INIT_LIST_HEAD(&pdo->entries);
    73     INIT_LIST_HEAD(&pdo->entries);
    68 
    74 
    69     if (ec_pdo_set_name(pdo, other_pdo->name))
    75     ret = ec_pdo_set_name(pdo, other_pdo->name);
       
    76     if (ret < 0)
    70         goto out_return;
    77         goto out_return;
    71 
    78 
    72     if (ec_pdo_copy_entries(pdo, other_pdo))
    79     ret = ec_pdo_copy_entries(pdo, other_pdo);
       
    80     if (ret < 0)
    73         goto out_clear;
    81         goto out_clear;
    74 
    82 
    75     return 0;
    83     return 0;
    76 
    84 
    77 out_clear:
    85 out_clear:
    78     ec_pdo_clear(pdo);
    86     ec_pdo_clear(pdo);
    79 out_return:
    87 out_return:
    80     return -1;
    88     return ret;
    81 }
    89 }
    82 
    90 
    83 /*****************************************************************************/
    91 /*****************************************************************************/
    84 
    92 
    85 /** Pdo destructor.
    93 /** Pdo destructor.
   109 }
   117 }
   110 
   118 
   111 /*****************************************************************************/
   119 /*****************************************************************************/
   112 
   120 
   113 /** Set Pdo name.
   121 /** Set Pdo name.
       
   122  *
       
   123  * \retval  0 Success.
       
   124  * \retval <0 Error code.
   114  */
   125  */
   115 int ec_pdo_set_name(
   126 int ec_pdo_set_name(
   116         ec_pdo_t *pdo, /**< Pdo. */
   127         ec_pdo_t *pdo, /**< Pdo. */
   117         const char *name /**< New name. */
   128         const char *name /**< New name. */
   118         )
   129         )
   126         kfree(pdo->name);
   137         kfree(pdo->name);
   127 
   138 
   128     if (name && (len = strlen(name))) {
   139     if (name && (len = strlen(name))) {
   129         if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) {
   140         if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) {
   130             EC_ERR("Failed to allocate Pdo name.\n");
   141             EC_ERR("Failed to allocate Pdo name.\n");
   131             return -1;
   142             return -ENOMEM;
   132         }
   143         }
   133         memcpy(pdo->name, name, len + 1);
   144         memcpy(pdo->name, name, len + 1);
   134     } else {
   145     } else {
   135         pdo->name = NULL;
   146         pdo->name = NULL;
   136     }
   147     }
   139 }
   150 }
   140 
   151 
   141 /*****************************************************************************/
   152 /*****************************************************************************/
   142 
   153 
   143 /** Add a new Pdo entry to the configuration.
   154 /** Add a new Pdo entry to the configuration.
       
   155  *
       
   156  * \retval Pointer to the added entry, otherwise a ERR_PTR() code.
   144  */
   157  */
   145 ec_pdo_entry_t *ec_pdo_add_entry(
   158 ec_pdo_entry_t *ec_pdo_add_entry(
   146         ec_pdo_t *pdo,
   159         ec_pdo_t *pdo,
   147         uint16_t index,
   160         uint16_t index,
   148         uint8_t subindex,
   161         uint8_t subindex,
   151 {
   164 {
   152     ec_pdo_entry_t *entry;
   165     ec_pdo_entry_t *entry;
   153 
   166 
   154     if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   167     if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   155         EC_ERR("Failed to allocate memory for Pdo entry.\n");
   168         EC_ERR("Failed to allocate memory for Pdo entry.\n");
   156         return NULL;
   169         return ERR_PTR(-ENOMEM);
   157     }
   170     }
   158 
   171 
   159     ec_pdo_entry_init(entry);
   172     ec_pdo_entry_init(entry);
   160     entry->index = index;
   173     entry->index = index;
   161     entry->subindex = subindex;
   174     entry->subindex = subindex;
   165 }
   178 }
   166 
   179 
   167 /*****************************************************************************/
   180 /*****************************************************************************/
   168 
   181 
   169 /** Copy Pdo entries from another Pdo.
   182 /** Copy Pdo entries from another Pdo.
       
   183  *
       
   184  * \retval  0 Success.
       
   185  * \retval <0 Error code.
   170  */
   186  */
   171 int ec_pdo_copy_entries(ec_pdo_t *pdo, const ec_pdo_t *other)
   187 int ec_pdo_copy_entries(ec_pdo_t *pdo, const ec_pdo_t *other)
   172 {
   188 {
   173     ec_pdo_entry_t *entry, *other_entry;
   189     ec_pdo_entry_t *entry, *other_entry;
       
   190     int ret;
   174 
   191 
   175     ec_pdo_clear_entries(pdo);
   192     ec_pdo_clear_entries(pdo);
   176 
   193 
   177     list_for_each_entry(other_entry, &other->entries, list) {
   194     list_for_each_entry(other_entry, &other->entries, list) {
   178         if (!(entry = (ec_pdo_entry_t *)
   195         if (!(entry = (ec_pdo_entry_t *)
   179                     kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   196                     kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
   180             EC_ERR("Failed to allocate memory for Pdo entry copy.\n");
   197             EC_ERR("Failed to allocate memory for Pdo entry copy.\n");
   181             return -1;
   198             return -ENOMEM;
   182         }
   199         }
   183 
   200 
   184         if (ec_pdo_entry_init_copy(entry, other_entry)) {
   201         ret = ec_pdo_entry_init_copy(entry, other_entry);
       
   202         if (ret < 0) {
   185             kfree(entry);
   203             kfree(entry);
   186             return -1;
   204             return ret;
   187         }
   205         }
   188 
   206 
   189         list_add_tail(&entry->list, &pdo->entries);
   207         list_add_tail(&entry->list, &pdo->entries);
   190     }
   208     }
   191 
   209