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