xref: /illumos-gate/usr/src/cmd/devfsadm/dpio_link.c (revision fd71220ba0fafcc9cf5ea0785db206f3f31336e7)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 Oxide Computer Company
14  */
15 
16 /*
17  * Our goal is to create the /dev/dpio/%s links that need to exist for each
18  * dpio(7) related entry. The kgpio driver creates a minor node for each dpio of
19  * the form 'dpio:<name>' and uses the type DDI_NT_GPIO_DPIO. For exmaple, the
20  * name 'dpio:foobar' would cause us to create /dev/dpio/foobar.
21  */
22 
23 #include <devfsadm.h>
24 #include <string.h>
25 
26 static int
dpio_link(di_minor_t minor,di_node_t node)27 dpio_link(di_minor_t minor, di_node_t node)
28 {
29 	const char *name, *colon;
30 	char buf[PATH_MAX];
31 
32 	name = di_minor_name(minor);
33 	if (name == NULL) {
34 		return (DEVFSADM_CONTINUE);
35 	}
36 
37 	colon = strchr(name, ':');
38 	if (colon == NULL) {
39 		return (DEVFSADM_CONTINUE);
40 	}
41 
42 	if (snprintf(buf, sizeof (buf), "dpio/%s", colon + 1) < sizeof (buf)) {
43 		(void) devfsadm_mklink(buf, node, minor, 0);
44 	}
45 
46 	return (DEVFSADM_CONTINUE);
47 }
48 
49 static devfsadm_create_t dpio_create_cbt[] = {
50 	{ "pseudo", DDI_NT_GPIO_DPIO, "kgpio", TYPE_EXACT | DRV_EXACT, ILEVEL_0,
51 	    dpio_link }
52 };
53 
54 static devfsadm_remove_t dpio_remove_cbt[] = {
55 	{ "pseudo", "^dpio/[A-Za-z0-9]+$", RM_POST | RM_HOT | RM_ALWAYS,
56 	    ILEVEL_0, devfsadm_rm_all },
57 };
58 
59 DEVFSADM_CREATE_INIT_V0(dpio_create_cbt);
60 DEVFSADM_REMOVE_INIT_V0(dpio_remove_cbt);
61