FreeBSD kernel sound device code
als4000.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2001 Orion Hodson <oho@acm.org>
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/*
30 * als4000.c - driver for the Avance Logic ALS 4000 chipset.
31 *
32 * The ALS4000 is effectively an SB16 with a PCI interface.
33 *
34 * This driver derives from ALS4000a.PDF, Bart Hartgers alsa driver, and
35 * SB16 register descriptions.
36 */
37
38#ifdef HAVE_KERNEL_OPTION_HEADERS
39#include "opt_snd.h"
40#endif
41
42#include <dev/sound/pcm/sound.h>
43#include <dev/sound/isa/sb.h>
45
46#include <dev/pci/pcireg.h>
47#include <dev/pci/pcivar.h>
48
49#include "mixer_if.h"
50
51SND_DECLARE_FILE("$FreeBSD$");
52
53/* Debugging macro's */
54#undef DEB
55#ifndef DEB
56#define DEB(x) /* x */
57#endif /* DEB */
58
59#define ALS_DEFAULT_BUFSZ 16384
60
61/* ------------------------------------------------------------------------- */
62/* Structures */
63
64struct sc_info;
65
66struct sc_chinfo {
67 struct sc_info *parent;
70 u_int32_t format, speed, phys_buf, bps;
71 u_int32_t dma_active:1, dma_was_active:1;
73 int dir;
74};
75
76struct sc_info {
77 device_t dev;
78 bus_space_tag_t st;
79 bus_space_handle_t sh;
80 bus_dma_tag_t parent_dmat;
81 struct resource *reg, *irq;
83 void *ih;
84 struct mtx *lock;
85
86 unsigned int bufsz;
87 struct sc_chinfo pch, rch;
88};
89
90/* Channel caps */
91
92static u_int32_t als_format[] = {
93 SND_FORMAT(AFMT_U8, 1, 0),
94 SND_FORMAT(AFMT_U8, 2, 0),
95 SND_FORMAT(AFMT_S16_LE, 1, 0),
96 SND_FORMAT(AFMT_S16_LE, 2, 0),
97 0
98};
99
100/*
101 * I don't believe this rotten soundcard can do 48k, really,
102 * trust me.
103 */
104static struct pcmchan_caps als_caps = { 4000, 44100, als_format, 0 };
105
106/* ------------------------------------------------------------------------- */
107/* Register Utilities */
108
109static u_int32_t
110als_gcr_rd(struct sc_info *sc, int index)
111{
112 bus_space_write_1(sc->st, sc->sh, ALS_GCR_INDEX, index);
113 return bus_space_read_4(sc->st, sc->sh, ALS_GCR_DATA);
114}
115
116static void
117als_gcr_wr(struct sc_info *sc, int index, int data)
118{
119 bus_space_write_1(sc->st, sc->sh, ALS_GCR_INDEX, index);
120 bus_space_write_4(sc->st, sc->sh, ALS_GCR_DATA, data);
121}
122
123static u_int8_t
125{
126 return bus_space_read_1(sc->st, sc->sh, ALS_SB_MPU_IRQ);
127}
128
129static void
130als_intr_wr(struct sc_info *sc, u_int8_t data)
131{
132 bus_space_write_1(sc->st, sc->sh, ALS_SB_MPU_IRQ, data);
133}
134
135static u_int8_t
136als_mix_rd(struct sc_info *sc, u_int8_t index)
137{
138 bus_space_write_1(sc->st, sc->sh, ALS_MIXER_INDEX, index);
139 return bus_space_read_1(sc->st, sc->sh, ALS_MIXER_DATA);
140}
141
142static void
143als_mix_wr(struct sc_info *sc, u_int8_t index, u_int8_t data)
144{
145 bus_space_write_1(sc->st, sc->sh, ALS_MIXER_INDEX, index);
146 bus_space_write_1(sc->st, sc->sh, ALS_MIXER_DATA, data);
147}
148
149static void
150als_esp_wr(struct sc_info *sc, u_int8_t data)
151{
152 u_int32_t tries, v;
153
154 tries = 1000;
155 do {
156 v = bus_space_read_1(sc->st, sc->sh, ALS_ESP_WR_STATUS);
157 if (~v & 0x80)
158 break;
159 DELAY(20);
160 } while (--tries != 0);
161
162 if (tries == 0)
163 device_printf(sc->dev, "als_esp_wr timeout");
164
165 bus_space_write_1(sc->st, sc->sh, ALS_ESP_WR_DATA, data);
166}
167
168static int
170{
171 u_int32_t tries, u, v;
172
173 bus_space_write_1(sc->st, sc->sh, ALS_ESP_RST, 1);
174 DELAY(10);
175 bus_space_write_1(sc->st, sc->sh, ALS_ESP_RST, 0);
176 DELAY(30);
177
178 tries = 1000;
179 do {
180 u = bus_space_read_1(sc->st, sc->sh, ALS_ESP_RD_STATUS8);
181 if (u & 0x80) {
182 v = bus_space_read_1(sc->st, sc->sh, ALS_ESP_RD_DATA);
183 if (v == 0xaa)
184 return 0;
185 else
186 break;
187 }
188 DELAY(20);
189 } while (--tries != 0);
190
191 if (tries == 0)
192 device_printf(sc->dev, "als_esp_reset timeout");
193 return 1;
194}
195
196static u_int8_t
197als_ack_read(struct sc_info *sc, u_int8_t addr)
198{
199 u_int8_t r = bus_space_read_1(sc->st, sc->sh, addr);
200 return r;
201}
202
203/* ------------------------------------------------------------------------- */
204/* Common pcm channel implementation */
205
206static void *
207alschan_init(kobj_t obj, void *devinfo,
208 struct snd_dbuf *b, struct pcm_channel *c, int dir)
209{
210 struct sc_info *sc = devinfo;
211 struct sc_chinfo *ch;
212
213 snd_mtxlock(sc->lock);
214 if (dir == PCMDIR_PLAY) {
215 ch = &sc->pch;
217 } else {
218 ch = &sc->rch;
220 }
221 ch->dir = dir;
222 ch->parent = sc;
223 ch->channel = c;
224 ch->bps = 1;
225 ch->format = SND_FORMAT(AFMT_U8, 1, 0);
227 ch->buffer = b;
228 snd_mtxunlock(sc->lock);
229
230 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, 0, sc->bufsz) != 0)
231 return NULL;
232
233 return ch;
234}
235
236static int
237alschan_setformat(kobj_t obj, void *data, u_int32_t format)
238{
239 struct sc_chinfo *ch = data;
240
241 ch->format = format;
242 return 0;
243}
244
245static u_int32_t
246alschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
247{
248 struct sc_chinfo *ch = data, *other;
249 struct sc_info *sc = ch->parent;
250
251 other = (ch->dir == PCMDIR_PLAY) ? &sc->rch : &sc->pch;
252
253 /* Deny request if other dma channel is active */
254 if (other->dma_active) {
255 ch->speed = other->speed;
256 return other->speed;
257 }
258
259 ch->speed = speed;
260 return speed;
261}
262
263static u_int32_t
264alschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
265{
266 struct sc_chinfo *ch = data;
267 struct sc_info *sc = ch->parent;
268
269 if (blocksize > sc->bufsz / 2) {
270 blocksize = sc->bufsz / 2;
271 }
273 return blocksize;
274}
275
276static u_int32_t
277alschan_getptr(kobj_t obj, void *data)
278{
279 struct sc_chinfo *ch = data;
280 struct sc_info *sc = ch->parent;
281 int32_t pos, sz;
282
283 snd_mtxlock(sc->lock);
284 pos = als_gcr_rd(ch->parent, ch->gcr_fifo_status) & 0xffff;
285 snd_mtxunlock(sc->lock);
286 sz = sndbuf_getsize(ch->buffer);
287 return (2 * sz - pos - 1) % sz;
288}
289
290static struct pcmchan_caps*
291alschan_getcaps(kobj_t obj, void *data)
292{
293 return &als_caps;
294}
295
296static void
298{
299 struct sc_info *sc = ch->parent;
300 struct sc_chinfo *other;
301
302 other = (ch->dir == PCMDIR_PLAY) ? &sc->rch : &sc->pch;
303 if (other->dma_active == 0) {
305 als_esp_wr(sc, ch->speed >> 8);
306 als_esp_wr(sc, ch->speed & 0xff);
307 } else {
308 DEB(printf("speed locked at %d (tried %d)\n",
309 other->speed, ch->speed));
310 }
311}
312
313/* ------------------------------------------------------------------------- */
314/* Playback channel implementation */
315#define ALS_8BIT_CMD(x, y) { (x), (y), DSP_DMA8, DSP_CMD_DMAPAUSE_8 }
316#define ALS_16BIT_CMD(x, y) { (x), (y), DSP_DMA16, DSP_CMD_DMAPAUSE_16 }
317
319 u_int32_t pcm_format; /* newpcm format */
320 u_int8_t format_val; /* sb16 format value */
321 u_int8_t dma_prog; /* sb16 dma program */
322 u_int8_t dma_stop; /* sb16 stop register */
323} static const playback_cmds[] = {
324 ALS_8BIT_CMD(SND_FORMAT(AFMT_U8, 1, 0), DSP_MODE_U8MONO),
326 ALS_16BIT_CMD(SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_MODE_S16MONO),
327 ALS_16BIT_CMD(SND_FORMAT(AFMT_S16_LE, 2, 0), DSP_MODE_S16STEREO),
329
330static const struct playback_command*
332{
333 u_int32_t i, n;
334
335 n = sizeof(playback_cmds) / sizeof(playback_cmds[0]);
336 for (i = 0; i < n; i++) {
337 if (playback_cmds[i].pcm_format == format) {
338 return &playback_cmds[i];
339 }
340 }
341 DEB(printf("als_get_playback_command: invalid format 0x%08x\n",
342 format));
343 return &playback_cmds[0];
344}
345
346static void
348{
349 const struct playback_command *p;
350 struct sc_info *sc = ch->parent;
351 u_int32_t buf, bufsz, count, dma_prog;
352
355 count = bufsz / 2;
356 if (ch->format & AFMT_16BIT)
357 count /= 2;
358 count--;
359
362
364 als_gcr_wr(sc, ALS_GCR_DMA0_MODE, (bufsz - 1) | 0x180000);
365
368
369 als_esp_wr(sc, dma_prog);
370 als_esp_wr(sc, p->format_val);
371 als_esp_wr(sc, count & 0xff);
372 als_esp_wr(sc, count >> 8);
373
374 ch->dma_active = 1;
375}
376
377static int
379{
380 const struct playback_command *p;
381 struct sc_info *sc = ch->parent;
382 u_int32_t active;
383
384 active = ch->dma_active;
385 if (active) {
387 als_esp_wr(sc, p->dma_stop);
388 }
389 ch->dma_active = 0;
390 return active;
391}
392
393static int
394alspchan_trigger(kobj_t obj, void *data, int go)
395{
396 struct sc_chinfo *ch = data;
397 struct sc_info *sc = ch->parent;
398
399 if (!PCMTRIG_COMMON(go))
400 return 0;
401
402 snd_mtxlock(sc->lock);
403 switch(go) {
404 case PCMTRIG_START:
406 break;
407 case PCMTRIG_STOP:
408 case PCMTRIG_ABORT:
410 break;
411 default:
412 break;
413 }
414 snd_mtxunlock(sc->lock);
415 return 0;
416}
417
418static kobj_method_t alspchan_methods[] = {
419 KOBJMETHOD(channel_init, alschan_init),
420 KOBJMETHOD(channel_setformat, alschan_setformat),
421 KOBJMETHOD(channel_setspeed, alschan_setspeed),
422 KOBJMETHOD(channel_setblocksize, alschan_setblocksize),
423 KOBJMETHOD(channel_trigger, alspchan_trigger),
424 KOBJMETHOD(channel_getptr, alschan_getptr),
425 KOBJMETHOD(channel_getcaps, alschan_getcaps),
427};
429
430/* ------------------------------------------------------------------------- */
431/* Capture channel implementation */
432
433static u_int8_t
434als_get_fifo_format(struct sc_info *sc, u_int32_t format)
435{
436 switch (format) {
437 case SND_FORMAT(AFMT_U8, 1, 0):
438 return ALS_FIFO1_8BIT;
439 case SND_FORMAT(AFMT_U8, 2, 0):
441 case SND_FORMAT(AFMT_S16_LE, 1, 0):
442 return ALS_FIFO1_SIGNED;
443 case SND_FORMAT(AFMT_S16_LE, 2, 0):
445 }
446 device_printf(sc->dev, "format not found: 0x%08x\n", format);
447 return ALS_FIFO1_8BIT;
448}
449
450static void
452{
453 struct sc_info *sc = ch->parent;
454 u_int32_t buf, bufsz, count, dma_prog;
455
458 count = bufsz / 2;
459 if (ch->format & AFMT_16BIT)
460 count /= 2;
461 count--;
462
465
468
471
472 dma_prog = ALS_FIFO1_RUN | als_get_fifo_format(sc, ch->format);
473 als_mix_wr(sc, ALS_FIFO1_CONTROL, dma_prog);
474
475 ch->dma_active = 1;
476}
477
478static int
480{
481 struct sc_info *sc = ch->parent;
482 u_int32_t active;
483
484 active = ch->dma_active;
485 if (active) {
487 }
488 ch->dma_active = 0;
489 return active;
490}
491
492static int
493alsrchan_trigger(kobj_t obj, void *data, int go)
494{
495 struct sc_chinfo *ch = data;
496 struct sc_info *sc = ch->parent;
497
498 snd_mtxlock(sc->lock);
499 switch(go) {
500 case PCMTRIG_START:
502 break;
503 case PCMTRIG_STOP:
504 case PCMTRIG_ABORT:
506 break;
507 }
508 snd_mtxunlock(sc->lock);
509 return 0;
510}
511
512static kobj_method_t alsrchan_methods[] = {
513 KOBJMETHOD(channel_init, alschan_init),
514 KOBJMETHOD(channel_setformat, alschan_setformat),
515 KOBJMETHOD(channel_setspeed, alschan_setspeed),
516 KOBJMETHOD(channel_setblocksize, alschan_setblocksize),
517 KOBJMETHOD(channel_trigger, alsrchan_trigger),
518 KOBJMETHOD(channel_getptr, alschan_getptr),
519 KOBJMETHOD(channel_getcaps, alschan_getcaps),
521};
523
524/* ------------------------------------------------------------------------- */
525/* Mixer related */
526
527/*
528 * ALS4000 has an sb16 mixer, with some additional controls that we do
529 * not yet a means to support.
530 */
531
532struct sb16props {
533 u_int8_t lreg;
534 u_int8_t rreg;
535 u_int8_t bits;
536 u_int8_t oselect;
537 u_int8_t iselect; /* left input mask */
538} static const amt[SOUND_MIXER_NRDEVICES] = {
539 [SOUND_MIXER_VOLUME] = { 0x30, 0x31, 5, 0x00, 0x00 },
540 [SOUND_MIXER_PCM] = { 0x32, 0x33, 5, 0x00, 0x00 },
541 [SOUND_MIXER_SYNTH] = { 0x34, 0x35, 5, 0x60, 0x40 },
542 [SOUND_MIXER_CD] = { 0x36, 0x37, 5, 0x06, 0x04 },
543 [SOUND_MIXER_LINE] = { 0x38, 0x39, 5, 0x18, 0x10 },
544 [SOUND_MIXER_MIC] = { 0x3a, 0x00, 5, 0x01, 0x01 },
545 [SOUND_MIXER_SPEAKER] = { 0x3b, 0x00, 2, 0x00, 0x00 },
546 [SOUND_MIXER_IGAIN] = { 0x3f, 0x40, 2, 0x00, 0x00 },
547 [SOUND_MIXER_OGAIN] = { 0x41, 0x42, 2, 0x00, 0x00 },
548 /* The following have register values but no h/w implementation */
549 [SOUND_MIXER_TREBLE] = { 0x44, 0x45, 4, 0x00, 0x00 },
550 [SOUND_MIXER_BASS] = { 0x46, 0x47, 4, 0x00, 0x00 }
552
553static int
555{
556 u_int32_t i, v;
557
558 for (i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
559 if (amt[i].bits) v |= 1 << i;
560 }
561 mix_setdevs(m, v);
562
563 for (i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
564 if (amt[i].iselect) v |= 1 << i;
565 }
566 mix_setrecdevs(m, v);
567 return 0;
568}
569
570static int
571alsmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
572{
573 struct sc_info *sc = mix_getdevinfo(m);
574 u_int32_t r, l, v, mask;
575
576 /* Fill upper n bits in mask with 1's */
577 mask = ((1 << amt[dev].bits) - 1) << (8 - amt[dev].bits);
578
579 l = (left * mask / 100) & mask;
580 v = als_mix_rd(sc, amt[dev].lreg) & ~mask;
581 als_mix_wr(sc, amt[dev].lreg, l | v);
582
583 if (amt[dev].rreg) {
584 r = (right * mask / 100) & mask;
585 v = als_mix_rd(sc, amt[dev].rreg) & ~mask;
586 als_mix_wr(sc, amt[dev].rreg, r | v);
587 } else {
588 r = 0;
589 }
590
591 /* Zero gain does not mute channel from output, but this does. */
592 v = als_mix_rd(sc, SB16_OMASK);
593 if (l == 0 && r == 0) {
594 v &= ~amt[dev].oselect;
595 } else {
596 v |= amt[dev].oselect;
597 }
598 als_mix_wr(sc, SB16_OMASK, v);
599 return 0;
600}
601
602static u_int32_t
603alsmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
604{
605 struct sc_info *sc = mix_getdevinfo(m);
606 u_int32_t i, l, r;
607
608 for (i = l = r = 0; i < SOUND_MIXER_NRDEVICES; i++) {
609 if (src & (1 << i)) {
610 if (amt[i].iselect == 1) { /* microphone */
611 l |= amt[i].iselect;
612 r |= amt[i].iselect;
613 } else {
614 l |= amt[i].iselect;
615 r |= amt[i].iselect >> 1;
616 }
617 }
618 }
619
620 als_mix_wr(sc, SB16_IMASK_L, l);
622 return src;
623}
624
625static kobj_method_t als_mixer_methods[] = {
626 KOBJMETHOD(mixer_init, alsmix_init),
627 KOBJMETHOD(mixer_set, alsmix_set),
630};
631MIXER_DECLARE(als_mixer);
632
633/* ------------------------------------------------------------------------- */
634/* Interrupt Handler */
635
636static void
637als_intr(void *p)
638{
639 struct sc_info *sc = (struct sc_info *)p;
640 u_int8_t intr, sb_status;
641
642 snd_mtxlock(sc->lock);
643 intr = als_intr_rd(sc);
644
645 if (intr & 0x80) {
646 snd_mtxunlock(sc->lock);
647 chn_intr(sc->pch.channel);
648 snd_mtxlock(sc->lock);
649 }
650
651 if (intr & 0x40) {
652 snd_mtxunlock(sc->lock);
653 chn_intr(sc->rch.channel);
654 snd_mtxlock(sc->lock);
655 }
656
657 /* ACK interrupt in PCI core */
658 als_intr_wr(sc, intr);
659
660 /* ACK interrupt in SB core */
661 sb_status = als_mix_rd(sc, IRQ_STAT);
662
663 if (sb_status & ALS_IRQ_STATUS8)
665 if (sb_status & ALS_IRQ_STATUS16)
667 if (sb_status & ALS_IRQ_MPUIN)
669 if (sb_status & ALS_IRQ_CR1E)
671
672 snd_mtxunlock(sc->lock);
673 return;
674}
675
676/* ------------------------------------------------------------------------- */
677/* H/W initialization */
678
679static int
680als_init(struct sc_info *sc)
681{
682 u_int32_t i, v;
683
684 /* Reset Chip */
685 if (als_esp_reset(sc)) {
686 return 1;
687 }
688
689 /* Enable write on DMA_SETUP register */
691 als_mix_wr(sc, ALS_SB16_CONFIG, v | 0x80);
692
693 /* Select DMA0 */
695
696 /* Disable write on DMA_SETUP register */
697 als_mix_wr(sc, ALS_SB16_CONFIG, v & 0x7f);
698
699 /* Enable interrupts */
700 v = als_gcr_rd(sc, ALS_GCR_MISC);
701 als_gcr_wr(sc, ALS_GCR_MISC, v | 0x28000);
702
703 /* Black out GCR DMA registers */
704 for (i = 0x91; i <= 0x96; i++) {
705 als_gcr_wr(sc, i, 0);
706 }
707
708 /* Emulation mode */
711 DEB(printf("GCR_DMA_EMULATION 0x%08x\n", v));
712 return 0;
713}
714
715static void
717{
718 /* Disable interrupts */
719 als_gcr_wr(sc, ALS_GCR_MISC, 0);
720}
721
722/* ------------------------------------------------------------------------- */
723/* Probe and attach card */
724
725static int
727{
728 if (pci_get_devid(dev) == ALS_PCI_ID0) {
729 device_set_desc(dev, "Avance Logic ALS4000");
730 return BUS_PROBE_DEFAULT;
731 }
732 return ENXIO;
733}
734
735static void
736als_resource_free(device_t dev, struct sc_info *sc)
737{
738 if (sc->reg) {
739 bus_release_resource(dev, SYS_RES_IOPORT, sc->regid, sc->reg);
740 sc->reg = NULL;
741 }
742 if (sc->ih) {
743 bus_teardown_intr(dev, sc->irq, sc->ih);
744 sc->ih = NULL;
745 }
746 if (sc->irq) {
747 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
748 sc->irq = NULL;
749 }
750 if (sc->parent_dmat) {
751 bus_dma_tag_destroy(sc->parent_dmat);
752 sc->parent_dmat = 0;
753 }
754 if (sc->lock) {
755 snd_mtxfree(sc->lock);
756 sc->lock = NULL;
757 }
758}
759
760static int
761als_resource_grab(device_t dev, struct sc_info *sc)
762{
763 sc->regid = PCIR_BAR(0);
764 sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->regid,
765 RF_ACTIVE);
766 if (sc->reg == NULL) {
767 device_printf(dev, "unable to allocate register space\n");
768 goto bad;
769 }
770 sc->st = rman_get_bustag(sc->reg);
771 sc->sh = rman_get_bushandle(sc->reg);
772
773 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
774 RF_ACTIVE | RF_SHAREABLE);
775 if (sc->irq == NULL) {
776 device_printf(dev, "unable to allocate interrupt\n");
777 goto bad;
778 }
779
780 if (snd_setup_intr(dev, sc->irq, INTR_MPSAFE, als_intr,
781 sc, &sc->ih)) {
782 device_printf(dev, "unable to setup interrupt\n");
783 goto bad;
784 }
785
786 sc->bufsz = pcm_getbuffersize(dev, 4096, ALS_DEFAULT_BUFSZ, 65536);
787
788 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev),
789 /*alignment*/2, /*boundary*/0,
790 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
791 /*highaddr*/BUS_SPACE_MAXADDR,
792 /*filter*/NULL, /*filterarg*/NULL,
793 /*maxsize*/sc->bufsz,
794 /*nsegments*/1, /*maxsegz*/0x3ffff,
795 /*flags*/0, /*lockfunc*/NULL,
796 /*lockarg*/NULL, &sc->parent_dmat) != 0) {
797 device_printf(dev, "unable to create dma tag\n");
798 goto bad;
799 }
800 return 0;
801 bad:
803 return ENXIO;
804}
805
806static int
808{
809 struct sc_info *sc;
810 char status[SND_STATUSLEN];
811
812 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
813 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_als4000 softc");
814 sc->dev = dev;
815
816 pci_enable_busmaster(dev);
817 /*
818 * By default the power to the various components on the
819 * ALS4000 is entirely controlled by the pci powerstate. We
820 * could attempt finer grained control by setting GCR6.31.
821 */
822 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
823 /* Reset the power state. */
824 device_printf(dev, "chip is in D%d power mode "
825 "-- setting to D0\n", pci_get_powerstate(dev));
826 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
827 }
828
829 if (als_resource_grab(dev, sc)) {
830 device_printf(dev, "failed to allocate resources\n");
831 goto bad_attach;
832 }
833
834 if (als_init(sc)) {
835 device_printf(dev, "failed to initialize hardware\n");
836 goto bad_attach;
837 }
838
839 if (mixer_init(dev, &als_mixer_class, sc)) {
840 device_printf(dev, "failed to initialize mixer\n");
841 goto bad_attach;
842 }
843
844 if (pcm_register(dev, sc, 1, 1)) {
845 device_printf(dev, "failed to register pcm entries\n");
846 goto bad_attach;
847 }
848
849 pcm_addchan(dev, PCMDIR_PLAY, &alspchan_class, sc);
850 pcm_addchan(dev, PCMDIR_REC, &alsrchan_class, sc);
851
852 snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
853 rman_get_start(sc->reg), rman_get_start(sc->irq),PCM_KLDSTRING(snd_als4000));
855 return 0;
856
857 bad_attach:
859 free(sc, M_DEVBUF);
860 return ENXIO;
861}
862
863static int
865{
866 struct sc_info *sc;
867 int r;
868
870 if (r)
871 return r;
872
873 sc = pcm_getdevinfo(dev);
874 als_uninit(sc);
876 free(sc, M_DEVBUF);
877 return 0;
878}
879
880static int
882{
883 struct sc_info *sc = pcm_getdevinfo(dev);
884
885 snd_mtxlock(sc->lock);
888 als_uninit(sc);
889 snd_mtxunlock(sc->lock);
890 return 0;
891}
892
893static int
895{
896 struct sc_info *sc = pcm_getdevinfo(dev);
897
898 snd_mtxlock(sc->lock);
899 if (als_init(sc) != 0) {
900 device_printf(dev, "unable to reinitialize the card\n");
901 snd_mtxunlock(sc->lock);
902 return ENXIO;
903 }
904
905 if (mixer_reinit(dev) != 0) {
906 device_printf(dev, "unable to reinitialize the mixer\n");
907 snd_mtxunlock(sc->lock);
908 return ENXIO;
909 }
910
911 if (sc->pch.dma_was_active) {
913 }
914
915 if (sc->rch.dma_was_active) {
917 }
918 snd_mtxunlock(sc->lock);
919
920 return 0;
921}
922
923static device_method_t als_methods[] = {
924 /* Device interface */
925 DEVMETHOD(device_probe, als_pci_probe),
926 DEVMETHOD(device_attach, als_pci_attach),
927 DEVMETHOD(device_detach, als_pci_detach),
928 DEVMETHOD(device_suspend, als_pci_suspend),
929 DEVMETHOD(device_resume, als_pci_resume),
930 { 0, 0 }
931};
932
933static driver_t als_driver = {
934 "pcm",
937};
938
939DRIVER_MODULE(snd_als4000, pci, als_driver, pcm_devclass, 0, 0);
941MODULE_VERSION(snd_als4000, 1);
u_int32_t data
Definition: ac97_if.m:60
void * devinfo
Definition: ac97_if.m:47
static int als_resource_grab(device_t dev, struct sc_info *sc)
Definition: als4000.c:761
static u_int32_t alsmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
Definition: als4000.c:603
static struct pcmchan_caps als_caps
Definition: als4000.c:104
static int als_pci_probe(device_t dev)
Definition: als4000.c:726
static driver_t als_driver
Definition: als4000.c:933
static u_int32_t als_gcr_rd(struct sc_info *sc, int index)
Definition: als4000.c:110
static void als_resource_free(device_t dev, struct sc_info *sc)
Definition: als4000.c:736
#define ALS_8BIT_CMD(x, y)
Definition: als4000.c:315
DRIVER_MODULE(snd_als4000, pci, als_driver, pcm_devclass, 0, 0)
static kobj_method_t als_mixer_methods[]
Definition: als4000.c:625
static int als_pci_resume(device_t dev)
Definition: als4000.c:894
MODULE_VERSION(snd_als4000, 1)
#define ALS_DEFAULT_BUFSZ
Definition: als4000.c:59
static int als_capture_stop(struct sc_chinfo *ch)
Definition: als4000.c:479
static int alsrchan_trigger(kobj_t obj, void *data, int go)
Definition: als4000.c:493
static u_int32_t als_format[]
Definition: als4000.c:92
static kobj_method_t alsrchan_methods[]
Definition: als4000.c:512
static void als_capture_start(struct sc_chinfo *ch)
Definition: als4000.c:451
struct playback_command playback_cmds[]
static int als_pci_suspend(device_t dev)
Definition: als4000.c:881
struct sb16props amt[SOUND_MIXER_NRDEVICES]
static void als_gcr_wr(struct sc_info *sc, int index, int data)
Definition: als4000.c:117
static const struct playback_command * als_get_playback_command(u_int32_t format)
Definition: als4000.c:331
static int alschan_setformat(kobj_t obj, void *data, u_int32_t format)
Definition: als4000.c:237
static void als_intr(void *p)
Definition: als4000.c:637
SND_DECLARE_FILE("$FreeBSD$")
static u_int32_t alschan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
Definition: als4000.c:264
static int als_playback_stop(struct sc_chinfo *ch)
Definition: als4000.c:378
CHANNEL_DECLARE(alspchan)
static void als_esp_wr(struct sc_info *sc, u_int8_t data)
Definition: als4000.c:150
static int als_esp_reset(struct sc_info *sc)
Definition: als4000.c:169
static void als_playback_start(struct sc_chinfo *ch)
Definition: als4000.c:347
static int als_init(struct sc_info *sc)
Definition: als4000.c:680
static u_int8_t als_intr_rd(struct sc_info *sc)
Definition: als4000.c:124
static int als_pci_detach(device_t dev)
Definition: als4000.c:864
static u_int32_t alschan_getptr(kobj_t obj, void *data)
Definition: als4000.c:277
static u_int32_t alschan_setspeed(kobj_t obj, void *data, u_int32_t speed)
Definition: als4000.c:246
MODULE_DEPEND(snd_als4000, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER)
static void als_mix_wr(struct sc_info *sc, u_int8_t index, u_int8_t data)
Definition: als4000.c:143
static device_method_t als_methods[]
Definition: als4000.c:923
static void als_set_speed(struct sc_chinfo *ch)
Definition: als4000.c:297
static u_int8_t als_get_fifo_format(struct sc_info *sc, u_int32_t format)
Definition: als4000.c:434
static int alspchan_trigger(kobj_t obj, void *data, int go)
Definition: als4000.c:394
static kobj_method_t alspchan_methods[]
Definition: als4000.c:418
static int alsmix_init(struct snd_mixer *m)
Definition: als4000.c:554
static struct pcmchan_caps * alschan_getcaps(kobj_t obj, void *data)
Definition: als4000.c:291
static u_int8_t als_ack_read(struct sc_info *sc, u_int8_t addr)
Definition: als4000.c:197
static int alsmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
Definition: als4000.c:571
static void als_uninit(struct sc_info *sc)
Definition: als4000.c:716
static u_int8_t als_mix_rd(struct sc_info *sc, u_int8_t index)
Definition: als4000.c:136
static void als_intr_wr(struct sc_info *sc, u_int8_t data)
Definition: als4000.c:130
static int als_pci_attach(device_t dev)
Definition: als4000.c:807
static void * alschan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
Definition: als4000.c:207
MIXER_DECLARE(als_mixer)
#define ALS_16BIT_CMD(x, y)
Definition: als4000.c:316
#define DEB(x)
Definition: als4000.c:56
#define ALS_ESP_WR_STATUS
Definition: als4000.h:80
#define ALS_FIFO1_SIGNED
Definition: als4000.h:72
#define ALS_CR1E_ACK_PORT
Definition: als4000.h:76
#define ALS_GCR_FIFO1_COUNT
Definition: als4000.h:50
#define ALS_ESP_RST
Definition: als4000.h:75
#define ALS_FIFO1_CONTROL
Definition: als4000.h:67
#define ALS_MIDI_DATA
Definition: als4000.h:85
#define ALS_SB16_DMA_SETUP
Definition: als4000.h:61
#define ALS_IRQ_MPUIN
Definition: als4000.h:91
#define ALS_GCR_INDEX
Definition: als4000.h:37
#define ALS_FIFO1_STEREO
Definition: als4000.h:71
#define ALS_GCR_FIFO0_STATUS
Definition: als4000.h:48
#define ALS_GCR_DMA0_START
Definition: als4000.h:40
#define ALS_FIFO1_LENGTH_LO
Definition: als4000.h:65
#define ALS_IRQ_CR1E
Definition: als4000.h:92
#define ALS_SB16_CONFIG
Definition: als4000.h:63
#define ALS_GCR_DMA0_MODE
Definition: als4000.h:41
#define ALS_IRQ_STATUS16
Definition: als4000.h:90
#define ALS_IRQ_STATUS8
Definition: als4000.h:89
#define ALS_FIFO1_STOP
Definition: als4000.h:68
#define ALS_ESP_RD_STATUS16
Definition: als4000.h:82
#define ALS_MIXER_DATA
Definition: als4000.h:58
#define ALS_GCR_MISC
Definition: als4000.h:38
#define ALS_GCR_DATA
Definition: als4000.h:36
#define ALS_GCR_DMA_EMULATION
Definition: als4000.h:46
#define ALS_ESP_RD_DATA
Definition: als4000.h:78
#define ALS_FIFO1_8BIT
Definition: als4000.h:73
#define ALS_MIXER_INDEX
Definition: als4000.h:59
#define ALS_ESP_SAMPLE_RATE
Definition: als4000.h:83
#define ALS_FIFO1_RUN
Definition: als4000.h:69
#define ALS_PCI_ID0
Definition: als4000.h:31
#define ALS_GCR_FIFO1_START
Definition: als4000.h:49
#define ALS_ESP_WR_DATA
Definition: als4000.h:79
#define ALS_FIFO1_LENGTH_HI
Definition: als4000.h:66
#define ALS_GCR_FIFO1_STATUS
Definition: als4000.h:52
#define ALS_SB_MPU_IRQ
Definition: als4000.h:56
#define ALS_ESP_RD_STATUS8
Definition: als4000.h:81
uint32_t format
Definition: audio_dai_if.m:39
uint32_t speed
Definition: audio_dai_if.m:86
int go
Definition: audio_dai_if.m:64
METHOD int intr
Definition: audio_dai_if.m:91
int sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags, unsigned int size)
Definition: buffer.c:93
bus_addr_t sndbuf_getbufaddr(struct snd_dbuf *buf)
Definition: buffer.c:66
unsigned int sndbuf_getsize(struct snd_dbuf *b)
Definition: buffer.c:435
int sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
Definition: buffer.c:164
void chn_intr(struct pcm_channel *c)
Definition: channel.c:660
#define PCMDIR_PLAY
Definition: channel.h:339
#define PCMTRIG_START
Definition: channel.h:344
#define PCMTRIG_STOP
Definition: channel.h:347
#define PCMDIR_REC
Definition: channel.h:341
#define PCMTRIG_COMMON(x)
Definition: channel.h:350
#define PCMTRIG_ABORT
Definition: channel.h:348
struct pcm_channel * c
Definition: channel_if.m:106
METHOD int free
Definition: channel_if.m:110
u_int32_t blocksize
Definition: channel_if.m:140
struct pcmchan_matrix * m
Definition: channel_if.m:232
struct snd_dbuf * b
Definition: channel_if.m:105
unsigned right
Definition: es137x.c:261
unsigned left
Definition: es137x.c:260
u_int32_t count
Definition: feeder_if.m:86
uint8_t mask
Definition: hdac.c:212
int dir
Definition: hdac_if.m:45
bus_addr_t buf
Definition: hdac_if.m:63
uint8_t r
uint8_t n
#define KOBJMETHOD_END
Definition: midi.c:76
static int mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
Definition: mixer.c:373
int mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
Definition: mixer.c:725
static int mixer_set(struct snd_mixer *m, u_int dev, u_int32_t muted, u_int lev)
Definition: mixer.c:247
void mix_setdevs(struct snd_mixer *m, u_int32_t v)
Definition: mixer.c:489
int mixer_reinit(device_t dev)
Definition: mixer.c:860
void * mix_getdevinfo(struct snd_mixer *m)
Definition: mixer.c:645
void mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
Record mask of available recording devices.
Definition: mixer.c:529
unsigned dev
Definition: mixer_if.m:59
u_int32_t src
Definition: mixer_if.m:66
bool * status
u_int index
uint64_t * addr
#define PCIR_BAR(x)
#define DSP_MODE_U8STEREO
Definition: sb.h:96
#define SB16_OMASK
Definition: sb.h:215
#define DSP_F16_AUTO
Definition: sb.h:134
#define DSP_MODE_S16STEREO
Definition: sb.h:98
#define DSP_F16_FIFO_ON
Definition: sb.h:135
#define DSP_F16_DAC
Definition: sb.h:132
#define DSP_MODE_U8MONO
Definition: sb.h:95
#define DSP_CMD_SPKON
Definition: sb.h:100
#define SB16_IMASK_L
Definition: sb.h:213
#define DSP_MODE_S16MONO
Definition: sb.h:97
#define SB16_IMASK_R
Definition: sb.h:214
#define IRQ_STAT
Definition: sb.h:193
void * snd_mtxcreate(const char *desc, const char *type)
Definition: sound.c:88
void * pcm_getdevinfo(device_t dev)
Definition: sound.c:832
int pcm_setstatus(device_t dev, char *str)
Definition: sound.c:766
devclass_t pcm_devclass
Definition: sound.c:49
void snd_mtxfree(void *m)
Definition: sound.c:98
int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
Definition: sound.c:692
int pcm_unregister(device_t dev)
Definition: sound.c:1170
int pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
Definition: sound.c:1080
int snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
Definition: sound.c:117
unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
Definition: sound.c:840
#define PCM_KLDSTRING(a)
Definition: sound.h:619
#define SND_FORMAT(f, c, e)
Definition: sound.h:238
#define SOUND_PREFVER
Definition: sound.h:103
#define SOUND_MAXVER
Definition: sound.h:104
#define DSP_DEFAULT_SPEED
Definition: sound.h:295
#define snd_mtxlock(m)
Definition: sound.h:347
#define snd_mtxunlock(m)
Definition: sound.h:348
#define SOUND_MINVER
Definition: sound.h:102
#define PCM_SOFTC_SIZE
Definition: sound.h:96
#define SND_STATUSLEN
Definition: sound.h:98
#define AFMT_16BIT
Definition: sound.h:191
u_int8_t dma_stop
Definition: als4000.c:322
u_int8_t format_val
Definition: als4000.c:320
u_int32_t pcm_format
Definition: als4000.c:319
u_int8_t dma_prog
Definition: als4000.c:321
u_int8_t lreg
Definition: als4000.c:533
u_int8_t rreg
Definition: als4000.c:534
u_int8_t bits
Definition: als4000.c:535
u_int8_t oselect
Definition: als4000.c:536
u_int8_t iselect
Definition: als4000.c:537
u_int32_t dma_was_active
Definition: als4000.c:71
u_int32_t speed
Definition: als4000.c:70
u_int8_t gcr_fifo_status
Definition: als4000.c:72
struct pcm_channel * channel
Definition: als4000.c:68
u_int32_t format
Definition: als4000.c:70
u_int32_t bps
Definition: als4000.c:70
struct sc_info * parent
Definition: als4000.c:67
u_int32_t dma_active
Definition: als4000.c:71
u_int32_t phys_buf
Definition: als4000.c:70
int dir
Definition: als4000.c:73
struct snd_dbuf * buffer
Definition: als4000.c:69
bus_space_tag_t st
Definition: als4000.c:78
struct mtx * lock
Definition: als4000.c:84
unsigned int bufsz
Definition: als4000.c:86
struct resource * irq
Definition: als4000.c:81
device_t dev
Definition: als4000.c:77
int irqid
Definition: als4000.c:82
struct resource * reg
Definition: als4000.c:81
struct sc_chinfo pch rch
Definition: als4000.c:87
void * ih
Definition: als4000.c:83
int regid
Definition: als4000.c:82
bus_dma_tag_t parent_dmat
Definition: als4000.c:80
struct sc_chinfo pch
Definition: cs4281.c:99
struct sc_chinfo ch[3]
Definition: ich.c:192
bus_space_handle_t sh
Definition: als4000.c:79