1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/******************************************************************************
*
* $Id$
*
* Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
*
* This file is part of the IgH EtherCAT Master.
*
* The IgH EtherCAT Master is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* The IgH EtherCAT Master is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the IgH EtherCAT Master; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* The license mentioned above concerns the source code only. Using the
* EtherCAT technology and brand is only permitted in compliance with the
* industrial property and similar rights of Beckhoff Automation GmbH.
*
*****************************************************************************/
/**
\file
EtherCAT process data object methods.
*/
/*****************************************************************************/
#include <linux/slab.h>
#include <linux/err.h>
#include "pdo.h"
/*****************************************************************************/
/** PDO constructor.
*/
void ec_pdo_init(
ec_pdo_t *pdo /**< EtherCAT PDO */
)
{
pdo->sync_index = -1; // not assigned
pdo->name = NULL;
INIT_LIST_HEAD(&pdo->entries);
}
/*****************************************************************************/
/** PDO copy constructor.
*
* \retval 0 Success.
* \retval <0 Error code.
*/
int ec_pdo_init_copy(
ec_pdo_t *pdo, /**< PDO to create. */
const ec_pdo_t *other_pdo /**< PDO to copy from. */
)
{
int ret = 0;
pdo->index = other_pdo->index;
pdo->sync_index = other_pdo->sync_index;
pdo->name = NULL;
INIT_LIST_HEAD(&pdo->entries);
ret = ec_pdo_set_name(pdo, other_pdo->name);
if (ret < 0)
goto out_return;
ret = ec_pdo_copy_entries(pdo, other_pdo);
if (ret < 0)
goto out_clear;
return 0;
out_clear:
ec_pdo_clear(pdo);
out_return:
return ret;
}
/*****************************************************************************/
/** PDO destructor.
*/
void ec_pdo_clear(ec_pdo_t *pdo /**< EtherCAT PDO. */)
{
if (pdo->name)
kfree(pdo->name);
ec_pdo_clear_entries(pdo);
}
/*****************************************************************************/
/** Clear PDO entry list.
*/
void ec_pdo_clear_entries(ec_pdo_t *pdo /**< EtherCAT PDO. */)
{
ec_pdo_entry_t *entry, *next;
// free all PDO entries
list_for_each_entry_safe(entry, next, &pdo->entries, list) {
list_del(&entry->list);
ec_pdo_entry_clear(entry);
kfree(entry);
}
}
/*****************************************************************************/
/** Set PDO name.
*
* \retval 0 Success.
* \retval <0 Error code.
*/
int ec_pdo_set_name(
ec_pdo_t *pdo, /**< PDO. */
const char *name /**< New name. */
)
{
unsigned int len;
if (pdo->name && name && !strcmp(pdo->name, name))
return 0;
if (pdo->name)
kfree(pdo->name);
if (name && (len = strlen(name))) {
if (!(pdo->name = (char *) kmalloc(len + 1, GFP_KERNEL))) {
EC_ERR("Failed to allocate PDO name.\n");
return -ENOMEM;
}
memcpy(pdo->name, name, len + 1);
} else {
pdo->name = NULL;
}
return 0;
}
/*****************************************************************************/
/** Add a new PDO entry to the configuration.
*
* \retval Pointer to the added entry, otherwise a ERR_PTR() code.
*/
ec_pdo_entry_t *ec_pdo_add_entry(
ec_pdo_t *pdo, /**< PDO. */
uint16_t index, /**< New entry's index. */
uint8_t subindex, /**< New entry's subindex. */
uint8_t bit_length /**< New entry's bit length. */
)
{
ec_pdo_entry_t *entry;
if (!(entry = kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
EC_ERR("Failed to allocate memory for PDO entry.\n");
return ERR_PTR(-ENOMEM);
}
ec_pdo_entry_init(entry);
entry->index = index;
entry->subindex = subindex;
entry->bit_length = bit_length;
list_add_tail(&entry->list, &pdo->entries);
return entry;
}
/*****************************************************************************/
/** Copy PDO entries from another PDO.
*
* \retval 0 Success.
* \retval <0 Error code.
*/
int ec_pdo_copy_entries(
ec_pdo_t *pdo, /**< PDO whos entries shall be replaced. */
const ec_pdo_t *other /**< Pdo with entries to copy. */
)
{
ec_pdo_entry_t *entry, *other_entry;
int ret;
ec_pdo_clear_entries(pdo);
list_for_each_entry(other_entry, &other->entries, list) {
if (!(entry = (ec_pdo_entry_t *)
kmalloc(sizeof(ec_pdo_entry_t), GFP_KERNEL))) {
EC_ERR("Failed to allocate memory for PDO entry copy.\n");
return -ENOMEM;
}
ret = ec_pdo_entry_init_copy(entry, other_entry);
if (ret < 0) {
kfree(entry);
return ret;
}
list_add_tail(&entry->list, &pdo->entries);
}
return 0;
}
/*****************************************************************************/
/** Compares the entries of two PDOs.
*
* \retval 1 The entries of the given PDOs are equal.
* \retval 0 The entries of the given PDOs differ.
*/
int ec_pdo_equal_entries(
const ec_pdo_t *pdo1, /**< First PDO. */
const ec_pdo_t *pdo2 /**< Second PDO. */
)
{
const struct list_head *head1, *head2, *item1, *item2;
const ec_pdo_entry_t *entry1, *entry2;
head1 = item1 = &pdo1->entries;
head2 = item2 = &pdo2->entries;
while (1) {
item1 = item1->next;
item2 = item2->next;
if ((item1 == head1) ^ (item2 == head2)) // unequal lengths
return 0;
if (item1 == head1) // both finished
break;
entry1 = list_entry(item1, ec_pdo_entry_t, list);
entry2 = list_entry(item2, ec_pdo_entry_t, list);
if (!ec_pdo_entry_equal(entry1, entry2))
return 0;
}
return 1;
}
/*****************************************************************************/
/** Get the number of PDO entries.
*
* \return Number of PDO entries.
*/
unsigned int ec_pdo_entry_count(
const ec_pdo_t *pdo /**< PDO. */
)
{
const ec_pdo_entry_t *entry;
unsigned int num = 0;
list_for_each_entry(entry, &pdo->entries, list) {
num++;
}
return num;
}
/*****************************************************************************/
/** Finds a PDO entry via its position in the list.
*
* Const version.
*
* \return Search result, or NULL.
*/
const ec_pdo_entry_t *ec_pdo_find_entry_by_pos_const(
const ec_pdo_t *pdo, /**< PDO. */
unsigned int pos /**< Position in the list. */
)
{
const ec_pdo_entry_t *entry;
list_for_each_entry(entry, &pdo->entries, list) {
if (pos--)
continue;
return entry;
}
return NULL;
}
/*****************************************************************************/
/** Outputs the PDOs in the list.
*/
void ec_pdo_print_entries(
const ec_pdo_t *pdo /**< PDO. */
)
{
const ec_pdo_entry_t *entry;
if (list_empty(&pdo->entries)) {
printk("(none)");
} else {
list_for_each_entry(entry, &pdo->entries, list) {
printk("0x%04X:%02X/%u",
entry->index, entry->subindex, entry->bit_length);
if (entry->list.next != &pdo->entries)
printk(" ");
}
}
}
/*****************************************************************************/
|