pad.c revision 1.1 1 /* $NetBSD: pad.c,v 1.1 2007/11/11 17:37:45 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jared D. McNeill.
18 * 4. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.1 2007/11/11 17:37:45 jmcneill Exp $");
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/conf.h>
41 #include <sys/buf.h>
42 #include <sys/kmem.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/proc.h>
46 #include <sys/condvar.h>
47 #include <sys/select.h>
48 #include <sys/audioio.h>
49
50 #include <dev/audio_if.h>
51 #include <dev/audiovar.h>
52 #include <dev/auconv.h>
53
54 #define PADUNIT(x) minor(x)
55
56 extern struct cfdriver pad_cd;
57
58 static struct audio_device pad_device = {
59 "Pseudo Audio",
60 "1.0",
61 "pad",
62 };
63
64 typedef struct pad_block {
65 uint8_t *pb_ptr;
66 int pb_len;
67 } pad_block_t;
68
69 typedef struct pad_softc {
70 struct device sc_dev;
71
72 int sc_open;
73 struct audio_encoding_set *sc_encodings;
74 void (*sc_intr)(void *);
75 void *sc_intrarg;
76
77 kcondvar_t sc_condvar;
78 kmutex_t sc_mutex;
79
80 struct audio_softc *sc_audiodev;
81 int sc_blksize;
82
83 #define PAD_BLKSIZE 1024
84 #define PAD_BUFSIZE 65536
85 uint8_t sc_audiobuf[PAD_BUFSIZE];
86 uint32_t sc_buflen;
87 uint32_t sc_rpos, sc_wpos;
88 } pad_softc_t;
89
90 static int pad_match(struct device *, struct cfdata *, void *);
91 static void pad_attach(struct device *, struct device *, void *);
92
93 static int pad_query_encoding(void *, struct audio_encoding *);
94 static int pad_set_params(void *, int, int,
95 audio_params_t *, audio_params_t *,
96 stream_filter_list_t *, stream_filter_list_t *);
97 static int pad_start_output(void *, void *, int,
98 void (*)(void *), void *);
99 static int pad_start_input(void *, void *, int,
100 void (*)(void *), void *);
101 static int pad_halt_output(void *);
102 static int pad_halt_input(void *);
103 static int pad_getdev(void *, struct audio_device *);
104 static int pad_set_port(void *, mixer_ctrl_t *);
105 static int pad_get_port(void *, mixer_ctrl_t *);
106 static int pad_query_devinfo(void *, mixer_devinfo_t *);
107 static int pad_get_props(void *);
108 static int pad_round_blocksize(void *, int, int, const audio_params_t *);
109
110 static const struct audio_hw_if pad_hw_if = {
111 .query_encoding = pad_query_encoding,
112 .set_params = pad_set_params,
113 .start_output = pad_start_output,
114 .start_input = pad_start_input,
115 .halt_output = pad_halt_output,
116 .halt_input = pad_halt_input,
117 .getdev = pad_getdev,
118 .set_port = pad_set_port,
119 .get_port = pad_get_port,
120 .query_devinfo = pad_query_devinfo,
121 .get_props = pad_get_props,
122 .round_blocksize = pad_round_blocksize,
123 };
124
125 #define PAD_NFORMATS 1
126 static const struct audio_format pad_formats[PAD_NFORMATS] = {
127 { NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
128 2, AUFMT_STEREO, 1, { 44100 } },
129 };
130
131 extern void padattach(int);
132
133 static pad_softc_t * pad_find_softc(dev_t);
134 static int pad_add_block(pad_softc_t *, uint8_t *, int);
135 static int pad_get_block(pad_softc_t *, pad_block_t *, int);
136
137 dev_type_open(pad_open);
138 dev_type_close(pad_close);
139 dev_type_read(pad_read);
140
141 const struct cdevsw pad_cdevsw = {
142 .d_open = pad_open,
143 .d_close = pad_close,
144 .d_read = pad_read,
145 .d_write = nowrite,
146 .d_ioctl = noioctl,
147 .d_stop = nostop,
148 .d_tty = notty,
149 .d_poll = nopoll,
150 .d_mmap = nommap,
151 .d_kqfilter = nokqfilter,
152 .d_flag = D_OTHER,
153 };
154
155 CFATTACH_DECL(pad, sizeof(pad_softc_t), pad_match, pad_attach, NULL, NULL);
156
157 void
158 padattach(int n)
159 {
160 int i, err;
161 struct cfdata *cf;
162
163 #ifdef DEBUG
164 printf("pad: requested %d units\n", n);
165 #endif
166
167 err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
168 if (err) {
169 aprint_error("%s: couldn't register cfattach: %d\n",
170 pad_cd.cd_name, err);
171 config_cfdriver_detach(&pad_cd);
172 return;
173 }
174
175 for (i = 0; i < n; i++) {
176 cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
177 if (cf == NULL) {
178 aprint_error("%s: couldn't allocate cfdata\n",
179 pad_cd.cd_name);
180 continue;
181 }
182 cf->cf_name = pad_cd.cd_name;
183 cf->cf_atname = pad_cd.cd_name;
184 cf->cf_unit = i;
185 cf->cf_fstate = FSTATE_STAR;
186
187 (void)config_attach_pseudo(cf);
188 }
189
190 return;
191 }
192
193 static pad_softc_t *
194 pad_find_softc(dev_t dev)
195 {
196 int unit;
197
198 unit = PADUNIT(dev);
199 if (unit >= pad_cd.cd_ndevs)
200 return NULL;
201
202 return pad_cd.cd_devs[unit];
203 }
204
205 static int
206 pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
207 {
208 int l;
209
210 if (sc->sc_buflen + blksize > PAD_BUFSIZE)
211 return ENOBUFS;
212
213 if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
214 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
215 else {
216 l = PAD_BUFSIZE - sc->sc_wpos;
217 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
218 memcpy(sc->sc_audiobuf, blk + l, blksize - l);
219 }
220
221 sc->sc_wpos += blksize;
222 if (sc->sc_wpos > PAD_BUFSIZE)
223 sc->sc_wpos -= PAD_BUFSIZE;
224
225 sc->sc_buflen += blksize;
226
227 return 0;
228 }
229
230 static int
231 pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
232 {
233 int l;
234
235 KASSERT(pb != NULL);
236
237 if (sc->sc_buflen < blksize)
238 return ERESTART;
239
240 pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
241 if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
242 pb->pb_len = blksize;
243 sc->sc_rpos += blksize;
244 } else {
245 l = PAD_BUFSIZE - sc->sc_rpos;
246 pb->pb_len = l;
247 sc->sc_rpos = 0;
248 }
249 sc->sc_buflen -= pb->pb_len;
250
251 return 0;
252 }
253
254 static int
255 pad_match(struct device *parent, struct cfdata *data, void *opaque)
256 {
257 return 1;
258 }
259
260 static void
261 pad_attach(struct device *parent, struct device *self, void *opaque)
262 {
263 pad_softc_t *sc;
264
265 sc = (pad_softc_t *)self;
266
267 aprint_normal_dev(self, "Pseudo Audio Device\n");
268 aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
269
270 sc->sc_open = 0;
271 if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
272 &sc->sc_encodings) != 0) {
273 aprint_error_dev(self, "couldn't create encodings\n");
274 return;
275 }
276
277 cv_init(&sc->sc_condvar, device_xname(self));
278 mutex_init(&sc->sc_mutex, MUTEX_DRIVER, IPL_AUDIO);
279
280 sc->sc_buflen = 0;
281 sc->sc_rpos = sc->sc_wpos = 0;
282 sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, &sc->sc_dev);
283
284 return;
285 }
286
287 int
288 pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
289 {
290 pad_softc_t *sc;
291
292 sc = pad_find_softc(dev);
293 if (sc == NULL)
294 return ENODEV;
295
296 if (sc->sc_open++) {
297 sc->sc_open--;
298 return EBUSY;
299 }
300
301 return 0;
302 }
303
304 int
305 pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
306 {
307 pad_softc_t *sc;
308
309 sc = pad_find_softc(dev);
310 if (sc == NULL)
311 return ENODEV;
312
313 KASSERT(sc->sc_open > 0);
314 sc->sc_open--;
315
316 return 0;
317 }
318
319 int
320 pad_read(dev_t dev, struct uio *uio, int flags)
321 {
322 pad_softc_t *sc;
323 pad_block_t pb;
324 void (*intr)(void *);
325 void *intrarg;
326 int err;
327
328 sc = pad_find_softc(dev);
329 if (sc == NULL)
330 return ENODEV;
331
332 err = 0;
333
334 intr = sc->sc_intr;
335 intrarg = sc->sc_intrarg;
336
337 while (uio->uio_resid > 0 && !err) {
338 err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
339 if (!err)
340 err = uiomove(pb.pb_ptr, pb.pb_len, uio);
341 else {
342 if (intr) {
343 (*intr)(intrarg);
344 intr = sc->sc_intr;
345 intrarg = sc->sc_intrarg;
346 err = 0;
347 continue;
348 }
349
350 mutex_enter(&sc->sc_mutex);
351 err = cv_timedwait_sig(&sc->sc_condvar, &sc->sc_mutex,
352 hz/100);
353 if (err != 0 && err != EWOULDBLOCK) {
354 mutex_exit(&sc->sc_mutex);
355 aprint_error_dev(&sc->sc_dev,
356 "cv_timedwait_sig returned %d\n", err);
357 return EINTR;
358 }
359 intr = sc->sc_intr;
360 intrarg = sc->sc_intrarg;
361 mutex_exit(&sc->sc_mutex);
362 err = 0;
363 }
364 }
365
366 if (intr)
367 (*intr)(intrarg);
368
369 return err;
370 }
371
372 static int
373 pad_query_encoding(void *opaque, struct audio_encoding *ae)
374 {
375 pad_softc_t *sc;
376
377 sc = (pad_softc_t *)opaque;
378
379 return auconv_query_encoding(sc->sc_encodings, ae);
380 }
381
382 static int
383 pad_set_params(void *opaque, int setmode, int usemode,
384 audio_params_t *play, audio_params_t *rec,
385 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
386 {
387 pad_softc_t *sc;
388
389 sc = (pad_softc_t *)opaque;
390
391 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
392 play, true, pfil) < 0)
393 return EINVAL;
394 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
395 rec, true, rfil) < 0)
396 return EINVAL;
397
398 return 0;
399 }
400
401 static int
402 pad_start_output(void *opaque, void *block, int blksize,
403 void (*intr)(void *), void *intrarg)
404 {
405 pad_softc_t *sc;
406 int err;
407
408 sc = (pad_softc_t *)opaque;
409
410 mutex_enter(&sc->sc_mutex);
411
412 sc->sc_intr = intr;
413 sc->sc_intrarg = intrarg;
414 sc->sc_blksize = blksize;
415
416 err = pad_add_block(sc, block, blksize);
417
418 cv_signal(&sc->sc_condvar);
419
420 mutex_exit(&sc->sc_mutex);
421
422 return err;
423 }
424
425 static int
426 pad_start_input(void *opaque, void *block, int blksize,
427 void (*intr)(void *), void *intrarg)
428 {
429 return EINVAL;
430 }
431
432 static int
433 pad_halt_output(void *opaque)
434 {
435 pad_softc_t *sc;
436
437 sc = (pad_softc_t *)opaque;
438 sc->sc_intr = NULL;
439 sc->sc_intrarg = NULL;
440 sc->sc_buflen = 0;
441 sc->sc_rpos = sc->sc_wpos = 0;
442
443 return 0;
444 }
445
446 static int
447 pad_halt_input(void *opaque)
448 {
449 return 0;
450 }
451
452 static int
453 pad_getdev(void *opaque, struct audio_device *ret)
454 {
455
456 *ret = pad_device;
457
458 return 0;
459 }
460
461 static int
462 pad_set_port(void *opaque, mixer_ctrl_t *mc)
463 {
464 return ENXIO;
465 }
466
467 static int
468 pad_get_port(void *opaque, mixer_ctrl_t *mc)
469 {
470 return ENXIO;
471 }
472
473 static int
474 pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
475 {
476 return ENXIO;
477 }
478
479 static int
480 pad_get_props(void *opaque)
481 {
482 return 0;
483 }
484
485 static int
486 pad_round_blocksize(void *opaque, int blksize, int mode,
487 const audio_params_t *p)
488 {
489 return PAD_BLKSIZE;
490 }
491