Staging: add epl stack
[platform/kernel/linux-starfive.git] / drivers / staging / epl / EplDlluCal.c
1 /****************************************************************************
2
3   (c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29
4       www.systec-electronic.com
5
6   Project:      openPOWERLINK
7
8   Description:  source file for DLL Communication Abstraction Layer module in EPL user part
9
10   License:
11
12     Redistribution and use in source and binary forms, with or without
13     modification, are permitted provided that the following conditions
14     are met:
15
16     1. Redistributions of source code must retain the above copyright
17        notice, this list of conditions and the following disclaimer.
18
19     2. Redistributions in binary form must reproduce the above copyright
20        notice, this list of conditions and the following disclaimer in the
21        documentation and/or other materials provided with the distribution.
22
23     3. Neither the name of SYSTEC electronic GmbH nor the names of its
24        contributors may be used to endorse or promote products derived
25        from this software without prior written permission. For written
26        permission, please contact info@systec-electronic.com.
27
28     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32     COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39     POSSIBILITY OF SUCH DAMAGE.
40
41     Severability Clause:
42
43         If a provision of this License is or becomes illegal, invalid or
44         unenforceable in any jurisdiction, that shall not affect:
45         1. the validity or enforceability in that jurisdiction of any other
46            provision of this License; or
47         2. the validity or enforceability in other jurisdictions of that or
48            any other provision of this License.
49
50   -------------------------------------------------------------------------
51
52                 $RCSfile: EplDlluCal.c,v $
53
54                 $Author: D.Krueger $
55
56                 $Revision: 1.7 $  $Date: 2008/10/17 15:32:32 $
57
58                 $State: Exp $
59
60                 Build Environment:
61                     GCC V3.4
62
63   -------------------------------------------------------------------------
64
65   Revision History:
66
67   2006/06/20 d.k.:   start of the implementation, version 1.00
68
69 ****************************************************************************/
70
71 #include "user/EplDlluCal.h"
72 #include "user/EplEventu.h"
73
74 #include "EplDllCal.h"
75
76 // include only if direct call between user- and kernelspace is enabled
77 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLK)) != 0)
78 #include "kernel/EplDllkCal.h"
79 #endif
80
81
82 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLU)) != 0)
83
84 /***************************************************************************/
85 /*                                                                         */
86 /*                                                                         */
87 /*          G L O B A L   D E F I N I T I O N S                            */
88 /*                                                                         */
89 /*                                                                         */
90 /***************************************************************************/
91
92 //---------------------------------------------------------------------------
93 // const defines
94 //---------------------------------------------------------------------------
95
96 //---------------------------------------------------------------------------
97 // local types
98 //---------------------------------------------------------------------------
99
100 //---------------------------------------------------------------------------
101 // modul globale vars
102 //---------------------------------------------------------------------------
103
104 //---------------------------------------------------------------------------
105 // local function prototypes
106 //---------------------------------------------------------------------------
107
108
109 /***************************************************************************/
110 /*                                                                         */
111 /*                                                                         */
112 /*          C L A S S  EplDlluCal                                          */
113 /*                                                                         */
114 /*                                                                         */
115 /***************************************************************************/
116 //
117 // Description:
118 //
119 //
120 /***************************************************************************/
121
122
123 //=========================================================================//
124 //                                                                         //
125 //          P R I V A T E   D E F I N I T I O N S                          //
126 //                                                                         //
127 //=========================================================================//
128
129 //---------------------------------------------------------------------------
130 // const defines
131 //---------------------------------------------------------------------------
132
133 //---------------------------------------------------------------------------
134 // local types
135 //---------------------------------------------------------------------------
136
137 typedef struct
138 {
139     tEplDlluCbAsnd  m_apfnDlluCbAsnd[EPL_DLL_MAX_ASND_SERVICE_ID];
140
141 } tEplDlluCalInstance;
142
143 //---------------------------------------------------------------------------
144 // local vars
145 //---------------------------------------------------------------------------
146
147 // if no dynamic memory allocation shall be used
148 // define structures statically
149 static tEplDlluCalInstance     EplDlluCalInstance_g;
150
151 //---------------------------------------------------------------------------
152 // local function prototypes
153 //---------------------------------------------------------------------------
154
155 static tEplKernel EplDlluCalSetAsndServiceIdFilter(tEplDllAsndServiceId ServiceId_p, tEplDllAsndFilter Filter_p);
156
157 //=========================================================================//
158 //                                                                         //
159 //          P U B L I C   F U N C T I O N S                                //
160 //                                                                         //
161 //=========================================================================//
162
163 //---------------------------------------------------------------------------
164 //
165 // Function:    EplDlluCalAddInstance()
166 //
167 // Description: add and initialize new instance of DLL CAL module
168 //
169 // Parameters:  none
170 //
171 // Returns:     tEplKernel              = error code
172 //
173 //
174 // State:
175 //
176 //---------------------------------------------------------------------------
177
178 tEplKernel EplDlluCalAddInstance()
179 {
180 tEplKernel      Ret = kEplSuccessful;
181
182     // reset instance structure
183     EPL_MEMSET(&EplDlluCalInstance_g, 0, sizeof (EplDlluCalInstance_g));
184
185     return Ret;
186 }
187
188 //---------------------------------------------------------------------------
189 //
190 // Function:    EplDlluCalDelInstance()
191 //
192 // Description: deletes an instance of DLL CAL module
193 //
194 // Parameters:  none
195 //
196 // Returns:     tEplKernel              = error code
197 //
198 //
199 // State:
200 //
201 //---------------------------------------------------------------------------
202
203 tEplKernel EplDlluCalDelInstance()
204 {
205 tEplKernel      Ret = kEplSuccessful;
206
207     // reset instance structure
208     EPL_MEMSET(&EplDlluCalInstance_g, 0, sizeof (EplDlluCalInstance_g));
209
210     return Ret;
211 }
212
213
214 //---------------------------------------------------------------------------
215 //
216 // Function:    EplDlluCalProcess
217 //
218 // Description: process the passed asynch frame
219 //
220 // Parameters:  pEvent_p                = event containing frame to be processed
221 //
222 // Returns:     tEplKernel              = error code
223 //
224 //
225 // State:
226 //
227 //---------------------------------------------------------------------------
228
229 tEplKernel EplDlluCalProcess(tEplEvent * pEvent_p)
230 {
231 tEplKernel      Ret = kEplSuccessful;
232 tEplMsgType     MsgType;
233 unsigned int    uiAsndServiceId;
234 tEplFrameInfo   FrameInfo;
235
236     if (pEvent_p->m_EventType == kEplEventTypeAsndRx)
237     {
238         FrameInfo.m_pFrame = (tEplFrame*) pEvent_p->m_pArg;
239         FrameInfo.m_uiFrameSize = pEvent_p->m_uiSize;
240         // extract NetTime
241         FrameInfo.m_NetTime = pEvent_p->m_NetTime;
242
243         MsgType = (tEplMsgType)AmiGetByteFromLe(&FrameInfo.m_pFrame->m_le_bMessageType);
244         if (MsgType != kEplMsgTypeAsnd)
245         {
246             Ret = kEplInvalidOperation;
247             goto Exit;
248         }
249
250         uiAsndServiceId = (unsigned int) AmiGetByteFromLe(&FrameInfo.m_pFrame->m_Data.m_Asnd.m_le_bServiceId);
251         if (uiAsndServiceId < EPL_DLL_MAX_ASND_SERVICE_ID)
252         {   // ASnd service ID is valid
253             if (EplDlluCalInstance_g.m_apfnDlluCbAsnd[uiAsndServiceId] != NULL)
254             {   // handler was registered
255                 Ret = EplDlluCalInstance_g.m_apfnDlluCbAsnd[uiAsndServiceId](&FrameInfo);
256             }
257         }
258     }
259
260 Exit:
261     return Ret;
262 }
263
264 //---------------------------------------------------------------------------
265 //
266 // Function:    EplDlluCalRegAsndService()
267 //
268 // Description: registers the specified handler for the specified
269 //              AsndServiceId with the specified node ID filter.
270 //
271 // Parameters:  ServiceId_p             = ASnd Service ID
272 //              pfnDlluCbAsnd_p         = callback function
273 //              Filter_p                = node ID filter
274 //
275 // Returns:     tEplKernel              = error code
276 //
277 //
278 // State:
279 //
280 //---------------------------------------------------------------------------
281
282 tEplKernel EplDlluCalRegAsndService(tEplDllAsndServiceId ServiceId_p, tEplDlluCbAsnd pfnDlluCbAsnd_p, tEplDllAsndFilter Filter_p)
283 {
284 tEplKernel  Ret = kEplSuccessful;
285
286     if (ServiceId_p < tabentries (EplDlluCalInstance_g.m_apfnDlluCbAsnd))
287     {
288         // memorize function pointer
289         EplDlluCalInstance_g.m_apfnDlluCbAsnd[ServiceId_p] = pfnDlluCbAsnd_p;
290
291         if (pfnDlluCbAsnd_p == NULL)
292         {   // close filter
293             Filter_p = kEplDllAsndFilterNone;
294         }
295
296         // set filter in DLL module in kernel part
297         Ret = EplDlluCalSetAsndServiceIdFilter(ServiceId_p, Filter_p);
298
299     }
300
301     return Ret;
302 }
303
304 //---------------------------------------------------------------------------
305 //
306 // Function:    EplDlluCalAsyncSend()
307 //
308 // Description: sends the frame with the specified priority.
309 //
310 // Parameters:  pFrameInfo_p            = frame
311 //                                        m_uiFrameSize does not include the
312 //                                        ethernet header (14 bytes)
313 //              Priority_p              = priority
314 //
315 // Returns:     tEplKernel              = error code
316 //
317 //
318 // State:
319 //
320 //---------------------------------------------------------------------------
321
322 tEplKernel EplDlluCalAsyncSend(tEplFrameInfo * pFrameInfo_p, tEplDllAsyncReqPriority Priority_p)
323 {
324 tEplKernel  Ret = kEplSuccessful;
325
326 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLK)) != 0)
327     pFrameInfo_p->m_uiFrameSize += 14;  // add size of ethernet header
328     Ret = EplDllkCalAsyncSend(pFrameInfo_p, Priority_p);
329 #else
330     Ret = kEplSuccessful;
331 #endif
332
333     return Ret;
334 }
335
336
337 #if(((EPL_MODULE_INTEGRATION) & (EPL_MODULE_NMT_MN)) != 0)
338
339 //---------------------------------------------------------------------------
340 //
341 // Function:    EplDlluCalIssueRequest()
342 //
343 // Description: issues a StatusRequest or a IdentRequest to the specified node.
344 //
345 // Parameters:  Service_p               = request service ID
346 //              uiNodeId_p              = node ID
347 //              bSoaFlag1_p             = flag1 for this node (transmit in SoA and PReq)
348 //                                        If 0xFF this flag is ignored.
349 //
350 // Returns:     tEplKernel              = error code
351 //
352 //
353 // State:
354 //
355 //---------------------------------------------------------------------------
356
357 tEplKernel EplDlluCalIssueRequest(tEplDllReqServiceId Service_p, unsigned int uiNodeId_p, BYTE bSoaFlag1_p)
358 {
359 tEplKernel  Ret = kEplSuccessful;
360
361     // add node to appropriate request queue
362     switch (Service_p)
363     {
364         case kEplDllReqServiceIdent:
365         case kEplDllReqServiceStatus:
366         {
367         tEplEvent   Event;
368         tEplDllCalIssueRequest  IssueReq;
369
370             Event.m_EventSink = kEplEventSinkDllkCal;
371             Event.m_EventType = kEplEventTypeDllkIssueReq;
372             IssueReq.m_Service = Service_p;
373             IssueReq.m_uiNodeId = uiNodeId_p;
374             IssueReq.m_bSoaFlag1 = bSoaFlag1_p;
375             Event.m_pArg = &IssueReq;
376             Event.m_uiSize = sizeof (IssueReq);
377
378             Ret = EplEventuPost(&Event);
379             break;
380         }
381
382         default:
383         {
384             Ret = kEplDllInvalidParam;
385             goto Exit;
386         }
387     }
388
389 Exit:
390     return Ret;
391 }
392
393
394 //---------------------------------------------------------------------------
395 //
396 // Function:    EplDlluCalAddNode()
397 //
398 // Description: adds the specified node to the isochronous phase.
399 //
400 // Parameters:  pNodeInfo_p             = pointer of node info structure
401 //
402 // Returns:     tEplKernel              = error code
403 //
404 //
405 // State:
406 //
407 //---------------------------------------------------------------------------
408
409 tEplKernel EplDlluCalAddNode(tEplDllNodeInfo * pNodeInfo_p)
410 {
411 tEplKernel  Ret = kEplSuccessful;
412 tEplEvent   Event;
413
414     Event.m_EventSink = kEplEventSinkDllkCal;
415     Event.m_EventType = kEplEventTypeDllkAddNode;
416     Event.m_pArg = pNodeInfo_p;
417     Event.m_uiSize = sizeof (tEplDllNodeInfo);
418
419     Ret = EplEventuPost(&Event);
420
421     return Ret;
422 }
423
424
425 //---------------------------------------------------------------------------
426 //
427 // Function:    EplDlluCalDeleteNode()
428 //
429 // Description: removes the specified node from the isochronous phase.
430 //
431 // Parameters:  uiNodeId_p              = node ID
432 //
433 // Returns:     tEplKernel              = error code
434 //
435 //
436 // State:
437 //
438 //---------------------------------------------------------------------------
439
440 tEplKernel EplDlluCalDeleteNode(unsigned int uiNodeId_p)
441 {
442 tEplKernel  Ret = kEplSuccessful;
443 tEplEvent   Event;
444
445     Event.m_EventSink = kEplEventSinkDllkCal;
446     Event.m_EventType = kEplEventTypeDllkDelNode;
447     Event.m_pArg = &uiNodeId_p;
448     Event.m_uiSize = sizeof (uiNodeId_p);
449
450     Ret = EplEventuPost(&Event);
451
452     return Ret;
453 }
454
455
456 //---------------------------------------------------------------------------
457 //
458 // Function:    EplDlluCalSoftDeleteNode()
459 //
460 // Description: removes the specified node softly from the isochronous phase.
461 //
462 // Parameters:  uiNodeId_p              = node ID
463 //
464 // Returns:     tEplKernel              = error code
465 //
466 //
467 // State:
468 //
469 //---------------------------------------------------------------------------
470
471 tEplKernel EplDlluCalSoftDeleteNode(unsigned int uiNodeId_p)
472 {
473 tEplKernel  Ret = kEplSuccessful;
474 tEplEvent   Event;
475
476     Event.m_EventSink = kEplEventSinkDllkCal;
477     Event.m_EventType = kEplEventTypeDllkSoftDelNode;
478     Event.m_pArg = &uiNodeId_p;
479     Event.m_uiSize = sizeof (uiNodeId_p);
480
481     Ret = EplEventuPost(&Event);
482
483     return Ret;
484 }
485
486
487 #endif // (((EPL_MODULE_INTEGRATION) & (EPL_MODULE_NMT_MN)) != 0)
488
489
490 //=========================================================================//
491 //                                                                         //
492 //          P R I V A T E   F U N C T I O N S                              //
493 //                                                                         //
494 //=========================================================================//
495
496 //---------------------------------------------------------------------------
497 //
498 // Function:    EplDlluCalSetAsndServiceIdFilter()
499 //
500 // Description: forwards call to EplDllkSetAsndServiceIdFilter() in kernel part
501 //
502 // Parameters:  ServiceId_p             = ASnd Service ID
503 //              Filter_p                = node ID filter
504 //
505 // Returns:     tEplKernel              = error code
506 //
507 //
508 // State:
509 //
510 //---------------------------------------------------------------------------
511
512 static tEplKernel EplDlluCalSetAsndServiceIdFilter(tEplDllAsndServiceId ServiceId_p, tEplDllAsndFilter Filter_p)
513 {
514 tEplKernel  Ret = kEplSuccessful;
515 tEplEvent   Event;
516 tEplDllCalAsndServiceIdFilter   ServFilter;
517
518     Event.m_EventSink = kEplEventSinkDllkCal;
519     Event.m_EventType = kEplEventTypeDllkServFilter;
520     ServFilter.m_ServiceId = ServiceId_p;
521     ServFilter.m_Filter = Filter_p;
522     Event.m_pArg = &ServFilter;
523     Event.m_uiSize = sizeof (ServFilter);
524
525     Ret = EplEventuPost(&Event);
526
527     return Ret;
528 }
529
530
531 #endif // (((EPL_MODULE_INTEGRATION) & (EPL_MODULE_DLLU)) != 0)
532
533 // EOF
534