aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/epl/ShbIpc-LinuxKernel.c
blob: 1d3cb3f13de1f09b2ae16b3f76cfd32f9712eb9f (plain)
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
/****************************************************************************

  (c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29
      www.systec-electronic.com

  Project:      Project independend shared buffer (linear + circular)

  Description:  Implementation of platform specific part for the
                shared buffer
                (Implementation for Linux KernelSpace)

  License:

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. Neither the name of SYSTEC electronic GmbH nor the names of its
       contributors may be used to endorse or promote products derived
       from this software without prior written permission. For written
       permission, please contact info@systec-electronic.com.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    Severability Clause:

        If a provision of this License is or becomes illegal, invalid or
        unenforceable in any jurisdiction, that shall not affect:
        1. the validity or enforceability in that jurisdiction of any other
           provision of this License; or
        2. the validity or enforceability in other jurisdictions of that or
           any other provision of this License.

  -------------------------------------------------------------------------

  2006/06/28 -rs:   V 1.00 (initial version)

****************************************************************************/

#include "global.h"
#include "SharedBuff.h"
#include "ShbIpc.h"
#include "ShbLinuxKernel.h"
#include "Debug.h"

#include <linux/string.h>
#include <linux/module.h>
#include <asm/processor.h>
//#include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/param.h>
#include <linux/spinlock.h>
#include <linux/wait.h>
#include <linux/completion.h>

/***************************************************************************/
/*                                                                         */
/*                                                                         */
/*          G L O B A L   D E F I N I T I O N S                            */
/*                                                                         */
/*                                                                         */
/***************************************************************************/

#if (!defined(SHBIPC_INLINED)) || defined(SHBIPC_INLINE_ENABLED)

//---------------------------------------------------------------------------
//  Configuration
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
//  Constant definitions
//---------------------------------------------------------------------------

#define MAX_LEN_BUFFER_ID       256

#define TIMEOUT_ENTER_ATOMIC    1000	// (ms) for debgging: INFINITE
#define TIMEOUT_TERM_THREAD     1000
#define INFINITE                3600

#define SBI_MAGIC_ID            0x5342492B	// magic ID ("SBI+")
#define SBH_MAGIC_ID            0x5342482A	// magic ID ("SBH*")

#define INVALID_ID              -1

#define TABLE_SIZE              10

//---------------------------------------------------------------------------
//  Local types
//---------------------------------------------------------------------------

// This structure is the common header for the shared memory region used
// by all processes attached this shared memory. It includes common
// information to administrate/manage the shared buffer from a couple of
// separated processes (e.g. the refernce counter). This structure is
// located at the start of the shared memory region itself and exists
// consequently only one times per shared memory instance.
typedef struct {

	unsigned long m_ulShMemSize;
	unsigned long m_ulRefCount;
	int m_iBufferId;
//    int                 m_iUserSpaceMem;           //0 for userspace mem   !=0 kernelspace mem
	spinlock_t m_SpinlockBuffAccess;
	BOOL m_fNewData;
	BOOL m_fJobReady;
	wait_queue_head_t m_WaitQueueNewData;
	wait_queue_head_t m_WaitQueueJobReady;

#ifndef NDEBUG
	unsigned long m_ulOwnerProcID;
#endif

} tShbMemHeader;

// This structure is the "external entry point" from a separate process
// to get access to a shared buffer. This structure includes all platform
// resp. target specific information to administrate/manage the shared
// buffer from a separate process. Every process attached to the shared
// buffer has its own runtime instance of this structure with its individual
// runtime data (e.g. the scope of an event handle is limitted to the
// owner process only). The structure member <m_pShbMemHeader> points
// to the (process specific) start address of the shared memory region
// itself.
typedef struct {
	unsigned long m_SbiMagicID;	// magic ID ("SBI+")
//    void*               m_pSharedMem;
	int m_tThreadNewDataId;
	long m_lThreadNewDataNice;	// nice value of the new data thread
	int m_tThreadJobReadyId;
	unsigned long m_ulFlagsBuffAccess;	// d.k. moved from tShbMemHeader, because each
	// process needs to store the interrupt flags separately
	tSigHndlrNewData m_pfnSigHndlrNewData;
	unsigned long m_ulTimeOutJobReady;
	tSigHndlrJobReady m_pfnSigHndlrJobReady;
	tShbMemHeader *m_pShbMemHeader;
	int m_iThreadTermFlag;
	struct completion m_CompletionNewData;
/*
    struct semaphore    *m_pSemBuffAccess;
    struct semaphore    *m_pSemNewData;
    struct semaphore    *m_pSemStopSignalingNewData;
    struct semaphore    *m_pSemJobReady;
*/
#ifndef NDEBUG
	unsigned long m_ulThreadIDNewData;
	unsigned long m_ulThreadIDJobReady;
#endif
} tShbMemInst;

//---------------------------------------------------------------------------
//  Prototypes of internal functions
//---------------------------------------------------------------------------

//tShbMemInst*            ShbIpcGetShbMemInst         (tShbInstance pShbInstance_p);
//tShbMemHeader*          ShbIpcGetShbMemHeader       (tShbMemInst* pShbMemInst_p);

//---------------------------------------------------------------------------
//  Get pointer to process local information structure
//---------------------------------------------------------------------------

static inline tShbMemInst *ShbIpcGetShbMemInst(tShbInstance pShbInstance_p)
{

	tShbMemInst *pShbMemInst;

	pShbMemInst = (tShbMemInst *) pShbInstance_p;

	return (pShbMemInst);

}

//---------------------------------------------------------------------------
//  Get pointer to shared memory header
//---------------------------------------------------------------------------

static inline tShbMemHeader *ShbIpcGetShbMemHeader(tShbMemInst * pShbMemInst_p)
{

	tShbMemHeader *pShbMemHeader;

	pShbMemHeader = pShbMemInst_p->m_pShbMemHeader;

	return (pShbMemHeader);

}

//  Get pointer to process local information structure
//#define ShbIpcGetShbMemInst(pShbInstance_p) ((tShbMemInst*)pShbInstance_p)

//  Get pointer to shared memory header
//#define ShbIpcGetShbMemHeader(pShbMemInst_p) (pShbMemInst_p->m_pShbMemHeader)

// not inlined internal functions
int ShbIpcThreadSignalNewData(void *pvThreadParam_p);
int ShbIpcThreadSignalJobReady(void *pvThreadParam_p);
#endif

//---------------------------------------------------------------------------
// modul globale vars
//---------------------------------------------------------------------------

#if !defined(SHBIPC_INLINE_ENABLED)
struct sShbMemTable *psMemTableElementFirst_g;

static void *ShbIpcAllocPrivateMem(unsigned long ulMemSize_p);
static int ShbIpcFindListElement(int iBufferId,
				 struct sShbMemTable
				 **ppsReturnMemTableElement);
static void ShbIpcAppendListElement(struct sShbMemTable *sNewMemTableElement);
static void ShbIpcDeleteListElement(int iBufferId);
static void ShbIpcCrc32GenTable(unsigned long aulCrcTable[256]);
static unsigned long ShbIpcCrc32GetCrc(const char *pcString,
				       unsigned long aulCrcTable[256]);

#endif

//=========================================================================//
//                                                                         //
//          P U B L I C   F U N C T I O N S                                //
//                                                                         //
//=========================================================================//

#if !defined(SHBIPC_INLINE_ENABLED)
// not inlined external functions

//---------------------------------------------------------------------------
//  Initialize IPC for Shared Buffer Module
//---------------------------------------------------------------------------

tShbError ShbIpcInit(void)
{
	psMemTableElementFirst_g = NULL;
	return (kShbOk);

}

//---------------------------------------------------------------------------
//  Deinitialize IPC for Shared Buffer Module
//---------------------------------------------------------------------------

tShbError ShbIpcExit(void)
{

	return (kShbOk);

}

//---------------------------------------------------------------------------
//  Allocate Shared Buffer
//---------------------------------------------------------------------------

tShbError ShbIpcAllocBuffer(unsigned long ulBufferSize_p,
			    const char *pszBufferID_p,
			    tShbInstance * ppShbInstance_p,
			    unsigned int *pfShbNewCreated_p)
{
	tShbError ShbError;
	int iBufferId = 0;
	unsigned long ulCrc32 = 0;
	unsigned int uiFirstProcess = 0;
	unsigned long ulShMemSize;
	tShbMemHeader *pShbMemHeader;
	tShbMemInst *pShbMemInst = NULL;
	tShbInstance pShbInstance;
	unsigned int fShMemNewCreated = FALSE;
	void *pSharedMem = NULL;
	unsigned long aulCrcTable[256];
	struct sShbMemTable *psMemTableElement;

	DEBUG_LVL_29_TRACE0("ShbIpcAllocBuffer \n");
	ulShMemSize = ulBufferSize_p + sizeof(tShbMemHeader);

	//create Buffer ID
	ShbIpcCrc32GenTable(aulCrcTable);
	ulCrc32 = ShbIpcCrc32GetCrc(pszBufferID_p, aulCrcTable);
	iBufferId = ulCrc32;
	DEBUG_LVL_29_TRACE2
	    ("ShbIpcAllocBuffer BufferSize:%d sizeof(tShb..):%d\n",
	     ulBufferSize_p, sizeof(tShbMemHeader));
	DEBUG_LVL_29_TRACE2("ShbIpcAllocBuffer BufferId:%d MemSize:%d\n",
			    iBufferId, ulShMemSize);
	//---------------------------------------------------------------
	// (1) open an existing or create a new shared memory
	//---------------------------------------------------------------
	//test if buffer already exists
	if (ShbIpcFindListElement(iBufferId, &psMemTableElement) == 0) {
		//Buffer already exists
		fShMemNewCreated = FALSE;
		pSharedMem = psMemTableElement->m_pBuffer;
		DEBUG_LVL_29_TRACE1
		    ("ShbIpcAllocBuffer attach Buffer at:%p Id:%d\n",
		     pSharedMem);
		uiFirstProcess = 1;
	} else {
		//create new Buffer
		fShMemNewCreated = TRUE;
		uiFirstProcess = 0;
		pSharedMem = kmalloc(ulShMemSize, GFP_KERNEL);
		DEBUG_LVL_29_TRACE2
		    ("ShbIpcAllocBuffer Create New Buffer at:%p Id:%d\n",
		     pSharedMem, iBufferId);
		if (pSharedMem == NULL) {
			//unable to create mem
			ShbError = kShbOutOfMem;
			goto Exit;
		}
		DEBUG_LVL_29_TRACE0("ShbIpcAllocBuffer create semas\n");
		// append Element to Mem Table
		psMemTableElement =
		    kmalloc(sizeof(struct sShbMemTable), GFP_KERNEL);
		psMemTableElement->m_iBufferId = iBufferId;
		psMemTableElement->m_pBuffer = pSharedMem;
		psMemTableElement->m_psNextMemTableElement = NULL;
		ShbIpcAppendListElement(psMemTableElement);
	}

	DEBUG_LVL_29_TRACE0("ShbIpcAllocBuffer update header\n");
	//update header
	pShbMemHeader = (tShbMemHeader *) pSharedMem;
	DEBUG_LVL_29_TRACE1
	    ("ShbIpcAllocBuffer 0 pShbMemHeader->m_ulShMemSize: %d\n",
	     pShbMemHeader->m_ulShMemSize);
	// allocate a memory block from process specific mempool to save
	// process local information to administrate/manage the shared buffer
	DEBUG_LVL_29_TRACE0("ShbIpcAllocBuffer alloc private mem\n");
	pShbMemInst =
	    (tShbMemInst *) ShbIpcAllocPrivateMem(sizeof(tShbMemInst));
	if (pShbMemInst == NULL) {
		ShbError = kShbOutOfMem;
		goto Exit;
	}
	// reset complete header to default values
	//pShbMemInst->m_SbiMagicID                             = SBI_MAGIC_ID;
//    pShbMemInst->m_pSharedMem                               = pSharedMem;
	pShbMemInst->m_tThreadNewDataId = INVALID_ID;
	pShbMemInst->m_tThreadJobReadyId = INVALID_ID;
	pShbMemInst->m_pfnSigHndlrNewData = NULL;
	pShbMemInst->m_ulTimeOutJobReady = 0;
	pShbMemInst->m_pfnSigHndlrJobReady = NULL;
	pShbMemInst->m_pShbMemHeader = pShbMemHeader;
	pShbMemInst->m_iThreadTermFlag = 0;

	// initialize completion etc.
	init_completion(&pShbMemInst->m_CompletionNewData);

	ShbError = kShbOk;
	if (fShMemNewCreated) {
		// this process was the first who wanted to use the shared memory,
		// so a new shared memory was created
		// -> setup new header information inside the shared memory region
		//    itself
		pShbMemHeader->m_ulShMemSize = ulShMemSize;
		pShbMemHeader->m_ulRefCount = 1;
		pShbMemHeader->m_iBufferId = iBufferId;
		// initialize spinlock
		spin_lock_init(&pShbMemHeader->m_SpinlockBuffAccess);
		// initialize wait queues
		init_waitqueue_head(&pShbMemHeader->m_WaitQueueNewData);
		init_waitqueue_head(&pShbMemHeader->m_WaitQueueJobReady);
	} else {
		// any other process has created the shared memory and this
		// process only has to attach to it
		// -> check and update existing header information inside the
		//    shared memory region itself
		if (pShbMemHeader->m_ulShMemSize != ulShMemSize) {
			ShbError = kShbOpenMismatch;
			goto Exit;
		}
		pShbMemHeader->m_ulRefCount++;
	}

      Exit:
	pShbInstance = (tShbInstance *) pShbMemInst;
	*pfShbNewCreated_p = fShMemNewCreated;
	*ppShbInstance_p = pShbInstance;
	return (ShbError);

}

//---------------------------------------------------------------------------
//  Release Shared Buffer
//---------------------------------------------------------------------------

tShbError ShbIpcReleaseBuffer(tShbInstance pShbInstance_p)
{
	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	tShbError ShbError;
	tShbError ShbError2;

	DEBUG_LVL_26_TRACE1("ShbIpcReleaseBuffer(%p)\n", pShbInstance_p);
	if (pShbInstance_p == NULL) {
		return (kShbOk);
	}
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance_p);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);

	// stop threads in any case, because they are bound to that specific instance
	ShbError2 = ShbIpcStopSignalingNewData(pShbInstance_p);
	// d.k.: Whats up with JobReady thread?
	//       Just wake it up, but without setting the semaphore variable
	wake_up_interruptible(&pShbMemHeader->m_WaitQueueJobReady);

	if (!--pShbMemHeader->m_ulRefCount) {
		ShbError = kShbOk;
		// delete mem table element
		ShbIpcDeleteListElement(pShbMemHeader->m_iBufferId);
		// delete shared mem
		kfree(pShbMemInst->m_pShbMemHeader);
	} else {
		ShbError = kShbMemUsedByOtherProcs;
	}
	//delete privat mem
	kfree(pShbMemInst);
	return (ShbError);
}

#endif // !defined(SHBIPC_INLINE_ENABLED)

#if (!defined(SHBIPC_INLINED)) || defined(SHBIPC_INLINE_ENABLED)

//---------------------------------------------------------------------------
//  Enter atomic section for Shared Buffer access
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcEnterAtomicSection(tShbInstance pShbInstance_p)
{

	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	tShbError ShbError = kShbOk;

	if (pShbInstance_p == NULL) {
		ShbError = kShbInvalidArg;
		goto Exit;
	}
	DEBUG_LVL_29_TRACE0("enter atomic\n");
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance_p);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);

	// lock interrupts
	spin_lock_irqsave(&pShbMemHeader->m_SpinlockBuffAccess,
			  pShbMemInst->m_ulFlagsBuffAccess);

      Exit:
	return ShbError;

}

//---------------------------------------------------------------------------
//  Leave atomic section for Shared Buffer access
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcLeaveAtomicSection(tShbInstance pShbInstance_p)
{

	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	tShbError ShbError = kShbOk;

	if (pShbInstance_p == NULL) {
		ShbError = kShbInvalidArg;
		goto Exit;
	}
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance_p);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);
	// unlock interrupts
	spin_unlock_irqrestore(&pShbMemHeader->m_SpinlockBuffAccess,
			       pShbMemInst->m_ulFlagsBuffAccess);

      Exit:
	DEBUG_LVL_29_TRACE0("Leave Atomic \n");
	return ShbError;

}

//---------------------------------------------------------------------------
//  Start signaling of new data (called from reading process)
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcStartSignalingNewData(tShbInstance
						      pShbInstance_p,
						      tSigHndlrNewData
						      pfnSignalHandlerNewData_p,
						      tShbPriority
						      ShbPriority_p)
{
	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	tShbError ShbError;

	DEBUG_LVL_29_TRACE0("------->ShbIpcStartSignalingNewData\n");
	if ((pShbInstance_p == NULL) || (pfnSignalHandlerNewData_p == NULL)) {
		return (kShbInvalidArg);
	}

	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance_p);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);
	ShbError = kShbOk;

	if ((pShbMemInst->m_tThreadNewDataId != INVALID_ID)
	    || (pShbMemInst->m_pfnSigHndlrNewData != NULL)) {
		ShbError = kShbAlreadySignaling;
		goto Exit;
	}
	DEBUG_LVL_26_TRACE2
	    ("ShbIpcStartSignalingNewData(%p) m_pfnSigHndlrNewData = %p\n",
	     pShbInstance_p, pfnSignalHandlerNewData_p);
	pShbMemInst->m_pfnSigHndlrNewData = pfnSignalHandlerNewData_p;
	pShbMemHeader->m_fNewData = FALSE;
	pShbMemInst->m_iThreadTermFlag = 0;

	switch (ShbPriority_p) {
	case kShbPriorityLow:
		pShbMemInst->m_lThreadNewDataNice = -2;
		break;

	case kShbPriorityNormal:
		pShbMemInst->m_lThreadNewDataNice = -9;
		break;

	case kshbPriorityHigh:
		pShbMemInst->m_lThreadNewDataNice = -20;
		break;

	}

	//create thread for signalling new data
	pShbMemInst->m_tThreadNewDataId =
	    kernel_thread(ShbIpcThreadSignalNewData, pShbInstance_p,
			  CLONE_KERNEL);

      Exit:
	return ShbError;

}

//---------------------------------------------------------------------------
//  Stop signaling of new data (called from reading process)
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcStopSignalingNewData(tShbInstance
						     pShbInstance_p)
{
	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	tShbError ShbError;

	DEBUG_LVL_29_TRACE0("------->ShbIpcStopSignalingNewData\n");
	if (pShbInstance_p == NULL) {
		return (kShbInvalidArg);
	}
	ShbError = kShbOk;
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance_p);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);

	DEBUG_LVL_26_TRACE2
	    ("ShbIpcStopSignalingNewData(%p) pfnSignHndlrNewData=%p\n",
	     pShbInstance_p, pShbMemInst->m_pfnSigHndlrNewData);
	if (pShbMemInst->m_pfnSigHndlrNewData != NULL) {	// signal handler was set before
		int iErr;
		//set termination flag in mem header
		pShbMemInst->m_iThreadTermFlag = 1;

		// check if thread is still running at all by sending the null-signal to this thread
		/* iErr = kill_proc(pShbMemInst->m_tThreadNewDataId, 0, 1); */
		iErr = send_sig(0, pShbMemInst->m_tThreadNewDataId, 1);
		if (iErr == 0) {
			// wake up thread, because it is still running
			wake_up_interruptible(&pShbMemHeader->
					      m_WaitQueueNewData);

			//wait for termination of thread
			wait_for_completion(&pShbMemInst->m_CompletionNewData);
		}

		pShbMemInst->m_pfnSigHndlrNewData = NULL;
		pShbMemInst->m_tThreadNewDataId = INVALID_ID;
	}

	return ShbError;

}

//---------------------------------------------------------------------------
//  Signal new data (called from writing process)
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcSignalNewData(tShbInstance pShbInstance_p)
{
	tShbMemHeader *pShbMemHeader;

	if (pShbInstance_p == NULL) {
		return (kShbInvalidArg);
	}
	pShbMemHeader =
	    ShbIpcGetShbMemHeader(ShbIpcGetShbMemInst(pShbInstance_p));
	//set semaphore
	pShbMemHeader->m_fNewData = TRUE;
	DEBUG_LVL_29_TRACE0("ShbIpcSignalNewData set Sem -> New Data\n");

	wake_up_interruptible(&pShbMemHeader->m_WaitQueueNewData);
	return (kShbOk);
}

//---------------------------------------------------------------------------
//  Start signaling for job ready (called from waiting process)
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcStartSignalingJobReady(tShbInstance
						       pShbInstance_p,
						       unsigned long
						       ulTimeOut_p,
						       tSigHndlrJobReady
						       pfnSignalHandlerJobReady_p)
{
	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	tShbError ShbError;

	if ((pShbInstance_p == NULL) || (pfnSignalHandlerJobReady_p == NULL)) {
		return (kShbInvalidArg);
	}
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance_p);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);

	ShbError = kShbOk;
	if ((pShbMemInst->m_tThreadJobReadyId != INVALID_ID)
	    || (pShbMemInst->m_pfnSigHndlrJobReady != NULL)) {
		ShbError = kShbAlreadySignaling;
		goto Exit;
	}
	pShbMemInst->m_ulTimeOutJobReady = ulTimeOut_p;
	pShbMemInst->m_pfnSigHndlrJobReady = pfnSignalHandlerJobReady_p;
	pShbMemHeader->m_fJobReady = FALSE;
	//create thread for signalling new data
	pShbMemInst->m_tThreadJobReadyId =
	    kernel_thread(ShbIpcThreadSignalJobReady, pShbInstance_p,
			  CLONE_KERNEL);
      Exit:
	return ShbError;
}

//---------------------------------------------------------------------------
//  Signal job ready (called from executing process)
//---------------------------------------------------------------------------

INLINE_FUNCTION tShbError ShbIpcSignalJobReady(tShbInstance pShbInstance_p)
{
	tShbMemHeader *pShbMemHeader;

	DEBUG_LVL_29_TRACE0("ShbIpcSignalJobReady\n");
	if (pShbInstance_p == NULL) {
		return (kShbInvalidArg);
	}
	pShbMemHeader =
	    ShbIpcGetShbMemHeader(ShbIpcGetShbMemInst(pShbInstance_p));
	//set semaphore
	pShbMemHeader->m_fJobReady = TRUE;
	DEBUG_LVL_29_TRACE0("ShbIpcSignalJobReady set Sem -> Job Ready \n");

	wake_up_interruptible(&pShbMemHeader->m_WaitQueueJobReady);
	return (kShbOk);
}

//---------------------------------------------------------------------------
//  Get pointer to common used share memory area
//---------------------------------------------------------------------------

INLINE_FUNCTION void *ShbIpcGetShMemPtr(tShbInstance pShbInstance_p)
{

	tShbMemHeader *pShbMemHeader;
	void *pShbShMemPtr;

	pShbMemHeader =
	    ShbIpcGetShbMemHeader(ShbIpcGetShbMemInst(pShbInstance_p));
	if (pShbMemHeader != NULL) {
		pShbShMemPtr = (BYTE *) pShbMemHeader + sizeof(tShbMemHeader);
	} else {
		pShbShMemPtr = NULL;
	}

	return (pShbShMemPtr);

}

#endif

//=========================================================================//
//                                                                         //
//          P R I V A T E   F U N C T I O N S                              //
//                                                                         //
//=========================================================================//

#if !defined(SHBIPC_INLINE_ENABLED)

//---------------------------------------------------------------------------
//  Get pointer to process local information structure
//---------------------------------------------------------------------------

/*tShbMemInst*  ShbIpcGetShbMemInst (
    tShbInstance pShbInstance_p)
{

tShbMemInst*  pShbMemInst;

    pShbMemInst = (tShbMemInst*)pShbInstance_p;

    return (pShbMemInst);

}
*/

//---------------------------------------------------------------------------
//  Get pointer to shared memory header
//---------------------------------------------------------------------------

/*tShbMemHeader*  ShbIpcGetShbMemHeader (
    tShbMemInst* pShbMemInst_p)
{

tShbMemHeader*  pShbMemHeader;

    pShbMemHeader = pShbMemInst_p->m_pShbMemHeader;

    return (pShbMemHeader);

}
*/

//---------------------------------------------------------------------------
//  Allocate a memory block from process specific mempool
//---------------------------------------------------------------------------

static void *ShbIpcAllocPrivateMem(unsigned long ulMemSize_p)
{
	tShbError ShbError;
	void *pMem;

	DEBUG_LVL_29_TRACE0("ShbIpcAllocPrivateMem \n");
	//get private mem
	pMem = kmalloc(ulMemSize_p, GFP_KERNEL);
	if (pMem == NULL) {
		//unable to create mem
		ShbError = kShbOutOfMem;
		goto Exit;
	}
      Exit:
	return (pMem);

}

//---------------------------------------------------------------------------
//  Thread for new data signaling
//---------------------------------------------------------------------------

int ShbIpcThreadSignalNewData(void *pvThreadParam_p)
{
	tShbInstance pShbInstance;
	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	int iRetVal = -1;
	int fCallAgain;

	daemonize("ShbND%p", pvThreadParam_p);
	allow_signal(SIGTERM);
	pShbInstance = (tShbMemInst *) pvThreadParam_p;
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);

	DEBUG_LVL_26_TRACE1("ShbIpcThreadSignalNewData(%p)\n", pvThreadParam_p);

	set_user_nice(current, pShbMemInst->m_lThreadNewDataNice);

//            DEBUG_LVL_29_TRACE1("ShbIpcThreadSignalNewData wait for New Data Sem %p\n",pShbMemInst->m_pSemNewData);
	do {
		iRetVal =
		    wait_event_interruptible(pShbMemHeader->m_WaitQueueNewData,
					     (pShbMemInst->m_iThreadTermFlag !=
					      0)
					     || (pShbMemHeader->m_fNewData !=
						 FALSE));

		if (iRetVal != 0) {	// signal pending
			break;
		}

		if (pShbMemHeader->m_fNewData != FALSE) {
			pShbMemHeader->m_fNewData = FALSE;
			do {
				fCallAgain =
				    pShbMemInst->
				    m_pfnSigHndlrNewData(pShbInstance);
				// call scheduler, which will execute any task with higher priority
				schedule();
			} while (fCallAgain != FALSE);
		}
	} while (pShbMemInst->m_iThreadTermFlag == 0);
	DEBUG_LVL_29_TRACE0("ShbIpcThreadSignalNewData terminated \n");
	//set thread completed
	complete_and_exit(&pShbMemInst->m_CompletionNewData, 0);
	return 0;
}

//---------------------------------------------------------------------------
//  Thread for new data Job Ready signaling
//---------------------------------------------------------------------------

int ShbIpcThreadSignalJobReady(void *pvThreadParam_p)
{
	tShbInstance pShbInstance;
	tShbMemInst *pShbMemInst;
	tShbMemHeader *pShbMemHeader;
	long lTimeOut;
	int iRetVal = -1;

	daemonize("ShbJR%p", pvThreadParam_p);
	allow_signal(SIGTERM);
	pShbInstance = (tShbMemInst *) pvThreadParam_p;
	pShbMemInst = ShbIpcGetShbMemInst(pShbInstance);
	pShbMemHeader = ShbIpcGetShbMemHeader(pShbMemInst);

	DEBUG_LVL_29_TRACE0
	    ("ShbIpcThreadSignalJobReady wait for job ready Sem\n");
	if (pShbMemInst->m_ulTimeOutJobReady != 0) {
		lTimeOut = (long)pShbMemInst->m_ulTimeOutJobReady;
		//wait for job ready semaphore
		iRetVal =
		    wait_event_interruptible_timeout(pShbMemHeader->
						     m_WaitQueueJobReady,
						     (pShbMemHeader->
						      m_fJobReady != FALSE),
						     lTimeOut);
	} else {
		//wait for job ready semaphore
		iRetVal =
		    wait_event_interruptible(pShbMemHeader->m_WaitQueueJobReady,
					     (pShbMemHeader->m_fJobReady !=
					      FALSE));
	}

	if (pShbMemInst->m_pfnSigHndlrJobReady != NULL) {
		//call Handler
		pShbMemInst->m_pfnSigHndlrJobReady(pShbInstance,
						   !pShbMemHeader->m_fJobReady);
	}

	pShbMemInst->m_pfnSigHndlrJobReady = NULL;
	return 0;
}

//Build the crc table
static void ShbIpcCrc32GenTable(unsigned long aulCrcTable[256])
{
	unsigned long ulCrc, ulPoly;
	int iIndexI, iIndexJ;

	ulPoly = 0xEDB88320L;
	for (iIndexI = 0; iIndexI < 256; iIndexI++) {
		ulCrc = iIndexI;
		for (iIndexJ = 8; iIndexJ > 0; iIndexJ--) {
			if (ulCrc & 1) {
				ulCrc = (ulCrc >> 1) ^ ulPoly;
			} else {
				ulCrc >>= 1;
			}
		}
		aulCrcTable[iIndexI] = ulCrc;
	}
}

//Calculate the crc value
static unsigned long ShbIpcCrc32GetCrc(const char *pcString,
				       unsigned long aulCrcTable[256])
{
	unsigned long ulCrc;
	int iIndex;

	ulCrc = 0xFFFFFFFF;
	for (iIndex = 0; iIndex < strlen(pcString); iIndex++) {
		ulCrc =
		    ((ulCrc >> 8) & 0x00FFFFFF) ^
		    aulCrcTable[(ulCrc ^ pcString[iIndex]) & 0xFF];
	}
	return (ulCrc ^ 0xFFFFFFFF);

}

static void ShbIpcAppendListElement(struct sShbMemTable *psNewMemTableElement)
{
	struct sShbMemTable *psMemTableElement = psMemTableElementFirst_g;
	psNewMemTableElement->m_psNextMemTableElement = NULL;

	if (psMemTableElementFirst_g != NULL) {	/* sind Elemente vorhanden */
		while (psMemTableElement->m_psNextMemTableElement != NULL) {	/* suche das letzte Element */
			psMemTableElement =
			    psMemTableElement->m_psNextMemTableElement;
		}
		psMemTableElement->m_psNextMemTableElement = psNewMemTableElement;	/*  Haenge das Element hinten an */
	} else {		/* wenn die liste leer ist, bin ich das erste Element */
		psMemTableElementFirst_g = psNewMemTableElement;
	}
}

static int ShbIpcFindListElement(int iBufferId,
				 struct sShbMemTable **ppsReturnMemTableElement)
{
	struct sShbMemTable *psMemTableElement = psMemTableElementFirst_g;
	while (psMemTableElement != NULL) {
		if (psMemTableElement->m_iBufferId == iBufferId) {
//printk("ShbIpcFindListElement Buffer at:%p Id:%d\n",psMemTableElement->m_pBuffer,psMemTableElement->m_iBufferId);
			*ppsReturnMemTableElement = psMemTableElement;
//printk("ShbIpcFindListElement Buffer at:%p Id:%d\n",(*ppsReturnMemTableElement)->m_pBuffer,(*ppsReturnMemTableElement)->m_iBufferId);
			return 0;
		}
		psMemTableElement = psMemTableElement->m_psNextMemTableElement;
	}
	return -1;
}

static void ShbIpcDeleteListElement(int iBufferId)
{
	struct sShbMemTable *psMemTableElement = psMemTableElementFirst_g;
	struct sShbMemTable *psMemTableElementOld = psMemTableElementFirst_g;
	if (psMemTableElement != NULL) {
		while ((psMemTableElement != NULL)
		       && (psMemTableElement->m_iBufferId != iBufferId)) {
			psMemTableElementOld = psMemTableElement;
			psMemTableElement =
			    psMemTableElement->m_psNextMemTableElement;
		}
		if (psMemTableElement != NULL) {
			if (psMemTableElement != psMemTableElementFirst_g) {
				psMemTableElementOld->m_psNextMemTableElement =
				    psMemTableElement->m_psNextMemTableElement;
				kfree(psMemTableElement);
			} else {
				kfree(psMemTableElement);
				psMemTableElementFirst_g = NULL;
			}

		}
	}

}

#endif