xref: /freebsd/usr.sbin/lptcontrol/lptcontrol.c (revision 21dc7d4f578ad6d23b31512cb85464b3d4dd93ec)
16dc3c6f7SGeoff Rehmet /*
26dc3c6f7SGeoff Rehmet  * Copyright (c) 1994 Geoffrey M. Rehmet
36dc3c6f7SGeoff Rehmet  * All rights reserved.
46dc3c6f7SGeoff Rehmet  *
56dc3c6f7SGeoff Rehmet  * Redistribution and use in source and binary forms, with or without
66dc3c6f7SGeoff Rehmet  * modification, are permitted provided that the following conditions
76dc3c6f7SGeoff Rehmet  * are met:
86dc3c6f7SGeoff Rehmet  * 1. Redistributions of source code must retain the above copyright
96dc3c6f7SGeoff Rehmet  *    notice, this list of conditions and the following disclaimer.
106dc3c6f7SGeoff Rehmet  * 2. Redistributions in binary form must reproduce the above copyright
116dc3c6f7SGeoff Rehmet  *    notice, this list of conditions and the following disclaimer in the
126dc3c6f7SGeoff Rehmet  *    documentation and/or other materials provided with the distribution.
136dc3c6f7SGeoff Rehmet  * 3. All advertising materials mentioning features or use of this software
146dc3c6f7SGeoff Rehmet  *    must display the following acknowledgement:
156dc3c6f7SGeoff Rehmet  *      This product includes software developed by Geoffrey M. Rehmet
166dc3c6f7SGeoff Rehmet  * 4. The name of the author may not be used to endorse or promote products
1721dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
186dc3c6f7SGeoff Rehmet  *
196dc3c6f7SGeoff Rehmet  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
206dc3c6f7SGeoff Rehmet  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
216dc3c6f7SGeoff Rehmet  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
226dc3c6f7SGeoff Rehmet  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
236dc3c6f7SGeoff Rehmet  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
246dc3c6f7SGeoff Rehmet  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
256dc3c6f7SGeoff Rehmet  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
266dc3c6f7SGeoff Rehmet  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
276dc3c6f7SGeoff Rehmet  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
286dc3c6f7SGeoff Rehmet  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
296dc3c6f7SGeoff Rehmet  */
306dc3c6f7SGeoff Rehmet 
31fe577fe9SPhilippe Charnier #ifndef lint
32fe577fe9SPhilippe Charnier static const char rcsid[] =
3397d92980SPeter Wemm   "$FreeBSD$";
34fe577fe9SPhilippe Charnier #endif /* not lint */
35fe577fe9SPhilippe Charnier 
3612365022SGeoff Rehmet #include <ctype.h>
37fe577fe9SPhilippe Charnier #include <err.h>
3812365022SGeoff Rehmet #include <limits.h>
3912365022SGeoff Rehmet #include <paths.h>
4012365022SGeoff Rehmet #include <stdio.h>
4112365022SGeoff Rehmet #include <stdlib.h>
4212365022SGeoff Rehmet #include <string.h>
4312365022SGeoff Rehmet #include <unistd.h>
4412365022SGeoff Rehmet 
45f8373524SPeter Wemm #include <dev/ppbus/lptio.h>
466dc3c6f7SGeoff Rehmet #include <sys/file.h>
476dc3c6f7SGeoff Rehmet #include <sys/ioctl.h>
4812365022SGeoff Rehmet #include <sys/types.h>
496dc3c6f7SGeoff Rehmet 
506dc3c6f7SGeoff Rehmet 
5112365022SGeoff Rehmet #define PATH_LPCTL	_PATH_DEV "lpctl"
522ae9a4a0SAlfred Perlstein #define DEFAULT_DEVICE	_PATH_DEV "lpt0"
536dc3c6f7SGeoff Rehmet #define IRQ_INVALID	-1
546dc3c6f7SGeoff Rehmet #define DO_POLL		0
556dc3c6f7SGeoff Rehmet #define USE_IRQ		1
56bc35c174SNicolas Souchu #define USE_EXT_MODE	2
57bc35c174SNicolas Souchu #define USE_STD_MODE	3
586dc3c6f7SGeoff Rehmet 
59167ce4fbSDima Dorfman static void usage(void)
606dc3c6f7SGeoff Rehmet {
613876b692SNicolas Souchu 	fprintf(stderr, "usage: lptcontrol -i | -p | -s | -e [-d device]\n");
626dc3c6f7SGeoff Rehmet 	exit(1);
636dc3c6f7SGeoff Rehmet }
646dc3c6f7SGeoff Rehmet 
656dc3c6f7SGeoff Rehmet static void set_interrupt_status(int irq_status, const char * file)
666dc3c6f7SGeoff Rehmet {
676dc3c6f7SGeoff Rehmet 	int	fd;
686dc3c6f7SGeoff Rehmet 
69fe577fe9SPhilippe Charnier 	if((fd = open(file, O_WRONLY, 0660)) < 0)
70fe577fe9SPhilippe Charnier 		err(1, "open");
71fe577fe9SPhilippe Charnier 	if(ioctl(fd, LPT_IRQ, &irq_status) < 0)
72fe577fe9SPhilippe Charnier 		err(1, "ioctl");
736dc3c6f7SGeoff Rehmet 	close(fd);
746dc3c6f7SGeoff Rehmet }
756dc3c6f7SGeoff Rehmet 
766dc3c6f7SGeoff Rehmet int main (int argc, char * argv[])
776dc3c6f7SGeoff Rehmet {
786dc3c6f7SGeoff Rehmet 	int		opt;
7912365022SGeoff Rehmet 	int		irq_status = IRQ_INVALID;
80167ce4fbSDima Dorfman 	const char	*device = DEFAULT_DEVICE;
816dc3c6f7SGeoff Rehmet 
823876b692SNicolas Souchu 	while((opt = getopt(argc, argv, "ipesd:")) != -1)
836dc3c6f7SGeoff Rehmet 		switch(opt) {
846dc3c6f7SGeoff Rehmet 		case 'i': irq_status = USE_IRQ; break;
856dc3c6f7SGeoff Rehmet 		case 'p': irq_status = DO_POLL; break;
86bc35c174SNicolas Souchu 		case 'e': irq_status = USE_EXT_MODE; break;
87bc35c174SNicolas Souchu 		case 's': irq_status = USE_STD_MODE; break;
883876b692SNicolas Souchu 		case 'd': device = optarg; break;
89fe577fe9SPhilippe Charnier 		default : usage();
906dc3c6f7SGeoff Rehmet 		}
9112365022SGeoff Rehmet 	if(irq_status == IRQ_INVALID)
92fe577fe9SPhilippe Charnier 		usage();
936dc3c6f7SGeoff Rehmet 
943876b692SNicolas Souchu 	set_interrupt_status(irq_status, device);
956dc3c6f7SGeoff Rehmet 
966dc3c6f7SGeoff Rehmet 	exit(0);
976dc3c6f7SGeoff Rehmet }
98