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@schleef.org>
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.16"
26 #include <sys/types.h>
28 #include <sys/sysmacros.h>
30 #include <sys/ioctl.h>
43 #define _(a) gettext(a)
48 int quiet=0,verbose=0;
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},
71 fputs("comedi_config version " CC_VERSION "\n",stderr);
73 _("usage: comedi_config [OPTIONS] <device file> [<driver> <opt1>,<opt2>,...]\n"
81 " print program version\n"
82 " -i, --init-data <filename>\n"
83 " Use file for driver initialization data, typically firmware code.\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"
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")
101 int main(int argc,char *argv[])
105 comedi_devinfo devinfo;
116 setlocale(LC_ALL, "");
117 bindtextdomain("comedilib", "/usr/share/locale");
118 textdomain("comedilib");
122 fprintf(stderr,_("comedi_config should be run as root. Attempting to continue anyway.\n"));
125 c=getopt_long(argc, argv, "rvVqi:", options, &index);
132 fputs("comedi_config version " CC_VERSION "\n",stderr);
145 read_buf_size = strtol(optarg, NULL, 0);
146 if(read_buf_size < 0)
148 fprintf(stderr, _("invalid buffer size\n"));
153 write_buf_size = strtol(optarg, NULL, 0);
154 if(write_buf_size < 0)
156 fprintf(stderr, _("invalid buffer size\n"));
165 if((argc-optind) < 1 || (argc-optind) > 3 ||
166 ((argc-optind) == 1 && read_buf_size == 0 && write_buf_size == 0 && remove == 0)){
176 fprintf(stderr,_("comedi.o not loaded\n"));
179 fprintf(stderr,_("device not configured\n"));
182 fprintf(stderr,_("modprobe problem\n"));
191 // if we are attaching or detaching a device
192 if((argc-optind) > 1 || ((argc-optind) == 1 && remove))
194 if(argc - optind > 1)
195 driver=argv[optind+1];
198 strncpy(it.board_name,driver,COMEDI_NAMELEN-1);
200 for(i=0;i<COMEDI_NDEVCONFOPTS;i++)it.options[i]=0;
202 if((argc-optind)==3){
209 if(i>=COMEDI_NDEVCONFOPTS)
213 if(sscanf(opts,"%i%n",&num,&k)>0){
222 ret=stat(fn,&statbuf);
228 /* this appears to be broken */
229 if( !(S_ISCHR(statbuf.st_mode)) ||
230 major(statbuf.st_dev)!=COMEDI_MAJOR){
232 fprintf(stderr,"warning: %s might not be a comedi device\n",fn);
239 init_fd = open(init_file,O_RDONLY);
247 init_size = buf.st_size;
248 init_data = malloc(init_size);
250 perror(_("allocating initialization data\n"));
254 ret = read(init_fd,init_data,init_size);
256 perror(_("reading initialization data\n"));
260 it.options[COMEDI_DEVCONF_AUX_DATA]=(int)init_data;
261 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]=init_size;
264 /* add: sanity check for device */
267 printf(_("configuring driver=%s "),it.board_name);
268 for(i=0;i<COMEDI_NDEVCONFOPTS;i++)printf("%d,",it.options[i]);
271 if(ioctl(fd,COMEDI_DEVCONFIG,remove?NULL:&it)<0){
273 perror(_("Configure failed!"));
274 fprintf(stderr,_("Check kernel log for more information\n"));
275 fprintf(stderr,_("Possible reasons for failure:\n"));
278 fprintf(stderr," \n");
281 fprintf(stderr,_(" Already configured\n"));
284 fprintf(stderr,_(" Driver not found\n"));
287 fprintf(stderr,_(" Not root\n"));
290 fprintf(stderr,_(" Comedi bug\n"));
293 fprintf(stderr,_(" Unknown\n"));
301 // dont do buffer resize if we have removed device
304 if(read_buf_size || write_buf_size){
305 ret = ioctl(fd,COMEDI_DEVINFO,&devinfo);
310 if(devinfo.version_code < ((7<<8) | (57))){
311 fprintf(stderr,_("Buffer resizing requires Comedi version >= 0.7.57\n"));
316 // do buffer resizing
319 if(devinfo.read_subdevice < 0){
320 fprintf(stderr,_("warning: no read subdevice, resize ignored\n"));
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)
328 perror(_("buffer resize error"));
333 printf(_("%s read buffer resized to %i kilobytes\n"),
340 if(devinfo.write_subdevice < 0){
341 fprintf(stderr,_("warning: no write subdevice, resize ignored\n"));
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)
349 perror(_("buffer resize error"));
354 printf(_("%s write buffer resized to %i kilobytes\n"),