1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2001 Orion Hodson <[email protected]>
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>
44 #include <dev/sound/pci/als4000.h>
45
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48
49 #include "mixer_if.h"
50
51 SND_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
64 struct sc_info;
65
66 struct sc_chinfo {
67 struct sc_info *parent;
68 struct pcm_channel *channel;
69 struct snd_dbuf *buffer;
70 u_int32_t format, speed, phys_buf, bps;
71 u_int32_t dma_active:1, dma_was_active:1;
72 u_int8_t gcr_fifo_status;
73 int dir;
74 };
75
76 struct 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;
82 int regid, irqid;
83 void *ih;
84 struct mtx *lock;
85
86 unsigned int bufsz;
87 struct sc_chinfo pch, rch;
88 };
89
90 /* Channel caps */
91
92 static 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 */
104 static struct pcmchan_caps als_caps = { 4000, 44100, als_format, 0 };
105
106 /* ------------------------------------------------------------------------- */
107 /* Register Utilities */
108
109 static u_int32_t
als_gcr_rd(struct sc_info * sc,int index)110 als_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
116 static void
als_gcr_wr(struct sc_info * sc,int index,int data)117 als_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
123 static u_int8_t
als_intr_rd(struct sc_info * sc)124 als_intr_rd(struct sc_info *sc)
125 {
126 return bus_space_read_1(sc->st, sc->sh, ALS_SB_MPU_IRQ);
127 }
128
129 static void
als_intr_wr(struct sc_info * sc,u_int8_t data)130 als_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
135 static u_int8_t
als_mix_rd(struct sc_info * sc,u_int8_t index)136 als_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
142 static void
als_mix_wr(struct sc_info * sc,u_int8_t index,u_int8_t data)143 als_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
149 static void
als_esp_wr(struct sc_info * sc,u_int8_t data)150 als_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
168 static int
als_esp_reset(struct sc_info * sc)169 als_esp_reset(struct sc_info *sc)
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
196 static u_int8_t
als_ack_read(struct sc_info * sc,u_int8_t addr)197 als_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
206 static void *
alschan_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)207 alschan_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;
216 ch->gcr_fifo_status = ALS_GCR_FIFO0_STATUS;
217 } else {
218 ch = &sc->rch;
219 ch->gcr_fifo_status = ALS_GCR_FIFO1_STATUS;
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);
226 ch->speed = DSP_DEFAULT_SPEED;
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
236 static int
alschan_setformat(kobj_t obj,void * data,u_int32_t format)237 alschan_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
245 static u_int32_t
alschan_setspeed(kobj_t obj,void * data,u_int32_t speed)246 alschan_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
263 static u_int32_t
alschan_setblocksize(kobj_t obj,void * data,u_int32_t blocksize)264 alschan_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 }
272 sndbuf_resize(ch->buffer, 2, blocksize);
273 return blocksize;
274 }
275
276 static u_int32_t
alschan_getptr(kobj_t obj,void * data)277 alschan_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
290 static struct pcmchan_caps*
alschan_getcaps(kobj_t obj,void * data)291 alschan_getcaps(kobj_t obj, void *data)
292 {
293 return &als_caps;
294 }
295
296 static void
als_set_speed(struct sc_chinfo * ch)297 als_set_speed(struct sc_chinfo *ch)
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) {
304 als_esp_wr(sc, ALS_ESP_SAMPLE_RATE);
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
316 #define ALS_8BIT_CMD(x, y) { (x), (y), DSP_DMA8, DSP_CMD_DMAPAUSE_8 }
317 #define ALS_16BIT_CMD(x, y) { (x), (y), DSP_DMA16, DSP_CMD_DMAPAUSE_16 }
318
319 struct playback_command {
320 u_int32_t pcm_format; /* newpcm format */
321 u_int8_t format_val; /* sb16 format value */
322 u_int8_t dma_prog; /* sb16 dma program */
323 u_int8_t dma_stop; /* sb16 stop register */
324 } static const playback_cmds[] = {
325 ALS_8BIT_CMD(SND_FORMAT(AFMT_U8, 1, 0), DSP_MODE_U8MONO),
326 ALS_8BIT_CMD(SND_FORMAT(AFMT_U8, 2, 0), DSP_MODE_U8STEREO),
327 ALS_16BIT_CMD(SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_MODE_S16MONO),
328 ALS_16BIT_CMD(SND_FORMAT(AFMT_S16_LE, 2, 0), DSP_MODE_S16STEREO),
329 };
330
331 static const struct playback_command*
als_get_playback_command(u_int32_t format)332 als_get_playback_command(u_int32_t format)
333 {
334 u_int32_t i, n;
335
336 n = sizeof(playback_cmds) / sizeof(playback_cmds[0]);
337 for (i = 0; i < n; i++) {
338 if (playback_cmds[i].pcm_format == format) {
339 return &playback_cmds[i];
340 }
341 }
342 DEB(printf("als_get_playback_command: invalid format 0x%08x\n",
343 format));
344 return &playback_cmds[0];
345 }
346
347 static void
als_playback_start(struct sc_chinfo * ch)348 als_playback_start(struct sc_chinfo *ch)
349 {
350 const struct playback_command *p;
351 struct sc_info *sc = ch->parent;
352 u_int32_t buf, bufsz, count, dma_prog;
353
354 buf = sndbuf_getbufaddr(ch->buffer);
355 bufsz = sndbuf_getsize(ch->buffer);
356 count = bufsz / 2;
357 if (ch->format & AFMT_16BIT)
358 count /= 2;
359 count--;
360
361 als_esp_wr(sc, DSP_CMD_SPKON);
362 als_set_speed(ch);
363
364 als_gcr_wr(sc, ALS_GCR_DMA0_START, buf);
365 als_gcr_wr(sc, ALS_GCR_DMA0_MODE, (bufsz - 1) | 0x180000);
366
367 p = als_get_playback_command(ch->format);
368 dma_prog = p->dma_prog | DSP_F16_DAC | DSP_F16_AUTO | DSP_F16_FIFO_ON;
369
370 als_esp_wr(sc, dma_prog);
371 als_esp_wr(sc, p->format_val);
372 als_esp_wr(sc, count & 0xff);
373 als_esp_wr(sc, count >> 8);
374
375 ch->dma_active = 1;
376 }
377
378 static int
als_playback_stop(struct sc_chinfo * ch)379 als_playback_stop(struct sc_chinfo *ch)
380 {
381 const struct playback_command *p;
382 struct sc_info *sc = ch->parent;
383 u_int32_t active;
384
385 active = ch->dma_active;
386 if (active) {
387 p = als_get_playback_command(ch->format);
388 als_esp_wr(sc, p->dma_stop);
389 }
390 ch->dma_active = 0;
391 return active;
392 }
393
394 static int
alspchan_trigger(kobj_t obj,void * data,int go)395 alspchan_trigger(kobj_t obj, void *data, int go)
396 {
397 struct sc_chinfo *ch = data;
398 struct sc_info *sc = ch->parent;
399
400 if (!PCMTRIG_COMMON(go))
401 return 0;
402
403 snd_mtxlock(sc->lock);
404 switch(go) {
405 case PCMTRIG_START:
406 als_playback_start(ch);
407 break;
408 case PCMTRIG_STOP:
409 case PCMTRIG_ABORT:
410 als_playback_stop(ch);
411 break;
412 default:
413 break;
414 }
415 snd_mtxunlock(sc->lock);
416 return 0;
417 }
418
419 static kobj_method_t alspchan_methods[] = {
420 KOBJMETHOD(channel_init, alschan_init),
421 KOBJMETHOD(channel_setformat, alschan_setformat),
422 KOBJMETHOD(channel_setspeed, alschan_setspeed),
423 KOBJMETHOD(channel_setblocksize, alschan_setblocksize),
424 KOBJMETHOD(channel_trigger, alspchan_trigger),
425 KOBJMETHOD(channel_getptr, alschan_getptr),
426 KOBJMETHOD(channel_getcaps, alschan_getcaps),
427 KOBJMETHOD_END
428 };
429 CHANNEL_DECLARE(alspchan);
430
431 /* ------------------------------------------------------------------------- */
432 /* Capture channel implementation */
433
434 static u_int8_t
als_get_fifo_format(struct sc_info * sc,u_int32_t format)435 als_get_fifo_format(struct sc_info *sc, u_int32_t format)
436 {
437 switch (format) {
438 case SND_FORMAT(AFMT_U8, 1, 0):
439 return ALS_FIFO1_8BIT;
440 case SND_FORMAT(AFMT_U8, 2, 0):
441 return ALS_FIFO1_8BIT | ALS_FIFO1_STEREO;
442 case SND_FORMAT(AFMT_S16_LE, 1, 0):
443 return ALS_FIFO1_SIGNED;
444 case SND_FORMAT(AFMT_S16_LE, 2, 0):
445 return ALS_FIFO1_SIGNED | ALS_FIFO1_STEREO;
446 }
447 device_printf(sc->dev, "format not found: 0x%08x\n", format);
448 return ALS_FIFO1_8BIT;
449 }
450
451 static void
als_capture_start(struct sc_chinfo * ch)452 als_capture_start(struct sc_chinfo *ch)
453 {
454 struct sc_info *sc = ch->parent;
455 u_int32_t buf, bufsz, count, dma_prog;
456
457 buf = sndbuf_getbufaddr(ch->buffer);
458 bufsz = sndbuf_getsize(ch->buffer);
459 count = bufsz / 2;
460 if (ch->format & AFMT_16BIT)
461 count /= 2;
462 count--;
463
464 als_esp_wr(sc, DSP_CMD_SPKON);
465 als_set_speed(ch);
466
467 als_gcr_wr(sc, ALS_GCR_FIFO1_START, buf);
468 als_gcr_wr(sc, ALS_GCR_FIFO1_COUNT, (bufsz - 1));
469
470 als_mix_wr(sc, ALS_FIFO1_LENGTH_LO, count & 0xff);
471 als_mix_wr(sc, ALS_FIFO1_LENGTH_HI, count >> 8);
472
473 dma_prog = ALS_FIFO1_RUN | als_get_fifo_format(sc, ch->format);
474 als_mix_wr(sc, ALS_FIFO1_CONTROL, dma_prog);
475
476 ch->dma_active = 1;
477 }
478
479 static int
als_capture_stop(struct sc_chinfo * ch)480 als_capture_stop(struct sc_chinfo *ch)
481 {
482 struct sc_info *sc = ch->parent;
483 u_int32_t active;
484
485 active = ch->dma_active;
486 if (active) {
487 als_mix_wr(sc, ALS_FIFO1_CONTROL, ALS_FIFO1_STOP);
488 }
489 ch->dma_active = 0;
490 return active;
491 }
492
493 static int
alsrchan_trigger(kobj_t obj,void * data,int go)494 alsrchan_trigger(kobj_t obj, void *data, int go)
495 {
496 struct sc_chinfo *ch = data;
497 struct sc_info *sc = ch->parent;
498
499 snd_mtxlock(sc->lock);
500 switch(go) {
501 case PCMTRIG_START:
502 als_capture_start(ch);
503 break;
504 case PCMTRIG_STOP:
505 case PCMTRIG_ABORT:
506 als_capture_stop(ch);
507 break;
508 }
509 snd_mtxunlock(sc->lock);
510 return 0;
511 }
512
513 static kobj_method_t alsrchan_methods[] = {
514 KOBJMETHOD(channel_init, alschan_init),
515 KOBJMETHOD(channel_setformat, alschan_setformat),
516 KOBJMETHOD(channel_setspeed, alschan_setspeed),
517 KOBJMETHOD(channel_setblocksize, alschan_setblocksize),
518 KOBJMETHOD(channel_trigger, alsrchan_trigger),
519 KOBJMETHOD(channel_getptr, alschan_getptr),
520 KOBJMETHOD(channel_getcaps, alschan_getcaps),
521 KOBJMETHOD_END
522 };
523 CHANNEL_DECLARE(alsrchan);
524
525 /* ------------------------------------------------------------------------- */
526 /* Mixer related */
527
528 /*
529 * ALS4000 has an sb16 mixer, with some additional controls that we do
530 * not yet a means to support.
531 */
532
533 struct sb16props {
534 u_int8_t lreg;
535 u_int8_t rreg;
536 u_int8_t bits;
537 u_int8_t oselect;
538 u_int8_t iselect; /* left input mask */
539 } static const amt[SOUND_MIXER_NRDEVICES] = {
540 [SOUND_MIXER_VOLUME] = { 0x30, 0x31, 5, 0x00, 0x00 },
541 [SOUND_MIXER_PCM] = { 0x32, 0x33, 5, 0x00, 0x00 },
542 [SOUND_MIXER_SYNTH] = { 0x34, 0x35, 5, 0x60, 0x40 },
543 [SOUND_MIXER_CD] = { 0x36, 0x37, 5, 0x06, 0x04 },
544 [SOUND_MIXER_LINE] = { 0x38, 0x39, 5, 0x18, 0x10 },
545 [SOUND_MIXER_MIC] = { 0x3a, 0x00, 5, 0x01, 0x01 },
546 [SOUND_MIXER_SPEAKER] = { 0x3b, 0x00, 2, 0x00, 0x00 },
547 [SOUND_MIXER_IGAIN] = { 0x3f, 0x40, 2, 0x00, 0x00 },
548 [SOUND_MIXER_OGAIN] = { 0x41, 0x42, 2, 0x00, 0x00 },
549 /* The following have register values but no h/w implementation */
550 [SOUND_MIXER_TREBLE] = { 0x44, 0x45, 4, 0x00, 0x00 },
551 [SOUND_MIXER_BASS] = { 0x46, 0x47, 4, 0x00, 0x00 }
552 };
553
554 static int
alsmix_init(struct snd_mixer * m)555 alsmix_init(struct snd_mixer *m)
556 {
557 u_int32_t i, v;
558
559 for (i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
560 if (amt[i].bits) v |= 1 << i;
561 }
562 mix_setdevs(m, v);
563
564 for (i = v = 0; i < SOUND_MIXER_NRDEVICES; i++) {
565 if (amt[i].iselect) v |= 1 << i;
566 }
567 mix_setrecdevs(m, v);
568 return 0;
569 }
570
571 static int
alsmix_set(struct snd_mixer * m,unsigned dev,unsigned left,unsigned right)572 alsmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
573 {
574 struct sc_info *sc = mix_getdevinfo(m);
575 u_int32_t r, l, v, mask;
576
577 /* Fill upper n bits in mask with 1's */
578 mask = ((1 << amt[dev].bits) - 1) << (8 - amt[dev].bits);
579
580 l = (left * mask / 100) & mask;
581 v = als_mix_rd(sc, amt[dev].lreg) & ~mask;
582 als_mix_wr(sc, amt[dev].lreg, l | v);
583
584 if (amt[dev].rreg) {
585 r = (right * mask / 100) & mask;
586 v = als_mix_rd(sc, amt[dev].rreg) & ~mask;
587 als_mix_wr(sc, amt[dev].rreg, r | v);
588 } else {
589 r = 0;
590 }
591
592 /* Zero gain does not mute channel from output, but this does. */
593 v = als_mix_rd(sc, SB16_OMASK);
594 if (l == 0 && r == 0) {
595 v &= ~amt[dev].oselect;
596 } else {
597 v |= amt[dev].oselect;
598 }
599 als_mix_wr(sc, SB16_OMASK, v);
600 return 0;
601 }
602
603 static u_int32_t
alsmix_setrecsrc(struct snd_mixer * m,u_int32_t src)604 alsmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
605 {
606 struct sc_info *sc = mix_getdevinfo(m);
607 u_int32_t i, l, r;
608
609 for (i = l = r = 0; i < SOUND_MIXER_NRDEVICES; i++) {
610 if (src & (1 << i)) {
611 if (amt[i].iselect == 1) { /* microphone */
612 l |= amt[i].iselect;
613 r |= amt[i].iselect;
614 } else {
615 l |= amt[i].iselect;
616 r |= amt[i].iselect >> 1;
617 }
618 }
619 }
620
621 als_mix_wr(sc, SB16_IMASK_L, l);
622 als_mix_wr(sc, SB16_IMASK_R, r);
623 return src;
624 }
625
626 static kobj_method_t als_mixer_methods[] = {
627 KOBJMETHOD(mixer_init, alsmix_init),
628 KOBJMETHOD(mixer_set, alsmix_set),
629 KOBJMETHOD(mixer_setrecsrc, alsmix_setrecsrc),
630 KOBJMETHOD_END
631 };
632 MIXER_DECLARE(als_mixer);
633
634 /* ------------------------------------------------------------------------- */
635 /* Interrupt Handler */
636
637 static void
als_intr(void * p)638 als_intr(void *p)
639 {
640 struct sc_info *sc = (struct sc_info *)p;
641 u_int8_t intr, sb_status;
642
643 snd_mtxlock(sc->lock);
644 intr = als_intr_rd(sc);
645
646 if (intr & 0x80) {
647 snd_mtxunlock(sc->lock);
648 chn_intr(sc->pch.channel);
649 snd_mtxlock(sc->lock);
650 }
651
652 if (intr & 0x40) {
653 snd_mtxunlock(sc->lock);
654 chn_intr(sc->rch.channel);
655 snd_mtxlock(sc->lock);
656 }
657
658 /* ACK interrupt in PCI core */
659 als_intr_wr(sc, intr);
660
661 /* ACK interrupt in SB core */
662 sb_status = als_mix_rd(sc, IRQ_STAT);
663
664 if (sb_status & ALS_IRQ_STATUS8)
665 als_ack_read(sc, ALS_ESP_RD_STATUS8);
666 if (sb_status & ALS_IRQ_STATUS16)
667 als_ack_read(sc, ALS_ESP_RD_STATUS16);
668 if (sb_status & ALS_IRQ_MPUIN)
669 als_ack_read(sc, ALS_MIDI_DATA);
670 if (sb_status & ALS_IRQ_CR1E)
671 als_ack_read(sc, ALS_CR1E_ACK_PORT);
672
673 snd_mtxunlock(sc->lock);
674 return;
675 }
676
677 /* ------------------------------------------------------------------------- */
678 /* H/W initialization */
679
680 static int
als_init(struct sc_info * sc)681 als_init(struct sc_info *sc)
682 {
683 u_int32_t i, v;
684
685 /* Reset Chip */
686 if (als_esp_reset(sc)) {
687 return 1;
688 }
689
690 /* Enable write on DMA_SETUP register */
691 v = als_mix_rd(sc, ALS_SB16_CONFIG);
692 als_mix_wr(sc, ALS_SB16_CONFIG, v | 0x80);
693
694 /* Select DMA0 */
695 als_mix_wr(sc, ALS_SB16_DMA_SETUP, 0x01);
696
697 /* Disable write on DMA_SETUP register */
698 als_mix_wr(sc, ALS_SB16_CONFIG, v & 0x7f);
699
700 /* Enable interrupts */
701 v = als_gcr_rd(sc, ALS_GCR_MISC);
702 als_gcr_wr(sc, ALS_GCR_MISC, v | 0x28000);
703
704 /* Black out GCR DMA registers */
705 for (i = 0x91; i <= 0x96; i++) {
706 als_gcr_wr(sc, i, 0);
707 }
708
709 /* Emulation mode */
710 v = als_gcr_rd(sc, ALS_GCR_DMA_EMULATION);
711 als_gcr_wr(sc, ALS_GCR_DMA_EMULATION, v);
712 DEB(printf("GCR_DMA_EMULATION 0x%08x\n", v));
713 return 0;
714 }
715
716 static void
als_uninit(struct sc_info * sc)717 als_uninit(struct sc_info *sc)
718 {
719 /* Disable interrupts */
720 als_gcr_wr(sc, ALS_GCR_MISC, 0);
721 }
722
723 /* ------------------------------------------------------------------------- */
724 /* Probe and attach card */
725
726 static int
als_pci_probe(device_t dev)727 als_pci_probe(device_t dev)
728 {
729 if (pci_get_devid(dev) == ALS_PCI_ID0) {
730 device_set_desc(dev, "Avance Logic ALS4000");
731 return BUS_PROBE_DEFAULT;
732 }
733 return ENXIO;
734 }
735
736 static void
als_resource_free(device_t dev,struct sc_info * sc)737 als_resource_free(device_t dev, struct sc_info *sc)
738 {
739 if (sc->reg) {
740 bus_release_resource(dev, SYS_RES_IOPORT, sc->regid, sc->reg);
741 sc->reg = NULL;
742 }
743 if (sc->ih) {
744 bus_teardown_intr(dev, sc->irq, sc->ih);
745 sc->ih = NULL;
746 }
747 if (sc->irq) {
748 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
749 sc->irq = NULL;
750 }
751 if (sc->parent_dmat) {
752 bus_dma_tag_destroy(sc->parent_dmat);
753 sc->parent_dmat = 0;
754 }
755 if (sc->lock) {
756 snd_mtxfree(sc->lock);
757 sc->lock = NULL;
758 }
759 }
760
761 static int
als_resource_grab(device_t dev,struct sc_info * sc)762 als_resource_grab(device_t dev, struct sc_info *sc)
763 {
764 sc->regid = PCIR_BAR(0);
765 sc->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->regid,
766 RF_ACTIVE);
767 if (sc->reg == NULL) {
768 device_printf(dev, "unable to allocate register space\n");
769 goto bad;
770 }
771 sc->st = rman_get_bustag(sc->reg);
772 sc->sh = rman_get_bushandle(sc->reg);
773
774 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
775 RF_ACTIVE | RF_SHAREABLE);
776 if (sc->irq == NULL) {
777 device_printf(dev, "unable to allocate interrupt\n");
778 goto bad;
779 }
780
781 if (snd_setup_intr(dev, sc->irq, INTR_MPSAFE, als_intr,
782 sc, &sc->ih)) {
783 device_printf(dev, "unable to setup interrupt\n");
784 goto bad;
785 }
786
787 sc->bufsz = pcm_getbuffersize(dev, 4096, ALS_DEFAULT_BUFSZ, 65536);
788
789 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev),
790 /*alignment*/2, /*boundary*/0,
791 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
792 /*highaddr*/BUS_SPACE_MAXADDR,
793 /*filter*/NULL, /*filterarg*/NULL,
794 /*maxsize*/sc->bufsz,
795 /*nsegments*/1, /*maxsegz*/0x3ffff,
796 /*flags*/0, /*lockfunc*/NULL,
797 /*lockarg*/NULL, &sc->parent_dmat) != 0) {
798 device_printf(dev, "unable to create dma tag\n");
799 goto bad;
800 }
801 return 0;
802 bad:
803 als_resource_free(dev, sc);
804 return ENXIO;
805 }
806
807 static int
als_pci_attach(device_t dev)808 als_pci_attach(device_t dev)
809 {
810 struct sc_info *sc;
811 char status[SND_STATUSLEN];
812
813 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
814 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "snd_als4000 softc");
815 sc->dev = dev;
816
817 pci_enable_busmaster(dev);
818 /*
819 * By default the power to the various components on the
820 * ALS4000 is entirely controlled by the pci powerstate. We
821 * could attempt finer grained control by setting GCR6.31.
822 */
823 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
824 /* Reset the power state. */
825 device_printf(dev, "chip is in D%d power mode "
826 "-- setting to D0\n", pci_get_powerstate(dev));
827 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
828 }
829
830 if (als_resource_grab(dev, sc)) {
831 device_printf(dev, "failed to allocate resources\n");
832 goto bad_attach;
833 }
834
835 if (als_init(sc)) {
836 device_printf(dev, "failed to initialize hardware\n");
837 goto bad_attach;
838 }
839
840 if (mixer_init(dev, &als_mixer_class, sc)) {
841 device_printf(dev, "failed to initialize mixer\n");
842 goto bad_attach;
843 }
844
845 if (pcm_register(dev, sc, 1, 1)) {
846 device_printf(dev, "failed to register pcm entries\n");
847 goto bad_attach;
848 }
849
850 pcm_addchan(dev, PCMDIR_PLAY, &alspchan_class, sc);
851 pcm_addchan(dev, PCMDIR_REC, &alsrchan_class, sc);
852
853 snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd %s",
854 rman_get_start(sc->reg), rman_get_start(sc->irq),PCM_KLDSTRING(snd_als4000));
855 pcm_setstatus(dev, status);
856 return 0;
857
858 bad_attach:
859 als_resource_free(dev, sc);
860 free(sc, M_DEVBUF);
861 return ENXIO;
862 }
863
864 static int
als_pci_detach(device_t dev)865 als_pci_detach(device_t dev)
866 {
867 struct sc_info *sc;
868 int r;
869
870 r = pcm_unregister(dev);
871 if (r)
872 return r;
873
874 sc = pcm_getdevinfo(dev);
875 als_uninit(sc);
876 als_resource_free(dev, sc);
877 free(sc, M_DEVBUF);
878 return 0;
879 }
880
881 static int
als_pci_suspend(device_t dev)882 als_pci_suspend(device_t dev)
883 {
884 struct sc_info *sc = pcm_getdevinfo(dev);
885
886 snd_mtxlock(sc->lock);
887 sc->pch.dma_was_active = als_playback_stop(&sc->pch);
888 sc->rch.dma_was_active = als_capture_stop(&sc->rch);
889 als_uninit(sc);
890 snd_mtxunlock(sc->lock);
891 return 0;
892 }
893
894 static int
als_pci_resume(device_t dev)895 als_pci_resume(device_t dev)
896 {
897 struct sc_info *sc = pcm_getdevinfo(dev);
898
899
900 snd_mtxlock(sc->lock);
901 if (als_init(sc) != 0) {
902 device_printf(dev, "unable to reinitialize the card\n");
903 snd_mtxunlock(sc->lock);
904 return ENXIO;
905 }
906
907 if (mixer_reinit(dev) != 0) {
908 device_printf(dev, "unable to reinitialize the mixer\n");
909 snd_mtxunlock(sc->lock);
910 return ENXIO;
911 }
912
913 if (sc->pch.dma_was_active) {
914 als_playback_start(&sc->pch);
915 }
916
917 if (sc->rch.dma_was_active) {
918 als_capture_start(&sc->rch);
919 }
920 snd_mtxunlock(sc->lock);
921
922 return 0;
923 }
924
925 static device_method_t als_methods[] = {
926 /* Device interface */
927 DEVMETHOD(device_probe, als_pci_probe),
928 DEVMETHOD(device_attach, als_pci_attach),
929 DEVMETHOD(device_detach, als_pci_detach),
930 DEVMETHOD(device_suspend, als_pci_suspend),
931 DEVMETHOD(device_resume, als_pci_resume),
932 { 0, 0 }
933 };
934
935 static driver_t als_driver = {
936 "pcm",
937 als_methods,
938 PCM_SOFTC_SIZE,
939 };
940
941 DRIVER_MODULE(snd_als4000, pci, als_driver, pcm_devclass, 0, 0);
942 MODULE_DEPEND(snd_als4000, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
943 MODULE_VERSION(snd_als4000, 1);
944