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 176dc3c6f7SGeoff Rehmet * derived from this software withough 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[] = 33bc35c174SNicolas Souchu "$Id: lptcontrol.c,v 1.6 1997/09/25 06:36:29 charnier Exp $"; 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 4512365022SGeoff Rehmet #include <machine/lpt.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" 5212365022SGeoff Rehmet #define DEFAULT_UNIT "0" 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 59fe577fe9SPhilippe Charnier static void usage() 606dc3c6f7SGeoff Rehmet { 61bc35c174SNicolas Souchu fprintf(stderr, "usage: lptcontrol -i | -p | -s | -e [-u <unit no.>]\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 7612365022SGeoff Rehmet static char * dev_file(char unit_no) 7712365022SGeoff Rehmet { 7812365022SGeoff Rehmet static char devname[_POSIX_PATH_MAX+1]; 7912365022SGeoff Rehmet int len; 8012365022SGeoff Rehmet 8112365022SGeoff Rehmet strncpy(devname, PATH_LPCTL, _POSIX_PATH_MAX); 8212365022SGeoff Rehmet devname[len = strlen(devname)] = unit_no; 8312365022SGeoff Rehmet devname[++len] = '\0'; 8412365022SGeoff Rehmet 8512365022SGeoff Rehmet return(devname); 8612365022SGeoff Rehmet } 876dc3c6f7SGeoff Rehmet 886dc3c6f7SGeoff Rehmet int main (int argc, char * argv[]) 896dc3c6f7SGeoff Rehmet { 906dc3c6f7SGeoff Rehmet int opt; 9112365022SGeoff Rehmet int irq_status = IRQ_INVALID; 9212365022SGeoff Rehmet char * unit = DEFAULT_UNIT; 936dc3c6f7SGeoff Rehmet 94bc35c174SNicolas Souchu while((opt = getopt(argc, argv, "ipesu:")) != -1) 956dc3c6f7SGeoff Rehmet switch(opt) { 966dc3c6f7SGeoff Rehmet case 'i': irq_status = USE_IRQ; break; 976dc3c6f7SGeoff Rehmet case 'p': irq_status = DO_POLL; break; 98bc35c174SNicolas Souchu case 'e': irq_status = USE_EXT_MODE; break; 99bc35c174SNicolas Souchu case 's': irq_status = USE_STD_MODE; break; 10012365022SGeoff Rehmet case 'u': unit = optarg; 10112365022SGeoff Rehmet if(!isdigit(*unit)) 102fe577fe9SPhilippe Charnier usage(); 10312365022SGeoff Rehmet break; 104fe577fe9SPhilippe Charnier default : usage(); 1056dc3c6f7SGeoff Rehmet } 10612365022SGeoff Rehmet if(irq_status == IRQ_INVALID) 107fe577fe9SPhilippe Charnier usage(); 1086dc3c6f7SGeoff Rehmet 10912365022SGeoff Rehmet set_interrupt_status(irq_status, dev_file(*unit)); 1106dc3c6f7SGeoff Rehmet 1116dc3c6f7SGeoff Rehmet exit(0); 1126dc3c6f7SGeoff Rehmet } 113