10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51676Sjpk * Common Development and Distribution License (the "License").
61676Sjpk * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12116SVikram.Hegde@Sun.COM * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <regex.h>
260Sstevel@tonic-gate #include <devfsadm.h>
270Sstevel@tonic-gate #include <strings.h>
289484Sgarrett.damore@Sun.COM #include <ctype.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <limits.h>
310Sstevel@tonic-gate #include <stdio.h>
329484Sgarrett.damore@Sun.COM #include <syslog.h>
331676Sjpk #include <bsm/devalloc.h>
349484Sgarrett.damore@Sun.COM #include <sys/audio.h>
359484Sgarrett.damore@Sun.COM #include <sys/soundcard.h>
369484Sgarrett.damore@Sun.COM #include <unistd.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #define MAX_AUDIO_LINK 100
390Sstevel@tonic-gate #define RE_SIZE 64
400Sstevel@tonic-gate
411676Sjpk extern int system_labeled;
421676Sjpk
43*12116SVikram.Hegde@Sun.COM static void check_audio_link(di_node_t anynode, char *secondary_link,
449484Sgarrett.damore@Sun.COM const char *primary_link_format);
459484Sgarrett.damore@Sun.COM
460Sstevel@tonic-gate static int audio_process(di_minor_t minor, di_node_t node);
479484Sgarrett.damore@Sun.COM static int sndstat_process(di_minor_t minor, di_node_t node);
480Sstevel@tonic-gate
490Sstevel@tonic-gate static devfsadm_create_t audio_cbt[] = {
500Sstevel@tonic-gate { "audio", "ddi_audio", NULL,
519484Sgarrett.damore@Sun.COM TYPE_EXACT, ILEVEL_0, audio_process
529484Sgarrett.damore@Sun.COM },
539484Sgarrett.damore@Sun.COM { "pseudo", "ddi_pseudo", "audio",
549484Sgarrett.damore@Sun.COM TYPE_EXACT|DRV_EXACT, ILEVEL_0, sndstat_process
559484Sgarrett.damore@Sun.COM },
560Sstevel@tonic-gate };
570Sstevel@tonic-gate
580Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(audio_cbt);
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * the following can't be one big RE with a bunch of alterations "|"
620Sstevel@tonic-gate * because recurse_dev_re() would not work.
630Sstevel@tonic-gate */
640Sstevel@tonic-gate static devfsadm_remove_t audio_remove_cbt[] = {
659484Sgarrett.damore@Sun.COM /*
669484Sgarrett.damore@Sun.COM * Secondary links.
679484Sgarrett.damore@Sun.COM */
689484Sgarrett.damore@Sun.COM
699484Sgarrett.damore@Sun.COM /* /dev/audio, /dev/audioctl, /dev/dsp */
7010586SGarrett.Damore@Sun.COM { "audio", "^audio$",
7110586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
720Sstevel@tonic-gate },
7310586SGarrett.Damore@Sun.COM { "audio", "^audioctl$",
7410586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
750Sstevel@tonic-gate },
7610586SGarrett.Damore@Sun.COM { "audio", "^dsp$",
7710586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
780Sstevel@tonic-gate },
7910586SGarrett.Damore@Sun.COM { "audio", "^mixer",
8010586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
8110586SGarrett.Damore@Sun.COM },
8210586SGarrett.Damore@Sun.COM { "audio", "^sndstat$",
839484Sgarrett.damore@Sun.COM RM_PRE|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
840Sstevel@tonic-gate },
8510586SGarrett.Damore@Sun.COM { "audio", "^mixer[0-9]+$",
8610586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
8710586SGarrett.Damore@Sun.COM },
8810586SGarrett.Damore@Sun.COM { "audio", "^dsp[0-9]+$",
8910586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
9010586SGarrett.Damore@Sun.COM },
9110586SGarrett.Damore@Sun.COM { "audio", "^sound/[0-9]+$",
9210586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
9310586SGarrett.Damore@Sun.COM },
9410586SGarrett.Damore@Sun.COM { "audio", "^sound/[0-9]+ctl$",
9510586SGarrett.Damore@Sun.COM RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
969484Sgarrett.damore@Sun.COM },
970Sstevel@tonic-gate };
980Sstevel@tonic-gate
990Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(audio_remove_cbt);
1000Sstevel@tonic-gate
101*12116SVikram.Hegde@Sun.COM static di_node_t anynode;
102*12116SVikram.Hegde@Sun.COM
103*12116SVikram.Hegde@Sun.COM int
minor_init(void)104*12116SVikram.Hegde@Sun.COM minor_init(void)
105*12116SVikram.Hegde@Sun.COM {
106*12116SVikram.Hegde@Sun.COM anynode = DI_NODE_NIL;
107*12116SVikram.Hegde@Sun.COM return (DEVFSADM_SUCCESS);
108*12116SVikram.Hegde@Sun.COM }
109*12116SVikram.Hegde@Sun.COM
1100Sstevel@tonic-gate int
minor_fini(void)1110Sstevel@tonic-gate minor_fini(void)
1120Sstevel@tonic-gate {
113*12116SVikram.Hegde@Sun.COM check_audio_link(anynode, "audio", "sound/%d");
114*12116SVikram.Hegde@Sun.COM check_audio_link(anynode, "audioctl", "sound/%dctl");
115*12116SVikram.Hegde@Sun.COM check_audio_link(anynode, "dsp", "dsp%d");
116*12116SVikram.Hegde@Sun.COM anynode = DI_NODE_NIL;
1170Sstevel@tonic-gate return (DEVFSADM_SUCCESS);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate #define COPYSUB(to, from, pm, pos) (void) strncpy(to, &from[pm[pos].rm_so], \
1220Sstevel@tonic-gate pm[pos].rm_eo - pm[pos].rm_so); \
1230Sstevel@tonic-gate to[pm[pos].rm_eo - pm[pos].rm_so] = 0;
1240Sstevel@tonic-gate
1259484Sgarrett.damore@Sun.COM static void
send_number(long num)1269484Sgarrett.damore@Sun.COM send_number(long num)
1279484Sgarrett.damore@Sun.COM {
1289484Sgarrett.damore@Sun.COM char buf[PATH_MAX+1];
1299484Sgarrett.damore@Sun.COM
1309484Sgarrett.damore@Sun.COM /*
1319484Sgarrett.damore@Sun.COM * This is not safe with -r.
1329484Sgarrett.damore@Sun.COM */
1339484Sgarrett.damore@Sun.COM if (strcmp(devfsadm_root_path(), "/") != 0)
1349484Sgarrett.damore@Sun.COM return;
1359484Sgarrett.damore@Sun.COM
1369484Sgarrett.damore@Sun.COM (void) snprintf(buf, sizeof (buf), "/dev/mixer%ld", num);
1379484Sgarrett.damore@Sun.COM if (device_exists(buf)) {
1389484Sgarrett.damore@Sun.COM int fd;
1399484Sgarrett.damore@Sun.COM
1409484Sgarrett.damore@Sun.COM if ((fd = open(buf, O_RDWR)) < 0)
1419484Sgarrett.damore@Sun.COM return;
1429484Sgarrett.damore@Sun.COM
1439484Sgarrett.damore@Sun.COM (void) ioctl(fd, SNDCTL_SUN_SEND_NUMBER, &num);
1449484Sgarrett.damore@Sun.COM (void) close(fd);
1459484Sgarrett.damore@Sun.COM devfsadm_print(CHATTY_MID,
1469484Sgarrett.damore@Sun.COM "sent devnum audio %ld to %s\n", num, buf);
1479484Sgarrett.damore@Sun.COM }
1489484Sgarrett.damore@Sun.COM }
1499484Sgarrett.damore@Sun.COM
1509484Sgarrett.damore@Sun.COM static int
sndstat_process(di_minor_t minor,di_node_t node)1519484Sgarrett.damore@Sun.COM sndstat_process(di_minor_t minor, di_node_t node)
1529484Sgarrett.damore@Sun.COM {
1539484Sgarrett.damore@Sun.COM char *mn;
1549484Sgarrett.damore@Sun.COM
1559484Sgarrett.damore@Sun.COM mn = di_minor_name(minor);
156*12116SVikram.Hegde@Sun.COM anynode = node;
1579484Sgarrett.damore@Sun.COM
1589484Sgarrett.damore@Sun.COM /*
1599484Sgarrett.damore@Sun.COM * "Special" handling for /dev/sndstat and /dev/mixer.
1609484Sgarrett.damore@Sun.COM */
1619484Sgarrett.damore@Sun.COM if (strcmp(mn, "sound,sndstat0") == 0) {
1629484Sgarrett.damore@Sun.COM (void) devfsadm_mklink("sndstat", node, minor, 0);
1639484Sgarrett.damore@Sun.COM (void) devfsadm_secondary_link("mixer", "sndstat", 0);
1649484Sgarrett.damore@Sun.COM }
1659484Sgarrett.damore@Sun.COM
1669484Sgarrett.damore@Sun.COM return (DEVFSADM_CONTINUE);
1679484Sgarrett.damore@Sun.COM }
1689484Sgarrett.damore@Sun.COM
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * This function is called for every audio node.
1710Sstevel@tonic-gate * Calls enumerate to assign a logical unit id, and then
1720Sstevel@tonic-gate * devfsadm_mklink to make the link.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate static int
audio_process(di_minor_t minor,di_node_t node)1750Sstevel@tonic-gate audio_process(di_minor_t minor, di_node_t node)
1760Sstevel@tonic-gate {
1771676Sjpk int flags = 0;
1789484Sgarrett.damore@Sun.COM char devpath[PATH_MAX + 1];
1799484Sgarrett.damore@Sun.COM char newpath[PATH_MAX + 1];
1800Sstevel@tonic-gate char *buf;
1810Sstevel@tonic-gate char *mn;
1829484Sgarrett.damore@Sun.COM char *tmp;
1839484Sgarrett.damore@Sun.COM char *ep;
1840Sstevel@tonic-gate char re_string[RE_SIZE+1];
1850Sstevel@tonic-gate devfsadm_enumerate_t rules[1] = {NULL};
18611253Sgdamore@opensolaris.org char base[PATH_MAX + 1];
18711253Sgdamore@opensolaris.org char linksrc[PATH_MAX + 1];
18811253Sgdamore@opensolaris.org char linkdst[PATH_MAX + 1];
18911253Sgdamore@opensolaris.org long num;
19011253Sgdamore@opensolaris.org long inst;
19111253Sgdamore@opensolaris.org int i;
19211253Sgdamore@opensolaris.org char *driver;
1930Sstevel@tonic-gate
1941676Sjpk if (system_labeled)
1951676Sjpk flags = DA_ADD|DA_AUDIO;
1961676Sjpk
197*12116SVikram.Hegde@Sun.COM anynode = node;
1989484Sgarrett.damore@Sun.COM mn = di_minor_name(minor);
1999484Sgarrett.damore@Sun.COM
2009484Sgarrett.damore@Sun.COM if ((tmp = di_devfs_path(node)) == NULL) {
2019484Sgarrett.damore@Sun.COM return (DEVFSADM_CONTINUE);
2029484Sgarrett.damore@Sun.COM }
2039484Sgarrett.damore@Sun.COM (void) snprintf(devpath, sizeof (devpath), "%s:%s", tmp, mn);
2049484Sgarrett.damore@Sun.COM di_devfs_path_free(tmp);
2059484Sgarrett.damore@Sun.COM
20611253Sgdamore@opensolaris.org if (strncmp(mn, "sound,", sizeof ("sound,") - 1) != 0) {
20711253Sgdamore@opensolaris.org devfsadm_errprint("SUNW_audio_link: "
20811253Sgdamore@opensolaris.org "can't find match for'%s'\n", mn);
2099484Sgarrett.damore@Sun.COM return (DEVFSADM_CONTINUE);
2109484Sgarrett.damore@Sun.COM }
2119484Sgarrett.damore@Sun.COM
21211253Sgdamore@opensolaris.org /* strlen("sound,") */
21311253Sgdamore@opensolaris.org (void) strlcpy(base, mn + 6, sizeof (base));
21411253Sgdamore@opensolaris.org mn = base;
2159484Sgarrett.damore@Sun.COM
21611253Sgdamore@opensolaris.org driver = di_driver_name(node);
2179484Sgarrett.damore@Sun.COM
21811253Sgdamore@opensolaris.org /* if driver name override in minor name */
21911253Sgdamore@opensolaris.org if ((tmp = strchr(mn, ',')) != NULL) {
22011253Sgdamore@opensolaris.org driver = mn;
22111253Sgdamore@opensolaris.org *tmp = '\0';
22211253Sgdamore@opensolaris.org mn = tmp + 1;
22311253Sgdamore@opensolaris.org }
2249484Sgarrett.damore@Sun.COM
22511253Sgdamore@opensolaris.org /* skip past "audio" portion of the minor name */
22611253Sgdamore@opensolaris.org if (strncmp(mn, "audio", sizeof ("audio") - 1) == 0) {
22711253Sgdamore@opensolaris.org mn += sizeof ("audio") - 1;
22811253Sgdamore@opensolaris.org }
2299484Sgarrett.damore@Sun.COM
23011253Sgdamore@opensolaris.org /* parse the instance number */
23111253Sgdamore@opensolaris.org for (i = strlen(mn); i; i--) {
23211253Sgdamore@opensolaris.org if (!isdigit(mn[i - 1]))
23311253Sgdamore@opensolaris.org break;
23411253Sgdamore@opensolaris.org }
23511253Sgdamore@opensolaris.org inst = strtol(mn + i, &ep, 10);
23611253Sgdamore@opensolaris.org mn[i] = 0; /* lop off the instance number */
2379484Sgarrett.damore@Sun.COM
23811253Sgdamore@opensolaris.org /*
23911253Sgdamore@opensolaris.org * First we create a node with the driver under /dev/sound.
24011253Sgdamore@opensolaris.org * Note that "instance numbers" used by the audio framework
24111253Sgdamore@opensolaris.org * are guaranteed to be unique for each driver.
24211253Sgdamore@opensolaris.org */
24311253Sgdamore@opensolaris.org (void) snprintf(newpath, sizeof (newpath), "sound/%s:%d%s",
24411253Sgdamore@opensolaris.org driver, inst, mn);
24511253Sgdamore@opensolaris.org (void) devfsadm_mklink(newpath, node, minor, flags);
2469484Sgarrett.damore@Sun.COM
24711253Sgdamore@opensolaris.org /*
24811253Sgdamore@opensolaris.org * The rest of this logic is a gross simplification that is
24911253Sgdamore@opensolaris.org * made possible by the fact that each audio node will have
25011253Sgdamore@opensolaris.org * several different minors associated with it. Rather than
25111253Sgdamore@opensolaris.org * processing each node separately, we just create the links
25211253Sgdamore@opensolaris.org * all at once.
25311253Sgdamore@opensolaris.org *
25411253Sgdamore@opensolaris.org * This reduces the chances of the various links being out of
25511253Sgdamore@opensolaris.org * sync with each other.
25611253Sgdamore@opensolaris.org */
25711253Sgdamore@opensolaris.org if (strcmp(mn, "mixer") != 0) {
2589484Sgarrett.damore@Sun.COM return (DEVFSADM_CONTINUE);
2599484Sgarrett.damore@Sun.COM }
2609484Sgarrett.damore@Sun.COM
26111253Sgdamore@opensolaris.org /*
26211253Sgdamore@opensolaris.org * Its the control node, so create the various
26311253Sgdamore@opensolaris.org * secondary links.
26411253Sgdamore@opensolaris.org */
26511253Sgdamore@opensolaris.org
26611253Sgdamore@opensolaris.org /*
26711253Sgdamore@opensolaris.org * We want a match against the physical path
26811253Sgdamore@opensolaris.org * without the minor name component.
26911253Sgdamore@opensolaris.org */
27011253Sgdamore@opensolaris.org (void) snprintf(re_string, RE_SIZE, "%s", "^mixer([0-9]+)");
27111253Sgdamore@opensolaris.org rules[0].re = re_string;
27211253Sgdamore@opensolaris.org rules[0].subexp = 1;
27311253Sgdamore@opensolaris.org rules[0].flags = MATCH_ALL;
27411253Sgdamore@opensolaris.org
27511253Sgdamore@opensolaris.org /*
27611253Sgdamore@opensolaris.org * enumerate finds the logical audio id, and stuffs
27711253Sgdamore@opensolaris.org * it in buf
27811253Sgdamore@opensolaris.org */
27911253Sgdamore@opensolaris.org (void) strlcpy(devpath, newpath, sizeof (devpath));
28011253Sgdamore@opensolaris.org if (devfsadm_enumerate_int(devpath, 0, &buf, rules, 1)) {
2819484Sgarrett.damore@Sun.COM return (DEVFSADM_CONTINUE);
2829484Sgarrett.damore@Sun.COM }
28311253Sgdamore@opensolaris.org num = strtol(buf, &ep, 10);
28411253Sgdamore@opensolaris.org free(buf);
2859484Sgarrett.damore@Sun.COM
28611253Sgdamore@opensolaris.org /* /dev/sound/0 */
28711253Sgdamore@opensolaris.org (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%ld",
28811253Sgdamore@opensolaris.org driver, inst);
28911253Sgdamore@opensolaris.org (void) snprintf(linkdst, sizeof (linkdst), "sound/%ld", num);
29011253Sgdamore@opensolaris.org (void) devfsadm_secondary_link(linkdst, linksrc, flags);
29111253Sgdamore@opensolaris.org
29211253Sgdamore@opensolaris.org (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%ldctl",
29311253Sgdamore@opensolaris.org driver, inst);
29411253Sgdamore@opensolaris.org (void) snprintf(linkdst, sizeof (linkdst), "sound/%ldctl", num);
29511253Sgdamore@opensolaris.org (void) devfsadm_secondary_link(linkdst, linksrc, flags);
29611253Sgdamore@opensolaris.org
29711253Sgdamore@opensolaris.org (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%lddsp",
29811253Sgdamore@opensolaris.org driver, inst);
29911253Sgdamore@opensolaris.org (void) snprintf(linkdst, sizeof (linkdst), "dsp%ld", num);
30011253Sgdamore@opensolaris.org (void) devfsadm_secondary_link(linkdst, linksrc, flags);
30111253Sgdamore@opensolaris.org
30211253Sgdamore@opensolaris.org (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%ldmixer",
30311253Sgdamore@opensolaris.org driver, inst);
30411253Sgdamore@opensolaris.org (void) snprintf(linkdst, sizeof (linkdst), "mixer%ld", num);
30511253Sgdamore@opensolaris.org (void) devfsadm_secondary_link(linkdst, linksrc, flags);
30611253Sgdamore@opensolaris.org
30711253Sgdamore@opensolaris.org /* Send control number */
30811253Sgdamore@opensolaris.org send_number(num);
30911253Sgdamore@opensolaris.org
3100Sstevel@tonic-gate return (DEVFSADM_CONTINUE);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate static void
check_audio_link(di_node_t anynode,char * secondary,const char * primary_format)314*12116SVikram.Hegde@Sun.COM check_audio_link(di_node_t anynode, char *secondary, const char *primary_format)
3150Sstevel@tonic-gate {
3169484Sgarrett.damore@Sun.COM char primary[PATH_MAX + 1];
3170Sstevel@tonic-gate int i;
3181676Sjpk int flags = 0;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate /* if link is present, return */
321*12116SVikram.Hegde@Sun.COM if (devfsadm_link_valid(anynode, secondary) == DEVFSADM_TRUE) {
3220Sstevel@tonic-gate return;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3251676Sjpk if (system_labeled)
3261676Sjpk flags = DA_ADD|DA_AUDIO;
3271676Sjpk
3280Sstevel@tonic-gate for (i = 0; i < MAX_AUDIO_LINK; i++) {
3299484Sgarrett.damore@Sun.COM (void) sprintf(primary, primary_format, i);
330*12116SVikram.Hegde@Sun.COM if (devfsadm_link_valid(anynode, primary) == DEVFSADM_TRUE) {
3319484Sgarrett.damore@Sun.COM /* we read link to get it to the master "real" link */
3329484Sgarrett.damore@Sun.COM (void) devfsadm_secondary_link(secondary,
3339484Sgarrett.damore@Sun.COM primary, flags);
3340Sstevel@tonic-gate break;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate }
338