Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/* $NetBSD: pad.c,v 1.85 2023/05/27 14:51:47 nat Exp $ */

/*-
 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.85 2023/05/27 14:51:47 nat Exp $");

#include <sys/param.h>
#include <sys/types.h>

#include <sys/audioio.h>
#include <sys/buf.h>
#include <sys/condvar.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/vnode.h>

#include <dev/audio/audio_if.h>
#include <dev/audio/audiovar.h>

#include <dev/pad/padvar.h>

#include "ioconf.h"

/* #define PAD_DEBUG */
#ifdef PAD_DEBUG
#define DPRINTF(fmt...)	printf(fmt)
#else
#define DPRINTF(fmt...) /**/
#endif

#define PADFREQ		44100
#define PADCHAN		2
#define PADPREC		16

typedef struct pad_block {
	uint8_t		*pb_ptr;
	int		pb_len;
} pad_block_t;

enum {
	PAD_OUTPUT_CLASS,
	PAD_INPUT_CLASS,
	PAD_OUTPUT_MASTER_VOLUME,
	PAD_INPUT_DAC_VOLUME,
	PAD_ENUM_LAST,
};

static int	pad_match(device_t, cfdata_t, void *);
static void	pad_attach(device_t, device_t, void *);
static int	pad_detach(device_t, int);
static void	pad_childdet(device_t, device_t);

static int	pad_query_format(void *, audio_format_query_t *);
static int	pad_set_format(void *, int,
		    const audio_params_t *, const audio_params_t *,
		    audio_filter_reg_t *, audio_filter_reg_t *);
static int	pad_start_output(void *, void *, int,
		    void (*)(void *), void *);
static int	pad_halt_output(void *);
static int	pad_getdev(void *, struct audio_device *);
static int	pad_set_port(void *, mixer_ctrl_t *);
static int	pad_get_port(void *, mixer_ctrl_t *);
static int	pad_query_devinfo(void *, mixer_devinfo_t *);
static int	pad_get_props(void *);
static void	pad_get_locks(void *, kmutex_t **, kmutex_t **);

static void	pad_done_output(void *);
static void	pad_swvol_codec(audio_filter_arg_t *);

static void	pad_close(struct pad_softc *);
static int	pad_read(struct pad_softc *, off_t *, struct uio *,
		    kauth_cred_t, int);

static int	fops_pad_close(struct file *);
static int	fops_pad_read(struct file *, off_t *, struct uio *,
		    kauth_cred_t, int);
static int	fops_pad_write(struct file *, off_t *, struct uio *,
		    kauth_cred_t, int);
static int	fops_pad_ioctl(struct file *, u_long, void *);
static int	fops_pad_kqfilter(struct file *, struct knote *);
static int	fops_pad_poll(struct file *, int);
static int	fops_pad_stat(struct file *, struct stat *);
static int	fops_pad_mmap(struct file *, off_t *, size_t, int, int *, int *,
		    struct uvm_object **, int *);

static const struct audio_hw_if pad_hw_if = {
	.query_format	= pad_query_format,
	.set_format	= pad_set_format,
	.start_output	= pad_start_output,
	.halt_output	= pad_halt_output,
	.getdev		= pad_getdev,
	.set_port	= pad_set_port,
	.get_port	= pad_get_port,
	.query_devinfo	= pad_query_devinfo,
	.get_props	= pad_get_props,
	.get_locks	= pad_get_locks,
};

#define PAD_NFORMATS	1
static const struct audio_format pad_formats[PAD_NFORMATS] = {
	{
		.mode		= AUMODE_PLAY,
		.encoding	= AUDIO_ENCODING_SLINEAR_LE,
		.validbits	= PADPREC,
		.precision	= PADPREC,
		.channels	= PADCHAN,
		.channel_mask	= AUFMT_STEREO,
		.frequency_type	= 1,
		.frequency	= { PADFREQ },
	},
};

extern void	padattach(int);

static int	pad_add_block(struct pad_softc *, uint8_t *, int);
static int	pad_get_block(struct pad_softc *, pad_block_t *, int, int);

static dev_type_open(pad_open);

const struct cdevsw pad_cdevsw = {
	.d_open		= pad_open,
	.d_close	= noclose,
	.d_read		= noread,
	.d_write	= nowrite,
	.d_ioctl	= noioctl,
	.d_stop		= nostop,
	.d_tty		= notty,
	.d_poll		= nopoll,
	.d_mmap		= nommap,
	.d_kqfilter	= nokqfilter,
	.d_discard	= nodiscard,
	.d_flag		= D_OTHER | D_MPSAFE,
};

const struct fileops pad_fileops = {
	.fo_name	= "pad",
	.fo_read	= fops_pad_read,
	.fo_write	= fops_pad_write,
	.fo_ioctl	= fops_pad_ioctl,
	.fo_fcntl	= fnullop_fcntl,
	.fo_stat	= fops_pad_stat,
	.fo_poll	= fops_pad_poll,
	.fo_close	= fops_pad_close,
	.fo_mmap	= fops_pad_mmap,
	.fo_kqfilter	= fops_pad_kqfilter,
	.fo_restart	= fnullop_restart
};

CFATTACH_DECL2_NEW(pad, sizeof(struct pad_softc),
    pad_match, pad_attach, pad_detach,
    NULL, NULL, pad_childdet);

void
padattach(int n)
{
	int error;

	error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
	if (error) {
		aprint_error("%s: couldn't register cfattach: %d\n",
		    pad_cd.cd_name, error);
		config_cfdriver_detach(&pad_cd);
		return;
	}
}

static int
pad_match(device_t parent, cfdata_t data, void *opaque)
{

	return 1;
}

static void
pad_attach(device_t parent, device_t self, void *opaque)
{
	struct pad_softc *sc = device_private(self);

	KASSERT(KERNEL_LOCKED_P());

	aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");

	sc->sc_dev = self;

	cv_init(&sc->sc_condvar, device_xname(sc->sc_dev));
	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
	callout_init(&sc->sc_pcallout, CALLOUT_MPSAFE);
	callout_setfunc(&sc->sc_pcallout, pad_done_output, sc);

	sc->sc_swvol = 255;
	sc->sc_buflen = 0;
	sc->sc_rpos = sc->sc_wpos = 0;
	sc->sc_audiodev = audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);

	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
		aprint_error_dev(sc->sc_dev,
		    "couldn't establish power handler\n");

	sc->sc_open = 1;
}

static int
pad_detach(device_t self, int flags)
{
	struct pad_softc *sc = device_private(self);
	int cmaj, mn;
	int error;

	KASSERT(KERNEL_LOCKED_P());

	/* Prevent detach without going through close -- e.g., drvctl.  */
	if (sc->sc_open)
		return EBUSY;

	error = config_detach_children(self, flags);
	if (error)
		return error;

	cmaj = cdevsw_lookup_major(&pad_cdevsw);
	mn = device_unit(sc->sc_dev);
	vdevgone(cmaj, mn, mn, VCHR);

	pmf_device_deregister(sc->sc_dev);

	callout_destroy(&sc->sc_pcallout);
	mutex_destroy(&sc->sc_lock);
	mutex_destroy(&sc->sc_intr_lock);
	cv_destroy(&sc->sc_condvar);

	return 0;
}

static void
pad_childdet(device_t self, device_t child)
{
	struct pad_softc *sc = device_private(self);

	KASSERT(KERNEL_LOCKED_P());

	if (child == sc->sc_audiodev)
		sc->sc_audiodev = NULL;
}

static int
pad_add_block(struct pad_softc *sc, uint8_t *blk, int blksize)
{
	int foff, flen, tlen;

	KASSERT(blksize >= 0);
	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (blksize > PAD_BUFSIZE ||
	    sc->sc_buflen > PAD_BUFSIZE - (unsigned)blksize)
		return ENOBUFS;

	foff = sc->sc_wpos;
	if (sc->sc_wpos + blksize <= PAD_BUFSIZE) {
		flen = blksize;
		tlen = 0;
	} else {
		flen = PAD_BUFSIZE - sc->sc_wpos;
		tlen = blksize - flen;
	}

	sc->sc_wpos = foff + blksize;
	if (sc->sc_wpos >= PAD_BUFSIZE)
		sc->sc_wpos -= PAD_BUFSIZE;

	/*
	 * release interrupt lock for bulk copy to audio buffer
	 */
	mutex_exit(&sc->sc_intr_lock);
	memcpy(sc->sc_audiobuf + foff, blk, flen);
	memcpy(sc->sc_audiobuf, blk + flen, tlen);
	mutex_enter(&sc->sc_intr_lock);

	sc->sc_buflen += blksize;
	cv_broadcast(&sc->sc_condvar);

	return 0;
}

static int
pad_get_block(struct pad_softc *sc, pad_block_t *pb, int maxblksize, int dowait)
{
	int l, blksize, error;

	KASSERT(maxblksize > 0);
	KASSERT(mutex_owned(&sc->sc_intr_lock));

	if (sc->sc_buflen == 0 && !dowait)
		return EAGAIN;

	while (sc->sc_buflen == 0) {
		DPRINTF("%s: wait\n", __func__);
		error = cv_wait_sig(&sc->sc_condvar, &sc->sc_intr_lock);
		DPRINTF("%s: wake up %d\n", __func__, err);
		if (error)
			return error;
	}
	blksize = uimin(maxblksize, sc->sc_buflen);

	pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
	if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
		pb->pb_len = blksize;
		sc->sc_rpos += blksize;
	} else {
		l = PAD_BUFSIZE - sc->sc_rpos;
		pb->pb_len = l;
		sc->sc_rpos = 0;
	}
	sc->sc_buflen -= pb->pb_len;

	return 0;
}

static int
pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
{
	struct file *fp = NULL;
	device_t self;
	struct pad_softc *sc = NULL;
	cfdata_t cf = NULL;
	int error, fd;

	error = fd_allocfile(&fp, &fd);
	if (error)
		goto out;

	cf = kmem_alloc(sizeof(*cf), KM_SLEEP);
	cf->cf_name = pad_cd.cd_name;
	cf->cf_atname = pad_cd.cd_name;
	cf->cf_unit = 0;
	cf->cf_fstate = FSTATE_STAR;

	self = config_attach_pseudo(cf);
	if (self == NULL) {
		error = ENXIO;
		goto out;
	}
	sc = device_private(self);
	KASSERT(sc->sc_dev == self);
	cf = NULL;

	error = fd_clone(fp, fd, flags, &pad_fileops, sc);
	KASSERT(error == EMOVEFD);
	fp = NULL;
	sc = NULL;

out:	if (sc)
		pad_close(sc);
	if (cf)
		kmem_free(cf, sizeof(*cf));
	if (fp)
		fd_abort(curproc, fp, fd);
	return error;
}

static void
pad_close(struct pad_softc *sc)
{
	device_t self = sc->sc_dev;
	cfdata_t cf = device_cfdata(self);

	/*
	 * XXX This is not quite enough to prevent racing with drvctl
	 * detach.  What can happen:
	 *
	 *	cpu0				cpu1
	 *
	 *	pad_close
	 *	take kernel lock
	 *	sc->sc_open = 0
	 *	drop kernel lock
	 *	wait for config_misc_lock
	 *					drvctl detach
	 *					take kernel lock
	 *					drop kernel lock
	 *					wait for config_misc_lock
	 *					retake kernel lock
	 *					drop config_misc_lock
	 *	take config_misc_lock
	 *	wait for kernel lock
	 *					pad_detach (sc_open=0 already)
	 *					free device
	 *					drop kernel lock
	 *	use device after free
	 *
	 * We need a way to grab a reference to the device so it won't
	 * be freed until we're done -- it's OK if we config_detach
	 * twice as long as it's idempotent, but not OK if the first
	 * config_detach frees the struct device before the second one
	 * has finished handling it.
	 */
	KERNEL_LOCK(1, NULL);
	KASSERT(sc->sc_open);
	sc->sc_open = 0;
	(void)config_detach(self, DETACH_FORCE);
	KERNEL_UNLOCK_ONE(NULL);

	kmem_free(cf, sizeof(*cf));
}

static int
fops_pad_close(struct file *fp)
{
	struct pad_softc *sc = fp->f_pad;

	pad_close(sc);

	return 0;
}

static int
fops_pad_poll(struct file *fp, int events)
{

	return POLLERR;
}

static int
fops_pad_kqfilter(struct file *fp, struct knote *kn)
{
	struct pad_softc *sc = fp->f_pad;
	dev_t dev;

	dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
	    device_unit(sc->sc_dev));

	return seltrue_kqfilter(dev, kn);
}

static int
fops_pad_ioctl(struct file *fp, u_long cmd, void *data)
{

	return ENODEV;
}

static int
fops_pad_stat(struct file *fp, struct stat *st)
{
	struct pad_softc *sc = fp->f_pad;

	memset(st, 0, sizeof(*st));

	st->st_dev = makedev(cdevsw_lookup_major(&pad_cdevsw),
	    device_unit(sc->sc_dev));

	st->st_uid = kauth_cred_geteuid(fp->f_cred);
	st->st_gid = kauth_cred_getegid(fp->f_cred);
	st->st_mode = S_IFCHR;

	return 0;
}

static int
fops_pad_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
    int *advicep, struct uvm_object **uobjp, int *maxprotp)
{

	return 1;
}

static int
fops_pad_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    int ioflag)
{
	struct pad_softc *sc = fp->f_pad;

	return pad_read(sc, offp, uio, cred, ioflag);
}

static int
pad_read(struct pad_softc *sc, off_t *offp, struct uio *uio, kauth_cred_t cred,
    int ioflag)
{
	pad_block_t pb;
	int err, first;

	err = 0;
	first = 1;
	DPRINTF("%s: resid=%zu\n", __func__, uio->uio_resid);
	while (uio->uio_resid > 0) {
		mutex_enter(&sc->sc_intr_lock);
		err = pad_get_block(sc, &pb, MIN(uio->uio_resid, INT_MAX), first);
		mutex_exit(&sc->sc_intr_lock);
		first = 0;
		if (err == EAGAIN) {
			err = 0;
			break;
		}
		if (err)
			break;

		DPRINTF("%s: move %d\n", __func__, pb.pb_len);
		err = uiomove(pb.pb_ptr, pb.pb_len, uio);
		if (err)
			break;
	}

	return err;
}

static int
fops_pad_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    int ioflag)
{

	return EOPNOTSUPP;
}

static int
pad_query_format(void *opaque, audio_format_query_t *afp)
{

	return audio_query_format(pad_formats, PAD_NFORMATS, afp);
}

static int
pad_set_format(void *opaque, int setmode,
    const audio_params_t *play, const audio_params_t *rec,
    audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
{
	struct pad_softc *sc = opaque;

	KASSERT(mutex_owned(&sc->sc_lock));

	/* XXX playback only */
	pfil->codec = pad_swvol_codec;
	pfil->context = sc;

	return 0;
}

static int
pad_start_output(void *opaque, void *block, int blksize,
    void (*intr)(void *), void *intrarg)
{
	struct pad_softc *sc = opaque;
	int err;
	u_int framesize;
	int ticks;

	KASSERT(mutex_owned(&sc->sc_intr_lock));

	sc->sc_intr = intr;
	sc->sc_intrarg = intrarg;

	DPRINTF("%s: blksize=%d\n", __func__, blksize);
	err = pad_add_block(sc, block, blksize);
	if (err) {
		DPRINTF("%s: failed: %d\n", __func__, err);
		/* "Silently" drop overflows, but keep pace */
		err = 0;
	}

	framesize = PADCHAN * (PADPREC / NBBY) * PADFREQ;

	sc->sc_resid += blksize;
	ticks = mstohz(sc->sc_resid * 1000 / framesize);
	sc->sc_resid -= hztoms(ticks) * framesize / 1000;

	DPRINTF("%s: callout ms=%d\n", __func__, ms);
	callout_schedule(&sc->sc_pcallout, ticks);

	return err;
}

static int
pad_halt_output(void *opaque)
{
	struct pad_softc *sc = opaque;

	DPRINTF("%s\n", __func__);
	KASSERT(mutex_owned(&sc->sc_intr_lock));

	callout_halt(&sc->sc_pcallout, &sc->sc_intr_lock);

	sc->sc_intr = NULL;
	sc->sc_intrarg = NULL;
	sc->sc_buflen = 0;
	sc->sc_resid = 0;
	sc->sc_rpos = sc->sc_wpos = 0;

	return 0;
}

static void
pad_done_output(void *arg)
{
	struct pad_softc *sc = arg;

	DPRINTF("%s\n", __func__);

	mutex_enter(&sc->sc_intr_lock);
	(*sc->sc_intr)(sc->sc_intrarg);
	mutex_exit(&sc->sc_intr_lock);
}

static int
pad_getdev(void *opaque, struct audio_device *ret)
{

	strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
	strlcpy(ret->version, osrelease, sizeof(ret->version));
	strlcpy(ret->config, "pad", sizeof(ret->config));

	return 0;
}

static int
pad_set_port(void *opaque, mixer_ctrl_t *mc)
{
	struct pad_softc *sc = opaque;

	KASSERT(mutex_owned(&sc->sc_lock));

	switch (mc->dev) {
	case PAD_OUTPUT_MASTER_VOLUME:
	case PAD_INPUT_DAC_VOLUME:
		if (mc->un.value.num_channels != 1)
			return EINVAL;
		sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
		return 0;
	}

	return ENXIO;
}

static int
pad_get_port(void *opaque, mixer_ctrl_t *mc)
{
	struct pad_softc *sc = opaque;

	KASSERT(mutex_owned(&sc->sc_lock));

	switch (mc->dev) {
	case PAD_OUTPUT_MASTER_VOLUME:
	case PAD_INPUT_DAC_VOLUME:
		if (mc->un.value.num_channels != 1)
			return EINVAL;
		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
		return 0;
	}

	return ENXIO;
}

static int
pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
{
	struct pad_softc *sc __diagused = opaque;

	KASSERT(mutex_owned(&sc->sc_lock));

	switch (di->index) {
	case PAD_OUTPUT_CLASS:
		di->mixer_class = PAD_OUTPUT_CLASS;
		strcpy(di->label.name, AudioCoutputs);
		di->type = AUDIO_MIXER_CLASS;
		di->next = di->prev = AUDIO_MIXER_LAST;
		return 0;
	case PAD_INPUT_CLASS:
		di->mixer_class = PAD_INPUT_CLASS;
		strcpy(di->label.name, AudioCinputs);
		di->type = AUDIO_MIXER_CLASS;
		di->next = di->prev = AUDIO_MIXER_LAST;
		return 0;
	case PAD_OUTPUT_MASTER_VOLUME:
		di->mixer_class = PAD_OUTPUT_CLASS;
		strcpy(di->label.name, AudioNmaster);
		di->type = AUDIO_MIXER_VALUE;
		di->next = di->prev = AUDIO_MIXER_LAST;
		di->un.v.num_channels = 1;
		strcpy(di->un.v.units.name, AudioNvolume);
		return 0;
	case PAD_INPUT_DAC_VOLUME:
		di->mixer_class = PAD_INPUT_CLASS;
		strcpy(di->label.name, AudioNdac);
		di->type = AUDIO_MIXER_VALUE;
		di->next = di->prev = AUDIO_MIXER_LAST;
		di->un.v.num_channels = 1;
		strcpy(di->un.v.units.name, AudioNvolume);
		return 0;
	}

	return ENXIO;
}

static int
pad_get_props(void *opaque)
{

	return AUDIO_PROP_PLAYBACK;
}

static void
pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
{
	struct pad_softc *sc = opaque;

	*intr = &sc->sc_intr_lock;
	*thread = &sc->sc_lock;
}

static void
pad_swvol_codec(audio_filter_arg_t *arg)
{
	struct pad_softc *sc = arg->context;
	const uint8_t *src;
	uint8_t *dst;
	u_int sample_count;
	u_int i;
	u_int bits;

	src = arg->src;
	dst = arg->dst;
	sample_count = arg->count * arg->srcfmt->channels;
	bits = arg->srcfmt->precision;

	for (i = 0; i < sample_count; i++) {
		int64_t v;

		switch (howmany(bits, NBBY)) {
		case 2: /* AUDIO_INTERNAL_BITS == 16 */
			v = *(const int16_t *)src;
			src += sizeof(int16_t);
			break;
		case 4: /* AUDIO_INTERNAL_BITS == 32 */
			v = *(const int32_t *)src;
			src += sizeof(int32_t);
			break;
		default:
			v = 0;
			break;
		}

		v = v * sc->sc_swvol / 255;

		if (PADPREC > bits)
			v = v << (PADPREC - bits);
		else if (PADPREC < bits)
			v = v >> (bits - PADPREC);

		/* AUDIO_ENCODING_SLINEAR_LE */
#if PADPREC > 0
		*dst++ = v;
#endif
#if PADPREC > 8
		v >>= 8;
		*dst++ = v;
#endif
#if PADPREC > 16
		v >>= 8;
		*dst++ = v;
#endif
#if PADPREC > 24
		v >>= 8;
		*dst++ = v;
#endif
	}
}

MODULE(MODULE_CLASS_DRIVER, pad, "audio");

#ifdef _MODULE

#include "ioconf.c"

devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;

/*
 * We need our own version of cfattach since config(1)'s ioconf does not
 * generate what we need
 */

static struct cfattach *pad_cfattachinit[] = { &pad_ca, NULL };

static struct cfattachinit pad_cfattach[] = {
	{ "pad", pad_cfattachinit },
	{ NULL, NULL }
};
#endif

static int
pad_modcmd(modcmd_t cmd, void *arg)
{
	int error = 0;

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
			    &pad_cdevsw, &cmajor);
		if (error)
			break;

		pad_cfattach[1] = cfattach_ioconf_pad[0];
		error = config_init_component(cfdriver_ioconf_pad,
		    pad_cfattach, cfdata_ioconf_pad);
		if (error) {
			devsw_detach(NULL, &pad_cdevsw);
			break;
		}
#endif
		break;

	case MODULE_CMD_FINI:
#ifdef _MODULE
		error = config_fini_component(cfdriver_ioconf_pad,
		    pad_cfattach, cfdata_ioconf_pad);
		if (error == 0)
			devsw_detach(NULL, &pad_cdevsw);
#endif
		break;

	default:
		error = ENOTTY;
	}

	return error;
}