File:
[DragonFly] /
src /
sys /
dev /
sound /
pcm /
mixer.c
Revision
1.6:
download - view:
text,
annotated -
select for diffs
Wed May 19 22:52:50 2004 UTC (9 years ago) by
dillon
Branches:
MAIN
CVS tags:
HEAD
Device layer rollup commit.
* cdevsw_add() is now required. cdevsw_add() and cdevsw_remove() may specify
a mask/match indicating the range of supported minor numbers. Multiple
cdevsw_add()'s using the same major number, but distinctly different
ranges, may be issued. All devices that failed to call cdevsw_add() before
now do.
* cdevsw_remove() now automatically marks all devices within its supported
range as being destroyed.
* vnode->v_rdev is no longer resolved when the vnode is created. Instead,
only v_udev (a newly added field) is resolved. v_rdev is resolved when
the vnode is opened and cleared on the last close.
* A great deal of code was making rather dubious assumptions with regards
to the validity of devices associated with vnodes, primarily due to
the persistence of a device structure due to being indexed by (major, minor)
instead of by (cdevsw, major, minor). In particular, if you run a program
which connects to a USB device and then you pull the USB device and plug
it back in, the vnode subsystem will continue to believe that the device
is open when, in fact, it isn't (because it was destroyed and recreated).
In particular, note that all the VFS mount procedures now check devices
via v_udev instead of v_rdev prior to calling VOP_OPEN(), since v_rdev
is NULL prior to the first open.
* The disk layer's device interaction has been rewritten. The disk layer
(i.e. the slice and disklabel management layer) no longer overloads
its data onto the device structure representing the underlying physical
disk. Instead, the disk layer uses the new cdevsw_add() functionality
to register its own cdevsw using the underlying device's major number,
and simply does NOT register the underlying device's cdevsw. No
confusion is created because the device hash is now based on
(cdevsw,major,minor) rather then (major,minor).
NOTE: This also means that underlying raw disk devices may use the entire
device minor number instead of having to reserve the bits used by the disk
layer, and also means that can we (theoretically) stack a fully
disklabel-supported 'disk' on top of any block device.
* The new reference counting scheme prevents this by associating a device
with a cdevsw and disconnecting the device from its cdevsw when the cdevsw
is removed. Additionally, all udev2dev() lookups run through the cdevsw
mask/match and only successfully find devices still associated with an
active cdevsw.
* Major work on MFS: MFS no longer shortcuts vnode and device creation. It
now creates a real vnode and a real device and implements real open and
close VOPs. Additionally, due to the disk layer changes, MFS is no longer
limited to 255 mounts. The new limit is 16 million. Since MFS creates a
real device node, mount_mfs will now create a real /dev/mfs<PID> device
that can be read from userland (e.g. so you can dump an MFS filesystem).
* BUF AND DEVICE STRATEGY changes. The struct buf contains a b_dev field.
In order to properly handle stacked devices we now require that the b_dev
field be initialized before the device strategy routine is called. This
required some additional work in various VFS implementations. To enforce
this requirement, biodone() now sets b_dev to NODEV. The new disk layer
will adjust b_dev before forwarding a request to the actual physical
device.
* A bug in the ISO CD boot sequence which resulted in a panic has been fixed.
Testing by: lots of people, but David Rhodus found the most aggregious bugs.
1: /*
2: * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24: * SUCH DAMAGE.
25: *
26: * $FreeBSD: src/sys/dev/sound/pcm/mixer.c,v 1.4.2.8 2002/04/22 15:49:36 cg Exp $
27: * $DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.6 2004/05/19 22:52:50 dillon Exp $
28: */
29:
30: #include <dev/sound/pcm/sound.h>
31:
32: #include "mixer_if.h"
33:
34: SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.6 2004/05/19 22:52:50 dillon Exp $");
35:
36: MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
37:
38: #define MIXER_NAMELEN 16
39: struct snd_mixer {
40: KOBJ_FIELDS;
41: const char *type;
42: void *devinfo;
43: int busy;
44: int hwvol_muted;
45: int hwvol_mixer;
46: int hwvol_step;
47: u_int32_t hwvol_mute_level;
48: u_int32_t devs;
49: u_int32_t recdevs;
50: u_int32_t recsrc;
51: u_int16_t level[32];
52: char name[MIXER_NAMELEN];
53: void *lock;
54: };
55:
56: static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
57: [SOUND_MIXER_VOLUME] = 75,
58: [SOUND_MIXER_BASS] = 50,
59: [SOUND_MIXER_TREBLE] = 50,
60: [SOUND_MIXER_SYNTH] = 75,
61: [SOUND_MIXER_PCM] = 75,
62: [SOUND_MIXER_SPEAKER] = 75,
63: [SOUND_MIXER_LINE] = 75,
64: [SOUND_MIXER_MIC] = 0,
65: [SOUND_MIXER_CD] = 75,
66: [SOUND_MIXER_LINE1] = 75,
67: [SOUND_MIXER_VIDEO] = 75,
68: [SOUND_MIXER_RECLEV] = 0,
69: [SOUND_MIXER_OGAIN] = 50,
70: [SOUND_MIXER_MONITOR] = 75,
71: };
72:
73: static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
74:
75: static d_open_t mixer_open;
76: static d_close_t mixer_close;
77:
78: static struct cdevsw mixer_cdevsw = {
79: /* name */ "mixer",
80: /* maj */ SND_CDEV_MAJOR,
81: /* flags */ 0,
82: /* port */ NULL,
83: /* clone */ NULL,
84:
85: /* open */ mixer_open,
86: /* close */ mixer_close,
87: /* read */ noread,
88: /* write */ nowrite,
89: /* ioctl */ mixer_ioctl,
90: /* poll */ nopoll,
91: /* mmap */ nommap,
92: /* strategy */ nostrategy,
93: /* dump */ nodump,
94: /* psize */ nopsize
95: };
96:
97: #ifdef USING_DEVFS
98: static eventhandler_tag mixer_ehtag;
99: #endif
100:
101: static dev_t
102: mixer_get_devt(device_t dev)
103: {
104: dev_t pdev;
105: int unit;
106:
107: unit = device_get_unit(dev);
108: pdev = make_adhoc_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0));
109:
110: return pdev;
111: }
112:
113: #ifdef SND_DYNSYSCTL
114: static int
115: mixer_lookup(char *devname)
116: {
117: int i;
118:
119: for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
120: if (strncmp(devname, snd_mixernames[i],
121: strlen(snd_mixernames[i])) == 0)
122: return i;
123: return -1;
124: }
125: #endif
126:
127: static int
128: mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
129: {
130: unsigned l, r;
131: int v;
132:
133: if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
134: return -1;
135:
136: l = min((lev & 0x00ff), 100);
137: r = min(((lev & 0xff00) >> 8), 100);
138:
139: v = MIXER_SET(mixer, dev, l, r);
140: if (v < 0)
141: return -1;
142:
143: mixer->level[dev] = l | (r << 8);
144: return 0;
145: }
146:
147: static int
148: mixer_get(struct snd_mixer *mixer, int dev)
149: {
150: if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
151: return mixer->level[dev];
152: else return -1;
153: }
154:
155: static int
156: mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
157: {
158: src &= mixer->recdevs;
159: if (src == 0)
160: src = SOUND_MASK_MIC;
161: mixer->recsrc = MIXER_SETRECSRC(mixer, src);
162: return 0;
163: }
164:
165: static int
166: mixer_getrecsrc(struct snd_mixer *mixer)
167: {
168: return mixer->recsrc;
169: }
170:
171: void
172: mix_setdevs(struct snd_mixer *m, u_int32_t v)
173: {
174: m->devs = v;
175: }
176:
177: void
178: mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
179: {
180: m->recdevs = v;
181: }
182:
183: u_int32_t
184: mix_getdevs(struct snd_mixer *m)
185: {
186: return m->devs;
187: }
188:
189: u_int32_t
190: mix_getrecdevs(struct snd_mixer *m)
191: {
192: return m->recdevs;
193: }
194:
195: void *
196: mix_getdevinfo(struct snd_mixer *m)
197: {
198: return m->devinfo;
199: }
200:
201: int
202: mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
203: {
204: struct snd_mixer *m;
205: u_int16_t v;
206: dev_t pdev;
207: int i, unit;
208:
209: m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
210: snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
211: m->lock = snd_mtxcreate(m->name, "pcm mixer");
212: m->type = cls->name;
213: m->devinfo = devinfo;
214: m->busy = 0;
215:
216: if (MIXER_INIT(m))
217: goto bad;
218:
219: for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
220: v = snd_mixerdefaults[i];
221: mixer_set(m, i, v | (v << 8));
222: }
223:
224: mixer_setrecsrc(m, SOUND_MASK_MIC);
225:
226: unit = device_get_unit(dev);
227: cdevsw_add(&mixer_cdevsw, PCMMKMINOR(-1, 0, 0), PCMMKMINOR(unit, 0, 0));
228: pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
229: UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
230: pdev->si_drv1 = m;
231:
232: return 0;
233:
234: bad:
235: snd_mtxlock(m->lock);
236: snd_mtxfree(m->lock);
237: kobj_delete((kobj_t)m, M_MIXER);
238: return -1;
239: }
240:
241: int
242: mixer_uninit(device_t dev)
243: {
244: int i;
245: struct snd_mixer *m;
246: dev_t pdev;
247:
248: pdev = mixer_get_devt(dev);
249: m = pdev->si_drv1;
250: snd_mtxlock(m->lock);
251:
252: if (m->busy) {
253: snd_mtxunlock(m->lock);
254: return EBUSY;
255: }
256:
257: pdev->si_drv1 = NULL;
258:
259: /*
260: * The mixer might be used by several entities, so we do not
261: * want to remove the cdevsw here. Destroy the device instead.
262: * destroy_dev() requires a reference count.
263: */
264: reference_dev(pdev);
265: destroy_dev(pdev);
266:
267: for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
268: mixer_set(m, i, 0);
269:
270: mixer_setrecsrc(m, SOUND_MASK_MIC);
271:
272: MIXER_UNINIT(m);
273:
274: snd_mtxfree(m->lock);
275: kobj_delete((kobj_t)m, M_MIXER);
276:
277: return 0;
278: }
279:
280: int
281: mixer_reinit(device_t dev)
282: {
283: struct snd_mixer *m;
284: dev_t pdev;
285: int i;
286:
287: pdev = mixer_get_devt(dev);
288: m = pdev->si_drv1;
289: snd_mtxlock(m->lock);
290:
291: i = MIXER_REINIT(m);
292: if (i) {
293: snd_mtxunlock(m->lock);
294: return i;
295: }
296:
297: for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
298: mixer_set(m, i, m->level[i]);
299:
300: mixer_setrecsrc(m, m->recsrc);
301: snd_mtxunlock(m->lock);
302:
303: return 0;
304: }
305:
306: #ifdef SND_DYNSYSCTL
307: static int
308: sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
309: {
310: char devname[32];
311: int error, dev;
312: struct snd_mixer *m;
313:
314: m = oidp->oid_arg1;
315: snd_mtxlock(m->lock);
316: strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
317: error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
318: if (error == 0 && req->newptr != NULL) {
319: dev = mixer_lookup(devname);
320: if (dev == -1) {
321: snd_mtxunlock(m->lock);
322: return EINVAL;
323: }
324: else if (dev != m->hwvol_mixer) {
325: m->hwvol_mixer = dev;
326: m->hwvol_muted = 0;
327: }
328: }
329: snd_mtxunlock(m->lock);
330: return error;
331: }
332: #endif
333:
334: int
335: mixer_hwvol_init(device_t dev)
336: {
337: struct snd_mixer *m;
338: dev_t pdev;
339:
340: pdev = mixer_get_devt(dev);
341: m = pdev->si_drv1;
342: snd_mtxlock(m->lock);
343:
344: m->hwvol_mixer = SOUND_MIXER_VOLUME;
345: m->hwvol_step = 5;
346: #ifdef SND_DYNSYSCTL
347: SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
348: OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
349: SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
350: OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
351: sysctl_hw_snd_hwvol_mixer, "A", "")
352: #endif
353: snd_mtxunlock(m->lock);
354: return 0;
355: }
356:
357: void
358: mixer_hwvol_mute(device_t dev)
359: {
360: struct snd_mixer *m;
361: dev_t pdev;
362:
363: pdev = mixer_get_devt(dev);
364: m = pdev->si_drv1;
365: snd_mtxlock(m->lock);
366: if (m->hwvol_muted) {
367: m->hwvol_muted = 0;
368: mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
369: } else {
370: m->hwvol_muted++;
371: m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
372: mixer_set(m, m->hwvol_mixer, 0);
373: }
374: snd_mtxunlock(m->lock);
375: }
376:
377: void
378: mixer_hwvol_step(device_t dev, int left_step, int right_step)
379: {
380: struct snd_mixer *m;
381: int level, left, right;
382: dev_t pdev;
383:
384: pdev = mixer_get_devt(dev);
385: m = pdev->si_drv1;
386: snd_mtxlock(m->lock);
387: if (m->hwvol_muted) {
388: m->hwvol_muted = 0;
389: level = m->hwvol_mute_level;
390: } else
391: level = mixer_get(m, m->hwvol_mixer);
392: if (level != -1) {
393: left = level & 0xff;
394: right = level >> 8;
395: left += left_step * m->hwvol_step;
396: if (left < 0)
397: left = 0;
398: right += right_step * m->hwvol_step;
399: if (right < 0)
400: right = 0;
401: mixer_set(m, m->hwvol_mixer, left | right << 8);
402: }
403: snd_mtxunlock(m->lock);
404: }
405:
406: /* ----------------------------------------------------------------------- */
407:
408: static int
409: mixer_open(dev_t i_dev, int flags, int mode, struct thread *td)
410: {
411: struct snd_mixer *m;
412: intrmask_t s;
413:
414: m = i_dev->si_drv1;
415: s = spltty();
416: snd_mtxlock(m->lock);
417:
418: m->busy++;
419:
420: snd_mtxunlock(m->lock);
421: splx(s);
422: return 0;
423: }
424:
425: static int
426: mixer_close(dev_t i_dev, int flags, int mode, struct thread *td)
427: {
428: struct snd_mixer *m;
429: intrmask_t s;
430:
431: m = i_dev->si_drv1;
432: s = spltty();
433: snd_mtxlock(m->lock);
434:
435: if (!m->busy) {
436: snd_mtxunlock(m->lock);
437: splx(s);
438: return EBADF;
439: }
440: m->busy--;
441:
442: snd_mtxunlock(m->lock);
443: splx(s);
444: return 0;
445: }
446:
447: int
448: mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
449: {
450: struct snd_mixer *m;
451: intrmask_t s;
452: int ret, *arg_i = (int *)arg;
453: int v = -1, j = cmd & 0xff;
454:
455: m = i_dev->si_drv1;
456: if (!m->busy)
457: return EBADF;
458:
459: s = spltty();
460: snd_mtxlock(m->lock);
461: if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
462: if (j == SOUND_MIXER_RECSRC)
463: ret = mixer_setrecsrc(m, *arg_i);
464: else
465: ret = mixer_set(m, j, *arg_i);
466: snd_mtxunlock(m->lock);
467: splx(s);
468: return (ret == 0)? 0 : ENXIO;
469: }
470:
471: if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
472: switch (j) {
473: case SOUND_MIXER_DEVMASK:
474: case SOUND_MIXER_CAPS:
475: case SOUND_MIXER_STEREODEVS:
476: v = mix_getdevs(m);
477: break;
478:
479: case SOUND_MIXER_RECMASK:
480: v = mix_getrecdevs(m);
481: break;
482:
483: case SOUND_MIXER_RECSRC:
484: v = mixer_getrecsrc(m);
485: break;
486:
487: default:
488: v = mixer_get(m, j);
489: }
490: *arg_i = v;
491: snd_mtxunlock(m->lock);
492: return (v != -1)? 0 : ENXIO;
493: }
494: snd_mtxunlock(m->lock);
495: splx(s);
496: return ENXIO;
497: }
498:
499: #ifdef USING_DEVFS
500: static void
501: mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
502: {
503: dev_t pdev;
504:
505: if (*dev != NODEV)
506: return;
507: if (strcmp(name, "mixer") == 0) {
508: pdev = make_adhoc_dev(&mixer_cdevsw,
509: PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
510: if (pdev->si_flags & SI_NAMED)
511: *dev = pdev;
512: }
513: }
514:
515: static void
516: mixer_sysinit(void *p)
517: {
518: mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
519: }
520:
521: static void
522: mixer_sysuninit(void *p)
523: {
524: if (mixer_ehtag != NULL)
525: EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
526: }
527:
528: SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
529: SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
530: #endif
531:
532: