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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
|
/******************************************************************************
*
* $Id$
*
* Copyright (C) 2006-2012 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.
*
* vim: expandtab
*
*****************************************************************************/
/**
\file
EtherCAT slave configuration methods.
*/
/*****************************************************************************/
#include <linux/module.h>
#include <linux/slab.h>
#include "globals.h"
#include "master.h"
#include "voe_handler.h"
#include "slave_config.h"
/*****************************************************************************/
/** Slave configuration constructor.
*
* See ecrt_master_slave_config() for the usage of the \a alias and \a
* position parameters.
*/
void ec_slave_config_init(
ec_slave_config_t *sc, /**< Slave configuration. */
ec_master_t *master, /**< EtherCAT master. */
uint16_t alias, /**< Slave alias. */
uint16_t position, /**< Slave position. */
uint32_t vendor_id, /**< Expected vendor ID. */
uint32_t product_code /**< Expected product code. */
)
{
unsigned int i;
sc->master = master;
sc->alias = alias;
sc->position = position;
sc->vendor_id = vendor_id;
sc->product_code = product_code;
sc->watchdog_divider = 0; // use default
sc->watchdog_intervals = 0; // use default
sc->slave = NULL;
for (i = 0; i < EC_MAX_SYNC_MANAGERS; i++)
ec_sync_config_init(&sc->sync_configs[i]);
sc->used_fmmus = 0;
sc->dc_assign_activate = 0x0000;
sc->dc_sync[0].cycle_time = 0U;
sc->dc_sync[1].cycle_time = 0;
sc->dc_sync[0].shift_time = 0U;
sc->dc_sync[1].shift_time = 0;
INIT_LIST_HEAD(&sc->sdo_configs);
INIT_LIST_HEAD(&sc->sdo_requests);
INIT_LIST_HEAD(&sc->reg_requests);
INIT_LIST_HEAD(&sc->voe_handlers);
INIT_LIST_HEAD(&sc->soe_configs);
ec_coe_emerg_ring_init(&sc->emerg_ring, sc);
}
/*****************************************************************************/
/** Slave configuration destructor.
*
* Clears and frees a slave configuration object.
*/
void ec_slave_config_clear(
ec_slave_config_t *sc /**< Slave configuration. */
)
{
unsigned int i;
ec_sdo_request_t *req, *next_req;
ec_voe_handler_t *voe, *next_voe;
ec_reg_request_t *reg, *next_reg;
ec_soe_request_t *soe, *next_soe;
ec_slave_config_detach(sc);
// Free sync managers
for (i = 0; i < EC_MAX_SYNC_MANAGERS; i++)
ec_sync_config_clear(&sc->sync_configs[i]);
// free all SDO configurations
list_for_each_entry_safe(req, next_req, &sc->sdo_configs, list) {
list_del(&req->list);
ec_sdo_request_clear(req);
kfree(req);
}
// free all SDO requests
list_for_each_entry_safe(req, next_req, &sc->sdo_requests, list) {
list_del(&req->list);
ec_sdo_request_clear(req);
kfree(req);
}
// free all register requests
list_for_each_entry_safe(reg, next_reg, &sc->reg_requests, list) {
list_del(®->list);
ec_reg_request_clear(reg);
kfree(reg);
}
// free all VoE handlers
list_for_each_entry_safe(voe, next_voe, &sc->voe_handlers, list) {
list_del(&voe->list);
ec_voe_handler_clear(voe);
kfree(voe);
}
// free all SoE configurations
list_for_each_entry_safe(soe, next_soe, &sc->soe_configs, list) {
list_del(&soe->list);
ec_soe_request_clear(soe);
kfree(soe);
}
ec_coe_emerg_ring_clear(&sc->emerg_ring);
}
/*****************************************************************************/
/** Prepares an FMMU configuration.
*
* Configuration data for the FMMU is saved in the slave config structure and
* is written to the slave during the configuration. The FMMU configuration
* is done in a way, that the complete data range of the corresponding sync
* manager is covered. Seperate FMMUs are configured for each domain. If the
* FMMU configuration is already prepared, the function does nothing and
* returns with success.
*
* \retval >=0 Success, logical offset byte address.
* \retval <0 Error code.
*/
int ec_slave_config_prepare_fmmu(
ec_slave_config_t *sc, /**< Slave configuration. */
ec_domain_t *domain, /**< Domain. */
uint8_t sync_index, /**< Sync manager index. */
ec_direction_t dir /**< PDO direction. */
)
{
unsigned int i;
ec_fmmu_config_t *fmmu;
// FMMU configuration already prepared?
for (i = 0; i < sc->used_fmmus; i++) {
fmmu = &sc->fmmu_configs[i];
if (fmmu->domain == domain && fmmu->sync_index == sync_index)
return fmmu->logical_start_address;
}
if (sc->used_fmmus == EC_MAX_FMMUS) {
EC_CONFIG_ERR(sc, "FMMU limit reached!\n");
return -EOVERFLOW;
}
fmmu = &sc->fmmu_configs[sc->used_fmmus++];
down(&sc->master->master_sem);
ec_fmmu_config_init(fmmu, sc, domain, sync_index, dir);
up(&sc->master->master_sem);
return fmmu->logical_start_address;
}
/*****************************************************************************/
/** Attaches the configuration to the addressed slave object.
*
* \retval 0 Success.
* \retval <0 Error code.
*/
int ec_slave_config_attach(
ec_slave_config_t *sc /**< Slave configuration. */
)
{
ec_slave_t *slave;
if (sc->slave)
return 0; // already attached
if (!(slave = ec_master_find_slave(
sc->master, sc->alias, sc->position))) {
EC_CONFIG_DBG(sc, 1, "Failed to find slave for configuration.\n");
return -ENOENT;
}
if (slave->config) {
EC_CONFIG_DBG(sc, 1, "Failed to attach configuration. Slave %u"
" already has a configuration!\n", slave->ring_position);
return -EEXIST;
}
if (
#ifdef EC_IDENT_WILDCARDS
sc->vendor_id != 0xffffffff &&
#endif
slave->sii.vendor_id != sc->vendor_id
) {
EC_CONFIG_DBG(sc, 1, "Slave %u has no matching vendor ID (0x%08X)"
" for configuration (0x%08X).\n",
slave->ring_position, slave->sii.vendor_id, sc->vendor_id);
return -EINVAL;
}
if (
#ifdef EC_IDENT_WILDCARDS
sc->product_code != 0xffffffff &&
#endif
slave->sii.product_code != sc->product_code
) {
EC_CONFIG_DBG(sc, 1, "Slave %u has no matching product code (0x%08X)"
" for configuration (0x%08X).\n",
slave->ring_position, slave->sii.product_code,
sc->product_code);
return -EINVAL;
}
// attach slave
slave->config = sc;
sc->slave = slave;
EC_CONFIG_DBG(sc, 1, "Attached slave %u.\n", slave->ring_position);
return 0;
}
/*****************************************************************************/
/** Detaches the configuration from a slave object.
*/
void ec_slave_config_detach(
ec_slave_config_t *sc /**< Slave configuration. */
)
{
if (sc->slave) {
ec_reg_request_t *reg;
sc->slave->config = NULL;
// invalidate processing register request
list_for_each_entry(reg, &sc->reg_requests, list) {
if (sc->slave->fsm.reg_request == reg) {
sc->slave->fsm.reg_request = NULL;
break;
}
}
sc->slave = NULL;
}
}
/*****************************************************************************/
/** Loads the default PDO assignment from the slave object.
*/
void ec_slave_config_load_default_sync_config(ec_slave_config_t *sc)
{
uint8_t sync_index;
ec_sync_config_t *sync_config;
const ec_sync_t *sync;
if (!sc->slave)
return;
for (sync_index = 0; sync_index < EC_MAX_SYNC_MANAGERS; sync_index++) {
sync_config = &sc->sync_configs[sync_index];
if ((sync = ec_slave_get_sync(sc->slave, sync_index))) {
sync_config->dir = ec_sync_default_direction(sync);
if (sync_config->dir == EC_DIR_INVALID)
EC_SLAVE_WARN(sc->slave,
"SM%u has an invalid direction field!\n", sync_index);
ec_pdo_list_copy(&sync_config->pdos, &sync->pdos);
}
}
}
/*****************************************************************************/
/** Loads the default mapping for a PDO from the slave object.
*/
void ec_slave_config_load_default_mapping(
const ec_slave_config_t *sc,
ec_pdo_t *pdo
)
{
unsigned int i;
const ec_sync_t *sync;
const ec_pdo_t *default_pdo;
if (!sc->slave)
return;
EC_CONFIG_DBG(sc, 1, "Loading default mapping for PDO 0x%04X.\n",
pdo->index);
// find PDO in any sync manager (it could be reassigned later)
for (i = 0; i < sc->slave->sii.sync_count; i++) {
sync = &sc->slave->sii.syncs[i];
list_for_each_entry(default_pdo, &sync->pdos.list, list) {
if (default_pdo->index != pdo->index)
continue;
if (default_pdo->name) {
EC_CONFIG_DBG(sc, 1, "Found PDO name \"%s\".\n",
default_pdo->name);
// take PDO name from assigned one
ec_pdo_set_name(pdo, default_pdo->name);
}
// copy entries (= default PDO mapping)
if (ec_pdo_copy_entries(pdo, default_pdo))
return;
if (sc->master->debug_level) {
const ec_pdo_entry_t *entry;
list_for_each_entry(entry, &pdo->entries, list) {
EC_CONFIG_DBG(sc, 1, "Entry 0x%04X:%02X.\n",
entry->index, entry->subindex);
}
}
return;
}
}
EC_CONFIG_DBG(sc, 1, "No default mapping found.\n");
}
/*****************************************************************************/
/** Get the number of SDO configurations.
*
* \return Number of SDO configurations.
*/
unsigned int ec_slave_config_sdo_count(
const ec_slave_config_t *sc /**< Slave configuration. */
)
{
const ec_sdo_request_t *req;
unsigned int count = 0;
list_for_each_entry(req, &sc->sdo_configs, list) {
count++;
}
return count;
}
/*****************************************************************************/
/** Finds an SDO configuration via its position in the list.
*
* Const version.
*
* \return Search result, or NULL.
*/
const ec_sdo_request_t *ec_slave_config_get_sdo_by_pos_const(
const ec_slave_config_t *sc, /**< Slave configuration. */
unsigned int pos /**< Position in the list. */
)
{
const ec_sdo_request_t *req;
list_for_each_entry(req, &sc->sdo_configs, list) {
if (pos--)
continue;
return req;
}
return NULL;
}
/*****************************************************************************/
/** Get the number of IDN configurations.
*
* \return Number of SDO configurations.
*/
unsigned int ec_slave_config_idn_count(
const ec_slave_config_t *sc /**< Slave configuration. */
)
{
const ec_soe_request_t *req;
unsigned int count = 0;
list_for_each_entry(req, &sc->soe_configs, list) {
count++;
}
return count;
}
/*****************************************************************************/
/** Finds an IDN configuration via its position in the list.
*
* Const version.
*
* \return Search result, or NULL.
*/
const ec_soe_request_t *ec_slave_config_get_idn_by_pos_const(
const ec_slave_config_t *sc, /**< Slave configuration. */
unsigned int pos /**< Position in the list. */
)
{
const ec_soe_request_t *req;
list_for_each_entry(req, &sc->soe_configs, list) {
if (pos--)
continue;
return req;
}
return NULL;
}
/*****************************************************************************/
/** Finds a CoE handler via its position in the list.
*
* \return Search result, or NULL.
*/
ec_sdo_request_t *ec_slave_config_find_sdo_request(
ec_slave_config_t *sc, /**< Slave configuration. */
unsigned int pos /**< Position in the list. */
)
{
ec_sdo_request_t *req;
list_for_each_entry(req, &sc->sdo_requests, list) {
if (pos--)
continue;
return req;
}
return NULL;
}
/*****************************************************************************/
/** Finds a register handler via its position in the list.
*
* \return Search result, or NULL.
*/
ec_reg_request_t *ec_slave_config_find_reg_request(
ec_slave_config_t *sc, /**< Slave configuration. */
unsigned int pos /**< Position in the list. */
)
{
ec_reg_request_t *reg;
list_for_each_entry(reg, &sc->reg_requests, list) {
if (pos--)
continue;
return reg;
}
return NULL;
}
/*****************************************************************************/
/** Finds a VoE handler via its position in the list.
*
* \return Search result, or NULL.
*/
ec_voe_handler_t *ec_slave_config_find_voe_handler(
ec_slave_config_t *sc, /**< Slave configuration. */
unsigned int pos /**< Position in the list. */
)
{
ec_voe_handler_t *voe;
list_for_each_entry(voe, &sc->voe_handlers, list) {
if (pos--)
continue;
return voe;
}
return NULL;
}
/******************************************************************************
* Application interface
*****************************************************************************/
int ecrt_slave_config_sync_manager(ec_slave_config_t *sc, uint8_t sync_index,
ec_direction_t dir, ec_watchdog_mode_t watchdog_mode)
{
ec_sync_config_t *sync_config;
EC_CONFIG_DBG(sc, 1, "ecrt_slave_config_sync_manager(sc = 0x%p,"
" sync_index = %u, dir = %i, watchdog_mode = %i)\n",
sc, sync_index, dir, watchdog_mode);
if (sync_index >= EC_MAX_SYNC_MANAGERS) {
EC_CONFIG_ERR(sc, "Invalid sync manager index %u!\n", sync_index);
return -ENOENT;
}
if (dir != EC_DIR_OUTPUT && dir != EC_DIR_INPUT) {
EC_CONFIG_ERR(sc, "Invalid direction %u!\n", (unsigned int) dir);
return -EINVAL;
}
sync_config = &sc->sync_configs[sync_index];
sync_config->dir = dir;
sync_config->watchdog_mode = watchdog_mode;
return 0;
}
/*****************************************************************************/
void ecrt_slave_config_watchdog(ec_slave_config_t *sc,
uint16_t divider, uint16_t intervals)
{
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, divider = %u, intervals = %u)\n",
__func__, sc, divider, intervals);
sc->watchdog_divider = divider;
sc->watchdog_intervals = intervals;
}
/*****************************************************************************/
int ecrt_slave_config_pdo_assign_add(ec_slave_config_t *sc,
uint8_t sync_index, uint16_t pdo_index)
{
ec_pdo_t *pdo;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, sync_index = %u, "
"pdo_index = 0x%04X)\n", __func__, sc, sync_index, pdo_index);
if (sync_index >= EC_MAX_SYNC_MANAGERS) {
EC_CONFIG_ERR(sc, "Invalid sync manager index %u!\n", sync_index);
return -EINVAL;
}
down(&sc->master->master_sem);
pdo = ec_pdo_list_add_pdo(&sc->sync_configs[sync_index].pdos, pdo_index);
if (IS_ERR(pdo)) {
up(&sc->master->master_sem);
return PTR_ERR(pdo);
}
pdo->sync_index = sync_index;
ec_slave_config_load_default_mapping(sc, pdo);
up(&sc->master->master_sem);
return 0;
}
/*****************************************************************************/
void ecrt_slave_config_pdo_assign_clear(ec_slave_config_t *sc,
uint8_t sync_index)
{
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, sync_index = %u)\n",
__func__, sc, sync_index);
if (sync_index >= EC_MAX_SYNC_MANAGERS) {
EC_CONFIG_ERR(sc, "Invalid sync manager index %u!\n", sync_index);
return;
}
down(&sc->master->master_sem);
ec_pdo_list_clear_pdos(&sc->sync_configs[sync_index].pdos);
up(&sc->master->master_sem);
}
/*****************************************************************************/
int ecrt_slave_config_pdo_mapping_add(ec_slave_config_t *sc,
uint16_t pdo_index, uint16_t entry_index, uint8_t entry_subindex,
uint8_t entry_bit_length)
{
uint8_t sync_index;
ec_pdo_t *pdo = NULL;
ec_pdo_entry_t *entry;
int retval = 0;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, "
"pdo_index = 0x%04X, entry_index = 0x%04X, "
"entry_subindex = 0x%02X, entry_bit_length = %u)\n",
__func__, sc, pdo_index, entry_index, entry_subindex,
entry_bit_length);
for (sync_index = 0; sync_index < EC_MAX_SYNC_MANAGERS; sync_index++)
if ((pdo = ec_pdo_list_find_pdo(
&sc->sync_configs[sync_index].pdos, pdo_index)))
break;
if (pdo) {
down(&sc->master->master_sem);
entry = ec_pdo_add_entry(pdo, entry_index, entry_subindex,
entry_bit_length);
up(&sc->master->master_sem);
if (IS_ERR(entry))
retval = PTR_ERR(entry);
} else {
EC_CONFIG_ERR(sc, "PDO 0x%04X is not assigned.\n", pdo_index);
retval = -ENOENT;
}
return retval;
}
/*****************************************************************************/
void ecrt_slave_config_pdo_mapping_clear(ec_slave_config_t *sc,
uint16_t pdo_index)
{
uint8_t sync_index;
ec_pdo_t *pdo = NULL;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, pdo_index = 0x%04X)\n",
__func__, sc, pdo_index);
for (sync_index = 0; sync_index < EC_MAX_SYNC_MANAGERS; sync_index++)
if ((pdo = ec_pdo_list_find_pdo(
&sc->sync_configs[sync_index].pdos, pdo_index)))
break;
if (pdo) {
down(&sc->master->master_sem);
ec_pdo_clear_entries(pdo);
up(&sc->master->master_sem);
} else {
EC_CONFIG_WARN(sc, "PDO 0x%04X is not assigned.\n", pdo_index);
}
}
/*****************************************************************************/
int ecrt_slave_config_pdos(ec_slave_config_t *sc,
unsigned int n_syncs, const ec_sync_info_t syncs[])
{
int ret;
unsigned int i, j, k;
const ec_sync_info_t *sync_info;
const ec_pdo_info_t *pdo_info;
const ec_pdo_entry_info_t *entry_info;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, n_syncs = %u, syncs = 0x%p)\n",
__func__, sc, n_syncs, syncs);
if (!syncs)
return 0;
for (i = 0; i < n_syncs; i++) {
sync_info = &syncs[i];
if (sync_info->index == (uint8_t) EC_END)
break;
if (sync_info->index >= EC_MAX_SYNC_MANAGERS) {
EC_CONFIG_ERR(sc, "Invalid sync manager index %u!\n",
sync_info->index);
return -ENOENT;
}
ret = ecrt_slave_config_sync_manager(sc, sync_info->index,
sync_info->dir, sync_info->watchdog_mode);
if (ret)
return ret;
ecrt_slave_config_pdo_assign_clear(sc, sync_info->index);
if (sync_info->n_pdos && sync_info->pdos) {
for (j = 0; j < sync_info->n_pdos; j++) {
pdo_info = &sync_info->pdos[j];
ret = ecrt_slave_config_pdo_assign_add(
sc, sync_info->index, pdo_info->index);
if (ret)
return ret;
ecrt_slave_config_pdo_mapping_clear(sc, pdo_info->index);
if (pdo_info->n_entries && pdo_info->entries) {
for (k = 0; k < pdo_info->n_entries; k++) {
entry_info = &pdo_info->entries[k];
ret = ecrt_slave_config_pdo_mapping_add(sc,
pdo_info->index, entry_info->index,
entry_info->subindex,
entry_info->bit_length);
if (ret)
return ret;
}
}
}
}
}
return 0;
}
/*****************************************************************************/
int ecrt_slave_config_reg_pdo_entry(
ec_slave_config_t *sc,
uint16_t index,
uint8_t subindex,
ec_domain_t *domain,
unsigned int *bit_position
)
{
uint8_t sync_index;
const ec_sync_config_t *sync_config;
unsigned int bit_offset, bit_pos;
ec_pdo_t *pdo;
ec_pdo_entry_t *entry;
int sync_offset;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, index = 0x%04X, "
"subindex = 0x%02X, domain = 0x%p, bit_position = 0x%p)\n",
__func__, sc, index, subindex, domain, bit_position);
for (sync_index = 0; sync_index < EC_MAX_SYNC_MANAGERS; sync_index++) {
sync_config = &sc->sync_configs[sync_index];
bit_offset = 0;
list_for_each_entry(pdo, &sync_config->pdos.list, list) {
list_for_each_entry(entry, &pdo->entries, list) {
if (entry->index != index || entry->subindex != subindex) {
bit_offset += entry->bit_length;
} else {
bit_pos = bit_offset % 8;
if (bit_position) {
*bit_position = bit_pos;
} else if (bit_pos) {
EC_CONFIG_ERR(sc, "PDO entry 0x%04X:%02X does"
" not byte-align.\n", index, subindex);
return -EFAULT;
}
sync_offset = ec_slave_config_prepare_fmmu(
sc, domain, sync_index, sync_config->dir);
if (sync_offset < 0)
return sync_offset;
return sync_offset + bit_offset / 8;
}
}
}
}
EC_CONFIG_ERR(sc, "PDO entry 0x%04X:%02X is not mapped.\n",
index, subindex);
return -ENOENT;
}
/*****************************************************************************/
int ecrt_slave_config_reg_pdo_entry_pos(
ec_slave_config_t *sc,
uint8_t sync_index,
unsigned int pdo_pos,
unsigned int entry_pos,
ec_domain_t *domain,
unsigned int *bit_position
)
{
const ec_sync_config_t *sync_config;
unsigned int bit_offset, pp, ep;
ec_pdo_t *pdo;
ec_pdo_entry_t *entry;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, sync_index = %u, pdo_pos = %u,"
" entry_pos = %u, domain = 0x%p, bit_position = 0x%p)\n",
__func__, sc, sync_index, pdo_pos, entry_pos,
domain, bit_position);
if (sync_index >= EC_MAX_SYNC_MANAGERS) {
EC_CONFIG_ERR(sc, "Invalid syncmanager position %u.\n", sync_index);
return -EINVAL;
}
sync_config = &sc->sync_configs[sync_index];
bit_offset = 0;
pp = 0;
list_for_each_entry(pdo, &sync_config->pdos.list, list) {
ep = 0;
list_for_each_entry(entry, &pdo->entries, list) {
if (pp != pdo_pos || ep != entry_pos) {
bit_offset += entry->bit_length;
} else {
unsigned int bit_pos = bit_offset % 8;
int sync_offset;
if (bit_position) {
*bit_position = bit_pos;
} else if (bit_pos) {
EC_CONFIG_ERR(sc, "PDO entry 0x%04X:%02X does"
" not byte-align.\n",
pdo->index, entry->subindex);
return -EFAULT;
}
sync_offset = ec_slave_config_prepare_fmmu(
sc, domain, sync_index, sync_config->dir);
if (sync_offset < 0)
return sync_offset;
return sync_offset + bit_offset / 8;
}
ep++;
}
pp++;
}
EC_CONFIG_ERR(sc, "PDO entry specification %u/%u/%u out of range.\n",
sync_index, pdo_pos, entry_pos);
return -ENOENT;
}
/*****************************************************************************/
void ecrt_slave_config_dc(ec_slave_config_t *sc, uint16_t assign_activate,
uint32_t sync0_cycle_time, int32_t sync0_shift_time,
uint32_t sync1_cycle_time, int32_t sync1_shift_time)
{
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, assign_activate = 0x%04X,"
" sync0_cycle = %u, sync0_shift = %i,"
" sync1_cycle = %u, sync1_shift = %i\n",
__func__, sc, assign_activate, sync0_cycle_time, sync0_shift_time,
sync1_cycle_time, sync1_shift_time);
sc->dc_assign_activate = assign_activate;
sc->dc_sync[0].cycle_time = sync0_cycle_time;
sc->dc_sync[0].shift_time = sync0_shift_time;
sc->dc_sync[1].cycle_time = sync1_cycle_time;
sc->dc_sync[1].shift_time = sync1_shift_time;
}
/*****************************************************************************/
int ecrt_slave_config_sdo(ec_slave_config_t *sc, uint16_t index,
uint8_t subindex, const uint8_t *data, size_t size)
{
ec_slave_t *slave = sc->slave;
ec_sdo_request_t *req;
int ret;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, index = 0x%04X, "
"subindex = 0x%02X, data = 0x%p, size = %zu)\n",
__func__, sc, index, subindex, data, size);
if (slave && !(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
EC_CONFIG_WARN(sc, "Attached slave does not support CoE!\n");
}
if (!(req = (ec_sdo_request_t *)
kmalloc(sizeof(ec_sdo_request_t), GFP_KERNEL))) {
EC_CONFIG_ERR(sc, "Failed to allocate memory for"
" SDO configuration!\n");
return -ENOMEM;
}
ec_sdo_request_init(req);
ecrt_sdo_request_index(req, index, subindex);
ret = ec_sdo_request_copy_data(req, data, size);
if (ret < 0) {
ec_sdo_request_clear(req);
kfree(req);
return ret;
}
down(&sc->master->master_sem);
list_add_tail(&req->list, &sc->sdo_configs);
up(&sc->master->master_sem);
return 0;
}
/*****************************************************************************/
int ecrt_slave_config_sdo8(ec_slave_config_t *sc, uint16_t index,
uint8_t subindex, uint8_t value)
{
uint8_t data[1];
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, index = 0x%04X, "
"subindex = 0x%02X, value = %u)\n",
__func__, sc, index, subindex, (unsigned int) value);
EC_WRITE_U8(data, value);
return ecrt_slave_config_sdo(sc, index, subindex, data, 1);
}
/*****************************************************************************/
int ecrt_slave_config_sdo16(ec_slave_config_t *sc, uint16_t index,
uint8_t subindex, uint16_t value)
{
uint8_t data[2];
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, index = 0x%04X, "
"subindex = 0x%02X, value = %u)\n",
__func__, sc, index, subindex, value);
EC_WRITE_U16(data, value);
return ecrt_slave_config_sdo(sc, index, subindex, data, 2);
}
/*****************************************************************************/
int ecrt_slave_config_sdo32(ec_slave_config_t *sc, uint16_t index,
uint8_t subindex, uint32_t value)
{
uint8_t data[4];
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, index = 0x%04X, "
"subindex = 0x%02X, value = %u)\n",
__func__, sc, index, subindex, value);
EC_WRITE_U32(data, value);
return ecrt_slave_config_sdo(sc, index, subindex, data, 4);
}
/*****************************************************************************/
int ecrt_slave_config_complete_sdo(ec_slave_config_t *sc, uint16_t index,
const uint8_t *data, size_t size)
{
ec_slave_t *slave = sc->slave;
ec_sdo_request_t *req;
int ret;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, index = 0x%04X, "
"data = 0x%p, size = %zu)\n", __func__, sc, index, data, size);
if (slave && !(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
EC_CONFIG_WARN(sc, "Attached slave does not support CoE!\n");
}
if (!(req = (ec_sdo_request_t *)
kmalloc(sizeof(ec_sdo_request_t), GFP_KERNEL))) {
EC_CONFIG_ERR(sc, "Failed to allocate memory for"
" SDO configuration!\n");
return -ENOMEM;
}
ec_sdo_request_init(req);
ecrt_sdo_request_index(req, index, 0);
req->complete_access = 1;
ret = ec_sdo_request_copy_data(req, data, size);
if (ret < 0) {
ec_sdo_request_clear(req);
kfree(req);
return ret;
}
down(&sc->master->master_sem);
list_add_tail(&req->list, &sc->sdo_configs);
up(&sc->master->master_sem);
return 0;
}
/*****************************************************************************/
int ecrt_slave_config_emerg_size(ec_slave_config_t *sc, size_t elements)
{
return ec_coe_emerg_ring_size(&sc->emerg_ring, elements);
}
/*****************************************************************************/
int ecrt_slave_config_emerg_pop(ec_slave_config_t *sc, uint8_t *target)
{
return ec_coe_emerg_ring_pop(&sc->emerg_ring, target);
}
/*****************************************************************************/
int ecrt_slave_config_emerg_clear(ec_slave_config_t *sc)
{
return ec_coe_emerg_ring_clear_ring(&sc->emerg_ring);
}
/*****************************************************************************/
int ecrt_slave_config_emerg_overruns(ec_slave_config_t *sc)
{
return ec_coe_emerg_ring_overruns(&sc->emerg_ring);
}
/*****************************************************************************/
/** Same as ecrt_slave_config_create_sdo_request(), but with ERR_PTR() return
* value.
*/
ec_sdo_request_t *ecrt_slave_config_create_sdo_request_err(
ec_slave_config_t *sc, uint16_t index, uint8_t subindex, size_t size)
{
ec_sdo_request_t *req;
int ret;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, "
"index = 0x%04X, subindex = 0x%02X, size = %zu)\n",
__func__, sc, index, subindex, size);
if (!(req = (ec_sdo_request_t *)
kmalloc(sizeof(ec_sdo_request_t), GFP_KERNEL))) {
EC_CONFIG_ERR(sc, "Failed to allocate SDO request memory!\n");
return ERR_PTR(-ENOMEM);
}
ec_sdo_request_init(req);
ecrt_sdo_request_index(req, index, subindex);
ret = ec_sdo_request_alloc(req, size);
if (ret < 0) {
ec_sdo_request_clear(req);
kfree(req);
return ERR_PTR(ret);
}
// prepare data for optional writing
memset(req->data, 0x00, size);
req->data_size = size;
down(&sc->master->master_sem);
list_add_tail(&req->list, &sc->sdo_requests);
up(&sc->master->master_sem);
return req;
}
/*****************************************************************************/
ec_sdo_request_t *ecrt_slave_config_create_sdo_request(
ec_slave_config_t *sc, uint16_t index, uint8_t subindex, size_t size)
{
ec_sdo_request_t *s = ecrt_slave_config_create_sdo_request_err(sc, index,
subindex, size);
return IS_ERR(s) ? NULL : s;
}
/*****************************************************************************/
/** Same as ecrt_slave_config_create_reg_request(), but with ERR_PTR() return
* value.
*/
ec_reg_request_t *ecrt_slave_config_create_reg_request_err(
ec_slave_config_t *sc, size_t size)
{
ec_reg_request_t *reg;
int ret;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, size = %zu)\n",
__func__, sc, size);
if (!(reg = (ec_reg_request_t *)
kmalloc(sizeof(ec_reg_request_t), GFP_KERNEL))) {
EC_CONFIG_ERR(sc, "Failed to allocate register request memory!\n");
return ERR_PTR(-ENOMEM);
}
ret = ec_reg_request_init(reg, size);
if (ret) {
kfree(reg);
return ERR_PTR(ret);
}
down(&sc->master->master_sem);
list_add_tail(®->list, &sc->reg_requests);
up(&sc->master->master_sem);
return reg;
}
/*****************************************************************************/
ec_reg_request_t *ecrt_slave_config_create_reg_request(
ec_slave_config_t *sc, size_t size)
{
ec_reg_request_t *reg =
ecrt_slave_config_create_reg_request_err(sc, size);
return IS_ERR(reg) ? NULL : reg;
}
/*****************************************************************************/
/** Same as ecrt_slave_config_create_voe_handler(), but with ERR_PTR() return
* value.
*/
ec_voe_handler_t *ecrt_slave_config_create_voe_handler_err(
ec_slave_config_t *sc, size_t size)
{
ec_voe_handler_t *voe;
int ret;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, size = %zu)\n", __func__, sc, size);
if (!(voe = (ec_voe_handler_t *)
kmalloc(sizeof(ec_voe_handler_t), GFP_KERNEL))) {
EC_CONFIG_ERR(sc, "Failed to allocate VoE request memory!\n");
return ERR_PTR(-ENOMEM);
}
ret = ec_voe_handler_init(voe, sc, size);
if (ret < 0) {
kfree(voe);
return ERR_PTR(ret);
}
down(&sc->master->master_sem);
list_add_tail(&voe->list, &sc->voe_handlers);
up(&sc->master->master_sem);
return voe;
}
/*****************************************************************************/
ec_voe_handler_t *ecrt_slave_config_create_voe_handler(
ec_slave_config_t *sc, size_t size)
{
ec_voe_handler_t *voe = ecrt_slave_config_create_voe_handler_err(sc,
size);
return IS_ERR(voe) ? NULL : voe;
}
/*****************************************************************************/
void ecrt_slave_config_state(const ec_slave_config_t *sc,
ec_slave_config_state_t *state)
{
state->online = sc->slave ? 1 : 0;
if (state->online) {
state->operational =
sc->slave->current_state == EC_SLAVE_STATE_OP
&& !sc->slave->force_config;
state->al_state = sc->slave->current_state;
} else {
state->operational = 0;
state->al_state = EC_SLAVE_STATE_UNKNOWN;
}
}
/*****************************************************************************/
int ecrt_slave_config_idn(ec_slave_config_t *sc, uint8_t drive_no,
uint16_t idn, ec_al_state_t state, const uint8_t *data,
size_t size)
{
ec_slave_t *slave = sc->slave;
ec_soe_request_t *req;
int ret;
EC_CONFIG_DBG(sc, 1, "%s(sc = 0x%p, drive_no = %u, idn = 0x%04X, "
"state = %u, data = 0x%p, size = %zu)\n",
__func__, sc, drive_no, idn, state, data, size);
if (drive_no > 7) {
EC_CONFIG_ERR(sc, "Invalid drive number %u!\n",
(unsigned int) drive_no);
return -EINVAL;
}
if (state != EC_AL_STATE_PREOP && state != EC_AL_STATE_SAFEOP) {
EC_CONFIG_ERR(sc, "AL state for IDN config"
" must be PREOP or SAFEOP!\n");
return -EINVAL;
}
if (slave && !(slave->sii.mailbox_protocols & EC_MBOX_SOE)) {
EC_CONFIG_WARN(sc, "Attached slave does not support SoE!\n");
}
if (!(req = (ec_soe_request_t *)
kmalloc(sizeof(ec_soe_request_t), GFP_KERNEL))) {
EC_CONFIG_ERR(sc, "Failed to allocate memory for"
" IDN configuration!\n");
return -ENOMEM;
}
ec_soe_request_init(req);
ec_soe_request_set_drive_no(req, drive_no);
ec_soe_request_set_idn(req, idn);
req->al_state = state;
ret = ec_soe_request_copy_data(req, data, size);
if (ret < 0) {
ec_soe_request_clear(req);
kfree(req);
return ret;
}
down(&sc->master->master_sem);
list_add_tail(&req->list, &sc->soe_configs);
up(&sc->master->master_sem);
return 0;
}
/*****************************************************************************/
/** \cond */
EXPORT_SYMBOL(ecrt_slave_config_sync_manager);
EXPORT_SYMBOL(ecrt_slave_config_watchdog);
EXPORT_SYMBOL(ecrt_slave_config_pdo_assign_add);
EXPORT_SYMBOL(ecrt_slave_config_pdo_assign_clear);
EXPORT_SYMBOL(ecrt_slave_config_pdo_mapping_add);
EXPORT_SYMBOL(ecrt_slave_config_pdo_mapping_clear);
EXPORT_SYMBOL(ecrt_slave_config_pdos);
EXPORT_SYMBOL(ecrt_slave_config_reg_pdo_entry);
EXPORT_SYMBOL(ecrt_slave_config_dc);
EXPORT_SYMBOL(ecrt_slave_config_sdo);
EXPORT_SYMBOL(ecrt_slave_config_sdo8);
EXPORT_SYMBOL(ecrt_slave_config_sdo16);
EXPORT_SYMBOL(ecrt_slave_config_sdo32);
EXPORT_SYMBOL(ecrt_slave_config_complete_sdo);
EXPORT_SYMBOL(ecrt_slave_config_emerg_size);
EXPORT_SYMBOL(ecrt_slave_config_emerg_pop);
EXPORT_SYMBOL(ecrt_slave_config_emerg_clear);
EXPORT_SYMBOL(ecrt_slave_config_emerg_overruns);
EXPORT_SYMBOL(ecrt_slave_config_create_sdo_request);
EXPORT_SYMBOL(ecrt_slave_config_create_voe_handler);
EXPORT_SYMBOL(ecrt_slave_config_create_reg_request);
EXPORT_SYMBOL(ecrt_slave_config_state);
EXPORT_SYMBOL(ecrt_slave_config_idn);
/** \endcond */
/*****************************************************************************/
|