comedi.h update
[comedilib.git] / lib / filler.c
1 /*
2     lib/filler.c
3     comedi library routines
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-8 David A. Schleef <ds@stm.lbl.gov>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
24 #include <stdio.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/ioctl.h>
32 #include <errno.h>
33 #include <comedi.h>
34 #include <string.h>
35
36 #include <libinternal.h>
37
38
39 /* these functions download information from the comedi module. */
40
41 static int do_test_for_cmd(comedi_t *dev,unsigned int subdevice);
42 static int do_test_for_insn(comedi_t *dev,unsigned int subdevice);
43
44
45 int get_subdevices(comedi_t *it)
46 {
47         int i,j;
48         int ret;
49         comedi_subdinfo *s;
50         subdevice *r;
51
52         s=malloc(sizeof(comedi_subdinfo)*it->n_subdevices);
53         ret=ioctl_subdinfo(it->fd,s);
54         debug_int(ret);
55
56         r=it->subdevices=realloc(it->subdevices,
57                 sizeof(subdevice)*it->n_subdevices);
58         debug_ptr(r);
59         memset(r,0,sizeof(subdevice)*it->n_subdevices);
60
61         for(i=0;i<it->n_subdevices;i++){
62                 r[i].type       = s[i].type;
63                 if(r[i].type==COMEDI_SUBD_UNUSED)continue;
64                 r[i].n_chan     = s[i].n_chan;
65                 r[i].subd_flags = s[i].subd_flags;
66                 r[i].timer_type = s[i].timer_type;
67                 r[i].len_chanlist = s[i].len_chanlist;
68                 r[i].maxdata    = s[i].maxdata;
69                 r[i].flags      = s[i].flags;
70                 r[i].range_type = s[i].range_type;
71
72                 if(r[i].subd_flags&SDF_FLAGS){
73                         r[i].flags_list=malloc(sizeof(*r[i].flags_list)*r[i].n_chan);
74                         debug_ptr(r[i].flags_list);
75                 }
76                 if(r[i].subd_flags&SDF_MAXDATA){
77                         r[i].maxdata_list=malloc(sizeof(*r[i].maxdata_list)*r[i].n_chan);
78                         debug_ptr(r[i].maxdata_list);
79                 }
80                 if(r[i].subd_flags&SDF_RANGETYPE){
81                         r[i].range_type_list=malloc(sizeof(*r[i].range_type_list)*r[i].n_chan);
82                         debug_ptr(r[i].range_type_list);
83                 }
84                 ret=ioctl_chaninfo(it->fd,i,r[i].maxdata_list,r[i].flags_list,r[i].range_type_list);
85                 debug_int(ret);
86
87                 if(r[i].subd_flags&SDF_RANGETYPE){
88                         r[i].rangeinfo_list=malloc(sizeof(*r[i].rangeinfo_list)*r[i].n_chan);
89                         debug_ptr(r[i].rangeinfo_list);
90                         for(j=0;j<r[i].n_chan;j++){
91                                 r[i].rangeinfo_list[j]=get_rangeinfo(it->fd,r[i].range_type_list[j]);
92                         }
93                 }else{
94                         r[i].rangeinfo=get_rangeinfo(it->fd,r[i].range_type);
95                 }
96
97                 r[i].has_cmd = do_test_for_cmd(it,i);
98                 r[i].has_insn = do_test_for_insn(it,i);
99         }
100
101         free(s);
102
103         return 0;
104 }
105
106 comedi_range *get_rangeinfo(int fd,unsigned int range_type)
107 {
108         comedi_krange *kr;
109         comedi_range *r;
110         int ret;
111         int i;
112
113         kr=malloc(sizeof(comedi_krange)*RANGE_LENGTH(range_type));
114         r=malloc(sizeof(comedi_range)*RANGE_LENGTH(range_type));
115
116         ret=ioctl_rangeinfo(fd,range_type,kr);
117         if(ret<0){
118                 fprintf(stderr,"ioctl_rangeinfo(%d,0x%08x,%p)\n",fd,range_type,kr);
119         }
120
121         for(i=0;i<RANGE_LENGTH(range_type);i++){
122                 r[i].min=kr[i].min*1e-6;
123                 r[i].max=kr[i].max*1e-6;
124                 r[i].unit=RF_UNIT(kr[i].flags);
125         }
126         free(kr);
127
128         return r;
129 }
130
131
132 /* some command testing */
133
134 static int do_test_for_cmd(comedi_t *dev,unsigned int subdevice)
135 {
136         comedi_cmd it;
137         int ret;
138
139         memset(&it,0,sizeof(it));
140
141         it.subdev = 0;
142         it.start_src = TRIG_ANY;
143         it.scan_begin_src = TRIG_ANY;
144         it.convert_src = TRIG_ANY;
145         it.scan_end_src = TRIG_ANY;
146         it.stop_src = TRIG_ANY;
147
148         ret = ioctl(dev->fd,COMEDI_CMDTEST,&it);
149
150         if(ret<0 && errno==EIO){
151                 return 0;
152         }
153         if(ret<0){
154                 fprintf(stderr,"BUG in do_test_for_cmd()\n");
155                 return 0;
156         }
157         return 1;
158 }
159
160 static int do_test_for_insn(comedi_t *dev,unsigned int subdevice)
161 {
162         comedi_insn insn;
163         comedi_insnlist il;
164         lsampl_t data[2];
165         int ret;
166
167         memset(&insn,0,sizeof(insn));
168
169         il.n_insns = 1;
170         il.insns = &insn;
171
172         insn.insn = INSN_GTOD;
173         insn.n = 2;
174         insn.data = (void *)&data;
175         insn.subdev = subdevice;
176
177         ret = ioctl(dev->fd,COMEDI_INSN,&il);
178
179         if(ret<0 && errno==EIO){
180                 return 0;
181         }
182         if(ret<0){
183                 fprintf(stderr,"BUG in do_test_for_insn()\n");
184                 return 0;
185         }
186         return 1;
187 }
188
189