pad.c revision 1.52.4.2 1 1.52.4.2 martin /* $NetBSD: pad.c,v 1.52.4.2 2020/04/13 08:04:25 martin Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.52.4.2 martin __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.52.4.2 2020/04/13 08:04:25 martin Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/types.h>
33 1.1 jmcneill #include <sys/param.h>
34 1.1 jmcneill #include <sys/conf.h>
35 1.1 jmcneill #include <sys/buf.h>
36 1.39 nat #include <sys/file.h>
37 1.39 nat #include <sys/filedesc.h>
38 1.39 nat #include <sys/vnode.h>
39 1.39 nat #include <sys/kauth.h>
40 1.1 jmcneill #include <sys/kmem.h>
41 1.1 jmcneill #include <sys/kernel.h>
42 1.1 jmcneill #include <sys/device.h>
43 1.52.4.2 martin #include <sys/poll.h>
44 1.1 jmcneill #include <sys/proc.h>
45 1.1 jmcneill #include <sys/condvar.h>
46 1.1 jmcneill #include <sys/select.h>
47 1.39 nat #include <sys/stat.h>
48 1.1 jmcneill #include <sys/audioio.h>
49 1.13 ahoka #include <sys/module.h>
50 1.1 jmcneill
51 1.52.4.1 christos #include <dev/audio/audio_if.h>
52 1.52.4.1 christos #include <dev/audio/audiovar.h>
53 1.1 jmcneill
54 1.2 jmcneill #include <dev/pad/padvar.h>
55 1.2 jmcneill
56 1.52.4.1 christos /* #define PAD_DEBUG */
57 1.52.4.1 christos #ifdef PAD_DEBUG
58 1.52.4.1 christos #define DPRINTF(fmt...) printf(fmt)
59 1.52.4.1 christos #else
60 1.52.4.1 christos #define DPRINTF(fmt...) /**/
61 1.52.4.1 christos #endif
62 1.52.4.1 christos
63 1.39 nat #define MAXDEVS 128
64 1.41 nat #define PADCLONER 254
65 1.1 jmcneill #define PADUNIT(x) minor(x)
66 1.1 jmcneill
67 1.37 nat #define PADFREQ 44100
68 1.37 nat #define PADCHAN 2
69 1.37 nat #define PADPREC 16
70 1.37 nat
71 1.1 jmcneill extern struct cfdriver pad_cd;
72 1.50 nat kmutex_t padconfig;
73 1.1 jmcneill
74 1.1 jmcneill typedef struct pad_block {
75 1.1 jmcneill uint8_t *pb_ptr;
76 1.1 jmcneill int pb_len;
77 1.1 jmcneill } pad_block_t;
78 1.1 jmcneill
79 1.2 jmcneill enum {
80 1.2 jmcneill PAD_OUTPUT_CLASS,
81 1.2 jmcneill PAD_INPUT_CLASS,
82 1.2 jmcneill PAD_OUTPUT_MASTER_VOLUME,
83 1.2 jmcneill PAD_INPUT_DAC_VOLUME,
84 1.2 jmcneill PAD_ENUM_LAST,
85 1.2 jmcneill };
86 1.1 jmcneill
87 1.10 christos static int pad_match(device_t, cfdata_t, void *);
88 1.5 dyoung static void pad_attach(device_t, device_t, void *);
89 1.5 dyoung static int pad_detach(device_t, int);
90 1.5 dyoung static void pad_childdet(device_t, device_t);
91 1.1 jmcneill
92 1.52.4.1 christos static int pad_query_format(void *, audio_format_query_t *);
93 1.52.4.1 christos static int pad_set_format(void *, int,
94 1.52.4.1 christos const audio_params_t *, const audio_params_t *,
95 1.52.4.1 christos audio_filter_reg_t *, audio_filter_reg_t *);
96 1.1 jmcneill static int pad_start_output(void *, void *, int,
97 1.52.4.2 martin void (*)(void *), void *);
98 1.1 jmcneill static int pad_halt_output(void *);
99 1.1 jmcneill static int pad_getdev(void *, struct audio_device *);
100 1.1 jmcneill static int pad_set_port(void *, mixer_ctrl_t *);
101 1.1 jmcneill static int pad_get_port(void *, mixer_ctrl_t *);
102 1.1 jmcneill static int pad_query_devinfo(void *, mixer_devinfo_t *);
103 1.1 jmcneill static int pad_get_props(void *);
104 1.17 jmcneill static void pad_get_locks(void *, kmutex_t **, kmutex_t **);
105 1.1 jmcneill
106 1.52.4.1 christos static void pad_done_output(void *);
107 1.52.4.1 christos static void pad_swvol_codec(audio_filter_arg_t *);
108 1.22 jmcneill
109 1.52.4.2 martin static int pad_close(struct pad_softc *);
110 1.52.4.2 martin static int pad_read(struct pad_softc *, off_t *, struct uio *,
111 1.52.4.2 martin kauth_cred_t, int);
112 1.52.4.2 martin
113 1.52.4.2 martin static int fops_pad_close(struct file *);
114 1.52.4.2 martin static int fops_pad_read(struct file *, off_t *, struct uio *,
115 1.52.4.2 martin kauth_cred_t, int);
116 1.52.4.2 martin static int fops_pad_write(struct file *, off_t *, struct uio *,
117 1.52.4.2 martin kauth_cred_t, int);
118 1.52.4.2 martin static int fops_pad_ioctl(struct file *, u_long, void *);
119 1.52.4.2 martin static int fops_pad_kqfilter(struct file *, struct knote *);
120 1.52.4.2 martin static int fops_pad_poll(struct file *, int);
121 1.52.4.2 martin static int fops_pad_stat(struct file *, struct stat *);
122 1.52.4.2 martin static int fops_pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
123 1.52.4.2 martin struct uvm_object **, int *);
124 1.32 pgoyette
125 1.1 jmcneill static const struct audio_hw_if pad_hw_if = {
126 1.52.4.2 martin .query_format = pad_query_format,
127 1.52.4.2 martin .set_format = pad_set_format,
128 1.52.4.2 martin .start_output = pad_start_output,
129 1.52.4.2 martin .halt_output = pad_halt_output,
130 1.52.4.2 martin .getdev = pad_getdev,
131 1.52.4.2 martin .set_port = pad_set_port,
132 1.52.4.2 martin .get_port = pad_get_port,
133 1.52.4.2 martin .query_devinfo = pad_query_devinfo,
134 1.52.4.2 martin .get_props = pad_get_props,
135 1.52.4.2 martin .get_locks = pad_get_locks,
136 1.1 jmcneill };
137 1.1 jmcneill
138 1.1 jmcneill #define PAD_NFORMATS 1
139 1.1 jmcneill static const struct audio_format pad_formats[PAD_NFORMATS] = {
140 1.52.4.1 christos {
141 1.52.4.1 christos .mode = AUMODE_PLAY,
142 1.52.4.2 martin .encoding = AUDIO_ENCODING_SLINEAR_LE,
143 1.52.4.1 christos .validbits = PADPREC,
144 1.52.4.1 christos .precision = PADPREC,
145 1.52.4.1 christos .channels = PADCHAN,
146 1.52.4.1 christos .channel_mask = AUFMT_STEREO,
147 1.52.4.1 christos .frequency_type = 1,
148 1.52.4.1 christos .frequency = { PADFREQ },
149 1.52.4.1 christos },
150 1.1 jmcneill };
151 1.1 jmcneill
152 1.1 jmcneill extern void padattach(int);
153 1.1 jmcneill
154 1.52.4.2 martin static int pad_add_block(struct pad_softc *, uint8_t *, int);
155 1.52.4.2 martin static int pad_get_block(struct pad_softc *, pad_block_t *, int);
156 1.1 jmcneill
157 1.52.4.2 martin dev_type_open(cdev_pad_open);
158 1.41 nat dev_type_close(cdev_pad_close);
159 1.41 nat dev_type_read(cdev_pad_read);
160 1.1 jmcneill
161 1.1 jmcneill const struct cdevsw pad_cdevsw = {
162 1.52.4.2 martin .d_open = cdev_pad_open,
163 1.52.4.2 martin .d_close = cdev_pad_close,
164 1.52.4.2 martin .d_read = cdev_pad_read,
165 1.52.4.2 martin .d_write = nowrite,
166 1.52.4.2 martin .d_ioctl = noioctl,
167 1.52.4.2 martin .d_stop = nostop,
168 1.52.4.2 martin .d_tty = notty,
169 1.52.4.2 martin .d_poll = nopoll,
170 1.52.4.2 martin .d_mmap = nommap,
171 1.52.4.2 martin .d_kqfilter = nokqfilter,
172 1.52.4.2 martin .d_discard = nodiscard,
173 1.52.4.2 martin .d_flag = D_OTHER | D_MPSAFE,
174 1.1 jmcneill };
175 1.1 jmcneill
176 1.39 nat const struct fileops pad_fileops = {
177 1.52.4.2 martin .fo_name = "pad",
178 1.52.4.2 martin .fo_read = fops_pad_read,
179 1.52.4.2 martin .fo_write = fops_pad_write,
180 1.52.4.2 martin .fo_ioctl = fops_pad_ioctl,
181 1.52.4.2 martin .fo_fcntl = fnullop_fcntl,
182 1.52.4.2 martin .fo_stat = fops_pad_stat,
183 1.52.4.2 martin .fo_poll = fops_pad_poll,
184 1.52.4.2 martin .fo_close = fops_pad_close,
185 1.52.4.2 martin .fo_mmap = fops_pad_mmap,
186 1.52.4.2 martin .fo_kqfilter = fops_pad_kqfilter,
187 1.52.4.2 martin .fo_restart = fnullop_restart
188 1.39 nat };
189 1.39 nat
190 1.52.4.2 martin CFATTACH_DECL2_NEW(pad, sizeof(struct pad_softc),
191 1.52.4.2 martin pad_match, pad_attach, pad_detach,
192 1.5 dyoung NULL, NULL, pad_childdet);
193 1.1 jmcneill
194 1.1 jmcneill void
195 1.1 jmcneill padattach(int n)
196 1.1 jmcneill {
197 1.39 nat int error;
198 1.1 jmcneill
199 1.39 nat error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
200 1.39 nat if (error) {
201 1.1 jmcneill aprint_error("%s: couldn't register cfattach: %d\n",
202 1.39 nat pad_cd.cd_name, error);
203 1.1 jmcneill config_cfdriver_detach(&pad_cd);
204 1.1 jmcneill return;
205 1.1 jmcneill }
206 1.50 nat mutex_init(&padconfig, MUTEX_DEFAULT, IPL_NONE);
207 1.1 jmcneill
208 1.1 jmcneill return;
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill static int
212 1.52.4.2 martin pad_match(device_t parent, cfdata_t data, void *opaque)
213 1.52.4.2 martin {
214 1.52.4.2 martin
215 1.52.4.2 martin return 1;
216 1.52.4.2 martin }
217 1.52.4.2 martin
218 1.52.4.2 martin static void
219 1.52.4.2 martin pad_attach(device_t parent, device_t self, void *opaque)
220 1.52.4.2 martin {
221 1.52.4.2 martin
222 1.52.4.2 martin aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
223 1.52.4.2 martin }
224 1.52.4.2 martin
225 1.52.4.2 martin static int
226 1.52.4.2 martin pad_detach(device_t self, int flags)
227 1.52.4.2 martin {
228 1.52.4.2 martin struct pad_softc *sc;
229 1.52.4.2 martin int cmaj, mn;
230 1.52.4.2 martin
231 1.52.4.2 martin sc = device_private(self);
232 1.52.4.2 martin cmaj = cdevsw_lookup_major(&pad_cdevsw);
233 1.52.4.2 martin mn = device_unit(sc->sc_dev);
234 1.52.4.2 martin if (!sc->sc_dying)
235 1.52.4.2 martin vdevgone(cmaj, mn, mn, VCHR);
236 1.52.4.2 martin
237 1.52.4.2 martin return 0;
238 1.52.4.2 martin }
239 1.52.4.2 martin
240 1.52.4.2 martin static void
241 1.52.4.2 martin pad_childdet(device_t self, device_t child)
242 1.52.4.2 martin {
243 1.52.4.2 martin struct pad_softc *sc = device_private(self);
244 1.52.4.2 martin
245 1.52.4.2 martin sc->sc_audiodev = NULL;
246 1.52.4.2 martin }
247 1.52.4.2 martin
248 1.52.4.2 martin static int
249 1.52.4.2 martin pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
250 1.1 jmcneill {
251 1.1 jmcneill int l;
252 1.1 jmcneill
253 1.1 jmcneill if (sc->sc_buflen + blksize > PAD_BUFSIZE)
254 1.1 jmcneill return ENOBUFS;
255 1.1 jmcneill
256 1.1 jmcneill if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
257 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
258 1.1 jmcneill else {
259 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_wpos;
260 1.1 jmcneill memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
261 1.1 jmcneill memcpy(sc->sc_audiobuf, blk + l, blksize - l);
262 1.1 jmcneill }
263 1.1 jmcneill
264 1.1 jmcneill sc->sc_wpos += blksize;
265 1.52.4.1 christos if (sc->sc_wpos >= PAD_BUFSIZE)
266 1.1 jmcneill sc->sc_wpos -= PAD_BUFSIZE;
267 1.1 jmcneill
268 1.1 jmcneill sc->sc_buflen += blksize;
269 1.1 jmcneill
270 1.1 jmcneill return 0;
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static int
274 1.52.4.2 martin pad_get_block(struct pad_softc *sc, pad_block_t *pb, int blksize)
275 1.1 jmcneill {
276 1.1 jmcneill int l;
277 1.1 jmcneill
278 1.1 jmcneill KASSERT(pb != NULL);
279 1.1 jmcneill
280 1.1 jmcneill pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
281 1.1 jmcneill if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
282 1.1 jmcneill pb->pb_len = blksize;
283 1.1 jmcneill sc->sc_rpos += blksize;
284 1.1 jmcneill } else {
285 1.1 jmcneill l = PAD_BUFSIZE - sc->sc_rpos;
286 1.1 jmcneill pb->pb_len = l;
287 1.1 jmcneill sc->sc_rpos = 0;
288 1.1 jmcneill }
289 1.1 jmcneill sc->sc_buflen -= pb->pb_len;
290 1.1 jmcneill
291 1.1 jmcneill return 0;
292 1.1 jmcneill }
293 1.1 jmcneill
294 1.1 jmcneill int
295 1.52.4.2 martin cdev_pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
296 1.1 jmcneill {
297 1.52.4.2 martin struct pad_softc *sc;
298 1.39 nat struct file *fp;
299 1.39 nat device_t paddev;
300 1.39 nat cfdata_t cf;
301 1.39 nat int error, fd, i;
302 1.1 jmcneill
303 1.52 pgoyette error = 0;
304 1.51 pgoyette
305 1.50 nat mutex_enter(&padconfig);
306 1.41 nat if (PADUNIT(dev) == PADCLONER) {
307 1.41 nat for (i = 0; i < MAXDEVS; i++) {
308 1.41 nat if (device_lookup(&pad_cd, i) == NULL)
309 1.41 nat break;
310 1.41 nat }
311 1.41 nat if (i == MAXDEVS)
312 1.50 nat goto bad;
313 1.41 nat } else {
314 1.41 nat if (PADUNIT(dev) >= MAXDEVS)
315 1.50 nat goto bad;
316 1.41 nat i = PADUNIT(dev);
317 1.39 nat }
318 1.1 jmcneill
319 1.39 nat cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
320 1.39 nat cf->cf_name = pad_cd.cd_name;
321 1.39 nat cf->cf_atname = pad_cd.cd_name;
322 1.39 nat cf->cf_unit = i;
323 1.39 nat cf->cf_fstate = FSTATE_STAR;
324 1.39 nat
325 1.50 nat bool existing = false;
326 1.48 pgoyette paddev = device_lookup(&pad_cd, minor(dev));
327 1.48 pgoyette if (paddev == NULL)
328 1.41 nat paddev = config_attach_pseudo(cf);
329 1.50 nat else
330 1.50 nat existing = true;
331 1.48 pgoyette if (paddev == NULL)
332 1.50 nat goto bad;
333 1.41 nat
334 1.39 nat sc = device_private(paddev);
335 1.41 nat if (sc == NULL)
336 1.50 nat goto bad;
337 1.39 nat
338 1.50 nat if (sc->sc_open == 1) {
339 1.50 nat mutex_exit(&padconfig);
340 1.1 jmcneill return EBUSY;
341 1.50 nat }
342 1.39 nat
343 1.41 nat sc->sc_dev = paddev;
344 1.41 nat sc->sc_dying = false;
345 1.41 nat
346 1.41 nat if (PADUNIT(dev) == PADCLONER) {
347 1.41 nat error = fd_allocfile(&fp, &fd);
348 1.41 nat if (error) {
349 1.50 nat if (existing == false)
350 1.50 nat config_detach(sc->sc_dev, 0);
351 1.50 nat mutex_exit(&padconfig);
352 1.41 nat return error;
353 1.41 nat }
354 1.39 nat }
355 1.39 nat
356 1.39 nat cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
357 1.52.4.1 christos mutex_init(&sc->sc_cond_lock, MUTEX_DEFAULT, IPL_NONE);
358 1.39 nat mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
359 1.39 nat mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
360 1.52.4.1 christos callout_init(&sc->sc_pcallout, 0/*XXX?*/);
361 1.39 nat
362 1.39 nat sc->sc_swvol = 255;
363 1.39 nat sc->sc_buflen = 0;
364 1.39 nat sc->sc_rpos = sc->sc_wpos = 0;
365 1.39 nat sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
366 1.39 nat
367 1.39 nat if (!pmf_device_register(sc->sc_dev, NULL, NULL))
368 1.52.4.2 martin aprint_error_dev(sc->sc_dev,
369 1.52.4.2 martin "couldn't establish power handler\n");
370 1.39 nat
371 1.41 nat if (PADUNIT(dev) == PADCLONER) {
372 1.41 nat error = fd_clone(fp, fd, flags, &pad_fileops, sc);
373 1.41 nat KASSERT(error == EMOVEFD);
374 1.41 nat }
375 1.39 nat sc->sc_open = 1;
376 1.50 nat mutex_exit(&padconfig);
377 1.39 nat
378 1.39 nat return error;
379 1.50 nat bad:
380 1.50 nat mutex_exit(&padconfig);
381 1.50 nat return ENXIO;
382 1.1 jmcneill }
383 1.1 jmcneill
384 1.39 nat static int
385 1.41 nat pad_close(struct pad_softc *sc)
386 1.41 nat {
387 1.50 nat int rc;
388 1.50 nat
389 1.41 nat if (sc == NULL)
390 1.41 nat return ENXIO;
391 1.41 nat
392 1.50 nat mutex_enter(&padconfig);
393 1.50 nat config_deactivate(sc->sc_audiodev);
394 1.50 nat
395 1.50 nat /* Start draining existing accessors of the device. */
396 1.50 nat if ((rc = config_detach_children(sc->sc_dev,
397 1.50 nat DETACH_SHUTDOWN|DETACH_FORCE)) != 0) {
398 1.50 nat mutex_exit(&padconfig);
399 1.50 nat return rc;
400 1.50 nat }
401 1.50 nat
402 1.50 nat mutex_enter(&sc->sc_lock);
403 1.50 nat sc->sc_dying = true;
404 1.50 nat cv_broadcast(&sc->sc_condvar);
405 1.50 nat mutex_exit(&sc->sc_lock);
406 1.50 nat
407 1.50 nat KASSERT(sc->sc_open > 0);
408 1.50 nat sc->sc_open = 0;
409 1.50 nat
410 1.50 nat pmf_device_deregister(sc->sc_dev);
411 1.50 nat
412 1.52.4.1 christos mutex_destroy(&sc->sc_cond_lock);
413 1.50 nat mutex_destroy(&sc->sc_lock);
414 1.50 nat mutex_destroy(&sc->sc_intr_lock);
415 1.50 nat cv_destroy(&sc->sc_condvar);
416 1.50 nat
417 1.50 nat rc = config_detach(sc->sc_dev, 0);
418 1.50 nat mutex_exit(&padconfig);
419 1.50 nat
420 1.50 nat return rc;
421 1.41 nat }
422 1.41 nat
423 1.41 nat static int
424 1.41 nat fops_pad_close(struct file *fp)
425 1.1 jmcneill {
426 1.52.4.2 martin struct pad_softc *sc;
427 1.41 nat int error;
428 1.1 jmcneill
429 1.39 nat sc = fp->f_pad;
430 1.1 jmcneill
431 1.41 nat error = pad_close(sc);
432 1.41 nat
433 1.41 nat if (error == 0)
434 1.41 nat fp->f_pad = NULL;
435 1.41 nat
436 1.41 nat return error;
437 1.41 nat }
438 1.39 nat
439 1.41 nat int
440 1.41 nat cdev_pad_close(dev_t dev, int flags, int ifmt, struct lwp *l)
441 1.41 nat {
442 1.52.4.2 martin struct pad_softc *sc;
443 1.41 nat sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
444 1.39 nat
445 1.41 nat return pad_close(sc);
446 1.39 nat }
447 1.39 nat
448 1.39 nat static int
449 1.52.4.2 martin fops_pad_poll(struct file *fp, int events)
450 1.39 nat {
451 1.52.4.2 martin
452 1.52.4.2 martin return POLLERR;
453 1.39 nat }
454 1.39 nat
455 1.39 nat static int
456 1.52.4.2 martin fops_pad_kqfilter(struct file *fp, struct knote *kn)
457 1.39 nat {
458 1.39 nat struct pad_softc *sc;
459 1.39 nat dev_t dev;
460 1.39 nat
461 1.39 nat sc = fp->f_pad;
462 1.39 nat if (sc == NULL)
463 1.39 nat return EIO;
464 1.39 nat
465 1.52.4.2 martin dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
466 1.52.4.2 martin device_unit(sc->sc_dev));
467 1.39 nat
468 1.39 nat return seltrue_kqfilter(dev, kn);
469 1.39 nat }
470 1.39 nat
471 1.39 nat static int
472 1.52.4.2 martin fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
473 1.39 nat {
474 1.52.4.2 martin
475 1.39 nat return ENODEV;
476 1.39 nat }
477 1.39 nat
478 1.39 nat static int
479 1.52.4.2 martin fops_pad_stat(struct file *fp, struct stat *st)
480 1.39 nat {
481 1.39 nat struct pad_softc *sc;
482 1.39 nat
483 1.39 nat sc = fp->f_pad;
484 1.39 nat if (sc == NULL)
485 1.39 nat return EIO;
486 1.39 nat
487 1.39 nat memset(st, 0, sizeof(*st));
488 1.39 nat
489 1.52.4.2 martin st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
490 1.52.4.2 martin device_unit(sc->sc_dev));
491 1.39 nat
492 1.39 nat st->st_uid = kauth_cred_geteuid(fp->f_cred);
493 1.39 nat st->st_gid = kauth_cred_getegid(fp->f_cred);
494 1.39 nat st->st_mode = S_IFCHR;
495 1.1 jmcneill
496 1.1 jmcneill return 0;
497 1.1 jmcneill }
498 1.1 jmcneill
499 1.39 nat static int
500 1.52.4.2 martin fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
501 1.52.4.2 martin int *advicep, struct uvm_object **uobjp, int *maxprotp)
502 1.39 nat {
503 1.52.4.2 martin
504 1.39 nat return 1;
505 1.39 nat }
506 1.39 nat
507 1.41 nat int
508 1.41 nat cdev_pad_read(dev_t dev, struct uio *uio, int ioflag)
509 1.41 nat {
510 1.52.4.2 martin struct pad_softc *sc;
511 1.52.4.2 martin
512 1.41 nat sc = device_private(device_lookup(&pad_cd, PADUNIT(dev)));
513 1.41 nat if (sc == NULL)
514 1.41 nat return ENXIO;
515 1.41 nat
516 1.41 nat return pad_read(sc, NULL, uio, NULL, ioflag);
517 1.41 nat }
518 1.41 nat
519 1.39 nat static int
520 1.41 nat fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
521 1.52.4.2 martin int ioflag)
522 1.41 nat {
523 1.52.4.2 martin struct pad_softc *sc;
524 1.41 nat
525 1.41 nat sc = fp->f_pad;
526 1.41 nat if (sc == NULL)
527 1.41 nat return ENXIO;
528 1.41 nat
529 1.41 nat return pad_read(sc, offp, uio, cred, ioflag);
530 1.41 nat }
531 1.41 nat
532 1.41 nat static int
533 1.41 nat pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
534 1.52.4.2 martin int ioflag)
535 1.1 jmcneill {
536 1.1 jmcneill pad_block_t pb;
537 1.52.4.1 christos int len;
538 1.52.4.1 christos int err;
539 1.1 jmcneill
540 1.1 jmcneill err = 0;
541 1.52.4.1 christos DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
542 1.27 nat while (uio->uio_resid > 0 && !err) {
543 1.52.4.1 christos mutex_enter(&sc->sc_cond_lock);
544 1.52.4.1 christos if (sc->sc_buflen == 0) {
545 1.52.4.1 christos DPRINTF("%s: wait\n", __func__);
546 1.52.4.1 christos err = cv_wait_sig(&sc->sc_condvar, &sc->sc_cond_lock);
547 1.52.4.1 christos DPRINTF("%s: wake up %d\n", __func__, err);
548 1.52.4.1 christos mutex_exit(&sc->sc_cond_lock);
549 1.52.4.1 christos if (err) {
550 1.52.4.1 christos if (err == ERESTART)
551 1.52.4.1 christos err = EINTR;
552 1.52.4.1 christos break;
553 1.24 nat }
554 1.52.4.1 christos if (sc->sc_buflen == 0)
555 1.52.4.1 christos break;
556 1.17 jmcneill continue;
557 1.17 jmcneill }
558 1.1 jmcneill
559 1.52.4.1 christos len = uimin(uio->uio_resid, sc->sc_buflen);
560 1.52.4.1 christos err = pad_get_block(sc, &pb, len);
561 1.52.4.1 christos mutex_exit(&sc->sc_cond_lock);
562 1.52.4.1 christos if (err)
563 1.24 nat break;
564 1.52.4.1 christos DPRINTF("%s: move %d\n", __func__, pb.pb_len);
565 1.52.4.1 christos uiomove(pb.pb_ptr, pb.pb_len, uio);
566 1.1 jmcneill }
567 1.1 jmcneill
568 1.1 jmcneill return err;
569 1.1 jmcneill }
570 1.1 jmcneill
571 1.1 jmcneill static int
572 1.52.4.2 martin fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
573 1.52.4.2 martin int ioflag)
574 1.39 nat {
575 1.52.4.2 martin
576 1.39 nat return EOPNOTSUPP;
577 1.39 nat }
578 1.39 nat
579 1.39 nat static int
580 1.52.4.1 christos pad_query_format(void *opaque, audio_format_query_t *afp)
581 1.26 nat {
582 1.26 nat
583 1.52.4.1 christos return audio_query_format(pad_formats, PAD_NFORMATS, afp);
584 1.26 nat }
585 1.26 nat
586 1.26 nat static int
587 1.52.4.1 christos pad_set_format(void *opaque, int setmode,
588 1.52.4.1 christos const audio_params_t *play, const audio_params_t *rec,
589 1.52.4.1 christos audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
590 1.1 jmcneill {
591 1.52.4.2 martin struct pad_softc *sc;
592 1.1 jmcneill
593 1.52.4.2 martin sc = (struct pad_softc *)opaque;
594 1.1 jmcneill
595 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
596 1.17 jmcneill
597 1.52.4.1 christos /* XXX playback only */
598 1.52.4.1 christos pfil->codec = pad_swvol_codec;
599 1.52.4.1 christos pfil->context = sc;
600 1.2 jmcneill
601 1.1 jmcneill return 0;
602 1.1 jmcneill }
603 1.1 jmcneill
604 1.1 jmcneill static int
605 1.1 jmcneill pad_start_output(void *opaque, void *block, int blksize,
606 1.1 jmcneill void (*intr)(void *), void *intrarg)
607 1.1 jmcneill {
608 1.52.4.2 martin struct pad_softc *sc;
609 1.1 jmcneill int err;
610 1.52.4.1 christos int ms;
611 1.1 jmcneill
612 1.52.4.2 martin sc = (struct pad_softc *)opaque;
613 1.1 jmcneill
614 1.52.4.1 christos KASSERT(mutex_owned(&sc->sc_intr_lock));
615 1.1 jmcneill
616 1.1 jmcneill sc->sc_intr = intr;
617 1.1 jmcneill sc->sc_intrarg = intrarg;
618 1.1 jmcneill sc->sc_blksize = blksize;
619 1.1 jmcneill
620 1.52.4.1 christos DPRINTF("%s: blksize=%d\n", __func__, blksize);
621 1.52.4.1 christos mutex_enter(&sc->sc_cond_lock);
622 1.1 jmcneill err = pad_add_block(sc, block, blksize);
623 1.52.4.1 christos mutex_exit(&sc->sc_cond_lock);
624 1.17 jmcneill cv_broadcast(&sc->sc_condvar);
625 1.52.4.2 martin if (err)
626 1.52.4.2 martin return err;
627 1.1 jmcneill
628 1.52.4.1 christos ms = blksize * 1000 / PADCHAN / (PADPREC / NBBY) / PADFREQ;
629 1.52.4.1 christos DPRINTF("%s: callout ms=%d\n", __func__, ms);
630 1.52.4.1 christos callout_reset(&sc->sc_pcallout, mstohz(ms), pad_done_output, sc);
631 1.52.4.1 christos
632 1.52.4.2 martin return 0;
633 1.1 jmcneill }
634 1.1 jmcneill
635 1.1 jmcneill static int
636 1.1 jmcneill pad_halt_output(void *opaque)
637 1.1 jmcneill {
638 1.52.4.2 martin struct pad_softc *sc;
639 1.1 jmcneill
640 1.52.4.2 martin sc = (struct pad_softc *)opaque;
641 1.17 jmcneill
642 1.52.4.1 christos DPRINTF("%s\n", __func__);
643 1.52.4.1 christos KASSERT(mutex_owned(&sc->sc_intr_lock));
644 1.17 jmcneill
645 1.52.4.1 christos cv_broadcast(&sc->sc_condvar);
646 1.52.4.1 christos callout_stop(&sc->sc_pcallout);
647 1.1 jmcneill sc->sc_intr = NULL;
648 1.1 jmcneill sc->sc_intrarg = NULL;
649 1.1 jmcneill sc->sc_buflen = 0;
650 1.1 jmcneill sc->sc_rpos = sc->sc_wpos = 0;
651 1.1 jmcneill
652 1.1 jmcneill return 0;
653 1.1 jmcneill }
654 1.1 jmcneill
655 1.52.4.1 christos static void
656 1.52.4.1 christos pad_done_output(void *arg)
657 1.52.4.1 christos {
658 1.52.4.2 martin struct pad_softc *sc;
659 1.52.4.1 christos
660 1.52.4.1 christos DPRINTF("%s\n", __func__);
661 1.52.4.2 martin sc = (struct pad_softc *)arg;
662 1.52.4.1 christos callout_stop(&sc->sc_pcallout);
663 1.52.4.1 christos
664 1.52.4.1 christos mutex_enter(&sc->sc_intr_lock);
665 1.52.4.1 christos (*sc->sc_intr)(sc->sc_intrarg);
666 1.52.4.1 christos mutex_exit(&sc->sc_intr_lock);
667 1.52.4.1 christos }
668 1.52.4.1 christos
669 1.1 jmcneill static int
670 1.1 jmcneill pad_getdev(void *opaque, struct audio_device *ret)
671 1.1 jmcneill {
672 1.52.4.2 martin
673 1.16 jmcneill strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
674 1.16 jmcneill strlcpy(ret->version, osrelease, sizeof(ret->version));
675 1.16 jmcneill strlcpy(ret->config, "pad", sizeof(ret->config));
676 1.1 jmcneill
677 1.1 jmcneill return 0;
678 1.1 jmcneill }
679 1.1 jmcneill
680 1.1 jmcneill static int
681 1.1 jmcneill pad_set_port(void *opaque, mixer_ctrl_t *mc)
682 1.1 jmcneill {
683 1.52.4.2 martin struct pad_softc *sc;
684 1.2 jmcneill
685 1.52.4.2 martin sc = (struct pad_softc *)opaque;
686 1.2 jmcneill
687 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
688 1.17 jmcneill
689 1.2 jmcneill switch (mc->dev) {
690 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
691 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
692 1.52.4.1 christos if (mc->un.value.num_channels != 1)
693 1.52.4.1 christos return EINVAL;
694 1.2 jmcneill sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
695 1.2 jmcneill return 0;
696 1.2 jmcneill }
697 1.2 jmcneill
698 1.1 jmcneill return ENXIO;
699 1.1 jmcneill }
700 1.1 jmcneill
701 1.1 jmcneill static int
702 1.1 jmcneill pad_get_port(void *opaque, mixer_ctrl_t *mc)
703 1.1 jmcneill {
704 1.52.4.2 martin struct pad_softc *sc;
705 1.2 jmcneill
706 1.52.4.2 martin sc = (struct pad_softc *)opaque;
707 1.2 jmcneill
708 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
709 1.17 jmcneill
710 1.2 jmcneill switch (mc->dev) {
711 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
712 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
713 1.52.4.1 christos if (mc->un.value.num_channels != 1)
714 1.52.4.1 christos return EINVAL;
715 1.2 jmcneill mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
716 1.2 jmcneill return 0;
717 1.2 jmcneill }
718 1.2 jmcneill
719 1.1 jmcneill return ENXIO;
720 1.1 jmcneill }
721 1.1 jmcneill
722 1.1 jmcneill static int
723 1.1 jmcneill pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
724 1.1 jmcneill {
725 1.52.4.2 martin struct pad_softc *sc __diagused;
726 1.2 jmcneill
727 1.52.4.2 martin sc = (struct pad_softc *)opaque;
728 1.2 jmcneill
729 1.17 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
730 1.17 jmcneill
731 1.2 jmcneill switch (di->index) {
732 1.2 jmcneill case PAD_OUTPUT_CLASS:
733 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
734 1.2 jmcneill strcpy(di->label.name, AudioCoutputs);
735 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
736 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
737 1.2 jmcneill return 0;
738 1.2 jmcneill case PAD_INPUT_CLASS:
739 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
740 1.2 jmcneill strcpy(di->label.name, AudioCinputs);
741 1.2 jmcneill di->type = AUDIO_MIXER_CLASS;
742 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
743 1.2 jmcneill return 0;
744 1.2 jmcneill case PAD_OUTPUT_MASTER_VOLUME:
745 1.2 jmcneill di->mixer_class = PAD_OUTPUT_CLASS;
746 1.2 jmcneill strcpy(di->label.name, AudioNmaster);
747 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
748 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
749 1.2 jmcneill di->un.v.num_channels = 1;
750 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
751 1.2 jmcneill return 0;
752 1.2 jmcneill case PAD_INPUT_DAC_VOLUME:
753 1.2 jmcneill di->mixer_class = PAD_INPUT_CLASS;
754 1.2 jmcneill strcpy(di->label.name, AudioNdac);
755 1.2 jmcneill di->type = AUDIO_MIXER_VALUE;
756 1.2 jmcneill di->next = di->prev = AUDIO_MIXER_LAST;
757 1.2 jmcneill di->un.v.num_channels = 1;
758 1.2 jmcneill strcpy(di->un.v.units.name, AudioNvolume);
759 1.2 jmcneill return 0;
760 1.2 jmcneill }
761 1.2 jmcneill
762 1.1 jmcneill return ENXIO;
763 1.1 jmcneill }
764 1.1 jmcneill
765 1.1 jmcneill static int
766 1.1 jmcneill pad_get_props(void *opaque)
767 1.1 jmcneill {
768 1.17 jmcneill
769 1.52.4.1 christos return AUDIO_PROP_PLAYBACK;
770 1.1 jmcneill }
771 1.13 ahoka
772 1.17 jmcneill static void
773 1.17 jmcneill pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
774 1.17 jmcneill {
775 1.52.4.2 martin struct pad_softc *sc;
776 1.17 jmcneill
777 1.52.4.2 martin sc = (struct pad_softc *)opaque;
778 1.17 jmcneill
779 1.17 jmcneill *intr = &sc->sc_intr_lock;
780 1.17 jmcneill *thread = &sc->sc_lock;
781 1.17 jmcneill }
782 1.17 jmcneill
783 1.22 jmcneill static void
784 1.52.4.1 christos pad_swvol_codec(audio_filter_arg_t *arg)
785 1.22 jmcneill {
786 1.52.4.1 christos struct pad_softc *sc = arg->context;
787 1.52.4.1 christos const aint_t *src;
788 1.52.4.1 christos aint_t *dst;
789 1.52.4.1 christos u_int sample_count;
790 1.52.4.1 christos u_int i;
791 1.52.4.1 christos
792 1.52.4.1 christos src = arg->src;
793 1.52.4.1 christos dst = arg->dst;
794 1.52.4.1 christos sample_count = arg->count * arg->srcfmt->channels;
795 1.52.4.1 christos for (i = 0; i < sample_count; i++) {
796 1.52.4.1 christos aint2_t v = (aint2_t)(*src++);
797 1.52.4.1 christos v = v * sc->sc_swvol / 255;
798 1.52.4.1 christos *dst++ = (aint_t)v;
799 1.52.4.1 christos }
800 1.22 jmcneill }
801 1.22 jmcneill
802 1.44 pgoyette MODULE(MODULE_CLASS_DRIVER, pad, "audio");
803 1.44 pgoyette
804 1.13 ahoka #ifdef _MODULE
805 1.13 ahoka
806 1.48 pgoyette #include "ioconf.c"
807 1.45 pgoyette
808 1.48 pgoyette devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
809 1.13 ahoka
810 1.48 pgoyette /*
811 1.48 pgoyette * We need our own version of cfattach since config(1)'s ioconf does not
812 1.48 pgoyette * generate what we need
813 1.48 pgoyette */
814 1.13 ahoka
815 1.48 pgoyette static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };
816 1.45 pgoyette
817 1.48 pgoyette static struct cfattachinit pad_cfattach[] = {
818 1.45 pgoyette { "pad", pad_cfattachinit },
819 1.45 pgoyette { NULL, NULL }
820 1.45 pgoyette };
821 1.47 pgoyette #endif
822 1.45 pgoyette
823 1.13 ahoka static int
824 1.13 ahoka pad_modcmd(modcmd_t cmd, void *arg)
825 1.13 ahoka {
826 1.44 pgoyette int error = 0;
827 1.13 ahoka
828 1.13 ahoka switch (cmd) {
829 1.13 ahoka case MODULE_CMD_INIT:
830 1.44 pgoyette #ifdef _MODULE
831 1.48 pgoyette pad_cfattach[1] = cfattach_ioconf_pad[0];
832 1.48 pgoyette error = config_init_component(cfdriver_ioconf_pad,
833 1.48 pgoyette pad_cfattach, cfdata_ioconf_pad);
834 1.45 pgoyette if (error)
835 1.44 pgoyette break;
836 1.13 ahoka
837 1.25 msaitoh error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
838 1.45 pgoyette &pad_cdevsw, &cmajor);
839 1.13 ahoka if (error) {
840 1.48 pgoyette config_fini_component(cfdriver_ioconf_pad,
841 1.48 pgoyette pad_cfattach, cfdata_ioconf_pad);
842 1.44 pgoyette break;
843 1.13 ahoka }
844 1.50 nat mutex_init(&padconfig, MUTEX_DEFAULT, IPL_NONE);
845 1.13 ahoka
846 1.44 pgoyette #endif
847 1.45 pgoyette break;
848 1.13 ahoka
849 1.13 ahoka case MODULE_CMD_FINI:
850 1.44 pgoyette #ifdef _MODULE
851 1.45 pgoyette error = devsw_detach(NULL, &pad_cdevsw);
852 1.45 pgoyette if (error)
853 1.45 pgoyette break;
854 1.45 pgoyette
855 1.48 pgoyette error = config_fini_component(cfdriver_ioconf_pad,
856 1.48 pgoyette pad_cfattach, cfdata_ioconf_pad);
857 1.13 ahoka if (error) {
858 1.49 pgoyette devsw_attach(pad_cd.cd_name, NULL, &bmajor,
859 1.45 pgoyette &pad_cdevsw, &cmajor);
860 1.44 pgoyette break;
861 1.13 ahoka }
862 1.50 nat mutex_destroy(&padconfig);
863 1.44 pgoyette #endif
864 1.45 pgoyette break;
865 1.13 ahoka
866 1.13 ahoka default:
867 1.44 pgoyette error = ENOTTY;
868 1.13 ahoka }
869 1.44 pgoyette
870 1.44 pgoyette return error;
871 1.13 ahoka }
872