|
root / base / usr / src / cmd / i2cadm / i2cadm_device.c
i2cadm_device.c C 718 lines 17.2 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2025 Oxide Computer Company
 */

/*
 * i2cadm device related operations.
 */

#include <err.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <ofmt.h>

#include "i2cadm.h"

/*
 * Attempt to apply filters. We accept the following filters that are trying to
 * match devices:
 *
 *  - Matching a specific address
 *  - Matching a portion of a device path
 *  - Matching a specific driver or instance
 *  - Matching a node name
 *
 * These filters are shared between both device list and device addrs.
 */
static bool
i2cadm_device_filt(const i2c_dev_info_t *info, int nfilts, char **filts,
    bool *used)
{
	bool match = false;
	char inst[128] = { '\0' }, addr[64] = { '\0' };
	const char *name, *path, *driver;
	size_t pathlen;

	if (nfilts == 0) {
		return (true);
	}

	name = i2c_device_info_name(info);
	path = i2c_device_info_path(info);
	pathlen = strlen(path);
	driver = i2c_device_info_driver(info);
	if (i2c_device_info_instance(info) != -1 && driver != NULL) {
		(void) snprintf(inst, sizeof (inst), "%s%d", driver,
		    i2c_device_info_instance(info));
	}

	const i2c_addr_t *ia = i2c_device_info_addr_primary(info);
	if (!i2c_addr_to_string(i2cadm.i2c_hdl, ia, addr, sizeof (addr))) {
		addr[0] = '\0';
	}

	/*
	 * Note, we have to go through all the filters to see if they match as
	 * someone could have specified something more than once.
	 */
	for (int i = 0; i < nfilts; i++) {
		if (strcmp(filts[i], name) == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		if (addr[0] != '\0' && strcmp(filts[i], addr) == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		if (driver != NULL && strcmp(filts[i], driver) == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		if (inst[0] != '\0' && strcmp(filts[i], inst) == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		if (strcmp(path, filts[i]) == 0) {
			used[i] = true;
			match = true;
			continue;
		}

		size_t len = strlen(filts[i]);
		if (len < pathlen && strncmp(path, filts[i], len) == 0) {
			used[i] = true;
			match = true;
			continue;
		}
	}

	return (match);
}

static int
i2cadm_device_iter(ofmt_handle_t ofmt, int argc, char *argv[], bool *filts,
    void (*func)(ofmt_handle_t, const i2c_dev_info_t *))
{
	int ret = EXIT_SUCCESS;
	bool print = false;
	const i2c_dev_disc_t *disc;
	i2c_dev_iter_t *iter;
	i2c_iter_t iret;

	if (!i2c_device_discover_init(i2cadm.i2c_hdl, &iter)) {
		i2cadm_fatal("failed to initialize device discovery");
	}

	while ((iret = i2c_device_discover_step(iter, &disc)) ==
	    I2C_ITER_VALID) {
		i2c_dev_info_t *info;

		if (!i2c_device_info_snap(i2cadm.i2c_hdl,
		    i2c_device_disc_devi(disc), &info)) {
			i2cadm_warn("failed to get device information for "
			    "%s", i2c_device_disc_path(disc));
			ret = EXIT_FAILURE;
			continue;
		}

		if (!i2cadm_device_filt(info, argc, argv, filts)) {
			i2c_device_info_free(info);
			continue;
		}

		func(ofmt, info);
		i2c_device_info_free(info);
		print = true;
	}

	if (iret == I2C_ITER_ERROR) {
		i2cadm_warn("failed to discover devices");
		ret = EXIT_FAILURE;
	}

	for (int i = 0; i < argc; i++) {
		if (!filts[i]) {
			warnx("filter '%s' did not match any devices",
			    argv[i]);
			ret = EXIT_FAILURE;
		}
	}

	if (!print && argc == 0) {
		warnx("no I2C devices found");
		ret = EXIT_FAILURE;
	}

	free(filts);
	ofmt_close(ofmt);
	i2c_device_discover_fini(iter);

	return (ret);
}

static void
i2cadm_device_addrs_usage(FILE *f)
{
	(void) fprintf(f, "\ti2cadm device addrs [-H] [-o field,[...] [-p]] "
	    "[filter]\n");
}

static void
i2cadm_device_addrs_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
	}

	(void) fprintf(stderr, "Usage:  i2cadm device addrs [-H] "
	    "[-o field[,...] [-p]] [filters]\n\n");
	(void) fprintf(stderr, "List addresses assigned to devices and their "
	    "source. Each <filter> selects\ndevices based upon its address, "
	    "the device's name, the driver's name, the\ndriver's instance, or "
	    "the I2C path. Multiple filters are treated as an OR. It\n is an "
	    "error if a filter isn't used.\n\n"
	    "\t-H\t\tomit the column header\n"
	    "\t-o field\toutput fields to print\n"
	    "\t-p\t\tparseable output (requires -o)\n");
	(void) fprintf(stderr, "\nThe following fields are supported:\n"
	    "\tpath\t\tthe device path\n"
	    "\ttype\t\tthe address's type\n"
	    "\taddr\t\tthe specific address\n"
	    "\tsource\tindicates where the address came from\n");
}

typedef enum {
	I2CADM_DEVICE_ADDRS_PATH,
	I2CADM_DEVICE_ADDRS_TYPE,
	I2CADM_DEVICE_ADDRS_ADDR,
	I2CADM_DEVICE_ADDRS_SOURCE
} i2cadm_device_addrs_otype_t;

typedef struct i2cadm_device_addrs_ofmt {
	const i2c_dev_info_t *idoa_info;
	const i2c_addr_t *idoa_addr;
	i2c_addr_source_t idoa_source;
} i2cadm_device_addrs_ofmt_t;

static boolean_t
i2cadm_device_addrs_ofmt_cb(ofmt_arg_t *ofarg, char *buf, uint_t buflen)
{
	const i2cadm_device_addrs_ofmt_t *arg = ofarg->ofmt_cbarg;
	size_t len;

	switch (ofarg->ofmt_id) {
	case I2CADM_DEVICE_ADDRS_PATH:
		len = strlcpy(buf, i2c_device_info_path(arg->idoa_info),
		    buflen);
		break;
	case I2CADM_DEVICE_ADDRS_TYPE:
		if (arg->idoa_addr->ia_type == I2C_ADDR_7BIT) {
			len = strlcpy(buf, "7-bit", buflen);
		} else if (arg->idoa_addr->ia_type == I2C_ADDR_10BIT) {
			len = strlcpy(buf, "10-bit", buflen);
		} else {
			len = snprintf(buf, buflen, "unknown (0x%x)",
			    arg->idoa_addr->ia_type);
		}
		break;
	case I2CADM_DEVICE_ADDRS_ADDR:
		len = snprintf(buf, buflen, "0x%02x", arg->idoa_addr->ia_addr);
		break;
	case I2CADM_DEVICE_ADDRS_SOURCE:
		switch (arg->idoa_source) {
		case I2C_ADDR_SOURCE_REG:
			len = strlcpy(buf, "platform", buflen);
			break;
		case I2C_ADDR_SOURCE_CLAIMED:
			len = strlcpy(buf, "claimed", buflen);
			break;
		case I2C_ADDR_SOURCE_SHARED:
			len = strlcpy(buf, "shared", buflen);
			break;
		default:
			len = snprintf(buf, buflen, "unknown (0x%x)",
			    arg->idoa_source);
		}
		break;
	default:
		return (B_FALSE);
	}

	return (len < buflen);
}

static const char *i2cadm_device_addrs_fields = "path,type,addr,source";
static const ofmt_field_t i2cadm_device_addrs_ofmt[] = {
	{ "PATH", 40, I2CADM_DEVICE_ADDRS_PATH, i2cadm_device_addrs_ofmt_cb },
	{ "TYPE", 10, I2CADM_DEVICE_ADDRS_TYPE, i2cadm_device_addrs_ofmt_cb },
	{ "ADDR", 10, I2CADM_DEVICE_ADDRS_ADDR, i2cadm_device_addrs_ofmt_cb },
	{ "SOURCE", 16, I2CADM_DEVICE_ADDRS_SOURCE,
	    i2cadm_device_addrs_ofmt_cb },
	{ NULL, 0, 0, NULL }
};

static void
i2cadm_device_addrs_cb(ofmt_handle_t ofmt, const i2c_dev_info_t *info)
{
	for (uint32_t i = 0; i < i2c_device_info_naddrs(info); i++) {
		i2cadm_device_addrs_ofmt_t arg;

		arg.idoa_info = info;
		arg.idoa_addr = i2c_device_info_addr(info, i);
		arg.idoa_source = i2c_device_info_addr_source(info, i);
		ofmt_print(ofmt, &arg);
	}
}

static int
i2cadm_device_addrs(int argc, char *argv[])
{
	int c;
	uint_t flags = 0;
	boolean_t parse = B_FALSE;
	const char *fields = NULL;
	bool *filts = NULL;
	ofmt_status_t oferr;
	ofmt_handle_t ofmt;

	while ((c = getopt(argc, argv, ":Ho:p")) != -1) {
		switch (c) {
		case 'H':
			flags |= OFMT_NOHEADER;
			break;
		case 'o':
			fields = optarg;
			break;
		case 'p':
			parse = B_TRUE;
			flags |= OFMT_PARSABLE;
			break;
		case ':':
			i2cadm_device_addrs_help("option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
			i2cadm_device_addrs_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	if (parse && fields == NULL) {
		errx(EXIT_USAGE, "-p requires fields specified with -o");
	}

	if (!parse) {
		flags |= OFMT_WRAP;
	}

	if (fields == NULL) {
		fields = i2cadm_device_addrs_fields;
	}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		filts = calloc(argc, sizeof (bool));
		if (filts == NULL) {
			err(EXIT_FAILURE, "failed to allocate memory for "
			    "filter tracking");
		}
	}

	oferr = ofmt_open(fields, i2cadm_device_addrs_ofmt, flags, 0, &ofmt);
	ofmt_check(oferr, parse, ofmt, i2cadm_ofmt_errx, warnx);

	return (i2cadm_device_iter(ofmt, argc, argv, filts,
	    i2cadm_device_addrs_cb));
}

static void
i2cadm_device_list_usage(FILE *f)
{
	(void) fprintf(f, "\ti2cadm device list [-H] [-o field,[...] [-p]] "
	    "[filter]\n");
}

static void
i2cadm_device_list_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
	}

	(void) fprintf(stderr, "Usage:  i2cadm device list [-H] "
	    "[-o field[,...] [-p]] [filter...]\n\n");
	(void) fprintf(stderr, "List I2C devices in the system. Each <filter> "
	    "selects devices based upon its\naddress, the device's name, the "
	    "driver's name, the driver's instance, or the\nI2C path. Multiple "
	    "filters are treated as an OR. It is an error if a filter\nisn't "
	    "used.\n\n"
	    "\t-H\t\tomit the column header\n"
	    "\t-o field\toutput fields to print\n"
	    "\t-p\t\tparseable output (requires -o)\n");
	(void) fprintf(stderr, "\nThe following fields are supported:\n"
	    "\tname\t\tthe name of the device\n"
	    "\taddr\t\tthe primary address of the device\n"
	    "\tinstance\tthe driver instance of the device\n"
	    "\tpath\t\tthe I2C path of the device\n");
}

typedef enum {
	I2CADM_DEVICE_LIST_NAME,
	I2CADM_DEVICE_LIST_ADDR,
	I2CADM_DEVICE_LIST_INSTANCE,
	I2CADM_DEVICE_LIST_PATH
} i2cadm_device_list_otype_t;

static boolean_t
i2cadm_device_list_ofmt_cb(ofmt_arg_t *ofarg, char *buf, uint_t buflen)
{
	const i2c_dev_info_t *info = ofarg->ofmt_cbarg;
	size_t len;
	const i2c_addr_t *addr;

	switch (ofarg->ofmt_id) {
	case I2CADM_DEVICE_LIST_NAME:
		len = strlcpy(buf, i2c_device_info_name(info), buflen);
		break;
	case I2CADM_DEVICE_LIST_ADDR:
		addr = i2c_device_info_addr_primary(info);
		if (!i2c_addr_to_string(i2cadm.i2c_hdl, addr, buf, buflen)) {
			return (B_FALSE);
		}

		return (B_TRUE);
	case I2CADM_DEVICE_LIST_INSTANCE:
		if (i2c_device_info_driver(info) != NULL &&
		    i2c_device_info_instance(info) != -1) {
			len = snprintf(buf, buflen, "%s%d",
			    i2c_device_info_driver(info),
			    i2c_device_info_instance(info));
		} else {
			len = strlcpy(buf, "--", buflen);
		}
		break;
	case I2CADM_DEVICE_LIST_PATH:
		len = strlcpy(buf, i2c_device_info_path(info), buflen);
		break;
	default:
		return (B_FALSE);
	}

	return (len < buflen);
}

static const char *i2cadm_device_list_fields = "name,addr,instance,path";
static const ofmt_field_t i2cadm_device_list_ofmt[] = {
	{ "NAME", 12, I2CADM_DEVICE_LIST_NAME, i2cadm_device_list_ofmt_cb },
	{ "ADDR", 12, I2CADM_DEVICE_LIST_ADDR, i2cadm_device_list_ofmt_cb },
	{ "INSTANCE", 16, I2CADM_DEVICE_LIST_INSTANCE,
	    i2cadm_device_list_ofmt_cb },
	{ "PATH", 40, I2CADM_DEVICE_LIST_PATH, i2cadm_device_list_ofmt_cb },
	{ NULL, 0, 0, NULL }
};

static void
i2cadm_device_list_cb(ofmt_handle_t ofmt, const i2c_dev_info_t *info)
{
	ofmt_print(ofmt, (void *)info);
}

static int
i2cadm_device_list(int argc, char *argv[])
{
	int c;
	uint_t flags = 0;
	boolean_t parse = B_FALSE;
	const char *fields = NULL;
	bool *filts = NULL;
	ofmt_status_t oferr;
	ofmt_handle_t ofmt;

	while ((c = getopt(argc, argv, ":Ho:p")) != -1) {
		switch (c) {
		case 'H':
			flags |= OFMT_NOHEADER;
			break;
		case 'o':
			fields = optarg;
			break;
		case 'p':
			parse = B_TRUE;
			flags |= OFMT_PARSABLE;
			break;
		case ':':
			i2cadm_device_list_help("option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
			i2cadm_device_list_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	if (parse && fields == NULL) {
		errx(EXIT_USAGE, "-p requires fields specified with -o");
	}

	if (!parse) {
		flags |= OFMT_WRAP;
	}

	if (fields == NULL) {
		fields = i2cadm_device_list_fields;
	}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		filts = calloc(argc, sizeof (bool));
		if (filts == NULL) {
			err(EXIT_FAILURE, "failed to allocate memory for "
			    "filter tracking");
		}
	}

	oferr = ofmt_open(fields, i2cadm_device_list_ofmt, flags, 0, &ofmt);
	ofmt_check(oferr, parse, ofmt, i2cadm_ofmt_errx, warnx);

	return (i2cadm_device_iter(ofmt, argc, argv, filts,
	    i2cadm_device_list_cb));
}

static void
i2cadm_device_add_usage(FILE *f)
{
	(void) fprintf(f, "\ti2cadm device add [-c compat] port name addr\n");
}

static void
i2cadm_device_add_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
	}

	(void) fprintf(stderr, "Usage:  i2cadm device add [-c comapt] "
	    "port name addr\n\n");
	(void) fprintf(stderr, "Inform the system of a new I2C device with the "
	    "specified address. The device\nwill be inserted under the given "
	    "bus (or multiplexer port if specified). The\naddress must be "
	    "unique on its multiplexor (if applicable) and bus.\n\n"
	    "\t-c compat\tAdd the driver compatible entry to the device. This "
	    "may\n\t\t\tbe specified multiple times. They will be added to "
	    "the\n\t\t\tdevice in the order specified.\n");
}

static int
i2cadm_device_add(int argc, char *argv[])
{
	int c;
	char **compat = NULL;
	size_t ncompat = 0, nalloc = 0;
	i2c_port_t *port;
	i2c_addr_t addr;
	i2c_dev_add_req_t *req;

	while ((c = getopt(argc, argv, ":c")) != -1) {
		switch (c) {
		case 'c':
			if (ncompat == nalloc) {
				nalloc += 8;
				compat = recallocarray(compat, ncompat, nalloc,
				    sizeof (char *));
				if (compat == NULL) {
					err(EXIT_FAILURE, "failed to allocate "
					    "memory for %zu compatible array "
					    "entries", nalloc);
				}
			}
			compat[ncompat] = optarg;
			ncompat++;
			break;
		case ':':
			i2cadm_device_add_help("option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
			i2cadm_device_add_help("unknown option: -%c", optopt);
			exit(EXIT_USAGE);
		}
	}

	argv += optind;
	argc -= optind;

	if (argc > 3) {
		errx(EXIT_USAGE, "encountered extraneous arguments starting "
		    "with %s", argv[3]);
	} else if (argc == 0) {
		errx(EXIT_FAILURE, "missing required port path, device name, "
		    "and device address");
	} else if (argc == 1) {
		errx(EXIT_FAILURE, "missing required device name and device "
		    "address");
	} else if (argc == 2) {
		errx(EXIT_FAILURE, "missing required device address");
	}

	if (!i2c_port_init_by_path(i2cadm.i2c_hdl, argv[0], &port)) {
		i2cadm_fatal("failed to parse port path %s", argv[0]);
	}

	if (!i2c_addr_parse(i2cadm.i2c_hdl, argv[2], &addr)) {
		i2cadm_fatal("failed to parse address %s", argv[2]);
	}

	if (!i2c_device_add_req_init(port, &req)) {
		i2cadm_fatal("failed to initialize device add request");
	}

	if (!i2c_device_add_req_set_addr(req, &addr)) {
		i2cadm_fatal("failed to set device address");
	}

	if (!i2c_device_add_req_set_name(req, argv[1])) {
		i2cadm_fatal("failed to set device name");
	}

	if (ncompat > 0 && !i2c_device_add_req_set_compatible(req, compat,
	    ncompat)) {
		i2cadm_fatal("failed to set device compatible[]");
	}

	if (!i2c_device_add_req_exec(req)) {
		i2cadm_fatal("failed to add device");
	}

	i2c_device_add_req_fini(req);
	i2c_port_fini(port);
	free(compat);
	return (EXIT_SUCCESS);
}

static void
i2cadm_device_remove_usage(FILE *f)
{
	(void) fprintf(f, "\ti2cadm device remove <path>\n");
}

static void
i2cadm_device_remove_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
	}

	(void) fprintf(stderr, "Usage:  i2cadm device remove <path>\n\n");
	(void) fprintf(stderr, "Remove the I2C device identified by the "
	    "specified path. If the device is in use,\nthis may "
	    "fail.\n");
}

static int
i2cadm_device_remove(int argc, char *argv[])
{
	int c;
	i2c_port_t *port;
	i2c_dev_info_t *info;

	while ((c = getopt(argc, argv, "")) != -1) {
		switch (c) {
		case ':':
			i2cadm_device_remove_help("option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
			i2cadm_device_remove_help("unknown option: -%c",
			    optopt);
			exit(EXIT_USAGE);
		}
	}

	argv += optind;
	argc -= optind;

	if (argc > 1) {
		errx(EXIT_USAGE, "encountered extraneous arguments starting "
		    "with %s", argv[1]);
	} else if (argc == 0) {
		errx(EXIT_FAILURE, "missing required device path");
	}

	if (!i2c_port_dev_init_by_path(i2cadm.i2c_hdl, argv[0], false, &port,
	    &info)) {
		i2cadm_fatal("failed to parse device path %s", argv[0]);
	}

	if (!i2c_device_rem(port, i2c_device_info_addr_primary(info))) {
		i2cadm_fatal("failed to remove device %s", argv[0]);
	}

	i2c_device_info_free(info);
	i2c_port_fini(port);
	return (EXIT_SUCCESS);
}

static i2cadm_cmdtab_t i2cadm_device_cmds[] = {
	{ "list", i2cadm_device_list, i2cadm_device_list_usage },
	{ "addrs", i2cadm_device_addrs, i2cadm_device_addrs_usage },
	{ "add", i2cadm_device_add, i2cadm_device_add_usage },
	{ "remove", i2cadm_device_remove, i2cadm_device_remove_usage }
};

int
i2cadm_device(int argc, char *argv[])
{
	return (i2cadm_walk_tab(i2cadm_device_cmds,
	    ARRAY_SIZE(i2cadm_device_cmds), argc, argv));
}

void
i2cadm_device_usage(FILE *f)
{
	i2cadm_walk_usage(i2cadm_device_cmds, ARRAY_SIZE(i2cadm_device_cmds),
	    f);
}