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