hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
/**
 * @file
 * Analogy for Linux, descriptor related features
 *
 * @note Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
 * @note Copyright (C) 2008 Alexis Berlemont <alexis.berlemont@free.fr>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <stdio.h>
#include <string.h>
#include <rtdm/analogy.h>
#include "internal.h"
#include "root_leaf.h"
 
#ifndef DOXYGEN_CPP
 
static void a4l_root_setup(a4l_root_t * rt,
              unsigned long gsize, unsigned long rsize)
{
   /* Common init */
   rt->offset = ((void *)rt + sizeof(a4l_root_t));
   rt->gsize = gsize;
   rt->id = 0xffffffff;
   rt->nb_leaf = 0;
   rt->lfnxt = NULL;
   rt->lfchd = NULL;
 
   /* Specific init */
   rt->data = rt->offset;
   rt->offset += rsize;
}
 
static int a4l_leaf_add(a4l_root_t * rt,
           a4l_leaf_t * lf,
           a4l_leaf_t ** lfchild, unsigned long lfsize)
{
   /* Basic checking */
   if (rt->offset +
       sizeof(a4l_leaf_t) + lfsize > ((void *)rt) + rt->gsize)
       return -ENOMEM;
 
   if (lf->nb_leaf != 0) {
       int i;
       a4l_leaf_t *lflst = lf->lfchd;
 
       for (i = 0; i < (lf->nb_leaf - 1); i++) {
           if (lflst == NULL)
               return -EFAULT;
           else
               lflst = lflst->lfnxt;
       }
       lflst->lfnxt = (a4l_leaf_t *) rt->offset;
   } else
       lf->lfchd = (a4l_leaf_t *) rt->offset;
 
   /* Inits parent leaf */
   lf->nb_leaf++;
   *lfchild = (a4l_leaf_t *) rt->offset;
   rt->offset += sizeof(a4l_leaf_t);
 
   /* Performs child leaf init */
   (*lfchild)->id = lf->nb_leaf - 1;
   (*lfchild)->nb_leaf = 0;
   (*lfchild)->lfnxt = NULL;
   (*lfchild)->lfchd = NULL;
   (*lfchild)->data = (void *)rt->offset;
 
   /* Performs root modifications */
   rt->offset += lfsize;
 
   return 0;
}
 
static inline a4l_leaf_t *a4l_leaf_get(a4l_leaf_t * lf,
                      unsigned int id)
{
   int i;
   a4l_leaf_t *lflst = lf->lfchd;
 
   for (i = 0; i < id && lflst != NULL; i++)
       lflst = lflst->lfnxt;
 
   return lflst;
}
 
static int __a4l_get_sbsize(int fd, a4l_desc_t * dsc)
{
   unsigned int i, j, nb_chan, nb_rng;
   int ret, res =
       dsc->nb_subd * (sizeof(a4l_sbinfo_t) + sizeof(a4l_leaf_t));
 
   for (i = 0; i < dsc->nb_subd; i++) {
 
       if ((ret = a4l_sys_nbchaninfo(fd, i, &nb_chan)) < 0)
           return ret;
 
       res += nb_chan * (sizeof(a4l_chinfo_t) + sizeof(a4l_leaf_t));
       for (j = 0; j < nb_chan; j++) {
           if ((ret = a4l_sys_nbrnginfo(fd, i, j, &nb_rng)) < 0)
               return ret;
           res += nb_rng * (sizeof(a4l_rnginfo_t) +
                    sizeof(a4l_leaf_t));
       }
   }
 
   return res;
}
 
static int __a4l_fill_desc(int fd, a4l_desc_t * dsc)
{
   int ret;
   unsigned int i, j;
   a4l_sbinfo_t *sbinfo;
   a4l_root_t *rt = (a4l_root_t *) dsc->sbdata;
 
   a4l_root_setup(rt, dsc->sbsize,
              dsc->nb_subd * sizeof(a4l_sbinfo_t));
   sbinfo = (a4l_sbinfo_t *) rt->data;
 
   if ((ret = a4l_sys_subdinfo(fd, sbinfo)) < 0)
       return ret;
 
   for (i = 0; i < dsc->nb_subd; i++) {
       a4l_leaf_t *lfs;
       a4l_chinfo_t *chinfo;
 
       /* For each subd, add a leaf for the channels even if
          the subd does not own any channel */
       ret = a4l_leaf_add(rt, (a4l_leaf_t *) rt, &lfs,
                sbinfo[i].nb_chan * sizeof(a4l_chinfo_t));
       if (ret < 0)
           return ret;
 
       /* If there is no channel, no need to go further */
       if(sbinfo[i].nb_chan == 0)
           continue;
 
       chinfo = (a4l_chinfo_t *) lfs->data;
 
       if ((ret = a4l_sys_chaninfo(fd, i, chinfo)) < 0)
           return ret;
 
       for (j = 0; j < sbinfo[i].nb_chan; j++) {
           a4l_leaf_t *lfc;
           a4l_rnginfo_t *rnginfo;
 
           /* For each channel, add a leaf for the ranges
              even if no range descriptor is available */
           ret = a4l_leaf_add(rt, lfs, &lfc,
                    chinfo[j].nb_rng *
                    sizeof(a4l_rnginfo_t));
           if (ret < 0)
               return ret;
 
 
           /* If there is no range, no need to go further */
           if(chinfo[j].nb_rng ==0)
               continue;
 
           rnginfo = (a4l_rnginfo_t *) lfc->data;
           if ((ret = a4l_sys_rnginfo(fd, i, j, rnginfo)) < 0)
               return ret;
       }
   }
 
   return 0;
}
 
#endif /* !DOXYGEN_CPP */
 
/*!
 * @ingroup analogy_lib_syscall
 * @defgroup analogy_lib_descriptor Descriptor Syscall API
 * @{
 */
 
/**
 * @brief Get a descriptor on an attached device
 *
 * Once the device has been attached, the function a4l_get_desc()
 * retrieves various information on the device (subdevices, channels,
 * ranges, etc.).
 * The function a4l_get_desc() can be called twice:
 * - The first time, almost all the fields, except sbdata, are set
 *   (board_name, nb_subd, idx_read_subd, idx_write_subd, magic,
 *   sbsize); the last field , sbdata, is supposed to be a pointer on
 *   a buffer, which size is defined by the field sbsize.
 * - The second time, the buffer pointed by sbdata is filled with data
 *   about the subdevices, the channels and the ranges.
 *
 * Between the two calls, an allocation must be performed in order to
 * recover a buffer large enough to contain all the data. These data
 * are set up according a root-leaf organization (device -> subdevice
 * -> channel -> range). They cannot be accessed directly; specific
 * functions are available so as to retrieve them:
 * - a4l_get_subdinfo() to get some subdevice's characteristics.
 * - a4l_get_chaninfo() to get some channel's characteristics.
 * - a4l_get_rnginfo() to get some range's characteristics.
 *
 * @param[in] fd Driver file descriptor
 * @param[out] dsc Device descriptor
 * @param[in] pass Description level to retrieve:
 * - A4L_BSC_DESC to get the basic descriptor (notably the size of
 *   the data buffer to allocate).
 * - A4L_CPLX_DESC to get the complex descriptor, the data buffer
 *   is filled with characteristics about the subdevices, the channels
 *   and the ranges.
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; the
 *    pass argument should be checked; check also the kernel log
 *    ("dmesg")
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENODEV is returned if the descriptor is incoherent (the device
 *    may be unattached)
 *
 */
 
int a4l_sys_desc(int fd, a4l_desc_t * dsc, int pass)
{
   int ret = 0;
 
   if (dsc == NULL ||
       (pass != A4L_BSC_DESC && dsc->magic != MAGIC_BSC_DESC))
       return -EINVAL;
 
   if (pass == A4L_BSC_DESC) {
 
       ret = a4l_sys_devinfo(fd, (a4l_dvinfo_t *) dsc);
       if (ret < 0)
           goto out_a4l_sys_desc;
 
       dsc->sbsize = __a4l_get_sbsize(fd, dsc);
       dsc->sbdata = NULL;
       dsc->magic = MAGIC_BSC_DESC;
   } else {
 
       if (!dsc->sbsize) {
           ret = -ENODEV;
           goto out_a4l_sys_desc;
       }
 
       ret = __a4l_fill_desc(fd, dsc);
       if (ret < 0)
           goto out_a4l_sys_desc;
 
       dsc->magic = MAGIC_CPLX_DESC;
   }
 
out_a4l_sys_desc:
   return ret;
}
 
/*! @} Descriptor Syscall API */
 
/*!
 * @ingroup analogy_lib_level1
 * @defgroup analogy_lib_descriptor1 Descriptor API
 *
 * This is the API interface used to fill and use Analogy device
 * descriptor structure
 * @{
 */
 
/**
 * @brief Open an Analogy device and basically fill the descriptor
 *
 * @param[out] dsc Device descriptor
 * @param[in] fname Device name
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; the
 *    fname and the dsc pointer should be checked; check also the
 *    kernel log ("dmesg")
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 *
 */
int a4l_open(a4l_desc_t *dsc, const char *fname)
{
   int ret;
 
   /* Basic checking */
   if (dsc == NULL)
       return -EINVAL;
 
   /* Initializes the descriptor */
   memset(dsc, 0, sizeof(a4l_desc_t));
 
   /* Opens the driver */
   dsc->fd = a4l_sys_open(fname);
   if (dsc->fd < 0)
       return dsc->fd;
 
   /* Basically fills the descriptor */
   ret = a4l_sys_desc(dsc->fd, dsc, A4L_BSC_DESC);
   if (ret < 0) {
       a4l_sys_close(dsc->fd);
   }
 
   return ret;
}
 
/**
 * @brief Close the Analogy device related with the descriptor
 *
 * The file descriptor is associated with a context. The context is
 * one of the enabler of asynchronous transfers. So, by closing the
 * file descriptor, the programer must keep in mind that the currently
 * occuring asynchronous transfer will cancelled.
 *
 * @param[in] dsc Device descriptor
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; the
 *    the dsc pointer should be checked; check also the kernel log
 *    ("dmesg")
 *
 */
int a4l_close(a4l_desc_t * dsc)
{
   /* Basic checking */
   if (dsc == NULL)
       return -EINVAL;
 
   return a4l_sys_close(dsc->fd);
}
 
/**
 * @brief Fill the descriptor with subdevices, channels and ranges
 * data
 *
 * @param[in] dsc Device descriptor partly filled by a4l_open().
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; the
 *    the dsc pointer should be checked; check also the kernel log
 *    ("dmesg")
 * - -EFAULT is returned if a user <-> kernel transfer went wrong
 * - -ENODEV is returned if the descriptor is incoherent (the device
 *    may be unattached)
 *
 */
int a4l_fill_desc(a4l_desc_t * dsc)
{
   /* Basic checking */
   if (dsc == NULL || dsc->fd < 0)
       return -EINVAL;
 
   /* Checks the descriptor has been basically filled */
   if (dsc->magic != MAGIC_BSC_DESC)
       return -EINVAL;
 
   return a4l_sys_desc(dsc->fd, dsc, A4L_CPLX_DESC);
}
 
/**
 * @brief Get an information structure on a specified subdevice
 *
 * @param[in] dsc Device descriptor filled by a4l_open() and
 * a4l_fill_desc()
 * @param[in] subd Subdevice index
 * @param[out] info Subdevice information structure
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; subd
 *    and the dsc pointer should be checked; check also the kernel log
 *    ("dmesg"); WARNING: a4l_fill_desc() should be called before
 *    using a4l_get_subdinfo().
 *
 */
int a4l_get_subdinfo(a4l_desc_t * dsc,
            unsigned int subd, a4l_sbinfo_t ** info)
{
   a4l_leaf_t *tmp;
 
   if (dsc == NULL || info == NULL)
       return -EINVAL;
 
   if (dsc->magic != MAGIC_CPLX_DESC)
       return -EINVAL;
 
   if (subd >= dsc->nb_subd)
       return -EINVAL;
 
   tmp = (a4l_leaf_t *) dsc->sbdata;
   *info = &(((a4l_sbinfo_t *) tmp->data)[subd]);
 
   return 0;
}
 
/**
 * @brief Get an information structure on a specified channel
 *
 * @param[in] dsc Device descriptor filled by a4l_open() and
 * a4l_fill_desc()
 * @param[in] subd Subdevice index
 * @param[in] chan Channel index
 * @param[out] info Channel information structure
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; subd,
 *    chan and the dsc pointer should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_get_chinfo()
 *
 */
int a4l_get_chinfo(a4l_desc_t * dsc,
          unsigned int subd,
          unsigned int chan, a4l_chinfo_t ** info)
{
   a4l_leaf_t *tmp;
 
   if (dsc == NULL || info == NULL)
       return -EINVAL;
 
   if (dsc->magic != MAGIC_CPLX_DESC)
       return -EINVAL;
 
   if (subd >= dsc->nb_subd)
       return -EINVAL;
 
   tmp = (a4l_leaf_t *) dsc->sbdata;
 
   if (chan >= ((a4l_sbinfo_t *) tmp->data)[subd].nb_chan)
       return -EINVAL;
 
   tmp = a4l_leaf_get(tmp, subd);
   *info = &(((a4l_chinfo_t *) tmp->data)[chan]);
 
   return 0;
}
 
/**
 * @brief Get an information structure on a specified range
 *
 * @param[in] dsc Device descriptor filled by a4l_open() and
 * a4l_fill_desc()
 * @param[in] subd Subdevice index
 * @param[in] chan Channel index
 * @param[in] rng Range index
 * @param[out] info Range information structure
 *
 * @return 0 on success. Otherwise:
 *
 * - -EINVAL is returned if some argument is missing or wrong; subd,
 *    chan, rng and the dsc pointer should be checked; check also the
 *    kernel log ("dmesg"); WARNING: a4l_fill_desc() should be called
 *    before using a4l_get_rnginfo()
 *
 */
int a4l_get_rnginfo(a4l_desc_t * dsc,
           unsigned int subd,
           unsigned int chan,
           unsigned int rng, a4l_rnginfo_t ** info)
{
   a4l_leaf_t *tmp;
 
   if (dsc == NULL || info == NULL)
       return -EINVAL;
 
   if (dsc->magic != MAGIC_CPLX_DESC)
       return -EINVAL;
 
   if (subd >= dsc->nb_subd)
       return -EINVAL;
 
   tmp = (a4l_leaf_t *) dsc->sbdata;
 
   if (chan >= ((a4l_sbinfo_t *) tmp->data)[subd].nb_chan)
       return -EINVAL;
 
   tmp = a4l_leaf_get(tmp, subd);
 
   if (rng >= ((a4l_chinfo_t *) tmp->data)[chan].nb_rng)
       return -EINVAL;
 
   tmp = a4l_leaf_get(tmp, chan);
   *info = &(((a4l_rnginfo_t *) tmp->data)[rng]);
 
   return 0;
}
 
/*! @} Descriptor API */