]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sound/isa/sb8.c
Import libxo-1.4.0:
[FreeBSD/FreeBSD.git] / sys / dev / sound / isa / sb8.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5  * Copyright (c) 1997,1998 Luigi Rizzo
6  *
7  * Derived from files in the Voxware 3.5 distribution,
8  * Copyright by Hannu Savolainen 1994, under the same copyright
9  * conditions.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include "opt_snd.h"
36 #endif
37
38 #include <dev/sound/pcm/sound.h>
39
40 #include  <dev/sound/isa/sb.h>
41 #include  <dev/sound/chip.h>
42
43 #include <isa/isavar.h>
44
45 #include "mixer_if.h"
46
47 SND_DECLARE_FILE("$FreeBSD$");
48
49 #define SB_DEFAULT_BUFSZ        4096
50
51 static u_int32_t sb_fmt[] = {
52         SND_FORMAT(AFMT_U8, 1, 0),
53         0
54 };
55 static struct pcmchan_caps sb200_playcaps = {4000, 23000, sb_fmt, 0};
56 static struct pcmchan_caps sb200_reccaps = {4000, 13000, sb_fmt, 0};
57 static struct pcmchan_caps sb201_playcaps = {4000, 44100, sb_fmt, 0};
58 static struct pcmchan_caps sb201_reccaps = {4000, 15000, sb_fmt, 0};
59
60 static u_int32_t sbpro_fmt[] = {
61         SND_FORMAT(AFMT_U8, 1, 0),
62         SND_FORMAT(AFMT_U8, 2, 0),
63         0
64 };
65 static struct pcmchan_caps sbpro_playcaps = {4000, 44100, sbpro_fmt, 0};
66 static struct pcmchan_caps sbpro_reccaps = {4000, 44100, sbpro_fmt, 0};
67
68 struct sb_info;
69
70 struct sb_chinfo {
71         struct sb_info *parent;
72         struct pcm_channel *channel;
73         struct snd_dbuf *buffer;
74         int dir;
75         u_int32_t fmt, spd, blksz;
76 };
77
78 struct sb_info {
79         device_t parent_dev;
80         struct resource *io_base;       /* I/O address for the board */
81         struct resource *irq;
82         struct resource *drq;
83         void *ih;
84         bus_dma_tag_t parent_dmat;
85
86         unsigned int bufsize;
87         int bd_id;
88         u_long bd_flags;       /* board-specific flags */
89         struct sb_chinfo pch, rch;
90 };
91
92 static int sb_rd(struct sb_info *sb, int reg);
93 static void sb_wr(struct sb_info *sb, int reg, u_int8_t val);
94 static int sb_dspready(struct sb_info *sb);
95 static int sb_cmd(struct sb_info *sb, u_char val);
96 static int sb_cmd1(struct sb_info *sb, u_char cmd, int val);
97 static int sb_cmd2(struct sb_info *sb, u_char cmd, int val);
98 static u_int sb_get_byte(struct sb_info *sb);
99 static void sb_setmixer(struct sb_info *sb, u_int port, u_int value);
100 static int sb_getmixer(struct sb_info *sb, u_int port);
101 static int sb_reset_dsp(struct sb_info *sb);
102
103 static void sb_intr(void *arg);
104 static int sb_speed(struct sb_chinfo *ch);
105 static int sb_start(struct sb_chinfo *ch);
106 static int sb_stop(struct sb_chinfo *ch);
107
108 /*
109  * Common code for the midi and pcm functions
110  *
111  * sb_cmd write a single byte to the CMD port.
112  * sb_cmd1 write a CMD + 1 byte arg
113  * sb_cmd2 write a CMD + 2 byte arg
114  * sb_get_byte returns a single byte from the DSP data port
115  */
116
117 static void
118 sb_lock(struct sb_info *sb) {
119
120         sbc_lock(device_get_softc(sb->parent_dev));
121 }
122
123 static void
124 sb_unlock(struct sb_info *sb) {
125
126         sbc_unlock(device_get_softc(sb->parent_dev));
127 }
128
129 static int
130 port_rd(struct resource *port, int off)
131 {
132         return bus_space_read_1(rman_get_bustag(port), rman_get_bushandle(port), off);
133 }
134
135 static void
136 port_wr(struct resource *port, int off, u_int8_t data)
137 {
138         bus_space_write_1(rman_get_bustag(port), rman_get_bushandle(port), off, data);
139 }
140
141 static int
142 sb_rd(struct sb_info *sb, int reg)
143 {
144         return port_rd(sb->io_base, reg);
145 }
146
147 static void
148 sb_wr(struct sb_info *sb, int reg, u_int8_t val)
149 {
150         port_wr(sb->io_base, reg, val);
151 }
152
153 static int
154 sb_dspready(struct sb_info *sb)
155 {
156         return ((sb_rd(sb, SBDSP_STATUS) & 0x80) == 0);
157 }
158
159 static int
160 sb_dspwr(struct sb_info *sb, u_char val)
161 {
162         int  i;
163
164         for (i = 0; i < 1000; i++) {
165                 if (sb_dspready(sb)) {
166                         sb_wr(sb, SBDSP_CMD, val);
167                         return 1;
168                 }
169                 if (i > 10) DELAY((i > 100)? 1000 : 10);
170         }
171         printf("sb_dspwr(0x%02x) timed out.\n", val);
172         return 0;
173 }
174
175 static int
176 sb_cmd(struct sb_info *sb, u_char val)
177 {
178 #if 0
179         printf("sb_cmd: %x\n", val);
180 #endif
181         return sb_dspwr(sb, val);
182 }
183
184 static int
185 sb_cmd1(struct sb_info *sb, u_char cmd, int val)
186 {
187 #if 0
188         printf("sb_cmd1: %x, %x\n", cmd, val);
189 #endif
190         if (sb_dspwr(sb, cmd)) {
191                 return sb_dspwr(sb, val & 0xff);
192         } else return 0;
193 }
194
195 static int
196 sb_cmd2(struct sb_info *sb, u_char cmd, int val)
197 {
198 #if 0
199         printf("sb_cmd2: %x, %x\n", cmd, val);
200 #endif
201         if (sb_dspwr(sb, cmd)) {
202                 return sb_dspwr(sb, val & 0xff) &&
203                        sb_dspwr(sb, (val >> 8) & 0xff);
204         } else return 0;
205 }
206
207 /*
208  * in the SB, there is a set of indirect "mixer" registers with
209  * address at offset 4, data at offset 5
210  *
211  * we don't need to interlock these, the mixer lock will suffice.
212  */
213 static void
214 sb_setmixer(struct sb_info *sb, u_int port, u_int value)
215 {
216         sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
217         DELAY(10);
218         sb_wr(sb, SB_MIX_DATA, (u_char) (value & 0xff));
219         DELAY(10);
220 }
221
222 static int
223 sb_getmixer(struct sb_info *sb, u_int port)
224 {
225         int val;
226
227         sb_wr(sb, SB_MIX_ADDR, (u_char) (port & 0xff)); /* Select register */
228         DELAY(10);
229         val = sb_rd(sb, SB_MIX_DATA);
230         DELAY(10);
231
232         return val;
233 }
234
235 static u_int
236 sb_get_byte(struct sb_info *sb)
237 {
238         int i;
239
240         for (i = 1000; i > 0; i--) {
241                 if (sb_rd(sb, DSP_DATA_AVAIL) & 0x80)
242                         return sb_rd(sb, DSP_READ);
243                 else
244                         DELAY(20);
245         }
246         return 0xffff;
247 }
248
249 static int
250 sb_reset_dsp(struct sb_info *sb)
251 {
252         sb_wr(sb, SBDSP_RST, 3);
253         DELAY(100);
254         sb_wr(sb, SBDSP_RST, 0);
255         if (sb_get_byte(sb) != 0xAA) {
256                 DEB(printf("sb_reset_dsp 0x%lx failed\n",
257                            rman_get_start(sb->io_base)));
258                 return ENXIO;   /* Sorry */
259         }
260         return 0;
261 }
262
263 static void
264 sb_release_resources(struct sb_info *sb, device_t dev)
265 {
266         if (sb->irq) {
267                 if (sb->ih)
268                         bus_teardown_intr(dev, sb->irq, sb->ih);
269                 bus_release_resource(dev, SYS_RES_IRQ, 0, sb->irq);
270                 sb->irq = NULL;
271         }
272         if (sb->drq) {
273                 isa_dma_release(rman_get_start(sb->drq));
274                 bus_release_resource(dev, SYS_RES_DRQ, 0, sb->drq);
275                 sb->drq = NULL;
276         }
277         if (sb->io_base) {
278                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sb->io_base);
279                 sb->io_base = NULL;
280         }
281         if (sb->parent_dmat) {
282                 bus_dma_tag_destroy(sb->parent_dmat);
283                 sb->parent_dmat = 0;
284         }
285         free(sb, M_DEVBUF);
286 }
287
288 static int
289 sb_alloc_resources(struct sb_info *sb, device_t dev)
290 {
291         int rid;
292
293         rid = 0;
294         if (!sb->io_base)
295                 sb->io_base = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
296                         &rid, RF_ACTIVE);
297         rid = 0;
298         if (!sb->irq)
299                 sb->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
300                         &rid, RF_ACTIVE);
301         rid = 0;
302         if (!sb->drq)
303                 sb->drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
304                         &rid, RF_ACTIVE);
305
306         if (sb->io_base && sb->drq && sb->irq) {
307                 isa_dma_acquire(rman_get_start(sb->drq));
308                 isa_dmainit(rman_get_start(sb->drq), sb->bufsize);
309
310                 return 0;
311         } else return ENXIO;
312 }
313
314 /************************************************************/
315
316 static int
317 sbpromix_init(struct snd_mixer *m)
318 {
319         struct sb_info *sb = mix_getdevinfo(m);
320
321         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_LINE |
322                        SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_VOLUME);
323
324         mix_setrecdevs(m, SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD);
325
326         sb_setmixer(sb, 0, 1); /* reset mixer */
327
328         return 0;
329 }
330
331 static int
332 sbpromix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
333 {
334         struct sb_info *sb = mix_getdevinfo(m);
335         int reg, max;
336         u_char val;
337
338         max = 7;
339         switch (dev) {
340         case SOUND_MIXER_PCM:
341                 reg = 0x04;
342                 break;
343
344         case SOUND_MIXER_MIC:
345                 reg = 0x0a;
346                 max = 3;
347                 break;
348
349         case SOUND_MIXER_VOLUME:
350                 reg = 0x22;
351                 break;
352
353         case SOUND_MIXER_SYNTH:
354                 reg = 0x26;
355                 break;
356
357         case SOUND_MIXER_CD:
358                 reg = 0x28;
359                 break;
360
361         case SOUND_MIXER_LINE:
362                 reg = 0x2e;
363                 break;
364
365         default:
366                 return -1;
367         }
368
369         left = (left * max) / 100;
370         right = (dev == SOUND_MIXER_MIC)? left : ((right * max) / 100);
371
372         val = (dev == SOUND_MIXER_MIC)? (left << 1) : (left << 5 | right << 1);
373         sb_setmixer(sb, reg, val);
374
375         left = (left * 100) / max;
376         right = (right * 100) / max;
377
378         return left | (right << 8);
379 }
380
381 static u_int32_t
382 sbpromix_setrecsrc(struct snd_mixer *m, u_int32_t src)
383 {
384         struct sb_info *sb = mix_getdevinfo(m);
385         u_char recdev;
386
387         if      (src == SOUND_MASK_LINE)
388                 recdev = 0x06;
389         else if (src == SOUND_MASK_CD)
390                 recdev = 0x02;
391         else { /* default: mic */
392                 src = SOUND_MASK_MIC;
393                 recdev = 0;
394         }
395         sb_setmixer(sb, RECORD_SRC, recdev | (sb_getmixer(sb, RECORD_SRC) & ~0x07));
396
397         return src;
398 }
399
400 static kobj_method_t sbpromix_mixer_methods[] = {
401         KOBJMETHOD(mixer_init,          sbpromix_init),
402         KOBJMETHOD(mixer_set,           sbpromix_set),
403         KOBJMETHOD(mixer_setrecsrc,     sbpromix_setrecsrc),
404         KOBJMETHOD_END
405 };
406 MIXER_DECLARE(sbpromix_mixer);
407
408 /************************************************************/
409
410 static int
411 sbmix_init(struct snd_mixer *m)
412 {
413         struct sb_info *sb = mix_getdevinfo(m);
414
415         mix_setdevs(m, SOUND_MASK_SYNTH | SOUND_MASK_PCM | SOUND_MASK_CD | SOUND_MASK_VOLUME);
416
417         mix_setrecdevs(m, 0);
418
419         sb_setmixer(sb, 0, 1); /* reset mixer */
420
421         return 0;
422 }
423
424 static int
425 sbmix_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
426 {
427         struct sb_info *sb = mix_getdevinfo(m);
428         int reg, max;
429
430         max = 7;
431         switch (dev) {
432         case SOUND_MIXER_VOLUME:
433                 reg = 0x2;
434                 break;
435
436         case SOUND_MIXER_SYNTH:
437                 reg = 0x6;
438                 break;
439
440         case SOUND_MIXER_CD:
441                 reg = 0x8;
442                 break;
443
444         case SOUND_MIXER_PCM:
445                 reg = 0x0a;
446                 max = 3;
447                 break;
448
449         default:
450                 return -1;
451         }
452
453         left = (left * max) / 100;
454
455         sb_setmixer(sb, reg, left << 1);
456
457         left = (left * 100) / max;
458
459         return left | (left << 8);
460 }
461
462 static u_int32_t
463 sbmix_setrecsrc(struct snd_mixer *m, u_int32_t src)
464 {
465         return 0;
466 }
467
468 static kobj_method_t sbmix_mixer_methods[] = {
469         KOBJMETHOD(mixer_init,          sbmix_init),
470         KOBJMETHOD(mixer_set,           sbmix_set),
471         KOBJMETHOD(mixer_setrecsrc,     sbmix_setrecsrc),
472         KOBJMETHOD_END
473 };
474 MIXER_DECLARE(sbmix_mixer);
475
476 /************************************************************/
477
478 static void
479 sb_intr(void *arg)
480 {
481         struct sb_info *sb = (struct sb_info *)arg;
482
483         sb_lock(sb);
484         if (sndbuf_runsz(sb->pch.buffer) > 0) {
485                 sb_unlock(sb);
486                 chn_intr(sb->pch.channel);
487                 sb_lock(sb);
488         }
489
490         if (sndbuf_runsz(sb->rch.buffer) > 0) {
491                 sb_unlock(sb);
492                 chn_intr(sb->rch.channel);
493                 sb_lock(sb);
494         }
495
496         sb_rd(sb, DSP_DATA_AVAIL); /* int ack */
497         sb_unlock(sb);
498 }
499
500 static int
501 sb_speed(struct sb_chinfo *ch)
502 {
503         struct sb_info *sb = ch->parent;
504         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
505         int stereo = (AFMT_CHANNEL(ch->fmt) > 1)? 1 : 0;
506         int speed, tmp, thresh, max;
507         u_char tconst;
508
509         if (sb->bd_id >= 0x300) {
510                 thresh = stereo? 11025 : 23000;
511                 max = stereo? 22050 : 44100;
512         } else if (sb->bd_id > 0x200) {
513                 thresh = play? 23000 : 13000;
514                 max = play? 44100 : 15000;
515         } else {
516                 thresh = 999999;
517                 max = play? 23000 : 13000;
518         }
519
520         speed = ch->spd;
521         if (speed > max)
522                 speed = max;
523
524         sb_lock(sb);
525         sb->bd_flags &= ~BD_F_HISPEED;
526         if (speed > thresh)
527                 sb->bd_flags |= BD_F_HISPEED;
528
529         tmp = 65536 - (256000000 / (speed << stereo));
530         tconst = tmp >> 8;
531
532         sb_cmd1(sb, 0x40, tconst); /* set time constant */
533
534         speed = (256000000 / (65536 - tmp)) >> stereo;
535
536         ch->spd = speed;
537         sb_unlock(sb);
538         return speed;
539 }
540
541 static int
542 sb_start(struct sb_chinfo *ch)
543 {
544         struct sb_info *sb = ch->parent;
545         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
546         int stereo = (AFMT_CHANNEL(ch->fmt) > 1)? 1 : 0;
547         int l = ch->blksz;
548         u_char i;
549
550         l--;
551
552         sb_lock(sb);
553         if (play)
554                 sb_cmd(sb, DSP_CMD_SPKON);
555
556         if (sb->bd_flags & BD_F_HISPEED)
557                 i = play? 0x90 : 0x98;
558         else
559                 i = play? 0x1c : 0x2c;
560
561         sb_setmixer(sb, 0x0e, stereo? 2 : 0);
562         sb_cmd2(sb, 0x48, l);
563         sb_cmd(sb, i);
564
565         sb->bd_flags |= BD_F_DMARUN;
566         sb_unlock(sb);
567         return 0;
568 }
569
570 static int
571 sb_stop(struct sb_chinfo *ch)
572 {
573         struct sb_info *sb = ch->parent;
574         int play = (ch->dir == PCMDIR_PLAY)? 1 : 0;
575
576         sb_lock(sb);
577         if (sb->bd_flags & BD_F_HISPEED)
578                 sb_reset_dsp(sb);
579         else {
580 #if 0
581                 /*
582                  * NOTE: DSP_CMD_DMAEXIT_8 does not work with old
583                  * soundblaster.
584                  */
585                 sb_cmd(sb, DSP_CMD_DMAEXIT_8);
586 #endif
587                 sb_reset_dsp(sb);
588         }
589
590         if (play)
591                 sb_cmd(sb, DSP_CMD_SPKOFF); /* speaker off */
592         sb_unlock(sb);
593         sb->bd_flags &= ~BD_F_DMARUN;
594         return 0;
595 }
596
597 /* channel interface */
598 static void *
599 sbchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
600 {
601         struct sb_info *sb = devinfo;
602         struct sb_chinfo *ch = (dir == PCMDIR_PLAY)? &sb->pch : &sb->rch;
603
604         ch->parent = sb;
605         ch->channel = c;
606         ch->dir = dir;
607         ch->buffer = b;
608         if (sndbuf_alloc(ch->buffer, sb->parent_dmat, 0, sb->bufsize) != 0)
609                 return NULL;
610         sndbuf_dmasetup(ch->buffer, sb->drq);
611         return ch;
612 }
613
614 static int
615 sbchan_setformat(kobj_t obj, void *data, u_int32_t format)
616 {
617         struct sb_chinfo *ch = data;
618
619         ch->fmt = format;
620         return 0;
621 }
622
623 static u_int32_t
624 sbchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
625 {
626         struct sb_chinfo *ch = data;
627
628         ch->spd = speed;
629         return sb_speed(ch);
630 }
631
632 static u_int32_t
633 sbchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
634 {
635         struct sb_chinfo *ch = data;
636
637         ch->blksz = blocksize;
638         return ch->blksz;
639 }
640
641 static int
642 sbchan_trigger(kobj_t obj, void *data, int go)
643 {
644         struct sb_chinfo *ch = data;
645
646         if (!PCMTRIG_COMMON(go))
647                 return 0;
648
649         sndbuf_dma(ch->buffer, go);
650         if (go == PCMTRIG_START)
651                 sb_start(ch);
652         else
653                 sb_stop(ch);
654         return 0;
655 }
656
657 static u_int32_t
658 sbchan_getptr(kobj_t obj, void *data)
659 {
660         struct sb_chinfo *ch = data;
661
662         return sndbuf_dmaptr(ch->buffer);
663 }
664
665 static struct pcmchan_caps *
666 sbchan_getcaps(kobj_t obj, void *data)
667 {
668         struct sb_chinfo *ch = data;
669         int p = (ch->dir == PCMDIR_PLAY)? 1 : 0;
670
671         if (ch->parent->bd_id == 0x200)
672                 return p? &sb200_playcaps : &sb200_reccaps;
673         if (ch->parent->bd_id < 0x300)
674                 return p? &sb201_playcaps : &sb201_reccaps;
675         return p? &sbpro_playcaps : &sbpro_reccaps;
676 }
677
678 static kobj_method_t sbchan_methods[] = {
679         KOBJMETHOD(channel_init,                sbchan_init),
680         KOBJMETHOD(channel_setformat,           sbchan_setformat),
681         KOBJMETHOD(channel_setspeed,            sbchan_setspeed),
682         KOBJMETHOD(channel_setblocksize,        sbchan_setblocksize),
683         KOBJMETHOD(channel_trigger,             sbchan_trigger),
684         KOBJMETHOD(channel_getptr,              sbchan_getptr),
685         KOBJMETHOD(channel_getcaps,             sbchan_getcaps),
686         KOBJMETHOD_END
687 };
688 CHANNEL_DECLARE(sbchan);
689
690 /************************************************************/
691
692 static int
693 sb_probe(device_t dev)
694 {
695         char buf[64];
696         uintptr_t func, ver, r, f;
697
698         /* The parent device has already been probed. */
699         r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
700         if (func != SCF_PCM)
701                 return (ENXIO);
702
703         r = BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
704         f = (ver & 0xffff0000) >> 16;
705         ver &= 0x0000ffff;
706         if ((f & BD_F_ESS) || (ver >= 0x400))
707                 return (ENXIO);
708
709         snprintf(buf, sizeof buf, "SB DSP %d.%02d", (int) ver >> 8, (int) ver & 0xff);
710
711         device_set_desc_copy(dev, buf);
712
713         return 0;
714 }
715
716 static int
717 sb_attach(device_t dev)
718 {
719         struct sb_info *sb;
720         char status[SND_STATUSLEN];
721         uintptr_t ver;
722
723         sb = malloc(sizeof(*sb), M_DEVBUF, M_WAITOK | M_ZERO);
724         sb->parent_dev = device_get_parent(dev);
725         BUS_READ_IVAR(device_get_parent(dev), dev, 1, &ver);
726         sb->bd_id = ver & 0x0000ffff;
727         sb->bd_flags = (ver & 0xffff0000) >> 16;
728         sb->bufsize = pcm_getbuffersize(dev, 4096, SB_DEFAULT_BUFSZ, 65536);
729
730         if (sb_alloc_resources(sb, dev))
731                 goto no;
732         if (sb_reset_dsp(sb))
733                 goto no;
734         if (mixer_init(dev, (sb->bd_id < 0x300)? &sbmix_mixer_class : &sbpromix_mixer_class, sb))
735                 goto no;
736         if (snd_setup_intr(dev, sb->irq, 0, sb_intr, sb, &sb->ih))
737                 goto no;
738
739         pcm_setflags(dev, pcm_getflags(dev) | SD_F_SIMPLEX);
740
741         if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
742                         /*boundary*/0,
743                         /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
744                         /*highaddr*/BUS_SPACE_MAXADDR,
745                         /*filter*/NULL, /*filterarg*/NULL,
746                         /*maxsize*/sb->bufsize, /*nsegments*/1,
747                         /*maxsegz*/0x3ffff, /*flags*/0,
748                         /*lockfunc*/busdma_lock_mutex, /*lockarg*/&Giant,
749                         &sb->parent_dmat) != 0) {
750                 device_printf(dev, "unable to create dma tag\n");
751                 goto no;
752         }
753
754         snprintf(status, SND_STATUSLEN, "at io 0x%jx irq %jd drq %jd bufsz %u %s",
755                 rman_get_start(sb->io_base), rman_get_start(sb->irq),
756                 rman_get_start(sb->drq), sb->bufsize, PCM_KLDSTRING(snd_sb8));
757
758         if (pcm_register(dev, sb, 1, 1))
759                 goto no;
760         pcm_addchan(dev, PCMDIR_REC, &sbchan_class, sb);
761         pcm_addchan(dev, PCMDIR_PLAY, &sbchan_class, sb);
762
763         pcm_setstatus(dev, status);
764
765         return 0;
766
767 no:
768         sb_release_resources(sb, dev);
769         return ENXIO;
770 }
771
772 static int
773 sb_detach(device_t dev)
774 {
775         int r;
776         struct sb_info *sb;
777
778         r = pcm_unregister(dev);
779         if (r)
780                 return r;
781
782         sb = pcm_getdevinfo(dev);
783         sb_release_resources(sb, dev);
784         return 0;
785 }
786
787 static device_method_t sb_methods[] = {
788         /* Device interface */
789         DEVMETHOD(device_probe,         sb_probe),
790         DEVMETHOD(device_attach,        sb_attach),
791         DEVMETHOD(device_detach,        sb_detach),
792
793         { 0, 0 }
794 };
795
796 static driver_t sb_driver = {
797         "pcm",
798         sb_methods,
799         PCM_SOFTC_SIZE,
800 };
801
802 DRIVER_MODULE(snd_sb8, sbc, sb_driver, pcm_devclass, 0, 0);
803 MODULE_DEPEND(snd_sb8, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
804 MODULE_DEPEND(snd_sb8, snd_sbc, 1, 1, 1);
805 MODULE_VERSION(snd_sb8, 1);
806
807
808
809