Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/* $NetBSD: chio.c,v 1.33 2017/10/16 17:08:35 jnemeth Exp $ */

/*-
 * Copyright (c) 1996, 1998, 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
 * NASA Ames Research Center.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Additional Copyright (c) 1997, by Matthew Jacob, for NASA/Ames Research Ctr.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"@(#) Copyright (c) 1996, 1998, 1999\
 The NetBSD Foundation, Inc.  All rights reserved.");
__RCSID("$NetBSD: chio.c,v 1.33 2017/10/16 17:08:35 jnemeth Exp $");
#endif

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/chio.h>
#include <sys/cdio.h>	/* for ATAPI CD changer; too bad it uses a lame API */

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "defs.h"
#include "pathnames.h"

__dead static void usage(void);
static void cleanup(void);
static int parse_element_type(const char *);
static int parse_element_unit(const char *);
static int parse_special(const char *);
static int is_special(const char *);
static const char *bits_to_string(int, const char *);

static int do_move(const char *, int, char **);
static int do_exchange(const char *, int, char **);
static int do_position(const char *, int, char **);
static int do_params(const char *, int, char **);
static int do_getpicker(const char *, int, char **);
static int do_setpicker(const char *, int, char **);
static int do_status(const char *, int, char **);
static int do_ielem(const char *, int, char **);
static int do_cdlu(const char *, int, char **);

/* Valid changer element types. */
const struct element_type elements[] = {
	{ "picker",		CHET_MT },
	{ "slot",		CHET_ST },
	{ "portal",		CHET_IE },
	{ "drive",		CHET_DT },
	{ NULL,			0 },
};

/* Valid commands. */
const struct changer_command commands[] = {
	{ "move", 	" <from ET> <from EU> <to ET> <to EU> [inv]",
	  do_move },

	{ "exchange",	" <src ET> <src EU> <dst1 ET> <dst1 EU>\n"
			"\t\t [<dst2 ET> <dst2 EU>] [inv1] [inv2]",
	  do_exchange },

	{ "position",	" <to ET> <to EU> [inv]", do_position },

	{ "params",	"",
	  do_params },

	{ "getpicker",	"",
	  do_getpicker },

	{ "setpicker",	" <picker>",
	  do_setpicker },

	{ "status",	" [<ET> [unit [count]]] [voltags]",
	  do_status },

	{ "ielem", 	"",
	  do_ielem },

	{ "cdlu",	" load|unload <slot>\n"
			"\t     abort",
	  do_cdlu },

	{ NULL,		NULL,
	  NULL },
};

/* Valid special words. */
const struct special_word specials[] = {
	{ "inv",		SW_INVERT },
	{ "inv1",		SW_INVERT1 },
	{ "inv2",		SW_INVERT2 },
	{ "voltags",		SW_VOLTAGS },
	{ NULL,			0 },
};

static const char *changer_name;
static int changer_fd;

int
main(int argc, char *argv[])
{
	int ch, i;

	setprogname(argv[0]);
	while ((ch = getopt(argc, argv, "f:")) != -1) {
		switch (ch) {
		case 'f':
			changer_name = optarg;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 0)
		usage();
		/* NOTREACHED */

	/* Get the default changer if not already specified. */
	if (changer_name == NULL)
		if ((changer_name = getenv(CHANGER_ENV_VAR)) == NULL)
			changer_name = _PATH_CH;

	/* Open the changer device. */
	if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1)
		err(EXIT_FAILURE, "%s: open", changer_name);
		/* NOTREACHED */

	/* Register cleanup function. */
	if (atexit(cleanup))
		err(EXIT_FAILURE, "can't register cleanup function");
		/* NOTREACHED */

	/* Find the specified command. */
	for (i = 0; commands[i].cc_name != NULL; ++i)
		if (strcmp(*argv, commands[i].cc_name) == 0)
			break;
	if (commands[i].cc_name == NULL)
		errx(EXIT_FAILURE, "unknown command: %s", *argv);
		/* NOTREACHED */

	/* Skip over the command name and call handler. */
	++argv; --argc;
	exit((*commands[i].cc_handler)(commands[i].cc_name, argc, argv));
	/* NOTREACHED */
}

static int
do_move(const char *cname, int argc, char **argv)
{
	struct changer_move_request cmd;
	int val;

	/*
	 * On a move command, we expect the following:
	 *
	 * <from ET> <from EU> <to ET> <to EU> [inv]
	 *
	 * where ET == element type and EU == element unit.
	 */
	if (argc < 4) {
		warnx("%s: too few arguments", cname);
		usage();
		/*NOTREACHED*/
	} else if (argc > 5) {
		warnx("%s: too many arguments", cname);
		usage();
		/*NOTREACHED*/
	}
	(void)memset(&cmd, 0, sizeof(cmd));

	/* <from ET>  */
	cmd.cm_fromtype = parse_element_type(*argv);
	++argv; --argc;

	/* <from EU> */
	cmd.cm_fromunit = parse_element_unit(*argv);
	++argv; --argc;

	/* <to ET> */
	cmd.cm_totype = parse_element_type(*argv);
	++argv; --argc;

	/* <to EU> */
	cmd.cm_tounit = parse_element_unit(*argv);
	++argv; --argc;

	/* Deal with optional command modifier. */
	if (argc) {
		val = parse_special(*argv);
		switch (val) {
		case SW_INVERT:
			cmd.cm_flags |= CM_INVERT;
			break;
		default:
			errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
			    cname, *argv);
			/* NOTREACHED */
		}
	}

	/* Send command to changer. */
	if (ioctl(changer_fd, CHIOMOVE, &cmd))
		err(EXIT_FAILURE, "%s: CHIOMOVE", changer_name);
		/* NOTREACHED */

	return (0);
}

static int
do_exchange(const char *cname, int argc, char **argv)
{
	struct changer_exchange_request cmd;
	int val;

	/*
	 * On an exchange command, we expect the following:
	 *
  * <src ET> <src EU> <dst1 ET> <dst1 EU> [<dst2 ET> <dst2 EU>] [inv1] [inv2]
	 *
	 * where ET == element type and EU == element unit.
	 */
	if (argc < 4) {
		warnx("%s: too few arguments", cname);
		usage();
		/*NOTREACHED*/
	} else if (argc > 8) {
		warnx("%s: too many arguments", cname);
		usage();
		/*NOTREACHED*/
	}
	(void)memset(&cmd, 0, sizeof(cmd));

	/* <src ET>  */
	cmd.ce_srctype = parse_element_type(*argv);
	++argv; --argc;

	/* <src EU> */
	cmd.ce_srcunit = parse_element_unit(*argv);
	++argv; --argc;

	/* <dst1 ET> */
	cmd.ce_fdsttype = parse_element_type(*argv);
	++argv; --argc;

	/* <dst1 EU> */
	cmd.ce_fdstunit = parse_element_unit(*argv);
	++argv; --argc;

	/*
	 * If the next token is a special word or there are no more
	 * arguments, then this is a case of simple exchange.
	 * dst2 == src.
	 */
	if ((argc == 0) || is_special(*argv)) {
		cmd.ce_sdsttype = cmd.ce_srctype;
		cmd.ce_sdstunit = cmd.ce_srcunit;
		goto do_special;
	}

	/* <dst2 ET> */
	cmd.ce_sdsttype = parse_element_type(*argv);
	++argv; --argc;

	/* <dst2 EU> */
	cmd.ce_sdstunit = parse_element_unit(*argv);
	++argv; --argc;

 do_special:
	/* Deal with optional command modifiers. */
	while (argc) {
		val = parse_special(*argv);
		++argv; --argc;
		switch (val) {
		case SW_INVERT1:
			cmd.ce_flags |= CE_INVERT1;
			break;
		case SW_INVERT2:
			cmd.ce_flags |= CE_INVERT2;
			break;
		default:
			errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
			    cname, *argv);
			/* NOTREACHED */
		}
	}

	/* Send command to changer. */
	if (ioctl(changer_fd, CHIOEXCHANGE, &cmd))
		err(EXIT_FAILURE, "%s: CHIOEXCHANGE", changer_name);
		/* NOTREACHED */

	return (0);
}

static int
do_position(const char *cname, int argc, char **argv)
{
	struct changer_position_request cmd;
	int val;

	/*
	 * On a position command, we expect the following:
	 *
	 * <to ET> <to EU> [inv]
	 *
	 * where ET == element type and EU == element unit.
	 */
	if (argc < 2) {
		warnx("%s: too few arguments", cname);
		usage();
		/*NOTREACHED*/
	} else if (argc > 3) {
		warnx("%s: too many arguments", cname);
		usage();
		/*NOTREACHED*/
	}
	(void)memset(&cmd, 0, sizeof(cmd));

	/* <to ET>  */
	cmd.cp_type = parse_element_type(*argv);
	++argv; --argc;

	/* <to EU> */
	cmd.cp_unit = parse_element_unit(*argv);
	++argv; --argc;

	/* Deal with optional command modifier. */
	if (argc) {
		val = parse_special(*argv);
		switch (val) {
		case SW_INVERT:
			cmd.cp_flags |= CP_INVERT;
			break;
		default:
			errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
			    cname, *argv);
			/* NOTREACHED */
		}
	}

	/* Send command to changer. */
	if (ioctl(changer_fd, CHIOPOSITION, &cmd))
		err(EXIT_FAILURE, "%s: CHIOPOSITION", changer_name);
		/* NOTREACHED */

	return (0);
}

/* ARGSUSED */
static int
do_params(const char *cname, int argc, char **argv)
{
	struct changer_params data;

	/* No arguments to this command. */
	if (argc) {
		warnx("%s: no arguments expected", cname);
		usage();
		/* NOTREACHED */
	}
	
	/* Get params from changer and display them. */
	(void)memset(&data, 0, sizeof(data));
	if (ioctl(changer_fd, CHIOGPARAMS, &data))
		err(EXIT_FAILURE, "%s: CHIOGPARAMS", changer_name);
		/* NOTREACHED */

#define	PLURAL(n)	(n) > 1 ? "s" : ""

	(void)printf("%s: %d slot%s, %d drive%s, %d picker%s",
	    changer_name,
	    data.cp_nslots, PLURAL(data.cp_nslots),
	    data.cp_ndrives, PLURAL(data.cp_ndrives),
	    data.cp_npickers, PLURAL(data.cp_npickers));
	if (data.cp_nportals)
		(void)printf(", %d portal%s", data.cp_nportals,
		    PLURAL(data.cp_nportals));

#undef PLURAL

	(void)printf("\n%s: current picker: %d\n", changer_name, data.cp_curpicker);

	return (0);
}

/* ARGSUSED */
static int
do_getpicker(const char *cname, int argc, char **argv)
{
	int picker;

	/* No arguments to this command. */
	if (argc) {
		warnx("%s: no arguments expected", cname);
		usage();
		/*NOTREACHED*/
	}

	/* Get current picker from changer and display it. */
	if (ioctl(changer_fd, CHIOGPICKER, &picker))
		err(EXIT_FAILURE, "%s: CHIOGPICKER", changer_name);
		/* NOTREACHED */

	(void)printf("%s: current picker: %d\n", changer_name, picker);

	return (0);
}

static int
do_setpicker(const char *cname, int argc, char **argv)
{
	int picker;

	if (argc < 1) {
		warnx("%s: too few arguments", cname);
		usage();
		/*NOTREACHED*/
	} else if (argc > 1) {
		warnx("%s: too many arguments", cname);
		usage();
		/*NOTREACHED*/
	}

	picker = parse_element_unit(*argv);

	/* Set the changer picker. */
	if (ioctl(changer_fd, CHIOSPICKER, &picker))
		err(EXIT_FAILURE, "%s: CHIOSPICKER", changer_name);

	return (0);
}

static int
do_status(const char *cname, int argc, char **argv)
{
	struct changer_element_status_request cmd;
	struct changer_params data;
	struct changer_element_status *ces;
	int i, chet, count, echet, flags, have_ucount, have_unit;
	int schet, ucount, unit;
	size_t size;

	flags = 0;
	ucount = 0;
	unit = 0;
	have_ucount = 0;
	have_unit = 0;

	/*
	 * On a status command, we expect the following:
	 *
	 * [<ET> [unit [count]]] [voltags]
	 *
	 * where ET == element type.
	 *
	 * If we get no element-related arguments, we get the status of all
	 * known element types.
	 */
	if (argc > 4) {
		warnx("%s: too many arguments", cname);
		usage();
		/*NOTREACHED*/
	}

	/*
	 * Get params from changer.  Specifically, we need the element
	 * counts.
	 */
	(void)memset(&data, 0, sizeof(data));
	if (ioctl(changer_fd, CHIOGPARAMS, &data))
		err(EXIT_FAILURE, "%s: CHIOGPARAMS", changer_name);
		/* NOTREACHED */

	schet = CHET_MT;
	echet = CHET_DT;

	for (; argc != 0; argc--, argv++) {
		/*
		 * If we have the voltags modifier, it must be the
		 * last argument.
		 */
		if (is_special(argv[0])) {
			if (argc != 1) {
				warnx("%s: malformed command line", cname);
				usage();
				/*NOTREACHED*/
			}
			if (parse_special(argv[0]) != SW_VOLTAGS)
				errx(EXIT_FAILURE,
				    "%s: inappropriate special word: %s",
				    cname, argv[0]);
				/* NOTREACHED */
			flags |= CESR_VOLTAGS;
			continue;
		}

		/*
		 * If we get an element type, we can't have specified
		 * anything else.
		 */
		if (isdigit((unsigned char)*argv[0]) == 0) {
			if (schet == echet || flags != 0 || have_unit ||
			    have_ucount) {
				warnx("%s: malformed command line", cname);
				usage();
				/*NOTREACHED*/
			}
			schet = echet = parse_element_type(argv[0]);
			continue;
		}

		/*
		 * We know we have a digit here.  If we do, we must
		 * have specified an element type.
		 */
		if (schet != echet) {
			warnx("%s: malformed command line", cname);
			usage();
			/*NOTREACHED*/
		}

		i = parse_element_unit(argv[0]);

		if (have_unit == 0) {
			unit = i;
			have_unit = 1;
		} else if (have_ucount == 0) {
			ucount = i;
			have_ucount = 1;
		} else {
			warnx("%s: malformed command line", cname);
			usage();
			/*NOTREACHED*/
		}
	}

	for (chet = schet; chet <= echet; ++chet) {
		switch (chet) {
		case CHET_MT:
			count = data.cp_npickers;
			break;
		case CHET_ST:
			count = data.cp_nslots;
			break;
		case CHET_IE:
			count = data.cp_nportals;
			break;
		case CHET_DT:
			count = data.cp_ndrives;
			break;
		default:
			/* To appease gcc -Wuninitialized. */
			count = 0;
		}

		if (count == 0) {
			if (schet != echet)
				continue;
			else {
				(void)printf("%s: no %s elements\n",
				    changer_name,
				    elements[chet].et_name);
				return (0);
			}
		}

		/*
		 * If we have a unit, we may or may not have a count.
		 * If we don't have a unit, we don't have a count, either.
		 *
		 * Make sure both are initialized.
		 */
		if (have_unit) {
			if (have_ucount == 0)
				ucount = 1;
		} else {
			unit = 0;
			ucount = count;
		}

		if ((unit + ucount) > count)
			errx(EXIT_FAILURE, "%s: invalid unit/count %d/%d",
			    cname, unit, ucount);
			/* NOTREACHED */

		size = ucount * sizeof(struct changer_element_status);

		/* Allocate storage for the status bytes. */
		if ((ces = malloc(size)) == NULL)
			errx(EXIT_FAILURE, "can't allocate status storage");
			/* NOTREACHED */

		(void)memset(ces, 0, size);
		(void)memset(&cmd, 0, sizeof(cmd));

		cmd.cesr_type = chet;
		cmd.cesr_unit = unit;
		cmd.cesr_count = ucount;
		cmd.cesr_flags = flags;
		cmd.cesr_data = ces;

		/*
		 * Should we deal with this eventually?
		 */
		cmd.cesr_vendor_data = NULL;

		if (ioctl(changer_fd, CHIOGSTATUS, &cmd)) {
			free(ces);
			err(EXIT_FAILURE, "%s: CHIOGSTATUS", changer_name);
			/* NOTREACHED */
		}

		/* Dump the status for each element of this type. */
		for (i = 0; i < ucount; i++) {
			(void)printf("%s %d: ", elements[chet].et_name,
			    unit + i);
			if ((ces[i].ces_flags & CESTATUS_STATUS_VALID) == 0) {
				(void)printf("status not available\n");
				continue;
			}
			(void)printf("%s", bits_to_string(ces[i].ces_flags,
			    CESTATUS_BITS));
			if (ces[i].ces_flags & CESTATUS_XNAME_VALID)
				(void)printf(" (%s)", ces[i].ces_xname);
			(void)printf("\n");
			if (ces[i].ces_flags & CESTATUS_PVOL_VALID)
				(void)printf("\tPrimary volume tag: %s "
				    "ver. %d\n",
				    ces[i].ces_pvoltag.cv_tag,
				    ces[i].ces_pvoltag.cv_serial);
			if (ces[i].ces_flags & CESTATUS_AVOL_VALID)
				(void)printf("\tAlternate volume tag: %s "
				    "ver. %d\n",
				    ces[i].ces_avoltag.cv_tag,
				    ces[i].ces_avoltag.cv_serial);
			if (ces[i].ces_flags & CESTATUS_FROM_VALID)
				(void)printf("\tFrom: %s %d\n",
				    elements[ces[i].ces_from_type].et_name,
				    ces[i].ces_from_unit);
			if (ces[i].ces_vendor_len)
				(void)printf("\tVendor-specific data size: "
				    "%lu\n", (u_long)ces[i].ces_vendor_len);
		}
		free(ces);
	}

	return (0);
}

/* ARGSUSED */
static int
do_ielem(const char *cname, int argc, char **argv)
{

	if (ioctl(changer_fd, CHIOIELEM, NULL))
		err(EXIT_FAILURE, "%s: CHIOIELEM", changer_name);
		/* NOTREACHED */

	return (0);
}

/* ARGSUSED */
static int
do_cdlu(const char *cname, int argc, char **argv)
{
	static const struct special_word cdlu_subcmds[] = {
		{ "load",	CD_LU_LOAD },
		{ "unload",	CD_LU_UNLOAD },
		{ "abort",	CD_LU_ABORT },
		{ NULL,		0 },
	};
	struct ioc_load_unload cmd;
	int i;

	/*
	 * This command is a little different, since we are mostly dealing
	 * with ATAPI CD changers, which have a lame API (since ATAPI doesn't
	 * have LUNs).
	 *
	 * We have 3 sub-commands: "load", "unload", and "abort".  The
	 * first two take a slot number.  The latter does not.
	 */

	if (argc < 1 || argc > 2)
		usage();
		/*NOTREACHED*/

	for (i = 0; cdlu_subcmds[i].sw_name != NULL; i++) {
		if (strcmp(argv[0], cdlu_subcmds[i].sw_name) == 0) {
			cmd.options = cdlu_subcmds[i].sw_value;
			break;
		}
	}
	if (cdlu_subcmds[i].sw_name == NULL)
		usage();
		/*NOTREACHED*/

	if (strcmp(argv[0], "abort") == 0)
		cmd.slot = 0;
	else
		cmd.slot = parse_element_unit(argv[1]);

	/*
	 * XXX Should maybe do something different with the device
	 * XXX handling for cdlu; think about this some more.
	 */
	if (ioctl(changer_fd, CDIOCLOADUNLOAD, &cmd))
		err(EXIT_FAILURE, "%s: CDIOCLOADUNLOAD", changer_name);
		/* NOTREACHED */

	return (0);
}

static int
parse_element_type(const char *cp)
{
	int i;

	for (i = 0; elements[i].et_name != NULL; ++i)
		if (strcmp(elements[i].et_name, cp) == 0)
			return (elements[i].et_type);

	errx(EXIT_FAILURE, "invalid element type `%s'", cp);
	/* NOTREACHED */
}

static int
parse_element_unit(const char *cp)
{
	char *p;
	int i;

	i = (int)strtol(cp, &p, 10);
	if ((i < 0) || (*p != '\0'))
		errx(EXIT_FAILURE, "invalid unit number `%s'", cp);

	return (i);
}

static int
parse_special(const char *cp)
{
	int val;

	val = is_special(cp);
	if (val)
		return (val);

	errx(EXIT_FAILURE, "invalid modifier `%s'", cp);
	/* NOTREACHED */
}

static int
is_special(const char *cp)
{
	int i;

	for (i = 0; specials[i].sw_name != NULL; ++i)
		if (strcmp(specials[i].sw_name, cp) == 0)
			return (specials[i].sw_value);

	return (0);
}

static const char *
bits_to_string(int v, const char *cp)
{
	static char buf[128];
	const char *np;
	char *bp, f;
	int first;

	bp = buf;
	*bp++ = '<';
	for (first = 1; (f = *cp++) != 0; cp = np) {
		for (np = cp; *np >= ' ';)
			np++;
		if ((v & (1 << (f - 1))) == 0)
			continue;
		if (first)
			first = 0;
		else
			*bp++ = ',';
		(void)memcpy(bp, cp, np - cp);
		bp += np - cp;
	}
	*bp++ = '>';
	*bp = '\0';

	return (buf);
}

static void
cleanup(void)
{

	/* Simple enough... */
	(void)close(changer_fd);
}

static void
usage(void)
{
	int i;

	(void)fprintf(stderr,
	    "usage: %s [-f changer] command arg1 arg2 [arg3 [...]]\n",
	    getprogname());

	(void)fprintf(stderr, "Where command (and args) are:\n");
	for (i = 0; commands[i].cc_name != NULL; i++)
		(void)fprintf(stderr, "\t%s%s\n", commands[i].cc_name,
		    commands[i].cc_args);
	exit(1);
	/* NOTREACHED */
}