-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathregDev.c
3180 lines (2947 loc) · 98.7 KB
/
regDev.c
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
/* Generic register Device Support */
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#define epicsTypesGLOBAL
#include <callback.h>
#include <dbAccess.h>
#include <dbScan.h>
#include <recSup.h>
#include <epicsTimer.h>
#include <epicsMessageQueue.h>
#include <epicsThread.h>
#include <cantProceed.h>
#include <epicsAssert.h>
#include <epicsExit.h>
#include <epicsStdioRedirect.h>
#include "memDisplay.h"
#include "regDevSup.h"
#define MAGIC_PRIV 2181699655U /* crc("regDev") */
#define MAGIC_NODE 2055989396U /* crc("regDeviceNode") */
#define CMD_READ 1
#define CMD_WRITE 2
#define CMD_EXIT 4
#if defined __USE_XOPEN2K8
#undef epicsMutexMustCreate
static epicsMutexId epicsMutexMustCreate() {
static pthread_mutexattr_t attr;
static int initialized = 0;
pthread_mutex_t *lock;
if (!initialized) {
pthread_mutexattr_init(&attr);
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
initialized = 1;
printf("regDev uses PTHREAD_PRIO_INHERIT mutex\n");
}
lock = callocMustSucceed(1, sizeof(pthread_mutex_t), "epicsMutexMustCreate");
pthread_mutex_init(lock, &attr);
return (epicsMutexId)lock;
}
#define epicsMutexLock(lock) pthread_mutex_lock((pthread_mutex_t*)lock);
#define epicsMutexUnlock(lock) pthread_mutex_unlock((pthread_mutex_t*)lock);
#endif
static regDeviceNode* registeredDevices = NULL;
epicsShareDef int regDevDebug = 0;
epicsExportAddress(int, regDevDebug);
#define regDevGetPriv() \
regDevPrivate* priv = record->dpvt; \
if (priv == NULL) { \
regDevPrintErr("record not initialized"); \
return S_dev_badInit; } \
assert(priv->magic == MAGIC_PRIV)
static int startswith(const unsigned char *s, const char *key)
{
int n = 0;
while (*key) {
if (*key != tolower(*s)) return 0;
key++;
s++;
n++;
}
return n;
}
/***********************************************************************
* Routine to parse IO arguments
* IO address line format:
*
* <name>:<addr>[:[init]] [T=<type>] [B=<bit>] [I=<invert>] [M=<mask>] [L=<low|strLen>] [H=<high>] [P=<packing>] [F=<feed>] [U=<update>]
*
* where: <name> - symbolic device name
* <addr> - address (byte number) within memory block
* (expressions containing +-*() allowed, no spaces!)
* <init> - optional init read address ( for output records )
* <type> - data type, see table below
* <bit> - bit number (least significant bit is 0)
* <invert> - mask of inverted bits
* <mask> - mask of valid bits
* <strLen> - string length
* <low> - raw value that mapps to EGUL
* <high> - raw value that mapps to EGUF
* <packing> - number of array values in one fifo register
* <feed> - bytes to the next array element of interlaces arrays
* <update> - milliseconds for periodic update of output records
**********************************************************************/
#define epicsInt64T (98)
#define epicsUInt64T (99)
#define regDevBCD8T (100)
#define regDevBCD16T (101)
#define regDevBCD32T (102)
#define regDevBCD64T (103)
#define regDevFirstType epicsInt64T
#define regDevLastType regDevBCD64T
static const struct {char* name; epicsType type;} datatypes [] =
{
/* Important for order:
* The default type must be the first entry.
* Names that are substrings of others names must come later
*/
{ "short", epicsInt16T },
{ "int16", epicsInt16T },
{ "int8", epicsInt8T },
{ "char", epicsUInt8T },
{ "byte", epicsUInt8T },
{ "uint8", epicsUInt8T },
{ "unsign8", epicsUInt8T },
{ "unsigned8", epicsUInt8T },
{ "word", epicsUInt16T },
{ "uint16", epicsUInt16T },
{ "unsign16", epicsUInt16T },
{ "unsigned16", epicsUInt16T },
{ "long", epicsInt32T },
{ "int32", epicsInt32T },
{ "dword", epicsUInt32T },
{ "uint32", epicsUInt32T },
{ "unsign32", epicsUInt32T },
{ "unsigned32", epicsUInt32T },
{ "longlong", epicsInt64T },
{ "int64", epicsInt64T },
{ "qword", epicsUInt64T },
{ "uint64", epicsUInt64T },
{ "unsign64", epicsUInt64T },
{ "unsigned64", epicsUInt64T },
{ "double", epicsFloat64T },
{ "real64", epicsFloat64T },
{ "float64", epicsFloat64T },
{ "single", epicsFloat32T },
{ "real32", epicsFloat32T },
{ "float32", epicsFloat32T },
{ "float", epicsFloat32T },
{ "string", epicsStringT },
{ "bcd8", regDevBCD8T },
{ "bcd16", regDevBCD16T },
{ "bcd32", regDevBCD32T },
{ "bcd64", regDevBCD64T },
{ "bcd", regDevBCD8T },
{ "time", regDevBCD8T } /* for backward compatibility */
};
const char* regDevTypeName(unsigned short dtype)
{
const char* regDevTypeNames [] = {
"epicsInt64",
"epicsUInt64",
"regDevBCD8",
"regDevBCD16",
"regDevBCD32",
"regDevBCD64",
};
if (dtype > regDevLastType) return "invalid";
return dtype < regDevFirstType ?
epicsTypeNames[dtype] :
regDevTypeNames[dtype-regDevFirstType];
}
epicsInt64 regDevParseExpr(unsigned char** pp);
epicsInt64 regDevParseValue(unsigned char** pp)
{
epicsInt64 val;
unsigned char *p = *pp;
int neg = 0;
while (isspace(*p)) p++;
if (*p == '+' || *p == '-') neg = *p++ == '-';
while (isspace(*p)) p++;
if (*p == '(')
{
p++;
val = regDevParseExpr(&p);
if (*p == ')') p++;
}
else val = strtoul((char*)p, (char**)&p, 0);
while (isspace(*p)) p++;
*pp = p;
return neg ? -val : val;
}
epicsInt64 regDevParseProd(unsigned char** pp)
{
epicsInt64 val = 1;
unsigned char *p = *pp;
while (isspace(*p)) p++;
while (*p == '*')
{
p++;
val *= regDevParseValue(&p);
}
*pp = p;
return val;
}
epicsInt64 regDevParseExpr(unsigned char** pp)
{
epicsInt64 sum = 0;
epicsInt64 val;
unsigned char *p = *pp;
do {
val = regDevParseValue(&p);
val *= regDevParseProd(&p);
sum += val;
} while (*p == '+' || *p == '-');
*pp = p;
return sum;
}
int regDevIoParse2(
const char* recordName,
char* parameterstring,
regDevPrivate* priv,
int types)
{
char devName[255];
regDeviceNode* device;
unsigned char* p = (unsigned char*)parameterstring;
char separator;
size_t nchar;
static const int maxtype = sizeof(datatypes)/sizeof(*datatypes);
int type = 0;
int hset = 0;
int lset = 0;
epicsInt64 H = 0;
epicsInt64 L = 0;
regDevDebugLog(DBG_INIT, "%s: \"%s\"\n", recordName, parameterstring);
/* Get rid of leading whitespace and non-alphanumeric chars */
while (!isalnum(*p)) if (*p++ == '\0')
{
errlogPrintf("regDevIoParse %s: no device name in parameter string \"%s\"\n",
recordName, parameterstring);
return S_dev_badArgument;
}
/* Get device name */
nchar = strcspn((char*)p, ":/ ");
strncpy(devName, (char*)p, nchar);
devName[nchar] = '\0';
p += nchar;
separator = *p++;
regDevDebugLog(DBG_INIT, "%s: device=%s\n",
recordName, devName);
for (device = registeredDevices; device; device = device->next)
{
if (strcmp(device->name, devName) == 0) break;
}
if (!device)
{
errlogPrintf("regDevIoParse %s: device '%s' not found\n",
recordName, devName);
return S_dev_noDevice;
}
priv->device = device;
/* Check device offset (for backward compatibility allow '/') */
if (separator == ':' || separator == '/')
{
ptrdiff_t offset = 0;
while (isspace(*p)) p++;
if (!isdigit(*p))
{
/* expect record name, maybe in ' quotes, maybe in () */
char recName[PVNAME_STRINGSZ];
int i = 0;
char quote = 0;
char parenthesis = 0;
if (*p == '(')
{
parenthesis = *p++;
while (isspace(*p)) p++;
}
if (*p == '\'') quote = *p++;
/* all non-whitespace chars are legal here except quote ', incl + and * */
while (*p && !isspace(*p) && *p != quote && i < sizeof(recName)-1) recName[i++] = *p++;
if (quote && *p == quote) p++;
recName[i] = 0;
priv->offsetRecord = mallocMustSucceed(sizeof (struct dbAddr), "regDevIoParse");
if (dbNameToAddr(recName, priv->offsetRecord) != S_dev_success)
{
free(priv->offsetRecord);
priv->offsetRecord = NULL;
errlogPrintf("regDevIoParse %s: record '%s' not found\n",
recordName, recName);
return S_dev_badArgument;
}
priv->offsetScale = regDevParseProd(&p);
if (parenthesis == '(')
{
ptrdiff_t scale;
offset = regDevParseExpr(&p);
if (*p == ')')
{
p++;
scale = regDevParseProd(&p);
priv->offsetScale *= scale;
offset *= scale;
}
}
}
offset += regDevParseExpr(&p);
if (offset < 0 && !priv->offsetRecord)
{
errlogPrintf("regDevIoParse %s: offset %" Z "d<0\n",
recordName, offset);
return S_dev_badArgument;
}
priv->offset = offset;
if (priv->offsetRecord)
regDevDebugLog(DBG_INIT,
"%s: offset='%s'*%" Z "d+%" Z "d(0x%" Z "x)\n",
recordName, priv->offsetRecord->precord->name,
priv->offsetScale, priv->offset, priv->offset);
else
regDevDebugLog(DBG_INIT,
"%s: offset=%" Z "d(0x%" Z "x)\n",
recordName, priv->offset, priv->offset);
separator = *p++;
}
else
{
priv->offset = 0;
}
/* Check readback offset (for backward compatibility allow '!' and '/') */
if (separator == ':' || separator == '/' || separator == '!')
{
unsigned char* p1;
ptrdiff_t rboffset;
if (!device->support->read)
{
errlogPrintf("regDevIoParse %s: can't read back from device without read function\n",
recordName);
return S_dev_wrongDevice;
}
while (isspace(*p)) p++;
p1 = p;
rboffset = regDevParseExpr(&p);
if (p1 == p)
{
if (priv->offsetRecord)
{
errlogPrintf("regDevIoParse %s: cannot read back from variable offset\n",
recordName);
return S_dev_badArgument;
}
priv->rboffset = priv->offset;
}
else
{
if (rboffset < 0)
{
errlogPrintf("regDevIoParse %s: readback offset %" Z "d < 0\n",
recordName, rboffset);
return S_dev_badArgument;
}
priv->rboffset = rboffset;
}
regDevDebugLog(DBG_INIT,
"%s: readback offset=0x%" Z "x\n", recordName, priv->rboffset);
separator = *p++;
}
else
{
regDevDebugLog(DBG_INIT,
"%s: no readback offset\n", recordName);
priv->rboffset = DONT_INIT;
}
/* set default values for parameters */
priv->bit = 0;
priv->L = 0;
priv->H = 0;
priv->invert = 0;
priv->mask = 0;
/* allow whitespaces before parameter for device support */
while ((separator == '\t') || (separator == ' '))
separator = *p++;
if (separator != '\'') p--; /* optional quote for compatibility */
/* optional parameters */
while (p && *p)
{
ptrdiff_t val;
char c = 0;
int i;
static const char* const parameter [] = {
"Ttype",
"Bbit",
"Iinvert",
"Iinv",
"Mmask",
"Llow",
"Llo",
"Llength",
"Llen",
"Hhigh",
"Hhi",
"Ppacking",
"Pfifopacking",
"Ffeed",
"Farrayfeed",
"Finterlace",
"Uupdate",
"Vvector",
"Vvec",
"Vivec",
"Virqvec",
"Virq",
"Vintvec",
"Vinterrupt"};
while (isspace(*p)) p++;
for (i = 0; i < sizeof(parameter)/sizeof(const char*); i++)
{
if (startswith(p, parameter[i]+1))
{
c = parameter[i][0];
regDevDebugLog(DBG_INIT, "%s: verbose parameter '%.*s'=%c\n",
recordName, (int)strlen(parameter[i]+1), p, c);
p += strlen(parameter[i]+1);
break;
}
}
if (!c) c = toupper(*p++);
while (isspace(*p)) p++;
if (*p == '=') p++;
while (isspace(*p)) p++;
switch (c)
{
case 'T': /* T=<datatype> */
for (type = 0; type < maxtype; type++)
{
nchar = startswith(p, datatypes[type].name);
if (nchar)
{
priv->dtype = datatypes[type].type;
p += nchar;
break;
}
}
if (type == maxtype)
{
errlogPrintf("regDevIoParse %s: invalid datatype '%s'\n",
recordName, p);
return S_dev_badArgument;
}
break;
case 'B': /* B=<bitnumber> */
val = regDevParseExpr(&p);
if (val < 0 || val >= 64)
{
errlogPrintf("regDevIoParse %s: invalid bit number %" Z "d\n",
recordName, val);
return S_dev_badArgument;
}
priv->bit = (epicsUInt8)val;
break;
case 'I': /* I=<invert> */
priv->invert = regDevParseExpr(&p);
break;
case 'M': /* I=<mask> */
priv->mask = regDevParseExpr(&p);
break;
case 'L': /* L=<low raw value> (converts to EGUL) */
L = regDevParseExpr(&p);
lset = 1;
break;
case 'H': /* L=<high raw value> (converts to EGUF) */
H = regDevParseExpr(&p);
hset = 1;
break;
case 'P': /* P=<packing> (for fifo) */
priv->fifopacking = (epicsUInt8)regDevParseExpr(&p);
break;
case 'F': /* F=<feed> offset to next element in arrays */
priv->interlace = regDevParseExpr(&p);
break;
case 'U': /* U=<update period [ms]> (T = trigger by updater bo record) */
if (toupper(*p) == 'T')
{
p++;
priv->update = -1;
}
else
priv->update = (epicsInt32)regDevParseExpr(&p);
break;
case 'V': /* V=<irq vector> */
priv->irqvec = (epicsInt32)regDevParseExpr(&p);
break;
case '\'':
if (separator == '\'')
{
p = 0;
break;
}
case ')':
errlogPrintf("regDevIoParse %s: unbalanced closing )\n",
recordName);
return S_dev_badArgument;
default:
errlogPrintf("regDevIoParse %s: unknown parameter '%c'\n",
recordName, *p);
return S_dev_badArgument;
}
}
/* check if bit number is in range */
if (priv->dlen && priv->bit >= priv->dlen*8)
{
errlogPrintf("regDevIoParse %s: invalid bit number %d (0...%d)\n",
recordName, priv->bit, priv->dlen*8-1);
return S_dev_badArgument;
}
/* get default values for L and H if user did not define them */
switch (priv->dtype)
{
case epicsUInt8T:
priv->dlen = 1;
if (!hset) H = 0xFF;
break;
case epicsUInt16T:
priv->dlen = 2;
if (!hset) H = 0xFFFF;
break;
case epicsUInt32T:
priv->dlen = 4;
if (!hset) H = 0xFFFFFFFF;
break;
case epicsUInt64T:
priv->dlen = 8;
if (!hset) H = 0xFFFFFFFFFFFFFFFFLL;
break;
case epicsInt8T:
priv->dlen = 1;
if (!lset) L = (types & TYPE_FLOAT) ? -0x7F : -0x80;
if (!hset) H = 0x7F;
break;
case epicsInt16T:
priv->dlen = 2;
if (!lset) L = (types & TYPE_FLOAT) ? -0x7FFF : -0x8000;
if (!hset) H = 0x7FFF;
break;
case epicsInt32T:
priv->dlen = 4;
if (!lset) L = (types & TYPE_FLOAT) ? -0x7FFFFFFFLL : -0x80000000LL;
if (!hset) H = 0x7FFFFFFF;
break;
case epicsInt64T:
priv->dlen = 8;
if (!lset) L = (types & TYPE_FLOAT) ? -0x7FFFFFFFFFFFFFFFLL : -0x7FFFFFFFFFFFFFFFLL -1;
if (!hset) H = 0x7FFFFFFFFFFFFFFFLL;
break;
case regDevBCD8T:
priv->dlen = 1;
if (!hset) H = 99;
break;
case regDevBCD16T:
priv->dlen = 2;
if (!hset) H = 9999;
break;
case regDevBCD32T:
priv->dlen = 4;
if (!hset) H = 99999999;
break;
case regDevBCD64T:
priv->dlen = 8;
if (!hset) H = 9999999999999999LL;
break;
case epicsFloat32T:
priv->dlen = 4;
break;
case epicsFloat64T:
priv->dlen = 8;
break;
case epicsStringT:
priv->dlen = 1;
if (!lset) L = MAX_STRING_SIZE;
/* for T=STRING L=... means length, not low */
lset=0;
default:
if (lset || hset) {
errlogPrintf("regDevIoParse %s: %s%s%s makes"
" no sense with T=%s. Ignored.\n",
recordName,
lset?"L":"",
(lset && hset)?" and ":"",
hset?"H":"",
datatypes[type].name);
}
break;
}
priv->L = L;
priv->H = H;
regDevDebugLog(DBG_INIT, "%s: T=%s dlen=%d\n", recordName, datatypes[type].name, priv->dlen);
regDevDebugLog(DBG_INIT, "%s: L=%lld(%#llx)\n", recordName, (long long)priv->L, (long long)priv->L);
regDevDebugLog(DBG_INIT, "%s: H=%lld(%#llx)\n", recordName, (long long)priv->H, (long long)priv->H);
regDevDebugLog(DBG_INIT, "%s: B=%d\n", recordName, priv->bit);
regDevDebugLog(DBG_INIT, "%s: I=%#llx\n", recordName, (long long)priv->invert);
regDevDebugLog(DBG_INIT, "%s: M=%#llx\n", recordName, (long long)priv->mask);
regDevDebugLog(DBG_INIT, "%s: P=%lli\n", recordName, (long long)priv->fifopacking);
regDevDebugLog(DBG_INIT, "%s: U=%lli\n", recordName, (long long)priv->update);
regDevDebugLog(DBG_INIT, "%s: V=%lli\n", recordName, (long long)priv->irqvec);
regDevDebugLog(DBG_INIT, "%s: F=%llu(%#llx)\n", recordName, (unsigned long long)priv->interlace,
(unsigned long long)priv->interlace);
return S_dev_success;
}
int regDevIoParse(dbCommon* record, struct link* link, int types)
{
int status;
if (link->type != INST_IO)
{
regDevPrintErr("illegal link field type %s",
pamaplinkType[link->type].strvalue);
status = S_dev_badInpType;
}
else
{
status = regDevIoParse2(record->name,
link->value.instio.string,
(regDevPrivate*) record->dpvt,
types);
if (status == S_dev_success) return status;
regDevPrintErr("invalid link field \"%s\"",
link->value.instio.string);
}
free(record->dpvt);
record->dpvt = NULL;
return status;
}
/********* Diver registration and access ****************************/
int regDevRegisterDevice(const char* name,
const regDevSupport* support, regDevice* driver, size_t size)
{
regDeviceNode **pdevice;
regDevDebugLog(DBG_INIT, "%s: support=%p, driver=%p\n",
name, support, driver);
for (pdevice = ®isteredDevices; *pdevice; pdevice = &(*pdevice)->next)
{
if (strcmp((*pdevice)->name, name) == 0)
{
errlogPrintf("regDevRegisterDevice %s: device already exists\n",
name);
return S_dev_multDevice;
}
}
*pdevice = (regDeviceNode*) callocMustSucceed(1, sizeof(regDeviceNode), "regDevRegisterDevice");
(*pdevice)->magic = MAGIC_NODE;
(*pdevice)->name = strdup(name);
(*pdevice)->size = size;
(*pdevice)->support = support;
(*pdevice)->driver = driver;
(*pdevice)->accesslock = epicsMutexMustCreate();
return S_dev_success;
}
/* Only for backward compatibility. Don't use! */
int regDevAsyncRegisterDevice(const char* name,
const regDevSupport* support, regDevice* driver)
{
return regDevRegisterDevice(name, support, driver, 0);
}
regDeviceNode* regDevGetDeviceNode(regDevice* driver) {
regDeviceNode* device;
for (device = registeredDevices; device; device = device->next)
if (device->driver == driver) break;
assert(device != NULL);
assert(device->magic == MAGIC_NODE);
return device;
}
regDevice* regDevFind(const char* name)
{
regDeviceNode* device;
if (!name || !*name) return NULL;
for (device = registeredDevices; device; device = device->next)
{
if (strcmp(name, device->name) == 0)
return device->driver;
}
return NULL;
}
/* Only for backward compatibility. Don't use! */
regDevice* regDevAsynFind(const char* name)
{
return regDevFind(name);
}
const char* regDevName(regDevice* driver)
{
return regDevGetDeviceNode(driver)->name;
}
int regDevRegisterDmaAlloc(regDevice* driver,
void* (*dmaAlloc) (regDevice *, void* ptr, size_t))
{
regDevGetDeviceNode(driver)->dmaAlloc = dmaAlloc;
return S_dev_success;
}
int regDevLock(regDevice* driver)
{
return epicsMutexLock(regDevGetDeviceNode(driver)->accesslock);
}
int regDevUnlock(regDevice* driver)
{
epicsMutexUnlock(regDevGetDeviceNode(driver)->accesslock);
return S_dev_success;
}
/********* Support for "I/O Intr" for input records ******************/
long regDevGetInIntInfo(int cmd, dbCommon *record, IOSCANPVT *ppvt)
{
regDeviceNode* device;
regDevGetPriv();
device = priv->device;
assert(device != NULL);
regDevDebugLog(DBG_INIT, "%s %s prio=%d irqvec=%d cmd=%d\n",
device->name, record->name, record->prio, priv->irqvec,
cmd);
if (priv->irqvec == -1 &&
(device->blockModes & REGDEV_BLOCK_READ) &&
record->prio != 2)
{
*ppvt = device->blockReceived;
}
else if (device->support->getInScanPvt)
{
epicsMutexLock(device->accesslock);
*ppvt = device->support->getInScanPvt(
device->driver, priv->offset, priv->dlen, priv->nelm, priv->irqvec, record->name);
epicsMutexUnlock(device->accesslock);
}
else
{
regDevPrintErr("input I/O Intr unsupported for device %s",
device->name);
return S_dev_badRequest;
}
if (*ppvt == NULL)
{
if (priv->irqvec != -1)
regDevPrintErr("no input I/O Intr for device %s interrupt vector %#x",
device->name, priv->irqvec);
else
regDevPrintErr("no input I/O Intr for device %s",
device->name);
return S_dev_badArgument;
}
return S_dev_success;
}
/********* Support for "I/O Intr" for output records ****************/
long regDevGetOutIntInfo(int cmd, dbCommon *record, IOSCANPVT *ppvt)
{
regDeviceNode* device;
regDevGetPriv();
device = priv->device;
assert(device != NULL);
regDevDebugLog(DBG_INIT, "%s %s prio=%d irqvec=%d cmd=%d\n",
device->name, record->name, record->prio, priv->irqvec, cmd);
if (priv->irqvec == -1 &&
(device->blockModes & REGDEV_BLOCK_WRITE) &&
record->prio != 2)
{
*ppvt = device->blockSent;
}
else if (device->support->getOutScanPvt)
{
epicsMutexLock(device->accesslock);
*ppvt = device->support->getOutScanPvt(
device->driver, priv->offset, priv->dlen, priv->nelm, priv->irqvec, record->name);
epicsMutexUnlock(device->accesslock);
}
else
{
regDevPrintErr("output I/O Intr unsupported for device %s",
device->name);
return S_dev_badRequest;
}
if (*ppvt == NULL)
{
if (priv->irqvec != -1)
regDevPrintErr("no output I/O Intr for device %s interrupt vector %#x",
device->name, priv->irqvec);
else
regDevPrintErr("no output I/O Intr for device %s",
device->name);
return S_dev_badArgument;
}
return S_dev_success;
}
/********* Report routine ********************************************/
long regDevReport(int level)
{
regDeviceNode* device;
if (!registeredDevices)
{
printf("no registered devices\n");
return S_dev_success;
}
printf("registered devices:\n");
for (device = registeredDevices; device; device = device->next)
{
size_t size = device->size;
printf(" \"%s\" size ", device->name);
if (size)
{
printf("%" Z "d", size);
if (size > 9) printf("=0x%" Z "x", size);
if (size > 1024*1024)
printf("=%" Z "dMiB", size >> 20);
else if (size > 1024)
printf("=%" Z "dKiB", size >> 10);
}
else
printf("unknown");
if (device->blockBuffer)
printf(" block@%p", device->blockBuffer);
if (device->support && device->support->report)
{
printf(" ");
fflush(stdout);
epicsMutexLock(device->accesslock);
device->support->report(device->driver, level);
epicsMutexUnlock(device->accesslock);
}
else
printf("\n");
}
return S_dev_success;
}
struct drvsup {
long number;
long (*report)();
long (*init)();
} regDev = {
2,
regDevReport,
NULL
};
epicsExportAddress(drvet, regDev);
/* routine to convert bytes from BCD to integer format. */
static epicsUInt64 bcd2i(epicsUInt64 bcd)
{
unsigned long i = 0;
unsigned long m = 1;
while (bcd)
{
i += (bcd & 0xF) * m;
m *= 10;
bcd >>= 4;
}
return i;
}
/* routine to convert bytes from integer to BCD format. */
static epicsUInt64 i2bcd(epicsUInt64 i)
{
int bcd = 0;
int s = 0;
while (i)
{
bcd += (i % 10) << s;
i /= 10;
s += 4;
}
return bcd;
}
/* generic device support init functions ****************************/
regDevPrivate* regDevAllocPriv(dbCommon *record)
{
regDevPrivate* priv;
regDevDebugLog(DBG_INIT, "%s\n", record->name);
priv = callocMustSucceed(1, sizeof(regDevPrivate),"regDevAllocPriv");
priv->magic = MAGIC_PRIV;
priv->dtype = epicsInt16T;
priv->irqvec=-1;
priv->nelm = 1;
record->dpvt = priv;
return priv;
}
int regDevAssertType(dbCommon *record, int allowedTypes)
{
unsigned short dtype;
regDevGetPriv();
dtype = priv->dtype;
regDevDebugLog(DBG_INIT, "%s: allows%s%s%s%s and uses %s\n",
record->name,
allowedTypes & TYPE_INT ? " INT" : "",
allowedTypes & TYPE_FLOAT ? " FLOAT" : "",
allowedTypes & TYPE_STRING ? " STRING" : "",
allowedTypes & TYPE_BCD ? " BCD" : "",
regDevTypeName(dtype));
switch (dtype)
{
case epicsInt8T:
case epicsUInt8T:
case epicsInt16T:
case epicsUInt16T:
case epicsInt32T:
case epicsUInt32T:
if (allowedTypes & TYPE_INT) return S_dev_success;
break;
case epicsFloat32T:
case epicsFloat64T:
if (allowedTypes & TYPE_FLOAT) return S_dev_success;
break;
case epicsStringT:
if (allowedTypes & TYPE_STRING) return S_dev_success;
break;
case regDevBCD8T:
case regDevBCD16T:
case regDevBCD32T:
case regDevBCD64T:
if (allowedTypes & TYPE_BCD) return S_dev_success;
break;
case epicsInt64T:
if (allowedTypes & (TYPE_INT|TYPE_FLOAT)) return S_dev_success;
break;
}
regDevPrintErr("illegal data type %s for this record type",
regDevTypeName(dtype));
free(record->dpvt);
record->dpvt = NULL;
return S_db_badField;
}
int regDevCheckFTVL(dbCommon* record, int ftvl)
{