revert change to COMEDI_INSN ioctl
[comedilib.git] / lib / comedi.c
1 /*
2     lib/comedi.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 int __comedi_init=0;
39
40 void initialize(void)
41 {
42         char *s;
43         
44         __comedi_init=1;
45
46         if( (s=getenv("COMEDILIB_LOGLEVEL")) ){
47                 __comedi_loglevel=strtol(s,NULL,10);
48                 fprintf(stderr,"setting loglevel to %d\n",__comedi_loglevel);
49         }
50 }
51   
52 comedi_t *comedi_open(const char *fn)
53 {
54         comedi_t *it;
55
56         if(!__comedi_init)
57                 initialize();
58
59         if(!(it=malloc(sizeof(comedi_t))))
60                 goto cleanup;
61         memset(it,0,sizeof(comedi_t));
62
63         if((it->fd=open(fn,O_RDWR))<0){
64                 libc_error();
65                 goto cleanup;
66         }
67
68         if(ioctl_devinfo(it->fd,&it->devinfo)<0)
69                 goto cleanup;
70
71         it->n_subdevices=it->devinfo.n_subdevs;
72
73         get_subdevices(it);
74
75         it->magic=COMEDILIB_MAGIC;
76
77         return it;
78 cleanup:
79         if(it)
80                 free(it);
81
82         return NULL;
83 }
84
85 #if 0
86 /* this is an example of how we do versioned symbols */
87 __asm__(".symver comedi_open_0,comedi_open@");
88 #endif
89
90 void comedi_close(comedi_t *it)
91 {
92         it->magic=0;
93
94         /* XXX should free all memory */
95
96         close(it->fd);
97 }
98
99 int comedi_cancel(comedi_t *it,unsigned int subdevice)
100 {
101         return ioctl(it->fd,COMEDI_CANCEL,subdevice);
102 }
103
104 int comedi_poll(comedi_t *it,unsigned int subdevice)
105 {
106 #ifdef HAVE_COMEDI_POLL
107         return ioctl(it->fd,COMEDI_POLL,subdevice);
108 #else
109         return -1;
110 #endif
111 }
112
113 int comedi_fileno(comedi_t *it)
114 {
115         if(!it)
116                 return -1;
117
118         return it->fd;
119 }
120
121 int comedi_trigger(comedi_t *it,comedi_trig *t)
122 {
123         if(!it || !t)
124                 return -1;
125
126         return ioctl_trigger(it->fd,t);
127 }
128
129 int comedi_command(comedi_t *it,comedi_cmd *t)
130 {
131         return ioctl(it->fd,COMEDI_CMD,t);
132 }
133
134 int comedi_command_test(comedi_t *it,comedi_cmd *t)
135 {
136         return ioctl(it->fd,COMEDI_CMDTEST,t);
137 }
138
139 int comedi_do_insnlist(comedi_t *it,comedi_insnlist *il)
140 {
141         return ioctl(it->fd,COMEDI_INSNLIST,il);
142 }
143
144 int comedi_do_insn(comedi_t *it,comedi_insn *insn)
145 {
146         comedi_insnlist il;
147
148         il.n_insns = 1;
149         il.insns = insn;
150
151         return ioctl(it->fd,COMEDI_INSNLIST,&il);
152 }
153
154 int comedi_lock(comedi_t *it,unsigned int subdevice)
155 {
156         return ioctl(it->fd,COMEDI_LOCK,subdevice);
157 }
158
159 int comedi_unlock(comedi_t *it,unsigned int subdevice)
160 {
161         return ioctl(it->fd,COMEDI_UNLOCK,subdevice);
162 }
163