2 comedi_config/comedi_config.c
3 configuration program for comedi kernel module
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998,2000 David A. Schleef <ds@stm.lbl.gov>
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.
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.
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.
24 #define CC_VERSION "0.7.13"
26 #include <sys/types.h>
28 #include <sys/sysmacros.h>
30 #include <sys/ioctl.h>
41 int quiet=0,verbose=0;
51 struct option options[] = {
52 { "verbose", 0, 0, 'v' },
53 { "quiet", 0, 0, 'q' },
54 { "version", 0, 0, 'V' },
55 { "init-data", 1, 0, 'i' },
56 { "remove", 0, 0, 'r' },
57 { "read-buffer", 1, NULL, 0x1000},
58 { "write-buffer", 1, NULL, 0x1001},
64 "comedi_config version " CC_VERSION "\n"
65 "usage: comedi_config [OPTIONS] <device file> [<driver> <opt1>,<opt2>,...]\n"
69 "\t\tverbose output\n"
73 "\t\tprint program version\n"
74 "\t-i --init-data <filename>\n"
75 "\t\tI don't know what this does\n"
77 "\t\tremove previously configured driver\n"
78 "\t--read-buffer <size>\n"
79 "\t\tset buffer size in kilobytes used for reads from the <device file>\n"
80 "\t--write-buffer <size>\n"
81 "\t\tset buffer size in kilobytes used for writes to the <device file>\n"
83 "<optN> are integers whose interpretation depends on\n"
84 "the driver. In general, however, for non-plug-and-play boards\n"
85 "opt1 refers to the I/O port address and opt2 refers to IRQ number\n"
86 "to be used by the driver. For plug-and-play boards, opt1 and opt2\n"
87 "are optional and allow you to specify the bus and slot of the card\n"
88 "(in case you have two identical cards).\n"
93 int main(int argc,char *argv[])
97 comedi_devinfo devinfo;
115 c=getopt_long(argc, argv, "rvVqi:", options, &index);
122 fputs("comedi_config version " CC_VERSION "\n",stderr);
135 read_buf_size = strtol(optarg, NULL, 0);
136 if(read_buf_size < 0)
138 fprintf(stderr, "invalid buffer size\n");
143 write_buf_size = strtol(optarg, NULL, 0);
144 if(write_buf_size < 0)
146 fprintf(stderr, "invalid buffer size\n");
155 if((argc-optind) < 1 || (argc-optind) > 3 ||
156 ((argc-optind) == 1 && read_buf_size == 0 && write_buf_size == 0 && remove == 0)){
166 fprintf(stderr,"comedi.o not loaded\n");
175 // if we are attaching or detaching a device
176 if((argc-optind) > 1 || ((argc-optind) == 1 && remove))
178 if(argc - optind > 1)
179 driver=argv[optind+1];
182 strncpy(it.board_name,driver,COMEDI_NAMELEN-1);
184 for(i=0;i<COMEDI_NDEVCONFOPTS;i++)it.options[i]=0;
186 if((argc-optind)==3){
193 if(i>=COMEDI_NDEVCONFOPTS)
197 if(sscanf(opts,"%i%n",&num,&k)>0){
206 ret=stat(fn,&statbuf);
212 /* this appears to be broken */
213 if( !(S_ISCHR(statbuf.st_mode)) ||
214 major(statbuf.st_dev)!=COMEDI_MAJOR){
216 fprintf(stderr,"warning: %s might not be a comedi device\n",fn);
223 init_fd = open(init_file,O_RDONLY);
231 init_size = buf.st_size;
232 init_data = malloc(init_size);
234 perror("allocating initialization data\n");
238 ret = read(init_fd,init_data,init_size);
240 perror("reading initialization data\n");
244 it.options[0]=(int)init_data;
245 it.options[1]=init_size;
248 /* add: sanity check for device */
251 printf("configuring driver=%s ",it.board_name);
252 for(i=0;i<COMEDI_NDEVCONFOPTS;i++)printf("%d,",it.options[i]);
255 if(ioctl(fd,COMEDI_DEVCONFIG,remove?NULL:&it)<0){
257 perror("Configure failed!");
258 fprintf(stderr,"Check kernel log for more information\n");
259 fprintf(stderr,"Possible reasons for failure:\n");
262 fprintf(stderr," \n");
265 fprintf(stderr," Already configured\n");
268 fprintf(stderr," Driver not found\n");
271 fprintf(stderr," Not root\n");
274 fprintf(stderr," Comedi bug\n");
277 fprintf(stderr," Unknown\n");
285 if(read_buf_size || write_buf_size){
286 ret = ioctl(fd,COMEDI_DEVINFO,&devinfo);
291 if(devinfo.version_code < ((7<<8) | (57))){
292 fprintf(stderr,"Buffer resizing requires Comedi version >= 0.7.57\n");
297 // do buffer resizing
300 if(devinfo.read_subdevice){
301 fprintf(stderr,"warning: no read subdevice, resize ignored\n");
303 memset(&bc, 0, sizeof(bc));
304 bc.subdevice = devinfo.read_subdevice;
305 bc.maximum_size = read_buf_size * 1024;
306 bc.size = read_buf_size * 1024;
307 if(ioctl(fd, COMEDI_BUFCONFIG, &bc) < 0)
309 perror("buffer resize error");
314 printf("%s read buffer resized to %i kilobytes\n",
321 if(devinfo.write_subdevice){
322 fprintf(stderr,"warning: no write subdevice, resize ignored\n");
324 memset(&bc, 0, sizeof(bc));
325 bc.subdevice = devinfo.write_subdevice;
326 bc.maximum_size = write_buf_size * 1024;
327 bc.size = write_buf_size * 1024;
328 if(ioctl(fd, COMEDI_BUFCONFIG, &bc) < 0)
330 perror("buffer resize error");
335 printf("%s write buffer resized to %i kilobytes\n",