35 |
35 |
36 #include "pdo.h" |
36 #include "pdo.h" |
37 |
37 |
38 /*****************************************************************************/ |
38 /*****************************************************************************/ |
39 |
39 |
40 /** Pdo constructor. |
40 /** PDO constructor. |
41 */ |
41 */ |
42 void ec_pdo_init( |
42 void ec_pdo_init( |
43 ec_pdo_t *pdo /**< EtherCAT Pdo */ |
43 ec_pdo_t *pdo /**< EtherCAT PDO */ |
44 ) |
44 ) |
45 { |
45 { |
46 pdo->sync_index = -1; // not assigned |
46 pdo->sync_index = -1; // not assigned |
47 pdo->name = NULL; |
47 pdo->name = NULL; |
48 INIT_LIST_HEAD(&pdo->entries); |
48 INIT_LIST_HEAD(&pdo->entries); |
49 } |
49 } |
50 |
50 |
51 /*****************************************************************************/ |
51 /*****************************************************************************/ |
52 |
52 |
53 /** Pdo copy constructor. |
53 /** PDO copy constructor. |
54 */ |
54 */ |
55 int ec_pdo_init_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo) |
55 int ec_pdo_init_copy(ec_pdo_t *pdo, const ec_pdo_t *other_pdo) |
56 { |
56 { |
57 pdo->index = other_pdo->index; |
57 pdo->index = other_pdo->index; |
58 pdo->sync_index = other_pdo->sync_index; |
58 pdo->sync_index = other_pdo->sync_index; |
73 return -1; |
73 return -1; |
74 } |
74 } |
75 |
75 |
76 /*****************************************************************************/ |
76 /*****************************************************************************/ |
77 |
77 |
78 /** Pdo destructor. |
78 /** PDO destructor. |
79 */ |
79 */ |
80 void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT Pdo. */) |
80 void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT PDO. */) |
81 { |
81 { |
82 if (pdo->name) |
82 if (pdo->name) |
83 kfree(pdo->name); |
83 kfree(pdo->name); |
84 |
84 |
85 ec_pdo_clear_entries(pdo); |
85 ec_pdo_clear_entries(pdo); |
86 } |
86 } |
87 |
87 |
88 /*****************************************************************************/ |
88 /*****************************************************************************/ |
89 |
89 |
90 /** Clear Pdo entry list. |
90 /** Clear PDO entry list. |
91 */ |
91 */ |
92 void ec_pdo_clear_entries(ec_pdo_t *pdo /**< EtherCAT Pdo. */) |
92 void ec_pdo_clear_entries(ec_pdo_t *pdo /**< EtherCAT PDO. */) |
93 { |
93 { |
94 ec_pdo_entry_t *entry, *next; |
94 ec_pdo_entry_t *entry, *next; |
95 |
95 |
96 // free all Pdo entries |
96 // free all PDO entries |
97 list_for_each_entry_safe(entry, next, &pdo->entries, list) { |
97 list_for_each_entry_safe(entry, next, &pdo->entries, list) { |
98 list_del(&entry->list); |
98 list_del(&entry->list); |
99 ec_pdo_entry_clear(entry); |
99 ec_pdo_entry_clear(entry); |
100 kfree(entry); |
100 kfree(entry); |
101 } |
101 } |
102 } |
102 } |
103 |
103 |
104 /*****************************************************************************/ |
104 /*****************************************************************************/ |
105 |
105 |
106 /** Set Pdo name. |
106 /** Set PDO name. |
107 */ |
107 */ |
108 int ec_pdo_set_name( |
108 int ec_pdo_set_name( |
109 ec_pdo_t *pdo, /**< Pdo. */ |
109 ec_pdo_t *pdo, /**< PDO. */ |
110 const char *name /**< New name. */ |
110 const char *name /**< New name. */ |
111 ) |
111 ) |
112 { |
112 { |
113 unsigned int len; |
113 unsigned int len; |
114 |
114 |
118 if (pdo->name) |
118 if (pdo->name) |
119 kfree(pdo->name); |
119 kfree(pdo->name); |
120 |
120 |
121 if (name && (len = strlen(name))) { |
121 if (name && (len = strlen(name))) { |
122 if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) { |
122 if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) { |
123 EC_ERR("Failed to allocate Pdo name.\n"); |
123 EC_ERR("Failed to allocate PDO name.\n"); |
124 return -1; |
124 return -1; |
125 } |
125 } |
126 memcpy(pdo->name, name, len + 1); |
126 memcpy(pdo->name, name, len + 1); |
127 } else { |
127 } else { |
128 pdo->name = NULL; |
128 pdo->name = NULL; |
143 ) |
143 ) |
144 { |
144 { |
145 ec_pdo_entry_t *entry; |
145 ec_pdo_entry_t *entry; |
146 |
146 |
147 if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { |
147 if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { |
148 EC_ERR("Failed to allocate memory for Pdo entry.\n"); |
148 EC_ERR("Failed to allocate memory for PDO entry.\n"); |
149 return NULL; |
149 return NULL; |
150 } |
150 } |
151 |
151 |
152 ec_pdo_entry_init(entry); |
152 ec_pdo_entry_init(entry); |
153 entry->index = index; |
153 entry->index = index; |
157 return entry; |
157 return entry; |
158 } |
158 } |
159 |
159 |
160 /*****************************************************************************/ |
160 /*****************************************************************************/ |
161 |
161 |
162 /** Copy Pdo entries from another Pdo. |
162 /** Copy PDO entries from another PDO. |
163 */ |
163 */ |
164 int ec_pdo_copy_entries(ec_pdo_t *pdo, const ec_pdo_t *other) |
164 int ec_pdo_copy_entries(ec_pdo_t *pdo, const ec_pdo_t *other) |
165 { |
165 { |
166 ec_pdo_entry_t *entry, *other_entry; |
166 ec_pdo_entry_t *entry, *other_entry; |
167 |
167 |
168 ec_pdo_clear_entries(pdo); |
168 ec_pdo_clear_entries(pdo); |
169 |
169 |
170 list_for_each_entry(other_entry, &other->entries, list) { |
170 list_for_each_entry(other_entry, &other->entries, list) { |
171 if (!(entry = (ec_pdo_entry_t *) |
171 if (!(entry = (ec_pdo_entry_t *) |
172 kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { |
172 kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) { |
173 EC_ERR("Failed to allocate memory for Pdo entry copy.\n"); |
173 EC_ERR("Failed to allocate memory for PDO entry copy.\n"); |
174 return -1; |
174 return -1; |
175 } |
175 } |
176 |
176 |
177 if (ec_pdo_entry_init_copy(entry, other_entry)) { |
177 if (ec_pdo_entry_init_copy(entry, other_entry)) { |
178 kfree(entry); |
178 kfree(entry); |
185 return 0; |
185 return 0; |
186 } |
186 } |
187 |
187 |
188 /*****************************************************************************/ |
188 /*****************************************************************************/ |
189 |
189 |
190 /** Compares the entries of two Pdos. |
190 /** Compares the entries of two PDOs. |
191 * |
191 * |
192 * \retval 1 The entries of the given Pdos are equal. |
192 * \retval 1 The entries of the given PDOs are equal. |
193 * \retval 0 The entries of the given Pdos differ. |
193 * \retval 0 The entries of the given PDOs differ. |
194 */ |
194 */ |
195 int ec_pdo_equal_entries( |
195 int ec_pdo_equal_entries( |
196 const ec_pdo_t *pdo1, /**< First Pdo. */ |
196 const ec_pdo_t *pdo1, /**< First PDO. */ |
197 const ec_pdo_t *pdo2 /**< Second Pdo. */ |
197 const ec_pdo_t *pdo2 /**< Second PDO. */ |
198 ) |
198 ) |
199 { |
199 { |
200 const struct list_head *head1, *head2, *item1, *item2; |
200 const struct list_head *head1, *head2, *item1, *item2; |
201 const ec_pdo_entry_t *entry1, *entry2; |
201 const ec_pdo_entry_t *entry1, *entry2; |
202 |
202 |
221 return 1; |
221 return 1; |
222 } |
222 } |
223 |
223 |
224 /*****************************************************************************/ |
224 /*****************************************************************************/ |
225 |
225 |
226 /** Get the number of Pdo entries. |
226 /** Get the number of PDO entries. |
227 * |
227 * |
228 * \return Number of Pdo entries. |
228 * \return Number of PDO entries. |
229 */ |
229 */ |
230 unsigned int ec_pdo_entry_count( |
230 unsigned int ec_pdo_entry_count( |
231 const ec_pdo_t *pdo /**< Pdo. */ |
231 const ec_pdo_t *pdo /**< PDO. */ |
232 ) |
232 ) |
233 { |
233 { |
234 const ec_pdo_entry_t *entry; |
234 const ec_pdo_entry_t *entry; |
235 unsigned int num = 0; |
235 unsigned int num = 0; |
236 |
236 |
241 return num; |
241 return num; |
242 } |
242 } |
243 |
243 |
244 /*****************************************************************************/ |
244 /*****************************************************************************/ |
245 |
245 |
246 /** Finds a Pdo entry via its position in the list. |
246 /** Finds a PDO entry via its position in the list. |
247 * |
247 * |
248 * Const version. |
248 * Const version. |
249 */ |
249 */ |
250 const ec_pdo_entry_t *ec_pdo_find_entry_by_pos_const( |
250 const ec_pdo_entry_t *ec_pdo_find_entry_by_pos_const( |
251 const ec_pdo_t *pdo, /**< Pdo. */ |
251 const ec_pdo_t *pdo, /**< PDO. */ |
252 unsigned int pos /**< Position in the list. */ |
252 unsigned int pos /**< Position in the list. */ |
253 ) |
253 ) |
254 { |
254 { |
255 const ec_pdo_entry_t *entry; |
255 const ec_pdo_entry_t *entry; |
256 |
256 |