mbgtools-lx  4.2.8
lx-shared.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: lx-shared.h 1.7 2018/11/22 16:39:10 martin TRASH $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * The definitions here could be shared with the standard
10  * Linux driver for Meinberg PCI cards.
11  *
12  * -----------------------------------------------------------------------
13  * $Log: lx-shared.h $
14  * Revision 1.7 2018/11/22 16:39:10 martin
15  * Refactored debug logging stuff.
16  * Revision 1.6 2018/09/21 15:25:19 martin
17  * Fixed preprocessor symbol definitions.
18  * Use safer string functions.
19  * Revision 1.5 2018/07/02 16:56:56 martin
20  * Use MBG_DEV_NAME_FMT format string.
21  * Unified kernel driver messages.
22  * Revision 1.4 2017/09/06 11:57:39 martin
23  * Fixed a compiler warning.
24  * Revision 1.3 2017/06/28 16:51:47 martin
25  * Avoid using sprintf().
26  * Revision 1.2 2013/01/02 14:48:15 martin
27  * Cleanup.
28  * Revision 1.1 2011/01/25 11:28:26 martin
29  *
30  **************************************************************************/
31 
32 #if !defined( DEBUG_SEM )
33  #if defined( DEBUG )
34  #define DEBUG_SEM 0 // eventually change this to '1'
35  #else
36  #define DEBUG_SEM 0 // always leave this '0'
37  #endif
38 #endif
39 
40 
41 #if DEBUG_SEM
42 
43 static __mbg_inline
44 void snprintf_pddev( char *s, size_t max_len, const PCPS_DDEV *pddev )
45 {
46  s[0] = 0; // Empty string just in case ...
47 
48  if ( pddev )
49  {
50  mbg_kdd_snprintf( s, max_len, ", dev: " MBG_DEV_NAME_FMT,
51  _pcps_ddev_type_name( pddev ),
52  _pcps_ddev_sernum( pddev ) );
53  }
54 
55 } // snprintf_pddev
56 
57 
58 
59 static __mbg_inline
60 void _sema_init_pddev( struct semaphore *ps, int n, const char *sem_name,
61  const char *fnc_name, const PCPS_DDEV *pddev )
62 {
63  char ws[40];
64 
65  snprintf_pddev( ws, sizeof( ws ), pddev );
66 
67  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%s initializing %s%s",
68  fnc_name, sem_name, ws );
69 
70  sema_init( ps, n );
71 
72 } // _sema_init_pddev
73 
74 
75 
76 static __mbg_inline
77 int _down_interruptible_pddev( struct semaphore *ps, const char *sem_name,
78  const char *fnc_name, const PCPS_DDEV *pddev )
79 {
80  int retval;
81  char ws[40];
82 
83  snprintf_pddev( ws, sizeof( ws ), pddev );
84 
85  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%s: going to get %s%s",
86  fnc_name, sem_name, ws );
87 
88  retval = down_interruptible( ps );
89 
90  if ( retval < 0 )
91  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%s: interrupted waiting for %s%s",
92  fnc_name, sem_name, ws );
93  else
94  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%s: got %s%s",
95  fnc_name, sem_name, ws );
96 
97  return retval;
98 
99 } // _down_interruptible_pddev
100 
101 
102 
103 static __mbg_inline
104 void _up_pddev( struct semaphore *ps, const char *sem_name,
105  const char *fnc_name, const PCPS_DDEV *pddev )
106 {
107  char ws[40];
108 
109  snprintf_pddev( ws, sizeof( ws ), pddev );
110 
111  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%s: releasing %s%s",
112  fnc_name, sem_name, ws );
113 
114  up( ps );
115 
116 } // _up_pddev
117 
118 
119 
120 static __mbg_inline
121 void _down( struct semaphore *ps, const char *sem_name,
122  const char *fnc_name, void *p_id_struc )
123 {
124  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: going to get %s",
125  p_id_struc, fnc_name, sem_name );
126 
127  down( ps );
128 
129  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: got %s",
130  p_id_struc, fnc_name, sem_name );
131 } // _down
132 
133 
134 
135 static __mbg_inline
136 int _down_interruptible( struct semaphore *ps, const char *sem_name,
137  const char *fnc_name, void *p_id_struc )
138 {
139  int retval;
140 
141  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: going to get %s",
142  p_id_struc, fnc_name, sem_name );
143 
144  retval = down_interruptible( ps );
145 
146  if ( retval < 0 )
147  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: interrupted when waiting for %s",
148  p_id_struc, fnc_name, sem_name );
149  else
150  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: got %s",
151  p_id_struc, fnc_name, sem_name );
152 
153  return retval;
154 
155 } // _down_interruptible
156 
157 
158 
159 static __mbg_inline
160 int _down_trylock( struct semaphore *ps, const char *sem_name,
161  const char *fnc_name, void *p_id_struc )
162 {
163  int retval;
164 
165  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: trying to get %s",
166  p_id_struc, fnc_name, sem_name );
167 
168  retval = down_trylock( ps );
169 
170  if ( retval < 0 )
171  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_WARN, "%p %s: failed to get %s",
172  p_id_struc, fnc_name, sem_name );
173  else
174  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: got %s",
175  p_id_struc, fnc_name, sem_name );
176 
177  return retval;
178 
179 } // _down_trylock
180 
181 
182 
183 static __mbg_inline
184 void _up( struct semaphore *ps, const char *sem_name,
185  const char *fnc_name, void *p_id_struc )
186 {
187  _mbgddmsg_3( DEBUG_SEM, MBG_LOG_INFO, "%p %s: releasing %s",
188  p_id_struc, fnc_name, sem_name );
189 
190  up( ps );
191 
192 } // _up
193 
194 #else
195 
196  #define _sema_init_pddev( _s, _n, _sn, _fn, _fp ) sema_init( _s, _n )
197  #define _down_interruptible_pddev( _s, _sn, _fn, _fp ) down_interruptible( _s )
198  #define _up_pddev( _s, _sn, _fn, _fp ) up( _s )
199  #define _down( _s, _sn, _fn, _fp ) down( _s )
200  #define _down_interruptible( _s, _sn, _fn, _fp ) down_interruptible( _s )
201  #define _down_trylock( _s, _sn, _fn, _fp ) down_trylock( _s )
202  #define _up( _s, _sn, _fn, _fp ) up( _s )
203 
204 #endif
205 
206 
207 
208 static __mbg_inline
209 void set_dev_connected( PCPS_DDEV *pddev, int state )
210 {
211  _mbgddmsg_3( DEBUG, MBG_LOG_INFO, "setting dev " MBG_DEV_NAME_FMT " connected state to %i",
212  _pcps_ddev_type_name( pddev ), _pcps_ddev_sernum( pddev ), state );
213  atomic_set( &pddev->connected, state );
214 
215 } // set_dev_connected
216 
217 
218 
219 static __mbg_inline
221 {
222  return atomic_read( &pddev->connected );
223 
224 } // get_dev_connected
225 
226 
227 
228 static __mbg_inline /*HDR*/
229 int mbgdrvr_get_pddev( PCPS_DDEV **ppddev, struct file *filp, const char *info )
230 {
231  PCPS_DDEV *pddev = NULL;
232  int ret_val = 0;
233 
234  if ( _down_interruptible( &sem_fops, "sem_fops", info, filp ) < 0 )
235  {
236  ret_val = -ERESTARTSYS;
237  goto out;
238  }
239 
240  pddev = *( (PCPS_DDEV **) filp->private_data );
241 
242  if ( pddev == NULL )
243  {
244  _mbgddmsg_2( DEBUG, MBG_LOG_ERR, "%p %s called with NULL device", filp, info );
245  ret_val = -ENODEV;
246  goto out_up_sem_fops;
247  }
248 
249  if ( !get_dev_connected( pddev ) )
250  {
251  _mbgddmsg_4( DEBUG, MBG_LOG_WARN, "%p %s called for disconnected dev " MBG_DEV_NAME_FMT,
252  filp, info, _pcps_ddev_type_name( pddev ), _pcps_ddev_sernum( pddev ) );
253  ret_val = -ENODEV;
254  goto out_up_sem_fops;
255  }
256 
257  _mbgddmsg_4( DEBUG, MBG_LOG_INFO, "%p %s called, dev: " MBG_DEV_NAME_FMT,
258  filp, info, _pcps_ddev_type_name( pddev ), _pcps_ddev_sernum( pddev ) );
259 
260 out_up_sem_fops:
261  _up( &sem_fops, "sem_fops", info, filp );
262 
263 out:
264  *ppddev = pddev;
265 
266  return ret_val;
267 
268 } // mbgdrvr_get_pddev
269 
270 
271 
#define _pcps_ddev_sernum(_p)
Definition: pcpsdrvr.h:1186
#define _up(_s, _sn, _fn, _fp)
Definition: lx-shared.h:202
int mbg_kdd_snprintf(char *buf, size_t size, const char *fmt,...)
#define _mbgddmsg_3(_f, _lvl, _fmt, _p1, _p2, _p3)
Definition: mbgddmsg.h:330
#define _down_interruptible_pddev(_s, _sn, _fn, _fp)
Definition: lx-shared.h:197
#define _sema_init_pddev(_s, _n, _sn, _fn, _fp)
Definition: lx-shared.h:196
#define MBG_DEV_NAME_FMT
A string format specifier for MBG_DEV_NAME.
Definition: pcpsdev.h:1534
static __mbg_inline int mbgdrvr_get_pddev(PCPS_DDEV **ppddev, struct file *filp, const char *info)
Definition: lx-shared.h:229
#define _up_pddev(_s, _sn, _fn, _fp)
Definition: lx-shared.h:198
static __mbg_inline void set_dev_connected(PCPS_DDEV *pddev, int state)
Definition: lx-shared.h:209
static __mbg_inline int get_dev_connected(PCPS_DDEV *pddev)
Definition: lx-shared.h:220
#define DEBUG_SEM
Definition: lx-shared.h:34
#define _mbgddmsg_4(_f, _lvl, _fmt, _p1, _p2, _p3, _p4)
Definition: mbgddmsg.h:336
#define _down_trylock(_s, _sn, _fn, _fp)
Definition: lx-shared.h:201
#define _mbgddmsg_2(_f, _lvl, _fmt, _p1, _p2)
Definition: mbgddmsg.h:324
atomic_t connected
Flag indicating if the device is "connected".
Definition: pcpsdrvr.h:863
#define _pcps_ddev_type_name(_p)
Definition: pcpsdrvr.h:1131
#define _down_interruptible(_s, _sn, _fn, _fp)
Definition: lx-shared.h:200
#define _down(_s, _sn, _fn, _fp)
Definition: lx-shared.h:199
static struct semaphore sem_fops