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