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
|
/*
lib/comedi.c
generic functions
COMEDILIB - Linux Control and Measurement Device Interface Library
Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
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, version 2.1
of the License.
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 <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include "libinternal.h"
INTERNAL int __comedi_init=0;
INTERNAL void initialize(void)
{
char *s;
__comedi_init=1;
if( (s=getenv("COMEDILIB_LOGLEVEL")) ){
__comedi_loglevel=strtol(s,NULL,0);
COMEDILIB_DEBUG(3,"setting loglevel to %d\n",__comedi_loglevel);
}
}
EXPORT_ALIAS_DEFAULT(_comedi_open,comedi_open,0.7.18);
comedi_t* _comedi_open(const char *fn)
{
comedi_t *it;
if(!__comedi_init)
initialize();
if(!(it=malloc(sizeof(comedi_t))))
goto cleanup;
memset(it,0,sizeof(comedi_t));
if((it->fd=open(fn,O_RDWR))<0){
libc_error();
goto cleanup;
}
if(comedi_ioctl(it->fd, COMEDI_DEVINFO, &it->devinfo) < 0)
goto cleanup;
it->n_subdevices=it->devinfo.n_subdevs;
if(get_subdevices(it) < 0)
goto cleanup;
it->magic=COMEDILIB_MAGIC;
return it;
cleanup:
if(it) {
/* As long as get_subdevices is the last action above,
it->subdevices should not need any cleanup, since
get_subdevices should have done the cleanup already */
if (it->fd >= 0)
close(it->fd);
free(it);
}
return NULL;
}
EXPORT_ALIAS_DEFAULT(_comedi_close,comedi_close,0.7.18);
int _comedi_close(comedi_t *it)
{
subdevice *s;
int i,j;
if(!valid_dev(it))
return -1;
it->magic=0;
for(i=0;i<it->n_subdevices;i++){
s=it->subdevices+i;
if(s->type==COMEDI_SUBD_UNUSED)
continue;
if(s->subd_flags&SDF_FLAGS){
free(s->flags_list);
}
if(s->subd_flags&SDF_MAXDATA){
free(s->maxdata_list);
}
if(s->subd_flags&SDF_RANGETYPE){
free(s->range_type_list);
for(j=0;j<s->n_chan;j++)
free(s->rangeinfo_list[j]);
free(s->rangeinfo_list);
}else{
free(s->rangeinfo);
}
if(s->cmd_mask)free(s->cmd_mask);
}
if(it->subdevices){
free(it->subdevices);
}
close(it->fd);
free(it);
return 0;
}
EXPORT_ALIAS_DEFAULT(_comedi_cancel,comedi_cancel,0.7.18);
int _comedi_cancel(comedi_t *it,unsigned int subdevice)
{
if(!valid_dev(it)) return -1;
return comedi_ioctl(it->fd, COMEDI_CANCEL, (void*)(unsigned long)subdevice);
}
EXPORT_ALIAS_DEFAULT(_comedi_poll,comedi_poll,0.7.18);
int _comedi_poll(comedi_t *it,unsigned int subdevice)
{
if(!valid_dev(it)) return -1;
return comedi_ioctl(it->fd, COMEDI_POLL, (void*)(unsigned long)subdevice);
}
EXPORT_ALIAS_DEFAULT(_comedi_fileno,comedi_fileno,0.7.18);
int _comedi_fileno(comedi_t *it)
{
if(!valid_dev(it)) return -1;
return it->fd;
}
EXPORT_ALIAS_DEFAULT(_comedi_trigger,comedi_trigger,0.7.18);
int _comedi_trigger(comedi_t *it,comedi_trig *t)
{
if(!valid_dev(it) || !t)
return -1;
return comedi_ioctl(it->fd, COMEDI_TRIG, t);
}
EXPORT_ALIAS_DEFAULT(_comedi_command,comedi_command,0.7.18);
int _comedi_command(comedi_t *it,comedi_cmd *t)
{
int ret;
if(!valid_dev(it)) return -1;
ret = comedi_ioctl(it->fd, COMEDI_CMD, t);
if(ret<0){
switch(__comedi_errno){
case EIO:
__comedi_errno = ECMDNOTSUPP;
break;
}
}
return ret;
}
EXPORT_ALIAS_DEFAULT(_comedi_command_test,comedi_command_test,0.7.18);
int _comedi_command_test(comedi_t *it,comedi_cmd *t)
{
int ret;
if(!valid_dev(it)) return -1;
ret = comedi_ioctl(it->fd, COMEDI_CMDTEST, t);
if(ret<0){
switch(__comedi_errno){
case EIO:
__comedi_errno = ECMDNOTSUPP;
break;
}
}
return ret;
}
EXPORT_ALIAS_DEFAULT(_comedi_do_insnlist,comedi_do_insnlist,0.7.18);
int _comedi_do_insnlist(comedi_t *it,comedi_insnlist *il)
{
if(!valid_dev(it)) return -1;
return comedi_ioctl(it->fd, COMEDI_INSNLIST, il);
}
EXPORT_ALIAS_DEFAULT(_comedi_do_insn,comedi_do_insn,0.7.18);
int _comedi_do_insn(comedi_t *it,comedi_insn *insn)
{
if(!valid_dev(it)) return -1;
if(it->has_insn_ioctl){
return comedi_ioctl(it->fd, COMEDI_INSN, insn);
}else{
comedi_insnlist il;
int ret;
il.n_insns = 1;
il.insns = insn;
ret = comedi_ioctl(it->fd, COMEDI_INSNLIST, &il);
if(ret<0)return ret;
return insn->n;
}
}
EXPORT_ALIAS_DEFAULT(_comedi_lock,comedi_lock,0.7.18);
int _comedi_lock(comedi_t *it,unsigned int subdevice)
{
if(!valid_dev(it)) return -1;
return comedi_ioctl(it->fd, COMEDI_LOCK, (void*)(unsigned long)subdevice);
}
EXPORT_ALIAS_DEFAULT(_comedi_unlock,comedi_unlock,0.7.18);
int _comedi_unlock(comedi_t *it,unsigned int subdevice)
{
if(!valid_dev(it)) return -1;
return comedi_ioctl(it->fd, COMEDI_UNLOCK, (void*)(unsigned long)subdevice);
}
EXPORT_ALIAS_DEFAULT(_comedi_set_read_subdevice,comedi_set_read_subdevice,0.11.0);
int _comedi_set_read_subdevice(comedi_t *it,unsigned int subdevice)
{
int ret;
if(!valid_dev(it)) return -1;
if(it->devinfo.read_subdevice >= 0 &&
it->devinfo.read_subdevice == subdevice){
return 0;
}
ret = comedi_ioctl(it->fd, COMEDI_SETRSUBD, (void*)(unsigned long)subdevice);
if(ret == 0){
it->devinfo.read_subdevice = subdevice;
}
return ret;
}
EXPORT_ALIAS_DEFAULT(_comedi_set_write_subdevice,comedi_set_write_subdevice,0.11.0);
int _comedi_set_write_subdevice(comedi_t *it,unsigned int subdevice)
{
int ret;
if(!valid_dev(it)) return -1;
if(it->devinfo.write_subdevice >= 0 &&
it->devinfo.write_subdevice == subdevice){
return 0;
}
ret = comedi_ioctl(it->fd, COMEDI_SETWSUBD, (void*)(unsigned long)subdevice);
if(ret == 0){
it->devinfo.write_subdevice = subdevice;
}
return ret;
}
|