8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / devfsadm / audio_link.c
blob42a9aa5cd71a0d6185963ae21def620486af382e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <regex.h>
26 #include <devfsadm.h>
27 #include <strings.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <syslog.h>
33 #include <bsm/devalloc.h>
34 #include <sys/audio.h>
35 #include <sys/soundcard.h>
36 #include <unistd.h>
38 #define MAX_AUDIO_LINK 100
39 #define RE_SIZE 64
41 extern int system_labeled;
43 static void check_audio_link(di_node_t anynode, char *secondary_link,
44 const char *primary_link_format);
46 static int audio_process(di_minor_t minor, di_node_t node);
47 static int sndstat_process(di_minor_t minor, di_node_t node);
49 static devfsadm_create_t audio_cbt[] = {
50 { "audio", "ddi_audio", NULL,
51 TYPE_EXACT, ILEVEL_0, audio_process
53 { "pseudo", "ddi_pseudo", "audio",
54 TYPE_EXACT|DRV_EXACT, ILEVEL_0, sndstat_process
58 DEVFSADM_CREATE_INIT_V0(audio_cbt);
61 * the following can't be one big RE with a bunch of alterations "|"
62 * because recurse_dev_re() would not work.
64 static devfsadm_remove_t audio_remove_cbt[] = {
66 * Secondary links.
69 /* /dev/audio, /dev/audioctl, /dev/dsp */
70 { "audio", "^audio$",
71 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
73 { "audio", "^audioctl$",
74 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
76 { "audio", "^dsp$",
77 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
79 { "audio", "^mixer",
80 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
82 { "audio", "^sndstat$",
83 RM_PRE|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
85 { "audio", "^mixer[0-9]+$",
86 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
88 { "audio", "^dsp[0-9]+$",
89 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
91 { "audio", "^sound/[0-9]+$",
92 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
94 { "audio", "^sound/[0-9]+ctl$",
95 RM_POST|RM_HOT|RM_ALWAYS, ILEVEL_0, devfsadm_rm_all
99 DEVFSADM_REMOVE_INIT_V0(audio_remove_cbt);
101 static di_node_t anynode;
104 minor_init(void)
106 anynode = DI_NODE_NIL;
107 return (DEVFSADM_SUCCESS);
111 minor_fini(void)
113 check_audio_link(anynode, "audio", "sound/%d");
114 check_audio_link(anynode, "audioctl", "sound/%dctl");
115 check_audio_link(anynode, "dsp", "dsp%d");
116 anynode = DI_NODE_NIL;
117 return (DEVFSADM_SUCCESS);
121 #define COPYSUB(to, from, pm, pos) (void) strncpy(to, &from[pm[pos].rm_so], \
122 pm[pos].rm_eo - pm[pos].rm_so); \
123 to[pm[pos].rm_eo - pm[pos].rm_so] = 0;
125 static void
126 send_number(long num)
128 char buf[PATH_MAX+1];
131 * This is not safe with -r.
133 if (strcmp(devfsadm_root_path(), "/") != 0)
134 return;
136 (void) snprintf(buf, sizeof (buf), "/dev/mixer%ld", num);
137 if (device_exists(buf)) {
138 int fd;
140 if ((fd = open(buf, O_RDWR)) < 0)
141 return;
143 (void) ioctl(fd, SNDCTL_SUN_SEND_NUMBER, &num);
144 (void) close(fd);
145 devfsadm_print(CHATTY_MID,
146 "sent devnum audio %ld to %s\n", num, buf);
150 static int
151 sndstat_process(di_minor_t minor, di_node_t node)
153 char *mn;
155 mn = di_minor_name(minor);
156 anynode = node;
159 * "Special" handling for /dev/sndstat and /dev/mixer.
161 if (strcmp(mn, "sound,sndstat0") == 0) {
162 (void) devfsadm_mklink("sndstat", node, minor, 0);
163 (void) devfsadm_secondary_link("mixer", "sndstat", 0);
166 return (DEVFSADM_CONTINUE);
170 * This function is called for every audio node.
171 * Calls enumerate to assign a logical unit id, and then
172 * devfsadm_mklink to make the link.
174 static int
175 audio_process(di_minor_t minor, di_node_t node)
177 int flags = 0;
178 char devpath[PATH_MAX + 1];
179 char newpath[PATH_MAX + 1];
180 char *buf;
181 char *mn;
182 char *tmp;
183 char *ep;
184 char re_string[RE_SIZE+1];
185 devfsadm_enumerate_t rules[1] = {NULL};
186 char base[PATH_MAX + 1];
187 char linksrc[PATH_MAX + 1];
188 char linkdst[PATH_MAX + 1];
189 long num;
190 long inst;
191 int i;
192 char *driver;
194 if (system_labeled)
195 flags = DA_ADD|DA_AUDIO;
197 anynode = node;
198 mn = di_minor_name(minor);
200 if ((tmp = di_devfs_path(node)) == NULL) {
201 return (DEVFSADM_CONTINUE);
203 (void) snprintf(devpath, sizeof (devpath), "%s:%s", tmp, mn);
204 di_devfs_path_free(tmp);
206 if (strncmp(mn, "sound,", sizeof ("sound,") - 1) != 0) {
207 devfsadm_errprint("SUNW_audio_link: "
208 "can't find match for'%s'\n", mn);
209 return (DEVFSADM_CONTINUE);
212 /* strlen("sound,") */
213 (void) strlcpy(base, mn + 6, sizeof (base));
214 mn = base;
216 driver = di_driver_name(node);
218 /* if driver name override in minor name */
219 if ((tmp = strchr(mn, ',')) != NULL) {
220 driver = mn;
221 *tmp = '\0';
222 mn = tmp + 1;
225 /* skip past "audio" portion of the minor name */
226 if (strncmp(mn, "audio", sizeof ("audio") - 1) == 0) {
227 mn += sizeof ("audio") - 1;
230 /* parse the instance number */
231 for (i = strlen(mn); i; i--) {
232 if (!isdigit(mn[i - 1]))
233 break;
235 inst = strtol(mn + i, &ep, 10);
236 mn[i] = 0; /* lop off the instance number */
239 * First we create a node with the driver under /dev/sound.
240 * Note that "instance numbers" used by the audio framework
241 * are guaranteed to be unique for each driver.
243 (void) snprintf(newpath, sizeof (newpath), "sound/%s:%d%s",
244 driver, inst, mn);
245 (void) devfsadm_mklink(newpath, node, minor, flags);
248 * The rest of this logic is a gross simplification that is
249 * made possible by the fact that each audio node will have
250 * several different minors associated with it. Rather than
251 * processing each node separately, we just create the links
252 * all at once.
254 * This reduces the chances of the various links being out of
255 * sync with each other.
257 if (strcmp(mn, "mixer") != 0) {
258 return (DEVFSADM_CONTINUE);
262 * Its the control node, so create the various
263 * secondary links.
267 * We want a match against the physical path
268 * without the minor name component.
270 (void) snprintf(re_string, RE_SIZE, "%s", "^mixer([0-9]+)");
271 rules[0].re = re_string;
272 rules[0].subexp = 1;
273 rules[0].flags = MATCH_ALL;
276 * enumerate finds the logical audio id, and stuffs
277 * it in buf
279 (void) strlcpy(devpath, newpath, sizeof (devpath));
280 if (devfsadm_enumerate_int(devpath, 0, &buf, rules, 1)) {
281 return (DEVFSADM_CONTINUE);
283 num = strtol(buf, &ep, 10);
284 free(buf);
286 /* /dev/sound/0 */
287 (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%ld",
288 driver, inst);
289 (void) snprintf(linkdst, sizeof (linkdst), "sound/%ld", num);
290 (void) devfsadm_secondary_link(linkdst, linksrc, flags);
292 (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%ldctl",
293 driver, inst);
294 (void) snprintf(linkdst, sizeof (linkdst), "sound/%ldctl", num);
295 (void) devfsadm_secondary_link(linkdst, linksrc, flags);
297 (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%lddsp",
298 driver, inst);
299 (void) snprintf(linkdst, sizeof (linkdst), "dsp%ld", num);
300 (void) devfsadm_secondary_link(linkdst, linksrc, flags);
302 (void) snprintf(linksrc, sizeof (linksrc), "sound/%s:%ldmixer",
303 driver, inst);
304 (void) snprintf(linkdst, sizeof (linkdst), "mixer%ld", num);
305 (void) devfsadm_secondary_link(linkdst, linksrc, flags);
307 /* Send control number */
308 send_number(num);
310 return (DEVFSADM_CONTINUE);
313 static void
314 check_audio_link(di_node_t anynode, char *secondary, const char *primary_format)
316 char primary[PATH_MAX + 1];
317 int i;
318 int flags = 0;
320 /* if link is present, return */
321 if (devfsadm_link_valid(anynode, secondary) == DEVFSADM_TRUE) {
322 return;
325 if (system_labeled)
326 flags = DA_ADD|DA_AUDIO;
328 for (i = 0; i < MAX_AUDIO_LINK; i++) {
329 (void) sprintf(primary, primary_format, i);
330 if (devfsadm_link_valid(anynode, primary) == DEVFSADM_TRUE) {
331 /* we read link to get it to the master "real" link */
332 (void) devfsadm_secondary_link(secondary,
333 primary, flags);
334 break;