-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcifxlinux.c
1689 lines (1492 loc) · 57.2 KB
/
cifxlinux.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
// SPDX-License-Identifier: MIT
/**************************************************************************************
*
* Copyright (c) 2024, Hilscher Gesellschaft fuer Systemautomation mbH. All Rights Reserved.
*
* Description: Linux specific driver / toolkit initialization
*
* Changes:
*
* Version Date Author Description
* ----------------------------------------------------------------------------------
* 1 02.01.24 SD changed licensing terms
*
**************************************************************************************/
#include "cifXToolkit.h"
#include "cifXHWFunctions.h"
#include "cifxlinux.h"
#include "cifXUser.h"
#include "cifxlinux_internal.h"
#include "HilPCIDefs.h"
#ifdef CIFXETHERNET
#include "netx_tap.h"
#endif
#ifdef CIFX_PLUGIN_SUPPORT
#include <dlfcn.h>
#include <sys/queue.h>
#endif
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/file.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
extern char* g_szDriverBaseDir;
extern void* g_pvTkitLock;
extern unsigned long g_ulDeviceCount;
extern PDEVICEINSTANCE* g_pptDevices;
#ifdef CIFXETHERNET
extern void* g_eth_list_lock;
#endif
FILE* g_logfd = 0;
#ifdef CIFX_PLUGIN_SUPPORT
struct CIFX_PLUGIN_T
{
SLIST_ENTRY(CIFX_PLUGIN_T) tList;
void* hPluginFile;
uint32_t ulDeviceCount;
struct CIFX_DEVICE_T** aptDevices;
};
static SLIST_HEAD(PLUGIN_LIST, CIFX_PLUGIN_T) s_tPluginList = SLIST_HEAD_INITIALIZER(PLUGIN_LIST);
#endif
#define COS_THREAD_STACK_MIN 0x1000 /* Stack size needed by Thread for
handling Toolkit's cyclic actions */
/*****************************************************************************/
/*! \file cifxlinux.c
* Linux specific initialization of toolkit / driver */
/*****************************************************************************/
static void __init() __attribute__((__constructor__));
static void __deinit() __attribute__((__destructor__));
static void* cifXPollingThread(void *arg);
static int polling_thread_running = 0;
static pthread_t polling_thread = {0};
static pthread_attr_t polling_thread_attr = {{0}};
static unsigned short polling_thread_stop = 0;
#ifdef CIFX_DRV_HWIF
void* HWIFDPMRead ( uint32_t ulOpt, void* pvDevInstance, void* pvDpmAddr, void* pvDst, uint32_t ulLen);
void* HWIFDPMWrite( uint32_t ulOpt, void* pvDevInstance, void* pvDpmAddr, void* pvSrc, uint32_t ulLen);
#endif
/*****************************************************************************/
/*! Initialization function called on load of libcifx_tk.so */
/*****************************************************************************/
static void __init()
{
//cifXTKitInit();
}
/*****************************************************************************/
/*! De-Initialization function called on unload of libcifx_tk.so */
/*****************************************************************************/
static void __deinit()
{
cifXDriverDeinit();
}
/*****************************************************************************/
/*! Helper function to open a uio device
* \param uio_num Number of uio device
* \param fCheckAccess If != 0 the driver denies access if card is already
* accesed
* \return fd of uio, -1 on error */
/*****************************************************************************/
int cifx_uio_open(int uio_num, int fCheckAccess)
{
char dev_name[16];
int fd;
int iRet;
sprintf(dev_name, "/dev/uio%d", uio_num);
fd = open(dev_name,O_RDWR);
/* if fCheckAccess is true lock access to the device */
if (1 == fCheckAccess)
{
/* lock file access (non blocking) */
if (0 != (iRet = flock( fd, LOCK_EX | LOCK_NB)))
{
if (errno == EWOULDBLOCK)
{
fprintf( stderr, "cifX%d may be opened by another process!\n", uio_num);
}
close(fd);
fd = -1;
}
}
return fd;
}
/*****************************************************************************/
/*! Helper function to open a ISA device
* \return fd -1 on error */
/*****************************************************************************/
int cifx_ISA_open(void)
{
char dev_name[16];
int fd;
sprintf(dev_name, "/dev/mem");
fd = open(dev_name,O_RDWR, O_SYNC);
return fd;
}
/*****************************************************************************/
/*! Helper function to close a ISA device */
/*****************************************************************************/
void cifx_ISA_close(int isa_fd)
{
close( isa_fd);
}
/*****************************************************************************/
/*! Checks if mapping matches specific type (dpm, extmem, dma)
* \param uio_num number of the uio device
* \param mapno Mapping number to validate
* \param tMemtype Memory type to be checked
* \return CIFX_NO_ERROR if memtype matches */
/*****************************************************************************/
int32_t validate_memtype( int uio_num, int mapno, CIFX_MEM_TYPE_E tMemtype)
{
int32_t ret = CIFX_DEV_FUNCTION_FAILED;
FILE* file;
char memtype[64];
char filename[64];
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/name", uio_num, mapno);
if ((file = fopen(filename,"r")) && (1==fscanf(file,"%s",memtype)))
{
switch (tMemtype)
{
case eMEM_DPM:
ret = ( 0 == strncmp("dpm", memtype, strlen("dpm")))? CIFX_NO_ERROR: CIFX_DEV_FUNCTION_FAILED;
break;
case eMEM_EXTMEM:
ret = ( 0 == strncmp("extmem", memtype, strlen("extmem")))? CIFX_NO_ERROR: CIFX_DEV_FUNCTION_FAILED;
break;
case eMEM_DMA:
ret = ( 0 == strncmp("dma", memtype, strlen("dma")))? CIFX_NO_ERROR: CIFX_DEV_FUNCTION_FAILED;
break;
default:
ret = CIFX_INVALID_PARAMETER;
break;
}
} else
{
/* no name specified -> old uio_netx driver */
/* name file does not exist -> old uio driver */
if ((NULL != file) || (errno == ENOENT))
{
/* memory type validation via requested mapno (to be downwards compatible) */
switch (tMemtype)
{
case eMEM_DPM: /* first mapno is DPM */
ret = (0 == mapno) ? CIFX_NO_ERROR : CIFX_DEV_FUNCTION_FAILED;
break;
case eMEM_EXTMEM:/* second mapno is ext mem */
ret = (1 == mapno) ? CIFX_NO_ERROR : CIFX_DEV_FUNCTION_FAILED;
break;
case eMEM_DMA:/* dma was not supported */
default:
ret = CIFX_INVALID_PARAMETER;
break;
}
}
}
if (NULL != file)
fclose(file);
return ret;
}
/*****************************************************************************/
/*! Iterates over mapping dir, searching for a specific memtype
* \param uio_num number of the uio device
* \param memtype to search for
* \param bar BAR to mapping file
* \return CIFX_NO_ERROR if memtype is found */
/*****************************************************************************/
int32_t find_memtype( int uio_num, CIFX_MEM_TYPE_E tMemtype, int *bar)
{
char addr_file[64];
struct dirent** namelist;
int32_t ret = CIFX_NO_MORE_ENTRIES;
int num_map;
int found = 0;
if (!bar)
return CIFX_INVALID_PARAMETER;
sprintf(addr_file, "/sys/class/uio/uio%d/maps/", uio_num);
num_map = scandir(addr_file, &namelist, 0, alphasort);
if(num_map > 0)
{
int currfile = 0;
for(;currfile < num_map; ++currfile)
{
if ((0 == found) && (CIFX_NO_ERROR == validate_memtype( uio_num, currfile, tMemtype)))
{
*bar = currfile;
found = 1;
}
free(namelist[currfile]);
}
free(namelist);
if (found)
ret = CIFX_NO_ERROR;
}
return ret;
}
/*****************************************************************************/
/*! Returns the memory size of a uio memory bar
* \param uio_num Number of uio device
* \param bar BAR to read memory size from
* \return size of the bar in bytes */
/*****************************************************************************/
unsigned long cifx_uio_get_mem_size(int uio_num, int bar)
{
unsigned long ret = ~0;
char filename[64];
FILE* file;
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/size",
uio_num, bar);
file = fopen(filename,"r");
if(file)
{
if (1!=fscanf(file, "0x%lx", &ret))
ret = ~0;
fclose(file);
}
return ret;
}
/*****************************************************************************/
/*! Returns the physical address uio memory bar
* \param uio_num Number of uio device
* \param bar BAR to read memory size from
* \return physical address of bar */
/*****************************************************************************/
unsigned long cifx_uio_get_mem_addr(int uio_num, int bar)
{
unsigned long ret = ~0;
char filename[64];
FILE* file;
sprintf(filename, "/sys/class/uio/uio%d/maps/map%d/addr",
uio_num, bar);
file = fopen(filename,"r");
if(file)
{
if (1!=fscanf(file,"0x%lx",&ret))
ret = ~0;
fclose(file);
}
return ret;
}
/*****************************************************************************/
/*! Returns the alias given via device-tree
* \param uio_num Number of uio device
* \return pointer to buffer - needs to be freed after usage */
/*****************************************************************************/
char* cifx_uio_get_device_alias(int uio_num)
{
char filename[64];
char ret[64];
char* alias = NULL;
FILE* file;
sprintf(filename, "/sys/class/uio/uio%d/name",uio_num);
file = fopen(filename,"r");
if(file)
{
if (1==fscanf(file,"%s",ret)) {
char* buf = strstr(ret, ",");
if ((buf != NULL) && ((buf+1-ret)<strlen(ret))) {
buf = strstr(buf+1, ",");
if ((buf != NULL) && ((buf+1-ret)<strlen(ret))) {
if (strcmp((buf+1),"-") != 0) {
alias = (char*)malloc(CIFx_MAX_INFO_NAME_LENTH);
sprintf(alias,"%s",buf+1);
}
}
}
}
fclose(file);
}
return alias;
}
/*****************************************************************************/
/*! Returns the startuptype of the device given via device-tree
* \param uio_num Number of uio device
* \return device type (ram,flash,auto,dont touch) */
/*****************************************************************************/
CIFX_TOOLKIT_DEVICETYPE_E cifx_uio_get_device_startuptype(int uio_num)
{
char buf[64];
char filename[64];
char* next = NULL;
CIFX_TOOLKIT_DEVICETYPE_E ret = eCIFX_DEVICE_AUTODETECT;
FILE* file;
sprintf(filename, "/sys/class/uio/uio%d/name",uio_num);
file = fopen(filename,"r");
if(file)
{
if (1==fscanf(file,"%s",buf)) {
next = strstr(buf, ",");
if ((next != NULL) && ((next+1-buf)<strlen(buf))) {
char* end = strstr(next+1, ",");
next+=1;
if (end != NULL) {
*end = 0;
}
}
}
fclose(file);
}
if (next != NULL) {
if (strncmp(next, UIO_NETX_START_TYPE_AUTO, CIFx_MAX_INFO_NAME_LENTH) == 0) {
ret = eCIFX_DEVICE_AUTODETECT;
} else if (strncmp(next, UIO_NETX_START_TYPE_RAM, CIFx_MAX_INFO_NAME_LENTH) == 0) {
ret = eCIFX_DEVICE_RAM_BASED;
} else if (strncmp(next, UIO_NETX_START_TYPE_FLASH, CIFx_MAX_INFO_NAME_LENTH) == 0) {
ret = eCIFX_DEVICE_FLASH_BASED;
} else if (strncmp(next, UIO_NETX_START_TYPE_DONTTOUCH, CIFx_MAX_INFO_NAME_LENTH) == 0) {
ret = eCIFX_DEVICE_DONT_TOUCH;
} else {
/* default to auto-detect */
ret = eCIFX_DEVICE_AUTODETECT;
}
}
return ret;
}
/*****************************************************************************/
/*! Check if the name of the uio matches the given name
* \param uio_num Number of uio device
* \param name Expected name of device
* \return !=0 if name matches */
/*****************************************************************************/
int cifx_uio_validate_name(int uio_num, const char* name)
{
char filename[64];
char uio_name[64];
FILE* file;
int ret = 0;
sprintf(filename, "/sys/class/uio/uio%d/name", uio_num);
if(NULL == (file = fopen(filename,"r")))
{
perror("Error opening file.");
} else
{
if(NULL == fgets(uio_name, sizeof(uio_name), file))
{
perror("Error querying name from uio");
} else
{
char* end = uio_name + strlen(uio_name) - 1;
while( (*end == '\n') || (*end == '\r') )
{
*end-- = '\0';
}
if(0 == strncmp(uio_name, name, strlen(name)))
ret = 1;
}
fclose(file);
}
return ret;
}
/*****************************************************************************/
/*! Map the memory of a uio device
* \param fd fd returned by uio_open
* \param uio_num number of uio-device
* \param bar number of mapping
* \param dpmbase Pointer to returned virtual base address of memory area
* \param dpmaddr Pointer to returned physical address of memory area
* \param dpmlen Pointer to returned length of memory area
* \return !=0 if mapping succeeded */
/*****************************************************************************/
int cifx_uio_map_mem(int uio_fd, int uio_num,
int map_num, void** membase,
unsigned long* memaddr,
unsigned long* memlen,
unsigned long flags)
{
int ret = 0;
if( (~0 != (*memaddr = cifx_uio_get_mem_addr(uio_num, map_num))) &&
(~0 != (*memlen = cifx_uio_get_mem_size(uio_num, map_num))) )
{
*membase = mmap(NULL, *memlen,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_LOCKED|MAP_POPULATE|flags,
uio_fd, map_num * getpagesize());
if(*membase != (void*)-1)
ret =1;
}
return ret;
}
/*****************************************************************************/
/*! Map the memory (DPM) of a uio_netx device
* \param fd fd returned by uio_open
* \param dpmbase Pointer to returned virtual base address of memory area
* \param dpmaddr Pointer to returned physical address of memory area
* \param dpmlen Pointer to returned length of memory area
* \return !=0 if mapping succeeded */
/*****************************************************************************/
int cifx_uio_map_dpm(int uio_fd, int uio_num,
void** dpmbase,
unsigned long* dpmaddr,
unsigned long* dpmlen)
{
int bar = 0;
if (CIFX_NO_ERROR == find_memtype( uio_num, eMEM_DPM, &bar))
{
return cifx_uio_map_mem( uio_fd, uio_num, bar, dpmbase, dpmaddr, dpmlen, 0);
}
return 0;
}
/*****************************************************************************/
/*! Map the extended memory of a uio_netx device
* \param fd fd returned by uio_open
* \param dpmbase Pointer to returned virtual base address of memory area
* \param dpmaddr Pointer to returned physical address of memory area
* \param dpmlen Pointer to returned length of memory area
* \return !=0 if mapping succeeded */
/*****************************************************************************/
int cifx_uio_map_ext_mem(int uio_fd, int uio_num,
void** dpmbase,
unsigned long* dpmaddr,
unsigned long* dpmlen)
{
int bar = 0;
if (CIFX_NO_ERROR == find_memtype( uio_num, eMEM_EXTMEM, &bar))
{
return cifx_uio_map_mem( uio_fd, uio_num, bar, dpmbase, dpmaddr, dpmlen, 0);
}
return 0;
}
#ifdef CIFX_TOOLKIT_DMA
/*****************************************************************************/
/*! Map the DMA memory of a uio device
* The function examines the ../uio[x]/maps/ directory searching for dynamic
* mappable resources (dyn_map[x]) for dma support (->name=dma)
* \param device uio device
* \return !=0 if mapping succeeded */
/*****************************************************************************/
void cifx_uio_map_dma_buffer(struct CIFX_DEVICE_T *device)
{
char addr_file[64];
struct dirent **namelist;
int num_map;
sprintf(addr_file, "/sys/class/uio/uio%d/maps/", device->uio_num);
num_map = scandir(addr_file, &namelist, 0, alphasort);
if(num_map > 0)
{
int currfile = 0;
device->dma_buffer_cnt = 0;
for(;(device->dma_buffer_cnt < CIFX_DMA_BUFFER_COUNT) && (currfile < num_map); ++currfile)
{
if (CIFX_NO_ERROR == validate_memtype( device->uio_num, currfile, eMEM_DMA))
{
void *membase = NULL;
unsigned long memaddr;
unsigned long memlen;
if (cifx_uio_map_mem(device->uio_fd, device->uio_num,
currfile, &membase,
&memaddr,
&memlen,
0))
{
int DMACounter = 0;
if ((DMACounter = memlen/(CIFX_DEFAULT_DMA_BUFFER_SIZE)))
{
while(DMACounter)
{
device->dma_buffer[device->dma_buffer_cnt].ulSize = CIFX_DEFAULT_DMA_BUFFER_SIZE;
device->dma_buffer[device->dma_buffer_cnt].ulPhysicalAddress = memaddr;
device->dma_buffer[device->dma_buffer_cnt].pvBuffer = membase;
memaddr += CIFX_DEFAULT_DMA_BUFFER_SIZE;
membase += CIFX_DEFAULT_DMA_BUFFER_SIZE;
#ifdef VERBOSE
printf("DMA buffer %d found at 0x%p / size=%d\n", device->dma_buffer_cnt,
device->dma_buffer[device->dma_buffer_cnt].pvBuffer,
device->dma_buffer[device->dma_buffer_cnt].ulSize);
#endif
device->dma_buffer_cnt++;
DMACounter--;
}
}
} else
{
perror("Error mapping DMA buffer!\n");
}
}
}
while(num_map-->0) {
free(namelist[num_map]);
}
free(namelist);
#ifdef VERBOSE
if (device->dma_buffer_cnt == 0) {
printf("\nThe uio_netx driver does not provide memory for DMA support!\n");
printf("If DMA is required, the uio_netx driver needs to be build with DMA support!\n\n");
}
#endif
}
}
/*****************************************************************************/
/*! Unmap the DMA memory of a uio device
* \param device uio device */
/*****************************************************************************/
void cifx_uio_unmap_dma_buffer(struct CIFX_DEVICE_T *device) {
char addr_file[64];
struct dirent **namelist;
int num_map;
sprintf(addr_file, "/sys/class/uio/uio%d/maps/", device->uio_num);
num_map = scandir(addr_file, &namelist, 0, alphasort);
if(num_map > 0) {
int currfile = 0;
for(;(device->dma_buffer_cnt > 0) && (currfile < num_map); ++currfile) {
if (CIFX_NO_ERROR == validate_memtype( device->uio_num, currfile, eMEM_DMA)) {
uint32_t no_of_buffers = cifx_uio_get_mem_size(device->uio_num, currfile) / (CIFX_DEFAULT_DMA_BUFFER_SIZE);
if ((no_of_buffers > 0) && (device->dma_buffer[CIFX_DMA_BUFFER_COUNT - device->dma_buffer_cnt].pvBuffer != NULL)) {
munmap( device->dma_buffer[CIFX_DMA_BUFFER_COUNT - device->dma_buffer_cnt].pvBuffer, cifx_uio_get_mem_size(device->uio_num, currfile));
}
if (no_of_buffers >= device->dma_buffer_cnt)
device->dma_buffer_cnt = 0;
else
device->dma_buffer_cnt -= no_of_buffers;
}
}
while(num_map-->0) {
free(namelist[num_map]);
}
free(namelist);
}
}
#endif
/*****************************************************************************/
/*! Map the memory (DPM) of a ISA device
* \param uio_fd fd returned by cifX_ISA_open -> open /dev/mem
* \param dpmbase Pointer to returned virtual base address of memory area
* \param dpmaddr physical address of memory area
* \param dpmlen length of memory area
* \return !=0 if mapping succeeded */
/*****************************************************************************/
int cifx_ISA_map_dpm(int fd,
void** dpmbase,
int dpmaddr,
int dpmlen)
{
int ret = 0;
*dpmbase = mmap(NULL, dpmlen,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_LOCKED|MAP_POPULATE,
fd, dpmaddr);
if(*dpmbase != (void*)-1)
ret =1;
return ret;
}
/*****************************************************************************/
/*! Unmap the memory (DPM) of a ISA device
* \param dpmbase Pointer to returned virtual base address of memory area
* \param dpmlen length of memory area */
/*****************************************************************************/
void cifx_ISA_unmap_dpm( void* dpmaddr, int dpmlen)
{
munmap( dpmaddr, dpmlen);
}
#ifndef CIFX_TOOLKIT_DISABLEPCI
/*****************************************************************************/
/*! Try to find a matching PCI card by verifying the physical BAR addresses
* \param dev_instance Device to search for. PCI data will be inserted into
O/S dependent part
* \return !=0 if a matching PCI card was found */
/*****************************************************************************/
static int match_pci_card(PDEVICEINSTANCE dev_instance, unsigned long ulPys_Addr)
{
struct pci_device_iterator *pci_dev_it;
struct pci_device *dev;
int ret = 0;
static const struct pci_id_match id_match =
{
.vendor_id = PCI_MATCH_ANY,
.device_id = PCI_MATCH_ANY,
.subvendor_id = PCI_MATCH_ANY,
.subdevice_id = PCI_MATCH_ANY,
};
pci_dev_it = pci_id_match_iterator_create(&id_match);
while( NULL != (dev = pci_device_next(pci_dev_it)) )
{
int bar;
pci_device_probe(dev);
for(bar = 0; bar < sizeof(dev->regions) / sizeof(dev->regions[0]); ++bar)
{
if(dev->regions[bar].base_addr == (pciaddr_t)ulPys_Addr)
{
PCIFX_DEVICE_INTERNAL_T internal = (PCIFX_DEVICE_INTERNAL_T)dev_instance->pvOSDependent;
#ifdef VERBOSE
printf("matched pci card @ bus=%d,dev=%d,func=%d,vendor=0x%x,device=0x%x,subvendor=0x%x,subdevice=0x%x \n",
dev->bus, dev->dev, dev->func,
dev->vendor_id, dev->device_id, dev->subvendor_id, dev->subdevice_id);
#endif
/* detect flash based card by device- and sub_device id */
if ( (dev->vendor_id == HILSCHER_PCI_VENDOR_ID) &&
(
((dev->device_id == NETPLC100C_PCI_DEVICE_ID) && (dev->subdevice_id == NETPLC100C_PCI_SUBYSTEM_ID_FLASH)) ||
((dev->device_id == NETJACK100_PCI_DEVICE_ID) && (dev->subdevice_id == NETJACK100_PCI_SUBYSTEM_ID_FLASH)) ||
(dev->device_id == CIFX4000_PCI_DEVICE_ID)
)
)
{
dev_instance->eDeviceType = eCIFX_DEVICE_FLASH_BASED;
}
internal->pci = *dev;
ret = 1;
break;
}
}
}
pci_iterator_destroy(pci_dev_it);
return ret;
}
#endif /* CIFX_TOOLKIT_DISABLEPCI */
/*****************************************************************************/
/*! Thread for cyclic Toolkit timer, which handles polling of COS bits on
* non-irq cards
* \param arg Pollinterval in ms
* \return NULL on termination */
/*****************************************************************************/
static void* cifXPollingThread(void *arg)
{
unsigned long pollinterval = (unsigned long)arg;
struct timespec polling_sleep;
polling_sleep.tv_sec = 0;
polling_sleep.tv_nsec = pollinterval * 1000 * 1000;
while( 0 == polling_thread_stop )
{
cifXTKitCyclicTimer();
nanosleep(&polling_sleep, NULL);
}
return NULL;
}
/*****************************************************************************/
/*! Wraps the cifX Toolkit callback to the user's known parameters
* The user does not need to know about our device instance, as he only
* provided a CIFX_DEVICE_T structure
* \param pvDeviceInstance Device the callback was made for
* \param eEvent Signalled event */
/*****************************************************************************/
static void cifXWrapEvent(void* pvDeviceInstance, CIFX_TOOLKIT_NOTIFY_E eEvent)
{
PDEVICEINSTANCE ptDevInstance = (PDEVICEINSTANCE)pvDeviceInstance;
PCIFX_DEVICE_INTERNAL_T ptInternal = (PCIFX_DEVICE_INTERNAL_T)ptDevInstance->pvOSDependent;
struct CIFX_DEVICE_T* ptDevice = ptInternal->userdevice;
ptDevice->notify(ptDevice, eEvent);
}
/*****************************************************************************/
/*! Internal function for adding device to toolkit control
* \param ptDevice Device to add
* \param num Number to use for identifier ("cifX<num>")
* \param user_card !=0 if card was given through user parameter
* \return CIFX_NO_ERROR on success */
/*****************************************************************************/
static int32_t cifXDriverAddDevice(struct CIFX_DEVICE_T* ptDevice, unsigned int num, int user_card)
{
PDEVICEINSTANCE ptDevInstance = NULL;
PCIFX_DEVICE_INTERNAL_T ptInternalDev = NULL;
int32_t ret = CIFX_FUNCTION_FAILED;
#ifdef CIFX_DRV_HWIF
if ( ((ptDevice->hwif_read) && (NULL == ptDevice->hwif_write)) ||
((ptDevice->hwif_write) && (NULL == ptDevice->hwif_read))) {
fprintf(stderr, "Error initializing device! Misconfigured HW-Function Interface (read- or write-function is not defined)!");
return CIFX_INVALID_PARAMETER;
}
#endif
if(NULL == (ptInternalDev = (PCIFX_DEVICE_INTERNAL_T)malloc(sizeof(*ptInternalDev))))
{
fprintf(stderr,"Error allocating internal device structures");
} else if(NULL == (ptDevInstance = (PDEVICEINSTANCE)malloc(sizeof(*ptDevInstance))))
{
fprintf(stderr, "Error allocating internal device structures");
free(ptInternalDev);
ptInternalDev = NULL;
} else
{
memset(ptDevInstance, 0, sizeof(*ptDevInstance));
memset(ptInternalDev, 0, sizeof(*ptInternalDev));
ptInternalDev->userdevice = ptDevice;
ptInternalDev->devinstance = ptDevInstance;
ptInternalDev->user_card = user_card;
ptDevInstance->pvOSDependent = (void*)ptInternalDev;
ptDevInstance->pbDPM = (unsigned char*)ptDevice->dpm;
ptDevInstance->ulDPMSize = ptDevice->dpmlen;
ptDevInstance->ulPhysicalAddress = ptDevice->dpmaddr;
ptDevInstance->pbExtendedMemory = (uint8_t*)ptDevice->extmem;
ptDevInstance->ulExtendedMemorySize = ptDevice->extmemlen;
#ifdef CIFX_DRV_HWIF
/* set to the linux default read/write function, to be able to handle all devices (also memory mapped) */
ptDevInstance->pfnHwIfRead = HWIFDPMRead;
ptDevInstance->pfnHwIfWrite = HWIFDPMWrite;
#endif
#ifdef CIFX_TOOLKIT_DMA
if (ptDevice->dma_buffer_cnt)
{
int i = 0;
ptDevInstance->ulDMABufferCount = ptDevice->dma_buffer_cnt;
for (i=0;i<ptDevice->dma_buffer_cnt;i++)
{
ptDevInstance->atDmaBuffers[i].ulSize = ptDevice->dma_buffer[i].ulSize;
ptDevInstance->atDmaBuffers[i].ulPhysicalAddress = ptDevice->dma_buffer[i].ulPhysicalAddress;
ptDevInstance->atDmaBuffers[i].pvBuffer = ptDevice->dma_buffer[i].pvBuffer;
ptDevInstance->atDmaBuffers[i].pvUser = NULL;
}
}
#endif
if(ptDevice->notify)
{
ptDevInstance->pfnNotify = cifXWrapEvent;
}
snprintf(ptDevInstance->szName,
sizeof(ptDevInstance->szName),
"cifX%u",
num);
if (ptDevice->uio_num>=0) {
/* extract alias from uio device name (can be used via device-tree) */
char* szAlias = cifx_uio_get_device_alias(ptDevice->uio_num);
if (szAlias != NULL) {
snprintf(ptDevInstance->szAlias,
sizeof(ptDevInstance->szName),
"%s",
szAlias);
free(szAlias);
}
}
/* Default to no logfile */
ptInternalDev->log_file = NULL;
/* Create log file if neccessary */
if(g_ulTraceLevel > 0)
{
size_t pathlen = strlen(g_szDriverBaseDir) + sizeof(ptDevInstance->szName) + 2 + 4; /* +2 for 1x NUL and 1 additional '/'
+4 for extension ".log" */
char* logfilepath = malloc(pathlen);
snprintf(logfilepath, pathlen, "%s/%s.log", g_szDriverBaseDir, ptDevInstance->szName);
if (g_logfd == 0) {
ptInternalDev->log_file = fopen(logfilepath, "w+");
} else {
ptInternalDev->log_file = g_logfd;
}
if( NULL == ptInternalDev->log_file)
{
perror("Error opening logfile. Traces will be printed to console!");
} else
{
DRIVER_INFORMATION tDriverInfo;
CIFXHANDLE hDrv;
/* Insert header into log file */
USER_Trace(ptDevInstance, 0, "----- cifX Driver Log started ---------------------");
if (xDriverOpen(&hDrv)) {
USER_Trace(ptDevInstance, 0, " %s / Error retrieving Toolkit version", LINUXCIFXDRV_VERSION);
} else {
xDriverGetInformation( hDrv, sizeof(tDriverInfo), &tDriverInfo);
xDriverClose(hDrv);
USER_Trace(ptDevInstance, 0, " %s / Toolkit %s", LINUXCIFXDRV_VERSION, tDriverInfo.abDriverVersion);
}
USER_Trace(ptDevInstance, 0, " Name : %s", ptDevInstance->szName);
USER_Trace(ptDevInstance, 0, " DPM : 0x%lx, len=%lu", ptDevInstance->ulPhysicalAddress, ptDevInstance->ulDPMSize);
USER_Trace(ptDevInstance, 0, "---------------------------------------------------");
}
free(logfilepath);
}
if(ptDevice->force_ram)
{
ptDevInstance->eDeviceType = eCIFX_DEVICE_RAM_BASED;
} else
{
if (ptDevice->uio_num>=0) {
/* in case of uio device device-type can be given (device-tree) */
ptDevInstance->eDeviceType = cifx_uio_get_device_startuptype(ptDevice->uio_num);
} else {
ptDevInstance->eDeviceType = eCIFX_DEVICE_AUTODETECT;
}
}
if(ptDevice->pci_card != 0)
{
#ifndef CIFX_TOOLKIT_DISABLEPCI
/* Try to find the card on the PCI bus. If it is not found, deny to work with this card */
if(!match_pci_card(ptDevInstance, ptDevice->dpmaddr))
{
/* Don't add this device */
if (g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInstance,
TRACE_LEVEL_ERROR,
"Error finding pci device (Phys. Addr 0x%lx) on PCI bus",
ptDevice->dpmaddr);
}
} else
{
ptDevInstance->fPCICard = 1;
ret = CIFX_NO_ERROR;
}
#else
if (g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
USER_Trace(ptDevInstance,
TRACE_LEVEL_ERROR,
"cifX Driver was compiled without PCI support. Unable to handle requested PCI card @0x%lx!",
ptDevice->dpmaddr);
}
#endif
} else
{
ptDevInstance->fPCICard = 0;
ret = CIFX_NO_ERROR;
}
#ifdef CIFX_DRV_HWIF
/* initialize the hardware function interface */
if (ptDevice->hwif_init) {
if (CIFX_NO_ERROR != (ret = ptDevice->hwif_init( ptDevice))) {
if (g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
char szError[1024] ={0};
USER_Trace(ptDevInstance,
TRACE_LEVEL_ERROR,
"Failed to initialize custom hardware interface. 'hwif_init' returns 0x%lx - %s! Skip adding custom device to toolkit!",
(unsigned int)ret,
((CIFX_NO_ERROR == xDriverGetErrorDescription( ret, szError, sizeof(szError))) ? szError : "Unknown error"));
}
}
}
#endif
/* Add the device to the toolkits handled device list */
if(CIFX_NO_ERROR == ret) {
if ((ret = cifXTKitAddDevice(ptDevInstance))) {
if (g_ulTraceLevel & TRACE_LEVEL_ERROR)
{
char szError[1024] ={0};
xDriverGetErrorDescription( ret, szError, sizeof(szError));
USER_Trace(ptDevInstance, 0, "Error: 0x%X, <%s>\n", (unsigned int)ret, szError);
}
#ifdef CIFX_DRV_HWIF
/* de-initialize the hardware function interface */
if (ptDevice->hwif_deinit)
ptDevice->hwif_deinit( ptDevice);
#endif
}
}
}
if(CIFX_NO_ERROR != ret)
{
free(ptDevInstance);
free(ptInternalDev);
#ifdef CIFXETHERNET
} else
{
CIFX_DEVICE_INFORMATION tDevInfo;
OS_Memset(&tDevInfo, 0, sizeof(tDevInfo));
/* Initalize file information structure */
tDevInfo.ulDeviceNumber = ptDevInstance->ulDeviceNumber;
tDevInfo.ulSerialNumber = ptDevInstance->ulSerialNumber;
tDevInfo.ulChannel = CIFX_SYSTEM_DEVICE;
tDevInfo.ptDeviceInstance = ptDevInstance;
if (0 != USER_GetEthernet( &tDevInfo))
{
NETX_ETH_DEV_CFG_T config;
sprintf( config.cifx_name, "%s", ptDevInstance->szName);
if (NULL != cifxeth_create_device( &config))
{
ptInternalDev->eth_support = 1;
if (g_ulTraceLevel & TRACE_LEVEL_INFO)
{