chio.c revision 1.25 1 1.25 wiz /* $NetBSD: chio.c,v 1.25 2004/06/25 14:27:57 wiz 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.25 wiz __RCSID("$NetBSD: chio.c,v 1.25 2004/06/25 14:27:57 wiz 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.18 enami #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.16 wiz
57 1.13 thorpej #include <ctype.h>
58 1.1 thorpej #include <err.h>
59 1.1 thorpej #include <errno.h>
60 1.1 thorpej #include <fcntl.h>
61 1.1 thorpej #include <limits.h>
62 1.1 thorpej #include <stdio.h>
63 1.1 thorpej #include <stdlib.h>
64 1.1 thorpej #include <string.h>
65 1.1 thorpej #include <unistd.h>
66 1.1 thorpej
67 1.1 thorpej #include "defs.h"
68 1.1 thorpej #include "pathnames.h"
69 1.1 thorpej
70 1.16 wiz int main(int, char *[]);
71 1.16 wiz static void usage(void);
72 1.16 wiz static void cleanup(void);
73 1.16 wiz static int parse_element_type(const char *);
74 1.16 wiz static int parse_element_unit(const char *);
75 1.16 wiz static int parse_special(const char *);
76 1.16 wiz static int is_special(const char *);
77 1.16 wiz static const char *bits_to_string(int, const char *);
78 1.16 wiz
79 1.16 wiz static int do_move(const char *, int, char **);
80 1.16 wiz static int do_exchange(const char *, int, char **);
81 1.16 wiz static int do_position(const char *, int, char **);
82 1.16 wiz static int do_params(const char *, int, char **);
83 1.16 wiz static int do_getpicker(const char *, int, char **);
84 1.16 wiz static int do_setpicker(const char *, int, char **);
85 1.16 wiz static int do_status(const char *, int, char **);
86 1.16 wiz static int do_ielem(const char *, int, char **);
87 1.16 wiz static int do_cdlu(const char *, int, char **);
88 1.1 thorpej
89 1.1 thorpej /* Valid changer element types. */
90 1.1 thorpej const struct element_type elements[] = {
91 1.1 thorpej { "picker", CHET_MT },
92 1.1 thorpej { "slot", CHET_ST },
93 1.1 thorpej { "portal", CHET_IE },
94 1.1 thorpej { "drive", CHET_DT },
95 1.1 thorpej { NULL, 0 },
96 1.1 thorpej };
97 1.1 thorpej
98 1.1 thorpej /* Valid commands. */
99 1.1 thorpej const struct changer_command commands[] = {
100 1.12 thorpej { "move", " <from ET> <from EU> <to ET> <to EU> [inv]",
101 1.12 thorpej do_move },
102 1.12 thorpej
103 1.11 hubertf { "exchange", " <src ET> <src EU> <dst1 ET> <dst1 EU>\n"
104 1.18 enami "\t\t [<dst2 ET> <dst2 EU>] [inv1] [inv2]",
105 1.12 thorpej do_exchange },
106 1.12 thorpej
107 1.12 thorpej { "position", " <to ET> <to EU> [inv]", do_position },
108 1.12 thorpej
109 1.12 thorpej { "params", "",
110 1.12 thorpej do_params },
111 1.12 thorpej
112 1.12 thorpej { "getpicker", "",
113 1.12 thorpej do_getpicker },
114 1.12 thorpej
115 1.12 thorpej { "setpicker", " <picker>",
116 1.12 thorpej do_setpicker },
117 1.12 thorpej
118 1.13 thorpej { "status", " [<ET> [unit [count]]] [voltags]",
119 1.12 thorpej do_status },
120 1.12 thorpej
121 1.12 thorpej { "ielem", "",
122 1.12 thorpej do_ielem },
123 1.12 thorpej
124 1.11 hubertf { "cdlu", " load|unload <slot>\n"
125 1.18 enami "\t abort",
126 1.12 thorpej do_cdlu },
127 1.12 thorpej
128 1.12 thorpej { NULL, NULL,
129 1.12 thorpej NULL },
130 1.1 thorpej };
131 1.1 thorpej
132 1.1 thorpej /* Valid special words. */
133 1.1 thorpej const struct special_word specials[] = {
134 1.1 thorpej { "inv", SW_INVERT },
135 1.1 thorpej { "inv1", SW_INVERT1 },
136 1.1 thorpej { "inv2", SW_INVERT2 },
137 1.13 thorpej { "voltags", SW_VOLTAGS },
138 1.1 thorpej { NULL, 0 },
139 1.1 thorpej };
140 1.1 thorpej
141 1.16 wiz static const char *changer_name;
142 1.16 wiz static int changer_fd;
143 1.1 thorpej
144 1.1 thorpej int
145 1.16 wiz main(int argc, char *argv[])
146 1.1 thorpej {
147 1.1 thorpej int ch, i;
148 1.1 thorpej
149 1.17 wiz setprogname(argv[0]);
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 default:
156 1.1 thorpej usage();
157 1.20 jschauma /* NOTREACHED */
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.20 jschauma /* NOTREACHED */
166 1.20 jschauma
167 1.1 thorpej /* Get the default changer if not already specified. */
168 1.1 thorpej if (changer_name == NULL)
169 1.1 thorpej if ((changer_name = getenv(CHANGER_ENV_VAR)) == NULL)
170 1.1 thorpej changer_name = _PATH_CH;
171 1.1 thorpej
172 1.1 thorpej /* Open the changer device. */
173 1.1 thorpej if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1)
174 1.22 jschauma err(EXIT_FAILURE, "%s: open", changer_name);
175 1.20 jschauma /* NOTREACHED */
176 1.1 thorpej
177 1.1 thorpej /* Register cleanup function. */
178 1.1 thorpej if (atexit(cleanup))
179 1.20 jschauma err(EXIT_FAILURE, "can't register cleanup function");
180 1.20 jschauma /* NOTREACHED */
181 1.1 thorpej
182 1.1 thorpej /* Find the specified command. */
183 1.1 thorpej for (i = 0; commands[i].cc_name != NULL; ++i)
184 1.1 thorpej if (strcmp(*argv, commands[i].cc_name) == 0)
185 1.1 thorpej break;
186 1.1 thorpej if (commands[i].cc_name == NULL)
187 1.20 jschauma errx(EXIT_FAILURE, "unknown command: %s", *argv);
188 1.20 jschauma /* NOTREACHED */
189 1.1 thorpej
190 1.1 thorpej /* Skip over the command name and call handler. */
191 1.1 thorpej ++argv; --argc;
192 1.18 enami exit((*commands[i].cc_handler)(commands[i].cc_name, argc, argv));
193 1.6 thorpej /* NOTREACHED */
194 1.1 thorpej }
195 1.1 thorpej
196 1.1 thorpej static int
197 1.16 wiz do_move(const char *cname, int argc, char **argv)
198 1.1 thorpej {
199 1.13 thorpej struct changer_move_request cmd;
200 1.1 thorpej int val;
201 1.1 thorpej
202 1.1 thorpej /*
203 1.1 thorpej * On a move command, we expect the following:
204 1.1 thorpej *
205 1.1 thorpej * <from ET> <from EU> <to ET> <to EU> [inv]
206 1.1 thorpej *
207 1.1 thorpej * where ET == element type and EU == element unit.
208 1.1 thorpej */
209 1.1 thorpej if (argc < 4) {
210 1.1 thorpej warnx("%s: too few arguments", cname);
211 1.11 hubertf usage();
212 1.20 jschauma /*NOTREACHED*/
213 1.1 thorpej } else if (argc > 5) {
214 1.1 thorpej warnx("%s: too many arguments", cname);
215 1.11 hubertf usage();
216 1.20 jschauma /*NOTREACHED*/
217 1.1 thorpej }
218 1.16 wiz (void)memset(&cmd, 0, sizeof(cmd));
219 1.1 thorpej
220 1.1 thorpej /* <from ET> */
221 1.1 thorpej cmd.cm_fromtype = parse_element_type(*argv);
222 1.1 thorpej ++argv; --argc;
223 1.1 thorpej
224 1.1 thorpej /* <from EU> */
225 1.1 thorpej cmd.cm_fromunit = parse_element_unit(*argv);
226 1.1 thorpej ++argv; --argc;
227 1.1 thorpej
228 1.1 thorpej /* <to ET> */
229 1.1 thorpej cmd.cm_totype = parse_element_type(*argv);
230 1.1 thorpej ++argv; --argc;
231 1.1 thorpej
232 1.1 thorpej /* <to EU> */
233 1.1 thorpej cmd.cm_tounit = parse_element_unit(*argv);
234 1.1 thorpej ++argv; --argc;
235 1.1 thorpej
236 1.1 thorpej /* Deal with optional command modifier. */
237 1.1 thorpej if (argc) {
238 1.1 thorpej val = parse_special(*argv);
239 1.1 thorpej switch (val) {
240 1.1 thorpej case SW_INVERT:
241 1.1 thorpej cmd.cm_flags |= CM_INVERT;
242 1.1 thorpej break;
243 1.1 thorpej default:
244 1.20 jschauma errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
245 1.1 thorpej cname, *argv);
246 1.1 thorpej /* NOTREACHED */
247 1.1 thorpej }
248 1.1 thorpej }
249 1.1 thorpej
250 1.1 thorpej /* Send command to changer. */
251 1.6 thorpej if (ioctl(changer_fd, CHIOMOVE, &cmd))
252 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOMOVE", changer_name);
253 1.20 jschauma /* NOTREACHED */
254 1.1 thorpej
255 1.1 thorpej return (0);
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej static int
259 1.16 wiz do_exchange(const char *cname, int argc, 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.20 jschauma /*NOTREACHED*/
275 1.1 thorpej } else if (argc > 8) {
276 1.1 thorpej warnx("%s: too many arguments", cname);
277 1.11 hubertf usage();
278 1.20 jschauma /*NOTREACHED*/
279 1.1 thorpej }
280 1.16 wiz (void)memset(&cmd, 0, sizeof(cmd));
281 1.1 thorpej
282 1.1 thorpej /* <src ET> */
283 1.1 thorpej cmd.ce_srctype = parse_element_type(*argv);
284 1.1 thorpej ++argv; --argc;
285 1.1 thorpej
286 1.1 thorpej /* <src EU> */
287 1.1 thorpej cmd.ce_srcunit = parse_element_unit(*argv);
288 1.1 thorpej ++argv; --argc;
289 1.1 thorpej
290 1.1 thorpej /* <dst1 ET> */
291 1.1 thorpej cmd.ce_fdsttype = parse_element_type(*argv);
292 1.1 thorpej ++argv; --argc;
293 1.1 thorpej
294 1.1 thorpej /* <dst1 EU> */
295 1.1 thorpej cmd.ce_fdstunit = parse_element_unit(*argv);
296 1.1 thorpej ++argv; --argc;
297 1.1 thorpej
298 1.1 thorpej /*
299 1.1 thorpej * If the next token is a special word or there are no more
300 1.1 thorpej * arguments, then this is a case of simple exchange.
301 1.1 thorpej * dst2 == src.
302 1.1 thorpej */
303 1.1 thorpej if ((argc == 0) || is_special(*argv)) {
304 1.1 thorpej cmd.ce_sdsttype = cmd.ce_srctype;
305 1.1 thorpej cmd.ce_sdstunit = cmd.ce_srcunit;
306 1.1 thorpej goto do_special;
307 1.1 thorpej }
308 1.1 thorpej
309 1.1 thorpej /* <dst2 ET> */
310 1.1 thorpej cmd.ce_sdsttype = parse_element_type(*argv);
311 1.1 thorpej ++argv; --argc;
312 1.1 thorpej
313 1.1 thorpej /* <dst2 EU> */
314 1.1 thorpej cmd.ce_sdstunit = parse_element_unit(*argv);
315 1.1 thorpej ++argv; --argc;
316 1.1 thorpej
317 1.1 thorpej do_special:
318 1.1 thorpej /* Deal with optional command modifiers. */
319 1.1 thorpej while (argc) {
320 1.1 thorpej val = parse_special(*argv);
321 1.1 thorpej ++argv; --argc;
322 1.1 thorpej switch (val) {
323 1.1 thorpej case SW_INVERT1:
324 1.1 thorpej cmd.ce_flags |= CE_INVERT1;
325 1.1 thorpej break;
326 1.1 thorpej case SW_INVERT2:
327 1.1 thorpej cmd.ce_flags |= CE_INVERT2;
328 1.1 thorpej break;
329 1.1 thorpej default:
330 1.20 jschauma errx(EXIT_FAILURE, "%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.22 jschauma err(EXIT_FAILURE, "%s: CHIOEXCHANGE", changer_name);
339 1.20 jschauma /* NOTREACHED */
340 1.1 thorpej
341 1.1 thorpej return (0);
342 1.1 thorpej }
343 1.1 thorpej
344 1.1 thorpej static int
345 1.16 wiz do_position(const char *cname, int argc, char **argv)
346 1.1 thorpej {
347 1.13 thorpej struct changer_position_request cmd;
348 1.1 thorpej int val;
349 1.1 thorpej
350 1.1 thorpej /*
351 1.1 thorpej * On a position command, we expect the following:
352 1.1 thorpej *
353 1.1 thorpej * <to ET> <to EU> [inv]
354 1.1 thorpej *
355 1.1 thorpej * where ET == element type and EU == element unit.
356 1.1 thorpej */
357 1.1 thorpej if (argc < 2) {
358 1.1 thorpej warnx("%s: too few arguments", cname);
359 1.11 hubertf usage();
360 1.20 jschauma /*NOTREACHED*/
361 1.1 thorpej } else if (argc > 3) {
362 1.1 thorpej warnx("%s: too many arguments", cname);
363 1.11 hubertf usage();
364 1.20 jschauma /*NOTREACHED*/
365 1.1 thorpej }
366 1.16 wiz (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 default:
384 1.20 jschauma errx(EXIT_FAILURE, "%s: inappropriate modifier `%s'",
385 1.1 thorpej cname, *argv);
386 1.1 thorpej /* NOTREACHED */
387 1.1 thorpej }
388 1.1 thorpej }
389 1.1 thorpej
390 1.1 thorpej /* Send command to changer. */
391 1.6 thorpej if (ioctl(changer_fd, CHIOPOSITION, &cmd))
392 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOPOSITION", changer_name);
393 1.20 jschauma /* NOTREACHED */
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.16 wiz do_params(const char *cname, int argc, char **argv)
401 1.1 thorpej {
402 1.1 thorpej struct changer_params data;
403 1.1 thorpej
404 1.1 thorpej /* No arguments to this command. */
405 1.1 thorpej if (argc) {
406 1.1 thorpej warnx("%s: no arguements expected", cname);
407 1.11 hubertf usage();
408 1.20 jschauma /* NOTREACHED */
409 1.1 thorpej }
410 1.20 jschauma
411 1.1 thorpej /* Get params from changer and display them. */
412 1.16 wiz (void)memset(&data, 0, sizeof(data));
413 1.6 thorpej if (ioctl(changer_fd, CHIOGPARAMS, &data))
414 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOGPARAMS", changer_name);
415 1.20 jschauma /* NOTREACHED */
416 1.1 thorpej
417 1.13 thorpej #define PLURAL(n) (n) > 1 ? "s" : ""
418 1.13 thorpej
419 1.16 wiz (void)printf("%s: %d slot%s, %d drive%s, %d picker%s",
420 1.22 jschauma changer_name,
421 1.13 thorpej data.cp_nslots, PLURAL(data.cp_nslots),
422 1.13 thorpej data.cp_ndrives, PLURAL(data.cp_ndrives),
423 1.13 thorpej data.cp_npickers, PLURAL(data.cp_npickers));
424 1.1 thorpej if (data.cp_nportals)
425 1.16 wiz (void)printf(", %d portal%s", data.cp_nportals,
426 1.13 thorpej PLURAL(data.cp_nportals));
427 1.13 thorpej
428 1.13 thorpej #undef PLURAL
429 1.13 thorpej
430 1.22 jschauma (void)printf("\n%s: current picker: %d\n", changer_name, data.cp_curpicker);
431 1.1 thorpej
432 1.1 thorpej return (0);
433 1.1 thorpej }
434 1.1 thorpej
435 1.6 thorpej /* ARGSUSED */
436 1.1 thorpej static int
437 1.16 wiz do_getpicker(const char *cname, int argc, char **argv)
438 1.1 thorpej {
439 1.1 thorpej int picker;
440 1.1 thorpej
441 1.1 thorpej /* No arguments to this command. */
442 1.1 thorpej if (argc) {
443 1.1 thorpej warnx("%s: no arguments expected", cname);
444 1.11 hubertf usage();
445 1.20 jschauma /*NOTREACHED*/
446 1.1 thorpej }
447 1.1 thorpej
448 1.1 thorpej /* Get current picker from changer and display it. */
449 1.6 thorpej if (ioctl(changer_fd, CHIOGPICKER, &picker))
450 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOGPICKER", changer_name);
451 1.20 jschauma /* NOTREACHED */
452 1.1 thorpej
453 1.22 jschauma (void)printf("%s: current picker: %d\n", changer_name, picker);
454 1.1 thorpej
455 1.1 thorpej return (0);
456 1.1 thorpej }
457 1.1 thorpej
458 1.1 thorpej static int
459 1.16 wiz do_setpicker(const char *cname, int argc, char **argv)
460 1.1 thorpej {
461 1.1 thorpej int picker;
462 1.1 thorpej
463 1.1 thorpej if (argc < 1) {
464 1.1 thorpej warnx("%s: too few arguments", cname);
465 1.11 hubertf usage();
466 1.20 jschauma /*NOTREACHED*/
467 1.1 thorpej } else if (argc > 1) {
468 1.1 thorpej warnx("%s: too many arguments", cname);
469 1.11 hubertf usage();
470 1.20 jschauma /*NOTREACHED*/
471 1.1 thorpej }
472 1.1 thorpej
473 1.1 thorpej picker = parse_element_unit(*argv);
474 1.1 thorpej
475 1.1 thorpej /* Set the changer picker. */
476 1.6 thorpej if (ioctl(changer_fd, CHIOSPICKER, &picker))
477 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOSPICKER", changer_name);
478 1.1 thorpej
479 1.1 thorpej return (0);
480 1.1 thorpej }
481 1.1 thorpej
482 1.1 thorpej static int
483 1.16 wiz do_status(const char *cname, int argc, char **argv)
484 1.1 thorpej {
485 1.13 thorpej struct changer_element_status_request cmd;
486 1.1 thorpej struct changer_params data;
487 1.13 thorpej struct changer_element_status *ces;
488 1.16 wiz int i, chet, count, echet, flags, have_ucount, have_unit;
489 1.16 wiz int schet, ucount, unit;
490 1.13 thorpej size_t size;
491 1.18 enami
492 1.16 wiz flags = 0;
493 1.16 wiz have_ucount = 0;
494 1.16 wiz have_unit = 0;
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.13 thorpej * [<ET> [unit [count]]] [voltags]
500 1.1 thorpej *
501 1.1 thorpej * where ET == element type.
502 1.1 thorpej *
503 1.13 thorpej * If we get no element-related arguments, we get the status of all
504 1.1 thorpej * known element types.
505 1.1 thorpej */
506 1.13 thorpej if (argc > 4) {
507 1.1 thorpej warnx("%s: too many arguments", cname);
508 1.11 hubertf usage();
509 1.20 jschauma /*NOTREACHED*/
510 1.1 thorpej }
511 1.1 thorpej
512 1.1 thorpej /*
513 1.1 thorpej * Get params from changer. Specifically, we need the element
514 1.1 thorpej * counts.
515 1.1 thorpej */
516 1.16 wiz (void)memset(&data, 0, sizeof(data));
517 1.6 thorpej if (ioctl(changer_fd, CHIOGPARAMS, &data))
518 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOGPARAMS", changer_name);
519 1.20 jschauma /* NOTREACHED */
520 1.1 thorpej
521 1.13 thorpej schet = CHET_MT;
522 1.13 thorpej echet = CHET_DT;
523 1.13 thorpej
524 1.13 thorpej for (; argc != 0; argc--, argv++) {
525 1.13 thorpej /*
526 1.13 thorpej * If we have the voltags modifier, it must be the
527 1.13 thorpej * last argument.
528 1.13 thorpej */
529 1.13 thorpej if (is_special(argv[0])) {
530 1.13 thorpej if (argc != 1) {
531 1.13 thorpej warnx("%s: malformed command line", cname);
532 1.13 thorpej usage();
533 1.20 jschauma /*NOTREACHED*/
534 1.13 thorpej }
535 1.13 thorpej if (parse_special(argv[0]) != SW_VOLTAGS)
536 1.20 jschauma errx(EXIT_FAILURE,
537 1.20 jschauma "%s: inappropriate special word: %s",
538 1.13 thorpej cname, argv[0]);
539 1.20 jschauma /* NOTREACHED */
540 1.13 thorpej flags |= CESR_VOLTAGS;
541 1.13 thorpej continue;
542 1.13 thorpej }
543 1.13 thorpej
544 1.13 thorpej /*
545 1.13 thorpej * If we get an element type, we can't have specified
546 1.13 thorpej * anything else.
547 1.13 thorpej */
548 1.13 thorpej if (isdigit(*argv[0]) == 0) {
549 1.13 thorpej if (schet == echet || flags != 0 || have_unit ||
550 1.13 thorpej have_ucount) {
551 1.13 thorpej warnx("%s: malformed command line", cname);
552 1.13 thorpej usage();
553 1.20 jschauma /*NOTREACHED*/
554 1.13 thorpej }
555 1.13 thorpej schet = echet = parse_element_type(argv[0]);
556 1.13 thorpej continue;
557 1.13 thorpej }
558 1.13 thorpej
559 1.13 thorpej /*
560 1.13 thorpej * We know we have a digit here. If we do, we must
561 1.13 thorpej * have specified an element type.
562 1.13 thorpej */
563 1.13 thorpej if (schet != echet) {
564 1.13 thorpej warnx("%s: malformed command line", cname);
565 1.13 thorpej usage();
566 1.20 jschauma /*NOTREACHED*/
567 1.13 thorpej }
568 1.13 thorpej
569 1.13 thorpej i = parse_element_unit(argv[0]);
570 1.13 thorpej
571 1.13 thorpej if (have_unit == 0) {
572 1.13 thorpej unit = i;
573 1.13 thorpej have_unit = 1;
574 1.13 thorpej } else if (have_ucount == 0) {
575 1.13 thorpej ucount = i;
576 1.13 thorpej have_ucount = 1;
577 1.13 thorpej } else {
578 1.13 thorpej warnx("%s: malformed command line", cname);
579 1.13 thorpej usage();
580 1.20 jschauma /*NOTREACHED*/
581 1.13 thorpej }
582 1.1 thorpej }
583 1.1 thorpej
584 1.1 thorpej for (chet = schet; chet <= echet; ++chet) {
585 1.1 thorpej switch (chet) {
586 1.1 thorpej case CHET_MT:
587 1.1 thorpej count = data.cp_npickers;
588 1.1 thorpej break;
589 1.1 thorpej case CHET_ST:
590 1.1 thorpej count = data.cp_nslots;
591 1.1 thorpej break;
592 1.1 thorpej case CHET_IE:
593 1.1 thorpej count = data.cp_nportals;
594 1.1 thorpej break;
595 1.1 thorpej case CHET_DT:
596 1.1 thorpej count = data.cp_ndrives;
597 1.1 thorpej break;
598 1.2 thorpej default:
599 1.2 thorpej /* To appease gcc -Wuninitialized. */
600 1.2 thorpej count = 0;
601 1.1 thorpej }
602 1.1 thorpej
603 1.1 thorpej if (count == 0) {
604 1.13 thorpej if (schet != echet)
605 1.1 thorpej continue;
606 1.1 thorpej else {
607 1.16 wiz (void)printf("%s: no %s elements\n",
608 1.22 jschauma changer_name,
609 1.13 thorpej elements[chet].et_name);
610 1.1 thorpej return (0);
611 1.1 thorpej }
612 1.1 thorpej }
613 1.1 thorpej
614 1.13 thorpej /*
615 1.13 thorpej * If we have a unit, we may or may not have a count.
616 1.13 thorpej * If we don't have a unit, we don't have a count, either.
617 1.13 thorpej *
618 1.13 thorpej * Make sure both are initialized.
619 1.13 thorpej */
620 1.13 thorpej if (have_unit) {
621 1.13 thorpej if (have_ucount == 0)
622 1.13 thorpej ucount = 1;
623 1.13 thorpej } else {
624 1.13 thorpej unit = 0;
625 1.13 thorpej ucount = count;
626 1.13 thorpej }
627 1.13 thorpej
628 1.13 thorpej if ((unit + ucount) > count)
629 1.20 jschauma errx(EXIT_FAILURE, "%s: unvalid unit/count %d/%d",
630 1.13 thorpej cname, unit, ucount);
631 1.20 jschauma /* NOTREACHED */
632 1.13 thorpej
633 1.13 thorpej size = ucount * sizeof(struct changer_element_status);
634 1.13 thorpej
635 1.1 thorpej /* Allocate storage for the status bytes. */
636 1.13 thorpej if ((ces = malloc(size)) == NULL)
637 1.20 jschauma errx(EXIT_FAILURE, "can't allocate status storage");
638 1.20 jschauma /* NOTREACHED */
639 1.1 thorpej
640 1.16 wiz (void)memset(ces, 0, size);
641 1.16 wiz (void)memset(&cmd, 0, sizeof(cmd));
642 1.1 thorpej
643 1.13 thorpej cmd.cesr_type = chet;
644 1.13 thorpej cmd.cesr_unit = unit;
645 1.13 thorpej cmd.cesr_count = ucount;
646 1.13 thorpej cmd.cesr_flags = flags;
647 1.13 thorpej cmd.cesr_data = ces;
648 1.13 thorpej
649 1.13 thorpej /*
650 1.13 thorpej * Should we deal with this eventually?
651 1.13 thorpej */
652 1.13 thorpej cmd.cesr_vendor_data = NULL;
653 1.1 thorpej
654 1.6 thorpej if (ioctl(changer_fd, CHIOGSTATUS, &cmd)) {
655 1.13 thorpej free(ces);
656 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOGSTATUS", changer_name);
657 1.20 jschauma /* NOTREACHED */
658 1.1 thorpej }
659 1.1 thorpej
660 1.1 thorpej /* Dump the status for each element of this type. */
661 1.13 thorpej for (i = 0; i < ucount; i++) {
662 1.16 wiz (void)printf("%s %d: ", elements[chet].et_name,
663 1.13 thorpej unit + i);
664 1.13 thorpej if ((ces[i].ces_flags & CESTATUS_STATUS_VALID) == 0) {
665 1.16 wiz (void)printf("status not available\n");
666 1.13 thorpej continue;
667 1.13 thorpej }
668 1.16 wiz (void)printf("%s", bits_to_string(ces[i].ces_flags,
669 1.13 thorpej CESTATUS_BITS));
670 1.13 thorpej if (ces[i].ces_flags & CESTATUS_XNAME_VALID)
671 1.16 wiz (void)printf(" (%s)", ces[i].ces_xname);
672 1.16 wiz (void)printf("\n");
673 1.13 thorpej if (ces[i].ces_flags & CESTATUS_PVOL_VALID)
674 1.16 wiz (void)printf("\tPrimary volume tag: %s "
675 1.13 thorpej "ver. %d\n",
676 1.13 thorpej ces[i].ces_pvoltag.cv_tag,
677 1.13 thorpej ces[i].ces_pvoltag.cv_serial);
678 1.13 thorpej if (ces[i].ces_flags & CESTATUS_AVOL_VALID)
679 1.16 wiz (void)printf("\tAlternate volume tag: %s "
680 1.13 thorpej "ver. %d\n",
681 1.13 thorpej ces[i].ces_avoltag.cv_tag,
682 1.13 thorpej ces[i].ces_avoltag.cv_serial);
683 1.13 thorpej if (ces[i].ces_flags & CESTATUS_FROM_VALID)
684 1.16 wiz (void)printf("\tFrom: %s %d\n",
685 1.13 thorpej elements[ces[i].ces_from_type].et_name,
686 1.13 thorpej ces[i].ces_from_unit);
687 1.14 thorpej if (ces[i].ces_vendor_len)
688 1.16 wiz (void)printf("\tVendor-specific data size: "
689 1.14 thorpej "%lu\n", (u_long)ces[i].ces_vendor_len);
690 1.1 thorpej }
691 1.13 thorpej free(ces);
692 1.1 thorpej }
693 1.1 thorpej
694 1.1 thorpej return (0);
695 1.1 thorpej }
696 1.1 thorpej
697 1.6 thorpej /* ARGSUSED */
698 1.1 thorpej static int
699 1.16 wiz do_ielem(const char *cname, int argc, char **argv)
700 1.5 mjacob {
701 1.18 enami
702 1.6 thorpej if (ioctl(changer_fd, CHIOIELEM, NULL))
703 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOIELEM", changer_name);
704 1.20 jschauma /* NOTREACHED */
705 1.5 mjacob
706 1.5 mjacob return (0);
707 1.5 mjacob }
708 1.5 mjacob
709 1.13 thorpej /* ARGSUSED */
710 1.7 hpeyerl static int
711 1.16 wiz do_cdlu(const char *cname, int argc, char **argv)
712 1.7 hpeyerl {
713 1.7 hpeyerl static const struct special_word cdlu_subcmds[] = {
714 1.7 hpeyerl { "load", CD_LU_LOAD },
715 1.7 hpeyerl { "unload", CD_LU_UNLOAD },
716 1.7 hpeyerl { "abort", CD_LU_ABORT },
717 1.7 hpeyerl { NULL, 0 },
718 1.7 hpeyerl };
719 1.16 wiz struct ioc_load_unload cmd;
720 1.16 wiz int i;
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.20 jschauma /*NOTREACHED*/
734 1.7 hpeyerl
735 1.7 hpeyerl for (i = 0; cdlu_subcmds[i].sw_name != NULL; i++) {
736 1.7 hpeyerl if (strcmp(argv[0], cdlu_subcmds[i].sw_name) == 0) {
737 1.7 hpeyerl cmd.options = cdlu_subcmds[i].sw_value;
738 1.7 hpeyerl break;
739 1.7 hpeyerl }
740 1.7 hpeyerl }
741 1.7 hpeyerl if (cdlu_subcmds[i].sw_name == NULL)
742 1.11 hubertf usage();
743 1.20 jschauma /*NOTREACHED*/
744 1.7 hpeyerl
745 1.7 hpeyerl if (strcmp(argv[0], "abort") == 0)
746 1.7 hpeyerl cmd.slot = 0;
747 1.7 hpeyerl else
748 1.7 hpeyerl cmd.slot = parse_element_unit(argv[1]);
749 1.7 hpeyerl
750 1.7 hpeyerl /*
751 1.7 hpeyerl * XXX Should maybe do something different with the device
752 1.7 hpeyerl * XXX handling for cdlu; think about this some more.
753 1.7 hpeyerl */
754 1.7 hpeyerl if (ioctl(changer_fd, CDIOCLOADUNLOAD, &cmd))
755 1.22 jschauma err(EXIT_FAILURE, "%s: CDIOCLOADUNLOAD", changer_name);
756 1.20 jschauma /* NOTREACHED */
757 1.7 hpeyerl
758 1.7 hpeyerl return (0);
759 1.7 hpeyerl }
760 1.5 mjacob
761 1.5 mjacob static int
762 1.16 wiz parse_element_type(const char *cp)
763 1.1 thorpej {
764 1.1 thorpej int i;
765 1.1 thorpej
766 1.1 thorpej for (i = 0; elements[i].et_name != NULL; ++i)
767 1.1 thorpej if (strcmp(elements[i].et_name, cp) == 0)
768 1.1 thorpej return (elements[i].et_type);
769 1.1 thorpej
770 1.20 jschauma errx(EXIT_FAILURE, "invalid element type `%s'", cp);
771 1.6 thorpej /* NOTREACHED */
772 1.1 thorpej }
773 1.1 thorpej
774 1.1 thorpej static int
775 1.16 wiz parse_element_unit(const char *cp)
776 1.1 thorpej {
777 1.16 wiz char *p;
778 1.1 thorpej int i;
779 1.1 thorpej
780 1.1 thorpej i = (int)strtol(cp, &p, 10);
781 1.1 thorpej if ((i < 0) || (*p != '\0'))
782 1.20 jschauma errx(EXIT_FAILURE, "invalid unit number `%s'", cp);
783 1.1 thorpej
784 1.1 thorpej return (i);
785 1.1 thorpej }
786 1.1 thorpej
787 1.1 thorpej static int
788 1.16 wiz parse_special(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.20 jschauma errx(EXIT_FAILURE, "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.16 wiz is_special(const char *cp)
802 1.1 thorpej {
803 1.1 thorpej int i;
804 1.1 thorpej
805 1.1 thorpej for (i = 0; specials[i].sw_name != NULL; ++i)
806 1.1 thorpej if (strcmp(specials[i].sw_name, cp) == 0)
807 1.1 thorpej return (specials[i].sw_value);
808 1.1 thorpej
809 1.1 thorpej return (0);
810 1.1 thorpej }
811 1.1 thorpej
812 1.8 mycroft static const char *
813 1.16 wiz bits_to_string(int v, const char *cp)
814 1.1 thorpej {
815 1.16 wiz static char buf[128];
816 1.1 thorpej const char *np;
817 1.16 wiz char *bp, f;
818 1.8 mycroft int first;
819 1.1 thorpej
820 1.1 thorpej bp = buf;
821 1.8 mycroft *bp++ = '<';
822 1.8 mycroft for (first = 1; (f = *cp++) != 0; cp = np) {
823 1.1 thorpej for (np = cp; *np >= ' ';)
824 1.1 thorpej np++;
825 1.1 thorpej if ((v & (1 << (f - 1))) == 0)
826 1.1 thorpej continue;
827 1.8 mycroft if (first)
828 1.8 mycroft first = 0;
829 1.8 mycroft else
830 1.8 mycroft *bp++ = ',';
831 1.16 wiz (void)memcpy(bp, cp, np - cp);
832 1.8 mycroft bp += np - cp;
833 1.1 thorpej }
834 1.8 mycroft *bp++ = '>';
835 1.8 mycroft *bp = '\0';
836 1.1 thorpej
837 1.1 thorpej return (buf);
838 1.1 thorpej }
839 1.1 thorpej
840 1.1 thorpej static void
841 1.16 wiz cleanup(void)
842 1.1 thorpej {
843 1.18 enami
844 1.1 thorpej /* Simple enough... */
845 1.1 thorpej (void)close(changer_fd);
846 1.1 thorpej }
847 1.1 thorpej
848 1.1 thorpej static void
849 1.16 wiz usage(void)
850 1.1 thorpej {
851 1.11 hubertf int i;
852 1.1 thorpej
853 1.25 wiz (void)fprintf(stderr,
854 1.25 wiz "usage: %s [-f changer] command arg1 arg2 [arg3 [...]]\n",
855 1.15 cgd getprogname());
856 1.18 enami
857 1.16 wiz (void)fprintf(stderr, "Where command (and args) are:\n");
858 1.13 thorpej for (i = 0; commands[i].cc_name != NULL; i++)
859 1.16 wiz (void)fprintf(stderr, "\t%s%s\n", commands[i].cc_name,
860 1.13 thorpej commands[i].cc_args);
861 1.1 thorpej exit(1);
862 1.10 mycroft /* NOTREACHED */
863 1.1 thorpej }
864