3 functions to retrieve kernel data
5 COMEDILIB - Linux Control and Measurement Device Interface Library
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation, version 2.1
13 This library 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 GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
31 #include <sys/ioctl.h>
36 #include "libinternal.h"
39 /* these functions download information from the comedi module. */
41 static int do_test_for_cmd(comedi_t *dev,unsigned int subdevice);
42 static int do_test_for_insn(comedi_t *dev);
43 static int do_test_for_insnlist(comedi_t *dev);
44 static int do_test_for_insn_bits(comedi_t *dev,unsigned int subdevice);
47 int get_subdevices(comedi_t *it)
55 s=malloc(sizeof(comedi_subdinfo)*it->n_subdevices);
58 ret = comedi_ioctl(it->fd, COMEDI_SUBDINFO, (unsigned long)s);
61 r=it->subdevices=realloc(it->subdevices,
62 sizeof(subdevice)*it->n_subdevices);
64 memset(r,0,sizeof(subdevice)*it->n_subdevices);
66 it->has_insnlist_ioctl = do_test_for_insnlist(it);
67 it->has_insn_ioctl = do_test_for_insn(it);
68 for(i=0;i<it->n_subdevices;i++){
69 r[i].type = s[i].type;
70 if(r[i].type==COMEDI_SUBD_UNUSED)continue;
71 r[i].n_chan = s[i].n_chan;
72 r[i].subd_flags = s[i].subd_flags;
73 r[i].timer_type = s[i].timer_type;
74 r[i].len_chanlist = s[i].len_chanlist;
75 r[i].maxdata = s[i].maxdata;
76 r[i].flags = s[i].flags;
77 r[i].range_type = s[i].range_type;
79 if(r[i].subd_flags&SDF_FLAGS){
80 r[i].flags_list=malloc(sizeof(*r[i].flags_list)*r[i].n_chan);
81 debug_ptr(r[i].flags_list);
83 if(r[i].subd_flags&SDF_MAXDATA){
84 r[i].maxdata_list=malloc(sizeof(*r[i].maxdata_list)*r[i].n_chan);
85 debug_ptr(r[i].maxdata_list);
87 if(r[i].subd_flags&SDF_RANGETYPE){
88 r[i].range_type_list=malloc(sizeof(*r[i].range_type_list)*r[i].n_chan);
89 debug_ptr(r[i].range_type_list);
92 ci.flaglist = r[i].flags_list;
93 ci.rangelist = r[i].range_type_list;
94 ci.maxdata_list = r[i].maxdata_list;
95 ret = comedi_ioctl(it->fd, COMEDI_CHANINFO, (unsigned long)&ci);
98 if(r[i].subd_flags&SDF_RANGETYPE){
99 r[i].rangeinfo_list=malloc(sizeof(*r[i].rangeinfo_list)*r[i].n_chan);
100 debug_ptr(r[i].rangeinfo_list);
101 for(j=0;j<r[i].n_chan;j++){
102 r[i].rangeinfo_list[j]=get_rangeinfo(it->fd,r[i].range_type_list[j]);
105 r[i].rangeinfo=get_rangeinfo(it->fd,r[i].range_type);
108 r[i].has_cmd = do_test_for_cmd(it,i);
109 if(it->has_insnlist_ioctl){
110 r[i].has_insn_bits = do_test_for_insn_bits(it,i);
112 r[i].has_insn_bits = 0;
121 comedi_range *get_rangeinfo(int fd,unsigned int range_type)
129 kr=malloc(sizeof(comedi_krange)*RANGE_LENGTH(range_type));
130 r=malloc(sizeof(comedi_range)*RANGE_LENGTH(range_type));
132 ri.range_type = range_type;
134 ret = comedi_ioctl(fd, COMEDI_RANGEINFO, (unsigned long)&ri);
136 fprintf(stderr,"ioctl(%d,COMEDI_RANGEINFO,0x%08x,%p)\n",fd,range_type,kr);
139 for(i=0;i<RANGE_LENGTH(range_type);i++){
140 r[i].min=kr[i].min*1e-6;
141 r[i].max=kr[i].max*1e-6;
142 r[i].unit=kr[i].flags;
150 /* some command testing */
152 static int do_test_for_cmd(comedi_t *dev,unsigned int subdevice)
154 /* SDF_CMD was added in 0.7.57 */
155 if(dev->devinfo.version_code >= COMEDI_VERSION_CODE(0,7,57)){
156 if(dev->subdevices[subdevice].subd_flags & SDF_CMD)
163 memset(&it,0,sizeof(it));
165 it.subdev = subdevice;
166 it.start_src = TRIG_ANY;
167 it.scan_begin_src = TRIG_ANY;
168 it.convert_src = TRIG_ANY;
169 it.scan_end_src = TRIG_ANY;
170 it.stop_src = TRIG_ANY;
172 ret = comedi_ioctl(dev->fd, COMEDI_CMDTEST, (unsigned long)&it);
174 if(ret<0 && errno==EIO){
178 fprintf(stderr,"BUG in do_test_for_cmd()\n");
185 static int do_test_for_insnlist(comedi_t *dev)
192 memset(&insn,0,sizeof(insn));
197 insn.insn = INSN_GTOD;
201 ret = comedi_ioctl(dev->fd, COMEDI_INSNLIST, (unsigned long)&il);
205 fprintf(stderr,"BUG in do_test_for_insn()\n");
212 /* the COMEID_INSN ioctl was introduced in comedi-0.7.60 */
213 static int do_test_for_insn(comedi_t *dev)
220 memset(&insn,0,sizeof(insn));
225 insn.insn = INSN_GTOD;
229 ret = comedi_ioctl(dev->fd, COMEDI_INSN, (unsigned long)&insn);
233 fprintf(stderr,"BUG in do_test_for_insn()\n");
240 static int do_test_for_insn_bits(comedi_t *dev,unsigned int subdevice)
247 if(dev->subdevices[subdevice].maxdata != 1)
250 memset(&insn,0,sizeof(insn));
255 insn.insn = INSN_BITS;
258 insn.subdev = subdevice;
263 ret = comedi_do_insnlist(dev,&il);
265 if(ret<0 && (errno==EINVAL || errno==EIO)){
269 perror("BUG in do_test_for_insn_bits()\n");