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
/*	$NetBSD: pas.c,v 1.72 2019/05/08 13:40:18 isaki Exp $	*/

/*
 * Copyright (c) 1991-1993 Regents of the University of California.
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the Computer Systems
 *	Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
 *
 */
/*
 * jfw 7/13/97 - The soundblaster code requires the generic bus-space
 * structures to be set up properly.  Rather than go to the effort of making
 * code for a dead line fully generic, properly set up the SB structures and
 * leave the rest x86/ISA/default-configuration specific.  If you have a
 * REAL computer, go buy a REAL sound card.
 */
/*
 * Todo:
 *	- look at other PAS drivers (for PAS native suport)
 *	- use common sb.c once emulation is setup
 */
/*
 * jfw 6/21/98 - WARNING:  the PAS native IO ports are scattered all around
 * IO port space (0x0388, 0x738B, 0xBF88, 0x2789, ...) which will make proper
 * reservation a real pain, so I'm not going to do it (while fixing the
 * current reservation code to "work").  As a sanity check, I reserve the
 * 0x0388 base address, but you probably shouldn't even think of trying this
 * driver unless you're certain you have the hardware installed and it doesn't
 * conflict with other hardware...
 */


#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pas.c,v 1.72 2019/05/08 13:40:18 isaki Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/proc.h>

#include <sys/cpu.h>
#include <sys/intr.h>
#include <sys/bus.h>
#include <machine/pio.h>

#include <sys/audioio.h>
#include <dev/audio/audio_if.h>
#include <dev/midi_if.h>

#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>

#include <dev/isa/sbdspvar.h>
#include <dev/isa/sbreg.h>

#define DEFINE_TRANSLATIONS
#include <dev/isa/pasreg.h>

#ifdef AUDIO_DEBUG
#define DPRINTF(x)	if (pasdebug) printf x
int	pasdebug = 0;
#else
#define DPRINTF(x)
#endif

/*
 * Software state, per SoundBlaster card.
 * The soundblaster has multiple functionality, which we must demultiplex.
 * One approach is to have one major device number for the soundblaster card,
 * and use different minor numbers to indicate which hardware function
 * we want.  This would make for one large driver.  Instead our approach
 * is to partition the design into a set of drivers that share an underlying
 * piece of hardware.  Most things are hard to share, for example, the audio
 * and midi ports.  For audio, we might want to mix two processes' signals,
 * and for midi we might want to merge streams (this is hard due to
 * running status).  Moreover, we should be able to re-use the high-level
 * modules with other kinds of hardware.  In this module, we only handle the
 * most basic communications with the sb card.
 */
struct pas_softc {
	struct sbdsp_softc sc_sbdsp;	/* base device, &c. */
	bus_space_handle_t pas_port_handle;    /* the pas-specific port */

	int model;
	int rev;
};

int	pas_getdev(void *, struct audio_device *);
void	pasconf(int, int, int, int);


/*
 * Define our interface to the higher level audio driver.
 */

const struct audio_hw_if pas_hw_if = {
	.open			= sbdsp_open,
	.close			= sbdsp_close,
	.query_format		= sbdsp_query_format,
	.set_format		= sbdsp_set_format,
	.round_blocksize	= sbdsp_round_blocksize,
	.halt_output		= sbdsp_halt_output,
	.halt_input		= sbdsp_halt_input,
	.speaker_ctl		= sbdsp_speaker_ctl,
	.getdev			= pas_getdev,
	.set_port		= sbdsp_mixer_set_port,
	.get_port		= sbdsp_mixer_get_port,
	.query_devinfo		= sbdsp_mixer_query_devinfo,
	.allocm			= sb_malloc,
	.freem			= sb_free,
	.round_buffersize	= sb_round_buffersize,
	.get_props		= sbdsp_get_props,
	.trigger_output		= sbdsp_trigger_output,
	.trigger_input		= sbdsp_trigger_input,
	.get_locks		= sbdsp_get_locks,
};

/* The Address Translation code is used to convert I/O register addresses to
   be relative to the given base -register */

static const char *pasnames[] = {
	"",
	"Plus",
	"CDPC",
	"16",
	"16Basic"
};

static struct audio_device pas_device = {
	"PAS,??",
	"",
	"pas"
};

/*XXX assume default I/O base address */
#define pasread(p) inb((p))
#define paswrite(d, p) outb((p), (d))

void
pasconf(int model, int sbbase, int sbirq, int sbdrq)
{

	paswrite(0x00, INTERRUPT_MASK);
	/* Local timer control register */
	paswrite(0x36, SAMPLE_COUNTER_CONTROL);
	/* Sample rate timer (16 bit) */
	paswrite(0x36, SAMPLE_RATE_TIMER);
	paswrite(0, SAMPLE_RATE_TIMER);
	/* Local timer control register */
	paswrite(0x74, SAMPLE_COUNTER_CONTROL);
	/* Sample count register (16 bit) */
	paswrite(0x74, SAMPLE_BUFFER_COUNTER);
	paswrite(0, SAMPLE_BUFFER_COUNTER);

	paswrite(P_C_PCM_MONO | P_C_PCM_DAC_MODE |
	    P_C_MIXER_CROSS_L_TO_L | P_C_MIXER_CROSS_R_TO_R,
	    PCM_CONTROL);
	paswrite(S_M_PCM_RESET | S_M_FM_RESET |
	    S_M_SB_RESET | S_M_MIXER_RESET, SERIAL_MIXER);

/*XXX*/
	paswrite(I_C_1_BOOT_RESET_ENABLE|1, IO_CONFIGURATION_1);

	paswrite(I_C_2_PCM_DMA_DISABLED, IO_CONFIGURATION_2);
	paswrite(I_C_3_PCM_IRQ_DISABLED, IO_CONFIGURATION_3);

#ifdef BROKEN_BUS_CLOCK
	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND |
	    S_C_1_FM_EMULATE_CLOCK, SYSTEM_CONFIGURATION_1);
#else
	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND,
	    SYSTEM_CONFIGURATION_1);
#endif

	/*XXX*/
	paswrite(0, SYSTEM_CONFIGURATION_2);
	paswrite(0, SYSTEM_CONFIGURATION_3);

	/* Sets mute off and selects filter rate of 17.897 kHz */
	paswrite(F_F_MIXER_UNMUTE | 0x01, FILTER_FREQUENCY);

	if (model == PAS_16 || model == PAS_16BASIC)
		paswrite(8, PRESCALE_DIVIDER);
	else
		paswrite(0, PRESCALE_DIVIDER);

	paswrite(P_M_MV508_ADDRESS | P_M_MV508_PCM, PARALLEL_MIXER);
	paswrite(5, PARALLEL_MIXER);

	/*
	 * Setup SoundBlaster emulation.
	 */
	paswrite((sbbase >> 4) & 0xf, EMULATION_ADDRESS);
	paswrite(E_C_SB_IRQ_translate[sbirq] | E_C_SB_DMA_translate[sbdrq],
	    EMULATION_CONFIGURATION);
	paswrite(C_E_SB_ENABLE, COMPATIBILITY_ENABLE);

	/*
	 * Set mid-range levels.
	 */
	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MODE, PARALLEL_MIXER);
	paswrite(P_M_MV508_LOUDNESS | P_M_MV508_ENHANCE_NONE, PARALLEL_MIXER);

	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_A, PARALLEL_MIXER);
	paswrite(50, PARALLEL_MIXER);
	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_B, PARALLEL_MIXER);
	paswrite(50, PARALLEL_MIXER);

	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_SB,
	    PARALLEL_MIXER);
	paswrite(P_M_MV508_OUTPUTMIX | 30, PARALLEL_MIXER);

	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_MIC,
	    PARALLEL_MIXER);
	paswrite(P_M_MV508_INPUTMIX | 30, PARALLEL_MIXER);
}

int	pasprobe(device_t, cfdata_t, void *);
void	pasattach(device_t, device_t, void *);
static	int pasfind(cfdata_t, struct pas_softc *,
    struct isa_attach_args *, int);
/* argument to pasfind */
#define PASPROBE  1
#define PASATTACH 0

CFATTACH_DECL_NEW(pas, sizeof(struct pas_softc),
    pasprobe, pasattach, NULL, NULL);

/*
 * Probe / attach routines.
 */

int
pasprobe(device_t parent, cfdata_t match, void *aux)
{
	struct isa_attach_args *ia;
	struct pas_softc probesc, *sc;

	ia = aux;
	sc = &probesc;
	if (ia->ia_nio < 1)
		return 0;
	if (ia->ia_nirq < 1)
		return 0;
	if (ia->ia_ndrq < 1)
		return 0;

	if (ISA_DIRECT_CONFIG(ia))
		return 0;

	memset(sc, 0, sizeof *sc);
	return pasfind(match, sc, ia, PASPROBE);
}

/*
 * Probe for the soundblaster hardware.
 */
static int
pasfind(cfdata_t match, struct pas_softc *sc,
    struct isa_attach_args *ia, int probing)
{
	int iobase;
	u_char id, t;
	int rc;

	rc = 0;			/* failure */
	/* ensure we can set this up as a sound blaster */
	if (!SB_BASE_VALID(ia->ia_io[0].ir_addr)) {
		printf("pas: configured SB iobase 0x%x invalid\n",
		    ia->ia_io[0].ir_addr);
		return 0;
	}

	if (bus_space_map(ia->ia_iot, PAS_DEFAULT_BASE, 1, 0,
	    &sc->pas_port_handle)) {
		printf("pas: can't map base register %x in probe\n",
		    PAS_DEFAULT_BASE);
		return 0;
	}

	/*
	 * WARNING: Setting an option like W:1 or so that disables
	 * warm boot reset of the card will screw up this detect code
	 * something fierce.  Adding code to handle this means possibly
	 * interfering with other cards on the bus if you have something
	 * on base port 0x388.  SO be forewarned.
	 */
	/* Talk to first board */
	outb(MASTER_DECODE, 0xbc);
	/* Set base address */

#if 0
	/* XXX Need to setup pseudo device */
	/* XXX What are good io addrs ? */
	if (iobase != PAS_DEFAULT_BASE) {
		printf("pas: configured iobase %d invalid\n", iobase);
		return 0;
	}
#else
	/* Start out talking to native PAS */
	iobase = PAS_DEFAULT_BASE;
#endif

	outb(MASTER_DECODE, iobase >> 2);
	/* One wait-state */
	paswrite(1, WAIT_STATE);

	id = pasread(INTERRUPT_MASK);
	if (id == 0xff || id == 0xfe) {
		/* sanity */
		DPRINTF(("pas: bogus card id\n"));
		goto unmap1;
	}
	/*
	 * We probably have a PAS-series board, now check for a
	 * PAS2-series board by trying to change the board revision
	 * bits.  PAS2-series hardware won't let you do this because
	 * the bits are read-only.
	 */
	t = id ^ 0xe0;
	paswrite(t, INTERRUPT_MASK);
	t = inb(INTERRUPT_MASK);
	paswrite(id, INTERRUPT_MASK);

	if (t != id) {
		/* Not a PAS2 */
		printf("pas: detected card but PAS2 test failed\n");
		goto unmap1;
	}
	/*XXX*/
	t = pasread(OPERATION_MODE_1) & 0xf;
	sc->model = O_M_1_to_card[t];
	if (sc->model != 0) {
		sc->rev = pasread(BOARD_REV_ID);
	} else {
		DPRINTF(("pas: bogus model id\n"));
		goto unmap1;
	}

	if (sc->model >= 0) {
		if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
			printf("pas: sb emulation requires known irq\n");
			goto unmap1;
		}
		pasconf(sc->model, ia->ia_io[0].ir_addr,
		    ia->ia_irq[0].ir_irq, 1);
	} else {
		DPRINTF(("pas: could not probe pas\n"));
		goto unmap1;
	}

	/* Now a SoundBlaster, so set up proper bus-space hooks
         * appropriately
         */

	sc->sc_sbdsp.sc_iobase = ia->ia_io[0].ir_addr;
	sc->sc_sbdsp.sc_iot = ia->ia_iot;

	/* Map i/o space [we map 24 ports which is the max of the sb and pro */
	if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr,
	    SBP_NPORT, 0, &sc->sc_sbdsp.sc_ioh)) {
		printf("pas: can't map i/o space 0x%x/%d in probe\n",
		    ia->ia_io[0].ir_addr, SBP_NPORT);
		goto unmap1;
	}

	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
		DPRINTF(("pas: couldn't reset card\n"));
		goto unmap;
	}

	/*
	 * Cannot auto-discover DMA channel.
	 */
	if (!SB_DRQ_VALID(ia->ia_drq[0].ir_drq)) {
		printf("pas: configured DMA chan %d invalid\n",
		    ia->ia_drq[0].ir_drq);
		goto unmap;
	}
	if (!SB_IRQ_VALID(ia->ia_irq[0].ir_irq)) {
		printf("pas: configured irq chan %d invalid\n",
		    ia->ia_drq[0].ir_drq);
		goto unmap;
	}

	sc->sc_sbdsp.sc_irq = ia->ia_irq[0].ir_irq;
	sc->sc_sbdsp.sc_drq8 = ia->ia_drq[0].ir_drq;
	sc->sc_sbdsp.sc_drq16 = -1; /* XXX */

	if (sbdsp_probe(&sc->sc_sbdsp, match) == 0) {
		DPRINTF(("pas: sbdsp probe failed\n"));
		goto unmap;
	}

	rc = 1;

	if (probing) {
		ia->ia_nio = 1;
		ia->ia_io[0].ir_size = SBP_NPORT;

		ia->ia_nirq = 1;
		ia->ia_ndrq = 1;

		ia->ia_niomem = 0;
	}

 unmap:
	if (rc == 0 || probing)
		bus_space_unmap(ia->ia_iot, sc->sc_sbdsp.sc_ioh,
		    SBP_NPORT);
 unmap1:
	if (rc == 0 || probing)
		bus_space_unmap(ia->ia_iot, PAS_DEFAULT_BASE, 1);
	return rc;
}

/*
 * Attach hardware to driver, attach hardware driver to audio
 * pseudo-device driver .
 */
void
pasattach(device_t parent, device_t self, void *aux)
{
	struct pas_softc *sc;
	struct isa_attach_args *ia;
	int iobase;

	sc = device_private(self);
	sc->sc_sbdsp.sc_dev = self;
	ia = (struct isa_attach_args *)aux;
	iobase = ia->ia_io[0].ir_addr;
	if (!pasfind(device_cfdata(self), sc, ia, PASATTACH)) {
		aprint_error_dev(self, "pasfind failed\n");
		return;
	}

	mutex_init(&sc->sc_sbdsp.sc_lock, MUTEX_DEFAULT, IPL_NONE);
	mutex_init(&sc->sc_sbdsp.sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);

	sc->sc_sbdsp.sc_ic = ia->ia_ic;
	sc->sc_sbdsp.sc_iobase = iobase;
	sc->sc_sbdsp.sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
	    IST_EDGE, IPL_AUDIO, sbdsp_intr, &sc->sc_sbdsp);

	aprint_normal(" ProAudio Spectrum %s [rev %d] ", pasnames[sc->model],
	    sc->rev);

	sbdsp_attach(&sc->sc_sbdsp);

	snprintf(pas_device.name, sizeof(pas_device.name), "pas,%s",
	    pasnames[sc->model]);
	snprintf(pas_device.version, sizeof(pas_device.version), "%d",
	    sc->rev);

	audio_attach_mi(&pas_hw_if, &sc->sc_sbdsp, sc->sc_sbdsp.sc_dev);
}

int
pas_getdev(void *addr, struct audio_device *retp)
{

	*retp = pas_device;
	return 0;
}