xref: /titanic_53/usr/src/cmd/devfsadm/audio_link.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <regex.h>
30*7c478bd9Sstevel@tonic-gate #include <devfsadm.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <limits.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	MAX_AUDIO_LINK 100
37*7c478bd9Sstevel@tonic-gate #define	RE_SIZE 64
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate static void check_audio_link(char *secondary_link,
40*7c478bd9Sstevel@tonic-gate 				const char *primary_link_format);
41*7c478bd9Sstevel@tonic-gate static int audio_process(di_minor_t minor, di_node_t node);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static devfsadm_create_t audio_cbt[] = {
44*7c478bd9Sstevel@tonic-gate 	{ "audio", "ddi_audio", NULL,
45*7c478bd9Sstevel@tonic-gate 	TYPE_EXACT, ILEVEL_0, audio_process
46*7c478bd9Sstevel@tonic-gate 	}
47*7c478bd9Sstevel@tonic-gate };
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(audio_cbt);
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * the following can't be one big RE with a bunch of alterations "|"
53*7c478bd9Sstevel@tonic-gate  * because recurse_dev_re() would not work.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate static devfsadm_remove_t audio_remove_cbt[] = {
56*7c478bd9Sstevel@tonic-gate 	{ "audio",
57*7c478bd9Sstevel@tonic-gate 	"^audio(ctl)?$",
58*7c478bd9Sstevel@tonic-gate 	RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_link
59*7c478bd9Sstevel@tonic-gate 	},
60*7c478bd9Sstevel@tonic-gate 	{ "audio",
61*7c478bd9Sstevel@tonic-gate 	"^sound/[0-9]+.*$",
62*7c478bd9Sstevel@tonic-gate 	RM_PRE|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
63*7c478bd9Sstevel@tonic-gate 	},
64*7c478bd9Sstevel@tonic-gate 	{ "audio",
65*7c478bd9Sstevel@tonic-gate 	"^isdn/[0-9]+/mgt$",
66*7c478bd9Sstevel@tonic-gate 	RM_PRE, ILEVEL_0, devfsadm_rm_all
67*7c478bd9Sstevel@tonic-gate 	},
68*7c478bd9Sstevel@tonic-gate 	{ "audio",
69*7c478bd9Sstevel@tonic-gate 	"^isdn/[0-9]+/aux/0(ctl)?$",
70*7c478bd9Sstevel@tonic-gate 	RM_PRE, ILEVEL_0, devfsadm_rm_all
71*7c478bd9Sstevel@tonic-gate 	},
72*7c478bd9Sstevel@tonic-gate 	{ "audio",
73*7c478bd9Sstevel@tonic-gate 	"^isdn/[0-9]+/(nt)|(te)/((dtrace)|(mgt)|(b1)|(b2)|(d))$",
74*7c478bd9Sstevel@tonic-gate 	RM_PRE, ILEVEL_0, devfsadm_rm_all
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate };
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(audio_remove_cbt);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate static regex_t isdn_re;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate #define	ISDN_RE "((nt)|(te)|(aux))\\,((0)|(0ctl)|(d)|(b1)|(b2)|(mgt)|(dtrace))"
83*7c478bd9Sstevel@tonic-gate #define	F 1
84*7c478bd9Sstevel@tonic-gate #define	S 5
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate int
87*7c478bd9Sstevel@tonic-gate minor_init(void)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	if (0 != regcomp(&isdn_re, ISDN_RE, REG_EXTENDED)) {
90*7c478bd9Sstevel@tonic-gate 		devfsadm_errprint("SUNW_audio_link: minor_init: regular "
91*7c478bd9Sstevel@tonic-gate 				    "expression bad: '%s'\n", ISDN_RE);
92*7c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
93*7c478bd9Sstevel@tonic-gate 	} else {
94*7c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate int
99*7c478bd9Sstevel@tonic-gate minor_fini(void)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	regfree(&isdn_re);
102*7c478bd9Sstevel@tonic-gate 	check_audio_link("audio", "sound/%d");
103*7c478bd9Sstevel@tonic-gate 	check_audio_link("audioctl", "sound/%dctl");
104*7c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate #define	COPYSUB(to, from, pm, pos) (void) strncpy(to, &from[pm[pos].rm_so], \
109*7c478bd9Sstevel@tonic-gate 		    pm[pos].rm_eo - pm[pos].rm_so); \
110*7c478bd9Sstevel@tonic-gate 		    to[pm[pos].rm_eo - pm[pos].rm_so] = 0;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * This function is called for every audio node.
114*7c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical unit id, and then
115*7c478bd9Sstevel@tonic-gate  * devfsadm_mklink to make the link.
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate static int
118*7c478bd9Sstevel@tonic-gate audio_process(di_minor_t minor, di_node_t node)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
121*7c478bd9Sstevel@tonic-gate 	char *buf;
122*7c478bd9Sstevel@tonic-gate 	char *mn;
123*7c478bd9Sstevel@tonic-gate 	char m1[10];
124*7c478bd9Sstevel@tonic-gate 	char m2[10];
125*7c478bd9Sstevel@tonic-gate 	char *devfspath;
126*7c478bd9Sstevel@tonic-gate 	char re_string[RE_SIZE+1];
127*7c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {NULL};
128*7c478bd9Sstevel@tonic-gate 	regmatch_t pmatch[12];
129*7c478bd9Sstevel@tonic-gate 	char *au_mn;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
135*7c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
138*7c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
139*7c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
140*7c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if (strstr(mn, "sound,") != NULL) {
143*7c478bd9Sstevel@tonic-gate 		(void) snprintf(re_string, RE_SIZE, "%s", "^sound$/^([0-9]+)");
144*7c478bd9Sstevel@tonic-gate 	} else {
145*7c478bd9Sstevel@tonic-gate 		(void) strcpy(re_string, "isdn/([0-9]+)");
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	/*
149*7c478bd9Sstevel@tonic-gate 	 * We want a match against the physical path
150*7c478bd9Sstevel@tonic-gate 	 * without the minor name component.
151*7c478bd9Sstevel@tonic-gate 	 */
152*7c478bd9Sstevel@tonic-gate 	rules[0].re = re_string;
153*7c478bd9Sstevel@tonic-gate 	rules[0].subexp = 1;
154*7c478bd9Sstevel@tonic-gate 	rules[0].flags = MATCH_ADDR;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/*
157*7c478bd9Sstevel@tonic-gate 	 * enumerate finds the logical audio id, and stuffs
158*7c478bd9Sstevel@tonic-gate 	 * it in buf
159*7c478bd9Sstevel@tonic-gate 	 */
160*7c478bd9Sstevel@tonic-gate 	if (devfsadm_enumerate_int(path, 0, &buf, rules, 1)) {
161*7c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	path[0] = '\0';
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if (strstr(mn, "sound,") != NULL) {
167*7c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "sound/");
168*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, buf);
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		/* if this is a minor node, tack on the correct suffix */
171*7c478bd9Sstevel@tonic-gate 		au_mn = strchr(mn, ',');
172*7c478bd9Sstevel@tonic-gate 		if (strcmp(++au_mn, "audio") != 0 &&
173*7c478bd9Sstevel@tonic-gate 		    strcmp(au_mn, "sbpro") != 0) {
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 			/*
176*7c478bd9Sstevel@tonic-gate 			 * audioctl/sbproctl are special cases. They are handled
177*7c478bd9Sstevel@tonic-gate 			 * by stripping off the audio/sbpro from the node name
178*7c478bd9Sstevel@tonic-gate 			 */
179*7c478bd9Sstevel@tonic-gate 			if (strcmp(au_mn, "audioctl") == 0 ||
180*7c478bd9Sstevel@tonic-gate 			    strcmp(au_mn, "sbproctl") == 0)
181*7c478bd9Sstevel@tonic-gate 				au_mn = strstr(au_mn, "ctl");
182*7c478bd9Sstevel@tonic-gate 			(void) strcat(path, au_mn);
183*7c478bd9Sstevel@tonic-gate 		}
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	if (regexec(&isdn_re, mn, sizeof (pmatch) / sizeof (pmatch[0]),
187*7c478bd9Sstevel@tonic-gate 	    pmatch, 0) == 0) {
188*7c478bd9Sstevel@tonic-gate 		COPYSUB(m1, mn, pmatch, F);
189*7c478bd9Sstevel@tonic-gate 		COPYSUB(m2, mn, pmatch, S);
190*7c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "isdn/");
191*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, buf);
192*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/");
193*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, m1);
194*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/");
195*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, m2);
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if (strstr("mgt,mgt", mn) != NULL) {
199*7c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "isdn/");
200*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, buf);
201*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/mgt");
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	free(buf);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if (path[0] == '\0') {
207*7c478bd9Sstevel@tonic-gate 		devfsadm_errprint("SUNW_audio_link: audio_process: can't find"
208*7c478bd9Sstevel@tonic-gate 			" match for'%s'\n", mn);
209*7c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	(void) devfsadm_mklink(path, node, minor, 0);
213*7c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate static void
218*7c478bd9Sstevel@tonic-gate check_audio_link(char *secondary_link, const char *primary_link_format)
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate 	char primary_link[PATH_MAX + 1];
221*7c478bd9Sstevel@tonic-gate 	int i;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	/* if link is present, return */
224*7c478bd9Sstevel@tonic-gate 	if (devfsadm_link_valid(secondary_link) == DEVFSADM_TRUE) {
225*7c478bd9Sstevel@tonic-gate 		return;
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_AUDIO_LINK; i++) {
229*7c478bd9Sstevel@tonic-gate 		(void) sprintf(primary_link, primary_link_format, i);
230*7c478bd9Sstevel@tonic-gate 		if (devfsadm_link_valid(primary_link) == DEVFSADM_TRUE) {
231*7c478bd9Sstevel@tonic-gate 			(void) devfsadm_secondary_link(secondary_link,
232*7c478bd9Sstevel@tonic-gate 						primary_link, 0);
233*7c478bd9Sstevel@tonic-gate 			break;
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate }
237