chio.c revision 1.28 1 1.28 msaitoh /* $NetBSD: chio.c,v 1.28 2007/04/29 20:23:34 msaitoh 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.28 msaitoh __RCSID("$NetBSD: chio.c,v 1.28 2007/04/29 20:23:34 msaitoh 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.28 msaitoh warnx("%s: no arguments 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.27 lukem ucount = 0;
494 1.27 lukem unit = 0;
495 1.16 wiz have_ucount = 0;
496 1.16 wiz have_unit = 0;
497 1.1 thorpej
498 1.1 thorpej /*
499 1.1 thorpej * On a status command, we expect the following:
500 1.1 thorpej *
501 1.13 thorpej * [<ET> [unit [count]]] [voltags]
502 1.1 thorpej *
503 1.1 thorpej * where ET == element type.
504 1.1 thorpej *
505 1.13 thorpej * If we get no element-related arguments, we get the status of all
506 1.1 thorpej * known element types.
507 1.1 thorpej */
508 1.13 thorpej if (argc > 4) {
509 1.1 thorpej warnx("%s: too many arguments", cname);
510 1.11 hubertf usage();
511 1.20 jschauma /*NOTREACHED*/
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.16 wiz (void)memset(&data, 0, sizeof(data));
519 1.6 thorpej if (ioctl(changer_fd, CHIOGPARAMS, &data))
520 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOGPARAMS", changer_name);
521 1.20 jschauma /* NOTREACHED */
522 1.1 thorpej
523 1.13 thorpej schet = CHET_MT;
524 1.13 thorpej echet = CHET_DT;
525 1.13 thorpej
526 1.13 thorpej for (; argc != 0; argc--, argv++) {
527 1.13 thorpej /*
528 1.13 thorpej * If we have the voltags modifier, it must be the
529 1.13 thorpej * last argument.
530 1.13 thorpej */
531 1.13 thorpej if (is_special(argv[0])) {
532 1.13 thorpej if (argc != 1) {
533 1.13 thorpej warnx("%s: malformed command line", cname);
534 1.13 thorpej usage();
535 1.20 jschauma /*NOTREACHED*/
536 1.13 thorpej }
537 1.13 thorpej if (parse_special(argv[0]) != SW_VOLTAGS)
538 1.20 jschauma errx(EXIT_FAILURE,
539 1.20 jschauma "%s: inappropriate special word: %s",
540 1.13 thorpej cname, argv[0]);
541 1.20 jschauma /* NOTREACHED */
542 1.13 thorpej flags |= CESR_VOLTAGS;
543 1.13 thorpej continue;
544 1.13 thorpej }
545 1.13 thorpej
546 1.13 thorpej /*
547 1.13 thorpej * If we get an element type, we can't have specified
548 1.13 thorpej * anything else.
549 1.13 thorpej */
550 1.26 dsl if (isdigit((unsigned char)*argv[0]) == 0) {
551 1.13 thorpej if (schet == echet || flags != 0 || have_unit ||
552 1.13 thorpej have_ucount) {
553 1.13 thorpej warnx("%s: malformed command line", cname);
554 1.13 thorpej usage();
555 1.20 jschauma /*NOTREACHED*/
556 1.13 thorpej }
557 1.13 thorpej schet = echet = parse_element_type(argv[0]);
558 1.13 thorpej continue;
559 1.13 thorpej }
560 1.13 thorpej
561 1.13 thorpej /*
562 1.13 thorpej * We know we have a digit here. If we do, we must
563 1.13 thorpej * have specified an element type.
564 1.13 thorpej */
565 1.13 thorpej if (schet != echet) {
566 1.13 thorpej warnx("%s: malformed command line", cname);
567 1.13 thorpej usage();
568 1.20 jschauma /*NOTREACHED*/
569 1.13 thorpej }
570 1.13 thorpej
571 1.13 thorpej i = parse_element_unit(argv[0]);
572 1.13 thorpej
573 1.13 thorpej if (have_unit == 0) {
574 1.13 thorpej unit = i;
575 1.13 thorpej have_unit = 1;
576 1.13 thorpej } else if (have_ucount == 0) {
577 1.13 thorpej ucount = i;
578 1.13 thorpej have_ucount = 1;
579 1.13 thorpej } else {
580 1.13 thorpej warnx("%s: malformed command line", cname);
581 1.13 thorpej usage();
582 1.20 jschauma /*NOTREACHED*/
583 1.13 thorpej }
584 1.1 thorpej }
585 1.1 thorpej
586 1.1 thorpej for (chet = schet; chet <= echet; ++chet) {
587 1.1 thorpej switch (chet) {
588 1.1 thorpej case CHET_MT:
589 1.1 thorpej count = data.cp_npickers;
590 1.1 thorpej break;
591 1.1 thorpej case CHET_ST:
592 1.1 thorpej count = data.cp_nslots;
593 1.1 thorpej break;
594 1.1 thorpej case CHET_IE:
595 1.1 thorpej count = data.cp_nportals;
596 1.1 thorpej break;
597 1.1 thorpej case CHET_DT:
598 1.1 thorpej count = data.cp_ndrives;
599 1.1 thorpej break;
600 1.2 thorpej default:
601 1.2 thorpej /* To appease gcc -Wuninitialized. */
602 1.2 thorpej count = 0;
603 1.1 thorpej }
604 1.1 thorpej
605 1.1 thorpej if (count == 0) {
606 1.13 thorpej if (schet != echet)
607 1.1 thorpej continue;
608 1.1 thorpej else {
609 1.16 wiz (void)printf("%s: no %s elements\n",
610 1.22 jschauma changer_name,
611 1.13 thorpej elements[chet].et_name);
612 1.1 thorpej return (0);
613 1.1 thorpej }
614 1.1 thorpej }
615 1.1 thorpej
616 1.13 thorpej /*
617 1.13 thorpej * If we have a unit, we may or may not have a count.
618 1.13 thorpej * If we don't have a unit, we don't have a count, either.
619 1.13 thorpej *
620 1.13 thorpej * Make sure both are initialized.
621 1.13 thorpej */
622 1.13 thorpej if (have_unit) {
623 1.13 thorpej if (have_ucount == 0)
624 1.13 thorpej ucount = 1;
625 1.13 thorpej } else {
626 1.13 thorpej unit = 0;
627 1.13 thorpej ucount = count;
628 1.13 thorpej }
629 1.13 thorpej
630 1.13 thorpej if ((unit + ucount) > count)
631 1.20 jschauma errx(EXIT_FAILURE, "%s: unvalid unit/count %d/%d",
632 1.13 thorpej cname, unit, ucount);
633 1.20 jschauma /* NOTREACHED */
634 1.13 thorpej
635 1.13 thorpej size = ucount * sizeof(struct changer_element_status);
636 1.13 thorpej
637 1.1 thorpej /* Allocate storage for the status bytes. */
638 1.13 thorpej if ((ces = malloc(size)) == NULL)
639 1.20 jschauma errx(EXIT_FAILURE, "can't allocate status storage");
640 1.20 jschauma /* NOTREACHED */
641 1.1 thorpej
642 1.16 wiz (void)memset(ces, 0, size);
643 1.16 wiz (void)memset(&cmd, 0, sizeof(cmd));
644 1.1 thorpej
645 1.13 thorpej cmd.cesr_type = chet;
646 1.13 thorpej cmd.cesr_unit = unit;
647 1.13 thorpej cmd.cesr_count = ucount;
648 1.13 thorpej cmd.cesr_flags = flags;
649 1.13 thorpej cmd.cesr_data = ces;
650 1.13 thorpej
651 1.13 thorpej /*
652 1.13 thorpej * Should we deal with this eventually?
653 1.13 thorpej */
654 1.13 thorpej cmd.cesr_vendor_data = NULL;
655 1.1 thorpej
656 1.6 thorpej if (ioctl(changer_fd, CHIOGSTATUS, &cmd)) {
657 1.13 thorpej free(ces);
658 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOGSTATUS", changer_name);
659 1.20 jschauma /* NOTREACHED */
660 1.1 thorpej }
661 1.1 thorpej
662 1.1 thorpej /* Dump the status for each element of this type. */
663 1.13 thorpej for (i = 0; i < ucount; i++) {
664 1.16 wiz (void)printf("%s %d: ", elements[chet].et_name,
665 1.13 thorpej unit + i);
666 1.13 thorpej if ((ces[i].ces_flags & CESTATUS_STATUS_VALID) == 0) {
667 1.16 wiz (void)printf("status not available\n");
668 1.13 thorpej continue;
669 1.13 thorpej }
670 1.16 wiz (void)printf("%s", bits_to_string(ces[i].ces_flags,
671 1.13 thorpej CESTATUS_BITS));
672 1.13 thorpej if (ces[i].ces_flags & CESTATUS_XNAME_VALID)
673 1.16 wiz (void)printf(" (%s)", ces[i].ces_xname);
674 1.16 wiz (void)printf("\n");
675 1.13 thorpej if (ces[i].ces_flags & CESTATUS_PVOL_VALID)
676 1.16 wiz (void)printf("\tPrimary volume tag: %s "
677 1.13 thorpej "ver. %d\n",
678 1.13 thorpej ces[i].ces_pvoltag.cv_tag,
679 1.13 thorpej ces[i].ces_pvoltag.cv_serial);
680 1.13 thorpej if (ces[i].ces_flags & CESTATUS_AVOL_VALID)
681 1.16 wiz (void)printf("\tAlternate volume tag: %s "
682 1.13 thorpej "ver. %d\n",
683 1.13 thorpej ces[i].ces_avoltag.cv_tag,
684 1.13 thorpej ces[i].ces_avoltag.cv_serial);
685 1.13 thorpej if (ces[i].ces_flags & CESTATUS_FROM_VALID)
686 1.16 wiz (void)printf("\tFrom: %s %d\n",
687 1.13 thorpej elements[ces[i].ces_from_type].et_name,
688 1.13 thorpej ces[i].ces_from_unit);
689 1.14 thorpej if (ces[i].ces_vendor_len)
690 1.16 wiz (void)printf("\tVendor-specific data size: "
691 1.14 thorpej "%lu\n", (u_long)ces[i].ces_vendor_len);
692 1.1 thorpej }
693 1.13 thorpej free(ces);
694 1.1 thorpej }
695 1.1 thorpej
696 1.1 thorpej return (0);
697 1.1 thorpej }
698 1.1 thorpej
699 1.6 thorpej /* ARGSUSED */
700 1.1 thorpej static int
701 1.16 wiz do_ielem(const char *cname, int argc, char **argv)
702 1.5 mjacob {
703 1.18 enami
704 1.6 thorpej if (ioctl(changer_fd, CHIOIELEM, NULL))
705 1.22 jschauma err(EXIT_FAILURE, "%s: CHIOIELEM", changer_name);
706 1.20 jschauma /* NOTREACHED */
707 1.5 mjacob
708 1.5 mjacob return (0);
709 1.5 mjacob }
710 1.5 mjacob
711 1.13 thorpej /* ARGSUSED */
712 1.7 hpeyerl static int
713 1.16 wiz do_cdlu(const char *cname, int argc, char **argv)
714 1.7 hpeyerl {
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.16 wiz struct ioc_load_unload cmd;
722 1.16 wiz int i;
723 1.7 hpeyerl
724 1.7 hpeyerl /*
725 1.7 hpeyerl * This command is a little different, since we are mostly dealing
726 1.7 hpeyerl * with ATAPI CD changers, which have a lame API (since ATAPI doesn't
727 1.7 hpeyerl * have LUNs).
728 1.7 hpeyerl *
729 1.7 hpeyerl * We have 3 sub-commands: "load", "unload", and "abort". The
730 1.7 hpeyerl * first two take a slot number. The latter does not.
731 1.7 hpeyerl */
732 1.7 hpeyerl
733 1.7 hpeyerl if (argc < 1 || argc > 2)
734 1.11 hubertf usage();
735 1.20 jschauma /*NOTREACHED*/
736 1.7 hpeyerl
737 1.7 hpeyerl for (i = 0; cdlu_subcmds[i].sw_name != NULL; i++) {
738 1.7 hpeyerl if (strcmp(argv[0], cdlu_subcmds[i].sw_name) == 0) {
739 1.7 hpeyerl cmd.options = cdlu_subcmds[i].sw_value;
740 1.7 hpeyerl break;
741 1.7 hpeyerl }
742 1.7 hpeyerl }
743 1.7 hpeyerl if (cdlu_subcmds[i].sw_name == NULL)
744 1.11 hubertf usage();
745 1.20 jschauma /*NOTREACHED*/
746 1.7 hpeyerl
747 1.7 hpeyerl if (strcmp(argv[0], "abort") == 0)
748 1.7 hpeyerl cmd.slot = 0;
749 1.7 hpeyerl else
750 1.7 hpeyerl cmd.slot = parse_element_unit(argv[1]);
751 1.7 hpeyerl
752 1.7 hpeyerl /*
753 1.7 hpeyerl * XXX Should maybe do something different with the device
754 1.7 hpeyerl * XXX handling for cdlu; think about this some more.
755 1.7 hpeyerl */
756 1.7 hpeyerl if (ioctl(changer_fd, CDIOCLOADUNLOAD, &cmd))
757 1.22 jschauma err(EXIT_FAILURE, "%s: CDIOCLOADUNLOAD", changer_name);
758 1.20 jschauma /* NOTREACHED */
759 1.7 hpeyerl
760 1.7 hpeyerl return (0);
761 1.7 hpeyerl }
762 1.5 mjacob
763 1.5 mjacob static int
764 1.16 wiz parse_element_type(const char *cp)
765 1.1 thorpej {
766 1.1 thorpej int i;
767 1.1 thorpej
768 1.1 thorpej for (i = 0; elements[i].et_name != NULL; ++i)
769 1.1 thorpej if (strcmp(elements[i].et_name, cp) == 0)
770 1.1 thorpej return (elements[i].et_type);
771 1.1 thorpej
772 1.20 jschauma errx(EXIT_FAILURE, "invalid element type `%s'", cp);
773 1.6 thorpej /* NOTREACHED */
774 1.1 thorpej }
775 1.1 thorpej
776 1.1 thorpej static int
777 1.16 wiz parse_element_unit(const char *cp)
778 1.1 thorpej {
779 1.16 wiz char *p;
780 1.1 thorpej int i;
781 1.1 thorpej
782 1.1 thorpej i = (int)strtol(cp, &p, 10);
783 1.1 thorpej if ((i < 0) || (*p != '\0'))
784 1.20 jschauma errx(EXIT_FAILURE, "invalid unit number `%s'", cp);
785 1.1 thorpej
786 1.1 thorpej return (i);
787 1.1 thorpej }
788 1.1 thorpej
789 1.1 thorpej static int
790 1.16 wiz parse_special(const char *cp)
791 1.1 thorpej {
792 1.1 thorpej int val;
793 1.1 thorpej
794 1.1 thorpej val = is_special(cp);
795 1.1 thorpej if (val)
796 1.1 thorpej return (val);
797 1.1 thorpej
798 1.20 jschauma errx(EXIT_FAILURE, "invalid modifier `%s'", cp);
799 1.6 thorpej /* NOTREACHED */
800 1.1 thorpej }
801 1.1 thorpej
802 1.1 thorpej static int
803 1.16 wiz is_special(const char *cp)
804 1.1 thorpej {
805 1.1 thorpej int i;
806 1.1 thorpej
807 1.1 thorpej for (i = 0; specials[i].sw_name != NULL; ++i)
808 1.1 thorpej if (strcmp(specials[i].sw_name, cp) == 0)
809 1.1 thorpej return (specials[i].sw_value);
810 1.1 thorpej
811 1.1 thorpej return (0);
812 1.1 thorpej }
813 1.1 thorpej
814 1.8 mycroft static const char *
815 1.16 wiz bits_to_string(int v, const char *cp)
816 1.1 thorpej {
817 1.16 wiz static char buf[128];
818 1.1 thorpej const char *np;
819 1.16 wiz char *bp, f;
820 1.8 mycroft int first;
821 1.1 thorpej
822 1.1 thorpej bp = buf;
823 1.8 mycroft *bp++ = '<';
824 1.8 mycroft for (first = 1; (f = *cp++) != 0; cp = np) {
825 1.1 thorpej for (np = cp; *np >= ' ';)
826 1.1 thorpej np++;
827 1.1 thorpej if ((v & (1 << (f - 1))) == 0)
828 1.1 thorpej continue;
829 1.8 mycroft if (first)
830 1.8 mycroft first = 0;
831 1.8 mycroft else
832 1.8 mycroft *bp++ = ',';
833 1.16 wiz (void)memcpy(bp, cp, np - cp);
834 1.8 mycroft bp += np - cp;
835 1.1 thorpej }
836 1.8 mycroft *bp++ = '>';
837 1.8 mycroft *bp = '\0';
838 1.1 thorpej
839 1.1 thorpej return (buf);
840 1.1 thorpej }
841 1.1 thorpej
842 1.1 thorpej static void
843 1.16 wiz cleanup(void)
844 1.1 thorpej {
845 1.18 enami
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.16 wiz usage(void)
852 1.1 thorpej {
853 1.11 hubertf int i;
854 1.1 thorpej
855 1.25 wiz (void)fprintf(stderr,
856 1.25 wiz "usage: %s [-f changer] command arg1 arg2 [arg3 [...]]\n",
857 1.15 cgd getprogname());
858 1.18 enami
859 1.16 wiz (void)fprintf(stderr, "Where command (and args) are:\n");
860 1.13 thorpej for (i = 0; commands[i].cc_name != NULL; i++)
861 1.16 wiz (void)fprintf(stderr, "\t%s%s\n", commands[i].cc_name,
862 1.13 thorpej commands[i].cc_args);
863 1.1 thorpej exit(1);
864 1.10 mycroft /* NOTREACHED */
865 1.1 thorpej }
866