xref: /titanic_44/usr/src/cmd/devfsadm/audio_link.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <regex.h>
297c478bd9Sstevel@tonic-gate #include <devfsadm.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
34*45916cd2Sjpk #include <bsm/devalloc.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	MAX_AUDIO_LINK 100
377c478bd9Sstevel@tonic-gate #define	RE_SIZE 64
387c478bd9Sstevel@tonic-gate 
39*45916cd2Sjpk extern int system_labeled;
40*45916cd2Sjpk 
417c478bd9Sstevel@tonic-gate static void check_audio_link(char *secondary_link,
427c478bd9Sstevel@tonic-gate 				const char *primary_link_format);
437c478bd9Sstevel@tonic-gate static int audio_process(di_minor_t minor, di_node_t node);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static devfsadm_create_t audio_cbt[] = {
467c478bd9Sstevel@tonic-gate 	{ "audio", "ddi_audio", NULL,
477c478bd9Sstevel@tonic-gate 	TYPE_EXACT, ILEVEL_0, audio_process
487c478bd9Sstevel@tonic-gate 	}
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(audio_cbt);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * the following can't be one big RE with a bunch of alterations "|"
557c478bd9Sstevel@tonic-gate  * because recurse_dev_re() would not work.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static devfsadm_remove_t audio_remove_cbt[] = {
587c478bd9Sstevel@tonic-gate 	{ "audio",
597c478bd9Sstevel@tonic-gate 	"^audio(ctl)?$",
607c478bd9Sstevel@tonic-gate 	RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_link
617c478bd9Sstevel@tonic-gate 	},
627c478bd9Sstevel@tonic-gate 	{ "audio",
637c478bd9Sstevel@tonic-gate 	"^sound/[0-9]+.*$",
647c478bd9Sstevel@tonic-gate 	RM_PRE|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
657c478bd9Sstevel@tonic-gate 	},
667c478bd9Sstevel@tonic-gate 	{ "audio",
677c478bd9Sstevel@tonic-gate 	"^isdn/[0-9]+/mgt$",
687c478bd9Sstevel@tonic-gate 	RM_PRE, ILEVEL_0, devfsadm_rm_all
697c478bd9Sstevel@tonic-gate 	},
707c478bd9Sstevel@tonic-gate 	{ "audio",
717c478bd9Sstevel@tonic-gate 	"^isdn/[0-9]+/aux/0(ctl)?$",
727c478bd9Sstevel@tonic-gate 	RM_PRE, ILEVEL_0, devfsadm_rm_all
737c478bd9Sstevel@tonic-gate 	},
747c478bd9Sstevel@tonic-gate 	{ "audio",
757c478bd9Sstevel@tonic-gate 	"^isdn/[0-9]+/(nt)|(te)/((dtrace)|(mgt)|(b1)|(b2)|(d))$",
767c478bd9Sstevel@tonic-gate 	RM_PRE, ILEVEL_0, devfsadm_rm_all
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(audio_remove_cbt);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static regex_t isdn_re;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	ISDN_RE "((nt)|(te)|(aux))\\,((0)|(0ctl)|(d)|(b1)|(b2)|(mgt)|(dtrace))"
857c478bd9Sstevel@tonic-gate #define	F 1
867c478bd9Sstevel@tonic-gate #define	S 5
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int
897c478bd9Sstevel@tonic-gate minor_init(void)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	if (0 != regcomp(&isdn_re, ISDN_RE, REG_EXTENDED)) {
927c478bd9Sstevel@tonic-gate 		devfsadm_errprint("SUNW_audio_link: minor_init: regular "
937c478bd9Sstevel@tonic-gate 				    "expression bad: '%s'\n", ISDN_RE);
947c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
957c478bd9Sstevel@tonic-gate 	} else {
967c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate int
1017c478bd9Sstevel@tonic-gate minor_fini(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	regfree(&isdn_re);
1047c478bd9Sstevel@tonic-gate 	check_audio_link("audio", "sound/%d");
1057c478bd9Sstevel@tonic-gate 	check_audio_link("audioctl", "sound/%dctl");
1067c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	COPYSUB(to, from, pm, pos) (void) strncpy(to, &from[pm[pos].rm_so], \
1117c478bd9Sstevel@tonic-gate 		    pm[pos].rm_eo - pm[pos].rm_so); \
1127c478bd9Sstevel@tonic-gate 		    to[pm[pos].rm_eo - pm[pos].rm_so] = 0;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * This function is called for every audio node.
1167c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical unit id, and then
1177c478bd9Sstevel@tonic-gate  * devfsadm_mklink to make the link.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static int
1207c478bd9Sstevel@tonic-gate audio_process(di_minor_t minor, di_node_t node)
1217c478bd9Sstevel@tonic-gate {
122*45916cd2Sjpk 	int flags = 0;
1237c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
1247c478bd9Sstevel@tonic-gate 	char *buf;
1257c478bd9Sstevel@tonic-gate 	char *mn;
1267c478bd9Sstevel@tonic-gate 	char m1[10];
1277c478bd9Sstevel@tonic-gate 	char m2[10];
1287c478bd9Sstevel@tonic-gate 	char *devfspath;
1297c478bd9Sstevel@tonic-gate 	char re_string[RE_SIZE+1];
1307c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {NULL};
1317c478bd9Sstevel@tonic-gate 	regmatch_t pmatch[12];
1327c478bd9Sstevel@tonic-gate 	char *au_mn;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
1387c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
1417c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
1427c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
1437c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (strstr(mn, "sound,") != NULL) {
1467c478bd9Sstevel@tonic-gate 		(void) snprintf(re_string, RE_SIZE, "%s", "^sound$/^([0-9]+)");
1477c478bd9Sstevel@tonic-gate 	} else {
1487c478bd9Sstevel@tonic-gate 		(void) strcpy(re_string, "isdn/([0-9]+)");
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * We want a match against the physical path
1537c478bd9Sstevel@tonic-gate 	 * without the minor name component.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	rules[0].re = re_string;
1567c478bd9Sstevel@tonic-gate 	rules[0].subexp = 1;
1577c478bd9Sstevel@tonic-gate 	rules[0].flags = MATCH_ADDR;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * enumerate finds the logical audio id, and stuffs
1617c478bd9Sstevel@tonic-gate 	 * it in buf
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	if (devfsadm_enumerate_int(path, 0, &buf, rules, 1)) {
1647c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	path[0] = '\0';
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (strstr(mn, "sound,") != NULL) {
1707c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "sound/");
1717c478bd9Sstevel@tonic-gate 		(void) strcat(path, buf);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		/* if this is a minor node, tack on the correct suffix */
1747c478bd9Sstevel@tonic-gate 		au_mn = strchr(mn, ',');
1757c478bd9Sstevel@tonic-gate 		if (strcmp(++au_mn, "audio") != 0 &&
1767c478bd9Sstevel@tonic-gate 		    strcmp(au_mn, "sbpro") != 0) {
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			/*
1797c478bd9Sstevel@tonic-gate 			 * audioctl/sbproctl are special cases. They are handled
1807c478bd9Sstevel@tonic-gate 			 * by stripping off the audio/sbpro from the node name
1817c478bd9Sstevel@tonic-gate 			 */
1827c478bd9Sstevel@tonic-gate 			if (strcmp(au_mn, "audioctl") == 0 ||
1837c478bd9Sstevel@tonic-gate 			    strcmp(au_mn, "sbproctl") == 0)
1847c478bd9Sstevel@tonic-gate 				au_mn = strstr(au_mn, "ctl");
1857c478bd9Sstevel@tonic-gate 			(void) strcat(path, au_mn);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (regexec(&isdn_re, mn, sizeof (pmatch) / sizeof (pmatch[0]),
1907c478bd9Sstevel@tonic-gate 	    pmatch, 0) == 0) {
1917c478bd9Sstevel@tonic-gate 		COPYSUB(m1, mn, pmatch, F);
1927c478bd9Sstevel@tonic-gate 		COPYSUB(m2, mn, pmatch, S);
1937c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "isdn/");
1947c478bd9Sstevel@tonic-gate 		(void) strcat(path, buf);
1957c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/");
1967c478bd9Sstevel@tonic-gate 		(void) strcat(path, m1);
1977c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/");
1987c478bd9Sstevel@tonic-gate 		(void) strcat(path, m2);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (strstr("mgt,mgt", mn) != NULL) {
2027c478bd9Sstevel@tonic-gate 		(void) strcpy(path, "isdn/");
2037c478bd9Sstevel@tonic-gate 		(void) strcat(path, buf);
2047c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/mgt");
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	free(buf);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (path[0] == '\0') {
2107c478bd9Sstevel@tonic-gate 		devfsadm_errprint("SUNW_audio_link: audio_process: can't find"
2117c478bd9Sstevel@tonic-gate 			" match for'%s'\n", mn);
2127c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
215*45916cd2Sjpk 	if (system_labeled)
216*45916cd2Sjpk 		flags = DA_ADD|DA_AUDIO;
217*45916cd2Sjpk 
218*45916cd2Sjpk 	(void) devfsadm_mklink(path, node, minor, flags);
2197c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static void
2247c478bd9Sstevel@tonic-gate check_audio_link(char *secondary_link, const char *primary_link_format)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	char primary_link[PATH_MAX + 1];
2277c478bd9Sstevel@tonic-gate 	int i;
228*45916cd2Sjpk 	int flags = 0;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* if link is present, return */
2317c478bd9Sstevel@tonic-gate 	if (devfsadm_link_valid(secondary_link) == DEVFSADM_TRUE) {
2327c478bd9Sstevel@tonic-gate 		return;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
235*45916cd2Sjpk 	if (system_labeled)
236*45916cd2Sjpk 		flags = DA_ADD|DA_AUDIO;
237*45916cd2Sjpk 
2387c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_AUDIO_LINK; i++) {
2397c478bd9Sstevel@tonic-gate 		(void) sprintf(primary_link, primary_link_format, i);
2407c478bd9Sstevel@tonic-gate 		if (devfsadm_link_valid(primary_link) == DEVFSADM_TRUE) {
2417c478bd9Sstevel@tonic-gate 			(void) devfsadm_secondary_link(secondary_link,
242*45916cd2Sjpk 						primary_link, flags);
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate }
247