xref: /freebsd/sys/dev/sound/usb/uaudio_pcm.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause
4  *
5  * Copyright (c) 2000-2002 Hiroyuki Aizu <aizu@navi.org>
6  * Copyright (c) 2006 Hans Petter Selasky
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifdef HAVE_KERNEL_OPTION_HEADERS
31 #include "opt_snd.h"
32 #endif
33 
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/chip.h>
36 #include <dev/sound/usb/uaudio.h>
37 
38 #include "mixer_if.h"
39 
40 /************************************************************/
41 static void *
42 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
43 {
44 	return (uaudio_chan_init(devinfo, b, c, dir));
45 }
46 
47 static int
48 ua_chan_free(kobj_t obj, void *data)
49 {
50 	return (uaudio_chan_free(data));
51 }
52 
53 static int
54 ua_chan_setformat(kobj_t obj, void *data, uint32_t format)
55 {
56 	/*
57 	 * At this point, no need to query as we
58 	 * shouldn't select an unsorted format
59 	 */
60 	return (uaudio_chan_set_param_format(data, format));
61 }
62 
63 static uint32_t
64 ua_chan_setspeed(kobj_t obj, void *data, uint32_t speed)
65 {
66 	return (uaudio_chan_set_param_speed(data, speed));
67 }
68 
69 static uint32_t
70 ua_chan_setblocksize(kobj_t obj, void *data, uint32_t blocksize)
71 {
72 	return (uaudio_chan_set_param_blocksize(data, blocksize));
73 }
74 
75 static int
76 ua_chan_setfragments(kobj_t obj, void *data, uint32_t blocksize, uint32_t blockcount)
77 {
78 	return (uaudio_chan_set_param_fragments(data, blocksize, blockcount));
79 }
80 
81 static int
82 ua_chan_trigger(kobj_t obj, void *data, int go)
83 {
84 	if (PCMTRIG_COMMON(go)) {
85 		if (go == PCMTRIG_START) {
86 			uaudio_chan_start(data);
87 		} else {
88 			uaudio_chan_stop(data);
89 		}
90 	}
91 	return (0);
92 }
93 
94 static uint32_t
95 ua_chan_getptr(kobj_t obj, void *data)
96 {
97 	return (uaudio_chan_getptr(data));
98 }
99 
100 static struct pcmchan_caps *
101 ua_chan_getcaps(kobj_t obj, void *data)
102 {
103 	return (uaudio_chan_getcaps(data));
104 }
105 
106 static struct pcmchan_matrix *
107 ua_chan_getmatrix(kobj_t obj, void *data, uint32_t format)
108 {
109 	return (uaudio_chan_getmatrix(data, format));
110 }
111 
112 static kobj_method_t ua_chan_methods[] = {
113 	KOBJMETHOD(channel_init, ua_chan_init),
114 	KOBJMETHOD(channel_free, ua_chan_free),
115 	KOBJMETHOD(channel_setformat, ua_chan_setformat),
116 	KOBJMETHOD(channel_setspeed, ua_chan_setspeed),
117 	KOBJMETHOD(channel_setblocksize, ua_chan_setblocksize),
118 	KOBJMETHOD(channel_setfragments, ua_chan_setfragments),
119 	KOBJMETHOD(channel_trigger, ua_chan_trigger),
120 	KOBJMETHOD(channel_getptr, ua_chan_getptr),
121 	KOBJMETHOD(channel_getcaps, ua_chan_getcaps),
122 	KOBJMETHOD(channel_getmatrix, ua_chan_getmatrix),
123 	KOBJMETHOD_END
124 };
125 
126 CHANNEL_DECLARE(ua_chan);
127 
128 /************************************************************/
129 static int
130 ua_mixer_init(struct snd_mixer *m)
131 {
132 	return (uaudio_mixer_init_sub(mix_getdevinfo(m), m));
133 }
134 
135 static int
136 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
137 {
138 	struct mtx *mtx = mixer_get_lock(m);
139 	uint8_t do_unlock;
140 
141 	if (mtx_owned(mtx)) {
142 		do_unlock = 0;
143 	} else {
144 		do_unlock = 1;
145 		mtx_lock(mtx);
146 	}
147 	uaudio_mixer_set(mix_getdevinfo(m), m, type, left, right);
148 	if (do_unlock) {
149 		mtx_unlock(mtx);
150 	}
151 	return (left | (right << 8));
152 }
153 
154 static uint32_t
155 ua_mixer_setrecsrc(struct snd_mixer *m, uint32_t src)
156 {
157 	struct mtx *mtx = mixer_get_lock(m);
158 	int retval;
159 	uint8_t do_unlock;
160 
161 	if (mtx_owned(mtx)) {
162 		do_unlock = 0;
163 	} else {
164 		do_unlock = 1;
165 		mtx_lock(mtx);
166 	}
167 	retval = uaudio_mixer_setrecsrc(mix_getdevinfo(m), m, src);
168 	if (do_unlock) {
169 		mtx_unlock(mtx);
170 	}
171 	return (retval);
172 }
173 
174 static int
175 ua_mixer_uninit(struct snd_mixer *m)
176 {
177 	return (uaudio_mixer_uninit_sub(mix_getdevinfo(m), m));
178 }
179 
180 static kobj_method_t ua_mixer_methods[] = {
181 	KOBJMETHOD(mixer_init, ua_mixer_init),
182 	KOBJMETHOD(mixer_uninit, ua_mixer_uninit),
183 	KOBJMETHOD(mixer_set, ua_mixer_set),
184 	KOBJMETHOD(mixer_setrecsrc, ua_mixer_setrecsrc),
185 	KOBJMETHOD_END
186 };
187 
188 MIXER_DECLARE(ua_mixer);
189 /************************************************************/
190 
191 static int
192 ua_probe(device_t dev)
193 {
194 	struct sndcard_func *func;
195 
196 	/* the parent device has already been probed */
197 
198 	func = device_get_ivars(dev);
199 
200 	if ((func == NULL) ||
201 	    (func->func != SCF_PCM)) {
202 		return (ENXIO);
203 	}
204 
205 	return (BUS_PROBE_DEFAULT);
206 }
207 
208 static int
209 ua_attach(device_t dev)
210 {
211 	return (uaudio_attach_sub(dev, &ua_mixer_class, &ua_chan_class));
212 }
213 
214 static int
215 ua_detach(device_t dev)
216 {
217 	return (uaudio_detach_sub(dev));
218 }
219 
220 /************************************************************/
221 
222 static device_method_t ua_pcm_methods[] = {
223 	/* Device interface */
224 	DEVMETHOD(device_probe, ua_probe),
225 	DEVMETHOD(device_attach, ua_attach),
226 	DEVMETHOD(device_detach, ua_detach),
227 
228 	DEVMETHOD_END
229 };
230 
231 static driver_t ua_pcm_driver = {
232 	"pcm",
233 	ua_pcm_methods,
234 	PCM_SOFTC_SIZE,
235 };
236 
237 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, 0, 0);
238 MODULE_DEPEND(ua_pcm, snd_uaudio, 1, 1, 1);
239 MODULE_DEPEND(ua_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
240 MODULE_VERSION(ua_pcm, 1);
241