Update from Bernd Porr. Sleeps only if the file is not there.
[comedilib.git] / comedi_config / comedi_config.c
1 /*
2     comedi_config/comedi_config.c
3     configuration program for comedi kernel module
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>
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 #define CC_VERSION      "0.7.16"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <getopt.h>
38
39 #include <comedi.h>
40
41 #ifdef I18N
42 #include <libintl.h>
43 #define _(a) gettext(a)
44 #else
45 #define _(a) (a)
46 #endif
47
48 int quiet=0,verbose=0;
49
50 int read_buf_size=0;
51 int write_buf_size=0;
52
53 int init_fd;
54 char *init_file;
55 void *init_data;
56 int init_size;
57
58 struct option options[] = {
59         { "verbose", 0, 0, 'v' },
60         { "quiet", 0, 0, 'q' },
61         { "version", 0, 0, 'V' },
62         { "init-data", 1, 0, 'i' },
63         { "remove", 0, 0, 'r' },
64         { "read-buffer", 1, NULL, 0x1000},
65         { "write-buffer", 1, NULL, 0x1001},
66         { 0 },
67 };
68
69 void do_help(int i)
70 {
71         fputs("comedi_config version " CC_VERSION "\n",stderr);
72         fputs(
73 _("usage:  comedi_config [OPTIONS] <device file> [<driver> <opt1>,<opt2>,...]\n"
74 "\n"
75 "OPTIONS:\n"
76 "  -v, --verbose\n"
77 "      verbose output\n"
78 "  -q, --quiet\n"
79 "      quiet output\n"
80 "  -V, --version\n"
81 "      print program version\n"
82 "  -i, --init-data <filename>\n"
83 "      Use file for driver initialization data, typically firmware code.\n"
84 "  -r, --remove\n"
85 "      remove previously configured driver\n"
86 "  --read-buffer <size>\n"
87 "      set buffer size in kilobytes used for reading\n"
88 "  --write-buffer <size>\n"
89 "      set buffer size in kilobytes used for writing\n"
90 "\n"
91 "  <optN> are integers whose interpretation is driver dependent.\n"
92 "  In general, for PCI boards, <opt1> and <opt2> refer to the bus/slot\n"
93 "  indices of the board.  If not specified, a board will automatically\n"
94 "  be chosen.  For non-PCI boards, <opt1> specifies the I/O port base\n"
95 "  address and, if applicable, <opt2> specifies the IRQ.  Additional\n"
96 "  options may be useful, see the Comedi documentation for details.\n")
97                 ,stderr);
98         exit(i);
99 }
100
101 int main(int argc,char *argv[])
102 {
103         comedi_devconfig it;
104         comedi_bufconfig bc;
105         comedi_devinfo devinfo;
106         int fd;
107         int c,i,num,k;
108         char *opts;
109         char *fn,*driver;
110         struct stat statbuf;
111         int ret;
112         int remove=0;
113         int index;
114
115 #ifdef I18N
116         setlocale(LC_ALL, "");
117         bindtextdomain("comedilib", "/usr/share/locale");
118         textdomain("comedilib");
119 #endif
120
121         if(geteuid() != 0)
122                 fprintf(stderr,_("comedi_config should be run as root.  Attempting to continue anyway.\n"));
123
124         while(1){
125                 c=getopt_long(argc, argv, "rvVqi:", options, &index);
126                 if(c==-1)break;
127                 switch(c){
128                 case 'v':
129                         verbose=1;
130                         break;
131                 case 'V':
132                         fputs("comedi_config version " CC_VERSION "\n",stderr);
133                         exit(0);
134                         break;
135                 case 'q':
136                         quiet=1;
137                         break;
138                 case 'r':
139                         remove=1;
140                         break;
141                 case 'i':
142                         init_file=optarg;
143                         break;
144                 case 0x1000:
145                         read_buf_size = strtol(optarg, NULL, 0);
146                         if(read_buf_size < 0)
147                         {
148                                 fprintf(stderr, _("invalid buffer size\n"));
149                                 exit(-1);
150                         }
151                         break;
152                 case 0x1001:
153                         write_buf_size = strtol(optarg, NULL, 0);
154                         if(write_buf_size < 0)
155                         {
156                                 fprintf(stderr, _("invalid buffer size\n"));
157                                 exit(-1);
158                         }
159                         break;
160                 default:
161                         do_help(1);
162                 }
163         }
164
165         if((argc-optind) < 1 || (argc-optind) > 3 ||
166                 ((argc-optind) == 1 && read_buf_size == 0 && write_buf_size == 0 && remove == 0)){
167                 do_help(1);
168         }
169
170         fn=argv[optind];
171
172         fd=open(fn,O_RDWR);
173         if(fd<0){
174                 switch(errno){
175                 case ENODEV:
176                         fprintf(stderr,_("comedi.o not loaded\n"));
177                         break;
178                 case ENXIO:
179                         fprintf(stderr,_("device not configured\n"));
180                         break;
181                 case EPERM:
182                         fprintf(stderr,_("modprobe problem\n"));
183                         break;
184                 default:
185                         perror(fn);
186                         break;
187                 }
188                 exit(1);
189         }
190
191         // if we are attaching or detaching a device
192         if((argc-optind) > 1 || ((argc-optind) == 1 && remove))
193         {
194                 if(argc - optind > 1)
195                         driver=argv[optind+1];
196                 else
197                         driver = "none";
198                 strncpy(it.board_name,driver,COMEDI_NAMELEN-1);
199
200                 for(i=0;i<COMEDI_NDEVCONFOPTS;i++)it.options[i]=0;
201
202                 if((argc-optind)==3){
203                         opts=argv[optind+2];
204                         i=0;
205                         while(*opts){
206                                 if((*opts)==','){
207                                         i++;
208                                         opts++;
209                                         if(i>=COMEDI_NDEVCONFOPTS)
210                                                 do_help(1);
211                                         continue;
212                                 }
213                                 if(sscanf(opts,"%i%n",&num,&k)>0){
214                                         it.options[i]=num;
215                                         opts+=k;
216                                         continue;
217                                 }
218                                 do_help(1);
219                         }
220                 }
221
222                 ret=stat(fn,&statbuf);
223                 if(ret<0){
224                         perror(fn);
225                         exit(1);
226                 }
227 #if 0
228                 /* this appears to be broken */
229                 if(  !(S_ISCHR(statbuf.st_mode)) ||
230                      major(statbuf.st_dev)!=COMEDI_MAJOR){
231                         if(!quiet)
232                                 fprintf(stderr,"warning: %s might not be a comedi device\n",fn);
233                 }
234 #endif
235
236                 if(init_file){
237                         struct stat buf;
238
239                         init_fd = open(init_file,O_RDONLY);
240                         if(init_fd<0){
241                                 perror(init_file);
242                                 exit(1);
243                         }
244
245                         fstat(init_fd,&buf);
246
247                         init_size = buf.st_size;
248                         init_data = malloc(init_size);
249                         if(init_data==NULL){
250                                 perror(_("allocating initialization data\n"));
251                                 exit(1);
252                         }
253
254                         ret = read(init_fd,init_data,init_size);
255                         if(ret<0){
256                                 perror(_("reading initialization data\n"));
257                                 exit(1);
258                         }
259
260                         it.options[COMEDI_DEVCONF_AUX_DATA]=(int)init_data;
261                         it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]=init_size;
262                 }
263
264         /* add: sanity check for device */
265
266                 if(verbose){
267                         printf(_("configuring driver=%s "),it.board_name);
268                         for(i=0;i<COMEDI_NDEVCONFOPTS;i++)printf("%d,",it.options[i]);
269                         printf("\n");
270                 }
271                 if(ioctl(fd,COMEDI_DEVCONFIG,remove?NULL:&it)<0){
272                         int err=errno;
273                         perror(_("Configure failed!"));
274                         fprintf(stderr,_("Check kernel log for more information\n"));
275                         fprintf(stderr,_("Possible reasons for failure:\n"));
276                         switch(err){
277                         case EINVAL:
278                                 fprintf(stderr,"  \n");
279                                 break;
280                         case EBUSY:
281                                 fprintf(stderr,_("  Already configured\n"));
282                                 break;
283                         case EIO:
284                                 fprintf(stderr,_("  Driver not found\n"));
285                                 break;
286                         case EPERM:
287                                 fprintf(stderr,_("  Not root\n"));
288                                 break;
289                         case EFAULT:
290                                 fprintf(stderr,_("  Comedi bug\n"));
291                                 break;
292                         default:
293                                 fprintf(stderr,_("  Unknown\n"));
294                                 break;
295                         }
296
297                         exit(1);
298                 }
299         }
300
301         // dont do buffer resize if we have removed device
302         if(remove == 0)
303         {
304                 if(read_buf_size || write_buf_size){
305                         ret = ioctl(fd,COMEDI_DEVINFO,&devinfo);
306                         if(ret<0){
307                                 perror("devinfo");
308                                 exit(1);
309                         }
310                         if(devinfo.version_code < ((7<<8) | (57))){
311                                 fprintf(stderr,_("Buffer resizing requires Comedi version >= 0.7.57\n"));
312                                 exit(1);
313                         }
314                 }
315
316                 // do buffer resizing
317                 if(read_buf_size)
318                 {
319                         if(devinfo.read_subdevice < 0){
320                                 fprintf(stderr,_("warning: no read subdevice, resize ignored\n"));
321                         }else{
322                                 memset(&bc, 0, sizeof(bc));
323                                 bc.subdevice = devinfo.read_subdevice;
324                                 bc.maximum_size = read_buf_size * 1024;
325                                 bc.size = read_buf_size * 1024;
326                                 if(ioctl(fd, COMEDI_BUFCONFIG, &bc) < 0)
327                                 {
328                                         perror(_("buffer resize error"));
329                                         exit(1);
330                                 }
331                                 if(verbose)
332                                 {
333                                         printf(_("%s read buffer resized to %i kilobytes\n"),
334                                                 fn, bc.size / 1024);
335                                 }
336                         }
337                 }
338                 if(write_buf_size)
339                 {
340                         if(devinfo.write_subdevice < 0){
341                                 fprintf(stderr,_("warning: no write subdevice, resize ignored\n"));
342                         }else{
343                                 memset(&bc, 0, sizeof(bc));
344                                 bc.subdevice = devinfo.write_subdevice;
345                                 bc.maximum_size = write_buf_size * 1024;
346                                 bc.size = write_buf_size * 1024;
347                                 if(ioctl(fd, COMEDI_BUFCONFIG, &bc) < 0)
348                                 {
349                                         perror(_("buffer resize error"));
350                                         exit(1);
351                                 }
352                                 if(verbose)
353                                 {
354                                         printf(_("%s write buffer resized to %i kilobytes\n"),
355                                                 fn, bc.size / 1024);
356                                 }
357                         }
358                 }
359         }
360
361         exit(0);
362 }
363