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