Home | History | Annotate | Line # | Download | only in chio
chio.c revision 1.15
      1  1.15      cgd /*	$NetBSD: chio.c,v 1.15 2001/02/19 22:39:39 cgd Exp $	*/
      2   1.1  thorpej 
      3  1.13  thorpej /*-
      4  1.13  thorpej  * Copyright (c) 1996, 1998, 1999 The NetBSD Foundation, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7  1.13  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.13  thorpej  * NASA Ames Research Center.
     10  1.13  thorpej  *
     11   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.1  thorpej  * modification, are permitted provided that the following conditions
     13   1.1  thorpej  * are met:
     14   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.13  thorpej  *    must display the following acknowledgement:
     21  1.13  thorpej  *	This product includes software developed by the NetBSD
     22  1.13  thorpej  *	Foundation, Inc. and its contributors.
     23  1.13  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.13  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.13  thorpej  *    from this software without specific prior written permission.
     26   1.1  thorpej  *
     27  1.13  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.13  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.13  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.13  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.13  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.13  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.13  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.13  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.13  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.13  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.13  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  thorpej  */
     39  1.13  thorpej 
     40   1.5   mjacob /*
     41   1.5   mjacob  * Additional Copyright (c) 1997, by Matthew Jacob, for NASA/Ames Research Ctr.
     42   1.5   mjacob  */
     43   1.1  thorpej 
     44   1.2  thorpej #include <sys/cdefs.h>
     45   1.2  thorpej #ifndef lint
     46   1.7  hpeyerl __COPYRIGHT(
     47  1.13  thorpej "@(#) Copyright (c) 1996, 1998, 1999\
     48  1.13  thorpej 	The NetBSD Foundation, Inc.  All rights reserved.");
     49  1.15      cgd __RCSID("$NetBSD: chio.c,v 1.15 2001/02/19 22:39:39 cgd Exp $");
     50   1.2  thorpej #endif
     51   1.2  thorpej 
     52   1.1  thorpej #include <sys/param.h>
     53   1.1  thorpej #include <sys/ioctl.h>
     54   1.1  thorpej #include <sys/chio.h>
     55   1.7  hpeyerl #include <sys/cdio.h>	/* for ATAPI CD changer; too bad it uses a lame API */
     56  1.13  thorpej #include <ctype.h>
     57   1.1  thorpej #include <err.h>
     58   1.1  thorpej #include <errno.h>
     59   1.1  thorpej #include <fcntl.h>
     60   1.1  thorpej #include <limits.h>
     61   1.1  thorpej #include <stdio.h>
     62   1.1  thorpej #include <stdlib.h>
     63   1.1  thorpej #include <string.h>
     64   1.1  thorpej #include <unistd.h>
     65   1.1  thorpej 
     66   1.1  thorpej #include "defs.h"
     67   1.1  thorpej #include "pathnames.h"
     68   1.1  thorpej 
     69   1.2  thorpej int	main __P((int, char *[]));
     70   1.1  thorpej static	void usage __P((void));
     71   1.1  thorpej static	void cleanup __P((void));
     72  1.13  thorpej static	int parse_element_type __P((const char *));
     73  1.13  thorpej static	int parse_element_unit __P((const char *));
     74  1.13  thorpej static	int parse_special __P((const char *));
     75  1.13  thorpej static	int is_special __P((const char *));
     76   1.8  mycroft static	const char *bits_to_string __P((int, const char *));
     77   1.1  thorpej 
     78  1.13  thorpej static	int do_move __P((const char *, int, char **));
     79  1.13  thorpej static	int do_exchange __P((const char *, int, char **));
     80  1.13  thorpej static	int do_position __P((const char *, int, char **));
     81  1.13  thorpej static	int do_params __P((const char *, int, char **));
     82  1.13  thorpej static	int do_getpicker __P((const char *, int, char **));
     83  1.13  thorpej static	int do_setpicker __P((const char *, int, char **));
     84  1.13  thorpej static	int do_status __P((const char *, int, char **));
     85  1.13  thorpej static	int do_ielem __P((const char *, int, char **));
     86  1.13  thorpej static	int do_cdlu __P((const char *, int, char **));
     87   1.1  thorpej 
     88   1.1  thorpej /* Valid changer element types. */
     89   1.1  thorpej const struct element_type elements[] = {
     90   1.1  thorpej 	{ "picker",		CHET_MT },
     91   1.1  thorpej 	{ "slot",		CHET_ST },
     92   1.1  thorpej 	{ "portal",		CHET_IE },
     93   1.1  thorpej 	{ "drive",		CHET_DT },
     94   1.1  thorpej 	{ NULL,			0 },
     95   1.1  thorpej };
     96   1.1  thorpej 
     97   1.1  thorpej /* Valid commands. */
     98   1.1  thorpej const struct changer_command commands[] = {
     99  1.12  thorpej 	{ "move", 	" <from ET> <from EU> <to ET> <to EU> [inv]",
    100  1.12  thorpej 	  do_move },
    101  1.12  thorpej 
    102  1.11  hubertf 	{ "exchange",	" <src ET> <src EU> <dst1 ET> <dst1 EU>\n"
    103  1.12  thorpej 	                "\t\t [<dst2 ET> <dst2 EU>] [inv1] [inv2]",
    104  1.12  thorpej 	  do_exchange },
    105  1.12  thorpej 
    106  1.12  thorpej 	{ "position",	" <to ET> <to EU> [inv]", do_position },
    107  1.12  thorpej 
    108  1.12  thorpej 	{ "params",	"",
    109  1.12  thorpej 	  do_params },
    110  1.12  thorpej 
    111  1.12  thorpej 	{ "getpicker",	"",
    112  1.12  thorpej 	  do_getpicker },
    113  1.12  thorpej 
    114  1.12  thorpej 	{ "setpicker",	" <picker>",
    115  1.12  thorpej 	  do_setpicker },
    116  1.12  thorpej 
    117  1.13  thorpej 	{ "status",	" [<ET> [unit [count]]] [voltags]",
    118  1.12  thorpej 	  do_status },
    119  1.12  thorpej 
    120  1.12  thorpej 	{ "ielem", 	"",
    121  1.12  thorpej 	  do_ielem },
    122  1.12  thorpej 
    123  1.11  hubertf 	{ "cdlu",	" load|unload <slot>\n"
    124  1.12  thorpej 	                "\t     abort",
    125  1.12  thorpej 	  do_cdlu },
    126  1.12  thorpej 
    127  1.12  thorpej 	{ NULL,		NULL,
    128  1.12  thorpej 	  NULL },
    129   1.1  thorpej };
    130   1.1  thorpej 
    131   1.1  thorpej /* Valid special words. */
    132   1.1  thorpej const struct special_word specials[] = {
    133   1.1  thorpej 	{ "inv",		SW_INVERT },
    134   1.1  thorpej 	{ "inv1",		SW_INVERT1 },
    135   1.1  thorpej 	{ "inv2",		SW_INVERT2 },
    136  1.13  thorpej 	{ "voltags",		SW_VOLTAGS },
    137   1.1  thorpej 	{ NULL,			0 },
    138   1.1  thorpej };
    139   1.1  thorpej 
    140   1.1  thorpej static	int changer_fd;
    141   1.8  mycroft static	const char *changer_name;
    142   1.1  thorpej 
    143   1.1  thorpej int
    144   1.1  thorpej main(argc, argv)
    145   1.1  thorpej 	int argc;
    146   1.2  thorpej 	char *argv[];
    147   1.1  thorpej {
    148   1.1  thorpej 	int ch, i;
    149   1.1  thorpej 
    150   1.1  thorpej 	while ((ch = getopt(argc, argv, "f:")) != -1) {
    151   1.1  thorpej 		switch (ch) {
    152   1.1  thorpej 		case 'f':
    153   1.1  thorpej 			changer_name = optarg;
    154   1.1  thorpej 			break;
    155   1.1  thorpej 
    156   1.1  thorpej 		default:
    157   1.1  thorpej 			usage();
    158   1.1  thorpej 		}
    159   1.1  thorpej 	}
    160   1.1  thorpej 	argc -= optind;
    161   1.1  thorpej 	argv += optind;
    162   1.1  thorpej 
    163   1.1  thorpej 	if (argc == 0)
    164   1.1  thorpej 		usage();
    165   1.1  thorpej 
    166   1.1  thorpej 	/* Get the default changer if not already specified. */
    167   1.1  thorpej 	if (changer_name == NULL)
    168   1.1  thorpej 		if ((changer_name = getenv(CHANGER_ENV_VAR)) == NULL)
    169   1.1  thorpej 			changer_name = _PATH_CH;
    170   1.1  thorpej 
    171   1.1  thorpej 	/* Open the changer device. */
    172   1.1  thorpej 	if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1)
    173   1.1  thorpej 		err(1, "%s: open", changer_name);
    174   1.1  thorpej 
    175   1.1  thorpej 	/* Register cleanup function. */
    176   1.1  thorpej 	if (atexit(cleanup))
    177   1.1  thorpej 		err(1, "can't register cleanup function");
    178   1.1  thorpej 
    179   1.1  thorpej 	/* Find the specified command. */
    180   1.1  thorpej 	for (i = 0; commands[i].cc_name != NULL; ++i)
    181   1.1  thorpej 		if (strcmp(*argv, commands[i].cc_name) == 0)
    182   1.1  thorpej 			break;
    183   1.1  thorpej 	if (commands[i].cc_name == NULL)
    184   1.1  thorpej 		errx(1, "unknown command: %s", *argv);
    185   1.1  thorpej 
    186   1.1  thorpej 	/* Skip over the command name and call handler. */
    187   1.1  thorpej 	++argv; --argc;
    188   1.1  thorpej 	exit ((*commands[i].cc_handler)(commands[i].cc_name, argc, argv));
    189   1.6  thorpej 	/* NOTREACHED */
    190   1.1  thorpej }
    191   1.1  thorpej 
    192   1.1  thorpej static int
    193   1.1  thorpej do_move(cname, argc, argv)
    194  1.13  thorpej 	const char *cname;
    195   1.1  thorpej 	int argc;
    196   1.1  thorpej 	char **argv;
    197   1.1  thorpej {
    198  1.13  thorpej 	struct changer_move_request cmd;
    199   1.1  thorpej 	int val;
    200   1.1  thorpej 
    201   1.1  thorpej 	/*
    202   1.1  thorpej 	 * On a move command, we expect the following:
    203   1.1  thorpej 	 *
    204   1.1  thorpej 	 * <from ET> <from EU> <to ET> <to EU> [inv]
    205   1.1  thorpej 	 *
    206   1.1  thorpej 	 * where ET == element type and EU == element unit.
    207   1.1  thorpej 	 */
    208   1.1  thorpej 	if (argc < 4) {
    209   1.1  thorpej 		warnx("%s: too few arguments", cname);
    210  1.11  hubertf 		usage();
    211   1.1  thorpej 	} else if (argc > 5) {
    212   1.1  thorpej 		warnx("%s: too many arguments", cname);
    213  1.11  hubertf 		usage();
    214   1.1  thorpej 	}
    215   1.6  thorpej 	(void) memset(&cmd, 0, sizeof(cmd));
    216   1.1  thorpej 
    217   1.1  thorpej 	/* <from ET>  */
    218   1.1  thorpej 	cmd.cm_fromtype = parse_element_type(*argv);
    219   1.1  thorpej 	++argv; --argc;
    220   1.1  thorpej 
    221   1.1  thorpej 	/* <from EU> */
    222   1.1  thorpej 	cmd.cm_fromunit = parse_element_unit(*argv);
    223   1.1  thorpej 	++argv; --argc;
    224   1.1  thorpej 
    225   1.1  thorpej 	/* <to ET> */
    226   1.1  thorpej 	cmd.cm_totype = parse_element_type(*argv);
    227   1.1  thorpej 	++argv; --argc;
    228   1.1  thorpej 
    229   1.1  thorpej 	/* <to EU> */
    230   1.1  thorpej 	cmd.cm_tounit = parse_element_unit(*argv);
    231   1.1  thorpej 	++argv; --argc;
    232   1.1  thorpej 
    233   1.1  thorpej 	/* Deal with optional command modifier. */
    234   1.1  thorpej 	if (argc) {
    235   1.1  thorpej 		val = parse_special(*argv);
    236   1.1  thorpej 		switch (val) {
    237   1.1  thorpej 		case SW_INVERT:
    238   1.1  thorpej 			cmd.cm_flags |= CM_INVERT;
    239   1.1  thorpej 			break;
    240   1.1  thorpej 
    241   1.1  thorpej 		default:
    242   1.1  thorpej 			errx(1, "%s: inappropriate modifier `%s'",
    243   1.1  thorpej 			    cname, *argv);
    244   1.1  thorpej 			/* NOTREACHED */
    245   1.1  thorpej 		}
    246   1.1  thorpej 	}
    247   1.1  thorpej 
    248   1.1  thorpej 	/* Send command to changer. */
    249   1.6  thorpej 	if (ioctl(changer_fd, CHIOMOVE, &cmd))
    250   1.1  thorpej 		err(1, "%s: CHIOMOVE", changer_name);
    251   1.1  thorpej 
    252   1.1  thorpej 	return (0);
    253   1.1  thorpej }
    254   1.1  thorpej 
    255   1.1  thorpej static int
    256   1.1  thorpej do_exchange(cname, argc, argv)
    257  1.13  thorpej 	const char *cname;
    258   1.1  thorpej 	int argc;
    259   1.1  thorpej 	char **argv;
    260   1.1  thorpej {
    261  1.13  thorpej 	struct changer_exchange_request cmd;
    262   1.1  thorpej 	int val;
    263   1.1  thorpej 
    264   1.1  thorpej 	/*
    265   1.1  thorpej 	 * On an exchange command, we expect the following:
    266   1.1  thorpej 	 *
    267   1.1  thorpej   * <src ET> <src EU> <dst1 ET> <dst1 EU> [<dst2 ET> <dst2 EU>] [inv1] [inv2]
    268   1.1  thorpej 	 *
    269   1.1  thorpej 	 * where ET == element type and EU == element unit.
    270   1.1  thorpej 	 */
    271   1.1  thorpej 	if (argc < 4) {
    272   1.1  thorpej 		warnx("%s: too few arguments", cname);
    273  1.11  hubertf 		usage();
    274   1.1  thorpej 	} else if (argc > 8) {
    275   1.1  thorpej 		warnx("%s: too many arguments", cname);
    276  1.11  hubertf 		usage();
    277   1.1  thorpej 	}
    278   1.6  thorpej 	(void) memset(&cmd, 0, sizeof(cmd));
    279   1.1  thorpej 
    280   1.1  thorpej 	/* <src ET>  */
    281   1.1  thorpej 	cmd.ce_srctype = parse_element_type(*argv);
    282   1.1  thorpej 	++argv; --argc;
    283   1.1  thorpej 
    284   1.1  thorpej 	/* <src EU> */
    285   1.1  thorpej 	cmd.ce_srcunit = parse_element_unit(*argv);
    286   1.1  thorpej 	++argv; --argc;
    287   1.1  thorpej 
    288   1.1  thorpej 	/* <dst1 ET> */
    289   1.1  thorpej 	cmd.ce_fdsttype = parse_element_type(*argv);
    290   1.1  thorpej 	++argv; --argc;
    291   1.1  thorpej 
    292   1.1  thorpej 	/* <dst1 EU> */
    293   1.1  thorpej 	cmd.ce_fdstunit = parse_element_unit(*argv);
    294   1.1  thorpej 	++argv; --argc;
    295   1.1  thorpej 
    296   1.1  thorpej 	/*
    297   1.1  thorpej 	 * If the next token is a special word or there are no more
    298   1.1  thorpej 	 * arguments, then this is a case of simple exchange.
    299   1.1  thorpej 	 * dst2 == src.
    300   1.1  thorpej 	 */
    301   1.1  thorpej 	if ((argc == 0) || is_special(*argv)) {
    302   1.1  thorpej 		cmd.ce_sdsttype = cmd.ce_srctype;
    303   1.1  thorpej 		cmd.ce_sdstunit = cmd.ce_srcunit;
    304   1.1  thorpej 		goto do_special;
    305   1.1  thorpej 	}
    306   1.1  thorpej 
    307   1.1  thorpej 	/* <dst2 ET> */
    308   1.1  thorpej 	cmd.ce_sdsttype = parse_element_type(*argv);
    309   1.1  thorpej 	++argv; --argc;
    310   1.1  thorpej 
    311   1.1  thorpej 	/* <dst2 EU> */
    312   1.1  thorpej 	cmd.ce_sdstunit = parse_element_unit(*argv);
    313   1.1  thorpej 	++argv; --argc;
    314   1.1  thorpej 
    315   1.1  thorpej  do_special:
    316   1.1  thorpej 	/* Deal with optional command modifiers. */
    317   1.1  thorpej 	while (argc) {
    318   1.1  thorpej 		val = parse_special(*argv);
    319   1.1  thorpej 		++argv; --argc;
    320   1.1  thorpej 		switch (val) {
    321   1.1  thorpej 		case SW_INVERT1:
    322   1.1  thorpej 			cmd.ce_flags |= CE_INVERT1;
    323   1.1  thorpej 			break;
    324   1.1  thorpej 
    325   1.1  thorpej 		case SW_INVERT2:
    326   1.1  thorpej 			cmd.ce_flags |= CE_INVERT2;
    327   1.1  thorpej 			break;
    328   1.1  thorpej 
    329   1.1  thorpej 		default:
    330   1.1  thorpej 			errx(1, "%s: inappropriate modifier `%s'",
    331   1.1  thorpej 			    cname, *argv);
    332   1.1  thorpej 			/* NOTREACHED */
    333   1.1  thorpej 		}
    334   1.1  thorpej 	}
    335   1.1  thorpej 
    336   1.1  thorpej 	/* Send command to changer. */
    337   1.6  thorpej 	if (ioctl(changer_fd, CHIOEXCHANGE, &cmd))
    338   1.1  thorpej 		err(1, "%s: CHIOEXCHANGE", changer_name);
    339   1.1  thorpej 
    340   1.1  thorpej 	return (0);
    341   1.1  thorpej }
    342   1.1  thorpej 
    343   1.1  thorpej static int
    344   1.1  thorpej do_position(cname, argc, argv)
    345  1.13  thorpej 	const char *cname;
    346   1.1  thorpej 	int argc;
    347   1.1  thorpej 	char **argv;
    348   1.1  thorpej {
    349  1.13  thorpej 	struct changer_position_request cmd;
    350   1.1  thorpej 	int val;
    351   1.1  thorpej 
    352   1.1  thorpej 	/*
    353   1.1  thorpej 	 * On a position command, we expect the following:
    354   1.1  thorpej 	 *
    355   1.1  thorpej 	 * <to ET> <to EU> [inv]
    356   1.1  thorpej 	 *
    357   1.1  thorpej 	 * where ET == element type and EU == element unit.
    358   1.1  thorpej 	 */
    359   1.1  thorpej 	if (argc < 2) {
    360   1.1  thorpej 		warnx("%s: too few arguments", cname);
    361  1.11  hubertf 		usage();
    362   1.1  thorpej 	} else if (argc > 3) {
    363   1.1  thorpej 		warnx("%s: too many arguments", cname);
    364  1.11  hubertf 		usage();
    365   1.1  thorpej 	}
    366   1.6  thorpej 	(void) memset(&cmd, 0, sizeof(cmd));
    367   1.1  thorpej 
    368   1.1  thorpej 	/* <to ET>  */
    369   1.1  thorpej 	cmd.cp_type = parse_element_type(*argv);
    370   1.1  thorpej 	++argv; --argc;
    371   1.1  thorpej 
    372   1.1  thorpej 	/* <to EU> */
    373   1.1  thorpej 	cmd.cp_unit = parse_element_unit(*argv);
    374   1.1  thorpej 	++argv; --argc;
    375   1.1  thorpej 
    376   1.1  thorpej 	/* Deal with optional command modifier. */
    377   1.1  thorpej 	if (argc) {
    378   1.1  thorpej 		val = parse_special(*argv);
    379   1.1  thorpej 		switch (val) {
    380   1.1  thorpej 		case SW_INVERT:
    381   1.1  thorpej 			cmd.cp_flags |= CP_INVERT;
    382   1.1  thorpej 			break;
    383   1.1  thorpej 
    384   1.1  thorpej 		default:
    385   1.1  thorpej 			errx(1, "%s: inappropriate modifier `%s'",
    386   1.1  thorpej 			    cname, *argv);
    387   1.1  thorpej 			/* NOTREACHED */
    388   1.1  thorpej 		}
    389   1.1  thorpej 	}
    390   1.1  thorpej 
    391   1.1  thorpej 	/* Send command to changer. */
    392   1.6  thorpej 	if (ioctl(changer_fd, CHIOPOSITION, &cmd))
    393   1.1  thorpej 		err(1, "%s: CHIOPOSITION", changer_name);
    394   1.1  thorpej 
    395   1.1  thorpej 	return (0);
    396   1.1  thorpej }
    397   1.1  thorpej 
    398   1.6  thorpej /* ARGSUSED */
    399   1.1  thorpej static int
    400   1.1  thorpej do_params(cname, argc, argv)
    401  1.13  thorpej 	const char *cname;
    402   1.1  thorpej 	int argc;
    403   1.1  thorpej 	char **argv;
    404   1.1  thorpej {
    405   1.1  thorpej 	struct changer_params data;
    406   1.1  thorpej 
    407   1.1  thorpej 	/* No arguments to this command. */
    408   1.1  thorpej 	if (argc) {
    409   1.1  thorpej 		warnx("%s: no arguements expected", cname);
    410  1.11  hubertf 		usage();
    411   1.1  thorpej 	}
    412   1.1  thorpej 
    413   1.1  thorpej 	/* Get params from changer and display them. */
    414   1.6  thorpej 	(void) memset(&data, 0, sizeof(data));
    415   1.6  thorpej 	if (ioctl(changer_fd, CHIOGPARAMS, &data))
    416   1.1  thorpej 		err(1, "%s: CHIOGPARAMS", changer_name);
    417   1.1  thorpej 
    418  1.13  thorpej #define	PLURAL(n)	(n) > 1 ? "s" : ""
    419  1.13  thorpej 
    420   1.6  thorpej 	(void) printf("%s: %d slot%s, %d drive%s, %d picker%s",
    421   1.1  thorpej 	    changer_name,
    422  1.13  thorpej 	    data.cp_nslots, PLURAL(data.cp_nslots),
    423  1.13  thorpej 	    data.cp_ndrives, PLURAL(data.cp_ndrives),
    424  1.13  thorpej 	    data.cp_npickers, PLURAL(data.cp_npickers));
    425   1.1  thorpej 	if (data.cp_nportals)
    426   1.6  thorpej 		(void) printf(", %d portal%s", data.cp_nportals,
    427  1.13  thorpej 		    PLURAL(data.cp_nportals));
    428  1.13  thorpej 
    429  1.13  thorpej #undef PLURAL
    430  1.13  thorpej 
    431   1.6  thorpej 	(void) printf("\n%s: current picker: %d\n", changer_name,
    432   1.6  thorpej 	    data.cp_curpicker);
    433   1.1  thorpej 
    434   1.1  thorpej 	return (0);
    435   1.1  thorpej }
    436   1.1  thorpej 
    437   1.6  thorpej /* ARGSUSED */
    438   1.1  thorpej static int
    439   1.1  thorpej do_getpicker(cname, argc, argv)
    440  1.13  thorpej 	const char *cname;
    441   1.1  thorpej 	int argc;
    442   1.1  thorpej 	char **argv;
    443   1.1  thorpej {
    444   1.1  thorpej 	int picker;
    445   1.1  thorpej 
    446   1.1  thorpej 	/* No arguments to this command. */
    447   1.1  thorpej 	if (argc) {
    448   1.1  thorpej 		warnx("%s: no arguments expected", cname);
    449  1.11  hubertf 		usage();
    450   1.1  thorpej 	}
    451   1.1  thorpej 
    452   1.1  thorpej 	/* Get current picker from changer and display it. */
    453   1.6  thorpej 	if (ioctl(changer_fd, CHIOGPICKER, &picker))
    454   1.1  thorpej 		err(1, "%s: CHIOGPICKER", changer_name);
    455   1.1  thorpej 
    456   1.6  thorpej 	(void) printf("%s: current picker: %d\n", changer_name, picker);
    457   1.1  thorpej 
    458   1.1  thorpej 	return (0);
    459   1.1  thorpej }
    460   1.1  thorpej 
    461   1.1  thorpej static int
    462   1.1  thorpej do_setpicker(cname, argc, argv)
    463  1.13  thorpej 	const char *cname;
    464   1.1  thorpej 	int argc;
    465   1.1  thorpej 	char **argv;
    466   1.1  thorpej {
    467   1.1  thorpej 	int picker;
    468   1.1  thorpej 
    469   1.1  thorpej 	if (argc < 1) {
    470   1.1  thorpej 		warnx("%s: too few arguments", cname);
    471  1.11  hubertf 		usage();
    472   1.1  thorpej 	} else if (argc > 1) {
    473   1.1  thorpej 		warnx("%s: too many arguments", cname);
    474  1.11  hubertf 		usage();
    475   1.1  thorpej 	}
    476   1.1  thorpej 
    477   1.1  thorpej 	picker = parse_element_unit(*argv);
    478   1.1  thorpej 
    479   1.1  thorpej 	/* Set the changer picker. */
    480   1.6  thorpej 	if (ioctl(changer_fd, CHIOSPICKER, &picker))
    481   1.1  thorpej 		err(1, "%s: CHIOSPICKER", changer_name);
    482   1.1  thorpej 
    483   1.1  thorpej 	return (0);
    484   1.1  thorpej }
    485   1.1  thorpej 
    486   1.1  thorpej static int
    487   1.1  thorpej do_status(cname, argc, argv)
    488  1.13  thorpej 	const char *cname;
    489   1.1  thorpej 	int argc;
    490   1.1  thorpej 	char **argv;
    491   1.1  thorpej {
    492  1.13  thorpej 	struct changer_element_status_request cmd;
    493   1.1  thorpej 	struct changer_params data;
    494  1.13  thorpej 	struct changer_element_status *ces;
    495  1.13  thorpej 	int i, chet, schet, echet, count, ucount, unit;
    496  1.13  thorpej 	int have_ucount = 0, have_unit = 0, flags = 0;
    497  1.13  thorpej 	size_t size;
    498   1.1  thorpej 
    499   1.1  thorpej 	/*
    500   1.1  thorpej 	 * On a status command, we expect the following:
    501   1.1  thorpej 	 *
    502  1.13  thorpej 	 * [<ET> [unit [count]]] [voltags]
    503   1.1  thorpej 	 *
    504   1.1  thorpej 	 * where ET == element type.
    505   1.1  thorpej 	 *
    506  1.13  thorpej 	 * If we get no element-related arguments, we get the status of all
    507   1.1  thorpej 	 * known element types.
    508   1.1  thorpej 	 */
    509  1.13  thorpej 	if (argc > 4) {
    510   1.1  thorpej 		warnx("%s: too many arguments", cname);
    511  1.11  hubertf 		usage();
    512   1.1  thorpej 	}
    513   1.1  thorpej 
    514   1.1  thorpej 	/*
    515   1.1  thorpej 	 * Get params from changer.  Specifically, we need the element
    516   1.1  thorpej 	 * counts.
    517   1.1  thorpej 	 */
    518   1.6  thorpej 	(void) memset(&data, 0, sizeof(data));
    519   1.6  thorpej 	if (ioctl(changer_fd, CHIOGPARAMS, &data))
    520   1.1  thorpej 		err(1, "%s: CHIOGPARAMS", changer_name);
    521   1.1  thorpej 
    522  1.13  thorpej 	schet = CHET_MT;
    523  1.13  thorpej 	echet = CHET_DT;
    524  1.13  thorpej 
    525  1.13  thorpej 	for (; argc != 0; argc--, argv++) {
    526  1.13  thorpej 		/*
    527  1.13  thorpej 		 * If we have the voltags modifier, it must be the
    528  1.13  thorpej 		 * last argument.
    529  1.13  thorpej 		 */
    530  1.13  thorpej 		if (is_special(argv[0])) {
    531  1.13  thorpej 			if (argc != 1) {
    532  1.13  thorpej 				warnx("%s: malformed command line", cname);
    533  1.13  thorpej 				usage();
    534  1.13  thorpej 			}
    535  1.13  thorpej 			if (parse_special(argv[0]) != SW_VOLTAGS)
    536  1.13  thorpej 				errx(1, "%s: inappropriate special word: %s",
    537  1.13  thorpej 				    cname, argv[0]);
    538  1.13  thorpej 			flags |= CESR_VOLTAGS;
    539  1.13  thorpej 			continue;
    540  1.13  thorpej 		}
    541  1.13  thorpej 
    542  1.13  thorpej 		/*
    543  1.13  thorpej 		 * If we get an element type, we can't have specified
    544  1.13  thorpej 		 * anything else.
    545  1.13  thorpej 		 */
    546  1.13  thorpej 		if (isdigit(*argv[0]) == 0) {
    547  1.13  thorpej 			if (schet == echet || flags != 0 || have_unit ||
    548  1.13  thorpej 			    have_ucount) {
    549  1.13  thorpej 				warnx("%s: malformed command line", cname);
    550  1.13  thorpej 				usage();
    551  1.13  thorpej 			}
    552  1.13  thorpej 			schet = echet = parse_element_type(argv[0]);
    553  1.13  thorpej 			continue;
    554  1.13  thorpej 		}
    555  1.13  thorpej 
    556  1.13  thorpej 		/*
    557  1.13  thorpej 		 * We know we have a digit here.  If we do, we must
    558  1.13  thorpej 		 * have specified an element type.
    559  1.13  thorpej 		 */
    560  1.13  thorpej 		if (schet != echet) {
    561  1.13  thorpej 			warnx("%s: malformed command line", cname);
    562  1.13  thorpej 			usage();
    563  1.13  thorpej 		}
    564  1.13  thorpej 
    565  1.13  thorpej 		i = parse_element_unit(argv[0]);
    566  1.13  thorpej 
    567  1.13  thorpej 		if (have_unit == 0) {
    568  1.13  thorpej 			unit = i;
    569  1.13  thorpej 			have_unit = 1;
    570  1.13  thorpej 		} else if (have_ucount == 0) {
    571  1.13  thorpej 			ucount = i;
    572  1.13  thorpej 			have_ucount = 1;
    573  1.13  thorpej 		} else {
    574  1.13  thorpej 			warnx("%s: malformed command line", cname);
    575  1.13  thorpej 			usage();
    576  1.13  thorpej 		}
    577   1.1  thorpej 	}
    578   1.1  thorpej 
    579   1.1  thorpej 	for (chet = schet; chet <= echet; ++chet) {
    580   1.1  thorpej 		switch (chet) {
    581   1.1  thorpej 		case CHET_MT:
    582   1.1  thorpej 			count = data.cp_npickers;
    583   1.1  thorpej 			break;
    584   1.1  thorpej 
    585   1.1  thorpej 		case CHET_ST:
    586   1.1  thorpej 			count = data.cp_nslots;
    587   1.1  thorpej 			break;
    588   1.1  thorpej 
    589   1.1  thorpej 		case CHET_IE:
    590   1.1  thorpej 			count = data.cp_nportals;
    591   1.1  thorpej 			break;
    592   1.1  thorpej 
    593   1.1  thorpej 		case CHET_DT:
    594   1.1  thorpej 			count = data.cp_ndrives;
    595   1.1  thorpej 			break;
    596   1.2  thorpej 
    597   1.2  thorpej 		default:
    598   1.2  thorpej 			/* To appease gcc -Wuninitialized. */
    599   1.2  thorpej 			count = 0;
    600   1.1  thorpej 		}
    601   1.1  thorpej 
    602   1.1  thorpej 		if (count == 0) {
    603  1.13  thorpej 			if (schet != echet)
    604   1.1  thorpej 				continue;
    605   1.1  thorpej 			else {
    606   1.6  thorpej 				(void) printf("%s: no %s elements\n",
    607  1.13  thorpej 				    changer_name,
    608  1.13  thorpej 				    elements[chet].et_name);
    609   1.1  thorpej 				return (0);
    610   1.1  thorpej 			}
    611   1.1  thorpej 		}
    612   1.1  thorpej 
    613  1.13  thorpej 		/*
    614  1.13  thorpej 		 * If we have a unit, we may or may not have a count.
    615  1.13  thorpej 		 * If we don't have a unit, we don't have a count, either.
    616  1.13  thorpej 		 *
    617  1.13  thorpej 		 * Make sure both are initialized.
    618  1.13  thorpej 		 */
    619  1.13  thorpej 		if (have_unit) {
    620  1.13  thorpej 			if (have_ucount == 0)
    621  1.13  thorpej 				ucount = 1;
    622  1.13  thorpej 		} else {
    623  1.13  thorpej 			unit = 0;
    624  1.13  thorpej 			ucount = count;
    625  1.13  thorpej 		}
    626  1.13  thorpej 
    627  1.13  thorpej 		if ((unit + ucount) > count)
    628  1.13  thorpej 			errx(1, "%s: unvalid unit/count %d/%d\n",
    629  1.13  thorpej 			    cname, unit, ucount);
    630  1.13  thorpej 
    631  1.13  thorpej 		size = ucount * sizeof(struct changer_element_status);
    632  1.13  thorpej 
    633   1.1  thorpej 		/* Allocate storage for the status bytes. */
    634  1.13  thorpej 		if ((ces = malloc(size)) == NULL)
    635   1.1  thorpej 			errx(1, "can't allocate status storage");
    636   1.1  thorpej 
    637  1.13  thorpej 		(void) memset(ces, 0, size);
    638   1.6  thorpej 		(void) memset(&cmd, 0, sizeof(cmd));
    639   1.1  thorpej 
    640  1.13  thorpej 		cmd.cesr_type = chet;
    641  1.13  thorpej 		cmd.cesr_unit = unit;
    642  1.13  thorpej 		cmd.cesr_count = ucount;
    643  1.13  thorpej 		cmd.cesr_flags = flags;
    644  1.13  thorpej 		cmd.cesr_data = ces;
    645  1.13  thorpej 
    646  1.13  thorpej 		/*
    647  1.13  thorpej 		 * Should we deal with this eventually?
    648  1.13  thorpej 		 */
    649  1.13  thorpej 		cmd.cesr_vendor_data = NULL;
    650   1.1  thorpej 
    651   1.6  thorpej 		if (ioctl(changer_fd, CHIOGSTATUS, &cmd)) {
    652  1.13  thorpej 			free(ces);
    653   1.1  thorpej 			err(1, "%s: CHIOGSTATUS", changer_name);
    654   1.1  thorpej 		}
    655   1.1  thorpej 
    656   1.1  thorpej 		/* Dump the status for each element of this type. */
    657  1.13  thorpej 		for (i = 0; i < ucount; i++) {
    658  1.13  thorpej 			(void) printf("%s %d: ", elements[chet].et_name,
    659  1.13  thorpej 			    unit + i);
    660  1.13  thorpej 			if ((ces[i].ces_flags & CESTATUS_STATUS_VALID) == 0) {
    661  1.13  thorpej 				(void) printf("status not available\n");
    662  1.13  thorpej 				continue;
    663  1.13  thorpej 			}
    664  1.13  thorpej 			(void) printf("%s", bits_to_string(ces[i].ces_flags,
    665  1.13  thorpej 			    CESTATUS_BITS));
    666  1.13  thorpej 			if (ces[i].ces_flags & CESTATUS_XNAME_VALID)
    667  1.13  thorpej 				(void) printf(" (%s)", ces[i].ces_xname);
    668  1.13  thorpej 			(void) printf("\n");
    669  1.13  thorpej 			if (ces[i].ces_flags & CESTATUS_PVOL_VALID)
    670  1.13  thorpej 				(void) printf("\tPrimary volume tag: %s "
    671  1.13  thorpej 				    "ver. %d\n",
    672  1.13  thorpej 				    ces[i].ces_pvoltag.cv_tag,
    673  1.13  thorpej 				    ces[i].ces_pvoltag.cv_serial);
    674  1.13  thorpej 			if (ces[i].ces_flags & CESTATUS_AVOL_VALID)
    675  1.13  thorpej 				(void) printf("\tAlternate volume tag: %s "
    676  1.13  thorpej 				    "ver. %d\n",
    677  1.13  thorpej 				    ces[i].ces_avoltag.cv_tag,
    678  1.13  thorpej 				    ces[i].ces_avoltag.cv_serial);
    679  1.13  thorpej 			if (ces[i].ces_flags & CESTATUS_FROM_VALID)
    680  1.13  thorpej 				(void) printf("\tFrom: %s %d\n",
    681  1.13  thorpej 				    elements[ces[i].ces_from_type].et_name,
    682  1.13  thorpej 				    ces[i].ces_from_unit);
    683  1.14  thorpej 			if (ces[i].ces_vendor_len)
    684  1.14  thorpej 				(void) printf("\tVendor-specific data size: "
    685  1.14  thorpej 				    "%lu\n", (u_long)ces[i].ces_vendor_len);
    686   1.1  thorpej 		}
    687  1.13  thorpej 		free(ces);
    688   1.1  thorpej 	}
    689   1.1  thorpej 
    690   1.1  thorpej 	return (0);
    691   1.1  thorpej }
    692   1.1  thorpej 
    693   1.6  thorpej /* ARGSUSED */
    694   1.1  thorpej static int
    695   1.5   mjacob do_ielem(cname, argc, argv)
    696  1.13  thorpej 	const char *cname;
    697   1.5   mjacob 	int argc;
    698   1.5   mjacob 	char **argv;
    699   1.5   mjacob {
    700   1.6  thorpej 	if (ioctl(changer_fd, CHIOIELEM, NULL))
    701   1.5   mjacob 		err(1, "%s: CHIOIELEM", changer_name);
    702   1.5   mjacob 
    703   1.5   mjacob 	return (0);
    704   1.5   mjacob }
    705   1.5   mjacob 
    706  1.13  thorpej /* ARGSUSED */
    707   1.7  hpeyerl static int
    708   1.7  hpeyerl do_cdlu(cname, argc, argv)
    709  1.13  thorpej 	const char *cname;
    710   1.7  hpeyerl 	int argc;
    711   1.7  hpeyerl 	char **argv;
    712   1.7  hpeyerl {
    713   1.7  hpeyerl 	struct ioc_load_unload cmd;
    714   1.7  hpeyerl 	int i;
    715   1.7  hpeyerl 	static const struct special_word cdlu_subcmds[] = {
    716   1.7  hpeyerl 		{ "load",	CD_LU_LOAD },
    717   1.7  hpeyerl 		{ "unload",	CD_LU_UNLOAD },
    718   1.7  hpeyerl 		{ "abort",	CD_LU_ABORT },
    719   1.7  hpeyerl 		{ NULL,		0 },
    720   1.7  hpeyerl 	};
    721   1.7  hpeyerl 
    722   1.7  hpeyerl 	/*
    723   1.7  hpeyerl 	 * This command is a little different, since we are mostly dealing
    724   1.7  hpeyerl 	 * with ATAPI CD changers, which have a lame API (since ATAPI doesn't
    725   1.7  hpeyerl 	 * have LUNs).
    726   1.7  hpeyerl 	 *
    727   1.7  hpeyerl 	 * We have 3 sub-commands: "load", "unload", and "abort".  The
    728   1.7  hpeyerl 	 * first two take a slot number.  The latter does not.
    729   1.7  hpeyerl 	 */
    730   1.7  hpeyerl 
    731   1.7  hpeyerl 	if (argc < 1 || argc > 2)
    732  1.11  hubertf 		usage();
    733   1.7  hpeyerl 
    734   1.7  hpeyerl 	for (i = 0; cdlu_subcmds[i].sw_name != NULL; i++) {
    735   1.7  hpeyerl 		if (strcmp(argv[0], cdlu_subcmds[i].sw_name) == 0) {
    736   1.7  hpeyerl 			cmd.options = cdlu_subcmds[i].sw_value;
    737   1.7  hpeyerl 			break;
    738   1.7  hpeyerl 		}
    739   1.7  hpeyerl 	}
    740   1.7  hpeyerl 	if (cdlu_subcmds[i].sw_name == NULL)
    741  1.11  hubertf 		usage();
    742   1.7  hpeyerl 
    743   1.7  hpeyerl 	if (strcmp(argv[0], "abort") == 0)
    744   1.7  hpeyerl 		cmd.slot = 0;
    745   1.7  hpeyerl 	else
    746   1.7  hpeyerl 		cmd.slot = parse_element_unit(argv[1]);
    747   1.7  hpeyerl 
    748   1.7  hpeyerl 	/*
    749   1.7  hpeyerl 	 * XXX Should maybe do something different with the device
    750   1.7  hpeyerl 	 * XXX handling for cdlu; think about this some more.
    751   1.7  hpeyerl 	 */
    752   1.7  hpeyerl 	if (ioctl(changer_fd, CDIOCLOADUNLOAD, &cmd))
    753   1.7  hpeyerl 		err(1, "%s: CDIOCLOADUNLOAD", changer_name);
    754   1.7  hpeyerl 
    755   1.7  hpeyerl 	return (0);
    756   1.7  hpeyerl }
    757   1.5   mjacob 
    758   1.5   mjacob static int
    759   1.1  thorpej parse_element_type(cp)
    760  1.13  thorpej 	const char *cp;
    761   1.1  thorpej {
    762   1.1  thorpej 	int i;
    763   1.1  thorpej 
    764   1.1  thorpej 	for (i = 0; elements[i].et_name != NULL; ++i)
    765   1.1  thorpej 		if (strcmp(elements[i].et_name, cp) == 0)
    766   1.1  thorpej 			return (elements[i].et_type);
    767   1.1  thorpej 
    768   1.1  thorpej 	errx(1, "invalid element type `%s'", cp);
    769   1.6  thorpej 	/* NOTREACHED */
    770   1.1  thorpej }
    771   1.1  thorpej 
    772   1.1  thorpej static int
    773   1.1  thorpej parse_element_unit(cp)
    774  1.13  thorpej 	const char *cp;
    775   1.1  thorpej {
    776   1.1  thorpej 	int i;
    777   1.1  thorpej 	char *p;
    778   1.1  thorpej 
    779   1.1  thorpej 	i = (int)strtol(cp, &p, 10);
    780   1.1  thorpej 	if ((i < 0) || (*p != '\0'))
    781   1.1  thorpej 		errx(1, "invalid unit number `%s'", cp);
    782   1.1  thorpej 
    783   1.1  thorpej 	return (i);
    784   1.1  thorpej }
    785   1.1  thorpej 
    786   1.1  thorpej static int
    787   1.1  thorpej parse_special(cp)
    788  1.13  thorpej 	const char *cp;
    789   1.1  thorpej {
    790   1.1  thorpej 	int val;
    791   1.1  thorpej 
    792   1.1  thorpej 	val = is_special(cp);
    793   1.1  thorpej 	if (val)
    794   1.1  thorpej 		return (val);
    795   1.1  thorpej 
    796   1.1  thorpej 	errx(1, "invalid modifier `%s'", cp);
    797   1.6  thorpej 	/* NOTREACHED */
    798   1.1  thorpej }
    799   1.1  thorpej 
    800   1.1  thorpej static int
    801   1.1  thorpej is_special(cp)
    802  1.13  thorpej 	const char *cp;
    803   1.1  thorpej {
    804   1.1  thorpej 	int i;
    805   1.1  thorpej 
    806   1.1  thorpej 	for (i = 0; specials[i].sw_name != NULL; ++i)
    807   1.1  thorpej 		if (strcmp(specials[i].sw_name, cp) == 0)
    808   1.1  thorpej 			return (specials[i].sw_value);
    809   1.1  thorpej 
    810   1.1  thorpej 	return (0);
    811   1.1  thorpej }
    812   1.1  thorpej 
    813   1.8  mycroft static const char *
    814   1.1  thorpej bits_to_string(v, cp)
    815   1.1  thorpej 	int v;
    816   1.1  thorpej 	const char *cp;
    817   1.1  thorpej {
    818   1.1  thorpej 	const char *np;
    819   1.8  mycroft 	char f, *bp;
    820   1.8  mycroft 	int first;
    821   1.1  thorpej 	static char buf[128];
    822   1.1  thorpej 
    823   1.1  thorpej 	bp = buf;
    824   1.8  mycroft 	*bp++ = '<';
    825   1.8  mycroft 	for (first = 1; (f = *cp++) != 0; cp = np) {
    826   1.1  thorpej 		for (np = cp; *np >= ' ';)
    827   1.1  thorpej 			np++;
    828   1.1  thorpej 		if ((v & (1 << (f - 1))) == 0)
    829   1.1  thorpej 			continue;
    830   1.8  mycroft 		if (first)
    831   1.8  mycroft 			first = 0;
    832   1.8  mycroft 		else
    833   1.8  mycroft 			*bp++ = ',';
    834   1.9  mycroft 		(void) memcpy(bp, cp, np - cp);
    835   1.8  mycroft 		bp += np - cp;
    836   1.1  thorpej 	}
    837   1.8  mycroft 	*bp++ = '>';
    838   1.8  mycroft 	*bp = '\0';
    839   1.1  thorpej 
    840   1.1  thorpej 	return (buf);
    841   1.1  thorpej }
    842   1.1  thorpej 
    843   1.1  thorpej static void
    844   1.1  thorpej cleanup()
    845   1.1  thorpej {
    846   1.1  thorpej 	/* Simple enough... */
    847   1.1  thorpej 	(void)close(changer_fd);
    848   1.1  thorpej }
    849   1.1  thorpej 
    850   1.1  thorpej static void
    851   1.1  thorpej usage()
    852   1.1  thorpej {
    853  1.11  hubertf 	int i;
    854   1.1  thorpej 
    855  1.15      cgd 	(void) fprintf(stderr, "Usage: %s command arg1 arg2 ...\n",
    856  1.15      cgd 	    getprogname());
    857  1.11  hubertf 
    858  1.11  hubertf 	(void) fprintf(stderr, "Where command (and args) are:\n");
    859  1.13  thorpej 	for (i = 0; commands[i].cc_name != NULL; i++)
    860  1.11  hubertf 		(void) fprintf(stderr, "\t%s%s\n", commands[i].cc_name,
    861  1.13  thorpej 		    commands[i].cc_args);
    862   1.1  thorpej 	exit(1);
    863  1.10  mycroft 	/* NOTREACHED */
    864   1.1  thorpej }
    865