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