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