Home | History | Annotate | Line # | Download | only in lptctl
lptctl.c revision 1.10
      1 /* $NetBSD: lptctl.c,v 1.10 2004/02/03 21:46:39 jdolecek Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gary Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: lptctl.c,v 1.10 2004/02/03 21:46:39 jdolecek Exp $");
     41 
     42 #include <stdio.h>
     43 #include <fcntl.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 #include <stdlib.h>
     47 #include <err.h>
     48 
     49 #include <sys/ioctl.h>
     50 
     51 #include <dev/ppbus/lptio.h>
     52 
     53 /* Prototypes */
     54 static void usage(int status);
     55 static void print_lpt_info(int, int);
     56 
     57 int
     58 main(const int argc, const char * const * argv) {
     59 	int fd, i;
     60 	int omode, mode, oflags, flags;
     61 
     62 	setprogname(argv[0]);
     63 
     64 	/* N = command name + device name + number of command-arg pairs */
     65 	/* Check number of arguments: at least 2, always even */
     66 	if((argc < 2) || (argc % 2 != 0))
     67 		usage(1);
     68 
     69 	if ((fd = open(argv[1], O_RDONLY, 0)) == -1)
     70 		err(2, "device open");
     71 
     72 	/* get current settings */
     73 	if (ioctl(fd, LPTGFLAGS, &flags) == -1)
     74 		err(2, "ioctl(LPTGFLAGS)");
     75 	oflags = flags;
     76 
     77 	if (ioctl(fd, LPTGMODE, &mode) == -1)
     78 		err(2, "ioctl(LPTGMODE)");
     79 	omode = mode;
     80 
     81 	/* Get command and arg pairs (if any) and do an ioctl for each */
     82 	for(i = 2; i < argc; i += 2) {
     83 		if (strcmp("dma", argv[i]) == 0) {
     84 			if (strcmp("yes", argv[i + 1]) == 0)
     85 				flags |= LPT_DMA;
     86 			else if (strcmp("no", argv[i + 1]) == 0)
     87 				flags &= ~LPT_DMA;
     88 			else {
     89 				errx(2, "invalid '%s' command argument '%s'",
     90 					argv[i], argv[i + 1]);
     91 			}
     92 		} else if (strcmp("mode", argv[i]) == 0) {
     93 			if (strcmp("standard", argv[i + 1]) == 0)
     94 				mode = mode_standard;
     95 			else if (strcmp("ps2", argv[i + 1]) == 0)
     96 				mode = mode_ps2;
     97 			else if (strcmp("nibble", argv[i + 1]) == 0)
     98 				mode = mode_nibble;
     99 			else if (strcmp("fast", argv[i + 1]) == 0)
    100 				mode = mode_fast;
    101 			else if (strcmp("ecp", argv[i + 1]) == 0)
    102 				mode = mode_ecp;
    103 			else if (strcmp("epp", argv[i + 1]) == 0)
    104 				mode = mode_epp;
    105 			else {
    106 				errx(2, "invalid '%s' command argument '%s'",
    107 					argv[i], argv[i+1]);
    108 			}
    109 		} else if (strcmp("ieee", argv[i]) == 0) {
    110 			if (strcmp("yes", argv[i + 1]) == 0)
    111 				flags |= LPT_IEEE;
    112 			else if (strcmp("no", argv[i + 1]) == 0)
    113 				flags &= ~LPT_IEEE;
    114 			else {
    115 				errx(2, "invalid '%s' command argument '%s'",
    116 					argv[i], argv[i+1]);
    117 			}
    118 		} else if (strcmp("intr", argv[i]) == 0) {
    119 			if (strcmp("yes", argv[i + 1]) == 0)
    120 				flags |= LPT_INTR;
    121 			else if (strcmp("no", argv[i + 1]) == 0)
    122 				flags &= ~LPT_INTR;
    123 			else {
    124 				errx(2, "invalid '%s' command argument '%s'",
    125 					argv[i], argv[i+1]);
    126 			}
    127 		} else if (strcmp("prime", argv[i]) == 0) {
    128 			if (strcmp("yes", argv[i + 1]) == 0)
    129 				flags |= LPT_PRIME;
    130 			else if (strcmp("no", argv[i + 1]) == 0)
    131 				flags &= ~LPT_PRIME;
    132 			else {
    133 				errx(2, "invalid '%s' command argument '%s'",
    134 					argv[i], argv[i+1]);
    135 			}
    136 		} else if (strcmp("autolf", argv[i]) == 0) {
    137 			if (strcmp("yes", argv[i + 1]) == 0)
    138 				flags |= LPT_AUTOLF;
    139 			else if (strcmp("no", argv[i + 1]) == 0)
    140 				flags &= ~LPT_AUTOLF;
    141 			else {
    142 				errx(2, "invalid '%s' command argument '%s'",
    143 					argv[i], argv[i+1]);
    144 			}
    145 		} else {
    146 			errx(2, "invalid command '%s'", argv[i]);
    147 		}
    148 	}
    149 
    150 	/* update mode and flags */
    151 	if (flags != oflags) {
    152 		if (ioctl(fd, LPTSFLAGS, &flags) == -1)
    153 			err(2, "ioctl(LPTSFLAGS)");
    154 	}
    155 	if (mode != omode) {
    156 		if (ioctl(fd, LPTSMODE, &mode) == -1)
    157 			err(2, "ioctl(LPTSMODE)");
    158 	}
    159 
    160 	/* Print out information on device */
    161 	printf("%s status:\n\t", argv[1]);
    162 	print_lpt_info(mode, flags);
    163 
    164 	exit(0);
    165 	/* NOTREACHED */
    166 }
    167 
    168 static void
    169 print_lpt_info(int mode, int flags) {
    170 	printf("mode=");
    171 	switch(mode) {
    172 	case mode_standard:
    173 		printf("standard ");
    174 		break;
    175 	case mode_nibble:
    176 		printf("nibble ");
    177 		break;
    178 	case mode_fast:
    179 		printf("fast ");
    180 		break;
    181 	case mode_ps2:
    182 		printf("ps2 ");
    183 		break;
    184 	case mode_ecp:
    185 		printf("ecp ");
    186 		break;
    187 	case mode_epp:
    188 		printf("epp ");
    189 		break;
    190 	default:
    191 		printf("<unknown> ");
    192 		break;
    193 	}
    194 
    195 	printf("dma=%s ", (flags & LPT_DMA) ? "yes" : "no");
    196 	printf("ieee=%s ", (flags & LPT_IEEE) ? "yes" : "no");
    197 	printf("intr=%s ", (flags & LPT_INTR) ? "yes" : "no");
    198 	printf("prime=%s ", (flags & LPT_PRIME) ? "yes" : "no");
    199 	printf("autolf=%s ", (flags & LPT_AUTOLF) ? "yes" : "no");
    200 
    201 	printf("\n");
    202 }
    203 
    204 static void
    205 usage(int status) {
    206 	printf("usage:\t%s /dev/device [[command arg] ...]\n"
    207 		"\tcommands are:\n"
    208 		"\t\tmode [standard|ps2|nibble|fast|ecp|epp]\n"
    209 		"\t\tdma [yes|no]\n"
    210 		"\t\tieee [yes|no]\n"
    211 		"\t\tintr [yes|no]\n"
    212 		"\t\tprime [yes|no]\n"
    213 		"\t\tautolf [yes|no]\n",
    214 		getprogname());
    215 	exit(status);
    216 }
    217