xref: /netbsd-src/usr.bin/scmdctl/scmdctl.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /*	$NetBSD: scmdctl.c,v 1.2 2024/11/03 10:43:27 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 2021 Brad Spencer <brad@anduin.eldar.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/cdefs.h>
20 #ifdef __RCSID
21 __RCSID("$NetBSD: scmdctl.c,v 1.2 2024/11/03 10:43:27 rillig Exp $");
22 #endif
23 
24 /* Main userland program that knows how to talk to the Sparkfun
25  * Serial Controlled Motor Driver (SCMD).  The device provides
26  * 127 registers that are used to interact with the motors.
27  * This program provides some convience commands to work with most
28  * of the abilities of the SCMD device.
29  *
30  * This knows how to talk to a SCMD device via:
31  *
32  * 1) The uart tty interface that is provided by the SCMD device
33  * 2) Userland SPI talking to something like /dev/spi0 directly
34  *    In most ways this acts like talking to the tty uart.
35  * 3) Using the scmd(4) i2c or spi driver.  This is, by far, the
36  *    fastest way to access the driver.  The other methods have
37  *    increased latency.
38  */
39 
40 #include <inttypes.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <err.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <termios.h>
50 #include <sys/ioctl.h>
51 #include <sys/time.h>
52 #include <dev/spi/spi_io.h>
53 
54 #include <dev/ic/scmdreg.h>
55 
56 #define EXTERN extern
57 #include "common.h"
58 #include "scmdctl.h"
59 #include "uart.h"
60 #include "i2cspi.h"
61 #include "printscmd.h"
62 #include "responses.h"
63 #include "scmdctlconst.h"
64 
65 int	ul_spisetup(int, int);
66 int	ttysetup(int, speed_t);
67 int	valid_cmd(const struct scmdcmd[], long unsigned int, char *);
68 
69 
70 static void
71 usage(void)
72 {
73 	const char *p = getprogname();
74 
75 	fprintf(stderr, "Usage: %s [-dlh] [-b baud rate] [-s SPI slave addr] device cmd args\n\n",
76 	    p);
77 
78 	for(long unsigned int i = 0;i < __arraycount(scmdcmds);i++) {
79 		fprintf(stderr,"%s [-dlh] [-b baud rate] [-s SPI slave addr] device %s %s\n",
80 		    p,scmdcmds[i].cmd,scmdcmds[i].helpargs);
81 	}
82 }
83 
84 int
85 valid_cmd(const struct scmdcmd c[], long unsigned int csize, char *cmdtocheck)
86 {
87 	int r = -1;
88 
89 	for(long unsigned int i = 0;i < csize;i++) {
90 		if (strncmp(cmdtocheck,c[i].cmd,16) == 0) {
91 			r = i;
92 			break;
93 		}
94 	}
95 
96 	return r;
97 }
98 
99 /* This is expected to fail if the device is not a classic tty */
100 int
101 ttysetup(int fd, speed_t spd)
102 {
103         struct termios  cntrl;
104 
105         (void)tcgetattr(fd, &cntrl);
106         (void)cfsetospeed(&cntrl, spd);
107         (void)cfsetispeed(&cntrl, spd);
108         cntrl.c_cflag &= ~(CSIZE|PARENB);
109         cntrl.c_cflag |= CS8;
110 	cntrl.c_cflag |= CLOCAL;
111         cntrl.c_iflag &= ~(ISTRIP|ICRNL);
112         cntrl.c_oflag &= ~OPOST;
113         cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
114         cntrl.c_cc[VMIN] = 1;
115         cntrl.c_cc[VTIME] = 0;
116 	cntrl.c_iflag &= ~(IXOFF|IXON);
117         return tcsetattr(fd, TCSAFLUSH, &cntrl);
118 }
119 
120 /* This is for userland SPI and is expected to fail if the device is
121  * not a /dev/spiN
122  */
123 int
124 ul_spisetup(int fd, int slave_addr)
125 {
126 	struct timespec ts;
127 	struct spi_ioctl_configure spi_c;
128 	int e;
129 
130 	spi_c.sic_addr = slave_addr;
131 #define SPI_MODE_0 0
132 #define SPI_MODE_1 1
133 #define SPI_MODE_2 2
134 #define SPI_MODE_3 3
135 	spi_c.sic_mode = SPI_MODE_0;
136 	spi_c.sic_speed = 1000000;
137 
138 	e = ioctl(fd,SPI_IOCTL_CONFIGURE,&spi_c);
139 	if (e != -1) {
140 		ts.tv_sec = 0;
141 		ts.tv_nsec = 50;
142 		nanosleep(&ts,NULL);
143 	}
144 
145 	return e;
146 }
147 
148 int
149 main(int argc, char *argv[])
150 {
151 	int c;
152 	bool debug = false;
153 	int fd = -1, error, ttyerror = 0, ul_spierror = 0, valid, validsub = -1;
154 	long baud_rate = 9600;
155 	long slave_a = 0;
156 	bool dev_is_uart = true;
157 	int uart_s = UART_IS_PURE_UART;
158 	struct scmd_identify_response ir;
159 	struct scmd_diag_response diag;
160 	struct scmd_motor_response motors;
161 	long module;
162 	char motor;
163 	int8_t reg_value;
164 	uint8_t reg = 0, reg_e = 0, ur, ebus_s, lock_state;
165 	uint8_t register_shadow[SCMD_REG_SIZE];
166 	int lock_type = -1;
167 	bool list_names = false;
168 	struct function_block func_block;
169 
170 	while ((c = getopt(argc, argv, "db:s:lh")) != -1 ) {
171 		switch (c) {
172 		case 'd':
173 			debug = true;
174 			break;
175 		case 'b':
176 			baud_rate = (long)strtoi(optarg, NULL, 0, 1, LONG_MAX, &error);
177 			if (error)
178 				warnc(error, "Conversion of `%s' to a baud rate "
179 				    "failed, using %ld", optarg, baud_rate);
180 			break;
181 		case 's':
182 			slave_a = (long)strtoi(optarg, NULL, 0, 0, LONG_MAX, &error);
183 			if (error)
184 				warnc(error, "Conversion of `%s' to a SPI slave address "
185 				    "failed, using %ld", optarg, slave_a);
186 			break;
187 		case 'l':
188 			list_names = true;
189 			break;
190 		case 'h':
191 		default:
192 			usage();
193 			exit(0);
194 		}
195 	}
196 
197 	argc -= optind;
198 	argv += optind;
199 
200 	if (debug) {
201 		fprintf(stderr,"ARGC: %d\n", argc);
202 		fprintf(stderr,"ARGV[0]: %s ; ARGV[1]: %s ; ARGV[2]: %s ; ARGV[3]: %s; ARGV[4]: %s; ARGV[5]: %s\n",
203 		    argv[0],argv[1],argv[2],argv[3],argv[4],argv[5]);
204 	}
205 
206 	if (list_names) {
207 		for(c = 0x00; c < SCMD_REG_SIZE;c++)
208 			printf("Register %d (0x%02X): %s\n",c,c,scmdregisternames[c]);
209 		exit(0);
210 	}
211 
212 	if (argc <= 1) {
213 		usage();
214 		exit(0);
215 	}
216 
217 	fd = open(argv[0], O_RDWR, 0);
218 	if (fd == -1) {
219 		err(EXIT_FAILURE, "open %s", argv[0]);
220 	}
221 
222 	/* Figure out what the device is.  First try uart tty,
223 	 * then SPI userland and the if those two fail, assume
224 	 * scmd(4).
225 	 */
226 	ttyerror = ttysetup(fd,(speed_t)baud_rate);
227 
228 	if (ttyerror) {
229 		ul_spierror = ul_spisetup(fd, slave_a);
230 		if (ul_spierror) {
231 			dev_is_uart = false;
232 		} else {
233 			uart_s = UART_IS_SPI_USERLAND;
234 		}
235 	}
236 	uart_set_subtype(uart_s, slave_a);
237 
238 	if (debug) {
239 		fprintf(stderr, "ttysetup: error return %d\n", ttyerror);
240 		fprintf(stderr, "ul_spisetup: error return %d\n", ul_spierror);
241 	}
242 
243 	/* A UART here is either a tty uart or a SPI userland device.
244 	 * They mostly end up working the same.
245 	 */
246 	if (dev_is_uart) {
247 		func_block.func_clear = &uart_clear;
248 		func_block.func_phy_read = &uart_read_register;
249 		func_block.func_phy_write = &uart_write_register;
250 	} else {
251 		func_block.func_clear = &i2cspi_clear;
252 		func_block.func_phy_read = &i2cspi_read_register;
253 		func_block.func_phy_write = &i2cspi_write_register;
254 	}
255 
256 	valid = valid_cmd(scmdcmds,__arraycount(scmdcmds),argv[1]);
257 
258 	if (valid != -1) {
259 		switch (scmdcmds[valid].id) {
260 		case SCMD_IDENTIFY:
261 			module = 0;
262 			if (argc == 3) {
263 				module = (long)strtoi(argv[2], NULL, 10, 0, 16, &error);
264 				if (error)
265 					warnc(error, "Conversion of '%s' module failed,"
266 					    " using %ld", argv[2], module);
267 			}
268 			error = common_identify(&func_block, fd, debug, module, &ir);
269 			break;
270 		case SCMD_DIAG:
271 			module = 0;
272 			if (argc == 3) {
273 				module = (long)strtoi(argv[2], NULL, 10, 0, 16, &error);
274 				if (error)
275 					warnc(error, "Conversion of '%s' module failed,"
276 					    " using %ld", argv[2], module);
277 			}
278 			error = common_diag(&func_block, fd, debug, module, &diag);
279 			break;
280 		case SCMD_MOTOR:
281 			if (argc >= 3) {
282 				validsub = valid_cmd(motorsubcmds,__arraycount(motorsubcmds),argv[2]);
283 				if (validsub != -1) {
284 					switch (motorsubcmds[validsub].id) {
285 					case SCMD_SUBMOTORGET:
286 						module = SCMD_ANY_MODULE;
287 						if (argc == 4) {
288 							module = (long)strtoi(argv[3], NULL, 10, 0, 16, &error);
289 							if (error)
290 								warnc(error, "Conversion of '%s' module failed,"
291 								    " using %ld", argv[3], module);
292 						}
293 						error = common_get_motor(&func_block, fd, debug, (int)module, &motors);
294 						break;
295 					case SCMD_SUBMOTORSET:
296 						if (argc == 6) {
297 							module = (long)strtoi(argv[3], NULL, 10, 0, 16, &error);
298 							if (error)
299 								warnc(error, "Conversion of '%s' module failed,"
300 								    " using %ld", argv[3], module);
301 							motor = argv[4][0];
302 							reg_value = (int8_t)strtoi(argv[5], NULL, 0, -127, 127, &error);
303 							if (error)
304 								err(EXIT_FAILURE,"Bad conversion for set motor for reg_value: %s", argv[5]);
305 						} else {
306 							fprintf(stderr,"Missing arguments to set motor command\n\n");
307 							usage();
308 							exit(1);
309 						}
310 						error = common_set_motor(&func_block, fd, debug, (int)module, motor, reg_value);
311 						break;
312 					case SCMD_SUBMOTORINVERT:
313 						if (argc == 5) {
314 							module = (long)strtoi(argv[3], NULL, 10, 0, 16, &error);
315 							if (error)
316 								warnc(error, "Conversion of '%s' module failed,"
317 								    " using %ld", argv[3], module);
318 							motor = argv[4][0];
319 						} else {
320 							fprintf(stderr,"Missing arguments to invert motor command\n\n");
321 							usage();
322 							exit(1);
323 						}
324 						error = common_invert_motor(&func_block, fd, debug, (int)module, motor);
325 						break;
326 					case SCMD_SUBMOTORBRIDGE:
327 						if (argc == 4) {
328 							module = (long)strtoi(argv[3], NULL, 10, 0, 16, &error);
329 							if (error)
330 								warnc(error, "Conversion of '%s' module failed,"
331 								    " using %ld", argv[3], module);
332 						} else {
333 							fprintf(stderr,"Missing arguments to bridge motor command\n\n");
334 							usage();
335 							exit(1);
336 						}
337 						error = common_bridge_motor(&func_block, fd, debug, (int)module);
338 						break;
339 					case SCMD_SUBMOTORDISABLE:
340 						error = common_enable_disable(&func_block, fd, debug, SCMD_DISABLE);
341 						break;
342 					case SCMD_SUBMOTORENABLE:
343 						error = common_enable_disable(&func_block, fd, debug, SCMD_ENABLE);
344 						break;
345 					default:
346 						fprintf(stderr,"Unhandled subcommand to motor: %s %d\n\n", argv[2], validsub);
347 						usage();
348 						exit(1);
349 						break;
350 					}
351 				} else {
352 					fprintf(stderr,"Unknown subcommand to motor: %s\n\n", argv[2]);
353 					usage();
354 					exit(1);
355 				}
356 			} else {
357 				fprintf(stderr,"Missing arguments to motor command\n\n");
358 				usage();
359 				exit(1);
360 			}
361 			break;
362 		case SCMD_READ:
363 			memset(register_shadow,SCMD_HOLE_VALUE + 1,SCMD_REG_SIZE);
364 			if (argc >= 4) {
365 				module = (long)strtoi(argv[2], NULL, 10, 0, 16, &error);
366 				if (error)
367 					warnc(error, "Conversion of '%s' module failed,"
368 					    " using %ld", argv[2], module);
369 				reg = (uint8_t)strtoi(argv[3], NULL, 0, 0, 0x7e, &error);
370 				if (error) {
371 					for(c = 0x00; c < SCMD_REG_SIZE;c++)
372 						if (strncmp(argv[3],scmdregisternames[c],15) == 0)
373 							break;
374 					if (c == SCMD_REG_SIZE) {
375 						fprintf(stderr,"Bad conversion for read register start: %s\n", argv[3]);
376 						exit(1);
377 					}
378 					reg = c;
379 				}
380 				reg_e = reg;
381 				if (argc == 5) {
382 					reg_e = (uint8_t)strtoi(argv[4], NULL, 0, 0, 0x7e, &error);
383 					if (error) {
384 						for(c = 0x00; c < SCMD_REG_SIZE;c++)
385 							if (strncmp(argv[4],scmdregisternames[c],15) == 0)
386 								break;
387 						if (c == SCMD_REG_SIZE) {
388 							fprintf(stderr,"Bad conversion for read register end: %s\n", argv[4]);
389 							exit(1);
390 						}
391 						reg_e = c;
392 					}
393 				}
394 				if (reg_e < reg) {
395 					fprintf(stderr,"Register end can not be less than register start: %d %d\n\n", reg, reg_e);
396 					usage();
397 					exit(1);
398 				}
399 				if (dev_is_uart) {
400 					error = uart_read_register(fd,debug,module,reg,reg_e,&register_shadow[reg]);
401 				} else {
402 					error = i2cspi_read_register(fd,debug,module,reg,reg_e,&register_shadow[reg]);
403 				}
404 			} else {
405 				fprintf(stderr,"Missing arguments to read_register command\n\n");
406 				usage();
407 				exit(1);
408 			}
409 			break;
410 		case SCMD_WRITE:
411 			if (argc == 5) {
412 				module = (long)strtoi(argv[2], NULL, 10, 0, 16, &error);
413 				if (error)
414 					warnc(error, "Conversion of '%s' module failed,"
415 					    " using %ld", argv[2], module);
416 				reg = (uint8_t)strtoi(argv[3], NULL, 0, 0, 0x7e, &error);
417 				if (error) {
418 					for(c = 0x00; c < SCMD_REG_SIZE;c++)
419 						if (strncmp(argv[3],scmdregisternames[c],15) == 0)
420 							break;
421 					if (c == SCMD_REG_SIZE) {
422 						fprintf(stderr,"Bad conversion for write register start: %s\n", argv[3]);
423 						exit(1);
424 					}
425 					reg = c;
426 				}
427 				reg_value = (int8_t)strtoi(argv[4], NULL, 0, 0, 0xff, &error);
428 				if (error)
429 					err(EXIT_FAILURE,"Bad conversion for write register for reg_value: %s", argv[4]);
430 				if (dev_is_uart) {
431 					error = uart_write_register(fd,debug,module,reg,reg_value);
432 				} else {
433 					error = i2cspi_write_register(fd,debug,module,reg,reg_value);
434 				}
435 			} else {
436 				fprintf(stderr,"Missing arguments to write_register command\n\n");
437 				usage();
438 				exit(1);
439 			}
440 			break;
441 		case SCMD_RESTART:
442 		case SCMD_ENUMERATE:
443 			error = common_control_1(&func_block, fd, debug, scmdcmds[valid].id);
444 			break;
445 		case SCMD_UPDATERATE:
446 			if (argc >= 3) {
447 				validsub = valid_cmd(updateratesubcmds,__arraycount(updateratesubcmds),argv[2]);
448 				if (validsub != -1) {
449 					switch (updateratesubcmds[validsub].id) {
450 					case SCMD_SUBURGET:
451 						error = common_get_update_rate(&func_block, fd, debug, &ur);
452 						break;
453 					case SCMD_SUBURSET:
454 						if (argc == 4) {
455 							ur = (uint8_t)strtoi(argv[3], NULL, 0, 0, 0xff, &error);
456 							if (error)
457 								err(EXIT_FAILURE,"Bad conversion for update_rate: %s", argv[3]);
458 							error = common_set_update_rate(&func_block, fd, debug, ur);
459 						} else {
460 							fprintf(stderr,"Missing arguments to set update_rate command\n\n");
461 							usage();
462 							exit(1);
463 						}
464 						break;
465 					case SCMD_SUBURFORCE:
466 						error = common_force_update(&func_block, fd, debug);
467 						break;
468 					default:
469 						fprintf(stderr,"Unhandled subcommand to updaterate: %s %d\n\n", argv[2], validsub);
470 						usage();
471 						exit(1);
472 						break;
473 					}
474 				} else {
475 					fprintf(stderr,"Unknown subcommand to updaterate: %s\n\n", argv[2]);
476 					usage();
477 					exit(1);
478 				}
479 			} else {
480 				fprintf(stderr,"Missing arguments to update_rate command\n\n");
481 				usage();
482 				exit(1);
483 			}
484 			break;
485 		case SCMD_EBUS:
486 			if (argc >= 3) {
487 				validsub = valid_cmd(ebussubcmds,__arraycount(ebussubcmds),argv[2]);
488 				if (validsub != -1) {
489 					switch (ebussubcmds[validsub].id) {
490 					case SCMD_SUBEBUSGET:
491 						error = common_get_ebus_speed(&func_block, fd, debug, &ebus_s);
492 						break;
493 					case SCMD_SUBEBUSSET:
494 						if (argc == 4) {
495 							for(ebus_s = 0; ebus_s < __arraycount(ebus_speeds);ebus_s++)
496 								if (strncmp(argv[3],ebus_speeds[ebus_s],8) == 0)
497 									break;
498 							if (ebus_s == __arraycount(ebus_speeds)) {
499 								fprintf(stderr,"Bad conversion for set expansion bus speed: %s\n", argv[3]);
500 								exit(1);
501 							}
502 							error = common_set_ebus_speed(&func_block, fd, debug, ebus_s);
503 						} else {
504 							fprintf(stderr,"Missing arguments to set expansion_bus command\n\n");
505 							usage();
506 							exit(1);
507 						}
508 						break;
509 					default:
510 						fprintf(stderr,"Unhandled subcommand to expansion_bus: %s %d\n\n", argv[2], validsub);
511 						usage();
512 						exit(1);
513 						break;
514 					}
515 				} else {
516 					fprintf(stderr,"Unknown subcommand to expansion_bus: %s\n\n", argv[2]);
517 					usage();
518 					exit(1);
519 				}
520 			} else {
521 				fprintf(stderr,"Missing arguments to expansion_bus_speed command\n\n");
522 				usage();
523 				exit(1);
524 			}
525 			break;
526 		case SCMD_LOCK:
527 			if (argc == 4) {
528 				validsub = valid_cmd(locksubcmds,__arraycount(locksubcmds),argv[2]);
529 				if (validsub != -1) {
530 					lock_type = valid_cmd(lockcmdtypes,__arraycount(lockcmdtypes),argv[3]);
531 					if (lock_type == -1) {
532 						fprintf(stderr,"Unknown lock type: %s\n\n", argv[3]);
533 						usage();
534 						exit(1);
535 					}
536 					lock_type = lockcmdtypes[lock_type].id;
537 
538 					if (debug)
539 						fprintf(stderr,"Lock type in lock command: %d\n",lock_type);
540 
541 					switch (locksubcmds[validsub].id) {
542 					case SCMD_SUBLOCKGET:
543 						error = common_get_lock_state(&func_block, fd, debug, lock_type, &lock_state);
544 						break;
545 					case SCMD_SUBLOCKLOCK:
546 						error = common_set_lock_state(&func_block, fd, debug, lock_type, SCMD_LOCK_LOCKED);
547 						break;
548 					case SCMD_SUBLOCKUNLOCK:
549 						error = common_set_lock_state(&func_block, fd, debug, lock_type, SCMD_LOCK_UNLOCK);
550 						break;
551 					default:
552 						fprintf(stderr,"Unhandled subcommand to lock: %s %d\n\n", argv[2], validsub);
553 						usage();
554 						exit(1);
555 						break;
556 					}
557 				} else {
558 					fprintf(stderr,"Unknown subcommand to lock: %s\n\n", argv[2]);
559 					usage();
560 					exit(1);
561 				}
562 			} else {
563 				fprintf(stderr,"Missing arguments to lock command\n\n");
564 				usage();
565 				exit(1);
566 			}
567 			break;
568 		case SCMD_SPIREADONE:
569 			error = 0;
570 			if (dev_is_uart &&
571 			    uart_s == UART_IS_SPI_USERLAND) {
572 				error = uart_ul_spi_read_one(fd,debug);
573 			}
574 			break;
575 		default:
576 			fprintf(stderr,"Unknown handling of command: %d\n",valid);
577 			exit(2);
578 			break;
579 		}
580 
581 		if (! error) {
582 			switch (scmdcmds[valid].id) {
583 			case SCMD_IDENTIFY:
584 				print_identify(&ir);
585 				break;
586 			case SCMD_DIAG:
587 				print_diag(&diag);
588 				break;
589 			case SCMD_MOTOR:
590 				if (validsub != -1 &&
591 				    motorsubcmds[validsub].id == SCMD_SUBMOTORGET)
592 					print_motor(&motors);
593 				break;
594 			case SCMD_READ:
595 				for(int g = reg; g <= reg_e; g++)
596 					printf("Register %d (0x%02X) (%s): %d (0x%02X)\n",g,g,scmdregisternames[g],register_shadow[g],register_shadow[g]);
597 				break;
598 			case SCMD_UPDATERATE:
599 				if (validsub != -1 &&
600 				    updateratesubcmds[validsub].id == SCMD_SUBURGET)
601 					printf("Update rate: %dms\n",ur);
602 				break;
603 			case SCMD_EBUS:
604 				if (validsub != -1 &&
605 				    ebussubcmds[validsub].id == SCMD_SUBEBUSGET)
606 					printf("Expansion bus speed: %s (0x%02X)\n",(ebus_s <= 0x03) ? ebus_speeds[ebus_s] : "Unknown",ebus_s);
607 				break;
608 			case SCMD_LOCK:
609 				if (validsub != -1 &&
610 				    locksubcmds[validsub].id == SCMD_SUBLOCKGET) {
611 					int x = SCMD_MASTER_LOCK_UNLOCKED;
612 
613 					if (lock_type == SCMD_LOCAL_USER_LOCK ||
614 					    lock_type == SCMD_GLOBAL_USER_LOCK)
615 						x = SCMD_USER_LOCK_UNLOCKED;
616 					printf("Lock state: %s (0x%02X)\n",(lock_state == x ? "Unlocked" : "Locked"),lock_state);
617 				}
618 				break;
619 			case SCMD_WRITE:
620 			case SCMD_RESTART:
621 			case SCMD_ENUMERATE:
622 			case SCMD_SPIREADONE:
623 				break;
624 			default:
625 				fprintf(stderr,"Unknown printing of command: %d\n",valid);
626 				exit(2);
627 				break;
628 			}
629 		} else {
630 			fprintf(stderr,"Error: %d\n", error);
631 			exit(1);
632 		}
633 	} else {
634 		fprintf(stderr,"Unknown command: %s\n\n",argv[1]);
635 		usage();
636 		exit(1);
637 	}
638 
639 	close(fd);
640 	exit(0);
641 }
642