remove suidregister
[comedilib.git] / lib / data.c
1 /*
2     lib/data.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
40 int comedi_data_write(comedi_t *it,unsigned int subdev,unsigned int chan,unsigned int range,
41                 unsigned int aref,lsampl_t data)
42 {
43         subdevice *s;
44
45         if(!valid_chan(it,subdev,chan))
46                 return -1;
47
48         s=it->subdevices+subdev;
49         
50         if(it->has_insnlist_ioctl){
51                 comedi_insn insn;
52
53                 memset(&insn,0,sizeof(insn));
54
55                 insn.insn = INSN_WRITE;
56                 insn.n = 1;
57                 insn.data = &data;
58                 insn.subdev = subdev;
59                 insn.chanspec = CR_PACK(chan,range,aref);
60
61                 return comedi_do_insn(it,&insn);
62         }else{
63                 comedi_trig cmd={
64                         mode:           0,
65                         flags:          TRIG_WRITE,
66                         n_chan:         1,
67                         n:              1,
68                         trigsrc:        0,
69                         trigvar:        0,
70                         trigvar1:       0,
71                 };
72                 sampl_t sdata=data;
73
74                 chan=CR_PACK(chan,range,aref);
75
76                 cmd.subdev=subdev;
77                 if(it->subdevices[subdev].subd_flags & SDF_LSAMPL){
78                         cmd.data=(sampl_t *)(&data);
79                 }else{
80                         cmd.data=&sdata;
81                 }
82                 cmd.chanlist=&chan;
83
84                 return ioctl_trigger(it->fd,&cmd);
85         }
86 }
87
88
89 int comedi_data_read_n(comedi_t *it,unsigned int subdev,unsigned int chan,
90                 unsigned int range, unsigned int aref,lsampl_t *data,
91                 unsigned int n)
92 {
93         subdevice *s;
94         comedi_insn insn;
95
96         if(!valid_chan(it,subdev,chan))
97                 return -1;
98
99         s=it->subdevices+subdev;
100
101         if(it->has_insnlist_ioctl){
102
103                 memset(&insn,0,sizeof(insn));
104
105                 insn.insn = INSN_READ;
106                 insn.n = 1;
107                 insn.data = data;
108                 insn.subdev = subdev;
109                 insn.chanspec = CR_PACK(chan,range,aref);
110
111                 return comedi_do_insn(it,&insn);
112         }else{
113                 /* There's no need to be fast for a case that is
114                  * obsolete. */
115                 return comedi_data_read(it,subdev,chan,range,aref,data);
116         }
117 }
118
119 int comedi_data_read(comedi_t *it,unsigned int subdev,unsigned int chan,unsigned int range,
120                 unsigned int aref,lsampl_t *data)
121 {
122         subdevice *s;
123
124         if(!valid_chan(it,subdev,chan))
125                 return -1;
126
127         s=it->subdevices+subdev;
128
129         if(it->has_insnlist_ioctl){
130                 comedi_insn insn;
131
132                 memset(&insn,0,sizeof(insn));
133
134                 insn.insn = INSN_READ;
135                 insn.n = 1;
136                 insn.data = data;
137                 insn.subdev = subdev;
138                 insn.chanspec = CR_PACK(chan,range,aref);
139
140                 return comedi_do_insn(it,&insn);
141         }else{
142                 comedi_trig cmd={
143                         mode:           0,
144                         flags:          0,
145                         n_chan:         1,
146                         n:              1,
147                         trigsrc:        0,
148                         trigvar:        0,
149                         trigvar1:       0,
150                 };
151                 int ret;
152                 sampl_t sdata;
153
154                 chan=CR_PACK(chan,range,aref);
155         
156                 cmd.subdev=subdev;
157                 cmd.chanlist=&chan;
158                 if(s->subd_flags & SDF_LSAMPL){
159                         cmd.data=(sampl_t *)data;
160                 }else{
161                         cmd.data=&sdata;
162                 }
163
164                 ret=ioctl_trigger(it->fd,&cmd);
165                 if(ret<0)
166                         return ret;
167
168                 if(!(s->subd_flags & SDF_LSAMPL)){
169                         *data=sdata;
170                 }
171
172                 return 0;
173         }
174 }
175