master/pdo_list.c
changeset 1327 4d179b06dd3c
parent 1326 ef907b0b5125
child 1363 11c0b2caa253
equal deleted inserted replaced
1326:ef907b0b5125 1327:4d179b06dd3c
    24  *
    24  *
    25  *****************************************************************************/
    25  *****************************************************************************/
    26 
    26 
    27 /**
    27 /**
    28    \file
    28    \file
    29    EtherCAT Pdo list methods.
    29    EtherCAT PDO list methods.
    30 */
    30 */
    31 
    31 
    32 /*****************************************************************************/
    32 /*****************************************************************************/
    33 
    33 
    34 #include <linux/module.h>
    34 #include <linux/module.h>
    40 
    40 
    41 #include "pdo_list.h"
    41 #include "pdo_list.h"
    42 
    42 
    43 /*****************************************************************************/
    43 /*****************************************************************************/
    44 
    44 
    45 /** Pdo list constructor.
    45 /** PDO list constructor.
    46  */
    46  */
    47 void ec_pdo_list_init(
    47 void ec_pdo_list_init(
    48         ec_pdo_list_t *pl /**< Pdo list. */
    48         ec_pdo_list_t *pl /**< PDO list. */
    49         )
    49         )
    50 {
    50 {
    51     INIT_LIST_HEAD(&pl->list);
    51     INIT_LIST_HEAD(&pl->list);
    52 }
    52 }
    53 
    53 
    54 /*****************************************************************************/
    54 /*****************************************************************************/
    55 
    55 
    56 /** Pdo list destructor.
    56 /** PDO list destructor.
    57  */
    57  */
    58 void ec_pdo_list_clear(ec_pdo_list_t *pl /**< Pdo list. */)
    58 void ec_pdo_list_clear(ec_pdo_list_t *pl /**< PDO list. */)
    59 {
    59 {
    60     ec_pdo_list_clear_pdos(pl);
    60     ec_pdo_list_clear_pdos(pl);
    61 }
    61 }
    62 
    62 
    63 /*****************************************************************************/
    63 /*****************************************************************************/
    64 
    64 
    65 /** Clears the list of mapped Pdos.
    65 /** Clears the list of mapped PDOs.
    66  */
    66  */
    67 void ec_pdo_list_clear_pdos(ec_pdo_list_t *pl /**< Pdo list. */)
    67 void ec_pdo_list_clear_pdos(ec_pdo_list_t *pl /**< PDO list. */)
    68 {
    68 {
    69     ec_pdo_t *pdo, *next;
    69     ec_pdo_t *pdo, *next;
    70 
    70 
    71     list_for_each_entry_safe(pdo, next, &pl->list, list) {
    71     list_for_each_entry_safe(pdo, next, &pl->list, list) {
    72         list_del_init(&pdo->list);
    72         list_del_init(&pdo->list);
    75     }
    75     }
    76 }
    76 }
    77 
    77 
    78 /*****************************************************************************/
    78 /*****************************************************************************/
    79 
    79 
    80 /** Calculates the total size of the mapped Pdo entries.
    80 /** Calculates the total size of the mapped PDO entries.
    81  *
    81  *
    82  * \retval Data size in byte.
    82  * \retval Data size in byte.
    83  */
    83  */
    84 uint16_t ec_pdo_list_total_size(
    84 uint16_t ec_pdo_list_total_size(
    85         const ec_pdo_list_t *pl /**< Pdo list. */
    85         const ec_pdo_list_t *pl /**< PDO list. */
    86         )
    86         )
    87 {
    87 {
    88     unsigned int bit_size;
    88     unsigned int bit_size;
    89     const ec_pdo_t *pdo;
    89     const ec_pdo_t *pdo;
    90     const ec_pdo_entry_t *pdo_entry;
    90     const ec_pdo_entry_t *pdo_entry;
   105     return byte_size;
   105     return byte_size;
   106 }
   106 }
   107 
   107 
   108 /*****************************************************************************/
   108 /*****************************************************************************/
   109 
   109 
   110 /** Add a new Pdo to the list.
   110 /** Add a new PDO to the list.
   111  *
   111  *
   112  * \return Pointer to new Pdo, otherwise an ERR_PTR() code.
   112  * \return Pointer to new PDO, otherwise an ERR_PTR() code.
   113  */
   113  */
   114 ec_pdo_t *ec_pdo_list_add_pdo(
   114 ec_pdo_t *ec_pdo_list_add_pdo(
   115         ec_pdo_list_t *pl, /**< Pdo list. */
   115         ec_pdo_list_t *pl, /**< PDO list. */
   116         uint16_t index /**< Pdo index. */
   116         uint16_t index /**< PDO index. */
   117         )
   117         )
   118 {
   118 {
   119     ec_pdo_t *pdo;
   119     ec_pdo_t *pdo;
   120 
   120 
   121     if (!(pdo = (ec_pdo_t *) kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) {
   121     if (!(pdo = (ec_pdo_t *) kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) {
   122         EC_ERR("Failed to allocate memory for Pdo.\n");
   122         EC_ERR("Failed to allocate memory for PDO.\n");
   123         return ERR_PTR(-ENOMEM);
   123         return ERR_PTR(-ENOMEM);
   124     }
   124     }
   125 
   125 
   126     ec_pdo_init(pdo);
   126     ec_pdo_init(pdo);
   127     pdo->index = index;
   127     pdo->index = index;
   129     return pdo;
   129     return pdo;
   130 }
   130 }
   131 
   131 
   132 /*****************************************************************************/
   132 /*****************************************************************************/
   133 
   133 
   134 /** Add the copy of an existing Pdo to the list.
   134 /** Add the copy of an existing PDO to the list.
   135  *
   135  *
   136  * \return 0 on success, else < 0
   136  * \return 0 on success, else < 0
   137  */
   137  */
   138 int ec_pdo_list_add_pdo_copy(
   138 int ec_pdo_list_add_pdo_copy(
   139         ec_pdo_list_t *pl, /**< Pdo list. */
   139         ec_pdo_list_t *pl, /**< PDO list. */
   140         const ec_pdo_t *pdo /**< Pdo to add. */
   140         const ec_pdo_t *pdo /**< PDO to add. */
   141         )
   141         )
   142 {
   142 {
   143     ec_pdo_t *mapped_pdo;
   143     ec_pdo_t *mapped_pdo;
   144     int ret;
   144     int ret;
   145 
   145 
   146     // Pdo already mapped?
   146     // PDO already mapped?
   147     list_for_each_entry(mapped_pdo, &pl->list, list) {
   147     list_for_each_entry(mapped_pdo, &pl->list, list) {
   148         if (mapped_pdo->index != pdo->index) continue;
   148         if (mapped_pdo->index != pdo->index) continue;
   149         EC_ERR("Pdo 0x%04X is already mapped!\n", pdo->index);
   149         EC_ERR("PDO 0x%04X is already mapped!\n", pdo->index);
   150         return -EEXIST;
   150         return -EEXIST;
   151     }
   151     }
   152     
   152     
   153     if (!(mapped_pdo = kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) {
   153     if (!(mapped_pdo = kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) {
   154         EC_ERR("Failed to allocate Pdo memory.\n");
   154         EC_ERR("Failed to allocate PDO memory.\n");
   155         return -ENOMEM;
   155         return -ENOMEM;
   156     }
   156     }
   157 
   157 
   158     ret = ec_pdo_init_copy(mapped_pdo, pdo);
   158     ret = ec_pdo_init_copy(mapped_pdo, pdo);
   159     if (ret < 0) {
   159     if (ret < 0) {
   165     return 0;
   165     return 0;
   166 }
   166 }
   167 
   167 
   168 /*****************************************************************************/
   168 /*****************************************************************************/
   169 
   169 
   170 /** Makes a deep copy of another Pdo list.
   170 /** Makes a deep copy of another PDO list.
   171  *
   171  *
   172  * \return 0 on success, else < 0
   172  * \return 0 on success, else < 0
   173  */
   173  */
   174 int ec_pdo_list_copy(
   174 int ec_pdo_list_copy(
   175         ec_pdo_list_t *pl, /**< Pdo list. */
   175         ec_pdo_list_t *pl, /**< PDO list. */
   176         const ec_pdo_list_t *other /**< Pdo list to copy from. */
   176         const ec_pdo_list_t *other /**< PDO list to copy from. */
   177         )
   177         )
   178 {
   178 {
   179     ec_pdo_t *other_pdo;
   179     ec_pdo_t *other_pdo;
   180     int ret;
   180     int ret;
   181 
   181 
   182     ec_pdo_list_clear_pdos(pl);
   182     ec_pdo_list_clear_pdos(pl);
   183 
   183 
   184     // Pdo already mapped?
   184     // PDO already mapped?
   185     list_for_each_entry(other_pdo, &other->list, list) {
   185     list_for_each_entry(other_pdo, &other->list, list) {
   186         ret = ec_pdo_list_add_pdo_copy(pl, other_pdo);
   186         ret = ec_pdo_list_add_pdo_copy(pl, other_pdo);
   187         if (ret)
   187         if (ret)
   188             return ret;
   188             return ret;
   189     }
   189     }
   191     return 0;
   191     return 0;
   192 }
   192 }
   193 
   193 
   194 /*****************************************************************************/
   194 /*****************************************************************************/
   195 
   195 
   196 /** Compares two Pdo lists.
   196 /** Compares two PDO lists.
   197  *
   197  *
   198  * Only the list is compared, not the Pdo entries (i. e. the Pdo
   198  * Only the list is compared, not the PDO entries (i. e. the PDO
   199  * mapping).
   199  * mapping).
   200  *
   200  *
   201  * \retval 1 The given Pdo lists are equal.
   201  * \retval 1 The given PDO lists are equal.
   202  * \retval 0 The given Pdo lists differ.
   202  * \retval 0 The given PDO lists differ.
   203  */
   203  */
   204 int ec_pdo_list_equal(
   204 int ec_pdo_list_equal(
   205         const ec_pdo_list_t *pl1, /**< First list. */
   205         const ec_pdo_list_t *pl1, /**< First list. */
   206         const ec_pdo_list_t *pl2 /**< Second list. */
   206         const ec_pdo_list_t *pl2 /**< Second list. */
   207         )
   207         )
   231     return 1;
   231     return 1;
   232 }
   232 }
   233 
   233 
   234 /*****************************************************************************/
   234 /*****************************************************************************/
   235 
   235 
   236 /** Finds a Pdo with the given index.
   236 /** Finds a PDO with the given index.
   237  */
   237  */
   238 ec_pdo_t *ec_pdo_list_find_pdo(
   238 ec_pdo_t *ec_pdo_list_find_pdo(
   239         const ec_pdo_list_t *pl, /**< Pdo list. */
   239         const ec_pdo_list_t *pl, /**< PDO list. */
   240         uint16_t index /**< Pdo index. */
   240         uint16_t index /**< PDO index. */
   241         )
   241         )
   242 {
   242 {
   243     ec_pdo_t *pdo;
   243     ec_pdo_t *pdo;
   244 
   244 
   245     list_for_each_entry(pdo, &pl->list, list) {
   245     list_for_each_entry(pdo, &pl->list, list) {
   251     return NULL;
   251     return NULL;
   252 }
   252 }
   253 
   253 
   254 /*****************************************************************************/
   254 /*****************************************************************************/
   255 
   255 
   256 /** Finds a Pdo with the given index and returns a const pointer.
   256 /** Finds a PDO with the given index and returns a const pointer.
   257  */
   257  */
   258 const ec_pdo_t *ec_pdo_list_find_pdo_const(
   258 const ec_pdo_t *ec_pdo_list_find_pdo_const(
   259         const ec_pdo_list_t *pl, /**< Pdo list. */
   259         const ec_pdo_list_t *pl, /**< PDO list. */
   260         uint16_t index /**< Pdo index. */
   260         uint16_t index /**< PDO index. */
   261         )
   261         )
   262 {
   262 {
   263     const ec_pdo_t *pdo;
   263     const ec_pdo_t *pdo;
   264 
   264 
   265     list_for_each_entry(pdo, &pl->list, list) {
   265     list_for_each_entry(pdo, &pl->list, list) {
   271     return NULL;
   271     return NULL;
   272 }
   272 }
   273 
   273 
   274 /*****************************************************************************/
   274 /*****************************************************************************/
   275 
   275 
   276 /** Finds a Pdo via its position in the list.
   276 /** Finds a PDO via its position in the list.
   277  *
   277  *
   278  * Const version.
   278  * Const version.
   279  */
   279  */
   280 const ec_pdo_t *ec_pdo_list_find_pdo_by_pos_const(
   280 const ec_pdo_t *ec_pdo_list_find_pdo_by_pos_const(
   281         const ec_pdo_list_t *pl, /**< Pdo list. */
   281         const ec_pdo_list_t *pl, /**< PDO list. */
   282         unsigned int pos /**< Position in the list. */
   282         unsigned int pos /**< Position in the list. */
   283         )
   283         )
   284 {
   284 {
   285     const ec_pdo_t *pdo;
   285     const ec_pdo_t *pdo;
   286 
   286 
   293     return NULL;
   293     return NULL;
   294 }
   294 }
   295 
   295 
   296 /*****************************************************************************/
   296 /*****************************************************************************/
   297 
   297 
   298 /** Get the number of Pdos in the list.
   298 /** Get the number of PDOs in the list.
   299  *
   299  *
   300  * \return Number of Pdos.
   300  * \return Number of PDOs.
   301  */
   301  */
   302 unsigned int ec_pdo_list_count(
   302 unsigned int ec_pdo_list_count(
   303         const ec_pdo_list_t *pl /**< Pdo list. */
   303         const ec_pdo_list_t *pl /**< PDO list. */
   304         )
   304         )
   305 {
   305 {
   306     const ec_pdo_t *pdo;
   306     const ec_pdo_t *pdo;
   307     unsigned int num = 0;
   307     unsigned int num = 0;
   308 
   308 
   313     return num;
   313     return num;
   314 }
   314 }
   315 
   315 
   316 /*****************************************************************************/
   316 /*****************************************************************************/
   317 
   317 
   318 /** Outputs the Pdos in the list.
   318 /** Outputs the PDOs in the list.
   319  */
   319  */
   320 void ec_pdo_list_print(
   320 void ec_pdo_list_print(
   321         const ec_pdo_list_t *pl /**< Pdo list. */
   321         const ec_pdo_list_t *pl /**< PDO list. */
   322         )
   322         )
   323 {
   323 {
   324     const ec_pdo_t *pdo;
   324     const ec_pdo_t *pdo;
   325 
   325 
   326     list_for_each_entry(pdo, &pl->list, list) {
   326     list_for_each_entry(pdo, &pl->list, list) {