Say hello to a sound system update from FreeBSD. This includes the long
[dragonfly.git] / sys / dev / sound / isa / sb16.c
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  * Copyright 1997,1998 Luigi Rizzo.
4  *
5  * Derived from files in the Voxware 3.5 distribution,
6  * Copyright by Hannu Savolainen 1994, under the same copyright
7  * conditions.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/dev/sound/isa/sb16.c,v 1.90.2.1 2005/12/30 19:55:53 netchild Exp $
32  * $DragonFly: src/sys/dev/sound/isa/sb16.c,v 1.8 2007/01/04 21:47:02 corecode Exp $
33  */
34
35 #include <dev/sound/pcm/sound.h>
36
37 #include  <dev/sound/isa/sb.h>
38 #include  <dev/sound/chip.h>
39
40 #include <bus/isa/isavar.h>
41
42 #include "mixer_if.h"
43
44 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/isa/sb16.c,v 1.8 2007/01/04 21:47:02 corecode Exp $");
45
46 #define SB16_BUFFSIZE   4096
47 #define PLAIN_SB16(x) ((((x)->bd_flags) & (BD_F_SB16|BD_F_SB16X)) == BD_F_SB16)
48
49 static u_int32_t sb16_fmt8[] = {
50         AFMT_U8,
51         AFMT_STEREO | AFMT_U8,
52         0
53 };
54 static struct pcmchan_caps sb16_caps8 = {5000, 45000, sb16_fmt8, 0};
55
56 static u_int32_t sb16_fmt16[] = {
57         AFMT_S16_LE,
58         AFMT_STEREO | AFMT_S16_LE,
59         0
60 };
61 static struct pcmchan_caps sb16_caps16 = {5000, 45000, sb16_fmt16, 0};
62
63 static u_int32_t sb16x_fmt[] = {
64         AFMT_U8,
65         AFMT_STEREO | AFMT_U8,
66         AFMT_S16_LE,
67         AFMT_STEREO | AFMT_S16_LE,
68         0
69 };
70 static struct pcmchan_caps sb16x_caps = {5000, 49000, sb16x_fmt, 0};
71
72 struct sb_info;
73
74 struct sb_chinfo {
75         struct sb_info *parent;
76         struct pcm_channel *channel;
77         struct snd_dbuf *buffer;
78         int dir, run, dch;
79         u_int32_t fmt, spd, blksz;
80 };
81
82 struct sb_info {
83         struct resource *io_base;       /* I/O address for the board */
84         struct resource *irq;
85         struct resource *drq1;
86         struct resource *drq2;
87         void *ih;
88         bus_dma_tag_t parent_dmat;
89
90         unsigned int bufsize;
91         int bd_id;
92         u_long bd_flags;       /* board-specific flags */
93         int prio, prio16;
94         struct sb_chinfo pch, rch;
95         device_t parent_dev;
96 };
97
98 #if 0
99 static void sb_lock(struct sb_info *sb);
100 static void sb_unlock(struct sb_info *sb);
101 static int sb_rd(struct sb_info *sb, int reg);
102 static void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
103 static int sb_cmd(struct sb_info *sb, u_char val);
104 /* static int sb_cmd1(struct sb_info *sb, u_char cmd, int val); */
105 static int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
106 static u_int sb_get_byte(struct sb_info *sb);
107 static void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
108 static int sb_getmixer(struct sb_info *sb, u_int port);
109 static int sb_reset_dsp(struct sb_info *sb);
110
111 static void sb_intr(void *arg);
112 #endif
113
114 /*
115  * Common code for the midi and pcm functions
116  *
117  * sb_cmd write a single byte to the CMD port.
118  * sb_cmd1 write a CMD + 1 byte arg
119  * sb_cmd2 write a CMD + 2 byte arg
120  * sb_get_byte returns a single byte from the DSP data port
121  */
122
123 static void
124 sb_lock(struct sb_info *sb) {
125
126         sbc_lock(device_get_softc(sb->parent_dev));
127 }
128
129 static void
130 sb_lockassert(struct sb_info *sb) {
131
132         sbc_lockassert(device_get_softc(sb->parent_dev));
133 }
134
135 static void
136 sb_unlock(struct sb_info *sb) {
137
138         sbc_unlock(device_get_softc(sb->parent_dev));
139 }
140
141 static int
142 port_rd(struct resource *port, int off)
143 {
144         return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off);
145 }
146
147 static void
148 port_wr(struct resource *port, int off, u_int8_t data)
149 {
150         bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data);
151 }
152
153 static int
154 sb_rd(struct sb_info *sb, int reg)
155 {
156         return port_rd(sb->io_base, reg);
157 }
158
159 static void
160 sb_wr(struct sb_info *sb, int reg, u_int8_t val)
161 {
162         port_wr(sb->io_base, reg, val);
163 }
164
165 static int
166 sb_dspwr(struct sb_info *sb, u_char val)
167 {
168         int  i;
169
170         for (i = 0; i < 1000; i++) {
171                 if ((sb_rd(sb, SBDSP_STATUS) & 0x80))
172                         DELAY((i > 100)? 1000 : 10);
173                 else {
174                         sb_wr(sb, SBDSP_CMD, val);
175                         return 1;
176                 }
177         }
178 #if __FreeBSD_version > 500000
179         if (curthread->td_intr_nesting_level == 0)
180                 kprintf("sb_dspwr(0x%02x) timed out.\n", val);
181 #endif
182         return 0;
183 }
184
185 static int
186 sb_cmd(struct sb_info *sb, u_char val)
187 {
188 #if 0
189         kprintf("sb_cmd: %x\n", val);
190 #endif
191         return sb_dspwr(sb, val);
192 }
193
194 /*
195 static int
196 sb_cmd1(struct sb_info *sb, u_char cmd, int val)
197 {
198 #if 0
199         kprintf("sb_cmd1: %x, %x\n", cmd, val);
200 #endif
201         if (sb_dspwr(sb, cmd)) {
202                 return sb_dspwr(sb, val & 0xff);
203         } else return 0;
204 }
205 */
206
207 static int
208 sb_cmd2(struct sb_info *sb, u_char cmd, int val)
209 {
210         int r;
211
212 #if 0
213         kprintf("sb_cmd2: %x, %x\n", cmd, val);
214 #endif
215         sb_lockassert(sb);
216         r = 0;
217         if (sb_dspwr(sb, cmd)) {
218                 if (sb_dspwr(sb, val & 0xff)) {
219                         if (sb_dspwr(sb, (val >> 8) & 0xff)) {
220                                 r = 1;
221                         }
222                 }
223         }
224
225         return r;
226 }
227
228 /*
229  * in the SB, there is a set of indirect "mixer" registers with
230  * address at offset 4, data at offset 5
231  */
232 static void
233 sb_setmixer(struct sb_info *sb, u_int port, u_int value)
234 {
235         sb_lock(sb);
236         sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
237         DELAY(10);
238         sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff));
239         DELAY(10);
240         sb_unlock(sb);
241 }
242
243 static int
244 sb_getmixer(struct sb_info *sb, u_int port)
245 {
246         int val;
247
248         sb_lockassert(sb);
249         sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
250         DELAY(10);
251         val = sb_rd(sb, SB_MIX_DATA);
252         DELAY(10);
253
254         return val;
255 }
256
257 static u_int
258 sb_get_byte(struct sb_info *sb)
259 {
260         int i;
261
262         for (i = 1000; i > 0; i--) {
263                 if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80)
264                         return sb_rd(sb, DSP_READ);
265                 else
266                         DELAY(20);
267         }
268         return 0xffff;
269 }
270
271 static int
272 sb_reset_dsp(struct sb_info *sb)
273 {
274         u_char b;
275
276         sb_lockassert(sb);
277         sb_wr(sb, SBDSP_RST, 3);
278         DELAY(100);
279         sb_wr(sb, SBDSP_RST, 0);
280         b = sb_get_byte(sb);
281         if (b != 0xAA) {
282                 DEB(kprintf("sb_reset_dsp 0x%lx failed\n",
283                            rman_get_start(sb->io_base)));
284                 return ENXIO;   /* Sorry */
285         }
286         return 0;
287 }
288
289 /************************************************************/
290
291 struct sb16_mixent {
292         int reg;
293         int bits;
294         int ofs;
295         int stereo;
296 };
297
298 static const struct sb16_mixent sb16_mixtab[32] = {
299         [SOUND_MIXER_VOLUME]    = { 0x30, 5, 3, 1 },
300         [SOUND_MIXER_PCM]       = { 0x32, 5, 3, 1 },
301         [SOUND_MIXER_SYNTH]     = { 0x34, 5, 3, 1 },
302         [SOUND_MIXER_CD]        = { 0x36, 5, 3, 1 },
303         [SOUND_MIXER_LINE]      = { 0x38, 5, 3, 1 },
304         [SOUND_MIXER_MIC]       = { 0x3a, 5, 3, 0 },
305         [SOUND_MIXER_SPEAKER]   = { 0x3b, 5, 3, 0 },
306         [SOUND_MIXER_IGAIN]     = { 0x3f, 2, 6, 1 },
307         [SOUND_MIXER_OGAIN]     = { 0x41, 2, 6, 1 },
308         [SOUND_MIXER_TREBLE]    = { 0x44, 4, 4, 1 },
309         [SOUND_MIXER_BASS]      = { 0x46, 4, 4, 1 },
310         [SOUND_MIXER_LINE1]     = { 0x52, 5, 3, 1 }
311 };
312
313 static int
314 sb16mix_init(struct snd_mixer *m)
315 {
316         struct sb_info *sb = mix_getdevinfo(m);
317
318         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_SPEAKER |
319                        SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD |
320                        SOUND_MASK_IGAIN | SOUND_MASK_OGAIN | SOUND_MASK_LINE1 |
321                        SOUND_MASK_VOLUME | SOUND_MASK_BASS | SOUND_MASK_TREBLE);
322
323         mix_setrecdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_LINE |
324                           SOUND_MASK_LINE1 | SOUND_MASK_MIC | SOUND_MASK_CD);
325
326         sb_setmixer(sb, 0x3c, 0x1f); /* make all output active */
327
328         sb_setmixer(sb, 0x3d, 0); /* make all inputs-l off */
329         sb_setmixer(sb, 0x3e, 0); /* make all inputs-r off */
330
331         return 0;
332 }
333
334 static int
335 rel2abs_volume(int x, int max)
336 {
337         int temp;
338         
339         temp = ((x * max) + 50) / 100;
340         if (temp > max)
341                 temp = max;
342         else if (temp < 0)
343                 temp = 0;
344         return (temp);
345 }
346
347 static int
348 sb16mix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
349 {
350         struct sb_info *sb = mix_getdevinfo(m);
351         const struct sb16_mixent *e;
352         int max;
353
354         e = &sb16_mixtab[dev];
355         max = (1 << e->bits) - 1;
356
357         left = rel2abs_volume(left, max);
358         right = rel2abs_volume(right, max);
359
360         sb_setmixer(sb, e->reg, left << e->ofs);
361         if (e->stereo)
362                 sb_setmixer(sb, e->reg + 1, right << e->ofs);
363         else
364                 right = left;
365
366         left = (left * 100) / max;
367         right = (right * 100) / max;
368
369         return left | (right << 8);
370 }
371
372 static int
373 sb16mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
374 {
375         struct sb_info *sb = mix_getdevinfo(m);
376         u_char recdev_l, recdev_r;
377
378         recdev_l = 0;
379         recdev_r = 0;
380         if (src & SOUND_MASK_MIC) {
381                 recdev_l |= 0x01; /* mono mic */
382                 recdev_r |= 0x01;
383         }
384
385         if (src & SOUND_MASK_CD) {
386                 recdev_l |= 0x04; /* l cd */
387                 recdev_r |= 0x02; /* r cd */
388         }
389
390         if (src & SOUND_MASK_LINE) {
391                 recdev_l |= 0x10; /* l line */
392                 recdev_r |= 0x08; /* r line */
393         }
394
395         if (src & SOUND_MASK_SYNTH) {
396                 recdev_l |= 0x40; /* l midi */
397                 recdev_r |= 0x20; /* r midi */
398         }
399
400         sb_setmixer(sb, SB16_IMASK_L, recdev_l);
401         sb_setmixer(sb, SB16_IMASK_R, recdev_r);
402
403         /* Switch on/off FM tuner source */
404         if (src & SOUND_MASK_LINE1)
405                 sb_setmixer(sb, 0x4a, 0x0c);
406         else
407                 sb_setmixer(sb, 0x4a, 0x00);
408
409         /*
410          * since the same volume controls apply to the input and
411          * output sections, the best approach to have a consistent
412          * behaviour among cards would be to disable the output path
413          * on devices which are used to record.
414          * However, since users like to have feedback, we only disable
415          * the mic -- permanently.
416          */
417         sb_setmixer(sb, SB16_OMASK, 0x1f & ~1);
418
419         return src;
420 }
421
422 static kobj_method_t sb16mix_mixer_methods[] = {
423         KOBJMETHOD(mixer_init,          sb16mix_init),
424         KOBJMETHOD(mixer_set,           sb16mix_set),
425         KOBJMETHOD(mixer_setrecsrc,     sb16mix_setrecsrc),
426         { 0, 0 }
427 };
428 MIXER_DECLARE(sb16mix_mixer);
429
430 /************************************************************/
431
432 static void
433 sb16_release_resources(struct sb_info *sb, device_t dev)
434 {
435         if (sb->irq) {
436                 if (sb->ih)
437                         bus_teardown_intr(dev, sb->irq, sb->ih);
438                 bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
439                 sb->irq = 0;
440         }
441         if (sb->drq2) {
442                 if (sb->drq2 != sb->drq1) {
443                         isa_dma_release(rman_get_start(sb->drq2));
444                         bus_release_resource(dev, SYS_RES_DRQ, 1, sb->drq2);
445                 }
446                 sb->drq2 = 0;
447         }
448         if (sb->drq1) {
449                 isa_dma_release(rman_get_start(sb->drq1));
450                 bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq1);
451                 sb->drq1 = 0;
452         }
453         if (sb->io_base) {
454                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
455                 sb->io_base = 0;
456         }
457         if (sb->parent_dmat) {
458                 bus_dma_tag_destroy(sb->parent_dmat);
459                 sb->parent_dmat = 0;
460         }
461         kfree(sb, M_DEVBUF);
462 }
463
464 static int
465 sb16_alloc_resources(struct sb_info *sb, device_t dev)
466 {
467         int rid;
468
469         rid = 0;
470         if (!sb->io_base)
471                 sb->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
472                         &rid, RF_ACTIVE);
473
474         rid = 0;
475         if (!sb->irq)
476                 sb->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
477                         RF_ACTIVE);
478
479         rid = 0;
480         if (!sb->drq1)
481                 sb->drq1 = bus_alloc_resource_any(dev, SYS_RES_DRQ, &rid,
482                         RF_ACTIVE);
483
484         rid = 1;
485         if (!sb->drq2)
486                 sb->drq2 = bus_alloc_resource_any(dev, SYS_RES_DRQ, &rid,
487                         RF_ACTIVE);
488
489         if (sb->io_base && sb->drq1 && sb->irq) {
490                 isa_dma_acquire(rman_get_start(sb->drq1));
491                 isa_dmainit(rman_get_start(sb->drq1), sb->bufsize);
492
493                 if (sb->drq2) {
494                         isa_dma_acquire(rman_get_start(sb->drq2));
495                         isa_dmainit(rman_get_start(sb->drq2), sb->bufsize);
496                 } else {
497                         sb->drq2 = sb->drq1;
498                         pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
499                 }
500                 return 0;
501         } else return ENXIO;
502 }
503
504 /* sbc does locking for us */
505 static void
506 sb_intr(void *arg)
507 {
508         struct sb_info *sb = (struct sb_info *)arg;
509         int reason, c;
510
511         /*
512          * The Vibra16X has separate flags for 8 and 16 bit transfers, but
513          * I have no idea how to tell capture from playback interrupts...
514          */
515
516         reason = 0;
517         sb_lock(sb);
518         c = sb_getmixer(sb, IRQ_STAT);
519         if (c & 1)
520                 sb_rd(sb, DSP_DATA_AVAIL); /* 8-bit int ack */
521
522         if (c & 2)
523                 sb_rd(sb, DSP_DATA_AVL16); /* 16-bit int ack */
524         sb_unlock(sb);
525
526         /*
527          * this tells us if the source is 8-bit or 16-bit dma. We
528          * have to check the io channel to map it to read or write...
529          */
530
531         if (sb->bd_flags & BD_F_SB16X) {
532                 if (c & 1) { /* 8-bit format */
533                         if (sb->pch.fmt & AFMT_8BIT)
534                                 reason |= 1;
535                         if (sb->rch.fmt & AFMT_8BIT)
536                                 reason |= 2;
537                 }
538                 if (c & 2) { /* 16-bit format */
539                         if (sb->pch.fmt & AFMT_16BIT)
540                                 reason |= 1;
541                         if (sb->rch.fmt & AFMT_16BIT)
542                                 reason |= 2;
543                 }
544         } else {
545                 if (c & 1) { /* 8-bit dma */
546                         if (sb->pch.dch == 1)
547                                 reason |= 1;
548                         if (sb->rch.dch == 1)
549                                 reason |= 2;
550                 }
551                 if (c & 2) { /* 16-bit dma */
552                         if (sb->pch.dch == 2)
553                                 reason |= 1;
554                         if (sb->rch.dch == 2)
555                                 reason |= 2;
556                 }
557         }
558 #if 0
559         kprintf("sb_intr: reason=%d c=0x%x\n", reason, c);
560 #endif
561         if ((reason & 1) && (sb->pch.run))
562                 chn_intr(sb->pch.channel);
563
564         if ((reason & 2) && (sb->rch.run))
565                 chn_intr(sb->rch.channel);
566 }
567
568 static int
569 sb_setup(struct sb_info *sb)
570 {
571         struct sb_chinfo *ch;
572         u_int8_t v;
573         int l, pprio;
574
575         sb_lock(sb);
576         if (sb->bd_flags & BD_F_DMARUN)
577                 sndbuf_dma(sb->pch.buffer, PCMTRIG_STOP);
578         if (sb->bd_flags & BD_F_DMARUN2)
579                 sndbuf_dma(sb->rch.buffer, PCMTRIG_STOP);
580         sb->bd_flags &= ~(BD_F_DMARUN | BD_F_DMARUN2);
581
582         sb_reset_dsp(sb);
583
584         if (sb->bd_flags & BD_F_SB16X) {
585                 /* full-duplex doesn't work! */
586                 pprio = sb->pch.run? 1 : 0;
587                 sndbuf_dmasetup(sb->pch.buffer, pprio? sb->drq1 : sb->drq2);
588                 sb->pch.dch = pprio? 1 : 0;
589                 sndbuf_dmasetup(sb->rch.buffer, pprio? sb->drq2 : sb->drq1);
590                 sb->rch.dch = pprio? 2 : 1;
591         } else {
592                 if (sb->pch.run && sb->rch.run) {
593                         pprio = (sb->rch.fmt & AFMT_16BIT)? 0 : 1;
594                         sndbuf_dmasetup(sb->pch.buffer, pprio? sb->drq2 : sb->drq1);
595                         sb->pch.dch = pprio? 2 : 1;
596                         sndbuf_dmasetup(sb->rch.buffer, pprio? sb->drq1 : sb->drq2);
597                         sb->rch.dch = pprio? 1 : 2;
598                 } else {
599                         if (sb->pch.run) {
600                                 sndbuf_dmasetup(sb->pch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1);
601                                 sb->pch.dch = (sb->pch.fmt & AFMT_16BIT)? 2 : 1;
602                                 sndbuf_dmasetup(sb->rch.buffer, (sb->pch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2);
603                                 sb->rch.dch = (sb->pch.fmt & AFMT_16BIT)? 1 : 2;
604                         } else if (sb->rch.run) {
605                                 sndbuf_dmasetup(sb->pch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq1 : sb->drq2);
606                                 sb->pch.dch = (sb->rch.fmt & AFMT_16BIT)? 1 : 2;
607                                 sndbuf_dmasetup(sb->rch.buffer, (sb->rch.fmt & AFMT_16BIT)? sb->drq2 : sb->drq1);
608                                 sb->rch.dch = (sb->rch.fmt & AFMT_16BIT)? 2 : 1;
609                         }
610                 }
611         }
612
613         sndbuf_dmasetdir(sb->pch.buffer, PCMDIR_PLAY);
614         sndbuf_dmasetdir(sb->rch.buffer, PCMDIR_REC);
615
616         /*
617         kprintf("setup: [pch = %d, pfmt = %d, pgo = %d] [rch = %d, rfmt = %d, rgo = %d]\n",
618                sb->pch.dch, sb->pch.fmt, sb->pch.run, sb->rch.dch, sb->rch.fmt, sb->rch.run);
619         */
620
621         ch = &sb->pch;
622         if (ch->run) {
623                 l = ch->blksz;
624                 if (ch->fmt & AFMT_16BIT)
625                         l >>= 1;
626                 l--;
627
628                 /* play speed */
629                 RANGE(ch->spd, 5000, 45000);
630                 sb_cmd(sb, DSP_CMD_OUT16);
631                 sb_cmd(sb, ch->spd >> 8);
632                 sb_cmd(sb, ch->spd & 0xff);
633
634                 /* play format, length */
635                 v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_DAC;
636                 v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
637                 sb_cmd(sb, v);
638
639                 v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
640                 v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
641                 sb_cmd2(sb, v, l);
642                 sndbuf_dma(ch->buffer, PCMTRIG_START);
643                 sb->bd_flags |= BD_F_DMARUN;
644         }
645
646         ch = &sb->rch;
647         if (ch->run) {
648                 l = ch->blksz;
649                 if (ch->fmt & AFMT_16BIT)
650                         l >>= 1;
651                 l--;
652
653                 /* record speed */
654                 RANGE(ch->spd, 5000, 45000);
655                 sb_cmd(sb, DSP_CMD_IN16);
656                 sb_cmd(sb, ch->spd >> 8);
657                 sb_cmd(sb, ch->spd & 0xff);
658
659                 /* record format, length */
660                 v = DSP_F16_AUTO | DSP_F16_FIFO_ON | DSP_F16_ADC;
661                 v |= (ch->fmt & AFMT_16BIT)? DSP_DMA16 : DSP_DMA8;
662                 sb_cmd(sb, v);
663
664                 v = (ch->fmt & AFMT_STEREO)? DSP_F16_STEREO : 0;
665                 v |= (ch->fmt & AFMT_SIGNED)? DSP_F16_SIGNED : 0;
666                 sb_cmd2(sb, v, l);
667                 sndbuf_dma(ch->buffer, PCMTRIG_START);
668                 sb->bd_flags |= BD_F_DMARUN2;
669         }
670         sb_unlock(sb);
671
672         return 0;
673 }
674
675 /* channel interface */
676 static void *
677 sb16chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
678 {
679         struct sb_info *sb = devinfo;
680         struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
681
682         ch->parent = sb;
683         ch->channel = c;
684         ch->buffer = b;
685         ch->dir = dir;
686
687         if (sndbuf_alloc(ch->buffer, sb->parent_dmat, sb->bufsize) != 0)
688                 return NULL;
689
690         return ch;
691 }
692
693 static int
694 sb16chan_setformat(kobj_t obj, void *data, u_int32_t format)
695 {
696         struct sb_chinfo *ch = data;
697         struct sb_info *sb = ch->parent;
698
699         ch->fmt = format;
700         sb->prio = ch->dir;
701         sb->prio16 = (ch->fmt & AFMT_16BIT)? 1 : 0;
702
703         return 0;
704 }
705
706 static int
707 sb16chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
708 {
709         struct sb_chinfo *ch = data;
710
711         ch->spd = speed;
712         return speed;
713 }
714
715 static int
716 sb16chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
717 {
718         struct sb_chinfo *ch = data;
719
720         ch->blksz = blocksize;
721         return ch->blksz;
722 }
723
724 static int
725 sb16chan_trigger(kobj_t obj, void *data, int go)
726 {
727         struct sb_chinfo *ch = data;
728         struct sb_info *sb = ch->parent;
729
730         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
731                 return 0;
732
733         if (go == PCMTRIG_START)
734                 ch->run = 1;
735         else
736                 ch->run = 0;
737
738         sb_setup(sb);
739
740         return 0;
741 }
742
743 static int
744 sb16chan_getptr(kobj_t obj, void *data)
745 {
746         struct sb_chinfo *ch = data;
747
748         return sndbuf_dmaptr(ch->buffer);
749 }
750
751 static struct pcmchan_caps *
752 sb16chan_getcaps(kobj_t obj, void *data)
753 {
754         struct sb_chinfo *ch = data;
755         struct sb_info *sb = ch->parent;
756
757         if ((sb->prio == 0) || (sb->prio == ch->dir))
758                 return &sb16x_caps;
759         else
760                 return sb->prio16? &sb16_caps8 : &sb16_caps16;
761 }
762
763 static int
764 sb16chan_resetdone(kobj_t obj, void *data)
765 {
766         struct sb_chinfo *ch = data;
767         struct sb_info *sb = ch->parent;
768
769         sb->prio = 0;
770
771         return 0;
772 }
773
774 static kobj_method_t sb16chan_methods[] = {
775         KOBJMETHOD(channel_init,                sb16chan_init),
776         KOBJMETHOD(channel_resetdone,           sb16chan_resetdone),
777         KOBJMETHOD(channel_setformat,           sb16chan_setformat),
778         KOBJMETHOD(channel_setspeed,            sb16chan_setspeed),
779         KOBJMETHOD(channel_setblocksize,        sb16chan_setblocksize),
780         KOBJMETHOD(channel_trigger,             sb16chan_trigger),
781         KOBJMETHOD(channel_getptr,              sb16chan_getptr),
782         KOBJMETHOD(channel_getcaps,             sb16chan_getcaps),
783         { 0, 0 }
784 };
785 CHANNEL_DECLARE(sb16chan);
786
787 /************************************************************/
788
789 static int
790 sb16_probe(device_t dev)
791 {
792         char buf[64];
793         uintptr_t func, ver, r, f;
794
795         /* The parent device has already been probed. */
796         r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
797         if (func != SCF_PCM)
798                 return (ENXIO);
799
800         r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
801         f = (ver & 0xffff0000) >> 16;
802         ver &= 0x0000ffff;
803         if (f & BD_F_SB16) {
804                 ksnprintf(buf, sizeof buf, "SB16 DSP %d.%02d%s", (int) ver >> 8, (int) ver & 0xff,
805                          (f & BD_F_SB16X)? " (ViBRA16X)" : "");
806                 device_set_desc_copy(dev, buf);
807                 return 0;
808         } else
809                 return (ENXIO);
810 }
811
812 static int
813 sb16_attach(device_t dev)
814 {
815         struct sb_info *sb;
816         uintptr_t ver;
817         char status[SND_STATUSLEN], status2[SND_STATUSLEN];
818
819         sb = (struct sb_info *)kmalloc(sizeof *sb, M_DEVBUF, M_NOWAIT | M_ZERO);
820         if (!sb)
821                 return ENXIO;
822
823         sb->parent_dev = device_get_parent(dev);
824         BUS_READ_IVAR(sb->parent_dev, dev, 1, &ver);
825         sb->bd_id = ver & 0x0000ffff;
826         sb->bd_flags = (ver & 0xffff0000) >> 16;
827         sb->bufsize = pcm_getbuffersize(dev, 4096, SB16_BUFFSIZE, 65536);
828
829         if (sb16_alloc_resources(sb, dev))
830                 goto no;
831         sb_lock(sb);
832         if (sb_reset_dsp(sb)) {
833                 sb_unlock(sb);
834                 goto no;
835         }
836         sb_unlock(sb);
837         if (mixer_init(dev, &sb16mix_mixer_class, sb))
838                 goto no;
839         if (snd_setup_intr(dev, sb->irq, 0, sb_intr, sb, &sb->ih))
840                 goto no;
841
842         if (sb->bd_flags & BD_F_SB16X)
843                 pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
844
845         sb->prio = 0;
846
847         if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
848                         /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
849                         /*highaddr*/BUS_SPACE_MAXADDR,
850                         /*filter*/NULL, /*filterarg*/NULL,
851                         /*maxsize*/sb->bufsize, /*nsegments*/1,
852                         /*maxsegz*/0x3ffff, /*flags*/0,
853                         &sb->parent_dmat) != 0) {
854                 device_printf(dev, "unable to create dma tag\n");
855                 goto no;
856         }
857
858         if (!(pcm_getflags(dev) & SD_F_SIMPLEX))
859                 ksnprintf(status2, SND_STATUSLEN, ":%ld", rman_get_start(sb->drq2));
860         else
861                 status2[0] = '\0';
862
863         ksnprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld drq %ld%s bufsz %u %s",
864                 rman_get_start(sb->io_base), rman_get_start(sb->irq),
865                 rman_get_start(sb->drq1), status2, sb->bufsize,
866                 PCM_KLDSTRING(snd_sb16));
867
868         if (pcm_register(dev, sb, 1, 1))
869                 goto no;
870         pcm_addchan(dev, PCMDIR_REC, &sb16chan_class, sb);
871         pcm_addchan(dev, PCMDIR_PLAY, &sb16chan_class, sb);
872
873         pcm_setstatus(dev, status);
874
875         return 0;
876
877 no:
878         sb16_release_resources(sb, dev);
879         return ENXIO;
880 }
881
882 static int
883 sb16_detach(device_t dev)
884 {
885         int r;
886         struct sb_info *sb;
887
888         r = pcm_unregister(dev);
889         if (r)
890                 return r;
891
892         sb = pcm_getdevinfo(dev);
893         sb16_release_resources(sb, dev);
894         return 0;
895 }
896
897 static device_method_t sb16_methods[] = {
898         /* Device interface */
899         DEVMETHOD(device_probe,         sb16_probe),
900         DEVMETHOD(device_attach,        sb16_attach),
901         DEVMETHOD(device_detach,        sb16_detach),
902
903         { 0, 0 }
904 };
905
906 static driver_t sb16_driver = {
907         "pcm",
908         sb16_methods,
909         PCM_SOFTC_SIZE,
910 };
911
912 DRIVER_MODULE(snd_sb16, sbc, sb16_driver, pcm_devclass, 0, 0);
913 MODULE_DEPEND(snd_sb16, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
914 MODULE_DEPEND(snd_sb16, snd_sbc, 1, 1, 1);
915 MODULE_VERSION(snd_sb16, 1);